Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mcollina/everysync
Make any API sync
https://github.com/mcollina/everysync
Last synced: about 1 month ago
JSON representation
Make any API sync
- Host: GitHub
- URL: https://github.com/mcollina/everysync
- Owner: mcollina
- License: mit
- Created: 2024-06-19T19:34:20.000Z (6 months ago)
- Default Branch: main
- Last Pushed: 2024-10-18T10:29:45.000Z (2 months ago)
- Last Synced: 2024-11-01T12:42:25.868Z (about 2 months ago)
- Language: JavaScript
- Size: 36.1 KB
- Stars: 17
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# everysync
Make any API sync with the help of [`node:worker_threads`](https://nodejs.org/api/worker_threads.html) and [`node:fs`](https://nodejs.org/api/worker_threads.html).
## Installation
```bash
npm install everysync
```## Expose async APIs from a worker thread
Caller side:
```javascript
const { join } = require('node:path')
const assert = require('node:assert')
const { Worker } = require('node:worker_threads')
const { makeSync } = require('everysync')const buffer = new SharedArrayBuffer(1024, {
maxByteLength: 64 * 1024 * 1024,
})
const worker = new Worker(join(__dirname, 'echo.mjs'), {
workerData: {
data: buffer,
},
})const api = makeSync(buffer)
assert.strictEqual(api.echo(42), 42)
worker.terminate()
```Worker side (`echo.mjs`):
```javascript
import { wire } from 'everysync'
import { workerData } from 'node:worker_threads'
import { setTimeout } from 'node:timers/promises'wire(workerData.data, {
async echo (value) {
await setTimeout(1000)
return value
},
})// Keep the event loop alive
setInterval(() => {}, 100000)
```## License
MIT