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

Avoid polluting the original object in case of Object.create #1797

Merged
merged 4 commits into from
Nov 13, 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/plenty-walls-clap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@whatwg-node/server': patch
---

Avoid polluting the original object in case of \`Object.create\`
80 changes: 11 additions & 69 deletions packages/server/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -527,75 +527,17 @@ export function isolateObject<TIsolatedObject extends object>(
if (waitUntilPromises == null) {
return {} as TIsolatedObject;
}
originalCtx = {} as TIsolatedObject;
}
const extraProps: Partial<TIsolatedObject> = {};
const deletedProps = new Set<string | symbol>();
return new Proxy(originalCtx, {
get(originalCtx, prop) {
if (waitUntilPromises != null && prop === 'waitUntil') {
return function waitUntil(promise: Promise<unknown>) {
waitUntilPromises.push(promise.catch(err => console.error(err)));
};
}
const extraPropVal = (extraProps as any)[prop];
if (extraPropVal != null) {
if (typeof extraPropVal === 'function') {
return extraPropVal.bind(extraProps);
}
return extraPropVal;
}
if (deletedProps.has(prop)) {
return undefined;
}
return (originalCtx as any)[prop];
},
set(_originalCtx, prop, value) {
(extraProps as any)[prop] = value;
return true;
},
has(originalCtx, prop) {
if (waitUntilPromises != null && prop === 'waitUntil') {
return true;
}
if (deletedProps.has(prop)) {
return false;
}
if (prop in extraProps) {
return true;
}
return prop in originalCtx;
},
defineProperty(_originalCtx, prop, descriptor) {
return Reflect.defineProperty(extraProps, prop, descriptor);
},
deleteProperty(_originalCtx, prop) {
if (prop in extraProps) {
return Reflect.deleteProperty(extraProps, prop);
}
deletedProps.add(prop);
return true;
},
ownKeys(originalCtx) {
const extraKeys = Reflect.ownKeys(extraProps);
const originalKeys = Reflect.ownKeys(originalCtx);
const deletedKeys = Array.from(deletedProps);
const allKeys = new Set(
extraKeys.concat(originalKeys.filter(keys => !deletedKeys.includes(keys))),
);
if (waitUntilPromises != null) {
allKeys.add('waitUntil');
}
return Array.from(allKeys);
},
getOwnPropertyDescriptor(originalCtx, prop) {
if (prop in extraProps) {
return Reflect.getOwnPropertyDescriptor(extraProps, prop);
}
if (deletedProps.has(prop)) {
return undefined;
}
return Reflect.getOwnPropertyDescriptor(originalCtx, prop);
return {
waitUntil(promise: Promise<unknown>) {
waitUntilPromises?.push(promise.catch(err => console.error(err)));
},
} as TIsolatedObject;
}
return Object.create(originalCtx, {
waitUntil: {
value(promise: Promise<unknown>) {
waitUntilPromises?.push(promise.catch(err => console.error(err)));
},
},
});
}
Expand Down
34 changes: 34 additions & 0 deletions packages/server/test/server-context.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,40 @@ describe('Server Context', () => {
expect(res.status).toBe(200);
expect(await res.json()).toEqual({ foo: 'bar', bar: 'baz' });
});
it('Do not pollute the original object in case of `Object.create`', async () => {
const serverAdapter = createServerAdapter((_req, context0: any) => {
context0.i = 0;
const context1 = Object.create(context0);
context1.i = 1;
const context2 = Object.create(context0);
context2.i = 2;
return Response.json({
i0: context0.i,
i1: context1.i,
i2: context2.i,
});
});
const res = await serverAdapter.fetch('http://localhost');
const resJson = await res.json();
expect(resJson).toEqual({
i0: 0,
i1: 1,
i2: 2,
});
});
it('retains the prototype in case of `Object.create`', async () => {
class MyContext {}
const serverAdapter = createServerAdapter((_req, context0: MyContext) => {
return Response.json({
isMyContext: context0 instanceof MyContext,
});
});
const res = await serverAdapter.fetch('http://localhost', new MyContext());
const resJson = await res.json();
expect(resJson).toEqual({
isMyContext: true,
});
});
},
{ noLibCurl: true },
);
Expand Down
11 changes: 11 additions & 0 deletions packages/server/test/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { isolateObject } from '../src/utils';

describe('isolateObject', () => {
test('Object.create does not share property assignments', () => {
const origin = isolateObject({});
const a = Object.create(origin);
const b = Object.create(origin);
a.a = 1;
expect(b.a).toEqual(undefined);
});
});
Loading