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

[miniflare] fix: make the magic proxy able to proxy objects containing functions #5670

Merged
merged 4 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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/wet-taxis-share.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"miniflare": patch
---

fix: make the magic proxy able to proxy objects containing functions
RamIdeas marked this conversation as resolved.
Show resolved Hide resolved
25 changes: 25 additions & 0 deletions packages/miniflare/src/workers/core/proxy.worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,37 @@ function isPlainObject(value: unknown) {
if(value?.constructor?.name === 'RpcStub') {
return false;
}
if(isObject(value)) {
const valueAsRecord = value as Record<string, unknown>;
if (objectContainsFunctions(valueAsRecord)){
return false;
}
}
return (
proto === Object.prototype ||
proto === null ||
Object.getOwnPropertyNames(proto).sort().join("\0") === objectProtoNames
);
}
function objectContainsFunctions(obj: Record<string, unknown>): boolean {
for(const [, entry] of Object.entries(obj)) {
if(typeof entry === 'function') {
return true;
}
if(isObject(entry)) {
if(objectContainsFunctions(entry as Record<string, unknown>)) {
return false;
}
}
}

return false;
}

function isObject(value: unknown) {
return value && typeof value === 'object';
}

function getType(value: unknown) {
return Object.prototype.toString.call(value).slice(8, -1); // `[object <type>]`
}
Expand Down
105 changes: 100 additions & 5 deletions packages/miniflare/test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1378,14 +1378,109 @@ test("Miniflare: getBindings() returns wrapped bindings", async (t) => {

interface Env {
Greeter: {
sayHello: (str: string) => string,
},
};
sayHello: (str: string) => string;
};
}
const { Greeter } = await mf.getBindings<Env>();

const helloWorld = Greeter.sayHello("World");

t.is(helloWorld, "Hello World");
});
test("Miniflare: getBindings() handles wrapped bindings returning objects containing functions", async (t) => {
const mf = new Miniflare({
workers: [
{
wrappedBindings: {
Greeter: {
scriptName: "greeter-obj-implementation",
},
},
modules: true,
script: "",
},
{
modules: true,
name: "greeter-obj-implementation",
script: `
export default function (env) {
const objWithFunction = {
greeting: "Hello",
sayHello(name) {
return this.greeting + ' ' + name;
}
};
return objWithFunction;
}
`,
},
],
});
t.teardown(() => mf.dispose());

interface Env {
Greeter: {
greeting: string;
sayHello: (str: string) => string;
};
}
const { Greeter } = await mf.getBindings<Env>();

const helloWorld = Greeter.sayHello("World");

t.is(helloWorld, "Hello World");
t.is(Greeter.greeting, "Hello");
});
test("Miniflare: getBindings() handles wrapped bindings returning objects containing nested functions", async (t) => {
const mf = new Miniflare({
workers: [
{
wrappedBindings: {
Greeter: {
scriptName: "greeter-obj-implementation",
},
},
modules: true,
script: "",
},
{
modules: true,
name: "greeter-obj-implementation",
script: `
export default function (env) {
const objWithFunction = {
obj: {
obj1: {
obj2: {
sayHello: (name) => "Hello " + name + " from a nested function"
}
}
}
};
return objWithFunction;
}
`,
},
],
});
t.teardown(() => mf.dispose());

interface Env {
Greeter: {
obj: {
obj1: {
obj2: {
sayHello: (str: string) => string;
};
};
};
};
}
const { Greeter } = await mf.getBindings<Env>();

const helloWorld = Greeter.sayHello('World');
const helloWorld = Greeter.obj.obj1.obj2.sayHello("World");

t.is(helloWorld, 'Hello World');
t.is(helloWorld, "Hello World from a nested function");
});
test("Miniflare: getWorker() allows dispatching events directly", async (t) => {
const mf = new Miniflare({
Expand Down
Loading