Skip to content

Instantly share code, notes, and snippets.

@sindresorhus
sindresorhus / esm-package.md
Last active May 4, 2024 15:48
Pure ESM package

Pure ESM package

The package that linked you here is now pure ESM. It cannot be require()'d from CommonJS.

This means you have the following choices:

  1. Use ESM yourself. (preferred)
    Use import foo from 'foo' instead of const foo = require('foo') to import the package. You also need to put "type": "module" in your package.json and more. Follow the below guide.
  2. If the package is used in an async context, you could use await import(…) from CommonJS instead of require(…).
  3. Stay on the existing version of the package until you can move to ESM.
@jmendiara
jmendiara / cache.ts
Last active December 14, 2020 14:15
LRU Cache for Promises in typescript
import LRU from 'lru-cache';
/**
* Small utility around an LRU cache that gives us some features:
* - Promisification: Async ready to easily change to a memcached/redis implementation
* - Improved get method, to make more expressive usage of the pattern "if not found
* in the cache, go get it, store in the cache, and return the value"
* - Funnel values: Promises are stored and returned, so if the value for a key
* is being obtained while another get is requested, the promise of the value is returned
* so only one request for value is done
@jmendiara
jmendiara / index.ts
Last active December 18, 2020 01:03
Concurrent TaskQueue with lifecycle notification in typescript (alike a Promise.map with concurrency and continue on failure)
import { TaskQueue, TaskSuccessEvent, TaskErrorEvent, QueueStartEvent, QueueCompleteEvent } from "/taskqueue";
// Define several task with optional titles
const firstTask = () => new Promise<void>((resolve, reject) => setTimeout(resolve, 1000));
firstTask.title = '1 second timeout';
const secondTask = () => new Promise<void>((resolve, reject) => setTimeout(() => reject(new Error('boom!')), 1000));
secondTask.title = '1 second failed timeout';
@br3ndonland
br3ndonland / github-actions-notes.md
Last active April 21, 2024 04:25
Getting the Gist of GitHub Actions
@merikan
merikan / Jenkinsfile
Last active April 27, 2024 03:58
Some Jenkinsfile examples
Some Jenkinsfile examples
@cherti
cherti / alert.sh
Created December 9, 2016 13:47
send a dummy alert to prometheus-alertmanager
#!/bin/bash
name=$RANDOM
url='http://localhost:9093/api/v1/alerts'
echo "firing up alert $name"
# change url o
curl -XPOST $url -d "[{
\"status\": \"firing\",
@stevemao
stevemao / np.sh
Last active June 15, 2022 00:37 — forked from sindresorhus/np.sh
Publish node module
# npm publish with goodies
# prerequisites:
# `npm install -g trash conventional-recommended-bump conventional-changelog conventional-github-releaser conventional-commits-detector json`
# `np` with optional argument `patch`/`minor`/`major`/`<version>`
# defaults to conventional-recommended-bump
# and optional argument preset `angular`/ `jquery` ...
# defaults to conventional-commits-detector
np() {
travis status --no-interactive &&
trash node_modules &>/dev/null;
@tswaters
tswaters / git-subdirectory-tracking.md
Last active February 19, 2024 21:15
Adding subdirectory of a remote repo to a subdirectory in local repo

This is way more complicated than it should be. The following conditions need to be met :

  1. need to be able to track and merge in upstream changes
  2. don't want remote commit messages in master
  3. only interested in sub-directory of another repo
  4. needs to go in a subdirectory in my repo.

In this particular case, I'm interested in bringing in the 'default' template of jsdoc as a sub-directory in my project so I could potentially make changes to the markup it genereates while also being able to update from upstream if there are changes. Ideally their template should be a separate repo added to jsdoc via a submodule -- this way I could fork it and things would be much easier.... but, it is what it is.

After much struggling with git, subtree and git-subtree, I ended up finding this http://archive.h2ik.co/2011/03/having-fun-with-git-subtree/ -- it basically sets up separate branches from tracking remote, the particular sub-directory, and uses git subtree contrib module to pull it all togther. Following are

@visnup
visnup / listenOnPortOrSocketFile.js
Last active May 17, 2019 11:57
Listen on a TCP port or a UNIX socket file in node.js. Handle EADDRINUSE for the socket file by deleting it and re-listening.
var fs = require('fs')
, net = require('net')
, http = require('http')
, port = process.env.PORT;
var app = function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
};
@Stuk
Stuk / exec.js
Created August 14, 2013 00:15
Wrap Node's `child_process.spawn` with a promise interface that rejects if the process has an error, or exits with a code other than zero.
var spawn = require("child_process").spawn;
var Q = require("q");
/**
* Wrap executing a command in a promise
* @param {string} command command to execute
* @param {Array<string>} args Arguments to the command.
* @param {string} cwd The working directory to run the command in.
* @return {Promise} A promise for the completion of the command.
*/