bun-types 0.1.11 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/types.d.ts +424 -15
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bun-types",
3
- "version": "0.1.11",
3
+ "version": "0.2.0",
4
4
  "description": "Type definitions for Bun, an incredibly fast JavaScript runtime",
5
5
  "types": "types.d.ts",
6
6
  "files": [
package/types.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- // Type definitions for bun 0.1.11
1
+ // Type definitions for bun 0.2.0
2
2
  // Project: https://github.com/oven-sh/bun
3
3
  // Definitions by: Jarred Sumner <https://github.com/Jarred-Sumner>
4
4
  // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
@@ -823,7 +823,10 @@ interface ResponseInit {
823
823
  * ```
824
824
  */
825
825
  declare class Response implements BlobInterface {
826
- constructor(body: BlobPart | BlobPart[], options?: ResponseInit);
826
+ constructor(
827
+ body?: ReadableStream | BlobPart | BlobPart[] | null,
828
+ options?: ResponseInit
829
+ );
827
830
 
828
831
  /**
829
832
  * Create a new {@link Response} with a JSON body
@@ -885,6 +888,21 @@ declare class Response implements BlobInterface {
885
888
  */
886
889
  readonly headers: Headers;
887
890
 
891
+ /**
892
+ * HTTP response body as a [ReadableStream](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream)
893
+ *
894
+ * This is part of web Streams
895
+ *
896
+ * @example
897
+ * ```ts
898
+ * const {body} = await fetch("https://remix.run");
899
+ * const reader = body.getReader();
900
+ * const {done, value} = await reader.read();
901
+ * console.log(value); // Uint8Array
902
+ * ```
903
+ */
904
+ readonly body: ReadableStream | null;
905
+
888
906
  /**
889
907
  * Has the body of the response already been consumed?
890
908
  */
@@ -1020,7 +1038,9 @@ interface RequestInit {
1020
1038
  /**
1021
1039
  * A boolean to set request's keepalive.
1022
1040
  *
1023
- * Note: as of Bun v0.0.74, this is not implemented yet.
1041
+ * Available in Bun v0.2.0 and above.
1042
+ *
1043
+ * This is enabled by default
1024
1044
  */
1025
1045
  keepalive?: boolean;
1026
1046
  /**
@@ -1055,6 +1075,11 @@ interface RequestInit {
1055
1075
  * This does nothing in Bun
1056
1076
  */
1057
1077
  window?: any;
1078
+
1079
+ /**
1080
+ * Enable or disable HTTP request timeout
1081
+ */
1082
+ timeout?: boolean;
1058
1083
  }
1059
1084
 
1060
1085
  /**
@@ -1105,6 +1130,19 @@ declare class Request implements BlobInterface {
1105
1130
  */
1106
1131
  text(): Promise<string>;
1107
1132
 
1133
+ /**
1134
+ * Consume the [`Request`](https://developer.mozilla.org/en-US/docs/Web/API/Request) body as a {@link ReadableStream}.
1135
+ *
1136
+ * Streaming **outgoing** HTTP request bodies via `fetch()` is not yet supported in
1137
+ * Bun.
1138
+ *
1139
+ * Reading **incoming** HTTP request bodies via `ReadableStream` in `Bun.serve()` is supported
1140
+ * as of Bun v0.2.0.
1141
+ *
1142
+ *
1143
+ */
1144
+ get body(): ReadableStream | null;
1145
+
1108
1146
  /**
1109
1147
  * Consume the [`Request`](https://developer.mozilla.org/en-US/docs/Web/API/Request) body as an ArrayBuffer.
1110
1148
  *
@@ -1436,7 +1474,21 @@ declare function clearTimeout(id?: number): void;
1436
1474
  *
1437
1475
  *
1438
1476
  */
1439
- declare function fetch(url: string, init?: RequestInit): Promise<Response>;
1477
+ declare function fetch(
1478
+ url: string,
1479
+ init?: RequestInit,
1480
+ /**
1481
+ * This is a custom property that is not part of the Fetch API specification.
1482
+ * It exists mostly as a debugging tool
1483
+ */
1484
+ bunOnlyOptions?: {
1485
+ /**
1486
+ * Log the raw HTTP request & response to stdout. This API may be
1487
+ * removed in a future version of Bun without notice.
1488
+ */
1489
+ verbose: boolean;
1490
+ }
1491
+ ): Promise<Response>;
1440
1492
 
1441
1493
  /**
1442
1494
  * Send a HTTP(s) request
@@ -1449,7 +1501,21 @@ declare function fetch(url: string, init?: RequestInit): Promise<Response>;
1449
1501
  *
1450
1502
  */
1451
1503
  // tslint:disable-next-line:unified-signatures
1452
- declare function fetch(request: Request, init?: RequestInit): Promise<Response>;
1504
+ declare function fetch(
1505
+ request: Request,
1506
+ init?: RequestInit,
1507
+ /**
1508
+ * This is a custom property that is not part of the Fetch API specification.
1509
+ * It exists mostly as a debugging tool
1510
+ */
1511
+ bunOnlyOptions?: {
1512
+ /**
1513
+ * Log the raw HTTP request & response to stdout. This API may be
1514
+ * removed in a future version of Bun without notice.
1515
+ */
1516
+ verbose: boolean;
1517
+ }
1518
+ ): Promise<Response>;
1453
1519
 
1454
1520
  declare function queueMicrotask(callback: () => void): void;
1455
1521
  /**
@@ -2108,17 +2174,9 @@ declare var Loader: {
2108
2174
  * instead.
2109
2175
  *
2110
2176
  * @param specifier - module specifier as it appears in transpiled source code
2177
+ * @param referrer - module specifier that is resolving this specifier
2111
2178
  */
2112
- resolve: (specifier: string) => Promise<string>;
2113
- /**
2114
- * Synchronously resolve a module specifier
2115
- *
2116
- * This may return a path to `node_modules.server.bun`, which will be confusing.
2117
- *
2118
- * Consider {@link Bun.resolveSync}
2119
- * instead.
2120
- */
2121
- resolveSync: (specifier: string, from: string) => string;
2179
+ resolve: (specifier: strin, referrer: string) => string;
2122
2180
  };
2123
2181
 
2124
2182
  /** This Streams API interface represents a readable stream of byte data. The Fetch API offers a concrete instance of a ReadableStream through the body property of a Response object. */
@@ -2139,6 +2197,8 @@ interface ReadableStream<R = any> {
2139
2197
  callbackfn: (value: any, key: number, parent: ReadableStream<R>) => void,
2140
2198
  thisArg?: any
2141
2199
  ): void;
2200
+ [Symbol.asyncIterator](): AsyncIterableIterator<R>;
2201
+ values(options: { preventCancel: boolean }): AsyncIterableIterator<R>;
2142
2202
  }
2143
2203
 
2144
2204
  declare var ReadableStream: {
@@ -13440,6 +13500,28 @@ declare module "bun" {
13440
13500
  end(): ArrayBuffer | Uint8Array;
13441
13501
  }
13442
13502
 
13503
+ /**
13504
+ * Fast incremental writer for files and pipes.
13505
+ *
13506
+ * This uses the same interface as {@link ArrayBufferSink}, but writes to a file or pipe.
13507
+ */
13508
+ export interface FileSink {
13509
+ /**
13510
+ * Write a chunk of data to the file.
13511
+ *
13512
+ * If the file descriptor is not writable yet, the data is buffered.
13513
+ */
13514
+ write(chunk: string | ArrayBufferView | ArrayBuffer): number;
13515
+ /**
13516
+ * Flush the internal buffer, committing the data to disk or the pipe.
13517
+ */
13518
+ flush(): number | Promise<number>;
13519
+ /**
13520
+ * Close the file descriptor. This also flushes the internal buffer.
13521
+ */
13522
+ end(error?: Error): number | Promise<number>;
13523
+ }
13524
+
13443
13525
  /**
13444
13526
  * [`Blob`](https://developer.mozilla.org/en-US/docs/Web/API/Blob) powered by the fastest system calls available for operating on files.
13445
13527
  *
@@ -13476,6 +13558,15 @@ declare module "bun" {
13476
13558
  * @param end - absolute offset in bytes (relative to 0)
13477
13559
  */
13478
13560
  slice(begin?: number, end?: number): FileBlob;
13561
+
13562
+ /**
13563
+ * Incremental writer for files and pipes.
13564
+ */
13565
+ writer(): FileSink;
13566
+
13567
+ readonly readable: ReadableStream;
13568
+
13569
+ // TODO: writable: WritableStream;
13479
13570
  }
13480
13571
 
13481
13572
  /**
@@ -13876,12 +13967,65 @@ declare module "bun" {
13876
13967
  */
13877
13968
  stop(): void;
13878
13969
 
13970
+ /**
13971
+ * Update the `fetch` and `error` handlers without restarting the server.
13972
+ *
13973
+ * This is useful if you want to change the behavior of your server without
13974
+ * restarting it or for hot reloading.
13975
+ *
13976
+ * @example
13977
+ *
13978
+ * ```js
13979
+ * // create the server
13980
+ * const server = Bun.serve({
13981
+ * fetch(request) {
13982
+ * return new Response("Hello World v1")
13983
+ * }
13984
+ * });
13985
+ *
13986
+ * // Update the server to return a different response
13987
+ * server.update({
13988
+ * fetch(request) {
13989
+ * return new Response("Hello World v2")
13990
+ * }
13991
+ * });
13992
+ * ```
13993
+ *
13994
+ * Passing other options such as `port` or `hostname` won't do anything.
13995
+ */
13996
+ reload(options: Serve): void;
13997
+
13998
+ /**
13999
+ * Mock the fetch handler for a running server.
14000
+ *
14001
+ * This feature is not fully implemented yet. It doesn't normalize URLs
14002
+ * consistently in all cases and it doesn't yet call the `error` handler
14003
+ * consistently. This needs to be fixed
14004
+ */
14005
+ fetch(request: Request): Response | Promise<Response>;
14006
+
13879
14007
  /**
13880
14008
  * How many requests are in-flight right now?
13881
14009
  */
13882
14010
  readonly pendingRequests: number;
13883
14011
  readonly port: number;
14012
+ /**
14013
+ * The hostname the server is listening on. Does not include the port
14014
+ * @example
14015
+ * ```js
14016
+ * "localhost"
14017
+ * ```
14018
+ */
13884
14019
  readonly hostname: string;
14020
+ /**
14021
+ * Is the server running in development mode?
14022
+ *
14023
+ * In development mode, `Bun.serve()` returns rendered error messages with
14024
+ * stack traces instead of a generic 500 error. This makes debugging easier,
14025
+ * but development mode shouldn't be used in production or you will risk
14026
+ * leaking sensitive information.
14027
+ *
14028
+ */
13885
14029
  readonly development: boolean;
13886
14030
  }
13887
14031
 
@@ -14653,6 +14797,271 @@ declare module "bun" {
14653
14797
  }
14654
14798
 
14655
14799
  var plugin: BunPlugin;
14800
+
14801
+ declare namespace SpawnOptions {
14802
+ type Readable =
14803
+ | "inherit"
14804
+ | "pipe"
14805
+ | null
14806
+ | undefined
14807
+ | FileBlob
14808
+ | ArrayBufferView
14809
+ | number;
14810
+
14811
+ type Writable =
14812
+ | "inherit"
14813
+ | "pipe"
14814
+ | null
14815
+ | undefined
14816
+ | FileBlob
14817
+ | ArrayBufferView
14818
+ | Blob
14819
+ | number
14820
+ | Response
14821
+ | Request;
14822
+
14823
+ interface OptionsObject {
14824
+ /**
14825
+ * The current working directory of the process
14826
+ *
14827
+ * Defaults to `process.cwd()`
14828
+ */
14829
+ cwd?: string;
14830
+
14831
+ /**
14832
+ * The environment variables of the process
14833
+ *
14834
+ * Defaults to `process.env` as it was when the current Bun process launched.
14835
+ *
14836
+ * Changes to `process.env` at runtime won't automatically be reflected in the default value. For that, you can pass `process.env` explicitly.
14837
+ *
14838
+ */
14839
+ env?: Record<string, string>;
14840
+
14841
+ /**
14842
+ * The standard file descriptors of the process
14843
+ * - `inherit`: The process will inherit the standard input of the current process
14844
+ * - `pipe`: The process will have a new pipe for standard input
14845
+ * - `null`: The process will have no standard input
14846
+ * - `ArrayBufferView`, `Blob`: The process will read from the buffer
14847
+ * - `number`: The process will read from the file descriptor
14848
+ * - `undefined`: The default value
14849
+ */
14850
+ stdio?: [
14851
+ SpawnOptions.Writable,
14852
+ SpawnOptions.Readable,
14853
+ SpawnOptions.Readable
14854
+ ];
14855
+ stdin?: SpawnOptions.Writable;
14856
+ stdout?: SpawnOptions.Readable;
14857
+ stderr?: SpawnOptions.Readable;
14858
+
14859
+ /**
14860
+ * Callback that runs when the {@link Subprocess} exits
14861
+ *
14862
+ * You can also do `await subprocess.exited` to wait for the process to exit.
14863
+ *
14864
+ * @example
14865
+ *
14866
+ * ```ts
14867
+ * const subprocess = spawn({
14868
+ * cmd: ["echo", "hello"],
14869
+ * onExit: (code) => {
14870
+ * console.log(`Process exited with code ${code}`);
14871
+ * },
14872
+ * });
14873
+ * ```
14874
+ */
14875
+ onExit?: (exitCode: number) => void | Promise<void>;
14876
+ }
14877
+ }
14878
+
14879
+ interface Subprocess {
14880
+ readonly stdin: undefined | number | FileSink;
14881
+ readonly stdout: undefined | number | ReadableStream;
14882
+ readonly stderr: undefined | number | ReadableStream;
14883
+
14884
+ /**
14885
+ * This returns the same value as {@link Subprocess.stdout}
14886
+ *
14887
+ * It exists for compatibility with {@link ReadableStream.pipeThrough}
14888
+ */
14889
+ readonly readable: undefined | number | ReadableStream;
14890
+
14891
+ /**
14892
+ * The process ID of the child process
14893
+ * @example
14894
+ * ```ts
14895
+ * const { pid } = Bun.spawn({ cmd: ["echo", "hello"] });
14896
+ * console.log(pid); // 1234
14897
+ * ```
14898
+ */
14899
+ readonly pid: number;
14900
+ /**
14901
+ * The exit code of the process
14902
+ *
14903
+ * The promise will resolve when the process exits
14904
+ */
14905
+ readonly exited: Promise<number>;
14906
+
14907
+ /**
14908
+ * Has the process exited?
14909
+ */
14910
+ readonly killed: boolean;
14911
+
14912
+ /**
14913
+ * Kill the process
14914
+ * @param exitCode The exitCode to send to the process
14915
+ */
14916
+ kill(exitCode?: number): void;
14917
+
14918
+ /**
14919
+ * This method will tell Bun to wait for this process to exit after you already
14920
+ * called `unref()`.
14921
+ *
14922
+ * Before shutting down, Bun will wait for all subprocesses to exit by default
14923
+ */
14924
+ ref(): void;
14925
+
14926
+ /**
14927
+ * Before shutting down, Bun will wait for all subprocesses to exit by default
14928
+ *
14929
+ * This method will tell Bun to not wait for this process to exit before shutting down.
14930
+ */
14931
+ unref(): void;
14932
+ }
14933
+
14934
+ interface SyncSubprocess {
14935
+ stdout?: Buffer;
14936
+ stderr?: Buffer;
14937
+ exitCode: number;
14938
+ success: boolean;
14939
+ }
14940
+
14941
+ /**
14942
+ * Spawn a new process
14943
+ *
14944
+ * ```js
14945
+ * const subprocess = Bun.spawn({
14946
+ * cmd: ["echo", "hello"],
14947
+ * stdout: "pipe",
14948
+ * });
14949
+ * const text = await readableStreamToText(subprocess.stdout);
14950
+ * console.log(text); // "hello\n"
14951
+ * ```
14952
+ *
14953
+ * Internally, this uses [posix_spawn(2)](https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/posix_spawn.2.html)
14954
+ */
14955
+ function spawn(
14956
+ options: SpawnOptions.OptionsObject & {
14957
+ /**
14958
+ * The command to run
14959
+ *
14960
+ * The first argument will be resolved to an absolute executable path. It must be a file, not a directory.
14961
+ *
14962
+ * If you explicitly set `PATH` in `env`, that `PATH` will be used to resolve the executable instead of the default `PATH`.
14963
+ *
14964
+ * To check if the command exists before running it, use `Bun.which(bin)`.
14965
+ *
14966
+ */
14967
+ cmd: [string, ...string[]];
14968
+ }
14969
+ ): Subprocess;
14970
+
14971
+ /**
14972
+ * Spawn a new process
14973
+ *
14974
+ * ```js
14975
+ * const {stdout} = Bun.spawn(["echo", "hello"]));
14976
+ * const text = await readableStreamToText(stdout);
14977
+ * console.log(text); // "hello\n"
14978
+ * ```
14979
+ *
14980
+ * Internally, this uses [posix_spawn(2)](https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/posix_spawn.2.html)
14981
+ */
14982
+ function spawn(
14983
+ /**
14984
+ * The command to run
14985
+ * @example
14986
+ * ```ts
14987
+ * const subprocess = Bun.spawn(["echo", "hello"]);
14988
+ */
14989
+ cmds: [
14990
+ /** One command is required */
14991
+ string,
14992
+ /** Additional arguments */
14993
+ ...string[]
14994
+ ],
14995
+ options?: SpawnOptions.OptionsObject
14996
+ ): Subprocess;
14997
+
14998
+ /**
14999
+ * Spawn a new process
15000
+ *
15001
+ * ```js
15002
+ * const {stdout} = Bun.spawnSync({
15003
+ * cmd: ["echo", "hello"],
15004
+ * });
15005
+ * console.log(stdout.toString()); // "hello\n"
15006
+ * ```
15007
+ *
15008
+ * Internally, this uses [posix_spawn(2)](https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/posix_spawn.2.html)
15009
+ */
15010
+ function spawnSync(
15011
+ options: SpawnOptions.OptionsObject & {
15012
+ /**
15013
+ * The command to run
15014
+ *
15015
+ * The first argument will be resolved to an absolute executable path. It must be a file, not a directory.
15016
+ *
15017
+ * If you explicitly set `PATH` in `env`, that `PATH` will be used to resolve the executable instead of the default `PATH`.
15018
+ *
15019
+ * To check if the command exists before running it, use `Bun.which(bin)`.
15020
+ *
15021
+ */
15022
+ cmd: [string, ...string[]];
15023
+ }
15024
+ ): SyncSubprocess;
15025
+
15026
+ /**
15027
+ * Synchronously spawn a new process
15028
+ *
15029
+ * ```js
15030
+ * const {stdout} = Bun.spawnSync(["echo", "hello"]));
15031
+ * console.log(stdout.toString()); // "hello\n"
15032
+ * ```
15033
+ *
15034
+ * Internally, this uses [posix_spawn(2)](https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/posix_spawn.2.html)
15035
+ */
15036
+ function spawnSync(
15037
+ /**
15038
+ * The command to run
15039
+ * @example
15040
+ * ```ts
15041
+ * const subprocess = Bun.spawn(["echo", "hello"]);
15042
+ */
15043
+ cmds: [
15044
+ /** One command is required */
15045
+ string,
15046
+ /** Additional arguments */
15047
+ ...string[]
15048
+ ],
15049
+ options?: SpawnOptions.OptionsObject
15050
+ ): SyncSubprocess;
15051
+
15052
+ /**
15053
+ * The current version of Bun
15054
+ * @example
15055
+ * "0.2.0"
15056
+ */
15057
+ export const version: string;
15058
+
15059
+ /**
15060
+ * The git sha at the time the currently-running version of Bun was compiled
15061
+ * @example
15062
+ * "a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2"
15063
+ */
15064
+ export const revision: string;
14656
15065
  }
14657
15066
 
14658
15067
  type TypedArray =