Skip to content

Instantly share code, notes, and snippets.

Bruno Jouhier bjouhier

Block or report user

Report or block bjouhier

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View foo-wrapped.js
const wrap = require('./wrapper').wrap;
function sleep(ms) {
return new Promise((resolve, reject) => {
setTimeout(resolve, ms);
})
}
async function f1() {
await wrap(sleep(1), __filename, 9);
throw new Error('error1');
View file-syntax.js
const fs = require('fs');
const fsp = require('path');
function scan(dir, results, mod) {
fs.readdirSync(dir).forEach(name => {
const path = fsp.join(dir, name);
let m;
if (fs.statSync(path).isDirectory()) {
scan(path, results, name === 'node_modules' ? null : mod === null ? name : mod);
} else if (mod && (m = /(.*)\.[jt]s$/.exec(name)) && !/\.(d|min|debug)$/.test(m[1])) {
View stupid-password-rule.js
"use strict";
// experimental value
// nsyms is size of alphabet (62 for letters + digits)
// len is password length
// maxReapeat is the max number of repeats we allow
// returns the probability of a password reject.
function expe(nsyms, len, maxRepeat) {
// generate random array of len symbols
const gen = len => {
@bjouhier
bjouhier / timedrift.js
Last active Apr 3, 2017
timedrift.js
View timedrift.js
"use strict";
function millis(hr) {
return hr[0] * 1000 + Math.floor(hr[1] / 1000000);
}
function busyWait(ms, cb) {
var hr0 = process.hrtime();
while (millis(process.hrtime(hr0)) < ms);
var elapsed = millis(process.hrtime(hr0));
View fibers-sleep-parallel.js
var Fiber = require('fibers');
function sleep(millis, id) {
var fiber = Fiber.current;
console.log(id + ": sleep begin");
setTimeout(function() {
console.log("wake up");
fiber.run();
}, millis)
Fiber.yield();
View fibers-sleep-sequential.js
var Fiber = require('fibers');
function sleep(millis, id) {
var fiber = Fiber.current;
console.log(id + ": sleep begin");
setTimeout(function() {
console.log("wake up");
fiber.run();
}, millis)
Fiber.yield();
View fibers-does-not-block.js
console.log("script begin");
var Fiber = require('fibers');
Fiber(function() {
var fiber = Fiber.current;
setTimeout(function() {
console.log("setTimeout callback begin");
fiber.run();
console.log("setTimeout callback end");
}, 10000)
View zeta.js
"use strict";
class Complex {
constructor(a, b) {
this.re = a;
this.im = b;
}
add(c) {
return new Complex(this.re + c.re, this.im + c.im);
}
View query-all.js
"use strict";
exports.queryAll = function(conn, sql, args, cb) {
var allRows = [];
conn.execute(sql, args, {
resultSet: true
}, function(err, result) {
if (err) return cb(err);
function fetch() {
View fibers.js
//fibers
function markComplete(postedItems, completionCallback) {
var errors = [];
var getItemToUpdate = function(item) {
var fiber = Fiber.current;
process.nextTick(function() {
client.queryEntity('tasks', 'partition1', item, function(err, task) {
fiber.run(task);
});
You can’t perform that action at this time.