Skip to content
Merged
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
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
"prepare": "husky install",
"lint": "eslint --cache --report-unused-disable-directives --ignore-path .gitignore --max-warnings=0 . --fix",
"lint:ci": "eslint --cache --report-unused-disable-directives --ignore-path .gitignore --max-warnings=0 .",
"build": "npm run build",
"test": "npm run test",
"format": "prettier --write .",
"format:ci": "prettier --check ."
},
Expand Down
16 changes: 4 additions & 12 deletions packages/cache/src/bootstrap/cache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ describe('Cache API', () => {
})
const cache = new NetlifyCache({
base64Encode,
getHost: () => host,
getToken: () => token,
getURL: () => url,
getContext: () => ({ host, token, url }),
name: 'my-cache',
userAgent,
})
Expand Down Expand Up @@ -86,9 +84,7 @@ describe('Cache API', () => {
})
const cache = new NetlifyCache({
base64Encode,
getHost: () => host,
getToken: () => token,
getURL: () => url,
getContext: () => ({ host, token, url }),
name: 'my-cache',
userAgent,
})
Expand Down Expand Up @@ -129,9 +125,7 @@ describe('Cache API', () => {
})
const cache = new NetlifyCache({
base64Encode,
getHost: () => host,
getToken: () => token,
getURL: () => url,
getContext: () => ({ host, token, url }),
name: 'my-cache',
userAgent,
})
Expand Down Expand Up @@ -172,9 +166,7 @@ describe('Cache API', () => {
})
const cache = new NetlifyCache({
base64Encode,
getHost: () => host,
getToken: () => token,
getURL: () => url,
getContext: () => ({ host, token, url }),
name: 'my-cache',
userAgent,
})
Expand Down
33 changes: 16 additions & 17 deletions packages/cache/src/bootstrap/cache.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Base64Encoder, EnvironmentOptions, Factory } from './environment.js'
import type { Base64Encoder, EnvironmentOptions, RequestContext, RequestContextFactory } from './environment.js'

import * as HEADERS from '../headers.js'

Expand All @@ -17,28 +17,24 @@ const serializeResourceHeaders = Symbol('serializeResourceHeaders')

export class NetlifyCache implements Cache {
#base64Encode: Base64Encoder
#getHost?: Factory<string>
#getToken: Factory<string>
#getURL: Factory<string>
#getContext: RequestContextFactory
#name: string
#userAgent?: string

constructor({ base64Encode, getHost, getToken, getURL, name, userAgent }: NetlifyCacheOptions) {
constructor({ base64Encode, getContext, name, userAgent }: NetlifyCacheOptions) {
this.#base64Encode = base64Encode
this.#getHost = getHost
this.#getToken = getToken
this.#getURL = getURL
this.#getContext = getContext
this.#name = name
this.#userAgent = userAgent
}

private [getInternalHeaders]() {
private [getInternalHeaders](requestContext: RequestContext) {
const { host, token } = requestContext
const headers: Record<string, string> = {
Authorization: `Bearer ${this.#getToken()}`,
Authorization: `Bearer ${token}`,
[HEADERS.ResourceStore]: this.#name,
}

const host = this.#getHost?.()
if (host) {
headers[HEADERS.NetlifyForwardedHost] = host
}
Expand Down Expand Up @@ -82,10 +78,11 @@ export class NetlifyCache implements Cache {

// eslint-disable-next-line class-methods-use-this, require-await, @typescript-eslint/no-unused-vars
async delete(request: RequestInfo) {
const context = this.#getContext()
const resourceURL = extractAndValidateURL(request)

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

Expand All @@ -100,10 +97,11 @@ export class NetlifyCache implements Cache {

async match(request: RequestInfo) {
try {
const context = this.#getContext()
const resourceURL = extractAndValidateURL(request)
const cacheURL = `${this.#getURL()}/${toCacheKey(resourceURL)}`
const cacheURL = `${context.url}/${toCacheKey(resourceURL)}`
const response = await fetch(cacheURL, {
headers: this[getInternalHeaders](),
headers: this[getInternalHeaders](context),
method: 'GET',
})

Expand Down Expand Up @@ -145,12 +143,13 @@ export class NetlifyCache implements Cache {
throw new TypeError("Cannot cache response with 'Vary: *' header.")
}

const context = this.#getContext()
const resourceURL = extractAndValidateURL(request)

await fetch(`${this.#getURL()}/${toCacheKey(resourceURL)}`, {
await fetch(`${context.url}/${toCacheKey(resourceURL)}`, {
body: response.body,
headers: {
...this[getInternalHeaders](),
...this[getInternalHeaders](context),
[HEADERS.ResourceHeaders]: this[serializeResourceHeaders](response.headers),
[HEADERS.ResourceStatus]: response.status.toString(),
},
Expand Down
13 changes: 9 additions & 4 deletions packages/cache/src/bootstrap/environment.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
export type Base64Encoder = (input: string) => string
export type Factory<T> = () => T

export interface EnvironmentOptions {
base64Encode: Base64Encoder
getHost: Factory<string>
getToken: Factory<string>
getURL: Factory<string>
getContext: RequestContextFactory
userAgent?: string
}

export type RequestContextFactory = () => RequestContext

export interface RequestContext {
host: string
token: string
url: string
}
7 changes: 1 addition & 6 deletions packages/cache/src/bootstrap/main.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
import { Base64Encoder, Factory } from './environment.ts'

export type { Base64Encoder }
export type HostFactory = Factory<string>
export type TokenFactory = Factory<string>
export type URLFactory = Factory<string>
export type { Base64Encoder, RequestContextFactory } from './environment.ts'
export { NetlifyCache } from './cache.ts'
export { NetlifyCacheStorage } from './cachestorage.ts'
4 changes: 1 addition & 3 deletions packages/cache/src/fetchwithcache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ let originalCaches = globalThis.caches
beforeAll(() => {
globalThis.caches = new NetlifyCacheStorage({
base64Encode,
getHost: () => host,
getToken: () => token,
getURL: () => url,
getContext: () => ({ host, token, url }),
})
})

Expand Down
Loading