Skip to content

Instantly share code, notes, and snippets.

View NickNaso's full-sized avatar
🎯
Focusing

Nicola Del Gobbo NickNaso

🎯
Focusing
View GitHub Profile
Napi::Value value;
Napi::Function dateFunc = value.Env().Global().Get("Date").As<Napi::Function>();
if (value.As<Napi::Object>().InstanceOf(dateFunc)) {
// it is a date
}
Napi::Function isIntegerFunction = value.Env().Global().Get("Number").As<Napi::Object>().Get("IsInteger").As<Napi::Function>();
bool isInt = isIntegerFunction.Call({value}).ToBoolean().Value();

It is not usually recommended to reuse errors since the stack trace is attached whenever the error is instantiated.

However in some cases the overhead of doing that may affect performance, especially if the error is an exception that is known to occur frequently and is handled.

Only in these cases it may be advantagous to reuse the created Error. In all other cases getting a full stacktrace created at the time the exception occurs is to be preferred as it gives much better information needed to debug the problem.

Run

➝ node errors.js

Summary

Tried to improve debugging when an express middleware is wrapped to auto-handle errors of an asnync function.

Turns out the below reads a bit better than a return Promise.catch() implementation, but still, once we reach the central express error handler, the line of the wrapped function that caused the error isn't included.

Implementation

'use strict'
@NickNaso
NickNaso / .generating-xcode-via-gyp.md
Created August 26, 2018 15:05 — forked from thlorenz/.generating-xcode-via-gyp.md
Generating Xcode projects from Node.js binding repos via gyp

This is specifically tailored to Node.js binding projects in which case the C++ layer is always a library.

Clone gyp:

git clone --depth 1 https://chromium.googlesource.com/external/gyp.git gyp

Add the below common.gypi file in the root of the project.

@NickNaso
NickNaso / native-addon-xcode.md
Last active August 26, 2018 15:14
Create xcode project for Node.js Native Add-Ons

How to create Xcode project for Node.js Native Add-Ons

These are the steps to create an Xcode project for your Node.js Native Add-Ons:

  • Go inside the folder of your project

  • Digit the following command: node-gyp configure --debug -- -f xcode

Now in the folder build you can find a file called binding.xcodeproj

@NickNaso
NickNaso / Dockerfile
Created August 10, 2018 19:34 — forked from PurpleBooth/Dockerfile
Create a static binary in go and put it in a from scratch docker container
FROM golang:1.9
RUN mkdir -p /go/src/github.com/purplebooth/example
WORKDIR /go/src/github.com/purplebooth/example
COPY . .
RUN go build -ldflags "-linkmode external -extldflags -static" -a main.go
FROM scratch
COPY --from=0 /go/src/github.com/purplebooth/example/main /main
CMD ["/main"]
@NickNaso
NickNaso / cgo1.go
Created July 7, 2018 11:05 — forked from tejainece/cgo1.go
Examples of calling C code from Golang
package main
//#include<stdio.h>
//void inC() {
// printf("I am in C code now!\n");
//}
import "C"
import "fmt"
func main() {
@NickNaso
NickNaso / binding.cc
Created July 6, 2018 08:32 — forked from gabrielschulhof/binding.cc
GC behaviour wrt. native function vs. plain object vs. JS function
#include <stdio.h>
#include <node.h>
class WeakRef {
public:
WeakRef(v8::Isolate* isolate, v8::Local<v8::Value> func, const char* string):
string_(string) {
pers_.Reset(isolate, func);
pers_.SetWeak(this, DeleteMe, v8::WeakCallbackType::kParameter);
}
@NickNaso
NickNaso / native-emitter-implementation.cc
Created May 25, 2018 18:13
Native emitter implementation
#include <chrono>
#include <thread>
#include <iostream>
#include "native-emitter.h"
Napi::FunctionReference NativeEmitter::constructor;
Napi::Object NativeEmitter::Init(Napi::Env env, Napi::Object exports) {
Napi::HandleScope scope(env);
@NickNaso
NickNaso / inherits-from-event-emitter.js
Created May 25, 2018 18:09
Inherits from Event Emitter
'use strict'
const EventEmitter = require('events').EventEmitter
const NativeEmitter = require('bindings')('native_emitter').NativeEmitter
const inherits = require('util').inherits
inherits(NativeEmitter, EventEmitter)
const emitter = new NativeEmitter()