shelving 1.196.3 → 1.198.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/api/index.d.ts CHANGED
@@ -3,6 +3,7 @@ export * from "./cache/EndpointCache.js";
3
3
  export * from "./endpoint/Endpoint.js";
4
4
  export * from "./endpoint/util.js";
5
5
  export * from "./provider/APIProvider.js";
6
+ export * from "./provider/CachedAPIProvider.js";
6
7
  export * from "./provider/ClientAPIProvider.js";
7
8
  export * from "./provider/DebugAPIProvider.js";
8
9
  export * from "./provider/JSONAPIProvider.js";
package/api/index.js CHANGED
@@ -3,6 +3,7 @@ export * from "./cache/EndpointCache.js";
3
3
  export * from "./endpoint/Endpoint.js";
4
4
  export * from "./endpoint/util.js";
5
5
  export * from "./provider/APIProvider.js";
6
+ export * from "./provider/CachedAPIProvider.js";
6
7
  export * from "./provider/ClientAPIProvider.js";
7
8
  export * from "./provider/DebugAPIProvider.js";
8
9
  export * from "./provider/JSONAPIProvider.js";
@@ -0,0 +1,20 @@
1
+ import type { AnyCaller } from "../../util/function.js";
2
+ import type { Endpoint } from "../endpoint/Endpoint.js";
3
+ import type { APIProvider } from "./APIProvider.js";
4
+ import { ThroughAPIProvider } from "./ThroughAPIProvider.js";
5
+ /**
6
+ * API provider wrapper that serves requests through an `APICache`.
7
+ * - Constructor accepts a `source` provider and an optional default `maxAge`.
8
+ * - On `call(...)`, triggers `cache.refresh(maxAge)` for the endpoint+payload before awaiting `cache.call(...)`.
9
+ * - `invalidate`, `invalidateAll`, `refresh`, and `refreshAll` pass through to the underlying cache and use `this.maxAge` as the default refresh timing.
10
+ */
11
+ export declare class CachedAPIProvider<P, R> extends ThroughAPIProvider<P, R> {
12
+ readonly maxAge: number | undefined;
13
+ private readonly _cache;
14
+ constructor(source: APIProvider<P, R>, maxAge?: number);
15
+ call<PP extends P, RR extends R>(endpoint: Endpoint<PP, RR>, payload: PP, maxAge?: number | undefined, caller?: AnyCaller): Promise<RR>;
16
+ invalidate<PP extends P, RR extends R>(endpoint: Endpoint<PP, RR>, payload: PP): void;
17
+ invalidateAll<PP extends P, RR extends R>(endpoint: Endpoint<PP, RR>): void;
18
+ refresh<PP extends P, RR extends R>(endpoint: Endpoint<PP, RR>, payload: PP): void;
19
+ refreshAll<PP extends P, RR extends R>(endpoint: Endpoint<PP, RR>): void;
20
+ }
@@ -0,0 +1,34 @@
1
+ import { APICache } from "../cache/APICache.js";
2
+ import { ThroughAPIProvider } from "./ThroughAPIProvider.js";
3
+ /**
4
+ * API provider wrapper that serves requests through an `APICache`.
5
+ * - Constructor accepts a `source` provider and an optional default `maxAge`.
6
+ * - On `call(...)`, triggers `cache.refresh(maxAge)` for the endpoint+payload before awaiting `cache.call(...)`.
7
+ * - `invalidate`, `invalidateAll`, `refresh`, and `refreshAll` pass through to the underlying cache and use `this.maxAge` as the default refresh timing.
8
+ */
9
+ export class CachedAPIProvider extends ThroughAPIProvider {
10
+ maxAge;
11
+ _cache;
12
+ constructor(source, maxAge) {
13
+ super(source);
14
+ this.maxAge = maxAge;
15
+ this._cache = new APICache(source);
16
+ }
17
+ // @ts-expect-error TS2416: intentionally diverges from `APIProvider.call` — `RequestOptions` are not used by the cache, so the third arg is repurposed as `maxAge`.
18
+ call(endpoint, payload, maxAge = this.maxAge, caller = this.call) {
19
+ this._cache.refresh(endpoint, payload, maxAge);
20
+ return this._cache.call(endpoint, payload, maxAge, caller);
21
+ }
22
+ invalidate(endpoint, payload) {
23
+ this._cache.invalidate(endpoint, payload);
24
+ }
25
+ invalidateAll(endpoint) {
26
+ this._cache.invalidateAll(endpoint);
27
+ }
28
+ refresh(endpoint, payload) {
29
+ this._cache.refresh(endpoint, payload, this.maxAge);
30
+ }
31
+ refreshAll(endpoint) {
32
+ this._cache.refreshAll(endpoint, this.maxAge);
33
+ }
34
+ }
@@ -21,7 +21,11 @@ export interface ClientAPIProviderOptions {
21
21
  * - Omits `signal` because it's not relevant at the provider level.
22
22
  */
23
23
  readonly options?: Omit<RequestOptions, "signal">;
24
- /** Timeout in milliseconds, or `undefined` for no timeout. */
24
+ /**
25
+ * Timeout in milliseconds before the request is aborted with `TimeoutError`.
26
+ * - Defaults to `20_000` (20 seconds) — chosen to fire before common platform wall-clock caps (Cloudflare Workers ~30s, Vercel/AWS API Gateway ~29s) so the abort propagates as a clean rejection instead of an opaque runtime termination.
27
+ * - Pass `0` to disable the timeout (e.g. for streaming or long-poll endpoints). Raise it for specifically slow endpoints.
28
+ */
25
29
  readonly timeout?: number | undefined;
26
30
  }
27
31
  /**
@@ -37,8 +41,8 @@ export declare class ClientAPIProvider<P = unknown, R = unknown> extends APIProv
37
41
  readonly url: URL;
38
42
  /** Default options used for HTTP requests created with `this.getRequest()` and `this.fetch()` */
39
43
  readonly options: RequestOptions;
40
- /** Timeout in milliseconds, or `undefined` for no timeout. */
41
- readonly timeout: number | undefined;
44
+ /** Timeout in milliseconds before the request is aborted, or `0` for no timeout. */
45
+ readonly timeout: number;
42
46
  constructor({ url, options, timeout }: ClientAPIProviderOptions);
43
47
  renderURL<PP extends P, RR extends R>(endpoint: Endpoint<PP, RR>, payload: PP, caller?: AnyCaller): URL;
44
48
  getRequest<PP extends P, RR extends R>(endpoint: Endpoint<PP, RR>, payload: PP, options?: RequestOptions, caller?: AnyCaller): Request;
@@ -19,9 +19,9 @@ export class ClientAPIProvider extends APIProvider {
19
19
  url;
20
20
  /** Default options used for HTTP requests created with `this.getRequest()` and `this.fetch()` */
21
21
  options;
22
- /** Timeout in milliseconds, or `undefined` for no timeout. */
22
+ /** Timeout in milliseconds before the request is aborted, or `0` for no timeout. */
23
23
  timeout;
24
- constructor({ url, options = {}, timeout }) {
24
+ constructor({ url, options = {}, timeout = 20_000 }) {
25
25
  super();
26
26
  this.url = requireBaseURL(url, ClientAPIProvider);
27
27
  this.options = options;
@@ -18,9 +18,9 @@ export class DebugAPIProvider extends ThroughAPIProvider {
18
18
  async fetch(request) {
19
19
  const url = this.url.toString();
20
20
  try {
21
- console.error("→ FETCH", url, await debugFullRequest(request));
21
+ console.debug("→ FETCH", url, await debugFullRequest(request));
22
22
  const response = await super.fetch(request);
23
- console.error("← FETCH", url, await debugFullResponse(response));
23
+ console.debug("← FETCH", url, await debugFullResponse(response));
24
24
  return response;
25
25
  }
26
26
  catch (reason) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "shelving",
3
- "version": "1.196.3",
3
+ "version": "1.198.0",
4
4
  "author": "Dave Houlbrooke <dave@shax.com>",
5
5
  "repository": {
6
6
  "type": "git",
@@ -1,7 +1,7 @@
1
1
  import type { NONE } from "../util/constants.js";
2
2
  import { FetchStore } from "./FetchStore.js";
3
- import { Store } from "./Store.js";
4
- export type PayloadFetchCallback<P, R> = (payload: P, signal: AbortSignal) => R | PromiseLike<R>;
3
+ import { type AsyncStoreInput, Store } from "./Store.js";
4
+ export type PayloadFetchCallback<P, R> = (payload: P, signal: AbortSignal) => AsyncStoreInput<R>;
5
5
  /**
6
6
  * Store that fetches its values from a remote source by sending a payload to them.
7
7
  *
package/util/debug.js CHANGED
@@ -77,8 +77,36 @@ async function _debugFullMessage(head, message) {
77
77
  const body = await _debugMessageBody(message);
78
78
  return `${head}${headers && `\n${headers}`}${body && `\n\n${body}`}`;
79
79
  }
80
+ /** Maximum bytes read from a Request/Response body when debugging. Caps memory and prevents hangs on streaming bodies. */
81
+ const _DEBUG_BODY_LIMIT = 64 * 1024;
80
82
  async function _debugMessageBody(message) {
81
- return message.bodyUsed ? "[body used]" : await message.clone().text();
83
+ if (message.bodyUsed)
84
+ return "[body used]";
85
+ const body = message.clone().body;
86
+ if (!body)
87
+ return "";
88
+ const reader = body.getReader();
89
+ const decoder = new TextDecoder();
90
+ let bytes = 0;
91
+ let text = "";
92
+ try {
93
+ while (true) {
94
+ const { done, value } = await reader.read();
95
+ if (done)
96
+ return text + decoder.decode();
97
+ bytes += value.byteLength;
98
+ if (bytes > _DEBUG_BODY_LIMIT) {
99
+ const keep = value.byteLength - (bytes - _DEBUG_BODY_LIMIT);
100
+ text += decoder.decode(value.subarray(0, keep), { stream: true });
101
+ await reader.cancel();
102
+ return `${text}${decoder.decode()}\n[truncated at ${_DEBUG_BODY_LIMIT} bytes]`;
103
+ }
104
+ text += decoder.decode(value, { stream: true });
105
+ }
106
+ }
107
+ catch (reason) {
108
+ return `${text}\n[read failed: ${reason?.message ?? reason}]`;
109
+ }
82
110
  }
83
111
  /** Debug a `Request` as a string. */
84
112
  export function debugRequest(request) {