Skip to content

Commit fcffbf2

Browse files
committed
feat: MsgPack/Cbor serialization
1 parent 64f962b commit fcffbf2

28 files changed

+608
-104
lines changed

e2e/datachannel/Int32Array.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { int32_arrays } from "../data.js";
2+
import { expect } from "https://esm.sh/v126/[email protected]/X-dHMvZXhwZWN0/es2021/chai.bundle.mjs";
3+
4+
/** @param {unknown[]} received */
5+
export const check = (received) => {
6+
for (const [i, typed_array] of int32_arrays.entries()) {
7+
expect(received[i]).to.be.an.instanceof(Int32Array);
8+
expect(received[i]).to.deep.equal(typed_array);
9+
}
10+
};
11+
/**
12+
* @param {import("../peerjs").DataConnection} dataConnection
13+
*/
14+
export const send = (dataConnection) => {
15+
for (const typed_array of int32_arrays) {
16+
dataConnection.send(typed_array);
17+
}
18+
};
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { int32_arrays } from "../data.js";
2+
import { expect } from "https://esm.sh/v126/[email protected]/X-dHMvZXhwZWN0/es2021/chai.bundle.mjs";
3+
4+
/** @param {unknown[]} received */
5+
export const check = (received) => {
6+
for (const [i, typed_array] of int32_arrays.entries()) {
7+
expect(received[i]).to.be.an.instanceof(Uint8Array);
8+
expect(received[i]).to.deep.equal(new Uint8Array(typed_array.buffer));
9+
}
10+
};
11+
/**
12+
* @param {import("../peerjs").DataConnection} dataConnection
13+
*/
14+
export const send = (dataConnection) => {
15+
for (const typed_array of int32_arrays) {
16+
dataConnection.send(typed_array);
17+
}
18+
};

e2e/datachannel/Uint8Array.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { uint8_arrays } from "../data.js";
2+
import { expect } from "https://esm.sh/v126/[email protected]/X-dHMvZXhwZWN0/es2021/chai.bundle.mjs";
3+
4+
/** @param {unknown[]} received */
5+
export const check = (received) => {
6+
for (const [i, typed_array] of uint8_arrays.entries()) {
7+
expect(received[i]).to.be.an.instanceof(Uint8Array);
8+
expect(received[i]).to.deep.equal(typed_array);
9+
}
10+
};
11+
/**
12+
* @param {import("../peerjs").DataConnection} dataConnection
13+
*/
14+
export const send = (dataConnection) => {
15+
for (const typed_array of uint8_arrays) {
16+
dataConnection.send(typed_array);
17+
}
18+
};
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { array_buffers } from "../data.js";
2+
import { expect } from "https://esm.sh/v126/[email protected]/X-dHMvZXhwZWN0/es2021/chai.bundle.mjs";
3+
4+
/** @param {unknown[]} received */
5+
export const check = (received) => {
6+
for (const [i, array_buffer] of array_buffers.entries()) {
7+
expect(received[i]).to.be.an.instanceof(Uint8Array);
8+
expect(received[i]).to.deep.equal(new Uint8Array(array_buffer));
9+
}
10+
};
11+
/**
12+
* @param {import("../peerjs").DataConnection} dataConnection
13+
*/
14+
export const send = (dataConnection) => {
15+
for (const array_buffer of array_buffers) {
16+
dataConnection.send(array_buffer);
17+
}
18+
};

e2e/datachannel/dates.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { dates } from "../data.js";
2+
import { expect } from "https://esm.sh/v126/[email protected]/X-dHMvZXhwZWN0/es2021/chai.bundle.mjs";
3+
4+
/** @param {unknown[]} received */
5+
export const check = (received) => {
6+
expect(received).to.deep.equal(dates);
7+
};
8+
/**
9+
* @param {import("../peerjs").DataConnection} dataConnection
10+
*/
11+
export const send = (dataConnection) => {
12+
for (const date of dates) {
13+
dataConnection.send(date);
14+
}
15+
};

e2e/datachannel/serialization.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,20 @@ const params = new URLSearchParams(document.location.search);
77
const testfile = params.get("testfile");
88
const serialization = params.get("serialization");
99

10-
const serializers = {};
10+
(async () => {
11+
let serializers = {};
12+
try {
13+
const { Cbor } = await import("/dist/serializer.cbor.mjs");
14+
const { MsgPack } = await import("/dist/serializer.msgpack.mjs");
15+
serializers = {
16+
Cbor,
17+
MsgPack,
18+
};
19+
} catch (e) {
20+
console.log(e);
21+
}
1122

12-
import(`./${testfile}.js`).then(({ check, send }) => {
23+
const { check, send } = await import(`./${testfile}.js`);
1324
document.getElementsByTagName("title")[0].innerText =
1425
window.location.hash.substring(1);
1526

@@ -80,4 +91,4 @@ import(`./${testfile}.js`).then(({ check, send }) => {
8091
messages.textContent = "Sent!";
8192
});
8293
window["connect-btn"].disabled = false;
83-
});
94+
})();
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import P from "./serialization.page.js";
2+
import { serializationTest } from "./serializationTest.js";
3+
import { browser, $, $$, expect } from "@wdio/globals";
4+
5+
describe("DataChannel:CBOR", function () {
6+
beforeAll(async function () {
7+
await P.init();
8+
});
9+
beforeEach(async function () {
10+
if (
11+
// @ts-ignore
12+
browser.capabilities.browserName === "firefox" &&
13+
// @ts-ignore
14+
parseInt(browser.capabilities.browserVersion) < 102
15+
) {
16+
pending("Firefox 102+ required for Streams");
17+
}
18+
});
19+
it("should transfer numbers", serializationTest("./numbers", "Cbor"));
20+
it("should transfer strings", serializationTest("./strings", "Cbor"));
21+
it("should transfer long string", serializationTest("./long_string", "Cbor"));
22+
it("should transfer objects", serializationTest("./objects", "Cbor"));
23+
it("should transfer arrays", serializationTest("./arrays", "Cbor"));
24+
it("should transfer dates", serializationTest("./dates", "Cbor"));
25+
it(
26+
"should transfer ArrayBuffers as Uint8Arrays",
27+
serializationTest("./arraybuffers_as_uint8array", "Cbor"),
28+
);
29+
it(
30+
"should transfer TypedArrayView",
31+
serializationTest("./typed_array_view", "Cbor"),
32+
);
33+
it("should transfer Uint8Arrays", serializationTest("./Uint8Array", "Cbor"));
34+
it("should transfer Int32Arrays", serializationTest("./Int32Array", "Cbor"));
35+
});
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import P from "./serialization.page.js";
2+
import { serializationTest } from "./serializationTest.js";
3+
import { browser } from "@wdio/globals";
4+
5+
describe("DataChannel:MsgPack", function () {
6+
beforeAll(async function () {
7+
await P.init();
8+
});
9+
beforeEach(async function () {
10+
if (
11+
// @ts-ignore
12+
browser.capabilities.browserName === "firefox" &&
13+
// @ts-ignore
14+
parseInt(browser.capabilities.browserVersion) < 102
15+
) {
16+
pending("Firefox 102+ required for Streams");
17+
}
18+
});
19+
it("should transfer numbers", serializationTest("./numbers", "MsgPack"));
20+
it("should transfer strings", serializationTest("./strings", "MsgPack"));
21+
it(
22+
"should transfer long string",
23+
serializationTest("./long_string", "MsgPack"),
24+
);
25+
it("should transfer objects", serializationTest("./objects", "MsgPack"));
26+
it("should transfer arrays", serializationTest("./arrays", "MsgPack"));
27+
it(
28+
"should transfer Dates as strings",
29+
serializationTest("./dates", "MsgPack"),
30+
);
31+
// it("should transfer ArrayBuffers", serializationTest("./arraybuffers", "MsgPack"));
32+
it(
33+
"should transfer TypedArrayView",
34+
serializationTest("./typed_array_view", "MsgPack"),
35+
);
36+
it(
37+
"should transfer Uint8Arrays",
38+
serializationTest("./Uint8Array", "MsgPack"),
39+
);
40+
it(
41+
"should transfer Int32Arrays as Uint8Arrays",
42+
serializationTest("./Int32Array_as_Uint8Array", "MsgPack"),
43+
);
44+
});

lib/api.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { util } from "./util";
22
import logger from "./logger";
3-
import { PeerJSOption } from "./optionInterfaces";
3+
import type { PeerJSOption } from "./optionInterfaces";
44
import { version } from "../package.json";
55

66
export class API {

lib/baseconnection.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { EventEmitter, ValidEventTypes } from "eventemitter3";
2-
import { Peer } from "./peer";
3-
import { ServerMessage } from "./servermessage";
4-
import { ConnectionType } from "./enums";
2+
import type { Peer } from "./peer";
3+
import type { ServerMessage } from "./servermessage";
4+
import type { ConnectionType } from "./enums";
55

66
export type BaseConnectionEvents = {
77
/**

0 commit comments

Comments
 (0)