/** * Differential check of the worker's crypto/url/util shims against Node. */ import { expect, test } from 'vitest' import { createHash as nodeCreateHash } from 'node:crypto' import { fileURLToPath as nodeFileURLToPath, pathToFileURL as nodePathToFileURL } from 'node:url' import { isDeepStrictEqual as nodeIsDeepStrictEqual, promisify as nodePromisify } from 'node:util' import * as shimCrypto from '@deepseek-ai/dsh-experimental-webworker-runtime/src/node/builtin_modules/implemented/crypto.ts' import * as shimUrl from '@deepseek-ai/dsh-experimental-webworker-runtime/src/node/builtin_modules/implemented/url.ts' import * as shimUtil from '@deepseek-ai/dsh-experimental-webworker-runtime/src/node/builtin_modules/implemented/util.ts' const compare = (label: string, actual: unknown, expected: unknown): void => { const [shimmed, node] = [JSON.stringify(actual), JSON.stringify(expected)] test(label, () => { expect(shimmed).toBe(node) }) } const INPUTS = ['', 'a', 'hello world', '中文字符串', 'ÿ', 'x'.repeat(100_000)] for (const algorithm of ['sha1', 'sha256', 'sha512']) { for (const input of INPUTS) { compare( `${algorithm}(${input.slice(0, 12)}… len ${String(input.length)})`, shimCrypto.createHash(algorithm).update(input).digest('hex'), nodeCreateHash(algorithm).update(input).digest('hex'), ) } // Chained updates must equal the concatenated input. compare( `${algorithm} chained update`, shimCrypto.createHash(algorithm).update('ab').update('cd').digest('hex'), nodeCreateHash(algorithm).update('abcd').digest('hex'), ) compare( `${algorithm} base64`, shimCrypto.createHash(algorithm).update('abc').digest('base64'), nodeCreateHash(algorithm).update('abc').digest('base64'), ) compare( `${algorithm} bytes`, [...shimCrypto.createHash(algorithm).update(new Uint8Array([1, 2, 3])).digest()], [...nodeCreateHash(algorithm).update(new Uint8Array([1, 2, 3])).digest()], ) } const PATHS = [ '/dsh/config/cordis.yml', '/a b/c', '/中文/x.md', '/a%b', '/dsh/node_modules/@scope/pkg/lib/index.js', ] for (const path of PATHS) { compare(`pathToFileURL(${path})`, shimUrl.pathToFileURL(path).href, nodePathToFileURL(path).href) compare( `fileURLToPath(pathToFileURL(${path}))`, shimUrl.fileURLToPath(shimUrl.pathToFileURL(path)), nodeFileURLToPath(nodePathToFileURL(path)), ) } const PAIRS: [unknown, unknown][] = [ [1, 1], [1, '1'], [{ a: 1 }, { a: 1 }], [{ a: 1 }, { a: 2 }], [{ a: 1 }, { a: 1, b: undefined }], [[1, 2], [1, 2]], [[1, 2], [2, 1]], [null, undefined], [{ a: { b: [1, { c: 2 }] } }, { a: { b: [1, { c: 2 }] } }], ] for (const [left, right] of PAIRS) { compare( `isDeepStrictEqual(${JSON.stringify(left)}, ${JSON.stringify(right)})`, shimUtil.isDeepStrictEqual(left, right), nodeIsDeepStrictEqual(left, right), ) } const callbackStyle = (value: number, callback: (error: unknown, result?: number) => void): void => { if (value < 0) callback(new Error('negative')) else callback(null, value * 2) } const shimPromised = shimUtil.promisify(callbackStyle) const nodePromised = nodePromisify(callbackStyle) compare('promisify success', await shimPromised(21), await nodePromised(21)) const shimError = await shimPromised(-1).then(() => undefined, (error: unknown) => (error as Error).message) const nodeError = await nodePromised(-1).then(() => undefined, (error: unknown) => (error as Error).message) compare('promisify rejection', shimError, nodeError)