Skip to content
Myr

duplex.js

This file

Path

test/helpers/duplex.js

59 lines · 1.8 KiB · JavaScript

Last changed by

Not known.

History of this file

Take it

The walk stopped after 200 commits: lines older than 9f65b86 point at it.

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 1–59why, one step back
1import {Transform} from 'node:stream';
2import {setTimeout, scheduler} from 'node:timers/promises';3import {callbackify} from 'node:util';4import {foobarObject, foobarString} from './input.js';5import {casedSuffix, prefix, suffix} from './generator.js';67const getDuplex = (transform, encoding, outerObjectMode) => objectMode => ({8 transform: new Transform({9 transform: callbackify(async function (value) {10 return transform.call(this, value);11 }),12 objectMode,13 encoding,14 }),15 objectMode: outerObjectMode,16});1718export const addNoopDuplex = (duplex, addNoopTransform, objectMode) => addNoopTransform19 ? [duplex, noopDuplex(objectMode)]20 : [duplex];2122export const noopDuplex = getDuplex(value => value);2324export const serializeDuplex = getDuplex(object => JSON.stringify(object));2526export const getOutputDuplex = (input, outerObjectMode) => getDuplex(() => input, undefined, outerObjectMode);2728export const outputObjectDuplex = () => getOutputDuplex(foobarObject)(true);2930export const getOutputsDuplex = inputs => getDuplex(function () {31 for (const input of inputs) {32 this.push(input);33 }34});3536export const noYieldDuplex = getDuplex(() => {});3738export const multipleYieldDuplex = getDuplex(async function (line) {39 this.push(prefix);40 await scheduler.yield();41 this.push(line);42 await scheduler.yield();43 this.push(suffix);44});4546export const uppercaseEncodingDuplex = (encoding, outerObjectMode) => getDuplex(buffer => buffer.toString().toUpperCase(), encoding, outerObjectMode);4748export const uppercaseBufferDuplex = uppercaseEncodingDuplex();4950export const throwingDuplex = cause => getDuplex(() => {51 throw cause;52});5354export const appendDuplex = getDuplex(string => `${string}${casedSuffix}`);5556export const timeoutDuplex = timeout => getDuplex(async () => {57 await setTimeout(timeout);58 return foobarString;59});