Skip to content

Commit

Permalink
support 64-bit bigints
Browse files Browse the repository at this point in the history
  • Loading branch information
russellmcc committed Oct 14, 2024
1 parent 8b7dc2d commit 8cbcc28
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
29 changes: 28 additions & 1 deletion lib/osc-utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,14 @@ const parseOscArg = (
rest: sliceDataView(view, 4),
};
}
case "h": {
const view = toView(buffer);
const bigint = view.getBigInt64(0, false);
return {
value: { type: "bigint", value: bigint },
rest: sliceDataView(view, 8),
};
}
case "m": {
const view = toView(buffer);
if (view.byteLength < 4) {
Expand Down Expand Up @@ -293,6 +301,11 @@ const toOscArgument = (arg: OscArgWithType): ArrayBuffer => {
ret.setFloat32(0, arg.value, false);
return ret.buffer;
}
case "bigint": {
const ret = new DataView(new ArrayBuffer(8));
ret.setBigInt64(0, arg.value, false);
return ret.buffer;
}
case "double": {
const ret = new DataView(new ArrayBuffer(8));
ret.setFloat64(0, arg.value, false);
Expand Down Expand Up @@ -349,7 +362,8 @@ export type OscTypeCode =
| "S"
| "c"
| "r"
| "m";
| "m"
| "h";

const RepresentationToTypeCode: {
[key in OscArgWithType["type"]]: OscTypeCode;
Expand All @@ -368,6 +382,7 @@ const RepresentationToTypeCode: {
character: "c",
color: "r",
midi: "m",
bigint: "h",
};

export type OscMidiPacket = [number, number, number, number];
Expand All @@ -385,6 +400,10 @@ export type OscArgOutput =
type: "integer";
value: number;
}
| {
type: "bigint";
value: bigint;
}
| {
type: "timetag";
value: TimeTag;
Expand Down Expand Up @@ -454,6 +473,10 @@ export type OscArgWithType =
type: "integer";
value: number;
}
| {
type: "bigint";
value: bigint;
}
| {
type: "timetag";
value: TimeTag | Date;
Expand Down Expand Up @@ -499,6 +522,7 @@ export type OscArgInput =
| OscArgWithType
| string
| number
| bigint
| Date
| OscColor
| ArrayBuffer
Expand Down Expand Up @@ -541,6 +565,9 @@ const toOscArgWithType = (arg: OscArgInput): OscArgWithType => {
if (typeof arg === "object" && "red" in arg) {
return { type: "color", value: arg };
}
if (typeof arg === "bigint") {
return { type: "bigint", value: arg };
}
if (arg === true) {
return { type: "true" };
}
Expand Down
6 changes: 6 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@ Where the `type` is one of the following:
- `string` - string value
- `float` - numeric value
- `integer` - numeric value
- `color` - JS object containing `red`, `green`, `blue`, `alpha` in range 0-255
- `midi` - four-element array of numbers representing a midi packet of data
- `symbol` - string value
- `character` - a single-character string
- `double` - numeric value
- `bigint` - 64-bit `bigint` value (watch out, this will be truncated to 64 bits!)
- `blob` - `ArrayBuffer`, `DataView`, `TypedArray` or node.js `Buffer`
- `true` - value is boolean true
- `false` - value is boolean false
Expand Down
4 changes: 4 additions & 0 deletions test/osc-utilities.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1214,6 +1214,10 @@ test("Roundtrip color works", () => {
]);
});

test("Roundtrip bigint works", () => {
roundTripMessage([1234567891234n]);
});

test("Roundtrip midi works", () => {
roundTripMessage([
{
Expand Down

0 comments on commit 8cbcc28

Please sign in to comment.