Something went wrong. Try again.
Offload functions to worker threads with shared memory primitives for Node.js.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960import { describe, it } from 'node:test';import assert from 'node:assert/strict';import { shared, bytes, bool } from 'moroutine';
describe('bytes', () => { it('bytes(n) creates standalone byte buffer', () => { const buf = bytes(16); const data = buf.load(); assert.equal(data.length, 16); assert.equal(data[0], 0); });
it('shared(bytes(n)) creates byte buffer', () => { const buf = shared(bytes(16)); assert.equal(buf.load().length, 16); });
it('load returns view (not copy)', () => { const buf = bytes(4); buf.view[0] = 42; assert.equal(buf.load()[0], 42); });
it('store writes exact length data', () => { const buf = bytes(4); buf.store(new Uint8Array([1, 2, 3, 4])); assert.deepEqual([...buf.load()], [1, 2, 3, 4]); });
it('store throws if length does not match', () => { const buf = bytes(4); assert.throws(() => buf.store(new Uint8Array([1, 2, 3])), /length/i); assert.throws(() => buf.store(new Uint8Array([1, 2, 3, 4, 5])), /length/i); });
it('view provides mutable direct access', () => { const buf = bytes(4); buf.view[0] = 0xff; buf.view[3] = 0xaa; assert.equal(buf.load()[0], 0xff); assert.equal(buf.load()[3], 0xaa); });
it('bytes in a struct schema', () => { const s = shared({ data: bytes(8), flag: bool }); s.fields.data.store(new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8])); s.fields.flag.store(true); assert.deepEqual([...s.fields.data.load()], [1, 2, 3, 4, 5, 6, 7, 8]); assert.equal(s.fields.flag.load(), true); });
it('bytes in struct shares one buffer', () => { const s = shared({ data: bytes(8), flag: bool }); const SHARED = Symbol.for('moroutine.shared'); const dataSer = (s.fields.data as any)[SHARED](); const flagSer = (s.fields.flag as any)[SHARED](); assert.equal(dataSer.buffer, flagSer.buffer); });});