diff --git a/package-lock.json b/package-lock.json index 1de8d15c837eecad53cb526a8226c6dc1f11dee0..0a56cf0d14db0515e4409f96db73d5ff16485d91 100644 --- a/package-lock.json +++ b/package-lock.json @@ -48,7 +48,8 @@ "tailwindcss": "^2.0.3", "tslib": "^2.0.0", "typescript": "^4.0.0", - "uuid": "^8.3.2" + "uuid": "^8.3.2", + "xxhash-addon": "^1.4.0" } }, "node_modules/@auditors/core": { @@ -6923,6 +6924,16 @@ "node": ">=0.4" } }, + "node_modules/xxhash-addon": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/xxhash-addon/-/xxhash-addon-1.4.0.tgz", + "integrity": "sha512-n3Ml0Vgvy7jMYJBlQIoFLjYxXNZQ5CbzW8E2Ynq2QCUpWMqCouooW7j02+7Oud5FijBuSrjQNuN/fCiz1SHN+w==", + "dev": true, + "hasInstallScript": true, + "engines": { + "node": ">=8.6.0 <9.0.0 || >=10.0.0" + } + }, "node_modules/yallist": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/yallist/-/yallist-4.0.0.tgz", @@ -11936,6 +11947,12 @@ "integrity": "sha512-LKYU1iAXJXUgAXn9URjiu+MWhyUXHsvfp7mcuYm9dSUKK0/CjtrUwFAxD82/mCWbtLsGjFIad0wIsod4zrTAEQ==", "dev": true }, + "xxhash-addon": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/xxhash-addon/-/xxhash-addon-1.4.0.tgz", + "integrity": "sha512-n3Ml0Vgvy7jMYJBlQIoFLjYxXNZQ5CbzW8E2Ynq2QCUpWMqCouooW7j02+7Oud5FijBuSrjQNuN/fCiz1SHN+w==", + "dev": true + }, "yallist": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/yallist/-/yallist-4.0.0.tgz", diff --git a/package.json b/package.json index bdf3a071f98e0d9bf617469a20a3f2c6de380c81..a83b823bdef75f5cb8dd85bb1cc83c60b84ad309 100644 --- a/package.json +++ b/package.json @@ -52,7 +52,8 @@ "tailwindcss": "^2.0.3", "tslib": "^2.0.0", "typescript": "^4.0.0", - "uuid": "^8.3.2" + "uuid": "^8.3.2", + "xxhash-addon": "^1.4.0" }, "type": "module" } diff --git a/src/routes/simulations/[simulation].json.ts b/src/routes/simulations/[simulation].json.ts index 702fb5fb62c2703581d229f99486ad6c7fae52f9..d9f5f76864dfe160a06d19372ba00334e47087bd 100644 --- a/src/routes/simulations/[simulation].json.ts +++ b/src/routes/simulations/[simulation].json.ts @@ -7,17 +7,10 @@ import { } from "@auditors/core" import type { JsonValue } from "@openfisca/ast" import type { RequestHandler } from "@sveltejs/kit" -import { createHash } from "crypto" import fs from "fs-extra" import path from "path" -import sanitizeFilename from "sanitize-filename" -import { walkDecompositionsCoreName } from "$lib/decompositions" -import type { Reform } from "$lib/reforms" import config from "$lib/server/config" -import { decompositionCoreByName, waterfalls } from "$lib/server/decompositions" -import { iterVariableInputVariables } from "$lib/server/variables" -import type { Situation } from "$lib/situations" const { simulationsDir } = config @@ -41,7 +34,7 @@ function auditParams(audit: Audit, dataUnknown: unknown): [unknown, unknown] { remainingKeys, auditTrimString, auditTest( - (value) => /^[0-9a-f]{64}$/.test(value), + (value) => /^[0-9a-f]{16}$/.test(value), "Invalid simulation token", ), auditRequire, diff --git a/src/routes/simulations/index.json.ts b/src/routes/simulations/index.json.ts index 854f28f655cc4a1ce68e4fe4e9b42a0f68f65e62..73b69d24e6c1aab48ef99ba48118d8edff380f0a 100644 --- a/src/routes/simulations/index.json.ts +++ b/src/routes/simulations/index.json.ts @@ -2,17 +2,11 @@ import type { Audit } from "@auditors/core" import { auditCleanArray, auditRequire, cleanAudit } from "@auditors/core" import type { JsonValue } from "@openfisca/ast" import type { RequestHandler } from "@sveltejs/kit" -import { createHash } from "crypto" import fs from "fs-extra" import path from "path" -import sanitizeFilename from "sanitize-filename" +import { XXHash3 } from "xxhash-addon" -import { walkDecompositionsCoreName } from "$lib/decompositions" -import type { Reform } from "$lib/reforms" import config from "$lib/server/config" -import { decompositionCoreByName, waterfalls } from "$lib/server/decompositions" -import { iterVariableInputVariables } from "$lib/server/variables" -import type { Situation } from "$lib/situations" const { simulationsDir } = config @@ -78,9 +72,9 @@ export const post: RequestHandler = async ({ } } const bodyJson = JSON.stringify(body, null, 2) - const hash = createHash("sha256") - hash.update(bodyJson) - const digest = hash.digest("hex") + const hasher = new XXHash3() + hasher.update(Buffer.from(bodyJson)) + const digest = hasher.digest().toString("hex") const simulationDir = path.join(simulationsDir, digest.substring(0, 2)) const simulationFilePath = path.join(simulationDir, `${digest}.json`)