Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cloudflare internal symbol to resourcetypes #2801

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/workerd/api/tests/global-scope-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -738,5 +738,9 @@ export const toStringTag = {
toString.call(new URLSearchParams()),
'[object URLSearchParams]'
);

const internalFlag = Symbol.for('cloudflare:internal-class');
strictEqual(Headers.prototype[internalFlag], internalFlag);
strictEqual(new Headers()[internalFlag], internalFlag);
},
};
14 changes: 14 additions & 0 deletions src/workerd/jsg/resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -1534,6 +1534,20 @@ class ResourceWrapper {
auto classname = v8StrIntern(isolate, typeName(typeid(T)));

prototype->Set(v8::Symbol::GetToStringTag(isolate), classname, v8::PropertyAttribute::DontEnum);

// Previously, miniflare would use the lack of a Symbol.toStringTag on a class to
// detect a type that came from the runtime. That's obviously a bit problematic because
// Symbol.toStringTag is required for full compliance on standard web platform APIs.
// To help use cases where it is necessary to detect if a class is a runtime class, we
// will add a special symbol to the prototype of the class to indicate. Note that
// because this uses the global symbol registry user code could still mark their own
// classes with this symbol but that's unlikely to be a problem in any practical case.
auto internalMarker =
v8::Symbol::For(isolate, v8StrIntern(isolate, "cloudflare:internal-class"));
prototype->Set(internalMarker, internalMarker,
static_cast<v8::PropertyAttribute>(v8::PropertyAttribute::DontEnum |
v8::PropertyAttribute::DontDelete | v8::PropertyAttribute::ReadOnly));

constructor->SetClassName(classname);

static_assert(kj::isSameType<typename T::jsgThis, T>(),
Expand Down