@nxtedition/shared 0.0.5 → 1.1.0

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 (2) hide show
  1. package/index.js +24 -6
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -32,7 +32,10 @@ async function* _reader({ sharedState, sharedBuffer }, cb) {
32
32
  const raw = buffer.slice(readPos + 4, readPos + len)
33
33
  readPos += len
34
34
  if (cb) {
35
- cb(raw)
35
+ const thenable = cb(raw)
36
+ if (thenable && typeof thenable.then === 'function') {
37
+ await thenable
38
+ }
36
39
  } else {
37
40
  yield raw
38
41
  }
@@ -64,9 +67,20 @@ export function writer({ sharedState, sharedBuffer }) {
64
67
  let flushing = null
65
68
 
66
69
  function tryWrite(...raw) {
70
+ if (Array.isArray(raw[0])) {
71
+ raw = raw[0]
72
+ }
73
+
74
+ if (!raw.length) {
75
+ return true
76
+ }
77
+
67
78
  readPos = Atomics.load(state, READ_INDEX)
68
79
 
69
- const len = raw.reduce((len, buf) => len + buf.byteLength, 4)
80
+ let len = 0
81
+ for (const buf of raw) {
82
+ len += buf.byteLength ?? Buffer.byteLength(buf)
83
+ }
70
84
 
71
85
  if (size - writePos < len + 4) {
72
86
  if (readPos < len + 4) {
@@ -87,8 +101,12 @@ export function writer({ sharedState, sharedBuffer }) {
87
101
  writePos += 4
88
102
 
89
103
  for (const buf of raw) {
90
- buffer.set(buf, writePos)
91
- writePos += buf.byteLength
104
+ if (typeof buf === 'string') {
105
+ writePos += buffer.write(buf, writePos)
106
+ } else {
107
+ buffer.set(buf, writePos)
108
+ writePos += buf.byteLength
109
+ }
92
110
  }
93
111
 
94
112
  Atomics.store(state, WRITE_INDEX, writePos)
@@ -111,14 +129,14 @@ export function writer({ sharedState, sharedBuffer }) {
111
129
  flushing = null
112
130
  }
113
131
 
114
- async function write(...raw) {
132
+ function write(...raw) {
115
133
  if (!queue.length && tryWrite(...raw)) {
116
134
  return
117
135
  }
118
136
 
119
137
  queue.push(Buffer.concat(raw))
120
138
 
121
- await (flushing ??= flush())
139
+ return (flushing ??= flush())
122
140
  }
123
141
 
124
142
  return write
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/shared",
3
- "version": "0.0.5",
3
+ "version": "1.1.0",
4
4
  "description": "Ring Buffer for NodeJS cross Worker communication",
5
5
  "main": "index.js",
6
6
  "repository": {