@netifyjs/netify 0.0.0-stage → 1.0.0-alpha.0
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.
- package/LICENSE +21 -0
- package/README.md +105 -2
- package/package.json +65 -4
- package/src/bootstrap/client/index.ts +191 -0
- package/src/bootstrap/dispatcher/index.ts +204 -0
- package/src/bootstrap/index.ts +22 -0
- package/src/bootstrap/reconnect/index.ts +334 -0
- package/src/bootstrap/server/index.ts +284 -0
- package/src/buffer/bits/index.ts +2 -0
- package/src/buffer/bits/reader/index.ts +64 -0
- package/src/buffer/bits/writer/index.ts +79 -0
- package/src/buffer/index.ts +1375 -0
- package/src/buffer/pool/index.ts +46 -0
- package/src/buffer/quantizer/index.ts +101 -0
- package/src/channel/attribute/index.ts +89 -0
- package/src/channel/context/index.ts +621 -0
- package/src/channel/group/index.ts +256 -0
- package/src/channel/handler/duplex/index.ts +105 -0
- package/src/channel/handler/inbound/index.ts +78 -0
- package/src/channel/handler/index.ts +135 -0
- package/src/channel/handler/outbound/index.ts +33 -0
- package/src/channel/index.ts +619 -0
- package/src/channel/initializer/index.ts +77 -0
- package/src/channel/outbound/index.ts +611 -0
- package/src/channel/pipeline/head/index.ts +100 -0
- package/src/channel/pipeline/index.ts +414 -0
- package/src/channel/pipeline/tail/index.ts +45 -0
- package/src/channel/pipeline/tracker/index.ts +120 -0
- package/src/channel/queue/index.ts +203 -0
- package/src/channel/resolved/index.ts +34 -0
- package/src/channel/server/index.ts +164 -0
- package/src/channel/shutdown/index.ts +25 -0
- package/src/channel/simple/index.ts +38 -0
- package/src/channel/transport/index.ts +74 -0
- package/src/channel/transport/socket/index.ts +80 -0
- package/src/cluster/index.ts +360 -0
- package/src/codec/binary/index.ts +102 -0
- package/src/codec/decoder/index.ts +479 -0
- package/src/codec/delimiter/index.ts +189 -0
- package/src/codec/encoder/index.ts +48 -0
- package/src/codec/error/index.ts +33 -0
- package/src/codec/fixed/index.ts +45 -0
- package/src/codec/http/aggregator/index.ts +236 -0
- package/src/codec/http/client/index.ts +120 -0
- package/src/codec/http/decoder/index.ts +4 -0
- package/src/codec/http/decoder/object/index.ts +644 -0
- package/src/codec/http/decoder/request/index.ts +49 -0
- package/src/codec/http/decoder/response/index.ts +59 -0
- package/src/codec/http/encoder/index.ts +3 -0
- package/src/codec/http/encoder/object/index.ts +196 -0
- package/src/codec/http/encoder/request/index.ts +24 -0
- package/src/codec/http/encoder/response/index.ts +26 -0
- package/src/codec/http/headers/index.ts +287 -0
- package/src/codec/http/index.ts +33 -0
- package/src/codec/http/message/content/index.ts +36 -0
- package/src/codec/http/message/full/index.ts +10 -0
- package/src/codec/http/message/full/request/index.ts +45 -0
- package/src/codec/http/message/full/response/index.ts +43 -0
- package/src/codec/http/message/head/index.ts +79 -0
- package/src/codec/http/message/index.ts +16 -0
- package/src/codec/http/message/last/index.ts +27 -0
- package/src/codec/http/message/request/index.ts +39 -0
- package/src/codec/http/message/response/index.ts +31 -0
- package/src/codec/http/method/index.ts +101 -0
- package/src/codec/http/server/index.ts +81 -0
- package/src/codec/http/status/index.ts +170 -0
- package/src/codec/http/version/index.ts +70 -0
- package/src/codec/index.ts +83 -0
- package/src/codec/length/index.ts +330 -0
- package/src/codec/line/index.ts +45 -0
- package/src/codec/message/decoder/index.ts +84 -0
- package/src/codec/message/encoder/index.ts +56 -0
- package/src/codec/message/index.ts +2 -0
- package/src/codec/msgpack/index.ts +51 -0
- package/src/codec/prepender/index.ts +296 -0
- package/src/codec/protobuf/index.ts +57 -0
- package/src/codec/string/decoder/index.ts +47 -0
- package/src/codec/string/encoder/index.ts +26 -0
- package/src/codec/string/index.ts +2 -0
- package/src/codec/websocket/client/index.ts +235 -0
- package/src/codec/websocket/decoder/index.ts +371 -0
- package/src/codec/websocket/encoder/index.ts +170 -0
- package/src/codec/websocket/error/index.ts +42 -0
- package/src/codec/websocket/frame/base/index.ts +55 -0
- package/src/codec/websocket/frame/binary/index.ts +24 -0
- package/src/codec/websocket/frame/close/index.ts +82 -0
- package/src/codec/websocket/frame/continuation/index.ts +31 -0
- package/src/codec/websocket/frame/index.ts +10 -0
- package/src/codec/websocket/frame/opcode/index.ts +35 -0
- package/src/codec/websocket/frame/ping/index.ts +24 -0
- package/src/codec/websocket/frame/pong/index.ts +24 -0
- package/src/codec/websocket/frame/text/index.ts +31 -0
- package/src/codec/websocket/handshake/index.ts +38 -0
- package/src/codec/websocket/index.ts +25 -0
- package/src/codec/websocket/key/index.ts +41 -0
- package/src/codec/websocket/protocol/index.ts +147 -0
- package/src/codec/websocket/server/index.ts +233 -0
- package/src/codec/websocket/status/index.ts +97 -0
- package/src/codec/websocket/upgrade/index.ts +116 -0
- package/src/datagram/bootstrap/index.ts +269 -0
- package/src/datagram/channel/index.ts +103 -0
- package/src/datagram/index.ts +10 -0
- package/src/datagram/packet/index.ts +72 -0
- package/src/datagram/transport/index.ts +239 -0
- package/src/handler/flush/index.ts +187 -0
- package/src/handler/idle/index.ts +295 -0
- package/src/handler/index.ts +22 -0
- package/src/handler/logging/index.ts +195 -0
- package/src/handler/metrics/index.ts +300 -0
- package/src/handler/offload/index.ts +141 -0
- package/src/handler/timeout/error/index.ts +29 -0
- package/src/handler/timeout/index.ts +3 -0
- package/src/handler/timeout/read/index.ts +44 -0
- package/src/handler/timeout/write/index.ts +89 -0
- package/src/handler/traffic/index.ts +259 -0
- package/src/index.ts +258 -0
- package/src/logger/index.ts +47 -0
- package/src/reliable/bootstrap/index.ts +154 -0
- package/src/reliable/cipher/index.ts +227 -0
- package/src/reliable/cookie/index.ts +78 -0
- package/src/reliable/handler/index.ts +1202 -0
- package/src/reliable/handshake/index.ts +144 -0
- package/src/reliable/index.ts +38 -0
- package/src/reliable/lane/index.ts +420 -0
- package/src/reliable/message/index.ts +102 -0
- package/src/reliable/packet/index.ts +117 -0
- package/src/reliable/peer/index.ts +929 -0
- package/src/reliable/registry/index.ts +107 -0
- package/src/serve/bootstrap/index.ts +606 -0
- package/src/serve/channel/index.ts +246 -0
- package/src/serve/index.ts +17 -0
- package/src/serve/outbox/index.ts +292 -0
- package/src/serve/transport/index.ts +96 -0
- package/src/serve/writer/index.ts +119 -0
- package/src/shard/envelope/index.ts +44 -0
- package/src/shard/index.ts +10 -0
- package/src/shard/link/index.ts +743 -0
- package/src/shard/map/index.ts +91 -0
- package/src/worker/group/index.ts +175 -0
- package/src/worker/index.ts +10 -0
- package/src/worker/pool/index.ts +235 -0
- package/src/worker/service/index.ts +55 -0
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { HttpHeaders } from '../../../headers/index.ts'
|
|
2
|
+
import type { HttpMethod } from '../../../method/index.ts'
|
|
3
|
+
import { HttpVersion } from '../../../version/index.ts'
|
|
4
|
+
import { HttpContent, type ContentSource } from '../../content/index.ts'
|
|
5
|
+
import { HttpRequest } from '../../request/index.ts'
|
|
6
|
+
import type { ByteBuf } from '../../../../../buffer/index.ts'
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* A request with the head and entire body together
|
|
10
|
+
* @class
|
|
11
|
+
*/
|
|
12
|
+
export class FullHttpRequest extends HttpRequest {
|
|
13
|
+
/**
|
|
14
|
+
* Whole body in one buffer
|
|
15
|
+
*/
|
|
16
|
+
public readonly content: ByteBuf
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Trailers from a chunked body, usually empty
|
|
20
|
+
*/
|
|
21
|
+
public readonly trailers: HttpHeaders
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Builds a complete request, with an empty body by default
|
|
25
|
+
* @param method request method
|
|
26
|
+
* @param uri path and query
|
|
27
|
+
* @param content body as bytes or text
|
|
28
|
+
* @param version defaults to HTTP/1.1
|
|
29
|
+
* @param headers starts empty if left out
|
|
30
|
+
* @param trailers usually left empty
|
|
31
|
+
*/
|
|
32
|
+
public constructor(
|
|
33
|
+
method: HttpMethod,
|
|
34
|
+
uri: string,
|
|
35
|
+
content: ContentSource = new Uint8Array(0),
|
|
36
|
+
version: HttpVersion = HttpVersion.HTTP_1_1,
|
|
37
|
+
headers: HttpHeaders = new HttpHeaders(),
|
|
38
|
+
trailers: HttpHeaders = new HttpHeaders(),
|
|
39
|
+
) {
|
|
40
|
+
super(method, uri, version, headers)
|
|
41
|
+
|
|
42
|
+
this.content = HttpContent.bufferOf(content)
|
|
43
|
+
this.trailers = trailers
|
|
44
|
+
}
|
|
45
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { HttpHeaders } from '../../../headers/index.ts'
|
|
2
|
+
import type { HttpResponseStatus } from '../../../status/index.ts'
|
|
3
|
+
import { HttpVersion } from '../../../version/index.ts'
|
|
4
|
+
import { HttpContent, type ContentSource } from '../../content/index.ts'
|
|
5
|
+
import { HttpResponse } from '../../response/index.ts'
|
|
6
|
+
import type { ByteBuf } from '../../../../../buffer/index.ts'
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* A response with the head and entire body together
|
|
10
|
+
* @class
|
|
11
|
+
*/
|
|
12
|
+
export class FullHttpResponse extends HttpResponse {
|
|
13
|
+
/**
|
|
14
|
+
* Whole body in one buffer
|
|
15
|
+
*/
|
|
16
|
+
public readonly content: ByteBuf
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Trailers from a chunked body, usually empty
|
|
20
|
+
*/
|
|
21
|
+
public readonly trailers: HttpHeaders
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Builds a complete response, with an empty body by default
|
|
25
|
+
* @param status code and phrase to send
|
|
26
|
+
* @param content body as bytes or text
|
|
27
|
+
* @param version defaults to HTTP/1.1
|
|
28
|
+
* @param headers starts empty if left out
|
|
29
|
+
* @param trailers usually left empty
|
|
30
|
+
*/
|
|
31
|
+
public constructor(
|
|
32
|
+
status: HttpResponseStatus,
|
|
33
|
+
content: ContentSource = new Uint8Array(0),
|
|
34
|
+
version: HttpVersion = HttpVersion.HTTP_1_1,
|
|
35
|
+
headers: HttpHeaders = new HttpHeaders(),
|
|
36
|
+
trailers: HttpHeaders = new HttpHeaders(),
|
|
37
|
+
) {
|
|
38
|
+
super(status, version, headers)
|
|
39
|
+
|
|
40
|
+
this.content = HttpContent.bufferOf(content)
|
|
41
|
+
this.trailers = trailers
|
|
42
|
+
}
|
|
43
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import type { HttpVersion } from '../../version/index.ts'
|
|
2
|
+
import type { HttpHeaders } from '../../headers/index.ts'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Start line and headers shared by requests and responses
|
|
6
|
+
* @class
|
|
7
|
+
*/
|
|
8
|
+
export abstract class HttpMessage {
|
|
9
|
+
/**
|
|
10
|
+
* HTTP version on the start line
|
|
11
|
+
*/
|
|
12
|
+
public version: HttpVersion
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Headers in the order they arrived
|
|
16
|
+
*/
|
|
17
|
+
public readonly headers: HttpHeaders
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Sets the shared parts of a request or response
|
|
21
|
+
* @param version HTTP version
|
|
22
|
+
* @param headers starting headers
|
|
23
|
+
*/
|
|
24
|
+
protected constructor(version: HttpVersion, headers: HttpHeaders) {
|
|
25
|
+
this.version = version
|
|
26
|
+
this.headers = headers
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Works out keep alive from Connection and the version default
|
|
31
|
+
*/
|
|
32
|
+
public isKeepAlive(): boolean {
|
|
33
|
+
if (this.headers.hasToken('connection', 'close')) return false
|
|
34
|
+
if (this.version.keepAliveDefault) return true
|
|
35
|
+
|
|
36
|
+
return this.headers.hasToken('connection', 'keep-alive')
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Writes a Connection header only when it differs from the default
|
|
41
|
+
* @param keepAlive true to keep the connection open
|
|
42
|
+
*/
|
|
43
|
+
public setKeepAlive(keepAlive: boolean): this {
|
|
44
|
+
if (keepAlive === this.version.keepAliveDefault) {
|
|
45
|
+
this.headers.remove('connection')
|
|
46
|
+
return this
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
this.headers.set('Connection', keepAlive ? 'keep-alive' : 'close')
|
|
50
|
+
return this
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Content-Length as a number, or null when missing or malformed
|
|
55
|
+
*/
|
|
56
|
+
public contentLength(): number | null {
|
|
57
|
+
const value = this.headers.get('content-length')?.trim() ?? ''
|
|
58
|
+
if (!/^\d+$/.test(value)) return null
|
|
59
|
+
|
|
60
|
+
const length = Number(value)
|
|
61
|
+
return Number.isSafeInteger(length) ? length : null
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Replaces Content-Length
|
|
66
|
+
* @param length body size in bytes
|
|
67
|
+
*/
|
|
68
|
+
public setContentLength(length: number): this {
|
|
69
|
+
this.headers.set('Content-Length', length)
|
|
70
|
+
return this
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* True when chunked is the last transfer coding
|
|
75
|
+
*/
|
|
76
|
+
public isChunked(): boolean {
|
|
77
|
+
return this.headers.tokens('transfer-encoding').at(-1) === 'chunked'
|
|
78
|
+
}
|
|
79
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { HttpMessage } from './head/index.ts'
|
|
2
|
+
import type { HttpContent } from './content/index.ts'
|
|
3
|
+
|
|
4
|
+
export { HttpContent } from './content/index.ts'
|
|
5
|
+
export type { ContentSource } from './content/index.ts'
|
|
6
|
+
export { FullHttpRequest, FullHttpResponse } from './full/index.ts'
|
|
7
|
+
export type { FullHttpMessage } from './full/index.ts'
|
|
8
|
+
export { HttpMessage } from './head/index.ts'
|
|
9
|
+
export { LastHttpContent } from './last/index.ts'
|
|
10
|
+
export { HttpRequest } from './request/index.ts'
|
|
11
|
+
export { HttpResponse } from './response/index.ts'
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Anything the HTTP codecs pass along, a head or a piece of body
|
|
15
|
+
*/
|
|
16
|
+
export type HttpObject = HttpMessage | HttpContent
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { HttpHeaders } from '../../headers/index.ts'
|
|
2
|
+
import { HttpContent, type ContentSource } from '../content/index.ts'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Final body chunk, which can also carry trailers
|
|
6
|
+
* @class
|
|
7
|
+
*/
|
|
8
|
+
export class LastHttpContent extends HttpContent {
|
|
9
|
+
/**
|
|
10
|
+
* Headers sent after a chunked body
|
|
11
|
+
*/
|
|
12
|
+
public readonly trailers: HttpHeaders
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Builds the final chunk, empty by default
|
|
16
|
+
* @param content last bytes of the body
|
|
17
|
+
* @param trailers trailing headers, none by default
|
|
18
|
+
*/
|
|
19
|
+
public constructor(
|
|
20
|
+
content: ContentSource = new Uint8Array(0),
|
|
21
|
+
trailers: HttpHeaders = new HttpHeaders(),
|
|
22
|
+
) {
|
|
23
|
+
super(content)
|
|
24
|
+
|
|
25
|
+
this.trailers = trailers
|
|
26
|
+
}
|
|
27
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { HttpHeaders } from '../../headers/index.ts'
|
|
2
|
+
import type { HttpMethod } from '../../method/index.ts'
|
|
3
|
+
import { HttpVersion } from '../../version/index.ts'
|
|
4
|
+
import { HttpMessage } from '../head/index.ts'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Request line and headers, with the body following separately
|
|
8
|
+
* @class
|
|
9
|
+
*/
|
|
10
|
+
export class HttpRequest extends HttpMessage {
|
|
11
|
+
/**
|
|
12
|
+
* GET, POST and so on
|
|
13
|
+
*/
|
|
14
|
+
public method: HttpMethod
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Path and query, or an absolute URL for proxies
|
|
18
|
+
*/
|
|
19
|
+
public uri: string
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Builds a request head, HTTP/1.1 by default
|
|
23
|
+
* @param method request method
|
|
24
|
+
* @param uri path and query
|
|
25
|
+
* @param version defaults to HTTP/1.1
|
|
26
|
+
* @param headers starts empty if left out
|
|
27
|
+
*/
|
|
28
|
+
public constructor(
|
|
29
|
+
method: HttpMethod,
|
|
30
|
+
uri: string,
|
|
31
|
+
version: HttpVersion = HttpVersion.HTTP_1_1,
|
|
32
|
+
headers: HttpHeaders = new HttpHeaders(),
|
|
33
|
+
) {
|
|
34
|
+
super(version, headers)
|
|
35
|
+
|
|
36
|
+
this.method = method
|
|
37
|
+
this.uri = uri
|
|
38
|
+
}
|
|
39
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { HttpHeaders } from '../../headers/index.ts'
|
|
2
|
+
import type { HttpResponseStatus } from '../../status/index.ts'
|
|
3
|
+
import { HttpVersion } from '../../version/index.ts'
|
|
4
|
+
import { HttpMessage } from '../head/index.ts'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Status line and headers, with the body following separately
|
|
8
|
+
* @class
|
|
9
|
+
*/
|
|
10
|
+
export class HttpResponse extends HttpMessage {
|
|
11
|
+
/**
|
|
12
|
+
* Code and reason phrase
|
|
13
|
+
*/
|
|
14
|
+
public status: HttpResponseStatus
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Builds a response head, HTTP/1.1 by default
|
|
18
|
+
* @param status code and phrase to send
|
|
19
|
+
* @param version defaults to HTTP/1.1
|
|
20
|
+
* @param headers starts empty if left out
|
|
21
|
+
*/
|
|
22
|
+
public constructor(
|
|
23
|
+
status: HttpResponseStatus,
|
|
24
|
+
version: HttpVersion = HttpVersion.HTTP_1_1,
|
|
25
|
+
headers: HttpHeaders = new HttpHeaders(),
|
|
26
|
+
) {
|
|
27
|
+
super(version, headers)
|
|
28
|
+
|
|
29
|
+
this.status = status
|
|
30
|
+
}
|
|
31
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* An HTTP method such as GET or POST
|
|
3
|
+
* @class
|
|
4
|
+
*/
|
|
5
|
+
export class HttpMethod {
|
|
6
|
+
/**
|
|
7
|
+
* Standard methods, so of() can hand back the shared instance
|
|
8
|
+
*/
|
|
9
|
+
private static readonly known: Map<string, HttpMethod> = new Map<
|
|
10
|
+
string,
|
|
11
|
+
HttpMethod
|
|
12
|
+
>()
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* GET
|
|
16
|
+
*/
|
|
17
|
+
public static readonly GET: HttpMethod = HttpMethod.register('GET')
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* HEAD, which never has a response body
|
|
21
|
+
*/
|
|
22
|
+
public static readonly HEAD: HttpMethod = HttpMethod.register('HEAD')
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* POST
|
|
26
|
+
*/
|
|
27
|
+
public static readonly POST: HttpMethod = HttpMethod.register('POST')
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* PUT
|
|
31
|
+
*/
|
|
32
|
+
public static readonly PUT: HttpMethod = HttpMethod.register('PUT')
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* DELETE
|
|
36
|
+
*/
|
|
37
|
+
public static readonly DELETE: HttpMethod = HttpMethod.register('DELETE')
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* PATCH
|
|
41
|
+
*/
|
|
42
|
+
public static readonly PATCH: HttpMethod = HttpMethod.register('PATCH')
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* OPTIONS
|
|
46
|
+
*/
|
|
47
|
+
public static readonly OPTIONS: HttpMethod = HttpMethod.register('OPTIONS')
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* TRACE
|
|
51
|
+
*/
|
|
52
|
+
public static readonly TRACE: HttpMethod = HttpMethod.register('TRACE')
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* CONNECT, used to open a tunnel
|
|
56
|
+
*/
|
|
57
|
+
public static readonly CONNECT: HttpMethod = HttpMethod.register('CONNECT')
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Name as sent, case sensitive
|
|
61
|
+
*/
|
|
62
|
+
public readonly name: string
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Builds a method, throwing if the name isn't a valid token
|
|
66
|
+
* @param name such as GET
|
|
67
|
+
*/
|
|
68
|
+
public constructor(name: string) {
|
|
69
|
+
if (!/^[!#$%&'*+.^_`|~0-9A-Za-z-]+$/.test(name)) {
|
|
70
|
+
throw new TypeError('Method is invalid')
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
this.name = name
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Returns the shared instance for a standard method, or a new one
|
|
78
|
+
* @param name as read off the wire
|
|
79
|
+
*/
|
|
80
|
+
public static of(name: string): HttpMethod {
|
|
81
|
+
return HttpMethod.known.get(name) ?? new HttpMethod(name)
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Builds a standard method and adds it to the lookup
|
|
86
|
+
* @param name standard method name
|
|
87
|
+
*/
|
|
88
|
+
private static register(name: string): HttpMethod {
|
|
89
|
+
const method = new HttpMethod(name)
|
|
90
|
+
|
|
91
|
+
HttpMethod.known.set(name, method)
|
|
92
|
+
return method
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Name as sent
|
|
97
|
+
*/
|
|
98
|
+
public toString(): string {
|
|
99
|
+
return this.name
|
|
100
|
+
}
|
|
101
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { DuplexHandlerAdapter } from '../../../channel/handler/duplex/index.ts'
|
|
2
|
+
import type { HttpDecoderOptions } from '../decoder/object/index.ts'
|
|
3
|
+
import { HttpRequestDecoder } from '../decoder/request/index.ts'
|
|
4
|
+
import { HttpResponseEncoder } from '../encoder/response/index.ts'
|
|
5
|
+
import type { HandlerContext } from '../../../channel/context/index.ts'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Server side HTTP in one handler, requests in and responses out
|
|
9
|
+
* @class
|
|
10
|
+
*/
|
|
11
|
+
export class HttpServerCodec extends DuplexHandlerAdapter {
|
|
12
|
+
/**
|
|
13
|
+
* Handles everything inbound
|
|
14
|
+
*/
|
|
15
|
+
private readonly decoder: HttpRequestDecoder
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Handles everything outbound
|
|
19
|
+
*/
|
|
20
|
+
private readonly encoder: HttpResponseEncoder
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Sets up both halves
|
|
24
|
+
* @param options size limits for incoming requests
|
|
25
|
+
*/
|
|
26
|
+
public constructor(options: HttpDecoderOptions = {}) {
|
|
27
|
+
super()
|
|
28
|
+
|
|
29
|
+
this.decoder = new HttpRequestDecoder(options)
|
|
30
|
+
this.encoder = new HttpResponseEncoder()
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Hands inbound bytes to the request decoder
|
|
35
|
+
* @param context where requests go
|
|
36
|
+
* @param message raw bytes, usually
|
|
37
|
+
*/
|
|
38
|
+
public override channelRead(
|
|
39
|
+
context: HandlerContext,
|
|
40
|
+
message: unknown,
|
|
41
|
+
): Promise<void> {
|
|
42
|
+
return this.decoder.channelRead(context, message)
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Lets the decoder finish up when the connection drops
|
|
47
|
+
* @param context the handler's context
|
|
48
|
+
*/
|
|
49
|
+
public override channelInactive(context: HandlerContext): Promise<void> {
|
|
50
|
+
return this.decoder.channelInactive(context)
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Forwards leftover bytes, handy after an upgrade
|
|
55
|
+
* @param context where the leftovers go
|
|
56
|
+
*/
|
|
57
|
+
public handlerRemoved(context: HandlerContext): Promise<void> {
|
|
58
|
+
return this.decoder.handlerRemoved(context)
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Hands outbound responses to the encoder
|
|
63
|
+
* @param context where the bytes go
|
|
64
|
+
* @param message a response head, body chunk or full response
|
|
65
|
+
*/
|
|
66
|
+
public override write(
|
|
67
|
+
context: HandlerContext,
|
|
68
|
+
message: unknown,
|
|
69
|
+
): Promise<void> {
|
|
70
|
+
return this.encoder.write(context, message)
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Writes a full response straight into the outbound buffer when it can
|
|
75
|
+
* @param context where the write is going
|
|
76
|
+
* @param message the response
|
|
77
|
+
*/
|
|
78
|
+
public writeSync(context: HandlerContext, message: unknown): boolean {
|
|
79
|
+
return context.writesToSocket && this.encoder.stage(context, message)
|
|
80
|
+
}
|
|
81
|
+
}
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* An HTTP status code with its reason phrase
|
|
3
|
+
* @class
|
|
4
|
+
*/
|
|
5
|
+
export class HttpResponseStatus {
|
|
6
|
+
/**
|
|
7
|
+
* Default phrases for the codes we know about
|
|
8
|
+
*/
|
|
9
|
+
private static readonly phrases: Map<number, string> = new Map<
|
|
10
|
+
number,
|
|
11
|
+
string
|
|
12
|
+
>([
|
|
13
|
+
[100, 'Continue'],
|
|
14
|
+
[101, 'Switching Protocols'],
|
|
15
|
+
[200, 'OK'],
|
|
16
|
+
[201, 'Created'],
|
|
17
|
+
[202, 'Accepted'],
|
|
18
|
+
[204, 'No Content'],
|
|
19
|
+
[206, 'Partial Content'],
|
|
20
|
+
[301, 'Moved Permanently'],
|
|
21
|
+
[302, 'Found'],
|
|
22
|
+
[303, 'See Other'],
|
|
23
|
+
[304, 'Not Modified'],
|
|
24
|
+
[307, 'Temporary Redirect'],
|
|
25
|
+
[308, 'Permanent Redirect'],
|
|
26
|
+
[400, 'Bad Request'],
|
|
27
|
+
[401, 'Unauthorized'],
|
|
28
|
+
[403, 'Forbidden'],
|
|
29
|
+
[404, 'Not Found'],
|
|
30
|
+
[405, 'Method Not Allowed'],
|
|
31
|
+
[408, 'Request Timeout'],
|
|
32
|
+
[409, 'Conflict'],
|
|
33
|
+
[411, 'Length Required'],
|
|
34
|
+
[413, 'Request Entity Too Large'],
|
|
35
|
+
[414, 'Request-URI Too Long'],
|
|
36
|
+
[417, 'Expectation Failed'],
|
|
37
|
+
[426, 'Upgrade Required'],
|
|
38
|
+
[429, 'Too Many Requests'],
|
|
39
|
+
[431, 'Request Header Fields Too Large'],
|
|
40
|
+
[500, 'Internal Server Error'],
|
|
41
|
+
[501, 'Not Implemented'],
|
|
42
|
+
[502, 'Bad Gateway'],
|
|
43
|
+
[503, 'Service Unavailable'],
|
|
44
|
+
[504, 'Gateway Timeout'],
|
|
45
|
+
[505, 'HTTP Version Not Supported'],
|
|
46
|
+
])
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* 100 Continue
|
|
50
|
+
*/
|
|
51
|
+
public static readonly CONTINUE: HttpResponseStatus =
|
|
52
|
+
HttpResponseStatus.valueOf(100)
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* 101 Switching Protocols
|
|
56
|
+
*/
|
|
57
|
+
public static readonly SWITCHING_PROTOCOLS: HttpResponseStatus =
|
|
58
|
+
HttpResponseStatus.valueOf(101)
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* 200 OK
|
|
62
|
+
*/
|
|
63
|
+
public static readonly OK: HttpResponseStatus =
|
|
64
|
+
HttpResponseStatus.valueOf(200)
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* 204 No Content
|
|
68
|
+
*/
|
|
69
|
+
public static readonly NO_CONTENT: HttpResponseStatus =
|
|
70
|
+
HttpResponseStatus.valueOf(204)
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* 304 Not Modified
|
|
74
|
+
*/
|
|
75
|
+
public static readonly NOT_MODIFIED: HttpResponseStatus =
|
|
76
|
+
HttpResponseStatus.valueOf(304)
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* 400 Bad Request
|
|
80
|
+
*/
|
|
81
|
+
public static readonly BAD_REQUEST: HttpResponseStatus =
|
|
82
|
+
HttpResponseStatus.valueOf(400)
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* 403 Forbidden
|
|
86
|
+
*/
|
|
87
|
+
public static readonly FORBIDDEN: HttpResponseStatus =
|
|
88
|
+
HttpResponseStatus.valueOf(403)
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* 404 Not Found
|
|
92
|
+
*/
|
|
93
|
+
public static readonly NOT_FOUND: HttpResponseStatus =
|
|
94
|
+
HttpResponseStatus.valueOf(404)
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* 413 Request Entity Too Large
|
|
98
|
+
*/
|
|
99
|
+
public static readonly REQUEST_ENTITY_TOO_LARGE: HttpResponseStatus =
|
|
100
|
+
HttpResponseStatus.valueOf(413)
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* 426 Upgrade Required
|
|
104
|
+
*/
|
|
105
|
+
public static readonly UPGRADE_REQUIRED: HttpResponseStatus =
|
|
106
|
+
HttpResponseStatus.valueOf(426)
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* 500 Internal Server Error
|
|
110
|
+
*/
|
|
111
|
+
public static readonly INTERNAL_SERVER_ERROR: HttpResponseStatus =
|
|
112
|
+
HttpResponseStatus.valueOf(500)
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Three digit code
|
|
116
|
+
*/
|
|
117
|
+
public readonly code: number
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Text that follows the code, such as Not Found
|
|
121
|
+
*/
|
|
122
|
+
public readonly reasonPhrase: string
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Builds a status, throwing on a bad code or a phrase with line breaks
|
|
126
|
+
* @param code between 100 and 999
|
|
127
|
+
* @param reasonPhrase text after the code
|
|
128
|
+
*/
|
|
129
|
+
public constructor(code: number, reasonPhrase: string) {
|
|
130
|
+
if (!Number.isInteger(code) || code < 100 || code > 999) {
|
|
131
|
+
throw new RangeError('Status code is invalid')
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
if (/[\r\n]/.test(reasonPhrase)) {
|
|
135
|
+
throw new TypeError('Reason phrase is invalid')
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
this.code = code
|
|
139
|
+
this.reasonPhrase = reasonPhrase
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Builds a status, filling in the usual phrase when none is given
|
|
144
|
+
* @param code three digit code
|
|
145
|
+
* @param reasonPhrase overrides the default phrase
|
|
146
|
+
*/
|
|
147
|
+
public static valueOf(
|
|
148
|
+
code: number,
|
|
149
|
+
reasonPhrase?: string,
|
|
150
|
+
): HttpResponseStatus {
|
|
151
|
+
const phrase =
|
|
152
|
+
reasonPhrase ?? HttpResponseStatus.phrases.get(code) ?? 'Unknown'
|
|
153
|
+
|
|
154
|
+
return new HttpResponseStatus(code, phrase)
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* True for 1xx codes
|
|
159
|
+
*/
|
|
160
|
+
public get isInformational(): boolean {
|
|
161
|
+
return this.code >= 100 && this.code < 200
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Formatted like 404 Not Found
|
|
166
|
+
*/
|
|
167
|
+
public toString(): string {
|
|
168
|
+
return `${String(this.code)} ${this.reasonPhrase}`
|
|
169
|
+
}
|
|
170
|
+
}
|