Skip to content

Commit

Permalink
Fix missing properties on WebSocket MessageEvent (#845)
Browse files Browse the repository at this point in the history
  • Loading branch information
FrederikBolding committed Oct 13, 2022
1 parent 0306a7c commit ed3d650
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
8 changes: 4 additions & 4 deletions packages/execution-environments/jest.config.js
Expand Up @@ -6,10 +6,10 @@ module.exports = {
coverageReporters: ['clover', 'json', 'lcov', 'text', 'json-summary'],
coverageThreshold: {
global: {
branches: 85.08,
functions: 93.23,
lines: 87.26,
statements: 87.45,
branches: 82.88,
functions: 92.53,
lines: 85.97,
statements: 86.15,
},
},
moduleFileExtensions: ['js', 'json', 'jsx', 'ts', 'tsx', 'node'],
Expand Down
38 changes: 24 additions & 14 deletions packages/execution-environments/src/common/endowments/network.ts
@@ -1,4 +1,4 @@
import { allFunctions, withTeardown } from '../utils';
import { allProperties, withTeardown } from '../utils';

type WebSocketCallback = (this: WebSocket, ev: any) => any;

Expand Down Expand Up @@ -396,19 +396,29 @@ const createNetwork = () => {
}

return (e) => {
const functions = allFunctions(e).map((key) => e[key].bind(this));
listener.apply(this, [
{
...e,
...functions,
target: this,
currentTarget: this,
srcElement: this,
ports: [this],
source: null,
composedPath: () => [this],
},
]);
// TODO: Should we migrate this to use a wrapper class?
const properties = [...allProperties(e)]
.filter(([_, key]) => key !== 'constructor')
.reduce<Record<string, any>>((acc, [obj, key]) => {
const stringKey = key.toString();
const descriptor = Reflect.getOwnPropertyDescriptor(obj, key);
if (typeof descriptor?.value === 'function') {
acc[stringKey] = e[stringKey].bind(this);
} else {
acc[stringKey] = e[stringKey];
}
return acc;
}, {});
const event = {
...properties,
target: this,
currentTarget: this,
srcElement: this,
ports: [this],
source: null,
composedPath: () => [this],
};
listener.apply(this, [event]);
};
}

Expand Down

0 comments on commit ed3d650

Please sign in to comment.