Skip to content

Commit e4d9fce

Browse files
chore: migrate to TypeScript
This change introduces an ESM build which will allow tree shaking. A CJS build is also provided for backward compatibility.
1 parent 78cd3bc commit e4d9fce

21 files changed

+585
-1522
lines changed

.eslintrc.json

-8
This file was deleted.

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
node_modules
22
.nyc_output
3+
build/

lib/commons.js renamed to lib/commons.ts

+1-5
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,4 @@ Object.keys(PACKET_TYPES).forEach(key => {
1414

1515
const ERROR_PACKET = { type: "error", data: "parser error" };
1616

17-
module.exports = {
18-
PACKET_TYPES,
19-
PACKET_TYPES_REVERSE,
20-
ERROR_PACKET
21-
};
17+
export { PACKET_TYPES, PACKET_TYPES_REVERSE, ERROR_PACKET };

lib/decodePacket.browser.js renamed to lib/decodePacket.browser.ts

+5-9
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
1-
const { PACKET_TYPES_REVERSE, ERROR_PACKET } = require("./commons");
1+
import { ERROR_PACKET, PACKET_TYPES_REVERSE } from "./commons.js";
2+
import { decode } from "base64-arraybuffer";
23

34
const withNativeArrayBuffer = typeof ArrayBuffer === "function";
45

5-
let base64decoder;
6-
if (withNativeArrayBuffer) {
7-
base64decoder = require("base64-arraybuffer");
8-
}
9-
106
const decodePacket = (encodedPacket, binaryType) => {
117
if (typeof encodedPacket !== "string") {
128
return {
@@ -36,8 +32,8 @@ const decodePacket = (encodedPacket, binaryType) => {
3632
};
3733

3834
const decodeBase64Packet = (data, binaryType) => {
39-
if (base64decoder) {
40-
const decoded = base64decoder.decode(data);
35+
if (withNativeArrayBuffer) {
36+
const decoded = decode(data);
4137
return mapBinary(decoded, binaryType);
4238
} else {
4339
return { base64: true, data }; // fallback for old browsers
@@ -54,4 +50,4 @@ const mapBinary = (data, binaryType) => {
5450
}
5551
};
5652

57-
module.exports = decodePacket;
53+
export default decodePacket;

lib/decodePacket.js renamed to lib/decodePacket.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
const { PACKET_TYPES_REVERSE, ERROR_PACKET } = require("./commons");
1+
import { ERROR_PACKET, PACKET_TYPES_REVERSE } from "./commons.js";
22

3-
const decodePacket = (encodedPacket, binaryType) => {
3+
const decodePacket = (encodedPacket, binaryType?) => {
44
if (typeof encodedPacket !== "string") {
55
return {
66
type: "message",
@@ -48,4 +48,4 @@ const toArrayBuffer = buffer => {
4848
return arrayBuffer;
4949
};
5050

51-
module.exports = decodePacket;
51+
export default decodePacket;

lib/encodePacket.browser.js renamed to lib/encodePacket.browser.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { PACKET_TYPES } = require("./commons");
1+
import { PACKET_TYPES } from "./commons.js";
22

33
const withNativeBlob =
44
typeof Blob === "function" ||
@@ -37,10 +37,10 @@ const encodePacket = ({ type, data }, supportsBinary, callback) => {
3737
const encodeBlobAsBase64 = (data, callback) => {
3838
const fileReader = new FileReader();
3939
fileReader.onload = function() {
40-
const content = fileReader.result.split(",")[1];
40+
const content = (fileReader.result as string).split(",")[1];
4141
callback("b" + content);
4242
};
4343
return fileReader.readAsDataURL(data);
4444
};
4545

46-
module.exports = encodePacket;
46+
export default encodePacket;

lib/encodePacket.js renamed to lib/encodePacket.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { PACKET_TYPES } = require("./commons");
1+
import { PACKET_TYPES } from "./commons.js";
22

33
const encodePacket = ({ type, data }, supportsBinary, callback) => {
44
if (data instanceof ArrayBuffer || ArrayBuffer.isView(data)) {
@@ -24,4 +24,4 @@ const encodeBuffer = (data, supportsBinary) => {
2424
return supportsBinary ? data : "b" + data.toString("base64");
2525
};
2626

27-
module.exports = encodePacket;
27+
export default encodePacket;

lib/index.js renamed to lib/index.ts

+5-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const encodePacket = require("./encodePacket");
2-
const decodePacket = require("./decodePacket");
1+
import encodePacket from "./encodePacket.js";
2+
import decodePacket from "./decodePacket.js";
33

44
const SEPARATOR = String.fromCharCode(30); // see https://en.wikipedia.org/wiki/Delimiter#ASCII_delimited_text
55

@@ -20,7 +20,7 @@ const encodePayload = (packets, callback) => {
2020
});
2121
};
2222

23-
const decodePayload = (encodedPayload, binaryType) => {
23+
const decodePayload = (encodedPayload, binaryType?) => {
2424
const encodedPackets = encodedPayload.split(SEPARATOR);
2525
const packets = [];
2626
for (let i = 0; i < encodedPackets.length; i++) {
@@ -33,10 +33,5 @@ const decodePayload = (encodedPayload, binaryType) => {
3333
return packets;
3434
};
3535

36-
module.exports = {
37-
protocol: 4,
38-
encodePacket,
39-
encodePayload,
40-
decodePacket,
41-
decodePayload
42-
};
36+
export const protocol = 5;
37+
export { encodePacket, encodePayload, decodePacket, decodePayload };

0 commit comments

Comments
 (0)