bare-http1 4.0.2 → 4.0.3

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/index.d.ts ADDED
@@ -0,0 +1,286 @@
1
+ import { Readable, ReadableEvents, Writable, WritableEvents } from 'bare-stream'
2
+ import {
3
+ TCPSocket,
4
+ TCPSocketOptions,
5
+ TCPSocketConnectOptions,
6
+ TCPServer,
7
+ TCPServerEvents
8
+ } from 'bare-tcp'
9
+ import Buffer from 'bare-buffer'
10
+ import URL from 'bare-url'
11
+ import constants, {
12
+ HTTPMethod,
13
+ HTTPStatusCode,
14
+ HTTPStatusMessage
15
+ } from './lib/constants'
16
+ import HTTPError from './lib/errors'
17
+
18
+ export {
19
+ constants,
20
+ type HTTPMethod,
21
+ type HTTPStatusCode,
22
+ type HTTPStatusMessage,
23
+ type HTTPError,
24
+ HTTPError as errors
25
+ }
26
+
27
+ export const STATUS_CODES: typeof constants.status
28
+
29
+ export interface HTTPIncomingMessageEvents extends ReadableEvents {
30
+ timeout: []
31
+ }
32
+
33
+ export interface HTTPIncomingMessageOptions {
34
+ method?: HTTPMethod
35
+ url?: URL | string
36
+ statusCode?: HTTPStatusCode
37
+ statusMessage?: HTTPStatusMessage
38
+ }
39
+
40
+ export interface HTTPIncomingMessage<
41
+ M extends HTTPIncomingMessageEvents = HTTPIncomingMessageEvents
42
+ > extends Readable<M> {
43
+ readonly socket: TCPSocket
44
+ readonly headers: Record<string, string | number>
45
+ readonly upgrade: boolean
46
+
47
+ readonly method: HTTPMethod
48
+ readonly url: URL | string
49
+
50
+ readonly statusCode: HTTPStatusCode
51
+ readonly statusMessage: HTTPStatusMessage
52
+
53
+ readonly httpVersion: '1.1'
54
+
55
+ getHeader(name: string): string | number | undefined
56
+ getHeaders(): Record<string, string | number>
57
+ hasHeader(name: string): boolean
58
+
59
+ setTimeout(ms: number, ontimeout?: () => void): this
60
+ }
61
+
62
+ export class HTTPIncomingMessage {
63
+ constructor(
64
+ socket?: TCPSocket,
65
+ headers?: Record<string, string | number>,
66
+ opts?: HTTPIncomingMessageOptions
67
+ )
68
+ }
69
+
70
+ export { HTTPIncomingMessage as IncomingMessage }
71
+
72
+ export interface HTTPOutgoingMessageEvents extends WritableEvents {
73
+ timeout: []
74
+ }
75
+
76
+ export interface HTTPOutgoingMessage<
77
+ M extends HTTPOutgoingMessageEvents = HTTPOutgoingMessageEvents
78
+ > extends Writable<M> {
79
+ readonly socket: TCPSocket
80
+ readonly headers: Record<string, string | number>
81
+ readonly headersSent: boolean
82
+ readonly upgrade: boolean
83
+
84
+ getHeader(name: string): string | number | undefined
85
+ getHeaders(): Record<string, string | number>
86
+ hasHeader(name: string): boolean
87
+ setHeader(name: string, value: string | number): void
88
+ flushHeaders(): void
89
+
90
+ setTimeout(ms: number, ontimeout?: () => void): this
91
+ }
92
+
93
+ export class HTTPOutgoingMessage {
94
+ constructor(socket?: TCPSocket)
95
+ }
96
+
97
+ export { HTTPOutgoingMessage as OutgoingMessage }
98
+
99
+ export interface HTTPAgentOptions {
100
+ keepAlive?: boolean
101
+ keepAliveMsecs?: number
102
+ }
103
+
104
+ export interface HTTPAgent {
105
+ createConnection(opts?: TCPSocketOptions & TCPSocketConnectOptions): TCPSocket
106
+
107
+ reuseSocket(socket: TCPSocket, req?: HTTPClientRequest): void
108
+
109
+ keepSocketAlive(socket: TCPSocket): boolean
110
+
111
+ getName(opts: { host: string; port: number }): string
112
+
113
+ addRequest(
114
+ req: HTTPClientRequest,
115
+ opts: TCPSocketOptions & TCPSocketConnectOptions
116
+ ): void
117
+
118
+ destroy(): void
119
+ }
120
+
121
+ export class HTTPAgent {
122
+ constructor(
123
+ opts?: HTTPAgentOptions & TCPSocketOptions & TCPSocketConnectOptions
124
+ )
125
+
126
+ static global: HTTPAgent
127
+ }
128
+
129
+ export const globalAgent: HTTPAgent
130
+
131
+ export { HTTPAgent as Agent }
132
+
133
+ export interface HTTPServerEvents extends TCPServerEvents {
134
+ request: [req: HTTPIncomingMessage, res: HTTPServerResponse]
135
+ upgrade: [req: HTTPIncomingMessage, socket: TCPSocket, head: Buffer]
136
+ timeout: [socket: TCPSocket]
137
+ }
138
+
139
+ export interface HTTPServer<M extends HTTPServerEvents = HTTPServerEvents>
140
+ extends TCPServer<M> {
141
+ readonly timeout: number | undefined
142
+
143
+ setTimeout(ms: number, ontimeout?: () => void): this
144
+ }
145
+
146
+ export class HTTPServer {
147
+ constructor(
148
+ opts?: HTTPServerConnectionOptions,
149
+ onrequest?: (req: HTTPIncomingMessage, res: HTTPServerResponse) => void
150
+ )
151
+
152
+ constructor(
153
+ onrequest: (req: HTTPIncomingMessage, res: HTTPServerResponse) => void
154
+ )
155
+ }
156
+
157
+ export { HTTPServer as Server }
158
+
159
+ export interface HTTPServerResponse extends HTTPOutgoingMessage {
160
+ readonly req: HTTPIncomingMessage
161
+ readonly statusCode: HTTPStatusCode
162
+ readonly statusMessage: HTTPStatusMessage | null
163
+
164
+ writeHead(
165
+ statusCode: HTTPStatusCode,
166
+ statusMessage?: HTTPStatusMessage,
167
+ headers?: Record<string, string | number>
168
+ ): void
169
+
170
+ writeHead(
171
+ statusCode: HTTPStatusCode,
172
+ headers?: Record<string, string | number>
173
+ ): void
174
+ }
175
+
176
+ export class HTTPServerResponse {
177
+ constructor(socket: TCPSocket, req: HTTPIncomingMessage, close: boolean)
178
+ }
179
+
180
+ export { HTTPServerResponse as ServerResponse }
181
+
182
+ export interface HTTPServerConnectionOptions {
183
+ IncomingMessage?: typeof HTTPIncomingMessage
184
+ ServerResponse?: typeof HTTPServerResponse
185
+ }
186
+
187
+ export interface HTTPServerConnection {
188
+ readonly server: HTTPServer
189
+ readonly socket: TCPSocket
190
+
191
+ readonly req: HTTPIncomingMessage | null
192
+ readonly res: HTTPServerResponse | null
193
+
194
+ readonly idle: boolean
195
+ }
196
+
197
+ export class HTTPServerConnection {
198
+ constructor(
199
+ server: HTTPServer,
200
+ socket: TCPSocket,
201
+ opts?: HTTPServerConnectionOptions
202
+ )
203
+
204
+ static for(socket: TCPSocket): HTTPServerConnection
205
+ }
206
+
207
+ export { HTTPServerConnection as ServerConnection }
208
+
209
+ export interface HTTPClientRequestEvents extends HTTPOutgoingMessageEvents {
210
+ response: [res: HTTPIncomingMessage]
211
+ upgrade: [res: HTTPIncomingMessage, socket: TCPSocket, head: Buffer]
212
+ }
213
+
214
+ export interface HTTPClientRequestOptions extends TCPSocketConnectOptions {
215
+ agent?: HTTPAgent | false
216
+ headers?: Record<string, string | number>
217
+ method?: HTTPMethod
218
+ path?: string
219
+ }
220
+
221
+ export interface HTTPClientRequest<
222
+ M extends HTTPClientRequestEvents = HTTPClientRequestEvents
223
+ > extends HTTPOutgoingMessage<M> {
224
+ readonly method: HTTPMethod
225
+ readonly path: string
226
+ readonly headers: Record<string, string | number>
227
+ }
228
+
229
+ export class HTTPClientRequest {
230
+ constructor(opts?: HTTPClientRequestOptions, onresponse?: () => void)
231
+
232
+ constructor(onresponse: () => void)
233
+ }
234
+
235
+ export { HTTPClientRequest as ClientRequest }
236
+
237
+ export interface HTTPClientConnectionOptions {
238
+ IncomingMessage?: typeof HTTPIncomingMessage
239
+ }
240
+
241
+ export interface HTTPClientConnection {
242
+ readonly socket: TCPSocket
243
+
244
+ readonly req: HTTPClientRequest | null
245
+ readonly res: HTTPIncomingMessage | null
246
+
247
+ readonly idle: boolean
248
+ }
249
+
250
+ export class HTTPClientConnection {
251
+ constructor(socket: TCPSocket, opts?: HTTPClientConnectionOptions)
252
+
253
+ static for(socket: TCPSocket): HTTPClientConnection | null
254
+
255
+ static from(
256
+ socket: TCPSocket,
257
+ opts?: HTTPClientConnectionOptions
258
+ ): HTTPClientConnection
259
+ }
260
+
261
+ export { HTTPClientConnection as ClientConnection }
262
+
263
+ export function createServer(
264
+ opts?: HTTPServerConnectionOptions,
265
+ onrequest?: (req: HTTPIncomingMessage, res: HTTPServerResponse) => void
266
+ ): HTTPServer
267
+
268
+ export function createServer(
269
+ onrequest: (req: HTTPIncomingMessage, res: HTTPServerResponse) => void
270
+ ): HTTPServer
271
+
272
+ export function request(
273
+ url: URL | string,
274
+ opts?: HTTPClientRequestOptions,
275
+ onresponse?: (res: HTTPIncomingMessage) => void
276
+ ): HTTPClientRequest
277
+
278
+ export function request(
279
+ url: URL | string,
280
+ onresponse: (res: HTTPIncomingMessage) => void
281
+ ): HTTPClientRequest
282
+
283
+ export function request(
284
+ opts: HTTPClientRequestOptions,
285
+ onresponse?: (res: HTTPIncomingMessage) => void
286
+ ): HTTPClientRequest
package/index.js CHANGED
@@ -12,6 +12,7 @@ exports.ClientRequest = require('./lib/client-request')
12
12
  exports.ClientConnection = require('./lib/client-connection')
13
13
 
14
14
  exports.constants = require('./lib/constants')
15
+ exports.errors = require('./lib/errors')
15
16
 
16
17
  exports.STATUS_CODES = exports.constants.status // For Node.js compatibility
17
18
 
package/lib/agent.js CHANGED
@@ -1,9 +1,13 @@
1
1
  const tcp = require('bare-tcp')
2
2
  const HTTPClientConnection = require('./client-connection')
3
+ const EventEmitter = require('bare-events')
3
4
 
4
- module.exports = class HTTPAgent {
5
+ module.exports = class HTTPAgent extends EventEmitter {
5
6
  constructor(opts = {}) {
7
+ super()
8
+
6
9
  const { keepAlive = false, keepAliveMsecs = 1000 } = opts
10
+ this.options = { keepAlive, keepAliveMsecs }
7
11
 
8
12
  this._sockets = new Map()
9
13
  this._freeSockets = new Map()
@@ -99,6 +103,8 @@ module.exports = class HTTPAgent {
99
103
  } else {
100
104
  socket.end()
101
105
  }
106
+
107
+ this.emit('free', socket)
102
108
  }
103
109
 
104
110
  _onremove(socket, name, all = true) {
@@ -0,0 +1,93 @@
1
+ declare const constants: {
2
+ state: {
3
+ BEFORE_HEAD: number
4
+ IN_HEAD: number
5
+ IN_BODY: number
6
+ BEFORE_CHUNK: number
7
+ IN_CHUNK: number
8
+ }
9
+ method: {
10
+ GET: 'GET'
11
+ HEAD: 'HEAD'
12
+ POST: 'POST'
13
+ PUT: 'PUT'
14
+ DELETE: 'DELETE'
15
+ CONNECT: 'CONNECT'
16
+ OPTIONS: 'OPTIONS'
17
+ TRACE: 'TRACE'
18
+ PATCH: 'PATCH'
19
+ }
20
+ status: {
21
+ 100: 'Continue'
22
+ 101: 'Switching Protocols'
23
+ 102: 'Processing'
24
+ 103: 'Early Hints'
25
+ 200: 'OK'
26
+ 201: 'Created'
27
+ 202: 'Accepted'
28
+ 203: 'Non-Authoritative Information'
29
+ 204: 'No Content'
30
+ 205: 'Reset Content'
31
+ 206: 'Partial Content'
32
+ 207: 'Multi-Status'
33
+ 208: 'Already Reported'
34
+ 226: 'IM Used'
35
+ 300: 'Multiple Choices'
36
+ 301: 'Moved Permanently'
37
+ 302: 'Found'
38
+ 303: 'See Other'
39
+ 304: 'Not Modified'
40
+ 305: 'Use Proxy'
41
+ 307: 'Temporary Redirect'
42
+ 308: 'Permanent Redirect'
43
+ 400: 'Bad Request'
44
+ 401: 'Unauthorized'
45
+ 402: 'Payment Required'
46
+ 403: 'Forbidden'
47
+ 404: 'Not Found'
48
+ 405: 'Method Not Allowed'
49
+ 406: 'Not Acceptable'
50
+ 407: 'Proxy Authentication Required'
51
+ 408: 'Request Timeout'
52
+ 409: 'Conflict'
53
+ 410: 'Gone'
54
+ 411: 'Length Required'
55
+ 412: 'Precondition Failed'
56
+ 413: 'Payload Too Large'
57
+ 414: 'URI Too Long'
58
+ 415: 'Unsupported Media Type'
59
+ 416: 'Range Not Satisfiable'
60
+ 417: 'Expectation Failed'
61
+ 418: "I'm a Teapot"
62
+ 421: 'Misdirected Request'
63
+ 422: 'Unprocessable Entity'
64
+ 423: 'Locked'
65
+ 424: 'Failed Dependency'
66
+ 425: 'Too Early'
67
+ 426: 'Upgrade Required'
68
+ 428: 'Precondition Required'
69
+ 429: 'Too Many Requests'
70
+ 431: 'Request Header Fields Too Large'
71
+ 451: 'Unavailable For Legal Reasons'
72
+ 500: 'Internal Server Error'
73
+ 501: 'Not Implemented'
74
+ 502: 'Bad Gateway'
75
+ 503: 'Service Unavailable'
76
+ 504: 'Gateway Timeout'
77
+ 505: 'HTTP Version Not Supported'
78
+ 506: 'Variant Also Negotiates'
79
+ 507: 'Insufficient Storage'
80
+ 508: 'Loop Detected'
81
+ 509: 'Bandwidth Limit Exceeded'
82
+ 510: 'Not Extended'
83
+ 511: 'Network Authentication Required'
84
+ }
85
+ }
86
+
87
+ declare namespace constants {
88
+ export type HTTPMethod = keyof typeof constants.method
89
+ export type HTTPStatusCode = keyof typeof constants.status
90
+ export type HTTPStatusMessage = (typeof constants.status)[HTTPStatusCode]
91
+ }
92
+
93
+ export = constants
@@ -0,0 +1,8 @@
1
+ declare class HTTPError extends Error {
2
+ constructor(msg: string, code: string, fn: Error)
3
+
4
+ static NOT_IMPLEMENTED(msg?: string): HTTPError
5
+ static CONNECTION_LOST(msg?: string): HTTPError
6
+ }
7
+
8
+ export = HTTPError
@@ -156,6 +156,7 @@ module.exports = class HTTPServerConnection {
156
156
  this._onreset()
157
157
  })
158
158
 
159
+ // Eagerly open the request stream
159
160
  this.req.resume()
160
161
  this.req.pause()
161
162
 
package/package.json CHANGED
@@ -1,14 +1,25 @@
1
1
  {
2
2
  "name": "bare-http1",
3
- "version": "4.0.2",
3
+ "version": "4.0.3",
4
4
  "description": "Native HTTP/1 library for JavaScript",
5
5
  "exports": {
6
- ".": "./index.js",
7
6
  "./package": "./package.json",
8
- "./constants": "./lib/constants.js"
7
+ ".": {
8
+ "types": "./index.d.ts",
9
+ "default": "./index.js"
10
+ },
11
+ "./constants": {
12
+ "types": "./lib/constants.d.ts",
13
+ "default": "./lib/constants.js"
14
+ },
15
+ "./errors": {
16
+ "types": "./lib/errors.d.ts",
17
+ "default": "./lib/errors.js"
18
+ }
9
19
  },
10
20
  "files": [
11
21
  "index.js",
22
+ "index.d.ts",
12
23
  "lib"
13
24
  ],
14
25
  "scripts": {
@@ -25,13 +36,27 @@
25
36
  },
26
37
  "homepage": "https://github.com/holepunchto/bare-http1#readme",
27
38
  "dependencies": {
28
- "bare-events": "^2.0.0",
39
+ "bare-events": "^2.6.0",
29
40
  "bare-stream": "^2.3.0",
30
- "bare-tcp": "^2.0.0"
41
+ "bare-tcp": "^2.0.3"
31
42
  },
32
43
  "devDependencies": {
44
+ "bare-buffer": "^3.0.2",
45
+ "bare-url": "^2.1.3",
33
46
  "brittle": "^3.3.0",
34
47
  "prettier": "^3.4.1",
35
48
  "prettier-config-standard": "^7.0.0"
49
+ },
50
+ "peerDependencies": {
51
+ "bare-buffer": "*",
52
+ "bare-url": "*"
53
+ },
54
+ "peerDependenciesMeta": {
55
+ "bare-buffer": {
56
+ "optional": true
57
+ },
58
+ "bare-url": {
59
+ "optional": true
60
+ }
36
61
  }
37
62
  }