Skip to content

Instantly share code, notes, and snippets.

Avatar
🔥
#BLM

Dan Levy justsml

🔥
#BLM
View GitHub Profile
View fetch-retry-with-curry.js
/**
* A **Smarter** retry wrapper with currying!
*/
function retryCurry(fn, retriesLeft = 5) {
const retryFn = (...args) => fn(...args)
.catch(err => retriesLeft > 0
? retryFn(fn, retriesLeft - 1)
: Promise.reject(err)
})
return retryFn
View location-redirect.js
const checkForRedirect = (response) => {
// Check for temporary redirect (307), or permanent (308)
if (response.status === 307 || response.status === 308) {
const location = response.headers.get('location')
if (!location) {
return Promise.reject(new Error('Invalid HTTP Redirect! No Location header.'));
}
// You can change the behavior here to any custom logic:
// e.g. open a "confirm" modal, log the redirect url, etc.
return fetch(location)
@justsml
justsml / extensions.json
Last active November 22, 2022 18:57
VS Code Config
View extensions.json
{
// See https://go.microsoft.com/fwlink/?LinkId=827846 to learn about workspace recommendations.
// Extension identifier format: ${publisher}.${name}. Example: vscode.csharp
// List of extensions which should be recommended for users of this workspace.
"recommendations": [
"streetsidesoftware.code-spell-checker",
"kisstkondoros.vscode-codemetrics",
"ms-azuretools.vscode-docker",
"dbaeumer.vscode-eslint",
View targeted-feature-flag.ts
/**
* `filteredFlagFactory` supports 3 states of a feature flag:
* - True,
* - False,
* - and restricted by ID(s).
*
*/
export function filteredFlagFactory(
flagValue: string,
defaultIdField = 'userId'
@justsml
justsml / asyncIterable-stream-forAwaitOf-examples.ts
Created August 1, 2022 00:24
Examples of using for-await-of syntax to consume streams.
View asyncIterable-stream-forAwaitOf-examples.ts
export const batchStream = (size: number) =>
async function* batchStream<TStreamType>(stream: AsyncIterable<TStreamType>) {
let buffer: TStreamType[] = [];
for await (const chunk of stream) {
buffer.push(chunk);
if (buffer.length >= size) {
yield buffer;
buffer = [];
}
}
@justsml
justsml / addSignalListener.mjs
Last active August 18, 2022 04:04
Nodejs snippet to handle `process` events (OS signals) easily with `addSignalListener()`
View addSignalListener.mjs
/**
* Handle process signal events easily with `addSignalListener()`.
*
* Automatically returns a convenient method to unregister your event handler(s), in case you need that functionality.
*
* ## Examples
*
* ```js
* addSignalListener(['SIGINT', 'SIGHUP', 'SIGUSR2'], (signal, event) =>
* console.log(`Process received a signal! ${signal}`, { event });
@justsml
justsml / settings.json
Created June 21, 2022 00:50
VS Code Recommended Settings
View settings.json
{
// TypeScript configuration
// Use the project-local typescript version / may be desired, comment out on old TS versions
"typescript.tsdk": "node_modules/typescript/lib",
"typescript.preferences.importModuleSpecifier": "relative",
// Terminal settings
"terminal.integrated.scrollback": 100000, // Terminal scrollback line limit
// Set terminal font size to match VS Code font size. See: https://www.NerdFonts.com/
"terminal.integrated.fontFamily": "'MesloLGS NF', 'Hack Nerd Font', Menlo, Monaco, 'Courier New', monospace",
View Open-Source-Security-Tooling.md

Security Tooling

The following security tools are used by both attackers & defenders. That's how you know it's good.

These tools go far beyond Dependabot, and can provide far richer details than almost any 3rd party scanning services (e.g. Nessus).

Dependency Scanning

@justsml
justsml / RectangleConfig.json
Last active May 19, 2022 21:05
Rectangle: customized config for ⭐️ Mac window layout utility.
View RectangleConfig.json
{
"bundleId" : "com.knollsoft.Rectangle",
"defaults" : {
"allowAnyShortcut" : {
"bool" : false
},
"almostMaximizeHeight" : {
"float" : 0
},
"almostMaximizeWidth" : {
View getFileAndDirname.js
import path from "path";
import { fileURLToPath } from "url";
/**
* Usage:
* const { __dirname, __filename } = getFileAndDirname(import.meta.url);
*/
export default function getFileAndDirname(importMetaUrl) {
const __filename = fileURLToPath(importMetaUrl);
const __dirname = path.dirname(__filename);