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
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
/// <reference no-default-lib="true" />
/// <reference lib="esnext" />
/// <reference lib="esnext.disposable" />
declare namespace Deno {
/** @category Network */
export interface NetAddr {
transport: "tcp" | "udp";
hostname: string;
port: number;
}
/** @category Network */
export interface UnixAddr {
transport: "unix" | "unixpacket";
path: string;
}
/** @category Network */
export type Addr = NetAddr | UnixAddr;
/** A generic network listener for stream-oriented protocols.
*
* @category Network
*/
export interface Listener<T extends Conn = Conn, A extends Addr = Addr>
extends AsyncIterable<T>, Disposable {
/** Waits for and resolves to the next connection to the `Listener`. */
accept(): Promise<T>;
/** Close closes the listener. Any pending accept promises will be rejected
* with errors. */
close(): void;
/** Return the address of the `Listener`. */
readonly addr: A;
[Symbol.asyncIterator](): AsyncIterableIterator<T>;
/**
* Make the listener block the event loop from finishing.
*
* Note: the listener blocks the event loop from finishing by default.
* This method is only meaningful after `.unref()` is called.
*/
ref(): void;
/** Make the listener not block the event loop from finishing. */
unref(): void;
}
/** Specialized listener that accepts TLS connections.
*
* @category Network
*/
export type TlsListener = Listener<TlsConn, NetAddr>;
/** Specialized listener that accepts TCP connections.
*
* @category Network
*/
export type TcpListener = Listener<TcpConn, NetAddr>;
/** Specialized listener that accepts Unix connections.
*
* @category Network
*/
export type UnixListener = Listener<UnixConn, UnixAddr>;
/** @category Network */
export interface Conn<A extends Addr = Addr> extends Disposable {
/** Read the incoming data from the connection into an array buffer (`p`).
*
* Resolves to either the number of bytes read during the operation or EOF
* (`null`) if there was nothing more to read.
*
* It is possible for a read to successfully return with `0` bytes. This
* does not indicate EOF.
*
* **It is not guaranteed that the full buffer will be read in a single
* call.**
*
* ```ts
* // If the text "hello world" is received by the client:
* const conn = await Deno.connect({ hostname: "example.com", port: 80 });
* const buf = new Uint8Array(100);
* const numberOfBytesRead = await conn.read(buf); // 11 bytes
* const text = new TextDecoder().decode(buf); // "hello world"
* ```
*
* @category I/O
*/
read(p: Uint8Array): Promise<number | null>;
/** Write the contents of the array buffer (`p`) to the connection.
*
* Resolves to the number of bytes written.
*
* **It is not guaranteed that the full buffer will be written in a single
* call.**
*
* ```ts
* const conn = await Deno.connect({ hostname: "example.com", port: 80 });
* const encoder = new TextEncoder();
* const data = encoder.encode("Hello world");
* const bytesWritten = await conn.write(data); // 11
* ```
*
* @category I/O
*/
write(p: Uint8Array): Promise<number>;
/** Closes the connection, freeing the resource.
*
* ```ts
* const conn = await Deno.connect({ hostname: "example.com", port: 80 });
*
* // ...
*
* conn.close();
* ```
*/
close(): void;
/** The local address of the connection. */
readonly localAddr: A;
/** The remote address of the connection. */
readonly remoteAddr: A;
/** Shuts down (`shutdown(2)`) the write side of the connection. Most
* callers should just use `close()`. */
closeWrite(): Promise<void>;
/** Make the connection block the event loop from finishing.
*
* Note: the connection blocks the event loop from finishing by default.
* This method is only meaningful after `.unref()` is called.
*/
ref(): void;
/** Make the connection not block the event loop from finishing. */
unref(): void;
readonly readable: ReadableStream<Uint8Array>;
readonly writable: WritableStream<Uint8Array>;
}
/** @category Network */
export interface TlsHandshakeInfo {
/**
* Contains the ALPN protocol selected during negotiation with the server.
* If no ALPN protocol selected, returns `null`.
*/
alpnProtocol: string | null;
}
/** @category Network */
export interface TlsConn extends Conn<NetAddr> {
/** Runs the client or server handshake protocol to completion if that has
* not happened yet. Calling this method is optional; the TLS handshake
* will be completed automatically as soon as data is sent or received. */
handshake(): Promise<TlsHandshakeInfo>;
}
/** @category Network */
export interface ListenOptions {
/** The port to listen on.
*
* Set to `0` to listen on any available port.
*/
port: number;
/** A literal IP address or host name that can be resolved to an IP address.
*
* __Note about `0.0.0.0`__ While listening `0.0.0.0` works on all platforms,
* the browsers on Windows don't work with the address `0.0.0.0`.
* You should show the message like `server running on localhost:8080` instead of
* `server running on 0.0.0.0:8080` if your program supports Windows.
*
* @default {"0.0.0.0"} */
hostname?: string;
}
/** @category Network */
export interface TcpListenOptions extends ListenOptions {
}
/** Listen announces on the local transport address.
*
* ```ts
* const listener1 = Deno.listen({ port: 80 })
* const listener2 = Deno.listen({ hostname: "192.0.2.1", port: 80 })
* const listener3 = Deno.listen({ hostname: "[2001:db8::1]", port: 80 });
* const listener4 = Deno.listen({ hostname: "golang.org", port: 80, transport: "tcp" });
* ```
*
* Requires `allow-net` permission.
*
* @tags allow-net
* @category Network
*/
export function listen(
options: TcpListenOptions & { transport?: "tcp" },
): TcpListener;
/** Options which can be set when opening a Unix listener via
* {@linkcode Deno.listen} or {@linkcode Deno.listenDatagram}.
*
* @category Network
*/
export interface UnixListenOptions {
/** A path to the Unix Socket. */
path: string;
}
/** Listen announces on the local transport address.
*
* ```ts
* const listener = Deno.listen({ path: "/foo/bar.sock", transport: "unix" })
* ```
*
* Requires `allow-read` and `allow-write` permission.
*
* @tags allow-read, allow-write
* @category Network
*/
// deno-lint-ignore adjacent-overload-signatures
export function listen(
options: UnixListenOptions & { transport: "unix" },
): UnixListener;
/**
* Provides certified key material from strings. The key material is provided in
* `PEM`-format (Privacy Enhanced Mail, https://www.rfc-editor.org/rfc/rfc1422) which can be identified by having
* `-----BEGIN-----` and `-----END-----` markers at the beginning and end of the strings. This type of key is not compatible
* with `DER`-format keys which are binary.
*
* Deno supports RSA, EC, and PKCS8-format keys.
*
* ```ts
* const key = {
* key: "-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----\n",
* cert: "-----BEGIN CERTIFICATE-----\n...\n-----END CERTIFICATE-----\n" }
* };
* ```
*
* @category Network
*/
export interface TlsCertifiedKeyPem {
/** The format of this key material, which must be PEM. */
keyFormat?: "pem";
/** Private key in `PEM` format. RSA, EC, and PKCS8-format keys are supported. */
key: string;
/** Certificate chain in `PEM` format. */
cert: string;
}
/** @category Network */
export interface ListenTlsOptions extends TcpListenOptions {
transport?: "tcp";
/** Application-Layer Protocol Negotiation (ALPN) protocols to announce to
* the client. If not specified, no ALPN extension will be included in the
* TLS handshake.
*/
alpnProtocols?: string[];
}
/** Listen announces on the local transport address over TLS (transport layer
* security).
*
* ```ts
* using listener = Deno.listenTls({
* port: 443,
* cert: Deno.readTextFileSync("./server.crt"),
* key: Deno.readTextFileSync("./server.key"),
* });
* ```
*
* Requires `allow-net` permission.
*
* @tags allow-net
* @category Network
*/
export function listenTls(
options: ListenTlsOptions & TlsCertifiedKeyPem,
): TlsListener;
/** @category Network */
export interface ConnectOptions {
/** The port to connect to. */
port: number;
/** A literal IP address or host name that can be resolved to an IP address.
* If not specified,
*
* @default {"127.0.0.1"} */
hostname?: string;
transport?: "tcp";
}
/**
* Connects to the hostname (default is "127.0.0.1") and port on the named
* transport (default is "tcp"), and resolves to the connection (`Conn`).
*
* ```ts
* const conn1 = await Deno.connect({ port: 80 });
* const conn2 = await Deno.connect({ hostname: "192.0.2.1", port: 80 });
* const conn3 = await Deno.connect({ hostname: "[2001:db8::1]", port: 80 });
* const conn4 = await Deno.connect({ hostname: "golang.org", port: 80, transport: "tcp" });
* ```
*
* Requires `allow-net` permission for "tcp".
*
* @tags allow-net
* @category Network
*/
export function connect(options: ConnectOptions): Promise<TcpConn>;
/** @category Network */
export interface TcpConn extends Conn<NetAddr> {
/**
* Enable/disable the use of Nagle's algorithm.
*
* @param [noDelay=true]
*/
setNoDelay(noDelay?: boolean): void;
/** Enable/disable keep-alive functionality. */
setKeepAlive(keepAlive?: boolean): void;
}
/** @category Network */
export interface UnixConnectOptions {
transport: "unix";
path: string;
}
/** @category Network */
export interface UnixConn extends Conn<UnixAddr> {}
/** Connects to the hostname (default is "127.0.0.1") and port on the named
* transport (default is "tcp"), and resolves to the connection (`Conn`).
*
* ```ts
* const conn1 = await Deno.connect({ port: 80 });
* const conn2 = await Deno.connect({ hostname: "192.0.2.1", port: 80 });
* const conn3 = await Deno.connect({ hostname: "[2001:db8::1]", port: 80 });
* const conn4 = await Deno.connect({ hostname: "golang.org", port: 80, transport: "tcp" });
* const conn5 = await Deno.connect({ path: "/foo/bar.sock", transport: "unix" });
* ```
*
* Requires `allow-net` permission for "tcp" and `allow-read` for "unix".
*
* @tags allow-net, allow-read
* @category Network
*/
// deno-lint-ignore adjacent-overload-signatures
export function connect(options: UnixConnectOptions): Promise<UnixConn>;
/** @category Network */
export interface ConnectTlsOptions {
/** The port to connect to. */
port: number;
/** A literal IP address or host name that can be resolved to an IP address.
*
* @default {"127.0.0.1"} */
hostname?: string;
/** A list of root certificates that will be used in addition to the
* default root certificates to verify the peer's certificate.
*
* Must be in PEM format. */
caCerts?: string[];
/** Application-Layer Protocol Negotiation (ALPN) protocols supported by
* the client. If not specified, no ALPN extension will be included in the
* TLS handshake.
*/
alpnProtocols?: string[];
}
/** Establishes a secure connection over TLS (transport layer security) using
* an optional list of CA certs, hostname (default is "127.0.0.1") and port.
*
* The CA cert list is optional and if not included Mozilla's root
* certificates will be used (see also https://github.com/ctz/webpki-roots for
* specifics).
*
* Mutual TLS (mTLS or client certificates) are supported by providing a
* `key` and `cert` in the options as PEM-encoded strings.
*
* ```ts
* const caCert = await Deno.readTextFile("./certs/my_custom_root_CA.pem");
* const conn1 = await Deno.connectTls({ port: 80 });
* const conn2 = await Deno.connectTls({ caCerts: [caCert], hostname: "192.0.2.1", port: 80 });
* const conn3 = await Deno.connectTls({ hostname: "[2001:db8::1]", port: 80 });
* const conn4 = await Deno.connectTls({ caCerts: [caCert], hostname: "golang.org", port: 80});
*
* const key = "----BEGIN PRIVATE KEY----...";
* const cert = "----BEGIN CERTIFICATE----...";
* const conn5 = await Deno.connectTls({ port: 80, key, cert });
* ```
*
* Requires `allow-net` permission.
*
* @tags allow-net
* @category Network
*/
export function connectTls(
options: ConnectTlsOptions | (ConnectTlsOptions & TlsCertifiedKeyPem),
): Promise<TlsConn>;
/** @category Network */
export interface StartTlsOptions {
/** A literal IP address or host name that can be resolved to an IP address.
*
* @default {"127.0.0.1"} */
hostname?: string;
/** A list of root certificates that will be used in addition to the
* default root certificates to verify the peer's certificate.
*
* Must be in PEM format. */
caCerts?: string[];
/** Application-Layer Protocol Negotiation (ALPN) protocols to announce to
* the client. If not specified, no ALPN extension will be included in the
* TLS handshake.
*/
alpnProtocols?: string[];
}
/** Start TLS handshake from an existing connection using an optional list of
* CA certificates, and hostname (default is "127.0.0.1"). Specifying CA certs
* is optional. By default the configured root certificates are used. Using
* this function requires that the other end of the connection is prepared for
* a TLS handshake.
*
* Note that this function *consumes* the TCP connection passed to it, thus the
* original TCP connection will be unusable after calling this. Additionally,
* you need to ensure that the TCP connection is not being used elsewhere when
* calling this function in order for the TCP connection to be consumed properly.
* For instance, if there is a `Promise` that is waiting for read operation on
* the TCP connection to complete, it is considered that the TCP connection is
* being used elsewhere. In such a case, this function will fail.
*
* ```ts
* const conn = await Deno.connect({ port: 80, hostname: "127.0.0.1" });
* const caCert = await Deno.readTextFile("./certs/my_custom_root_CA.pem");
* // `conn` becomes unusable after calling `Deno.startTls`
* const tlsConn = await Deno.startTls(conn, { caCerts: [caCert], hostname: "localhost" });
* ```
*
* Requires `allow-net` permission.
*
* @tags allow-net
* @category Network
*/
export function startTls(
conn: TcpConn,
options?: StartTlsOptions,
): Promise<TlsConn>;
export {}; // only export exports
}
|