bare-http1 1.1.2 → 1.2.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/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 *
@@ -344,13 +324,13 @@ static js_value_t *
344
324
  bare_http_close (js_env_t *env, js_callback_info_t *info) {
345
325
  int err;
346
326
 
347
- size_t argc = 3;
348
- js_value_t *argv[3];
327
+ size_t argc = 1;
328
+ js_value_t *argv[1];
349
329
 
350
330
  err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
351
331
  assert(err == 0);
352
332
 
353
- assert(argc == 3);
333
+ assert(argc == 1);
354
334
 
355
335
  bare_http_server_t *self;
356
336
  err = js_get_typedarray_info(env, argv[0], NULL, (void **) &self, NULL, NULL, NULL);
@@ -361,6 +341,83 @@ 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_ref (js_env_t *env, js_callback_info_t *info) {
346
+ int err;
347
+
348
+ size_t argc = 1;
349
+ js_value_t *argv[1];
350
+
351
+ err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
352
+ assert(err == 0);
353
+
354
+ assert(argc == 1);
355
+
356
+ bare_http_server_t *self;
357
+ err = js_get_typedarray_info(env, argv[0], NULL, (void **) &self, NULL, NULL, NULL);
358
+ assert(err == 0);
359
+
360
+ uv_ref((uv_handle_t *) self);
361
+
362
+ return NULL;
363
+ }
364
+
365
+ static js_value_t *
366
+ bare_http_unref (js_env_t *env, js_callback_info_t *info) {
367
+ int err;
368
+
369
+ size_t argc = 1;
370
+ js_value_t *argv[1];
371
+
372
+ err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
373
+ assert(err == 0);
374
+
375
+ assert(argc == 1);
376
+
377
+ bare_http_server_t *self;
378
+ err = js_get_typedarray_info(env, argv[0], NULL, (void **) &self, NULL, NULL, NULL);
379
+ assert(err == 0);
380
+
381
+ uv_unref((uv_handle_t *) self);
382
+
383
+ return NULL;
384
+ }
385
+
386
+ static js_value_t *
387
+ bare_http_accept (js_env_t *env, js_callback_info_t *info) {
388
+ int err;
389
+
390
+ size_t argc = 2;
391
+ js_value_t *argv[2];
392
+
393
+ err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
394
+ assert(err == 0);
395
+
396
+ bare_http_server_t *server;
397
+ err = js_get_typedarray_info(env, argv[0], NULL, (void **) &server, NULL, NULL, NULL);
398
+ assert(err == 0);
399
+
400
+ bare_http_connection_t *client;
401
+ err = js_get_typedarray_info(env, argv[1], NULL, (void **) &client, NULL, NULL, NULL);
402
+ assert(err == 0);
403
+
404
+ uv_loop_t *loop;
405
+ js_get_env_loop(env, &loop);
406
+
407
+ err = uv_tcp_init(loop, (uv_tcp_t *) client);
408
+ assert(err == 0);
409
+
410
+ client->server = server;
411
+
412
+ if (uv_accept((uv_stream_t *) server, (uv_stream_t *) client) == 0) {
413
+ uv_read_start((uv_stream_t *) client, on_alloc_buffer, on_read);
414
+ } else {
415
+ uv_close((uv_handle_t *) client, on_connection_close);
416
+ }
417
+
418
+ return NULL;
419
+ }
420
+
364
421
  static js_value_t *
365
422
  bare_http_connection_write (js_env_t *env, js_callback_info_t *info) {
366
423
  int err;
@@ -495,11 +552,26 @@ init (js_env_t *env, js_value_t *exports) {
495
552
  js_create_function(env, "bind", -1, bare_http_bind, NULL, &fn);
496
553
  js_set_named_property(env, exports, "bind", fn);
497
554
  }
555
+ {
556
+ js_value_t *fn;
557
+ js_create_function(env, "accept", -1, bare_http_accept, NULL, &fn);
558
+ js_set_named_property(env, exports, "accept", fn);
559
+ }
498
560
  {
499
561
  js_value_t *fn;
500
562
  js_create_function(env, "close", -1, bare_http_close, NULL, &fn);
501
563
  js_set_named_property(env, exports, "close", fn);
502
564
  }
565
+ {
566
+ js_value_t *fn;
567
+ js_create_function(env, "ref", -1, bare_http_ref, NULL, &fn);
568
+ js_set_named_property(env, exports, "ref", fn);
569
+ }
570
+ {
571
+ js_value_t *fn;
572
+ js_create_function(env, "unref", -1, bare_http_unref, NULL, &fn);
573
+ js_set_named_property(env, exports, "unref", fn);
574
+ }
503
575
  {
504
576
  js_value_t *fn;
505
577
  js_create_function(env, "connectionWrite", -1, bare_http_connection_write, 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) {
@@ -276,6 +276,14 @@ module.exports = class Server extends EventEmitter {
276
276
 
277
277
  return this
278
278
  }
279
+
280
+ ref () {
281
+ binding.ref(this.handle)
282
+ }
283
+
284
+ unref () {
285
+ binding.unref(this.handle)
286
+ }
279
287
  }
280
288
 
281
289
  class Request extends stream.Readable {
@@ -300,16 +308,17 @@ class Request extends stream.Readable {
300
308
 
301
309
  class Response extends stream.Writable {
302
310
  constructor (socket, request, close) {
303
- super()
311
+ super({ map: mapToBuffer })
304
312
 
305
313
  this.statusCode = 200
306
314
  this.headers = {}
307
315
  this.socket = socket
308
316
  this.request = request
309
- this.headersFlushed = false
317
+ this.headersSent = false
310
318
  this.chunked = true
311
319
  this.close = close
312
320
  this.ondrain = null
321
+ this.finishing = false
313
322
  this.onlyHeaders = this.request.method === 'HEAD'
314
323
 
315
324
  socket.on('drain', () => this._writeContinue())
@@ -317,8 +326,7 @@ class Response extends stream.Writable {
317
326
 
318
327
  writeHead (statusCode, headers) {
319
328
  this.statusCode = statusCode
320
- this.headers = headers
321
- this.flushHeaders()
329
+ this.headers = headers || {}
322
330
  }
323
331
 
324
332
  _writeContinue () {
@@ -335,10 +343,15 @@ class Response extends stream.Writable {
335
343
  }
336
344
 
337
345
  _write (data, callback) {
338
- if (this.headersFlushed === false) this.flushHeaders()
339
- if (this.onlyHeaders === true) return callback(null)
346
+ if (this.headersSent === false) {
347
+ if (this.finishing) {
348
+ const bytes = data.byteLength + this._writableState.buffered
349
+ this.setHeader('Content-Length', '' + bytes)
350
+ }
351
+ this.flushHeaders()
352
+ }
340
353
 
341
- if (typeof data === 'string') data = Buffer.from(data)
354
+ if (this.onlyHeaders === true) return callback(null)
342
355
 
343
356
  if (this.chunked) {
344
357
  data = Buffer.concat([
@@ -357,7 +370,10 @@ class Response extends stream.Writable {
357
370
  }
358
371
 
359
372
  _final (callback) {
360
- if (this.headersFlushed === false) this.flushHeaders()
373
+ if (this.headersSent === false) {
374
+ this.setHeader('Content-Length', '0')
375
+ this.flushHeaders()
376
+ }
361
377
 
362
378
  if (this.chunked && this.onlyHeaders === false) this.socket.write(Buffer.from('0\r\n\r\n'))
363
379
  if (this.close) this.socket.end()
@@ -365,6 +381,11 @@ class Response extends stream.Writable {
365
381
  callback(null)
366
382
  }
367
383
 
384
+ end (data) {
385
+ this.finishing = true
386
+ return super.end(data)
387
+ }
388
+
368
389
  setHeader (name, value) {
369
390
  this.headers[name.toLowerCase()] = value
370
391
  }
@@ -374,7 +395,7 @@ class Response extends stream.Writable {
374
395
  }
375
396
 
376
397
  flushHeaders () {
377
- if (this.headersFlushed === true) return
398
+ if (this.headersSent === true) return
378
399
 
379
400
  let h = 'HTTP/1.1 ' + this.statusCode + ' ' + STATUS_CODES.get(this.statusCode) + '\r\n'
380
401
  for (const name of Object.keys(this.headers)) {
@@ -390,7 +411,7 @@ class Response extends stream.Writable {
390
411
  h += '\r\n'
391
412
 
392
413
  this.socket.write(Buffer.from(h))
393
- this.headersFlushed = true
414
+ this.headersSent = true
394
415
  }
395
416
  }
396
417
 
@@ -403,3 +424,7 @@ function httpCase (n) {
403
424
  }
404
425
 
405
426
  function noop () {}
427
+
428
+ function mapToBuffer (b) {
429
+ return typeof b === 'string' ? Buffer.from(b) : b
430
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-http1",
3
- "version": "1.1.2",
3
+ "version": "1.2.0",
4
4
  "description": "Native HTTP/1 library for JavaScript",
5
5
  "main": "index.js",
6
6
  "files": [
@@ -11,9 +11,7 @@
11
11
  "prebuilds"
12
12
  ],
13
13
  "scripts": {
14
- "test": "standard",
15
- "install": "bare-dev rebuild",
16
- "prebuild": "bare-dev prebuild"
14
+ "test": "standard && bare test.js"
17
15
  },
18
16
  "repository": {
19
17
  "type": "git",
@@ -29,6 +27,8 @@
29
27
  "streamx": "^2.13.0"
30
28
  },
31
29
  "devDependencies": {
30
+ "bare-subprocess": "^1.0.0",
31
+ "brittle": "^3.3.0",
32
32
  "mime-types": "^2.1.35",
33
33
  "pump": "^3.0.0",
34
34
  "range-parser": "^1.2.1",