Skip to content

Instantly share code, notes, and snippets.

Avatar

Jason Byrne jasonbyrne

View GitHub Profile
@jasonbyrne
jasonbyrne / webhook.ts
Created Sep 10, 2020
Flagpole <3s Ngrok
View webhook.ts
import flagpole from "flagpole";
import * as ngrok from "ngrok";
flagpole("Just a basic webhook callback", async (suite) => {
// Create webhook scenario
const webhook = await suite
.scenario("Wait for a webhook", "resource")
.webhook("GET /foo")
.next(async (context) => {
context.comment("Webhook was hit!");
@jasonbyrne
jasonbyrne / asset.ts
Created Apr 27, 2020
Figure out different asset domains based on which CDN provider
View asset.ts
const hosts = {
cloudFlare: "https://flosports-video.s3.amazon.com",
stackPath: "https://cdn.stackpath.flosports.tv",
};
function isStackPath(request: Request): boolean {
const url = new URL(request.url);
return !!url.hostname.match(/stackpath/);
}
@jasonbyrne
jasonbyrne / modify-body.ts
Created Apr 27, 2020
Modify the body at edge with Cloudflare and StackPAth
View modify-body.ts
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);
@jasonbyrne
jasonbyrne / custom-header2.ts
Created Apr 27, 2020
Add custom header at edge that works on StackPath and Cloudflare
View custom-header2.ts
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 / custom-header.ts
Last active Apr 27, 2020
Cloudflare Worker to add a header
View custom-header.ts
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 / lambda.js
Created Apr 27, 2020
Lambda@Edge Simplest Example
View lambda.js
'use strict';
exports.handler = (event, context, callback) => {
const request = event.Records[0].cf.request;
return callback(null, request);
};
@jasonbyrne
jasonbyrne / index.ts
Created Apr 27, 2020
Simplest CDN Worker
View index.ts
function handleRequest(request: Request): Promise<Response> {
return fetch(request);
}
addEventListener('fetch', (event) => {
event.respondWith(handleRequest(event.request))
});
@jasonbyrne
jasonbyrne / index.ts
Last active Mar 19, 2020
CloudFlare Workers Router in TypeScript
View index.ts
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 / npmjs.js
Created Sep 5, 2019
Flagpole suite with Puppeteer
View npmjs.js
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 / flagpole-demo-test.js
Created Aug 15, 2019
Sample Test Suite created in the Flagpole Walkthrough on August 15, 2019
View flagpole-demo-test.js
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) => {
You can’t perform that action at this time.