const channels = globalThis.__fetchletChannels;
function connect(hostname, port, secureTransport = "off") {
const record = {
messages: [],
opened: false,
secured: false,
ended: false,
refused: null,
close: null,
};
let wake = () => {};
record.changed = () =>
new Promise((resolve) => {
wake = resolve;
});
const notify = () => wake();
record.id = channels.connect(
{ hostname, port, secureTransport },
{
open() {
record.opened = true;
notify();
},
secure() {
record.secured = true;
notify();
},
end() {
record.ended = true;
notify();
},
message(data) {
record.messages.push(typeof data === "string" ? data : Array.from(data));
notify();
},
close(code, reason) {
record.close = { code, reason };
notify();
},
refused(message) {
record.refused = message;
notify();
},
},
);
return record;
}
async function until(record, done) {
while (!done()) {
await record.changed();
}
}
const text = (pieces) => String.fromCharCode(...pieces.flat());
const ascii = (string) => Uint8Array.from(string, (character) => character.charCodeAt(0));
globalThis.fetchletExports = {
async exchange(hostname, port, message, expected, secureTransport) {
const record = connect(hostname, port, secureTransport);
await until(record, () => record.opened || record.refused !== null);
if (record.refused !== null) {
return { refused: record.refused };
}
await channels.write(record.id, ascii(message));
await until(record, () => record.close !== null || record.messages.flat().length >= expected);
channels.close(record.id, 1000, "");
await until(record, () => record.close !== null);
return { received: text(record.messages), close: record.close };
},
async drain(hostname, port) {
const record = connect(hostname, port);
await until(record, () => record.close !== null || record.refused !== null);
return { received: text(record.messages), close: record.close, refused: record.refused };
},
async leave(hostname, port) {
const record = connect(hostname, port);
await until(record, () => record.opened);
channels.send(record.id, ascii("left open"));
return "returned";
},
async signals(hostname, port) {
const record = connect(hostname, port, "starttls");
await until(record, () => record.opened);
await channels.write(record.id, ascii("a"));
await until(record, () => record.messages.length === 1);
channels.startTls(record.id);
await until(record, () => record.secured);
await channels.write(record.id, ascii("b"));
await until(record, () => record.messages.length === 2);
channels.endWrites(record.id);
await until(record, () => record.close !== null);
return {
received: record.messages.map((message) => text([message])),
secured: record.secured,
ended: record.ended,
close: record.close,
};
},
async misplaced(hostname, port) {
const code = (operation) => {
try {
operation();
return "accepted";
} catch (error) {
return error.code;
}
};
const result = {};
const starting = connect(hostname, port, "starttls");
result.beforeOpen = code(() => channels.startTls(starting.id));
await until(starting, () => starting.opened);
channels.startTls(starting.id);
result.twice = code(() => channels.startTls(starting.id));
const plain = connect(hostname, port, "off");
await until(plain, () => plain.opened);
result.plain = code(() => channels.startTls(plain.id));
const ending = connect(hostname, port, "starttls");
await until(ending, () => ending.opened);
channels.endWrites(ending.id);
result.afterEnd = code(() => channels.startTls(ending.id));
result.endTwice = code(() => channels.endWrites(ending.id));
const channel = channels.open("ECHO", null, {
open() {},
message() {},
close() {},
});
result.channel = code(() => channels.endWrites(channel));
return result;
},
code(hostname, port, secureTransport) {
try {
connect(hostname, port, secureTransport);
return "connected";
} catch (error) {
return error.code;
}
},
async refused(hostname, port, secureTransport) {
const record = connect(hostname, port, secureTransport);
await until(record, () => record.opened || record.refused !== null);
return { opened: record.opened, refused: record.refused };
},
};