Skip to content

Instantly share code, notes, and snippets.

View christophemarois's full-sized avatar

Christophe Marois christophemarois

  • Pathway Medical
  • Montreal
View GitHub Profile
@christophemarois
christophemarois / cloudflare-tunnels.md
Last active April 9, 2024 13:24
Cloudflare Tunnels

Cloudflare is a better and free ngrok. With a domain that uses it, set it up like this

  1. Setup a tunnel
cloudflared tunnel login
cloudflared tunnel create test
cloudflared tunnel route dns test test.christophemarois.com
@christophemarois
christophemarois / redactPasswordFromUrl.ts
Created March 7, 2024 15:08
redactPasswordFromUrl.ts
/** Transform the password part of a connection string URL into [redacted].
* Useful for logging. */
export function redactPasswordFromUrl(url: string) {
return url.replace(/^([^:]+):\/\/([^:]*):([^@]+)@/, '$1://$2:[redacted]@')
}
@christophemarois
christophemarois / icloud-cal-in-thunderbird.md
Last active October 10, 2023 13:43
Icloud calendars in Thunderbird
  1. Create an app-specific password for iCloud account (check Bitwarden notes)
  2. Use https://github.com/midnightmonster/icloud-calendar-urls to get your calendar's secret URL
curl -s https://raw.githubusercontent.com/midnightmonster/icloud-calendar-urls/master/icloud_calendar_urls | bash
  1. In thunderbird, add calendar with your icloud username, secret url and app-specific password
@christophemarois
christophemarois / node-monitoring.md
Last active October 2, 2023 13:50
Node Grafana/Prometheus Performance Monitoring

Monitor in Node (PerformanceObserver is not yet implemented in Bun. in bun 1.0.3)

import { collectDefaultMetrics, register } from 'prom-client'

if (req.method === 'GET' && pathname === '/metrics/') {
  return new Response(await register.metrics(), {
    headers: {
      'Content-Type': register.contentType,
 },
@christophemarois
christophemarois / zod-unique-array.ts
Created September 29, 2023 23:04
Zod Unique Array
/** Array where are elements are expected to be unique by a custom key */
export function zUniqueArray<
ArrSchema extends z.ZodArray<z.ZodTypeAny, 'many'>,
UniqueVal,
>(
uniqueBy: (item: z.infer<ArrSchema>[number]) => UniqueVal,
schema: ArrSchema,
) {
return schema.superRefine((items, ctx) => {
const seen = new Set<UniqueVal>()
@christophemarois
christophemarois / git_list_branch_changes.sh
Created September 15, 2023 12:57
List branch changes in git
# list changed files in a branch compared to its base branch
# usage: git_list_branch_changes [base_branch?] | xargs echo
function git_list_branch_changes {
local BASE_BRANCH="${1:-main}"
(git diff --diff-filter=MA --name-only $BASE_BRANCH... && git status --short --porcelain | awk '{print $2}') | sort -u
}
@christophemarois
christophemarois / node-cli-args-parse.ts
Created August 9, 2023 14:15
Native parsing for CLI args in node
import { parseArgs } from 'node:util'
const args = parseArgs({
options: { env: { type: 'string' }, dryRun: { type: 'boolean' } },
})
@christophemarois
christophemarois / node-inspector.ts
Last active July 12, 2023 18:24
Boot up a node inspector anywhere
// will act --inspect-brk, but only when evaluated
// works inside the main thread, a worker thread
// also works with typescript sources, built-in support in tsx
require('node:inspector').open(undefined, undefined, true)
@christophemarois
christophemarois / streamWretchResponseToFile.ts
Last active July 5, 2023 19:31
Stream a wretch response directly to a file in node.js
import wretch, { WretchResponse } from 'wretch'
import fs from 'node:fs'
async function streamWretchResponseToFile(
res: WretchResponse,
writable: fs.WriteStream,
) {
if (!res.body) {
throw new Error('Response body is null')
}
@christophemarois
christophemarois / zMapKeys.ts
Last active June 16, 2023 21:26
zMapKeys.ts
// Type inference does not work yet
export function zMapKeys<
Shape extends z.ZodRawShape,
T extends z.ZodObject<Shape>,
Key extends keyof Shape,
MapFn extends <T extends Key>(v: Shape[T], k: T) => PropertyKey,
>(schema: T, renameMap: Record<Key, PropertyKey | MapFn>) {
const keys = Object.keys(schema.shape) as Key[]
return schema.transform((input) => {
let output: Record<PropertyKey, any> = {}