node-osc 11.1.1 → 11.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.gitattributes +11 -0
- package/.github/workflows/bump-version.yml +2 -0
- package/.github/workflows/nodejs.yml +3 -0
- package/LICENSE +201 -165
- package/README.md +135 -42
- package/dist/lib/Bundle.js +66 -0
- package/dist/lib/Client.js +137 -22
- package/dist/lib/Message.js +87 -1
- package/dist/lib/Server.js +117 -6
- package/dist/lib/index.js +3 -0
- package/dist/lib/internal/decode.js +4 -4
- package/dist/lib/{internal/osc.js → osc.js} +70 -5
- package/dist/test/lib/osc.js +395 -0
- package/dist/test/test-client.js +152 -0
- package/dist/test/test-e2e.js +9 -3
- package/dist/test/test-encode-decode.js +849 -0
- package/dist/test/test-error-handling.js +116 -0
- package/dist/test/test-osc-internal.js +399 -41
- package/dist/test/test-promises.js +250 -0
- package/dist/test/test-types.js +42 -0
- package/dist/test/util.js +15 -8
- package/docs/API.md +477 -0
- package/docs/GUIDE.md +605 -0
- package/examples/README.md +119 -0
- package/examples/async-await.mjs +57 -0
- package/examples/bundle-example.mjs +92 -0
- package/examples/client.js +22 -5
- package/examples/error-handling.mjs +152 -0
- package/examples/esm.mjs +21 -0
- package/examples/server.js +16 -0
- package/jsdoc.json +16 -0
- package/lib/Bundle.mjs +66 -0
- package/lib/Client.mjs +137 -22
- package/lib/Message.mjs +87 -1
- package/lib/Server.mjs +117 -6
- package/lib/index.mjs +1 -0
- package/lib/internal/decode.mjs +4 -4
- package/lib/{internal/osc.mjs → osc.mjs} +71 -4
- package/package.json +12 -10
- package/rollup.config.mjs +48 -41
- package/scripts/generate-docs.mjs +229 -0
- package/test/fixtures/types/test-cjs-types.ts +19 -0
- package/test/fixtures/types/test-esm-types.ts +35 -0
- package/test/fixtures/types/tsconfig-cjs.test.json +17 -0
- package/test/fixtures/types/tsconfig-esm.test.json +17 -0
- package/test/test-bundle.mjs +0 -1
- package/test/test-client.mjs +152 -0
- package/test/test-e2e.mjs +9 -3
- package/test/test-encode-decode.mjs +847 -0
- package/test/test-error-handling.mjs +115 -0
- package/test/test-osc-internal.mjs +400 -42
- package/test/test-promises.mjs +249 -0
- package/test/test-types.mjs +39 -0
- package/test/util.mjs +15 -8
- package/tsconfig.json +45 -0
- package/types/Bundle.d.mts +70 -0
- package/types/Bundle.d.mts.map +1 -0
- package/types/Client.d.mts +101 -0
- package/types/Client.d.mts.map +1 -0
- package/types/Message.d.mts +84 -0
- package/types/Message.d.mts.map +1 -0
- package/types/Server.d.mts +98 -0
- package/types/Server.d.mts.map +1 -0
- package/types/index.d.mts +6 -0
- package/types/index.d.mts.map +1 -0
- package/types/internal/decode.d.mts +4 -0
- package/types/internal/decode.d.mts.map +1 -0
- package/types/osc.d.mts +66 -0
- package/types/osc.d.mts.map +1 -0
package/dist/lib/Server.js
CHANGED
|
@@ -4,14 +4,84 @@ var node_dgram = require('node:dgram');
|
|
|
4
4
|
var node_events = require('node:events');
|
|
5
5
|
var decode = require('#decode');
|
|
6
6
|
|
|
7
|
+
/**
|
|
8
|
+
* OSC Server for receiving messages and bundles over UDP.
|
|
9
|
+
*
|
|
10
|
+
* Emits the following events:
|
|
11
|
+
* - 'listening': Emitted when the server starts listening
|
|
12
|
+
* - 'message': Emitted when an OSC message is received (receives msg array and rinfo object)
|
|
13
|
+
* - 'bundle': Emitted when an OSC bundle is received (receives bundle object and rinfo object)
|
|
14
|
+
* - 'error': Emitted when a socket error or decoding error occurs (receives error and rinfo)
|
|
15
|
+
* - Address-specific events: Emitted for each message address (e.g., '/test')
|
|
16
|
+
*
|
|
17
|
+
* @class
|
|
18
|
+
* @extends EventEmitter
|
|
19
|
+
*
|
|
20
|
+
* @fires Server#listening
|
|
21
|
+
* @fires Server#message
|
|
22
|
+
* @fires Server#bundle
|
|
23
|
+
* @fires Server#error
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* // Create and listen for messages
|
|
27
|
+
* const server = new Server(3333, '0.0.0.0', () => {
|
|
28
|
+
* console.log('Server is listening');
|
|
29
|
+
* });
|
|
30
|
+
*
|
|
31
|
+
* server.on('message', (msg, rinfo) => {
|
|
32
|
+
* console.log('Message:', msg);
|
|
33
|
+
* console.log('From:', rinfo.address, rinfo.port);
|
|
34
|
+
* });
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* // Using async/await with events.once
|
|
38
|
+
* import { once } from 'node:events';
|
|
39
|
+
*
|
|
40
|
+
* const server = new Server(3333, '0.0.0.0');
|
|
41
|
+
* await once(server, 'listening');
|
|
42
|
+
*
|
|
43
|
+
* server.on('message', (msg) => {
|
|
44
|
+
* console.log('Message:', msg);
|
|
45
|
+
* });
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* // Listen for specific OSC addresses
|
|
49
|
+
* server.on('/note', (msg) => {
|
|
50
|
+
* const [address, pitch, velocity] = msg;
|
|
51
|
+
* console.log(`Note: ${pitch}, Velocity: ${velocity}`);
|
|
52
|
+
* });
|
|
53
|
+
*/
|
|
7
54
|
class Server extends node_events.EventEmitter {
|
|
55
|
+
/**
|
|
56
|
+
* Create an OSC Server.
|
|
57
|
+
*
|
|
58
|
+
* @param {number} port - The port to listen on.
|
|
59
|
+
* @param {string} [host='127.0.0.1'] - The host address to bind to. Use '0.0.0.0' to listen on all interfaces.
|
|
60
|
+
* @param {Function} [cb] - Optional callback function called when server starts listening.
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* // Basic server
|
|
64
|
+
* const server = new Server(3333);
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* // Server on all interfaces with callback
|
|
68
|
+
* const server = new Server(3333, '0.0.0.0', () => {
|
|
69
|
+
* console.log('Server started');
|
|
70
|
+
* });
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* // Host parameter can be omitted, callback as second parameter
|
|
74
|
+
* const server = new Server(3333, () => {
|
|
75
|
+
* console.log('Server started on 127.0.0.1');
|
|
76
|
+
* });
|
|
77
|
+
*/
|
|
8
78
|
constructor(port, host='127.0.0.1', cb) {
|
|
9
79
|
super();
|
|
10
80
|
if (typeof host === 'function') {
|
|
11
81
|
cb = host;
|
|
12
82
|
host = '127.0.0.1';
|
|
13
83
|
}
|
|
14
|
-
|
|
84
|
+
|
|
15
85
|
let decoded;
|
|
16
86
|
this.port = port;
|
|
17
87
|
this.host = host;
|
|
@@ -20,10 +90,20 @@ class Server extends node_events.EventEmitter {
|
|
|
20
90
|
reuseAddr: true
|
|
21
91
|
});
|
|
22
92
|
this._sock.bind(port, host);
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
93
|
+
|
|
94
|
+
// Support both callback and promise-based listening
|
|
95
|
+
if (cb) {
|
|
96
|
+
this._sock.on('listening', () => {
|
|
97
|
+
this.emit('listening');
|
|
98
|
+
cb();
|
|
99
|
+
});
|
|
100
|
+
} else {
|
|
101
|
+
// For promise support, still emit the event but don't require a callback
|
|
102
|
+
this._sock.on('listening', () => {
|
|
103
|
+
this.emit('listening');
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
|
|
27
107
|
this._sock.on('message', (msg, rinfo) => {
|
|
28
108
|
try {
|
|
29
109
|
decoded = decode(msg);
|
|
@@ -41,9 +121,40 @@ class Server extends node_events.EventEmitter {
|
|
|
41
121
|
this.emit(decoded[0], decoded, rinfo);
|
|
42
122
|
}
|
|
43
123
|
});
|
|
124
|
+
|
|
125
|
+
this._sock.on('error', (err) => {
|
|
126
|
+
this.emit('error', err);
|
|
127
|
+
});
|
|
44
128
|
}
|
|
129
|
+
/**
|
|
130
|
+
* Close the server socket.
|
|
131
|
+
*
|
|
132
|
+
* This method can be used with either a callback or as a Promise.
|
|
133
|
+
*
|
|
134
|
+
* @param {Function} [cb] - Optional callback function called when socket is closed.
|
|
135
|
+
* @returns {Promise<void>|undefined} Returns a Promise if no callback is provided.
|
|
136
|
+
*
|
|
137
|
+
* @example
|
|
138
|
+
* // With callback
|
|
139
|
+
* server.close((err) => {
|
|
140
|
+
* if (err) console.error(err);
|
|
141
|
+
* });
|
|
142
|
+
*
|
|
143
|
+
* @example
|
|
144
|
+
* // With async/await
|
|
145
|
+
* await server.close();
|
|
146
|
+
*/
|
|
45
147
|
close(cb) {
|
|
46
|
-
|
|
148
|
+
if (cb) {
|
|
149
|
+
this._sock.close(cb);
|
|
150
|
+
} else {
|
|
151
|
+
return new Promise((resolve, reject) => {
|
|
152
|
+
this._sock.close((err) => {
|
|
153
|
+
if (err) reject(err);
|
|
154
|
+
else resolve();
|
|
155
|
+
});
|
|
156
|
+
});
|
|
157
|
+
}
|
|
47
158
|
}
|
|
48
159
|
}
|
|
49
160
|
|
package/dist/lib/index.js
CHANGED
|
@@ -4,6 +4,7 @@ var Message = require('./Message.js');
|
|
|
4
4
|
var Bundle = require('./Bundle.js');
|
|
5
5
|
var Server = require('./Server.js');
|
|
6
6
|
var Client = require('./Client.js');
|
|
7
|
+
var osc = require('./osc.js');
|
|
7
8
|
|
|
8
9
|
|
|
9
10
|
|
|
@@ -11,3 +12,5 @@ exports.Message = Message;
|
|
|
11
12
|
exports.Bundle = Bundle;
|
|
12
13
|
exports.Server = Server;
|
|
13
14
|
exports.Client = Client;
|
|
15
|
+
exports.decode = osc.decode;
|
|
16
|
+
exports.encode = osc.encode;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
3
|
+
var osc = require('../osc.js');
|
|
4
4
|
|
|
5
5
|
function sanitizeMessage(decoded) {
|
|
6
6
|
const message = [];
|
|
@@ -19,8 +19,8 @@ function sanitizeBundle(decoded) {
|
|
|
19
19
|
return decoded;
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
-
function
|
|
23
|
-
const decoded =
|
|
22
|
+
function decodeAndSanitize(data, customDecode = osc.decode) {
|
|
23
|
+
const decoded = customDecode(data);
|
|
24
24
|
if (decoded.oscType === 'bundle') {
|
|
25
25
|
return sanitizeBundle(decoded);
|
|
26
26
|
}
|
|
@@ -32,4 +32,4 @@ function decode(data, customFromBuffer = _osc.fromBuffer) {
|
|
|
32
32
|
}
|
|
33
33
|
}
|
|
34
34
|
|
|
35
|
-
module.exports =
|
|
35
|
+
module.exports = decodeAndSanitize;
|
|
@@ -150,6 +150,9 @@ function encodeArgument(arg) {
|
|
|
150
150
|
// For doubles, use float for now (OSC 1.0 doesn't have double)
|
|
151
151
|
return { tag: 'f', data: writeFloat32(arg.value) };
|
|
152
152
|
case 'T':
|
|
153
|
+
return { tag: 'T', data: node_buffer.Buffer.alloc(0) };
|
|
154
|
+
case 'F':
|
|
155
|
+
return { tag: 'F', data: node_buffer.Buffer.alloc(0) };
|
|
153
156
|
case 'boolean':
|
|
154
157
|
return arg.value ? { tag: 'T', data: node_buffer.Buffer.alloc(0) } : { tag: 'F', data: node_buffer.Buffer.alloc(0) };
|
|
155
158
|
case 'm':
|
|
@@ -203,7 +206,37 @@ function decodeArgument(tag, buffer, offset) {
|
|
|
203
206
|
}
|
|
204
207
|
}
|
|
205
208
|
|
|
206
|
-
|
|
209
|
+
/**
|
|
210
|
+
* Encode an OSC message or bundle to a Buffer.
|
|
211
|
+
*
|
|
212
|
+
* This low-level function converts OSC messages and bundles into binary format
|
|
213
|
+
* for transmission or storage. Useful for sending OSC over custom transports
|
|
214
|
+
* (WebSocket, TCP, HTTP), storing to files, or implementing custom OSC routers.
|
|
215
|
+
*
|
|
216
|
+
* @param {Object} message - OSC message or bundle object with oscType property
|
|
217
|
+
* @returns {Buffer} The encoded OSC data ready for transmission
|
|
218
|
+
*
|
|
219
|
+
* @example
|
|
220
|
+
* // Encode a message
|
|
221
|
+
* import { Message, encode } from 'node-osc';
|
|
222
|
+
*
|
|
223
|
+
* const message = new Message('/oscillator/frequency', 440);
|
|
224
|
+
* const buffer = encode(message);
|
|
225
|
+
* console.log('Encoded bytes:', buffer.length);
|
|
226
|
+
*
|
|
227
|
+
* @example
|
|
228
|
+
* // Encode a bundle
|
|
229
|
+
* import { Bundle, encode } from 'node-osc';
|
|
230
|
+
*
|
|
231
|
+
* const bundle = new Bundle(['/one', 1], ['/two', 2]);
|
|
232
|
+
* const buffer = encode(bundle);
|
|
233
|
+
*
|
|
234
|
+
* @example
|
|
235
|
+
* // Send over WebSocket
|
|
236
|
+
* const buffer = encode(message);
|
|
237
|
+
* websocket.send(buffer);
|
|
238
|
+
*/
|
|
239
|
+
function encode(message) {
|
|
207
240
|
if (message.oscType === 'bundle') {
|
|
208
241
|
return encodeBundleToBuffer(message);
|
|
209
242
|
} else {
|
|
@@ -254,7 +287,39 @@ function encodeBundleToBuffer(bundle) {
|
|
|
254
287
|
return node_buffer.Buffer.concat([bundleStringBuffer, timetagBuffer, ...elementBuffers]);
|
|
255
288
|
}
|
|
256
289
|
|
|
257
|
-
|
|
290
|
+
/**
|
|
291
|
+
* Decode a Buffer containing OSC data into a message or bundle object.
|
|
292
|
+
*
|
|
293
|
+
* This low-level function parses binary OSC data back into JavaScript objects.
|
|
294
|
+
* Useful for receiving OSC over custom transports, reading from files,
|
|
295
|
+
* or implementing custom OSC routers.
|
|
296
|
+
*
|
|
297
|
+
* @param {Buffer} buffer - The Buffer containing OSC data
|
|
298
|
+
* @returns {Object} The decoded OSC message or bundle. Messages have
|
|
299
|
+
* {oscType: 'message', address: string, args: Array}, bundles have
|
|
300
|
+
* {oscType: 'bundle', timetag: number, elements: Array}
|
|
301
|
+
* @throws {Error} If the buffer contains malformed OSC data
|
|
302
|
+
*
|
|
303
|
+
* @example
|
|
304
|
+
* // Decode received data
|
|
305
|
+
* import { decode } from 'node-osc';
|
|
306
|
+
*
|
|
307
|
+
* const decoded = decode(buffer);
|
|
308
|
+
* if (decoded.oscType === 'message') {
|
|
309
|
+
* console.log('Address:', decoded.address);
|
|
310
|
+
* console.log('Arguments:', decoded.args);
|
|
311
|
+
* }
|
|
312
|
+
*
|
|
313
|
+
* @example
|
|
314
|
+
* // Round-trip encode/decode
|
|
315
|
+
* import { Message, encode, decode } from 'node-osc';
|
|
316
|
+
*
|
|
317
|
+
* const original = new Message('/test', 42, 'hello');
|
|
318
|
+
* const buffer = encode(original);
|
|
319
|
+
* const decoded = decode(buffer);
|
|
320
|
+
* console.log(decoded.address); // '/test'
|
|
321
|
+
*/
|
|
322
|
+
function decode(buffer) {
|
|
258
323
|
// Check if it's a bundle or message
|
|
259
324
|
if (buffer.length >= 8 && buffer.subarray(0, 8).toString() === '#bundle\0') {
|
|
260
325
|
return decodeBundleFromBuffer(buffer);
|
|
@@ -314,7 +379,7 @@ function decodeBundleFromBuffer(buffer) {
|
|
|
314
379
|
|
|
315
380
|
// Read element data
|
|
316
381
|
const elementBuffer = buffer.subarray(offset, offset + size);
|
|
317
|
-
const element =
|
|
382
|
+
const element = decode(elementBuffer);
|
|
318
383
|
elements.push(element);
|
|
319
384
|
offset += size;
|
|
320
385
|
}
|
|
@@ -326,5 +391,5 @@ function decodeBundleFromBuffer(buffer) {
|
|
|
326
391
|
};
|
|
327
392
|
}
|
|
328
393
|
|
|
329
|
-
exports.
|
|
330
|
-
exports.
|
|
394
|
+
exports.decode = decode;
|
|
395
|
+
exports.encode = encode;
|
|
@@ -0,0 +1,395 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var node_buffer = require('node:buffer');
|
|
4
|
+
|
|
5
|
+
// OSC 1.0 Protocol Implementation
|
|
6
|
+
// Based on http://opensoundcontrol.org/spec-1_0
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
function padString(str) {
|
|
10
|
+
const nullTerminated = str + '\0';
|
|
11
|
+
const padding = 4 - (nullTerminated.length % 4);
|
|
12
|
+
return nullTerminated + '\0'.repeat(padding === 4 ? 0 : padding);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function readString(buffer, offset) {
|
|
16
|
+
let end = offset;
|
|
17
|
+
while (end < buffer.length && buffer[end] !== 0) {
|
|
18
|
+
end++;
|
|
19
|
+
}
|
|
20
|
+
const str = buffer.subarray(offset, end).toString('utf8');
|
|
21
|
+
// Find next 4-byte boundary
|
|
22
|
+
const paddedLength = Math.ceil((end - offset + 1) / 4) * 4;
|
|
23
|
+
return { value: str, offset: offset + paddedLength };
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function writeInt32(value) {
|
|
27
|
+
const buffer = node_buffer.Buffer.alloc(4);
|
|
28
|
+
buffer.writeInt32BE(value, 0);
|
|
29
|
+
return buffer;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function readInt32(buffer, offset) {
|
|
33
|
+
const value = buffer.readInt32BE(offset);
|
|
34
|
+
return { value, offset: offset + 4 };
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function writeFloat32(value) {
|
|
38
|
+
const buffer = node_buffer.Buffer.alloc(4);
|
|
39
|
+
buffer.writeFloatBE(value, 0);
|
|
40
|
+
return buffer;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function readFloat32(buffer, offset) {
|
|
44
|
+
const value = buffer.readFloatBE(offset);
|
|
45
|
+
return { value, offset: offset + 4 };
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function writeBlob(value) {
|
|
49
|
+
const length = value.length;
|
|
50
|
+
const lengthBuffer = writeInt32(length);
|
|
51
|
+
const padding = 4 - (length % 4);
|
|
52
|
+
const paddingBuffer = node_buffer.Buffer.alloc(padding === 4 ? 0 : padding);
|
|
53
|
+
return node_buffer.Buffer.concat([lengthBuffer, value, paddingBuffer]);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function readBlob(buffer, offset) {
|
|
57
|
+
const lengthResult = readInt32(buffer, offset);
|
|
58
|
+
const length = lengthResult.value;
|
|
59
|
+
const data = buffer.subarray(lengthResult.offset, lengthResult.offset + length);
|
|
60
|
+
const padding = 4 - (length % 4);
|
|
61
|
+
const nextOffset = lengthResult.offset + length + (padding === 4 ? 0 : padding);
|
|
62
|
+
return { value: data, offset: nextOffset };
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function writeTimeTag(value) {
|
|
66
|
+
// For now, treat timetag as a double (8 bytes)
|
|
67
|
+
// OSC timetag is 64-bit: 32-bit seconds since 1900, 32-bit fractional
|
|
68
|
+
const buffer = node_buffer.Buffer.alloc(8);
|
|
69
|
+
if (typeof value === 'number') {
|
|
70
|
+
// Convert to OSC timetag format
|
|
71
|
+
const seconds = Math.floor(value);
|
|
72
|
+
const fraction = Math.floor((value - seconds) * 0x100000000);
|
|
73
|
+
buffer.writeUInt32BE(seconds + 2208988800, 0); // Add epoch offset (1900 vs 1970)
|
|
74
|
+
buffer.writeUInt32BE(fraction, 4);
|
|
75
|
+
} else {
|
|
76
|
+
// If not a number, write zeros (immediate execution)
|
|
77
|
+
buffer.writeUInt32BE(0, 0);
|
|
78
|
+
buffer.writeUInt32BE(1, 4);
|
|
79
|
+
}
|
|
80
|
+
return buffer;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function readTimeTag(buffer, offset) {
|
|
84
|
+
const seconds = buffer.readUInt32BE(offset);
|
|
85
|
+
const fraction = buffer.readUInt32BE(offset + 4);
|
|
86
|
+
|
|
87
|
+
let value;
|
|
88
|
+
if (seconds === 0 && fraction === 1) {
|
|
89
|
+
// Immediate execution
|
|
90
|
+
value = 0;
|
|
91
|
+
} else {
|
|
92
|
+
// Convert from OSC epoch (1900) to Unix epoch (1970)
|
|
93
|
+
const unixSeconds = seconds - 2208988800;
|
|
94
|
+
const fractionalSeconds = fraction / 0x100000000;
|
|
95
|
+
value = unixSeconds + fractionalSeconds;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
return { value, offset: offset + 8 };
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function writeMidi(value) {
|
|
102
|
+
// MIDI message is 4 bytes: port id, status byte, data1, data2
|
|
103
|
+
const buffer = node_buffer.Buffer.alloc(4);
|
|
104
|
+
|
|
105
|
+
if (node_buffer.Buffer.isBuffer(value)) {
|
|
106
|
+
if (value.length !== 4) {
|
|
107
|
+
throw new Error('MIDI message must be exactly 4 bytes');
|
|
108
|
+
}
|
|
109
|
+
value.copy(buffer);
|
|
110
|
+
} else if (typeof value === 'object' && value !== null) {
|
|
111
|
+
// Allow object format: { port: 0, status: 144, data1: 60, data2: 127 }
|
|
112
|
+
buffer.writeUInt8(value.port || 0, 0);
|
|
113
|
+
buffer.writeUInt8(value.status || 0, 1);
|
|
114
|
+
buffer.writeUInt8(value.data1 || 0, 2);
|
|
115
|
+
buffer.writeUInt8(value.data2 || 0, 3);
|
|
116
|
+
} else {
|
|
117
|
+
throw new Error('MIDI value must be a 4-byte Buffer or object with port, status, data1, data2 properties');
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
return buffer;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function readMidi(buffer, offset) {
|
|
124
|
+
if (offset + 4 > buffer.length) {
|
|
125
|
+
throw new Error('Not enough bytes for MIDI message');
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const value = buffer.subarray(offset, offset + 4);
|
|
129
|
+
return { value, offset: offset + 4 };
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function encodeArgument(arg) {
|
|
133
|
+
if (typeof arg === 'object' && arg.type && arg.value !== undefined) {
|
|
134
|
+
// Explicit type specification
|
|
135
|
+
switch (arg.type) {
|
|
136
|
+
case 'i':
|
|
137
|
+
case 'integer':
|
|
138
|
+
return { tag: 'i', data: writeInt32(arg.value) };
|
|
139
|
+
case 'f':
|
|
140
|
+
case 'float':
|
|
141
|
+
return { tag: 'f', data: writeFloat32(arg.value) };
|
|
142
|
+
case 's':
|
|
143
|
+
case 'string':
|
|
144
|
+
return { tag: 's', data: node_buffer.Buffer.from(padString(arg.value)) };
|
|
145
|
+
case 'b':
|
|
146
|
+
case 'blob':
|
|
147
|
+
return { tag: 'b', data: writeBlob(arg.value) };
|
|
148
|
+
case 'd':
|
|
149
|
+
case 'double':
|
|
150
|
+
// For doubles, use float for now (OSC 1.0 doesn't have double)
|
|
151
|
+
return { tag: 'f', data: writeFloat32(arg.value) };
|
|
152
|
+
case 'T':
|
|
153
|
+
return { tag: 'T', data: node_buffer.Buffer.alloc(0) };
|
|
154
|
+
case 'F':
|
|
155
|
+
return { tag: 'F', data: node_buffer.Buffer.alloc(0) };
|
|
156
|
+
case 'boolean':
|
|
157
|
+
return arg.value ? { tag: 'T', data: node_buffer.Buffer.alloc(0) } : { tag: 'F', data: node_buffer.Buffer.alloc(0) };
|
|
158
|
+
case 'm':
|
|
159
|
+
case 'midi':
|
|
160
|
+
return { tag: 'm', data: writeMidi(arg.value) };
|
|
161
|
+
default:
|
|
162
|
+
throw new Error(`Unknown argument type: ${arg.type}`);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// Infer type from JavaScript type
|
|
167
|
+
switch (typeof arg) {
|
|
168
|
+
case 'number':
|
|
169
|
+
if (Number.isInteger(arg)) {
|
|
170
|
+
return { tag: 'i', data: writeInt32(arg) };
|
|
171
|
+
} else {
|
|
172
|
+
return { tag: 'f', data: writeFloat32(arg) };
|
|
173
|
+
}
|
|
174
|
+
case 'string':
|
|
175
|
+
return { tag: 's', data: node_buffer.Buffer.from(padString(arg)) };
|
|
176
|
+
case 'boolean':
|
|
177
|
+
return arg ? { tag: 'T', data: node_buffer.Buffer.alloc(0) } : { tag: 'F', data: node_buffer.Buffer.alloc(0) };
|
|
178
|
+
default:
|
|
179
|
+
if (node_buffer.Buffer.isBuffer(arg)) {
|
|
180
|
+
return { tag: 'b', data: writeBlob(arg) };
|
|
181
|
+
}
|
|
182
|
+
throw new Error(`Don't know how to encode argument: ${arg}`);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
function decodeArgument(tag, buffer, offset) {
|
|
187
|
+
switch (tag) {
|
|
188
|
+
case 'i':
|
|
189
|
+
return readInt32(buffer, offset);
|
|
190
|
+
case 'f':
|
|
191
|
+
return readFloat32(buffer, offset);
|
|
192
|
+
case 's':
|
|
193
|
+
return readString(buffer, offset);
|
|
194
|
+
case 'b':
|
|
195
|
+
return readBlob(buffer, offset);
|
|
196
|
+
case 'T':
|
|
197
|
+
return { value: true, offset };
|
|
198
|
+
case 'F':
|
|
199
|
+
return { value: false, offset };
|
|
200
|
+
case 'N':
|
|
201
|
+
return { value: null, offset };
|
|
202
|
+
case 'm':
|
|
203
|
+
return readMidi(buffer, offset);
|
|
204
|
+
default:
|
|
205
|
+
throw new Error(`I don't understand the argument code ${tag}`);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Encode an OSC message or bundle to a Buffer.
|
|
211
|
+
*
|
|
212
|
+
* This low-level function converts OSC messages and bundles into binary format
|
|
213
|
+
* for transmission or storage. Useful for sending OSC over custom transports
|
|
214
|
+
* (WebSocket, TCP, HTTP), storing to files, or implementing custom OSC routers.
|
|
215
|
+
*
|
|
216
|
+
* @param {Object} message - OSC message or bundle object with oscType property
|
|
217
|
+
* @returns {Buffer} The encoded OSC data ready for transmission
|
|
218
|
+
*
|
|
219
|
+
* @example
|
|
220
|
+
* // Encode a message
|
|
221
|
+
* import { Message, encode } from 'node-osc';
|
|
222
|
+
*
|
|
223
|
+
* const message = new Message('/oscillator/frequency', 440);
|
|
224
|
+
* const buffer = encode(message);
|
|
225
|
+
* console.log('Encoded bytes:', buffer.length);
|
|
226
|
+
*
|
|
227
|
+
* @example
|
|
228
|
+
* // Encode a bundle
|
|
229
|
+
* import { Bundle, encode } from 'node-osc';
|
|
230
|
+
*
|
|
231
|
+
* const bundle = new Bundle(['/one', 1], ['/two', 2]);
|
|
232
|
+
* const buffer = encode(bundle);
|
|
233
|
+
*
|
|
234
|
+
* @example
|
|
235
|
+
* // Send over WebSocket
|
|
236
|
+
* const buffer = encode(message);
|
|
237
|
+
* websocket.send(buffer);
|
|
238
|
+
*/
|
|
239
|
+
function encode(message) {
|
|
240
|
+
if (message.oscType === 'bundle') {
|
|
241
|
+
return encodeBundleToBuffer(message);
|
|
242
|
+
} else {
|
|
243
|
+
return encodeMessageToBuffer(message);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
function encodeMessageToBuffer(message) {
|
|
248
|
+
// OSC Message format:
|
|
249
|
+
// Address pattern (padded string)
|
|
250
|
+
// Type tag string (padded string starting with ,)
|
|
251
|
+
// Arguments (encoded according to type tags)
|
|
252
|
+
|
|
253
|
+
const address = padString(message.address);
|
|
254
|
+
const addressBuffer = node_buffer.Buffer.from(address);
|
|
255
|
+
|
|
256
|
+
const encodedArgs = message.args.map(encodeArgument);
|
|
257
|
+
const typeTags = ',' + encodedArgs.map(arg => arg.tag).join('');
|
|
258
|
+
const typeTagsBuffer = node_buffer.Buffer.from(padString(typeTags));
|
|
259
|
+
|
|
260
|
+
const argumentBuffers = encodedArgs.map(arg => arg.data);
|
|
261
|
+
|
|
262
|
+
return node_buffer.Buffer.concat([addressBuffer, typeTagsBuffer, ...argumentBuffers]);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
function encodeBundleToBuffer(bundle) {
|
|
266
|
+
// OSC Bundle format:
|
|
267
|
+
// "#bundle" (padded string)
|
|
268
|
+
// Timetag (8 bytes)
|
|
269
|
+
// Elements (each prefixed with size)
|
|
270
|
+
|
|
271
|
+
const bundleString = padString('#bundle');
|
|
272
|
+
const bundleStringBuffer = node_buffer.Buffer.from(bundleString);
|
|
273
|
+
|
|
274
|
+
const timetagBuffer = writeTimeTag(bundle.timetag);
|
|
275
|
+
|
|
276
|
+
const elementBuffers = bundle.elements.map(element => {
|
|
277
|
+
let elementBuffer;
|
|
278
|
+
if (element.oscType === 'bundle') {
|
|
279
|
+
elementBuffer = encodeBundleToBuffer(element);
|
|
280
|
+
} else {
|
|
281
|
+
elementBuffer = encodeMessageToBuffer(element);
|
|
282
|
+
}
|
|
283
|
+
const sizeBuffer = writeInt32(elementBuffer.length);
|
|
284
|
+
return node_buffer.Buffer.concat([sizeBuffer, elementBuffer]);
|
|
285
|
+
});
|
|
286
|
+
|
|
287
|
+
return node_buffer.Buffer.concat([bundleStringBuffer, timetagBuffer, ...elementBuffers]);
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/**
|
|
291
|
+
* Decode a Buffer containing OSC data into a message or bundle object.
|
|
292
|
+
*
|
|
293
|
+
* This low-level function parses binary OSC data back into JavaScript objects.
|
|
294
|
+
* Useful for receiving OSC over custom transports, reading from files,
|
|
295
|
+
* or implementing custom OSC routers.
|
|
296
|
+
*
|
|
297
|
+
* @param {Buffer} buffer - The Buffer containing OSC data
|
|
298
|
+
* @returns {Object} The decoded OSC message or bundle. Messages have
|
|
299
|
+
* {oscType: 'message', address: string, args: Array}, bundles have
|
|
300
|
+
* {oscType: 'bundle', timetag: number, elements: Array}
|
|
301
|
+
* @throws {Error} If the buffer contains malformed OSC data
|
|
302
|
+
*
|
|
303
|
+
* @example
|
|
304
|
+
* // Decode received data
|
|
305
|
+
* import { decode } from 'node-osc';
|
|
306
|
+
*
|
|
307
|
+
* const decoded = decode(buffer);
|
|
308
|
+
* if (decoded.oscType === 'message') {
|
|
309
|
+
* console.log('Address:', decoded.address);
|
|
310
|
+
* console.log('Arguments:', decoded.args);
|
|
311
|
+
* }
|
|
312
|
+
*
|
|
313
|
+
* @example
|
|
314
|
+
* // Round-trip encode/decode
|
|
315
|
+
* import { Message, encode, decode } from 'node-osc';
|
|
316
|
+
*
|
|
317
|
+
* const original = new Message('/test', 42, 'hello');
|
|
318
|
+
* const buffer = encode(original);
|
|
319
|
+
* const decoded = decode(buffer);
|
|
320
|
+
* console.log(decoded.address); // '/test'
|
|
321
|
+
*/
|
|
322
|
+
function decode(buffer) {
|
|
323
|
+
// Check if it's a bundle or message
|
|
324
|
+
if (buffer.length >= 8 && buffer.subarray(0, 8).toString() === '#bundle\0') {
|
|
325
|
+
return decodeBundleFromBuffer(buffer);
|
|
326
|
+
} else {
|
|
327
|
+
return decodeMessageFromBuffer(buffer);
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
function decodeMessageFromBuffer(buffer) {
|
|
332
|
+
let offset = 0;
|
|
333
|
+
|
|
334
|
+
// Read address pattern
|
|
335
|
+
const addressResult = readString(buffer, offset);
|
|
336
|
+
const address = addressResult.value;
|
|
337
|
+
offset = addressResult.offset;
|
|
338
|
+
|
|
339
|
+
// Read type tag string
|
|
340
|
+
const typeTagsResult = readString(buffer, offset);
|
|
341
|
+
const typeTags = typeTagsResult.value;
|
|
342
|
+
offset = typeTagsResult.offset;
|
|
343
|
+
|
|
344
|
+
if (!typeTags.startsWith(',')) {
|
|
345
|
+
throw new Error('Malformed Packet');
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
const tags = typeTags.slice(1); // Remove leading comma
|
|
349
|
+
const args = [];
|
|
350
|
+
|
|
351
|
+
for (const tag of tags) {
|
|
352
|
+
const argResult = decodeArgument(tag, buffer, offset);
|
|
353
|
+
args.push({ value: argResult.value });
|
|
354
|
+
offset = argResult.offset;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
return {
|
|
358
|
+
oscType: 'message',
|
|
359
|
+
address,
|
|
360
|
+
args
|
|
361
|
+
};
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
function decodeBundleFromBuffer(buffer) {
|
|
365
|
+
let offset = 8; // Skip "#bundle\0"
|
|
366
|
+
|
|
367
|
+
// Read timetag
|
|
368
|
+
const timetagResult = readTimeTag(buffer, offset);
|
|
369
|
+
const timetag = timetagResult.value;
|
|
370
|
+
offset = timetagResult.offset;
|
|
371
|
+
|
|
372
|
+
const elements = [];
|
|
373
|
+
|
|
374
|
+
while (offset < buffer.length) {
|
|
375
|
+
// Read element size
|
|
376
|
+
const sizeResult = readInt32(buffer, offset);
|
|
377
|
+
const size = sizeResult.value;
|
|
378
|
+
offset = sizeResult.offset;
|
|
379
|
+
|
|
380
|
+
// Read element data
|
|
381
|
+
const elementBuffer = buffer.subarray(offset, offset + size);
|
|
382
|
+
const element = decode(elementBuffer);
|
|
383
|
+
elements.push(element);
|
|
384
|
+
offset += size;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
return {
|
|
388
|
+
oscType: 'bundle',
|
|
389
|
+
timetag,
|
|
390
|
+
elements
|
|
391
|
+
};
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
exports.decode = decode;
|
|
395
|
+
exports.encode = encode;
|