@nxtedition/lib 26.5.0 → 26.6.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/package.json +1 -1
  2. package/shared.js +24 -2
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/lib",
3
- "version": "26.5.0",
3
+ "version": "26.6.0",
4
4
  "license": "MIT",
5
5
  "author": "Robert Nagy <robert.nagy@boffins.se>",
6
6
  "type": "module",
package/shared.js CHANGED
@@ -296,7 +296,7 @@ export function writer({ sharedState, sharedBuffer }, { yield: onYield, logger }
296
296
  * @param {number} len The maximum expected length of the payload.
297
297
  * @param {({ buffer, view, offset, length }) => number} fn The callback that writes the data.
298
298
  */
299
- function write(len, fn) {
299
+ function writeSync(len, fn) {
300
300
  if (len < 0) {
301
301
  throw new Error(`Length ${len} is negative`)
302
302
  }
@@ -325,6 +325,28 @@ export function writer({ sharedState, sharedBuffer }, { yield: onYield, logger }
325
325
  }
326
326
  }
327
327
 
328
+ function tryWrite(len, fn) {
329
+ if (len < 0) {
330
+ throw new Error(`Length ${len} is negative`)
331
+ }
332
+ if (len >= 2 ** 31 || len > size - 8) {
333
+ throw new Error(`Length ${len} exceeds maximum allowed size`)
334
+ }
335
+
336
+ if (!_acquire(len)) {
337
+ readPos = Atomics.load(state, READ_INDEX) | 0
338
+ if (!_acquire(len)) {
339
+ return false
340
+ }
341
+ }
342
+
343
+ _write(len, fn)
344
+
345
+ if (writePos === readPos) {
346
+ throw new Error(`Write position ${writePos} cannot equal read position ${readPos}`)
347
+ }
348
+ }
349
+
328
350
  function _flush() {
329
351
  if (pending > 0) {
330
352
  Atomics.store(state, WRITE_INDEX, writePos)
@@ -341,5 +363,5 @@ export function writer({ sharedState, sharedBuffer }, { yield: onYield, logger }
341
363
  }
342
364
  }
343
365
 
344
- return { write, cork }
366
+ return { tryWrite, writeSync, cork }
345
367
  }