Skip to content

Instantly share code, notes, and snippets.

View jasonbyrne's full-sized avatar

Jason Byrne jasonbyrne

View GitHub Profile
@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);
@jasonbyrne
jasonbyrne / asset.ts
Created April 27, 2020 13:56
Figure out different asset domains based on which CDN provider
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 / webhook.ts
Created September 10, 2020 17:05
Flagpole <3s Ngrok
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 / pubsub.ts
Last active November 19, 2020 01:45 — forked from johnnycardy/pubsub.ts
Simple pub/sub TypeScript class for Angular
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root',
})
export class PubSub {
private topics: { [id: string]: PubSubTopic } = {};
private constructor() {}
@jasonbyrne
jasonbyrne / summernote-pastclean.js
Created October 14, 2015 18:21
Plugin for Summernote WYSIWYG editor that cleans up the pasted in content
/**
* Summernote PasteClean
*
* This is a plugin for Summernote (www.summernote.org) WYSIWYG editor.
* It will clean up the content your editors may paste in for unknown sources
* into your CMS. It strips Word special characters, style attributes, script
* tags, and other annoyances so that the content can be clean HTML with
* no unknown hitchhiking scripts or styles.
*
* @author Jason Byrne, FloSports <jason.byrne@flosports.tv>
@jasonbyrne
jasonbyrne / kv-poc.js
Created February 24, 2021 16:46
Very basic POC to test CloudFlare workers with KV and waitUntil
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 / 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 / fileHandler.ts
Last active April 19, 2021 02:24
Firebase Functions + Express file uploader handler
/* Firebase Functions messes with your request and will wreck your day because none of the
* traditional upload handlers with Express will work.
*
* Credit: https://stackoverflow.com/questions/47242340/how-to-perform-an-http-file-upload-using-express-on-cloud-functions-for-firebase
*/
const Busboy = require('busboy');
const allowedMethods: string[] = ['POST', 'PUT'];
export class FileUpload {
@jasonbyrne
jasonbyrne / exception-handler.ts
Last active December 22, 2021 03:30
Nest JS Exception Handler
import { ExceptionFilter, Catch, ArgumentsHost } from '@nestjs/common';
import { Response } from 'express';
import { IncomingMessage } from 'http';
import { HttpException, HttpStatus } from '@nestjs/common';
export const getStatusCode = (exception: unknown): number => {
return exception instanceof HttpException
? exception.getStatus()
: HttpStatus.INTERNAL_SERVER_ERROR;
};
@jasonbyrne
jasonbyrne / main.ts
Last active December 22, 2021 03:32
NestJS Bootstrap with custom global exception filter
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { GlobalExceptionFilter } from './exception-handler';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.useGlobalFilters(new GlobalExceptionFilter());
await app.listen(3000);
}
bootstrap();