From 516731058882cc81d67705ad0e3469e61d179461 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduardo=20Bou=C3=A7as?= Date: Thu, 2 Nov 2023 09:45:46 +0000 Subject: [PATCH] feat: load context from global variable --- README.md | 6 +- src/environment.ts | 20 +++---- src/main.test.ts | 140 ++++++++++++++++++++++++++++++++------------- 3 files changed, 112 insertions(+), 54 deletions(-) diff --git a/README.md b/README.md index 71ec84b..9a1d93e 100644 --- a/README.md +++ b/README.md @@ -95,9 +95,9 @@ Rather than explicitly passing the configuration context to the `getStore` metho environment. This is particularly useful for setups where the configuration data is held by one system and the data needs to be accessed in another system, with no direct communication between the two. -To do this, the system that holds the configuration data should set an environment variable called -`NETLIFY_BLOBS_CONTEXT` with a Base64-encoded, JSON-stringified representation of an object with the following -properties: +To do this, the system that holds the configuration data should set a global variable called `netlifyBlobsContext` or an +environment variable called `NETLIFY_BLOBS_CONTEXT` with a Base64-encoded, JSON-stringified representation of an object +with the following properties: - `apiURL` (optional) or `edgeURL`: URL of the Netlify API (for [API access](#api-access)) or the edge endpoint (for [Edge access](#edge-access)) diff --git a/src/environment.ts b/src/environment.ts index 5d4e1e6..2e4ec1e 100644 --- a/src/environment.ts +++ b/src/environment.ts @@ -1,14 +1,12 @@ import { Buffer } from 'node:buffer' import { env } from 'node:process' -/** - * The name of the environment variable that holds the context in a Base64, - * JSON-encoded object. If we ever need to change the encoding or the shape - * of this object, we should bump the version and create a new variable, so - * that the client knows how to consume the data and can advise the user to - * update the client if needed. - */ -const NETLIFY_CONTEXT_VARIABLE = 'NETLIFY_BLOBS_CONTEXT' +declare global { + // Using `var` so that the declaration is hoisted in such a way that we can + // reference it before it's initialized. + // eslint-disable-next-line no-var + var netlifyBlobsContext: unknown +} /** * The context object that we expect in the environment. @@ -22,11 +20,13 @@ export interface EnvironmentContext { } export const getEnvironmentContext = (): EnvironmentContext => { - if (!env[NETLIFY_CONTEXT_VARIABLE]) { + const context = globalThis.netlifyBlobsContext || env.NETLIFY_BLOBS_CONTEXT + + if (typeof context !== 'string' || !context) { return {} } - const data = Buffer.from(env[NETLIFY_CONTEXT_VARIABLE], 'base64').toString() + const data = Buffer.from(context, 'base64').toString() try { return JSON.parse(data) as EnvironmentContext diff --git a/src/main.test.ts b/src/main.test.ts index fb50705..a54673d 100644 --- a/src/main.test.ts +++ b/src/main.test.ts @@ -27,6 +27,7 @@ beforeAll(async () => { afterEach(() => { delete env.NETLIFY_BLOBS_CONTEXT + delete globalThis.netlifyBlobsContext }) const deployID = '6527dfab35be400008332a1d' @@ -239,51 +240,108 @@ describe('get', () => { expect(mockStore.fulfilled).toBeTruthy() }) - test('Loads credentials from the environment', async () => { - const tokens = ['some-token-1', 'another-token-2'] - const mockStore = new MockFetch() - .get({ - headers: { authorization: `Bearer ${tokens[0]}` }, - response: new Response(value), - url: `${edgeURL}/${siteID}/images/${key}`, - }) - .get({ - headers: { authorization: `Bearer ${tokens[0]}` }, - response: new Response(value), - url: `${edgeURL}/${siteID}/images/${key}`, - }) - .get({ - headers: { authorization: `Bearer ${tokens[1]}` }, - response: new Response(value), - url: `${edgeURL}/${siteID}/images/${key}`, - }) - .get({ - headers: { authorization: `Bearer ${tokens[1]}` }, - response: new Response(value), - url: `${edgeURL}/${siteID}/images/${key}`, - }) - - globalThis.fetch = mockStore.fetch - - for (let index = 0; index <= 1; index++) { - const context = { - edgeURL, - siteID, - token: tokens[index], + describe('Loads credentials from the environment', () => { + test('From the `NETLIFY_BLOBS_CONTEXT` environment variable', async () => { + const tokens = ['some-token-1', 'another-token-2'] + const mockStore = new MockFetch() + .get({ + headers: { authorization: `Bearer ${tokens[0]}` }, + response: new Response(value), + url: `${edgeURL}/${siteID}/images/${key}`, + }) + .get({ + headers: { authorization: `Bearer ${tokens[0]}` }, + response: new Response(value), + url: `${edgeURL}/${siteID}/images/${key}`, + }) + .get({ + headers: { authorization: `Bearer ${tokens[1]}` }, + response: new Response(value), + url: `${edgeURL}/${siteID}/images/${key}`, + }) + .get({ + headers: { authorization: `Bearer ${tokens[1]}` }, + response: new Response(value), + url: `${edgeURL}/${siteID}/images/${key}`, + }) + + globalThis.fetch = mockStore.fetch + + for (let index = 0; index <= 1; index++) { + const context = { + edgeURL, + siteID, + token: tokens[index], + } + + env.NETLIFY_BLOBS_CONTEXT = Buffer.from(JSON.stringify(context)).toString('base64') + + const store = getStore('images') + + const string = await store.get(key) + expect(string).toBe(value) + + const stream = await store.get(key, { type: 'stream' }) + expect(await streamToString(stream as unknown as NodeJS.ReadableStream)).toBe(value) } - env.NETLIFY_BLOBS_CONTEXT = Buffer.from(JSON.stringify(context)).toString('base64') - - const store = getStore('images') - - const string = await store.get(key) - expect(string).toBe(value) + expect(mockStore.fulfilled).toBeTruthy() + }) - const stream = await store.get(key, { type: 'stream' }) - expect(await streamToString(stream as unknown as NodeJS.ReadableStream)).toBe(value) - } + test('From the `netlifyBlobsContext` global variable', async () => { + const tokens = ['some-token-1', 'another-token-2'] + const mockStore = new MockFetch() + .get({ + headers: { authorization: `Bearer ${tokens[0]}` }, + response: new Response(value), + url: `${edgeURL}/${siteID}/images/${key}`, + }) + .get({ + headers: { authorization: `Bearer ${tokens[0]}` }, + response: new Response(value), + url: `${edgeURL}/${siteID}/images/${key}`, + }) + .get({ + headers: { authorization: `Bearer ${tokens[1]}` }, + response: new Response(value), + url: `${edgeURL}/${siteID}/images/${key}`, + }) + .get({ + headers: { authorization: `Bearer ${tokens[1]}` }, + response: new Response(value), + url: `${edgeURL}/${siteID}/images/${key}`, + }) + + globalThis.fetch = mockStore.fetch + + for (let index = 0; index <= 1; index++) { + const context1 = { + edgeURL, + siteID, + token: 'not-the-right-token', + } + + env.NETLIFY_BLOBS_CONTEXT = Buffer.from(JSON.stringify(context1)).toString('base64') + + const context2 = { + edgeURL, + siteID, + token: tokens[index], + } + + globalThis.netlifyBlobsContext = Buffer.from(JSON.stringify(context2)).toString('base64') + + const store = getStore('images') + + const string = await store.get(key) + expect(string).toBe(value) + + const stream = await store.get(key, { type: 'stream' }) + expect(await streamToString(stream as unknown as NodeJS.ReadableStream)).toBe(value) + } - expect(mockStore.fulfilled).toBeTruthy() + expect(mockStore.fulfilled).toBeTruthy() + }) }) }) })