deno.land / std@0.166.0 / node / http.ts

نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
import { type Deferred, deferred } from "../async/deferred.ts";import { core } from "./_core.ts";import { _normalizeArgs, ListenOptions, Socket } from "./net.ts";import { Buffer } from "./buffer.ts";import { ERR_SERVER_NOT_RUNNING } from "./internal/errors.ts";import { EventEmitter } from "./events.ts";import { nextTick } from "./_next_tick.ts";import { Status as STATUS_CODES } from "../http/http_status.ts";import { validatePort } from "./internal/validators.mjs";import { Readable as NodeReadable, Writable as NodeWritable,} from "./stream.ts";import { OutgoingMessage } from "./_http_outgoing.ts";import { Agent } from "./_http_agent.mjs";import { chunkExpression as RE_TE_CHUNKED } from "./_http_common.ts";import { urlToHttpOptions } from "./internal/url.ts";import { constants, TCP } from "./internal_binding/tcp_wrap.ts";
const METHODS = [ "ACL", "BIND", "CHECKOUT", "CONNECT", "COPY", "DELETE", "GET", "HEAD", "LINK", "LOCK", "M-SEARCH", "MERGE", "MKACTIVITY", "MKCALENDAR", "MKCOL", "MOVE", "NOTIFY", "OPTIONS", "PATCH", "POST", "PROPFIND", "PROPPATCH", "PURGE", "PUT", "REBIND", "REPORT", "SEARCH", "SOURCE", "SUBSCRIBE", "TRACE", "UNBIND", "UNLINK", "UNLOCK", "UNSUBSCRIBE",];
type Chunk = string | Buffer | Uint8Array;
// @ts-ignore Deno[Deno.internal] is used on purpose hereconst DenoServe = Deno[Deno.internal]?.nodeUnstable?.serve || Deno.serve;// @ts-ignore Deno[Deno.internal] is used on purpose hereconst DenoUpgradeHttpRaw = Deno[Deno.internal]?.nodeUnstable?.upgradeHttpRaw || Deno.upgradeHttpRaw;
function chunkToU8(chunk: Chunk): Uint8Array { if (typeof chunk === "string") { return core.encode(chunk); } return chunk;}
export interface RequestOptions { agent?: Agent; auth?: string; createConnection?: () => unknown; defaultPort?: number; family?: number; headers?: Record<string, string>; hints?: number; host?: string; hostname?: string; insecureHTTPParser?: boolean; localAddress?: string; localPort?: number; lookup?: () => void; maxHeaderSize?: number; method?: string; path?: string; port?: number; protocol?: string; setHost?: boolean; socketPath?: string; timeout?: number; signal?: AbortSignal; href?: string;}
// TODO: Implement ClientRequest methods (e.g. setHeader())/** ClientRequest represents the http(s) request from the client */class ClientRequest extends NodeWritable { defaultProtocol = "http:"; body: null | ReadableStream = null; controller: ReadableStreamDefaultController | null = null; constructor( public opts: RequestOptions, public cb?: (res: IncomingMessageForClient) => void, ) { super(); }
// deno-lint-ignore no-explicit-any override _write(chunk: any, _enc: string, cb: () => void) { if (this.controller) { this.controller.enqueue(chunk); cb(); return; }
this.body = new ReadableStream({ start: (controller) => { this.controller = controller; controller.enqueue(chunk); cb(); }, }); }
override async _final() { if (this.controller) { this.controller.close(); }
const body = await this._createBody(this.body, this.opts); const client = await this._createCustomClient(); const opts = { body, method: this.opts.method, client, headers: this.opts.headers, }; const mayResponse = fetch(this._createUrlStrFromOptions(this.opts), opts) .catch((e) => { if (e.message.includes("connection closed before message completed")) { // Node.js seems ignoring this error } else { this.emit("error", e); } return undefined; }); const res = new IncomingMessageForClient( await mayResponse, this._createSocket(), ); this.emit("response", res); if (client) { res.on("end", () => { client.close(); }); } this.cb?.(res); }
abort() { this.destroy(); }
async _createBody( body: ReadableStream | null, opts: RequestOptions, ): Promise<Buffer | ReadableStream | null> { if (!body) return null; if (!opts.headers) return body;
const headers = Object.fromEntries( Object.entries(opts.headers).map(([k, v]) => [k.toLowerCase(), v]), );
if ( !RE_TE_CHUNKED.test(headers["transfer-encoding"]) && !Number.isNaN(Number.parseInt(headers["content-length"], 10)) ) { const bufferList: Buffer[] = []; for await (const chunk of body) { bufferList.push(chunk); } return Buffer.concat(bufferList); }
return body; }
_createCustomClient(): Promise<Deno.HttpClient | undefined> { return Promise.resolve(undefined); }
_createSocket(): Socket { // Note: Creates a dummy socket for the compatibility // Sometimes the libraries check some properties of socket // e.g. if (!response.socket.authorized) { ... } return new Socket({}); }
_createUrlStrFromOptions(opts: RequestOptions): string { if (opts.href) { return opts.href; } const protocol = opts.protocol ?? this.defaultProtocol; const auth = opts.auth; const host = opts.host ?? opts.hostname ?? "localhost"; const defaultPort = opts.agent?.defaultPort; const port = opts.port ?? defaultPort ?? 80; let path = opts.path ?? "/"; if (!path.startsWith("/")) { path = "/" + path; } return `${protocol}//${auth ? `${auth}@` : ""}${host}${ port === 80 ? "" : `:${port}` }${path}`; }
setTimeout() { console.log("not implemented: ClientRequest.setTimeout"); }}
/** IncomingMessage for http(s) client */export class IncomingMessageForClient extends NodeReadable { reader: ReadableStreamDefaultReader | undefined; #statusMessage = ""; constructor(public response: Response | undefined, public socket: Socket) { super(); this.reader = response?.body?.getReader(); }
override async _read(_size: number) { if (this.reader === undefined) { this.push(null); return; } try { const res = await this.reader.read(); if (res.done) { this.push(null); return; } this.push(res.value); } catch (e) { // deno-lint-ignore no-explicit-any this.destroy(e as any); } }
get headers() { if (this.response) { return Object.fromEntries(this.response.headers.entries()); } return {}; }
get trailers() { return {}; }
get statusCode() { return this.response?.status || 0; }
get statusMessage() { return this.#statusMessage || this.response?.statusText || ""; }
set statusMessage(v: string) { this.#statusMessage = v; }}
export class ServerResponse extends NodeWritable { statusCode?: number = undefined; statusMessage?: string = undefined; #headers = new Headers({}); #readable: ReadableStream; override writable = true; // used by `npm:on-finished` finished = false; headersSent = false; #firstChunk: Chunk | null = null; // Used if --unstable flag IS NOT present #reqEvent?: Deno.RequestEvent; // Used if --unstable flag IS present #resolve?: (value: Response | PromiseLike<Response>) => void; #isFlashRequest: boolean;
constructor( reqEvent: undefined | Deno.RequestEvent, resolve: undefined | ((value: Response | PromiseLike<Response>) => void), ) { let controller: ReadableByteStreamController; const readable = new ReadableStream({ start(c) { controller = c as ReadableByteStreamController; }, }); super({ autoDestroy: true, defaultEncoding: "utf-8", emitClose: true, write: (chunk, _encoding, cb) => { if (!this.headersSent) { if (this.#firstChunk === null) { this.#firstChunk = chunk; return cb(); } else { controller.enqueue(chunkToU8(this.#firstChunk)); this.#firstChunk = null; this.respond(false); } } controller.enqueue(chunkToU8(chunk)); return cb(); }, final: (cb) => { if (this.#firstChunk) { this.respond(true, this.#firstChunk); } else if (!this.headersSent) { this.respond(true); } controller.close(); return cb(); }, destroy: (err, cb) => { if (err) { controller.error(err); } return cb(null); }, }); this.#readable = readable; this.#resolve = resolve; this.#reqEvent = reqEvent; this.#isFlashRequest = typeof resolve !== "undefined"; }
setHeader(name: string, value: string) { this.#headers.set(name, value); return this; }
getHeader(name: string) { return this.#headers.get(name); } removeHeader(name: string) { return this.#headers.delete(name); } getHeaderNames() { return Array.from(this.#headers.keys()); } hasHeader(name: string) { return this.#headers.has(name); }
writeHead(status: number, headers: Record<string, string>) { this.statusCode = status; for (const k in headers) { this.#headers.set(k, headers[k]); } return this; }
#ensureHeaders(singleChunk?: Chunk) { if (this.statusCode === undefined) { this.statusCode = 200; this.statusMessage = "OK"; } // Only taken if --unstable IS NOT present if ( !this.#isFlashRequest && typeof singleChunk === "string" && !this.hasHeader("content-type") ) { this.setHeader("content-type", "text/plain;charset=UTF-8"); } }
respond(final: boolean, singleChunk?: Chunk) { this.headersSent = true; this.#ensureHeaders(singleChunk); const body = singleChunk ?? (final ? null : this.#readable); if (this.#isFlashRequest) { this.#resolve!( new Response(body, { headers: this.#headers, status: this.statusCode, statusText: this.statusMessage, }), ); } else { this.#reqEvent!.respondWith( new Response(body, { headers: this.#headers, status: this.statusCode, statusText: this.statusMessage, }), ).catch(() => { // ignore this error }); } }
// deno-lint-ignore no-explicit-any override end(chunk?: any, encoding?: any, cb?: any): this { this.finished = true; if (this.#isFlashRequest) { // Flash sets both of these headers. this.#headers.delete("transfer-encoding"); this.#headers.delete("content-length"); } else if (!chunk && this.#headers.has("transfer-encoding")) { // FIXME(bnoordhuis) Node sends a zero length chunked body instead, i.e., // the trailing "0\r\n", but respondWith() just hangs when I try that. this.#headers.set("content-length", "0"); this.#headers.delete("transfer-encoding"); }
// @ts-expect-error The signature for cb is stricter than the one implemented here return super.end(chunk, encoding, cb); }}
// TODO(@AaronO): optimizeexport class IncomingMessageForServer extends NodeReadable { #req: Request; url: string; method: string;
constructor(req: Request) { // Check if no body (GET/HEAD/OPTIONS/...) const reader = req.body?.getReader(); super({ autoDestroy: true, emitClose: true, objectMode: false, read: async function (_size) { if (!reader) { return this.push(null); }
try { const { value } = await reader!.read(); this.push(value !== undefined ? Buffer.from(value) : null); } catch (err) { this.destroy(err as Error); } }, destroy: (err, cb) => { reader?.cancel().finally(() => cb(err)); }, }); // TODO: consider more robust path extraction, e.g: // url: (new URL(request.url).pathname), this.url = req.url?.slice(req.url.indexOf("/", 8)); this.method = req.method; this.#req = req; }
get aborted() { return false; }
get httpVersion() { return "1.1"; }
get headers() { return Object.fromEntries(this.#req.headers.entries()); }
get upgrade(): boolean { return Boolean( this.#req.headers.get("connection")?.toLowerCase().includes("upgrade") && this.#req.headers.get("upgrade"), ); }}
type ServerHandler = ( req: IncomingMessageForServer, res: ServerResponse,) => void;
export function Server(handler?: ServerHandler): ServerImpl { return new ServerImpl(handler);}
class ServerImpl extends EventEmitter { #isFlashServer: boolean;
#httpConnections: Set<Deno.HttpConn> = new Set(); #listener?: Deno.Listener;
#addr?: Deno.NetAddr; #hasClosed = false; #ac?: AbortController; #servePromise?: Deferred<void>; listening = false;
constructor(handler?: ServerHandler) { super(); // @ts-ignore Might be undefined without `--unstable` flag this.#isFlashServer = typeof DenoServe == "function"; if (this.#isFlashServer) { this.#servePromise = deferred(); this.#servePromise.then(() => this.emit("close")); } if (handler !== undefined) { this.on("request", handler); } }
listen(...args: unknown[]): this { // TODO(bnoordhuis) Delegate to net.Server#listen(). const normalized = _normalizeArgs(args); const options = normalized[0] as Partial<ListenOptions>; const cb = normalized[1];
if (cb !== null) { // @ts-ignore change EventEmitter's sig to use CallableFunction this.once("listening", cb); }
let port = 0; if (typeof options.port === "number" || typeof options.port === "string") { validatePort(options.port, "options.port"); port = options.port | 0; }
// TODO(bnoordhuis) Node prefers [::] when host is omitted, // we on the other hand default to 0.0.0.0. if (this.#isFlashServer) { const hostname = options.host ?? "0.0.0.0"; this.#addr = { hostname, port, } as Deno.NetAddr; this.listening = true; nextTick(() => this.#serve()); } else { this.listening = true; const hostname = options.host ?? ""; this.#listener = Deno.listen({ port, hostname }); nextTick(() => this.#listenLoop()); }
return this; }
async #listenLoop() { const go = async (httpConn: Deno.HttpConn) => { try { for (;;) { let reqEvent = null; try { // Note: httpConn.nextRequest() calls httpConn.close() on error. reqEvent = await httpConn.nextRequest(); } catch { // Connection closed. // TODO(bnoordhuis) Emit "clientError" event on the http.Server // instance? Node emits it when request parsing fails and expects // the listener to send a raw 4xx HTTP response on the underlying // net.Socket but we don't have one to pass to the listener. } if (reqEvent === null) { break; } const req = new IncomingMessageForServer(reqEvent.request); const res = new ServerResponse(reqEvent, undefined); this.emit("request", req, res); } } finally { this.#httpConnections.delete(httpConn); } };
const listener = this.#listener;
if (listener !== undefined) { this.emit("listening");
for await (const conn of listener) { let httpConn: Deno.HttpConn; try { httpConn = Deno.serveHttp(conn); } catch { continue; /// Connection closed. }
this.#httpConnections.add(httpConn); go(httpConn); } } }
#serve() { const ac = new AbortController(); const handler = (request: Request) => { const req = new IncomingMessageForServer(request); if (req.upgrade && this.listenerCount("upgrade") > 0) { const [conn, head] = DenoUpgradeHttpRaw(request) as [ Deno.Conn, Uint8Array, ]; const socket = new Socket({ handle: new TCP(constants.SERVER, conn), }); this.emit("upgrade", req, socket, Buffer.from(head)); } else { return new Promise<Response>((resolve): void => { const res = new ServerResponse(undefined, resolve); this.emit("request", req, res); }); } };
if (this.#hasClosed) { return; } this.#ac = ac; DenoServe( { handler: handler as Deno.ServeHandler, ...this.#addr, signal: ac.signal, // @ts-ignore Might be any without `--unstable` flag onListen: ({ port }) => { this.#addr!.port = port; this.emit("listening"); }, }, ).then(() => this.#servePromise!.resolve()); }
setTimeout() { console.error("Not implemented: Server.setTimeout()"); }
close(cb?: (err?: Error) => void): this { const listening = this.listening; this.listening = false;
this.#hasClosed = true; if (typeof cb === "function") { if (listening) { this.once("close", cb); } else { this.once("close", function close() { cb(new ERR_SERVER_NOT_RUNNING()); }); } }
if (this.#isFlashServer) { if (listening && this.#ac) { this.#ac.abort(); this.#ac = undefined; } else { this.#servePromise!.resolve(); } } else { nextTick(() => this.emit("close"));
if (listening) { this.#listener!.close(); this.#listener = undefined;
for (const httpConn of this.#httpConnections) { try { httpConn.close(); } catch { // Already closed. } }
this.#httpConnections.clear(); } }
return this; }
address() { let addr; if (this.#isFlashServer) { addr = this.#addr!; } else { addr = this.#listener!.addr as Deno.NetAddr; } return { port: addr.port, address: addr.hostname, }; }}
Server.prototype = ServerImpl.prototype;
export function createServer(handler?: ServerHandler) { return Server(handler);}
/** Makes an HTTP request. */export function request( url: string | URL, cb?: (res: IncomingMessageForClient) => void,): ClientRequest;export function request( opts: RequestOptions, cb?: (res: IncomingMessageForClient) => void,): ClientRequest;export function request( url: string | URL, opts: RequestOptions, cb?: (res: IncomingMessageForClient) => void,): ClientRequest;// deno-lint-ignore no-explicit-anyexport function request(...args: any[]) { let options = {}; if (typeof args[0] === "string") { options = urlToHttpOptions(new URL(args.shift())); } else if (args[0] instanceof URL) { options = urlToHttpOptions(args.shift()); } if (args[0] && typeof args[0] !== "function") { Object.assign(options, args.shift()); } args.unshift(options); return new ClientRequest(args[0], args[1]);}
/** Makes a `GET` HTTP request. */export function get( url: string | URL, cb?: (res: IncomingMessageForClient) => void,): ClientRequest;export function get( opts: RequestOptions, cb?: (res: IncomingMessageForClient) => void,): ClientRequest;export function get( url: string | URL, opts: RequestOptions, cb?: (res: IncomingMessageForClient) => void,): ClientRequest;// deno-lint-ignore no-explicit-anyexport function get(...args: any[]) { const req = request(args[0], args[1], args[2]); req.end(); return req;}
export { Agent, ClientRequest, IncomingMessageForServer as IncomingMessage, METHODS, OutgoingMessage, STATUS_CODES,};export default { Agent, ClientRequest, STATUS_CODES, METHODS, createServer, Server, IncomingMessage: IncomingMessageForServer, IncomingMessageForClient, IncomingMessageForServer, OutgoingMessage, ServerResponse, request, get,};
std
Deno standard library
GitHub Repositorydenoland/deno_std
GitHub Stars
2447

Version Info

Tagged at
2 months ago