Skip to content

Instantly share code, notes, and snippets.

Avatar

Trevor Norris trevnorris

View GitHub Profile
@trevnorris
trevnorris / example_output.log
Created November 16, 2022 19:33
Use async_hooks to automatically create stack traces of resources
View example_output.log
8 TickObject
at AsyncStackTrace.initFn (index.js:89:18)
6 TCPWRAP
at AsyncStackTrace.initFn (index.js:89:18)
3 TCPSERVERWRAP
at AsyncStackTrace.initFn (index.js:89:18)
at Object.<anonymous> (test/http-noop.js:11:4)
View triangular_weighted_random.js
// Call this function with the number of levels in the queue.
// It will return the weighted random position.
function getPosition(n) {
return n - tRoot(randomT(n));
}
function triangular(n) {
return n * (n + 1) / 2;
}
View unicode_event_loop.txt
Example Event Loop Execution Timeline
L₁ L₂ L₃ L₄
(1) ━━━┪ ┏━━━━━━━━━━━━━━╈┳━━━━━━━━━┯━━━━━━━━━━━╈┳━━━━┓ ┢━━━━━
┃ ┃ e₁ ┃┃ e₂ │ e₃ ┃┃ e₄ ┃ ┃ e₅
───┨ ┠──────────────┨┠─────────┼───────────╂╂────┨ ┠─────
(2) ┄┄┄┺━━━╃┄┄┄┄┄┄┄┄┄┄┄┄┄┄┺╃┄┄┄┄┄┄┄┄┄┴┄┄┄┄┄┄┄┄┄┄┄┺╃┄┄┄┄┺━━━━╃┄┄┄┄┄
│ [e₂] [e₃] │ [e₄] │ │
[e₁] [e₂] [e₄] [e₅]
[e₃]
@trevnorris
trevnorris / 01_pass_template_fn_to_fn_ptr.cc
Last active January 27, 2020 21:36
various c++ code examples demonstrating different techniques
View 01_pass_template_fn_to_fn_ptr.cc
// Demonstrates how to pass a template function to a non-template function
// pointer by specifying the template parameter when it's assigned.
// This allows the void* to be safely cast when the function is called.
#include <cstdio>
struct A { int val = 42; };
struct B { int foo = 0; int val = 45; };
void (*cbp)(void*);
@trevnorris
trevnorris / git_cheat_sheet.md
Last active January 29, 2020 00:09
cheat sheet of git commands I use
View git_cheat_sheet.md

note: A lot of this assumes a repo has already been forked.

Basics

First make sure all your commits have your name/email:

git config --global user.name "YOUR NAME"
git config --global user.email "YOUR EMAIL"
@trevnorris
trevnorris / build_steps.txt
Last active August 10, 2018 20:30
How to build node with shared libuv
View build_steps.txt
libuv:
$ ./gyp_uv.py -f make -Dlibrary=shared_library
$ BUILDTYPE=Release make -j6 -C out
node:
$ ./configure --shared-libuv --shared-libuv-includes=$LIBUV_PATH/include --shared-libuv-libpath=$LIBUV_PATH/out/Release
$ make -j6
binary build:
$ CONFIG_FLAGS="--shared-libuv --shared-libuv-includes=$LIBUV_PATH/libuv/include --shared-libuv-libpath=$LIBUV_PATH/out/Release" DISTTYPE=custom CUSTOMTAG="$(date +'%s')" make -j6 binary
View rethrow_without_verbose.diff
diff --git a/test/addons/async-hello-world/binding.cc b/test/addons/async-hello-world/binding.cc
index da2bd41..5ddd61f 100644
--- a/test/addons/async-hello-world/binding.cc
+++ b/test/addons/async-hello-world/binding.cc
@@ -44,13 +44,11 @@ void AfterAsync(uv_work_t* r) {
v8::Local<v8::Function>::New(isolate, req->callback);
callback->Call(isolate->GetCurrentContext()->Global(), 2, argv);
+ printf("did rethrow: %s\n", !try_catch.ReThrow().IsEmpty() ? "true" : "false");
+
View udp_client.js
'use strict';
Error.stackTraceLimit = Infinity;
const dgram = require('dgram');
const print = process._rawDebug;
// Problem doesn't exist with localhost, so plug in a Google server's IP.
const HOST = '172.217.5.110';
const client = dgram.createSocket('udp4');
const message = Buffer.alloc((1 << 16) - 29);
@trevnorris
trevnorris / fs-scan-n-read-bench.js
Last active March 11, 2017 00:25
Benchmark to show performance difference between fs sync and async calls
View fs-scan-n-read-bench.js
'use strict';
const MAX_DEPTH = 4;
const fs = require('fs');
const print = process._rawDebug;
const sep = require('path').sep;
const tmpdir = require('os').tmpdir() + sep + 'data-file-bench-' + genName();
const file_list = newDir();
var current_dir = file_list;
@trevnorris
trevnorris / intercept-stdio.cc
Last active February 27, 2017 21:16
Example of how to intercept messages to stdout/stderr
View intercept-stdio.cc
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#if defined(__unix__) || defined(__unix) || \
(defined(__APPLE__) && defined(__MACH__))
#include <unistd.h>
#endif