From 55b4e4a4deca00cba9e3ec5b9b65ebb101c7267b Mon Sep 17 00:00:00 2001 From: Samuel Macleod Date: Wed, 25 Sep 2024 18:58:31 +0100 Subject: [PATCH] Use symbol to identify internal classes --- .../miniflare/src/workers/core/proxy.worker.ts | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/packages/miniflare/src/workers/core/proxy.worker.ts b/packages/miniflare/src/workers/core/proxy.worker.ts index 99c3e2db1cb1..8e5556aa3c18 100644 --- a/packages/miniflare/src/workers/core/proxy.worker.ts +++ b/packages/miniflare/src/workers/core/proxy.worker.ts @@ -91,14 +91,20 @@ function objectContainsFunctions( return false; } -function isObject(value: unknown) { - return value && typeof value === "object"; +function isObject( + value: unknown +): value is Record { + return !!value && typeof value === "object"; } function getType(value: unknown) { return Object.prototype.toString.call(value).slice(8, -1); // `[object ]` } +function isInternal(value: unknown) { + return isObject(value) && value[Symbol.for("cloudflare:internal-class")]; +} + type Env = Record & { [CoreBindings.DATA_PROXY_SECRET]: ArrayBuffer; }; @@ -119,7 +125,11 @@ export class ProxyServer implements DurableObject { // should only ever return `Object`, as none override `Symbol.toStringTag` // https://tc39.es/ecma262/multipage/fundamental-objects.html#sec-object.prototype.tostring const type = getType(value); - if ((type === "Object" && !isPlainObject(value)) || type === "Promise") { + const isInternalClass = isInternal(value); + if ( + ((type === "Object" || isInternalClass) && !isPlainObject(value)) || + type === "Promise" + ) { const address = this.nextHeapAddress++; this.heap.set(address, value); assert(value !== null);