Skip to content

Instantly share code, notes, and snippets.

View jasonbyrne's full-sized avatar

Jason Byrne jasonbyrne

View GitHub Profile
@jasonbyrne
jasonbyrne / index.ts
Last active January 23, 2019 02:05
StackPath EdgeEngine simple auth proof of concept
import { Auth } from './auth';
addEventListener('fetch', (event: StackPathEvent) => {
event.respondWith(handleRequest(event.request));
});
async function handleRequest(request: StackPathRequest): Promise<Response> {
try {
const auth: Auth = new Auth(request);
if (!auth.isAuthenticated()) {
@jasonbyrne
jasonbyrne / package.json
Last active March 19, 2019 15:34
Firebase Functions Example
{
"name": "functions",
"scripts": {
"lint": "tslint --project tsconfig.json",
"build": "tsc",
"serve": "npm run build && firebase serve --only functions",
"shell": "npm run build && firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
@jasonbyrne
jasonbyrne / flagpole-demo-test.js
Created August 15, 2019 21:43
Sample Test Suite created in the Flagpole Walkthrough on August 15, 2019
const { Flagpole } = require('flagpole');
const searchTerm = 'Flagpole QA';
const suite = Flagpole.suite('Basic Smoke Test of Site')
.base('https://www.google.com');
suite.html('Homepage Loads')
.open('/')
.next('Test the basic headers', (ctx) => {
@jasonbyrne
jasonbyrne / npmjs.js
Created September 5, 2019 05:34
Flagpole suite with Puppeteer
const { Flagpole } = require('flagpole');
const opts = {
headless: false,
width: 1280,
height: 600
};
const suite = Flagpole.suite('Basic Smoke Test of npmjs.com')
.base('https://www.npmjs.com/');
@jasonbyrne
jasonbyrne / index.ts
Last active March 6, 2021 12:37
CloudFlare Workers Router in TypeScript
import { Router } from "./helpers/router";
import { getQueryString } from "./helpers/querystring";
const MAGIC_WORD = "flo";
function catchAll(request: Request): Response {
return fetch(request.url, request);
}
function validateTokenThenFetch(
@jasonbyrne
jasonbyrne / index.ts
Created April 27, 2020 13:07
Simplest CDN Worker
function handleRequest(request: Request): Promise<Response> {
return fetch(request);
}
addEventListener('fetch', (event) => {
event.respondWith(handleRequest(event.request))
});
@jasonbyrne
jasonbyrne / lambda.js
Created April 27, 2020 13:12
Lambda@Edge Simplest Example
'use strict';
exports.handler = (event, context, callback) => {
const request = event.Records[0].cf.request;
return callback(null, request);
};
@jasonbyrne
jasonbyrne / custom-header.ts
Last active April 27, 2020 13:26
Cloudflare Worker to add a header
export async function addCustomHeader(response: Response): Promise<Response> {
const modifiedResponse = new Response(response.body, response);
modifiedResponse.headers.set("X-My-Custom-Header", "Hello there!");
return modifiedResponse;
}
@jasonbyrne
jasonbyrne / custom-header2.ts
Created April 27, 2020 13:26
Add custom header at edge that works on StackPath and Cloudflare
export async function addCustomHeader(response: Response): Promise<Response> {
const modifiedResponse = new Response(await response.arrayBuffer(), response);
modifiedResponse.headers.set("X-My-Custom-Header", "Hello there!");
return modifiedResponse;
}
@jasonbyrne
jasonbyrne / modify-body.ts
Created April 27, 2020 13:35
Modify the body at edge with Cloudflare and StackPAth
export async function personalizeBody(
response: Response,
request: Request
): Promise<Response> {
const originalBody = await response.text();
const newBody = originalBody.replace(
/{{ timestamp }}/g,
new Date().toUTCString()
);
const modifiedResponse = new Response(newBody, response);