60 Commits

Author SHA1 Message Date
jwetzell cc14c4c886 bump @jwetzell/osc to v1.6.0 2024-10-25 12:15:25 -05:00
jwetzell a4b29de9c9 Merge pull request #27 from jwetzell/dependabot/npm_and_yarn/types/node-22.7.7
Bump @types/node from 22.7.5 to 22.7.7
2024-10-25 12:13:41 -05:00
jwetzell 86f062a2ce fix import in decode test 2024-10-25 09:25:07 -05:00
jwetzell b7d129556a Merge pull request #29 from jwetzell/esm-and-cjs
add support for bundling both esm and cjs versions
2024-10-25 09:15:48 -05:00
jwetzell 2ee515ae5b npm run format:write 2024-10-25 09:10:52 -05:00
jwetzell 2089893a25 fix import in tests 2024-10-25 09:05:33 -05:00
jwetzell 2377599f12 add support for bundling both esm and cjs versions 2024-10-24 16:59:17 -05:00
jwetzell f18327d7b1 Create LICENSE 2024-10-22 08:35:24 -05:00
dependabot[bot] ad4efcf43d Bump @types/node from 22.7.5 to 22.7.7
Bumps [@types/node](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/HEAD/types/node) from 22.7.5 to 22.7.7.
- [Release notes](https://github.com/DefinitelyTyped/DefinitelyTyped/releases)
- [Commits](https://github.com/DefinitelyTyped/DefinitelyTyped/commits/HEAD/types/node)

---
updated-dependencies:
- dependency-name: "@types/node"
  dependency-type: direct:development
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
2024-10-21 17:47:28 +00:00
jwetzell 1b10930a8e bump @jwetzell/osc to v1.5.0 2024-10-21 12:46:15 -05:00
jwetzell 0c9fafc129 Merge pull request #28 from jwetzell/generic-decode
add function to guess type and decode
2024-10-21 12:45:52 -05:00
jwetzell aa1cae428b add function to guess type and decode 2024-10-21 12:44:43 -05:00
jwetzell 5449c8ec5b bump @jwetzell/osc to v1.4.0 2024-10-15 21:57:18 -05:00
jwetzell bf355706bb more bundle tests 2024-10-15 21:55:51 -05:00
jwetzell 31e0cb7d23 Merge pull request #26 from jwetzell/bundle-min-size
fix bundle minimum size check
2024-10-15 21:48:08 -05:00
jwetzell af6dd46303 fix bundle minimum size check 2024-10-15 21:47:33 -05:00
jwetzell 2ea1b928ba more tests 2024-10-15 21:41:47 -05:00
jwetzell 7bffb53691 split up passing and failing tests 2024-10-15 21:38:53 -05:00
jwetzell abc9445b2c npm run format:write 2024-10-15 21:36:53 -05:00
jwetzell 69a3eeec9f Merge pull request #25 from jwetzell/osc-address-only
allow no type string
2024-10-15 21:36:16 -05:00
jwetzell 99ae9d8a3b allow no type string 2024-10-15 21:35:53 -05:00
jwetzell b3dc125df3 npm run format:write 2024-10-14 21:35:16 -05:00
jwetzell 713fdc8959 Merge pull request #24 from jwetzell/message-error-handling
rework message testing for easier addition of failing tests
2024-10-14 21:34:39 -05:00
jwetzell 1a9decfa30 rework tests to have easily add tests that should fail 2024-10-14 21:33:35 -05:00
jwetzell 374bf0d39f messageToBuffer should error on invalid address 2024-10-14 21:15:29 -05:00
jwetzell 1f214396ae bump @jwetzell/osc to v1.3.0 2024-10-14 19:48:24 -05:00
jwetzell 925ff2a409 Merge pull request #23 from jwetzell/osc-array-type
add support for osc array types
2024-10-14 19:47:54 -05:00
jwetzell 5a5a6ab0a5 add support for osc array types 2024-10-14 19:46:49 -05:00
jwetzell 512b2d63c0 Merge pull request #19 from jwetzell/dependabot/npm_and_yarn/typescript-5.6.3
Bump typescript from 5.6.2 to 5.6.3
2024-10-14 07:17:08 -05:00
jwetzell e592229815 Merge pull request #20 from jwetzell/dependabot/npm_and_yarn/types/node-22.7.5
Bump @types/node from 22.7.4 to 22.7.5
2024-10-14 07:17:01 -05:00
jwetzell 768d6d5a4b Merge pull request #21 from jwetzell/dependabot/npm_and_yarn/commander-12.1.0
Bump commander from 11.1.0 to 12.1.0
2024-10-14 07:16:42 -05:00
dependabot[bot] 119574743d Bump commander from 11.1.0 to 12.1.0
Bumps [commander](https://github.com/tj/commander.js) from 11.1.0 to 12.1.0.
- [Release notes](https://github.com/tj/commander.js/releases)
- [Changelog](https://github.com/tj/commander.js/blob/master/CHANGELOG.md)
- [Commits](https://github.com/tj/commander.js/compare/v11.1.0...v12.1.0)

---
updated-dependencies:
- dependency-name: commander
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
2024-10-14 05:14:18 +00:00
dependabot[bot] 709312585f Bump @types/node from 22.7.4 to 22.7.5
Bumps [@types/node](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/HEAD/types/node) from 22.7.4 to 22.7.5.
- [Release notes](https://github.com/DefinitelyTyped/DefinitelyTyped/releases)
- [Commits](https://github.com/DefinitelyTyped/DefinitelyTyped/commits/HEAD/types/node)

---
updated-dependencies:
- dependency-name: "@types/node"
  dependency-type: direct:development
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
2024-10-14 05:14:13 +00:00
dependabot[bot] fdaa8b8ed1 Bump typescript from 5.6.2 to 5.6.3
Bumps [typescript](https://github.com/microsoft/TypeScript) from 5.6.2 to 5.6.3.
- [Release notes](https://github.com/microsoft/TypeScript/releases)
- [Changelog](https://github.com/microsoft/TypeScript/blob/main/azure-pipelines.release.yml)
- [Commits](https://github.com/microsoft/TypeScript/compare/v5.6.2...v5.6.3)

---
updated-dependencies:
- dependency-name: typescript
  dependency-type: direct:development
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
2024-10-14 05:14:07 +00:00
jwetzell 91fdf60737 Merge pull request #18 from jwetzell/osc-d-type
add support for osc d type
2024-10-13 21:45:29 -05:00
jwetzell e5d559eb1a add support for osc d type 2024-10-13 21:45:03 -05:00
jwetzell 1c68227aac npm run format:write 2024-10-13 21:36:19 -05:00
jwetzell f32b8ce679 Merge pull request #17 from jwetzell/osc-h-type
add support for osc h type
2024-10-13 21:35:32 -05:00
jwetzell 0df2dec5e9 add support for osc h type 2024-10-13 21:34:44 -05:00
jwetzell d587b0cf19 fix error message on time tag fromBuffer 2024-10-13 21:18:41 -05:00
jwetzell eca0639b38 Merge pull request #16 from jwetzell/osc-I-type
add support for osc infinitum type
2024-10-13 21:18:15 -05:00
jwetzell 7601cfb096 add support for osc infinitum type 2024-10-13 21:16:22 -05:00
jwetzell f9f763d6c4 bump @jwetzell/osc to v1.2.0 2024-10-13 17:18:01 -05:00
jwetzell 786e71fd82 Merge pull request #15 from jwetzell/osc-N-type
add support for osc nil type
2024-10-13 17:17:19 -05:00
jwetzell a2adc3d2f4 fix bundle test description 2024-10-13 17:15:19 -05:00
jwetzell 4ec6bae5fe add support for osc nil type 2024-10-13 17:15:02 -05:00
jwetzell f75b663812 bump @jwetzell/osc to v1.1.0 2024-10-13 16:45:46 -05:00
jwetzell 7a72853e41 disable npm version git tagging 2024-10-13 16:44:54 -05:00
jwetzell 81e0a542c5 npm run format:write 2024-10-13 16:43:32 -05:00
jwetzell 278d09bb6f Merge pull request #14 from jwetzell/bundle-multiple-content-chunks
fix osc bundles not parsing multiple content chunks correctly
2024-10-13 16:43:08 -05:00
jwetzell 4b56f09a64 add test osc bundle from resolume 2024-10-13 16:42:11 -05:00
jwetzell b44e5d306a fix osc bundles not parsing multiple content chunks correctly 2024-10-13 16:41:49 -05:00
jwetzell 7a3ad57482 Merge pull request #13 from jwetzell/osc-r-type
add support for osc color type
2024-10-13 16:40:08 -05:00
jwetzell 13a8334504 add test for color type 2024-10-13 16:39:31 -05:00
jwetzell e0bae3b386 remove app build workflows 2024-10-13 16:38:52 -05:00
jwetzell 2716ccc349 add support for osc color type 2024-10-13 16:38:14 -05:00
jwetzell acc93189a9 update readosc to v0.2.0 2024-10-13 15:40:46 -05:00
jwetzell 180c9ff76a Merge pull request #12 from jwetzell/readosc-update
allow readosc to make use of remainingBytes in messageFromBuffer
2024-10-13 15:40:21 -05:00
jwetzell 25fe42415e have readosc make use of remaining bytes 2024-10-13 15:39:13 -05:00
jwetzell fd6949c897 update @jwetzell/osc in readosc to v1.0.0 2024-10-13 15:38:41 -05:00
25 changed files with 785 additions and 288 deletions
-25
View File
@@ -1,25 +0,0 @@
name: Check makeosc builds
on:
pull_request:
branches:
- main
paths:
- 'apps/makeosc/**'
jobs:
build-webui:
runs-on: ubuntu-latest
steps:
- name: Check out repository
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
- name: Set up Node.js
uses: actions/setup-node@1e60f620b9541d16bece96c5465dc8ee9832be0b # v4.0.3
with:
node-version-file: '.nvmrc'
cache: 'npm'
cache-dependency-path: 'package-lock.json'
- name: Install Node.js dependencies
run: npm ci --workspace makeosc
- run: npm run build --workspace makeosc
-25
View File
@@ -1,25 +0,0 @@
name: Check readosc builds
on:
pull_request:
branches:
- main
paths:
- 'apps/readosc/**'
jobs:
build-webui:
runs-on: ubuntu-latest
steps:
- name: Check out repository
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
- name: Set up Node.js
uses: actions/setup-node@1e60f620b9541d16bece96c5465dc8ee9832be0b # v4.0.3
with:
node-version-file: '.nvmrc'
cache: 'npm'
cache-dependency-path: 'package-lock.json'
- name: Install Node.js dependencies
run: npm ci --workspace readosc
- run: npm run build --workspace readosc
-25
View File
@@ -1,25 +0,0 @@
name: Check sendosc builds
on:
pull_request:
branches:
- main
paths:
- 'apps/sendosc/**'
jobs:
build-webui:
runs-on: ubuntu-latest
steps:
- name: Check out repository
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
- name: Set up Node.js
uses: actions/setup-node@1e60f620b9541d16bece96c5465dc8ee9832be0b # v4.0.3
with:
node-version-file: '.nvmrc'
cache: 'npm'
cache-dependency-path: 'package-lock.json'
- name: Install Node.js dependencies
run: npm ci --workspace sendosc
- run: npm run build --workspace sendosc
+1
View File
@@ -0,0 +1 @@
git-tag-version=false
+21
View File
@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2024 Joel Wetzell
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
+1 -1
View File
@@ -21,7 +21,7 @@
"license": "MIT",
"dependencies": {
"@jwetzell/osc": "0.2.2",
"commander": "^11.0.0",
"commander": "^12.1.0",
"slip": "^1.0.2"
}
}
+3 -3
View File
@@ -1,6 +1,6 @@
{
"name": "readosc",
"version": "0.1.0",
"version": "0.2.0",
"description": "Simple NodeJS utility for parsing osc buffers from stdin",
"main": "src/main.js",
"bin": {
@@ -20,7 +20,7 @@
},
"license": "MIT",
"dependencies": {
"@jwetzell/osc": "0.2.2",
"commander": "^11.0.0"
"@jwetzell/osc": "1.0.0",
"commander": "^12.1.0"
}
}
+10 -2
View File
@@ -10,8 +10,16 @@ program.description('simple util to read osc packets from stdin');
program.action(() => {
process.stdin.on('data', (data) => {
try {
const message = osc.messageFromBuffer(data);
console.log(JSON.stringify(message));
let remainingBytes = data;
while (remainingBytes.length > 0) {
try {
const [message, bytesAfterMessage] = osc.messageFromBuffer(remainingBytes);
remainingBytes = bytesAfterMessage;
console.log(JSON.stringify(message));
} catch (error) {
console.error({ error: error.toString() });
}
}
} catch (error) {
console.error({ error: error.toString() });
}
+1 -1
View File
@@ -21,7 +21,7 @@
"license": "MIT",
"dependencies": {
"@jwetzell/osc": "0.2.2",
"commander": "^11.0.0",
"commander": "^12.1.0",
"slip": "^1.0.2"
}
}
+21 -18
View File
@@ -23,7 +23,7 @@
"license": "MIT",
"dependencies": {
"@jwetzell/osc": "0.2.2",
"commander": "^11.0.0",
"commander": "^12.1.0",
"slip": "^1.0.2"
},
"bin": {
@@ -36,27 +36,27 @@
"integrity": "sha512-HobCAMnDDSrKfrMBrF5ySA8fKO8Qjkn2HDKysuV51sHrpXABKJFQt5OemIQgbnSMNWU0kZ0+FF9xRrivHHaDTg=="
},
"apps/readosc": {
"version": "0.1.0",
"version": "0.2.0",
"license": "MIT",
"dependencies": {
"@jwetzell/osc": "0.2.2",
"commander": "^11.0.0"
"@jwetzell/osc": "1.0.0",
"commander": "^12.1.0"
},
"bin": {
"readosc": "src/main.js"
}
},
"apps/readosc/node_modules/@jwetzell/osc": {
"version": "0.2.2",
"resolved": "https://registry.npmjs.org/@jwetzell/osc/-/osc-0.2.2.tgz",
"integrity": "sha512-HobCAMnDDSrKfrMBrF5ySA8fKO8Qjkn2HDKysuV51sHrpXABKJFQt5OemIQgbnSMNWU0kZ0+FF9xRrivHHaDTg=="
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/@jwetzell/osc/-/osc-1.0.0.tgz",
"integrity": "sha512-242gA8R4k9vBbRJydqoKAG1WiQ9BUO6tFoemuC3eywRt5ns5qrlzIBEIwX0poQyMZdZB7XyzvR5rtbFC5tadKg=="
},
"apps/sendosc": {
"version": "1.0.1",
"license": "MIT",
"dependencies": {
"@jwetzell/osc": "0.2.2",
"commander": "^11.0.0",
"commander": "^12.1.0",
"slip": "^1.0.2"
},
"bin": {
@@ -255,9 +255,10 @@
"peer": true
},
"node_modules/@types/node": {
"version": "22.7.4",
"version": "22.7.7",
"resolved": "https://registry.npmjs.org/@types/node/-/node-22.7.7.tgz",
"integrity": "sha512-SRxCrrg9CL/y54aiMCG3edPKdprgMVGDXjA3gB8UmmBW5TcXzRUYAh8EWzTnSJFAd1rgImPELza+A3bJ+qxz8Q==",
"dev": true,
"license": "MIT",
"dependencies": {
"undici-types": "~6.19.2"
}
@@ -611,10 +612,11 @@
"license": "MIT"
},
"node_modules/commander": {
"version": "11.1.0",
"license": "MIT",
"version": "12.1.0",
"resolved": "https://registry.npmjs.org/commander/-/commander-12.1.0.tgz",
"integrity": "sha512-Vw8qHK3bZM9y/P10u3Vib8o/DdkvA2OtPtZvD871QKjy74Wj1WSKFILMPRPSdUSx5RFK1arlJzEtA4PkFgnbuA==",
"engines": {
"node": ">=16"
"node": ">=18"
}
},
"node_modules/concat-map": {
@@ -3406,9 +3408,10 @@
}
},
"node_modules/typescript": {
"version": "5.6.2",
"version": "5.6.3",
"resolved": "https://registry.npmjs.org/typescript/-/typescript-5.6.3.tgz",
"integrity": "sha512-hjcS1mhfuyi4WW8IWtjP7brDrG2cuDZukyrYrSauoXGNgx0S7zceP07adYkJycEr56BOUTNPzbInooiN3fn1qw==",
"dev": true,
"license": "Apache-2.0",
"bin": {
"tsc": "bin/tsc",
"tsserver": "bin/tsserver"
@@ -3648,12 +3651,12 @@
},
"packages/osc": {
"name": "@jwetzell/osc",
"version": "1.0.0",
"version": "1.6.0",
"license": "MIT",
"devDependencies": {
"@types/node": "22.7.4",
"@types/node": "22.7.7",
"rimraf": "6.0.1",
"typescript": "5.6.2"
"typescript": "5.6.3"
}
}
}
+17 -6
View File
@@ -1,12 +1,23 @@
{
"name": "@jwetzell/osc",
"version": "1.0.0",
"version": "1.6.0",
"description": "",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"main": "./dist/cjs/index.js",
"module": "./dist/esm/index.js",
"types": "./dist/types/index.d.ts",
"exports": {
".": {
"import": "./dist/esm/index.js",
"require": "./dist/cjs/index.js",
"types": "./dist/types/index.d.ts"
}
},
"scripts": {
"prebuild": "rimraf dist",
"build": "tsc",
"build": "npm run build:cjs && npm run build:esm && npm run build:types",
"build:cjs": "tsc -p tsconfig.cjs.json",
"build:esm": "tsc -p tsconfig.esm.json",
"build:types": "tsc -p tsconfig.types.json",
"prepublishOnly": "npm run build",
"pretest": "npm run build",
"test": "node --test --experimental-test-coverage"
@@ -25,8 +36,8 @@
},
"license": "MIT",
"devDependencies": {
"@types/node": "22.7.4",
"@types/node": "22.7.7",
"rimraf": "6.0.1",
"typescript": "5.6.2"
"typescript": "5.6.3"
}
}
+42 -32
View File
@@ -3,7 +3,7 @@ import { OSCBundle } from './models';
import { oscTypeConverterMap } from './osc-types';
export function bundleFromBuffer(bytes: Uint8Array): [OSCBundle | undefined, Uint8Array | undefined] {
if (bytes.length < 8) {
if (bytes.length < 20) {
throw new Error('bundle has to be at least 20 bytes');
}
@@ -22,42 +22,52 @@ export function bundleFromBuffer(bytes: Uint8Array): [OSCBundle | undefined, Uin
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');
}
// let [contentSize, remainingBytes] = oscTypeConverterMap.i.fromBuffer(bytesAfterTimeTag);
let remainingBytes = bytesAfterTimeTag;
const bundleContentBytes = remainingBytes.subarray(0, contentSize);
while (!endOfBundle) {
let [contentSize, bytesAfterContentSize] = oscTypeConverterMap.i.fromBuffer(remainingBytes);
if (bundleContentBytes[0] === 35) {
// # character indicating contents is a bundle
const [content, bytesAfterContent] = bundleFromBuffer(bundleContentBytes);
if (content) {
bundleContents.push(content);
}
} else if (bundleContentBytes[0] === 47) {
const [content, bytesAfterContent] = messageFromBuffer(bundleContentBytes);
if (content && content !== undefined) {
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;
}
if (typeof contentSize !== 'number') {
throw new Error('problem decoding content size');
}
remainingBytes = bytesAfterContentSize;
if (remainingBytes.length < contentSize) {
throw new Error('bundle does not contain enough data');
}
const bundleContentBytes = bytesAfterContentSize.subarray(0, contentSize);
if (bundleContentBytes[0] === 35) {
// # character indicating contents is a bundle
const [content, bytesAfterContent] = bundleFromBuffer(bundleContentBytes);
if (content) {
bundleContents.push(content);
}
} else if (bundleContentBytes[0] === 47) {
const [content, bytesAfterContent] = messageFromBuffer(bundleContentBytes);
if (content && content !== undefined) {
bundleContents.push(content);
}
} else {
throw new Error('bundle contents does not look like a OSC message or bundle');
}
remainingBytes = bytesAfterContentSize.subarray(contentSize);
if (remainingBytes.length === 0) {
endOfBundle = true;
}
return [{
timeTag,
contents: bundleContents,
},remainingBytes];
}
return [undefined, undefined]
return [
{
timeTag,
contents: bundleContents,
},
remainingBytes,
];
}
export function bundleToBuffer(bundle: OSCBundle): Uint8Array {
+14
View File
@@ -1,3 +1,17 @@
import { bundleFromBuffer } from './bundle';
import { messageFromBuffer } from './message';
import { OSCBundle, OSCMessage } from './models';
export * from './models';
export * from './message';
export * from './bundle';
export function fromBuffer(bytes: Uint8Array): [OSCBundle | OSCMessage | undefined, Uint8Array | undefined] {
if (bytes[0] === 47) { // starts with '/'
return messageFromBuffer(bytes);
} else if (bytes[0] === 35) { // starts with '#'
return bundleFromBuffer(bytes)
} else {
throw new Error('bytes do not look like an OSC message or bundle');
}
}
+70 -10
View File
@@ -28,17 +28,38 @@ function argsToBuffer(args: OSCArg[]) {
}
export function messageToBuffer(message: OSCMessage): Uint8Array {
if (!message.address.startsWith('/')) {
throw new Error('osc message must start with a /');
}
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}`);
let typeString = ',';
const flatOSCArgs: OSCArg[] = [];
message.args.forEach((oscArg) => {
if (Array.isArray(oscArg)) {
typeString += '[';
oscArg.forEach((oscArg) => {
typeString += oscArg.type;
flatOSCArgs.push(oscArg);
});
typeString += ']';
} else {
typeString += oscArg.type;
flatOSCArgs.push(oscArg);
}
});
const typesBuffer = oscTypeConverterMap.s.toBuffer(typeString);
if (typesBuffer === undefined) {
throw new Error('problem encoding types');
}
const argsBuffer = argsToBuffer(message.args);
const argsBuffer = argsToBuffer(flatOSCArgs);
const buffer = new Uint8Array(addressBuffer.length + typesBuffer.length + argsBuffer.length);
buffer.set(addressBuffer, 0);
buffer.set(typesBuffer, addressBuffer.length);
@@ -51,19 +72,51 @@ export function messageFromBuffer(bytes: Uint8Array): [OSCMessage | undefined, U
throw new Error('osc message must start with a /');
}
const oscArgs: OSCArg[] = [];
const oscArgs: (OSCArg | OSCArg[])[] = [];
const [address, bytesAfterAddress] = oscTypeConverterMap.s.fromBuffer(bytes);
if (typeof address === 'string') {
if (bytesAfterAddress.length === 0) {
// NOTE(jwetzell): OSC 1.0 spec says that messages without a type string should be allowed
return [
{
address,
args: [],
},
bytesAfterAddress,
];
}
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;
let oscArrayArg: OSCArg[] = [];
let insideArgArray = false;
for (let index = 1; index < typeString.length; index++) {
const argType = typeString.charAt(index) as OSCType;
if (argType === '[') {
if (insideArgArray) {
throw new Error('osc arg array opened without closing previous arg array');
}
oscArrayArg = [];
insideArgArray = true;
continue;
}
if (argType === ']') {
if (!insideArgArray) {
throw new Error('osc arg array closed without opening arg array');
}
oscArgs.push(oscArrayArg);
oscArrayArg = [];
insideArgArray = false;
continue;
}
const oscTypeConverter = oscTypeConverterMap[argType];
if (oscTypeConverter === undefined) {
throw new Error('unknown OSC type');
@@ -74,15 +127,22 @@ export function messageFromBuffer(bytes: Uint8Array): [OSCMessage | undefined, U
type: argType,
value: value,
};
oscArgs.push(arg);
if (insideArgArray) {
oscArrayArg.push(arg);
} else {
oscArgs.push(arg);
}
}
argsBuffer = remainingBytes;
}
return [{
address,
args: oscArgs,
}, argsBuffer];
return [
{
address,
args: oscArgs,
},
argsBuffer,
];
}
}
return [undefined, undefined]
return [undefined, undefined];
}
+16 -5
View File
@@ -1,11 +1,18 @@
export type OSCType = 's' | 'i' | 'f' | 'b' | 'T' | 'F' | 't';
export type OSCType = 's' | 'i' | 'f' | 'b' | 'T' | 'F' | 't' | 'r' | 'N' | '[' | ']';
export type OSCArg = {
type: OSCType;
value: string | number | Uint8Array | boolean | OSCTimeTag;
value: string | number | Uint8Array | boolean | OSCTimeTag | OSCColor | bigint | null;
};
export type OSCTimeTag = [number, number];
export type OSCColor = {
r: number;
g: number;
b: number;
a: number;
};
export type OSCBundle = {
timeTag: OSCTimeTag;
contents: (OSCBundle | OSCMessage)[];
@@ -13,10 +20,14 @@ export type OSCBundle = {
export type OSCMessage = {
address: string;
args: OSCArg[];
args: (OSCArg | OSCArg[])[];
};
export type OSCTypeConverter = {
toBuffer: (value: string | number | Uint8Array | boolean | OSCTimeTag) => Uint8Array | undefined;
fromBuffer: (buffer: Uint8Array) => [string | number | Uint8Array | boolean | OSCTimeTag | undefined, Uint8Array];
toBuffer: (
value: string | number | Uint8Array | boolean | OSCTimeTag | OSCColor | null | bigint
) => Uint8Array | undefined;
fromBuffer: (
buffer: Uint8Array
) => [string | number | Uint8Array | boolean | OSCTimeTag | OSCColor | null | bigint | undefined, Uint8Array];
};
+115 -1
View File
@@ -160,7 +160,7 @@ export const oscTypeConverterMap: { [key: string]: OSCTypeConverter } = {
},
fromBuffer: (buffer) => {
if (buffer.length < 8) {
throw new Error('osc time tag must be greater than 8 bytes');
throw new Error('osc time tag must be at least 8 bytes');
}
const [seconds, bytesAfterSeconds] = oscTypeConverterMap.i.fromBuffer(buffer);
const [fractional, bytesAfterFractional] = oscTypeConverterMap.i.fromBuffer(bytesAfterSeconds);
@@ -171,4 +171,118 @@ export const oscTypeConverterMap: { [key: string]: OSCTypeConverter } = {
return [undefined, bytesAfterFractional];
},
},
r: {
toBuffer: (color) => {
if (color === undefined || color === null) {
throw new TypeError('osc type r called with undefined or null value');
}
if (typeof color === 'object' && 'r' in color && 'g' in color && 'b' in color && 'a' in color) {
const view = new DataView(new ArrayBuffer(4));
view.setUint8(0, color.r);
view.setUint8(1, color.g);
view.setUint8(2, color.b);
view.setUint8(3, color.a);
return new Uint8Array(view.buffer);
}
throw new TypeError('osc type r called with non OSCColor object');
},
fromBuffer: (buffer) => {
if (buffer.length < 4) {
throw new Error('osc color must be at least 4 bytes');
}
const red = buffer[0];
const green = buffer[1];
const blue = buffer[2];
const alpha = buffer[3];
if (red !== undefined && green !== undefined && blue !== undefined && alpha !== undefined) {
if (
typeof red === 'number' &&
typeof green === 'number' &&
typeof blue === 'number' &&
typeof alpha === 'number'
) {
return [
{
r: red,
g: green,
b: blue,
a: alpha,
},
buffer.subarray(4),
];
}
}
throw new Error('problem converting osc ');
},
},
N: {
toBuffer: () => {
return new Uint8Array(0);
},
fromBuffer: (buffer) => {
return [null, buffer];
},
},
I: {
toBuffer: () => {
return new Uint8Array();
},
fromBuffer: (buffer) => {
return [Number.MAX_SAFE_INTEGER, buffer];
},
},
h: {
toBuffer: (number) => {
const view = new DataView(new ArrayBuffer(8));
if (typeof number === 'number') {
view.setBigInt64(0, BigInt(number));
} else if (typeof number === 'bigint') {
view.setBigInt64(0, number);
} else {
throw new TypeError('osc type h toBuffer called with non number or bigint value');
}
return new Uint8Array(view.buffer);
},
fromBuffer: (buffer) => {
if (buffer.length < 8) {
throw new Error('osc int64 must be at least 8 bytes');
}
const view = new DataView(new ArrayBuffer(8));
buffer.subarray(0, 8).forEach((byte, index) => {
view.setUint8(index, byte);
});
return [view.getBigInt64(0), buffer.subarray(8)];
},
},
d: {
toBuffer: (number) => {
if (typeof number === 'number') {
const view = new DataView(new ArrayBuffer(8));
view.setFloat64(0, number);
return new Uint8Array(view.buffer);
}
throw new TypeError('osc type d toBuffer called with non number value');
},
fromBuffer: (buffer) => {
if (buffer.length < 8) {
throw new Error('not enough bytes to read a osc float');
}
const view = new DataView(new ArrayBuffer(8));
buffer.subarray(0, 8).forEach((byte, index) => {
view.setUint8(index, byte);
});
const value = view.getFloat64(0);
return [value, buffer.subarray(8)];
},
},
};
+173 -6
View File
@@ -1,8 +1,8 @@
const { deepEqual, equal } = require('assert');
const { deepEqual, equal, throws } = require('assert');
const { describe, it } = require('node:test');
const osc = require('../dist/index');
const osc = require('../dist/cjs/index');
const tests = [
const goodTests = [
{
description: 'simple contents single message',
bytes: new Uint8Array([
@@ -20,14 +20,181 @@ const tests = [
contents: [{ address: '/oscillator/4/frequency', args: [{ type: 'f', value: 440 }] }],
},
},
{
description: 'simple contents single bundle',
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, 0x34]),
...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: [
{
timeTag: [32, 0],
contents: [{ address: '/oscillator/4/frequency', args: [{ type: 'f', value: 440 }] }],
},
],
},
},
{
description: 'resolume bundle example',
bytes: new Uint8Array([
0x23, 0x62, 0x75, 0x6e, 0x64, 0x6c, 0x65, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
0x28, 0x2f, 0x63, 0x6f, 0x6d, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x2f, 0x6c, 0x61, 0x79, 0x65, 0x72,
0x73, 0x2f, 0x31, 0x2f, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x00, 0x00, 0x2c, 0x66, 0x00, 0x00, 0x3d,
0x6d, 0x9c, 0x9c, 0x00, 0x00, 0x00, 0x2c, 0x2f, 0x63, 0x6f, 0x6d, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e,
0x2f, 0x73, 0x65, 0x6c, 0x65, 0x63, 0x74, 0x65, 0x64, 0x6c, 0x61, 0x79, 0x65, 0x72, 0x2f, 0x70, 0x6f, 0x73, 0x69,
0x74, 0x69, 0x6f, 0x6e, 0x00, 0x2c, 0x66, 0x00, 0x00, 0x3d, 0x6d, 0x9c, 0x9c, 0x00, 0x00, 0x00, 0x3c, 0x2f, 0x63,
0x6f, 0x6d, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x2f, 0x6c, 0x61, 0x79, 0x65, 0x72, 0x73, 0x2f, 0x31,
0x2f, 0x63, 0x6c, 0x69, 0x70, 0x73, 0x2f, 0x34, 0x2f, 0x74, 0x72, 0x61, 0x6e, 0x73, 0x70, 0x6f, 0x72, 0x74, 0x2f,
0x70, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x00, 0x00, 0x00, 0x00, 0x2c, 0x66, 0x00, 0x00, 0x3d, 0x6d, 0x9c,
0x9c, 0x00, 0x00, 0x00, 0x38, 0x2f, 0x63, 0x6f, 0x6d, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x2f, 0x73,
0x65, 0x6c, 0x65, 0x63, 0x74, 0x65, 0x64, 0x63, 0x6c, 0x69, 0x70, 0x2f, 0x74, 0x72, 0x61, 0x6e, 0x73, 0x70, 0x6f,
0x72, 0x74, 0x2f, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x00, 0x00, 0x00, 0x00, 0x2c, 0x66, 0x00, 0x00,
0x3d, 0x6d, 0x9c, 0x9c, 0x00, 0x00, 0x00, 0x28, 0x2f, 0x63, 0x6f, 0x6d, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f,
0x6e, 0x2f, 0x6c, 0x61, 0x79, 0x65, 0x72, 0x73, 0x2f, 0x31, 0x2f, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e,
0x00, 0x00, 0x2c, 0x66, 0x00, 0x00, 0x3d, 0x6d, 0x9c, 0x9c, 0x00, 0x00, 0x00, 0x2c, 0x2f, 0x63, 0x6f, 0x6d, 0x70,
0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x2f, 0x73, 0x65, 0x6c, 0x65, 0x63, 0x74, 0x65, 0x64, 0x6c, 0x61, 0x79,
0x65, 0x72, 0x2f, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x00, 0x2c, 0x66, 0x00, 0x00, 0x3d, 0x6d, 0x9c,
0x9c, 0x00, 0x00, 0x00, 0x3c, 0x2f, 0x63, 0x6f, 0x6d, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x2f, 0x6c,
0x61, 0x79, 0x65, 0x72, 0x73, 0x2f, 0x31, 0x2f, 0x63, 0x6c, 0x69, 0x70, 0x73, 0x2f, 0x34, 0x2f, 0x74, 0x72, 0x61,
0x6e, 0x73, 0x70, 0x6f, 0x72, 0x74, 0x2f, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x00, 0x00, 0x00, 0x00,
0x2c, 0x66, 0x00, 0x00, 0x3d, 0x6d, 0x9c, 0x9c, 0x00, 0x00, 0x00, 0x38, 0x2f, 0x63, 0x6f, 0x6d, 0x70, 0x6f, 0x73,
0x69, 0x74, 0x69, 0x6f, 0x6e, 0x2f, 0x73, 0x65, 0x6c, 0x65, 0x63, 0x74, 0x65, 0x64, 0x63, 0x6c, 0x69, 0x70, 0x2f,
0x74, 0x72, 0x61, 0x6e, 0x73, 0x70, 0x6f, 0x72, 0x74, 0x2f, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x00,
0x00, 0x00, 0x00, 0x2c, 0x66, 0x00, 0x00, 0x3d, 0x6d, 0x9c, 0x9c,
]),
expected: {
timeTag: [0, 1],
contents: [
{
address: '/composition/layers/1/position',
args: [{ type: 'f', value: 0.05801068246364593505859375 }],
},
{
address: '/composition/selectedlayer/position',
args: [{ type: 'f', value: 0.05801068246364593505859375 }],
},
{
address: '/composition/layers/1/clips/4/transport/position',
args: [{ type: 'f', value: 0.05801068246364593505859375 }],
},
{
address: '/composition/selectedclip/transport/position',
args: [{ type: 'f', value: 0.05801068246364593505859375 }],
},
{
address: '/composition/layers/1/position',
args: [{ type: 'f', value: 0.05801068246364593505859375 }],
},
{
address: '/composition/selectedlayer/position',
args: [{ type: 'f', value: 0.05801068246364593505859375 }],
},
{
address: '/composition/layers/1/clips/4/transport/position',
args: [{ type: 'f', value: 0.05801068246364593505859375 }],
},
{
address: '/composition/selectedclip/transport/position',
args: [{ type: 'f', value: 0.05801068246364593505859375 }],
},
],
},
},
];
describe('OSC Bundle Decoding', () => {
tests.forEach((bundleTest) => {
const badTests = [
{
description: 'bad bundle header',
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,
]),
]),
throwsMessage: {
name: /^Error$/,
message: 'bundle must start with #bundle',
},
},
{
description: 'incomplete bundle',
bytes: new Uint8Array([
...new TextEncoder().encode('#bundle'),
...new Uint8Array([0x00]),
...new Uint8Array([0, 0, 0, 32, 0, 0, 0, 0]),
]),
throwsMessage: {
name: /^Error$/,
message: 'bundle has to be at least 20 bytes',
},
},
{
description: 'bundle not enough bytes',
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]),
]),
throwsMessage: {
name: /^Error$/,
message: 'bundle does not contain enough data',
},
},
{
description: 'bad bundle contents',
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([
0x2a, 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,
]),
]),
throwsMessage: {
name: /^Error$/,
message: 'bundle contents does not look like a OSC message or bundle',
},
},
];
describe('OSC Bundle Decoding Pass', () => {
goodTests.forEach((bundleTest) => {
it(bundleTest.description, () => {
const [encoded, remainingBytes] = osc.bundleFromBuffer(bundleTest.bytes);
equal(remainingBytes.length, 0)
equal(remainingBytes.length, 0);
deepEqual(encoded, bundleTest.expected);
});
});
});
describe('OSC Bundle Decoding Throws', () => {
badTests.forEach((bundleTest) => {
it(bundleTest.description, () => {
throws(() => {
osc.bundleFromBuffer(bundleTest.bytes);
}, bundleTest.throwsMessage);
});
});
});
+1 -1
View File
@@ -1,6 +1,6 @@
const { deepEqual } = require('assert');
const { describe, it } = require('node:test');
const osc = require('../dist/index');
const osc = require('../dist/cjs/index');
const tests = [
{
+41
View File
@@ -0,0 +1,41 @@
const { deepEqual, equal } = require('assert');
const { describe, it } = require('node:test');
const osc = require('../dist/cjs/index');
const tests = [
{
description: 'simple bundle',
expected: {
timeTag: [32, 0],
contents: [{ address: '/oscillator/4/frequency', args: [{ type: 'f', value: 440 }] }],
},
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,
]),
]),
},
{
description: 'osc 1.0 spec example 1',
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 }] },
},
]
describe('OSC Bytes Decoding', () => {
tests.forEach((bytesTest) => {
it(bytesTest.description, () => {
const [decoded, remainingBytes] = osc.fromBuffer(bytesTest.bytes);
deepEqual(decoded, bytesTest.expected);
equal(remainingBytes.length, 0)
});
});
});
+120 -74
View File
@@ -1,8 +1,8 @@
const { deepEqual, throws, equal } = require('assert');
const { describe, it } = require('node:test');
const osc = require('../dist/index');
const osc = require('../dist/cjs/index');
const tests = [
const goodTests = [
{
description: 'simple address no args',
bytes: new Uint8Array([47, 104, 101, 108, 108, 111, 0, 0, 44, 0, 0, 0]),
@@ -38,6 +38,57 @@ const tests = [
bytes: new Uint8Array([47, 104, 101, 108, 108, 111, 0, 0, 44, 70, 0, 0]),
expected: { address: '/hello', args: [{ type: 'F', value: false }] },
},
{
description: 'simple address color arg',
bytes: new Uint8Array([47, 104, 101, 108, 108, 111, 0, 0, 44, 114, 0, 0, 20, 21, 22, 10]),
expected: { address: '/hello', args: [{ type: 'r', value: { r: 20, g: 21, b: 22, a: 10 } }] },
},
{
description: 'simple address nil arg',
bytes: new Uint8Array([47, 104, 101, 108, 108, 111, 0, 0, 44, 78, 0, 0]),
expected: { address: '/hello', args: [{ type: 'N', value: null }] },
},
{
description: 'simple address Inifinitum arg',
bytes: new Uint8Array([47, 104, 101, 108, 108, 111, 0, 0, 44, 73, 0, 0]),
expected: { address: '/hello', args: [{ type: 'I', value: Number.MAX_SAFE_INTEGER }] },
},
{
description: 'simple address int64 arg',
bytes: new Uint8Array([47, 104, 101, 108, 108, 111, 0, 0, 44, 104, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255]),
expected: { address: '/hello', args: [{ type: 'h', value: 281474976710655n }] },
},
{
description: 'simple address float64 arg',
bytes: new Uint8Array([
47, 104, 101, 108, 108, 111, 0, 0, 44, 100, 0, 0, 0x40, 0x29, 0x87, 0xec, 0x82, 0x74, 0xb9, 0xe6,
]),
expected: { address: '/hello', args: [{ type: 'd', value: 12.7654763 }] },
},
{
description: 'simple address array arg',
bytes: new Uint8Array([
47, 104, 101, 108, 108, 111, 0, 0, 44, 91, 100, 105, 93, 0, 0, 0, 0x40, 0x29, 0x87, 0xec, 0x82, 0x74, 0xb9, 0xe6,
0, 0, 3, 232,
]),
expected: {
address: '/hello',
args: [
[
{ type: 'd', value: 12.7654763 },
{ type: 'i', value: 1000 },
],
],
},
},
{
description: 'simple address no type string',
bytes: new Uint8Array([47, 104, 101, 108, 108, 111, 0, 0]),
expected: {
address: '/hello',
args: [],
},
},
{
description: 'osc 1.0 spec example 1',
bytes: new Uint8Array([
@@ -66,83 +117,78 @@ const tests = [
},
];
describe('OSC Message Decoding', () => {
tests.forEach((messageTest) => {
const badTests = [
{
description: 'bad address',
bytes: new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00, 0x00, 0x2c, 0x66, 0x00, 0x00, 0x42, 0x0a, 0x00, 0x00]),
throwsMessage: { name: /^Error$/, message: /must start with/ },
},
{
description: 'bad type string',
bytes: new Uint8Array([0x2f, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00, 0x00, 0x66, 0x00, 0x00, 0x00, 0x42, 0x0a, 0x00]),
throwsMessage: { name: /^Error$/, message: /type string must start with/ },
},
{
description: 'unknown type',
bytes: new Uint8Array([
0x2f, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00, 0x00, 0x2c, 0x7a, 0x00, 0x00, 0x42, 0x0a, 0x00, 0x00,
]),
throwsMessage: { name: /^Error$/, message: /unknown/ },
},
{
description: 'float arg missing bytes',
bytes: new Uint8Array([0x2f, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00, 0x00, 0x2c, 0x66, 0x00, 0x00, 0x42, 0x0a, 0x00]),
throwsMessage: { name: /^Error$/, message: /not enough bytes/ },
},
{
description: 'int arg missing bytes',
bytes: new Uint8Array([0x2f, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00, 0x00, 0x2c, 0x69, 0x00, 0x00, 0x00, 0x00, 0x00]),
throwsMessage: { name: /^Error$/, message: /not enough bytes/ },
},
{
description: 'blob bytes too small',
bytes: new Uint8Array([
0x2f, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00, 0x00, 0x2c, 0x62, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x62, 0x6c, 0x6f,
]),
throwsMessage: { name: /^Error$/, message: /not enough bytes/ },
},
{
description: 'float64 missing bytes',
bytes: new Uint8Array([
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/ },
},
{
description: 'int64 missing bytes',
bytes: new Uint8Array([47, 104, 101, 108, 108, 111, 0, 0, 44, 104, 0, 0, 0, 0, 255, 255, 255, 255, 255]),
throwsMessage: { name: /^Error$/, message: /must be at least/ },
},
{
description: 'color missing bytes',
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/ },
},
];
describe('OSC Message Decoding Pass', () => {
goodTests.forEach((messageTest) => {
it(messageTest.description, () => {
const [decoded, remainingBytes] = osc.messageFromBuffer(messageTest.bytes);
equal(remainingBytes.length, 0)
equal(remainingBytes.length, 0);
deepEqual(decoded, messageTest.expected);
});
});
});
it('bad address', () => {
throws(
() => {
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/ }
);
});
it('bad type string', () => {
throws(
() => {
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/ }
);
});
it('unknown type', () => {
throws(
() => {
osc.messageFromBuffer(
new Uint8Array([
0x2f, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00, 0x00, 0x2c, 0x7a, 0x00, 0x00, 0x42, 0x0a, 0x00, 0x00,
])
);
},
{ name: /^Error$/, message: /unknown/ }
);
});
it('float arg missing bytes', () => {
throws(
() => {
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/ }
);
});
it('int arg missing bytes', () => {
throws(
() => {
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/ }
);
});
it('blob bytes too small', () => {
throws(
() => {
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/ }
);
describe('OSC Message Decoding Throws', () => {
badTests.forEach((messageTest) => {
it(messageTest.description, () => {
throws(() => {
osc.messageFromBuffer(messageTest.bytes);
}, messageTest.throwsMessage);
});
});
});
+87 -46
View File
@@ -1,8 +1,8 @@
const { deepEqual, throws } = require('assert');
const { describe, it } = require('node:test');
const osc = require('../dist/index');
const osc = require('../dist/cjs/index');
const tests = [
const goodTests = [
{
description: 'simple address no args',
message: { address: '/hello', args: [] },
@@ -38,6 +38,44 @@ const tests = [
message: { address: '/hello', args: [{ type: 'F', value: false }] },
expected: new Uint8Array([47, 104, 101, 108, 108, 111, 0, 0, 44, 70, 0, 0]),
},
{
description: 'simple address color arg',
message: { address: '/hello', args: [{ type: 'r', value: { r: 20, g: 21, b: 22, a: 10 } }] },
expected: new Uint8Array([47, 104, 101, 108, 108, 111, 0, 0, 44, 114, 0, 0, 20, 21, 22, 10]),
},
{
description: 'simple address nil arg',
message: { address: '/hello', args: [{ type: 'N', value: null }] },
expected: new Uint8Array([47, 104, 101, 108, 108, 111, 0, 0, 44, 78, 0, 0]),
},
{
description: 'simple address int64 arg',
message: { address: '/hello', args: [{ type: 'h', value: 281474976710655n }] },
expected: new Uint8Array([47, 104, 101, 108, 108, 111, 0, 0, 44, 104, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255]),
},
{
description: 'simple address float64 arg',
message: { address: '/hello', args: [{ type: 'd', value: 12.7654763 }] },
expected: new Uint8Array([
47, 104, 101, 108, 108, 111, 0, 0, 44, 100, 0, 0, 0x40, 0x29, 0x87, 0xec, 0x82, 0x74, 0xb9, 0xe6,
]),
},
{
description: 'simple address array arg',
message: {
address: '/hello',
args: [
[
{ type: 'd', value: 12.7654763 },
{ type: 'i', value: 1000 },
],
],
},
expected: new Uint8Array([
47, 104, 101, 108, 108, 111, 0, 0, 44, 91, 100, 105, 93, 0, 0, 0, 0x40, 0x29, 0x87, 0xec, 0x82, 0x74, 0xb9, 0xe6,
0, 0, 3, 232,
]),
},
{
description: 'osc 1.0 spec example 1',
message: { address: '/oscillator/4/frequency', args: [{ type: 'f', value: 440 }] },
@@ -66,55 +104,58 @@ const tests = [
},
];
describe('OSC Message Encoding', () => {
tests.forEach((messageTest) => {
const badTests = [
{
description: 'bad string arg',
message: { address: '/address', args: [{ type: 's', value: 123 }] },
throwsMessage: { name: /^TypeError$/, message: /non string/ },
},
{
description: 'bad integer arg',
message: { address: '/address', args: [{ type: 'i', value: 'hi' }] },
throwsMessage: { name: /^TypeError$/, message: /non number/ },
},
{
description: 'bad float arg',
message: { address: '/address', args: [{ type: 'f', value: 'hi' }] },
throwsMessage: { name: /^TypeError$/, message: /non number/ },
},
{
description: 'bad blob arg',
message: { address: '/address', args: [{ type: 'b', value: 123 }] },
throwsMessage: { name: /^TypeError$/, message: /non Uint8Array/ },
},
{
description: 'unknown arg type',
message: { address: '/address', args: [{ type: 'z', value: 123 }] },
throwsMessage: { name: /^TypeError$/, message: /unknown type z/ },
},
{
description: 'address that does not start with / should throw',
message: { address: 'address', args: [] },
throwsMessage: {
name: /^Error$/,
message: 'osc message must start with a /',
},
},
];
describe('OSC Message Encoding Pass', () => {
goodTests.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$/ }
);
describe('OSC Message Encoding Throws', () => {
badTests.forEach((messageTest) => {
it(messageTest.description, () => {
throws(() => {
osc.messageToBuffer(messageTest.message);
}, messageTest.throwsMessage);
});
});
});
+8
View File
@@ -0,0 +1,8 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "CommonJS",
"declaration": false,
"outDir": "./dist/cjs"
}
}
+8
View File
@@ -0,0 +1,8 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "ESNext",
"declaration": false,
"outDir": "./dist/esm"
}
}
+7 -7
View File
@@ -1,12 +1,12 @@
{
"compilerOptions": {
"moduleResolution": "Node",
"lib": ["ESNext"],
"target": "ESNext",
"esModuleInterop": true,
"module": "commonjs",
"target": "ES2020",
"declaration": true,
"rootDir": "./src",
"strict": true,
"outDir": "dist"
"isolatedModules": true,
"rootDir": "src/",
"strict": true
},
"exclude": ["node_modules", "dist", "examples"]
"include": ["src/"]
}
+8
View File
@@ -0,0 +1,8 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"emitDeclarationOnly": true,
"declaration": true,
"outDir": "./dist/types"
}
}