@toapi/worker 1.2.4 → 1.3.0

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 ADDED
@@ -0,0 +1,22 @@
1
+ # @toapi/worker
2
+
3
+ Service-worker request handling for Toapi: offline-capable caching and remote tag revalidation, working alongside [@toapi/server](https://www.npmjs.com/package/@toapi/server)'s caching and [@toapi/client](https://www.npmjs.com/package/@toapi/client).
4
+
5
+ 📚 [Docs](https://toapi-js.github.io/toapi/worker/)
6
+
7
+ ## Installation
8
+
9
+ ```bash
10
+ npm install @toapi/worker
11
+ ```
12
+
13
+ ## Quick start
14
+
15
+ ```ts
16
+ // service-worker.ts
17
+ import { setupToapiWorker } from "@toapi/worker";
18
+
19
+ declare const self: ServiceWorkerGlobalScope;
20
+
21
+ setupToapiWorker();
22
+ ```
package/dist/cache.d.ts CHANGED
@@ -12,5 +12,5 @@ export declare function deleteCacheEntry(req: Request): Promise<void>;
12
12
  export declare function getMetadata(url: string): Promise<CacheMeta | null>;
13
13
  export declare function getCachedEntry(req: Request): Promise<Response | undefined>;
14
14
  export declare function invalidateTags(tags: string[]): Promise<void>;
15
- export declare function expireAll(): Promise<string[]>;
15
+ export declare function expireAll(): Promise<void>;
16
16
  //# sourceMappingURL=cache.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"cache.d.ts","sourceRoot":"","sources":["../src/cache.ts"],"names":[],"mappings":"AAMA,eAAO,MAAM,eAAe,SAAS,CAAC;AACtC,eAAO,MAAM,eAAe,SAAS,CAAC;AAEtC,MAAM,WAAW,SAAS;IACxB,IAAI,EAAE,MAAM,EAAE,CAAC;IACf,SAAS,EAAE,MAAM,GAAG,IAAI,CAAC;CAC1B;AAKD,wBAAgB,eAAe,yBAS9B;AAED,wBAAgB,SAAS,mBAMxB;AAED,wBAAsB,WAAW,kBAIhC;AAED,wBAAsB,eAAe,CAAC,GAAG,EAAE,OAAO,EAAE,GAAG,EAAE,QAAQ,iBAiChE;AAED,wBAAsB,gBAAgB,CAAC,GAAG,EAAE,OAAO,iBAmClD;AAED,wBAAsB,WAAW,CAAC,GAAG,EAAE,MAAM,6BAU5C;AAED,wBAAsB,cAAc,CAAC,GAAG,EAAE,OAAO,iCAGhD;AAED,wBAAsB,cAAc,CAAC,IAAI,EAAE,MAAM,EAAE,iBA4BlD;AAED,wBAAsB,SAAS,sBAwB9B"}
1
+ {"version":3,"file":"cache.d.ts","sourceRoot":"","sources":["../src/cache.ts"],"names":[],"mappings":"AAMA,eAAO,MAAM,eAAe,SAAS,CAAC;AACtC,eAAO,MAAM,eAAe,SAAS,CAAC;AAEtC,MAAM,WAAW,SAAS;IACxB,IAAI,EAAE,MAAM,EAAE,CAAC;IACf,SAAS,EAAE,MAAM,GAAG,IAAI,CAAC;CAC1B;AAKD,wBAAgB,eAAe,yBAS9B;AAED,wBAAgB,SAAS,mBAMxB;AAED,wBAAsB,WAAW,kBAIhC;AAED,wBAAsB,eAAe,CAAC,GAAG,EAAE,OAAO,EAAE,GAAG,EAAE,QAAQ,iBAiChE;AAED,wBAAsB,gBAAgB,CAAC,GAAG,EAAE,OAAO,iBAmClD;AAED,wBAAsB,WAAW,CAAC,GAAG,EAAE,MAAM,6BAU5C;AAED,wBAAsB,cAAc,CAAC,GAAG,EAAE,OAAO,iCAGhD;AAED,wBAAsB,cAAc,CAAC,IAAI,EAAE,MAAM,EAAE,iBA4BlD;AAED,wBAAsB,SAAS,kBAW9B"}
package/dist/cache.js CHANGED
@@ -104,9 +104,9 @@ export async function invalidateTags(tags) {
104
104
  const tagsStore = tx.objectStore(TAGS_STORE_NAME);
105
105
  const metaStore = tx.objectStore(META_STORE_NAME);
106
106
  for (const tag of tags) {
107
- const req = tagsStore.get(tag);
108
- req.onsuccess = () => {
109
- const urls = req.result ?? [];
107
+ const tagRequest = tagsStore.get(tag);
108
+ tagRequest.onsuccess = () => {
109
+ const urls = tagRequest.result ?? [];
110
110
  for (const url of urls) {
111
111
  metaStore.delete(url);
112
112
  deletes.push(cache.delete(url));
@@ -119,23 +119,10 @@ export async function invalidateTags(tags) {
119
119
  export async function expireAll() {
120
120
  const metaDb = await openCacheMetaDB();
121
121
  return new Promise((resolve, reject) => {
122
- const tags = new Set();
123
122
  const tx = metaDb.transaction([META_STORE_NAME], "readwrite");
124
- tx.oncomplete = () => resolve(Array.from(tags));
123
+ tx.oncomplete = () => resolve();
125
124
  tx.onerror = () => reject(new Error(`Failed to expire all cache entries`));
126
125
  const metaStore = tx.objectStore(META_STORE_NAME);
127
- const cursorRequest = metaStore.openCursor();
128
- cursorRequest.onsuccess = () => {
129
- const cursor = cursorRequest.result;
130
- if (!cursor)
131
- return;
132
- const value = cursor.value;
133
- value.expiresAt = Date.now();
134
- for (const tag of value.tags) {
135
- tags.add(tag);
136
- }
137
- metaStore.put(value, cursor.key);
138
- cursor.continue();
139
- };
126
+ metaStore.clear();
140
127
  });
141
128
  }
@@ -1 +1 @@
1
- {"version":3,"file":"revalidation-stream.d.ts","sourceRoot":"","sources":["../src/revalidation-stream.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,KAAK,MAAM,EACZ,MAAM,eAAe,CAAC;AAMvB,UAAU,OAAO;IACf,GAAG,EAAE,MAAM,CAAC;IACZ,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED,wBAAsB,sBAAsB,CAAC,EAC3C,GAAG,EACH,OAAc,EACd,MAAM,EAAE,YAAY,GACrB,EAAE,OAAO,iBAqFT"}
1
+ {"version":3,"file":"revalidation-stream.d.ts","sourceRoot":"","sources":["../src/revalidation-stream.ts"],"names":[],"mappings":"AAAA,OAAO,EAIL,KAAK,MAAM,EACZ,MAAM,eAAe,CAAC;AAMvB,UAAU,OAAO;IACf,GAAG,EAAE,MAAM,CAAC;IACZ,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED,wBAAsB,sBAAsB,CAAC,EAC3C,GAAG,EACH,OAAc,EACd,MAAM,EAAE,YAAY,GACrB,EAAE,OAAO,iBAuFT"}
@@ -1,4 +1,4 @@
1
- import { INVALIDATION_POST_EVENT, TAGS_CONTENT_TYPE, } from "@toapi/common";
1
+ import { CONNECT_POST_EVENT, INVALIDATION_POST_EVENT, TAGS_CONTENT_TYPE, } from "@toapi/common";
2
2
  import { deleteCache, expireAll, invalidateTags } from "./cache.js";
3
3
  import { consoleFallback } from "./console-fallback.js";
4
4
  export async function listenForInvalidations({ url, timeout = 5000, logger: customLogger, }) {
@@ -29,15 +29,16 @@ export async function listenForInvalidations({ url, timeout = 5000, logger: cust
29
29
  }
30
30
  logger.info("Invalidation Stream Connection Established");
31
31
  try {
32
- const tags = await expireAll();
32
+ await expireAll();
33
33
  const clients = await self.clients.matchAll();
34
34
  for (const client of clients) {
35
- client.postMessage({ type: INVALIDATION_POST_EVENT, tags });
35
+ client.postMessage({ type: CONNECT_POST_EVENT });
36
36
  }
37
37
  logger.info("Marked all cached entries as expired");
38
38
  }
39
- catch {
40
- logger.warn("Failed to expire existing cache entries");
39
+ catch (error) {
40
+ const formattedError = error instanceof Error ? `${error.name} ${error.message}` : String(error);
41
+ logger.warn(`Failed to expire existing cache entries: ${formattedError}`);
41
42
  }
42
43
  try {
43
44
  let buffer = "";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@toapi/worker",
3
- "version": "1.2.4",
3
+ "version": "1.3.0",
4
4
  "author": {
5
5
  "name": "Michel Smola",
6
6
  "email": "michel.smola@farbenmeer.de"
@@ -20,18 +20,18 @@
20
20
  }
21
21
  },
22
22
  "dependencies": {
23
- "@toapi/common": "^1.2.4"
23
+ "@toapi/common": "^1.3.0"
24
24
  },
25
25
  "devDependencies": {
26
26
  "@types/node": "^25.0.3",
27
- "vitest": "^4.0.16"
27
+ "vitest": "^5.0.0"
28
28
  },
29
29
  "peerDependencies": {
30
- "typescript": "^5 || ^6.0.0"
30
+ "typescript": "^5 || ^6.0.0 || ^7.0.0"
31
31
  },
32
32
  "repository": {
33
33
  "type": "git",
34
- "url": "git+https://github.com/farbenmeer/tapi.git"
34
+ "url": "git+https://github.com/toapi-js/toapi.git"
35
35
  },
36
36
  "scripts": {
37
37
  "build": "tsc --noEmit false",