@lls/offline 24.22.0-b609d443 → 24.22.0-bfdb7edf

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.
@@ -19,7 +19,7 @@ var makeSyncWorker = (offlineWorker2, methodNames, dbg) => {
19
19
  const makeFn = (methodName) => {
20
20
  return ({ ports, ...arg } = {}) => {
21
21
  return new Promise((resolve, reject) => {
22
- const eventId = `${methodName}:${Date.now()}`;
22
+ const eventId = `${methodName}:${Date.now()}-${Math.random().toFixed(5)}`;
23
23
  dic[eventId] = { resolve, reject };
24
24
  offlineWorker2.postMessage({ eventId, methodName, arg }, ports);
25
25
  });
@@ -62,7 +62,7 @@ var makeSyncHandler = a(
62
62
  })
63
63
  );
64
64
  var jsonToBuf = (json) => new TextEncoder().encode(JSON.stringify(json));
65
- var openCache = async () => caches.open(`LLS_OFFLINE_${process.env.CONF_ENV}`);
65
+ var openCache = async (mode) => caches.open(`LLS_OFFLINE_${mode}`);
66
66
 
67
67
  // src/worker/swFetchScreenCall.ts
68
68
  var zipSlugName = (str) => str.replace(/[^a-zA-Z0-9-.]/g, "");
@@ -75,6 +75,15 @@ var cocoreFetchPartialPageScreen = async (_ev, { offlineWorkerSync: offlineWorke
75
75
  const gqlPage = await offlineWorkerSync2.loadPageContent({ entryId });
76
76
  return { data: gqlPage };
77
77
  };
78
+ var cocoreFetchBookScreen = async (_ev, { offlineWorkerSync: offlineWorkerSync2, variables, knownLinks: knownLinks2 }) => {
79
+ const key = `gqls/${zipSlugName(variables.uris[0] || "")}.json`;
80
+ const entryId = knownLinks2.get(key);
81
+ if (!entryId) {
82
+ throw new Error(`uri not found ${key}`);
83
+ }
84
+ const gqlBook = await offlineWorkerSync2.loadPageContent({ entryId });
85
+ return { data: gqlBook };
86
+ };
78
87
  var cocoreFetchPartialPageSiblingsScreen = async (_ev) => {
79
88
  return { data: { viewer: {} } };
80
89
  };
@@ -189,6 +198,7 @@ var coreFetchPagesApi = async (ev, { offlineWorkerSync: offlineWorkerSync2, vari
189
198
  };
190
199
  var handles = {
191
200
  coreFetchBookDrawerScreen,
201
+ cocoreFetchBookScreen,
192
202
  coreFetchPagesApi,
193
203
  cocoreFetchPartialPageScreen,
194
204
  cocoreFetchPartialPageSiblingsScreen
@@ -220,21 +230,20 @@ var setKnownLinks = (knownLinks2, { entryNames, bookId }) => {
220
230
  };
221
231
 
222
232
  // src/worker/fetch-worker.ts
223
- var fetchAsset = async (ev, { entryId, mimeType, offlineWorkerSync: offlineWorkerSync2 }) => {
233
+ var STORAGE = "cache";
234
+ var CONF_ENV = "development";
235
+ var fetchAsset = async (_ev, { entryId, mimeType, offlineWorkerSync: offlineWorkerSync2 }) => {
224
236
  try {
225
237
  const buf = await offlineWorkerSync2.readZipEntry(entryId);
226
- const url = ev.request.url;
227
238
  return new Response(buf, {
228
239
  headers: {
229
240
  "Cache-Control": "max-age=600, s-maxage=600, public, proxy-revalidate",
241
+ "Content-Length": buf.byteLength,
230
242
  "Content-Type": mimeType
231
243
  },
232
244
  // @ts-ignore: poor ts typing
233
- ok: true,
234
- redirected: false,
235
245
  status: 200,
236
- type: "cors",
237
- url
246
+ statusText: "OK"
238
247
  });
239
248
  } catch (e) {
240
249
  console.error("FAILED fetchAsset", e);
@@ -242,8 +251,7 @@ var fetchAsset = async (ev, { entryId, mimeType, offlineWorkerSync: offlineWorke
242
251
  // @ts-ignore: poor ts typing
243
252
  ok: false,
244
253
  status: 500,
245
- type: "cors",
246
- url: ev.request.url
254
+ statusText: e?.toString?.() || "fail fetch"
247
255
  });
248
256
  }
249
257
  };
@@ -286,7 +294,7 @@ var ACCEPTED_EXTENSIONS = {
286
294
  ".webp": "image/webp"
287
295
  };
288
296
  var putInCache = async (request, response) => {
289
- const cache = await openCache();
297
+ const cache = await openCache(CONF_ENV);
290
298
  await cache.put(request, response);
291
299
  };
292
300
  var onlineFirst = async (request, event) => {
@@ -315,7 +323,6 @@ var onlineFirst = async (request, event) => {
315
323
  return responseFromCache;
316
324
  }
317
325
  };
318
- var STORAGE = "cache";
319
326
  var swSelf = self;
320
327
  swSelf.addEventListener("fetch", (ev) => {
321
328
  const url = ev.request.url;
@@ -362,16 +369,23 @@ var initChannel = async (__, { ports }) => {
362
369
  );
363
370
  offlineWorkerSync = sender;
364
371
  offlineWorker.onmessage = handler.handle;
365
- offlineWorkerSync.on("setKnownLinks", setKnownLinks.bind(null, setKnownLinks));
372
+ offlineWorkerSync.on("setKnownLinks", setKnownLinks.bind(null, knownLinks));
366
373
  offlineWorker.postMessage({ methodName: "sendAppReady" });
367
374
  };
368
375
  var knownLinks = /* @__PURE__ */ new Map();
369
- var configure = ({ storage } = {}) => {
376
+ var configure = ({
377
+ storage,
378
+ confEnv
379
+ } = {}) => {
370
380
  STORAGE = "cache";
381
+ CONF_ENV = "development";
371
382
  if (typeof storage !== "undefined") {
372
383
  STORAGE = storage;
373
384
  }
374
- console.info("sw::configure", { STORAGE });
385
+ if (typeof confEnv !== "undefined") {
386
+ CONF_ENV = confEnv;
387
+ }
388
+ console.info("sw::configure", { STORAGE, CONF_ENV });
375
389
  };
376
390
  swSelf.onmessage = async ({
377
391
  data: { methodName, arg } = {},
@@ -30,7 +30,7 @@ var makeSyncWorker = (offlineWorker, methodNames, dbg) => {
30
30
  const makeFn = (methodName) => {
31
31
  return ({ ports, ...arg } = {}) => {
32
32
  return new Promise((resolve, reject) => {
33
- const eventId = `${methodName}:${Date.now()}`;
33
+ const eventId = `${methodName}:${Date.now()}-${Math.random().toFixed(5)}`;
34
34
  dic[eventId] = { resolve, reject };
35
35
  offlineWorker.postMessage({ eventId, methodName, arg }, ports);
36
36
  });
@@ -83,7 +83,7 @@ var combineHandlers = (...handlers) => {
83
83
  console.error("no handlers matched", methodName);
84
84
  };
85
85
  };
86
- var openCache = async () => caches.open(`LLS_OFFLINE_${process.env.CONF_ENV}`);
86
+ var openCache = async (mode) => caches.open(`LLS_OFFLINE_${mode}`);
87
87
 
88
88
  // src/worker/wwApi.ts
89
89
  var STATE = {
@@ -844,6 +844,7 @@ var loadZip = async (buf, bookId) => {
844
844
  var CHECK_UPDATES = true;
845
845
  var STORAGE = "cache";
846
846
  var ZIP_BASE_URL = "https://ci.lls.fr/artefacts/content/development";
847
+ var CONF_ENV = "development";
847
848
  var LOADED_BOOKS = /* @__PURE__ */ new Map();
848
849
  var fetchZipFile = async (zipUrl, { forceRefresh } = {}) => {
849
850
  const fileName = zipUrl.split("/").slice(-1)[0];
@@ -923,7 +924,7 @@ var fetchZipFile = async (zipUrl, { forceRefresh } = {}) => {
923
924
  await accessHandle.write(arrayBuffer);
924
925
  await accessHandle.close();
925
926
  } else {
926
- const cache = await openCache();
927
+ const cache = await openCache(CONF_ENV);
927
928
  const response2 = new Response(arrayBuffer, {
928
929
  headers: {
929
930
  "Cache-Control": "max-age=600, s-maxage=600, public, proxy-revalidate",
@@ -1024,9 +1025,11 @@ var loadBooks = async ({ primaryOnly = false } = {}) => {
1024
1025
  const opfsRoot = await navigator.storage.getDirectory();
1025
1026
  entries = await opfsRoot.values();
1026
1027
  } else {
1027
- const cache = await openCache();
1028
+ const cache = await openCache(CONF_ENV);
1028
1029
  const keys = await cache.keys();
1029
- const zips = keys.filter((key2) => key2.url.endsWith(".zip"));
1030
+ const zips = keys.filter((key2) => {
1031
+ return key2.url.match(/lecture-cp\.zip/);
1032
+ });
1030
1033
  entries = zips.map((zip) => ({ name: String(zip.url.split("/").pop()) }));
1031
1034
  }
1032
1035
  for await (const entry of entries) {
@@ -1049,11 +1052,13 @@ var loadBooks = async ({ primaryOnly = false } = {}) => {
1049
1052
  var configure = ({
1050
1053
  storage,
1051
1054
  checkUpdates,
1052
- zipBaseUrl
1055
+ zipBaseUrl,
1056
+ confEnv
1053
1057
  }) => {
1054
1058
  CHECK_UPDATES = true;
1055
1059
  STORAGE = "cache";
1056
1060
  ZIP_BASE_URL = "https://ci.lls.fr/artefacts/content/development";
1061
+ CONF_ENV = "development";
1057
1062
  if (typeof storage !== "undefined") {
1058
1063
  STORAGE = storage;
1059
1064
  }
@@ -1063,7 +1068,10 @@ var configure = ({
1063
1068
  if (typeof zipBaseUrl !== "undefined") {
1064
1069
  ZIP_BASE_URL = zipBaseUrl;
1065
1070
  }
1066
- console.info("ow::configure", { STORAGE, CHECK_UPDATES, ZIP_BASE_URL });
1071
+ if (typeof confEnv !== "undefined") {
1072
+ CONF_ENV = confEnv;
1073
+ }
1074
+ console.info("ow::configure", { STORAGE, CHECK_UPDATES, ZIP_BASE_URL, CONF_ENV });
1067
1075
  };
1068
1076
  var loadPreload2 = async () => {
1069
1077
  const zipUrl = "https://lls-ci-fr.s3.eu-west-3.amazonaws.com/artefacts/content/development/preload.zip";
package/lib/index.js CHANGED
@@ -30,7 +30,7 @@ var makeSyncWorker = (offlineWorker2, methodNames, dbg) => {
30
30
  const makeFn = (methodName) => {
31
31
  return ({ ports, ...arg } = {}) => {
32
32
  return new Promise((resolve, reject) => {
33
- const eventId = `${methodName}:${Date.now()}`;
33
+ const eventId = `${methodName}:${Date.now()}-${Math.random().toFixed(5)}`;
34
34
  dic[eventId] = { resolve, reject };
35
35
  offlineWorker2.postMessage({ eventId, methodName, arg }, ports);
36
36
  });
@@ -125,10 +125,11 @@ var installWorker = async ({
125
125
  nativeListZips,
126
126
  storage,
127
127
  zipBaseUrl,
128
- checkUpdates
128
+ checkUpdates,
129
+ confEnv
129
130
  } = {}) => {
130
131
  const fetchWorker = await fetchWorkerPromise;
131
- const configureOptions = { storage, zipBaseUrl, checkUpdates };
132
+ const configureOptions = { storage, zipBaseUrl, checkUpdates, confEnv };
132
133
  if (nativeReadFile) {
133
134
  const wwHandler = makeSyncHandler(offlineWorker, { nativeReadFile, nativeWriteFile, nativeListZips });
134
135
  offlineWorker.onmessage = combineHandlers(handler, wwHandler);
@@ -162,7 +163,8 @@ var useOfflineWorker_default = ({
162
163
  nativeListZips,
163
164
  checkUpdates,
164
165
  storage,
165
- zipBaseUrl
166
+ zipBaseUrl,
167
+ confEnv
166
168
  } = {}) => {
167
169
  const [ready, setReady] = useState(false);
168
170
  useEffect(() => {
@@ -174,14 +176,15 @@ var useOfflineWorker_default = ({
174
176
  nativeListZips,
175
177
  storage,
176
178
  zipBaseUrl,
177
- checkUpdates
179
+ checkUpdates,
180
+ confEnv
178
181
  });
179
182
  await Promise.all([workersReadyPromise, setBooksLoadedPromise]);
180
183
  fetch(window.location.href.replace(window.location.search, ""));
181
184
  setReady(true);
182
185
  };
183
186
  fn();
184
- }, [primaryOnly, nativeReadFile, nativeWriteFile, nativeListZips, storage, zipBaseUrl, checkUpdates]);
187
+ }, [primaryOnly, nativeReadFile, nativeWriteFile, nativeListZips, storage, zipBaseUrl, checkUpdates, confEnv]);
185
188
  const loadBook = useCallback(
186
189
  async ({ id, forceRefresh }, cbk) => {
187
190
  offlineWorkerSync.on("opfsProgress", cbk);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lls/offline",
3
- "version": "24.22.0-b609d443",
3
+ "version": "24.22.0-bfdb7edf",
4
4
  "description": "offline",
5
5
  "main": "./lib/index.js",
6
6
  "module": "./lib/index.js",
@@ -52,7 +52,7 @@
52
52
  "vite": "7.0.0",
53
53
  "vite-tsconfig-paths": "5.1.4",
54
54
  "zod": "3.25.50",
55
- "@lls/vitescripts": "24.22.0-b609d443"
55
+ "@lls/vitescripts": "24.22.0-bfdb7edf"
56
56
  },
57
57
  "dependencies": {
58
58
  "fflate": "0.8.2"