mirror of
https://github.com/jwetzell/osc-js.git
synced 2026-09-06 22:58:58 +00:00
npm run format:write
This commit is contained in:
@@ -7,11 +7,13 @@ export * from './message';
|
|||||||
export * from './bundle';
|
export * from './bundle';
|
||||||
|
|
||||||
export function fromBuffer(bytes: Uint8Array): [OSCBundle | OSCMessage | undefined, Uint8Array | undefined] {
|
export function fromBuffer(bytes: Uint8Array): [OSCBundle | OSCMessage | undefined, Uint8Array | undefined] {
|
||||||
if (bytes[0] === 47) { // starts with '/'
|
if (bytes[0] === 47) {
|
||||||
return messageFromBuffer(bytes);
|
// starts with '/'
|
||||||
} else if (bytes[0] === 35) { // starts with '#'
|
return messageFromBuffer(bytes);
|
||||||
return bundleFromBuffer(bytes)
|
} else if (bytes[0] === 35) {
|
||||||
} else {
|
// starts with '#'
|
||||||
throw new Error('bytes do not look like an OSC message or bundle');
|
return bundleFromBuffer(bytes);
|
||||||
}
|
} else {
|
||||||
|
throw new Error('bytes do not look like an OSC message or bundle');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -3,39 +3,39 @@ const { describe, it } = require('node:test');
|
|||||||
const osc = require('../dist/cjs/index');
|
const osc = require('../dist/cjs/index');
|
||||||
|
|
||||||
const tests = [
|
const tests = [
|
||||||
{
|
{
|
||||||
description: 'simple bundle',
|
description: 'simple bundle',
|
||||||
expected: {
|
expected: {
|
||||||
timeTag: [32, 0],
|
timeTag: [32, 0],
|
||||||
contents: [{ address: '/oscillator/4/frequency', args: [{ type: 'f', value: 440 }] }],
|
contents: [{ address: '/oscillator/4/frequency', args: [{ type: 'f', value: 440 }] }],
|
||||||
},
|
},
|
||||||
bytes: new Uint8Array([
|
bytes: new Uint8Array([
|
||||||
...new TextEncoder().encode('#bundle'),
|
...new TextEncoder().encode('#bundle'),
|
||||||
...new Uint8Array([0x00]),
|
...new Uint8Array([0x00]),
|
||||||
...new Uint8Array([0, 0, 0, 32, 0, 0, 0, 0]),
|
...new Uint8Array([0, 0, 0, 32, 0, 0, 0, 0]),
|
||||||
...new Uint8Array([0x00, 0x00, 0x00, 0x20]),
|
...new Uint8Array([0x00, 0x00, 0x00, 0x20]),
|
||||||
...new Uint8Array([
|
...new Uint8Array([
|
||||||
0x2f, 0x6f, 0x73, 0x63, 0x69, 0x6c, 0x6c, 0x61, 0x74, 0x6f, 0x72, 0x2f, 0x34, 0x2f, 0x66, 0x72, 0x65, 0x71,
|
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,
|
0x75, 0x65, 0x6e, 0x63, 0x79, 0x00, 0x2c, 0x66, 0x00, 0x00, 0x43, 0xdc, 0x00, 0x00,
|
||||||
]),
|
]),
|
||||||
]),
|
]),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
description: 'osc 1.0 spec example 1',
|
description: 'osc 1.0 spec example 1',
|
||||||
bytes: new Uint8Array([
|
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,
|
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,
|
102, 0, 0, 67, 220, 0, 0,
|
||||||
]),
|
]),
|
||||||
expected: { address: '/oscillator/4/frequency', args: [{ type: 'f', value: 440 }] },
|
expected: { address: '/oscillator/4/frequency', args: [{ type: 'f', value: 440 }] },
|
||||||
},
|
},
|
||||||
]
|
];
|
||||||
|
|
||||||
describe('OSC Bytes Decoding', () => {
|
describe('OSC Bytes Decoding', () => {
|
||||||
tests.forEach((bytesTest) => {
|
tests.forEach((bytesTest) => {
|
||||||
it(bytesTest.description, () => {
|
it(bytesTest.description, () => {
|
||||||
const [decoded, remainingBytes] = osc.fromBuffer(bytesTest.bytes);
|
const [decoded, remainingBytes] = osc.fromBuffer(bytesTest.bytes);
|
||||||
deepEqual(decoded, bytesTest.expected);
|
deepEqual(decoded, bytesTest.expected);
|
||||||
equal(remainingBytes.length, 0)
|
equal(remainingBytes.length, 0);
|
||||||
});
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|||||||
@@ -155,9 +155,7 @@ const badTests = [
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
description: 'float64 missing bytes',
|
description: 'float64 missing bytes',
|
||||||
bytes: new Uint8Array([
|
bytes: new Uint8Array([47, 104, 101, 108, 108, 111, 0, 0, 44, 100, 0, 0, 0x40, 0x29, 0x87, 0xec, 0x82, 0x74, 0xb9]),
|
||||||
47, 104, 101, 108, 108, 111, 0, 0, 44, 100, 0, 0, 0x40, 0x29, 0x87, 0xec, 0x82, 0x74, 0xb9,
|
|
||||||
]),
|
|
||||||
throwsMessage: { name: /^Error$/, message: /not enough bytes/ },
|
throwsMessage: { name: /^Error$/, message: /not enough bytes/ },
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -170,7 +168,6 @@ const badTests = [
|
|||||||
bytes: new Uint8Array([47, 104, 101, 108, 108, 111, 0, 0, 44, 114, 0, 0, 20, 21, 22]),
|
bytes: new Uint8Array([47, 104, 101, 108, 108, 111, 0, 0, 44, 114, 0, 0, 20, 21, 22]),
|
||||||
throwsMessage: { name: /^Error$/, message: /must be at least/ },
|
throwsMessage: { name: /^Error$/, message: /must be at least/ },
|
||||||
},
|
},
|
||||||
|
|
||||||
];
|
];
|
||||||
|
|
||||||
describe('OSC Message Decoding Pass', () => {
|
describe('OSC Message Decoding Pass', () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user