diff --git a/bun.lockb b/bun.lockb index 4ccfae2715..24d5f46ecd 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/packages/bun-types/bun.d.ts b/packages/bun-types/bun.d.ts index d6e7b0ee17..82e56dba92 100644 --- a/packages/bun-types/bun.d.ts +++ b/packages/bun-types/bun.d.ts @@ -16,6 +16,7 @@ declare module "bun" { import type { Encoding as CryptoEncoding } from "crypto"; import type { CipherNameAndProtocol, EphemeralKeyInfo, PeerCertificate } from "tls"; + interface Env { NODE_ENV?: string; /** @@ -163,6 +164,19 @@ declare module "bun" { */ arrayBuffer(): ArrayBuffer; + /** + * Read from stdout as a Uint8Array + * + * @returns Stdout as a Uint8Array + * @example + * + * ```ts + * const output = await $`echo hello`; + * console.log(output.bytes()); // Uint8Array { byteLength: 6 } + * ``` + */ + bytes(): Uint8Array; + /** * Read from stdout as a Blob * @@ -411,9 +425,9 @@ declare module "bun" { arrayBuffer(): ArrayBuffer; /** - * Read from stdout as an Uint8Array + * Read from stdout as a Uint8Array * - * @returns Stdout as an Uint8Array + * @returns Stdout as a Uint8Array * @example * * ```ts diff --git a/packages/bun-types/deprecated.d.ts b/packages/bun-types/deprecated.d.ts index 5a90164689..3b04ec3528 100644 --- a/packages/bun-types/deprecated.d.ts +++ b/packages/bun-types/deprecated.d.ts @@ -3,30 +3,30 @@ declare module "bun" { * @deprecated Renamed to `ErrorLike` */ type Errorlike = ErrorLike; - interface TLSOptions { - /** - * File path to a TLS key - * - * To enable TLS, this option is required. - * - * @deprecated since v0.6.3 - Use `key: Bun.file(path)` instead. - */ - keyFile?: string; - /** - * File path to a TLS certificate - * - * To enable TLS, this option is required. - * - * @deprecated since v0.6.3 - Use `cert: Bun.file(path)` instead. - */ - certFile?: string; - /** - * File path to a .pem file for a custom root CA - * - * @deprecated since v0.6.3 - Use `ca: Bun.file(path)` instead. - */ - caFile?: string; - } + // interface TLSOptions { + // /** + // * File path to a TLS key + // * + // * To enable TLS, this option is required. + // * + // * @deprecated since v0.6.3 - Use `key: Bun.file(path)` instead. + // */ + // keyFile?: string; + // /** + // * File path to a TLS certificate + // * + // * To enable TLS, this option is required. + // * + // * @deprecated since v0.6.3 - Use `cert: Bun.file(path)` instead. + // */ + // certFile?: string; + // /** + // * File path to a .pem file for a custom root CA + // * + // * @deprecated since v0.6.3 - Use `ca: Bun.file(path)` instead. + // */ + // caFile?: string; + // } } declare namespace NodeJS { diff --git a/packages/bun-types/fetch.d.ts b/packages/bun-types/fetch.d.ts index 01c53de54d..82f8d4c0b7 100644 --- a/packages/bun-types/fetch.d.ts +++ b/packages/bun-types/fetch.d.ts @@ -1,6 +1,3 @@ -type _Response = typeof globalThis extends { onmessage: any } ? {} : import("undici-types").Response; - -export interface Response extends _Response {} export declare class Response { constructor(body?: Bun.BodyInit | null | undefined, init?: Bun.ResponseInit | undefined); @@ -51,3 +48,9 @@ export declare class Response { */ static error(): Response; } + +export declare class Request { + constructor(...params: Parameters); + + // TODO: Rest of types +} diff --git a/packages/bun-types/globals.d.ts b/packages/bun-types/globals.d.ts index a29bcc91cb..e895bb8c48 100644 --- a/packages/bun-types/globals.d.ts +++ b/packages/bun-types/globals.d.ts @@ -6,6 +6,14 @@ type _ReadableStream = typeof globalThis extends { } ? T : import("stream/web").ReadableStream; + +type _RequestInit = typeof globalThis extends { + onerror: any; + RequestInit: infer T; +} + ? T + : NonNullable[1]>; + type _WritableStream = typeof globalThis extends { onerror: any; WritableStream: infer T; @@ -141,17 +149,9 @@ import type { TextDecoder as NodeTextDecoder, TextEncoder as NodeTextEncoder } f import type { MessagePort } from "worker_threads"; import type { WebSocket as _WebSocket } from "ws"; -declare module "*.txt" { - var text: string; - export = text; -} - -declare module "*.toml" { - var contents: any; - export = contents; -} - declare global { + var onmessage: never; // Not a fan of this - required so Node.js' globals.d.ts doesn't conflict with our own definitions. Not a perfect solution though. + var Bun: typeof import("bun"); namespace NodeJS { @@ -804,9 +804,10 @@ declare global { readonly lastModified: number; readonly name: string; } - var File: typeof globalThis extends { onerror: any; File: infer T } ? T : typeof File; - interface FetchRequestInit extends RequestInit { + var File: File; + + interface RequestInit extends _RequestInit { /** * Log the raw HTTP request & response to stdout. This API may be * removed in a future version of Bun without notice. @@ -823,10 +824,7 @@ declare global { /** * Override the default TLS options */ - tls?: { - rejectUnauthorized?: boolean | undefined; // Defaults to true - checkServerIdentity?: any; // TODO: change `any` to `checkServerIdentity` - }; + tls?: import("bun").TLSOptions; } /** @@ -915,29 +913,8 @@ declare global { new (): ShadowRealm; }; - interface Fetch { - /** - * Send a HTTP(s) request - * - * @param request Request object - * @param init A structured value that contains settings for the fetch() request. - * - * @returns A promise that resolves to {@link Response} object. - */ - (request: Request, init?: RequestInit): Promise; - - /** - * Send a HTTP(s) request - * - * @param url URL string - * @param init A structured value that contains settings for the fetch() request. - * - * @returns A promise that resolves to {@link Response} object. - */ - (url: string | URL | Request, init?: FetchRequestInit): Promise; - - (input: string | URL | globalThis.Request, init?: RequestInit): Promise; - + // @types/node defines fetch, we can merge our `.preconnect(url)` 'static' method + namespace fetch { /** * Start the DNS resolution, TCP connection, and TLS handshake for a request * before the request is actually sent. @@ -946,13 +923,14 @@ declare global { * long-running task that will delay the request starting. * * This is a bun-specific API and is not part of the Fetch API specification. + * + * @param url A string or URL instance to preconnect */ - preconnect(url: string | URL): void; + export function preconnect(url: string | URL): void; } - var fetch: Fetch; - function queueMicrotask(callback: (...args: any[]) => void): void; + /** * Log an error using the default exception handler * @param error Error or string @@ -1835,10 +1813,10 @@ declare global { readonly main: boolean; /** Alias of `import.meta.dir`. Exists for Node.js compatibility */ - readonly dirname: string; + /* readonly */ dirname: string; // Ideally this would be `readonly`, but Node.js doesn't mark it readonly so TypeScript tells us off /** Alias of `import.meta.path`. Exists for Node.js compatibility */ - readonly filename: string; + /* readonly */ filename: string; // Ideally this would be `readonly`, but Node.js doesn't mark it readonly so TypeScript tells us off } /** @@ -1969,17 +1947,17 @@ declare global { ? T : typeof import("./fetch").Response; - var Request: typeof globalThis extends { - onerror: any; - Request: infer T; - } - ? T - : { - prototype: Request; - new (requestInfo: string, requestInit?: RequestInit): Request; - new (requestInfo: RequestInit & { url: string }): Request; - new (requestInfo: Request, requestInit?: RequestInit): Request; - }; + // var Request: typeof globalThis extends { + // onerror: any; + // Request: infer T; + // } + // ? T + // : { + // prototype: Request; + // new (requestInfo: string, requestInit?: RequestInit): Request; + // new (requestInfo: RequestInit & { url: string }): Request; + // new (requestInfo: Request, requestInit?: RequestInit): Request; + // }; interface Headers { /** diff --git a/packages/bun-types/index.d.ts b/packages/bun-types/index.d.ts index c0ceea7286..71a6a4f33b 100644 --- a/packages/bun-types/index.d.ts +++ b/packages/bun-types/index.d.ts @@ -2,6 +2,8 @@ // Definitions by: Jarred Sumner // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +/// + /// /// /// @@ -10,6 +12,7 @@ //// /// +/// /// /// /// diff --git a/packages/bun-types/package.json b/packages/bun-types/package.json index ced7abf069..8567bbf58d 100644 --- a/packages/bun-types/package.json +++ b/packages/bun-types/package.json @@ -14,7 +14,8 @@ "homepage": "https://bun.sh", "dependencies": { "@types/node": "~20.12.8", - "@types/ws": "~8.5.10" + "@types/ws": "~8.5.10", + "undici-fetch": "^1.0.0-rc.4" }, "devDependencies": { "@biomejs/biome": "^1.5.3", diff --git a/packages/bun-types/test/fetch.test.ts b/packages/bun-types/test/fetch.test.ts new file mode 100644 index 0000000000..6c10b4861b --- /dev/null +++ b/packages/bun-types/test/fetch.test.ts @@ -0,0 +1,12 @@ +fetch.preconnect("bun"); +fetch("bun"); + +fetch("bun", { + verbose: true, +}); + +const init: RequestInit = { + proxy: "12345", + verbose: true, + method: "GET", +}; diff --git a/packages/bun-types/test/toml.test.ts b/packages/bun-types/test/toml.test.ts deleted file mode 100644 index 6b93b6150a..0000000000 --- a/packages/bun-types/test/toml.test.ts +++ /dev/null @@ -1,4 +0,0 @@ -import data from "./bunfig.toml"; -import { expectAny } from "./utilities.test"; - -expectAny(data); diff --git a/packages/bun-types/test/wildcards.test.ts b/packages/bun-types/test/wildcards.test.ts new file mode 100644 index 0000000000..3fb959ee07 --- /dev/null +++ b/packages/bun-types/test/wildcards.test.ts @@ -0,0 +1,6 @@ +import toml from "./bunfig.toml"; +import text from "./bunfig.txt"; +import { expectAny, expectType } from "./utilities.test"; + +expectAny(toml); +expectType(text); diff --git a/packages/bun-types/wildcards.d.ts b/packages/bun-types/wildcards.d.ts new file mode 100644 index 0000000000..b4ac81a0a3 --- /dev/null +++ b/packages/bun-types/wildcards.d.ts @@ -0,0 +1,9 @@ +declare module "*.txt" { + var text: string; + export = text; +} + +declare module "*.toml" { + var contents: any; + export = contents; +}