@forge/kvs

2.0.5-next.12.0.5-next.1-experimental-e9e08bb
out/index.js
~out/index.jsModified
+31
Index: package/out/index.js
===================================================================
--- package/out/index.js
+++ package/out/index.js
@@ -4,8 +4,11 @@
 const tslib_1 = require("tslib");
 const kvs_1 = require("./kvs");
 const storage_api_1 = require("./storage-api");
 function getFetchClient() {
+    const localClient = getLocalStorageFetchClient();
+    if (localClient)
+        return localClient;
     const runtime = global.__forge_runtime__;
     if (runtime?.kvs?.url && runtime?.kvs?.host) {
         const { proxy, kvs, tracing } = runtime;
         return async function (path, options) {
@@ -28,8 +31,36 @@
             remote: 'kvs'
         }, path, options);
     };
 }
+function getLocalStorageFetchClient() {
+    if (process.env.FORGE_LOCAL_STORAGE !== '1')
+        return undefined;
+    const baseUrl = process.env.FORGE_LOCAL_KVS_URL;
+    const appId = process.env.FORGE_LOCAL_STORAGE_APP_ID;
+    const environmentId = process.env.FORGE_LOCAL_STORAGE_ENVIRONMENT_ID;
+    const installationId = process.env.FORGE_LOCAL_STORAGE_INSTALLATION_ID;
+    if (!baseUrl || !appId || !environmentId || !installationId) {
+        throw new Error('Forge local KVS routing is enabled but its local namespace is incomplete.');
+    }
+    if (baseUrl !== 'http://127.0.0.1:18090') {
+        throw new Error('Forge local KVS endpoint must be http://127.0.0.1:18090.');
+    }
+    const fetchImplementation = globalThis.__forge_local_fetch__;
+    if (typeof fetchImplementation !== 'function') {
+        throw new Error('Forge local KVS routing requires the tunnel local-fetch bridge.');
+    }
+    const endpoint = new URL(baseUrl);
+    return async (requestPath, options) => (await fetchImplementation(new URL(requestPath, endpoint), {
+        ...options,
+        headers: {
+            ...options?.headers,
+            'x-forge-app-id': appId,
+            'x-forge-environment-id': environmentId,
+            'x-forge-installation-id': installationId
+        }
+    }));
+}
 const storageApi = new storage_api_1.StorageApi(getFetchClient());
 const kvs = new kvs_1.KvsImpl(storageApi);
 exports.kvs = kvs;
 var conditions_1 = require("./conditions");