bun-types 0.2.1 → 0.2.2

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 +546 -37
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bun-types",
3
- "version": "0.2.1",
3
+ "version": "0.2.2",
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.2.1
1
+ // Type definitions for bun 0.2.2
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
@@ -699,7 +699,7 @@ interface BlobInterface {
699
699
  json<TJSONReturnType = unknown>(): Promise<TJSONReturnType>;
700
700
  }
701
701
 
702
- type BlobPart = string | Blob | ArrayBufferView | ArrayBuffer;
702
+ type BlobPart = string | Blob | BufferSource | ArrayBuffer;
703
703
  interface BlobPropertyBag {
704
704
  /** Set a default "type" */
705
705
  type?: string;
@@ -753,14 +753,14 @@ declare class Blob implements BlobInterface {
753
753
  /**
754
754
  * Create a new [Blob](https://developer.mozilla.org/en-US/docs/Web/API/Blob)
755
755
  *
756
- * @param `parts` - An array of strings, numbers, TypedArray, or [Blob](https://developer.mozilla.org/en-US/docs/Web/API/Blob) objects
756
+ * @param `parts` - An array of strings, numbers, BufferSource, or [Blob](https://developer.mozilla.org/en-US/docs/Web/API/Blob) objects
757
757
  * @param `options` - An object containing properties to be added to the [Blob](https://developer.mozilla.org/en-US/docs/Web/API/Blob)
758
758
  */
759
759
  constructor(parts?: BlobPart[] | Blob, options?: BlobPropertyBag);
760
760
  /**
761
761
  * Create a new view **without 🚫 copying** the underlying data.
762
762
  *
763
- * Similar to [`TypedArray.subarray`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/subarray)
763
+ * Similar to [`BufferSource.subarray`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BufferSource/subarray)
764
764
  *
765
765
  * @param begin The index that sets the beginning of the view.
766
766
  * @param end The index that sets the end of the view.
@@ -1227,7 +1227,9 @@ declare class Request implements BlobInterface {
1227
1227
  }
1228
1228
 
1229
1229
  interface Crypto {
1230
- getRandomValues<T extends TypedArray = TypedArray>(array: T): T;
1230
+ readonly subtle: SubtleCrypto;
1231
+
1232
+ getRandomValues<T extends BufferSource = BufferSource>(array: T): T;
1231
1233
  /**
1232
1234
  * Generate a cryptographically secure random UUID.
1233
1235
  *
@@ -1294,7 +1296,7 @@ declare class TextEncoder {
1294
1296
  * @param src The text to encode.
1295
1297
  * @param dest The array to hold the encode result.
1296
1298
  */
1297
- encodeInto(src?: string, dest?: TypedArray): EncodeIntoResult;
1299
+ encodeInto(src?: string, dest?: BufferSource): EncodeIntoResult;
1298
1300
  }
1299
1301
 
1300
1302
  /**
@@ -1333,9 +1335,9 @@ declare class TextDecoder {
1333
1335
  * internally and emitted after the next call to `textDecoder.decode()`.
1334
1336
  *
1335
1337
  * If `textDecoder.fatal` is `true`, decoding errors that occur will result in a`TypeError` being thrown.
1336
- * @param input An `ArrayBuffer`, `DataView` or `TypedArray` instance containing the encoded data.
1338
+ * @param input An `ArrayBuffer`, `DataView` or `BufferSource` instance containing the encoded data.
1337
1339
  */
1338
- decode(input?: TypedArray | ArrayBuffer): string;
1340
+ decode(input?: BufferSource | ArrayBuffer): string;
1339
1341
  }
1340
1342
 
1341
1343
  /**
@@ -1894,8 +1896,8 @@ interface WebSocket extends EventTarget {
1894
1896
  readonly url: string;
1895
1897
  /** Closes the WebSocket connection, optionally using code as the the WebSocket connection close code and reason as the the WebSocket connection close reason. */
1896
1898
  close(code?: number, reason?: string): void;
1897
- /** Transmits data using the WebSocket connection. data can be a string, an ArrayBuffer, or an ArrayBufferView. */
1898
- send(data: string | ArrayBufferLike | ArrayBufferView): void;
1899
+ /** Transmits data using the WebSocket connection. data can be a string, an ArrayBuffer, or an BufferSource. */
1900
+ send(data: string | ArrayBufferLike | BufferSource): void;
1899
1901
  readonly CLOSED: number;
1900
1902
  readonly CLOSING: number;
1901
1903
  readonly CONNECTING: number;
@@ -2070,7 +2072,7 @@ declare var AbortSignal: {
2070
2072
 
2071
2073
  // type AlgorithmIdentifier = Algorithm | string;
2072
2074
  // type BodyInit = ReadableStream | XMLHttpRequestBodyInit;
2073
- type BufferSource = ArrayBufferView | ArrayBuffer;
2075
+ type BufferSource = ArrayBufferView | ArrayBuffer | SharedArrayBuffer;
2074
2076
  // type COSEAlgorithmIdentifier = number;
2075
2077
  // type CSSNumberish = number;
2076
2078
  // type CanvasImageSource =
@@ -2176,7 +2178,7 @@ declare var Loader: {
2176
2178
  * @param specifier - module specifier as it appears in transpiled source code
2177
2179
  * @param referrer - module specifier that is resolving this specifier
2178
2180
  */
2179
- resolve: (specifier: strin, referrer: string) => string;
2181
+ resolve: (specifier: string, referrer: string) => string;
2180
2182
  };
2181
2183
 
2182
2184
  /** 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. */
@@ -2198,7 +2200,7 @@ interface ReadableStream<R = any> {
2198
2200
  thisArg?: any
2199
2201
  ): void;
2200
2202
  [Symbol.asyncIterator](): AsyncIterableIterator<R>;
2201
- values(options: { preventCancel: boolean }): AsyncIterableIterator<R>;
2203
+ values(options?: { preventCancel: boolean }): AsyncIterableIterator<R>;
2202
2204
  }
2203
2205
 
2204
2206
  declare var ReadableStream: {
@@ -2224,9 +2226,9 @@ interface QueuingStrategyInit {
2224
2226
  }
2225
2227
 
2226
2228
  /** This Streams API interface provides a built-in byte length queuing strategy that can be used when constructing streams. */
2227
- interface ByteLengthQueuingStrategy extends QueuingStrategy<ArrayBufferView> {
2229
+ interface ByteLengthQueuingStrategy extends QueuingStrategy<BufferSource> {
2228
2230
  readonly highWaterMark: number;
2229
- readonly size: QueuingStrategySize<ArrayBufferView>;
2231
+ readonly size: QueuingStrategySize<BufferSource>;
2230
2232
  }
2231
2233
 
2232
2234
  declare var ByteLengthQueuingStrategy: {
@@ -2243,7 +2245,7 @@ interface ReadableStreamDefaultController<R = any> {
2243
2245
 
2244
2246
  interface ReadableStreamDirectController {
2245
2247
  close(error?: Error): void;
2246
- write(data: ArrayBufferView | ArrayBuffer | string): number | Promise<number>;
2248
+ write(data: BufferSource | ArrayBuffer | string): number | Promise<number>;
2247
2249
  end(): number | Promise<number>;
2248
2250
  flush(): number | Promise<number>;
2249
2251
  start(): void;
@@ -2500,7 +2502,329 @@ interface ErrnoException extends Error {
2500
2502
  declare function alert(message?: string): void;
2501
2503
  declare function confirm(message?: string): boolean;
2502
2504
  declare function prompt(message?: string, _default?: string): string | null;
2503
- declare function require(path: string): import(path);
2505
+
2506
+ /*
2507
+
2508
+ Web Crypto API
2509
+
2510
+ */
2511
+
2512
+ type KeyFormat = "jwk" | "pkcs8" | "raw" | "spki";
2513
+ type KeyType = "private" | "public" | "secret";
2514
+ type KeyUsage =
2515
+ | "decrypt"
2516
+ | "deriveBits"
2517
+ | "deriveKey"
2518
+ | "encrypt"
2519
+ | "sign"
2520
+ | "unwrapKey"
2521
+ | "verify"
2522
+ | "wrapKey";
2523
+ type HashAlgorithmIdentifier = AlgorithmIdentifier;
2524
+ type NamedCurve = string;
2525
+
2526
+ type BigInteger = Uint8Array;
2527
+
2528
+ interface KeyAlgorithm {
2529
+ name: string;
2530
+ }
2531
+
2532
+ interface Algorithm {
2533
+ name: string;
2534
+ }
2535
+
2536
+ interface AesCbcParams extends Algorithm {
2537
+ iv: BufferSource;
2538
+ }
2539
+
2540
+ interface AesCtrParams extends Algorithm {
2541
+ counter: BufferSource;
2542
+ length: number;
2543
+ }
2544
+
2545
+ interface AesDerivedKeyParams extends Algorithm {
2546
+ length: number;
2547
+ }
2548
+
2549
+ interface AesGcmParams extends Algorithm {
2550
+ additionalData?: BufferSource;
2551
+ iv: BufferSource;
2552
+ tagLength?: number;
2553
+ }
2554
+
2555
+ interface AesKeyAlgorithm extends KeyAlgorithm {
2556
+ length: number;
2557
+ }
2558
+
2559
+ interface AesKeyGenParams extends Algorithm {
2560
+ length: number;
2561
+ }
2562
+
2563
+ interface EcKeyAlgorithm extends KeyAlgorithm {
2564
+ namedCurve: NamedCurve;
2565
+ }
2566
+
2567
+ interface EcKeyGenParams extends Algorithm {
2568
+ namedCurve: NamedCurve;
2569
+ }
2570
+
2571
+ interface EcKeyImportParams extends Algorithm {
2572
+ namedCurve: NamedCurve;
2573
+ }
2574
+
2575
+ interface EcdhKeyDeriveParams extends Algorithm {
2576
+ public: CryptoKey;
2577
+ }
2578
+
2579
+ interface EcdsaParams extends Algorithm {
2580
+ hash: HashAlgorithmIdentifier;
2581
+ }
2582
+
2583
+ interface JsonWebKey {
2584
+ alg?: string;
2585
+ crv?: string;
2586
+ d?: string;
2587
+ dp?: string;
2588
+ dq?: string;
2589
+ e?: string;
2590
+ ext?: boolean;
2591
+ k?: string;
2592
+ key_ops?: string[];
2593
+ kty?: string;
2594
+ n?: string;
2595
+ oth?: RsaOtherPrimesInfo[];
2596
+ p?: string;
2597
+ q?: string;
2598
+ qi?: string;
2599
+ use?: string;
2600
+ x?: string;
2601
+ y?: string;
2602
+ }
2603
+
2604
+ interface HkdfParams extends Algorithm {
2605
+ hash: HashAlgorithmIdentifier;
2606
+ info: BufferSource;
2607
+ salt: BufferSource;
2608
+ }
2609
+
2610
+ interface HmacImportParams extends Algorithm {
2611
+ hash: HashAlgorithmIdentifier;
2612
+ length?: number;
2613
+ }
2614
+
2615
+ interface HmacKeyAlgorithm extends KeyAlgorithm {
2616
+ hash: KeyAlgorithm;
2617
+ length: number;
2618
+ }
2619
+
2620
+ interface HmacKeyGenParams extends Algorithm {
2621
+ hash: HashAlgorithmIdentifier;
2622
+ length?: number;
2623
+ }
2624
+
2625
+ interface Pbkdf2Params extends Algorithm {
2626
+ hash: HashAlgorithmIdentifier;
2627
+ iterations: number;
2628
+ salt: BufferSource;
2629
+ }
2630
+
2631
+ interface RsaHashedImportParams extends Algorithm {
2632
+ hash: HashAlgorithmIdentifier;
2633
+ }
2634
+
2635
+ interface RsaHashedKeyAlgorithm extends RsaKeyAlgorithm {
2636
+ hash: KeyAlgorithm;
2637
+ }
2638
+
2639
+ interface RsaHashedKeyGenParams extends RsaKeyGenParams {
2640
+ hash: HashAlgorithmIdentifier;
2641
+ }
2642
+
2643
+ interface RsaKeyAlgorithm extends KeyAlgorithm {
2644
+ modulusLength: number;
2645
+ publicExponent: BigInteger;
2646
+ }
2647
+
2648
+ interface RsaKeyGenParams extends Algorithm {
2649
+ modulusLength: number;
2650
+ publicExponent: BigInteger;
2651
+ }
2652
+
2653
+ interface RsaOaepParams extends Algorithm {
2654
+ label?: BufferSource;
2655
+ }
2656
+
2657
+ interface RsaOtherPrimesInfo {
2658
+ d?: string;
2659
+ r?: string;
2660
+ t?: string;
2661
+ }
2662
+
2663
+ interface CryptoKeyPair {
2664
+ privateKey: CryptoKey;
2665
+ publicKey: CryptoKey;
2666
+ }
2667
+
2668
+ type AlgorithmIdentifier = Algorithm | string;
2669
+
2670
+ /**
2671
+ * This Web Crypto API interface provides a number of low-level cryptographic functions. It is accessed via the Crypto.subtle properties available in a window context (via Window.crypto).
2672
+ */
2673
+ interface SubtleCrypto {
2674
+ decrypt(
2675
+ algorithm:
2676
+ | AlgorithmIdentifier
2677
+ | RsaOaepParams
2678
+ | AesCtrParams
2679
+ | AesCbcParams
2680
+ | AesGcmParams,
2681
+ key: CryptoKey,
2682
+ data: BufferSource
2683
+ ): Promise<ArrayBuffer>;
2684
+ deriveBits(
2685
+ algorithm:
2686
+ | AlgorithmIdentifier
2687
+ | EcdhKeyDeriveParams
2688
+ | HkdfParams
2689
+ | Pbkdf2Params,
2690
+ baseKey: CryptoKey,
2691
+ length: number
2692
+ ): Promise<ArrayBuffer>;
2693
+ deriveKey(
2694
+ algorithm:
2695
+ | AlgorithmIdentifier
2696
+ | EcdhKeyDeriveParams
2697
+ | HkdfParams
2698
+ | Pbkdf2Params,
2699
+ baseKey: CryptoKey,
2700
+ derivedKeyType:
2701
+ | AlgorithmIdentifier
2702
+ | AesDerivedKeyParams
2703
+ | HmacImportParams
2704
+ | HkdfParams
2705
+ | Pbkdf2Params,
2706
+ extractable: boolean,
2707
+ keyUsages: KeyUsage[]
2708
+ ): Promise<CryptoKey>;
2709
+ digest(
2710
+ algorithm: AlgorithmIdentifier,
2711
+ data: BufferSource
2712
+ ): Promise<ArrayBuffer>;
2713
+ encrypt(
2714
+ algorithm:
2715
+ | AlgorithmIdentifier
2716
+ | RsaOaepParams
2717
+ | AesCtrParams
2718
+ | AesCbcParams
2719
+ | AesGcmParams,
2720
+ key: CryptoKey,
2721
+ data: BufferSource
2722
+ ): Promise<ArrayBuffer>;
2723
+ exportKey(format: "jwk", key: CryptoKey): Promise<JsonWebKey>;
2724
+ exportKey(
2725
+ format: Exclude<KeyFormat, "jwk">,
2726
+ key: CryptoKey
2727
+ ): Promise<ArrayBuffer>;
2728
+ generateKey(
2729
+ algorithm: RsaHashedKeyGenParams | EcKeyGenParams,
2730
+ extractable: boolean,
2731
+ keyUsages: ReadonlyArray<KeyUsage>
2732
+ ): Promise<CryptoKeyPair>;
2733
+ generateKey(
2734
+ algorithm: AesKeyGenParams | HmacKeyGenParams | Pbkdf2Params,
2735
+ extractable: boolean,
2736
+ keyUsages: ReadonlyArray<KeyUsage>
2737
+ ): Promise<CryptoKey>;
2738
+ generateKey(
2739
+ algorithm: AlgorithmIdentifier,
2740
+ extractable: boolean,
2741
+ keyUsages: KeyUsage[]
2742
+ ): Promise<CryptoKeyPair | CryptoKey>;
2743
+ importKey(
2744
+ format: "jwk",
2745
+ keyData: JsonWebKey,
2746
+ algorithm:
2747
+ | AlgorithmIdentifier
2748
+ | RsaHashedImportParams
2749
+ | EcKeyImportParams
2750
+ | HmacImportParams
2751
+ | AesKeyAlgorithm,
2752
+ extractable: boolean,
2753
+ keyUsages: ReadonlyArray<KeyUsage>
2754
+ ): Promise<CryptoKey>;
2755
+ importKey(
2756
+ format: Exclude<KeyFormat, "jwk">,
2757
+ keyData: BufferSource,
2758
+ algorithm:
2759
+ | AlgorithmIdentifier
2760
+ | RsaHashedImportParams
2761
+ | EcKeyImportParams
2762
+ | HmacImportParams
2763
+ | AesKeyAlgorithm,
2764
+ extractable: boolean,
2765
+ keyUsages: KeyUsage[]
2766
+ ): Promise<CryptoKey>;
2767
+ sign(
2768
+ algorithm: AlgorithmIdentifier | RsaPssParams | EcdsaParams,
2769
+ key: CryptoKey,
2770
+ data: BufferSource
2771
+ ): Promise<ArrayBuffer>;
2772
+ unwrapKey(
2773
+ format: KeyFormat,
2774
+ wrappedKey: BufferSource,
2775
+ unwrappingKey: CryptoKey,
2776
+ unwrapAlgorithm:
2777
+ | AlgorithmIdentifier
2778
+ | RsaOaepParams
2779
+ | AesCtrParams
2780
+ | AesCbcParams
2781
+ | AesGcmParams,
2782
+ unwrappedKeyAlgorithm:
2783
+ | AlgorithmIdentifier
2784
+ | RsaHashedImportParams
2785
+ | EcKeyImportParams
2786
+ | HmacImportParams
2787
+ | AesKeyAlgorithm,
2788
+ extractable: boolean,
2789
+ keyUsages: KeyUsage[]
2790
+ ): Promise<CryptoKey>;
2791
+ verify(
2792
+ algorithm: AlgorithmIdentifier | RsaPssParams | EcdsaParams,
2793
+ key: CryptoKey,
2794
+ signature: BufferSource,
2795
+ data: BufferSource
2796
+ ): Promise<boolean>;
2797
+ wrapKey(
2798
+ format: KeyFormat,
2799
+ key: CryptoKey,
2800
+ wrappingKey: CryptoKey,
2801
+ wrapAlgorithm:
2802
+ | AlgorithmIdentifier
2803
+ | RsaOaepParams
2804
+ | AesCtrParams
2805
+ | AesCbcParams
2806
+ | AesGcmParams
2807
+ ): Promise<ArrayBuffer>;
2808
+ }
2809
+
2810
+ interface RsaPssParams extends Algorithm {
2811
+ saltLength: number;
2812
+ }
2813
+
2814
+ /**
2815
+ * The CryptoKey dictionary of the Web Crypto API represents a cryptographic key.
2816
+ */
2817
+ interface CryptoKey {
2818
+ readonly algorithm: KeyAlgorithm;
2819
+ readonly extractable: boolean;
2820
+ readonly type: KeyType;
2821
+ readonly usages: KeyUsage[];
2822
+ }
2823
+
2824
+ declare var CryptoKey: {
2825
+ prototype: CryptoKey;
2826
+ new (): CryptoKey;
2827
+ };
2504
2828
 
2505
2829
 
2506
2830
  // ./string_decoder.d.ts
@@ -11710,6 +12034,9 @@ declare module "stream" {
11710
12034
  read?(this: Readable, size: number): void;
11711
12035
  }
11712
12036
  class Readable<R = any> extends Stream implements ReadableStream {
12037
+ // TODO: improve type later
12038
+ values: any;
12039
+
11713
12040
  readonly locked: boolean;
11714
12041
  cancel(reason?: any): Promise<void>;
11715
12042
  getReader(): ReadableStreamDefaultReader<R>;
@@ -13453,6 +13780,17 @@ declare module "bun" {
13453
13780
  */
13454
13781
  export function pathToFileURL(path: string): URL;
13455
13782
 
13783
+ export interface Peek {
13784
+ <T = undefined>(promise: T | Promise<T>): Promise<T> | T;
13785
+ status<T = undefined>(
13786
+ promise: T | Promise<T>
13787
+ ): "pending" | "fulfilled" | "rejected";
13788
+ }
13789
+ /**
13790
+ * Extract the value from the Promise in the same tick of the event loop
13791
+ */
13792
+ export const peek: Peek;
13793
+
13456
13794
  /**
13457
13795
  * Convert a {@link URL} to a filesystem path.
13458
13796
  * @param url The URL to convert.
@@ -14031,7 +14369,7 @@ declare module "bun" {
14031
14369
  publishText(
14032
14370
  topic: string,
14033
14371
  data: string,
14034
- compress?: bool
14372
+ compress?: boolean
14035
14373
  ): ServerWebSocketSendStatus;
14036
14374
 
14037
14375
  /**
@@ -14434,19 +14772,7 @@ declare module "bun" {
14434
14772
  syscall?: string;
14435
14773
  }
14436
14774
 
14437
- export interface SSLAdvancedOptions {
14438
- passphrase?: string;
14439
- caFile?: string;
14440
- dhParamsFile?: string;
14441
-
14442
- /**
14443
- * This sets `OPENSSL_RELEASE_BUFFERS` to 1.
14444
- * It reduces overall performance but saves some memory.
14445
- * @default false
14446
- */
14447
- lowMemoryMode?: boolean;
14448
- }
14449
- interface SSLOptions {
14775
+ interface TLSOptions {
14450
14776
  /**
14451
14777
  * File path to a TLS key
14452
14778
  *
@@ -14459,19 +14785,29 @@ declare module "bun" {
14459
14785
  * To enable TLS, this option is required.
14460
14786
  */
14461
14787
  certFile: string;
14788
+
14789
+ passphrase?: string;
14790
+ caFile?: string;
14791
+ dhParamsFile?: string;
14792
+
14793
+ /**
14794
+ * This sets `OPENSSL_RELEASE_BUFFERS` to 1.
14795
+ * It reduces overall performance but saves some memory.
14796
+ * @default false
14797
+ */
14798
+ lowMemoryMode?: boolean;
14462
14799
  }
14463
14800
 
14464
- export type SSLServeOptions<WebSocketDataType = undefined> = (
14801
+ export type TLSServeOptions<WebSocketDataType = undefined> = (
14465
14802
  | WebSocketServeOptions<WebSocketDataType>
14466
14803
  | ServerWebSocket
14467
14804
  ) &
14468
- SSLOptions &
14469
- SSLAdvancedOptions & {
14805
+ TLSOptions & {
14470
14806
  /**
14471
14807
  * The keys are [SNI](https://en.wikipedia.org/wiki/Server_Name_Indication) hostnames.
14472
14808
  * The values are SSL options objects.
14473
14809
  */
14474
- serverNames: Record<string, SSLOptions & SSLAdvancedOptions>;
14810
+ serverNames: Record<string, TLSOptions>;
14475
14811
  };
14476
14812
 
14477
14813
  /**
@@ -14616,7 +14952,7 @@ declare module "bun" {
14616
14952
  }
14617
14953
 
14618
14954
  export type Serve<WebSocketDataType = undefined> =
14619
- | SSLServeOptions<WebSocketDataType>
14955
+ | TLSServeOptions<WebSocketDataType>
14620
14956
  | WebSocketServeOptions<WebSocketDataType>
14621
14957
  | ServeOptions;
14622
14958
 
@@ -15387,7 +15723,180 @@ declare module "bun" {
15387
15723
 
15388
15724
  var plugin: BunPlugin;
15389
15725
 
15390
- declare namespace SpawnOptions {
15726
+ interface Socket<Data = undefined> {
15727
+ /**
15728
+ * Write `data` to the socket
15729
+ *
15730
+ * @param data The data to write to the socket
15731
+ * @param byteOffset The offset in the buffer to start writing from (defaults to 0)
15732
+ * @param byteLength The number of bytes to write (defaults to the length of the buffer)
15733
+ *
15734
+ * When passed a string, `byteOffset` and `byteLength` refer to the UTF-8 offset, not the string character offset.
15735
+ *
15736
+ * This is unbuffered as of Bun v0.2.2. That means individual write() calls
15737
+ * will be slow. In the future, Bun will buffer writes and flush them at the
15738
+ * end of the tick, when the event loop is idle, or sooner if the buffer is full.
15739
+ */
15740
+ write(
15741
+ data: string | BufferSource,
15742
+ byteOffset?: number,
15743
+ byteLength?: number
15744
+ ): number;
15745
+
15746
+ /**
15747
+ * The data context for the socket.
15748
+ */
15749
+ data: Data;
15750
+
15751
+ /**
15752
+ * Like {@link Socket.write} except it includes a TCP FIN packet
15753
+ *
15754
+ * Use it to send your last message and close the connection.
15755
+ */
15756
+ end(
15757
+ data?: string | BufferSource,
15758
+ byteOffset?: number,
15759
+ byteLength?: number
15760
+ ): number;
15761
+
15762
+ /**
15763
+ * Close the socket immediately
15764
+ */
15765
+ end(): void;
15766
+
15767
+ /**
15768
+ * Keep Bun's process alive at least until this socket is closed
15769
+ *
15770
+ * After the socket has closed, the socket is unref'd, the process may exit,
15771
+ * and this becomes a no-op
15772
+ */
15773
+ ref(): void;
15774
+
15775
+ /**
15776
+ * Set a timeout until the socket automatically closes.
15777
+ *
15778
+ * To reset the timeout, call this function again.
15779
+ *
15780
+ * When a timeout happens, the `timeout` callback is called and the socket is closed.
15781
+ */
15782
+ timeout(seconds: number): void;
15783
+
15784
+ /**
15785
+ * Shutdown writes to a socket
15786
+ *
15787
+ * This makes the socket a half-closed socket. It can still receive data.
15788
+ *
15789
+ * This calls [shutdown(2)](https://man7.org/linux/man-pages/man2/shutdown.2.html) internally
15790
+ */
15791
+ shutdown(halfClose?: boolean): void;
15792
+
15793
+ readonly readyState: "open" | "closing" | "closed";
15794
+
15795
+ /**
15796
+ * Allow Bun's process to exit even if this socket is still open
15797
+ *
15798
+ * After the socket has closed, this function does nothing.
15799
+ */
15800
+ unref(): void;
15801
+
15802
+ /**
15803
+ * Reset the socket's callbacks. This is useful with `bun --hot` to facilitate hot reloading.
15804
+ *
15805
+ * This will apply to all sockets from the same {@link Listener}. it is per socket only for {@link Bun.connect}.
15806
+ */
15807
+ reload(handler: SocketHandler): void;
15808
+
15809
+ /**
15810
+ * Get the server that created this socket
15811
+ *
15812
+ * This will return undefined if the socket was created by {@link Bun.connect} or if the listener has already closed.
15813
+ */
15814
+ readonly listener?: SocketListener;
15815
+ }
15816
+
15817
+ interface SocketListener<Options extends SocketOptions = SocketOptions> {
15818
+ stop(): void;
15819
+ ref(): void;
15820
+ unref(): void;
15821
+ reload(options: Pick<Partial<Options>, "socket">): void;
15822
+ data: Options["data"];
15823
+ }
15824
+ interface TCPSocketListener<Options extends TCPSocketOptions<unknown>>
15825
+ extends SocketListener<Options> {
15826
+ readonly port: number;
15827
+ readonly hostname: string;
15828
+ }
15829
+ interface UnixSocketListener<Options extends UnixSocketOptions<unknown>>
15830
+ extends SocketListener<Options> {
15831
+ readonly unix: string;
15832
+ }
15833
+
15834
+ interface TCPSocket extends Socket {}
15835
+ interface TLSSocket extends Socket {}
15836
+
15837
+ interface SocketHandler<Data = unknown> {
15838
+ open(socket: Socket<Data>): void | Promise<void>;
15839
+ close?(socket: Socket<Data>): void | Promise<void>;
15840
+ error?(socket: Socket<Data>, error: Error): void | Promise<void>;
15841
+ data?(socket: Socket<Data>, data: BufferSource): void | Promise<void>;
15842
+ drain?(socket: Socket<Data>): void | Promise<void>;
15843
+ }
15844
+
15845
+ interface SocketOptions<Data = unknown> {
15846
+ socket: SocketHandler<Data>;
15847
+ tls?: TLSOptions;
15848
+ data: Data;
15849
+ }
15850
+ interface TCPSocketOptions<Data = undefined> extends SocketOptions<Data> {
15851
+ hostname: string;
15852
+ port: number;
15853
+ }
15854
+
15855
+ interface UnixSocketOptions<Data = undefined> extends SocketOptions<Data> {
15856
+ unix: string;
15857
+ }
15858
+
15859
+ /**
15860
+ *
15861
+ * Create a TCP client that connects to a server
15862
+ *
15863
+ * @param options The options to use when creating the client
15864
+ * @param options.socket The socket handler to use
15865
+ * @param options.data The per-instance data context
15866
+ * @param options.hostname The hostname to connect to
15867
+ * @param options.port The port to connect to
15868
+ * @param options.tls The TLS configuration object
15869
+ * @param options.unix The unix socket to connect to
15870
+ *
15871
+ */
15872
+ export function connect<Data = undefined>(
15873
+ options: TCPSocketOptions<Data>
15874
+ ): Promise<TCPSocketListener<typeof options>>;
15875
+ export function connect<Data = undefined>(
15876
+ options: UnixSocketOptions<Data>
15877
+ ): Promise<UnixSocketListener<typeof options>>;
15878
+
15879
+ /**
15880
+ *
15881
+ * Create a TCP server that listens on a port
15882
+ *
15883
+ * @param options The options to use when creating the server
15884
+ * @param options.socket The socket handler to use
15885
+ * @param options.data The per-instance data context
15886
+ * @param options.hostname The hostname to connect to
15887
+ * @param options.port The port to connect to
15888
+ * @param options.tls The TLS configuration object
15889
+ * @param options.unix The unix socket to connect to
15890
+ *
15891
+ */
15892
+ export function listen<Data = undefined>(
15893
+ options: TCPSocketOptions<Data>
15894
+ ): TCPSocketListener<typeof options>;
15895
+ export function listen<Data = undefined>(
15896
+ options: UnixSocketOptions<Data>
15897
+ ): UnixSocketListener<typeof options>;
15898
+
15899
+ namespace SpawnOptions {
15391
15900
  type Readable =
15392
15901
  | "inherit"
15393
15902
  | "pipe"