Skip to content
Draft
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
18 changes: 18 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/cache/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
"vitest": "^3.0.0"
},
"dependencies": {
"@netlify/otel": "^4.3.1",
"@netlify/runtime-utils": "2.2.0"
}
}
104 changes: 67 additions & 37 deletions packages/cache/src/bootstrap/cache.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { getTracer, withActiveSpan } from '@netlify/otel'
import { SpanStatusCode } from '@netlify/otel/opentelemetry'
import { base64Encode } from '@netlify/runtime-utils'

import { EnvironmentOptions, RequestContext, Operation, RequestContextFactory } from './environment.js'
Expand Down Expand Up @@ -105,11 +107,18 @@ export class NetlifyCache implements Cache {
const context = this.#getContext({ operation: Operation.Delete })

if (context) {
const resourceURL = extractAndValidateURL(request)

await fetch(`${context.url}/${toCacheKey(resourceURL)}`, {
headers: this[getInternalHeaders](context),
method: 'DELETE',
await withActiveSpan(getTracer(), 'cache.delete', async (span) => {
const resourceURL = extractAndValidateURL(request)

span?.setAttributes({
'cache.store': this.#name,
'cache.key': resourceURL.toString(),
})

await fetch(`${context.url}/${toCacheKey(resourceURL)}`, {
headers: this[getInternalHeaders](context),
method: 'DELETE',
})
})
}

Expand All @@ -129,21 +138,30 @@ export class NetlifyCache implements Cache {
return
}

const resourceURL = extractAndValidateURL(request)
const cacheURL = `${context.url}/${toCacheKey(resourceURL)}`
const response = await fetch(cacheURL, {
headers: {
...(request instanceof Request ? this[serializeRequestHeaders](request.headers) : {}),
...this[getInternalHeaders](context),
},
method: 'GET',
})

if (!response.ok) {
return
}
return await withActiveSpan(getTracer(), 'cache.read', async (span) => {
const resourceURL = extractAndValidateURL(request)
const cacheURL = `${context.url}/${toCacheKey(resourceURL)}`

span?.setAttributes({
'cache.store': this.#name,
'cache.key': resourceURL.toString(),
})

const response = await fetch(cacheURL, {
headers: {
...(request instanceof Request ? this[serializeRequestHeaders](request.headers) : {}),
...this[getInternalHeaders](context),
},
method: 'GET',
})

if (!response.ok) {
span?.setStatus({ code: SpanStatusCode.ERROR })
return
}

return response
return response
})
} catch {
// no-op
}
Expand Down Expand Up @@ -182,26 +200,38 @@ export class NetlifyCache implements Cache {
return
}

const resourceURL = extractAndValidateURL(request)

const cacheResponse = await fetch(`${context.url}/${toCacheKey(resourceURL)}`, {
body: response.body,
headers: {
...this[getInternalHeaders](context),
[HEADERS.ResourceHeaders]: this[serializeResponseHeaders](response.headers),
[HEADERS.ResourceStatus]: response.status.toString(),
},
// @ts-expect-error https://github.com/whatwg/fetch/pull/1457
duplex: 'half',
method: 'POST',
})
await withActiveSpan(getTracer(), 'cache.write', async (span) => {
const resourceURL = extractAndValidateURL(request)

if (!cacheResponse.ok) {
const errorDetail = cacheResponse.headers?.get(HEADERS.ErrorDetail) ?? ''
const errorMessage = ERROR_CODES[errorDetail as keyof typeof ERROR_CODES] || GENERIC_ERROR
span?.setAttributes({
'cache.store': this.#name,
'cache.key': resourceURL.toString(),
})

context.logger?.(`Failed to write to the cache: ${errorMessage}`)
}
const cacheResponse = await fetch(`${context.url}/${toCacheKey(resourceURL)}`, {
body: response.body,
headers: {
...this[getInternalHeaders](context),
[HEADERS.ResourceHeaders]: this[serializeResponseHeaders](response.headers),
[HEADERS.ResourceStatus]: response.status.toString(),
},
// @ts-expect-error https://github.com/whatwg/fetch/pull/1457
duplex: 'half',
method: 'POST',
})

if (!cacheResponse.ok) {
const errorDetail = cacheResponse.headers?.get(HEADERS.ErrorDetail) ?? ''
const errorMessage = ERROR_CODES[errorDetail as keyof typeof ERROR_CODES] || GENERIC_ERROR

span?.setStatus({
code: SpanStatusCode.ERROR,
message: errorMessage,
})

context.logger?.(`Failed to write to the cache: ${errorMessage}`)
}
})
}
}

Expand Down
4 changes: 2 additions & 2 deletions packages/cache/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
"allowImportingTsExtensions": true,
"emitDeclarationOnly": true,
"target": "ES2020",
"module": "es2020",
"module": "NodeNext",
"allowJs": true,
"declaration": true,
"declarationMap": false,
"sourceMap": false,
"outDir": "./dist",
"removeComments": false,
"strict": true,
"moduleResolution": "node",
"moduleResolution": "nodenext",
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
Expand Down