Skip to content

Commit 1d6bfe4

Browse files
committed
Switch from cbor-js to cbor2
1 parent d94b000 commit 1d6bfe4

File tree

7 files changed

+54
-184
lines changed

7 files changed

+54
-184
lines changed

package-lock.json

Lines changed: 23 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
"@xmldom/xmldom": "^0.9.8",
4545
"bson": "^7.0.0",
4646
"buffer": "^6.0.3",
47-
"cbor-js": "^0.1.0",
47+
"cbor2": "^2.0.1",
4848
"eventemitter3": "^5.0.1",
4949
"fast-png": "^7.0.1",
5050
"uuid": "^13.0.0",

src/core/transport/Transport.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ import {
1111
isRosbridgePngMessage,
1212
} from "../../types/protocol.ts";
1313
import { deserialize } from "bson";
14-
import CBOR from "cbor-js";
15-
import typedArrayTagger from "../../util/cborTypedArrayTags.ts";
14+
import { decode } from "cbor2";
1615
import decompressPng from "../../util/decompressPng.ts";
1716

1817
/**
@@ -221,7 +220,7 @@ export abstract class AbstractTransport
221220
* It is one technique for compressing JSON data.
222221
*/
223222
private handleCborMessage(cbor: ArrayBuffer) {
224-
const data: unknown = CBOR.decode(cbor, typedArrayTagger);
223+
const data: unknown = decode(new Uint8Array(cbor));
225224
if (isRosbridgeMessage(data)) {
226225
this.handleRosbridgeMessage(data);
227226
} else {

src/types/cbor-js.d.ts

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/util/cborTypedArrayTags.ts

Lines changed: 0 additions & 137 deletions
This file was deleted.

test/cbor.test.ts

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { describe, it, expect } from "vitest";
2-
import CBOR from "cbor-js";
3-
import cborTypedArrayTagger from "../src/util/cborTypedArrayTags.ts";
2+
import { decode } from "cbor2";
43

54
/** Convert hex string to ArrayBuffer. */
65
function hexToBuffer(hex: string) {
@@ -11,13 +10,13 @@ function hexToBuffer(hex: string) {
1110
const arr = tokens.map(function (t) {
1211
return parseInt(t, 16);
1312
});
14-
return new Uint8Array(arr).buffer;
13+
return new Uint8Array(arr);
1514
}
1615

1716
describe("CBOR Typed Array Tagger", function () {
1817
it("should convert tagged Uint16Array", function () {
1918
const data = hexToBuffer("d84546010002000300");
20-
const msg = CBOR.decode(data, cborTypedArrayTagger);
19+
const msg = decode(data);
2120

2221
if (!(msg instanceof Uint16Array)) {
2322
throw new Error("Expected Uint16Array");
@@ -30,7 +29,7 @@ describe("CBOR Typed Array Tagger", function () {
3029

3130
it("should convert tagged Uint32Array", function () {
3231
const data = hexToBuffer("d8464c010000000200000003000000");
33-
const msg = CBOR.decode(data, cborTypedArrayTagger);
32+
const msg = decode(data);
3433

3534
if (!(msg instanceof Uint32Array)) {
3635
throw new Error("Expected Uint32Array");
@@ -41,24 +40,24 @@ describe("CBOR Typed Array Tagger", function () {
4140
expect(msg[2]).to.equal(3);
4241
});
4342

44-
it("should convert tagged Uint64Array", function () {
43+
it("should convert tagged BigUint64Array", function () {
4544
const data = hexToBuffer(
4645
"d8475818010000000000000002000000000000000300000000000000",
4746
);
48-
const msg = CBOR.decode(data, cborTypedArrayTagger);
47+
const msg = decode(data);
4948

50-
if (!Array.isArray(msg)) {
51-
throw new Error("Expected Array");
49+
if (!(msg instanceof BigUint64Array)) {
50+
throw new Error("Expected BigUint64Array");
5251
}
5352
expect(msg).to.have.lengthOf(3);
54-
expect(msg[0]).to.equal(1);
55-
expect(msg[1]).to.equal(2);
56-
expect(msg[2]).to.equal(3);
53+
expect(msg[0]).to.equal(BigInt(1));
54+
expect(msg[1]).to.equal(BigInt(2));
55+
expect(msg[2]).to.equal(BigInt(3));
5756
});
5857

5958
it("should convert tagged Int8Array", function () {
6059
const data = hexToBuffer("d8484301fe03");
61-
const msg = CBOR.decode(data, cborTypedArrayTagger);
60+
const msg = decode(data);
6261

6362
if (!(msg instanceof Int8Array)) {
6463
throw new Error("Expected Int8Array");
@@ -71,7 +70,7 @@ describe("CBOR Typed Array Tagger", function () {
7170

7271
it("should convert tagged Int16Array", function () {
7372
const data = hexToBuffer("d84d460100feff0300");
74-
const msg = CBOR.decode(data, cborTypedArrayTagger);
73+
const msg = decode(data);
7574

7675
if (!(msg instanceof Int16Array)) {
7776
throw new Error("Expected Int16Array");
@@ -84,7 +83,7 @@ describe("CBOR Typed Array Tagger", function () {
8483

8584
it("should convert tagged Int32Array", function () {
8685
const data = hexToBuffer("d84e4c01000000feffffff03000000");
87-
const msg = CBOR.decode(data, cborTypedArrayTagger);
86+
const msg = decode(data);
8887

8988
if (!(msg instanceof Int32Array)) {
9089
throw new Error("Expected Int32Array");
@@ -95,24 +94,24 @@ describe("CBOR Typed Array Tagger", function () {
9594
expect(msg[2]).to.equal(3);
9695
});
9796

98-
it("should convert tagged Int64Array", function () {
97+
it("should convert tagged BigInt64Array", function () {
9998
const data = hexToBuffer(
10099
"d84f58180100000000000000feffffffffffffff0300000000000000",
101100
);
102-
const msg = CBOR.decode(data, cborTypedArrayTagger);
101+
const msg = decode(data);
103102

104-
if (!Array.isArray(msg)) {
105-
throw new Error("Expected Array");
103+
if (!(msg instanceof BigInt64Array)) {
104+
throw new Error("Expected BigInt64Array");
106105
}
107106
expect(msg).to.have.lengthOf(3);
108-
expect(msg[0]).to.equal(1);
109-
expect(msg[1]).to.equal(-2);
110-
expect(msg[2]).to.equal(3);
107+
expect(msg[0]).to.equal(BigInt(1));
108+
expect(msg[1]).to.equal(BigInt(-2));
109+
expect(msg[2]).to.equal(BigInt(3));
111110
});
112111

113112
it("should convert tagged Float32Array", function () {
114113
const data = hexToBuffer("d8554ccdcc8c3fcdcc0cc033335340");
115-
const msg = CBOR.decode(data, cborTypedArrayTagger);
114+
const msg = decode(data);
116115

117116
if (!(msg instanceof Float32Array)) {
118117
throw new Error("Expected Float32Array");
@@ -127,7 +126,7 @@ describe("CBOR Typed Array Tagger", function () {
127126
const data = hexToBuffer(
128127
"d85658189a9999999999f13f9a999999999901c06666666666660a40",
129128
);
130-
const msg = CBOR.decode(data, cborTypedArrayTagger);
129+
const msg = decode(data);
131130

132131
if (!(msg instanceof Float64Array)) {
133132
throw new Error("Expected Float64Array");
@@ -140,7 +139,7 @@ describe("CBOR Typed Array Tagger", function () {
140139

141140
it("should be able to unpack two typed arrays", function () {
142141
const data = hexToBuffer("82d8484308fe05d84d460100feff0300");
143-
const msg = CBOR.decode(data, cborTypedArrayTagger);
142+
const msg = decode(data);
144143

145144
if (!Array.isArray(msg)) {
146145
throw new Error("Expected Array");

test/transport.test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import type {
88
RosbridgeMessage,
99
RosbridgePngMessage,
1010
} from "../src/types/protocol.ts";
11-
import CBOR from "cbor-js";
11+
import { encode } from "cbor2";
1212
import * as fastpng from "fast-png";
1313
import * as bson from "bson";
1414
import * as ws from "ws";
@@ -287,8 +287,9 @@ describe("Transport", () => {
287287
});
288288

289289
it("should handle CBOR message", async () => {
290-
const successMessage = CBOR.encode({ op: "test" });
291-
const failureMessage = CBOR.encode({ foo: "bar" });
290+
// CBOR data comes as ArrayBuffer from WebSocket, not Uint8Array
291+
const successMessage = encode({ op: "test" }).buffer;
292+
const failureMessage = encode({ foo: "bar" }).buffer;
292293

293294
// -- SUCCESS -- //
294295

0 commit comments

Comments
 (0)