21 Commits

Author SHA1 Message Date
jwetzell 85cfb3a100 remove esbuild 2024-10-09 19:21:29 -05:00
jwetzell f4202dd5ff fix readosc publish workflow 2024-10-09 19:12:31 -05:00
jwetzell 8e8d96fbb4 add readosc tool 2024-10-09 19:10:08 -05:00
jwetzell b393397972 add format option for makeosc 2024-10-09 19:03:41 -05:00
jwetzell 127c7d781d npm run format:write 2024-10-09 18:48:49 -05:00
jwetzell c404299e3e add makeosc utility 2024-10-09 18:48:40 -05:00
jwetzell e997b784c0 switch sendosc to osc library 2024-10-09 18:46:38 -05:00
jwetzell a5d155ac04 Merge pull request #9 from jwetzell/consolidate-osc-tools
Consolidate osc tools
2024-10-09 17:36:02 -05:00
jwetzell 77a414b33a add readme 2024-10-09 17:34:24 -05:00
jwetzell d5098d718e prettier write 2024-10-09 17:32:13 -05:00
jwetzell 1556867e7c add dist to prettierignore 2024-10-09 17:31:07 -05:00
jwetzell e85c1aa334 add author and repo to root package.json 2024-10-09 17:31:02 -05:00
jwetzell abb9ee60e3 npm pkg fix 2024-10-09 17:30:47 -05:00
jwetzell 7b681be033 bring in sendosc and convert to npm workspaces 2024-10-09 17:28:49 -05:00
jwetzell efce70806f bump version to 0.2.2 2024-10-09 12:34:16 -05:00
jwetzell 030c8b63bf remove debug logging 2024-10-09 12:33:38 -05:00
jwetzell 6f2714dc74 split things up 2024-10-09 12:33:28 -05:00
jwetzell cdb40284e6 add tag wildcard 2024-10-08 20:11:38 -05:00
jwetzell c66c6516ee bump version to 0.2.1 2024-10-08 20:08:43 -05:00
jwetzell ee67362828 fix types property 2024-10-08 20:08:27 -05:00
jwetzell 48131d4d0c change publish to on tag 2024-10-08 20:06:40 -05:00
43 changed files with 4152 additions and 478 deletions
+12
View File
@@ -0,0 +1,12 @@
module.exports = {
env: {
node: true,
commonjs: true,
es2021: true,
},
extends: ['airbnb', 'prettier'],
parserOptions: {
ecmaVersion: 'latest',
},
ignorePatterns: ['**/node_modules', '**/dist'],
};
@@ -1,8 +1,10 @@
name: Check osc-js builds
name: Check makeosc builds
on:
pull_request:
branches:
- main
paths:
- 'apps/makeosc/**'
jobs:
build-webui:
runs-on: ubuntu-latest
@@ -18,6 +20,6 @@ jobs:
cache-dependency-path: 'package-lock.json'
- name: Install Node.js dependencies
run: npm ci
run: npm ci --workspace makeosc
- run: npm run build
- run: npm run build --workspace makeosc
+25
View File
@@ -0,0 +1,25 @@
name: Check @jwetzell/osc builds
on:
pull_request:
branches:
- main
paths:
- 'packages/osc/**'
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 @jwetzell/osc
- run: npm run build --workspace @jwetzell/osc
+25
View File
@@ -0,0 +1,25 @@
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
@@ -0,0 +1,25 @@
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
+24
View File
@@ -0,0 +1,24 @@
name: Publish makeosc to npmjs
on:
push:
tags:
- 'makeosc/*'
jobs:
publish:
permissions:
id-token: write
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
- uses: actions/setup-node@1e60f620b9541d16bece96c5465dc8ee9832be0b # v4.0.3
with:
node-version-file: '.nvmrc'
registry-url: 'https://registry.npmjs.org'
cache: 'npm'
cache-dependency-path: './package-lock.json'
- name: Install Node.js dependencies
run: npm ci --workspace=makeosc
- name: Publish to NPM
run: npm publish --provenance --access=public --workspace=makeosc
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
@@ -1,6 +1,8 @@
name: Publish to npmjs
name: Publish @jwetzell/osc to npmjs
on:
release:
push:
tags:
- 'osc/*'
jobs:
publish-lib:
permissions:
@@ -16,9 +18,9 @@ jobs:
cache-dependency-path: 'package-lock.json'
- name: Install Node.js dependencies
run: npm ci
run: npm ci --workspace @jwetzell/osc
- name: Publish to NPM
run: npm publish --provenance --access=public
run: npm publish --provenance --access=public --workspace @jwetzell/osc
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
+24
View File
@@ -0,0 +1,24 @@
name: Publish readosc to npmjs
on:
push:
tags:
- 'readosc/*'
jobs:
publish:
permissions:
id-token: write
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
- uses: actions/setup-node@1e60f620b9541d16bece96c5465dc8ee9832be0b # v4.0.3
with:
node-version-file: '.nvmrc'
registry-url: 'https://registry.npmjs.org'
cache: 'npm'
cache-dependency-path: './package-lock.json'
- name: Install Node.js dependencies
run: npm ci --workspace=readosc
- name: Publish to NPM
run: npm publish --provenance --access=public --workspace=readosc
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
+24
View File
@@ -0,0 +1,24 @@
name: Publish sendosc to npmjs
on:
push:
tags:
- 'sendosc/*'
jobs:
publish:
permissions:
id-token: write
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
- uses: actions/setup-node@1e60f620b9541d16bece96c5465dc8ee9832be0b # v4.0.3
with:
node-version-file: '.nvmrc'
registry-url: 'https://registry.npmjs.org'
cache: 'npm'
cache-dependency-path: './package-lock.json'
- name: Install Node.js dependencies
run: npm ci --workspace=sendosc
- name: Publish to NPM
run: npm publish --provenance --access=public --workspace=sendosc
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
@@ -1,8 +1,10 @@
name: Run tests
name: Run @jwetzell/osc tests
on:
pull_request:
branches:
- main
paths:
- 'packages/osc/**'
jobs:
test:
runs-on: ubuntu-latest
@@ -18,6 +20,6 @@ jobs:
cache-dependency-path: './package-lock.json'
- name: Install Node.js dependencies
run: npm ci
run: npm ci --workspace @jwetzell/osc
- run: npm run test
- run: npm run test --workspace @jwetzell/osc
+1 -2
View File
@@ -1,2 +1 @@
node_modules
dist
node_modules
+2 -1
View File
@@ -1,2 +1,3 @@
*.md
*.min.*
*.min.*
dist
+5
View File
@@ -0,0 +1,5 @@
# osc-js
Collection of OSC things written in JS/TS
- [osc encoding/decoding library](./packages/osc/)
- [cli for sending osc messages](./apps/sendosc/)
+1
View File
@@ -0,0 +1 @@
dist
+41
View File
@@ -0,0 +1,41 @@
![npm](https://img.shields.io/npm/v/makeosc)
# makeosc
Simple NodeJS script for generating OSC bytes output to stdout.
## Usage
```
Usage: makeosc [options]
simple util to make osc buffers
Options:
-V, --version output the version number
--address <address> OSC address
--args <args...> osc args (default: [])
--slip slip encode message (default: false)
--types <types...> osc arg types (choices: "s", "i", "f", "b", default: [])
-h, --help display help for command
```
- using npx `npx makeosc@latest --address /hello`
- install using `npm install -g makeosc@latest` and run `makeosc --address /hello`
## Notes
- `--types` option is a space seperate list of type characters that will determine what the corresponding argument type will be set to
- optional
- uses type codes from the OSC spec
- `--args` option is a space-separated list of arguments. If a corresponding type is not found in the `--types` option it will default to string (`s`).
- blobs (`b`) are to be entered as hex string representing the buffer to be sent so the ASCII string `hello` would be `68656c6c6f`
- the default protocol is UDP but can be changed to TCP using the `--protocol` flag
## Examples
- `makeosc --address /test --args 1.0`
- `makeosc --address /this/is/sent/via/tcp`
- `makeosc --address /test/with/types --args 1 2.0 three --types i f`
- note that `types` is not the same length as `args` so the remaining argument (`three`) will default to string
- `makeosc --address /blob --args 68656c6c6f --types b`
+27
View File
@@ -0,0 +1,27 @@
{
"name": "makeosc",
"version": "0.1.0",
"description": "Simple NodeJS utility for generating osc buffers",
"main": "src/main.js",
"bin": {
"makeosc": "src/main.js"
},
"files": [
"src/"
],
"author": {
"name": "Joel Wetzell",
"email": "me@jwetzell.com",
"url": "https://jwetzell.com"
},
"repository": {
"type": "git",
"url": "git+https://github.com/jwetzell/osc-js.git"
},
"license": "MIT",
"dependencies": {
"@jwetzell/osc": "0.2.2",
"commander": "^11.0.0",
"slip": "^1.0.2"
}
}
+52
View File
@@ -0,0 +1,52 @@
#!/usr/bin/env node
const { Option, program } = require('commander');
const slip = require('slip');
const { argToTypedArg } = require('./utils');
const osc = require('@jwetzell/osc');
const packageInfo = require('../package.json');
program.name(packageInfo.name);
program.version(packageInfo.version);
program.description('simple util to make osc buffers');
program.addOption(new Option('--address <address>', 'OSC address').makeOptionMandatory());
program.addOption(new Option('--args <args...>', 'osc args').default([]));
program.addOption(new Option('--slip', 'slip encode message').default(false));
program.addOption(new Option('--types <types...>', 'osc arg types').choices(['s', 'i', 'f', 'b']).default([]));
program.addOption(
new Option('-f --format <format>', 'output format').choices(['bytes', 'json', 'f', 'b']).default('bytes')
);
program.action((options) => {
const { address, args, types, format } = options;
const typedArgs = args?.map((rawArg, index) => {
const argType = types[index] || 's';
return {
type: argType,
value: argToTypedArg(rawArg, argType),
};
});
const oscMsg = {
address,
args: typedArgs,
};
if (format === 'json') {
process.stdout.write(JSON.stringify(oscMsg));
} else if (format === 'bytes') {
let oscMsgBuffer = osc.messageToBuffer(oscMsg);
if (options.slip) {
oscMsgBuffer = slip.encode(oscMsgBuffer);
}
process.stdout.write(oscMsgBuffer);
}
});
program.parse();
process.stdout.on('error', (err) => {
if (err.code === 'EPIPE') {
process.exit(0);
}
});
+31
View File
@@ -0,0 +1,31 @@
const oscTypeConverterMap = {
s: {
fromString: (string) => string,
},
f: {
fromString: (string) => Number.parseFloat(string),
},
i: {
fromString: (string) => Number.parseInt(string, 10),
},
b: {
fromString: (string) => Buffer.from(string, 'hex'),
},
};
function argToTypedArg(rawArg, type = 's') {
const typeConverter = oscTypeConverterMap[type];
if (typeConverter === undefined) {
throw new Error('osc type error: unknown type '.concat(type));
}
if (typeConverter.fromString === undefined) {
throw new Error('osc type error: no string converter for type '.concat(type));
}
return typeConverter.fromString(rawArg);
}
module.exports = {
argToTypedArg,
};
+1
View File
@@ -0,0 +1 @@
dist
+20
View File
@@ -0,0 +1,20 @@
![npm](https://img.shields.io/npm/v/readosc)
# readosc
Simple NodeJS script for parsing OSC bytes from stdin.
## Usage
```
Usage: readosc [options]
simple util to read osc packets from stdin
Options:
-V, --version output the version number
-h, --help display help for command
```
- using npx `npx readosc@latest`
- install using `npm install -g readosc@latest` and run `readosc`
+26
View File
@@ -0,0 +1,26 @@
{
"name": "readosc",
"version": "0.1.0",
"description": "Simple NodeJS utility for parsing osc buffers from stdin",
"main": "src/main.js",
"bin": {
"readosc": "src/main.js"
},
"files": [
"src/"
],
"author": {
"name": "Joel Wetzell",
"email": "me@jwetzell.com",
"url": "https://jwetzell.com"
},
"repository": {
"type": "git",
"url": "git+https://github.com/jwetzell/osc-js.git"
},
"license": "MIT",
"dependencies": {
"@jwetzell/osc": "0.2.2",
"commander": "^11.0.0"
}
}
+20
View File
@@ -0,0 +1,20 @@
#!/usr/bin/env node
const { program } = require('commander');
const osc = require('@jwetzell/osc');
const packageInfo = require('../package.json');
program.name(packageInfo.name);
program.version(packageInfo.version);
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));
} catch (error) {
console.error({ error: error.toString() });
}
});
});
program.parse();
+1
View File
@@ -0,0 +1 @@
dist
+44
View File
@@ -0,0 +1,44 @@
![npm](https://img.shields.io/npm/v/sendosc)
# sendosc
Simple NodeJS script for sending OSC via TCP or UDP originally inspired by this [c++ utility](https://github.com/yoggy/sendosc) of the same name.
## Usage
```
Usage: sendosc [options]
simple util to sendosc
Options:
-V, --version output the version number
--protocol <protocol> Network protocol (choices: "tcp", "udp", default: "udp")
--host <host> the host to send osc to
--port <port> the port to send osc to
--address <address> OSC address
--args <args...> osc args (default: [])
--slip slip encode message (default: false)
--types <types...> osc arg types (choices: "s", "i", "f", "b", default: [])
-h, --help display help for command
```
- using npx `npx sendosc@latest --host 127.0.0.1 --port 9999 --address /hello`
- install using `npm install -g sendosc@latest` and run `sendosc --host 127.0.0.1 --port 9999 --address /hello`
## Notes
- `--types` option is a space seperate list of type characters that will determine what the corresponding argument type will be set to
- optional
- uses type codes from the OSC spec
- `--args` option is a space-separated list of arguments. If a corresponding type is not found in the `--types` option it will default to string (`s`).
- blobs (`b`) are to be entered as hex string representing the buffer to be sent so the ASCII string `hello` would be `68656c6c6f`
- the default protocol is UDP but can be changed to TCP using the `--protocol` flag
## Examples
- `sendosc --host 127.0.0.1 --port 8000 --address /test --args 1.0`
- `sendosc --protocol tcp --host 127.0.0.1 --port 8000 --address /this/is/sent/via/tcp`
- `sendosc --host 127.0.0.1 --port 8000 --address /test/with/types --args 1 2.0 three --types i f`
- note that `types` is not the same length as `args` so the remaining argument (`three`) will default to string
- `sendosc --host 127.0.0.1 --port 8000 --address /blob --args 68656c6c6f --types b`
+27
View File
@@ -0,0 +1,27 @@
{
"name": "sendosc",
"version": "1.0.1",
"description": "Simple NodeJS utility for sending osc",
"main": "src/main.js",
"bin": {
"sendosc": "src/main.js"
},
"files": [
"src/"
],
"author": {
"name": "Joel Wetzell",
"email": "me@jwetzell.com",
"url": "https://jwetzell.com"
},
"repository": {
"type": "git",
"url": "git+https://github.com/jwetzell/osc-js.git"
},
"license": "MIT",
"dependencies": {
"@jwetzell/osc": "0.2.2",
"commander": "^11.0.0",
"slip": "^1.0.2"
}
}
+62
View File
@@ -0,0 +1,62 @@
#!/usr/bin/env node
const { Option, program } = require('commander');
const dgram = require('dgram');
const net = require('net');
const slip = require('slip');
const osc = require('@jwetzell/osc');
const { argToTypedArg } = require('./utils');
const packageInfo = require('../package.json');
program.name(packageInfo.name);
program.version(packageInfo.version);
program.description('simple util to sendosc');
program.addOption(new Option('--protocol <protocol>', 'Network protocol').choices(['tcp', 'udp']).default('udp'));
program.addOption(new Option('--host <host>', 'the host to send osc to').makeOptionMandatory());
program.addOption(new Option('--port <port>', 'the port to send osc to').makeOptionMandatory());
program.addOption(new Option('--address <address>', 'OSC address').makeOptionMandatory());
program.addOption(new Option('--args <args...>', 'osc args').default([]));
program.addOption(new Option('--slip', 'slip encode message').default(false));
program.addOption(new Option('--types <types...>', 'osc arg types').choices(['s', 'i', 'f', 'b']).default([]));
program.action((options) => {
const { host, port, address, args, types } = options;
const typedArgs = args?.map((rawArg, index) => {
const argType = types[index] || 's';
return {
type: argType,
value: argToTypedArg(rawArg, argType),
};
});
let oscMsgBuffer = osc.messageToBuffer({
address,
args: typedArgs,
});
if (options.slip) {
oscMsgBuffer = slip.encode(oscMsgBuffer);
}
if (options.protocol === 'tcp') {
const client = net.Socket();
client.on('error', (error) => {
console.error(error);
});
client.connect(port, host, () => {
client.write(oscMsgBuffer, () => {
client.destroy();
});
});
} else if (options.protocol === 'udp') {
const client = dgram.createSocket('udp4');
client.send(oscMsgBuffer, port, host, (error) => {
if (error) {
console.error(error);
}
client.close();
});
}
});
program.parse();
+31
View File
@@ -0,0 +1,31 @@
const oscTypeConverterMap = {
s: {
fromString: (string) => string,
},
f: {
fromString: (string) => Number.parseFloat(string),
},
i: {
fromString: (string) => Number.parseInt(string, 10),
},
b: {
fromString: (string) => Buffer.from(string, 'hex'),
},
};
function argToTypedArg(rawArg, type = 's') {
const typeConverter = oscTypeConverterMap[type];
if (typeConverter === undefined) {
throw new Error('osc type error: unknown type '.concat(type));
}
if (typeConverter.fromString === undefined) {
throw new Error('osc type error: no string converter for type '.concat(type));
}
return typeConverter.fromString(rawArg);
}
module.exports = {
argToTypedArg,
};
+3211 -128
View File
File diff suppressed because it is too large Load Diff
+19 -20
View File
@@ -1,30 +1,29 @@
{
"name": "@jwetzell/osc",
"version": "0.2.0",
"description": "",
"main": "dist/index.js",
"types": "dist/index.d.js",
"scripts": {
"prebuild": "rimraf dist",
"build": "tsc",
"prepublishOnly": "npm run build",
"pretest": "npm run build",
"test": "node --test --experimental-test-coverage"
},
"files": [
"dist"
],
"name": "osc",
"version": "0.0.0",
"private": true,
"author": {
"name": "Joel Wetzell",
"email": "me@jwetzell.com",
"url": "https://jwetzell.com"
},
"repository": "https://github.com/jwetzell/osc-js",
"license": "ISC",
"repository": {
"type": "git",
"url": "git+https://github.com/jwetzell/osc-js.git"
},
"scripts": {
"lint:check": "eslint ./",
"format:check": "prettier ./ --check",
"format:write": "prettier ./ --write"
},
"workspaces": [
"apps/*",
"packages/*"
],
"devDependencies": {
"@types/node": "22.7.4",
"prettier": "3.3.3",
"rimraf": "6.0.1",
"typescript": "5.6.2"
"eslint": "^8.48.0",
"eslint-config-airbnb": "^19.0.4",
"eslint-config-prettier": "^9.0.0"
}
}
+1
View File
@@ -0,0 +1 @@
dist
+32
View File
@@ -0,0 +1,32 @@
{
"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": {
"type": "git",
"url": "git+https://github.com/jwetzell/osc-js.git"
},
"license": "MIT",
"devDependencies": {
"@types/node": "22.7.4",
"rimraf": "6.0.1",
"typescript": "5.6.2"
}
}
+89
View File
@@ -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]);
}
+3
View File
@@ -0,0 +1,3 @@
export * from './models';
export * from './message';
export * from './bundle';
+76
View File
@@ -0,0 +1,76 @@
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,
};
}
}
}
+154
View File
@@ -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];
},
},
};
-2
View File
@@ -1,2 +0,0 @@
export * from './osc';
export * from './models';
-315
View File
@@ -1,315 +0,0 @@
import { OSCType, OSCArg, OSCMessage, OSCTypeConverter, OSCBundle, OSCTimeTag } from './models';
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];
},
},
};
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,
};
}
}
}
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);
console.log(bundleContentBytes);
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]);
}