From ed3d6502dacc63e185dc477e0298c2351133675b Mon Sep 17 00:00:00 2001 From: Frederik Bolding Date: Thu, 13 Oct 2022 18:20:51 +0200 Subject: [PATCH] Fix missing properties on WebSocket MessageEvent (#845) --- .../execution-environments/jest.config.js | 8 ++-- .../src/common/endowments/network.ts | 38 ++++++++++++------- 2 files changed, 28 insertions(+), 18 deletions(-) diff --git a/packages/execution-environments/jest.config.js b/packages/execution-environments/jest.config.js index f112c2ae80..ef008d746d 100644 --- a/packages/execution-environments/jest.config.js +++ b/packages/execution-environments/jest.config.js @@ -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'], diff --git a/packages/execution-environments/src/common/endowments/network.ts b/packages/execution-environments/src/common/endowments/network.ts index 698b741d1a..fc737644c5 100644 --- a/packages/execution-environments/src/common/endowments/network.ts +++ b/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; @@ -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>((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]); }; }