Skip to content

Instantly share code, notes, and snippets.

Avatar

Jason Byrne jasonbyrne

View GitHub Profile
@jasonbyrne
jasonbyrne / kv-poc.js
Created Feb 24, 2021
Very basic POC to test CloudFlare workers with KV and waitUntil
View kv-poc.js
addEventListener('fetch', event => event.respondWith(handleRequest(event)));
async function handleRequest(event) {
const request = event.request;
// Parse query string
const { searchParams } = new URL(request.url);
let qsName = searchParams.get('name');
// If name in query string, add it to kv async
if (qsName) {
// Run this async to response, but wait for it to complete before terminating
@jasonbyrne
jasonbyrne / pubsub.ts
Last active Nov 19, 2020 — forked from johnnycardy/pubsub.ts
Simple pub/sub TypeScript class for Angular
View pubsub.ts
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root',
})
export class PubSub {
private topics: { [id: string]: PubSubTopic } = {};
private constructor() {}
@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(