/
githubmirror
/
deno
Обзор
Документация
Войти
/
githubmirror
/
deno
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
ext/node/polyfills/internal/streams/duplexpair.js
69 строк
2 KB
Bartek Iwańczuk
perf(ext/node): replace node: imports with core.loadExtScript for buffer and nextTick (#33862)
06 май 2026, 12:39
Не верифицирован
06 май 2026, 12:39
e481bf7
Код
Авторство
О чём код?
// deno-lint-ignore-file // Copyright 2018-2026 the Deno authors. MIT license. import process from "node:process"; import { core, primordials } from "ext:core/mod.js"; import { Duplex } from "node:stream"; const assert = core.loadExtScript( "ext:deno_node/internal/assert.mjs", ); "use strict"; const { Symbol, } = primordials; const kCallback = Symbol("Callback"); const kInitOtherSide = Symbol("InitOtherSide"); class DuplexSide extends Duplex { #otherSide = null; constructor(options) { super(options); this[kCallback] = null; this.#otherSide = null; } [kInitOtherSide](otherSide) { // Ensure this can only be set once, to enforce encapsulation. if (this.#otherSide === null) { this.#otherSide = otherSide; } else { assert(this.#otherSide === null); } } _read() { const callback = this[kCallback]; if (callback) { this[kCallback] = null; callback(); } } _write(chunk, encoding, callback) { assert(this.#otherSide !== null); assert(this.#otherSide[kCallback] === null); if (chunk.length === 0) { process.nextTick(callback); } else { this.#otherSide.push(chunk); this.#otherSide[kCallback] = callback; } } _final(callback) { this.#otherSide.on("end", callback); this.#otherSide.push(null); } } function duplexPair(options) { const side0 = new DuplexSide(options); const side1 = new DuplexSide(options); side0[kInitOtherSide](side1); side1[kInitOtherSide](side0); return [side0, side1]; } export default duplexPair; export { duplexPair };