Skip to content

Instantly share code, notes, and snippets.

View sam-heller's full-sized avatar

Sam Heller sam-heller

View GitHub Profile
@sam-heller
sam-heller / splitforward.js
Last active July 30, 2023 06:19
Split forward for Cloudflare email worker
export default {
async email(message, env, ctx) {
const forwardList = ["one@one.com", "two@two.com"];
if (forwardList.indexOf(message.headers.get("from")) != -1) {
await message.forward("three@three.com")
}
await message.forward("four@four.com");
}
}
@sam-heller
sam-heller / github-plz-teach-it-tact.js
Created November 5, 2022 04:50
My Copilot seems to be highly opinonated
import Downloader from 'nodejs-file-downloader';
import path from 'node:path'
import fs from 'node:fs'
/**
* Started each line out with `@todo: [a-z]` and took copilots first suggestion
*
* @todo: Add a way to check if the file exists before downloading it
* @todo: Build a way to check if the file exists before downloading it
* @todo: Create a way to check if the file exists before downloading it
@sam-heller
sam-heller / cf-secret-rest-update.sh
Created March 24, 2021 16:00
Update Cloudflare KV Secret Via REST
curl --request PUT \
--url https://api.cloudflare.com/client/v4/accounts/***ACCOUNT_ID***/workers/scripts/***KV-NAMESPACE-SLUG***/secrets \
--header 'Content-Type: application/json' \
--header 'X-Auth-Email: ***AUTH-EMAIL***' \
--header 'X-Auth-Key: ***AUTH-TOKEN***' \
--data '{
"name" : "secret-key",
"text" : "secret-value",
"type" : "secret_text"
}'
@sam-heller
sam-heller / comparison-dropins.js
Last active March 26, 2021 00:40
Personal JS Dropins to make my life a lil bit less crazypants
global.get = {}
global.cflog = (m, d) =>{if (d instanceof Object){for (let key of Object.keys(d)){m += ` ${key}='${d[key]}'`}};console.log(m)} ;
global.objlog = (m, o) =>{console.log(`${m}: ` + util.inspect(o))}
global.obj2str = (o) => {return Object.prototype.toString.call(o).split(' ')[1].replace(']','');}
global.isNot = (t, a) => {return !is(t,a);}
get.objectFromJson = (s, f={}) => {return is('jsonstr', str) ? JSON.parse(s) : f;};
get.jsonFromObject = (o, f='{}') => {return is('obj', o) ? JSON.stringify(o) : f;};
get.byKey = (o,k,f) => {return ((is('obj',o) && (r = Reflect.get(o,k)) && isNot('undef', r)) ? r : f )}
global.is = (t,a) => {
switch(t.toLowerCase()){
@sam-heller
sam-heller / over-engineering.js
Last active March 23, 2021 10:36
My Favorite Javascript Object Extensions
/**
* Mostly pointless and somewhat reckless over-engineering for funskis.
* Probably a terrible idea.
*
* Two viewpoints from SO : http://bit.ly/315ttP8
* ~~ Pro ~~
* I think it's fine if it works in your target environment.
*
* Also I think prototype extension paranoia is overblown. As long as you use hasOwnProperty()
* like a good developer that it's all fine. Worst case, you overload that property elsewhere
@sam-heller
sam-heller / buffer-split.js
Created March 22, 2021 01:24
Spltting a buffer apart and reassembling
const fs = require('fs');
const split = require('split-buffer')
let data = fs.readFileSync('build/test.json')
let buffers = split(data, 100000)
fs.writeFileSync('b1', buffers[0])
fs.writeFileSync('b2', buffers[1])
fs.writeFileSync('b3', buffers[2])
let rebuild = Buffer.concat([fs.readFileSync('b1'), fs.readFileSync('b2'), fs.readFileSync('b3')]);
console.log(Buffer.compare(data, rebuild));
fs.writeFileSync('build/rebuilt.json', rebuild)
@sam-heller
sam-heller / postman-cloudflare-migadu.js
Last active August 3, 2022 09:05
Provision Migadu Email to a Cloudflare domain via Postman
//Parent Request to https://api.cloudflare.com/client/v4/zones/**zone_id**/dns_records?type=mx,txt,cname,srv
const current_zone = ''; //Zone ID
const verify_code = ''; //value from the hosted-email-verify TXT entry supplied by Migadu
const host = ''; //Hostname
const cloudflare_token = ''; //Cloudflare API Token
const base_url = 'https://api.cloudflare.com/client/v4';
const dns_records_url = base_url + '/zones/' + current_zone + '/dns_records/';
request = {header: {"Authorization" : "Bearer " + cloudflare_token,"Content-Type" : "application/json"}};
const updates = [
@sam-heller
sam-heller / keybase.md
Created June 10, 2019 12:31
much base, so key

Keybase proof

I hereby claim:

  • I am sam-heller on github.
  • I am faitaccompli (https://keybase.io/faitaccompli) on keybase.
  • I have a public key ASAORD_zFcvlpLr4byY5gZSw678d6_hHCtTWfa9IDvSfWAo

To claim this, I am signing this object:

@sam-heller
sam-heller / gist:d0f0b4ec6b0dab94026a0b1df6f0f2e5
Created February 3, 2019 02:52
Convert iso image to os x .img file for writings and fun times
hdiutil convert -format UDRW -o /path/to/target.img /path/to/source.iso
hdiutil convert -format UDRW -o /path/to/target.img /path/to/source.iso