From 4f1a4a3f2aca8992afdfafa02afecb55e8f535bd Mon Sep 17 00:00:00 2001 From: Sameh Khamis Date: Wed, 18 Nov 2026 09:17:33 -0800 Subject: [PATCH] Let an application ask the library catalog with its own fetch (#1810) createApplicationTools({ library: { fetchImpl } }) hands the fetch to the library source, so a server that wants the application's credentials on its library routes answers the catalog instead of refusing it. --- changelog.d/1810-library-catalog-fetch.md | 4 ++++ src/app/tools.js | 3 +++ src/library/controller.js | 4 ++++ src/library/controller.test.mjs | 28 +++++++++++++++++++++++ 4 files changed, 39 insertions(+) create mode 100644 changelog.d/1810-library-catalog-fetch.md diff --git a/changelog.d/1810-library-catalog-fetch.md b/changelog.d/1810-library-catalog-fetch.md new file mode 100644 index 0000000000..4911a5632f --- /dev/null +++ b/changelog.d/1810-library-catalog-fetch.md @@ -0,0 +1,4 @@ +- An application can ask the Data Library's catalog with its own fetch + (`createApplicationTools({ library: { fetchImpl } })`), for a server that + wants the application's credentials on its library routes (samehkhamis, + #1810). diff --git a/src/app/tools.js b/src/app/tools.js index 86c67ff791..29ed4f3448 100644 --- a/src/app/tools.js +++ b/src/app/tools.js @@ -50,6 +50,8 @@ import { overlayHost } from './layers/overlayHost.js'; * runs after each layer disable (a scene without one keeps that memory). * `library.fetchTile` and `library.fetchData` fetch the Data Library's tiles * and proxied datasets in place of the defaults (`createLibrarySource`). + * `library.fetchImpl` asks the library server (its catalog) in place of the + * page's fetch, for a server that wants the application's own credentials. * `library.requireCatalog` offers only what the server's library catalog * names, and nothing while it does not answer. */ @@ -165,6 +167,7 @@ export function createApplicationTools({ requestRender: governorRequestRender, // An application's own tile and dataset fetches, when it keeps them in // its own cache. + fetchImpl: library.fetchImpl ?? undefined, fetchTile: library.fetchTile ?? null, fetchData: library.fetchData ?? null, requireCatalog: library.requireCatalog === true, diff --git a/src/library/controller.js b/src/library/controller.js index f6773ca290..762961d8bc 100644 --- a/src/library/controller.js +++ b/src/library/controller.js @@ -54,6 +54,8 @@ function safeLocalStorage() { * (`createLibrarySource`). * @param {boolean} [options.requireCatalog] Offer only what the server's * catalog names, nothing while it does not answer; it is asked again later. + * @param {typeof fetch} [options.fetchImpl] Asks the library server (its + * catalog) in place of the page's fetch (`createLibrarySource`). */ export function createDataLibrary({ entries = LIBRARY_ENTRIES, @@ -70,9 +72,11 @@ export function createDataLibrary({ fetchTile = null, fetchData = null, requireCatalog = false, + fetchImpl = undefined, source = createLibrarySource({ entries, storage, + fetchImpl, fetchTile, fetchData, requireCatalog, diff --git a/src/library/controller.test.mjs b/src/library/controller.test.mjs index af6d656246..ece7445007 100644 --- a/src/library/controller.test.mjs +++ b/src/library/controller.test.mjs @@ -862,3 +862,31 @@ test('a required catalog that does not answer offers nothing and is asked again assert.ok(calls.every((url) => url === '/api/library/catalog')); library.destroy(); }); + +test("the library asks its catalog with the application's own fetch", async (t) => { + const manager = sealedManager(t); + const calls = []; + const library = createDataLibrary({ + entries, + ledger, + dataManager: manager, + services: {}, + storage: memoryStorage(null), + initialHash: '', + requireCatalog: true, + fetchImpl: async (url) => { + calls.push(String(url)); + return Response.json({ + entries: [{ id: 'night-lights', available: true, reason: null }], + }); + }, + setTimer: () => {}, + clearTimer: () => {}, + }); + await library.start(); + assert.deepEqual(calls, ['/api/library/catalog']); + assert.deepEqual( + library.list().map(({ entry }) => entry.id), + ['night-lights'], + ); +});