@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 +3 -3
- package/dist/tiny.cjs +86 -35
- package/dist/tiny.d.cts +2 -2
- package/dist/tiny.d.ts +2 -2
- package/dist/tiny.js +86 -35
- package/package.json +5 -2
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
|
|
96
|
-
|
|
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
|
|
256
|
+
const store = opts.cache ?? true ? cacheStore(url, opts.cacheDir) : void 0;
|
|
257
257
|
const doFetch = opts.fetch ?? globalThis.fetch;
|
|
258
|
-
if (
|
|
259
|
-
const cached = await
|
|
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
|
|
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 (
|
|
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
|
-
|
|
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
|
|
332
|
+
return new URL(url, base).href;
|
|
324
333
|
} catch {
|
|
325
|
-
return
|
|
334
|
+
return url;
|
|
326
335
|
}
|
|
327
336
|
}
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
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 (
|
|
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
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
}
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
const
|
|
378
|
-
|
|
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 (
|
|
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 (
|
|
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
|
|
44
|
+
const store = opts.cache ?? true ? cacheStore(url, opts.cacheDir) : void 0;
|
|
45
45
|
const doFetch = opts.fetch ?? globalThis.fetch;
|
|
46
|
-
if (
|
|
47
|
-
const cached = await
|
|
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
|
|
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 (
|
|
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
|
-
|
|
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
|
|
120
|
+
return new URL(url, base).href;
|
|
112
121
|
} catch {
|
|
113
|
-
return
|
|
122
|
+
return url;
|
|
114
123
|
}
|
|
115
124
|
}
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
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 (
|
|
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
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
const
|
|
166
|
-
|
|
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.
|
|
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": "^
|
|
72
|
+
"vitest": "^5"
|
|
70
73
|
}
|
|
71
74
|
}
|