,
this._onClose = function() {
t.onClose({
code: 4,
reason: "connection closed"
})
}
}
close() {
this._transport.close(),
null !== this._onClose && this._onClose()
}
send(e, t, n) {
const i = {
session: t,
node: n,
data: e
}
, r = {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(i),
mode: "cors",
credentials: "same-origin"
};
(0,
this.options.fetch)(this.options.emulationEndpoint, r)
}
}
class WebtransportTransport {
constructor(e, t) {
this.endpoint = e,
this.options = t,
this._transport = null,
this._stream = null,
this._writer = null,
this._utf8decoder = new TextDecoder,
this._protocol = "json"
}
name() {
return "webtransport"
}
subName() {
return "webtransport"
}
emulation() {
return !1
}
supported() {
return void 0 !== this.options.webtransport && null !== this.options.webtransport
}
initialize(e, t) {
return __awaiter(this, void 0, void 0, (function*() {
let n;
n = globalThis && globalThis.document && globalThis.document.baseURI ? new URL(this.endpoint,globalThis.document.baseURI) : new URL(this.endpoint),
"protobuf" === e && n.searchParams.append("cf_protocol", "protobuf"),
this._protocol = e;
const i = new EventTarget;
this._transport = new this.options.webtransport(n.toString()),
this._transport.closed.then(( () => {
t.onClose({
code: 4,
reason: "connection closed"
})
}
)).catch(( () => {
t.onClose({
code: 4,
reason: "connection closed"
})
}
));
try {
yield this._transport.ready
} catch (e) {
return void this.close()
}
let r;
try {
r = yield this._transport.createBidirectionalStream()
} catch (e) {
return void this.close()
}
this._stream = r,
this._writer = this._stream.writable.getWriter(),
i.addEventListener("close", ( () => {
t.onClose({
code: 4,
reason: "connection closed"
})
}
)),
i.addEventListener("message", (e => {
t.onMessage(e.data)
}
)),
this._startReading(i),
t.onOpen()
}
))
}
_startReading(e) {
return __awaiter(this, void 0, void 0, (function*() {
const t = this._stream.readable.getReader();
let n = ""
, i = 0
, r = new Uint8Array;
try {
for (; ; ) {
const {done: s, value: o} = yield t.read();
if (o.length > 0)
if ("json" === this._protocol)
for (n += this._utf8decoder.decode(o); i < n.length; )
if ("\n" === n[i]) {
const t = n.substring(0, i);
e.dispatchEvent(new MessageEvent("message",{
data: t
})),
n = n.substring(i + 1),
i = 0
} else
++i;
else {
const t = new Uint8Array(r.length + o.length);
for (t.set(r),
t.set(o, r.length),
r = t; ; ) {
const t = this.options.decoder.decodeReply(r);
if (!t.ok)
break;
{
const n = r.slice(0, t.pos);
e.dispatchEvent(new MessageEvent("message",{
data: n
})),
r = r.slice(t.pos)
}
}
}
if (s)
break
}
} catch (t) {
e.dispatchEvent(new Event("close"))
}
}
))
}
close() {
return __awaiter(this, void 0, void 0, (function*() {
|