UNPKG

@cloudflare/vitest-pool-workers

Version:

Workers Vitest integration for writing Vitest unit and integration tests that run inside the Workers runtime

1 lines 65.6 kB
{"version":3,"file":"index.mjs","names":["index","value","structuredSerializableReducers: ReducersRevivers","structuredSerializableRevivers: ReducersRevivers","devalue.stringify","devalue.parse","name: string","cwd: string | undefined","fileName: string | null","error: { stack?: string }","promiseResolve: (() => void) | undefined"],"sources":["../../../../node_modules/.pnpm/devalue@5.6.3/node_modules/devalue/src/utils.js","../../../../node_modules/.pnpm/devalue@5.6.3/node_modules/devalue/src/base64.js","../../../../node_modules/.pnpm/devalue@5.6.3/node_modules/devalue/src/constants.js","../../../../node_modules/.pnpm/devalue@5.6.3/node_modules/devalue/src/parse.js","../../../../node_modules/.pnpm/devalue@5.6.3/node_modules/devalue/src/stringify.js","../../../miniflare/src/workers/core/devalue.ts","../../src/worker/index.ts"],"sourcesContent":["/** @type {Record<string, string>} */\nexport const escaped = {\n\t'<': '\\\\u003C',\n\t'\\\\': '\\\\\\\\',\n\t'\\b': '\\\\b',\n\t'\\f': '\\\\f',\n\t'\\n': '\\\\n',\n\t'\\r': '\\\\r',\n\t'\\t': '\\\\t',\n\t'\\u2028': '\\\\u2028',\n\t'\\u2029': '\\\\u2029'\n};\n\nexport class DevalueError extends Error {\n\t/**\n\t * @param {string} message\n\t * @param {string[]} keys\n\t * @param {any} [value] - The value that failed to be serialized\n\t * @param {any} [root] - The root value being serialized\n\t */\n\tconstructor(message, keys, value, root) {\n\t\tsuper(message);\n\t\tthis.name = 'DevalueError';\n\t\tthis.path = keys.join('');\n\t\tthis.value = value;\n\t\tthis.root = root;\n\t}\n}\n\n/** @param {any} thing */\nexport function is_primitive(thing) {\n\treturn Object(thing) !== thing;\n}\n\nconst object_proto_names = /* @__PURE__ */ Object.getOwnPropertyNames(\n\tObject.prototype\n)\n\t.sort()\n\t.join('\\0');\n\n/** @param {any} thing */\nexport function is_plain_object(thing) {\n\tconst proto = Object.getPrototypeOf(thing);\n\n\treturn (\n\t\tproto === Object.prototype ||\n\t\tproto === null ||\n\t\tObject.getPrototypeOf(proto) === null ||\n\t\tObject.getOwnPropertyNames(proto).sort().join('\\0') === object_proto_names\n\t);\n}\n\n/** @param {any} thing */\nexport function get_type(thing) {\n\treturn Object.prototype.toString.call(thing).slice(8, -1);\n}\n\n/** @param {string} char */\nfunction get_escaped_char(char) {\n\tswitch (char) {\n\t\tcase '\"':\n\t\t\treturn '\\\\\"';\n\t\tcase '<':\n\t\t\treturn '\\\\u003C';\n\t\tcase '\\\\':\n\t\t\treturn '\\\\\\\\';\n\t\tcase '\\n':\n\t\t\treturn '\\\\n';\n\t\tcase '\\r':\n\t\t\treturn '\\\\r';\n\t\tcase '\\t':\n\t\t\treturn '\\\\t';\n\t\tcase '\\b':\n\t\t\treturn '\\\\b';\n\t\tcase '\\f':\n\t\t\treturn '\\\\f';\n\t\tcase '\\u2028':\n\t\t\treturn '\\\\u2028';\n\t\tcase '\\u2029':\n\t\t\treturn '\\\\u2029';\n\t\tdefault:\n\t\t\treturn char < ' '\n\t\t\t\t? `\\\\u${char.charCodeAt(0).toString(16).padStart(4, '0')}`\n\t\t\t\t: '';\n\t}\n}\n\n/** @param {string} str */\nexport function stringify_string(str) {\n\tlet result = '';\n\tlet last_pos = 0;\n\tconst len = str.length;\n\n\tfor (let i = 0; i < len; i += 1) {\n\t\tconst char = str[i];\n\t\tconst replacement = get_escaped_char(char);\n\t\tif (replacement) {\n\t\t\tresult += str.slice(last_pos, i) + replacement;\n\t\t\tlast_pos = i + 1;\n\t\t}\n\t}\n\n\treturn `\"${last_pos === 0 ? str : result + str.slice(last_pos)}\"`;\n}\n\n/** @param {Record<string | symbol, any>} object */\nexport function enumerable_symbols(object) {\n\treturn Object.getOwnPropertySymbols(object).filter(\n\t\t(symbol) => Object.getOwnPropertyDescriptor(object, symbol).enumerable\n\t);\n}\n\nconst is_identifier = /^[a-zA-Z_$][a-zA-Z_$0-9]*$/;\n\n/** @param {string} key */\nexport function stringify_key(key) {\n\treturn is_identifier.test(key) ? '.' + key : '[' + JSON.stringify(key) + ']';\n}\n\n/** @param {string} s */\nfunction is_valid_array_index(s) {\n\tif (s.length === 0) return false;\n\tif (s.length > 1 && s.charCodeAt(0) === 48) return false; // leading zero\n\tfor (let i = 0; i < s.length; i++) {\n\t\tconst c = s.charCodeAt(i);\n\t\tif (c < 48 || c > 57) return false;\n\t}\n\t// by this point we know it's a string of digits, but it has to be within the range of valid array indices\n\tconst n = +s;\n\tif (n >= 2 ** 32 - 1) return false;\n\tif (n < 0) return false;\n\treturn true;\n}\n\n/**\n * Finds the populated indices of an array.\n * @param {unknown[]} array\n */\nexport function valid_array_indices(array) {\n\tconst keys = Object.keys(array);\n\tfor (var i = keys.length - 1; i >= 0; i--) {\n\t\tif (is_valid_array_index(keys[i])) {\n\t\t\tbreak;\n\t\t}\n\t}\n\tkeys.length = i + 1;\n\treturn keys;\n}\n","/**\n * Base64 Encodes an arraybuffer\n * @param {ArrayBuffer} arraybuffer\n * @returns {string}\n */\nexport function encode64(arraybuffer) {\n const dv = new DataView(arraybuffer);\n let binaryString = \"\";\n\n for (let i = 0; i < arraybuffer.byteLength; i++) {\n binaryString += String.fromCharCode(dv.getUint8(i));\n }\n\n return binaryToAscii(binaryString);\n}\n\n/**\n * Decodes a base64 string into an arraybuffer\n * @param {string} string\n * @returns {ArrayBuffer}\n */\nexport function decode64(string) {\n const binaryString = asciiToBinary(string);\n const arraybuffer = new ArrayBuffer(binaryString.length);\n const dv = new DataView(arraybuffer);\n\n for (let i = 0; i < arraybuffer.byteLength; i++) {\n dv.setUint8(i, binaryString.charCodeAt(i));\n }\n\n return arraybuffer;\n}\n\nconst KEY_STRING =\n \"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/\";\n\n/**\n * Substitute for atob since it's deprecated in node.\n * Does not do any input validation.\n *\n * @see https://github.com/jsdom/abab/blob/master/lib/atob.js\n *\n * @param {string} data\n * @returns {string}\n */\nfunction asciiToBinary(data) {\n if (data.length % 4 === 0) {\n data = data.replace(/==?$/, \"\");\n }\n\n let output = \"\";\n let buffer = 0;\n let accumulatedBits = 0;\n\n for (let i = 0; i < data.length; i++) {\n buffer <<= 6;\n buffer |= KEY_STRING.indexOf(data[i]);\n accumulatedBits += 6;\n if (accumulatedBits === 24) {\n output += String.fromCharCode((buffer & 0xff0000) >> 16);\n output += String.fromCharCode((buffer & 0xff00) >> 8);\n output += String.fromCharCode(buffer & 0xff);\n buffer = accumulatedBits = 0;\n }\n }\n if (accumulatedBits === 12) {\n buffer >>= 4;\n output += String.fromCharCode(buffer);\n } else if (accumulatedBits === 18) {\n buffer >>= 2;\n output += String.fromCharCode((buffer & 0xff00) >> 8);\n output += String.fromCharCode(buffer & 0xff);\n }\n return output;\n}\n\n/**\n * Substitute for btoa since it's deprecated in node.\n * Does not do any input validation.\n *\n * @see https://github.com/jsdom/abab/blob/master/lib/btoa.js\n *\n * @param {string} str\n * @returns {string}\n */\nfunction binaryToAscii(str) {\n let out = \"\";\n for (let i = 0; i < str.length; i += 3) {\n /** @type {[number, number, number, number]} */\n const groupsOfSix = [undefined, undefined, undefined, undefined];\n groupsOfSix[0] = str.charCodeAt(i) >> 2;\n groupsOfSix[1] = (str.charCodeAt(i) & 0x03) << 4;\n if (str.length > i + 1) {\n groupsOfSix[1] |= str.charCodeAt(i + 1) >> 4;\n groupsOfSix[2] = (str.charCodeAt(i + 1) & 0x0f) << 2;\n }\n if (str.length > i + 2) {\n groupsOfSix[2] |= str.charCodeAt(i + 2) >> 6;\n groupsOfSix[3] = str.charCodeAt(i + 2) & 0x3f;\n }\n for (let j = 0; j < groupsOfSix.length; j++) {\n if (typeof groupsOfSix[j] === \"undefined\") {\n out += \"=\";\n } else {\n out += KEY_STRING[groupsOfSix[j]];\n }\n }\n }\n return out;\n}\n","export const UNDEFINED = -1;\nexport const HOLE = -2;\nexport const NAN = -3;\nexport const POSITIVE_INFINITY = -4;\nexport const NEGATIVE_INFINITY = -5;\nexport const NEGATIVE_ZERO = -6;\nexport const SPARSE = -7;\n","import { decode64 } from './base64.js';\nimport {\n\tHOLE,\n\tNAN,\n\tNEGATIVE_INFINITY,\n\tNEGATIVE_ZERO,\n\tPOSITIVE_INFINITY,\n\tSPARSE,\n\tUNDEFINED\n} from './constants.js';\n\n/**\n * Revive a value serialized with `devalue.stringify`\n * @param {string} serialized\n * @param {Record<string, (value: any) => any>} [revivers]\n */\nexport function parse(serialized, revivers) {\n\treturn unflatten(JSON.parse(serialized), revivers);\n}\n\n/**\n * Revive a value flattened with `devalue.stringify`\n * @param {number | any[]} parsed\n * @param {Record<string, (value: any) => any>} [revivers]\n */\nexport function unflatten(parsed, revivers) {\n\tif (typeof parsed === 'number') return hydrate(parsed, true);\n\n\tif (!Array.isArray(parsed) || parsed.length === 0) {\n\t\tthrow new Error('Invalid input');\n\t}\n\n\tconst values = /** @type {any[]} */ (parsed);\n\n\tconst hydrated = Array(values.length);\n\n\t/**\n\t * A set of values currently being hydrated with custom revivers,\n\t * used to detect invalid cyclical dependencies\n\t * @type {Set<number> | null}\n\t */\n\tlet hydrating = null;\n\n\t/**\n\t * @param {number} index\n\t * @returns {any}\n\t */\n\tfunction hydrate(index, standalone = false) {\n\t\tif (index === UNDEFINED) return undefined;\n\t\tif (index === NAN) return NaN;\n\t\tif (index === POSITIVE_INFINITY) return Infinity;\n\t\tif (index === NEGATIVE_INFINITY) return -Infinity;\n\t\tif (index === NEGATIVE_ZERO) return -0;\n\n\t\tif (standalone || typeof index !== 'number') {\n\t\t\tthrow new Error(`Invalid input`);\n\t\t}\n\n\t\tif (index in hydrated) return hydrated[index];\n\n\t\tconst value = values[index];\n\n\t\tif (!value || typeof value !== 'object') {\n\t\t\thydrated[index] = value;\n\t\t} else if (Array.isArray(value)) {\n\t\t\tif (typeof value[0] === 'string') {\n\t\t\t\tconst type = value[0];\n\n\t\t\t\tconst reviver =\n\t\t\t\t\trevivers && Object.hasOwn(revivers, type)\n\t\t\t\t\t\t? revivers[type]\n\t\t\t\t\t\t: undefined;\n\n\t\t\t\tif (reviver) {\n\t\t\t\t\tlet i = value[1];\n\t\t\t\t\tif (typeof i !== 'number') {\n\t\t\t\t\t\t// if it's not a number, it was serialized by a builtin reviver\n\t\t\t\t\t\t// so we need to munge it into the format expected by a custom reviver\n\t\t\t\t\t\ti = values.push(value[1]) - 1;\n\t\t\t\t\t}\n\n\t\t\t\t\thydrating ??= new Set();\n\n\t\t\t\t\tif (hydrating.has(i)) {\n\t\t\t\t\t\tthrow new Error('Invalid circular reference');\n\t\t\t\t\t}\n\n\t\t\t\t\thydrating.add(i);\n\t\t\t\t\thydrated[index] = reviver(hydrate(i));\n\t\t\t\t\thydrating.delete(i);\n\n\t\t\t\t\treturn hydrated[index];\n\t\t\t\t}\n\n\t\t\t\tswitch (type) {\n\t\t\t\t\tcase 'Date':\n\t\t\t\t\t\thydrated[index] = new Date(value[1]);\n\t\t\t\t\t\tbreak;\n\n\t\t\t\t\tcase 'Set':\n\t\t\t\t\t\tconst set = new Set();\n\t\t\t\t\t\thydrated[index] = set;\n\t\t\t\t\t\tfor (let i = 1; i < value.length; i += 1) {\n\t\t\t\t\t\t\tset.add(hydrate(value[i]));\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\n\t\t\t\t\tcase 'Map':\n\t\t\t\t\t\tconst map = new Map();\n\t\t\t\t\t\thydrated[index] = map;\n\t\t\t\t\t\tfor (let i = 1; i < value.length; i += 2) {\n\t\t\t\t\t\t\tmap.set(hydrate(value[i]), hydrate(value[i + 1]));\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\n\t\t\t\t\tcase 'RegExp':\n\t\t\t\t\t\thydrated[index] = new RegExp(value[1], value[2]);\n\t\t\t\t\t\tbreak;\n\n\t\t\t\t\tcase 'Object':\n\t\t\t\t\t\thydrated[index] = Object(value[1]);\n\t\t\t\t\t\tbreak;\n\n\t\t\t\t\tcase 'BigInt':\n\t\t\t\t\t\thydrated[index] = BigInt(value[1]);\n\t\t\t\t\t\tbreak;\n\n\t\t\t\t\tcase 'null':\n\t\t\t\t\t\tconst obj = Object.create(null);\n\t\t\t\t\t\thydrated[index] = obj;\n\t\t\t\t\t\tfor (let i = 1; i < value.length; i += 2) {\n\t\t\t\t\t\t\tobj[value[i]] = hydrate(value[i + 1]);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\n\t\t\t\t\tcase 'Int8Array':\n\t\t\t\t\tcase 'Uint8Array':\n\t\t\t\t\tcase 'Uint8ClampedArray':\n\t\t\t\t\tcase 'Int16Array':\n\t\t\t\t\tcase 'Uint16Array':\n\t\t\t\t\tcase 'Int32Array':\n\t\t\t\t\tcase 'Uint32Array':\n\t\t\t\t\tcase 'Float32Array':\n\t\t\t\t\tcase 'Float64Array':\n\t\t\t\t\tcase 'BigInt64Array':\n\t\t\t\t\tcase 'BigUint64Array': {\n\t\t\t\t\t\tif (values[value[1]][0] !== 'ArrayBuffer') {\n\t\t\t\t\t\t\t// without this, if we receive malformed input we could\n\t\t\t\t\t\t\t// end up trying to hydrate in a circle or allocate\n\t\t\t\t\t\t\t// huge amounts of memory when we call `new TypedArrayConstructor(buffer)`\n\t\t\t\t\t\t\tthrow new Error('Invalid data');\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tconst TypedArrayConstructor = globalThis[type];\n\t\t\t\t\t\tconst buffer = hydrate(value[1]);\n\t\t\t\t\t\tconst typedArray = new TypedArrayConstructor(buffer);\n\n\t\t\t\t\t\thydrated[index] =\n\t\t\t\t\t\t\tvalue[2] !== undefined\n\t\t\t\t\t\t\t\t? typedArray.subarray(value[2], value[3])\n\t\t\t\t\t\t\t\t: typedArray;\n\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\n\t\t\t\t\tcase 'ArrayBuffer': {\n\t\t\t\t\t\tconst base64 = value[1];\n\t\t\t\t\t\tif (typeof base64 !== 'string') {\n\t\t\t\t\t\t\tthrow new Error('Invalid ArrayBuffer encoding');\n\t\t\t\t\t\t}\n\t\t\t\t\t\tconst arraybuffer = decode64(base64);\n\t\t\t\t\t\thydrated[index] = arraybuffer;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\n\t\t\t\t\tcase 'Temporal.Duration':\n\t\t\t\t\tcase 'Temporal.Instant':\n\t\t\t\t\tcase 'Temporal.PlainDate':\n\t\t\t\t\tcase 'Temporal.PlainTime':\n\t\t\t\t\tcase 'Temporal.PlainDateTime':\n\t\t\t\t\tcase 'Temporal.PlainMonthDay':\n\t\t\t\t\tcase 'Temporal.PlainYearMonth':\n\t\t\t\t\tcase 'Temporal.ZonedDateTime': {\n\t\t\t\t\t\tconst temporalName = type.slice(9);\n\t\t\t\t\t\t// @ts-expect-error TS doesn't know about Temporal yet\n\t\t\t\t\t\thydrated[index] = Temporal[temporalName].from(value[1]);\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\n\t\t\t\t\tcase 'URL': {\n\t\t\t\t\t\tconst url = new URL(value[1]);\n\t\t\t\t\t\thydrated[index] = url;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\n\t\t\t\t\tcase 'URLSearchParams': {\n\t\t\t\t\t\tconst url = new URLSearchParams(value[1]);\n\t\t\t\t\t\thydrated[index] = url;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\n\t\t\t\t\tdefault:\n\t\t\t\t\t\tthrow new Error(`Unknown type ${type}`);\n\t\t\t\t}\n\t\t\t} else if (value[0] === SPARSE) {\n\t\t\t\t// Sparse array encoding: [SPARSE, length, idx, val, idx, val, ...]\n\t\t\t\tconst len = value[1];\n\n\t\t\t\tconst array = new Array(len);\n\t\t\t\thydrated[index] = array;\n\n\t\t\t\tfor (let i = 2; i < value.length; i += 2) {\n\t\t\t\t\tconst idx = value[i];\n\t\t\t\t\tarray[idx] = hydrate(value[i + 1]);\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tconst array = new Array(value.length);\n\t\t\t\thydrated[index] = array;\n\n\t\t\t\tfor (let i = 0; i < value.length; i += 1) {\n\t\t\t\t\tconst n = value[i];\n\t\t\t\t\tif (n === HOLE) continue;\n\n\t\t\t\t\tarray[i] = hydrate(n);\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\t/** @type {Record<string, any>} */\n\t\t\tconst object = {};\n\t\t\thydrated[index] = object;\n\n\t\t\tfor (const key of Object.keys(value)) {\n\t\t\t\tif (key === '__proto__') {\n\t\t\t\t\tthrow new Error('Cannot parse an object with a `__proto__` property');\n\t\t\t\t}\n\n\t\t\t\tconst n = value[key];\n\t\t\t\tobject[key] = hydrate(n);\n\t\t\t}\n\t\t}\n\n\t\treturn hydrated[index];\n\t}\n\n\treturn hydrate(0);\n}\n","import {\n\tDevalueError,\n\tenumerable_symbols,\n\tget_type,\n\tis_plain_object,\n\tis_primitive,\n\tstringify_key,\n\tstringify_string,\n\tvalid_array_indices\n} from './utils.js';\nimport {\n\tHOLE,\n\tNAN,\n\tNEGATIVE_INFINITY,\n\tNEGATIVE_ZERO,\n\tPOSITIVE_INFINITY,\n\tSPARSE,\n\tUNDEFINED\n} from './constants.js';\nimport { encode64 } from './base64.js';\n\n/**\n * Turn a value into a JSON string that can be parsed with `devalue.parse`\n * @param {any} value\n * @param {Record<string, (value: any) => any>} [reducers]\n */\nexport function stringify(value, reducers) {\n\t/** @type {any[]} */\n\tconst stringified = [];\n\n\t/** @type {Map<any, number>} */\n\tconst indexes = new Map();\n\n\t/** @type {Array<{ key: string, fn: (value: any) => any }>} */\n\tconst custom = [];\n\tif (reducers) {\n\t\tfor (const key of Object.getOwnPropertyNames(reducers)) {\n\t\t\tcustom.push({ key, fn: reducers[key] });\n\t\t}\n\t}\n\n\t/** @type {string[]} */\n\tconst keys = [];\n\n\tlet p = 0;\n\n\t/** @param {any} thing */\n\tfunction flatten(thing) {\n\t\tif (thing === undefined) return UNDEFINED;\n\t\tif (Number.isNaN(thing)) return NAN;\n\t\tif (thing === Infinity) return POSITIVE_INFINITY;\n\t\tif (thing === -Infinity) return NEGATIVE_INFINITY;\n\t\tif (thing === 0 && 1 / thing < 0) return NEGATIVE_ZERO;\n\n\t\tif (indexes.has(thing)) return indexes.get(thing);\n\n\t\tconst index = p++;\n\t\tindexes.set(thing, index);\n\n\t\tfor (const { key, fn } of custom) {\n\t\t\tconst value = fn(thing);\n\t\t\tif (value) {\n\t\t\t\tstringified[index] = `[\"${key}\",${flatten(value)}]`;\n\t\t\t\treturn index;\n\t\t\t}\n\t\t}\n\n\t\tif (typeof thing === 'function') {\n\t\t\tthrow new DevalueError(`Cannot stringify a function`, keys, thing, value);\n\t\t}\n\n\t\tlet str = '';\n\n\t\tif (is_primitive(thing)) {\n\t\t\tstr = stringify_primitive(thing);\n\t\t} else {\n\t\t\tconst type = get_type(thing);\n\n\t\t\tswitch (type) {\n\t\t\t\tcase 'Number':\n\t\t\t\tcase 'String':\n\t\t\t\tcase 'Boolean':\n\t\t\t\t\tstr = `[\"Object\",${stringify_primitive(thing)}]`;\n\t\t\t\t\tbreak;\n\n\t\t\t\tcase 'BigInt':\n\t\t\t\t\tstr = `[\"BigInt\",${thing}]`;\n\t\t\t\t\tbreak;\n\n\t\t\t\tcase 'Date':\n\t\t\t\t\tconst valid = !isNaN(thing.getDate());\n\t\t\t\t\tstr = `[\"Date\",\"${valid ? thing.toISOString() : ''}\"]`;\n\t\t\t\t\tbreak;\n\n\t\t\t\tcase 'URL':\n\t\t\t\t\tstr = `[\"URL\",${stringify_string(thing.toString())}]`;\n\t\t\t\t\tbreak;\n\n\t\t\t\tcase 'URLSearchParams':\n\t\t\t\t\tstr = `[\"URLSearchParams\",${stringify_string(thing.toString())}]`;\n\t\t\t\t\tbreak;\n\n\t\t\t\tcase 'RegExp':\n\t\t\t\t\tconst { source, flags } = thing;\n\t\t\t\t\tstr = flags\n\t\t\t\t\t\t? `[\"RegExp\",${stringify_string(source)},\"${flags}\"]`\n\t\t\t\t\t\t: `[\"RegExp\",${stringify_string(source)}]`;\n\t\t\t\t\tbreak;\n\n\t\t\t\tcase 'Array': {\n\t\t\t\t\t// For dense arrays (no holes), we iterate normally.\n\t\t\t\t\t// When we encounter the first hole, we call Object.keys\n\t\t\t\t\t// to determine the sparseness, then decide between:\n\t\t\t\t\t// - HOLE encoding: [-2, val, -2, ...] (default)\n\t\t\t\t\t// - Sparse encoding: [-7, length, idx, val, ...] (for very sparse arrays)\n\t\t\t\t\t// Only the sparse path avoids iterating every slot, which\n\t\t\t\t\t// is what protects against the DoS of e.g. `arr[1000000] = 1`.\n\t\t\t\t\tlet mostly_dense = false;\n\n\t\t\t\t\tstr = '[';\n\n\t\t\t\t\tfor (let i = 0; i < thing.length; i += 1) {\n\t\t\t\t\t\tif (i > 0) str += ',';\n\n\t\t\t\t\t\tif (Object.hasOwn(thing, i)) {\n\t\t\t\t\t\t\tkeys.push(`[${i}]`);\n\t\t\t\t\t\t\tstr += flatten(thing[i]);\n\t\t\t\t\t\t\tkeys.pop();\n\t\t\t\t\t\t} else if (mostly_dense) {\n\t\t\t\t\t\t\t// Use dense encoding. The heuristic guarantees the\n\t\t\t\t\t\t\t// array is only mildly sparse, so iterating over every\n\t\t\t\t\t\t\t// slot is fine.\n\t\t\t\t\t\t\tstr += HOLE;\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t// Decide between HOLE encoding and sparse encoding.\n\t\t\t\t\t\t\t//\n\t\t\t\t\t\t\t// HOLE encoding: each hole is serialized as the HOLE\n\t\t\t\t\t\t\t// sentinel (-2). For example, [, \"a\", ,] becomes\n\t\t\t\t\t\t\t// [-2, 0, -2]. Each hole costs 3 chars (\"-2\" + comma).\n\t\t\t\t\t\t\t//\n\t\t\t\t\t\t\t// Sparse encoding: lists only populated indices.\n\t\t\t\t\t\t\t// For example, [, \"a\", ,] becomes [-7, 3, 1, 0] — the\n\t\t\t\t\t\t\t// -7 sentinel, the array length (3), then index-value\n\t\t\t\t\t\t\t// pairs. This avoids paying per-hole, but each element\n\t\t\t\t\t\t\t// costs extra chars to write its index.\n\t\t\t\t\t\t\t//\n\t\t\t\t\t\t\t// The values are the same size either way, so the\n\t\t\t\t\t\t\t// choice comes down to structural overhead:\n\t\t\t\t\t\t\t//\n\t\t\t\t\t\t\t// HOLE overhead:\n\t\t\t\t\t\t\t// 3 chars per hole (\"-2\" + comma)\n\t\t\t\t\t\t\t// = (L - P) * 3\n\t\t\t\t\t\t\t//\n\t\t\t\t\t\t\t// Sparse overhead:\n\t\t\t\t\t\t\t// \"-7,\" — 3 chars (sparse sentinel + comma)\n\t\t\t\t\t\t\t// + length + \",\" — (d + 1) chars (array length + comma)\n\t\t\t\t\t\t\t// + per element: index + \",\" — (d + 1) chars\n\t\t\t\t\t\t\t// = (4 + d) + P * (d + 1)\n\t\t\t\t\t\t\t//\n\t\t\t\t\t\t\t// where L is the array length, P is the number of\n\t\t\t\t\t\t\t// populated elements, and d is the number of digits\n\t\t\t\t\t\t\t// in L (an upper bound on the digits in any index).\n\t\t\t\t\t\t\t//\n\t\t\t\t\t\t\t// Sparse encoding is cheaper when:\n\t\t\t\t\t\t\t// (4 + d) + P * (d + 1) < (L - P) * 3\n\t\t\t\t\t\t\tconst populated_keys = valid_array_indices(/** @type {any[]} */ (thing));\n\t\t\t\t\t\t\tconst population = populated_keys.length;\n\t\t\t\t\t\t\tconst d = String(thing.length).length;\n\n\t\t\t\t\t\t\tconst hole_cost = (thing.length - population) * 3;\n\t\t\t\t\t\t\tconst sparse_cost = 4 + d + population * (d + 1);\n\n\t\t\t\t\t\t\tif (hole_cost > sparse_cost) {\n\t\t\t\t\t\t\t\tstr = '[' + SPARSE + ',' + thing.length;\n\t\t\t\t\t\t\t\tfor (let j = 0; j < populated_keys.length; j++) {\n\t\t\t\t\t\t\t\t\tconst key = populated_keys[j];\n\t\t\t\t\t\t\t\t\tkeys.push(`[${key}]`);\n\t\t\t\t\t\t\t\t\tstr += ',' + key + ',' + flatten(thing[key]);\n\t\t\t\t\t\t\t\t\tkeys.pop();\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\tmostly_dense = true;\n\t\t\t\t\t\t\t\tstr += HOLE;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\tstr += ']';\n\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\n\t\t\t\tcase 'Set':\n\t\t\t\t\tstr = '[\"Set\"';\n\n\t\t\t\t\tfor (const value of thing) {\n\t\t\t\t\t\tstr += `,${flatten(value)}`;\n\t\t\t\t\t}\n\n\t\t\t\t\tstr += ']';\n\t\t\t\t\tbreak;\n\n\t\t\t\tcase 'Map':\n\t\t\t\t\tstr = '[\"Map\"';\n\n\t\t\t\t\tfor (const [key, value] of thing) {\n\t\t\t\t\t\tkeys.push(\n\t\t\t\t\t\t\t`.get(${is_primitive(key) ? stringify_primitive(key) : '...'})`\n\t\t\t\t\t\t);\n\t\t\t\t\t\tstr += `,${flatten(key)},${flatten(value)}`;\n\t\t\t\t\t\tkeys.pop();\n\t\t\t\t\t}\n\n\t\t\t\t\tstr += ']';\n\t\t\t\t\tbreak;\n\n\t\t\t\tcase 'Int8Array':\n\t\t\t\tcase 'Uint8Array':\n\t\t\t\tcase 'Uint8ClampedArray':\n\t\t\t\tcase 'Int16Array':\n\t\t\t\tcase 'Uint16Array':\n\t\t\t\tcase 'Int32Array':\n\t\t\t\tcase 'Uint32Array':\n\t\t\t\tcase 'Float32Array':\n\t\t\t\tcase 'Float64Array':\n\t\t\t\tcase 'BigInt64Array':\n\t\t\t\tcase 'BigUint64Array': {\n\t\t\t\t\t/** @type {import(\"./types.js\").TypedArray} */\n\t\t\t\t\tconst typedArray = thing;\n\t\t\t\t\tstr = '[\"' + type + '\",' + flatten(typedArray.buffer);\n\n\t\t\t\t\tconst a = thing.byteOffset;\n\t\t\t\t\tconst b = a + thing.byteLength;\n\n\t\t\t\t\t// handle subarrays\n\t\t\t\t\tif (a > 0 || b !== typedArray.buffer.byteLength) {\n\t\t\t\t\t\tconst m = +/(\\d+)/.exec(type)[1] / 8;\n\t\t\t\t\t\tstr += `,${a / m},${b / m}`;\n\t\t\t\t\t}\n\n\t\t\t\t\tstr += ']';\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\n\t\t\t\tcase 'ArrayBuffer': {\n\t\t\t\t\t/** @type {ArrayBuffer} */\n\t\t\t\t\tconst arraybuffer = thing;\n\t\t\t\t\tconst base64 = encode64(arraybuffer);\n\n\t\t\t\t\tstr = `[\"ArrayBuffer\",\"${base64}\"]`;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\n\t\t\t\tcase 'Temporal.Duration':\n\t\t\t\tcase 'Temporal.Instant':\n\t\t\t\tcase 'Temporal.PlainDate':\n\t\t\t\tcase 'Temporal.PlainTime':\n\t\t\t\tcase 'Temporal.PlainDateTime':\n\t\t\t\tcase 'Temporal.PlainMonthDay':\n\t\t\t\tcase 'Temporal.PlainYearMonth':\n\t\t\t\tcase 'Temporal.ZonedDateTime':\n\t\t\t\t\tstr = `[\"${type}\",${stringify_string(thing.toString())}]`;\n\t\t\t\t\tbreak;\n\n\t\t\t\tdefault:\n\t\t\t\t\tif (!is_plain_object(thing)) {\n\t\t\t\t\t\tthrow new DevalueError(\n\t\t\t\t\t\t\t`Cannot stringify arbitrary non-POJOs`,\n\t\t\t\t\t\t\tkeys,\n\t\t\t\t\t\t\tthing,\n\t\t\t\t\t\t\tvalue\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\n\t\t\t\t\tif (enumerable_symbols(thing).length > 0) {\n\t\t\t\t\t\tthrow new DevalueError(\n\t\t\t\t\t\t\t`Cannot stringify POJOs with symbolic keys`,\n\t\t\t\t\t\t\tkeys,\n\t\t\t\t\t\t\tthing,\n\t\t\t\t\t\t\tvalue\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\n\t\t\t\t\tif (Object.getPrototypeOf(thing) === null) {\n\t\t\t\t\t\tstr = '[\"null\"';\n\t\t\t\t\t\tfor (const key of Object.keys(thing)) {\n\t\t\t\t\t\t\tif (key === '__proto__') {\n\t\t\t\t\t\t\t\tthrow new DevalueError(\n\t\t\t\t\t\t\t\t\t`Cannot stringify objects with __proto__ keys`,\n\t\t\t\t\t\t\t\t\tkeys,\n\t\t\t\t\t\t\t\t\tthing,\n\t\t\t\t\t\t\t\t\tvalue\n\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\tkeys.push(stringify_key(key));\n\t\t\t\t\t\t\tstr += `,${stringify_string(key)},${flatten(thing[key])}`;\n\t\t\t\t\t\t\tkeys.pop();\n\t\t\t\t\t\t}\n\t\t\t\t\t\tstr += ']';\n\t\t\t\t\t} else {\n\t\t\t\t\t\tstr = '{';\n\t\t\t\t\t\tlet started = false;\n\t\t\t\t\t\tfor (const key of Object.keys(thing)) {\n\t\t\t\t\t\t\tif (key === '__proto__') {\n\t\t\t\t\t\t\t\tthrow new DevalueError(\n\t\t\t\t\t\t\t\t\t`Cannot stringify objects with __proto__ keys`,\n\t\t\t\t\t\t\t\t\tkeys,\n\t\t\t\t\t\t\t\t\tthing,\n\t\t\t\t\t\t\t\t\tvalue\n\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\tif (started) str += ',';\n\t\t\t\t\t\t\tstarted = true;\n\t\t\t\t\t\t\tkeys.push(stringify_key(key));\n\t\t\t\t\t\t\tstr += `${stringify_string(key)}:${flatten(thing[key])}`;\n\t\t\t\t\t\t\tkeys.pop();\n\t\t\t\t\t\t}\n\t\t\t\t\t\tstr += '}';\n\t\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tstringified[index] = str;\n\t\treturn index;\n\t}\n\n\tconst index = flatten(value);\n\n\t// special case — value is represented as a negative index\n\tif (index < 0) return `${index}`;\n\n\treturn `[${stringified.join(',')}]`;\n}\n\n/**\n * @param {any} thing\n * @returns {string}\n */\nfunction stringify_primitive(thing) {\n\tconst type = typeof thing;\n\tif (type === 'string') return stringify_string(thing);\n\tif (thing instanceof String) return stringify_string(thing.toString());\n\tif (thing === void 0) return UNDEFINED.toString();\n\tif (thing === 0 && 1 / thing < 0) return NEGATIVE_ZERO.toString();\n\tif (type === 'bigint') return `[\"BigInt\",\"${thing}\"]`;\n\treturn String(thing);\n}\n","import assert from \"node:assert\";\nimport { Buffer } from \"node:buffer\";\nimport { parse, stringify } from \"devalue\";\nimport type {\n\tBlob as WorkerBlob,\n\tBlobOptions as WorkerBlobOptions,\n\tFile as WorkerFile,\n\tFileOptions as WorkerFileOptions,\n\tHeaders as WorkerHeaders,\n\tReadableStream as WorkerReadableStream,\n\tRequest as WorkerRequest,\n\tResponse as WorkerResponse,\n} from \"@cloudflare/workers-types/experimental\";\n\n// This file implements `devalue` reducers and revivers for structured-\n// serialisable types not supported by default. See serialisable types here:\n// https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm#supported_types\n\nexport type ReducerReviver = (value: unknown) => unknown;\nexport type ReducersRevivers = Record<string, ReducerReviver>;\n\nconst ALLOWED_ARRAY_BUFFER_VIEW_CONSTRUCTORS = [\n\tDataView<ArrayBuffer>,\n\tInt8Array,\n\tUint8Array,\n\tUint8ClampedArray,\n\tInt16Array,\n\tUint16Array,\n\tInt32Array,\n\tUint32Array,\n\tFloat32Array,\n\tFloat64Array,\n\tBigInt64Array,\n\tBigUint64Array,\n] as const;\nconst ALLOWED_ERROR_CONSTRUCTORS = [\n\tEvalError,\n\tRangeError,\n\tReferenceError,\n\tSyntaxError,\n\tTypeError,\n\tURIError,\n\tError, // `Error` last so more specific error subclasses preferred\n] as const;\n\nexport const structuredSerializableReducers: ReducersRevivers = {\n\tArrayBuffer(value) {\n\t\tif (value instanceof ArrayBuffer) {\n\t\t\t// Return single element array so empty `ArrayBuffer` serialised as truthy\n\t\t\treturn [Buffer.from(value).toString(\"base64\")];\n\t\t}\n\t},\n\tArrayBufferView(value) {\n\t\tif (ArrayBuffer.isView(value)) {\n\t\t\tlet name = value.constructor.name;\n\t\t\t// Subclasses like Node.js `Buffer` extend standard typed arrays but\n\t\t\t// aren't available in all runtimes. Normalise to the parent constructor.\n\t\t\tif (\n\t\t\t\t!ALLOWED_ARRAY_BUFFER_VIEW_CONSTRUCTORS.some((c) => c.name === name)\n\t\t\t) {\n\t\t\t\tfor (const ctor of ALLOWED_ARRAY_BUFFER_VIEW_CONSTRUCTORS) {\n\t\t\t\t\tif (value instanceof ctor) {\n\t\t\t\t\t\tname = ctor.name;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tlet buf = value.buffer;\n\t\t\tlet off = value.byteOffset;\n\t\t\tif (off !== 0 || buf.byteLength !== value.byteLength) {\n\t\t\t\tbuf = buf.slice(off, off + value.byteLength);\n\t\t\t\toff = 0;\n\t\t\t}\n\t\t\treturn [name, buf, off, value.byteLength];\n\t\t}\n\t},\n\tRegExp(value) {\n\t\tif (value instanceof RegExp) {\n\t\t\tconst { source, flags } = value;\n\t\t\tconst encoded = Buffer.from(source).toString(\"base64\");\n\t\t\treturn flags ? [\"RegExp\", encoded, flags] : [\"RegExp\", encoded];\n\t\t}\n\t},\n\tError(value) {\n\t\tfor (const ctor of ALLOWED_ERROR_CONSTRUCTORS) {\n\t\t\tif (value instanceof ctor && value.name === ctor.name) {\n\t\t\t\treturn [value.name, value.message, value.stack, value.cause];\n\t\t\t}\n\t\t}\n\t\tif (value instanceof Error) {\n\t\t\treturn [\"Error\", value.message, value.stack, value.cause];\n\t\t}\n\t},\n};\nexport const structuredSerializableRevivers: ReducersRevivers = {\n\tArrayBuffer(value) {\n\t\tassert(Array.isArray(value));\n\t\tconst [encoded] = value as unknown[];\n\t\tassert(typeof encoded === \"string\");\n\t\tconst view = Buffer.from(encoded, \"base64\");\n\t\treturn view.buffer.slice(\n\t\t\tview.byteOffset,\n\t\t\tview.byteOffset + view.byteLength\n\t\t);\n\t},\n\tArrayBufferView(value) {\n\t\tassert(Array.isArray(value));\n\t\tconst [name, buffer, byteOffset, byteLength] = value as unknown[];\n\t\tassert(typeof name === \"string\");\n\t\tassert(buffer instanceof ArrayBuffer);\n\t\tassert(typeof byteOffset === \"number\");\n\t\tassert(typeof byteLength === \"number\");\n\t\tconst ctor = (globalThis as Record<string, unknown>)[\n\t\t\tname\n\t\t] as (typeof ALLOWED_ARRAY_BUFFER_VIEW_CONSTRUCTORS)[number];\n\t\tassert(ALLOWED_ARRAY_BUFFER_VIEW_CONSTRUCTORS.includes(ctor));\n\t\tlet length = byteLength;\n\t\tif (\"BYTES_PER_ELEMENT\" in ctor) length /= ctor.BYTES_PER_ELEMENT;\n\t\treturn new ctor(buffer as ArrayBuffer, byteOffset, length);\n\t},\n\tRegExp(value) {\n\t\tassert(Array.isArray(value));\n\t\tconst [name, encoded, flags] = value;\n\t\tassert(typeof name === \"string\");\n\t\tassert(typeof encoded === \"string\");\n\t\tconst source = Buffer.from(encoded, \"base64\").toString(\"utf-8\");\n\t\treturn new RegExp(source, flags);\n\t},\n\tError(value) {\n\t\tassert(Array.isArray(value));\n\t\tconst [name, message, stack, cause] = value as unknown[];\n\t\tassert(typeof name === \"string\");\n\t\tassert(typeof message === \"string\");\n\t\tassert(stack === undefined || typeof stack === \"string\");\n\t\tconst ctor = (globalThis as Record<string, unknown>)[\n\t\t\tname\n\t\t] as (typeof ALLOWED_ERROR_CONSTRUCTORS)[number];\n\t\tassert(ALLOWED_ERROR_CONSTRUCTORS.includes(ctor));\n\t\tconst error = new ctor(message, { cause });\n\t\terror.stack = stack;\n\t\treturn error;\n\t},\n};\n\n// This file gets imported both by Node and workers. These platforms have\n// different ways of accessing/performing operations required by this code.\n// This interface should be implemented by both platforms to provide this\n// functionality. `RS` is the type of `ReadableStream`.\nexport interface PlatformImpl<RS> {\n\tBlob: typeof WorkerBlob;\n\tFile: typeof WorkerFile;\n\tHeaders: typeof WorkerHeaders;\n\tRequest: typeof WorkerRequest;\n\tResponse: typeof WorkerResponse;\n\n\tisReadableStream(value: unknown): value is RS;\n\tbufferReadableStream(stream: RS): Promise<ArrayBuffer>;\n\tunbufferReadableStream(buffer: ArrayBuffer): RS;\n}\n\nexport function createHTTPReducers(\n\timpl: PlatformImpl<unknown>\n): ReducersRevivers {\n\treturn {\n\t\tHeaders(val) {\n\t\t\tif (val instanceof impl.Headers) return [...val.entries()];\n\t\t},\n\t\tRequest(val) {\n\t\t\tif (val instanceof impl.Request) {\n\t\t\t\treturn [val.method, val.url, val.headers, val.cf, val.body];\n\t\t\t}\n\t\t},\n\t\tResponse(val) {\n\t\t\tif (val instanceof impl.Response) {\n\t\t\t\treturn [val.status, val.statusText, val.headers, val.cf, val.body];\n\t\t\t}\n\t\t},\n\t};\n}\nexport function createHTTPRevivers<RS>(\n\timpl: PlatformImpl<RS>\n): ReducersRevivers {\n\treturn {\n\t\tHeaders(value) {\n\t\t\tassert(typeof value === \"object\" && value !== null);\n\t\t\treturn new impl.Headers(value as string[][]);\n\t\t},\n\t\tRequest(value) {\n\t\t\tassert(Array.isArray(value));\n\t\t\tconst [method, url, headers, cf, body] = value as unknown[];\n\t\t\tassert(typeof method === \"string\");\n\t\t\tassert(typeof url === \"string\");\n\t\t\tassert(headers instanceof impl.Headers);\n\t\t\tassert(body === null || impl.isReadableStream(body));\n\t\t\treturn new impl.Request(url, {\n\t\t\t\tmethod,\n\t\t\t\theaders,\n\t\t\t\tcf,\n\t\t\t\t// @ts-expect-error `duplex` is not required by `workerd` yet\n\t\t\t\tduplex: body === null ? undefined : \"half\",\n\t\t\t\tbody: body as WorkerReadableStream | null,\n\t\t\t});\n\t\t},\n\t\tResponse(value) {\n\t\t\tassert(Array.isArray(value));\n\t\t\tconst [status, statusText, headers, cf, body] = value as unknown[];\n\t\t\tassert(typeof status === \"number\");\n\t\t\tassert(typeof statusText === \"string\");\n\t\t\tassert(headers instanceof impl.Headers);\n\t\t\tassert(body === null || impl.isReadableStream(body));\n\t\t\treturn new impl.Response(body as WorkerReadableStream | null, {\n\t\t\t\tstatus,\n\t\t\t\tstatusText,\n\t\t\t\theaders,\n\t\t\t\tcf,\n\t\t\t});\n\t\t},\n\t};\n}\n\nexport interface StringifiedWithStream<RS> {\n\tvalue: string;\n\tunbufferedStream?: RS;\n}\n// `devalue` `stringify()` that allows a single stream to be \"unbuffered\", and\n// sent separately. Other streams will be buffered.\nexport function stringifyWithStreams<RS>(\n\timpl: PlatformImpl<RS>,\n\tvalue: unknown,\n\treducers: ReducersRevivers,\n\tallowUnbufferedStream: boolean\n): StringifiedWithStream<RS> | Promise<StringifiedWithStream<RS>> {\n\tlet unbufferedStream: RS | undefined;\n\t// The tricky thing here is that `devalue` `stringify()` is synchronous, and\n\t// doesn't support asynchronous reducers. Assuming we visit values in the same\n\t// order each time, we can use an array to store buffer promises.\n\tconst bufferPromises: Promise<ArrayBuffer>[] = [];\n\tconst streamReducers: ReducersRevivers = {\n\t\tReadableStream(value) {\n\t\t\tif (impl.isReadableStream(value)) {\n\t\t\t\tif (allowUnbufferedStream && unbufferedStream === undefined) {\n\t\t\t\t\tunbufferedStream = value;\n\t\t\t\t} else {\n\t\t\t\t\tbufferPromises.push(impl.bufferReadableStream(value));\n\t\t\t\t}\n\t\t\t\t// Using `true` to signify unbuffered stream, buffered streams will\n\t\t\t\t// have this replaced with an `ArrayBuffer` on the 2nd `stringify()`\n\t\t\t\t// If we don't have any buffer promises, this will encode to the correct\n\t\t\t\t// value, so we don't need to re-`stringify()`.\n\t\t\t\treturn true;\n\t\t\t}\n\t\t},\n\t\tBlob(value) {\n\t\t\tif (value instanceof impl.Blob) {\n\t\t\t\t// `Blob`s are always buffered. We can't just serialise with a stream\n\t\t\t\t// here (and recursively use the reducer above), because `workerd`\n\t\t\t\t// doesn't allow us to synchronously reconstruct a `Blob` from a stream:\n\t\t\t\t// its `new Blob([...])` doesn't support `ReadableStream` blob bits.\n\t\t\t\tbufferPromises.push(value.arrayBuffer());\n\t\t\t\treturn true;\n\t\t\t}\n\t\t},\n\n\t\t...reducers,\n\t};\n\tif (typeof value === \"function\") {\n\t\tvalue = new __MiniflareFunctionWrapper(\n\t\t\tvalue as ConstructorParameters<typeof __MiniflareFunctionWrapper>[0]\n\t\t);\n\t}\n\tconst stringifiedValue = stringify(value, streamReducers);\n\t// If we didn't need to buffer anything, we've just encoded correctly. Note\n\t// `unbufferedStream` may be undefined if the `value` didn't contain streams.\n\t// Note also in this case we're returning synchronously, so we can use this\n\t// for synchronous methods too.\n\tif (bufferPromises.length === 0) {\n\t\treturn { value: stringifiedValue, unbufferedStream };\n\t}\n\n\t// Otherwise, wait for buffering to complete, and `stringify()` again with\n\t// a reducer that expects buffers.\n\treturn Promise.all(bufferPromises).then((streamBuffers) => {\n\t\t// Again, we're assuming values are visited in the same order, so `shift()`\n\t\t// will give us the next correct buffer\n\t\tstreamReducers.ReadableStream = function (value) {\n\t\t\tif (impl.isReadableStream(value)) {\n\t\t\t\tif (value === unbufferedStream) {\n\t\t\t\t\treturn true;\n\t\t\t\t} else {\n\t\t\t\t\treturn streamBuffers.shift();\n\t\t\t\t}\n\t\t\t}\n\t\t};\n\t\tstreamReducers.Blob = function (value) {\n\t\t\tif (value instanceof impl.Blob) {\n\t\t\t\tconst array: unknown[] = [streamBuffers.shift(), value.type];\n\t\t\t\tif (value instanceof impl.File) {\n\t\t\t\t\tarray.push(value.name, value.lastModified);\n\t\t\t\t}\n\t\t\t\treturn array;\n\t\t\t}\n\t\t};\n\t\tconst stringifiedValue = stringify(value, streamReducers);\n\t\treturn { value: stringifiedValue, unbufferedStream };\n\t});\n}\n\n// functions can't be stringified, so we wrap them into a class that we then use to pseudo-serialize them\n// we also add a proxy and make sure that properties set on the function object are accessible\n// (this is in particular necessary for RpcStubs)\nexport class __MiniflareFunctionWrapper {\n\tconstructor(\n\t\tfnWithProps: ((...args: unknown[]) => unknown) & {\n\t\t\t[key: string | symbol]: unknown;\n\t\t}\n\t) {\n\t\treturn new Proxy(this, {\n\t\t\tget: (_, key) => {\n\t\t\t\tif (key === \"__miniflareWrappedFunction\") return fnWithProps;\n\t\t\t\treturn fnWithProps[key];\n\t\t\t},\n\t\t});\n\t}\n}\n\nexport function parseWithReadableStreams<RS>(\n\timpl: PlatformImpl<RS>,\n\tstringified: StringifiedWithStream<RS>,\n\trevivers: ReducersRevivers\n): unknown {\n\tconst streamRevivers: ReducersRevivers = {\n\t\tReadableStream(value) {\n\t\t\tif (value === true) {\n\t\t\t\tassert(stringified.unbufferedStream !== undefined);\n\t\t\t\treturn stringified.unbufferedStream;\n\t\t\t}\n\t\t\tassert(value instanceof ArrayBuffer);\n\t\t\treturn impl.unbufferReadableStream(value);\n\t\t},\n\t\tBlob(value) {\n\t\t\tassert(Array.isArray(value));\n\t\t\tif (value.length === 2) {\n\t\t\t\t// Blob\n\t\t\t\tconst [buffer, type] = value as unknown[];\n\t\t\t\tassert(buffer instanceof ArrayBuffer);\n\t\t\t\tassert(typeof type === \"string\");\n\t\t\t\tconst opts: WorkerBlobOptions = {};\n\t\t\t\tif (type !== \"\") opts.type = type;\n\t\t\t\treturn new impl.Blob([buffer], opts);\n\t\t\t} else {\n\t\t\t\t// File\n\t\t\t\tassert(value.length === 4);\n\t\t\t\tconst [buffer, type, name, lastModified] = value as unknown[];\n\t\t\t\tassert(buffer instanceof ArrayBuffer);\n\t\t\t\tassert(typeof type === \"string\");\n\t\t\t\tassert(typeof name === \"string\");\n\t\t\t\tassert(typeof lastModified === \"number\");\n\t\t\t\tconst opts: WorkerFileOptions = { lastModified };\n\t\t\t\tif (type !== \"\") opts.type = type;\n\t\t\t\treturn new impl.File([buffer], name, opts);\n\t\t\t}\n\t\t},\n\t\t...revivers,\n\t};\n\n\treturn parse(stringified.value, streamRevivers);\n}\n","import assert from \"node:assert\";\nimport * as vm from \"node:vm\";\nimport defines from \"__VITEST_POOL_WORKERS_DEFINES\";\nimport {\n\tcreateWorkerEntrypointWrapper,\n\tmaybeHandleRunRequest,\n\tregisterHandlerAndGlobalWaitUntil,\n\trunInRunnerObject,\n} from \"cloudflare:test-internal\";\nimport { DurableObject } from \"cloudflare:workers\";\nimport * as devalue from \"devalue\";\n// Using relative path here to ensure `esbuild` bundles it\nimport {\n\tstructuredSerializableReducers,\n\tstructuredSerializableRevivers,\n} from \"../../../miniflare/src/workers/core/devalue\";\n\nfunction structuredSerializableStringify(value: unknown): string {\n\treturn devalue.stringify(value, structuredSerializableReducers);\n}\nfunction structuredSerializableParse(value: string): unknown {\n\treturn devalue.parse(value, structuredSerializableRevivers);\n}\n\n// Mock Service Worker needs this — stub with no-op methods since workerd\n// doesn't provide BroadcastChannel\nglobalThis.BroadcastChannel = class {\n\tconstructor(public name: string) {}\n\tpostMessage(_message: unknown) {}\n\tclose() {}\n\taddEventListener(_type: string, _listener: unknown) {}\n\tremoveEventListener(_type: string, _listener: unknown) {}\n\tonmessage: ((event: unknown) => void) | null = null;\n\tonmessageerror: ((event: unknown) => void) | null = null;\n} as unknown as typeof BroadcastChannel;\n\nlet cwd: string | undefined;\nprocess.cwd = () => {\n\tassert(cwd !== undefined, \"Expected cwd to be set\");\n\treturn cwd;\n};\n\nglobalThis.__console = console;\n\n// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type -- V8 stack trace API requires Function type for Error.captureStackTrace\nfunction getCallerFileName(of: Function): string | null {\n\tconst originalStackTraceLimit = Error.stackTraceLimit;\n\tconst originalPrepareStackTrace = Error.prepareStackTrace;\n\ttry {\n\t\tlet fileName: string | null = null;\n\t\tError.stackTraceLimit = 1;\n\t\tError.prepareStackTrace = (_error, callSites) => {\n\t\t\tfileName = callSites[0]?.getFileName();\n\t\t\treturn \"\";\n\t\t};\n\t\tconst error: { stack?: string } = {};\n\t\tError.captureStackTrace(error, of);\n\t\tvoid error.stack; // Access to generate stack trace\n\t\treturn fileName;\n\t} finally {\n\t\tError.stackTraceLimit = originalStackTraceLimit;\n\t\tError.prepareStackTrace = originalPrepareStackTrace;\n\t}\n}\n\nconst originalSetTimeout = globalThis.setTimeout;\nconst originalClearTimeout = globalThis.clearTimeout;\n\nconst timeoutPromiseResolves = new Map<unknown, () => void>();\nconst monkeypatchedSetTimeout = (...args: Parameters<typeof setTimeout>) => {\n\tconst [callback, delay, ...restArgs] = args;\n\tconst callbackName = args[0]?.name ?? \"\";\n\tconst callerFileName = getCallerFileName(monkeypatchedSetTimeout);\n\tconst fromVitest =\n\t\t/\\/node_modules\\/(\\.pnpm\\/|\\.store\\/)?vitest/.test(callerFileName ?? \"\") ||\n\t\t/\\/packages\\/vitest\\/dist/.test(callerFileName ?? \"\") ||\n\t\t/\\/node_modules\\/(\\.pnpm\\/|\\.store\\/)?@voidzero-dev[+/]vite-plus-test/.test(\n\t\t\tcallerFileName ?? \"\"\n\t\t);\n\n\t// If this `setTimeout()` isn't from Vitest, or has a non-zero delay,\n\t// just call the original function\n\tif (!fromVitest || delay) {\n\t\treturn originalSetTimeout.apply(globalThis, args);\n\t}\n\n\t// HACK: `vitest/dist/vendor/vi.js` attempts to call `setTimeout` when setting\n\t// up global mocks. Unfortunately, the runner Durable Object's IO context\n\t// isn't preserved through `import()` so this fails. To get around this, look\n\t// for the `setTimeout()` call and return a recognisable timeout value that's\n\t// still `number` typed\n\t// (https://github.com/sinonjs/fake-timers/blob/c85ef142837afdbc732b0f73fdba30c3bd037965/src/fake-timers-src.js#L154)\n\tif (callbackName === \"NOOP\") {\n\t\treturn -0.5;\n\t}\n\n\t// Make sure `setTimeout()`s from Vitest without delays are `waitUntil()`ed\n\t// if we're running within an `export default` handler. This ensures all\n\t// `console.log()`s are displayed, as Vitest uses `setTimeout()` for grouping.\n\tlet promiseResolve: (() => void) | undefined;\n\tconst promise = new Promise<void>((resolve) => {\n\t\tpromiseResolve = resolve;\n\t});\n\tassert(promiseResolve !== undefined);\n\tregisterHandlerAndGlobalWaitUntil(promise);\n\tconst id = originalSetTimeout.call(globalThis, () => {\n\t\tpromiseResolve?.();\n\t\tcallback?.(...restArgs);\n\t});\n\ttimeoutPromiseResolves.set(id, promiseResolve);\n\treturn id;\n};\n// @ts-expect-error __promisify__ types only required for Node.js\nglobalThis.setTimeout = monkeypatchedSetTimeout;\n// @ts-expect-error overload types not compatible\nglobalThis.clearTimeout = (...args: Parameters<typeof clearTimeout>) => {\n\tconst id = args[0];\n\tif (id === -0.5) {\n\t\treturn;\n\t}\n\n\t// Make sure we resolve any timeout promises we're clearing\n\t// (e.g. `console.log()`ing twice, the 2nd will clear the timeout set by the\n\t// first, but we'll still be `waitUntil()`ing on the original `Promise`)\n\tconst maybePromiseResolve = timeoutPromiseResolves.get(id);\n\ttimeoutPromiseResolves.delete(id);\n\tmaybePromiseResolve?.();\n\n\treturn originalClearTimeout.apply(globalThis, args);\n};\n\nfunction isDifferentIOContextError(e: unknown) {\n\treturn (\n\t\te instanceof Error &&\n\t\te.message.startsWith(\"Cannot perform I/O on behalf of a different\") // \"request\" or \"Durable Object\"\n\t);\n}\n\nlet patchedFunction = false;\nfunction ensurePatchedFunction(unsafeEval: UnsafeEval) {\n\tif (patchedFunction) {\n\t\treturn;\n\t}\n\tpatchedFunction = true;\n\t// `new Function()` is used by `@vitest/snapshot`\n\tglobalThis.Function = new Proxy(globalThis.Function, {\n\t\tconstruct(_target, args, _newTarget) {\n\t\t\t// `new Function()` and `UnsafeEval#newFunction()` have reversed args\n\t\t\tconst script = args.pop();\n\t\t\treturn unsafeEval.newFunction(script, \"anonymous\", ...args);\n\t\t},\n\t});\n}\n\nfunction applyDefines() {\n\t// Based off `/@vite/env` implementation:\n\t// https://github.com/vitejs/vite/blob/v5.1.4/packages/vite/src/client/env.ts\n\tfor (const [key, value] of Object.entries(defines)) {\n\t\tconst segments = key.split(\".\");\n\t\tlet target = globalThis as Record<string, unknown>;\n\t\tfor (let i = 0; i < segments.length; i++) {\n\t\t\tconst segment = segments[i];\n\t\t\tif (i === segments.length - 1) {\n\t\t\t\ttarget[segment] = value;\n\t\t\t} else {\n\t\t\t\ttarget = (target[segment] ??= {}) as Record<string, unknown>;\n\t\t\t}\n\t\t}\n\t}\n}\n\n// `__VITEST_POOL_WORKERS_RUNNER_DURABLE_OBJECT__` is a singleton\nexport class __VITEST_POOL_WORKERS_RUNNER_DURABLE_OBJECT__ extends DurableObject {\n\tconstructor(_state: DurableObjectState, doEnv: Cloudflare.Env) {\n\t\tsuper(_state, doEnv);\n\t\tvm._setUnsafeEval(doEnv.__VITEST_POOL_WORKERS_UNSAFE_EVAL);\n\t\tensurePatchedFunction(doEnv.__VITEST_POOL_WORKERS_UNSAFE_EVAL);\n\t\tapplyDefines();\n\t}\n\n\tasync handleVitestRunRequest(request: Request): Promise<Response> {\n\t\tassert.strictEqual(request.headers.get(\"Upgrade\"), \"websocket\");\n\t\tconst { 0: poolSocket, 1: poolResponseSocket } = new WebSocketPair();\n\n\t\tconst workerDataHeader = request.headers.get(\"MF-Vitest-Worker-Data\");\n\t\tassert(workerDataHeader);\n\n\t\tconst wd = structuredSerializableParse(workerDataHeader);\n\t\tassert(\n\t\t\twd && typeof wd === \"object\" && \"cwd\" in wd && typeof wd.cwd === \"string\"\n\t\t);\n\n\t\tcwd = wd.cwd;\n\n\t\tconst { init, runBaseTests, setupEnvironment } =\n\t\t\tawait import(\"vitest/worker\");\n\n\t\tpoolSocket.accept();\n\n\t\tinit({\n\t\t\tpost: (response) => {\n\t\t\t\ttry {\n\t\t\t\t\tpoolSocket.send(structuredSerializableStringify(response));\n\t\t\t\t} catch (error) {\n\t\t\t\t\t// If the user called `console.log()` or similar from inside an\n\t\t\t\t\t// `export default { fetch() {} }` handler (via `SELF`/`exports`) or\n\t\t\t\t\t// from inside their own Durable Object, Vitest will try to send an\n\t\t\t\t\t// RPC message from a non-`RunnerObject` I/O context. We'd still like\n\t\t\t\t\t// to send the message, so if we detect a cross-DO I/O error we\n\t\t\t\t\t// resend from the runner object.\n\t\t\t\t\t// (Dynamic `import()` cross-DO errors are handled separately by the\n\t\t\t\t\t// onModuleRunner transport patch below — see #12924.)\n\t\t\t\t\tif (isDifferentIOContextError(error)) {\n\t\t\t\t\t\tconst promise = runInRunnerObject(() => {\n\t\t\t\t\t\t\tpoolSocket.send(structuredSerializableStringify(response));\n\t\t\t\t\t\t}).catch((e) => {\n\t\t\t\t\t\t\t__console.error(\n\t\t\t\t\t\t\t\t\"Error sending to pool inside runner:\",\n\t\t\t\t\t\t\t\te,\n\t\t\t\t\t\t\t\tresponse\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t});\n\t\t\t\t\t\tregisterHandlerAndGlobalWaitUntil(promise);\n\t\t\t\t\t} else {\n\t\t\t\t\t\t__console.error(\"Error sending to pool:\", error, response);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t},\n\t\t\ton: (callback) => {\n\t\t\t\tpoolSocket.addEventListener(\"message\", (m) => {\n\t\t\t\t\tcallback(structuredSerializableParse(m.data));\n\t\t\t\t});\n\t\t\t},\n\t\t\trunTests: (state, traces) => runBaseTests(\"run\", state, traces),\n\t\t\tcollectTests: (state, traces) => runBaseTests(\"collect\", state, traces),\n\t\t\tsetup: setupEnvironment,\n\t\t\t// Patch the module runner's transport so that `invoke()` calls always\n\t\t\t// execute inside the Runner DO's I/O context. Without this, a dynamic\n\t\t\t// `import()` inside an entrypoint handler (which runs in a *different*\n\t\t\t// DO context) fails with \"Cannot perform I/O on behalf of a different\n\t\t\t// Durable Object\". See: https://github.com/cloudflare/workers-sdk/issues/12924\n\t\t\tonModuleRunner(moduleRunner: unknown) {\n\t\t\t\tconst runner = moduleRunner as {\n\t\t\t\t\ttransport?: { invoke?: (...args: unknown[]) => unknown };\n\t\t\t\t};\n\t\t\t\tif (runner.transport?.invoke) {\n\t\t\t\t\tconst originalInvoke = runner.transport.invoke.bind(runner.transport);\n\t\t\t\t\trunner.transport.invoke = (...args: unknown[]) => {\n\t\t\t\t\t\treturn runInRunnerObject(() => originalInvoke(...args));\n\t\t\t\t\t};\n\t\t\t\t} else {\n\t\t\t\t\t__console.warn(\n\t\t\t\t\t\t\"[vitest-pool-workers] Could not patch module runner transport. \" +\n\t\t\t\t\t\t\t\"Dynamic import() inside entrypoint/DO handlers may fail.\"\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t},\n\t\t});\n\n\t\treturn new Response(null, { status: 101, webSocket: poolResponseSocket });\n\t}\n\n\tasync fetch(request: Request): Promise<Response> {\n\t\tconst response = await maybeHandleRunRequest(request, this);\n\t\tif (response !== undefined) {\n\t\t\treturn response;\n\t\t}\n\n\t\treturn this.handleVitestRunRequest(request);\n\t}\n}\n\nexport default createWorkerEntrypointWrapper(\"default\");\n\n// Re-export user export wrappers\nexport * from \"__VITEST_POOL_WORKERS_USER_OBJECT\";\n"],"x_google_ignoreList":[0,1,2,3,4],"m