GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — test: add `makeDuplexPair()` helper · nodejs/node@af3e074

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • test/common
    • README.md
    • duplexpair.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎test/common/README.md‎

Copy file name to clipboardExpand all lines: test/common/README.md
+9Lines changed: 9 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ This directory contains modules used to test the Node.js implementation.
55
## Table of Contents
66

77
* [Common module API](#common-module-api)
8+
* [Duplex pair helper](#duplex-pair-helper)
89
* [WPT module](#wpt-module)
910

1011
## Common Module API
@@ -318,6 +319,14 @@ Decrements the `Countdown` counter.
318319
Specifies the remaining number of times `Countdown.prototype.dec()` must be
319320
called before the callback is invoked.
320321

322+
## Duplex pair helper
323+
324+
The `common/duplexpair` module exports a single function `makeDuplexPair`,
325+
which returns an object `{ clientSide, serverSide }` where each side is a
326+
`Duplex` stream connected to the other side.
327+
328+
There is no difference between client or server side beyond their names.
329+
321330
## Fixtures Module
322331

323332
The `common/fixtures` module provides convenience methods for working with
Collapse file

‎test/common/duplexpair.js‎

Copy file name to clipboard
+45Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/* eslint-disable required-modules */
2+
'use strict';
3+
const { Duplex } = require('stream');
4+
const assert = require('assert');
5+
6+
const kCallback = Symbol('Callback');
7+
const kOtherSide = Symbol('Other');
8+
9+
class DuplexSocket extends Duplex {
10+
constructor() {
11+
super();
12+
this[kCallback] = null;
13+
this[kOtherSide] = null;
14+
}
15+
16+
_read() {
17+
const callback = this[kCallback];
18+
if (callback) {
19+
this[kCallback] = null;
20+
callback();
21+
}
22+
}
23+
24+
_write(chunk, encoding, callback) {
25+
assert.notStrictEqual(this[kOtherSide], null);
26+
assert.strictEqual(this[kOtherSide][kCallback], null);
27+
this[kOtherSide][kCallback] = callback;
28+
this[kOtherSide].push(chunk);
29+
}
30+
31+
_final(callback) {
32+
this[kOtherSide].on('end', callback);
33+
this[kOtherSide].push(null);
34+
}
35+
}
36+
37+
function makeDuplexPair() {
38+
const clientSide = new DuplexSocket();
39+
const serverSide = new DuplexSocket();
40+
clientSide[kOtherSide] = serverSide;
41+
serverSide[kOtherSide] = clientSide;
42+
return { clientSide, serverSide };
43+
}
44+
45+
module.exports = makeDuplexPair;

0 commit comments

Comments
0 (0)
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.