bare-http1 1.1.2 → 1.1.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/CMakeLists.txt CHANGED
@@ -1,13 +1,13 @@
1
1
  cmake_minimum_required(VERSION 3.25)
2
2
 
3
- project(bare_http C)
3
+ project(bare_http1 C)
4
4
 
5
5
  include(bare)
6
6
 
7
- add_bare_module(bare_http)
7
+ add_bare_module(bare_http1)
8
8
 
9
9
  target_sources(
10
- bare_http
10
+ bare_http1
11
11
  PRIVATE
12
12
  binding.c
13
13
  )
package/binding.c CHANGED
@@ -202,27 +202,7 @@ on_new_connection (uv_stream_t *server, int status) {
202
202
  err = js_get_reference_value(env, self->on_connection, &on_connection);
203
203
  assert(err == 0);
204
204
 
205
- js_value_t *res;
206
-
207
- err = js_call_function(env, ctx, on_connection, 0, NULL, &res);
208
- if (err < 0) return;
209
-
210
- bare_http_connection_t *client;
211
- size_t client_size;
212
-
213
- err = js_get_typedarray_info(env, res, NULL, (void **) &client, &client_size, NULL, NULL);
214
- assert(err == 0);
215
-
216
- err = uv_tcp_init(loop, (uv_tcp_t *) client);
217
- assert(err == 0);
218
-
219
- client->server = self;
220
-
221
- if (uv_accept(server, (uv_stream_t *) client) == 0) {
222
- uv_read_start((uv_stream_t *) client, on_alloc_buffer, on_read);
223
- } else {
224
- uv_close((uv_handle_t *) client, on_connection_close);
225
- }
205
+ js_call_function(env, ctx, on_connection, 0, NULL, NULL);
226
206
  }
227
207
 
228
208
  static js_value_t *
@@ -361,6 +341,41 @@ bare_http_close (js_env_t *env, js_callback_info_t *info) {
361
341
  return NULL;
362
342
  }
363
343
 
344
+ static js_value_t *
345
+ bare_http_accept (js_env_t *env, js_callback_info_t *info) {
346
+ int err;
347
+
348
+ size_t argc = 2;
349
+ js_value_t *argv[2];
350
+
351
+ err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
352
+ assert(err == 0);
353
+
354
+ bare_http_server_t *server;
355
+ err = js_get_typedarray_info(env, argv[0], NULL, (void **) &server, NULL, NULL, NULL);
356
+ assert(err == 0);
357
+
358
+ bare_http_connection_t *client;
359
+ err = js_get_typedarray_info(env, argv[1], NULL, (void **) &client, NULL, NULL, NULL);
360
+ assert(err == 0);
361
+
362
+ uv_loop_t *loop;
363
+ js_get_env_loop(env, &loop);
364
+
365
+ err = uv_tcp_init(loop, (uv_tcp_t *) client);
366
+ assert(err == 0);
367
+
368
+ client->server = server;
369
+
370
+ if (uv_accept((uv_stream_t *) server, (uv_stream_t *) client) == 0) {
371
+ uv_read_start((uv_stream_t *) client, on_alloc_buffer, on_read);
372
+ } else {
373
+ uv_close((uv_handle_t *) client, on_connection_close);
374
+ }
375
+
376
+ return NULL;
377
+ }
378
+
364
379
  static js_value_t *
365
380
  bare_http_connection_write (js_env_t *env, js_callback_info_t *info) {
366
381
  int err;
@@ -495,6 +510,11 @@ init (js_env_t *env, js_value_t *exports) {
495
510
  js_create_function(env, "bind", -1, bare_http_bind, NULL, &fn);
496
511
  js_set_named_property(env, exports, "bind", fn);
497
512
  }
513
+ {
514
+ js_value_t *fn;
515
+ js_create_function(env, "accept", -1, bare_http_accept, NULL, &fn);
516
+ js_set_named_property(env, exports, "accept", fn);
517
+ }
498
518
  {
499
519
  js_value_t *fn;
500
520
  js_create_function(env, "close", -1, bare_http_close, NULL, &fn);
package/index.js CHANGED
@@ -200,9 +200,9 @@ module.exports = class Server extends EventEmitter {
200
200
  else if (this.closing && this.connections.length === 0) binding.close(this.handle)
201
201
  })
202
202
 
203
- this.emit('connection', c)
203
+ binding.accept(this.handle, c.handle)
204
204
 
205
- return c.handle
205
+ this.emit('connection', c)
206
206
  }
207
207
 
208
208
  _onread (id, read) {
@@ -300,16 +300,17 @@ class Request extends stream.Readable {
300
300
 
301
301
  class Response extends stream.Writable {
302
302
  constructor (socket, request, close) {
303
- super()
303
+ super({ map: mapToBuffer })
304
304
 
305
305
  this.statusCode = 200
306
306
  this.headers = {}
307
307
  this.socket = socket
308
308
  this.request = request
309
- this.headersFlushed = false
309
+ this.headersSent = false
310
310
  this.chunked = true
311
311
  this.close = close
312
312
  this.ondrain = null
313
+ this.finishing = false
313
314
  this.onlyHeaders = this.request.method === 'HEAD'
314
315
 
315
316
  socket.on('drain', () => this._writeContinue())
@@ -317,8 +318,7 @@ class Response extends stream.Writable {
317
318
 
318
319
  writeHead (statusCode, headers) {
319
320
  this.statusCode = statusCode
320
- this.headers = headers
321
- this.flushHeaders()
321
+ this.headers = headers || {}
322
322
  }
323
323
 
324
324
  _writeContinue () {
@@ -335,10 +335,15 @@ class Response extends stream.Writable {
335
335
  }
336
336
 
337
337
  _write (data, callback) {
338
- if (this.headersFlushed === false) this.flushHeaders()
339
- if (this.onlyHeaders === true) return callback(null)
338
+ if (this.headersSent === false) {
339
+ if (this.finishing) {
340
+ const bytes = data.byteLength + this._writableState.buffered
341
+ this.setHeader('Content-Length', '' + bytes)
342
+ }
343
+ this.flushHeaders()
344
+ }
340
345
 
341
- if (typeof data === 'string') data = Buffer.from(data)
346
+ if (this.onlyHeaders === true) return callback(null)
342
347
 
343
348
  if (this.chunked) {
344
349
  data = Buffer.concat([
@@ -357,7 +362,10 @@ class Response extends stream.Writable {
357
362
  }
358
363
 
359
364
  _final (callback) {
360
- if (this.headersFlushed === false) this.flushHeaders()
365
+ if (this.headersSent === false) {
366
+ this.setHeader('Content-Length', '0')
367
+ this.flushHeaders()
368
+ }
361
369
 
362
370
  if (this.chunked && this.onlyHeaders === false) this.socket.write(Buffer.from('0\r\n\r\n'))
363
371
  if (this.close) this.socket.end()
@@ -365,6 +373,11 @@ class Response extends stream.Writable {
365
373
  callback(null)
366
374
  }
367
375
 
376
+ end (data) {
377
+ this.finishing = true
378
+ return super.end(data)
379
+ }
380
+
368
381
  setHeader (name, value) {
369
382
  this.headers[name.toLowerCase()] = value
370
383
  }
@@ -374,7 +387,7 @@ class Response extends stream.Writable {
374
387
  }
375
388
 
376
389
  flushHeaders () {
377
- if (this.headersFlushed === true) return
390
+ if (this.headersSent === true) return
378
391
 
379
392
  let h = 'HTTP/1.1 ' + this.statusCode + ' ' + STATUS_CODES.get(this.statusCode) + '\r\n'
380
393
  for (const name of Object.keys(this.headers)) {
@@ -390,7 +403,7 @@ class Response extends stream.Writable {
390
403
  h += '\r\n'
391
404
 
392
405
  this.socket.write(Buffer.from(h))
393
- this.headersFlushed = true
406
+ this.headersSent = true
394
407
  }
395
408
  }
396
409
 
@@ -403,3 +416,7 @@ function httpCase (n) {
403
416
  }
404
417
 
405
418
  function noop () {}
419
+
420
+ function mapToBuffer (b) {
421
+ return typeof b === 'string' ? Buffer.from(b) : b
422
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-http1",
3
- "version": "1.1.2",
3
+ "version": "1.1.3",
4
4
  "description": "Native HTTP/1 library for JavaScript",
5
5
  "main": "index.js",
6
6
  "files": [
@@ -11,7 +11,7 @@
11
11
  "prebuilds"
12
12
  ],
13
13
  "scripts": {
14
- "test": "standard",
14
+ "test": "standard && bare test.js",
15
15
  "install": "bare-dev rebuild",
16
16
  "prebuild": "bare-dev prebuild"
17
17
  },
@@ -29,6 +29,8 @@
29
29
  "streamx": "^2.13.0"
30
30
  },
31
31
  "devDependencies": {
32
+ "bare-subprocess": "^1.0.0",
33
+ "brittle": "^3.3.0",
32
34
  "mime-types": "^2.1.35",
33
35
  "pump": "^3.0.0",
34
36
  "range-parser": "^1.2.1",