@dejadex/web 0.0.0-stage → 0.1.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.
- package/CHANGELOG.md +35 -0
- package/README.md +222 -2
- package/dist/errors.d.ts +8 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +9 -0
- package/dist/errors.js.map +1 -0
- package/dist/geometry/homography.d.ts +75 -0
- package/dist/geometry/homography.d.ts.map +1 -0
- package/dist/geometry/homography.js +146 -0
- package/dist/geometry/homography.js.map +1 -0
- package/dist/identify/assets.d.ts +39 -0
- package/dist/identify/assets.d.ts.map +1 -0
- package/dist/identify/assets.js +167 -0
- package/dist/identify/assets.js.map +1 -0
- package/dist/identify/decode.d.ts +6 -0
- package/dist/identify/decode.d.ts.map +1 -0
- package/dist/identify/decode.js +53 -0
- package/dist/identify/decode.js.map +1 -0
- package/dist/identify/engine.d.ts +32 -0
- package/dist/identify/engine.d.ts.map +1 -0
- package/dist/identify/engine.js +127 -0
- package/dist/identify/engine.js.map +1 -0
- package/dist/identify/identifier.d.ts +91 -0
- package/dist/identify/identifier.d.ts.map +1 -0
- package/dist/identify/identifier.js +332 -0
- package/dist/identify/identifier.js.map +1 -0
- package/dist/identify/manifest.d.ts +60 -0
- package/dist/identify/manifest.d.ts.map +1 -0
- package/dist/identify/manifest.js +211 -0
- package/dist/identify/manifest.js.map +1 -0
- package/dist/identify/preprocess.d.ts +52 -0
- package/dist/identify/preprocess.d.ts.map +1 -0
- package/dist/identify/preprocess.js +195 -0
- package/dist/identify/preprocess.js.map +1 -0
- package/dist/identify/router.d.ts +60 -0
- package/dist/identify/router.d.ts.map +1 -0
- package/dist/identify/router.js +83 -0
- package/dist/identify/router.js.map +1 -0
- package/dist/identify/runtime.d.ts +69 -0
- package/dist/identify/runtime.d.ts.map +1 -0
- package/dist/identify/runtime.js +140 -0
- package/dist/identify/runtime.js.map +1 -0
- package/dist/identify/search.d.ts +35 -0
- package/dist/identify/search.d.ts.map +1 -0
- package/dist/identify/search.js +99 -0
- package/dist/identify/search.js.map +1 -0
- package/dist/identify/server.d.ts +60 -0
- package/dist/identify/server.d.ts.map +1 -0
- package/dist/identify/server.js +149 -0
- package/dist/identify/server.js.map +1 -0
- package/dist/identify/shard.d.ts +31 -0
- package/dist/identify/shard.d.ts.map +1 -0
- package/dist/identify/shard.js +146 -0
- package/dist/identify/shard.js.map +1 -0
- package/dist/identify/types.d.ts +107 -0
- package/dist/identify/types.d.ts.map +1 -0
- package/dist/identify/types.js +7 -0
- package/dist/identify/types.js.map +1 -0
- package/dist/identify/worker.d.ts +2 -0
- package/dist/identify/worker.d.ts.map +1 -0
- package/dist/identify/worker.js +13 -0
- package/dist/identify/worker.js.map +1 -0
- package/dist/identify/workerProtocol.d.ts +54 -0
- package/dist/identify/workerProtocol.d.ts.map +1 -0
- package/dist/identify/workerProtocol.js +113 -0
- package/dist/identify/workerProtocol.js.map +1 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +16 -0
- package/dist/index.js.map +1 -0
- package/dist/lease/verify.d.ts +69 -0
- package/dist/lease/verify.d.ts.map +1 -0
- package/dist/lease/verify.js +235 -0
- package/dist/lease/verify.js.map +1 -0
- package/dist/scanner.d.ts +10 -0
- package/dist/scanner.d.ts.map +1 -0
- package/dist/scanner.js +58 -0
- package/dist/scanner.js.map +1 -0
- package/dist/types.d.ts +180 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/usage/meter.d.ts +83 -0
- package/dist/usage/meter.d.ts.map +1 -0
- package/dist/usage/meter.js +111 -0
- package/dist/usage/meter.js.map +1 -0
- package/dist/usage/store.d.ts +40 -0
- package/dist/usage/store.d.ts.map +1 -0
- package/dist/usage/store.js +248 -0
- package/dist/usage/store.js.map +1 -0
- package/package.json +64 -4
- package/src/errors.ts +24 -0
- package/src/geometry/homography.ts +178 -0
- package/src/identify/assets.ts +189 -0
- package/src/identify/decode.ts +54 -0
- package/src/identify/engine.ts +167 -0
- package/src/identify/identifier.ts +431 -0
- package/src/identify/manifest.ts +240 -0
- package/src/identify/preprocess.ts +227 -0
- package/src/identify/router.ts +115 -0
- package/src/identify/runtime.ts +197 -0
- package/src/identify/search.ts +115 -0
- package/src/identify/server.ts +167 -0
- package/src/identify/shard.ts +154 -0
- package/src/identify/types.ts +122 -0
- package/src/identify/worker.ts +13 -0
- package/src/identify/workerProtocol.ts +136 -0
- package/src/index.ts +71 -0
- package/src/lease/verify.ts +252 -0
- package/src/scanner.ts +56 -0
- package/src/types.ts +196 -0
- package/src/usage/meter.ts +165 -0
- package/src/usage/store.ts +282 -0
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { randomId } from './store.js';
|
|
2
|
+
/** Reports per batch handed to the host. */
|
|
3
|
+
export const MAX_BATCH_REPORTS = 500;
|
|
4
|
+
/** Sessions whose seen cards are remembered at once; the oldest is forgotten first. */
|
|
5
|
+
const MAX_SESSIONS = 32;
|
|
6
|
+
export class UsageMeter {
|
|
7
|
+
store;
|
|
8
|
+
now;
|
|
9
|
+
newId;
|
|
10
|
+
/** session → card ids already charged (by either side) in it. */
|
|
11
|
+
seen = new Map();
|
|
12
|
+
/** lease id → cards charged under it on this page (device + service). */
|
|
13
|
+
used = new Map();
|
|
14
|
+
chain = Promise.resolve();
|
|
15
|
+
constructor(options) {
|
|
16
|
+
this.store = options.store;
|
|
17
|
+
this.now = options.now ?? (() => Date.now());
|
|
18
|
+
this.newId = options.newId ?? (() => (typeof crypto.randomUUID === 'function' ? crypto.randomUUID() : randomId()));
|
|
19
|
+
}
|
|
20
|
+
/** Cards charged under `leaseId` on this page so far. */
|
|
21
|
+
usedUnder(leaseId) {
|
|
22
|
+
return this.used.get(leaseId) ?? 0;
|
|
23
|
+
}
|
|
24
|
+
/** Whether the lease's allowance is used up. */
|
|
25
|
+
exhausted(lease) {
|
|
26
|
+
return this.usedUnder(lease.jti) >= lease.allowance;
|
|
27
|
+
}
|
|
28
|
+
/** Tallies one call's answers; resolves once the tally is stored. */
|
|
29
|
+
record(input) {
|
|
30
|
+
return this.serial(async () => {
|
|
31
|
+
const charged = [];
|
|
32
|
+
let serverCharged = 0;
|
|
33
|
+
if (input.purpose !== 'retry') {
|
|
34
|
+
const seen = this.sessionSet(input.session);
|
|
35
|
+
for (const r of input.results) {
|
|
36
|
+
const top = r.candidates[0];
|
|
37
|
+
if (r.kind !== 'card' || !r.confident || !top || seen.has(top.cardId))
|
|
38
|
+
continue;
|
|
39
|
+
seen.add(top.cardId);
|
|
40
|
+
if (r.identificationId !== undefined)
|
|
41
|
+
serverCharged++;
|
|
42
|
+
else
|
|
43
|
+
charged.push(top.cardId);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
const leaseId = input.lease.jti;
|
|
47
|
+
this.used.set(leaseId, this.usedUnder(leaseId) + serverCharged + charged.length);
|
|
48
|
+
await this.store.tally(leaseId, input.session, charged, 1, this.now());
|
|
49
|
+
return { charged, serverCharged };
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Seals the open tallies and hands the queued reports to `deliver` in
|
|
54
|
+
* batches of at most 500, oldest first. Each batch is removed once `deliver`
|
|
55
|
+
* resolves; a rejection stops the drain, leaves that batch queued, and is
|
|
56
|
+
* rethrown.
|
|
57
|
+
*/
|
|
58
|
+
async drain(deliver) {
|
|
59
|
+
// Sealing goes through the record queue, so a tally being written is not split.
|
|
60
|
+
await this.serial(() => this.store.seal(this.newId));
|
|
61
|
+
const deviceId = await this.store.deviceId();
|
|
62
|
+
let delivered = 0;
|
|
63
|
+
for (;;) {
|
|
64
|
+
const reports = await this.store.peek(MAX_BATCH_REPORTS);
|
|
65
|
+
if (reports.length === 0)
|
|
66
|
+
break;
|
|
67
|
+
await deliver({ deviceId, reports });
|
|
68
|
+
await this.store.remove(reports.map((r) => r.reportId));
|
|
69
|
+
await this.store.markReported(reports);
|
|
70
|
+
delivered += reports.length;
|
|
71
|
+
}
|
|
72
|
+
return { delivered, pending: await this.store.size() };
|
|
73
|
+
}
|
|
74
|
+
pending() {
|
|
75
|
+
return this.store.size();
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Makes sure the lease gets at least one report, even if nothing is ever
|
|
79
|
+
* identified under it: the service renews a lease only once the previous
|
|
80
|
+
* one has reported (an empty report counts).
|
|
81
|
+
*/
|
|
82
|
+
touch(leaseId, session) {
|
|
83
|
+
return this.serial(() => this.store.tally(leaseId, session, [], 0, this.now()));
|
|
84
|
+
}
|
|
85
|
+
/** What the host's lease request carries about the lease being renewed. */
|
|
86
|
+
async leaseRequest(previousLeaseId) {
|
|
87
|
+
return {
|
|
88
|
+
previousLeaseId,
|
|
89
|
+
usageReportedThrough: previousLeaseId === null ? null : await this.store.reportedThrough(previousLeaseId),
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
sessionSet(session) {
|
|
93
|
+
let set = this.seen.get(session);
|
|
94
|
+
if (!set) {
|
|
95
|
+
set = new Set();
|
|
96
|
+
this.seen.set(session, set);
|
|
97
|
+
if (this.seen.size > MAX_SESSIONS) {
|
|
98
|
+
const oldest = this.seen.keys().next().value;
|
|
99
|
+
if (oldest !== undefined)
|
|
100
|
+
this.seen.delete(oldest);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
return set;
|
|
104
|
+
}
|
|
105
|
+
serial(task) {
|
|
106
|
+
const run = this.chain.then(task, task);
|
|
107
|
+
this.chain = run.catch(() => undefined);
|
|
108
|
+
return run;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
//# sourceMappingURL=meter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"meter.js","sourceRoot":"","sources":["../../src/usage/meter.ts"],"names":[],"mappings":"AAqBA,OAAO,EAAE,QAAQ,EAAmB,MAAM,YAAY,CAAA;AAEtD,4CAA4C;AAC5C,MAAM,CAAC,MAAM,iBAAiB,GAAG,GA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U,CAAC,OAAe;QAChC,IAAI,GAAG,GAAG,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,CAAA;QAChC,IAAI,CAAC,GAAG,EAAE,CAAC;YACT,GAAG,GAAG,IAAI,GAAG,EAAE,CAAA;YACf,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,OAAO,EAAE,GAAG,CAAC,CAAA;YAC3B,IAAI,IAAI,CAAC,IAAI,CAAC,IAAI,GAAG,YAAY,EAAE,CAAC;gBAClC,MAAM,MAAM,GAAG,IAAI,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC,IAAI,EAAE,CAAC,KAAK,CAAA;gBAC5C,IAAI,MAAM,KAAK,SAAS;oBAAE,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,CAAA;YACpD,CAAC;QACH,CAAC;QACD,OAAO,GAAG,CAAA;IACZ,CAAC;IAEO,MAAM,CAAI,IAAsB;QACtC,MAAM,GAAG,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,CAAA;QACvC,IAAI,CAAC,KAAK,GAAG,GAAG,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,SAAS,CAAC,CAAA;QACvC,OAAO,GAAG,CAAA;IACZ,CAAC;CACF"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { UsageReport } from '../types.js';
|
|
2
|
+
export interface Tally {
|
|
3
|
+
readonly leaseId: string;
|
|
4
|
+
readonly sessionId: string;
|
|
5
|
+
/** Distinct, in first-seen order. */
|
|
6
|
+
readonly cardIds: readonly string[];
|
|
7
|
+
readonly calls: number;
|
|
8
|
+
/** Device clock (ms) of the latest addition. */
|
|
9
|
+
readonly at: number;
|
|
10
|
+
}
|
|
11
|
+
export interface UsageStore {
|
|
12
|
+
/** A random per-install id (at least 128 bits), created on first use. */
|
|
13
|
+
deviceId(): Promise<string>;
|
|
14
|
+
/** Adds cards (deduplicated) and calls to the open tally of (lease, session). */
|
|
15
|
+
tally(leaseId: string, sessionId: string, cardIds: readonly string[], calls: number, at: number): Promise<void>;
|
|
16
|
+
/** Seals every open tally into a queued report; returns how many were sealed. */
|
|
17
|
+
seal(newReportId: () => string): Promise<number>;
|
|
18
|
+
/** The oldest `limit` queued reports, in queue order. */
|
|
19
|
+
peek(limit: number): Promise<readonly UsageReport[]>;
|
|
20
|
+
remove(reportIds: readonly string[]): Promise<void>;
|
|
21
|
+
/** Records that these reports were delivered: each lease's highest delivered `seq`. */
|
|
22
|
+
markReported(reports: readonly UsageReport[]): Promise<void>;
|
|
23
|
+
/** The highest delivered `seq` of a lease, or null when none was. */
|
|
24
|
+
reportedThrough(leaseId: string): Promise<number | null>;
|
|
25
|
+
/** Queued reports plus open tallies. */
|
|
26
|
+
size(): Promise<number>;
|
|
27
|
+
/**
|
|
28
|
+
* Releases what the store holds open (the IndexedDB connection). Later calls
|
|
29
|
+
* reject with `ScannerError` `INVALID_OPTIONS`. Optional for stores a host
|
|
30
|
+
* implements; the identifier closes only a store it opened itself.
|
|
31
|
+
*/
|
|
32
|
+
close?(): void;
|
|
33
|
+
}
|
|
34
|
+
export declare function randomId(bytes?: number): string;
|
|
35
|
+
export declare function memoryUsageStore(): UsageStore;
|
|
36
|
+
/** The IndexedDB store. `factory` defaults to the global `indexedDB`. */
|
|
37
|
+
export declare function indexedDbUsageStore(factory?: IDBFactory): Promise<UsageStore>;
|
|
38
|
+
/** IndexedDB where it opens, else memory (private windows can refuse it). */
|
|
39
|
+
export declare function defaultUsageStore(): Promise<UsageStore>;
|
|
40
|
+
//# sourceMappingURL=store.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"store.d.ts","sourceRoot":"","sources":["../../src/usage/store.ts"],"names":[],"mappings":"AAyBA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAA;AAE9C,MAAM,WAAW,KAAK;IACpB,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAA;IACxB,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CAAC,OAAO,GAAE,UAAsB,GAAG,OAAO,CAAC,UAAU,CAAC,CAiH9F;AAED,6EAA6E;AAC7E,wBAAsB,iBAAiB,IAAI,OAAO,CAAC,UAAU,CAAC,CAO7D"}
|
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The device's usage queue: what was counted waits here, across reloads,
|
|
3
|
+
* until the host has delivered it. IndexedDB in the browser; an in-memory
|
|
4
|
+
* store for tests and for runtimes without IndexedDB (where usage then lives
|
|
5
|
+
* only as long as the page).
|
|
6
|
+
*
|
|
7
|
+
* Two stages:
|
|
8
|
+
* - a **tally** per (lease, session) collects the cards first identified in
|
|
9
|
+
* that session and the number of calls, as answers come in;
|
|
10
|
+
* - `seal` turns every open tally into a `UsageReport` with the lease's next
|
|
11
|
+
* `seq` and a fresh `reportId`, atomically. A sealed report never changes,
|
|
12
|
+
* so delivering it twice is harmless (the service dedupes by `reportId`).
|
|
13
|
+
*
|
|
14
|
+
* Database `dejadex-usage`, version 1:
|
|
15
|
+
* tallies key `<leaseId>\n<sessionId>` → Tally
|
|
16
|
+
* reports keyPath `report.reportId`, index `order` → { order, report }
|
|
17
|
+
* meta `deviceId`, `order`, `seq:<leaseId>`, `reported:<leaseId>`
|
|
18
|
+
*
|
|
19
|
+
* The connection is never held against the host: on `versionchange` (the
|
|
20
|
+
* host's `indexedDB.deleteDatabase('dejadex-usage')` on sign-out, or a newer
|
|
21
|
+
* page upgrading it) the store closes itself, so the delete proceeds. A closed
|
|
22
|
+
* store rejects every later call with a `ScannerError`; nothing is dropped
|
|
23
|
+
* without the caller hearing of it.
|
|
24
|
+
*/
|
|
25
|
+
import { ScannerError } from '../errors.js';
|
|
26
|
+
export function randomId(bytes = 16) {
|
|
27
|
+
const b = new Uint8Array(bytes);
|
|
28
|
+
crypto.getRandomValues(b);
|
|
29
|
+
let out = '';
|
|
30
|
+
for (const x of b)
|
|
31
|
+
out += x.toString(16).padStart(2, '0');
|
|
32
|
+
return out;
|
|
33
|
+
}
|
|
34
|
+
const tallyKey = (leaseId, sessionId) => `${leaseId}\n${sessionId}`;
|
|
35
|
+
function merge(prev, leaseId, sessionId, cardIds, calls, at) {
|
|
36
|
+
const cards = [...(prev?.cardIds ?? [])];
|
|
37
|
+
for (const id of cardIds)
|
|
38
|
+
if (!cards.includes(id))
|
|
39
|
+
cards.push(id);
|
|
40
|
+
return { leaseId, sessionId, cardIds: cards, calls: (prev?.calls ?? 0) + calls, at };
|
|
41
|
+
}
|
|
42
|
+
const toReport = (t, seq, reportId) => ({
|
|
43
|
+
reportId,
|
|
44
|
+
leaseId: t.leaseId,
|
|
45
|
+
seq,
|
|
46
|
+
sessionId: t.sessionId,
|
|
47
|
+
distinctCards: t.cardIds.length,
|
|
48
|
+
cardIds: [...t.cardIds],
|
|
49
|
+
calls: t.calls,
|
|
50
|
+
at: t.at,
|
|
51
|
+
});
|
|
52
|
+
export function memoryUsageStore() {
|
|
53
|
+
let device = null;
|
|
54
|
+
const seq = new Map();
|
|
55
|
+
const tallies = new Map();
|
|
56
|
+
const queue = [];
|
|
57
|
+
const reported = new Map();
|
|
58
|
+
return {
|
|
59
|
+
async deviceId() {
|
|
60
|
+
device ??= randomId();
|
|
61
|
+
return device;
|
|
62
|
+
},
|
|
63
|
+
async tally(leaseId, sessionId, cardIds, calls, at) {
|
|
64
|
+
const key = tallyKey(leaseId, sessionId);
|
|
65
|
+
tallies.set(key, merge(tallies.get(key), leaseId, sessionId, cardIds, calls, at));
|
|
66
|
+
},
|
|
67
|
+
async seal(newReportId) {
|
|
68
|
+
const open = [...tallies.values()];
|
|
69
|
+
tallies.clear();
|
|
70
|
+
for (const t of open) {
|
|
71
|
+
const next = (seq.get(t.leaseId) ?? 0) + 1;
|
|
72
|
+
seq.set(t.leaseId, next);
|
|
73
|
+
queue.push(toReport(t, next, newReportId()));
|
|
74
|
+
}
|
|
75
|
+
return open.length;
|
|
76
|
+
},
|
|
77
|
+
async peek(limit) {
|
|
78
|
+
return queue.slice(0, limit);
|
|
79
|
+
},
|
|
80
|
+
async remove(ids) {
|
|
81
|
+
const drop = new Set(ids);
|
|
82
|
+
for (let i = queue.length - 1; i >= 0; i--)
|
|
83
|
+
if (drop.has(queue[i].reportId))
|
|
84
|
+
queue.splice(i, 1);
|
|
85
|
+
},
|
|
86
|
+
async markReported(reports) {
|
|
87
|
+
for (const r of reports)
|
|
88
|
+
reported.set(r.leaseId, Math.max(reported.get(r.leaseId) ?? 0, r.seq));
|
|
89
|
+
},
|
|
90
|
+
async reportedThrough(leaseId) {
|
|
91
|
+
return reported.get(leaseId) ?? null;
|
|
92
|
+
},
|
|
93
|
+
async size() {
|
|
94
|
+
return queue.length + tallies.size;
|
|
95
|
+
},
|
|
96
|
+
/** Nothing to release: memory lives as long as the page. */
|
|
97
|
+
close() { },
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
const DB_NAME = 'dejadex-usage';
|
|
101
|
+
const DB_VERSION = 1;
|
|
102
|
+
function request(req) {
|
|
103
|
+
return new Promise((resolve, reject) => {
|
|
104
|
+
req.onsuccess = () => resolve(req.result);
|
|
105
|
+
req.onerror = () => reject(req.error ?? new Error('IndexedDB request failed'));
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
function done(tx) {
|
|
109
|
+
return new Promise((resolve, reject) => {
|
|
110
|
+
tx.oncomplete = () => resolve();
|
|
111
|
+
tx.onabort = () => reject(tx.error ?? new Error('IndexedDB transaction aborted'));
|
|
112
|
+
tx.onerror = () => reject(tx.error ?? new Error('IndexedDB transaction failed'));
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
/** The IndexedDB store. `factory` defaults to the global `indexedDB`. */
|
|
116
|
+
export async function indexedDbUsageStore(factory = indexedDB) {
|
|
117
|
+
const open = factory.open(DB_NAME, DB_VERSION);
|
|
118
|
+
open.onupgradeneeded = () => {
|
|
119
|
+
const db = open.result;
|
|
120
|
+
if (!db.objectStoreNames.contains('tallies'))
|
|
121
|
+
db.createObjectStore('tallies');
|
|
122
|
+
if (!db.objectStoreNames.contains('reports')) {
|
|
123
|
+
db.createObjectStore('reports', { keyPath: 'report.reportId' }).createIndex('order', 'order', { unique: true });
|
|
124
|
+
}
|
|
125
|
+
if (!db.objectStoreNames.contains('meta'))
|
|
126
|
+
db.createObjectStore('meta');
|
|
127
|
+
};
|
|
128
|
+
const db = await request(open);
|
|
129
|
+
/** Why the connection is gone, once it is. */
|
|
130
|
+
let closed = null;
|
|
131
|
+
const shut = (why) => {
|
|
132
|
+
if (closed !== null)
|
|
133
|
+
return;
|
|
134
|
+
closed = why;
|
|
135
|
+
db.close();
|
|
136
|
+
};
|
|
137
|
+
// Another party wants the database (a host's deleteDatabase, a newer
|
|
138
|
+
// version): let go, or its request is blocked for the life of the document.
|
|
139
|
+
db.onversionchange = () => shut('it was deleted or upgraded elsewhere');
|
|
140
|
+
// The browser closed it (storage cleared, the disk went away).
|
|
141
|
+
db.onclose = () => {
|
|
142
|
+
closed ??= 'the browser closed it';
|
|
143
|
+
};
|
|
144
|
+
const transaction = (names, mode) => {
|
|
145
|
+
if (closed !== null)
|
|
146
|
+
throw new ScannerError('INVALID_OPTIONS', `the usage store is closed: ${closed}`);
|
|
147
|
+
return db.transaction(names, mode);
|
|
148
|
+
};
|
|
149
|
+
return {
|
|
150
|
+
async deviceId() {
|
|
151
|
+
const tx = transaction('meta', 'readwrite');
|
|
152
|
+
const meta = tx.objectStore('meta');
|
|
153
|
+
let id = (await request(meta.get('deviceId')));
|
|
154
|
+
if (typeof id !== 'string') {
|
|
155
|
+
id = randomId();
|
|
156
|
+
meta.put(id, 'deviceId');
|
|
157
|
+
}
|
|
158
|
+
await done(tx);
|
|
159
|
+
return id;
|
|
160
|
+
},
|
|
161
|
+
async tally(leaseId, sessionId, cardIds, calls, at) {
|
|
162
|
+
const tx = transaction('tallies', 'readwrite');
|
|
163
|
+
const store = tx.objectStore('tallies');
|
|
164
|
+
const key = tallyKey(leaseId, sessionId);
|
|
165
|
+
const prev = (await request(store.get(key)));
|
|
166
|
+
store.put(merge(prev, leaseId, sessionId, cardIds, calls, at), key);
|
|
167
|
+
await done(tx);
|
|
168
|
+
},
|
|
169
|
+
async seal(newReportId) {
|
|
170
|
+
const tx = transaction(['tallies', 'reports', 'meta'], 'readwrite');
|
|
171
|
+
const tallies = tx.objectStore('tallies');
|
|
172
|
+
const reports = tx.objectStore('reports');
|
|
173
|
+
const meta = tx.objectStore('meta');
|
|
174
|
+
const open = (await request(tallies.getAll()));
|
|
175
|
+
let order = (await request(meta.get('order'))) ?? 0;
|
|
176
|
+
const seqs = new Map();
|
|
177
|
+
for (const t of open) {
|
|
178
|
+
const key = `seq:${t.leaseId}`;
|
|
179
|
+
let last = seqs.get(key);
|
|
180
|
+
if (last === undefined)
|
|
181
|
+
last = (await request(meta.get(key))) ?? 0;
|
|
182
|
+
const next = last + 1;
|
|
183
|
+
seqs.set(key, next);
|
|
184
|
+
reports.put({ order: ++order, report: toReport(t, next, newReportId()) });
|
|
185
|
+
}
|
|
186
|
+
for (const [key, value] of seqs)
|
|
187
|
+
meta.put(value, key);
|
|
188
|
+
meta.put(order, 'order');
|
|
189
|
+
tallies.clear();
|
|
190
|
+
await done(tx);
|
|
191
|
+
return open.length;
|
|
192
|
+
},
|
|
193
|
+
async peek(limit) {
|
|
194
|
+
const tx = transaction('reports', 'readonly');
|
|
195
|
+
const rows = (await request(tx.objectStore('reports').index('order').getAll(null, limit)));
|
|
196
|
+
await done(tx);
|
|
197
|
+
return rows.map((r) => r.report);
|
|
198
|
+
},
|
|
199
|
+
async remove(ids) {
|
|
200
|
+
const tx = transaction('reports', 'readwrite');
|
|
201
|
+
const store = tx.objectStore('reports');
|
|
202
|
+
for (const id of ids)
|
|
203
|
+
store.delete(id);
|
|
204
|
+
await done(tx);
|
|
205
|
+
},
|
|
206
|
+
async markReported(reports) {
|
|
207
|
+
const tx = transaction('meta', 'readwrite');
|
|
208
|
+
const meta = tx.objectStore('meta');
|
|
209
|
+
const top = new Map();
|
|
210
|
+
for (const r of reports)
|
|
211
|
+
top.set(r.leaseId, Math.max(top.get(r.leaseId) ?? 0, r.seq));
|
|
212
|
+
for (const [leaseId, seq] of top) {
|
|
213
|
+
const key = `reported:${leaseId}`;
|
|
214
|
+
const prev = (await request(meta.get(key)));
|
|
215
|
+
if (prev === undefined || seq > prev)
|
|
216
|
+
meta.put(seq, key);
|
|
217
|
+
}
|
|
218
|
+
await done(tx);
|
|
219
|
+
},
|
|
220
|
+
async reportedThrough(leaseId) {
|
|
221
|
+
const tx = transaction('meta', 'readonly');
|
|
222
|
+
const v = (await request(tx.objectStore('meta').get(`reported:${leaseId}`)));
|
|
223
|
+
await done(tx);
|
|
224
|
+
return v ?? null;
|
|
225
|
+
},
|
|
226
|
+
async size() {
|
|
227
|
+
const tx = transaction(['reports', 'tallies'], 'readonly');
|
|
228
|
+
const [a, b] = await Promise.all([request(tx.objectStore('reports').count()), request(tx.objectStore('tallies').count())]);
|
|
229
|
+
await done(tx);
|
|
230
|
+
return a + b;
|
|
231
|
+
},
|
|
232
|
+
close() {
|
|
233
|
+
shut('close() was called');
|
|
234
|
+
},
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
/** IndexedDB where it opens, else memory (private windows can refuse it). */
|
|
238
|
+
export async function defaultUsageStore() {
|
|
239
|
+
try {
|
|
240
|
+
if (typeof indexedDB !== 'undefined')
|
|
241
|
+
return await indexedDbUsageStore();
|
|
242
|
+
}
|
|
243
|
+
catch {
|
|
244
|
+
// fall through to memory
|
|
245
|
+
}
|
|
246
|
+
return memoryUsageStore();
|
|
247
|
+
}
|
|
248
|
+
//# sourceMappingURL=store.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"store.js","sourceRoot":"","sources":["../../src/usage/store.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAA;AAqC3C,MAAM,UAAU,QAAQ,CAAC,KA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K,WAAW;YAAE,OAAO,MAAM,mBAAmB,EAAE,CAAA;IAC1E,CAAC;IAAC,MAAM,CAAC;QACP,yBAAyB;IAC3B,CAAC;IACD,OAAO,gBAAgB,EAAE,CAAA;AAC3B,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,66 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dejadex/web",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "Framework-free browser SDK: on-device card detection, tracking and identification",
|
|
5
|
+
"license": "UNLICENSED",
|
|
6
|
+
"homepage": "https://dejadex.com",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/dejadexhq/DejaDex.git",
|
|
10
|
+
"directory": "sdk/web"
|
|
11
|
+
},
|
|
12
|
+
"type": "module",
|
|
13
|
+
"main": "./dist/index.js",
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"exports": {
|
|
16
|
+
".": {
|
|
17
|
+
"types": "./dist/index.d.ts",
|
|
18
|
+
"import": "./dist/index.js"
|
|
19
|
+
},
|
|
20
|
+
"./identify-worker": {
|
|
21
|
+
"types": "./dist/identify/worker.d.ts",
|
|
22
|
+
"import": "./dist/identify/worker.js"
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
"files": [
|
|
26
|
+
"dist",
|
|
27
|
+
"src",
|
|
28
|
+
"!src/**/*.test.ts",
|
|
29
|
+
"!src/**/*.testutil.ts",
|
|
30
|
+
"!src/**/__fixtures__",
|
|
31
|
+
"CHANGELOG.md"
|
|
32
|
+
],
|
|
33
|
+
"sideEffects": [
|
|
34
|
+
"./dist/identify/worker.js"
|
|
35
|
+
],
|
|
36
|
+
"scripts": {
|
|
37
|
+
"build": "tsc -p tsconfig.build.json",
|
|
38
|
+
"prepack": "npm run build",
|
|
39
|
+
"typecheck": "tsc --noEmit",
|
|
40
|
+
"lint": "eslint . --report-unused-disable-directives --max-warnings 0",
|
|
41
|
+
"test": "vitest run",
|
|
42
|
+
"test:watch": "vitest",
|
|
43
|
+
"assets:dev": "node scripts/dev-assets.mjs",
|
|
44
|
+
"measure:identify": "npm run build && node scripts/measure-identify.mjs"
|
|
45
|
+
},
|
|
46
|
+
"engines": {
|
|
47
|
+
"node": ">=22"
|
|
48
|
+
},
|
|
49
|
+
"publishConfig": {
|
|
50
|
+
"registry": "https://registry.npmjs.org/",
|
|
51
|
+
"access": "public"
|
|
52
|
+
},
|
|
53
|
+
"devDependencies": {
|
|
54
|
+
"@eslint/js": "^9.39.5",
|
|
55
|
+
"eslint": "^9.39.5",
|
|
56
|
+
"fake-indexeddb": "6.2.6",
|
|
57
|
+
"globals": "^15.15.0",
|
|
58
|
+
"typescript": "^5.9.3",
|
|
59
|
+
"typescript-eslint": "^8.65.0",
|
|
60
|
+
"vitest": "^5.0.5"
|
|
61
|
+
},
|
|
62
|
+
"dependencies": {
|
|
63
|
+
"@noble/ed25519": "3.2.0",
|
|
64
|
+
"onnxruntime-web": "1.30.0"
|
|
65
|
+
}
|
|
66
|
+
}
|
package/src/errors.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/** Machine-readable reasons the scanner refuses or stops. Stable; hosts branch on them. */
|
|
2
|
+
export type ScannerErrorCode =
|
|
3
|
+
| 'INVALID_OPTIONS'
|
|
4
|
+
| 'NOT_IMPLEMENTED'
|
|
5
|
+
| 'LEASE_INVALID'
|
|
6
|
+
| 'LEASE_EXPIRED'
|
|
7
|
+
| 'ALLOWANCE_EXHAUSTED'
|
|
8
|
+
| 'ORIGIN_NOT_ALLOWED'
|
|
9
|
+
| 'DOMAIN_NOT_ALLOWED'
|
|
10
|
+
| 'UNSUPPORTED_DEVICE'
|
|
11
|
+
| 'MODEL_INTEGRITY'
|
|
12
|
+
| 'NETWORK'
|
|
13
|
+
|
|
14
|
+
export class ScannerError extends Error {
|
|
15
|
+
override readonly name = 'ScannerError'
|
|
16
|
+
|
|
17
|
+
constructor(
|
|
18
|
+
readonly code: ScannerErrorCode,
|
|
19
|
+
message: string,
|
|
20
|
+
options?: ErrorOptions,
|
|
21
|
+
) {
|
|
22
|
+
super(message, options)
|
|
23
|
+
}
|
|
24
|
+
}
|