# fastify-sandbox

> Load Fastify plugins in a sandbox

Latest version **0.13.1** (published 2023-05-29) · MIT license · 0 weekly downloads

## Install

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

## 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.13.1 |
| Published | 2023-05-29 |
| First published | 2022-09-26 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 24 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 59 |
| Author | Matteo Collina |
| Maintainers | matteo.collina |
| Keywords | fastify, sandbox, synchronous, worker, hot, reload |

## Links

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

## Dependencies (2)

- [import-fresh](https://npm.io/package/import-fresh.md) ^3.3.0
- [@matteo.collina/worker](https://npm.io/package/@matteo.collina/worker.md) ^3.0.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.13.1 (latest) — 2023-05-29
- 0.13.0 — 2023-05-24
- 0.12.0 — 2023-05-15
- 0.11.0 — 2022-11-22
- 0.10.0 — 2022-11-08
- 0.9.2 — 2022-11-04
- 0.9.1 — 2022-11-03
- 0.9.0 — 2022-11-02
- 0.8.0 — 2022-09-26

## README

# fastify-sandbox

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

The plugin can be both commonjs or esm.

This modules pairs well with [`@fastify/restartable`](https://github.com/fastify/restartable)
to provide hot-reloading mechanism for Fastify applications.

## Install

```bash
npm i fastify-sandbox
```

## Usage

```js
'use strict'

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

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(sandbox, {
  path: __dirname + '/plugin.js',
  options: { // this object will be passed as the options of the loaded plugin
    hello: "world"
  },
  onError (err) {
    // uncaught exceptions within the sandbox will land inside this
    // callback
  }
})

app.listen(3000)
```

Inside `plugin.js`:

```js
'use strict'

// We are in a different V8 Context 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 compiler support

In case there is no compiler toolchain available in the system,
compiling the code needed to 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 sandbox = require('fastify-sandbox')

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(sandbox, {
  path: __dirname + '/plugin.js',
  fallback: true
})

app.listen(3000)
```

Note that ESM is only supported via the native code.

## Caveats

This module does not offer any protection against malicious code.

## License

MIT

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