Skip to content

Instantly share code, notes, and snippets.

Avatar

Jason Byrne jasonbyrne

View GitHub Profile
@jasonbyrne
jasonbyrne / extracted-email-message.interface.ts
Created Jan 20, 2023
Email message as extracted by postal-mime from Cloudflare Email Router's raw property
View extracted-email-message.interface.ts
export interface EmailAttachment {
filename: string;
mimeType: string;
disposition: string;
related: boolean;
contentId: string;
content: unknown;
}
export interface EmailAddress {
@jasonbyrne
jasonbyrne / index.ts
Created Jan 20, 2023
Cloudflare Workers Email Routing POC
View index.ts
const PostalMime = require('postal-mime');
export interface EmailMessage {
readonly from: string;
readonly to: string;
readonly headers: Headers;
readonly raw: ReadableStream;
readonly rawSize: number;
setReject(reason: String): void;
forward(rcptTo: string, headers?: Headers): Promise<void>;
@jasonbyrne
jasonbyrne / time-ago.ts
Created Jan 17, 2023
Simple ago function to calculate, with a concise string, how old a post is
View time-ago.ts
const DAY_SECONDS = 86400;
const WEEK_SECONDS = 604800;
const YEAR_SECONDS = WEEK_SECONDS * 52;
export function timeAgo(date: Date) {
const epoch = Math.round(date.getTime() / 1000);
const now = Math.round(Date.now() / 1000);
const seconds = now - epoch;
const minutes = Math.round(seconds / 60);
const hours = Math.round(seconds / 3600);
View isOneOfMyValues.ts
const values = ['foo', 'bar'] as const;
type MyValues = typeof values[number];
function isOneOfMyValues(elem: unknown): elem is MyValues {
const opts: string[] = [...values];
return opts.includes(String(elem));
}
function lengthOfMyThing(x: MyValues | null) {
if (isOneOfMyValues(x)) {
@jasonbyrne
jasonbyrne / color-names.js
Created May 17, 2022
Standard HTML Color Names Array
View color-names.js
export const COLOR_NAMES = [
'aliceblue',
'antiquewhite',
'aqua',
'aquamarine',
'azure',
'beige',
'bisque',
'black',
'blanchedalmond',
@jasonbyrne
jasonbyrne / generate-svg.ts
Created May 17, 2022
Generate Placeholder SVG with Cloudflare Workers
View generate-svg.ts
// Credit:: https://github.com/cloudfour/simple-svg-placeholder
type SvgOptions = {
width: number
height: number
text: string
fontFamily: string
fontWeight: string
bgColor: string
textColor: string
@jasonbyrne
jasonbyrne / fetch-thumbnail.ts
Created May 17, 2022
Fetch Thumbnail, resize and serve with Cloudflare Worker
View fetch-thumbnail.ts
import { IncomingRequest } from '../incoming-request'
const buckets: { [key: string]: string } = {
'bucket-name': 'https://bucket-url/',
}
interface RequestInitWithCf extends RequestInit {
cf: RequestInitCfProperties & {
image: BasicImageTransformations & {
quality?: number | undefined
@jasonbyrne
jasonbyrne / hello-world.ts
Created May 17, 2022
CloudFlare Workers Router in TypeScript v2
View hello-world.ts
export function helloWorld(): Response {
return new Response('Hello World', {
status: 200,
})
}
@jasonbyrne
jasonbyrne / merge-objects.ts
Created May 2, 2022
Deep Merge and Shallow Merge
View merge-objects.ts
export const shallowMerge = <T extends object = Record<string, any>>(
...objects: T[]
): T => {
return objects.reduce((prev, cur) => ({ ...prev, ...cur }), {} as T);
};
export const deepMerge = <T extends object = Record<string, any>>(
target: T,
...sources: object[]
): T => {
@jasonbyrne
jasonbyrne / human-readable-list.ts
Created May 2, 2022
Turn an array into a comma-separated string with and or or at the end
View human-readable-list.ts
export const humanReadableList = (
array: unknown[],
join: ',' | ';' = ',',
finalJoin = 'and',
): string => {
if (!Array.isArray(array) || array.length == 0) return '';
if (array.length == 1) return String(array[0]);
const arr = array.slice(0),
last = arr.pop();
return array.length > 2