diff options
author | 2023-08-04 12:56:34 -0700 | |
---|---|---|
committer | 2023-08-04 12:56:34 -0700 | |
commit | 63d265780f122f8390a71b3a15cea4c71b540b8f (patch) | |
tree | ce24d797c70e981712ef9fbdd39d72d5cf2a5d90 | |
parent | 18a2e18ae1959d0198e8262cbc5bc8a1760981fe (diff) | |
download | bun-63d265780f122f8390a71b3a15cea4c71b540b8f.tar.gz bun-63d265780f122f8390a71b3a15cea4c71b540b8f.tar.zst bun-63d265780f122f8390a71b3a15cea4c71b540b8f.zip |
Fix types (#3963)
* Fix types
* Add ws module
* Add *.toml module declaration
* Clean up
-rwxr-xr-x | bun.lockb | bin | 73269 -> 72575 bytes | |||
-rw-r--r-- | docs/nav.ts | 3 | ||||
-rw-r--r-- | package.json | 1 | ||||
-rw-r--r-- | packages/bun-types/diagnostics_channel.d.ts | 3 | ||||
-rw-r--r-- | packages/bun-types/globals.d.ts | 289 | ||||
-rw-r--r-- | packages/bun-types/index.d.ts | 5 | ||||
-rw-r--r-- | packages/bun-types/tests/globals.test-d.ts | 3 | ||||
-rw-r--r-- | packages/bun-types/tests/toml.test-d.ts | 4 | ||||
-rw-r--r-- | packages/bun-types/tests/worker.test-d.ts | 4 | ||||
-rw-r--r-- | packages/bun-types/tests/ws.test-d.ts | 11 | ||||
-rw-r--r-- | packages/bun-types/tsconfig.json | 7 | ||||
-rw-r--r-- | packages/bun-types/vm.d.ts (renamed from packages/bun-types/node-vm.d.ts) | 0 | ||||
-rw-r--r-- | packages/bun-types/worker_threads.d.ts | 614 | ||||
-rw-r--r-- | packages/bun-types/ws.d.ts | 222 |
14 files changed, 892 insertions, 274 deletions
Binary files differ diff --git a/docs/nav.ts b/docs/nav.ts index 55ef9a929..9eadf59be 100644 --- a/docs/nav.ts +++ b/docs/nav.ts @@ -38,10 +38,11 @@ export default { page("templates", "Templates", { description: "Hit the ground running with one of Bun's official templates, or download a template from GitHub.", }), - page("templates", "Guides", { + page("guides", "Guides", { description: "A set of walkthrough guides and code snippets for performing common tasks with Bun", href: "/guides", }), + // page("typescript", "TypeScript"), // divider("CLI"), diff --git a/package.json b/package.json index c8ff73ac3..5cb6cf8e7 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,6 @@ }, "devDependencies": { "@types/react": "^18.0.25", - "@types/ws": "^8.5.5", "@typescript-eslint/eslint-plugin": "^5.31.0", "@typescript-eslint/parser": "^5.31.0", "bun-webkit": "0.0.1-9d9172d3242b40f16fa980ead750dc9ab248065f" diff --git a/packages/bun-types/diagnostics_channel.d.ts b/packages/bun-types/diagnostics_channel.d.ts index 0f780ca29..afc63c922 100644 --- a/packages/bun-types/diagnostics_channel.d.ts +++ b/packages/bun-types/diagnostics_channel.d.ts @@ -1,4 +1,3 @@ -type AsyncLocalStorage<T> = import("async_hooks").AsyncLocalStorage<T>; /** * The `node:diagnostics_channel` module provides an API to create named channels * to report arbitrary message data for diagnostics purposes. @@ -24,6 +23,8 @@ type AsyncLocalStorage<T> = import("async_hooks").AsyncLocalStorage<T>; * @see [source](https://github.com/nodejs/node/blob/v20.2.0/lib/diagnostics_channel.js) */ declare module "diagnostics_channel" { + import { AsyncLocalStorage } from "async_hooks"; + // type AsyncLocalStorage<T> = import("async_hooks").AsyncLocalStorage<T>; type ChannelListener = (message: unknown, name: string | symbol) => void; /** * Check if there are active subscribers to the named channel. This is helpful if diff --git a/packages/bun-types/globals.d.ts b/packages/bun-types/globals.d.ts index 685339a02..bef79f57b 100644 --- a/packages/bun-types/globals.d.ts +++ b/packages/bun-types/globals.d.ts @@ -366,172 +366,15 @@ declare function structuredClone<T>( options?: StructuredSerializeOptions, ): T; -/** - * This Channel Messaging API interface allows us to create a new message channel and send data through it via its two MessagePort properties. - * - * [MDN Reference](https://developer.mozilla.org/docs/Web/API/MessageChannel) - */ -interface MessageChannel { - /** - * Returns the first MessagePort object. - * - * [MDN Reference](https://developer.mozilla.org/docs/Web/API/MessageChannel/port1) - */ - readonly port1: MessagePort; - /** - * Returns the second MessagePort object. - * - * [MDN Reference](https://developer.mozilla.org/docs/Web/API/MessageChannel/port2) - */ - readonly port2: MessagePort; -} - -declare var MessageChannel: { - prototype: MessageChannel; - new (): MessageChannel; -}; - -interface MessagePortEventMap { - message: MessageEvent; - messageerror: MessageEvent; -} - -/** - * This Channel Messaging API interface represents one of the two ports of a MessageChannel, allowing messages to be sent from one port and listening out for them arriving at the other. - * - * [MDN Reference](https://developer.mozilla.org/docs/Web/API/MessagePort) - */ -interface MessagePort extends EventTarget { - /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/MessagePort/message_event) */ - onmessage: ((this: MessagePort, ev: MessageEvent) => any) | null; - /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/MessagePort/messageerror_event) */ - onmessageerror: ((this: MessagePort, ev: MessageEvent) => any) | null; - /** - * Disconnects the port, so that it is no longer active. - * - * [MDN Reference](https://developer.mozilla.org/docs/Web/API/MessagePort/close) - */ - close(): void; - /** - * Posts a message through the channel. Objects listed in transfer are transferred, not just cloned, meaning that they are no longer usable on the sending side. - * - * Throws a "DataCloneError" DOMException if transfer contains duplicate objects or port, or if message could not be cloned. - * - * [MDN Reference](https://developer.mozilla.org/docs/Web/API/MessagePort/postMessage) - */ - postMessage(message: any, transfer: Transferable[]): void; - postMessage(message: any, options?: StructuredSerializeOptions): void; - /** - * Begins dispatching messages received on the port. - * - * [MDN Reference](https://developer.mozilla.org/docs/Web/API/MessagePort/start) - */ - start(): void; - addEventListener<K extends keyof MessagePortEventMap>( - type: K, - listener: (this: MessagePort, ev: MessagePortEventMap[K]) => any, - options?: boolean | AddEventListenerOptions, - ): void; - addEventListener( - type: string, - listener: EventListenerOrEventListenerObject, - options?: boolean | AddEventListenerOptions, - ): void; - removeEventListener<K extends keyof MessagePortEventMap>( - type: K, - listener: (this: MessagePort, ev: MessagePortEventMap[K]) => any, - options?: boolean | EventListenerOptions, - ): void; - removeEventListener( - type: string, - listener: EventListenerOrEventListenerObject, - options?: boolean | EventListenerOptions, - ): void; - - /** - * Keep the process alive until the MessagePort is closed or `unref`'d - */ - ref(): void; - /** - * Undo a previous `ref()` - */ - unref(): void; - /** - * Returns true if the MessagePort is `ref`'d - */ - hasRef(): boolean; -} - -declare var MessagePort: { - prototype: MessagePort; - new (): MessagePort; -}; - -interface BroadcastChannelEventMap { - message: MessageEvent; - messageerror: MessageEvent; -} - -/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/BroadcastChannel) */ -interface BroadcastChannel extends EventTarget { - /** - * Returns the channel name (as passed to the constructor). - * - * [MDN Reference](https://developer.mozilla.org/docs/Web/API/BroadcastChannel/name) - */ - readonly name: string; - /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/BroadcastChannel/message_event) */ - onmessage: ((this: BroadcastChannel, ev: MessageEvent) => any) | null; - /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/BroadcastChannel/messageerror_event) */ - onmessageerror: ((this: BroadcastChannel, ev: MessageEvent) => any) | null; - /** - * Closes the BroadcastChannel object, opening it up to garbage collection. - * - * [MDN Reference](https://developer.mozilla.org/docs/Web/API/BroadcastChannel/close) - */ - close(): void; - /** - * Sends the given message to other BroadcastChannel objects set up for this channel. Messages can be structured objects, e.g. nested objects and arrays. - * - * [MDN Reference](https://developer.mozilla.org/docs/Web/API/BroadcastChannel/postMessage) - */ - postMessage(message: any): void; - addEventListener<K extends keyof BroadcastChannelEventMap>( - type: K, - listener: (this: BroadcastChannel, ev: BroadcastChannelEventMap[K]) => any, - options?: boolean | AddEventListenerOptions, - ): void; - addEventListener( - type: string, - listener: EventListenerOrEventListenerObject, - options?: boolean | AddEventListenerOptions, - ): void; - removeEventListener<K extends keyof BroadcastChannelEventMap>( - type: K, - listener: (this: BroadcastChannel, ev: BroadcastChannelEventMap[K]) => any, - options?: boolean | EventListenerOptions, - ): void; - removeEventListener( - type: string, - listener: EventListenerOrEventListenerObject, - options?: boolean | EventListenerOptions, - ): void; - - /** - * Keep the process alive until the BroadcastChannel is closed or `unref`'d. - * BroadcastChannel is `ref`'d by default. - */ - ref(): void; - /** - * Undo a previous `ref()` - */ - unref(): void; -} +declare var MessagePort: typeof import("worker_threads").MessagePort; +declare type MessagePort = import("worker_threads").MessagePort; +declare var MessageChannel: typeof import("worker_threads").MessageChannel; +declare type MessageChannel = import("worker_threads").MessageChannel; +declare var BroadcastChannel: typeof import("worker_threads").BroadcastChannel; +declare type BroadcastChannel = import("worker_threads").BroadcastChannel; -declare var BroadcastChannel: { - prototype: BroadcastChannel; - new (name: string): BroadcastChannel; -}; +declare var Worker: typeof import("worker_threads").Worker; +declare type Worker = typeof import("worker_threads").Worker; interface EncodeIntoResult { /** @@ -866,14 +709,7 @@ declare var FormData: { new (): FormData; }; -declare class Blob implements BlobInterface { - /** - * Create a new [Blob](https://developer.mozilla.org/en-US/docs/Web/API/Blob) - * - * @param `parts` - An array of strings, numbers, BufferSource, or [Blob](https://developer.mozilla.org/en-US/docs/Web/API/Blob) objects - * @param `options` - An object containing properties to be added to the [Blob](https://developer.mozilla.org/en-US/docs/Web/API/Blob) - */ - constructor(parts?: BlobPart[], options?: BlobPropertyBag); +declare interface Blob { /** * Create a new view **without 🚫 copying** the underlying data. * @@ -949,6 +785,16 @@ declare class Blob implements BlobInterface { type: string; readonly size: number; } +declare var Blob: { + prototype: Blob; + /** + * Create a new [Blob](https://developer.mozilla.org/en-US/docs/Web/API/Blob) + * + * @param `parts` - An array of strings, numbers, BufferSource, or [Blob](https://developer.mozilla.org/en-US/docs/Web/API/Blob) objects + * @param `options` - An object containing properties to be added to the [Blob](https://developer.mozilla.org/en-US/docs/Web/API/Blob) + */ + new (parts?: BlobPart[], options?: BlobPropertyBag): Blob; +}; interface ResponseInit { headers?: HeadersInit; @@ -1635,24 +1481,6 @@ declare class ShadowRealm { evaluate(sourceText: string): any; } -interface Blob { - /** - * Read the contents of the [`Blob`](https://developer.mozilla.org/en-US/docs/Web/API/Blob) as a JSON object - * @warn in browsers, this function is only available for `Response` and `Request` - */ - json(): Promise<any>; - /** - * Read the [`Blob`](https://developer.mozilla.org/en-US/docs/Web/API/Blob) as a UTF-8 string - * @link https://developer.mozilla.org/en-US/docs/Web/API/Blob/text - */ - text(): Promise<string>; - /** - * Read the [`Blob`](https://developer.mozilla.org/en-US/docs/Web/API/Blob) as an ArrayBuffer object - * @link https://developer.mozilla.org/en-US/docs/Web/API/Blob/arrayBuffer - */ - arrayBuffer(): Promise<ArrayBuffer>; -} - declare var performance: { /** * Milliseconds since Bun.js started @@ -3663,48 +3491,17 @@ declare module "*.txt" { export = text; } +declare module "*.toml" { + var contents: unknown; + export = contents; +} + interface EventSourceEventMap { error: Event; message: MessageEvent; open: Event; } -interface Worker extends EventTarget { - onerror: ((this: Worker, ev: ErrorEvent) => any) | null; - onmessage: ((this: Worker, ev: MessageEvent) => any) | null; - onmessageerror: ((this: Worker, ev: MessageEvent) => any) | null; - - addEventListener<K extends keyof WorkerEventMap>( - type: K, - listener: (this: Worker, ev: WorkerEventMap[K]) => any, - options?: boolean | AddEventListenerOptions, - ): void; - - removeEventListener<K extends keyof WorkerEventMap>( - type: K, - listener: (this: Worker, ev: WorkerEventMap[K]) => any, - options?: boolean | EventListenerOptions, - ): void; - - terminate(): void; - - postMessage(message: any, transfer?: Transferable[]): void; - - /** - * Keep the process alive until the worker is terminated or `unref`'d - */ - ref(): void; - /** - * Undo a previous `ref()` - */ - unref(): void; - - /** - * Unique per-process thread ID. Main thread ID is always `0`. - */ - readonly threadId: number; -} - /** * Post a message to the parent thread. * @@ -3712,44 +3509,6 @@ interface Worker extends EventTarget { */ declare function postMessage(message: any, transfer?: Transferable[]): void; -declare var Worker: { - prototype: Worker; - new (stringUrl: string | URL, options?: WorkerOptions): Worker; -}; - -interface WorkerOptions { - name?: string; - - /** - * Use less memory, but make the worker slower. - * - * Internally, this sets the heap size configuration in JavaScriptCore to be - * the small heap instead of the large heap. - */ - smol?: boolean; - - /** - * When `true`, the worker will keep the parent thread alive until the worker is terminated or `unref`'d. - * When `false`, the worker will not keep the parent thread alive. - * - * By default, this is `false`. - */ - ref?: boolean; - - /** - * Does nothing in Bun - */ - type?: string; -} - -interface WorkerEventMap { - message: MessageEvent; - messageerror: MessageEvent; - error: ErrorEvent; - open: Event; - close: Event; -} - interface EventSource extends EventTarget { onerror: ((this: EventSource, ev: ErrorEvent) => any) | null; onmessage: ((this: EventSource, ev: MessageEvent) => any) | null; diff --git a/packages/bun-types/index.d.ts b/packages/bun-types/index.d.ts index 981a4dbc7..e5f866c21 100644 --- a/packages/bun-types/index.d.ts +++ b/packages/bun-types/index.d.ts @@ -6,6 +6,7 @@ /// <reference no-default-lib="true" /> /// <reference lib="esnext" /> /// <reference path="./assert.d.ts" /> +/// <reference path="./async_hooks.d.ts" /> /// <reference path="./buffer.d.ts" /> /// <reference path="./bun-test.d.ts" /> /// <reference path="./bun.d.ts" /> @@ -13,6 +14,7 @@ /// <reference path="./console.d.ts" /> /// <reference path="./constants.d.ts" /> /// <reference path="./crypto.d.ts" /> +/// <reference path="./diagnostics_channel.d.ts" /> /// <reference path="./dns.d.ts" /> /// <reference path="./dns/promises.d.ts" /> /// <reference path="./domain.d.ts" /> @@ -42,4 +44,7 @@ /// <reference path="./tty.d.ts" /> /// <reference path="./url.d.ts" /> /// <reference path="./util.d.ts" /> +/// <reference path="./vm.d.ts" /> +/// <reference path="./worker_threads.d.ts" /> +/// <reference path="./ws.d.ts" /> /// <reference path="./zlib.d.ts" /> diff --git a/packages/bun-types/tests/globals.test-d.ts b/packages/bun-types/tests/globals.test-d.ts index 64b37be13..a5441f201 100644 --- a/packages/bun-types/tests/globals.test-d.ts +++ b/packages/bun-types/tests/globals.test-d.ts @@ -93,3 +93,6 @@ new Request("", { method: "POST" }); Bun.sleepSync(1); // sleep for 1 ms (not recommended) await Bun.sleep(1); // sleep for 1 ms (recommended) + +Blob; +WebSocket; diff --git a/packages/bun-types/tests/toml.test-d.ts b/packages/bun-types/tests/toml.test-d.ts new file mode 100644 index 000000000..eaf20e782 --- /dev/null +++ b/packages/bun-types/tests/toml.test-d.ts @@ -0,0 +1,4 @@ +import { expectType } from "tsd"; +import data from "../../../bunfig.toml"; + +expectType<unknown>(data); diff --git a/packages/bun-types/tests/worker.test-d.ts b/packages/bun-types/tests/worker.test-d.ts index 65cb82c5b..8a2ec7883 100644 --- a/packages/bun-types/tests/worker.test-d.ts +++ b/packages/bun-types/tests/worker.test-d.ts @@ -1,7 +1,7 @@ import { Worker } from "node:worker_threads"; -const workerthread = new Worker("./worker.js"); - +const _workerthread = new Worker("./worker.js"); +_workerthread; const worker = new Worker("./worker.ts"); worker.addEventListener("message", (event: MessageEvent) => { console.log("Message from worker:", event.data); diff --git a/packages/bun-types/tests/ws.test-d.ts b/packages/bun-types/tests/ws.test-d.ts new file mode 100644 index 000000000..fdc4072af --- /dev/null +++ b/packages/bun-types/tests/ws.test-d.ts @@ -0,0 +1,11 @@ +import { WebSocket, WebSocketServer } from "ws"; + +const ws = new WebSocket("ws://www.host.com/path"); + +ws.send("asdf"); + +const wss = new WebSocketServer({ + port: 8080, + perMessageDeflate: false, +}); +wss; diff --git a/packages/bun-types/tsconfig.json b/packages/bun-types/tsconfig.json index e346b31b4..ad946c2e0 100644 --- a/packages/bun-types/tsconfig.json +++ b/packages/bun-types/tsconfig.json @@ -1,8 +1,6 @@ { "compilerOptions": { - "lib": [ - "ESNext" - ], + "lib": ["ESNext"], "skipLibCheck": false, "strict": true, "target": "esnext", @@ -10,7 +8,8 @@ "moduleResolution": "node", "allowSyntheticDefaultImports": true, "disableSolutionSearching": true, - "noUnusedLocals": true + "noUnusedLocals": true, + "outDir": "build" }, "exclude": [ "dist", diff --git a/packages/bun-types/node-vm.d.ts b/packages/bun-types/vm.d.ts index 649abc037..649abc037 100644 --- a/packages/bun-types/node-vm.d.ts +++ b/packages/bun-types/vm.d.ts diff --git a/packages/bun-types/worker_threads.d.ts b/packages/bun-types/worker_threads.d.ts new file mode 100644 index 000000000..49f844ab0 --- /dev/null +++ b/packages/bun-types/worker_threads.d.ts @@ -0,0 +1,614 @@ +/** + * The `worker_threads` module enables the use of threads that execute JavaScript + * in parallel. To access it: + * + * ```js + * const worker = require('worker_threads'); + * ``` + * + * Workers (threads) are useful for performing CPU-intensive JavaScript operations. + * They do not help much with I/O-intensive work. The Node.js built-in + * asynchronous I/O operations are more efficient than Workers can be. + * + * Unlike `child_process` or `cluster`, `worker_threads` can share memory. They do + * so by transferring `ArrayBuffer` instances or sharing `SharedArrayBuffer`instances. + * + * ```js + * const { + * Worker, isMainThread, parentPort, workerData + * } = require('worker_threads'); + * + * if (isMainThread) { + * module.exports = function parseJSAsync(script) { + * return new Promise((resolve, reject) => { + * const worker = new Worker(__filename, { + * workerData: script + * }); + * worker.on('message', resolve); + * worker.on('error', reject); + * worker.on('exit', (code) => { + * if (code !== 0) + * reject(new Error(`Worker stopped with exit code ${code}`)); + * }); + * }); + * }; + * } else { + * const { parse } = require('some-js-parsing-library'); + * const script = workerData; + * parentPort.postMessage(parse(script)); + * } + * ``` + * + * The above example spawns a Worker thread for each `parseJSAsync()` call. In + * practice, use a pool of Workers for these kinds of tasks. Otherwise, the + * overhead of creating Workers would likely exceed their benefit. + * + * When implementing a worker pool, use the `AsyncResource` API to inform + * diagnostic tools (e.g. to provide asynchronous stack traces) about the + * correlation between tasks and their outcomes. See `"Using AsyncResource for a Worker thread pool"` in the `async_hooks` documentation for an example implementation. + * + * Worker threads inherit non-process-specific options by default. Refer to `Worker constructor options` to know how to customize worker thread options, + * specifically `argv` and `execArgv` options. + * @see [source](https://github.com/nodejs/node/blob/v18.0.0/lib/worker_threads.js) + */ +declare module "worker_threads" { + // import { Blob } from "node:buffer"; + import { Context } from "node:vm"; + import { EventEmitter } from "node:events"; + // import { EventLoopUtilityFunction } from "node:perf_hooks"; + // import { FileHandle } from "node:fs/promises"; + // import { Readable, Writable } from "node:stream"; + import { URL } from "node:url"; + // import { X509Certificate } from "node:crypto"; + const isMainThread: boolean; + const parentPort: null | MessagePort; + const resourceLimits: ResourceLimits; + const SHARE_ENV: unique symbol; + const threadId: number; + const workerData: any; + + // interface WorkerPerformance { + // eventLoopUtilization: EventLoopUtilityFunction; + // } + type TransferListItem = + | ArrayBuffer + | MessagePort + // | FileHandle + // | X509Certificate + | Blob; + /** + * Instances of the `worker.MessagePort` class represent one end of an + * asynchronous, two-way communications channel. It can be used to transfer + * structured data, memory regions and other `MessagePort`s between different `Worker` s. + * + * This implementation matches [browser `MessagePort`](https://developer.mozilla.org/en-US/docs/Web/API/MessagePort) s. + * @since v10.5.0 + */ + class MessagePort extends EventEmitter { + /** + * Disables further sending of messages on either side of the connection. + * This method can be called when no further communication will happen over this`MessagePort`. + * + * The `'close' event` is emitted on both `MessagePort` instances that + * are part of the channel. + * @since v10.5.0 + */ + close(): void; + /** + * Sends a JavaScript value to the receiving side of this channel.`value` is transferred in a way which is compatible with + * the [HTML structured clone algorithm](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm). + * + * In particular, the significant differences to `JSON` are: + * + * * `value` may contain circular references. + * * `value` may contain instances of builtin JS types such as `RegExp`s,`BigInt`s, `Map`s, `Set`s, etc. + * * `value` may contain typed arrays, both using `ArrayBuffer`s + * and `SharedArrayBuffer`s. + * * `value` may contain [`WebAssembly.Module`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/Module) instances. + * * `value` may not contain native (C++-backed) objects other than: + * + * ```js + * const { MessageChannel } = require('worker_threads'); + * const { port1, port2 } = new MessageChannel(); + * + * port1.on('message', (message) => console.log(message)); + * + * const circularData = {}; + * circularData.foo = circularData; + * // Prints: { foo: [Circular] } + * port2.postMessage(circularData); + * ``` + * + * `transferList` may be a list of [`ArrayBuffer`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer), `MessagePort` and `FileHandle` objects. + * After transferring, they are not usable on the sending side of the channel + * anymore (even if they are not contained in `value`). Unlike with `child processes`, transferring handles such as network sockets is currently + * not supported. + * + * If `value` contains [`SharedArrayBuffer`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer) instances, those are accessible + * from either thread. They cannot be listed in `transferList`. + * + * `value` may still contain `ArrayBuffer` instances that are not in`transferList`; in that case, the underlying memory is copied rather than moved. + * + * ```js + * const { MessageChannel } = require('worker_threads'); + * const { port1, port2 } = new MessageChannel(); + * + * port1.on('message', (message) => console.log(message)); + * + * const uint8Array = new Uint8Array([ 1, 2, 3, 4 ]); + * // This posts a copy of `uint8Array`: + * port2.postMessage(uint8Array); + * // This does not copy data, but renders `uint8Array` unusable: + * port2.postMessage(uint8Array, [ uint8Array.buffer ]); + * + * // The memory for the `sharedUint8Array` is accessible from both the + * // original and the copy received by `.on('message')`: + * const sharedUint8Array = new Uint8Array(new SharedArrayBuffer(4)); + * port2.postMessage(sharedUint8Array); + * + * // This transfers a freshly created message port to the receiver. + * // This can be used, for example, to create communication channels between + * // multiple `Worker` threads that are children of the same parent thread. + * const otherChannel = new MessageChannel(); + * port2.postMessage({ port: otherChannel.port1 }, [ otherChannel.port1 ]); + * ``` + * + * The message object is cloned immediately, and can be modified after + * posting without having side effects. + * + * For more information on the serialization and deserialization mechanisms + * behind this API, see the `serialization API of the v8 module`. + * @since v10.5.0 + */ + postMessage( + value: any, + transferList?: ReadonlyArray<TransferListItem>, + ): void; + /** + * Opposite of `unref()`. Calling `ref()` on a previously `unref()`ed port does _not_ let the program exit if it's the only active handle left (the default + * behavior). If the port is `ref()`ed, calling `ref()` again has no effect. + * + * If listeners are attached or removed using `.on('message')`, the port + * is `ref()`ed and `unref()`ed automatically depending on whether + * listeners for the event exist. + * @since v10.5.0 + */ + ref(): void; + /** + * Calling `unref()` on a port allows the thread to exit if this is the only + * active handle in the event system. If the port is already `unref()`ed calling`unref()` again has no effect. + * + * If listeners are attached or removed using `.on('message')`, the port is`ref()`ed and `unref()`ed automatically depending on whether + * listeners for the event exist. + * @since v10.5.0 + */ + unref(): void; + /** + * Starts receiving messages on this `MessagePort`. When using this port + * as an event emitter, this is called automatically once `'message'`listeners are attached. + * + * This method exists for parity with the Web `MessagePort` API. In Node.js, + * it is only useful for ignoring messages when no event listener is present. + * Node.js also diverges in its handling of `.onmessage`. Setting it + * automatically calls `.start()`, but unsetting it lets messages queue up + * until a new handler is set or the port is discarded. + * @since v10.5.0 + */ + start(): void; + addListener(event: "close", listener: () => void): this; + addListener(event: "message", listener: (value: any) => void): this; + addListener(event: "messageerror", listener: (error: Error) => void): this; + addListener( + event: string | symbol, + listener: (...args: any[]) => void, + ): this; + emit(event: "close"): boolean; + emit(event: "message", value: any): boolean; + emit(event: "messageerror", error: Error): boolean; + emit(event: string | symbol, ...args: any[]): boolean; + on(event: "close", listener: () => void): this; + on(event: "message", listener: (value: any) => void): this; + on(event: "messageerror", listener: (error: Error) => void): this; + on(event: string | symbol, listener: (...args: any[]) => void): this; + once(event: "close", listener: () => void): this; + once(event: "message", listener: (value: any) => void): this; + once(event: "messageerror", listener: (error: Error) => void): this; + once(event: string | symbol, listener: (...args: any[]) => void): this; + prependListener(event: "close", listener: () => void): this; + prependListener(event: "message", listener: (value: any) => void): this; + prependListener( + event: "messageerror", + listener: (error: Error) => void, + ): this; + prependListener( + event: string | symbol, + listener: (...args: any[]) => void, + ): this; + prependOnceListener(event: "close", listener: () => void): this; + prependOnceListener(event: "message", listener: (value: any) => void): this; + prependOnceListener( + event: "messageerror", + listener: (error: Error) => void, + ): this; + prependOnceListener( + event: string | symbol, + listener: (...args: any[]) => void, + ): this; + removeListener(event: "close", listener: () => void): this; + removeListener(event: "message", listener: (value: any) => void): this; + removeListener( + event: "messageerror", + listener: (error: Error) => void, + ): this; + removeListener( + event: string | symbol, + listener: (...args: any[]) => void, + ): this; + off(event: "close", listener: () => void): this; + off(event: "message", listener: (value: any) => void): this; + off(event: "messageerror", listener: (error: Error) => void): this; + off(event: string | symbol, listener: (...args: any[]) => void): this; + } + interface WorkerOptions { + /** + * List of arguments which would be stringified and appended to + * `process.argv` in the worker. This is mostly similar to the `workerData` + * but the values will be available on the global `process.argv` as if they + * were passed as CLI options to the script. + */ + argv?: any[] | undefined; + env?: Record<string, string> | typeof SHARE_ENV | undefined; + eval?: boolean | undefined; + workerData?: any; + stdin?: boolean | undefined; + stdout?: boolean | undefined; + stderr?: boolean | undefined; + execArgv?: string[] | undefined; + resourceLimits?: ResourceLimits | undefined; + /** + * Additional data to send in the first worker message. + */ + transferList?: TransferListItem[] | undefined; + /** + * @default true + */ + trackUnmanagedFds?: boolean | undefined; + } + interface ResourceLimits { + /** + * The maximum size of a heap space for recently created objects. + */ + maxYoungGenerationSizeMb?: number | undefined; + /** + * The maximum size of the main heap in MB. + */ + maxOldGenerationSizeMb?: number | undefined; + /** + * The size of a pre-allocated memory range used for generated code. + */ + codeRangeSizeMb?: number | undefined; + /** + * The default maximum stack size for the thread. Small values may lead to unusable Worker instances. + * @default 4 + */ + stackSizeMb?: number | undefined; + } + /** + * The `Worker` class represents an independent JavaScript execution thread. + * Most Node.js APIs are available inside of it. + * + * Notable differences inside a Worker environment are: + * + * * The `process.stdin`, `process.stdout` and `process.stderr` may be redirected by the parent thread. + * * The `require('worker_threads').isMainThread` property is set to `false`. + * * The `require('worker_threads').parentPort` message port is available. + * * `process.exit()` does not stop the whole program, just the single thread, + * and `process.abort()` is not available. + * * `process.chdir()` and `process` methods that set group or user ids + * are not available. + * * `process.env` is a copy of the parent thread's environment variables, + * unless otherwise specified. Changes to one copy are not visible in other + * threads, and are not visible to native add-ons (unless `worker.SHARE_ENV` is passed as the `env` option to the `Worker` constructor). + * * `process.title` cannot be modified. + * * Signals are not delivered through `process.on('...')`. + * * Execution may stop at any point as a result of `worker.terminate()` being invoked. + * * IPC channels from parent processes are not accessible. + * * The `trace_events` module is not supported. + * * Native add-ons can only be loaded from multiple threads if they fulfill `certain conditions`. + * + * Creating `Worker` instances inside of other `Worker`s is possible. + * + * Like [Web Workers](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API) and the `cluster module`, two-way communication can be + * achieved through inter-thread message passing. Internally, a `Worker` has a + * built-in pair of `MessagePort` s that are already associated with each other + * when the `Worker` is created. While the `MessagePort` object on the parent side + * is not directly exposed, its functionalities are exposed through `worker.postMessage()` and the `worker.on('message')` event + * on the `Worker` object for the parent thread. + * + * To create custom messaging channels (which is encouraged over using the default + * global channel because it facilitates separation of concerns), users can create + * a `MessageChannel` object on either thread and pass one of the`MessagePort`s on that `MessageChannel` to the other thread through a + * pre-existing channel, such as the global one. + * + * See `port.postMessage()` for more information on how messages are passed, + * and what kind of JavaScript values can be successfully transported through + * the thread barrier. + * + * ```js + * const assert = require('assert'); + * const { + * Worker, MessageChannel, MessagePort, isMainThread, parentPort + * } = require('worker_threads'); + * if (isMainThread) { + * const worker = new Worker(__filename); + * const subChannel = new MessageChannel(); + * worker.postMessage({ hereIsYourPort: subChannel.port1 }, [subChannel.port1]); + * subChannel.port2.on('message', (value) => { + * console.log('received:', value); + * }); + * } else { + * parentPort.once('message', (value) => { + * assert(value.hereIsYourPort instanceof MessagePort); + * value.hereIsYourPort.postMessage('the worker is sending this'); + * value.hereIsYourPort.close(); + * }); + * } + * ``` + * @since v10.5.0 + */ + interface Worker extends EventTarget { + onerror: ((this: Worker, ev: ErrorEvent) => any) | null; + onmessage: ((this: Worker, ev: MessageEvent) => any) | null; + onmessageerror: ((this: Worker, ev: MessageEvent) => any) | null; + + addEventListener<K extends keyof WorkerEventMap>( + type: K, + listener: (this: Worker, ev: WorkerEventMap[K]) => any, + options?: boolean | AddEventListenerOptions, + ): void; + + removeEventListener<K extends keyof WorkerEventMap>( + type: K, + listener: (this: Worker, ev: WorkerEventMap[K]) => any, + options?: boolean | EventListenerOptions, + ): void; + + terminate(): void; + + postMessage(message: any, transfer?: Transferable[]): void; + + /** + * Keep the process alive until the worker is terminated or `unref`'d + */ + ref(): void; + /** + * Undo a previous `ref()` + */ + unref(): void; + + /** + * Unique per-process thread ID. Main thread ID is always `0`. + */ + readonly threadId: number; + } + var Worker: { + prototype: Worker; + new (stringUrl: string | URL, options?: WorkerOptions): Worker; + }; + interface WorkerOptions { + name?: string; + + /** + * Use less memory, but make the worker slower. + * + * Internally, this sets the heap size configuration in JavaScriptCore to be + * the small heap instead of the large heap. + */ + smol?: boolean; + + /** + * When `true`, the worker will keep the parent thread alive until the worker is terminated or `unref`'d. + * When `false`, the worker will not keep the parent thread alive. + * + * By default, this is `false`. + */ + ref?: boolean; + + /** + * Does nothing in Bun + */ + type?: string; + } + + interface WorkerEventMap { + message: MessageEvent; + messageerror: MessageEvent; + error: ErrorEvent; + open: Event; + close: Event; + } + + interface BroadcastChannelEventMap { + message: MessageEvent; + messageerror: MessageEvent; + } + + /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/BroadcastChannel) */ + interface BroadcastChannel extends EventTarget { + /** + * Returns the channel name (as passed to the constructor). + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/BroadcastChannel/name) + */ + readonly name: string; + /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/BroadcastChannel/message_event) */ + onmessage: ((this: BroadcastChannel, ev: MessageEvent) => any) | null; + /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/BroadcastChannel/messageerror_event) */ + onmessageerror: ((this: BroadcastChannel, ev: MessageEvent) => any) | null; + /** + * Closes the BroadcastChannel object, opening it up to garbage collection. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/BroadcastChannel/close) + */ + close(): void; + /** + * Sends the given message to other BroadcastChannel objects set up for this channel. Messages can be structured objects, e.g. nested objects and arrays. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/BroadcastChannel/postMessage) + */ + postMessage(message: any): void; + addEventListener<K extends keyof BroadcastChannelEventMap>( + type: K, + listener: ( + this: BroadcastChannel, + ev: BroadcastChannelEventMap[K], + ) => any, + options?: boolean | AddEventListenerOptions, + ): void; + addEventListener( + type: string, + listener: EventListenerOrEventListenerObject, + options?: boolean | AddEventListenerOptions, + ): void; + removeEventListener<K extends keyof BroadcastChannelEventMap>( + type: K, + listener: ( + this: BroadcastChannel, + ev: BroadcastChannelEventMap[K], + ) => any, + options?: boolean | EventListenerOptions, + ): void; + removeEventListener( + type: string, + listener: EventListenerOrEventListenerObject, + options?: boolean | EventListenerOptions, + ): void; + + /** + * Keep the process alive until the BroadcastChannel is closed or `unref`'d. + * BroadcastChannel is `ref`'d by default. + */ + ref(): void; + /** + * Undo a previous `ref()` + */ + unref(): void; + } + + var BroadcastChannel: { + prototype: BroadcastChannel; + new (name: string): BroadcastChannel; + }; + + function markAsUntransferable(object: object): void; + /** + * Transfer a `MessagePort` to a different `vm` Context. The original `port`object is rendered unusable, and the returned `MessagePort` instance + * takes its place. + * + * The returned `MessagePort` is an object in the target context and + * inherits from its global `Object` class. Objects passed to the [`port.onmessage()`](https://developer.mozilla.org/en-US/docs/Web/API/MessagePort/onmessage) listener are also created in the + * target context + * and inherit from its global `Object` class. + * + * However, the created `MessagePort` no longer inherits from [`EventTarget`](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget), and only + * [`port.onmessage()`](https://developer.mozilla.org/en-US/docs/Web/API/MessagePort/onmessage) can be used to receive + * events using it. + * @since v11.13.0 + * @param port The message port to transfer. + * @param contextifiedSandbox A `contextified` object as returned by the `vm.createContext()` method. + */ + function moveMessagePortToContext( + port: MessagePort, + contextifiedSandbox: Context, + ): MessagePort; + /** + * Receive a single message from a given `MessagePort`. If no message is available,`undefined` is returned, otherwise an object with a single `message` property + * that contains the message payload, corresponding to the oldest message in the`MessagePort`’s queue. + * + * ```js + * const { MessageChannel, receiveMessageOnPort } = require('worker_threads'); + * const { port1, port2 } = new MessageChannel(); + * port1.postMessage({ hello: 'world' }); + * + * console.log(receiveMessageOnPort(port2)); + * // Prints: { message: { hello: 'world' } } + * console.log(receiveMessageOnPort(port2)); + * // Prints: undefined + * ``` + * + * When this function is used, no `'message'` event is emitted and the`onmessage` listener is not invoked. + * @since v12.3.0 + */ + function receiveMessageOnPort(port: MessagePort): + | { + message: any; + } + | undefined; + type Serializable = string | object | number | boolean | bigint; + /** + * Within a worker thread, `worker.getEnvironmentData()` returns a clone + * of data passed to the spawning thread's `worker.setEnvironmentData()`. + * Every new `Worker` receives its own copy of the environment data + * automatically. + * + * ```js + * const { + * Worker, + * isMainThread, + * setEnvironmentData, + * getEnvironmentData, + * } = require('worker_threads'); + * + * if (isMainThread) { + * setEnvironmentData('Hello', 'World!'); + * const worker = new Worker(__filename); + * } else { + * console.log(getEnvironmentData('Hello')); // Prints 'World!'. + * } + * ``` + * @since v15.12.0, v14.18.0 + * @param key Any arbitrary, cloneable JavaScript value that can be used as a {Map} key. + */ + function getEnvironmentData(key: Serializable): Serializable; + /** + * The `worker.setEnvironmentData()` API sets the content of`worker.getEnvironmentData()` in the current thread and all new `Worker`instances spawned from the current context. + * @since v15.12.0, v14.18.0 + * @param key Any arbitrary, cloneable JavaScript value that can be used as a {Map} key. + * @param value Any arbitrary, cloneable JavaScript value that will be cloned and passed automatically to all new `Worker` instances. If `value` is passed as `undefined`, any previously set value + * for the `key` will be deleted. + */ + function setEnvironmentData(key: Serializable, value: Serializable): void; + + /** + * This Channel Messaging API interface allows us to create a new message channel and send data through it via its two MessagePort properties. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/MessageChannel) + */ + interface MessageChannel { + /** + * Returns the first MessagePort object. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/MessageChannel/port1) + */ + readonly port1: MessagePort; + /** + * Returns the second MessagePort object. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/MessageChannel/port2) + */ + readonly port2: MessagePort; + } + + var MessageChannel: { + prototype: MessageChannel; + new (): MessageChannel; + }; + + interface MessagePortEventMap { + message: MessageEvent; + messageerror: MessageEvent; + } +} +declare module "node:worker_threads" { + export * from "worker_threads"; +} diff --git a/packages/bun-types/ws.d.ts b/packages/bun-types/ws.d.ts new file mode 100644 index 000000000..a14f87edd --- /dev/null +++ b/packages/bun-types/ws.d.ts @@ -0,0 +1,222 @@ +/** + * The `node:diagnostics_channel` module provides an API to create named channels + * to report arbitrary message data for diagnostics purposes. + * + * It can be accessed using: + * + * ```js + * import diagnostics_channel from 'node:diagnostics_channel'; + * ``` + * + * It is intended that a module writer wanting to report diagnostics messages + * will create one or many top-level channels to report messages through. + * Channels may also be acquired at runtime but it is not encouraged + * due to the additional overhead of doing so. Channels may be exported for + * convenience, but as long as the name is known it can be acquired anywhere. + * + * If you intend for your module to produce diagnostics data for others to + * consume it is recommended that you include documentation of what named + * channels are used along with the shape of the message data. Channel names + * should generally include the module name to avoid collisions with data from + * other modules. + * @since Bun v0.7.2 + * @see [source](https://github.com/nodejs/node/blob/v20.2.0/lib/diagnostics_channel.js) + */ +declare module "ws" { + import { + IncomingMessage, + OutgoingHttpHeaders, + Server as HTTPServer, + } from "http"; + import { Duplex, EventEmitter } from "stream"; + // import {Server as HTTPServer} from "http"; + import { Server as HTTPSServer } from "https"; + var WebSocket: typeof global.WebSocket; + interface WebSocket extends globalThis.WebSocket {} + + type VerifyClientCallbackSync< + Request extends IncomingMessage = IncomingMessage, + > = (info: { origin: string; secure: boolean; req: Request }) => boolean; + type VerifyClientCallbackAsync< + Request extends IncomingMessage = IncomingMessage, + > = ( + info: { origin: string; secure: boolean; req: Request }, + callback: ( + res: boolean, + code?: number, + message?: string, + headers?: OutgoingHttpHeaders, + ) => void, + ) => void; + + interface WebSocketServerOptions< + U extends typeof WebSocket = typeof WebSocket, + V extends typeof IncomingMessage = typeof IncomingMessage, + > { + host?: string | undefined; + port?: number | undefined; + backlog?: number | undefined; + server?: HTTPServer<V> | HTTPSServer<V> | undefined; + verifyClient?: + | VerifyClientCallbackAsync<InstanceType<V>> + | VerifyClientCallbackSync<InstanceType<V>> + | undefined; + handleProtocols?: ( + protocols: Set<string>, + request: InstanceType<V>, + ) => string | false; + path?: string | undefined; + noServer?: boolean | undefined; + clientTracking?: boolean | undefined; + perMessageDeflate?: boolean; // | PerMessageDeflateOptions | undefined; + // maxPayload?: number | undefined; + // skipUTF8Validation?: boolean | undefined; + WebSocket?: U | undefined; + } + + interface AddressInfo { + address: string; + family: string; + port: number; + } + + // WebSocket Server + class WebSocketServer< + T extends typeof WebSocket = typeof WebSocket, + U extends typeof IncomingMessage = typeof IncomingMessage, + > extends EventEmitter { + options: WebSocketServerOptions<T, U>; + path: string; + clients: Set<InstanceType<T>>; + + constructor(options?: WebSocketServerOptions<T, U>, callback?: () => void); + + address(): AddressInfo | string; + close(cb?: (err?: Error) => void): void; + handleUpgrade( + request: InstanceType<U>, + socket: Duplex, + upgradeHead: Buffer, + callback: (client: InstanceType<T>, request: InstanceType<U>) => void, + ): void; + shouldHandle(request: InstanceType<U>): boolean | Promise<boolean>; + + // Events + on( + event: "connection", + cb: ( + this: WebSocketServer<T>, + socket: InstanceType<T>, + request: InstanceType<U>, + ) => void, + ): this; + on( + event: "error", + cb: (this: WebSocketServer<T>, error: Error) => void, + ): this; + on( + event: "headers", + cb: ( + this: WebSocketServer<T>, + headers: string[], + request: InstanceType<U>, + ) => void, + ): this; + on( + event: "close" | "listening", + cb: (this: WebSocketServer<T>) => void, + ): this; + on( + event: string | symbol, + listener: (this: WebSocketServer<T>, ...args: any[]) => void, + ): this; + + once( + event: "connection", + cb: ( + this: WebSocketServer<T>, + socket: InstanceType<T>, + request: InstanceType<U>, + ) => void, + ): this; + once( + event: "error", + cb: (this: WebSocketServer<T>, error: Error) => void, + ): this; + once( + event: "headers", + cb: ( + this: WebSocketServer<T>, + headers: string[], + request: InstanceType<U>, + ) => void, + ): this; + once( + event: "close" | "listening", + cb: (this: WebSocketServer<T>) => void, + ): this; + once( + event: string | symbol, + listener: (this: WebSocketServer<T>, ...args: any[]) => void, + ): this; + + off( + event: "connection", + cb: ( + this: WebSocketServer<T>, + socket: InstanceType<T>, + request: InstanceType<U>, + ) => void, + ): this; + off( + event: "error", + cb: (this: WebSocketServer<T>, error: Error) => void, + ): this; + off( + event: "headers", + cb: ( + this: WebSocketServer<T>, + headers: string[], + request: InstanceType<U>, + ) => void, + ): this; + off( + event: "close" | "listening", + cb: (this: WebSocketServer<T>) => void, + ): this; + off( + event: string | symbol, + listener: (this: WebSocketServer<T>, ...args: any[]) => void, + ): this; + + addListener( + event: "connection", + cb: (client: InstanceType<T>, request: InstanceType<U>) => void, + ): this; + addListener(event: "error", cb: (err: Error) => void): this; + addListener( + event: "headers", + cb: (headers: string[], request: InstanceType<U>) => void, + ): this; + addListener(event: "close" | "listening", cb: () => void): this; + addListener( + event: string | symbol, + listener: (...args: any[]) => void, + ): this; + + removeListener( + event: "connection", + cb: (client: InstanceType<T>, request: InstanceType<U>) => void, + ): this; + removeListener(event: "error", cb: (err: Error) => void): this; + removeListener( + event: "headers", + cb: (headers: string[], request: InstanceType<U>) => void, + ): this; + removeListener(event: "close" | "listening", cb: () => void): this; + removeListener( + event: string | symbol, + listener: (...args: any[]) => void, + ): this; + } +} |