https://github.com/suin/runspace
Isolated execution context for Node.js.
https://github.com/suin/runspace
isolation sandbox
Last synced: 11 months ago
JSON representation
Isolated execution context for Node.js.
- Host: GitHub
- URL: https://github.com/suin/runspace
- Owner: suin
- License: mit
- Created: 2020-10-05T14:04:52.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2020-10-09T07:27:41.000Z (over 5 years ago)
- Last Synced: 2025-07-03T00:53:28.942Z (12 months ago)
- Topics: isolation, sandbox
- Language: TypeScript
- Homepage: https://suin.github.io/runspace/
- Size: 337 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# @suin/runspace
Isolated execution context for Node.js.
## Installation
```shell script
# via NPM
npm install --save @suin/runspace
# via Yarn
yarn add @suin/runspace
```
## Usage
Basic usage:
```javascript
// main.js
const { ThreadSpace, ChildProcessSpace } = require("@suin/runspace");
(async () => {
const space = new ThreadSpace({ filename: "./target.js" }) // Or new ChildProcessSpace({ filename: "./target.js" })
.on("message", (message) => console.log(message))
.on("error", (error) => console.error(error))
.on("rejection", (reason) => console.error(reason));
await space.waitStart();
space.send("Hello");
await space.waitStop();
})();
// target.js
process.on("message", (message) => {
process.send(message + " World");
process.exit();
});
// Output:
// "Hello World"
```
Waits a message from the program inside the space:
```javascript
// target.js
const http = require("http");
const server = http.createServer((req, res) => {
res.write("OK");
res.end();
});
server.listen(8000, () => {
process.send("HTTP_SERVER_READY");
});
// main.js
const { ThreadSpace } = require("@suin/runspace");
const fetch = require("node-fetch");
(async () => {
const space = new ThreadSpace({ filename: "./target.js" });
await space.waitStart();
await space.waitMessage((message) => message === "HTTP_SERVER_READY");
const res = await fetch("http://localhost:8000");
const text = await res.text();
await space.stop();
console.log(text); // Output: "OK"
})();
```