bring in sendosc and convert to npm workspaces

This commit is contained in:
2024-10-09 17:28:49 -05:00
parent efce70806f
commit 7b681be033
26 changed files with 3572 additions and 162 deletions
+13
View File
@@ -0,0 +1,13 @@
module.exports = {
env: {
node: true,
commonjs: true,
es2021: true,
},
extends: ['airbnb', 'prettier'],
parserOptions: {
ecmaVersion: 'latest',
},
ignorePatterns: ["**/node_modules", "**/dist"]
};
+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
@@ -1,8 +1,10 @@
name: Check osc-js builds name: Check sendosc builds
on: on:
pull_request: pull_request:
branches: branches:
- main - main
paths:
- 'apps/sendosc/**'
jobs: jobs:
build-webui: build-webui:
runs-on: ubuntu-latest runs-on: ubuntu-latest
@@ -18,6 +20,6 @@ jobs:
cache-dependency-path: 'package-lock.json' cache-dependency-path: 'package-lock.json'
- name: Install Node.js dependencies - name: Install Node.js dependencies
run: npm ci run: npm ci --workspace sendosc
- run: npm run build - run: npm run build --workspace sendosc
@@ -1,8 +1,8 @@
name: Publish to npmjs name: Publish @jwetzell/osc to npmjs
on: on:
push: push:
tags: tags:
- '*' - 'osc/*'
jobs: jobs:
publish-lib: publish-lib:
permissions: permissions:
@@ -18,9 +18,9 @@ jobs:
cache-dependency-path: 'package-lock.json' cache-dependency-path: 'package-lock.json'
- name: Install Node.js dependencies - name: Install Node.js dependencies
run: npm ci run: npm ci --workspace @jwetzell/osc
- name: Publish to NPM - name: Publish to NPM
run: npm publish --provenance --access=public run: npm publish --provenance --access=public --workspace @jwetzell/osc
env: env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} 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: on:
pull_request: pull_request:
branches: branches:
- main - main
paths:
- 'packages/osc/**'
jobs: jobs:
test: test:
runs-on: ubuntu-latest runs-on: ubuntu-latest
@@ -18,6 +20,6 @@ jobs:
cache-dependency-path: './package-lock.json' cache-dependency-path: './package-lock.json'
- name: Install Node.js dependencies - 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 node_modules
dist
+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": "dist/main.js",
"bin": "dist/main.js",
"scripts": {
"prebuild": "rimraf dist",
"build": "esbuild src/main.js --bundle --platform=node --outfile=dist/main.js",
"prepack": "npm run build"
},
"author": {
"name": "Joel Wetzell",
"email": "me@jwetzell.com",
"url": "https://jwetzell.com"
},
"repository": "https://github.com/jwetzell/osc-js",
"license": "MIT",
"dependencies": {
"commander": "^11.0.0",
"slip": "^1.0.2"
},
"devDependencies": {
"esbuild": "0.24.0",
"rimraf": "6.0.1"
}
}
+61
View File
@@ -0,0 +1,61 @@
#!/usr/bin/env node
const { Option, program } = require('commander');
const dgram = require('dgram');
const net = require('net');
const slip = require('slip');
const osc = require('./osc');
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: osc.argToTypedArg(rawArg, argType),
};
});
let oscMsgBuffer = osc.toBuffer({
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();
+83
View File
@@ -0,0 +1,83 @@
const oscTypeConverterMap = {
s: {
toBuffer: (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');
},
fromString: (string) => string,
},
f: {
toBuffer: (number) => {
const buffer = Buffer.alloc(4);
buffer.writeFloatBE(number);
return buffer;
},
fromString: (string) => Number.parseFloat(string),
},
i: {
toBuffer: (number) => {
const buffer = Buffer.alloc(4);
buffer.writeInt32BE(number);
return buffer;
},
fromString: (string) => Number.parseInt(string, 10),
},
b: {
toBuffer: (data) => {
const sizeBuffer = oscTypeConverterMap.i.toBuffer(data.length);
const padSize = 4 - (data.length % 4);
const padBuffer = Buffer.from(Array(padSize).fill(0));
return Buffer.concat([sizeBuffer, data, padBuffer]);
},
fromString: (string) => Buffer.from(string, 'hex'),
},
};
function argsToBuffer(args) {
const argBuffers = [];
for (let index = 0; index < args.length; index += 1) {
const arg = args[index];
const typeConverter = oscTypeConverterMap[arg.type];
if (typeConverter === undefined) {
throw new Error('osc type error: unknown type '.concat(arg.type));
}
if (typeConverter.fromString === undefined) {
throw new Error('osc type error: no string converter for type '.concat(arg.type));
}
argBuffers.push(typeConverter.toBuffer(arg.value));
}
return Buffer.concat(argBuffers);
}
function toBuffer({ address, args }) {
const addressBuffer = oscTypeConverterMap.s.toBuffer(address);
const typeString = args.map((arg) => arg.type).join('');
const typesBuffer = oscTypeConverterMap.s.toBuffer(`,${typeString}`);
const argsBuffer = argsToBuffer(args);
return Buffer.concat([addressBuffer, typesBuffer, argsBuffer]);
}
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 = {
toBuffer,
argToTypedArg,
};
+3236 -128
View File
File diff suppressed because it is too large Load Diff
+13 -22
View File
@@ -1,30 +1,21 @@
{ {
"name": "@jwetzell/osc", "name": "osc",
"version": "0.2.2", "version": "0.0.0",
"description": "", "private": true,
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": { "scripts": {
"prebuild": "rimraf dist", "lint:check": "eslint ./",
"build": "tsc", "format:check": "prettier ./ --check",
"prepublishOnly": "npm run build", "format:write": "prettier ./ --write"
"pretest": "npm run build",
"test": "node --test --experimental-test-coverage"
}, },
"files": [ "workspaces": [
"dist" "apps/*",
"packages/*"
], ],
"author": {
"name": "Joel Wetzell",
"email": "me@jwetzell.com",
"url": "https://jwetzell.com"
},
"repository": "https://github.com/jwetzell/osc-js",
"license": "ISC",
"devDependencies": { "devDependencies": {
"@types/node": "22.7.4",
"prettier": "3.3.3", "prettier": "3.3.3",
"rimraf": "6.0.1", "eslint": "^8.48.0",
"typescript": "5.6.2" "eslint-config-airbnb": "^19.0.4",
"eslint-config-prettier": "^9.0.0"
} }
} }
+1
View File
@@ -0,0 +1 @@
dist
+29
View File
@@ -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"
}
}