Skip to content

Commit befa8d6

Browse files
committed
Add simulator
1 parent e623c46 commit befa8d6

2 files changed

Lines changed: 35 additions & 0 deletions

File tree

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,15 @@ Once the server's started, you can use the client to create and manage remote Hy
4848
### API
4949
To work with Hyperspace, you'll probably want to start with the [Node.js client library](https://github.com/hyperspace-org/client). The README over there provides detailed API info.
5050

51+
### Simulator
52+
53+
Hyperspace includes a "simulator" that can be used to create one-off Hyperspace instances, which are useful for testing.
54+
55+
```js
56+
const createHyperspaceSimulator = require('hyperspace/simulator')
57+
// client and server are instances of HyperspaceServer and HyperspaceClient that can be used for testing.
58+
const { client, server, cleanup } = await createHyperspaceSimulator()
59+
```
60+
5161
### License
5262
MIT

simulator.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const ram = require('random-access-memory')
2+
3+
const { Server, Client } = require('.')
4+
5+
module.exports = async function createHyperspaceSimulator () {
6+
const simulatorId = `hyperspace-simulator-${process.pid}`
7+
8+
const server = new Server({
9+
host: simulatorId,
10+
storage: ram,
11+
noMigrate: true
12+
})
13+
await server.open()
14+
15+
const client = new Client({
16+
host: simulatorId
17+
})
18+
19+
return { client, server, cleanup }
20+
21+
async function cleanup () {
22+
if (client) await client.close()
23+
if (server) await server.close()
24+
}
25+
}

0 commit comments

Comments
 (0)