CodeSampleX

示例

get-stream 6.0.1: read readable stream to string with encoding and maxBuffer options using get-stream

已验证示例 — npm get-stream 6.0.1: read readable stream to string with encoding and maxBuffer options using get-stream. contract 在 node 22 · linux alpine/x64 …

sha256:3f3b9c9629f127dcfec0f6e7c79ceab74501b3901af68bec92befc267284bf9c

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 1 表示只有作者;大于 1 表示还有其他人构建过。密钥是自行生成的,背后没有注册身份,因此计的是密钥而非人。 MIT-0

执行证据

声明的环境与签名的运行分开呈现,你可以看到这个样本究竟运行了什么、在哪里运行。

证据依据
签名契约通过
验证回执
1
构建过它的签名密钥
1
声明的环境 linux 24 · ubuntu · glibc 2.39 x64 npm

验证运行环境

环境 契约 阶段 运行日期
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

案例

HOW
目标
read readable stream to string with encoding and maxBuffer options using get-stream
包
符号
  • getStream
创建时间
2026-08-27T12:31:55Z

契约

  1. getStream reads a readable stream and resolves to its complete string content
  2. getStream decodes stream chunks using the specified encoding option
  3. getStream resolves to an empty string when given an empty readable stream
  4. getStream rejects with MaxBufferError when stream content length exceeds maxBuffer option
  5. getStream rejects with the emitted error when the stream encounters an error
  6. getStream rejects with an error when called without a stream argument

文件

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

下载源代码构件 (tar.gz)

源代码

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: read readable stream to string with encoding and maxBuffer options using get-stream
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/get-stream@6.0.1
Demonstrate these symbols/APIs:
  - getStream

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:a35daad4a29f81ab9ad9ee7d48976af3e2094124b745dc400415fff4fd36f0a3","contract":["getStream reads a readable stream and resolves to its complete string content","getStream decodes stream chunks using the specified encoding option","getStream resolves to an empty string when given an empty readable stream","getStream rejects with MaxBufferError when stream content length exceeds maxBuffer option","getStream rejects with the emitted error when the stream encounters an error","getStream rejects with an error when called without a stream argument"],"goal":"read readable stream to string with encoding and maxBuffer options using get-stream","kind":"HOW","packages":["pkg:npm/get-stream@6.0.1"],"schemaVersion":1,"symbols":["getStream"]},"contractCommand":["node","test/contract.mjs"],"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/get-stream@6.0.1"],"schemaVersion":1,"subject":"pkg:npm/get-stream@6.0.1","symbols":["getStream"],"verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "sample-get-stream",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-get-stream",
      "version": "1.0.0",
      "license": "MIT-0",
      "dependencies": {
        "get-stream": "6.0.1"
      }
    },
    "node_modules/get-stream": {
      "version": "6.0.1",
      "resolved": "https://registry.npmjs.org/get-stream/-/get-stream-6.0.1.tgz",
      "integrity": "sha512-ts6Wi+2j3jQjqi70w5AlN8DFnkSwC+MqmxEzdEALB2qXZYV3X/b1CTfgPLGJNMeAWxdPfU8FO1ms3NUfaHCPYg==",
      "license": "MIT",
      "engines": {
        "node": ">=10"
      },
      "funding": {
        "url": "https://github.com/sponsors/sindresorhus"
      }
    }
  }
}
package.json
{
  "name": "sample-get-stream",
  "version": "1.0.0",
  "private": true,
  "license": "MIT-0",
  "main": "src/index.js",
  "dependencies": {
    "get-stream": "6.0.1"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "read readable stream to string with encoding and maxBuffer options using get-stream",
  "kind": "HOW",
  "packages": [
    "pkg:npm/get-stream@6.0.1"
  ],
  "symbols": [
    "getStream"
  ]
}
src/index.js
const getStream = require('get-stream');

/**
 * Reads a readable stream and resolves to its string content.
 *
 * @param {import('node:stream').Readable} stream - The stream to read.
 * @param {object} [options] - Optional options passed to get-stream.
 * @returns {Promise<string>} The stream content as a string.
 */
async function readStreamToString(stream, options) {
  return getStream(stream, options);
}

/**
 * Reads a stream using a specific character encoding.
 *
 * @param {import('node:stream').Readable} stream - The stream to read.
 * @param {string} encoding - The character encoding (e.g., 'utf8', 'hex', 'base64').
 * @returns {Promise<string>} The decoded string.
 */
async function readStreamWithEncoding(stream, encoding) {
  return getStream(stream, { encoding });
}

/**
 * Reads a stream with a maximum buffer size limit.
 *
 * @param {import('node:stream').Readable} stream - The stream to read.
 * @param {number} maxBuffer - Maximum allowed character length.
 * @returns {Promise<string>} The stream content as a string.
 */
async function readStreamWithLimit(stream, maxBuffer) {
  return getStream(stream, { maxBuffer });
}

module.exports = {
  getStream,
  readStreamToString,
  readStreamWithEncoding,
  readStreamWithLimit,
  MaxBufferError: getStream.MaxBufferError
};
test/contract.mjs
import assert from 'node:assert/strict';
import { Readable } from 'node:stream';
import { createRequire } from 'node:module';

const require = createRequire(import.meta.url);
const getStream = require('get-stream');
const {
  readStreamToString,
  readStreamWithEncoding,
  readStreamWithLimit,
  MaxBufferError
} = require('../src/index.js');

async function testReadStreamToString() {
  const stream = Readable.from(['hello ', 'world', '!']);
  const result = await getStream(stream);
  assert.strictEqual(typeof result, 'string');
  assert.strictEqual(result, 'hello world!');

  const bufStream = Readable.from([Buffer.from('stream '), Buffer.from('data')]);
  const bufResult = await readStreamToString(bufStream);
  assert.strictEqual(bufResult, 'stream data');
}

async function testReadStreamWithEncoding() {
  const data = 'Hello, CodeSampleX!';
  const hexExpected = Buffer.from(data).toString('hex');
  const base64Expected = Buffer.from(data).toString('base64');

  const streamHex = Readable.from([Buffer.from(data)]);
  const hexResult = await getStream(streamHex, { encoding: 'hex' });
  assert.strictEqual(hexResult, hexExpected);

  const streamBase64 = Readable.from([Buffer.from(data)]);
  const base64Result = await readStreamWithEncoding(streamBase64, 'base64');
  assert.strictEqual(base64Result, base64Expected);
}

async function testEmptyStream() {
  const stream = Readable.from([]);
  const result = await getStream(stream);
  assert.strictEqual(typeof result, 'string');
  assert.strictEqual(result, '');

  const helperStream = Readable.from([]);
  const helperResult = await readStreamToString(helperStream);
  assert.strictEqual(helperResult, '');
}

async function testMaxBufferLimit() {
  const stream = Readable.from(['long content exceeding limit']);
  await assert.rejects(
    async () => {
      await getStream(stream, { maxBuffer: 5 });
    },
    (err) => {
      assert.ok(err instanceof MaxBufferError || err.name === 'MaxBufferError', 'should be MaxBufferError');
      assert.strictEqual(err.message, 'maxBuffer exceeded');
      return true;
    }
  );

  const helperStream = Readable.from(['another long content']);
  await assert.rejects(
    async () => {
      await readStreamWithLimit(helperStream, 4);
    },
    (err) => {
      assert.ok(err instanceof MaxBufferError || err.name === 'MaxBufferError', 'should be MaxBufferError');
      assert.strictEqual(err.message, 'maxBuffer exceeded');
      return true;
    }
  );
}

async function testStreamError() {
  const customError = new Error('simulated stream error');
  const stream = new Readable({
    read() {
      this.destroy(customError);
    }
  });

  await assert.rejects(
    async () => {
      await getStream(stream);
    },
    (err) => {
      assert.strictEqual(err, customError);
      return true;
    }
  );
}

async function testMissingStream() {
  await assert.rejects(
    async () => {
      await getStream(null);
    },
    {
      message: 'Expected a stream'
    }
  );

  await assert.rejects(
    async () => {
      await readStreamToString(undefined);
    },
    {
      message: 'Expected a stream'
    }
  );
}

async function runAll() {
  await testReadStreamToString();
  await testReadStreamWithEncoding();
  await testEmptyStream();
  await testMaxBufferLimit();
  await testStreamError();
  await testMissingStream();
  console.log('All getStream contract tests passed successfully.');
}

await runAll();

原始种子者

匿名