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

Prevent any type of class instances #46

Merged
merged 3 commits into from
Jan 31, 2024
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
5 changes: 5 additions & 0 deletions .changeset/pretty-files-visit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"deepsignal": patch
---

Reject class instances and everything that is not strictly plain objects or arrays.
6 changes: 1 addition & 5 deletions packages/deepsignal/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,7 @@ const wellKnownSymbols = new Set(
const supported = new Set([Object, Array]);
const shouldProxy = (val: any): boolean => {
if (typeof val !== "object" || val === null) return false;
const isBuiltIn =
typeof val.constructor === "function" &&
val.constructor.name in globalThis &&
(globalThis as any)[val.constructor.name] === val.constructor;
return (!isBuiltIn || supported.has(val.constructor)) && !ignore.has(val);
return supported.has(val.constructor) && !ignore.has(val);
};

/** TYPES **/
Expand Down
28 changes: 14 additions & 14 deletions packages/deepsignal/core/test/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -874,13 +874,20 @@ describe("deepsignal/core", () => {
});
});

describe("built-ins", () => {
it("should throw when trying to deepsignal a built-in", () => {
window.MyClass = class MyClass {};
const obj = new window.MyClass();
describe("unsupported data structures", () => {
it("should throw when trying to deepsignal a class instance", () => {
class MyClass {}
const obj = new MyClass();
expect(() => deepSignal(obj)).to.throw();
});

it("should not wrap a class instance", () => {
class MyClass {}
const obj = new MyClass();
const store = deepSignal({ obj });
expect(store.obj).to.equal(obj);
});

it("should not wrap built-ins in proxies", () => {
window.MyClass = class MyClass {};
const obj = new window.MyClass();
Expand All @@ -903,19 +910,19 @@ describe("deepsignal/core", () => {
expect(store.$b.value).to.equal(2);
});

it("should not wrap Date", () => {
it("should not wrap dates", () => {
const date = new Date();
const store = deepSignal({ date });
expect(store.date).to.equal(date);
});

it("should not wrap RegExp", () => {
it("should not wrap regular expressions", () => {
const regex = new RegExp("");
const store = deepSignal({ regex });
expect(store.regex).to.equal(regex);
});

it("should not wrap Maps", () => {
it("should not wrap Map", () => {
const map = new Map();
const store = deepSignal({ map });
expect(store.map).to.equal(map);
Expand All @@ -926,13 +933,6 @@ describe("deepsignal/core", () => {
const store = deepSignal({ set });
expect(store.set).to.equal(set);
});

it("should not wrap built-ins in proxies", () => {
window.MyClass = class MyClass {};
const obj = new window.MyClass();
const store = deepSignal({ obj });
expect(store.obj).to.equal(obj);
});
});

describe("symbols", () => {
Expand Down
Loading