Skip to content

Instantly share code, notes, and snippets.

View dpjayasekara's full-sized avatar

dpjayasekara

View GitHub Profile
@dpjayasekara
dpjayasekara / restify-request-logger.js
Created September 25, 2019 15:20 — forked from deepal/restify-request-logger.js
Restify middleware for request logging including response code and response time
this.server.pre((req, res, next) => {
this.logger.debug(`Start ${req.method} ${req.url}`);
const requestStart = process.hrtime();
res.on('finish', () => {
const [reqTimeS, reqTimeNS] = process.hrtime(requestStart),
requestDurationMS = (reqTimeS * 1e3 + reqTimeNS / 1e9).toFixed(2);
this.logger.debug(`End ${req.method} ${req.url} (status=${res.statusCode} duration=${requestDurationMS}ms)`);
});
next();
});
@dpjayasekara
dpjayasekara / worker.js
Created October 14, 2019 21:11 — forked from deepal/worker.js
BCrypt Server worker
const {wrapAsWorker} = require('./pool');
const encrypt = require('../encrypt');
wrapAsWorker(encrypt);
@dpjayasekara
dpjayasekara / encrypt.js
Created October 14, 2019 21:11 — forked from deepal/encrypt.js
Encrypt with bcrypt
const bcrypt = require('bcrypt');
module.exports = (plainText) => {
const salt = bcrypt.genSaltSync(12);
return bcrypt.hashSync(plainText, salt);
}
@dpjayasekara
dpjayasekara / bcrypt_server_sync.js
Created October 14, 2019 21:11 — forked from deepal/bcrypt_server_sync.js
BCrypt server with sync bcrypt function
const express = require('express');
const encrypt = require('./encrypt');
const app = express();
app.get('/', (req, res, next) => {
const enc = encrypt(req.query.text);
res.status(200).send(enc);
});
app.listen(8080);
@dpjayasekara
dpjayasekara / bcrypt_server_multithreaded.js
Created October 14, 2019 21:11 — forked from deepal/bcrypt_server_multithreaded.js
Multi-threaded with BCrypt server with dynamically spawned workers
const express = require('express');
const {Worker, isMainThread, parentPort, workerData} = require('worker_threads');
const encrypt = require('./encrypt');
if (isMainThread) {
const app = express();
app.get('/', (req, res, next) => {
console.log('serving request...');
const worker = new Worker(__filename, {workerData: req.query.text});
[
{
type: 'create',
tid: 'c3b88a9f-9c74-4036-8203-7ac818958335',
data: {
title: 'New user account',
description: 'Create a new user account for Captain America',
status: 'open',
assignee: 'shield-support'
},
db.createView('tickets', 'ticketevents', [
{
$sort: { timestamp: 1 }
},
{
$group: {
_id: '$tid',
ticketDetails: {
$mergeObjects: '$data'
},
@dpjayasekara
dpjayasekara / v8-compile-javascript-example.cc
Created October 14, 2019 21:11 — forked from deepal/v8-compile-javascript-example.cc
Simple example how V8 can compile JavaScript source and run it
// Create a new context.
Local<Context> context = Context::New(isolate);
// Enter the context for compiling and running the hello world script.
Context::Scope context_scope(context);
// Create a string containing the JavaScript source code.
Local<String> source = String::NewFromUtf8(isolate, "'Hello' + ', World!'");
// Compile the source code.
const {EventEmitter} = require('events');
const emitter = new EventEmitter();
emitter.on('message', (message) => {
console.log(`Handler 1 message: ${message}`);
});
emitter.on('message', (message) => {
console.log(`Handler 2 message: ${message}`);
});
@dpjayasekara
dpjayasekara / v8-templates-example.cc
Created October 14, 2019 21:11 — forked from deepal/v8-templates-example.cc
Simple example how V8 Function\Object Templates can be used
// Create a template for the global object and set the built-in global functions
Local<ObjectTemplate> global = ObjectTemplate::New(isolate);
global->Set(
String::NewFromUtf8(isolate, "log"),
FunctionTemplate::New(isolate, LogCallback)
);
// Each processor gets its own context so different processors do not affect each other
Persistent<Context> context = Context::New(isolate, NULL, global);