CodeSampleX

Exemple

duplexer2 0.1.4: combine writable and readable streams into a duplex stream

Échantillon vérifié pour npm duplexer2 0.1.4: combine writable and readable streams into a duplex stream. Le contrat s'est exécuté sur node 22 · linux…

sha256:f44aa117f492da0969fb29831feb2d5504f61174c87389fdf7ae14aa10d43c8f

Ce réseau offre une seule chose : un échantillon qui compile. Il l'a exécuté dans un bac à sable et conservé le reçu signé. Il ne note rien et ne garantit rien : si le même code compile chez vous, il ne l'a pas mesuré. Combien de clés de signature distinctes ont déposé un reçu de contrat réussi. Une seule, c'est l'auteur ; plus d'une signifie que quelqu'un d'autre l'a compilé aussi. Une clé est auto-générée sans identité enregistrée derrière, donc on compte des clés, pas des personnes. MIT-0

Preuves d'exécution

L'environnement déclaré et les exécutions signées sont séparés, pour que vous voyiez exactement ce que cet échantillon a exécuté et où.

Base de preuve
Contrat signé réussi
Reçus de vérification
1
Clés de signature qui l’ont compilé
1
Environnement déclaré linux 24 · ubuntu · glibc 2.39 x64 npm

Environnements des exécutions de vérification

Environnement Contrat Étapes Exécution
node 22 · linux alpine/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1node:22-alpine@sha256:c610fcdfb1d5…
2026-08-27

Cas

HOW
Objectif
combine writable and readable streams into a duplex stream
Paquets
Symboles
  • duplexer2
Créé
2026-08-27T20:20:00Z

Contrat

  1. duplexer2(writable, readable) returns a Duplex stream combining writable and readable streams
  2. writing data to duplex stream forwards chunks to the underlying writable stream
  3. reading data from duplex stream pulls chunks from the underlying readable stream
  4. duplexer2 bubbles error events from writable stream to duplex stream by default
  5. duplexer2 bubbles error events from readable stream to duplex stream by default
  6. duplexer2 suppresses error bubbling when bubbleErrors option is false
  7. duplexer2 supports stream options such as objectMode

Fichiers

  • PROMPT.md
  • csx.json
  • package-lock.json
  • package.json
  • spec.json
  • test/contract.js

Télécharger l’artefact source (tar.gz)

Code source

PROMPT.md
Clean-room public code sample — generation instructions

Write a brand-new, minimal, self-contained code sample in this clean-room directory.
Do not copy, paraphrase, or reference any existing project source. Work only from this spec.

A csx.json manifest scaffold already exists. Do not recreate it from memory. Preserve its case.goal, packages and symbols; fill its empty case.contract with exact assertions and correct its environment, commands and verifierAdapter for the files you generate.

Goal: combine writable and readable streams into a duplex stream
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/duplexer2@0.1.4
Demonstrate these symbols/APIs:
  - duplexer2

Rules:
  - One focused purpose; the smallest project that proves the goal.
  - Include a contract test (test/contract.*) that runs OFFLINE and exits 0 exactly when the goal behavior works.
  - Pin every dependency with a lockfile so resolution is reproducible.
  - No secrets, credentials, or tokens. No real URLs (only example.com or localhost). No absolute paths.
  - No personal names, emails, company names, or project identifiers of any kind.
  - No binaries and no generated output (node_modules, dist, target, venv, .git, .env).
  - Keep it under 200 files and 256KB packed.
csx.json
{"case":{"caseId":"case:sha256:64c1df41a596c65714f32cc05aed8988b47d13a930b1845af74394adda18c843","contract":["duplexer2(writable, readable) returns a Duplex stream combining writable and readable streams","writing data to duplex stream forwards chunks to the underlying writable stream","reading data from duplex stream pulls chunks from the underlying readable stream","duplexer2 bubbles error events from writable stream to duplex stream by default","duplexer2 bubbles error events from readable stream to duplex stream by default","duplexer2 suppresses error bubbling when bubbleErrors option is false","duplexer2 supports stream options such as objectMode"],"goal":"combine writable and readable streams into a duplex stream","kind":"HOW","packages":["pkg:npm/duplexer2@0.1.4"],"schemaVersion":1,"symbols":["duplexer2"]},"contractCommand":["node","test/contract.js"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"npm","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"npm","schemaVersion":1},"license":"MIT-0","packages":["pkg:npm/duplexer2@0.1.4"],"schemaVersion":1,"subject":"pkg:npm/duplexer2@0.1.4","symbols":["duplexer2"],"verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "csx-sample-duplexer2",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "csx-sample-duplexer2",
      "version": "1.0.0",
      "license": "MIT-0",
      "dependencies": {
        "duplexer2": "0.1.4"
      }
    },
    "node_modules/core-util-is": {
      "version": "1.0.3",
      "resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.3.tgz",
      "integrity": "sha512-ZQBvi1DcpJ4GDqanjucZ2Hj3wEO5pZDS89BWbkcrvdxksJorwUDDZamX9ldFkp9aw2lmBDLgkObEA4DWNJ9FYQ==",
      "license": "MIT"
    },
    "node_modules/duplexer2": {
      "version": "0.1.4",
      "resolved": "https://registry.npmjs.org/duplexer2/-/duplexer2-0.1.4.tgz",
      "integrity": "sha512-asLFVfWWtJ90ZyOUHMqk7/S2w2guQKxUI2itj3d92ADHhxUSbCMGi1f1cBcJ7xM1To+pE/Khbwo1yuNbMEPKeA==",
      "license": "BSD-3-Clause",
      "dependencies": {
        "readable-stream": "^2.0.2"
      }
    },
    "node_modules/inherits": {
      "version": "2.0.4",
      "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz",
      "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==",
      "license": "ISC"
    },
    "node_modules/isarray": {
      "version": "1.0.0",
      "resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz",
      "integrity": "sha512-VLghIWNM6ELQzo7zwmcg0NmTVyWKYjvIeM83yjp0wRDTmUnrM678fQbcKBo6n2CJEF0szoG//ytg+TKla89ALQ==",
      "license": "MIT"
    },
    "node_modules/process-nextick-args": {
      "version": "2.0.1",
      "resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-2.0.1.tgz",
      "integrity": "sha512-3ouUOpQhtgrbOa17J7+uxOTpITYWaGP7/AhoR3+A+/1e9skrzelGi/dXzEYyvbxubEF6Wn2ypscTKiKJFFn1ag==",
      "license": "MIT"
    },
    "node_modules/readable-stream": {
      "version": "2.3.8",
      "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-2.3.8.tgz",
      "integrity": "sha512-8p0AUk4XODgIewSi0l8Epjs+EVnWiK7NoDIEGU0HhE7+ZyY8D1IMY7odu5lRrFXGg71L15KG8QrPmum45RTtdA==",
      "license": "MIT",
      "dependencies": {
        "core-util-is": "~1.0.0",
        "inherits": "~2.0.3",
        "isarray": "~1.0.0",
        "process-nextick-args": "~2.0.0",
        "safe-buffer": "~5.1.1",
        "string_decoder": "~1.1.1",
        "util-deprecate": "~1.0.1"
      }
    },
    "node_modules/safe-buffer": {
      "version": "5.1.2",
      "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz",
      "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==",
      "license": "MIT"
    },
    "node_modules/string_decoder": {
      "version": "1.1.1",
      "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.1.1.tgz",
      "integrity": "sha512-n/ShnvDi6FHbbVfviro+WojiFzv+s8MPMHBczVePfUpDJLwoLT0ht1l4YwBCbi8pJAveEEdnkHyPyTP/mzRfwg==",
      "license": "MIT",
      "dependencies": {
        "safe-buffer": "~5.1.0"
      }
    },
    "node_modules/util-deprecate": {
      "version": "1.0.2",
      "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz",
      "integrity": "sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw==",
      "license": "MIT"
    }
  }
}
package.json
{
  "name": "csx-sample-duplexer2",
  "version": "1.0.0",
  "private": true,
  "description": "Clean-room verification for duplexer2",
  "main": "test/contract.js",
  "scripts": {
    "test": "node test/contract.js"
  },
  "dependencies": {
    "duplexer2": "0.1.4"
  },
  "license": "MIT-0"
}
spec.json
{
  "schemaVersion": 1,
  "goal": "combine writable and readable streams into a duplex stream",
  "kind": "HOW",
  "packages": [
    "pkg:npm/duplexer2@0.1.4"
  ],
  "symbols": [
    "duplexer2"
  ]
}
test/contract.js
const assert = require('node:assert/strict');
const { PassThrough, Readable, Writable } = require('node:stream');
const duplexer2 = require('duplexer2');

async function runTests() {
  // Test 1: duplexer2(writable, readable) returns a Duplex stream combining writable and readable streams
  {
    const writable = new PassThrough();
    const readable = new PassThrough();
    const duplex = duplexer2(writable, readable);

    assert(duplex instanceof duplexer2.DuplexWrapper, 'duplex should be an instance of DuplexWrapper');
    assert.strictEqual(typeof duplex.pipe, 'function', 'duplex should have pipe method');
    assert.strictEqual(typeof duplex.write, 'function', 'duplex should have write method');
    assert.strictEqual(typeof duplex.read, 'function', 'duplex should have read method');
  }

  // Test 2: writing data to duplex stream forwards chunks to the underlying writable stream
  await new Promise((resolve, reject) => {
    const written = [];
    const writable = new Writable({
      write(chunk, encoding, callback) {
        written.push(chunk.toString());
        callback();
      }
    });
    const readable = new PassThrough();
    const duplex = duplexer2(writable, readable);

    duplex.write('hello');
    duplex.write(' ');
    duplex.write('world');
    duplex.end();

    duplex.on('finish', () => {
      try {
        assert.deepStrictEqual(written, ['hello', ' ', 'world']);
        resolve();
      } catch (err) {
        reject(err);
      }
    });
  });

  // Test 3: reading data from duplex stream pulls chunks from the underlying readable stream
  await new Promise((resolve, reject) => {
    const chunks = [];
    const writable = new PassThrough();
    const readable = Readable.from(['alpha', 'beta', 'gamma']);
    const duplex = duplexer2(writable, readable);

    duplex.on('data', (chunk) => {
      chunks.push(chunk.toString());
    });

    duplex.on('end', () => {
      try {
        assert.deepStrictEqual(chunks, ['alpha', 'beta', 'gamma']);
        resolve();
      } catch (err) {
        reject(err);
      }
    });
  });

  // Test 4: duplexer2 bubbles error events from writable stream to duplex stream by default
  await new Promise((resolve, reject) => {
    const writable = new PassThrough();
    const readable = new PassThrough();
    const duplex = duplexer2(writable, readable);

    const testError = new Error('writable error');

    duplex.on('error', (err) => {
      try {
        assert.strictEqual(err, testError);
        resolve();
      } catch (e) {
        reject(e);
      }
    });

    writable.emit('error', testError);
  });

  // Test 5: duplexer2 bubbles error events from readable stream to duplex stream by default
  await new Promise((resolve, reject) => {
    const writable = new PassThrough();
    const readable = new PassThrough();
    const duplex = duplexer2(writable, readable);

    const testError = new Error('readable error');

    duplex.on('error', (err) => {
      try {
        assert.strictEqual(err, testError);
        resolve();
      } catch (e) {
        reject(e);
      }
    });

    readable.emit('error', testError);
  });

  // Test 6: duplexer2 suppresses error bubbling when bubbleErrors option is false
  await new Promise((resolve, reject) => {
    const writable = new PassThrough();
    const readable = new PassThrough();
    const duplex = duplexer2({ bubbleErrors: false }, writable, readable);

    let duplexErrored = false;
    duplex.on('error', () => {
      duplexErrored = true;
    });

    writable.on('error', () => {});
    readable.on('error', () => {});

    writable.emit('error', new Error('suppressed writable error'));
    readable.emit('error', new Error('suppressed readable error'));

    setImmediate(() => {
      try {
        assert.strictEqual(duplexErrored, false, 'duplex stream should not receive bubbled error when bubbleErrors is false');
        resolve();
      } catch (err) {
        reject(err);
      }
    });
  });

  // Test 7: duplexer2 supports stream options such as objectMode
  await new Promise((resolve, reject) => {
    const receivedObjects = [];
    const writable = new Writable({
      objectMode: true,
      write(chunk, encoding, callback) {
        receivedObjects.push(chunk);
        callback();
      }
    });
    const readable = new Readable({
      objectMode: true,
      read() {
        this.push({ id: 1, name: 'first' });
        this.push({ id: 2, name: 'second' });
        this.push(null);
      }
    });

    const duplex = duplexer2({ objectMode: true }, writable, readable);

    const outputObjects = [];
    duplex.on('data', (chunk) => {
      outputObjects.push(chunk);
    });

    duplex.write({ input: 'item1' });
    duplex.write({ input: 'item2' });
    duplex.end();

    duplex.on('end', () => {
      try {
        assert.deepStrictEqual(receivedObjects, [{ input: 'item1' }, { input: 'item2' }]);
        assert.deepStrictEqual(outputObjects, [{ id: 1, name: 'first' }, { id: 2, name: 'second' }]);
        resolve();
      } catch (err) {
        reject(err);
      }
    });
  });

  console.log('All duplexer2 contract assertions passed successfully.');
}

runTests().catch((err) => {
  console.error('Contract test failed:', err);
  process.exit(1);
});

Seeder d'origine

anonyme