diff options
-rw-r--r-- | types/bun/bun.d.ts | 112 |
1 files changed, 107 insertions, 5 deletions
diff --git a/types/bun/bun.d.ts b/types/bun/bun.d.ts index a28040903..aeb9f6307 100644 --- a/types/bun/bun.d.ts +++ b/types/bun/bun.d.ts @@ -451,10 +451,7 @@ declare global { * ``` */ class Response implements BlobInterface { - constructor( - body: BlobPart | BlobPart[] | Blob | FileBlob, - options?: ResponseInit - ); + constructor(body: BlobPart | BlobPart[], options?: ResponseInit); /** * Create a new {@link Response} with a JSON body @@ -479,7 +476,7 @@ declare global { * ``` * @link https://github.com/whatwg/fetch/issues/1389 */ - static json(body?: any, options?: ResponseInit): Response; + static json(body?: any, options?: ResponseInit | number): Response; /** * Create a new {@link Response} that redirects to url * @@ -902,6 +899,89 @@ declare global { decode(input?: TypedArray | ArrayBuffer): string; } + /** + * ShadowRealms are a distinct global environment, with its own global object + * containing its own intrinsics and built-ins (standard objects that are not + * bound to global variables, like the initial value of Object.prototype). + * + * + * @example + * + * ```js + * const red = new ShadowRealm(); + * + * // realms can import modules that will execute within it's own environment. + * // When the module is resolved, it captured the binding value, or creates a new + * // wrapped function that is connected to the callable binding. + * const redAdd = await red.importValue('./inside-code.js', 'add'); + * + * // redAdd is a wrapped function exotic object that chains it's call to the + * // respective imported binding. + * let result = redAdd(2, 3); + * + * console.assert(result === 5); // yields true + * + * // The evaluate method can provide quick code evaluation within the constructed + * // shadowRealm without requiring any module loading, while it still requires CSP + * // relaxing. + * globalThis.someValue = 1; + * red.evaluate('globalThis.someValue = 2'); // Affects only the ShadowRealm's global + * console.assert(globalThis.someValue === 1); + * + * // The wrapped functions can also wrap other functions the other way around. + * const setUniqueValue = + * await red.importValue('./inside-code.js', 'setUniqueValue'); + * + * // setUniqueValue = (cb) => (cb(globalThis.someValue) * 2); + * + * result = setUniqueValue((x) => x ** 3); + * + * console.assert(result === 16); // yields true + * ``` + */ + class ShadowRealm { + /** + * Creates a new [ShadowRealm](https://github.com/tc39/proposal-shadowrealm/blob/main/explainer.md#introduction) + * + * @example + * + * ```js + * const red = new ShadowRealm(); + * + * // realms can import modules that will execute within it's own environment. + * // When the module is resolved, it captured the binding value, or creates a new + * // wrapped function that is connected to the callable binding. + * const redAdd = await red.importValue('./inside-code.js', 'add'); + * + * // redAdd is a wrapped function exotic object that chains it's call to the + * // respective imported binding. + * let result = redAdd(2, 3); + * + * console.assert(result === 5); // yields true + * + * // The evaluate method can provide quick code evaluation within the constructed + * // shadowRealm without requiring any module loading, while it still requires CSP + * // relaxing. + * globalThis.someValue = 1; + * red.evaluate('globalThis.someValue = 2'); // Affects only the ShadowRealm's global + * console.assert(globalThis.someValue === 1); + * + * // The wrapped functions can also wrap other functions the other way around. + * const setUniqueValue = + * await red.importValue('./inside-code.js', 'setUniqueValue'); + * + * // setUniqueValue = (cb) => (cb(globalThis.someValue) * 2); + * + * result = setUniqueValue((x) => x ** 3); + * + * console.assert(result === 16); // yields true + * ``` + */ + constructor(); + importValue(specifier: string, bindingName: string): Promise<any>; + evaluate(sourceText: string): any; + } + type TypedArray = | Uint8Array | Int8Array @@ -914,6 +994,28 @@ declare global { | Float64Array; namespace Bun { + type HashFunction = ( + data: string | ArrayBufferView | ArrayBuffer, + seed?: number + ) => number | bigint; + + /** + * Hash a string or array buffer using Wyhash + * + * This is not a cryptographic hash function. + * @param data The data to hash. + * @param seed The seed to use. + */ + const hash: HashFunction & { + wyhash: HashFunction; + crc32: HashFunction; + adler32: HashFunction; + cityHash32: HashFunction; + cityHash64: HashFunction; + murmur32v3: HashFunction; + murmur64v2: HashFunction; + }; + namespace WebPlatform { type Encoding = "utf-8" | "windows-1252" | "utf-16"; } |