@nxtedition/shared 1.0.0 → 1.1.1

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/index.js +22 -4
  2. package/package-lock.json +1513 -0
  3. 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 = 4
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)