Skip to content

Commit

Permalink
Use symbol to identify internal classes
Browse files Browse the repository at this point in the history
  • Loading branch information
penalosa committed Sep 25, 2024
1 parent 982e446 commit 55b4e4a
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions packages/miniflare/src/workers/core/proxy.worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,20 @@ function objectContainsFunctions(
return false;
}

function isObject(value: unknown) {
return value && typeof value === "object";
function isObject(
value: unknown
): value is Record<string | number | symbol, unknown> {
return !!value && typeof value === "object";
}

function getType(value: unknown) {
return Object.prototype.toString.call(value).slice(8, -1); // `[object <type>]`
}

function isInternal(value: unknown) {
return isObject(value) && value[Symbol.for("cloudflare:internal-class")];
}

type Env = Record<string, unknown> & {
[CoreBindings.DATA_PROXY_SECRET]: ArrayBuffer;
};
Expand All @@ -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);
Expand Down

0 comments on commit 55b4e4a

Please sign in to comment.