Skip to content

Instantly share code, notes, and snippets.

@geon
geon / async-iterator-server.ts
Created November 30, 2018 08:47
How to build a socket server with async and for-await.
import { createServer, Socket } from "net";
// Promisified socket.write().
const write = (socket: Socket, data: Buffer | string) =>
new Promise((resolve, reject) =>
socket.write(data as Buffer, (error: Error | undefined) => {
if (error) {
reject();
} else {
resolve();
@timdown
timdown / trim_canvas.js
Created July 26, 2017 16:15
Returns a copy of a canvas element with surrounding transparent space removed
var trimCanvas = (function() {
function rowBlank(imageData, width, y) {
for (var x = 0; x < width; ++x) {
if (imageData.data[y * width * 4 + x * 4 + 3] !== 0) return false;
}
return true;
}
function columnBlank(imageData, width, x, top, bottom) {
for (var y = top; y < bottom; ++y) {