Skip to content

Commit 07b2ec5

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

File tree

7 files changed

+55
-184
lines changed

7 files changed

+55
-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: 25 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,15 +10,16 @@ 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)) {
22+
console.error(msg);
2323
throw new Error("Expected Uint16Array");
2424
}
2525
expect(msg).to.have.lengthOf(3);
@@ -30,7 +30,7 @@ describe("CBOR Typed Array Tagger", function () {
3030

3131
it("should convert tagged Uint32Array", function () {
3232
const data = hexToBuffer("d8464c010000000200000003000000");
33-
const msg = CBOR.decode(data, cborTypedArrayTagger);
33+
const msg = decode(data);
3434

3535
if (!(msg instanceof Uint32Array)) {
3636
throw new Error("Expected Uint32Array");
@@ -41,24 +41,24 @@ describe("CBOR Typed Array Tagger", function () {
4141
expect(msg[2]).to.equal(3);
4242
});
4343

44-
it("should convert tagged Uint64Array", function () {
44+
it("should convert tagged BigUint64Array", function () {
4545
const data = hexToBuffer(
4646
"d8475818010000000000000002000000000000000300000000000000",
4747
);
48-
const msg = CBOR.decode(data, cborTypedArrayTagger);
48+
const msg = decode(data);
4949

50-
if (!Array.isArray(msg)) {
51-
throw new Error("Expected Array");
50+
if (!(msg instanceof BigUint64Array)) {
51+
throw new Error("Expected BigUint64Array");
5252
}
5353
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);
54+
expect(msg[0]).to.equal(BigInt(1));
55+
expect(msg[1]).to.equal(BigInt(2));
56+
expect(msg[2]).to.equal(BigInt(3));
5757
});
5858

5959
it("should convert tagged Int8Array", function () {
6060
const data = hexToBuffer("d8484301fe03");
61-
const msg = CBOR.decode(data, cborTypedArrayTagger);
61+
const msg = decode(data);
6262

6363
if (!(msg instanceof Int8Array)) {
6464
throw new Error("Expected Int8Array");
@@ -71,7 +71,7 @@ describe("CBOR Typed Array Tagger", function () {
7171

7272
it("should convert tagged Int16Array", function () {
7373
const data = hexToBuffer("d84d460100feff0300");
74-
const msg = CBOR.decode(data, cborTypedArrayTagger);
74+
const msg = decode(data);
7575

7676
if (!(msg instanceof Int16Array)) {
7777
throw new Error("Expected Int16Array");
@@ -84,7 +84,7 @@ describe("CBOR Typed Array Tagger", function () {
8484

8585
it("should convert tagged Int32Array", function () {
8686
const data = hexToBuffer("d84e4c01000000feffffff03000000");
87-
const msg = CBOR.decode(data, cborTypedArrayTagger);
87+
const msg = decode(data);
8888

8989
if (!(msg instanceof Int32Array)) {
9090
throw new Error("Expected Int32Array");
@@ -95,24 +95,24 @@ describe("CBOR Typed Array Tagger", function () {
9595
expect(msg[2]).to.equal(3);
9696
});
9797

98-
it("should convert tagged Int64Array", function () {
98+
it("should convert tagged BigInt64Array", function () {
9999
const data = hexToBuffer(
100100
"d84f58180100000000000000feffffffffffffff0300000000000000",
101101
);
102-
const msg = CBOR.decode(data, cborTypedArrayTagger);
102+
const msg = decode(data);
103103

104-
if (!Array.isArray(msg)) {
105-
throw new Error("Expected Array");
104+
if (!(msg instanceof BigInt64Array)) {
105+
throw new Error("Expected BigInt64Array");
106106
}
107107
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);
108+
expect(msg[0]).to.equal(BigInt(1));
109+
expect(msg[1]).to.equal(BigInt(-2));
110+
expect(msg[2]).to.equal(BigInt(3));
111111
});
112112

113113
it("should convert tagged Float32Array", function () {
114114
const data = hexToBuffer("d8554ccdcc8c3fcdcc0cc033335340");
115-
const msg = CBOR.decode(data, cborTypedArrayTagger);
115+
const msg = decode(data);
116116

117117
if (!(msg instanceof Float32Array)) {
118118
throw new Error("Expected Float32Array");
@@ -127,7 +127,7 @@ describe("CBOR Typed Array Tagger", function () {
127127
const data = hexToBuffer(
128128
"d85658189a9999999999f13f9a999999999901c06666666666660a40",
129129
);
130-
const msg = CBOR.decode(data, cborTypedArrayTagger);
130+
const msg = decode(data);
131131

132132
if (!(msg instanceof Float64Array)) {
133133
throw new Error("Expected Float64Array");
@@ -140,7 +140,7 @@ describe("CBOR Typed Array Tagger", function () {
140140

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

145145
if (!Array.isArray(msg)) {
146146
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)