Skip to content

Instantly share code, notes, and snippets.

View Aschen's full-sized avatar
💭
:trollface:

Adrien Maret Aschen

💭
:trollface:
View GitHub Profile
@Aschen
Aschen / expose-admin-console.ts
Created November 1, 2021 15:42
Expose Admin Console in a Kuzzle controller
app.controller.register('redirect', {
actions: {
proxy: {
handler: async request => {
const { css, js, img, fonts } = request.input.args;
let url;
let contentType;
if (css) {
url = `http://console.kuzzle.io/css/${css}`
@Aschen
Aschen / pipe-restrict-index-list.js
Created September 18, 2020 12:01
Restrict index and collection list to user who are allowed to act on it
class KuzzlePlugin {
init (config, context) {
this.pipes = {
'index:afterList': 'restrictIndexes',
'collection:afterList': 'restrictCollections'
};
}
@Aschen
Aschen / README.md
Last active June 23, 2020 04:13
Geofencing precision

Kuzzle Geofencing precision

I have created a geofencing area around the Ram Manohar Lohia Hospital in New Delhi by using geojson.io.

Ram Manohar Lohia Hospital

I added two point, one inside and the other one outside the hospital but very close to geofencing zone limit (~2m).

Then I use Kuzzle realtime engine to subscribe to notification about document entering or exiting the Hospital geofencing area.

@Aschen
Aschen / unique-id.js
Created May 28, 2020 10:26
Generate random unique ID
const CHARSET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
function uniqueId (size) {
let id = '';
for (let idx = size; idx--;) {
id += CHARSET[(Math.random() * CHARSET.length | 0)];
}
return id;
@Aschen
Aschen / README.md
Last active January 10, 2021 19:47
V8 Promise Benchmarks

V8 Promise Benchmarks

EDIT: 2021-01-10
Fixed here: nodejs/node#34291

EDIT: 2020-07-10
Explanation here: nodejs/node#33384 (comment)

By benchmarking promises against different version of Node.js, I noticed a performance drop in traditional promises (then/catch) between Node.js 10 and Node.js 12.

const contextStore = new Map();
contextStore.set('requestId', uuid());
asyncLocalStorage.run(contextStore, async () => {
// entering asynchronous context
});
const log = message => {
const requestId = asyncLocalStorage.getStore();
if (requestId) {
console.log(`[${requestId}] ${message}`);
}
else {
console.log(message);
}
};
const { AsyncLocalStorage } = require('async_hooks');
const asyncLocalStorage = new AsyncLocalStorage();
app.get('/', (request, response) => {
const requestId = uuid();
asyncLocalStorage.run(requestId, async () => {
// entering asynchronous context
log('Start processing');
await emailService.notify(request.body.emails);
class EmailService {
constructor (log) {
this.log = log;
}
async notify (emails, requestId) {
for (const email of emails) {
this.log(`[${requestId}] Send email: ${email}`);
sendGrid.send(email);
}
const app = express();
const log = message => console.log(message);
const emailService = new EmailService(logger);
app.get('/', (request, response) => {
const requestId = uuid();
log(`[${requestId}] Start processing`);
await emailService.notify(request.body.emails, requestId);