@nxtedition/shared 0.0.1 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +6 -10
  2. package/index.js +16 -4
  3. package/package.json +2 -1
package/README.md CHANGED
@@ -25,13 +25,12 @@ const worker = new Worker(new URL('worker.js', import.meta.url), {
25
25
 
26
26
  const writeToWorker = shared.writer(reader)
27
27
 
28
- shared.reader(writer, async (buffer) => {
28
+ writeToWorker(Buffer.from('ping'))
29
+
30
+ for await (const buffer of shared.reader(writer)) {
29
31
  console.log(`From worker ${buffer}`)
30
32
  await tp.setTimeout(1e3) // Backpressure
31
- })
32
-
33
- while (true) {
34
- await writeToParent(Buffer.from('Hello from parent')
33
+ writeToWorker(Buffer.from('pong'))
35
34
  }
36
35
  ```
37
36
 
@@ -43,12 +42,9 @@ import tp from 'timers/promise'
43
42
 
44
43
  const writeToParent = shared.writer(workerData.writer)
45
44
 
46
- shared.reader(workerData.reader, (buffer) => {
45
+ for await (const buffer of shared.reader(workerData.reader)) {
47
46
  console.log(`From parent ${buffer}`)
48
47
  await tp.setTimeout(1e3) // Backpressure
49
- })
50
-
51
- while (true) {
52
- await writeToParent(Buffer.from('Hello from worker')
48
+ writeToWorker(Buffer.from('pong'))
53
49
  }
54
50
  ```
package/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  const WRITE_INDEX = 0
2
2
  const READ_INDEX = 1
3
+ const END_OF_PACKET = -1
3
4
 
4
5
  export function alloc(size) {
5
6
  return {
@@ -8,7 +9,7 @@ export function alloc(size) {
8
9
  }
9
10
  }
10
11
 
11
- export async function reader({ sharedState, sharedBuffer }, cb) {
12
+ async function* _reader({ sharedState, sharedBuffer }, cb) {
12
13
  const state = new Int32Array(sharedState)
13
14
  const buffer = Buffer.from(sharedBuffer)
14
15
 
@@ -25,13 +26,16 @@ export async function reader({ sharedState, sharedBuffer }, cb) {
25
26
  while (readPos !== writePos) {
26
27
  const len = buffer.readInt32LE(readPos)
27
28
 
28
- if (len === -1) {
29
+ if (len === END_OF_PACKET) {
29
30
  readPos = 0
30
31
  } else {
31
32
  const raw = buffer.slice(readPos + 4, readPos + len)
32
33
  readPos += len
33
-
34
- await cb(raw)
34
+ if (cb) {
35
+ cb()
36
+ } else {
37
+ yield raw
38
+ }
35
39
  }
36
40
 
37
41
  Atomics.store(state, READ_INDEX, readPos)
@@ -41,6 +45,14 @@ export async function reader({ sharedState, sharedBuffer }, cb) {
41
45
  }
42
46
  }
43
47
 
48
+ export function reader(options, cb) {
49
+ if (cb) {
50
+ _reader(options, cb).next()
51
+ } else {
52
+ return _reader(options)
53
+ }
54
+ }
55
+
44
56
  export function writer({ sharedState, sharedBuffer }) {
45
57
  const state = new Int32Array(sharedState)
46
58
  const buffer = Buffer.from(sharedBuffer)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/shared",
3
- "version": "0.0.1",
3
+ "version": "0.0.4",
4
4
  "description": "Ring Buffer for NodeJS cross Worker communication",
5
5
  "main": "index.js",
6
6
  "repository": {
@@ -12,6 +12,7 @@
12
12
  "bugs": {
13
13
  "url": "https://github.com/nxtedition/shared/issues"
14
14
  },
15
+ "type": "module",
15
16
  "homepage": "https://github.com/nxtedition/shared#readme",
16
17
  "lint-staged": {
17
18
  "*.{js,jsx,ts}": [