Skip to content

mcollina/everysync

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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:

import { join } from 'node:path'
import { strictEqual } from 'node:assert'
import { Worker } from 'node:worker_threads'
import { makeSync } from 'everysync'

const buffer = new SharedArrayBuffer(1024, {
  maxByteLength: 64 * 1024 * 1024,
})
const worker = new Worker(join(import.meta.dirname, 'echo.mjs'), {
  workerData: {
    data: buffer,
  },
})

const api = makeSync(buffer)

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

About

Make any API sync

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published

Contributors 3

  •  
  •  
  •