Skip to content

Commit 8abdfc5

Browse files
committed
refactor: update variable name
1 parent 4fc010e commit 8abdfc5

File tree

4 files changed

+93
-35
lines changed

4 files changed

+93
-35
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,9 @@ Rather than explicitly passing the configuration context to the `getStore` metho
9595
environment. This is particularly useful for setups where the configuration data is held by one system and the data
9696
needs to be accessed in another system, with no direct communication between the two.
9797

98-
To do this, the system that holds the configuration data should set an environment variable called `NETLIFY_BLOBS_1`
99-
with a Base64-encoded, JSON-stringified representation of an object with the following properties:
98+
To do this, the system that holds the configuration data should set an environment variable called
99+
`NETLIFY_BLOBS_CONTEXT` with a Base64-encoded, JSON-stringified representation of an object with the following
100+
properties:
100101

101102
- `apiURL` (optional) or `edgeURL`: URL of the Netlify API (for [API access](#api-access)) or the edge endpoint (for
102103
[Edge access](#edge-access))

src/client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { BlobInput, HTTPMethod } from './types.ts'
99
// of this object, we should bump the version and create a new variable, so
1010
// that the client knows how to consume the data and can advise the user to
1111
// update the client if needed.
12-
export const NETLIFY_CONTEXT_VARIABLE = 'NETLIFY_BLOBS_1'
12+
export const NETLIFY_CONTEXT_VARIABLE = 'NETLIFY_BLOBS_CONTEXT'
1313

1414
export interface Context {
1515
apiURL?: string

src/main.test.ts

Lines changed: 86 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { env, version as nodeVersion } from 'node:process'
44

55
import semver from 'semver'
66
import tmp from 'tmp-promise'
7-
import { describe, test, expect, beforeAll } from 'vitest'
7+
import { describe, test, expect, beforeAll, afterEach } from 'vitest'
88

99
import { MockFetch } from '../test/mock_fetch.js'
1010
import { streamToString } from '../test/util.js'
@@ -26,8 +26,12 @@ beforeAll(async () => {
2626
}
2727
})
2828

29-
const deployID = 'abcdef'
30-
const siteID = '12345'
29+
afterEach(() => {
30+
delete env.NETLIFY_BLOBS_CONTEXT
31+
})
32+
33+
const deployID = '6527dfab35be400008332a1d'
34+
const siteID = '9a003659-aaaa-0000-aaaa-63d3720d8621'
3135
const key = '54321'
3236
const complexKey = '/artista/canção'
3337
const value = 'some value'
@@ -191,7 +195,7 @@ describe('get', () => {
191195
})
192196
})
193197

194-
describe('With context credentials', () => {
198+
describe('With edge credentials', () => {
195199
test('Reads from the blob store', async () => {
196200
const mockStore = new MockFetch()
197201
.get({
@@ -265,6 +269,53 @@ describe('get', () => {
265269

266270
expect(mockStore.fulfilled).toBeTruthy()
267271
})
272+
273+
test('Loads credentials from the environment', async () => {
274+
const tokens = ['some-token-1', 'another-token-2']
275+
const mockStore = new MockFetch()
276+
.get({
277+
headers: { authorization: `Bearer ${tokens[0]}` },
278+
response: new Response(value),
279+
url: `${edgeURL}/${siteID}/images/${key}`,
280+
})
281+
.get({
282+
headers: { authorization: `Bearer ${tokens[0]}` },
283+
response: new Response(value),
284+
url: `${edgeURL}/${siteID}/images/${key}`,
285+
})
286+
.get({
287+
headers: { authorization: `Bearer ${tokens[1]}` },
288+
response: new Response(value),
289+
url: `${edgeURL}/${siteID}/images/${key}`,
290+
})
291+
.get({
292+
headers: { authorization: `Bearer ${tokens[1]}` },
293+
response: new Response(value),
294+
url: `${edgeURL}/${siteID}/images/${key}`,
295+
})
296+
297+
globalThis.fetch = mockStore.fetcher
298+
299+
for (let index = 0; index <= 1; index++) {
300+
const context = {
301+
edgeURL,
302+
siteID,
303+
token: tokens[index],
304+
}
305+
306+
env.NETLIFY_BLOBS_CONTEXT = Buffer.from(JSON.stringify(context)).toString('base64')
307+
308+
const store = getStore('images')
309+
310+
const string = await store.get(key)
311+
expect(string).toBe(value)
312+
313+
const stream = await store.get(key, { type: 'stream' })
314+
expect(await streamToString(stream as unknown as NodeJS.ReadableStream)).toBe(value)
315+
}
316+
317+
expect(mockStore.fulfilled).toBeTruthy()
318+
})
268319
})
269320

270321
test('Throws when the instance is missing required configuration properties', async () => {
@@ -560,7 +611,7 @@ describe('set', () => {
560611
})
561612
})
562613

563-
describe('With context credentials', () => {
614+
describe('With edge credentials', () => {
564615
test('Writes to the blob store', async () => {
565616
const mockStore = new MockFetch()
566617
.put({
@@ -712,7 +763,7 @@ describe('setJSON', () => {
712763
})
713764
})
714765

715-
describe('With context credentials', () => {
766+
describe('With edge credentials', () => {
716767
test('Writes to the blob store', async () => {
717768
const mockStore = new MockFetch().put({
718769
body: JSON.stringify({ value }),
@@ -829,7 +880,7 @@ describe('delete', () => {
829880
})
830881
})
831882

832-
describe('With context credentials', () => {
883+
describe('With edge credentials', () => {
833884
test('Deletes from the blob store', async () => {
834885
const mockStore = new MockFetch().delete({
835886
headers: { authorization: `Bearer ${edgeToken}` },
@@ -898,51 +949,56 @@ describe('delete', () => {
898949
})
899950
})
900951

901-
describe('Global client', () => {
902-
test('Reads from the blob store', async () => {
903-
const tokens = ['some-token-1', 'another-token-2']
952+
describe.only('Deploy scope', () => {
953+
test('Returns a deploy-scoped store if the `deployID` parameter is supplied', async () => {
954+
const mockToken = 'some-token'
904955
const mockStore = new MockFetch()
905956
.get({
906-
headers: { authorization: `Bearer ${tokens[0]}` },
957+
headers: { authorization: `Bearer ${mockToken}` },
907958
response: new Response(value),
908959
url: `${edgeURL}/${siteID}/images/${key}`,
909960
})
910961
.get({
911-
headers: { authorization: `Bearer ${tokens[0]}` },
962+
headers: { authorization: `Bearer ${mockToken}` },
912963
response: new Response(value),
913964
url: `${edgeURL}/${siteID}/images/${key}`,
914965
})
915966
.get({
916-
headers: { authorization: `Bearer ${tokens[1]}` },
967+
headers: { authorization: `Bearer ${mockToken}` },
917968
response: new Response(value),
918-
url: `${edgeURL}/${siteID}/images/${key}`,
969+
url: `${edgeURL}/${siteID}/${deployID}/${key}`,
919970
})
920971
.get({
921-
headers: { authorization: `Bearer ${tokens[1]}` },
972+
headers: { authorization: `Bearer ${mockToken}` },
922973
response: new Response(value),
923-
url: `${edgeURL}/${siteID}/images/${key}`,
974+
url: `${edgeURL}/${siteID}/${deployID}/${key}`,
924975
})
925976

926977
globalThis.fetch = mockStore.fetcher
927978

928-
for (let index = 0; index <= 1; index++) {
929-
const context = {
930-
edgeURL,
931-
deployID,
932-
siteID,
933-
token: tokens[index],
934-
}
979+
const context = {
980+
edgeURL,
981+
siteID,
982+
token: mockToken,
983+
}
935984

936-
env.NETLIFY_BLOBS_1 = Buffer.from(JSON.stringify(context)).toString('base64')
985+
env.NETLIFY_BLOBS_CONTEXT = Buffer.from(JSON.stringify(context)).toString('base64')
937986

938-
const store = getStore('images')
987+
const siteStore = getStore('images')
939988

940-
const string = await store.get(key)
941-
expect(string).toBe(value)
989+
const string1 = await siteStore.get(key)
990+
expect(string1).toBe(value)
942991

943-
const stream = await store.get(key, { type: 'stream' })
944-
expect(await streamToString(stream as unknown as NodeJS.ReadableStream)).toBe(value)
945-
}
992+
const stream1 = await siteStore.get(key, { type: 'stream' })
993+
expect(await streamToString(stream1 as unknown as NodeJS.ReadableStream)).toBe(value)
994+
995+
const deployStore = getStore({ deployID })
996+
997+
const string2 = await deployStore.get(key)
998+
expect(string2).toBe(value)
999+
1000+
const stream2 = await deployStore.get(key, { type: 'stream' })
1001+
expect(await streamToString(stream2 as unknown as NodeJS.ReadableStream)).toBe(value)
9461002

9471003
expect(mockStore.fulfilled).toBeTruthy()
9481004
})

src/store.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ interface GetNamedStoreOptions extends Context {
179179

180180
export const getStore: {
181181
(name: string): Store
182-
(options: GetDeployStoreOptions | GetNamedStoreOptions): Store
182+
(options: GetDeployStoreOptions | GetNamedStoreOptions | { deployID: string }): Store
183183
} = (input) => {
184184
if (typeof input === 'string') {
185185
const client = new Client()
@@ -188,7 +188,8 @@ export const getStore: {
188188
}
189189

190190
if ('deployID' in input) {
191-
const { deployID, ...context } = input
191+
const { deployID, ...otherProps } = input
192+
const context = 'siteID' in otherProps && 'token' in otherProps ? otherProps : undefined
192193
const client = new Client(context)
193194

194195
return new Store({ client, name: deployID })

0 commit comments

Comments
 (0)