-
Notifications
You must be signed in to change notification settings - Fork 569
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3144d27
commit 123b68c
Showing
11 changed files
with
1,853 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,7 +17,6 @@ jobs: | |
strategy: | ||
matrix: | ||
node-version: | ||
- 10 | ||
- 18 | ||
|
||
steps: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { Transport } from "../transport"; | ||
import debugModule from "debug"; | ||
|
||
const debug = debugModule("engine:webtransport"); | ||
|
||
const BINARY_HEADER = Buffer.of(54); | ||
|
||
function shouldIncludeBinaryHeader(packet, encoded) { | ||
// 48 === "0".charCodeAt(0) (OPEN packet type) | ||
// 54 === "6".charCodeAt(0) (NOOP packet type) | ||
return ( | ||
packet.type === "message" && | ||
typeof packet.data !== "string" && | ||
encoded[0] >= 48 && | ||
encoded[0] <= 54 | ||
); | ||
} | ||
|
||
/** | ||
* Reference: https://developer.mozilla.org/en-US/docs/Web/API/WebTransport_API | ||
*/ | ||
export class WebTransport extends Transport { | ||
private readonly writer; | ||
|
||
constructor(private readonly session, stream, reader) { | ||
super({ _query: { EIO: "4" } }); | ||
this.writer = stream.writable.getWriter(); | ||
(async () => { | ||
let binaryFlag = false; | ||
while (true) { | ||
const { value, done } = await reader.read(); | ||
if (done) { | ||
debug("session is closed"); | ||
break; | ||
} | ||
debug("received chunk: %o", value); | ||
if (!binaryFlag && value.byteLength === 1 && value[0] === 54) { | ||
binaryFlag = true; | ||
continue; | ||
} | ||
this.onPacket( | ||
this.parser.decodePacketFromBinary(value, binaryFlag, "nodebuffer") | ||
); | ||
binaryFlag = false; | ||
} | ||
})(); | ||
|
||
session.closed.then(() => this.onClose()); | ||
|
||
this.writable = true; | ||
} | ||
|
||
get name() { | ||
return "webtransport"; | ||
} | ||
|
||
get supportsFraming() { | ||
return true; | ||
} | ||
|
||
send(packets) { | ||
this.writable = false; | ||
|
||
for (let i = 0; i < packets.length; i++) { | ||
const packet = packets[i]; | ||
const isLast = i + 1 === packets.length; | ||
|
||
this.parser.encodePacketToBinary(packet, (data) => { | ||
if (shouldIncludeBinaryHeader(packet, data)) { | ||
debug("writing binary header"); | ||
this.writer.write(BINARY_HEADER); | ||
} | ||
debug("writing chunk: %o", data); | ||
this.writer.write(data); | ||
if (isLast) { | ||
this.writable = true; | ||
this.emit("drain"); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
doClose(fn) { | ||
debug("closing WebTransport session"); | ||
this.session.close(); | ||
fn && fn(); | ||
} | ||
} |
Oops, something went wrong.