@structured-world/structured-public-domains 0.0.42 → 0.0.44

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/README.md CHANGED
@@ -92,9 +92,9 @@ entire PSL on every CI run, so both languages return identical results.
92
92
 
93
93
  For consumers who want always-fresh PSL **without bumping the package version**,
94
94
  the `/tiny` entry ships *without* the embedded blob. It fetches the prebuilt
95
- binary trie at runtime and caches it locally (Node: temp file with a TTL;
96
- browser: CacheStorage). After the first `await load()`, the lookup API is
97
- identical and synchronous.
95
+ binary trie at runtime and caches it locally with a TTL (browser windows and
96
+ workers: IndexedDB, including pages served over plain http; Node: a temp file).
97
+ After the first `await load()`, the lookup API is identical and synchronous.
98
98
 
99
99
  ```typescript
100
100
  import { load, registrableDomain } from '@structured-world/structured-public-domains/tiny';
package/dist/tiny.cjs CHANGED
@@ -251,18 +251,18 @@ async function doLoad(opts, gen) {
251
251
  cachedTrie = trie;
252
252
  }
253
253
  async function fetchAndParse(opts) {
254
- const url = opts.url ?? DEFAULT_PSL_URL;
254
+ const url = resolveUrl(opts.url ?? DEFAULT_PSL_URL);
255
255
  const ttlMs = opts.cacheTtlMs ?? DEFAULT_TTL_MS;
256
- const useCache = opts.cache ?? true;
256
+ const store = opts.cache ?? true ? cacheStore(url, opts.cacheDir) : void 0;
257
257
  const doFetch = opts.fetch ?? globalThis.fetch;
258
- if (useCache && opts.force !== true) {
259
- const cached = await readCache(url, ttlMs, opts.cacheDir);
258
+ if (store !== void 0 && opts.force !== true) {
259
+ const cached = await store.read(ttlMs).catch(() => void 0);
260
260
  if (cached !== void 0) {
261
261
  try {
262
262
  validateTrie(cached);
263
263
  return [cached, parseTrie(cached)];
264
264
  } catch {
265
- await deleteCache(url, opts.cacheDir).catch(() => void 0);
265
+ await store.remove().catch(() => void 0);
266
266
  }
267
267
  }
268
268
  }
@@ -272,7 +272,7 @@ async function fetchAndParse(opts) {
272
272
  const data = await fetchBytes(doFetch, url);
273
273
  validateTrie(data);
274
274
  const trie = parseTrie(data);
275
- if (useCache) await writeCache(url, data, opts.cacheDir).catch(() => void 0);
275
+ if (store !== void 0) await store.write(data).catch(() => void 0);
276
276
  return [data, trie];
277
277
  }
278
278
  function isLoaded() {
@@ -318,25 +318,45 @@ function cacheKey(url) {
318
318
  }
319
319
  return (h >>> 0).toString(16).padStart(8, "0");
320
320
  }
321
- async function readCache(url, ttlMs, cacheDir) {
321
+ function isStale(stampedAt, ttlMs) {
322
+ const now = Date.now();
323
+ return !Number.isFinite(stampedAt) || stampedAt > now || now - stampedAt >= ttlMs;
324
+ }
325
+ function idbFactory() {
326
+ return typeof indexedDB !== "undefined" ? indexedDB : void 0;
327
+ }
328
+ function resolveUrl(url) {
329
+ const base = typeof document !== "undefined" ? document.baseURI : typeof location !== "undefined" ? location.href : void 0;
330
+ if (base === void 0) return url;
322
331
  try {
323
- return isNode ? await readNodeCache(url, ttlMs, cacheDir) : await readBrowserCache(url, ttlMs);
332
+ return new URL(url, base).href;
324
333
  } catch {
325
- return void 0;
334
+ return url;
326
335
  }
327
336
  }
328
- async function writeCache(url, data, cacheDir) {
329
- if (isNode) await writeNodeCache(url, data, cacheDir);
330
- else await writeBrowserCache(url, data);
331
- }
332
- async function deleteCache(url, cacheDir) {
333
- if (isNode) {
334
- const { fs, file } = await nodeCache(url, cacheDir);
335
- fs.rmSync(file, { force: true });
336
- } else if (typeof caches !== "undefined") {
337
- const cache = await caches.open("structured-public-domains");
338
- await cache.delete(url);
337
+ function cacheStore(url, cacheDir) {
338
+ let idb;
339
+ try {
340
+ idb = idbFactory();
341
+ } catch {
342
+ return void 0;
339
343
  }
344
+ if (idb !== void 0) {
345
+ return {
346
+ read: (ttlMs) => readIdbCache(idb, url, ttlMs),
347
+ write: (data) => writeIdbCache(idb, url, data),
348
+ remove: () => idbRun(idb, "readwrite", (store) => store.delete(url))
349
+ };
350
+ }
351
+ if (!isNode) return void 0;
352
+ return {
353
+ read: (ttlMs) => readNodeCache(url, ttlMs, cacheDir),
354
+ write: (data) => writeNodeCache(url, data, cacheDir),
355
+ remove: async () => {
356
+ const { fs, file } = await nodeCache(url, cacheDir);
357
+ fs.rmSync(file, { force: true });
358
+ }
359
+ };
340
360
  }
341
361
  var nodeRequire = (m) => import(
342
362
  /* @vite-ignore */
@@ -352,7 +372,7 @@ async function nodeCache(url, cacheDir) {
352
372
  async function readNodeCache(url, ttlMs, cacheDir) {
353
373
  const { fs, file } = await nodeCache(url, cacheDir);
354
374
  const stat = fs.statSync(file);
355
- if (Date.now() - stat.mtimeMs >= ttlMs) return void 0;
375
+ if (isStale(Math.floor(stat.mtimeMs), ttlMs)) return void 0;
356
376
  return new Uint8Array(fs.readFileSync(file));
357
377
  }
358
378
  async function writeNodeCache(url, data, cacheDir) {
@@ -362,20 +382,51 @@ async function writeNodeCache(url, data, cacheDir) {
362
382
  fs.writeFileSync(tmp, data);
363
383
  fs.renameSync(tmp, file);
364
384
  }
365
- async function readBrowserCache(url, ttlMs) {
366
- if (typeof caches === "undefined") return void 0;
367
- const cache = await caches.open("structured-public-domains");
368
- const res = await cache.match(url);
369
- if (res == null) return void 0;
370
- const cachedAt = Number(res.headers.get("x-cached-at") ?? "0");
371
- if (!Number.isFinite(cachedAt) || Date.now() - cachedAt >= ttlMs) return void 0;
372
- return new Uint8Array(await res.arrayBuffer());
373
- }
374
- async function writeBrowserCache(url, data) {
375
- if (typeof caches === "undefined") return;
376
- const cache = await caches.open("structured-public-domains");
377
- const body = data.slice();
378
- await cache.put(url, new Response(body, { headers: { "x-cached-at": String(Date.now()) } }));
385
+ var IDB_NAME = "structured-public-domains";
386
+ var IDB_STORE = "psl";
387
+ function idbOpen(idb) {
388
+ return new Promise((resolve, reject) => {
389
+ const req = idb.open(IDB_NAME, 1);
390
+ req.onupgradeneeded = () => req.result.createObjectStore(IDB_STORE);
391
+ req.onsuccess = () => resolve(req.result);
392
+ req.onerror = () => reject(req.error);
393
+ });
394
+ }
395
+ var IDB_DEADLINE_MS = 1e3;
396
+ function idbRun(idb, mode, op) {
397
+ const work = idbOpen(idb).then(async (db) => {
398
+ try {
399
+ return await new Promise((resolve, reject) => {
400
+ const tx = db.transaction(IDB_STORE, mode);
401
+ const req = op(tx.objectStore(IDB_STORE));
402
+ tx.oncomplete = () => resolve(req.result);
403
+ tx.onabort = () => reject(tx.error);
404
+ });
405
+ } finally {
406
+ db.close();
407
+ }
408
+ });
409
+ return withDeadline(work, IDB_DEADLINE_MS);
410
+ }
411
+ function withDeadline(work, ms) {
412
+ let timer;
413
+ const deadline = new Promise((_, reject) => {
414
+ timer = setTimeout(() => reject(new Error(`IndexedDB did not answer within ${ms}ms`)), ms);
415
+ });
416
+ work.catch(() => void 0);
417
+ return Promise.race([work, deadline]).finally(() => clearTimeout(timer));
418
+ }
419
+ async function readIdbCache(idb, url, ttlMs) {
420
+ const entry = await idbRun(idb, "readonly", (store) => store.get(url));
421
+ if (typeof entry !== "object" || entry === null) return void 0;
422
+ const { bytes, cachedAt } = entry;
423
+ if (!(bytes instanceof Uint8Array) || typeof cachedAt !== "number") return void 0;
424
+ if (isStale(cachedAt, ttlMs)) return void 0;
425
+ return bytes;
426
+ }
427
+ async function writeIdbCache(idb, url, data) {
428
+ const entry = { bytes: data, cachedAt: Date.now() };
429
+ await idbRun(idb, "readwrite", (store) => store.put(entry, url));
379
430
  }
380
431
 
381
432
  exports.DEFAULT_PSL_URL = DEFAULT_PSL_URL;
package/dist/tiny.d.cts CHANGED
@@ -8,11 +8,11 @@ interface LoadOptions {
8
8
  url?: string;
9
9
  /** Custom fetch implementation (e.g. for proxies/tests). Defaults to the global `fetch`. */
10
10
  fetch?: typeof fetch;
11
- /** Enable the persistent local cache (Node file / browser CacheStorage). Default `true`. */
11
+ /** Enable the persistent local cache (IndexedDB where available, else a Node file). Default `true`. */
12
12
  cache?: boolean;
13
13
  /** Cache freshness window in milliseconds. Default 24h. */
14
14
  cacheTtlMs?: number;
15
- /** Node only: directory for the cache file. Defaults to an OS temp subdir. */
15
+ /** Node file cache only: directory for the cache file. Defaults to an OS temp subdir. */
16
16
  cacheDir?: string;
17
17
  /** Re-fetch even if already loaded / cached. Default `false`. */
18
18
  force?: boolean;
package/dist/tiny.d.ts CHANGED
@@ -8,11 +8,11 @@ interface LoadOptions {
8
8
  url?: string;
9
9
  /** Custom fetch implementation (e.g. for proxies/tests). Defaults to the global `fetch`. */
10
10
  fetch?: typeof fetch;
11
- /** Enable the persistent local cache (Node file / browser CacheStorage). Default `true`. */
11
+ /** Enable the persistent local cache (IndexedDB where available, else a Node file). Default `true`. */
12
12
  cache?: boolean;
13
13
  /** Cache freshness window in milliseconds. Default 24h. */
14
14
  cacheTtlMs?: number;
15
- /** Node only: directory for the cache file. Defaults to an OS temp subdir. */
15
+ /** Node file cache only: directory for the cache file. Defaults to an OS temp subdir. */
16
16
  cacheDir?: string;
17
17
  /** Re-fetch even if already loaded / cached. Default `false`. */
18
18
  force?: boolean;
package/dist/tiny.js CHANGED
@@ -39,18 +39,18 @@ async function doLoad(opts, gen) {
39
39
  cachedTrie = trie;
40
40
  }
41
41
  async function fetchAndParse(opts) {
42
- const url = opts.url ?? DEFAULT_PSL_URL;
42
+ const url = resolveUrl(opts.url ?? DEFAULT_PSL_URL);
43
43
  const ttlMs = opts.cacheTtlMs ?? DEFAULT_TTL_MS;
44
- const useCache = opts.cache ?? true;
44
+ const store = opts.cache ?? true ? cacheStore(url, opts.cacheDir) : void 0;
45
45
  const doFetch = opts.fetch ?? globalThis.fetch;
46
- if (useCache && opts.force !== true) {
47
- const cached = await readCache(url, ttlMs, opts.cacheDir);
46
+ if (store !== void 0 && opts.force !== true) {
47
+ const cached = await store.read(ttlMs).catch(() => void 0);
48
48
  if (cached !== void 0) {
49
49
  try {
50
50
  validateTrie(cached);
51
51
  return [cached, parseTrie(cached)];
52
52
  } catch {
53
- await deleteCache(url, opts.cacheDir).catch(() => void 0);
53
+ await store.remove().catch(() => void 0);
54
54
  }
55
55
  }
56
56
  }
@@ -60,7 +60,7 @@ async function fetchAndParse(opts) {
60
60
  const data = await fetchBytes(doFetch, url);
61
61
  validateTrie(data);
62
62
  const trie = parseTrie(data);
63
- if (useCache) await writeCache(url, data, opts.cacheDir).catch(() => void 0);
63
+ if (store !== void 0) await store.write(data).catch(() => void 0);
64
64
  return [data, trie];
65
65
  }
66
66
  function isLoaded() {
@@ -106,25 +106,45 @@ function cacheKey(url) {
106
106
  }
107
107
  return (h >>> 0).toString(16).padStart(8, "0");
108
108
  }
109
- async function readCache(url, ttlMs, cacheDir) {
109
+ function isStale(stampedAt, ttlMs) {
110
+ const now = Date.now();
111
+ return !Number.isFinite(stampedAt) || stampedAt > now || now - stampedAt >= ttlMs;
112
+ }
113
+ function idbFactory() {
114
+ return typeof indexedDB !== "undefined" ? indexedDB : void 0;
115
+ }
116
+ function resolveUrl(url) {
117
+ const base = typeof document !== "undefined" ? document.baseURI : typeof location !== "undefined" ? location.href : void 0;
118
+ if (base === void 0) return url;
110
119
  try {
111
- return isNode ? await readNodeCache(url, ttlMs, cacheDir) : await readBrowserCache(url, ttlMs);
120
+ return new URL(url, base).href;
112
121
  } catch {
113
- return void 0;
122
+ return url;
114
123
  }
115
124
  }
116
- async function writeCache(url, data, cacheDir) {
117
- if (isNode) await writeNodeCache(url, data, cacheDir);
118
- else await writeBrowserCache(url, data);
119
- }
120
- async function deleteCache(url, cacheDir) {
121
- if (isNode) {
122
- const { fs, file } = await nodeCache(url, cacheDir);
123
- fs.rmSync(file, { force: true });
124
- } else if (typeof caches !== "undefined") {
125
- const cache = await caches.open("structured-public-domains");
126
- await cache.delete(url);
125
+ function cacheStore(url, cacheDir) {
126
+ let idb;
127
+ try {
128
+ idb = idbFactory();
129
+ } catch {
130
+ return void 0;
131
+ }
132
+ if (idb !== void 0) {
133
+ return {
134
+ read: (ttlMs) => readIdbCache(idb, url, ttlMs),
135
+ write: (data) => writeIdbCache(idb, url, data),
136
+ remove: () => idbRun(idb, "readwrite", (store) => store.delete(url))
137
+ };
127
138
  }
139
+ if (!isNode) return void 0;
140
+ return {
141
+ read: (ttlMs) => readNodeCache(url, ttlMs, cacheDir),
142
+ write: (data) => writeNodeCache(url, data, cacheDir),
143
+ remove: async () => {
144
+ const { fs, file } = await nodeCache(url, cacheDir);
145
+ fs.rmSync(file, { force: true });
146
+ }
147
+ };
128
148
  }
129
149
  var nodeRequire = (m) => import(
130
150
  /* @vite-ignore */
@@ -140,7 +160,7 @@ async function nodeCache(url, cacheDir) {
140
160
  async function readNodeCache(url, ttlMs, cacheDir) {
141
161
  const { fs, file } = await nodeCache(url, cacheDir);
142
162
  const stat = fs.statSync(file);
143
- if (Date.now() - stat.mtimeMs >= ttlMs) return void 0;
163
+ if (isStale(Math.floor(stat.mtimeMs), ttlMs)) return void 0;
144
164
  return new Uint8Array(fs.readFileSync(file));
145
165
  }
146
166
  async function writeNodeCache(url, data, cacheDir) {
@@ -150,20 +170,51 @@ async function writeNodeCache(url, data, cacheDir) {
150
170
  fs.writeFileSync(tmp, data);
151
171
  fs.renameSync(tmp, file);
152
172
  }
153
- async function readBrowserCache(url, ttlMs) {
154
- if (typeof caches === "undefined") return void 0;
155
- const cache = await caches.open("structured-public-domains");
156
- const res = await cache.match(url);
157
- if (res == null) return void 0;
158
- const cachedAt = Number(res.headers.get("x-cached-at") ?? "0");
159
- if (!Number.isFinite(cachedAt) || Date.now() - cachedAt >= ttlMs) return void 0;
160
- return new Uint8Array(await res.arrayBuffer());
161
- }
162
- async function writeBrowserCache(url, data) {
163
- if (typeof caches === "undefined") return;
164
- const cache = await caches.open("structured-public-domains");
165
- const body = data.slice();
166
- await cache.put(url, new Response(body, { headers: { "x-cached-at": String(Date.now()) } }));
173
+ var IDB_NAME = "structured-public-domains";
174
+ var IDB_STORE = "psl";
175
+ function idbOpen(idb) {
176
+ return new Promise((resolve, reject) => {
177
+ const req = idb.open(IDB_NAME, 1);
178
+ req.onupgradeneeded = () => req.result.createObjectStore(IDB_STORE);
179
+ req.onsuccess = () => resolve(req.result);
180
+ req.onerror = () => reject(req.error);
181
+ });
182
+ }
183
+ var IDB_DEADLINE_MS = 1e3;
184
+ function idbRun(idb, mode, op) {
185
+ const work = idbOpen(idb).then(async (db) => {
186
+ try {
187
+ return await new Promise((resolve, reject) => {
188
+ const tx = db.transaction(IDB_STORE, mode);
189
+ const req = op(tx.objectStore(IDB_STORE));
190
+ tx.oncomplete = () => resolve(req.result);
191
+ tx.onabort = () => reject(tx.error);
192
+ });
193
+ } finally {
194
+ db.close();
195
+ }
196
+ });
197
+ return withDeadline(work, IDB_DEADLINE_MS);
198
+ }
199
+ function withDeadline(work, ms) {
200
+ let timer;
201
+ const deadline = new Promise((_, reject) => {
202
+ timer = setTimeout(() => reject(new Error(`IndexedDB did not answer within ${ms}ms`)), ms);
203
+ });
204
+ work.catch(() => void 0);
205
+ return Promise.race([work, deadline]).finally(() => clearTimeout(timer));
206
+ }
207
+ async function readIdbCache(idb, url, ttlMs) {
208
+ const entry = await idbRun(idb, "readonly", (store) => store.get(url));
209
+ if (typeof entry !== "object" || entry === null) return void 0;
210
+ const { bytes, cachedAt } = entry;
211
+ if (!(bytes instanceof Uint8Array) || typeof cachedAt !== "number") return void 0;
212
+ if (isStale(cachedAt, ttlMs)) return void 0;
213
+ return bytes;
214
+ }
215
+ async function writeIdbCache(idb, url, data) {
216
+ const entry = { bytes: data, cachedAt: Date.now() };
217
+ await idbRun(idb, "readwrite", (store) => store.put(entry, url));
167
218
  }
168
219
 
169
220
  export { DEFAULT_PSL_URL, isKnownSuffix, isLoaded, load, lookup, pslData, registrableDomain };
package/package.json CHANGED
@@ -1,8 +1,9 @@
1
1
  {
2
2
  "name": "@structured-world/structured-public-domains",
3
- "version": "0.0.42",
3
+ "version": "0.0.44",
4
4
  "description": "Compact Public Suffix List (PSL) as a native TypeScript package: embedded ~108KB binary trie, synchronous zero-dependency lookups, ESM + CommonJS. Checked daily against publicsuffix.org.",
5
5
  "license": "Apache-2.0",
6
+ "author": "Dmitry Prudnikov <mail@polaz.com>",
6
7
  "repository": {
7
8
  "type": "git",
8
9
  "url": "https://github.com/structured-world/structured-public-domains"
@@ -64,8 +65,10 @@
64
65
  },
65
66
  "devDependencies": {
66
67
  "@types/node": "^22",
68
+ "@vitest/coverage-v8": "^5.0.5",
69
+ "fake-indexeddb": "^6.2.6",
67
70
  "tsup": "^8",
68
71
  "typescript": "^5.6",
69
- "vitest": "^3"
72
+ "vitest": "^5"
70
73
  }
71
74
  }