Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"@xmldom/xmldom": "^0.9.8",
"bson": "^7.0.0",
"buffer": "^6.0.3",
"cbor-js": "^0.1.0",
"cbor2": "^2.0.1",
"eventemitter3": "^5.0.1",
"fast-png": "^7.0.1",
"uuid": "^13.0.0",
Expand Down
5 changes: 2 additions & 3 deletions src/core/transport/Transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ import {
isRosbridgePngMessage,
} from "../../types/protocol.ts";
import { deserialize } from "bson";
import CBOR from "cbor-js";
import typedArrayTagger from "../../util/cborTypedArrayTags.ts";
import { decode } from "cbor2";
import decompressPng from "../../util/decompressPng.ts";

/**
Expand Down Expand Up @@ -221,7 +220,7 @@ export abstract class AbstractTransport
* It is one technique for compressing JSON data.
*/
private handleCborMessage(cbor: ArrayBuffer) {
const data: unknown = CBOR.decode(cbor, typedArrayTagger);
const data: unknown = decode(new Uint8Array(cbor));
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uint8Arrays are views on ArrayBuffers, so this (thankfully) doesn't do any copying of potentially large data.

if (isRosbridgeMessage(data)) {
this.handleRosbridgeMessage(data);
} else {
Expand Down
8 changes: 0 additions & 8 deletions src/types/cbor-js.d.ts

This file was deleted.

137 changes: 0 additions & 137 deletions src/util/cborTypedArrayTags.ts

This file was deleted.

49 changes: 24 additions & 25 deletions test/cbor.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { describe, it, expect } from "vitest";
import CBOR from "cbor-js";
import cborTypedArrayTagger from "../src/util/cborTypedArrayTags.ts";
import { decode } from "cbor2";

/** Convert hex string to ArrayBuffer. */
function hexToBuffer(hex: string) {
Expand All @@ -11,13 +10,13 @@ function hexToBuffer(hex: string) {
const arr = tokens.map(function (t) {
return parseInt(t, 16);
});
return new Uint8Array(arr).buffer;
return new Uint8Array(arr);
}

describe("CBOR Typed Array Tagger", function () {
it("should convert tagged Uint16Array", function () {
const data = hexToBuffer("d84546010002000300");
const msg = CBOR.decode(data, cborTypedArrayTagger);
const msg = decode(data);

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

it("should convert tagged Uint32Array", function () {
const data = hexToBuffer("d8464c010000000200000003000000");
const msg = CBOR.decode(data, cborTypedArrayTagger);
const msg = decode(data);

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

it("should convert tagged Uint64Array", function () {
it("should convert tagged BigUint64Array", function () {
const data = hexToBuffer(
"d8475818010000000000000002000000000000000300000000000000",
);
const msg = CBOR.decode(data, cborTypedArrayTagger);
const msg = decode(data);

if (!Array.isArray(msg)) {
throw new Error("Expected Array");
if (!(msg instanceof BigUint64Array)) {
throw new Error("Expected BigUint64Array");
}
expect(msg).to.have.lengthOf(3);
expect(msg[0]).to.equal(1);
expect(msg[1]).to.equal(2);
expect(msg[2]).to.equal(3);
expect(msg[0]).to.equal(BigInt(1));
expect(msg[1]).to.equal(BigInt(2));
expect(msg[2]).to.equal(BigInt(3));
});

it("should convert tagged Int8Array", function () {
const data = hexToBuffer("d8484301fe03");
const msg = CBOR.decode(data, cborTypedArrayTagger);
const msg = decode(data);

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

it("should convert tagged Int16Array", function () {
const data = hexToBuffer("d84d460100feff0300");
const msg = CBOR.decode(data, cborTypedArrayTagger);
const msg = decode(data);

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

it("should convert tagged Int32Array", function () {
const data = hexToBuffer("d84e4c01000000feffffff03000000");
const msg = CBOR.decode(data, cborTypedArrayTagger);
const msg = decode(data);

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

it("should convert tagged Int64Array", function () {
it("should convert tagged BigInt64Array", function () {
const data = hexToBuffer(
"d84f58180100000000000000feffffffffffffff0300000000000000",
);
const msg = CBOR.decode(data, cborTypedArrayTagger);
const msg = decode(data);

if (!Array.isArray(msg)) {
throw new Error("Expected Array");
if (!(msg instanceof BigInt64Array)) {
throw new Error("Expected BigInt64Array");
}
expect(msg).to.have.lengthOf(3);
expect(msg[0]).to.equal(1);
expect(msg[1]).to.equal(-2);
expect(msg[2]).to.equal(3);
expect(msg[0]).to.equal(BigInt(1));
expect(msg[1]).to.equal(BigInt(-2));
expect(msg[2]).to.equal(BigInt(3));
});

it("should convert tagged Float32Array", function () {
const data = hexToBuffer("d8554ccdcc8c3fcdcc0cc033335340");
const msg = CBOR.decode(data, cborTypedArrayTagger);
const msg = decode(data);

if (!(msg instanceof Float32Array)) {
throw new Error("Expected Float32Array");
Expand All @@ -127,7 +126,7 @@ describe("CBOR Typed Array Tagger", function () {
const data = hexToBuffer(
"d85658189a9999999999f13f9a999999999901c06666666666660a40",
);
const msg = CBOR.decode(data, cborTypedArrayTagger);
const msg = decode(data);

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

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

if (!Array.isArray(msg)) {
throw new Error("Expected Array");
Expand Down
7 changes: 4 additions & 3 deletions test/transport.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type {
RosbridgeMessage,
RosbridgePngMessage,
} from "../src/types/protocol.ts";
import CBOR from "cbor-js";
import { encode } from "cbor2";
import * as fastpng from "fast-png";
import * as bson from "bson";
import * as ws from "ws";
Expand Down Expand Up @@ -287,8 +287,9 @@ describe("Transport", () => {
});

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

// -- SUCCESS -- //

Expand Down