Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions packages/cache/__tests__/instanceSlug.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import * as http from 'http'
import {AddressInfo} from 'net'
import fetch from 'node-fetch'
import {getCacheEntry, reserveCache} from '../src/internal/cacheHttpClient'

jest.mock('node-fetch', () => jest.fn())
jest.mock('../src/internal/downloadUtils', () => ({}))

test.each([undefined, 'enterprise'])(
'sends instance slug on reserve and restore (%s)',
async slug => {
const previous = {...process.env}
let receivedSlug: string | string[] | undefined
const server = http.createServer((req, res) => {
receivedSlug = req.headers['x-blacksmith-instance-slug']
req.resume()
res.writeHead(200, {'Content-Type': 'application/json'})
res.end(JSON.stringify({cacheId: 1}))
})
await new Promise<void>(resolve => server.listen(0, '127.0.0.1', resolve))
try {
if (slug) process.env.BLACKSMITH_INSTANCE_SLUG = slug
else delete process.env.BLACKSMITH_INSTANCE_SLUG
process.env.BLACKSMITH_CACHE_URL = `http://127.0.0.1:${
(server.address() as AddressInfo).port
}`
process.env.GITHUB_REPO_NAME = 'owner/repo'
process.env.BLACKSMITH_CACHE_TOKEN = 'test-token'
await reserveCache('key', ['node_modules'])
expect(receivedSlug).toBe(slug || 'github')
;(fetch as jest.MockedFunction<typeof fetch>).mockResolvedValue({
status: 204
} as any)
await getCacheEntry(['key'], ['node_modules'])
expect(fetch).toHaveBeenCalledWith(
expect.any(String),
expect.objectContaining({
headers: expect.objectContaining({
'X-Blacksmith-Instance-Slug': slug || 'github'
})
})
)
} finally {
process.env = previous
await new Promise<void>((resolve, reject) =>
server.close(err => (err ? reject(err) : resolve()))
)
}
}
)
8 changes: 6 additions & 2 deletions packages/cache/src/internal/cacheHttpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ function getRequestOptions(): RequestOptions {
const requestOptions: RequestOptions = {
headers: {
Accept: createAcceptHeader('application/json', '6.0-preview.1'),
'X-Blacksmith-Instance-Slug':
process.env['BLACKSMITH_INSTANCE_SLUG'] || 'github',
'X-Github-Repo-Name': process.env['GITHUB_REPO_NAME']
}
}
Expand Down Expand Up @@ -141,6 +143,8 @@ export async function getCacheEntry(
headers: {
Accept: createAcceptHeader('application/json', '6.0-preview.1'),
'X-Github-Repo-Name': repoName || '',
'X-Blacksmith-Instance-Slug':
process.env['BLACKSMITH_INSTANCE_SLUG'] || 'github',
Authorization: `Bearer ${cacheToken}`,
'X-Cache-Region': process.env['BLACKSMITH_REGION'] ?? 'eu-central',
'User-Agent': 'node-fetch/cache'
Expand Down Expand Up @@ -182,7 +186,7 @@ export async function getCacheEntry(
} catch (error) {
const isTimeout = error.name === 'AbortError'
const status = error.response?.status

if ((status && status >= 500) || isTimeout) {
retries++
if (retries <= maxRetries) {
Expand All @@ -198,7 +202,7 @@ export async function getCacheEntry(
continue
}
}

if (status) {
throw new Error(`Cache service responded with ${status}`)
} else if (isTimeout) {
Expand Down
Loading