@di-framework/cli-plugin-platform 0.0.1 → 6.0.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 (169) hide show
  1. package/README.md +408 -0
  2. package/dist/actor-protocol.d.ts +4 -0
  3. package/dist/actor-protocol.js +162 -0
  4. package/dist/actors.d.ts +31 -0
  5. package/dist/actors.js +296 -0
  6. package/dist/args.d.ts +15 -0
  7. package/dist/args.js +66 -0
  8. package/dist/assets/http-adapter.js +876 -0
  9. package/dist/assets/platform/.gitignore.tmpl +6 -0
  10. package/dist/assets/platform/Pulumi.yaml.tmpl +6 -0
  11. package/dist/assets/platform/README.md +208 -0
  12. package/dist/assets/platform/index.ts.tmpl +2 -0
  13. package/dist/assets/platform/package.json.tmpl +18 -0
  14. package/dist/assets/platform/tsconfig.json +11 -0
  15. package/dist/assets/queue-adapter.js +60 -0
  16. package/dist/assets/sqlite/BUILD-INFO.txt +13 -0
  17. package/dist/assets/sqlite/SHA256SUMS +2 -0
  18. package/dist/assets/sqlite/di-framework-sqlite.wasm +0 -0
  19. package/dist/assets/sqlite/di-framework-sqlite.wit +337 -0
  20. package/dist/assets/wit/deps/LICENSE.md +8 -0
  21. package/dist/assets/wit/deps/di-framework-queues/package.wit +18 -0
  22. package/dist/assets/wit/deps/di-framework-sqlite/package.wit +211 -0
  23. package/dist/assets/wit/deps/wasi-cli/package.wit +70 -0
  24. package/dist/assets/wit/deps/wasi-clocks/package.wit +42 -0
  25. package/dist/assets/wit/deps/wasi-config/package.wit +15 -0
  26. package/dist/assets/wit/deps/wasi-filesystem/package.wit +184 -0
  27. package/dist/assets/wit/deps/wasi-http/package.wit +170 -0
  28. package/dist/assets/wit/deps/wasi-random/package.wit +17 -0
  29. package/dist/assets/wit/deps/wasi-sockets/package.wit +105 -0
  30. package/dist/assets/wit/deps/wasi-tls/package.wit +26 -0
  31. package/dist/assets/wit/deps/wasmcloud-blobstore/package.wit +153 -0
  32. package/dist/assets/wit/deps/wasmcloud-keyvalue/package.wit +370 -0
  33. package/dist/assets/wit/deps/wasmcloud-messaging/package.wit +125 -0
  34. package/dist/assets/wit/deps/wasmcloud-postgres/package.wit +435 -0
  35. package/dist/assets/wit/deps/wasmcloud-secrets/package.wit +80 -0
  36. package/dist/async-transform.d.ts +3 -0
  37. package/dist/async-transform.js +60 -0
  38. package/dist/bindings.d.ts +34 -0
  39. package/dist/bindings.js +307 -0
  40. package/dist/build.d.ts +29 -0
  41. package/dist/build.js +325 -0
  42. package/dist/command.d.ts +3 -0
  43. package/dist/command.js +141 -0
  44. package/dist/control/auth.d.ts +18 -0
  45. package/dist/control/auth.js +89 -0
  46. package/dist/control/cron.d.ts +7 -0
  47. package/dist/control/cron.js +86 -0
  48. package/dist/control/index.d.ts +4 -0
  49. package/dist/control/index.js +4 -0
  50. package/dist/control/network.d.ts +12 -0
  51. package/dist/control/network.js +49 -0
  52. package/dist/control/queues.d.ts +15 -0
  53. package/dist/control/queues.js +77 -0
  54. package/dist/cron.d.ts +26 -0
  55. package/dist/cron.js +239 -0
  56. package/dist/deploy.d.ts +29 -0
  57. package/dist/deploy.js +57 -0
  58. package/dist/deps.d.ts +96 -0
  59. package/dist/deps.js +303 -0
  60. package/dist/destroy.d.ts +3 -0
  61. package/dist/destroy.js +24 -0
  62. package/dist/dev-runner.d.ts +24 -0
  63. package/dist/dev-runner.js +83 -0
  64. package/dist/dev.d.ts +8 -0
  65. package/dist/dev.js +76 -0
  66. package/dist/discovery.d.ts +15 -0
  67. package/dist/discovery.js +182 -0
  68. package/dist/doctor.d.ts +8 -0
  69. package/dist/doctor.js +82 -0
  70. package/dist/guests.d.ts +9 -0
  71. package/dist/guests.js +56 -0
  72. package/dist/host-interface.d.ts +29 -0
  73. package/dist/host-interface.js +142 -0
  74. package/dist/index.d.ts +33 -0
  75. package/dist/index.js +39 -0
  76. package/dist/kubernetes.d.ts +9 -0
  77. package/dist/kubernetes.js +17 -0
  78. package/dist/managed-bindings.d.ts +9 -0
  79. package/dist/managed-bindings.js +150 -0
  80. package/dist/manifest.d.ts +36 -0
  81. package/dist/manifest.js +257 -0
  82. package/dist/namespace.d.ts +2 -0
  83. package/dist/namespace.js +4 -0
  84. package/dist/node-compat/async-hooks.d.ts +29 -0
  85. package/dist/node-compat/async-hooks.js +80 -0
  86. package/dist/node-compat/bootstrap.d.ts +1 -0
  87. package/dist/node-compat/bootstrap.js +9 -0
  88. package/dist/node-compat/bytes.d.ts +7 -0
  89. package/dist/node-compat/bytes.js +41 -0
  90. package/dist/node-compat/crypto-hash.d.ts +27 -0
  91. package/dist/node-compat/crypto-hash.js +108 -0
  92. package/dist/node-compat/crypto-subtle.d.ts +59 -0
  93. package/dist/node-compat/crypto-subtle.js +288 -0
  94. package/dist/node-compat/crypto.d.ts +136 -0
  95. package/dist/node-compat/crypto.js +142 -0
  96. package/dist/node-compat/dgram.d.ts +49 -0
  97. package/dist/node-compat/dgram.js +197 -0
  98. package/dist/node-compat/env.d.ts +6 -0
  99. package/dist/node-compat/env.js +88 -0
  100. package/dist/node-compat/fetch-runtime.d.ts +80 -0
  101. package/dist/node-compat/fetch-runtime.js +314 -0
  102. package/dist/node-compat/fs.d.ts +84 -0
  103. package/dist/node-compat/fs.js +285 -0
  104. package/dist/node-compat/http-parser.d.ts +40 -0
  105. package/dist/node-compat/http-parser.js +185 -0
  106. package/dist/node-compat/http.d.ts +130 -0
  107. package/dist/node-compat/http.js +688 -0
  108. package/dist/node-compat/https.d.ts +28 -0
  109. package/dist/node-compat/https.js +88 -0
  110. package/dist/node-compat/module.d.ts +11 -0
  111. package/dist/node-compat/module.js +35 -0
  112. package/dist/node-compat/net.d.ts +119 -0
  113. package/dist/node-compat/net.js +441 -0
  114. package/dist/node-compat/process.d.ts +25 -0
  115. package/dist/node-compat/process.js +63 -0
  116. package/dist/node-compat/runtime-application.d.ts +4 -0
  117. package/dist/node-compat/runtime-application.js +24 -0
  118. package/dist/node-compat/seed-virtual.d.ts +6 -0
  119. package/dist/node-compat/seed-virtual.js +6 -0
  120. package/dist/node-compat/seed.d.ts +20 -0
  121. package/dist/node-compat/seed.js +106 -0
  122. package/dist/node-compat/socket-address.d.ts +35 -0
  123. package/dist/node-compat/socket-address.js +162 -0
  124. package/dist/node-compat/timers.d.ts +32 -0
  125. package/dist/node-compat/timers.js +96 -0
  126. package/dist/node-compat/tls.d.ts +57 -0
  127. package/dist/node-compat/tls.js +290 -0
  128. package/dist/node-compat/wasi-random.d.ts +2 -0
  129. package/dist/node-compat/wasi-random.js +25 -0
  130. package/dist/node-compat/wasi-sockets.d.ts +47 -0
  131. package/dist/node-compat/wasi-sockets.js +134 -0
  132. package/dist/oci.d.ts +9 -0
  133. package/dist/oci.js +9 -0
  134. package/dist/paths.d.ts +7 -0
  135. package/dist/paths.js +13 -0
  136. package/dist/platform-init.d.ts +13 -0
  137. package/dist/platform-init.js +265 -0
  138. package/dist/platform.d.ts +22 -0
  139. package/dist/platform.js +170 -0
  140. package/dist/project.d.ts +39 -0
  141. package/dist/project.js +130 -0
  142. package/dist/publish.d.ts +15 -0
  143. package/dist/publish.js +57 -0
  144. package/dist/pulumi.d.ts +4 -0
  145. package/dist/pulumi.js +27 -0
  146. package/dist/queues-module.d.ts +4 -0
  147. package/dist/queues-module.js +89 -0
  148. package/dist/queues.d.ts +16 -0
  149. package/dist/queues.js +167 -0
  150. package/dist/registry.d.ts +16 -0
  151. package/dist/registry.js +27 -0
  152. package/dist/service.d.ts +162 -0
  153. package/dist/service.js +678 -0
  154. package/dist/support.d.ts +14 -0
  155. package/dist/support.js +37 -0
  156. package/dist/target.d.ts +18 -0
  157. package/dist/target.js +45 -0
  158. package/dist/toml.d.ts +6 -0
  159. package/dist/toml.js +245 -0
  160. package/dist/wash-dev.d.ts +18 -0
  161. package/dist/wash-dev.js +80 -0
  162. package/dist/wit.d.ts +76 -0
  163. package/dist/wit.js +248 -0
  164. package/dist/workload-members.d.ts +12 -0
  165. package/dist/workload-members.js +136 -0
  166. package/dist/workload.d.ts +45 -0
  167. package/dist/workload.js +457 -0
  168. package/package.json +55 -8
  169. package/src/index.ts +0 -2
@@ -0,0 +1,688 @@
1
+ import { EventEmitter } from 'node:events';
2
+ import { concatBytes, toBytes, toNodeBuffer } from './bytes.js';
3
+ import { CHUNKED_END, ChunkedDecoder, contentLengthOf, encodeChunk, headerValue, isChunked, isUpgrade, MAX_HEADER_SIZE, parseHttpRequest, parseHttpResponse, serializeHttpRequest, serializeHttpResponse, } from './http-parser.js';
4
+ import { createConnection, createServer as createNetServer, } from './net.js';
5
+ export const METHODS = [
6
+ 'ACL',
7
+ 'BIND',
8
+ 'CHECKOUT',
9
+ 'CONNECT',
10
+ 'COPY',
11
+ 'DELETE',
12
+ 'GET',
13
+ 'HEAD',
14
+ 'LINK',
15
+ 'LOCK',
16
+ 'M-SEARCH',
17
+ 'MERGE',
18
+ 'MKACTIVITY',
19
+ 'MKCALENDAR',
20
+ 'MKCOL',
21
+ 'MOVE',
22
+ 'NOTIFY',
23
+ 'OPTIONS',
24
+ 'PATCH',
25
+ 'POST',
26
+ 'PRI',
27
+ 'PROPFIND',
28
+ 'PROPPATCH',
29
+ 'PURGE',
30
+ 'PUT',
31
+ 'REBIND',
32
+ 'REPORT',
33
+ 'SEARCH',
34
+ 'SOURCE',
35
+ 'SUBSCRIBE',
36
+ 'TRACE',
37
+ 'UNBIND',
38
+ 'UNLINK',
39
+ 'UNLOCK',
40
+ 'UNSUBSCRIBE',
41
+ ];
42
+ export const STATUS_CODES = {
43
+ 100: 'Continue',
44
+ 101: 'Switching Protocols',
45
+ 200: 'OK',
46
+ 201: 'Created',
47
+ 202: 'Accepted',
48
+ 204: 'No Content',
49
+ 301: 'Moved Permanently',
50
+ 302: 'Found',
51
+ 304: 'Not Modified',
52
+ 400: 'Bad Request',
53
+ 401: 'Unauthorized',
54
+ 403: 'Forbidden',
55
+ 404: 'Not Found',
56
+ 405: 'Method Not Allowed',
57
+ 408: 'Request Timeout',
58
+ 409: 'Conflict',
59
+ 411: 'Length Required',
60
+ 413: 'Payload Too Large',
61
+ 414: 'URI Too Long',
62
+ 415: 'Unsupported Media Type',
63
+ 426: 'Upgrade Required',
64
+ 429: 'Too Many Requests',
65
+ 500: 'Internal Server Error',
66
+ 501: 'Not Implemented',
67
+ 502: 'Bad Gateway',
68
+ 503: 'Service Unavailable',
69
+ 504: 'Gateway Timeout',
70
+ 505: 'HTTP Version Not Supported',
71
+ };
72
+ export const maxHeaderSize = MAX_HEADER_SIZE;
73
+ function emitError(emitter, error) {
74
+ if (typeof emitter.listenerCount === 'function' && emitter.listenerCount('error') === 0)
75
+ return;
76
+ emitter.emit('error', error);
77
+ }
78
+ function lowerHeaders(input) {
79
+ const headers = {};
80
+ if (input === undefined)
81
+ return headers;
82
+ for (const [name, value] of Object.entries(input)) {
83
+ if (value === undefined)
84
+ continue;
85
+ headers[name.toLowerCase()] = Array.isArray(value) ? value.map(String) : String(value);
86
+ }
87
+ return headers;
88
+ }
89
+ export class IncomingMessage extends EventEmitter {
90
+ httpVersion = '1.1';
91
+ httpVersionMajor = 1;
92
+ httpVersionMinor = 1;
93
+ complete = false;
94
+ aborted = false;
95
+ method = 'GET';
96
+ url = '/';
97
+ statusCode = 200;
98
+ statusMessage = '';
99
+ headers = {};
100
+ rawHeaders = [];
101
+ socket;
102
+ readable = true;
103
+ constructor(socket) {
104
+ super();
105
+ this.socket = socket;
106
+ }
107
+ pushBody(chunk) {
108
+ if (chunk.length === 0)
109
+ return;
110
+ this.emit('data', toNodeBuffer(chunk));
111
+ }
112
+ finish() {
113
+ if (this.complete)
114
+ return;
115
+ this.complete = true;
116
+ this.readable = false;
117
+ this.emit('end');
118
+ }
119
+ setTimeout(_msecs, callback) {
120
+ if (typeof callback === 'function')
121
+ this.once('timeout', callback);
122
+ return this;
123
+ }
124
+ }
125
+ export class ServerResponse extends EventEmitter {
126
+ statusCode = 200;
127
+ statusMessage = '';
128
+ headersSent = false;
129
+ finished = false;
130
+ writable = true;
131
+ writableEnded = false;
132
+ req;
133
+ socket;
134
+ headers = {};
135
+ chunked = false;
136
+ bodyChunks = [];
137
+ constructor(req, socket) {
138
+ super();
139
+ this.req = req;
140
+ this.socket = socket;
141
+ }
142
+ setHeader(name, value) {
143
+ this.headers[name.toLowerCase()] = Array.isArray(value)
144
+ ? [...value].map(String)
145
+ : String(value);
146
+ return this;
147
+ }
148
+ getHeader(name) {
149
+ return this.headers[name.toLowerCase()];
150
+ }
151
+ removeHeader(name) {
152
+ delete this.headers[name.toLowerCase()];
153
+ return this;
154
+ }
155
+ writeHead(statusCode, statusMessageOrHeaders, maybeHeaders) {
156
+ this.statusCode = statusCode;
157
+ if (typeof statusMessageOrHeaders === 'string') {
158
+ this.statusMessage = statusMessageOrHeaders;
159
+ if (maybeHeaders !== undefined)
160
+ Object.assign(this.headers, lowerHeaders(maybeHeaders));
161
+ }
162
+ else if (statusMessageOrHeaders !== undefined) {
163
+ Object.assign(this.headers, lowerHeaders(statusMessageOrHeaders));
164
+ }
165
+ this.flushHeaders();
166
+ return this;
167
+ }
168
+ flushHeaders() {
169
+ if (this.headersSent)
170
+ return;
171
+ if (this.statusMessage.length === 0) {
172
+ this.statusMessage = STATUS_CODES[this.statusCode] ?? '';
173
+ }
174
+ this.headersSent = true;
175
+ this.socket.write(serializeHttpResponse(this.statusCode, this.statusMessage, this.headers));
176
+ }
177
+ write(chunk, encodingOrCallback, maybeCallback) {
178
+ const callback = typeof encodingOrCallback === 'function' ? encodingOrCallback : maybeCallback;
179
+ if (this.writableEnded) {
180
+ const error = new Error('write after end');
181
+ error.code = 'ERR_STREAM_WRITE_AFTER_END';
182
+ if (callback !== undefined)
183
+ queueMicrotask(() => callback(error));
184
+ else
185
+ emitError(this, error);
186
+ return false;
187
+ }
188
+ const bytes = toBytes(chunk);
189
+ if (!this.headersSent) {
190
+ if (headerValue(this.headers, 'content-length') === undefined) {
191
+ this.setHeader('Transfer-Encoding', 'chunked');
192
+ this.chunked = true;
193
+ }
194
+ this.flushHeaders();
195
+ }
196
+ if (bytes.length > 0) {
197
+ this.socket.write(this.chunked ? encodeChunk(bytes) : bytes);
198
+ }
199
+ if (callback !== undefined)
200
+ queueMicrotask(() => callback(null));
201
+ return true;
202
+ }
203
+ end(chunk, encodingOrCallback, maybeCallback) {
204
+ const callback = typeof chunk === 'function'
205
+ ? chunk
206
+ : typeof encodingOrCallback === 'function'
207
+ ? encodingOrCallback
208
+ : maybeCallback;
209
+ const data = typeof chunk === 'function' || chunk === undefined ? undefined : chunk;
210
+ if (this.writableEnded) {
211
+ if (typeof callback === 'function')
212
+ queueMicrotask(callback);
213
+ return this;
214
+ }
215
+ this.writableEnded = true;
216
+ this.finished = true;
217
+ this.writable = false;
218
+ if (!this.headersSent) {
219
+ const bytes = data === undefined ? new Uint8Array() : toBytes(data);
220
+ if (headerValue(this.headers, 'content-length') === undefined) {
221
+ this.setHeader('Content-Length', String(bytes.length));
222
+ }
223
+ this.flushHeaders();
224
+ if (bytes.length > 0)
225
+ this.socket.write(bytes);
226
+ }
227
+ else if (data !== undefined) {
228
+ const bytes = toBytes(data);
229
+ this.socket.write(this.chunked ? encodeChunk(bytes) : bytes);
230
+ }
231
+ if (this.chunked)
232
+ this.socket.write(CHUNKED_END);
233
+ const connection = headerValue(this.headers, 'connection')?.toLowerCase();
234
+ if (connection === 'close' || this.req.httpVersion === '1.0')
235
+ this.socket.end();
236
+ queueMicrotask(() => {
237
+ this.emit('finish');
238
+ if (typeof callback === 'function')
239
+ callback();
240
+ });
241
+ return this;
242
+ }
243
+ setTimeout(_msecs, callback) {
244
+ if (typeof callback === 'function')
245
+ this.once('timeout', callback);
246
+ return this;
247
+ }
248
+ }
249
+ export class Agent extends EventEmitter {
250
+ keepAlive = false;
251
+ maxSockets = Infinity;
252
+ destroy() { }
253
+ }
254
+ export const globalAgent = new Agent();
255
+ export class ClientRequest extends EventEmitter {
256
+ method = 'GET';
257
+ path = '/';
258
+ host = 'localhost';
259
+ port = 80;
260
+ headers = {};
261
+ socket;
262
+ aborted = false;
263
+ writable = true;
264
+ writableEnded = false;
265
+ headersSent = false;
266
+ body = [];
267
+ options;
268
+ constructor(options, callback) {
269
+ super();
270
+ this.options = options;
271
+ this.method = (options.method ?? 'GET').toUpperCase();
272
+ this.path = options.path ?? '/';
273
+ this.host = options.hostname ?? options.host ?? 'localhost';
274
+ this.port = Number(options.port ?? options.defaultPort ?? 80);
275
+ this.headers = lowerHeaders(options.headers);
276
+ if (typeof callback === 'function')
277
+ this.once('response', callback);
278
+ if (headerValue(this.headers, 'host') === undefined) {
279
+ const hostname = this.host.includes(':') && !this.host.startsWith('[') ? `[${this.host}]` : this.host;
280
+ const hostHeader = this.port === (options.defaultPort ?? 80) ? hostname : `${hostname}:${this.port}`;
281
+ this.headers.host = hostHeader;
282
+ }
283
+ queueMicrotask(() => this.open());
284
+ }
285
+ setHeader(name, value) {
286
+ this.headers[name.toLowerCase()] = Array.isArray(value)
287
+ ? [...value].map(String)
288
+ : String(value);
289
+ return this;
290
+ }
291
+ getHeader(name) {
292
+ return this.headers[name.toLowerCase()];
293
+ }
294
+ write(chunk, encodingOrCallback, maybeCallback) {
295
+ const callback = typeof encodingOrCallback === 'function' ? encodingOrCallback : maybeCallback;
296
+ this.body.push(toBytes(chunk));
297
+ if (typeof callback === 'function')
298
+ queueMicrotask(callback);
299
+ return true;
300
+ }
301
+ end(chunk, encodingOrCallback, maybeCallback) {
302
+ const callback = typeof chunk === 'function'
303
+ ? chunk
304
+ : typeof encodingOrCallback === 'function'
305
+ ? encodingOrCallback
306
+ : maybeCallback;
307
+ if (typeof chunk !== 'function' && chunk !== undefined)
308
+ this.body.push(toBytes(chunk));
309
+ this.writableEnded = true;
310
+ this.writable = false;
311
+ if (typeof callback === 'function')
312
+ this.once('finish', callback);
313
+ this.flush();
314
+ return this;
315
+ }
316
+ abort() {
317
+ this.aborted = true;
318
+ this.socket?.destroy();
319
+ this.emit('abort');
320
+ }
321
+ destroy(error) {
322
+ this.aborted = true;
323
+ this.socket?.destroy(error);
324
+ return this;
325
+ }
326
+ setTimeout(msecs, callback) {
327
+ if (typeof callback === 'function')
328
+ this.once('timeout', callback);
329
+ this.socket?.setTimeout(msecs, () => this.emit('timeout'));
330
+ return this;
331
+ }
332
+ open() {
333
+ if (this.aborted)
334
+ return;
335
+ const onSocket = (socket) => {
336
+ if (this.socket === socket)
337
+ return;
338
+ if (this.aborted) {
339
+ socket.destroy();
340
+ return;
341
+ }
342
+ this.socket = socket;
343
+ this.emit('socket', socket);
344
+ socket.on('error', (error) => emitError(this, error));
345
+ if (socket.connecting)
346
+ socket.once('encrypted' in socket ? 'secureConnect' : 'connect', () => this.flush());
347
+ else
348
+ this.flush();
349
+ };
350
+ if (this.options.createConnection !== undefined) {
351
+ try {
352
+ const created = this.options.createConnection(this.options, (error, socket) => {
353
+ if (error !== null)
354
+ emitError(this, error);
355
+ else
356
+ onSocket(socket);
357
+ });
358
+ if (created !== undefined)
359
+ onSocket(created);
360
+ }
361
+ catch (error) {
362
+ emitError(this, error);
363
+ }
364
+ return;
365
+ }
366
+ onSocket(createConnection({ port: this.port, host: this.host }));
367
+ }
368
+ flush() {
369
+ if (this.aborted || this.socket === undefined || this.headersSent)
370
+ return;
371
+ if (this.socket.connecting)
372
+ return;
373
+ if (!this.writableEnded)
374
+ return;
375
+ const body = concatBytes(...this.body);
376
+ const chunked = isChunked(this.headers);
377
+ if (chunked)
378
+ delete this.headers['content-length'];
379
+ else if (headerValue(this.headers, 'content-length') === undefined && body.length > 0) {
380
+ this.headers['content-length'] = String(body.length);
381
+ }
382
+ this.headersSent = true;
383
+ void this.readResponse(this.socket);
384
+ this.socket.write(serializeHttpRequest(this.method, this.path, this.headers));
385
+ if (chunked) {
386
+ for (const chunk of this.body) {
387
+ if (chunk.length > 0)
388
+ this.socket.write(encodeChunk(chunk));
389
+ }
390
+ this.socket.write(CHUNKED_END);
391
+ }
392
+ else if (body.length > 0)
393
+ this.socket.write(body);
394
+ this.emit('finish');
395
+ }
396
+ async readResponse(socket) {
397
+ let buffer = new Uint8Array();
398
+ let complete = false;
399
+ let failed = false;
400
+ let closeDelimited;
401
+ socket.on('error', () => {
402
+ failed = true;
403
+ });
404
+ socket.once('end', () => {
405
+ if (complete || failed || this.aborted)
406
+ return;
407
+ if (closeDelimited !== undefined)
408
+ closeDelimited.finish();
409
+ else
410
+ emitError(this, Object.assign(new Error('Socket ended before the HTTP response completed'), {
411
+ code: 'ECONNRESET',
412
+ }));
413
+ });
414
+ const onData = (chunk) => {
415
+ buffer = concatBytes(buffer, toBytes(chunk));
416
+ let parsed;
417
+ try {
418
+ parsed = parseHttpResponse(buffer);
419
+ }
420
+ catch (error) {
421
+ failed = true;
422
+ emitError(this, error);
423
+ return;
424
+ }
425
+ if (parsed === undefined) {
426
+ if (buffer.length > MAX_HEADER_SIZE)
427
+ emitError(this, new Error('HTTP header too large'));
428
+ return;
429
+ }
430
+ socket.removeListener('data', onData);
431
+ const leftover = buffer.subarray(parsed.headerLength);
432
+ const incoming = new IncomingMessage(socket);
433
+ incoming.once('end', () => {
434
+ complete = true;
435
+ });
436
+ incoming.httpVersion = parsed.httpVersion;
437
+ incoming.statusCode = parsed.statusCode;
438
+ incoming.statusMessage = parsed.statusMessage;
439
+ incoming.headers = parsed.headers;
440
+ incoming.rawHeaders = parsed.rawHeaders;
441
+ if (isUpgrade(parsed.headers) && parsed.statusCode === 101) {
442
+ complete = true;
443
+ socket.pause();
444
+ this.emit('upgrade', incoming, socket, toNodeBuffer(leftover));
445
+ return;
446
+ }
447
+ this.emit('response', incoming);
448
+ const noBody = this.method === 'HEAD' || parsed.statusCode === 204 || parsed.statusCode === 304;
449
+ if (noBody) {
450
+ complete = true;
451
+ incoming.finish();
452
+ return;
453
+ }
454
+ if (isChunked(parsed.headers)) {
455
+ readChunkedBody(socket, incoming, leftover, this, () => { });
456
+ return;
457
+ }
458
+ const length = contentLengthOf(parsed.headers);
459
+ if (length === undefined) {
460
+ closeDelimited = incoming;
461
+ if (leftover.length > 0)
462
+ incoming.pushBody(leftover);
463
+ socket.on('data', (chunk) => incoming.pushBody(toBytes(chunk)));
464
+ return;
465
+ }
466
+ if (leftover.length > 0)
467
+ incoming.pushBody(leftover.subarray(0, length));
468
+ if (leftover.length >= length)
469
+ incoming.finish();
470
+ else {
471
+ let remaining = length - leftover.length;
472
+ const bodyData = (chunk) => {
473
+ const bytes = toBytes(chunk);
474
+ const take = bytes.subarray(0, remaining);
475
+ remaining -= take.length;
476
+ incoming.pushBody(take);
477
+ if (remaining <= 0) {
478
+ socket.removeListener('data', bodyData);
479
+ incoming.finish();
480
+ }
481
+ };
482
+ socket.on('data', bodyData);
483
+ }
484
+ };
485
+ socket.on('data', onData);
486
+ }
487
+ }
488
+ function readChunkedBody(socket, incoming, initial, owner, onComplete) {
489
+ const decoder = new ChunkedDecoder();
490
+ const onData = (chunk) => {
491
+ let leftover;
492
+ try {
493
+ leftover = decoder.write(toBytes(chunk), (data) => incoming.pushBody(data));
494
+ }
495
+ catch (error) {
496
+ socket.removeListener('data', onData);
497
+ socket.destroy();
498
+ emitError(owner, error);
499
+ return;
500
+ }
501
+ if (leftover !== undefined) {
502
+ socket.removeListener('data', onData);
503
+ incoming.finish();
504
+ onComplete(leftover);
505
+ }
506
+ };
507
+ socket.on('data', onData);
508
+ onData(initial);
509
+ }
510
+ export class Server extends EventEmitter {
511
+ netServer;
512
+ listening = false;
513
+ maxHeadersCount = 2000;
514
+ timeout = 0;
515
+ requestListener;
516
+ constructor(requestListener) {
517
+ super();
518
+ this.requestListener = requestListener;
519
+ if (requestListener !== undefined)
520
+ this.on('request', requestListener);
521
+ this.netServer = createNetServer((socket) => this.handleConnection(socket));
522
+ this.netServer.on('listening', () => {
523
+ this.listening = true;
524
+ this.emit('listening');
525
+ });
526
+ this.netServer.on('error', (error) => emitError(this, error));
527
+ this.netServer.on('close', () => {
528
+ this.listening = false;
529
+ this.emit('close');
530
+ });
531
+ }
532
+ listen(portOrOptions, hostnameOrCallback, backlogOrCallback, maybeCallback) {
533
+ this.netServer.listen(portOrOptions, hostnameOrCallback, backlogOrCallback, maybeCallback);
534
+ return this;
535
+ }
536
+ address() {
537
+ return this.netServer.address();
538
+ }
539
+ close(callback) {
540
+ this.netServer.close(callback);
541
+ return this;
542
+ }
543
+ ref() {
544
+ this.netServer.ref();
545
+ return this;
546
+ }
547
+ unref() {
548
+ this.netServer.unref();
549
+ return this;
550
+ }
551
+ handleConnection(socket) {
552
+ let buffer = new Uint8Array();
553
+ const onData = (chunk) => {
554
+ buffer = concatBytes(buffer, toBytes(chunk));
555
+ let parsed;
556
+ try {
557
+ parsed = parseHttpRequest(buffer);
558
+ }
559
+ catch (error) {
560
+ emitError(this, error);
561
+ socket.destroy();
562
+ return;
563
+ }
564
+ if (parsed === undefined) {
565
+ if (buffer.length > MAX_HEADER_SIZE)
566
+ socket.destroy();
567
+ return;
568
+ }
569
+ const leftover = buffer.subarray(parsed.headerLength);
570
+ buffer = new Uint8Array();
571
+ const req = new IncomingMessage(socket);
572
+ req.method = parsed.method;
573
+ req.url = parsed.url;
574
+ req.httpVersion = parsed.httpVersion;
575
+ req.headers = parsed.headers;
576
+ req.rawHeaders = parsed.rawHeaders;
577
+ if (isUpgrade(parsed.headers)) {
578
+ socket.pause();
579
+ socket.removeListener('data', onData);
580
+ if (typeof this.listenerCount === 'function' && this.listenerCount('upgrade') === 0) {
581
+ socket.destroy();
582
+ return;
583
+ }
584
+ this.emit('upgrade', req, socket, toNodeBuffer(leftover));
585
+ return;
586
+ }
587
+ const res = new ServerResponse(req, socket);
588
+ if (isChunked(parsed.headers)) {
589
+ socket.removeListener('data', onData);
590
+ this.emit('request', req, res);
591
+ readChunkedBody(socket, req, leftover, this, (rest) => {
592
+ socket.on('data', onData);
593
+ if (rest.length > 0)
594
+ onData(rest);
595
+ });
596
+ return;
597
+ }
598
+ const length = contentLengthOf(parsed.headers);
599
+ const hasBody = length !== undefined && length > 0 && req.method !== 'HEAD';
600
+ this.emit('request', req, res);
601
+ if (!hasBody) {
602
+ if (leftover.length > 0)
603
+ buffer = leftover;
604
+ req.finish();
605
+ return;
606
+ }
607
+ let remaining = length;
608
+ if (leftover.length > 0) {
609
+ const take = leftover.subarray(0, remaining);
610
+ remaining -= take.length;
611
+ req.pushBody(take);
612
+ buffer = leftover.subarray(take.length);
613
+ }
614
+ if (remaining <= 0) {
615
+ req.finish();
616
+ return;
617
+ }
618
+ const bodyData = (next) => {
619
+ const bytes = toBytes(next);
620
+ const take = bytes.subarray(0, remaining);
621
+ remaining -= take.length;
622
+ req.pushBody(take);
623
+ if (remaining <= 0) {
624
+ socket.removeListener('data', bodyData);
625
+ socket.on('data', onData);
626
+ buffer = bytes.subarray(take.length);
627
+ req.finish();
628
+ }
629
+ };
630
+ socket.removeListener('data', onData);
631
+ socket.on('data', bodyData);
632
+ };
633
+ socket.on('data', onData);
634
+ socket.on('error', (error) => emitError(this, error));
635
+ }
636
+ }
637
+ export function createServer(options, listener) {
638
+ if (typeof options === 'function')
639
+ return new Server(options);
640
+ return new Server(listener);
641
+ }
642
+ export function request(urlOrOptions, optionsOrCallback, maybeCallback) {
643
+ let options = {};
644
+ let callback = maybeCallback;
645
+ if (typeof urlOrOptions === 'string' || urlOrOptions instanceof URL) {
646
+ const url = typeof urlOrOptions === 'string' ? new URL(urlOrOptions) : urlOrOptions;
647
+ options = {
648
+ host: url.hostname,
649
+ hostname: url.hostname,
650
+ port: url.port === '' ? 80 : Number(url.port),
651
+ path: `${url.pathname}${url.search}`,
652
+ protocol: url.protocol,
653
+ };
654
+ if (typeof optionsOrCallback === 'function')
655
+ callback = optionsOrCallback;
656
+ else if (optionsOrCallback !== undefined)
657
+ options = { ...options, ...optionsOrCallback };
658
+ }
659
+ else {
660
+ options = urlOrOptions;
661
+ if (typeof optionsOrCallback === 'function')
662
+ callback = optionsOrCallback;
663
+ }
664
+ if (options.protocol !== undefined && options.protocol !== 'http:') {
665
+ throw Object.assign(new TypeError(`Protocol "${options.protocol}" not supported. Expected "http:"`), { code: 'ERR_INVALID_PROTOCOL' });
666
+ }
667
+ return new ClientRequest(options, callback);
668
+ }
669
+ export function get(urlOrOptions, optionsOrCallback, maybeCallback) {
670
+ const req = request(urlOrOptions, optionsOrCallback, maybeCallback);
671
+ req.method = 'GET';
672
+ req.end();
673
+ return req;
674
+ }
675
+ export default {
676
+ METHODS,
677
+ STATUS_CODES,
678
+ maxHeaderSize,
679
+ IncomingMessage,
680
+ ServerResponse,
681
+ Server,
682
+ Agent,
683
+ globalAgent,
684
+ ClientRequest,
685
+ createServer,
686
+ request,
687
+ get,
688
+ };