@prezly/theme-kit-core 10.9.2 → 10.10.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/build/content-delivery/ContentDelivery.cjs +138 -18
- package/build/content-delivery/ContentDelivery.d.ts +15 -1
- package/build/content-delivery/ContentDelivery.mjs +137 -17
- package/build/content-delivery/ContentDelivery.test.cjs +369 -5
- package/build/content-delivery/ContentDelivery.test.mjs +371 -7
- package/build/content-delivery/RequestCoalescer.cjs +7 -2
- package/build/content-delivery/RequestCoalescer.d.ts +1 -1
- package/build/content-delivery/RequestCoalescer.mjs +8 -3
- package/build/content-delivery/cache/memory.cjs +11 -5
- package/build/content-delivery/cache/memory.mjs +11 -5
- package/build/content-delivery/cache/memory.test.cjs +30 -0
- package/build/content-delivery/cache/memory.test.mjs +30 -0
- package/build/content-delivery/cache/stacked.cjs +19 -7
- package/build/content-delivery/cache/stacked.mjs +19 -7
- package/build/content-delivery/cache/type.d.ts +15 -2
- package/build/content-delivery/index.cjs +32 -1
- package/build/content-delivery/index.d.ts +2 -0
- package/build/content-delivery/index.mjs +3 -1
- package/build/content-delivery/metrics.cjs +280 -0
- package/build/content-delivery/metrics.d.ts +14 -0
- package/build/content-delivery/metrics.mjs +273 -0
- package/build/content-delivery/telemetry.cjs +52 -0
- package/build/content-delivery/telemetry.d.ts +58 -0
- package/build/content-delivery/telemetry.mjs +42 -0
- package/build/content-delivery/telemetry.test.cjs +354 -0
- package/build/content-delivery/telemetry.test.mjs +352 -0
- package/package.json +72 -76
|
@@ -3,11 +3,12 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports.allStories = void 0;
|
|
6
|
+
exports.allStories = exports.DEFAULT_NEGATIVE_TTL = void 0;
|
|
7
7
|
exports.createClient = createClient;
|
|
8
8
|
exports.story = exports.stories = exports.galleries = void 0;
|
|
9
9
|
var _sdk = require("@prezly/sdk");
|
|
10
10
|
var _RequestCoalescer = require("./RequestCoalescer.cjs");
|
|
11
|
+
var _telemetry = require("./telemetry.cjs");
|
|
11
12
|
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
12
13
|
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
13
14
|
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
|
|
@@ -15,6 +16,12 @@ function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol"
|
|
|
15
16
|
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|
16
17
|
function asyncGeneratorStep(n, t, e, r, o, a, c) { try { var i = n[a](c), u = i.value; } catch (n) { return void e(n); } i.done ? t(u) : Promise.resolve(u).then(r, o); }
|
|
17
18
|
function _asyncToGenerator(n) { return function () { var t = this, e = arguments; return new Promise(function (r, o) { var a = n.apply(t, e); function _next(n) { asyncGeneratorStep(a, r, o, _next, _throw, "next", n); } function _throw(n) { asyncGeneratorStep(a, r, o, _next, _throw, "throw", n); } _next(void 0); }); }; }
|
|
19
|
+
/**
|
|
20
|
+
* Not-found results are cached only briefly: long enough to absorb repeated
|
|
21
|
+
* requests for the same missing slug, short enough that a story published
|
|
22
|
+
* without a cache version change appears within a minute.
|
|
23
|
+
*/
|
|
24
|
+
var DEFAULT_NEGATIVE_TTL = exports.DEFAULT_NEGATIVE_TTL = 60;
|
|
18
25
|
var stories;
|
|
19
26
|
var allStories;
|
|
20
27
|
var story;
|
|
@@ -32,7 +39,8 @@ function createClient(prezly, newsroomUuid, newsroomThemeUuid) {
|
|
|
32
39
|
var _ref = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {},
|
|
33
40
|
_ref$formats = _ref.formats,
|
|
34
41
|
formats = _ref$formats === void 0 ? [_sdk.Story.FormatVersion.SLATEJS_V4] : _ref$formats,
|
|
35
|
-
cache = _ref.cache
|
|
42
|
+
cache = _ref.cache,
|
|
43
|
+
telemetry = _ref.telemetry;
|
|
36
44
|
var client = {
|
|
37
45
|
newsroom() {
|
|
38
46
|
return prezly.newsrooms.get(newsroomUuid);
|
|
@@ -288,6 +296,7 @@ function createClient(prezly, newsroomUuid, newsroomThemeUuid) {
|
|
|
288
296
|
}
|
|
289
297
|
};
|
|
290
298
|
if (cache) {
|
|
299
|
+
var _cache$negativeTtl;
|
|
291
300
|
if (cache.scope !== undefined && !cache.scope) {
|
|
292
301
|
throw new Error('Content cache scope must not be empty.');
|
|
293
302
|
}
|
|
@@ -295,12 +304,44 @@ function createClient(prezly, newsroomUuid, newsroomThemeUuid) {
|
|
|
295
304
|
// A single schema prefix keeps old readers away from scoped envelopes.
|
|
296
305
|
// Scope/version changes overwrite the same keys rather than adding namespaces.
|
|
297
306
|
var storage = cache.scope === undefined ? cache.storage : cache.storage.namespace('scoped-v1');
|
|
298
|
-
injectCache(client, storage.namespace(namespace), cache.latestVersion, cache.scope, namespace, UNCACHED_METHODS);
|
|
307
|
+
injectCache(client, storage.namespace(namespace), cache.latestVersion, cache.scope, namespace, UNCACHED_METHODS, telemetry, (_cache$negativeTtl = cache.negativeTtl) !== null && _cache$negativeTtl !== void 0 ? _cache$negativeTtl : DEFAULT_NEGATIVE_TTL);
|
|
308
|
+
} else if (telemetry) {
|
|
309
|
+
var _loop2 = function _loop2() {
|
|
310
|
+
if (UNCACHED_METHODS.includes(methodName)) return 1; // continue
|
|
311
|
+
var invoke = client[methodName].bind(client);
|
|
312
|
+
var operation = contentOperation(methodName);
|
|
313
|
+
client[methodName] = function () {
|
|
314
|
+
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
315
|
+
args[_key] = arguments[_key];
|
|
316
|
+
}
|
|
317
|
+
(0, _telemetry.emit)(telemetry, {
|
|
318
|
+
type: 'content_request',
|
|
319
|
+
operation
|
|
320
|
+
});
|
|
321
|
+
return observeOrigin(() => invoke(...args), telemetry, operation);
|
|
322
|
+
};
|
|
323
|
+
};
|
|
324
|
+
for (var methodName of Object.keys(client)) {
|
|
325
|
+
if (_loop2()) continue;
|
|
326
|
+
}
|
|
299
327
|
}
|
|
300
328
|
return client;
|
|
301
329
|
}
|
|
330
|
+
function readScopedValue(stored, scope) {
|
|
331
|
+
if ((stored === null || stored === void 0 ? void 0 : stored.scope) !== scope) return undefined;
|
|
332
|
+
if (stored.value === null) {
|
|
333
|
+
var fresh = typeof stored.expires === 'number' && stored.expires > Date.now() / 1000;
|
|
334
|
+
return fresh ? null : undefined;
|
|
335
|
+
}
|
|
336
|
+
return stored.value;
|
|
337
|
+
}
|
|
302
338
|
function injectCache(client, cache, latestVersion, scope, namespace) {
|
|
303
339
|
var uncachedMethods = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : [];
|
|
340
|
+
var telemetry = arguments.length > 6 ? arguments[6] : undefined;
|
|
341
|
+
var negativeTtl = arguments.length > 7 && arguments[7] !== undefined ? arguments[7] : DEFAULT_NEGATIVE_TTL;
|
|
342
|
+
if (!Number.isInteger(negativeTtl) || negativeTtl < 1) {
|
|
343
|
+
throw new RangeError('The negative cache TTL must be a positive integer number of seconds.');
|
|
344
|
+
}
|
|
304
345
|
// An opaque SDK client does not expose its credentials/source. Callers without
|
|
305
346
|
// an explicit scope keep request-local sharing; the Next.js adapter supplies one.
|
|
306
347
|
var requests = scope === undefined ? new _RequestCoalescer.RequestCoalescer() : _RequestCoalescer.sharedContentRequests;
|
|
@@ -308,44 +349,123 @@ function injectCache(client, cache, latestVersion, scope, namespace) {
|
|
|
308
349
|
methodNames.forEach(methodName => {
|
|
309
350
|
if (uncachedMethods.includes(methodName)) return;
|
|
310
351
|
var uncachedFn = client[methodName].bind(client);
|
|
352
|
+
var operation = contentOperation(methodName);
|
|
311
353
|
client[methodName] = /*#__PURE__*/_asyncToGenerator(function* () {
|
|
312
|
-
for (var
|
|
313
|
-
args[
|
|
354
|
+
for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
|
|
355
|
+
args[_key2] = arguments[_key2];
|
|
314
356
|
}
|
|
357
|
+
(0, _telemetry.emit)(telemetry, {
|
|
358
|
+
type: 'content_request',
|
|
359
|
+
operation
|
|
360
|
+
});
|
|
315
361
|
// Preserve existing argument serialization; query canonicalization is separate.
|
|
316
362
|
var cacheKey = "".concat(methodName, ":").concat(JSON.stringify(args));
|
|
317
363
|
var dedupeKey = JSON.stringify([scope, namespace, latestVersion, cacheKey]);
|
|
318
364
|
return requests.run(dedupeKey, /*#__PURE__*/_asyncToGenerator(function* () {
|
|
319
365
|
try {
|
|
320
|
-
var
|
|
321
|
-
var
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
366
|
+
var layer = 'custom';
|
|
367
|
+
var _stored = telemetry ? yield cache.get(cacheKey, latestVersion, source => {
|
|
368
|
+
layer = source === 'memory' || source === 'redis' ? source : 'custom';
|
|
369
|
+
}) : yield cache.get(cacheKey, latestVersion);
|
|
370
|
+
// Only an absent entry is a miss: a stored `false` or `0` is a valid
|
|
371
|
+
// result. A stored `null` (not found) is a hit only inside its own
|
|
372
|
+
// deadline, which lives in the scoped envelope. Unscoped clients
|
|
373
|
+
// cannot carry that deadline and keep treating null as a miss.
|
|
374
|
+
var cached = scope === undefined ? _stored === null ? undefined : _stored : readScopedValue(_stored, scope);
|
|
375
|
+
if (cached !== undefined) {
|
|
376
|
+
(0, _telemetry.emit)(telemetry, {
|
|
377
|
+
type: 'cache_hit',
|
|
378
|
+
operation,
|
|
379
|
+
layer
|
|
380
|
+
});
|
|
381
|
+
return {
|
|
382
|
+
value: cached
|
|
383
|
+
};
|
|
384
|
+
}
|
|
327
385
|
} catch (_unused) {
|
|
386
|
+
(0, _telemetry.emit)(telemetry, {
|
|
387
|
+
type: 'cache_error',
|
|
388
|
+
operation,
|
|
389
|
+
action: 'read'
|
|
390
|
+
});
|
|
328
391
|
// A cache failure must not prevent a bounded origin fallback.
|
|
329
392
|
}
|
|
330
|
-
|
|
331
|
-
|
|
393
|
+
(0, _telemetry.emit)(telemetry, {
|
|
394
|
+
type: 'cache_miss',
|
|
395
|
+
operation
|
|
396
|
+
});
|
|
397
|
+
var value = yield observeOrigin(() => uncachedFn(...args), telemetry, operation);
|
|
398
|
+
// `null` means the API answered 403/404/410 for this lookup. Keep it
|
|
399
|
+
// only briefly, with the deadline in the envelope and a matching
|
|
400
|
+
// layer hint. Errors (401, 429, 5xx, transport) throw above and are
|
|
401
|
+
// never stored. `undefined` results are not stored either.
|
|
402
|
+
var negative = value === null && scope !== undefined;
|
|
403
|
+
var stored = scope === undefined ? value : negative ? {
|
|
404
|
+
scope,
|
|
405
|
+
value,
|
|
406
|
+
// Fractional seconds: flooring would shorten a 1-second
|
|
407
|
+
// retention to almost nothing.
|
|
408
|
+
expires: Date.now() / 1000 + negativeTtl
|
|
409
|
+
} : {
|
|
332
410
|
scope,
|
|
333
411
|
value
|
|
334
412
|
};
|
|
413
|
+
var options = negative ? {
|
|
414
|
+
ttl: negativeTtl
|
|
415
|
+
} : undefined;
|
|
335
416
|
// Observe synchronous and asynchronous write failures without holding
|
|
336
417
|
// up a successful response or leaving an unhandled rejection.
|
|
337
|
-
var cacheWrite = Promise.resolve().then(() => cache.set(cacheKey, stored, latestVersion)).catch(() =>
|
|
418
|
+
var cacheWrite = value === undefined || value === null && scope === undefined ? undefined : Promise.resolve().then(() => cache.set(cacheKey, stored, latestVersion, options)).catch(() => {
|
|
419
|
+
(0, _telemetry.emit)(telemetry, {
|
|
420
|
+
type: 'cache_error',
|
|
421
|
+
operation,
|
|
422
|
+
action: 'write'
|
|
423
|
+
});
|
|
424
|
+
});
|
|
338
425
|
return {
|
|
339
426
|
value,
|
|
340
427
|
cacheWrite
|
|
341
428
|
};
|
|
342
|
-
}))
|
|
429
|
+
}), telemetry ? event => (0, _telemetry.emit)(telemetry, {
|
|
430
|
+
type: event === 'coalesced' ? 'coalesced' : 'content_rejected',
|
|
431
|
+
operation
|
|
432
|
+
}) : undefined);
|
|
343
433
|
});
|
|
344
434
|
});
|
|
345
435
|
}
|
|
436
|
+
function contentOperation(name) {
|
|
437
|
+
return _telemetry.CONTENT_OPERATIONS.includes(name) ? name : 'other';
|
|
438
|
+
}
|
|
439
|
+
function observeOrigin(invoke, telemetry, operation) {
|
|
440
|
+
if (!telemetry) return invoke();
|
|
441
|
+
var start = (0, _telemetry.telemetryNow)();
|
|
442
|
+
var complete = outcome => (0, _telemetry.emit)(telemetry, {
|
|
443
|
+
type: 'origin',
|
|
444
|
+
operation,
|
|
445
|
+
outcome,
|
|
446
|
+
seconds: (0, _telemetry.elapsedSeconds)(start)
|
|
447
|
+
});
|
|
448
|
+
try {
|
|
449
|
+
var value = invoke();
|
|
450
|
+
if (value && typeof value.then === 'function') {
|
|
451
|
+
return Promise.resolve(value).then(result => {
|
|
452
|
+
complete('success');
|
|
453
|
+
return result;
|
|
454
|
+
}, error => {
|
|
455
|
+
complete('error');
|
|
456
|
+
throw error;
|
|
457
|
+
});
|
|
458
|
+
}
|
|
459
|
+
complete('success');
|
|
460
|
+
return value;
|
|
461
|
+
} catch (error) {
|
|
462
|
+
complete('error');
|
|
463
|
+
throw error;
|
|
464
|
+
}
|
|
465
|
+
}
|
|
346
466
|
function mergeQueries() {
|
|
347
|
-
for (var
|
|
348
|
-
queries[
|
|
467
|
+
for (var _len3 = arguments.length, queries = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
|
|
468
|
+
queries[_key3] = arguments[_key3];
|
|
349
469
|
}
|
|
350
470
|
var queryObjects = queries.filter(query => Boolean(query)).map(query => {
|
|
351
471
|
if (typeof query === 'string') {
|
|
@@ -1,15 +1,29 @@
|
|
|
1
1
|
import type { Culture, Newsroom, NewsroomTheme, PrezlyClient, Query, TranslatedCategory } from '@prezly/sdk';
|
|
2
2
|
import { Category, NewsroomGallery, Stories, Story } from '@prezly/sdk';
|
|
3
3
|
import type { Cache, UnixTimestampInSeconds } from './cache';
|
|
4
|
+
import { type Telemetry } from './telemetry';
|
|
4
5
|
export interface Options {
|
|
5
6
|
formats?: Story.FormatVersion[];
|
|
7
|
+
telemetry?: Telemetry;
|
|
6
8
|
cache?: {
|
|
7
9
|
storage: Cache;
|
|
8
10
|
latestVersion: UnixTimestampInSeconds;
|
|
9
11
|
/** Stable source/authorization identity. Enables cross-client request sharing. */
|
|
10
12
|
scope?: string;
|
|
13
|
+
/**
|
|
14
|
+
* Retention in seconds for `null` results, i.e. content the API reported as
|
|
15
|
+
* not found, gone or forbidden. Defaults to `DEFAULT_NEGATIVE_TTL`.
|
|
16
|
+
* A cache version change still invalidates them immediately.
|
|
17
|
+
*/
|
|
18
|
+
negativeTtl?: number;
|
|
11
19
|
};
|
|
12
20
|
}
|
|
21
|
+
/**
|
|
22
|
+
* Not-found results are cached only briefly: long enough to absorb repeated
|
|
23
|
+
* requests for the same missing slug, short enough that a story published
|
|
24
|
+
* without a cache version change appears within a minute.
|
|
25
|
+
*/
|
|
26
|
+
export declare const DEFAULT_NEGATIVE_TTL = 60;
|
|
13
27
|
export declare namespace stories {
|
|
14
28
|
interface SearchParams {
|
|
15
29
|
search?: string;
|
|
@@ -55,7 +69,7 @@ export declare namespace galleries {
|
|
|
55
69
|
}
|
|
56
70
|
}
|
|
57
71
|
export type Client = ReturnType<typeof createClient>;
|
|
58
|
-
export declare function createClient(prezly: PrezlyClient, newsroomUuid: Newsroom['uuid'], newsroomThemeUuid: NewsroomTheme['id'] | undefined, { formats, cache }?: Options): {
|
|
72
|
+
export declare function createClient(prezly: PrezlyClient, newsroomUuid: Newsroom['uuid'], newsroomThemeUuid: NewsroomTheme['id'] | undefined, { formats, cache, telemetry }?: Options): {
|
|
59
73
|
newsroom(): Promise<Newsroom>;
|
|
60
74
|
theme(): Promise<import("@prezly/sdk").NewsroomThemePreset> | undefined;
|
|
61
75
|
themeSettings(): Promise<Record<string, any> | undefined>;
|
|
@@ -7,6 +7,13 @@ function asyncGeneratorStep(n, t, e, r, o, a, c) { try { var i = n[a](c), u = i.
|
|
|
7
7
|
function _asyncToGenerator(n) { return function () { var t = this, e = arguments; return new Promise(function (r, o) { var a = n.apply(t, e); function _next(n) { asyncGeneratorStep(a, r, o, _next, _throw, "next", n); } function _throw(n) { asyncGeneratorStep(a, r, o, _next, _throw, "throw", n); } _next(void 0); }); }; }
|
|
8
8
|
import { ApiError, Category, NewsroomGallery, SortOrder, Stories, Story } from '@prezly/sdk';
|
|
9
9
|
import { RequestCoalescer, sharedContentRequests } from "./RequestCoalescer.mjs";
|
|
10
|
+
import { CONTENT_OPERATIONS, emit, elapsedSeconds, telemetryNow } from "./telemetry.mjs";
|
|
11
|
+
/**
|
|
12
|
+
* Not-found results are cached only briefly: long enough to absorb repeated
|
|
13
|
+
* requests for the same missing slug, short enough that a story published
|
|
14
|
+
* without a cache version change appears within a minute.
|
|
15
|
+
*/
|
|
16
|
+
export var DEFAULT_NEGATIVE_TTL = 60;
|
|
10
17
|
export var stories;
|
|
11
18
|
export var allStories;
|
|
12
19
|
export var story;
|
|
@@ -24,7 +31,8 @@ export function createClient(prezly, newsroomUuid, newsroomThemeUuid) {
|
|
|
24
31
|
var _ref = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {},
|
|
25
32
|
_ref$formats = _ref.formats,
|
|
26
33
|
formats = _ref$formats === void 0 ? [Story.FormatVersion.SLATEJS_V4] : _ref$formats,
|
|
27
|
-
cache = _ref.cache
|
|
34
|
+
cache = _ref.cache,
|
|
35
|
+
telemetry = _ref.telemetry;
|
|
28
36
|
var client = {
|
|
29
37
|
newsroom() {
|
|
30
38
|
return prezly.newsrooms.get(newsroomUuid);
|
|
@@ -280,6 +288,7 @@ export function createClient(prezly, newsroomUuid, newsroomThemeUuid) {
|
|
|
280
288
|
}
|
|
281
289
|
};
|
|
282
290
|
if (cache) {
|
|
291
|
+
var _cache$negativeTtl;
|
|
283
292
|
if (cache.scope !== undefined && !cache.scope) {
|
|
284
293
|
throw new Error('Content cache scope must not be empty.');
|
|
285
294
|
}
|
|
@@ -287,12 +296,44 @@ export function createClient(prezly, newsroomUuid, newsroomThemeUuid) {
|
|
|
287
296
|
// A single schema prefix keeps old readers away from scoped envelopes.
|
|
288
297
|
// Scope/version changes overwrite the same keys rather than adding namespaces.
|
|
289
298
|
var storage = cache.scope === undefined ? cache.storage : cache.storage.namespace('scoped-v1');
|
|
290
|
-
injectCache(client, storage.namespace(namespace), cache.latestVersion, cache.scope, namespace, UNCACHED_METHODS);
|
|
299
|
+
injectCache(client, storage.namespace(namespace), cache.latestVersion, cache.scope, namespace, UNCACHED_METHODS, telemetry, (_cache$negativeTtl = cache.negativeTtl) !== null && _cache$negativeTtl !== void 0 ? _cache$negativeTtl : DEFAULT_NEGATIVE_TTL);
|
|
300
|
+
} else if (telemetry) {
|
|
301
|
+
var _loop2 = function _loop2() {
|
|
302
|
+
if (UNCACHED_METHODS.includes(methodName)) return 1; // continue
|
|
303
|
+
var invoke = client[methodName].bind(client);
|
|
304
|
+
var operation = contentOperation(methodName);
|
|
305
|
+
client[methodName] = function () {
|
|
306
|
+
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
307
|
+
args[_key] = arguments[_key];
|
|
308
|
+
}
|
|
309
|
+
emit(telemetry, {
|
|
310
|
+
type: 'content_request',
|
|
311
|
+
operation
|
|
312
|
+
});
|
|
313
|
+
return observeOrigin(() => invoke(...args), telemetry, operation);
|
|
314
|
+
};
|
|
315
|
+
};
|
|
316
|
+
for (var methodName of Object.keys(client)) {
|
|
317
|
+
if (_loop2()) continue;
|
|
318
|
+
}
|
|
291
319
|
}
|
|
292
320
|
return client;
|
|
293
321
|
}
|
|
322
|
+
function readScopedValue(stored, scope) {
|
|
323
|
+
if ((stored === null || stored === void 0 ? void 0 : stored.scope) !== scope) return undefined;
|
|
324
|
+
if (stored.value === null) {
|
|
325
|
+
var fresh = typeof stored.expires === 'number' && stored.expires > Date.now() / 1000;
|
|
326
|
+
return fresh ? null : undefined;
|
|
327
|
+
}
|
|
328
|
+
return stored.value;
|
|
329
|
+
}
|
|
294
330
|
function injectCache(client, cache, latestVersion, scope, namespace) {
|
|
295
331
|
var uncachedMethods = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : [];
|
|
332
|
+
var telemetry = arguments.length > 6 ? arguments[6] : undefined;
|
|
333
|
+
var negativeTtl = arguments.length > 7 && arguments[7] !== undefined ? arguments[7] : DEFAULT_NEGATIVE_TTL;
|
|
334
|
+
if (!Number.isInteger(negativeTtl) || negativeTtl < 1) {
|
|
335
|
+
throw new RangeError('The negative cache TTL must be a positive integer number of seconds.');
|
|
336
|
+
}
|
|
296
337
|
// An opaque SDK client does not expose its credentials/source. Callers without
|
|
297
338
|
// an explicit scope keep request-local sharing; the Next.js adapter supplies one.
|
|
298
339
|
var requests = scope === undefined ? new RequestCoalescer() : sharedContentRequests;
|
|
@@ -300,44 +341,123 @@ function injectCache(client, cache, latestVersion, scope, namespace) {
|
|
|
300
341
|
methodNames.forEach(methodName => {
|
|
301
342
|
if (uncachedMethods.includes(methodName)) return;
|
|
302
343
|
var uncachedFn = client[methodName].bind(client);
|
|
344
|
+
var operation = contentOperation(methodName);
|
|
303
345
|
client[methodName] = /*#__PURE__*/_asyncToGenerator(function* () {
|
|
304
|
-
for (var
|
|
305
|
-
args[
|
|
346
|
+
for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
|
|
347
|
+
args[_key2] = arguments[_key2];
|
|
306
348
|
}
|
|
349
|
+
emit(telemetry, {
|
|
350
|
+
type: 'content_request',
|
|
351
|
+
operation
|
|
352
|
+
});
|
|
307
353
|
// Preserve existing argument serialization; query canonicalization is separate.
|
|
308
354
|
var cacheKey = "".concat(methodName, ":").concat(JSON.stringify(args));
|
|
309
355
|
var dedupeKey = JSON.stringify([scope, namespace, latestVersion, cacheKey]);
|
|
310
356
|
return requests.run(dedupeKey, /*#__PURE__*/_asyncToGenerator(function* () {
|
|
311
357
|
try {
|
|
312
|
-
var
|
|
313
|
-
var
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
358
|
+
var layer = 'custom';
|
|
359
|
+
var _stored = telemetry ? yield cache.get(cacheKey, latestVersion, source => {
|
|
360
|
+
layer = source === 'memory' || source === 'redis' ? source : 'custom';
|
|
361
|
+
}) : yield cache.get(cacheKey, latestVersion);
|
|
362
|
+
// Only an absent entry is a miss: a stored `false` or `0` is a valid
|
|
363
|
+
// result. A stored `null` (not found) is a hit only inside its own
|
|
364
|
+
// deadline, which lives in the scoped envelope. Unscoped clients
|
|
365
|
+
// cannot carry that deadline and keep treating null as a miss.
|
|
366
|
+
var cached = scope === undefined ? _stored === null ? undefined : _stored : readScopedValue(_stored, scope);
|
|
367
|
+
if (cached !== undefined) {
|
|
368
|
+
emit(telemetry, {
|
|
369
|
+
type: 'cache_hit',
|
|
370
|
+
operation,
|
|
371
|
+
layer
|
|
372
|
+
});
|
|
373
|
+
return {
|
|
374
|
+
value: cached
|
|
375
|
+
};
|
|
376
|
+
}
|
|
319
377
|
} catch (_unused) {
|
|
378
|
+
emit(telemetry, {
|
|
379
|
+
type: 'cache_error',
|
|
380
|
+
operation,
|
|
381
|
+
action: 'read'
|
|
382
|
+
});
|
|
320
383
|
// A cache failure must not prevent a bounded origin fallback.
|
|
321
384
|
}
|
|
322
|
-
|
|
323
|
-
|
|
385
|
+
emit(telemetry, {
|
|
386
|
+
type: 'cache_miss',
|
|
387
|
+
operation
|
|
388
|
+
});
|
|
389
|
+
var value = yield observeOrigin(() => uncachedFn(...args), telemetry, operation);
|
|
390
|
+
// `null` means the API answered 403/404/410 for this lookup. Keep it
|
|
391
|
+
// only briefly, with the deadline in the envelope and a matching
|
|
392
|
+
// layer hint. Errors (401, 429, 5xx, transport) throw above and are
|
|
393
|
+
// never stored. `undefined` results are not stored either.
|
|
394
|
+
var negative = value === null && scope !== undefined;
|
|
395
|
+
var stored = scope === undefined ? value : negative ? {
|
|
396
|
+
scope,
|
|
397
|
+
value,
|
|
398
|
+
// Fractional seconds: flooring would shorten a 1-second
|
|
399
|
+
// retention to almost nothing.
|
|
400
|
+
expires: Date.now() / 1000 + negativeTtl
|
|
401
|
+
} : {
|
|
324
402
|
scope,
|
|
325
403
|
value
|
|
326
404
|
};
|
|
405
|
+
var options = negative ? {
|
|
406
|
+
ttl: negativeTtl
|
|
407
|
+
} : undefined;
|
|
327
408
|
// Observe synchronous and asynchronous write failures without holding
|
|
328
409
|
// up a successful response or leaving an unhandled rejection.
|
|
329
|
-
var cacheWrite = Promise.resolve().then(() => cache.set(cacheKey, stored, latestVersion)).catch(() =>
|
|
410
|
+
var cacheWrite = value === undefined || value === null && scope === undefined ? undefined : Promise.resolve().then(() => cache.set(cacheKey, stored, latestVersion, options)).catch(() => {
|
|
411
|
+
emit(telemetry, {
|
|
412
|
+
type: 'cache_error',
|
|
413
|
+
operation,
|
|
414
|
+
action: 'write'
|
|
415
|
+
});
|
|
416
|
+
});
|
|
330
417
|
return {
|
|
331
418
|
value,
|
|
332
419
|
cacheWrite
|
|
333
420
|
};
|
|
334
|
-
})
|
|
421
|
+
}), telemetry ? event => emit(telemetry, {
|
|
422
|
+
type: event === 'coalesced' ? 'coalesced' : 'content_rejected',
|
|
423
|
+
operation
|
|
424
|
+
}) : undefined);
|
|
335
425
|
});
|
|
336
426
|
});
|
|
337
427
|
}
|
|
428
|
+
function contentOperation(name) {
|
|
429
|
+
return CONTENT_OPERATIONS.includes(name) ? name : 'other';
|
|
430
|
+
}
|
|
431
|
+
function observeOrigin(invoke, telemetry, operation) {
|
|
432
|
+
if (!telemetry) return invoke();
|
|
433
|
+
var start = telemetryNow();
|
|
434
|
+
var complete = outcome => emit(telemetry, {
|
|
435
|
+
type: 'origin',
|
|
436
|
+
operation,
|
|
437
|
+
outcome,
|
|
438
|
+
seconds: elapsedSeconds(start)
|
|
439
|
+
});
|
|
440
|
+
try {
|
|
441
|
+
var value = invoke();
|
|
442
|
+
if (value && typeof value.then === 'function') {
|
|
443
|
+
return Promise.resolve(value).then(result => {
|
|
444
|
+
complete('success');
|
|
445
|
+
return result;
|
|
446
|
+
}, error => {
|
|
447
|
+
complete('error');
|
|
448
|
+
throw error;
|
|
449
|
+
});
|
|
450
|
+
}
|
|
451
|
+
complete('success');
|
|
452
|
+
return value;
|
|
453
|
+
} catch (error) {
|
|
454
|
+
complete('error');
|
|
455
|
+
throw error;
|
|
456
|
+
}
|
|
457
|
+
}
|
|
338
458
|
function mergeQueries() {
|
|
339
|
-
for (var
|
|
340
|
-
queries[
|
|
459
|
+
for (var _len3 = arguments.length, queries = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
|
|
460
|
+
queries[_key3] = arguments[_key3];
|
|
341
461
|
}
|
|
342
462
|
var queryObjects = queries.filter(query => Boolean(query)).map(query => {
|
|
343
463
|
if (typeof query === 'string') {
|