Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: initial polyfill functionality #173

Merged
merged 5 commits into from
Aug 8, 2023
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
408 changes: 4 additions & 404 deletions lib/index.d.ts

Large diffs are not rendered by default.

31 changes: 30 additions & 1 deletion package-lock.json

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

18 changes: 15 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,17 @@
"version": "0.4.3",
"description": "libdatachannel node bindings",
"type": "module",
"exports": "./lib/index.js",
"exports": {
".": {
"import": "./lib/index.js"
},
"./polyfill": {
"import": "./polyfill/polyfill.js"
},
"./ponyfill": {
"import": "./polyfill/ponyfill.js"
}
},
"typings": "lib/index.d.ts",
"engines": {
"node": ">=12.0.0"
Expand Down Expand Up @@ -70,9 +80,11 @@
"typescript": "^4.6.3"
},
"dependencies": {
"prebuild-install": "^7.1.1"
"p-defer": "^4.0.0",
"prebuild-install": "^7.1.1",
"uint8-util": "^2.2.2"
},
"bundleDependencies": [
"prebuild-install"
]
}
}
27 changes: 27 additions & 0 deletions polyfill/Events.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// @ts-check

export class PeerConnectionIceEvent extends Event {
#candidate = null;
get candidate() {
return this.#candidate;
}

constructor(candidate) {
super('icecandidate');

this.#candidate = candidate;
}
}

export class DataChannelEvent extends Event {
#channel = null;
get channel() {
return this.#channel;
}

constructor(channel) {
super('datachannel');

this.#channel = channel;
}
}
113 changes: 92 additions & 21 deletions polyfill/RTCDataChannel.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,75 @@
import DOMException from 'node-domexception';
// @ts-check
import { text2arr } from 'uint8-util';
// DOMException has existed for quite a while, but was only exposed in node 17 and above, this is a hack to get it early in engines 10 or above tho we only support since node 15 for event target

// eslint-disable-next-line @typescript-eslint/no-empty-function
const noop = () => {};

export default class RTCDataChannel extends EventTarget {
constructor() {
/**
* @class
* @implements {RTCDataChannel}
*/
export default class extends EventTarget {
/**
* @param {import("../lib/index.d.ts").DataChannel} dataChannel
* @param {any} opts
*/
constructor(dataChannel, opts = {}) {
super();

this.#dataChannel = dataChannel;
this.#readyState = 'connecting';
this.#bufferedAmountLowThreshold = 0;

this.#binaryType = 'arraybuffer';

this.#dataChannel.onOpen(() => {
this.#readyState = 'open';
this.dispatchEvent(new Event('open'));
});
this.#dataChannel.onClosed(() => {
this.#readyState = 'closed';
this.dispatchEvent(new Event('close'));
});
this.#dataChannel.onError((/** @type {string | undefined} */ msg) => {
this.#readyState = 'closed';
this.dispatchEvent(
new RTCErrorEvent('error', {
error: new RTCError(
{
errorDetail: 'data-channel-failure',
},
msg,
),
}),
);
});
this.#dataChannel.onBufferedAmountLow(() => {
this.dispatchEvent(new Event('bufferedamountlow'));
});
this.#dataChannel.onMessage((/** @type {string | Uint8Array} */ data) => {
if (typeof data === 'string') {
data = text2arr(data);
}

this.dispatchEvent(new MessageEvent('message', { data }));
});

// forward events to properties
this.addEventListener('message', this.onmessage);
this.addEventListener('bufferedamountlow', this.onbufferedamountlow);
this.addEventListener('error', this.onerror);
this.addEventListener('close', this.onclose);
this.addEventListener('closing', this.onclosing);
this.addEventListener('open', this.onopen);

this.#maxPacketLifeTime = opts.maxPacketLifeTime ?? null;
this.#maxRetransmits = opts.maxRetransmits ?? null;
this.#negotiated = opts.negotiated ?? false;
this.#ordered = opts.ordered ?? true;
}

#dataChannel;
/** @type {BinaryType} */
#binaryType = 'blob';

set binaryType(type) {
Expand All @@ -24,10 +85,8 @@ export default class RTCDataChannel extends EventTarget {
return this.#binaryType;
}

#bufferedAmount = 0;

get bufferedAmount() {
return this.#bufferedAmount;
return this.#dataChannel.bufferedAmount();
}

#bufferedAmountLowThreshold = 0;
Expand All @@ -39,18 +98,15 @@ export default class RTCDataChannel extends EventTarget {
set bufferedAmountLowThreshold(value) {
const number = Number(value) || 0;
this.#bufferedAmountLowThreshold = number;
this.#dataChannel.setBufferedAmountLowThreshold(number);
}

#id = 0;

get id() {
return this.#id;
return this.#dataChannel.getId();
}

#label = 0;

get label() {
return this.#label;
return this.#dataChannel.getLabel();
}

#maxPacketLifeTime = 0;
Expand All @@ -65,7 +121,7 @@ export default class RTCDataChannel extends EventTarget {
return this.#maxRetransmits;
}

#negotiated = 0;
#negotiated = false;

get negotiated() {
return this.#negotiated;
Expand All @@ -79,20 +135,19 @@ export default class RTCDataChannel extends EventTarget {
onmessage = noop;
onopen = noop;

#ordered = 0;
#ordered = false;

get ordered() {
return this.#ordered;
}

#protocol = 0;

get protocol() {
return this.#protocol;
return this.#dataChannel.getProtocol();
}

// connecting, open, closing, or closed
#readyState = 0;
/** @type {RTCDataChannelState} */
#readyState = 'connecting';

get readyState() {
return this.#readyState;
Expand All @@ -105,7 +160,23 @@ export default class RTCDataChannel extends EventTarget {
'InvalidStateError',
);
}
// Needs network error, type error implemented and strict data type checking.
// This may be a string, a Blob, an ArrayBuffer, a TypedArray or a DataView object.

// Needs network error, type error implemented
if (typeof data === 'string') {
this.#dataChannel.sendMessage(data);
} else if (data instanceof Blob) {
data.arrayBuffer().then((ab) => {
this.#dataChannel.sendMessageBinary(new Uint8Array(ab));
});
} else {
this.#dataChannel.sendMessageBinary(new Uint8Array(data));
}
}

close() {
this.#readyState = 'closing';
this.dispatchEvent(new Event('closing'));

this.#dataChannel.close();
}
}
Loading