# fastify-isolate

> Load Fastify plugins in a different isolate

Latest version **0.7.1** (published 2022-09-16) · MIT license · 0 weekly downloads

## Install

```sh
npm install fastify-isolate
pnpm add fastify-isolate
yarn add fastify-isolate
bun add fastify-isolate
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.7.1 |
| Published | 2022-09-16 |
| First published | 2022-02-23 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 15.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 59 |
| Author | Matteo Collina |
| Maintainers | matteo.collina |
| Keywords | fastify, isolate, synchronous, worker |

## Links

- npm: https://www.npmjs.com/package/fastify-isolate
- Repository: https://github.com/mcollina/fastify-isolate
- Homepage: https://github.com/mcollina/fastify-isolate#readme
- Issues: https://github.com/mcollina/fastify-isolate/issues
- npm.io page: https://npm.io/package/fastify-isolate

## Dependencies (2)

- [import-fresh](https://npm.io/package/import-fresh.md) ^3.3.0
- [@matteo.collina/isolates](https://npm.io/package/@matteo.collina/isolates.md) ^2.1.0

## Alternatives

- [cron](https://npm.io/package/cron.md) — 4.9M weekly downloads
- [@vercel/queue](https://npm.io/package/@vercel/queue.md) — 731.6K weekly downloads
- [create-sonicjs](https://npm.io/package/create-sonicjs.md) — 1.6K weekly downloads
- [@exellix/jobs-api](https://npm.io/package/@exellix/jobs-api.md) — 941 weekly downloads
- [@forwardimpact/libskill](https://npm.io/package/@forwardimpact/libskill.md) — 575 weekly downloads

## Recent versions

- 0.7.1 (latest) — 2022-09-16
- 0.7.0 — 2022-09-15
- 0.6.1 — 2022-09-08
- 0.6.0 — 2022-09-08
- 0.5.0 — 2022-08-16
- 0.4.0 — 2022-08-08
- 0.3.2 — 2022-06-22
- 0.3.1 — 2022-06-20
- 0.3.0 — 2022-04-14
- 0.2.0 — 2022-04-10
- 0.1.2 — 2022-03-05
- 0.1.1 — 2022-02-23
- 0.1.0 — 2022-02-23

## README

# 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

```bash
npm i fastify-isolate
```

## Usage

```js
'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`:

```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](https://npm.im/import-fresh)
instead.

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

```js
'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

---
_Source: https://npm.io/package/fastify-isolate · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
