Merge pull request #10 from jwetzell/remove-node-buffer

Remove Node Buffer
This commit is contained in:
2024-10-13 15:15:46 -05:00
committed by GitHub
8 changed files with 181 additions and 74 deletions
+23 -6
View File
@@ -2,12 +2,12 @@ import { messageFromBuffer, messageToBuffer } from './message';
import { OSCBundle } from './models';
import { oscTypeConverterMap } from './osc-types';
export function bundleFromBuffer(bytes: Buffer): OSCBundle | undefined {
export function bundleFromBuffer(bytes: Uint8Array): OSCBundle | undefined {
if (bytes.length < 8) {
throw new Error('bundle has to be at least 20 bytes');
}
if (bytes.subarray(0, 7).toString('ascii') !== '#bundle') {
if (new TextDecoder().decode(bytes.subarray(0, 7)) !== '#bundle') {
throw new Error('bundle must start with #bundle');
}
@@ -59,7 +59,7 @@ export function bundleFromBuffer(bytes: Buffer): OSCBundle | undefined {
};
}
export function bundleToBuffer(bundle: OSCBundle): Buffer {
export function bundleToBuffer(bundle: OSCBundle): Uint8Array {
const headerBuffer = oscTypeConverterMap.s.toBuffer('#bundle');
if (headerBuffer === undefined) {
@@ -72,7 +72,9 @@ export function bundleToBuffer(bundle: OSCBundle): Buffer {
throw new Error('problem encoding buffer time tag');
}
const contentsBuffers: Buffer[] = [];
const contentsBuffers: Uint8Array[] = [];
let contentsBuffersTotalLength = 0;
bundle.contents.forEach((bundleContent) => {
if ('address' in bundleContent) {
@@ -80,10 +82,25 @@ export function bundleToBuffer(bundle: OSCBundle): Buffer {
const contentSizeBuffer = oscTypeConverterMap.i.toBuffer(contentBuffer.length);
if (contentBuffer && contentSizeBuffer) {
contentsBuffers.push(Buffer.concat([contentSizeBuffer, contentBuffer]));
const buffer = new Uint8Array(contentSizeBuffer.length + contentBuffer.length);
buffer.set(contentSizeBuffer, 0);
buffer.set(contentBuffer, contentSizeBuffer.length);
contentsBuffers.push(buffer);
contentsBuffersTotalLength += buffer.length;
}
}
});
return Buffer.concat([headerBuffer, timeTagBuffer, ...contentsBuffers]);
const buffer = new Uint8Array(headerBuffer.length + timeTagBuffer.length + contentsBuffersTotalLength);
let offset = 0;
buffer.set(headerBuffer, offset);
offset += headerBuffer.length;
buffer.set(timeTagBuffer, offset);
offset += timeTagBuffer.length;
contentsBuffers.forEach((contentBuffer) => {
buffer.set(contentBuffer, offset);
offset += contentBuffer.length;
});
return buffer;
}
+17 -5
View File
@@ -2,7 +2,8 @@ import { OSCMessage, OSCArg, OSCType } from './models';
import { oscTypeConverterMap } from './osc-types';
function argsToBuffer(args: OSCArg[]) {
const argBuffers: Buffer[] = [];
const argBuffers: Uint8Array[] = [];
let argBuffersTotalLength = 0;
for (let index = 0; index < args.length; index += 1) {
const arg = args[index];
@@ -14,12 +15,19 @@ function argsToBuffer(args: OSCArg[]) {
const buffer = typeConverter.toBuffer(arg.value);
if (buffer !== undefined) {
argBuffers.push(buffer);
argBuffersTotalLength += buffer.length;
}
}
return Buffer.concat(argBuffers);
const buffer = new Uint8Array(argBuffersTotalLength);
let offset = 0;
argBuffers.forEach((argBuffer) => {
buffer.set(argBuffer, offset);
offset += argBuffer.length;
});
return buffer;
}
export function messageToBuffer(message: OSCMessage): Buffer {
export function messageToBuffer(message: OSCMessage): Uint8Array {
const addressBuffer = oscTypeConverterMap.s.toBuffer(message.address);
if (addressBuffer === undefined) {
throw new Error('problem encoding address');
@@ -31,10 +39,14 @@ export function messageToBuffer(message: OSCMessage): Buffer {
throw new Error('problem encoding types');
}
const argsBuffer = argsToBuffer(message.args);
return Buffer.concat([addressBuffer, typesBuffer, argsBuffer]);
const buffer = new Uint8Array(addressBuffer.length + typesBuffer.length + argsBuffer.length);
buffer.set(addressBuffer, 0);
buffer.set(typesBuffer, addressBuffer.length);
buffer.set(argsBuffer, addressBuffer.length + typesBuffer.length);
return buffer;
}
export function messageFromBuffer(bytes: Buffer): OSCMessage | undefined {
export function messageFromBuffer(bytes: Uint8Array): OSCMessage | undefined {
if (bytes[0] !== 47) {
throw new Error('osc message must start with a /');
}
+3 -3
View File
@@ -1,7 +1,7 @@
export type OSCType = 's' | 'i' | 'f' | 'b' | 'T' | 'F' | 't';
export type OSCArg = {
type: OSCType;
value: string | number | Buffer | boolean | OSCTimeTag;
value: string | number | Uint8Array | boolean | OSCTimeTag;
};
export type OSCTimeTag = [number, number];
@@ -17,6 +17,6 @@ export type OSCMessage = {
};
export type OSCTypeConverter = {
toBuffer: (value: string | number | Buffer | boolean | OSCTimeTag) => Buffer | undefined;
fromBuffer: (buffer: Buffer) => [string | number | Buffer | boolean | OSCTimeTag | undefined, Buffer];
toBuffer: (value: string | number | Uint8Array | boolean | OSCTimeTag) => Uint8Array | undefined;
fromBuffer: (buffer: Uint8Array) => [string | number | Uint8Array | boolean | OSCTimeTag | undefined, Uint8Array];
};
+40 -20
View File
@@ -1,4 +1,4 @@
import { OSCType, OSCArg, OSCMessage, OSCTypeConverter, OSCBundle, OSCTimeTag } from './models';
import { OSCTypeConverter, OSCTimeTag } from './models';
export const oscTypeConverterMap: { [key: string]: OSCTypeConverter } = {
s: {
@@ -9,11 +9,11 @@ export const oscTypeConverterMap: { [key: string]: OSCTypeConverter } = {
if (padSize < 4) {
oscString = oscString.padEnd(oscString.length + padSize, '\u0000');
}
return Buffer.from(oscString, 'ascii');
return new TextEncoder().encode(oscString);
}
throw new TypeError('osc type s toBuffer called with non string value');
},
fromBuffer: (bytes: Buffer) => {
fromBuffer: (bytes) => {
let stringEnd = 0;
let stringPaddingEnd = 0;
for (let index = 0; index < bytes.length; index++) {
@@ -29,15 +29,15 @@ export const oscTypeConverterMap: { [key: string]: OSCTypeConverter } = {
}
}
return [bytes.toString('ascii', 0, stringEnd), bytes.subarray(stringPaddingEnd)];
return [new TextDecoder().decode(bytes.subarray(0, stringEnd)), bytes.subarray(stringPaddingEnd)];
},
},
f: {
toBuffer: (number) => {
if (typeof number === 'number') {
const buffer = Buffer.alloc(4);
buffer.writeFloatBE(number);
return buffer;
const view = new DataView(new ArrayBuffer(4));
view.setFloat32(0, number);
return new Uint8Array(view.buffer);
}
throw new TypeError('osc type f toBuffer called with non number value');
},
@@ -45,17 +45,22 @@ export const oscTypeConverterMap: { [key: string]: OSCTypeConverter } = {
if (buffer.length < 4) {
throw new Error('not enough bytes to read a osc float');
}
const view = new DataView(new ArrayBuffer(4));
const value = buffer.readFloatBE();
buffer.slice(0, 4).forEach((byte, index) => {
view.setUint8(index, byte);
});
const value = view.getFloat32(0);
return [value, buffer.subarray(4)];
},
},
i: {
toBuffer: (number) => {
if (typeof number === 'number') {
const buffer = Buffer.alloc(4);
buffer.writeInt32BE(number);
return buffer;
const view = new DataView(new ArrayBuffer(4));
view.setInt32(0, number);
return new Uint8Array(view.buffer);
}
throw new TypeError('osc type i toBuffer called with non number value');
},
@@ -64,21 +69,33 @@ export const oscTypeConverterMap: { [key: string]: OSCTypeConverter } = {
throw new Error('not enough bytes to read a osc integer');
}
const value = buffer.readInt32BE();
const view = new DataView(new ArrayBuffer(4));
buffer.slice(0, 4).forEach((byte, index) => {
view.setUint8(index, byte);
});
const value = view.getInt32(0);
return [value, buffer.subarray(4)];
},
},
b: {
toBuffer: (data) => {
if (Buffer.isBuffer(data)) {
if (data instanceof Uint8Array) {
const sizeBuffer = oscTypeConverterMap.i.toBuffer(data.length);
if (sizeBuffer) {
const padSize = 4 - (data.length % 4);
const padBuffer = padSize < 4 ? Buffer.from(Array(padSize).fill(0)) : Buffer.from([]);
return Buffer.concat([sizeBuffer, data, padBuffer]);
let padSize = 4 - (data.length % 4);
if (padSize === 4) {
padSize = 0;
}
const buffer = new Uint8Array(4 + data.length + padSize);
buffer.set(sizeBuffer);
buffer.set(data, 4);
return buffer;
}
}
throw new TypeError('osc type b toBuffer called with non Buffer value');
throw new TypeError('osc type b toBuffer called with non Uint8Array value');
},
fromBuffer: (buffer) => {
const [blobLength, blobBytes] = oscTypeConverterMap.i.fromBuffer(buffer);
@@ -97,7 +114,7 @@ export const oscTypeConverterMap: { [key: string]: OSCTypeConverter } = {
},
T: {
toBuffer: () => {
return Buffer.alloc(0);
return new Uint8Array(0);
},
fromBuffer: (buffer) => {
return [true, buffer];
@@ -105,7 +122,7 @@ export const oscTypeConverterMap: { [key: string]: OSCTypeConverter } = {
},
F: {
toBuffer: () => {
return Buffer.alloc(0);
return new Uint8Array(0);
},
fromBuffer: (buffer) => {
return [false, buffer];
@@ -135,7 +152,10 @@ export const oscTypeConverterMap: { [key: string]: OSCTypeConverter } = {
const fractionalBuffer = oscTypeConverterMap.i.toBuffer(fractional);
if (secondsBuffer && fractionalBuffer) {
return Buffer.concat([secondsBuffer, fractionalBuffer]);
const buffer = new Uint8Array(8);
buffer.set(secondsBuffer);
buffer.set(fractionalBuffer, 4);
return buffer;
}
},
fromBuffer: (buffer) => {
+11 -8
View File
@@ -5,24 +5,27 @@ const osc = require('../dist/index');
const tests = [
{
description: 'simple contents single message',
bytes: new Uint8Array([
...new TextEncoder().encode('#bundle'),
...new Uint8Array([0x00]),
...new Uint8Array([0, 0, 0, 32, 0, 0, 0, 0]),
...new Uint8Array([0x00, 0x00, 0x00, 0x20]),
...new Uint8Array([
0x2f, 0x6f, 0x73, 0x63, 0x69, 0x6c, 0x6c, 0x61, 0x74, 0x6f, 0x72, 0x2f, 0x34, 0x2f, 0x66, 0x72, 0x65, 0x71,
0x75, 0x65, 0x6e, 0x63, 0x79, 0x00, 0x2c, 0x66, 0x00, 0x00, 0x43, 0xdc, 0x00, 0x00,
]),
]),
expected: {
timeTag: [32, 0],
contents: [{ address: '/oscillator/4/frequency', args: [{ type: 'f', value: 440 }] }],
},
bundle: Buffer.concat([
Buffer.from('#bundle', 'ascii'),
Buffer.from([0x00]),
Buffer.from([0, 0, 0, 32, 0, 0, 0, 0]),
Buffer.from('00000020', 'hex'),
Buffer.from('2f6f7363696c6c61746f722f342f6672657175656e6379002c66000043dc0000', 'hex'),
]),
},
];
describe('OSC Bundle Decoding', () => {
tests.forEach((bundleTest) => {
it(bundleTest.description, () => {
const encoded = osc.bundleFromBuffer(bundleTest.bundle);
const encoded = osc.bundleFromBuffer(bundleTest.bytes);
deepEqual(encoded, bundleTest.expected);
});
});
+14 -8
View File
@@ -1,17 +1,23 @@
const { deepEqual, throws } = require('assert');
const { deepEqual } = require('assert');
const { describe, it } = require('node:test');
const osc = require('../dist/index');
const tests = [
{
description: 'simple contents single message',
bundle: { timeTag: [32, 0], contents: [{ address: '/oscillator/4/frequency', args: [{ type: 'f', value: 440 }] }] },
expected: Buffer.concat([
Buffer.from('#bundle', 'ascii'),
Buffer.from([0x00]),
Buffer.from([0, 0, 0, 32, 0, 0, 0, 0]),
Buffer.from('00000020', 'hex'),
Buffer.from('2f6f7363696c6c61746f722f342f6672657175656e6379002c66000043dc0000', 'hex'),
bundle: {
timeTag: [32, 0],
contents: [{ address: '/oscillator/4/frequency', args: [{ type: 'f', value: 440 }] }],
},
expected: new Uint8Array([
...new TextEncoder().encode('#bundle'),
...new Uint8Array([0x00]),
...new Uint8Array([0, 0, 0, 32, 0, 0, 0, 0]),
...new Uint8Array([0x00, 0x00, 0x00, 0x20]),
...new Uint8Array([
0x2f, 0x6f, 0x73, 0x63, 0x69, 0x6c, 0x6c, 0x61, 0x74, 0x6f, 0x72, 0x2f, 0x34, 0x2f, 0x66, 0x72, 0x65, 0x71,
0x75, 0x65, 0x6e, 0x63, 0x79, 0x00, 0x2c, 0x66, 0x00, 0x00, 0x43, 0xdc, 0x00, 0x00,
]),
]),
},
];
+39 -16
View File
@@ -5,47 +5,53 @@ const osc = require('../dist/index');
const tests = [
{
description: 'simple address no args',
bytes: Buffer.from('2f68656c6c6f00002c000000', 'hex'),
bytes: new Uint8Array([47, 104, 101, 108, 108, 111, 0, 0, 44, 0, 0, 0]),
expected: { address: '/hello', args: [] },
},
{
description: 'simple address string arg',
bytes: Buffer.from('2f68656c6c6f00002c7300006172673100000000', 'hex'),
bytes: new Uint8Array([47, 104, 101, 108, 108, 111, 0, 0, 44, 115, 0, 0, 97, 114, 103, 49, 0, 0, 0, 0]),
expected: { address: '/hello', args: [{ type: 's', value: 'arg1' }] },
},
{
description: 'simple address integer arg',
bytes: Buffer.from('2f68656c6c6f00002c69000000000023', 'hex'),
bytes: new Uint8Array([47, 104, 101, 108, 108, 111, 0, 0, 44, 105, 0, 0, 0, 0, 0, 35]),
expected: { address: '/hello', args: [{ type: 'i', value: 35 }] },
},
{
description: 'simple address float arg',
bytes: Buffer.from('2f68656c6c6f00002c660000420a0000', 'hex'),
bytes: new Uint8Array([47, 104, 101, 108, 108, 111, 0, 0, 44, 102, 0, 0, 66, 10, 0, 0]),
expected: { address: '/hello', args: [{ type: 'f', value: 34.5 }] },
},
{
description: 'simple address blob arg',
bytes: Buffer.from('2f68656c6c6f00002c62000000000004626c6f62', 'hex'),
expected: { address: '/hello', args: [{ type: 'b', value: Buffer.from('blob') }] },
bytes: new Uint8Array([47, 104, 101, 108, 108, 111, 0, 0, 44, 98, 0, 0, 0, 0, 0, 4, 98, 108, 111, 98]),
expected: { address: '/hello', args: [{ type: 'b', value: new TextEncoder().encode('blob') }] },
},
{
description: 'simple address True arg',
bytes: Buffer.from('2f68656c6c6f00002c540000', 'hex'),
bytes: new Uint8Array([47, 104, 101, 108, 108, 111, 0, 0, 44, 84, 0, 0]),
expected: { address: '/hello', args: [{ type: 'T', value: true }] },
},
{
description: 'simple address False arg',
bytes: Buffer.from('2f68656c6c6f00002c460000', 'hex'),
bytes: new Uint8Array([47, 104, 101, 108, 108, 111, 0, 0, 44, 70, 0, 0]),
expected: { address: '/hello', args: [{ type: 'F', value: false }] },
},
{
description: 'osc 1.0 spec example 1',
bytes: Buffer.from('2f6f7363696c6c61746f722f342f6672657175656e6379002c66000043dc0000', 'hex'),
bytes: new Uint8Array([
47, 111, 115, 99, 105, 108, 108, 97, 116, 111, 114, 47, 52, 47, 102, 114, 101, 113, 117, 101, 110, 99, 121, 0, 44,
102, 0, 0, 67, 220, 0, 0,
]),
expected: { address: '/oscillator/4/frequency', args: [{ type: 'f', value: 440 }] },
},
{
description: 'osc 1.0 spec example 2',
bytes: Buffer.from('2f666f6f000000002c69697366660000000003e8ffffffff68656c6c6f0000003f9df3b640b5b22d', 'hex'),
bytes: new Uint8Array([
47, 102, 111, 111, 0, 0, 0, 0, 44, 105, 105, 115, 102, 102, 0, 0, 0, 0, 3, 232, 255, 255, 255, 255, 104, 101, 108,
108, 111, 0, 0, 0, 63, 157, 243, 182, 64, 181, 178, 45,
]),
expected: {
address: '/foo',
args: [
@@ -71,7 +77,9 @@ describe('OSC Message Decoding', () => {
it('bad address', () => {
throws(
() => {
osc.messageFromBuffer(Buffer.from('68656c6c6f00002c660000420a0000', 'hex'));
osc.messageFromBuffer(
new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00, 0x00, 0x2c, 0x66, 0x00, 0x00, 0x42, 0x0a, 0x00, 0x00])
);
},
{ name: /^Error$/, message: /must start with/ }
);
@@ -80,7 +88,9 @@ describe('OSC Message Decoding', () => {
it('bad type string', () => {
throws(
() => {
osc.messageFromBuffer(Buffer.from('2f68656c6c6f000066000000420a00', 'hex'));
osc.messageFromBuffer(
new Uint8Array([0x2f, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00, 0x00, 0x66, 0x00, 0x00, 0x00, 0x42, 0x0a, 0x00])
);
},
{ name: /^Error$/, message: /type string must start with/ }
);
@@ -89,7 +99,11 @@ describe('OSC Message Decoding', () => {
it('unknown type', () => {
throws(
() => {
osc.messageFromBuffer(Buffer.from('2f68656c6c6f00002c7a0000420a0000', 'hex'));
osc.messageFromBuffer(
new Uint8Array([
0x2f, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00, 0x00, 0x2c, 0x7a, 0x00, 0x00, 0x42, 0x0a, 0x00, 0x00,
])
);
},
{ name: /^Error$/, message: /unknown/ }
);
@@ -98,7 +112,9 @@ describe('OSC Message Decoding', () => {
it('float arg missing bytes', () => {
throws(
() => {
osc.messageFromBuffer(Buffer.from('2f68656c6c6f00002c660000420a00', 'hex'));
osc.messageFromBuffer(
new Uint8Array([0x2f, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00, 0x00, 0x2c, 0x66, 0x00, 0x00, 0x42, 0x0a, 0x00])
);
},
{ name: /^Error$/, message: /not enough bytes/ }
);
@@ -107,7 +123,9 @@ describe('OSC Message Decoding', () => {
it('int arg missing bytes', () => {
throws(
() => {
osc.messageFromBuffer(Buffer.from('2f68656c6c6f00002c690000000000', 'hex'));
osc.messageFromBuffer(
new Uint8Array([0x2f, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00, 0x00, 0x2c, 0x69, 0x00, 0x00, 0x00, 0x00, 0x00])
);
},
{ name: /^Error$/, message: /not enough bytes/ }
);
@@ -116,7 +134,12 @@ describe('OSC Message Decoding', () => {
it('blob bytes too small', () => {
throws(
() => {
osc.messageFromBuffer(Buffer.from('2f68656c6c6f00002c62000000000004626c6f', 'hex'));
osc.messageFromBuffer(
new Uint8Array([
0x2f, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00, 0x00, 0x2c, 0x62, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x62, 0x6c,
0x6f,
])
);
},
{ name: /^Error$/, message: /not enough bytes/ }
);
+34 -8
View File
@@ -6,37 +6,63 @@ const tests = [
{
description: 'simple address no args',
message: { address: '/hello', args: [] },
expected: Buffer.from('2f68656c6c6f00002c000000', 'hex'),
expected: new Uint8Array([47, 104, 101, 108, 108, 111, 0, 0, 44, 0, 0, 0]),
},
{
description: 'simple address string arg',
message: { address: '/hello', args: [{ type: 's', value: 'arg1' }] },
expected: Buffer.from('2f68656c6c6f00002c7300006172673100000000', 'hex'),
expected: new Uint8Array([47, 104, 101, 108, 108, 111, 0, 0, 44, 115, 0, 0, 97, 114, 103, 49, 0, 0, 0, 0]),
},
{
description: 'simple address integer arg',
message: { address: '/hello', args: [{ type: 'i', value: 35 }] },
expected: Buffer.from('2f68656c6c6f00002c69000000000023', 'hex'),
expected: new Uint8Array([47, 104, 101, 108, 108, 111, 0, 0, 44, 105, 0, 0, 0, 0, 0, 35]),
},
{
description: 'simple address float arg',
message: { address: '/hello', args: [{ type: 'f', value: 34.5 }] },
expected: Buffer.from('2f68656c6c6f00002c660000420a0000', 'hex'),
expected: new Uint8Array([47, 104, 101, 108, 108, 111, 0, 0, 44, 102, 0, 0, 66, 10, 0, 0]),
},
{
description: 'simple address blob arg',
message: { address: '/hello', args: [{ type: 'b', value: Buffer.from('blob') }] },
expected: Buffer.from('2f68656c6c6f00002c62000000000004626c6f62', 'hex'),
message: { address: '/hello', args: [{ type: 'b', value: new TextEncoder().encode('blob') }] },
expected: new Uint8Array([47, 104, 101, 108, 108, 111, 0, 0, 44, 98, 0, 0, 0, 0, 0, 4, 98, 108, 111, 98]),
},
{
description: 'simple address True arg',
message: { address: '/hello', args: [{ type: 'T', value: true }] },
expected: Buffer.from('2f68656c6c6f00002c540000', 'hex'),
expected: new Uint8Array([47, 104, 101, 108, 108, 111, 0, 0, 44, 84, 0, 0]),
},
{
description: 'simple address False arg',
message: { address: '/hello', args: [{ type: 'F', value: false }] },
expected: Buffer.from('2f68656c6c6f00002c460000', 'hex'),
expected: new Uint8Array([47, 104, 101, 108, 108, 111, 0, 0, 44, 70, 0, 0]),
},
{
description: 'osc 1.0 spec example 1',
message: { address: '/oscillator/4/frequency', args: [{ type: 'f', value: 440 }] },
expected: new Uint8Array([
47, 111, 115, 99, 105, 108, 108, 97, 116, 111, 114, 47, 52, 47, 102, 114, 101, 113, 117, 101, 110, 99, 121, 0, 44,
102, 0, 0, 67, 220, 0, 0,
]),
},
{
description: 'osc 1.0 spec example 2',
message: {
address: '/foo',
args: [
{ type: 'i', value: 1000 },
{ type: 'i', value: -1 },
{ type: 's', value: 'hello' },
// thanks IEEE 754
{ type: 'f', value: 1.2339999675750732421875 },
{ type: 'f', value: 5.677999973297119140625 },
],
},
expected: new Uint8Array([
47, 102, 111, 111, 0, 0, 0, 0, 44, 105, 105, 115, 102, 102, 0, 0, 0, 0, 3, 232, 255, 255, 255, 255, 104, 101, 108,
108, 111, 0, 0, 0, 63, 157, 243, 182, 64, 181, 178, 45,
]),
},
];