0.7.1 • Published 2 years ago

fastify-isolate v0.7.1

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

fastify-isolate

Loads a fastify plugin into a separate V8 isolate. It will have a different require.cache, so loaded modules could be safely gc'ed once the isolate goes out of scope.

The plugin can be both commonjs or esm.

Install

npm i fastify-isolate

Usage

'use strict'

const Fastify = require('fastify')
const isolate = require('fastify-isolate')

const app = Fastify()

app.addHook('onRequest', async function (req) {
  req.p = Promise.resolve('hello')
  console.log('promise constructor is the same', Object.getPrototypeOf(req.p).constructor === Promise)
})

app.register(isolate, {
  path: __dirname + '/plugin.js',
  onError (err) {
    // uncaught exceptions within the isolate will land inside this
    // callback
  }
})

app.listen(3000)

Inside plugin.js:

'use strict'

// We are in a diff V8 isolate now
const sleep = require('timers/promises').setTimeout

module.exports = async function (app) {
  app.get('/', async (req, res) => {
    console.log('promise constructor is different', Object.getPrototypeOf(req.p).constructor === Promise)
    return 'Hello World!'
  })
}

Missing isolates support

In case there is no compiler toolchain available in the system, compiling the isolates support for the current Node.js version would be impossible. In this case we rely on import-fresh instead.

It's also possible to turn on the fallback mechanism with the fallback: true option:

'use strict'

const Fastify = require('fastify')
const isolate = require('fastify-isolate')

const app = Fastify()

app.addHook('onRequest', async function (req) {
  req.p = Promise.resolve('hello')
  console.log('promise constructor is the same', Object.getPrototypeOf(req.p).constructor === Promise)
})

app.register(isolate, {
  path: __dirname + '/plugin.js',
  fallback: true
})

app.listen(3000)

License

MIT

0.7.1

2 years ago

0.7.0

2 years ago

0.6.1

2 years ago

0.6.0

2 years ago

0.5.0

2 years ago

0.3.2

2 years ago

0.4.0

2 years ago

0.3.1

2 years ago

0.3.0

2 years ago

0.2.0

2 years ago

0.1.2

2 years ago

0.1.1

2 years ago

0.1.0

2 years ago