nucleus-core-ts 0.10.253 → 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.253
1
+ 0.10.254
@@ -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,7 +25,7 @@ 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)) ?? inFlight(queue.entries, payload, clock) ?? 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 [
@@ -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.253",
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",