nucleus-core-ts 0.10.252 → 0.10.254

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/dist/.build-ok CHANGED
@@ -1 +1 @@
1
- 0.10.252
1
+ 0.10.254
@@ -32,3 +32,11 @@ export declare const DEFAULT_READ_KEYS: readonly string[];
32
32
  * replay has made that write; one recorded with a time only once the playhead has reached it.
33
33
  */
34
34
  export declare function nextEntry(queue: Queue, payload: string | null, clock: Clock): Entry | undefined;
35
+ /**
36
+ * How far ahead an answer for the SAME payload is served instead of another payload's answer already due. `at` is
37
+ * when an answer ARRIVED, so a call the page makes now was in flight in the recording too: ACE asked the skills list
38
+ * for scope "mine" (answered empty at 964 ms), then "public" (answered at 1357 ms). Handing the due "mine" answer to
39
+ * the "public" call told the replayed page the skill was missing, and a warning the recording never showed came up.
40
+ * The bound keeps a much later answer of that payload (a state the page reached later) out.
41
+ */
42
+ export declare const IN_FLIGHT_MS = 60000;
@@ -1,3 +1,4 @@
1
+ import { nearest } from './nearest';
1
2
  /** Endpoints that only read, besides every `GET_` key. Any other key is a write the visitor's actions trigger. */ export const DEFAULT_READ_KEYS = [
2
3
  'ME',
3
4
  'NOTIFICATION_UNSEEN_COUNT'
@@ -24,13 +25,23 @@ function seen(entry, clock) {
24
25
  */ export function nextEntry(queue, payload, clock) {
25
26
  const floor = queue.last?.at === undefined ? queue.last?.epoch ?? 0 : 0;
26
27
  const ready = queue.entries.filter((entry)=>due(entry, clock, floor));
27
- const fresh = latest(ready.filter((entry)=>entry.payload === payload)) ?? latest(ready);
28
+ const fresh = latest(ready.filter((entry)=>entry.payload === payload)) ?? inFlight(queue.entries, payload, clock) ?? latest(nearest(ready, payload));
28
29
  if (fresh) return fresh;
29
30
  const past = queue.entries.filter((entry)=>seen(entry, clock));
30
31
  return [
31
32
  ...past
32
33
  ].reverse().find((entry)=>entry.payload === payload) ?? queue.last ?? past.at(-1) ?? queue.entries.find((entry)=>entry.at === undefined) ?? earliestAhead(queue.entries, payload, clock);
33
34
  }
35
+ /**
36
+ * How far ahead an answer for the SAME payload is served instead of another payload's answer already due. `at` is
37
+ * when an answer ARRIVED, so a call the page makes now was in flight in the recording too: ACE asked the skills list
38
+ * for scope "mine" (answered empty at 964 ms), then "public" (answered at 1357 ms). Handing the due "mine" answer to
39
+ * the "public" call told the replayed page the skill was missing, and a warning the recording never showed came up.
40
+ * The bound keeps a much later answer of that payload (a state the page reached later) out.
41
+ */ export const IN_FLIGHT_MS = 60000;
42
+ function inFlight(entries, payload, clock) {
43
+ return entries.filter((entry)=>entry.payload === payload && entry.at !== undefined && entry.at > clock.playhead).filter((entry)=>(entry.at ?? 0) - clock.playhead <= IN_FLIGHT_MS && !(entry.write && entry.served)).sort((a, b)=>(a.at ?? 0) - (b.at ?? 0))[0];
44
+ }
34
45
  /**
35
46
  * A call made before the first recorded answer of its key: the recorded call was made before its answer arrived too
36
47
  * (`at` is when the answer came), so a page that asks at load gets the first answer ahead, the one for the same payload
@@ -0,0 +1,13 @@
1
+ /**
2
+ * The recorded answers whose payload is CLOSEST to the live one, for a call no answer was recorded for exactly.
3
+ *
4
+ * A page changes its requests after a recording is made: the ACE model catalogue asked `page_size: 100` when it was
5
+ * recorded (pages 1 and 2) and `page_size: 500` a week later. With no exact match the replay used to serve the latest
6
+ * answer of the key, page 2, and the replayed picker showed page 2's first row instead of the pinned default on page 1.
7
+ * Closest is the most top-level fields with the same value (`model_category`, `page`, `available_only` for page 1).
8
+ * When no answer shares a field (or the payloads are not objects) every answer is as close as any other, and the
9
+ * caller's own order decides, as before.
10
+ */
11
+ export declare function nearest<T extends {
12
+ payload: string | null;
13
+ }>(entries: readonly T[], payload: string | null): T[];
@@ -0,0 +1,51 @@
1
+ /**
2
+ * The recorded answers whose payload is CLOSEST to the live one, for a call no answer was recorded for exactly.
3
+ *
4
+ * A page changes its requests after a recording is made: the ACE model catalogue asked `page_size: 100` when it was
5
+ * recorded (pages 1 and 2) and `page_size: 500` a week later. With no exact match the replay used to serve the latest
6
+ * answer of the key, page 2, and the replayed picker showed page 2's first row instead of the pinned default on page 1.
7
+ * Closest is the most top-level fields with the same value (`model_category`, `page`, `available_only` for page 1).
8
+ * When no answer shares a field (or the payloads are not objects) every answer is as close as any other, and the
9
+ * caller's own order decides, as before.
10
+ */ export function nearest(entries, payload) {
11
+ const live = fieldsOf(payload);
12
+ if (live.size === 0 || entries.length === 0) return [
13
+ ...entries
14
+ ];
15
+ const scored = entries.map((entry)=>({
16
+ entry,
17
+ score: shared(live, fieldsOf(entry.payload))
18
+ }));
19
+ const best = Math.max(...scored.map(({ score })=>score));
20
+ if (best === 0) return [
21
+ ...entries
22
+ ];
23
+ return scored.filter(({ score })=>score === best).map(({ entry })=>entry);
24
+ }
25
+ const parsed = new Map();
26
+ function fieldsOf(text) {
27
+ if (text === null) return new Map();
28
+ const known = parsed.get(text);
29
+ if (known) return known;
30
+ const fields = new Map();
31
+ const value = readJson(text);
32
+ if (value !== null && typeof value === 'object' && !Array.isArray(value)) {
33
+ for (const [key, field] of Object.entries(value))fields.set(key, JSON.stringify(field));
34
+ }
35
+ if (parsed.size > 2000) parsed.clear();
36
+ parsed.set(text, fields);
37
+ return fields;
38
+ }
39
+ function readJson(text) {
40
+ try {
41
+ const value = JSON.parse(text);
42
+ return value;
43
+ } catch {
44
+ return null;
45
+ }
46
+ }
47
+ function shared(a, b) {
48
+ let count = 0;
49
+ for (const [key, value] of a)if (b.get(key) === value) count += 1;
50
+ return count;
51
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nucleus-core-ts",
3
- "version": "0.10.252",
3
+ "version": "0.10.254",
4
4
  "description": "Production-ready, enterprise-grade TypeScript framework for building multi-tenant APIs",
5
5
  "author": "Hidayet Can Özcan <hidayetcan@gmail.com>",
6
6
  "license": "SEE LICENSE IN LICENSE",