diff options
author | 2023-07-28 16:55:49 -0700 | |
---|---|---|
committer | 2023-07-28 16:55:49 -0700 | |
commit | d432448ac81b1419a1307d24509ab91a65779205 (patch) | |
tree | bc504665945bbc1ec3d20d7966c02c77f9a2e59d | |
parent | d614fdfaac13346d71ecf24712abaefe8224687d (diff) | |
download | bun-d432448ac81b1419a1307d24509ab91a65779205.tar.gz bun-d432448ac81b1419a1307d24509ab91a65779205.tar.zst bun-d432448ac81b1419a1307d24509ab91a65779205.zip |
fix types and add message channel/port gc test
-rw-r--r-- | packages/bun-types/globals.d.ts | 62 | ||||
-rw-r--r-- | test/js/web/workers/message-channel.test.ts | 18 |
2 files changed, 18 insertions, 62 deletions
diff --git a/packages/bun-types/globals.d.ts b/packages/bun-types/globals.d.ts index bf374adfe..e2ee4699d 100644 --- a/packages/bun-types/globals.d.ts +++ b/packages/bun-types/globals.d.ts @@ -2,7 +2,7 @@ * "blob" is not supported yet */ type BinaryType = "nodebuffer" | "arraybuffer" | "blob"; -type Transferable = ArrayBuffer; +type Transferable = ArrayBuffer | MessagePort; type MessageEventSource = undefined; type Encoding = "utf-8" | "windows-1252" | "utf-16"; type Platform = @@ -391,64 +391,6 @@ declare var MessageChannel: { new (): MessageChannel; }; -/** - * A message received by a target object. - * - * [MDN Reference](https://developer.mozilla.org/docs/Web/API/MessageEvent) - */ -interface MessageEvent<T = any> extends Event { - /** - * Returns the data of the message. - * - * [MDN Reference](https://developer.mozilla.org/docs/Web/API/MessageEvent/data) - */ - readonly data: T; - /** - * Returns the last event ID string, for server-sent events. - * - * [MDN Reference](https://developer.mozilla.org/docs/Web/API/MessageEvent/lastEventId) - */ - readonly lastEventId: string; - /** - * Returns the origin of the message, for server-sent events and cross-document messaging. - * - * [MDN Reference](https://developer.mozilla.org/docs/Web/API/MessageEvent/origin) - */ - readonly origin: string; - /** - * Returns the MessagePort array sent with the message, for cross-document messaging and channel messaging. - * - * [MDN Reference](https://developer.mozilla.org/docs/Web/API/MessageEvent/ports) - */ - readonly ports: ReadonlyArray<MessagePort>; - /** - * Returns the WindowProxy of the source window, for cross-document messaging, and the MessagePort being attached, in the connect event fired at SharedWorkerGlobalScope objects. - * - * [MDN Reference](https://developer.mozilla.org/docs/Web/API/MessageEvent/source) - */ - readonly source: MessageEventSource | null; - /** - * @deprecated - * - * [MDN Reference](https://developer.mozilla.org/docs/Web/API/MessageEvent/initMessageEvent) - */ - initMessageEvent( - type: string, - bubbles?: boolean, - cancelable?: boolean, - data?: any, - origin?: string, - lastEventId?: string, - source?: MessageEventSource | null, - ports?: MessagePort[], - ): void; -} - -declare var MessageEvent: { - prototype: MessageEvent; - new <T>(type: string, eventInitDict?: MessageEventInit<T>): MessageEvent<T>; -}; - interface MessagePortEventMap { message: MessageEvent; messageerror: MessageEvent; @@ -2037,6 +1979,8 @@ interface MessageEvent<T = any> extends Event { readonly lastEventId: string; /** Returns the origin of the message, for server-sent events and cross-document messaging. */ readonly origin: string; + /** Returns the MessagePort array sent with the message, for cross-document messaging and channel messaging. */ + readonly ports: ReadonlyArray<MessagePort>; readonly source: MessageEventSource; /** @deprecated */ initMessageEvent( diff --git a/test/js/web/workers/message-channel.test.ts b/test/js/web/workers/message-channel.test.ts index 7d3e3cc25..40c74e097 100644 --- a/test/js/web/workers/message-channel.test.ts +++ b/test/js/web/workers/message-channel.test.ts @@ -118,7 +118,7 @@ test("message channel created on other thread", done => { worker.onmessage = e => { expect(e.data).toBeInstanceOf(MessagePort); var port = e.data; - port.onmessage = e => { + port.onmessage = (e: MessageEvent) => { expect(e.data).toEqual("done!"); done(); }; @@ -144,9 +144,11 @@ test("many message channels", done => { channel.port1.postMessage("entangled port", [channel.port2]); }).toThrow(); expect(() => { + // @ts-ignore channel.port1.postMessage("null port", [channel3.port1, null, channel3.port2]); }).toThrow(); expect(() => { + // @ts-ignore channel.port1.postMessage("notAPort", [channel3.port1, {}, channel3.port2]); }).toThrow(); expect(() => { @@ -159,15 +161,17 @@ test("many message channels", done => { }).not.toThrow(); expect(() => { + // @ts-ignore channel.port1.postMessage("notAnArray", "foo"); }).toThrow(); expect(() => { + // @ts-ignore channel.port1.postMessage("notASequence", [{ length: 3 }]); }).toThrow(); // Should not crash (we should figure out that the array contains undefined // entries). - var largePortArray = []; + var largePortArray: MessagePort[] = []; largePortArray[1234567890] = channel4.port1; expect(() => { channel.port1.postMessage("largeSequence", largePortArray); @@ -175,7 +179,7 @@ test("many message channels", done => { channel.port1.postMessage("done"); - function testTransfers(done) { + function testTransfers(done: any) { var channel0 = new MessageChannel(); var c1 = new MessageChannel(); @@ -252,3 +256,11 @@ test("many message channels", done => { } }; }); + +test("gc", () => { + for (let i = 0; i < 1000; i++) { + var e = new MessageChannel(); + e.port1; + e.port2; + } +}); |