cf-workers-native 0.1.5 → 0.1.7

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 (197) hide show
  1. package/README.md +2 -2
  2. package/dist/browser/documents/page-source.js +1 -1
  3. package/dist/browser/headers/index.d.ts +2 -0
  4. package/dist/browser/headers/index.js +5 -0
  5. package/dist/browser/response/index.d.ts +2 -0
  6. package/dist/browser/response/index.js +11 -3
  7. package/dist/browser/structured-clone/index.d.ts +3 -0
  8. package/dist/browser/structured-clone/index.js +75 -7
  9. package/dist/browser/websocket/attachment-size.d.ts +3 -0
  10. package/dist/browser/websocket/attachment-size.js +336 -0
  11. package/dist/browser/websocket/brand.d.ts +4 -0
  12. package/dist/browser/websocket/brand.js +16 -0
  13. package/dist/browser/websocket/close-event.d.ts +7 -0
  14. package/dist/browser/websocket/close-event.js +30 -0
  15. package/dist/browser/websocket/compression.d.ts +5 -0
  16. package/dist/browser/websocket/compression.js +124 -0
  17. package/dist/browser/websocket/conversion.d.ts +2 -0
  18. package/dist/browser/websocket/conversion.js +13 -0
  19. package/dist/browser/websocket/endpoint.d.ts +39 -0
  20. package/dist/browser/websocket/endpoint.js +379 -0
  21. package/dist/browser/websocket/frames.d.ts +21 -0
  22. package/dist/browser/websocket/frames.js +121 -0
  23. package/dist/browser/websocket/native.d.ts +14 -0
  24. package/dist/browser/websocket/native.js +226 -0
  25. package/dist/cloudflare/apis/cloudflare-node/index.js +11 -23
  26. package/dist/compat/crypto/LICENSE-3RD-PARTY.md +20 -0
  27. package/dist/compat/crypto/LICENSE.boringssl +238 -0
  28. package/dist/compat/crypto/LICENSE.ncrypto +21 -0
  29. package/dist/compat/crypto/asymmetric.js +141 -68
  30. package/dist/compat/crypto/cipher-handle.d.ts +27 -0
  31. package/dist/compat/crypto/cipher-handle.js +239 -0
  32. package/dist/compat/crypto/cipher-info.d.ts +12 -0
  33. package/dist/compat/crypto/cipher-info.js +324 -0
  34. package/dist/compat/crypto/constants.d.ts +62 -0
  35. package/dist/compat/crypto/constants.js +68 -0
  36. package/dist/compat/crypto/dh-handle.d.ts +17 -0
  37. package/dist/compat/crypto/dh-handle.js +180 -0
  38. package/dist/compat/crypto/dh.d.ts +53 -0
  39. package/dist/compat/crypto/dh.js +138 -0
  40. package/dist/compat/crypto/digest-stream.d.ts +15 -0
  41. package/dist/compat/crypto/digest-stream.js +213 -0
  42. package/dist/compat/crypto/digests.d.ts +3 -0
  43. package/dist/compat/crypto/digests.js +32 -0
  44. package/dist/compat/crypto/ecdh.d.ts +16 -6
  45. package/dist/compat/crypto/hash-handle.d.ts +23 -0
  46. package/dist/compat/crypto/hash-handle.js +70 -0
  47. package/dist/compat/crypto/hash.d.ts +37 -0
  48. package/dist/compat/crypto/hash.js +218 -0
  49. package/dist/compat/crypto/hkdf.js +40 -14
  50. package/dist/compat/crypto/index.d.cts +168 -1
  51. package/dist/compat/crypto/index.d.ts +191 -8
  52. package/dist/compat/crypto/index.js +36 -23
  53. package/dist/compat/crypto/key-errors.d.ts +10 -0
  54. package/dist/compat/crypto/key-errors.js +17 -0
  55. package/dist/compat/crypto/key-export.d.ts +8 -0
  56. package/dist/compat/crypto/key-export.js +49 -0
  57. package/dist/compat/crypto/key-formats.d.ts +2 -1
  58. package/dist/compat/crypto/key-formats.js +162 -31
  59. package/dist/compat/crypto/key-options.d.ts +1 -0
  60. package/dist/compat/crypto/key-options.js +74 -0
  61. package/dist/compat/crypto/key-state.d.ts +1 -0
  62. package/dist/compat/crypto/keys.d.ts +20 -12
  63. package/dist/compat/crypto/keys.js +129 -59
  64. package/dist/compat/crypto/metadata.d.ts +9 -0
  65. package/dist/compat/crypto/metadata.js +89 -2
  66. package/dist/compat/crypto/native-cipher.d.ts +17 -0
  67. package/dist/compat/crypto/native-cipher.js +192 -0
  68. package/dist/compat/crypto/native-crypto.d.ts +14 -1
  69. package/dist/compat/crypto/native-crypto.js +182 -6
  70. package/dist/compat/crypto/pbkdf2-handle.d.ts +4 -0
  71. package/dist/compat/crypto/pbkdf2-handle.js +26 -0
  72. package/dist/compat/crypto/pbkdf2.d.ts +5 -0
  73. package/dist/compat/crypto/pbkdf2.js +60 -0
  74. package/dist/compat/crypto/prime.js +4 -4
  75. package/dist/compat/crypto/random.d.ts +11 -0
  76. package/dist/compat/crypto/random.js +86 -6
  77. package/dist/compat/crypto/rsa.d.ts +17 -1
  78. package/dist/compat/crypto/rsa.js +8 -4
  79. package/dist/compat/crypto/scrypt.d.ts +3 -3
  80. package/dist/compat/crypto/scrypt.js +25 -23
  81. package/dist/compat/crypto/sign-handle.d.ts +3 -3
  82. package/dist/compat/crypto/sign-handle.js +55 -11
  83. package/dist/compat/crypto/signatures.d.ts +17 -11
  84. package/dist/compat/crypto/subtle-digest.d.ts +1 -0
  85. package/dist/compat/crypto/subtle-digest.js +56 -0
  86. package/dist/compat/crypto/symmetric.d.ts +40 -24
  87. package/dist/compat/crypto/symmetric.js +229 -94
  88. package/dist/compat/crypto/validation.d.ts +4 -0
  89. package/dist/compat/crypto/validation.js +29 -2
  90. package/dist/compat/crypto/webcrypto.d.ts +13 -0
  91. package/dist/compat/crypto/webcrypto.js +51 -0
  92. package/dist/compat/crypto/x509.js +2 -1
  93. package/dist/compat/events/index.js +1 -1
  94. package/dist/compat/fs/LICENSE-3RD-PARTY.md +11 -0
  95. package/dist/compat/fs/LICENSE-APACHE +177 -0
  96. package/dist/compat/fs/directory-backend.d.ts +6 -0
  97. package/dist/compat/fs/directory-backend.js +51 -0
  98. package/dist/compat/fs/directory.d.ts +37 -0
  99. package/dist/compat/fs/directory.js +137 -0
  100. package/dist/compat/fs/embedded.d.ts +1 -1
  101. package/dist/compat/fs/embedded.js +7 -2
  102. package/dist/compat/fs/file-handle.d.ts +75 -0
  103. package/dist/compat/fs/file-handle.js +249 -0
  104. package/dist/compat/fs/glob-internal.d.ts +15 -0
  105. package/dist/compat/fs/glob-internal.js +417 -0
  106. package/dist/compat/fs/glob.d.ts +10 -0
  107. package/dist/compat/fs/glob.js +71 -0
  108. package/dist/compat/fs/index.d.ts +6 -3
  109. package/dist/compat/fs/index.js +596 -50
  110. package/dist/compat/fs/io-validation.d.ts +14 -0
  111. package/dist/compat/fs/io-validation.js +130 -0
  112. package/dist/compat/fs/io.d.ts +1 -0
  113. package/dist/compat/fs/io.js +283 -0
  114. package/dist/compat/fs/promises/index.d.ts +3 -1
  115. package/dist/compat/fs/public-types.d.ts +120 -0
  116. package/dist/compat/fs/public-types.js +1 -0
  117. package/dist/compat/fs/stream-errors.d.ts +6 -0
  118. package/dist/compat/fs/stream-errors.js +8 -0
  119. package/dist/compat/fs/stream-utils.d.ts +16 -0
  120. package/dist/compat/fs/stream-utils.js +29 -0
  121. package/dist/compat/fs/streams.d.ts +104 -0
  122. package/dist/compat/fs/streams.js +794 -0
  123. package/dist/compat/fs/writable.d.ts +8 -0
  124. package/dist/compat/fs/writable.js +412 -22
  125. package/dist/compat/http/index.d.ts +11 -28
  126. package/dist/compat/http/index.js +81 -125
  127. package/dist/compat/http/server-options.d.ts +1 -0
  128. package/dist/compat/http/server-options.js +87 -0
  129. package/dist/compat/http/unique-headers.d.ts +1 -0
  130. package/dist/compat/http/unique-headers.js +21 -0
  131. package/dist/compat/http-outgoing/index.d.ts +2 -1
  132. package/dist/compat/http-outgoing/index.js +2 -17
  133. package/dist/compat/http-server/index.d.ts +2 -2
  134. package/dist/compat/http-server/index.js +4 -85
  135. package/dist/compat/https/index.d.ts +4 -2
  136. package/dist/compat/net/sockets.js +98 -20
  137. package/dist/compat/net/validators.d.ts +1 -0
  138. package/dist/compat/net/validators.js +8 -0
  139. package/dist/compat/process/configuration.d.ts +113 -0
  140. package/dist/compat/process/configuration.js +115 -0
  141. package/dist/compat/process/directory.d.ts +4 -0
  142. package/dist/compat/process/directory.js +28 -0
  143. package/dist/compat/process/files.d.ts +3 -0
  144. package/dist/compat/process/files.js +10 -0
  145. package/dist/compat/process/index.d.ts +15 -13
  146. package/dist/compat/process/index.js +47 -74
  147. package/dist/compat/process/scheduling.d.ts +2 -0
  148. package/dist/compat/process/scheduling.js +60 -0
  149. package/dist/compat/process/state.js +1 -0
  150. package/dist/compat/process/stdio.d.ts +18 -0
  151. package/dist/compat/process/stdio.js +66 -0
  152. package/dist/compat/stream/destroy.d.ts +1 -0
  153. package/dist/compat/stream/destroy.js +21 -1
  154. package/dist/compat/stream/readable.js +10 -2
  155. package/dist/compat/tls/jsstream.js +16 -6
  156. package/dist/compat/tls/wrap.d.ts +1 -1
  157. package/dist/compat/tls/wrap.js +0 -3
  158. package/dist/compat/util/inspect.js +5 -1
  159. package/dist/compat/zlib/LICENSE-3RD-PARTY.md +11 -0
  160. package/dist/compat/zlib/LICENSE-APACHE +177 -0
  161. package/dist/compat/zlib/LICENSE.zlib +22 -0
  162. package/dist/compat/zlib/classic-base.cjs +600 -0
  163. package/dist/compat/zlib/classic-base.d.cts +39 -0
  164. package/dist/compat/zlib/classic-binding.d.ts +28 -0
  165. package/dist/compat/zlib/classic-binding.js +105 -0
  166. package/dist/compat/zlib/classic.d.ts +44 -0
  167. package/dist/compat/zlib/classic.js +199 -0
  168. package/dist/compat/zlib/codec-binding.d.ts +33 -0
  169. package/dist/compat/zlib/codec-binding.js +141 -0
  170. package/dist/compat/zlib/compression.d.ts +22 -51
  171. package/dist/compat/zlib/compression.js +129 -218
  172. package/dist/compat/zlib/errors.d.ts +21 -0
  173. package/dist/compat/zlib/errors.js +32 -0
  174. package/dist/compat/zlib/index.d.ts +367 -3
  175. package/dist/compat/zlib/index.js +28 -11
  176. package/dist/compat/zlib/input.d.ts +16 -0
  177. package/dist/compat/zlib/input.js +132 -0
  178. package/dist/compat/zlib/native-codecs.d.ts +7 -0
  179. package/dist/compat/zlib/native-codecs.js +22 -10
  180. package/dist/compat/zlib/native-zlib.d.ts +5 -0
  181. package/dist/compat/zlib/native-zlib.js +52 -0
  182. package/dist/compat/zlib/public-types.d.ts +48 -0
  183. package/dist/compat/zlib/public-types.js +1 -0
  184. package/dist/compat/zlib/validators.d.ts +3 -0
  185. package/dist/compat/zlib/validators.js +18 -0
  186. package/dist/shared/buffer-source.d.ts +5 -0
  187. package/dist/shared/buffer-source.js +70 -0
  188. package/dist/shared/globals/platform.d.ts +8 -0
  189. package/dist/shared/globals/platform.js +1 -0
  190. package/dist/shared/globals/web.js +5 -0
  191. package/dist/shared/request-context.d.ts +2 -0
  192. package/dist/shared/request-context.js +11 -0
  193. package/dist/shared/request-scope.js +4 -1
  194. package/dist/tooling/shared.js +6 -0
  195. package/dist/tooling/vite.js +4 -0
  196. package/native/android/src/main/java/jp/rdlabo/workerscapacitor/WorkerSockets.java +38 -7
  197. package/package.json +2 -2
@@ -1,13 +1,20 @@
1
+ import { storeHTTPOptions } from "./server-options.js";
2
+ import { WebSocket } from "../../browser/websocket/endpoint.js";
3
+ import { CloseEvent } from "../../browser/websocket/close-event.js";
1
4
  import { asyncDispose } from "../../shared/globals/symbols.js";
2
5
  // Copyright (c) 2017-2026 Cloudflare, Inc.
3
6
  // Licensed under the Apache 2.0 license found in the LICENSE file or at:
4
7
  // https://opensource.org/licenses/Apache-2.0
5
8
  // Copyright Joyent and Node contributors. All rights reserved. MIT license.
6
9
  // Adapted from workerd v1.20261012.1 node:http compatibility sources.
7
- import { Readable, Writable } from "node:stream";
10
+ import { Readable, Writable, getDefaultHighWaterMark } from "node:stream";
8
11
  import { EventEmitter } from "node:events";
9
12
  import { Buffer } from "node:buffer";
10
13
  import { ephemeral, hasHandler, kSetIncomingBody, kSetIncomingFetchResponse, kSetIncomingSocket, release, setHandler, } from "./registry.js";
14
+ import { validateInteger, validateString, validateBoolean, validateNumber, validateObject, validateOneOf, } from "../net/validators.js";
15
+ import { AbortError, ERR_INVALID_ARG_TYPE } from "../net/errors.js";
16
+ import { kIncomingMessage, kServerResponse, kUniqueHeaders, } from "./symbols.js";
17
+ import { parseUniqueHeadersOption } from "./unique-headers.js";
11
18
  export const METHODS = [
12
19
  "ACL",
13
20
  "BIND",
@@ -116,12 +123,7 @@ function coded(error, code) {
116
123
  }
117
124
  function validateType(value, type, name) {
118
125
  if (typeof value !== type)
119
- throw coded(new TypeError(`${name} must be of type ${type}`), "ERR_INVALID_ARG_TYPE");
120
- }
121
- function validateInteger(value, name, minimum = Number.MIN_SAFE_INTEGER) {
122
- validateType(value, "number", name);
123
- if (!Number.isSafeInteger(value) || value < minimum)
124
- throw coded(new RangeError(`${name} is out of range`), "ERR_OUT_OF_RANGE");
126
+ throw new ERR_INVALID_ARG_TYPE(name, type, value);
125
127
  }
126
128
  function unsupportedOption(name) {
127
129
  throw coded(new Error(`The ${name} option is not implemented`), "ERR_OPTION_NOT_IMPLEMENTED");
@@ -129,18 +131,6 @@ function unsupportedOption(name) {
129
131
  function notImplemented(name) {
130
132
  return coded(new Error(`The ${name} method is not implemented`), "ERR_METHOD_NOT_IMPLEMENTED");
131
133
  }
132
- function validateString(value, name) {
133
- if (typeof value !== "string")
134
- throw coded(new TypeError(`The "${name}" argument must be of type string.`), "ERR_INVALID_ARG_TYPE");
135
- }
136
- function validateBoolean(value, name) {
137
- if (typeof value !== "boolean")
138
- throw coded(new TypeError(`The "${name}" argument must be of type boolean.`), "ERR_INVALID_ARG_TYPE");
139
- }
140
- function validateNumber(value, name) {
141
- if (typeof value !== "number")
142
- throw coded(new TypeError(`The "${name}" argument must be of type number.`), "ERR_INVALID_ARG_TYPE");
143
- }
144
134
  export function validateHeaderName(name, label = "Header name") {
145
135
  if (typeof name !== "string" || !/^[!#$%&'*+.^_`|~0-9A-Za-z-]+$/.test(name))
146
136
  throw coded(new TypeError(`${label} must be a valid HTTP token ["${String(name)}"]`), "ERR_INVALID_HTTP_TOKEN");
@@ -158,6 +148,7 @@ export class OutgoingMessage extends Writable {
158
148
  strictContentLength = false;
159
149
  bytesWritten = 0;
160
150
  endingChunk = false;
151
+ #rejectNonStandardBodyWrites = false;
161
152
  chunkedEncoding = false;
162
153
  sendDate = false;
163
154
  shouldKeepAlive = true;
@@ -182,6 +173,8 @@ export class OutgoingMessage extends Writable {
182
173
  _onPendingData = () => { };
183
174
  constructor(_req, options = {}) {
184
175
  super({ autoDestroy: false, highWaterMark: options.highWaterMark });
176
+ this.#rejectNonStandardBodyWrites =
177
+ options.rejectNonStandardBodyWrites ?? false;
185
178
  }
186
179
  get headersSent() {
187
180
  return this._header !== null || this._headerSent;
@@ -286,6 +279,13 @@ export class OutgoingMessage extends Writable {
286
279
  throw coded(new Error(`Response body's content-length of ${total} byte(s) does not match the content-length of ${expected} byte(s) set in header`), "ERR_HTTP_CONTENT_LENGTH_MISMATCH");
287
280
  this.bytesWritten = total;
288
281
  }
282
+ #checkBodyAllowed() {
283
+ if (!this._hasBody &&
284
+ this.#rejectNonStandardBodyWrites &&
285
+ !this.finished &&
286
+ !this.destroyed)
287
+ throw coded(new Error("Request with GET/HEAD method cannot have body"), "ERR_HTTP_BODY_NOT_ALLOWED");
288
+ }
289
289
  write(chunk, encoding, callback) {
290
290
  if (chunk === null)
291
291
  throw coded(new TypeError("May not write null values to stream"), "ERR_STREAM_NULL_VALUES");
@@ -295,6 +295,7 @@ export class OutgoingMessage extends Writable {
295
295
  this.#checkContentLength(chunk, encoding, false);
296
296
  if (!this._header && !this.finished && !this.destroyed)
297
297
  this._implicitHeader();
298
+ this.#checkBodyAllowed();
298
299
  return super.write(chunk, encoding, callback);
299
300
  }
300
301
  destroy(error) {
@@ -342,10 +343,20 @@ export class OutgoingMessage extends Writable {
342
343
  !this.finished &&
343
344
  !this.destroyed)
344
345
  this._implicitHeader();
346
+ if (chunk &&
347
+ typeof chunk !== "function" &&
348
+ !this.finished &&
349
+ !this.destroyed) {
350
+ if (typeof chunk !== "string" && !(chunk instanceof Uint8Array))
351
+ throw new ERR_INVALID_ARG_TYPE("chunk", ["string", "Buffer", "Uint8Array"], chunk);
352
+ if (!this._header)
353
+ this._implicitHeader();
354
+ this.#checkBodyAllowed();
355
+ }
345
356
  this.#checkContentLength(chunk, encoding, true);
346
357
  this.endingChunk = true;
347
358
  try {
348
- super.end(chunk, encoding, callback);
359
+ super.end((chunk || undefined), encoding, callback);
349
360
  }
350
361
  finally {
351
362
  this.endingChunk = false;
@@ -578,7 +589,9 @@ export class IncomingMessage extends Readable {
578
589
  }
579
590
  }, (error) => {
580
591
  this.reading = false;
581
- this.destroy(error);
592
+ this.destroy(this.idleTimer && error instanceof TypeError
593
+ ? new Error(error.message)
594
+ : error);
582
595
  });
583
596
  }
584
597
  _destroy(error, callback) {
@@ -609,6 +622,13 @@ export class ServerResponse extends OutgoingMessage {
609
622
  super(req, options);
610
623
  this.req = req;
611
624
  this.socket = req.socket;
625
+ this.once("finish", () => queueMicrotask(() => {
626
+ if (this._closed)
627
+ return;
628
+ this.destroyed = true;
629
+ this._closed = true;
630
+ this.emit("close");
631
+ }));
612
632
  this.body = new ReadableStream({
613
633
  start: (controller) => {
614
634
  this.controller = controller;
@@ -740,10 +760,11 @@ export class Server extends EventEmitter {
740
760
  rejectNonStandardBodyWrites = false;
741
761
  keepAliveTimeout = 5_000;
742
762
  keepAliveTimeoutBuffer = 1_000;
743
- highWaterMark = 16 * 1024;
763
+ highWaterMark = getDefaultHighWaterMark(false);
744
764
  port;
745
- IncomingMessageClass = IncomingMessage;
746
- ServerResponseClass = ServerResponse;
765
+ [kIncomingMessage] = IncomingMessage;
766
+ [kServerResponse] = ServerResponse;
767
+ [kUniqueHeaders] = null;
747
768
  constructor(options, listener) {
748
769
  super({ captureRejections: true });
749
770
  this.emit = ((event, ...args) => {
@@ -764,63 +785,27 @@ export class Server extends EventEmitter {
764
785
  }
765
786
  return listeners.length !== 0;
766
787
  });
767
- if (typeof options === "function")
788
+ if (options != null)
789
+ storeHTTPOptions.call(this, options);
790
+ const values = options;
791
+ if (values?.highWaterMark !== undefined) {
792
+ validateNumber(values.highWaterMark, "options.highWaterMark");
793
+ if (values.highWaterMark > 0)
794
+ this.highWaterMark = values.highWaterMark;
795
+ }
796
+ if (typeof options === "function") {
768
797
  listener = options;
769
- else if (options != null) {
770
- if (typeof options !== "object")
771
- throw coded(new TypeError('The "options" argument must be of type object.'), "ERR_INVALID_ARG_TYPE");
772
- if (options.maxHeaderSize !== undefined) {
773
- validateInteger(options.maxHeaderSize, "maxHeaderSize", 0);
774
- unsupportedOption("maxHeaderSize");
775
- }
776
- for (const key of ["requestTimeout", "headersTimeout"]) {
777
- if (options[key] !== undefined) {
778
- validateInteger(options[key], key, 0);
779
- this[key] = options[key];
780
- }
781
- }
782
- this.headersTimeout =
783
- options.headersTimeout ?? Math.min(60_000, this.requestTimeout);
784
- if (this.headersTimeout > 0 &&
785
- this.requestTimeout > 0 &&
786
- this.headersTimeout > this.requestTimeout)
787
- throw coded(new RangeError("headersTimeout must be <= requestTimeout"), "ERR_OUT_OF_RANGE");
788
- for (const key of [
789
- "keepAliveTimeout",
790
- "keepAliveTimeoutBuffer",
791
- "connectionsCheckingInterval",
792
- ]) {
793
- if (options[key] !== undefined) {
794
- validateInteger(options[key], key, key === "keepAliveTimeoutBuffer" ? Number.MIN_SAFE_INTEGER : 0);
795
- unsupportedOption(key);
796
- }
797
- }
798
- for (const key of [
799
- "insecureHTTPParser",
800
- "requireHostHeader",
801
- "joinDuplicateHeaders",
802
- "rejectNonStandardBodyWrites",
803
- ])
804
- if (options[key] !== undefined)
805
- validateType(options[key], "boolean", key);
806
- this.requireHostHeader = options.requireHostHeader ?? true;
807
- this.joinDuplicateHeaders = options.joinDuplicateHeaders ?? false;
808
- this.rejectNonStandardBodyWrites =
809
- options.rejectNonStandardBodyWrites ?? false;
810
- if (options.highWaterMark !== undefined) {
811
- validateType(options.highWaterMark, "number", "highWaterMark");
812
- if (options.highWaterMark > 0)
813
- this.highWaterMark = options.highWaterMark;
814
- }
815
- if (options.IncomingMessage)
816
- this.IncomingMessageClass =
817
- options.IncomingMessage;
818
- if (options.ServerResponse)
819
- this.ServerResponseClass =
820
- options.ServerResponse;
798
+ options = {};
799
+ }
800
+ else if (options == null) {
801
+ options = {};
802
+ }
803
+ else {
804
+ validateObject(options, "options");
821
805
  }
822
806
  if (listener)
823
807
  this.on("request", listener);
808
+ this[kUniqueHeaders] = parseUniqueHeadersOption(options.uniqueHeaders);
824
809
  }
825
810
  [captureRejection](error, event, ...args) {
826
811
  if (event === "request") {
@@ -852,8 +837,8 @@ export class Server extends EventEmitter {
852
837
  setHandler(port, {
853
838
  server: this,
854
839
  createRequestResponse: () => ({
855
- IncomingMessage: this.IncomingMessageClass,
856
- ServerResponse: this.ServerResponseClass,
840
+ IncomingMessage: this[kIncomingMessage],
841
+ ServerResponse: this[kServerResponse],
857
842
  }),
858
843
  });
859
844
  const callback = [...args].reverse().find((arg) => typeof arg === "function") ??
@@ -943,14 +928,10 @@ export class Agent extends EventEmitter {
943
928
  this.maxSockets = this.options.maxSockets || Agent.defaultMaxSockets;
944
929
  this.maxFreeSockets = this.options.maxFreeSockets || 256;
945
930
  this.scheduling = this.options.scheduling || "lifo";
946
- if (!["fifo", "lifo"].includes(this.scheduling))
947
- throw coded(new TypeError("options.scheduling is invalid"), "ERR_INVALID_ARG_VALUE");
931
+ validateOneOf(this.scheduling, "scheduling", ["fifo", "lifo"]);
948
932
  const maximum = this.options.maxTotalSockets;
949
- if (maximum !== undefined) {
950
- validateNumber(maximum, "maxTotalSockets");
951
- if (Number.isNaN(maximum) || maximum < 1)
952
- throw coded(new RangeError("maxTotalSockets is out of range"), "ERR_OUT_OF_RANGE");
953
- }
933
+ if (maximum !== undefined)
934
+ validateNumber(maximum, "maxTotalSockets", 1);
954
935
  this.maxTotalSockets = maximum === undefined ? Infinity : maximum;
955
936
  }
956
937
  createConnection() {
@@ -996,8 +977,7 @@ export class ClientRequest extends OutgoingMessage {
996
977
  timeout;
997
978
  _ended = false;
998
979
  abortController = new AbortController();
999
- upload;
1000
- uploadWriter;
980
+ upload = [];
1001
981
  response;
1002
982
  timer;
1003
983
  sent = false;
@@ -1093,10 +1073,11 @@ export class ClientRequest extends OutgoingMessage {
1093
1073
  if (values.auth && !this.hasHeader("authorization"))
1094
1074
  this.setHeader("Authorization", `Basic ${Buffer.from(values.auth).toString("base64")}`);
1095
1075
  if (values.signal) {
1076
+ const onAbort = () => this.destroy(new AbortError(undefined, { cause: values.signal.reason }));
1096
1077
  if (values.signal.aborted)
1097
- this.destroy(values.signal.reason);
1078
+ onAbort();
1098
1079
  else
1099
- values.signal.addEventListener("abort", () => this.destroy(values.signal.reason), { once: true });
1080
+ values.signal.addEventListener("abort", onAbort, { once: true });
1100
1081
  }
1101
1082
  if (values.timeout !== undefined)
1102
1083
  this.setTimeout(values.timeout);
@@ -1109,39 +1090,20 @@ export class ClientRequest extends OutgoingMessage {
1109
1090
  this._header = `${this.method} ${this.path} HTTP/1.1\r\n`;
1110
1091
  }
1111
1092
  write(chunk, encoding, callback) {
1112
- return super.write(chunk, encoding, callback);
1093
+ return super.write(chunk instanceof Uint8Array ? Buffer.from(chunk) : chunk, encoding, callback);
1113
1094
  }
1114
1095
  _write(chunk, encoding, callback) {
1115
1096
  if (this.method === "GET" || this.method === "HEAD") {
1116
1097
  callback();
1117
1098
  return;
1118
1099
  }
1119
- try {
1120
- if (!this.upload) {
1121
- this.upload = new TransformStream();
1122
- this.uploadWriter = this.upload.writable.getWriter();
1123
- }
1124
- this.#send();
1125
- void this.uploadWriter.write(Buffer.from(chunk, encoding)).then(() => callback(), (error) => {
1126
- this.destroy(error);
1127
- callback(error);
1128
- });
1129
- }
1130
- catch (error) {
1131
- this.destroy(error);
1132
- callback(error);
1133
- }
1100
+ this.upload.push(Buffer.from(chunk, encoding));
1101
+ callback();
1134
1102
  }
1135
1103
  _final = (callback) => {
1136
1104
  try {
1137
1105
  this.#send();
1138
- if (this.uploadWriter)
1139
- void this.uploadWriter.close().then(() => callback(), (error) => {
1140
- this.destroy(error);
1141
- callback(error);
1142
- });
1143
- else
1144
- callback();
1106
+ callback();
1145
1107
  }
1146
1108
  catch (error) {
1147
1109
  this.destroy(error);
@@ -1150,11 +1112,6 @@ export class ClientRequest extends OutgoingMessage {
1150
1112
  };
1151
1113
  flushHeaders = () => {
1152
1114
  super.flushHeaders();
1153
- if (this.method !== "GET" && this.method !== "HEAD" && !this.upload) {
1154
- this.upload = new TransformStream();
1155
- this.uploadWriter = this.upload.writable.getWriter();
1156
- }
1157
- this.#send();
1158
1115
  };
1159
1116
  #send() {
1160
1117
  if (this.destroyed || this.sent)
@@ -1185,7 +1142,9 @@ export class ClientRequest extends OutgoingMessage {
1185
1142
  : value
1186
1143
  : [value])
1187
1144
  headers.push([name, String(item)]);
1188
- const body = this.upload?.readable ?? null;
1145
+ const body = this.upload.length
1146
+ ? new Blob(this.upload)
1147
+ : null;
1189
1148
  const response = missingHost
1190
1149
  ? Promise.resolve(new Response(null, {
1191
1150
  status: 400,
@@ -1265,10 +1224,8 @@ export class ClientRequest extends OutgoingMessage {
1265
1224
  this.destroy(coded(new Error("The operation was aborted"), "ABORT_ERR"));
1266
1225
  }, this.timeout);
1267
1226
  }
1268
- #releaseUpload(error = new Error("Request closed")) {
1269
- void this.uploadWriter?.abort(error).catch(() => { });
1270
- this.uploadWriter = undefined;
1271
- this.upload = undefined;
1227
+ #releaseUpload(_error) {
1228
+ this.upload = [];
1272
1229
  }
1273
1230
  #emitClose() {
1274
1231
  if (!this._closed) {
@@ -1329,8 +1286,7 @@ export function setMaxIdleHTTPParsers(max) {
1329
1286
  export function _connectionListener() {
1330
1287
  throw notImplemented("_connectionListener");
1331
1288
  }
1332
- export const WebSocket = globalThis.WebSocket;
1333
- export const CloseEvent = globalThis.CloseEvent;
1289
+ export { WebSocket, CloseEvent };
1334
1290
  export const MessageEvent = globalThis.MessageEvent;
1335
1291
  export default {
1336
1292
  validateHeaderName,
@@ -0,0 +1 @@
1
+ export declare function storeHTTPOptions(this: any, options: any): void;
@@ -0,0 +1,87 @@
1
+ // Copyright (c) 2017-2022 Cloudflare, Inc.
2
+ // Licensed under the Apache 2.0 license found in the LICENSE file or at:
3
+ // https://opensource.org/licenses/Apache-2.0
4
+ // Copyright Joyent and Node contributors. All rights reserved. MIT license.
5
+ // Shared by node:http and node:_http_server; read options in pinned Workers order.
6
+ import { IncomingMessage, ServerResponse } from "./index.js";
7
+ import { kIncomingMessage, kServerResponse } from "./symbols.js";
8
+ import { validateInteger, validateBoolean } from "../net/validators.js";
9
+ import { ERR_OUT_OF_RANGE } from "../net/errors.js";
10
+ function unsupportedOption(name) {
11
+ throw Object.assign(new Error(`The ${name} option is not implemented`), {
12
+ code: "ERR_OPTION_NOT_IMPLEMENTED",
13
+ });
14
+ }
15
+ export function storeHTTPOptions(options) {
16
+ this[kIncomingMessage] = options.IncomingMessage || IncomingMessage;
17
+ this[kServerResponse] = options.ServerResponse || ServerResponse;
18
+ const maxHeaderSize = options.maxHeaderSize;
19
+ if (maxHeaderSize !== undefined) {
20
+ validateInteger(maxHeaderSize, "maxHeaderSize", 0);
21
+ unsupportedOption("maxHeaderSize");
22
+ }
23
+ const insecureHTTPParser = options.insecureHTTPParser;
24
+ if (insecureHTTPParser !== undefined) {
25
+ // If enabled it will use a HTTP parser with leniency flags enabled.
26
+ // Since our implementation does not use any http parser, and uses "fetch" API,
27
+ // it doesn't make sense to support this option.
28
+ validateBoolean(insecureHTTPParser, "options.insecureHTTPParser");
29
+ }
30
+ const requestTimeout = options.requestTimeout;
31
+ if (requestTimeout !== undefined) {
32
+ validateInteger(requestTimeout, "requestTimeout", 0);
33
+ this.requestTimeout = requestTimeout;
34
+ }
35
+ else {
36
+ this.requestTimeout = 300_000; // 5 minutes
37
+ }
38
+ const headersTimeout = options.headersTimeout;
39
+ if (headersTimeout !== undefined) {
40
+ validateInteger(headersTimeout, "headersTimeout", 0);
41
+ this.headersTimeout = headersTimeout;
42
+ }
43
+ else {
44
+ this.headersTimeout = Math.min(60_000, this.requestTimeout); // Minimum between 60 seconds or requestTimeout
45
+ }
46
+ if (this.requestTimeout > 0 &&
47
+ this.headersTimeout > 0 &&
48
+ this.headersTimeout > this.requestTimeout) {
49
+ throw new ERR_OUT_OF_RANGE("headersTimeout", "<= requestTimeout", headersTimeout);
50
+ }
51
+ const keepAliveTimeout = options.keepAliveTimeout;
52
+ if (keepAliveTimeout !== undefined) {
53
+ validateInteger(keepAliveTimeout, "keepAliveTimeout", 0);
54
+ unsupportedOption("keepAliveTimeout");
55
+ }
56
+ const keepAliveTimeoutBuffer = options.keepAliveTimeoutBuffer;
57
+ if (keepAliveTimeoutBuffer !== undefined) {
58
+ validateInteger(keepAliveTimeoutBuffer, "keepAliveTimeoutBuffer");
59
+ unsupportedOption("keepAliveTimeoutBuffer");
60
+ }
61
+ const connectionsCheckingInterval = options.connectionsCheckingInterval;
62
+ if (connectionsCheckingInterval !== undefined) {
63
+ validateInteger(connectionsCheckingInterval, "connectionsCheckingInterval", 0);
64
+ unsupportedOption("connectionsCheckingInterval");
65
+ }
66
+ const requireHostHeader = options.requireHostHeader;
67
+ if (requireHostHeader !== undefined) {
68
+ validateBoolean(requireHostHeader, "options.requireHostHeader");
69
+ this.requireHostHeader = requireHostHeader;
70
+ }
71
+ else {
72
+ this.requireHostHeader = true;
73
+ }
74
+ const joinDuplicateHeaders = options.joinDuplicateHeaders;
75
+ if (joinDuplicateHeaders !== undefined) {
76
+ validateBoolean(joinDuplicateHeaders, "options.joinDuplicateHeaders");
77
+ }
78
+ this.joinDuplicateHeaders = joinDuplicateHeaders ?? false;
79
+ const rejectNonStandardBodyWrites = options.rejectNonStandardBodyWrites;
80
+ if (rejectNonStandardBodyWrites !== undefined) {
81
+ validateBoolean(rejectNonStandardBodyWrites, "options.rejectNonStandardBodyWrites");
82
+ this.rejectNonStandardBodyWrites = rejectNonStandardBodyWrites;
83
+ }
84
+ else {
85
+ this.rejectNonStandardBodyWrites = false;
86
+ }
87
+ }
@@ -0,0 +1 @@
1
+ export declare function parseUniqueHeadersOption(headers?: (string | string[])[]): Set<string> | null;
@@ -0,0 +1,21 @@
1
+ // Copyright (c) 2017-2022 Cloudflare, Inc.
2
+ // Licensed under the Apache 2.0 license found in the LICENSE file or at:
3
+ // https://opensource.org/licenses/Apache-2.0
4
+ // Copyright Joyent and Node contributors. All rights reserved. MIT license.
5
+ export function parseUniqueHeadersOption(headers) {
6
+ if (!Array.isArray(headers)) {
7
+ return null;
8
+ }
9
+ const unique = new Set();
10
+ for (const header of headers) {
11
+ if (Array.isArray(header)) {
12
+ for (const h of header) {
13
+ unique.add(h.toLowerCase());
14
+ }
15
+ }
16
+ else {
17
+ unique.add(header.toLowerCase());
18
+ }
19
+ }
20
+ return unique;
21
+ }
@@ -1,7 +1,8 @@
1
1
  import { OutgoingMessage, validateHeaderName, validateHeaderValue } from "../http/index.js";
2
2
  import { kUniqueHeaders, kHighWaterMark } from "../http/symbols.js";
3
3
  export { OutgoingMessage, validateHeaderName, validateHeaderValue, kUniqueHeaders, kHighWaterMark, };
4
- export declare function parseUniqueHeadersOption(headers?: (string | string[])[]): Set<string> | null;
4
+ import { parseUniqueHeadersOption } from "../http/unique-headers.js";
5
+ export { parseUniqueHeadersOption };
5
6
  declare const _default: {
6
7
  OutgoingMessage: typeof OutgoingMessage;
7
8
  validateHeaderName: typeof validateHeaderName;
@@ -5,23 +5,8 @@
5
5
  import { OutgoingMessage, validateHeaderName, validateHeaderValue, } from "../http/index.js";
6
6
  import { kUniqueHeaders, kHighWaterMark } from "../http/symbols.js";
7
7
  export { OutgoingMessage, validateHeaderName, validateHeaderValue, kUniqueHeaders, kHighWaterMark, };
8
- export function parseUniqueHeadersOption(headers) {
9
- if (!Array.isArray(headers)) {
10
- return null;
11
- }
12
- const unique = new Set();
13
- for (const header of headers) {
14
- if (Array.isArray(header)) {
15
- for (const h of header) {
16
- unique.add(h.toLowerCase());
17
- }
18
- }
19
- else {
20
- unique.add(header.toLowerCase());
21
- }
22
- }
23
- return unique;
24
- }
8
+ import { parseUniqueHeadersOption } from "../http/unique-headers.js";
9
+ export { parseUniqueHeadersOption };
25
10
  export default {
26
11
  OutgoingMessage,
27
12
  validateHeaderName,
@@ -1,8 +1,9 @@
1
+ import { storeHTTPOptions } from "../http/server-options.js";
1
2
  import { Server, ServerResponse, STATUS_CODES, _connectionListener } from "../http/index.js";
2
3
  import { kServerResponse, kConnectionsCheckingInterval } from "../http/symbols.js";
3
4
  export { Server, ServerResponse, STATUS_CODES, kServerResponse, kConnectionsCheckingInterval, _connectionListener, };
4
5
  export declare function setupConnectionsTracking(): never;
5
- export declare function storeHTTPOptions(this: any, options: any): void;
6
+ export { storeHTTPOptions };
6
7
  export declare function httpServerPreClose(server: any): void;
7
8
  declare const _default: {
8
9
  Server: typeof Server;
@@ -12,7 +13,6 @@ declare const _default: {
12
13
  kConnectionsCheckingInterval: symbol;
13
14
  _connectionListener: typeof _connectionListener;
14
15
  setupConnectionsTracking: typeof setupConnectionsTracking;
15
- storeHTTPOptions: typeof storeHTTPOptions;
16
16
  httpServerPreClose: typeof httpServerPreClose;
17
17
  };
18
18
  export default _default;
@@ -1,95 +1,15 @@
1
+ import { storeHTTPOptions } from "../http/server-options.js";
1
2
  // Copyright (c) 2017-2022 Cloudflare, Inc.
2
3
  // Licensed under the Apache 2.0 license found in the LICENSE file or at:
3
4
  // https://opensource.org/licenses/Apache-2.0
4
5
  // Copyright Joyent and Node contributors. All rights reserved. MIT license.
5
- import { Server, ServerResponse, IncomingMessage, STATUS_CODES, _connectionListener, } from "../http/index.js";
6
- import { kIncomingMessage, kServerResponse, kConnectionsCheckingInterval, } from "../http/symbols.js";
7
- import { validateInteger } from "../crypto/validation.js";
8
- import { validateBoolean } from "../net/validators.js";
9
- import { ERR_OUT_OF_RANGE } from "../net/errors.js";
10
- class ERR_OPTION_NOT_IMPLEMENTED extends Error {
11
- code = "ERR_OPTION_NOT_IMPLEMENTED";
12
- constructor(option) {
13
- super(`The ${option} option is not implemented`);
14
- }
15
- }
6
+ import { Server, ServerResponse, STATUS_CODES, _connectionListener, } from "../http/index.js";
7
+ import { kServerResponse, kConnectionsCheckingInterval, } from "../http/symbols.js";
16
8
  export { Server, ServerResponse, STATUS_CODES, kServerResponse, kConnectionsCheckingInterval, _connectionListener, };
17
9
  export function setupConnectionsTracking() {
18
10
  throw Object.assign(new Error("The setupConnectionsTracking method is not implemented"), { code: "ERR_METHOD_NOT_IMPLEMENTED" });
19
11
  }
20
- export function storeHTTPOptions(options) {
21
- this[kIncomingMessage] = options.IncomingMessage || IncomingMessage;
22
- this[kServerResponse] = options.ServerResponse || ServerResponse;
23
- const maxHeaderSize = options.maxHeaderSize;
24
- if (maxHeaderSize !== undefined) {
25
- validateInteger(maxHeaderSize, "maxHeaderSize", 0);
26
- throw new ERR_OPTION_NOT_IMPLEMENTED("maxHeaderSize");
27
- }
28
- const insecureHTTPParser = options.insecureHTTPParser;
29
- if (insecureHTTPParser !== undefined) {
30
- // If enabled it will use a HTTP parser with leniency flags enabled.
31
- // Since our implementation does not use any http parser, and uses "fetch" API,
32
- // it doesn't make sense to support this option.
33
- validateBoolean(insecureHTTPParser, "options.insecureHTTPParser");
34
- }
35
- const requestTimeout = options.requestTimeout;
36
- if (requestTimeout !== undefined) {
37
- validateInteger(requestTimeout, "requestTimeout", 0);
38
- this.requestTimeout = requestTimeout;
39
- }
40
- else {
41
- this.requestTimeout = 300_000; // 5 minutes
42
- }
43
- const headersTimeout = options.headersTimeout;
44
- if (headersTimeout !== undefined) {
45
- validateInteger(headersTimeout, "headersTimeout", 0);
46
- this.headersTimeout = headersTimeout;
47
- }
48
- else {
49
- this.headersTimeout = Math.min(60_000, this.requestTimeout); // Minimum between 60 seconds or requestTimeout
50
- }
51
- if (this.requestTimeout > 0 &&
52
- this.headersTimeout > 0 &&
53
- this.headersTimeout > this.requestTimeout) {
54
- throw new ERR_OUT_OF_RANGE("headersTimeout", "<= requestTimeout", headersTimeout);
55
- }
56
- const keepAliveTimeout = options.keepAliveTimeout;
57
- if (keepAliveTimeout !== undefined) {
58
- validateInteger(keepAliveTimeout, "keepAliveTimeout", 0);
59
- throw new ERR_OPTION_NOT_IMPLEMENTED("keepAliveTimeout");
60
- }
61
- const keepAliveTimeoutBuffer = options.keepAliveTimeoutBuffer;
62
- if (keepAliveTimeoutBuffer !== undefined) {
63
- validateInteger(keepAliveTimeoutBuffer, "keepAliveTimeoutBuffer");
64
- throw new ERR_OPTION_NOT_IMPLEMENTED("keepAliveTimeoutBuffer");
65
- }
66
- const connectionsCheckingInterval = options.connectionsCheckingInterval;
67
- if (connectionsCheckingInterval !== undefined) {
68
- validateInteger(connectionsCheckingInterval, "connectionsCheckingInterval", 0);
69
- throw new ERR_OPTION_NOT_IMPLEMENTED("connectionsCheckingInterval");
70
- }
71
- const requireHostHeader = options.requireHostHeader;
72
- if (requireHostHeader !== undefined) {
73
- validateBoolean(requireHostHeader, "options.requireHostHeader");
74
- this.requireHostHeader = requireHostHeader;
75
- }
76
- else {
77
- this.requireHostHeader = true;
78
- }
79
- const joinDuplicateHeaders = options.joinDuplicateHeaders;
80
- if (joinDuplicateHeaders !== undefined) {
81
- validateBoolean(joinDuplicateHeaders, "options.joinDuplicateHeaders");
82
- }
83
- this.joinDuplicateHeaders = joinDuplicateHeaders ?? false;
84
- const rejectNonStandardBodyWrites = options.rejectNonStandardBodyWrites;
85
- if (rejectNonStandardBodyWrites !== undefined) {
86
- validateBoolean(rejectNonStandardBodyWrites, "options.rejectNonStandardBodyWrites");
87
- this.rejectNonStandardBodyWrites = rejectNonStandardBodyWrites;
88
- }
89
- else {
90
- this.rejectNonStandardBodyWrites = false;
91
- }
92
- }
12
+ export { storeHTTPOptions };
93
13
  export function httpServerPreClose(server) {
94
14
  server.closeIdleConnections();
95
15
  clearInterval(server[kConnectionsCheckingInterval]);
@@ -102,6 +22,5 @@ export default {
102
22
  kConnectionsCheckingInterval,
103
23
  _connectionListener,
104
24
  setupConnectionsTracking,
105
- storeHTTPOptions,
106
25
  httpServerPreClose,
107
26
  };