Skip to content

Instantly share code, notes, and snippets.

View eschwartz's full-sized avatar

Edan Schwartz eschwartz

View GitHub Profile
@eschwartz
eschwartz / promise-batch.js
Last active November 9, 2016 15:38
Run a series of async batch jobs
/**
* Run a series of async functions in "batches",
* so that `batchSize` number of fns are running concurrently
*
* @param {function():Promise<T>[]} fns
* @param {int} batchSize
* @return {Promise<T[]>}
*/
function batch(fns, batchSize) {
const batchFns = _.chunk(fns, batchSize)
@eschwartz
eschwartz / S3InMemoryClient.js
Last active September 27, 2016 21:13
S3 Mock, using In-Memory key->val store
const co = require('co');
const stream = require('stream');
function S3InMemoryClient() {
var memFs = {};
const client = {
upload: (opts, cb) => co(function*() {
const data = opts.Body instanceof stream.Readable ?
@eschwartz
eschwartz / assert-partial-no-lodash.ts
Last active December 1, 2017 23:04
assert-partial.js
import * as assert from 'assert';
function assertPartial(actualObj:any, expectedPartial: any, msg?:string) {
const actualPartial:any = Object.keys(expectedPartial)
.reduce((part, key) => ({
...part,
[key]: actualPartial[key]
}), {});
assert.deepStrictEqual(actualPartial, expectedPartial, msg);
@eschwartz
eschwartz / mongo-hook.js
Last active October 3, 2016 16:58
MongoDB Test Hook
const co = require('co');
const portfinder = require('portfinder');
const mongoServer = require('mongodb-prebuilt');
const tmp = require('tmp');
tmp.setGracefulCleanup();
var db, mongoDataTmp;
before(() => co(function*() {
const dsn = process.env.MONGO_DB_DSN || (yield startMongoServer());
@eschwartz
eschwartz / promise-timeout.js
Created October 10, 2016 16:52
Promise Timeout
/**
* @param {Promise<T>} p
* @param {int} duration Milliseconds until timeout
*
* @returns {Promise<T, TimeoutError>}
*/
function timeout(p, duration) {
const failAfterDuration = new Promise((resolve, reject) => {
const err = Object.assign(new Error(`Timed out after ${duration}ms`), { name: 'TimeoutError' });
@eschwartz
eschwartz / download.js
Last active November 3, 2016 18:34
Download File
/**
*
* @param {string|Object} reqOpts Url, or `request` options
* @param {string} destFile file path
* @throws ExternalServiceError
* @return {Promise}
*/
function download(reqOpts, destFile) {
return new Promise((resolve, reject) => {
request(reqOpts)
@eschwartz
eschwartz / tmp.js
Last active December 13, 2017 21:23
Promise wrapper for node-tmp
const co = require('co');
const _tmp = require('tmp');
_tmp.setGracefulCleanup();
// Promise wrappers around tmp
const tmp = {
/** @return {Promise<{path, cleanup}>} */
file: () => co(function* () {
return yield cb => _tmp.file({ unsafeCleanup: true }, (err, path, fd, cleanup) => cb(err, {
path,
@eschwartz
eschwartz / loadEnv.node8.js
Last active November 14, 2017 17:57
Load env vars from S3
const S3 = require('aws-sdk').S3;
const dotenv = require('dotenv');
const Url = require('url');
const p = require('util').promisify;
const fs = require('fs');
/**
* @param {string} envUri
* @returns {Promise<Object>}
*/
@eschwartz
eschwartz / ReplicationController.js
Last active November 16, 2016 17:22
Node.js ReplicationController model
const _ = require('lodash');
/**
* @param {{
* name: string,
* version: string,
* image: string,
* labels?: Object,
* replicas?: int,
* imagePullPolicy?: 'Always' | 'Never' | 'IfNotPresent',
@eschwartz
eschwartz / create-k8s-config.js
Last active November 16, 2016 17:41
Generate a Kubernetes ReplicationController
// See https://gist.github.com/eschwartz/4f16fcb963954d433753ffe32ce5d5ca
const ReplicationController = require('./ReplicationController');
const fs = require('fs-extra');
// See https://gist.github.com/eschwartz/310a7d9938b60eaf49c8d56c1ccfb759
const loadS3Env = require('./loadS3Env');
const path = require('path');
const Cli = require('admiral-cli');
const co = require('co');
function main() {