Skip to content
Open
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
19 changes: 16 additions & 3 deletions examples/io/tcp/websocketclient/websocketclient.js
Original file line number Diff line number Diff line change
Expand Up @@ -541,11 +541,24 @@ class WebSocketClient {

message.push("", "");

//@@ if headers exceed count, send in pieces
message = ArrayBuffer.fromString(message.join("\r\n"));
options.request = new Uint8Array(ArrayBuffer.fromString(message.join("\r\n")));
this.#state = "sendRequest";
}
case "sendRequest": {
const options = this.#options;
const request = options.request;
const use = Math.min(this.#writable, request.byteLength);
if (!use)
break;
const message = request.subarray(0, use);
this.#writable = this.#socket.write(message);

this.#state = "receiveStatus"
if (use < request.byteLength) {
options.request = request.subarray(use);
break;
}
delete options.request;
this.#state = "receiveStatus";
this.#line = "";
options.flags = 0;
this.#socket.format = NumberFormat;
Expand Down
49 changes: 49 additions & 0 deletions tests/modules/network/websocket/client/request-backpressure.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*---
description: WebSocket upgrade request respects socket write capacity
flags: [async, module]
---*/
import Timer from "timer";
import WebSocketClient from "embedded:network/websocket/client";

const capacity = 64;
let client;
class Resolver {
resolve(options) {
options.onResolved(options.host, "127.0.0.1");
}
}
class Socket {
#options;
static writes = 0;

constructor(options) {
this.#options = options;
Timer.set(() => options.onWritable(capacity));
}
write(buffer) {
assert(buffer.byteLength <= capacity, "write exceeds reported capacity");
Socket.writes++;
const bytes = new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength);
const length = bytes.byteLength;
if ((length >= 4) && (13 === bytes[length - 4]) && (10 === bytes[length - 3]) &&
(13 === bytes[length - 2]) && (10 === bytes[length - 1])) {
Timer.set(() => $DO(() => {
assert(Socket.writes > 1, "request should be split across writes");
client.close();
})());
}
else
Timer.set(() => this.#options.onWritable(capacity));
return 0;
}
close() {}
set format(_) {}
}
$TESTMC.timeout(1_000);
client = new WebSocketClient({
host: "example.com",
headers: [["X-Padding", "x".repeat(160)]],
dns: {io: Resolver},
socket: {io: Socket},
onError() { $DONE("unexpected error"); }
});
2 changes: 2 additions & 0 deletions tools/testmc/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"$(MODULES)/data/base64/manifest.json",
"$(MODULES)/data/crc/manifest.json",
"$(MODULES)/data/hex/manifest.json",
"$(MODULES)/data/logical/manifest.json",
"$(MODULES)/data/qrcode/manifest.json",
"$(MODULES)/data/text/decoder/manifest.json",
"$(MODULES)/data/text/encoder/manifest.json",
Expand Down Expand Up @@ -42,6 +43,7 @@
"commodetto/*": "$(MODULES)/commodetto/commodettoPocoBlit",
"commodetto/cfe": "$(MODULES)/commodetto/cfeBMF",
"commodetto/checksumOut": "./commodettoChecksumOut",
"embedded:network/websocket/client": "$(MODDABLE)/examples/io/tcp/websocketclient/websocketclient",
"piu/Sound": "$(MODULES)/piu/MC/piuSound"
},
"preload": [
Expand Down