bdy 1.26.1-stage → 1.26.2-dev

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.
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "bdy",
3
3
  "preferGlobal": false,
4
- "version": "1.26.1-stage",
4
+ "version": "1.26.2-dev",
5
5
  "type": "commonjs",
6
6
  "license": "MIT",
7
7
  "homepage": "https://buddy.works/docs/cli",
@@ -163,10 +163,18 @@ class TunnelHttp extends events_1.default {
163
163
  const headers = this.req.headers || {};
164
164
  if (headers['content-length'] !== undefined)
165
165
  return {};
166
- const hasBody = this.req.stream
167
- ? !this.req.stream.endAfterHeaders
168
- : headers['transfer-encoding'] !== undefined;
169
- return hasBody ? { 'transfer-encoding': 'chunked' } : {};
166
+ return this.hasRequestBody() ? { 'transfer-encoding': 'chunked' } : {};
167
+ }
168
+ // Whether the incoming request carries a body. h2: the client sent no END_STREAM on HEADERS.
169
+ // h1: framed by transfer-encoding or a non-zero content-length - without either there is none.
170
+ hasRequestBody() {
171
+ const headers = this.req.headers || {};
172
+ if (this.req.stream)
173
+ return !this.req.stream.endAfterHeaders;
174
+ if (headers['transfer-encoding'] !== undefined)
175
+ return true;
176
+ const cl = headers['content-length'];
177
+ return cl !== undefined && cl !== '0';
170
178
  }
171
179
  getRequestHeaders(http2) {
172
180
  const h = {};
@@ -188,6 +196,16 @@ class TunnelHttp extends events_1.default {
188
196
  }
189
197
  return h;
190
198
  }
199
+ // The target's response on its way to the client: cleared, plus a permissive CORS default. Only
200
+ // here - getClearedHeaders also builds the request to the target, and the default used to leak
201
+ // into it as a request header the target never asked for.
202
+ getClearedResponseHeaders(headers = {}, compressionMethod, allowUpgrade = false) {
203
+ const tmp = this.getClearedHeaders(headers, compressionMethod, allowUpgrade);
204
+ if (tmp['access-control-allow-origin'] === undefined) {
205
+ tmp['access-control-allow-origin'] = '*';
206
+ }
207
+ return tmp;
208
+ }
191
209
  getClearedHeaders(headers = {}, compressionMethod, allowUpgrade = false) {
192
210
  const hop = [
193
211
  ':method',
@@ -231,9 +249,6 @@ class TunnelHttp extends events_1.default {
231
249
  tmp[name] = headers[name];
232
250
  }
233
251
  });
234
- if (tmp['access-control-allow-origin'] === undefined) {
235
- tmp['access-control-allow-origin'] = '*';
236
- }
237
252
  if (tmp['set-cookie']) {
238
253
  const setCookie = [];
239
254
  const h = (this.req.headers.host ||
@@ -286,7 +301,7 @@ class TunnelHttp extends events_1.default {
286
301
  const statusCode = this.proxyRes.statusCode;
287
302
  const resHeaders = this.proxyRes.headers;
288
303
  const compressionMethod = compression_1.default.detect(this.compression || false, reqHeaders, resHeaders);
289
- this.outputHeaders(statusCode, this.getClearedHeaders(resHeaders, compressionMethod));
304
+ this.outputHeaders(statusCode, this.getClearedResponseHeaders(resHeaders, compressionMethod));
290
305
  if (this.logRequest) {
291
306
  (0, node_stream_1.pipeline)(this.proxyRes, this.logRequest.responseBody, compression_1.default.compress(compressionMethod), this.res, () => {
292
307
  this.clear();
@@ -334,7 +349,7 @@ class TunnelHttp extends events_1.default {
334
349
  const statusCode = this.proxyRes.statusCode;
335
350
  const resHeaders = this.proxyRes.headers;
336
351
  const compressionMethod = compression_1.default.detect(this.compression || false, reqHeaders, resHeaders);
337
- this.outputHeaders(statusCode, this.getClearedHeaders(resHeaders, compressionMethod));
352
+ this.outputHeaders(statusCode, this.getClearedResponseHeaders(resHeaders, compressionMethod));
338
353
  if (this.logRequest) {
339
354
  (0, node_stream_1.pipeline)(this.proxyRes, this.logRequest.responseBody, compression_1.default.compress(compressionMethod), this.res, () => {
340
355
  this.clear();
@@ -361,12 +376,12 @@ class TunnelHttp extends events_1.default {
361
376
  }
362
377
  r.on('response', (statusCode, resHeaders, stream) => {
363
378
  if (!stream) {
364
- this.outputHeaders(statusCode, this.getClearedHeaders(resHeaders));
379
+ this.outputHeaders(statusCode, this.getClearedResponseHeaders(resHeaders));
365
380
  this.res.end();
366
381
  }
367
382
  else {
368
383
  const compressionMethod = compression_1.default.detect(this.compression || false, reqHeaders, resHeaders);
369
- this.outputHeaders(statusCode, this.getClearedHeaders(resHeaders, compressionMethod));
384
+ this.outputHeaders(statusCode, this.getClearedResponseHeaders(resHeaders, compressionMethod));
370
385
  if (this.logRequest) {
371
386
  (0, node_stream_1.pipeline)(stream, this.logRequest.responseBody, compression_1.default.compress(compressionMethod), this.res, () => {
372
387
  this.clear();
@@ -431,7 +446,10 @@ class TunnelHttp extends events_1.default {
431
446
  this.decrementConcurrent = decrementConcurrent;
432
447
  let r;
433
448
  try {
434
- r = client.request(headers);
449
+ // endStream explicitly: node's default ends the stream right after HEADERS for GET, HEAD
450
+ // and DELETE, so the body piped in afterwards failed with ERR_STREAM_WRITE_AFTER_END and a
451
+ // DELETE/GET with a body never reached an h2 target (#1an0ffhx1bd).
452
+ r = client.request(headers, { endStream: !this.hasRequestBody() });
435
453
  }
436
454
  catch (err) {
437
455
  decrementConcurrent();
@@ -459,7 +477,7 @@ class TunnelHttp extends events_1.default {
459
477
  r.setTimeout(0);
460
478
  const status = resHeaders[':status'] || 200;
461
479
  const compressionMethod = compression_1.default.detect(this.compression || false, reqHeaders, resHeaders);
462
- this.outputHeaders(status, this.getClearedHeaders(resHeaders, compressionMethod));
480
+ this.outputHeaders(status, this.getClearedResponseHeaders(resHeaders, compressionMethod));
463
481
  if (this.logRequest) {
464
482
  (0, node_stream_1.pipeline)(r, this.logRequest.responseBody, compression_1.default.compress(compressionMethod), this.res, () => {
465
483
  decrementConcurrent();
@@ -541,7 +559,7 @@ class TunnelHttp extends events_1.default {
541
559
  this.httpLog.newResponse(101, resHeaders, this.logRequest);
542
560
  }
543
561
  socket.setTimeout(0);
544
- const headers = this.getClearedHeaders(resHeaders, null, true);
562
+ const headers = this.getClearedResponseHeaders(resHeaders, null, true);
545
563
  this.clear();
546
564
  let msg = 'HTTP/1.1 101 Web Socket Protocol Handshake\r\n';
547
565
  Object.keys(headers).forEach((k) => {
@@ -146,10 +146,12 @@ class ServerHttp2 extends events_1.default {
146
146
  let client = http2_1.default.connect(`http://localhost:${address.port}`, {
147
147
  maxSessionMemory: 100,
148
148
  });
149
+ // endStream explicitly, as in TunnelHttp.createRequestHttps2: node's default closes a
150
+ // GET/HEAD/DELETE stream after HEADERS and the body write below would fail.
149
151
  let req = client.request({
150
152
  ...logRequest.headers,
151
153
  ':authority': this.host
152
- });
154
+ }, { endStream: logRequest.requestBody.data.length === 0 });
153
155
  if (logRequest.requestBody.data.length > 0)
154
156
  req.write(logRequest.requestBody.data);
155
157
  req.on('response', () => {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "bdy",
3
3
  "preferGlobal": false,
4
- "version": "1.26.1-stage",
4
+ "version": "1.26.2-dev",
5
5
  "type": "commonjs",
6
6
  "license": "MIT",
7
7
  "homepage": "https://buddy.works/docs/cli",