Skip to content

Instantly share code, notes, and snippets.

View barisere's full-sized avatar

Barisere Jonathan barisere

View GitHub Profile
@barisere
barisere / AMQP_message_sequencer.ts
Last active February 6, 2022 08:35
A TypeScript class that allows you read AMQP messages one-at-a-time by sequencing them through a Node.js event emitter.
import { subscriber } from '@private-rabbitmq-wrapper';
import { EventEmitter, once } from 'events';
import { ConsumeMessage, Replies } from 'amqplib';
interface Options {
exchange: string;
eventName: string;
queueName: string;
forwardedEventName: (_: ConsumeMessage) => string;
}
@barisere
barisere / docker-image-migration-steps.txt
Created May 23, 2021 15:05
Retaining Docker images while switching storage drivers
user@hostname:~
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
dpage/pgadmin4 latest 048c641d6e64 4 weeks ago 244MB
debian buster 0d587dfbc4f4 6 weeks ago 114MB
golang 1.16-alpine 14ee78639386 7 weeks ago 301MB
alpine latest 49f356fa4513 7 weeks ago 5.61MB
postgres 13 88590756b124 7 weeks ago 314MB
postgres latest 88590756b124 7 weeks ago 314MB
golang 1.16 64bd1a7b99b7 8 weeks ago 862MB
package main_test
import (
"testing"
"testing/quick"
"unsafe"
)
func all(xs []*int, f func(x *int) bool) (result bool) {
result = true
@barisere
barisere / caesar.ml
Created January 21, 2020 18:37
An OCaml implementation of the core functions of the Caesar cipher. See the Python implementation at https://gist.github.com/barisere/cf57c33eb826156f6b2e6b78f6b087c6.
open Core
module type Caesar_intf = sig
val decrypt: string -> int -> string
val encrypt: string -> int -> string
end
module Caesar: Caesar_intf = struct
let transform_alpha scale c =
if not (Char.is_alpha c) then c else
@barisere
barisere / caesar.py
Created January 21, 2020 10:23
A boring command line program that encrypts or decrypts text using Caesar cipher.
import sys
def normalize(c: str):
"""
Given c, a string of only one character, normalize returns the
character position of c in the English alphabet, with 'a' | 'A' = 0
and 'z' | 'Z' = 25, and an indication of whether c was uppercase.
Only alphabetic ASCII characters are transformed.
"""
if not (c.isascii() and c.isalpha()):
@barisere
barisere / extract-mongo-id.ts
Last active December 2, 2018 13:57
Transform a Mongoose Document object's ObjectId fields to strings. This transformation is necessary, in order to use class-transformer's plainToClass function on documents with ObjectId fields. class-transformer fails and throws an error when converting the BSON ObjectId type to a Buffer.
function extractMongoId(document) {
const doc = document.toObject ? document.toObject() : document;
for (const [key, value] of Object.entries(doc)) {
if (value instanceof ObjectID) {
delete doc[key];
doc[key] = value.toHexString();
} else if (value instanceof Object) {
doc[key] = extractMongoId(value);
}
if (Array.isArray(value)) {
// file resource-controllers.js
const { makeWorker, cancelJob } = require("./undo-queue");
async function deleteResource (/* parameters */) {
// code to delete the resource
}
const scheduleResourceDeletion = makeWorker(deleteResource);
exports.delete = function (req, res, next) {
function makeJobId (task) {
const hash = crypto.createHash("md5");
hash.update(JSON.stringify(task), "utf8");
return hash.digest("hex");
}
/**
* Registers `workerFn` as a function for adding and processing tasks.
* The tasks are identified by workerFn's name property, so use named functions.
*
// undo-queue.js
"use strict";
const crypto = require("crypto");
const Queue = require("bull");
const undoQueueWorkers = new Map();
const { redis: redisConfig } = require("../config");
router.delete('/resource/:id', (req, res) => {
const id = req.params.id;
deleteRequestMap.set(id, deleteResourceOnTimeout(id, 10000));
res.status(200).json({ cancelId: deleteRequestMap.get(id) }).end();
});
router.get('/resource/:id/cancelDelete', (req, res) => {
const id = req.params.id;
clearTimeout(deleteRequestMap.get(id));
res.status(200).json({ message: 'Request cancelled' }).end();