@webqit/fetch-plus 0.1.9 → 0.1.11

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/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  ],
12
12
  "homepage": "https://fetch-plus.netlify.app/",
13
13
  "icon": "https://webqit.io/icon.svg",
14
- "version": "0.1.9",
14
+ "version": "0.1.11",
15
15
  "license": "MIT",
16
16
  "repository": {
17
17
  "type": "git",
@@ -33,14 +33,14 @@
33
33
  "@webqit/util": "^0.8.16"
34
34
  },
35
35
  "peerDependencies": {
36
- "@webqit/observer": "^3.8.17",
37
- "@webqit/port-plus": "^0.1.13",
38
- "@webqit/url-plus": "^0.1.3"
36
+ "@webqit/observer": "^3.8.19",
37
+ "@webqit/port-plus": "^0.1.15",
38
+ "@webqit/url-plus": "^0.1.4"
39
39
  },
40
40
  "devDependencies": {
41
- "@webqit/observer": "^3.8.17",
42
- "@webqit/port-plus": "^0.1.14",
43
- "@webqit/url-plus": "^0.1.3",
41
+ "@webqit/observer": "^3.8.19",
42
+ "@webqit/port-plus": "^0.1.15",
43
+ "@webqit/url-plus": "^0.1.4",
44
44
  "chai": "^4.3.4",
45
45
  "chai-as-promised": "^7.1.1",
46
46
  "esbuild": "^0.20.2",
@@ -28,13 +28,16 @@ export class LiveResponse extends EventTarget {
28
28
  }
29
29
 
30
30
  static hasPort(respone) {
31
- return !!respone.headers?.get?.(this.xHeaderName)?.trim();
31
+ const responseMeta = _meta(respone);
32
+ return !!responseMeta.get('port')
33
+ || !!respone.headers?.get?.(this.xHeaderName)?.trim();
32
34
  }
33
35
 
34
- static getPort(respone, autoStart = false) {
36
+ static getPort(respone, { handshake = 1 } = {}) {
35
37
  if (!/Response/.test(this.test(respone))) {
36
38
  return;
37
39
  }
40
+
38
41
  const responseMeta = _meta(respone);
39
42
 
40
43
  if (!responseMeta.has('port')) {
@@ -47,8 +50,8 @@ export class LiveResponse extends EventTarget {
47
50
  }
48
51
 
49
52
  const port = scheme === 'channel'
50
- ? new BroadcastChannelPlus(portID, { autoStart, postAwaitsOpen: true, clientServerMode: 'client' })
51
- : new WebSocketPort(portID, { autoStart, naturalOpen: false, postAwaitsOpen: true });
53
+ ? new BroadcastChannelPlus(portID, { handshake, postAwaitsOpen: true, clientServerMode: 'client' })
54
+ : new WebSocketPort(portID, { handshake, postAwaitsOpen: true });
52
55
 
53
56
  responseMeta.set('port', port);
54
57
  }
@@ -57,7 +60,7 @@ export class LiveResponse extends EventTarget {
57
60
  }
58
61
 
59
62
  static attachPort(respone, port) {
60
- if (!(port instanceof MessagePortPlus)) {
63
+ if (port && !(port instanceof MessagePortPlus)) {
61
64
  throw new Error('Client must be a MessagePortPlus interface');
62
65
  }
63
66
  const responseMeta = _meta(respone);
@@ -128,7 +131,8 @@ export class LiveResponse extends EventTarget {
128
131
 
129
132
  /* Level 3 props */
130
133
 
131
- get port() { return this.constructor.getPort(this, true); }
134
+ #port;
135
+ get port() { return this.#port; }
132
136
 
133
137
  // Lifecycle
134
138
 
@@ -245,6 +249,8 @@ export class LiveResponse extends EventTarget {
245
249
  this.#body = $body;
246
250
  this.#concurrent = !!responseFrame.concurrent;
247
251
 
252
+ this.#port = responseFrame.port;
253
+
248
254
  if (!this.#concurrent) {
249
255
  this.#concurrencyAbortController.abort();
250
256
  this.#concurrencyAbortController = new AbortController;
@@ -290,14 +296,18 @@ export class LiveResponse extends EventTarget {
290
296
  // ----------- "Response" handler
291
297
 
292
298
  const execReplaceWithResponse = async (frame, response, options) => {
293
- let body, jsonSuccess = false;
294
- try {
295
- body = response instanceof Response
296
- ? await ResponsePlus.prototype.any.call(response, { to: 'json' })
297
- : (await response.readyStateChange('live')).body;
298
- jsonSuccess = true;
299
- } catch (e) {
300
- body = await ResponsePlus.prototype.any.call(response);
299
+ let body, port, jsonSuccess = true;
300
+ if (response instanceof Response) {
301
+ try {
302
+ body = await ResponsePlus.prototype.any.call(response, { to: 'json' });
303
+ } catch (e) {
304
+ jsonSuccess = false;
305
+ body = await ResponsePlus.prototype.any.call(response);
306
+ }
307
+ port = this.constructor.getPort(response, { handshake: 2 });
308
+ } else {
309
+ body = (await response.readyStateChange('live')).body;
310
+ port = response.port;
301
311
  }
302
312
  directReplaceWith(frame, {
303
313
  body,
@@ -306,6 +316,7 @@ export class LiveResponse extends EventTarget {
306
316
  headers: response.headers,
307
317
  concurrent: response.concurrent, // for response === LiveResponse
308
318
  ...options,
319
+ port,
309
320
  type: response.type,
310
321
  redirected: response.redirected,
311
322
  url: response.url,
@@ -321,10 +332,7 @@ export class LiveResponse extends EventTarget {
321
332
  return response;
322
333
  }
323
334
 
324
- if (this.constructor.hasPort(response)) {
325
- const port = this.constructor.getPort(response);
326
- port.start();
327
-
335
+ if (port) {
328
336
  // Bind to upstream mutations
329
337
  if (jsonSuccess) {
330
338
  port.projectMutations({
@@ -335,7 +343,7 @@ export class LiveResponse extends EventTarget {
335
343
  }
336
344
 
337
345
  // Bind to replacements
338
- return new Promise((resolve) => {
346
+ const returnValue = new Promise((resolve) => {
339
347
  const replaceHandler = (e) => {
340
348
  const { body, ...options } = e.data;
341
349
  wrapReplaceWith(null, body, { ...options });
@@ -347,6 +355,11 @@ export class LiveResponse extends EventTarget {
347
355
  }, { once: true });
348
356
  port.readyStateChange('close').then(resolve);
349
357
  });
358
+
359
+ // Must come after having listened to events
360
+ port.start();
361
+
362
+ return returnValue;
350
363
  }
351
364
  };
352
365
 
@@ -461,7 +474,7 @@ export class LiveResponse extends EventTarget {
461
474
  });
462
475
 
463
476
  const responseMeta = _meta(this);
464
- _wq(response).set('meta', responseMeta);
477
+ _wq(response).set('meta', new Map(responseMeta));
465
478
 
466
479
  if (!clientPort) return response;
467
480
 
@@ -486,6 +499,9 @@ export class LiveResponse extends EventTarget {
486
499
  status: this.#status,
487
500
  statusText: this.#statusText,
488
501
  headers,
502
+ type: this.type,
503
+ url: this.url,
504
+ redirect: this.redirect,
489
505
  concurrent: this.#concurrent,
490
506
  }, { type: 'response.replace', live: true/*gracefully ignored if not an object*/, signal: AbortSignal.any([this.#concurrencyAbortController.signal].concat(abortSignal || []))/* stop observing mutations on body a new body takes effect */ });
491
507
  };