shelving 1.195.0 → 1.195.1

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.
@@ -2,7 +2,9 @@ import type { AnyCaller } from "../../util/function.js";
2
2
  import type { RequestOptions } from "../../util/http.js";
3
3
  import type { Endpoint } from "../endpoint/Endpoint.js";
4
4
  import { ThroughAPIProvider } from "./ThroughAPIProvider.js";
5
- /** Provider that logs API operations to the console. */
5
+ /** Provider that logs operations to the console. */
6
6
  export declare class DebugAPIProvider<P, R> extends ThroughAPIProvider<P, R> {
7
- call<PP extends P, RR extends R>(endpoint: Endpoint<PP, RR>, payload: PP, options?: RequestOptions, caller?: AnyCaller): Promise<RR>;
7
+ getRequest<PP extends P, RR extends R>(endpoint: Endpoint<PP, RR>, payload: PP, options?: RequestOptions, caller?: AnyCaller): Request;
8
+ fetch(request: Request): Promise<Response>;
9
+ parseResponse<PP extends P, RR extends R>(endpoint: Endpoint<PP, RR>, response: Response, caller?: AnyCaller): Promise<RR>;
8
10
  }
@@ -1,37 +1,43 @@
1
1
  import { debugFullRequest, debugFullResponse } from "../../util/debug.js";
2
2
  import { ThroughAPIProvider } from "./ThroughAPIProvider.js";
3
- /** Provider that logs API operations to the console. */
3
+ /** Provider that logs operations to the console. */
4
4
  export class DebugAPIProvider extends ThroughAPIProvider {
5
- async call(endpoint, payload, options, caller = this.call) {
6
- // Turn the payload into a request and debug it before sending.
7
- let request;
5
+ getRequest(endpoint, payload, options, caller = this.getRequest) {
6
+ const url = this.url.toString();
7
+ const ep = endpoint.toString();
8
8
  try {
9
- request = this.getRequest(endpoint, payload, options, caller);
9
+ const request = super.getRequest(endpoint, payload, options, caller);
10
+ console.debug("✔ REQUEST", url, ep, payload);
11
+ return request;
10
12
  }
11
13
  catch (reason) {
12
- console.error("✘ FETCH", this.url.toString(), endpoint.toString(), payload, reason);
14
+ console.error("✘ REQUEST", url, ep, payload, reason);
13
15
  throw reason;
14
16
  }
15
- const debuggedRequest = await debugFullRequest(request);
16
- console.debug("… FETCH", this.url.toString(), endpoint.toString(), payload, debuggedRequest);
17
- // Fetch the response and debug if it throws.
18
- let response;
17
+ }
18
+ async fetch(request) {
19
+ const url = this.url.toString();
19
20
  try {
20
- response = await this.fetch(request);
21
+ console.error("→ FETCH", url, await debugFullRequest(request));
22
+ const response = await super.fetch(request);
23
+ console.error("← FETCH", url, await debugFullResponse(response));
24
+ return response;
21
25
  }
22
26
  catch (reason) {
23
- console.error("✘ FETCH", this.url.toString(), endpoint.toString(), payload, debuggedRequest, reason);
27
+ console.error("✘ FETCH", url, reason);
24
28
  throw reason;
25
29
  }
26
- const debuggedResponse = await debugFullResponse(response);
27
- // Convert the result or any parsing error.
30
+ }
31
+ async parseResponse(endpoint, response, caller = this.parseResponse) {
32
+ const url = this.url.toString();
33
+ const ep = endpoint.toString();
28
34
  try {
29
- const result = await this.parseResponse(endpoint, response, caller);
30
- console.debug("✔ FETCH", this.url.toString(), endpoint.toString(), payload, debuggedRequest, debuggedResponse, result);
35
+ const result = await super.parseResponse(endpoint, response, caller);
36
+ console.debug("✔ RESPONSE", url, ep, result);
31
37
  return result;
32
38
  }
33
39
  catch (reason) {
34
- console.error("✘ FETCH", this.url.toString(), endpoint.toString(), payload, debuggedRequest, debuggedResponse, reason);
40
+ console.error("✘ RESPONSE", url, ep, reason);
35
41
  throw reason;
36
42
  }
37
43
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "shelving",
3
- "version": "1.195.0",
3
+ "version": "1.195.1",
4
4
  "author": "Dave Houlbrooke <dave@shax.com>",
5
5
  "repository": {
6
6
  "type": "git",
package/util/sequence.js CHANGED
@@ -2,8 +2,12 @@ import { UnexpectedError } from "../error/UnexpectedError.js";
2
2
  import { getDeferred, getDelay } from "./async.js";
3
3
  import { ABORT } from "./constants.js";
4
4
  /** Turn a `Promise<R>` into an `IteratorAbortResult<R>` */
5
- async function _awaitAbort(value) {
6
- return { done: ABORT, value: await value };
5
+ function _awaitAbortResult(value) {
6
+ return value.then(_getAbortResult);
7
+ }
8
+ /** Turn a result `R` into an `IteratorAbortResult<R>` */
9
+ function _getAbortResult(value) {
10
+ return { done: ABORT, value };
7
11
  }
8
12
  /** Call an iterator's `return()` method (if it exists) with an initial value, and return the `value` it returns. */
9
13
  async function _iteratorReturn(iterator, initial, caller) {
@@ -26,13 +30,12 @@ export function isSequence(value) {
26
30
  /** Infinite sequence that yields until a `SIGNAL` is received. */
27
31
  export async function* repeatUntil(source, ...signals) {
28
32
  const iterator = source[Symbol.asyncIterator]();
33
+ const aborts = signals.map(_awaitAbortResult);
29
34
  let n;
30
35
  while (true) {
31
36
  try {
32
- const { done, value } = await Promise.race([
33
- iterator.next(n),
34
- ...signals.map(_awaitAbort),
35
- ]);
37
+ const next = iterator.next(n);
38
+ const { done, value } = await (aborts.length ? Promise.race([next, ...aborts]) : next);
36
39
  if (done) {
37
40
  // For aborts, tell the iterator we're no longer using it.
38
41
  if (done === ABORT)
@@ -81,7 +84,7 @@ export async function* callSequence(sequence, callback) {
81
84
  export function runSequence(sequence, onNext, onError, onReturn) {
82
85
  const { promise, resolve } = getDeferred();
83
86
  void _runSequenceIterator(sequence[Symbol.asyncIterator](), promise, onNext, onError, onReturn);
84
- return (value) => resolve({ done: ABORT, value });
87
+ return (value) => resolve(_getAbortResult(value));
85
88
  }
86
89
  async function _runSequenceIterator(iterator, stopped, onNext, onError, onReturn) {
87
90
  let n;