everysync

0.2.2 • Public • Published

everysync

Make any API sync with the help of node:worker_threads and node:fs.

Installation

npm install everysync

Expose async APIs from a worker thread

Caller side:

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):

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

Readme

Keywords

none

Package Sidebar

Install

npm i everysync

Weekly Downloads

1

Version

0.2.2

License

MIT

Unpacked Size

9.36 kB

Total Files

14

Last publish

Collaborators

  • matteo.collina