@growthbook/edge-lambda
Advanced tools
Comparing version 0.0.10 to 0.0.20
@@ -6,6 +6,6 @@ import { Context } from "@growthbook/edge-utils"; | ||
export declare function getRequestHeader(req: any, key: string): any; | ||
export declare function sendResponse(ctx: Context, _?: any, headers?: Record<string, any>, body?: string, cookies?: Record<string, string>, status?: number): any; | ||
export declare function fetchFn(_: Context, url: string): Promise<Response>; | ||
export declare function proxyRequest(ctx: Context, req: any): Promise<Response>; | ||
export declare function sendResponse(ctx: Context<Request, Response>, _?: any, headers?: Record<string, any>, body?: string, cookies?: Record<string, string>, status?: number): any; | ||
export declare function fetchFn(ctx: Context<Request, Response>, url: string, req: any): Promise<Response>; | ||
export declare function proxyRequest(ctx: Context<Request, Response>, req: any): Promise<Response>; | ||
export declare function getCookie(req: any, key: string): string; | ||
export declare function setCookie(res: any, key: string, value: string): void; |
@@ -7,3 +7,10 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.setCookie = exports.getCookie = exports.proxyRequest = exports.fetchFn = exports.sendResponse = exports.getRequestHeader = exports.getRequestMethod = exports.buildGetRequestURL = void 0; | ||
exports.buildGetRequestURL = buildGetRequestURL; | ||
exports.getRequestMethod = getRequestMethod; | ||
exports.getRequestHeader = getRequestHeader; | ||
exports.sendResponse = sendResponse; | ||
exports.fetchFn = fetchFn; | ||
exports.proxyRequest = proxyRequest; | ||
exports.getCookie = getCookie; | ||
exports.setCookie = setCookie; | ||
const edge_utils_1 = require("@growthbook/edge-utils"); | ||
@@ -28,11 +35,8 @@ const cookie_1 = __importDefault(require("cookie")); | ||
} | ||
exports.buildGetRequestURL = buildGetRequestURL; | ||
function getRequestMethod(req) { | ||
return req.method.toUpperCase(); | ||
} | ||
exports.getRequestMethod = getRequestMethod; | ||
function getRequestHeader(req, key) { | ||
return req.headers?.[key]?.value || undefined; | ||
} | ||
exports.getRequestHeader = getRequestHeader; | ||
function sendResponse(ctx, _, headers, body, cookies, status) { | ||
@@ -57,11 +61,5 @@ const res = { | ||
} | ||
exports.sendResponse = sendResponse; | ||
async function fetchFn(_, url) { | ||
// @ts-ignore | ||
return fetch(url); | ||
} | ||
exports.fetchFn = fetchFn; | ||
async function proxyRequest(ctx, req) { | ||
const originUrl = (0, edge_utils_1.getOriginUrl)(ctx, req.url); | ||
return fetch(originUrl, { | ||
async function fetchFn(ctx, url, req) { | ||
const maxRedirects = 5; | ||
let response = await fetch(url, { | ||
method: req.method, | ||
@@ -71,4 +69,22 @@ headers: req.headers, | ||
}); | ||
if (!ctx.config.followRedirects) { | ||
return response; | ||
} | ||
let location = response.headers.get('location'); | ||
let redirectCount = 0; | ||
while (response.status >= 300 && response.status < 400 && location && redirectCount < maxRedirects) { | ||
response = await fetch(location, { | ||
method: req.method, | ||
headers: req.headers, | ||
body: req.body, | ||
}); | ||
location = response.headers.get('location'); | ||
redirectCount++; | ||
} | ||
return response; | ||
} | ||
exports.proxyRequest = proxyRequest; | ||
async function proxyRequest(ctx, req) { | ||
const originUrl = (0, edge_utils_1.getOriginUrl)(ctx, req.url); | ||
return fetchFn(ctx, originUrl, req); | ||
} | ||
function getCookie(req, key) { | ||
@@ -90,3 +106,2 @@ const parsedCookie = {}; | ||
} | ||
exports.getCookie = getCookie; | ||
function setCookie(res, key, value) { | ||
@@ -102,3 +117,2 @@ const COOKIE_DAYS = 400; // 400 days is the max cookie duration for chrome | ||
} | ||
exports.setCookie = setCookie; | ||
//# sourceMappingURL=helpers.js.map |
@@ -1,5 +0,6 @@ | ||
import { Config } from "@growthbook/edge-utils"; | ||
import { Config, Hooks, Helpers } from "@growthbook/edge-utils"; | ||
import { Env } from "./init"; | ||
export declare function handleRequest(event: any, callback: any, env?: Env, config?: Partial<Config>): Promise<unknown>; | ||
export declare function handleRequest(event: any, callback: any, env?: Env, config?: Partial<Config>, hooks?: Hooks<Request, Response>, helpers?: Partial<Helpers<Request, Response>>): Promise<unknown>; | ||
export type { Env } from "./init"; | ||
export { mapHeadersToConfigEnv } from "./init"; | ||
export * as helpers from "./helpers"; |
"use strict"; | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { | ||
Object.defineProperty(o, "default", { enumerable: true, value: v }); | ||
}) : function(o, v) { | ||
o["default"] = v; | ||
}); | ||
var __importStar = (this && this.__importStar) || (function () { | ||
var ownKeys = function(o) { | ||
ownKeys = Object.getOwnPropertyNames || function (o) { | ||
var ar = []; | ||
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; | ||
return ar; | ||
}; | ||
return ownKeys(o); | ||
}; | ||
return function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); | ||
__setModuleDefault(result, mod); | ||
return result; | ||
}; | ||
})(); | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.mapHeadersToConfigEnv = exports.handleRequest = void 0; | ||
exports.helpers = exports.mapHeadersToConfigEnv = void 0; | ||
exports.handleRequest = handleRequest; | ||
const edge_utils_1 = require("@growthbook/edge-utils"); | ||
const init_1 = require("./init"); | ||
async function handleRequest(event, callback, env, config) { | ||
async function handleRequest(event, callback, env, config, hooks, helpers) { | ||
const request = event.Records[0].cf.request; | ||
const context = await (0, init_1.init)(env, config); | ||
const context = await (0, init_1.init)(env, config, hooks, helpers); | ||
const response = await (0, edge_utils_1.edgeApp)(context, request); | ||
@@ -15,5 +49,5 @@ if (env?.returnResponse) | ||
} | ||
exports.handleRequest = handleRequest; | ||
var init_2 = require("./init"); | ||
Object.defineProperty(exports, "mapHeadersToConfigEnv", { enumerable: true, get: function () { return init_2.mapHeadersToConfigEnv; } }); | ||
exports.helpers = __importStar(require("./helpers")); | ||
//# sourceMappingURL=index.js.map |
@@ -1,2 +0,2 @@ | ||
import { Context, ConfigEnv, Config } from "@growthbook/edge-utils"; | ||
import { Context, ConfigEnv, Config, Helpers, Hooks } from "@growthbook/edge-utils"; | ||
export interface Env extends ConfigEnv { | ||
@@ -8,3 +8,3 @@ host?: string; | ||
} | ||
export declare function init(env?: ConfigEnv, config?: Partial<Config>): Promise<Context>; | ||
export declare function init(env?: ConfigEnv, config?: Partial<Config>, hooks?: Hooks<Request, Response>, helpers?: Partial<Helpers<Request, Response>>): Promise<Context<Request, Response>>; | ||
export declare function mapHeadersToConfigEnv(req: any, originType?: "custom" | "s3", prefix?: string): ConfigEnv; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.mapHeadersToConfigEnv = exports.init = void 0; | ||
exports.init = init; | ||
exports.mapHeadersToConfigEnv = mapHeadersToConfigEnv; | ||
const edge_utils_1 = require("@growthbook/edge-utils"); | ||
const helpers_1 = require("./helpers"); | ||
async function init(env, config) { | ||
async function init(env, config, hooks, helpers) { | ||
const context = edge_utils_1.defaultContext; | ||
@@ -30,5 +31,10 @@ const configEnv = env || {}; | ||
context.helpers.setCookie = helpers_1.setCookie; | ||
if (helpers) { | ||
Object.assign(context.helpers, helpers); | ||
} | ||
if (hooks) { | ||
context.hooks = hooks; | ||
} | ||
return context; | ||
} | ||
exports.init = init; | ||
function mapHeadersToConfigEnv( | ||
@@ -53,3 +59,2 @@ // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
} | ||
exports.mapHeadersToConfigEnv = mapHeadersToConfigEnv; | ||
//# sourceMappingURL=init.js.map |
{ | ||
"name": "@growthbook/edge-lambda", | ||
"description": "GrowthBook edge app for Lambda@Edge", | ||
"version": "0.0.10", | ||
"version": "0.0.20", | ||
"main": "dist/index.js", | ||
@@ -19,11 +19,11 @@ "license": "MIT", | ||
"dependencies": { | ||
"@growthbook/edge-utils": "^0.1.7", | ||
"cookie": "^1.0.1" | ||
"@growthbook/edge-utils": "^0.2.0", | ||
"cookie": "^1.0.2" | ||
}, | ||
"devDependencies": { | ||
"@growthbook/growthbook": "^1.2.1", | ||
"concurrently": "^9.0.1", | ||
"rimraf": "^5.0.5", | ||
"typescript": "5.2.2" | ||
"@growthbook/growthbook": "^1.3.1", | ||
"concurrently": "^9.1.2", | ||
"rimraf": "^6.0.1", | ||
"typescript": "^5.7.2" | ||
} | ||
} |
@@ -9,2 +9,3 @@ # Lambda@Edge App & SDK | ||
- Automatically run server-side or hybrid URL Redirect Experiments without flicker or delay. | ||
- Perform custom feature flagging and experimentation logic. | ||
- Inject the JavaScript SDK with hydrated payload, allowing the front-end to pick up where the edge left off without any extra network requests. | ||
@@ -11,0 +12,0 @@ |
@@ -37,3 +37,3 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ | ||
export function sendResponse( | ||
ctx: Context, | ||
ctx: Context<Request, Response>, | ||
_?: any, | ||
@@ -64,10 +64,6 @@ headers?: Record<string, any>, | ||
export async function fetchFn(_: Context, url: string) { | ||
// @ts-ignore | ||
return fetch(url); | ||
} | ||
export async function fetchFn(ctx: Context<Request, Response>, url: string, req: any) { | ||
const maxRedirects = 5; | ||
export async function proxyRequest(ctx: Context, req: any) { | ||
const originUrl = getOriginUrl(ctx as Context<unknown, unknown>, req.url); | ||
return fetch(originUrl, { | ||
let response = await fetch(url, { | ||
method: req.method, | ||
@@ -77,4 +73,27 @@ headers: req.headers, | ||
}); | ||
if (!ctx.config.followRedirects) { | ||
return response; | ||
} | ||
let location = response.headers.get('location'); | ||
let redirectCount = 0; | ||
while (response.status >= 300 && response.status < 400 && location && redirectCount < maxRedirects) { | ||
response = await fetch(location, { | ||
method: req.method, | ||
headers: req.headers, | ||
body: req.body, | ||
}); | ||
location = response.headers.get('location'); | ||
redirectCount++; | ||
} | ||
return response; | ||
} | ||
export async function proxyRequest(ctx: Context<Request, Response>, req: any) { | ||
const originUrl = getOriginUrl(ctx as Context<unknown, unknown>, req.url); | ||
return fetchFn(ctx, originUrl, req); | ||
} | ||
export function getCookie(req: any, key: string): string { | ||
@@ -81,0 +100,0 @@ const parsedCookie: Record<string, string> = {}; |
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import { edgeApp, Config } from "@growthbook/edge-utils"; | ||
import { edgeApp, Config, Hooks, Helpers } from "@growthbook/edge-utils"; | ||
import { init, Env } from "./init"; | ||
@@ -11,5 +11,7 @@ | ||
config?: Partial<Config>, | ||
hooks?: Hooks<Request, Response>, | ||
helpers?: Partial<Helpers<Request, Response>>, | ||
) { | ||
const request = event.Records[0].cf.request; | ||
const context = await init(env, config); | ||
const context = await init(env, config, hooks, helpers); | ||
const response = await edgeApp(context, request); | ||
@@ -22,1 +24,2 @@ if (env?.returnResponse) return response; | ||
export { mapHeadersToConfigEnv } from "./init"; | ||
export * as helpers from "./helpers"; |
@@ -6,3 +6,3 @@ import { | ||
ConfigEnv, | ||
Config, | ||
Config, Helpers, Hooks | ||
} from "@growthbook/edge-utils"; | ||
@@ -32,3 +32,5 @@ import { | ||
config?: Partial<Config>, | ||
): Promise<Context> { | ||
hooks?: Hooks<Request, Response>, | ||
helpers?: Partial<Helpers<Request, Response>>, | ||
): Promise<Context<Request, Response>> { | ||
const context = defaultContext; | ||
@@ -60,2 +62,10 @@ const configEnv = env || {}; | ||
if (helpers) { | ||
Object.assign(context.helpers, helpers); | ||
} | ||
if (hooks) { | ||
context.hooks = hooks; | ||
} | ||
return context; | ||
@@ -62,0 +72,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
27016
484
24
+ Added@growthbook/edge-utils@0.2.0(transitive)
+ Addednode-html-parser@7.0.1(transitive)
+ Addedpako@2.1.0(transitive)
- Removed@growthbook/edge-utils@0.1.7(transitive)
- Removednode-html-parser@6.1.13(transitive)
Updatedcookie@^1.0.2