diff --git a/packages/geoprocessing/src/clients/tasks.ts b/packages/geoprocessing/src/clients/tasks.ts index e7e8389eb7..50d60b9c99 100644 --- a/packages/geoprocessing/src/clients/tasks.ts +++ b/packages/geoprocessing/src/clients/tasks.ts @@ -1,34 +1,5 @@ import { GeoprocessingTask } from "../aws/tasks.js"; -import { - GeoprocessingRequest, - GeoprocessingRequestParams, - SketchProperties, -} from "../types/index.js"; -import md5 from "spark-md5"; -import canonicalize from "../util/canonicalize.js"; - -/** - * Generates a cache key for a geoprocessing request, given sketch properties and optional extra parameters (must be JSON compatible object) - * Extra parameters are canonicalized and hashed using md5 to ensure cache key is consistent. Canonicalization ensures object keys are consistent - * but not arrays. If you use arrays as extraParam values, make sure the order stays the same and sort first if needed to generate a consistent cache key. - */ -export const genTaskCacheKey = ( - /** Properties of sketch to generate cache key for */ - props: SketchProperties, - /** Extra parameters to include in cache key */ - extraParams: GeoprocessingRequestParams = {} -) => { - let cacheKey = `${props.id}-${props.updatedAt}`; - if (Object.keys(extraParams).length > 0) { - // Ensure JSON object has consistent stringification - const canon = canonicalize(extraParams); - // Hash the stringified JSON object - const hash = md5.hash(JSON.stringify(canon)); - // Append the hash to the cache key to keep the key semi-human-readable - cacheKey = `${cacheKey}-${hash}`; - } - return cacheKey; -}; +import { GeoprocessingRequest } from "../types/index.js"; /** * Runs task by sending GET request to url with payload and optional flags diff --git a/packages/geoprocessing/src/hooks/useFunction.ts b/packages/geoprocessing/src/hooks/useFunction.ts index 906788428d..8220f81f9f 100644 --- a/packages/geoprocessing/src/hooks/useFunction.ts +++ b/packages/geoprocessing/src/hooks/useFunction.ts @@ -7,7 +7,8 @@ import { GeoprocessingProject, GeoprocessingRequestParams, } from "../types/index.js"; -import { runTask, finishTask, genTaskCacheKey } from "../clients/tasks.js"; +import { runTask, finishTask } from "../clients/tasks.js"; +import { genTaskCacheKey } from "../util/genTaskCacheKey.js"; import cloneDeep from "lodash/cloneDeep.js"; interface PendingRequest { diff --git a/packages/geoprocessing/src/util/genTaskCacheKey.ts b/packages/geoprocessing/src/util/genTaskCacheKey.ts new file mode 100644 index 0000000000..2703178748 --- /dev/null +++ b/packages/geoprocessing/src/util/genTaskCacheKey.ts @@ -0,0 +1,26 @@ +import { SketchProperties } from "../types/index.js"; +import md5 from "spark-md5"; +import canonicalize from "../util/canonicalize.js"; + +/** + * Generates a cache key for a geoprocessing request, given sketch properties and optional extra parameters (must be JSON compatible object) + * Extra parameters are canonicalized and hashed using md5 to ensure cache key is consistent. Canonicalization ensures object keys are consistent + * but not arrays. If you use arrays as extraParam values, make sure the order stays the same and sort first if needed to generate a consistent cache key. + */ +export const genTaskCacheKey = ( + /** Properties of sketch to generate cache key for */ + props: SketchProperties, + /** Extra parameters to include in cache key */ + extraParams: Record = {} +) => { + let cacheKey = `${props.id}-${props.updatedAt}`; + if (Object.keys(extraParams).length > 0) { + // Ensure JSON object has consistent stringification + const canon = canonicalize(extraParams); + // Hash the stringified JSON object + const hash = md5.hash(JSON.stringify(canon)); + // Append the hash to the cache key to keep the key semi-human-readable + cacheKey = `${cacheKey}-${hash}`; + } + return cacheKey; +}; diff --git a/packages/geoprocessing/src/util/index.ts b/packages/geoprocessing/src/util/index.ts index ebaafd99dd..46e3cd8349 100644 --- a/packages/geoprocessing/src/util/index.ts +++ b/packages/geoprocessing/src/util/index.ts @@ -1 +1,2 @@ export * from "./genRandomPolygons.js"; +export * from "./genTaskCacheKey.js";