Skip to content

Instantly share code, notes, and snippets.

View NickNaso's full-sized avatar
🎯
Focusing

Nicola Del Gobbo NickNaso

🎯
Focusing
View GitHub Profile
@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 / 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 / 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 / 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 / .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.

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'

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
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();
@NickNaso
NickNaso / binding.cc
Created September 19, 2018 18:44 — forked from gabrielschulhof/binding.cc
Doing extends on the native side
#include <stdio.h>
#include <node.h>
static void
NativeClassConstructor(const v8::FunctionCallbackInfo<v8::Value>& info) {
}
static void
NativeClassProtoMethod(const v8::FunctionCallbackInfo<v8::Value>& info) {
fprintf(stderr, "NativeClassProtoMethod was called\n");
@NickNaso
NickNaso / express-axios-example.js
Last active October 31, 2018 20:50
Example how to use axios from Node.js
'use strict'
const axios = require('axios')
const express = require('express')
const app = express()
app.use((err, req, res, next) => {
console.error('Error happened ...')
console.log(err)
res.status(500).json({error: 'Error happened on your application', code: 'E500'})