@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.
Files changed (142) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +105 -2
  3. package/package.json +65 -4
  4. package/src/bootstrap/client/index.ts +191 -0
  5. package/src/bootstrap/dispatcher/index.ts +204 -0
  6. package/src/bootstrap/index.ts +22 -0
  7. package/src/bootstrap/reconnect/index.ts +334 -0
  8. package/src/bootstrap/server/index.ts +284 -0
  9. package/src/buffer/bits/index.ts +2 -0
  10. package/src/buffer/bits/reader/index.ts +64 -0
  11. package/src/buffer/bits/writer/index.ts +79 -0
  12. package/src/buffer/index.ts +1375 -0
  13. package/src/buffer/pool/index.ts +46 -0
  14. package/src/buffer/quantizer/index.ts +101 -0
  15. package/src/channel/attribute/index.ts +89 -0
  16. package/src/channel/context/index.ts +621 -0
  17. package/src/channel/group/index.ts +256 -0
  18. package/src/channel/handler/duplex/index.ts +105 -0
  19. package/src/channel/handler/inbound/index.ts +78 -0
  20. package/src/channel/handler/index.ts +135 -0
  21. package/src/channel/handler/outbound/index.ts +33 -0
  22. package/src/channel/index.ts +619 -0
  23. package/src/channel/initializer/index.ts +77 -0
  24. package/src/channel/outbound/index.ts +611 -0
  25. package/src/channel/pipeline/head/index.ts +100 -0
  26. package/src/channel/pipeline/index.ts +414 -0
  27. package/src/channel/pipeline/tail/index.ts +45 -0
  28. package/src/channel/pipeline/tracker/index.ts +120 -0
  29. package/src/channel/queue/index.ts +203 -0
  30. package/src/channel/resolved/index.ts +34 -0
  31. package/src/channel/server/index.ts +164 -0
  32. package/src/channel/shutdown/index.ts +25 -0
  33. package/src/channel/simple/index.ts +38 -0
  34. package/src/channel/transport/index.ts +74 -0
  35. package/src/channel/transport/socket/index.ts +80 -0
  36. package/src/cluster/index.ts +360 -0
  37. package/src/codec/binary/index.ts +102 -0
  38. package/src/codec/decoder/index.ts +479 -0
  39. package/src/codec/delimiter/index.ts +189 -0
  40. package/src/codec/encoder/index.ts +48 -0
  41. package/src/codec/error/index.ts +33 -0
  42. package/src/codec/fixed/index.ts +45 -0
  43. package/src/codec/http/aggregator/index.ts +236 -0
  44. package/src/codec/http/client/index.ts +120 -0
  45. package/src/codec/http/decoder/index.ts +4 -0
  46. package/src/codec/http/decoder/object/index.ts +644 -0
  47. package/src/codec/http/decoder/request/index.ts +49 -0
  48. package/src/codec/http/decoder/response/index.ts +59 -0
  49. package/src/codec/http/encoder/index.ts +3 -0
  50. package/src/codec/http/encoder/object/index.ts +196 -0
  51. package/src/codec/http/encoder/request/index.ts +24 -0
  52. package/src/codec/http/encoder/response/index.ts +26 -0
  53. package/src/codec/http/headers/index.ts +287 -0
  54. package/src/codec/http/index.ts +33 -0
  55. package/src/codec/http/message/content/index.ts +36 -0
  56. package/src/codec/http/message/full/index.ts +10 -0
  57. package/src/codec/http/message/full/request/index.ts +45 -0
  58. package/src/codec/http/message/full/response/index.ts +43 -0
  59. package/src/codec/http/message/head/index.ts +79 -0
  60. package/src/codec/http/message/index.ts +16 -0
  61. package/src/codec/http/message/last/index.ts +27 -0
  62. package/src/codec/http/message/request/index.ts +39 -0
  63. package/src/codec/http/message/response/index.ts +31 -0
  64. package/src/codec/http/method/index.ts +101 -0
  65. package/src/codec/http/server/index.ts +81 -0
  66. package/src/codec/http/status/index.ts +170 -0
  67. package/src/codec/http/version/index.ts +70 -0
  68. package/src/codec/index.ts +83 -0
  69. package/src/codec/length/index.ts +330 -0
  70. package/src/codec/line/index.ts +45 -0
  71. package/src/codec/message/decoder/index.ts +84 -0
  72. package/src/codec/message/encoder/index.ts +56 -0
  73. package/src/codec/message/index.ts +2 -0
  74. package/src/codec/msgpack/index.ts +51 -0
  75. package/src/codec/prepender/index.ts +296 -0
  76. package/src/codec/protobuf/index.ts +57 -0
  77. package/src/codec/string/decoder/index.ts +47 -0
  78. package/src/codec/string/encoder/index.ts +26 -0
  79. package/src/codec/string/index.ts +2 -0
  80. package/src/codec/websocket/client/index.ts +235 -0
  81. package/src/codec/websocket/decoder/index.ts +371 -0
  82. package/src/codec/websocket/encoder/index.ts +170 -0
  83. package/src/codec/websocket/error/index.ts +42 -0
  84. package/src/codec/websocket/frame/base/index.ts +55 -0
  85. package/src/codec/websocket/frame/binary/index.ts +24 -0
  86. package/src/codec/websocket/frame/close/index.ts +82 -0
  87. package/src/codec/websocket/frame/continuation/index.ts +31 -0
  88. package/src/codec/websocket/frame/index.ts +10 -0
  89. package/src/codec/websocket/frame/opcode/index.ts +35 -0
  90. package/src/codec/websocket/frame/ping/index.ts +24 -0
  91. package/src/codec/websocket/frame/pong/index.ts +24 -0
  92. package/src/codec/websocket/frame/text/index.ts +31 -0
  93. package/src/codec/websocket/handshake/index.ts +38 -0
  94. package/src/codec/websocket/index.ts +25 -0
  95. package/src/codec/websocket/key/index.ts +41 -0
  96. package/src/codec/websocket/protocol/index.ts +147 -0
  97. package/src/codec/websocket/server/index.ts +233 -0
  98. package/src/codec/websocket/status/index.ts +97 -0
  99. package/src/codec/websocket/upgrade/index.ts +116 -0
  100. package/src/datagram/bootstrap/index.ts +269 -0
  101. package/src/datagram/channel/index.ts +103 -0
  102. package/src/datagram/index.ts +10 -0
  103. package/src/datagram/packet/index.ts +72 -0
  104. package/src/datagram/transport/index.ts +239 -0
  105. package/src/handler/flush/index.ts +187 -0
  106. package/src/handler/idle/index.ts +295 -0
  107. package/src/handler/index.ts +22 -0
  108. package/src/handler/logging/index.ts +195 -0
  109. package/src/handler/metrics/index.ts +300 -0
  110. package/src/handler/offload/index.ts +141 -0
  111. package/src/handler/timeout/error/index.ts +29 -0
  112. package/src/handler/timeout/index.ts +3 -0
  113. package/src/handler/timeout/read/index.ts +44 -0
  114. package/src/handler/timeout/write/index.ts +89 -0
  115. package/src/handler/traffic/index.ts +259 -0
  116. package/src/index.ts +258 -0
  117. package/src/logger/index.ts +47 -0
  118. package/src/reliable/bootstrap/index.ts +154 -0
  119. package/src/reliable/cipher/index.ts +227 -0
  120. package/src/reliable/cookie/index.ts +78 -0
  121. package/src/reliable/handler/index.ts +1202 -0
  122. package/src/reliable/handshake/index.ts +144 -0
  123. package/src/reliable/index.ts +38 -0
  124. package/src/reliable/lane/index.ts +420 -0
  125. package/src/reliable/message/index.ts +102 -0
  126. package/src/reliable/packet/index.ts +117 -0
  127. package/src/reliable/peer/index.ts +929 -0
  128. package/src/reliable/registry/index.ts +107 -0
  129. package/src/serve/bootstrap/index.ts +606 -0
  130. package/src/serve/channel/index.ts +246 -0
  131. package/src/serve/index.ts +17 -0
  132. package/src/serve/outbox/index.ts +292 -0
  133. package/src/serve/transport/index.ts +96 -0
  134. package/src/serve/writer/index.ts +119 -0
  135. package/src/shard/envelope/index.ts +44 -0
  136. package/src/shard/index.ts +10 -0
  137. package/src/shard/link/index.ts +743 -0
  138. package/src/shard/map/index.ts +91 -0
  139. package/src/worker/group/index.ts +175 -0
  140. package/src/worker/index.ts +10 -0
  141. package/src/worker/pool/index.ts +235 -0
  142. package/src/worker/service/index.ts +55 -0
@@ -0,0 +1,59 @@
1
+ import { DecoderError } from '../../../error/index.ts'
2
+ import type { HttpMessage } from '../../message/head/index.ts'
3
+ import { HttpResponse } from '../../message/response/index.ts'
4
+ import { HttpResponseStatus } from '../../status/index.ts'
5
+ import { HttpVersion } from '../../version/index.ts'
6
+ import { HttpObjectDecoder } from '../object/index.ts'
7
+
8
+ /**
9
+ * Client side decoder that reads HTTP/1.x responses
10
+ * @class
11
+ */
12
+ export class HttpResponseDecoder extends HttpObjectDecoder {
13
+ /**
14
+ * Version, three digit code and an optional reason
15
+ */
16
+ private static readonly pattern: RegExp =
17
+ /^(HTTP\/\d\.\d)[ \t]+(\d{3})(?:[ \t]+(.*))?$/
18
+
19
+ /**
20
+ * Builds a response head, refusing anything but HTTP/1.x
21
+ * @param line first line of the response
22
+ */
23
+ protected createMessage(line: string): HttpResponse {
24
+ const match = HttpResponseDecoder.pattern.exec(line.trimStart())
25
+ const version = HttpVersion.parse(match?.[1] ?? '')
26
+ const code = match?.[2]
27
+ if (match === null || code === undefined) {
28
+ throw new DecoderError('Status line is invalid')
29
+ }
30
+
31
+ if (version?.major !== 1) throw new DecoderError('Version is unsupported')
32
+
33
+ const reason = (match[3] ?? '').trim()
34
+ const status = HttpResponseStatus.valueOf(
35
+ Number(code),
36
+ reason === '' ? undefined : reason,
37
+ )
38
+ return new HttpResponse(status, version)
39
+ }
40
+
41
+ /**
42
+ * 1xx, 204 and 304 replies never carry a body
43
+ * @param message head that has come in
44
+ */
45
+ protected isContentAlwaysEmpty(message: HttpMessage): boolean {
46
+ if (!(message instanceof HttpResponse)) return false
47
+
48
+ const code = message.status.code
49
+ return (code >= 100 && code < 200) || code === 204 || code === 304
50
+ }
51
+
52
+ /**
53
+ * True for 101 Switching Protocols
54
+ * @param message head that has come in
55
+ */
56
+ protected isSwitchingProtocols(message: HttpMessage): boolean {
57
+ return message instanceof HttpResponse && message.status.code === 101
58
+ }
59
+ }
@@ -0,0 +1,3 @@
1
+ export { HttpObjectEncoder } from './object/index.ts'
2
+ export { HttpRequestEncoder } from './request/index.ts'
3
+ export { HttpResponseEncoder } from './response/index.ts'
@@ -0,0 +1,196 @@
1
+ import { ByteBuf } from '../../../../buffer/index.ts'
2
+ import type { HandlerContext } from '../../../../channel/context/index.ts'
3
+ import { MessageToByteEncoder } from '../../../encoder/index.ts'
4
+ import { EncoderError } from '../../../error/index.ts'
5
+ import type { HttpHeaders } from '../../headers/index.ts'
6
+ import { HttpContent } from '../../message/content/index.ts'
7
+ import { FullHttpRequest } from '../../message/full/request/index.ts'
8
+ import { FullHttpResponse } from '../../message/full/response/index.ts'
9
+ import type { HttpMessage } from '../../message/head/index.ts'
10
+ import { LastHttpContent } from '../../message/last/index.ts'
11
+
12
+ /**
13
+ * Whether a head has gone out and how its body is framed
14
+ */
15
+ type EncoderState = 'initial' | 'plain' | 'chunked'
16
+
17
+ /**
18
+ * Shared encoder for HTTP heads, body chunks and full messages
19
+ * @class
20
+ */
21
+ export abstract class HttpObjectEncoder<
22
+ M extends HttpMessage,
23
+ > extends MessageToByteEncoder<M | HttpContent> {
24
+ /**
25
+ * Tracks whether we're mid message
26
+ */
27
+ private state: EncoderState
28
+
29
+ /**
30
+ * Starts out waiting for a head
31
+ */
32
+ public constructor() {
33
+ super()
34
+
35
+ this.state = 'initial'
36
+ }
37
+
38
+ /**
39
+ * Request or response head, depending on the subclass
40
+ * @param message the outbound message
41
+ */
42
+ protected abstract acceptsMessage(message: unknown): message is M
43
+
44
+ /**
45
+ * First line of the message, without the CRLF
46
+ * @param message head being written
47
+ */
48
+ protected abstract initialLine(message: M): string
49
+
50
+ /**
51
+ * Takes body chunks plus whatever heads the subclass handles
52
+ * @param message the outbound message
53
+ */
54
+ protected accepts(message: unknown): message is M | HttpContent {
55
+ return message instanceof HttpContent || this.acceptsMessage(message)
56
+ }
57
+
58
+ /**
59
+ * Writes a head, a body chunk or a whole message, refusing a new head mid message
60
+ * @param context unused here
61
+ * @param message head, chunk or full message
62
+ */
63
+ protected encode(context: HandlerContext, message: M | HttpContent): ByteBuf {
64
+ const out = ByteBuf.allocate()
65
+
66
+ if (message instanceof HttpContent) {
67
+ this.encodeContent(out, message.content, message)
68
+ return out
69
+ }
70
+
71
+ if (this.state !== 'initial')
72
+ throw new EncoderError('Message is unfinished')
73
+
74
+ out.writeString(`${this.initialLine(message)}\r\n`)
75
+ this.writeHeaders(out, message.headers)
76
+ this.state = message.isChunked() ? 'chunked' : 'plain'
77
+ if (
78
+ message instanceof FullHttpRequest ||
79
+ message instanceof FullHttpResponse
80
+ ) {
81
+ this.encodeContent(out, message.content, message)
82
+ }
83
+ return out
84
+ }
85
+
86
+ /**
87
+ * Writes the body raw or as chunks, ending the message when trailers come along
88
+ * @param out buffer being written to
89
+ * @param content body bytes to write
90
+ * @param source where the bytes came from, used to find trailers
91
+ */
92
+ private encodeContent(
93
+ out: ByteBuf,
94
+ content: ByteBuf,
95
+ source: HttpContent | FullHttpRequest | FullHttpResponse,
96
+ ): void {
97
+ const bytes = content.toUint8Array()
98
+ const trailers = HttpObjectEncoder.trailersOf(source)
99
+ if (this.state === 'initial')
100
+ throw new EncoderError('Content is unexpected')
101
+
102
+ if (this.state === 'plain') {
103
+ out.writeBytes(bytes)
104
+ } else {
105
+ if (bytes.length > 0) {
106
+ out.writeString(`${bytes.length.toString(16)}\r\n`)
107
+ out.writeBytes(bytes)
108
+ out.writeString('\r\n')
109
+ }
110
+
111
+ if (trailers !== null) {
112
+ out.writeString('0\r\n')
113
+ this.writeHeaders(out, trailers)
114
+ }
115
+ }
116
+
117
+ if (trailers !== null) this.state = 'initial'
118
+ }
119
+
120
+ /**
121
+ * Writes a whole message straight into the outbound buffer, false when it can't
122
+ * @param context where the write is going
123
+ * @param message the message
124
+ */
125
+ public stage(context: HandlerContext, message: unknown): boolean {
126
+ if (this.state !== 'initial') return false
127
+ if (!(
128
+ message instanceof FullHttpRequest || message instanceof FullHttpResponse
129
+ )) {
130
+ return false
131
+ }
132
+ if (!this.acceptsMessage(message) || message.isChunked()) return false
133
+
134
+ let head = `${this.initialLine(message)}\r\n`
135
+ for (const [name, value] of message.headers) head += `${name}: ${value}\r\n`
136
+ head += '\r\n'
137
+ if (!HttpObjectEncoder.isAscii(head)) return false
138
+
139
+ const body = message.content
140
+ const channel = context.channel
141
+ const offset = channel.reserve(head.length + body.readableBytes)
142
+ if (offset < 0) return false
143
+
144
+ const target = channel.stagingBuffer
145
+ for (let index = 0; index < head.length; index++) {
146
+ target[offset + index] = head.charCodeAt(index)
147
+ }
148
+
149
+ ByteBuf.copyBytes(
150
+ body.array,
151
+ body.arrayOffset + body.readerIndex,
152
+ target,
153
+ offset + head.length,
154
+ body.readableBytes,
155
+ )
156
+ channel.commitStaged()
157
+ return true
158
+ }
159
+
160
+ /**
161
+ * Whether every character fits in one byte of ASCII
162
+ * @param text the text
163
+ */
164
+ private static isAscii(text: string): boolean {
165
+ for (let index = 0; index < text.length; index++) {
166
+ if (text.charCodeAt(index) > 0x7f) return false
167
+ }
168
+
169
+ return true
170
+ }
171
+
172
+ /**
173
+ * Writes each header line and the blank line after them
174
+ * @param out buffer being written to
175
+ * @param headers headers or trailers
176
+ */
177
+ private writeHeaders(out: ByteBuf, headers: HttpHeaders): void {
178
+ let text = ''
179
+
180
+ for (const [name, value] of headers) text += `${name}: ${value}\r\n`
181
+ out.writeString(`${text}\r\n`)
182
+ }
183
+
184
+ /**
185
+ * Trailers if this ends the message, otherwise null
186
+ * @param source chunk or full message
187
+ */
188
+ private static trailersOf(
189
+ source: HttpContent | FullHttpRequest | FullHttpResponse,
190
+ ): HttpHeaders | null {
191
+ if (source instanceof LastHttpContent) return source.trailers
192
+ if (source instanceof HttpContent) return null
193
+
194
+ return source.trailers
195
+ }
196
+ }
@@ -0,0 +1,24 @@
1
+ import { HttpRequest } from '../../message/request/index.ts'
2
+ import { HttpObjectEncoder } from '../object/index.ts'
3
+
4
+ /**
5
+ * Client side encoder for HTTP requests
6
+ * @class
7
+ */
8
+ export class HttpRequestEncoder extends HttpObjectEncoder<HttpRequest> {
9
+ /**
10
+ * Takes request heads
11
+ * @param message the outbound message
12
+ */
13
+ protected acceptsMessage(message: unknown): message is HttpRequest {
14
+ return message instanceof HttpRequest
15
+ }
16
+
17
+ /**
18
+ * Method, target and version, such as GET / HTTP/1.1
19
+ * @param message head being written
20
+ */
21
+ protected initialLine(message: HttpRequest): string {
22
+ return `${message.method.name} ${message.uri} ${message.version.text}`
23
+ }
24
+ }
@@ -0,0 +1,26 @@
1
+ import { HttpResponse } from '../../message/response/index.ts'
2
+ import { HttpObjectEncoder } from '../object/index.ts'
3
+
4
+ /**
5
+ * Server side encoder for HTTP responses
6
+ * @class
7
+ */
8
+ export class HttpResponseEncoder extends HttpObjectEncoder<HttpResponse> {
9
+ /**
10
+ * Takes response heads
11
+ * @param message the outbound message
12
+ */
13
+ protected acceptsMessage(message: unknown): message is HttpResponse {
14
+ return message instanceof HttpResponse
15
+ }
16
+
17
+ /**
18
+ * Version, code and reason, such as HTTP/1.1 200 OK
19
+ * @param message head being written
20
+ */
21
+ protected initialLine(message: HttpResponse): string {
22
+ const { status, version } = message
23
+
24
+ return `${version.text} ${String(status.code)} ${status.reasonPhrase}`
25
+ }
26
+ }
@@ -0,0 +1,287 @@
1
+ /**
2
+ * One header as a name and value pair
3
+ */
4
+ export type HeaderEntry = readonly [name: string, value: string]
5
+
6
+ /**
7
+ * Numbers are fine too, they get turned into strings
8
+ */
9
+ export type HeaderValue = string | number
10
+
11
+ /**
12
+ * One header as kept internally
13
+ */
14
+ type StoredHeader = {
15
+ /**
16
+ * Lowercased name used for lookups
17
+ */
18
+ readonly key: string
19
+
20
+ /**
21
+ * Original spelling, kept for writing out
22
+ */
23
+ readonly name: string
24
+
25
+ /**
26
+ * Value as a string
27
+ */
28
+ readonly value: string
29
+ }
30
+
31
+ /**
32
+ * HTTP headers that keep their order, ignore case and allow repeats
33
+ * @class
34
+ */
35
+ export class HttpHeaders implements Iterable<HeaderEntry> {
36
+ /**
37
+ * RFC 9110 token characters
38
+ */
39
+ private static readonly token: RegExp = /^[!#$%&'*+.^_`|~0-9A-Za-z-]+$/
40
+
41
+ /**
42
+ * CR, LF and NUL, which would let someone inject headers
43
+ */
44
+ private static readonly forbidden: RegExp = /[\r\n\0]/
45
+
46
+ /**
47
+ * Every header in the order it was added
48
+ */
49
+ private readonly list: StoredHeader[]
50
+
51
+ /**
52
+ * Starts with the given entries, checking each one
53
+ * @param entries headers to start with
54
+ */
55
+ public constructor(entries: Iterable<HeaderEntry> = []) {
56
+ this.list = []
57
+
58
+ for (const [name, value] of entries) this.add(name, value)
59
+ }
60
+
61
+ /**
62
+ * True if the name is a valid HTTP token
63
+ * @param name name to check
64
+ */
65
+ public static isValidName(name: string): boolean {
66
+ return HttpHeaders.token.test(name)
67
+ }
68
+
69
+ /**
70
+ * True unless the value has CR, LF or NUL in it
71
+ * @param value value to check
72
+ */
73
+ public static isValidValue(value: string): boolean {
74
+ return !HttpHeaders.forbidden.test(value)
75
+ }
76
+
77
+ /**
78
+ * How many entries there are, repeats included
79
+ */
80
+ public get size(): number {
81
+ return this.list.length
82
+ }
83
+
84
+ /**
85
+ * First value for this name, or null
86
+ * @param name any case works
87
+ */
88
+ public get(name: string): string | null {
89
+ const key = name.toLowerCase()
90
+
91
+ for (const header of this.list) {
92
+ if (header.key === key) return header.value
93
+ }
94
+
95
+ return null
96
+ }
97
+
98
+ /**
99
+ * All values for this name, in order
100
+ * @param name any case works
101
+ */
102
+ public getAll(name: string): string[] {
103
+ const key = name.toLowerCase()
104
+ const values: string[] = []
105
+
106
+ for (const header of this.list) {
107
+ if (header.key === key) values.push(header.value)
108
+ }
109
+
110
+ return values
111
+ }
112
+
113
+ /**
114
+ * True if the name appears at least once
115
+ * @param name any case works
116
+ */
117
+ public has(name: string): boolean {
118
+ return this.get(name) !== null
119
+ }
120
+
121
+ /**
122
+ * Looks for a token in a list header like Connection, ignoring case
123
+ * @param name header to search
124
+ * @param token what to look for, such as close
125
+ */
126
+ public hasToken(name: string, token: string): boolean {
127
+ const key = name.toLowerCase()
128
+ const wanted = token.toLowerCase()
129
+
130
+ for (const header of this.list) {
131
+ if (header.key !== key) continue
132
+ const value = header.value
133
+ if (value.length === wanted.length) {
134
+ if (value.toLowerCase() === wanted) return true
135
+ continue
136
+ }
137
+
138
+ if (HttpHeaders.listHas(value, wanted)) return true
139
+ }
140
+
141
+ return false
142
+ }
143
+
144
+ /**
145
+ * Whether a comma separated value holds a token, ignoring case and spaces
146
+ * @param value the header value
147
+ * @param wanted a lowercase token
148
+ */
149
+ private static listHas(value: string, wanted: string): boolean {
150
+ let start = 0
151
+
152
+ while (start <= value.length) {
153
+ let end = value.indexOf(',', start)
154
+ if (end === -1) end = value.length
155
+ if (value.slice(start, end).trim().toLowerCase() === wanted) return true
156
+ start = end + 1
157
+ }
158
+
159
+ return false
160
+ }
161
+
162
+ /**
163
+ * Splits every value on commas and lowercases the pieces
164
+ * @param name header to split
165
+ */
166
+ public tokens(name: string): string[] {
167
+ const tokens: string[] = []
168
+
169
+ for (const value of this.getAll(name)) {
170
+ for (const part of value.split(',')) {
171
+ const token = part.trim().toLowerCase()
172
+ if (token !== '') tokens.push(token)
173
+ }
174
+ }
175
+
176
+ return tokens
177
+ }
178
+
179
+ /**
180
+ * Adds a value after checking it, keeping any existing ones
181
+ * @param name header name
182
+ * @param value string or number
183
+ */
184
+ public add(name: string, value: HeaderValue): this {
185
+ const text = String(value)
186
+ const reason = HttpHeaders.refusal(name, text)
187
+ if (reason !== null) throw new TypeError(reason)
188
+
189
+ this.list.push({ key: name.toLowerCase(), name, value: text })
190
+ return this
191
+ }
192
+
193
+ /**
194
+ * Skips validation, for the decoder which has checked already
195
+ * @param name header name
196
+ * @param value header value
197
+ */
198
+ public append(name: string, value: string): this {
199
+ this.list.push({ key: name.toLowerCase(), name, value })
200
+ return this
201
+ }
202
+
203
+ /**
204
+ * Drops existing values for the name and adds the new ones
205
+ * @param name header name
206
+ * @param value one value or a list of them
207
+ */
208
+ public set(name: string, value: HeaderValue | readonly HeaderValue[]): this {
209
+ const values: readonly HeaderValue[] =
210
+ typeof value === 'object' ? value : [value]
211
+
212
+ this.remove(name)
213
+ for (const item of values) this.add(name, item)
214
+ return this
215
+ }
216
+
217
+ /**
218
+ * Drops every entry with this name
219
+ * @param name any case works
220
+ */
221
+ public remove(name: string): this {
222
+ const key = name.toLowerCase()
223
+
224
+ for (let index = this.list.length - 1; index >= 0; index--) {
225
+ if (this.list[index]?.key === key) this.list.splice(index, 1)
226
+ }
227
+
228
+ return this
229
+ }
230
+
231
+ /**
232
+ * Empties the lot
233
+ */
234
+ public clear(): this {
235
+ this.list.length = 0
236
+ return this
237
+ }
238
+
239
+ /**
240
+ * Each name once, spelt as it first appeared
241
+ */
242
+ public names(): string[] {
243
+ const seen = new Map<string, string>()
244
+
245
+ for (const header of this.list) {
246
+ if (!seen.has(header.key)) seen.set(header.key, header.name)
247
+ }
248
+
249
+ return [...seen.values()]
250
+ }
251
+
252
+ /**
253
+ * Every name and value pair, repeats included
254
+ */
255
+ public entries(): HeaderEntry[] {
256
+ return this.list.map((header: StoredHeader): HeaderEntry => [
257
+ header.name,
258
+ header.value,
259
+ ])
260
+ }
261
+
262
+ /**
263
+ * A separate copy that can be changed freely
264
+ */
265
+ public copy(): HttpHeaders {
266
+ return new HttpHeaders(this.entries())
267
+ }
268
+
269
+ /**
270
+ * Lets you loop over the headers with for of
271
+ */
272
+ public [Symbol.iterator](): Iterator<HeaderEntry> {
273
+ return this.entries()[Symbol.iterator]()
274
+ }
275
+
276
+ /**
277
+ * Reports a bad name or value
278
+ * @param name name to check
279
+ * @param value value to check
280
+ */
281
+ private static refusal(name: string, value: string): string | null {
282
+ if (!HttpHeaders.isValidName(name)) return 'Header name is invalid'
283
+ if (!HttpHeaders.isValidValue(value)) return 'Header value is invalid'
284
+
285
+ return null
286
+ }
287
+ }
@@ -0,0 +1,33 @@
1
+ export { HttpObjectAggregator } from './aggregator/index.ts'
2
+ export { HttpClientCodec } from './client/index.ts'
3
+ export {
4
+ HttpObjectDecoder,
5
+ HttpRequestDecoder,
6
+ HttpResponseDecoder,
7
+ } from './decoder/index.ts'
8
+ export type { HttpDecoderOptions } from './decoder/index.ts'
9
+ export {
10
+ HttpObjectEncoder,
11
+ HttpRequestEncoder,
12
+ HttpResponseEncoder,
13
+ } from './encoder/index.ts'
14
+ export { HttpHeaders } from './headers/index.ts'
15
+ export type { HeaderEntry, HeaderValue } from './headers/index.ts'
16
+ export {
17
+ FullHttpRequest,
18
+ FullHttpResponse,
19
+ HttpContent,
20
+ HttpMessage,
21
+ HttpRequest,
22
+ HttpResponse,
23
+ LastHttpContent,
24
+ } from './message/index.ts'
25
+ export type {
26
+ ContentSource,
27
+ FullHttpMessage,
28
+ HttpObject,
29
+ } from './message/index.ts'
30
+ export { HttpMethod } from './method/index.ts'
31
+ export { HttpServerCodec } from './server/index.ts'
32
+ export { HttpResponseStatus } from './status/index.ts'
33
+ export { HttpVersion } from './version/index.ts'
@@ -0,0 +1,36 @@
1
+ import { ByteBuf } from '../../../../buffer/index.ts'
2
+
3
+ /**
4
+ * Anything we can turn into body bytes, strings go in as UTF-8
5
+ */
6
+ export type ContentSource = ByteBuf | Uint8Array | string
7
+
8
+ /**
9
+ * One chunk of an HTTP body
10
+ * @class
11
+ */
12
+ export class HttpContent {
13
+ /**
14
+ * Bytes in this chunk
15
+ */
16
+ public readonly content: ByteBuf
17
+
18
+ /**
19
+ * Wraps bytes or text as a body chunk, empty by default
20
+ * @param content bytes or text
21
+ */
22
+ public constructor(content: ContentSource = new Uint8Array(0)) {
23
+ this.content = HttpContent.bufferOf(content)
24
+ }
25
+
26
+ /**
27
+ * Turns bytes or text into a ByteBuf, wrapping rather than copying arrays
28
+ * @param source what to convert
29
+ */
30
+ public static bufferOf(source: ContentSource): ByteBuf {
31
+ if (source instanceof ByteBuf) return source
32
+ if (typeof source === 'string') return ByteBuf.fromString(source)
33
+
34
+ return ByteBuf.wrap(source)
35
+ }
36
+ }
@@ -0,0 +1,10 @@
1
+ import type { FullHttpResponse } from './response/index.ts'
2
+ import type { FullHttpRequest } from './request/index.ts'
3
+
4
+ export { FullHttpRequest } from './request/index.ts'
5
+ export { FullHttpResponse } from './response/index.ts'
6
+
7
+ /**
8
+ * A request or response that carries its entire body
9
+ */
10
+ export type FullHttpMessage = FullHttpRequest | FullHttpResponse