firebase-on-cloudflare
Advanced tools
Comparing version 0.0.1 to 0.0.2
@@ -1,2 +0,2 @@ | ||
export declare function requestCacheKey(request: Request): Promise<Request>; | ||
export declare function varyHash(request: Request): Promise<string>; | ||
export declare function requestCacheKey(request: Request, seed: string): Promise<Request>; | ||
export declare function varyHash(request: Request, seed: string): Promise<string>; |
@@ -13,3 +13,3 @@ "use strict"; | ||
// requestCacheKey builds a Request object that can be used as a cloudflare cache key | ||
function requestCacheKey(request) { | ||
function requestCacheKey(request, seed) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
@@ -21,3 +21,3 @@ // Ignore non GETs | ||
// Hash request | ||
const hash = yield varyHash(request); | ||
const hash = yield varyHash(request, seed); | ||
// Get original path | ||
@@ -34,5 +34,4 @@ const url = new URL(request.url); | ||
// varyHash hashes a request's headers so we can use them in the cache key | ||
function varyHash(request) { | ||
function varyHash(request, seed) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const seed = '44'; | ||
const varyKeys = ['Accept-Encoding', 'Authorization', 'Cookie', 'X-CDN-Host']; | ||
@@ -39,0 +38,0 @@ const values = varyKeys.map(k => request.headers.get(k) || ''); |
import { Matcher } from './rewrites'; | ||
import { FirebaseConfig, FetchEvent } from './types'; | ||
interface ExtraOptions { | ||
headers?: HeaderOptions; | ||
seed?: string; | ||
} | ||
interface HeaderOptions { | ||
[key: string]: string | null; | ||
} | ||
export default class FirebaseOnCloudflare { | ||
@@ -8,3 +15,5 @@ matcher: Matcher; | ||
hostingEndpoint: URL; | ||
constructor(projectID: string, config: FirebaseConfig); | ||
globalHeaders: HeaderOptions; | ||
seed: string; | ||
constructor(projectID: string, config: FirebaseConfig, extra?: ExtraOptions); | ||
serve(event: FetchEvent): Promise<Response>; | ||
@@ -14,1 +23,2 @@ _serve(event: FetchEvent): Promise<Response>; | ||
} | ||
export {}; |
@@ -15,3 +15,3 @@ "use strict"; | ||
class FirebaseOnCloudflare { | ||
constructor(projectID, config) { | ||
constructor(projectID, config, extra) { | ||
// Keep project ID | ||
@@ -25,2 +25,6 @@ this.projectID = projectID; | ||
this.hostingEndpoint = urls_1.fbhostingEndpoint(projectID); | ||
// Custom headers | ||
this.globalHeaders = (extra && extra.headers) ? extra.headers : {}; | ||
// Cache seed | ||
this.seed = (extra && extra.seed) ? extra.seed : '42'; | ||
} | ||
@@ -37,5 +41,5 @@ serve(event) { | ||
const request = event.request; | ||
const hash = yield cache_1.varyHash(request); | ||
const hash = yield cache_1.varyHash(request, this.seed); | ||
// Compute cache key to simulate 'Vary' caching support | ||
const cacheKey = yield cache_1.requestCacheKey(request); | ||
const cacheKey = yield cache_1.requestCacheKey(request, this.seed); | ||
// Check cache | ||
@@ -47,6 +51,3 @@ const cfCaches = caches; | ||
// Change headers for cache hit | ||
const headers = new Headers(response.headers); | ||
headers.set('via', 'magic cache'); | ||
headers.set('x-magic-hash', hash); | ||
headers.delete('link'); | ||
const headers = headerChanges(response.headers, Object.assign({}, this.globalHeaders, { 'via': 'magic cache', 'x-magic-hash': hash, 'link': null })); | ||
return customHeaders(response, headers); | ||
@@ -64,6 +65,3 @@ } | ||
// Change headers for cache miss | ||
const headers = new Headers(response.headers); | ||
headers.set('via', 'no cache'); | ||
headers.set('x-magic-hash', hash); | ||
headers.delete('link'); | ||
const headers = headerChanges(response.headers, Object.assign({}, this.globalHeaders, { 'via': 'no cache', 'x-magic-hash': hash, 'link': null })); | ||
return customHeaders(response, headers); | ||
@@ -108,2 +106,16 @@ }); | ||
} | ||
function headerChanges(headers, changes) { | ||
const copy = new Headers(headers); | ||
const keys = Object.keys(changes).sort(); | ||
keys.forEach(key => { | ||
const value = changes[key]; | ||
if (value) { | ||
copy.set(key, value); | ||
} | ||
else { | ||
copy.delete(key); | ||
} | ||
}); | ||
return copy; | ||
} | ||
// Returns a new response with customized headers (provided) | ||
@@ -110,0 +122,0 @@ function customHeaders(response, headers) { |
{ | ||
"name": "firebase-on-cloudflare", | ||
"main": "./dist/index.js", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"dependencies": {}, | ||
@@ -6,0 +6,0 @@ "files": [ |
15289
351