summaryrefslogtreecommitdiff
path: root/ext/websocket/01_websocket.js
diff options
context:
space:
mode:
authorLeo Kettmeir <crowlkats@toaxl.com>2023-01-20 17:20:14 +0100
committerGitHub <noreply@github.com>2023-01-20 17:20:14 +0100
commit638b6ef554676422c43cc5c0ae2285ba369740bf (patch)
treef7308f8a02773bde4fbf4a2aa442eb6ca368761d /ext/websocket/01_websocket.js
parentda23f7f8763cab90fbbf37d9c7cddf3758d7d364 (diff)
Revert "perf(ext/websocket): optimize socket.send (#16320)" (#17480)
This reverts commit 36307c45
Diffstat (limited to 'ext/websocket/01_websocket.js')
-rw-r--r--ext/websocket/01_websocket.js74
1 files changed, 28 insertions, 46 deletions
diff --git a/ext/websocket/01_websocket.js b/ext/websocket/01_websocket.js
index 0d0a4211a..9b7c45e70 100644
--- a/ext/websocket/01_websocket.js
+++ b/ext/websocket/01_websocket.js
@@ -26,6 +26,7 @@
ArrayPrototypeJoin,
ArrayPrototypeMap,
ArrayPrototypeSome,
+ DataView,
ErrorPrototypeToString,
ObjectDefineProperties,
ObjectPrototypeIsPrototypeOf,
@@ -34,14 +35,13 @@
Set,
// TODO(lucacasonato): add SharedArrayBuffer to primordials
// SharedArrayBufferPrototype
+ String,
StringPrototypeEndsWith,
StringPrototypeToLowerCase,
Symbol,
SymbolIterator,
PromisePrototypeCatch,
- queueMicrotask,
SymbolFor,
- Uint8Array,
} = window.__bootstrap.primordials;
webidl.converters["sequence<DOMString> or DOMString"] = (V, opts) => {
@@ -300,58 +300,40 @@
throw new DOMException("readyState not OPEN", "InvalidStateError");
}
- if (typeof data === "string") {
- // try to send in one go!
- const d = core.byteLength(data);
- const sent = ops.op_ws_try_send_string(this[_rid], data);
- this[_bufferedAmount] += d;
- if (!sent) {
- PromisePrototypeThen(
- core.opAsync("op_ws_send_string", this[_rid], data),
- () => {
- this[_bufferedAmount] -= d;
- },
- );
- } else {
- // Spec expects data to be start flushing on next tick but oh well...
- // we already sent it so we can just decrement the bufferedAmount
- // on the next tick.
- queueMicrotask(() => {
- this[_bufferedAmount] -= d;
- });
- }
- return;
- }
-
const sendTypedArray = (ta) => {
- // try to send in one go!
- const sent = ops.op_ws_try_send_binary(this[_rid], ta);
this[_bufferedAmount] += ta.byteLength;
- if (!sent) {
- PromisePrototypeThen(
- core.opAsync("op_ws_send_binary", this[_rid], ta),
- () => {
- this[_bufferedAmount] -= ta.byteLength;
- },
- );
- } else {
- // Spec expects data to be start flushing on next tick but oh well...
- // we already sent it so we can just decrement the bufferedAmount
- // on the next tick.
- queueMicrotask(() => {
+ PromisePrototypeThen(
+ core.opAsync("op_ws_send", this[_rid], {
+ kind: "binary",
+ value: ta,
+ }),
+ () => {
this[_bufferedAmount] -= ta.byteLength;
- });
- }
+ },
+ );
};
- if (ObjectPrototypeIsPrototypeOf(ArrayBufferPrototype, data)) {
- sendTypedArray(new Uint8Array(data));
+ if (ObjectPrototypeIsPrototypeOf(BlobPrototype, data)) {
+ PromisePrototypeThen(
+ data.slice().arrayBuffer(),
+ (ab) => sendTypedArray(new DataView(ab)),
+ );
} else if (ArrayBufferIsView(data)) {
sendTypedArray(data);
- } else if (ObjectPrototypeIsPrototypeOf(BlobPrototype, data)) {
+ } else if (ObjectPrototypeIsPrototypeOf(ArrayBufferPrototype, data)) {
+ sendTypedArray(new DataView(data));
+ } else {
+ const string = String(data);
+ const d = core.encode(string);
+ this[_bufferedAmount] += d.byteLength;
PromisePrototypeThen(
- data.slice().arrayBuffer(),
- (ab) => sendTypedArray(new Uint8Array(ab)),
+ core.opAsync("op_ws_send", this[_rid], {
+ kind: "text",
+ value: string,
+ }),
+ () => {
+ this[_bufferedAmount] -= d.byteLength;
+ },
);
}
}