Skip to content

Instantly share code, notes, and snippets.

View assaf's full-sized avatar

Assaf Arkin assaf

View GitHub Profile
@assaf
assaf / stream.ts
Last active December 23, 2022 03:54
Read status updates from Mastodon streaming API
type StreamEvent =
| { event: "update"; status: Status }
| { event: "status.update"; status: Status }
| { event: "delete"; id: string; status?: never };
/**
* Use like this:
* const { events } = await stream(`https://${instance}/api/v1/streaming/public`);
* for await (const { id, status } of events) {
* if (status) … do something …
@assaf
assaf / truncate.test.ts
Created December 11, 2020 01:39
Truncate long strings, but don't break in middle of a short word
import truncate from "./truncate";
describe("Truncate string", () => {
test("empty string returns self", () => expect(truncate("")).toBe(""));
test("short string returns self", () => {
expect(truncate("word", 8)).toBe("word");
expect(truncate("too long", 8)).toBe("too long");
});
@assaf
assaf / hostnames.txt
Created August 12, 2015 00:30
Time challenge
labnotes.org
example.com
mathforum.org
slack.com
github.com
facebook.com
twitter.com
const Util = require('util');
const a = [];
console.log([].concat(a).length); // => 0
console.log([].slice.call(a).length); // => 0
const Arrayish = function() {
Array.call(this);
}
Util.inherits(Arrayish, Array);
@assaf
assaf / nvmish
Created March 24, 2015 15:43
nvm use <package.json>
#!/usr/bin/env bash
#
# Run nvmish in your current project directory to use the version of io.js/Node
# as specified in package.json. It will also install that version, if not
# already installed.
#
# If package.json specifies engines.iojs, uses the corresponding version of
# io.js, otherwise, if package.json specifies engines.node, uses the
# corresponding version of Node.js.
#
@assaf
assaf / gist:ebd81856ca725f080c52
Created November 11, 2014 20:24
Zero downtime deploy with Dokku (original blog post)

Dokku is a wonderful tool for quickly deploying apps to production, but it's missing zero downtime deploys. Not anymore!

Even our svelte Node.js app needs a few seconds to fully load the main code, and open connections to back-ends servers, before it can start handling requests.

The thing with Dokku: it switches traffic over to the new container before the server loads complete, so each deploy has several seconds of downtime.

That's best case scenario. When we screw deployment (usually some configuration issue that's different in production), even the quickest hands in the west, you still get a few minutes of downtime.

And so it became necessary that I learn how to catch and handle exceptions in Bash (yay, traps!) and shift a few lines of code around.

var Immutable = require("immutable"),
t = require("transducers-js"),
comp = t.comp,
map = t.map,
filter = t.filter,
transduce = t.transduce;
var inc = function(n) { return n + 1; };
var isEven = function(n) { return n % 2 == 0; };
var sum = function(a,b) { return a+b; };

Keybase proof

I hereby claim:

  • I am assaf on github.
  • I am assaf (https://keybase.io/assaf) on keybase.
  • I have a public key whose fingerprint is E318 FD85 22D4 CBAE 142D F811 43A7 A5CA 24D1 FDF5

To claim this, I am signing this object:

@assaf
assaf / gist:6bc56255459aa75f27b3
Created July 9, 2014 00:43
.git/hook/pre-commit
#!/bin/sh
#
# Run npm shrinkwrap on every commit, make sure we have the most recent
# dependencies checked into git.
DEPTH=10 # Set this to N to include more descendants in shrinkwrap
PATH=/usr/local/bin:$PATH
# Success or error, npm shrinkwrap will output to both stdout and stderr.
# We're only interested in what stderr has to say when it fails.
@assaf
assaf / gist:9674758
Last active August 29, 2015 13:57
Mix use
// Liberally adapted from our test suites, which uses generators for complex steps (before/after/etc).
describe("With new customer", function() {
let business;
let customer1, customer2, customer3;
before(function*() {
// We have many tests, but you can only call listen() once
// on an HTTP server, so this is a case for resolve-once, and
// we use a promise.
yield Server.listen();