mirror of
https://github.com/jwetzell/osc-js.git
synced 2026-08-20 14:29:00 +00:00
bring in sendosc and convert to npm workspaces
This commit is contained in:
@@ -0,0 +1 @@
|
||||
dist
|
||||
@@ -0,0 +1,29 @@
|
||||
{
|
||||
"name": "@jwetzell/osc",
|
||||
"version": "0.2.2",
|
||||
"description": "",
|
||||
"main": "dist/index.js",
|
||||
"types": "dist/index.d.ts",
|
||||
"scripts": {
|
||||
"prebuild": "rimraf dist",
|
||||
"build": "tsc",
|
||||
"prepublishOnly": "npm run build",
|
||||
"pretest": "npm run build",
|
||||
"test": "node --test --experimental-test-coverage"
|
||||
},
|
||||
"files": [
|
||||
"dist"
|
||||
],
|
||||
"author": {
|
||||
"name": "Joel Wetzell",
|
||||
"email": "me@jwetzell.com",
|
||||
"url": "https://jwetzell.com"
|
||||
},
|
||||
"repository": "https://github.com/jwetzell/osc-js",
|
||||
"license": "MIT",
|
||||
"devDependencies": {
|
||||
"@types/node": "22.7.4",
|
||||
"rimraf": "6.0.1",
|
||||
"typescript": "5.6.2"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
import { messageFromBuffer, messageToBuffer } from "./message";
|
||||
import { OSCBundle } from "./models";
|
||||
import { oscTypeConverterMap } from "./osc-types";
|
||||
|
||||
export function bundleFromBuffer(bytes: Buffer): 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') {
|
||||
throw new Error('bundle must start with #bundle');
|
||||
}
|
||||
|
||||
const [bundleHeader, bytesAfterHeader] = oscTypeConverterMap.s.fromBuffer(bytes);
|
||||
let [timeTag, bytesAfterTimeTag] = oscTypeConverterMap.t.fromBuffer(bytesAfterHeader);
|
||||
|
||||
if (!Array.isArray(timeTag)) {
|
||||
throw new Error('problem getting bundle time tag');
|
||||
}
|
||||
|
||||
const bundleContents = [];
|
||||
|
||||
let endOfBundle = false;
|
||||
|
||||
let [contentSize, remainingBytes] = oscTypeConverterMap.i.fromBuffer(bytesAfterTimeTag);
|
||||
if (typeof contentSize === 'number') {
|
||||
while (!endOfBundle) {
|
||||
if (remainingBytes.length < contentSize) {
|
||||
throw new Error('bundle does not contain enough data');
|
||||
}
|
||||
|
||||
const bundleContentBytes = remainingBytes.subarray(0, contentSize);
|
||||
|
||||
if (bundleContentBytes[0] === 35) {
|
||||
// # character indicating contents is a bundle
|
||||
const content = bundleFromBuffer(bundleContentBytes);
|
||||
if (content) {
|
||||
bundleContents.push(content);
|
||||
}
|
||||
} else if (bundleContentBytes[0] === 47) {
|
||||
const content = messageFromBuffer(bundleContentBytes);
|
||||
if (content) {
|
||||
bundleContents.push(content);
|
||||
}
|
||||
} else {
|
||||
throw new Error('bundle contents does not look like a OSC message or bundle');
|
||||
}
|
||||
|
||||
remainingBytes = remainingBytes.subarray(contentSize);
|
||||
if (remainingBytes.length === 0) {
|
||||
endOfBundle = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
timeTag,
|
||||
contents: bundleContents,
|
||||
};
|
||||
}
|
||||
|
||||
export function bundleToBuffer(bundle: OSCBundle): Buffer {
|
||||
const headerBuffer = oscTypeConverterMap.s.toBuffer('#bundle');
|
||||
|
||||
if (headerBuffer === undefined) {
|
||||
throw new Error('problem encoding buffer header');
|
||||
}
|
||||
|
||||
const timeTagBuffer = oscTypeConverterMap.t.toBuffer(bundle.timeTag);
|
||||
|
||||
if (timeTagBuffer === undefined) {
|
||||
throw new Error('problem encoding buffer time tag');
|
||||
}
|
||||
|
||||
const contentsBuffers: Buffer[] = [];
|
||||
|
||||
bundle.contents.forEach((bundleContent) => {
|
||||
if ('address' in bundleContent) {
|
||||
const contentBuffer = messageToBuffer(bundleContent);
|
||||
const contentSizeBuffer = oscTypeConverterMap.i.toBuffer(contentBuffer.length);
|
||||
|
||||
if (contentBuffer && contentSizeBuffer) {
|
||||
contentsBuffers.push(Buffer.concat([contentSizeBuffer, contentBuffer]));
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return Buffer.concat([headerBuffer, timeTagBuffer, ...contentsBuffers]);
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
export * from './models';
|
||||
export * from './message';
|
||||
export * from './bundle';
|
||||
@@ -0,0 +1,77 @@
|
||||
import { OSCMessage, OSCArg, OSCType } from "./models";
|
||||
import { oscTypeConverterMap } from "./osc-types";
|
||||
|
||||
function argsToBuffer(args: OSCArg[]) {
|
||||
const argBuffers: Buffer[] = [];
|
||||
|
||||
for (let index = 0; index < args.length; index += 1) {
|
||||
const arg = args[index];
|
||||
const typeConverter = oscTypeConverterMap[arg.type];
|
||||
if (typeConverter === undefined) {
|
||||
throw new TypeError('unknown type '.concat(arg.type));
|
||||
}
|
||||
|
||||
const buffer = typeConverter.toBuffer(arg.value);
|
||||
if (buffer !== undefined) {
|
||||
argBuffers.push(buffer);
|
||||
}
|
||||
}
|
||||
return Buffer.concat(argBuffers);
|
||||
}
|
||||
|
||||
|
||||
export function messageToBuffer(message: OSCMessage): Buffer {
|
||||
const addressBuffer = oscTypeConverterMap.s.toBuffer(message.address);
|
||||
if (addressBuffer === undefined) {
|
||||
throw new Error('problem encoding address');
|
||||
}
|
||||
|
||||
const typeString = message.args.map((arg) => arg.type).join('');
|
||||
const typesBuffer = oscTypeConverterMap.s.toBuffer(`,${typeString}`);
|
||||
if (typesBuffer === undefined) {
|
||||
throw new Error('problem encoding types');
|
||||
}
|
||||
const argsBuffer = argsToBuffer(message.args);
|
||||
return Buffer.concat([addressBuffer, typesBuffer, argsBuffer]);
|
||||
}
|
||||
|
||||
export function messageFromBuffer(bytes: Buffer): OSCMessage | undefined {
|
||||
if (bytes[0] !== 47) {
|
||||
throw new Error('osc message must start with a /');
|
||||
}
|
||||
|
||||
const oscArgs: OSCArg[] = [];
|
||||
|
||||
const [address, bytesAfterAddress] = oscTypeConverterMap.s.fromBuffer(bytes);
|
||||
if (typeof address === 'string') {
|
||||
let [typeString, bytesAfterType] = oscTypeConverterMap.s.fromBuffer(bytesAfterAddress);
|
||||
if (typeof typeString === 'string') {
|
||||
if (!typeString.startsWith(',')) {
|
||||
throw new Error('osc type string must start with a ,');
|
||||
}
|
||||
let argsBuffer = bytesAfterType;
|
||||
for (let index = 1; index < typeString.length; index++) {
|
||||
const argType = typeString.charAt(index) as OSCType;
|
||||
|
||||
const oscTypeConverter = oscTypeConverterMap[argType];
|
||||
if (oscTypeConverter === undefined) {
|
||||
throw new Error('unknown OSC type');
|
||||
}
|
||||
const [value, remainingBytes] = oscTypeConverter.fromBuffer(argsBuffer);
|
||||
if (value !== undefined) {
|
||||
const arg: OSCArg = {
|
||||
type: argType,
|
||||
value: value,
|
||||
};
|
||||
oscArgs.push(arg);
|
||||
}
|
||||
argsBuffer = remainingBytes;
|
||||
}
|
||||
|
||||
return {
|
||||
address,
|
||||
args: oscArgs,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
export type OSCType = 's' | 'i' | 'f' | 'b' | 'T' | 'F' | 't';
|
||||
export type OSCArg = {
|
||||
type: OSCType;
|
||||
value: string | number | Buffer | boolean | OSCTimeTag;
|
||||
};
|
||||
|
||||
export type OSCTimeTag = [number, number];
|
||||
|
||||
export type OSCBundle = {
|
||||
timeTag: OSCTimeTag;
|
||||
contents: (OSCBundle | OSCMessage)[];
|
||||
};
|
||||
|
||||
export type OSCMessage = {
|
||||
address: string;
|
||||
args: OSCArg[];
|
||||
};
|
||||
|
||||
export type OSCTypeConverter = {
|
||||
toBuffer: (value: string | number | Buffer | boolean | OSCTimeTag) => Buffer | undefined;
|
||||
fromBuffer: (buffer: Buffer) => [string | number | Buffer | boolean | OSCTimeTag | undefined, Buffer];
|
||||
};
|
||||
@@ -0,0 +1,154 @@
|
||||
import { OSCType, OSCArg, OSCMessage, OSCTypeConverter, OSCBundle, OSCTimeTag } from './models';
|
||||
|
||||
export const oscTypeConverterMap: { [key: string]: OSCTypeConverter } = {
|
||||
s: {
|
||||
toBuffer: (string) => {
|
||||
if (typeof string === 'string') {
|
||||
let oscString = `${string}\u0000`;
|
||||
const padSize = 4 - (oscString.length % 4);
|
||||
if (padSize < 4) {
|
||||
oscString = oscString.padEnd(oscString.length + padSize, '\u0000');
|
||||
}
|
||||
return Buffer.from(oscString, 'ascii');
|
||||
}
|
||||
throw new TypeError('osc type s toBuffer called with non string value');
|
||||
},
|
||||
fromBuffer: (bytes: Buffer) => {
|
||||
let stringEnd = 0;
|
||||
let stringPaddingEnd = 0;
|
||||
for (let index = 0; index < bytes.length; index++) {
|
||||
if (bytes[index] === 0) {
|
||||
stringEnd = index;
|
||||
stringPaddingEnd = index + 1;
|
||||
const stringPadding = 4 - ((stringEnd + 1) % 4);
|
||||
|
||||
if (stringPadding < 4) {
|
||||
stringPaddingEnd += stringPadding;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return [bytes.toString('ascii', 0, stringEnd), bytes.subarray(stringPaddingEnd)];
|
||||
},
|
||||
},
|
||||
f: {
|
||||
toBuffer: (number) => {
|
||||
if (typeof number === 'number') {
|
||||
const buffer = Buffer.alloc(4);
|
||||
buffer.writeFloatBE(number);
|
||||
return buffer;
|
||||
}
|
||||
throw new TypeError('osc type f toBuffer called with non number value');
|
||||
},
|
||||
fromBuffer: (buffer) => {
|
||||
if (buffer.length < 4) {
|
||||
throw new Error('not enough bytes to read a osc float');
|
||||
}
|
||||
|
||||
const value = buffer.readFloatBE();
|
||||
return [value, buffer.subarray(4)];
|
||||
},
|
||||
},
|
||||
i: {
|
||||
toBuffer: (number) => {
|
||||
if (typeof number === 'number') {
|
||||
const buffer = Buffer.alloc(4);
|
||||
buffer.writeInt32BE(number);
|
||||
return buffer;
|
||||
}
|
||||
throw new TypeError('osc type i toBuffer called with non number value');
|
||||
},
|
||||
fromBuffer: (buffer) => {
|
||||
if (buffer.length < 4) {
|
||||
throw new Error('not enough bytes to read a osc integer');
|
||||
}
|
||||
|
||||
const value = buffer.readInt32BE();
|
||||
return [value, buffer.subarray(4)];
|
||||
},
|
||||
},
|
||||
b: {
|
||||
toBuffer: (data) => {
|
||||
if (Buffer.isBuffer(data)) {
|
||||
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]);
|
||||
}
|
||||
}
|
||||
throw new TypeError('osc type b toBuffer called with non Buffer value');
|
||||
},
|
||||
fromBuffer: (buffer) => {
|
||||
const [blobLength, blobBytes] = oscTypeConverterMap.i.fromBuffer(buffer);
|
||||
if (typeof blobLength === 'number') {
|
||||
if (blobBytes.length < blobLength) {
|
||||
throw new Error('not enough bytes left for blob length specified');
|
||||
}
|
||||
const value = blobBytes.subarray(0, blobLength);
|
||||
const blobPadding = 4 - (blobLength % 4);
|
||||
const blobEnd = blobPadding < 4 ? blobLength + blobPadding : blobLength;
|
||||
return [value, blobBytes.subarray(blobEnd)];
|
||||
} else {
|
||||
throw new Error('unexpected value for blob length');
|
||||
}
|
||||
},
|
||||
},
|
||||
T: {
|
||||
toBuffer: () => {
|
||||
return Buffer.alloc(0);
|
||||
},
|
||||
fromBuffer: (buffer) => {
|
||||
return [true, buffer];
|
||||
},
|
||||
},
|
||||
F: {
|
||||
toBuffer: () => {
|
||||
return Buffer.alloc(0);
|
||||
},
|
||||
fromBuffer: (buffer) => {
|
||||
return [false, buffer];
|
||||
},
|
||||
},
|
||||
t: {
|
||||
toBuffer: (timetag) => {
|
||||
if (!Array.isArray(timetag)) {
|
||||
throw new TypeError('osc type t toBuffer called with non array value');
|
||||
}
|
||||
|
||||
if (timetag.length != 2) {
|
||||
throw new TypeError('osc type t array should have exactly 2 elements');
|
||||
}
|
||||
|
||||
const seconds = timetag[0];
|
||||
if (typeof seconds !== 'number') {
|
||||
throw new TypeError('osc type t seconds part should be a number');
|
||||
}
|
||||
|
||||
const fractional = timetag[1];
|
||||
if (typeof fractional !== 'number') {
|
||||
throw new TypeError('osc type t fractional part should be a number');
|
||||
}
|
||||
|
||||
const secondsBuffer = oscTypeConverterMap.i.toBuffer(seconds);
|
||||
const fractionalBuffer = oscTypeConverterMap.i.toBuffer(fractional);
|
||||
|
||||
if (secondsBuffer && fractionalBuffer) {
|
||||
return Buffer.concat([secondsBuffer, fractionalBuffer]);
|
||||
}
|
||||
},
|
||||
fromBuffer: (buffer) => {
|
||||
if (buffer.length < 8) {
|
||||
throw new Error('osc time tag must be greater than 8 bytes');
|
||||
}
|
||||
const [seconds, bytesAfterSeconds] = oscTypeConverterMap.i.fromBuffer(buffer);
|
||||
const [fractional, bytesAfterFractional] = oscTypeConverterMap.i.fromBuffer(bytesAfterSeconds);
|
||||
if (typeof seconds === 'number' && typeof fractional === 'number') {
|
||||
const timeTag: OSCTimeTag = [seconds, fractional];
|
||||
return [timeTag, bytesAfterFractional];
|
||||
}
|
||||
return [undefined, bytesAfterFractional];
|
||||
},
|
||||
},
|
||||
};
|
||||
@@ -0,0 +1,29 @@
|
||||
const { deepEqual } = require('assert');
|
||||
const { describe, it } = require('node:test');
|
||||
const osc = require('../dist/index');
|
||||
|
||||
const tests = [
|
||||
{
|
||||
description: 'simple contents single message',
|
||||
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);
|
||||
deepEqual(encoded, bundleTest.expected);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,26 @@
|
||||
const { deepEqual, throws } = 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'),
|
||||
]),
|
||||
},
|
||||
];
|
||||
|
||||
describe('OSC Bundle Encoding', () => {
|
||||
tests.forEach((bundleTest) => {
|
||||
it(bundleTest.description, () => {
|
||||
const encoded = osc.bundleToBuffer(bundleTest.bundle);
|
||||
deepEqual(encoded, bundleTest.expected);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,124 @@
|
||||
const { deepEqual, throws } = require('assert');
|
||||
const { describe, it } = require('node:test');
|
||||
const osc = require('../dist/index');
|
||||
|
||||
const tests = [
|
||||
{
|
||||
description: 'simple address no args',
|
||||
bytes: Buffer.from('2f68656c6c6f00002c000000', 'hex'),
|
||||
expected: { address: '/hello', args: [] },
|
||||
},
|
||||
{
|
||||
description: 'simple address string arg',
|
||||
bytes: Buffer.from('2f68656c6c6f00002c7300006172673100000000', 'hex'),
|
||||
expected: { address: '/hello', args: [{ type: 's', value: 'arg1' }] },
|
||||
},
|
||||
{
|
||||
description: 'simple address integer arg',
|
||||
bytes: Buffer.from('2f68656c6c6f00002c69000000000023', 'hex'),
|
||||
expected: { address: '/hello', args: [{ type: 'i', value: 35 }] },
|
||||
},
|
||||
{
|
||||
description: 'simple address float arg',
|
||||
bytes: Buffer.from('2f68656c6c6f00002c660000420a0000', 'hex'),
|
||||
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') }] },
|
||||
},
|
||||
{
|
||||
description: 'simple address True arg',
|
||||
bytes: Buffer.from('2f68656c6c6f00002c540000', 'hex'),
|
||||
expected: { address: '/hello', args: [{ type: 'T', value: true }] },
|
||||
},
|
||||
{
|
||||
description: 'simple address False arg',
|
||||
bytes: Buffer.from('2f68656c6c6f00002c460000', 'hex'),
|
||||
expected: { address: '/hello', args: [{ type: 'F', value: false }] },
|
||||
},
|
||||
{
|
||||
description: 'osc 1.0 spec example 1',
|
||||
bytes: Buffer.from('2f6f7363696c6c61746f722f342f6672657175656e6379002c66000043dc0000', 'hex'),
|
||||
expected: { address: '/oscillator/4/frequency', args: [{ type: 'f', value: 440 }] },
|
||||
},
|
||||
{
|
||||
description: 'osc 1.0 spec example 2',
|
||||
bytes: Buffer.from('2f666f6f000000002c69697366660000000003e8ffffffff68656c6c6f0000003f9df3b640b5b22d', 'hex'),
|
||||
expected: {
|
||||
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 },
|
||||
],
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
describe('OSC Message Decoding', () => {
|
||||
tests.forEach((messageTest) => {
|
||||
it(messageTest.description, () => {
|
||||
const decoded = osc.messageFromBuffer(messageTest.bytes);
|
||||
deepEqual(decoded, messageTest.expected);
|
||||
});
|
||||
});
|
||||
|
||||
it('bad address', () => {
|
||||
throws(
|
||||
() => {
|
||||
osc.messageFromBuffer(Buffer.from('68656c6c6f00002c660000420a0000', 'hex'));
|
||||
},
|
||||
{ name: /^Error$/, message: /must start with/ }
|
||||
);
|
||||
});
|
||||
|
||||
it('bad type string', () => {
|
||||
throws(
|
||||
() => {
|
||||
osc.messageFromBuffer(Buffer.from('2f68656c6c6f000066000000420a00', 'hex'));
|
||||
},
|
||||
{ name: /^Error$/, message: /type string must start with/ }
|
||||
);
|
||||
});
|
||||
|
||||
it('unknown type', () => {
|
||||
throws(
|
||||
() => {
|
||||
osc.messageFromBuffer(Buffer.from('2f68656c6c6f00002c7a0000420a0000', 'hex'));
|
||||
},
|
||||
{ name: /^Error$/, message: /unknown/ }
|
||||
);
|
||||
});
|
||||
|
||||
it('float arg missing bytes', () => {
|
||||
throws(
|
||||
() => {
|
||||
osc.messageFromBuffer(Buffer.from('2f68656c6c6f00002c660000420a00', 'hex'));
|
||||
},
|
||||
{ name: /^Error$/, message: /not enough bytes/ }
|
||||
);
|
||||
});
|
||||
|
||||
it('int arg missing bytes', () => {
|
||||
throws(
|
||||
() => {
|
||||
osc.messageFromBuffer(Buffer.from('2f68656c6c6f00002c690000000000', 'hex'));
|
||||
},
|
||||
{ name: /^Error$/, message: /not enough bytes/ }
|
||||
);
|
||||
});
|
||||
|
||||
it('blob bytes too small', () => {
|
||||
throws(
|
||||
() => {
|
||||
osc.messageFromBuffer(Buffer.from('2f68656c6c6f00002c62000000000004626c6f', 'hex'));
|
||||
},
|
||||
{ name: /^Error$/, message: /not enough bytes/ }
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,94 @@
|
||||
const { deepEqual, throws } = require('assert');
|
||||
const { describe, it } = require('node:test');
|
||||
const osc = require('../dist/index');
|
||||
|
||||
const tests = [
|
||||
{
|
||||
description: 'simple address no args',
|
||||
message: { address: '/hello', args: [] },
|
||||
expected: Buffer.from('2f68656c6c6f00002c000000', 'hex'),
|
||||
},
|
||||
{
|
||||
description: 'simple address string arg',
|
||||
message: { address: '/hello', args: [{ type: 's', value: 'arg1' }] },
|
||||
expected: Buffer.from('2f68656c6c6f00002c7300006172673100000000', 'hex'),
|
||||
},
|
||||
{
|
||||
description: 'simple address integer arg',
|
||||
message: { address: '/hello', args: [{ type: 'i', value: 35 }] },
|
||||
expected: Buffer.from('2f68656c6c6f00002c69000000000023', 'hex'),
|
||||
},
|
||||
{
|
||||
description: 'simple address float arg',
|
||||
message: { address: '/hello', args: [{ type: 'f', value: 34.5 }] },
|
||||
expected: Buffer.from('2f68656c6c6f00002c660000420a0000', 'hex'),
|
||||
},
|
||||
{
|
||||
description: 'simple address blob arg',
|
||||
message: { address: '/hello', args: [{ type: 'b', value: Buffer.from('blob') }] },
|
||||
expected: Buffer.from('2f68656c6c6f00002c62000000000004626c6f62', 'hex'),
|
||||
},
|
||||
{
|
||||
description: 'simple address True arg',
|
||||
message: { address: '/hello', args: [{ type: 'T', value: true }] },
|
||||
expected: Buffer.from('2f68656c6c6f00002c540000', 'hex'),
|
||||
},
|
||||
{
|
||||
description: 'simple address False arg',
|
||||
message: { address: '/hello', args: [{ type: 'F', value: false }] },
|
||||
expected: Buffer.from('2f68656c6c6f00002c460000', 'hex'),
|
||||
},
|
||||
];
|
||||
|
||||
describe('OSC Message Encoding', () => {
|
||||
tests.forEach((messageTest) => {
|
||||
it(messageTest.description, () => {
|
||||
const encoded = osc.messageToBuffer(messageTest.message);
|
||||
deepEqual(encoded, messageTest.expected);
|
||||
});
|
||||
});
|
||||
it('bad string arg', () => {
|
||||
throws(
|
||||
() => {
|
||||
osc.messageToBuffer({ address: '/address', args: [{ type: 's', value: 123 }] });
|
||||
},
|
||||
{ name: /^TypeError$/ }
|
||||
);
|
||||
});
|
||||
|
||||
it('bad integer arg', () => {
|
||||
throws(
|
||||
() => {
|
||||
osc.messageToBuffer({ address: '/address', args: [{ type: 'i', value: 'hi' }] });
|
||||
},
|
||||
{ name: /^TypeError$/ }
|
||||
);
|
||||
});
|
||||
|
||||
it('bad float arg', () => {
|
||||
throws(
|
||||
() => {
|
||||
osc.messageToBuffer({ address: '/address', args: [{ type: 'f', value: 'hi' }] });
|
||||
},
|
||||
{ name: /^TypeError$/ }
|
||||
);
|
||||
});
|
||||
|
||||
it('bad blob arg', () => {
|
||||
throws(
|
||||
() => {
|
||||
osc.messageToBuffer({ address: '/address', args: [{ type: 'b', value: 123 }] });
|
||||
},
|
||||
{ name: /^TypeError$/ }
|
||||
);
|
||||
});
|
||||
|
||||
it('unknown arg type', () => {
|
||||
throws(
|
||||
() => {
|
||||
osc.messageToBuffer({ address: '/address', args: [{ type: 'z', value: 123 }] });
|
||||
},
|
||||
{ name: /^TypeError$/ }
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"esModuleInterop": true,
|
||||
"module": "commonjs",
|
||||
"target": "ES2020",
|
||||
"declaration": true,
|
||||
"rootDir": "./src",
|
||||
"strict": true,
|
||||
"outDir": "dist"
|
||||
},
|
||||
"exclude": ["node_modules", "dist", "examples"]
|
||||
}
|
||||
Reference in New Issue
Block a user