CodeSampleX

示例

get-stream 5.2.0: MaxBufferError

已验证示例 — npm get-stream 5.2.0: MaxBufferError. contract 在 node 22 · linux alpine/x64 · docker 上运行并通过: assert MaxBufferError constructor creates an instance of…

sha256:0e73e8a9370a76961a64fe1cd69e5005e1a6ac8f20555b8ccd8f82c5f857db08

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

执行证据

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

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

验证运行环境

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

案例

HOW
目标
verify get-stream.MaxBufferError in pkg:npm/get-stream@5.2.0
包
符号
  • get-stream.MaxBufferError
环境
node 22.23.2
创建时间
2026-08-23T13:31:09Z

契约

  1. assert MaxBufferError constructor creates an instance of Error with name MaxBufferError and default message
  2. assert getStream rejects with MaxBufferError and string bufferedData when stream string length exceeds maxBuffer limit
  3. assert getStream.buffer rejects with MaxBufferError and Buffer bufferedData when stream byte size exceeds maxBuffer limit
  4. assert getStream.array rejects with MaxBufferError and Array bufferedData when stream item count exceeds maxBuffer limit
  5. assert helper functions reject with MaxBufferError when stream size exceeds specified maxBuffer limit
  6. assert helper functions resolve correctly when stream size is within specified maxBuffer limit
  7. assert isMaxBufferError accurately identifies MaxBufferError instances

文件

  • PROMPT.md
  • csx.json
  • index.js
  • package-lock.json
  • package.json
  • spec.json
  • 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: verify get-stream.MaxBufferError in pkg:npm/get-stream@5.2.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/get-stream@5.2.0
Demonstrate these symbols/APIs:
  - get-stream.MaxBufferError

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:03c957a2ed712b90a01703ceec96d2091be5c8537006adb6f1f8b5f498c856cc","contract":["assert MaxBufferError constructor creates an instance of Error with name MaxBufferError and default message","assert getStream rejects with MaxBufferError and string bufferedData when stream string length exceeds maxBuffer limit","assert getStream.buffer rejects with MaxBufferError and Buffer bufferedData when stream byte size exceeds maxBuffer limit","assert getStream.array rejects with MaxBufferError and Array bufferedData when stream item count exceeds maxBuffer limit","assert helper functions reject with MaxBufferError when stream size exceeds specified maxBuffer limit","assert helper functions resolve correctly when stream size is within specified maxBuffer limit","assert isMaxBufferError accurately identifies MaxBufferError instances"],"goal":"verify get-stream.MaxBufferError in pkg:npm/get-stream@5.2.0","kind":"HOW","packages":["pkg:npm/get-stream@5.2.0"],"schemaVersion":1,"symbols":["get-stream.MaxBufferError"]},"contractCommand":["node","test/contract.mjs"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"npm","executionContext":"node","language":"javascript","libc":"glibc","libcVersion":"2.39","moduleSystem":"cjs","os":"linux","osVersionBucket":"24","packageManager":"npm","packageManagerVersion":"10.9.8","runtime":"node","runtimeVersion":"22.23.2","schemaVersion":1},"license":"MIT-0","packages":["pkg:npm/get-stream@5.2.0"],"schemaVersion":1,"subject":"pkg:npm/get-stream@5.2.0","symbols":["get-stream.MaxBufferError"],"verifierAdapter":"node-typescript@1"}
index.js
'use strict';

const getStream = require('get-stream');

/**
 * Reads a stream into a string with an enforced maximum buffer limit.
 *
 * @param {import('node:stream').Readable} stream - Readable stream to consume.
 * @param {number} [maxBuffer=Infinity] - Maximum allowed buffer size in characters/bytes.
 * @returns {Promise<string>} Fully buffered string content.
 * @throws {getStream.MaxBufferError} If stream size exceeds maxBuffer.
 */
async function readStringWithLimit(stream, maxBuffer = Infinity) {
  return getStream(stream, { maxBuffer });
}

/**
 * Reads a stream into a Buffer with an enforced maximum buffer limit.
 *
 * @param {import('node:stream').Readable} stream - Readable stream to consume.
 * @param {number} [maxBuffer=Infinity] - Maximum allowed buffer size in bytes.
 * @returns {Promise<Buffer>} Fully buffered Buffer content.
 * @throws {getStream.MaxBufferError} If stream size exceeds maxBuffer.
 */
async function readBufferWithLimit(stream, maxBuffer = Infinity) {
  return getStream.buffer(stream, { maxBuffer });
}

/**
 * Reads an object-mode stream into an Array with an enforced maximum item count limit.
 *
 * @param {import('node:stream').Readable} stream - Object-mode readable stream to consume.
 * @param {number} [maxBuffer=Infinity] - Maximum allowed number of items.
 * @returns {Promise<Array<*>>} Fully buffered array of items.
 * @throws {getStream.MaxBufferError} If item count exceeds maxBuffer.
 */
async function readArrayWithLimit(stream, maxBuffer = Infinity) {
  return getStream.array(stream, { maxBuffer });
}

/**
 * Checks whether an error is an instance of getStream.MaxBufferError.
 *
 * @param {*} error - The error to inspect.
 * @returns {boolean} True if error is a MaxBufferError.
 */
function isMaxBufferError(error) {
  return error instanceof getStream.MaxBufferError;
}

module.exports = {
  getStream,
  MaxBufferError: getStream.MaxBufferError,
  readStringWithLimit,
  readBufferWithLimit,
  readArrayWithLimit,
  isMaxBufferError
};
package-lock.json
{
  "name": "sample-get-stream-max-buffer-error",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-get-stream-max-buffer-error",
      "version": "1.0.0",
      "license": "MIT-0",
      "dependencies": {
        "get-stream": "5.2.0"
      }
    },
    "node_modules/end-of-stream": {
      "version": "1.4.5",
      "resolved": "https://registry.npmjs.org/end-of-stream/-/end-of-stream-1.4.5.tgz",
      "integrity": "sha512-ooEGc6HP26xXq/N+GCGOT0JKCLDGrq2bQUZrQ7gyrJiZANJ/8YDTxTpQBXGMn+WbIQXNVpyWymm7KYVICQnyOg==",
      "license": "MIT",
      "dependencies": {
        "once": "^1.4.0"
      }
    },
    "node_modules/get-stream": {
      "version": "5.2.0",
      "resolved": "https://registry.npmjs.org/get-stream/-/get-stream-5.2.0.tgz",
      "integrity": "sha512-nBF+F1rAZVCu/p7rjzgA+Yb4lfYXrpl7a6VmJrU8wF9I1CKvP/QwPNZHnOlwbTkY6dvtFIzFMSyQXbLoTQPRpA==",
      "license": "MIT",
      "dependencies": {
        "pump": "^3.0.0"
      },
      "engines": {
        "node": ">=8"
      },
      "funding": {
        "url": "https://github.com/sponsors/sindresorhus"
      }
    },
    "node_modules/once": {
      "version": "1.4.0",
      "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz",
      "integrity": "sha512-lNaJgI+2Q5URQBkccEKHTQOPaXdUxnZZElQTZY0MFUAuaEqe1E+Nyvgdz/aIyNi6Z9MzO5dv1H8n58/GELp3+w==",
      "license": "ISC",
      "dependencies": {
        "wrappy": "1"
      }
    },
    "node_modules/pump": {
      "version": "3.0.4",
      "resolved": "https://registry.npmjs.org/pump/-/pump-3.0.4.tgz",
      "integrity": "sha512-VS7sjc6KR7e1ukRFhQSY5LM2uBWAUPiOPa/A3mkKmiMwSmRFUITt0xuj+/lesgnCv+dPIEYlkzrcyXgquIHMcA==",
      "license": "MIT",
      "dependencies": {
        "end-of-stream": "^1.1.0",
        "once": "^1.3.1"
      }
    },
    "node_modules/wrappy": {
      "version": "1.0.2",
      "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz",
      "integrity": "sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==",
      "license": "ISC"
    }
  }
}
package.json
{
  "name": "sample-get-stream-max-buffer-error",
  "version": "1.0.0",
  "private": true,
  "description": "Clean-room sample verifying get-stream.MaxBufferError in pkg:npm/get-stream@5.2.0",
  "main": "index.js",
  "license": "MIT-0",
  "dependencies": {
    "get-stream": "5.2.0"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify get-stream.MaxBufferError in pkg:npm/get-stream@5.2.0",
  "kind": "HOW",
  "packages": [
    "pkg:npm/get-stream@5.2.0"
  ],
  "symbols": [
    "get-stream.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,
  MaxBufferError,
  readStringWithLimit,
  readBufferWithLimit,
  readArrayWithLimit,
  isMaxBufferError
} = require('../index.js');

async function testAll() {
  // 1. MaxBufferError class and inheritance verification
  assert.equal(typeof MaxBufferError, 'function', 'MaxBufferError must be a constructor function/class');
  assert.equal(MaxBufferError, getStream.MaxBufferError, 'Exported MaxBufferError must match getStream.MaxBufferError');

  const manualError = new MaxBufferError();
  assert.ok(manualError instanceof Error, 'MaxBufferError instance must be an instanceof Error');
  assert.ok(manualError instanceof MaxBufferError, 'MaxBufferError instance must be an instanceof MaxBufferError');
  assert.equal(manualError.name, 'MaxBufferError', 'MaxBufferError instance name must be MaxBufferError');
  assert.equal(manualError.message, 'maxBuffer exceeded', 'Default error message must be "maxBuffer exceeded"');

  // 2. String stream overflow behavior
  const stringChunks = ['alpha', 'beta', 'gamma'];
  const stringStream = Readable.from(stringChunks);
  await assert.rejects(
    async () => {
      await getStream(stringStream, { maxBuffer: 7 });
    },
    (err) => {
      assert.ok(err instanceof MaxBufferError, 'Rejected error must be an instance of MaxBufferError');
      assert.equal(err.name, 'MaxBufferError', 'Error name must be MaxBufferError');
      assert.equal(typeof err.bufferedData, 'string', 'bufferedData on string stream error must be a string');
      assert.ok(err.bufferedData.length > 0, 'bufferedData must contain accumulated stream data');
      return true;
    },
    'getStream must reject with MaxBufferError when total string length exceeds maxBuffer'
  );

  // 3. Buffer stream overflow behavior
  const bufferChunks = [Buffer.from('hello'), Buffer.from(' world'), Buffer.from('!')];
  const bufferStream = Readable.from(bufferChunks);
  await assert.rejects(
    async () => {
      await getStream.buffer(bufferStream, { maxBuffer: 6 });
    },
    (err) => {
      assert.ok(err instanceof MaxBufferError, 'Rejected error must be an instance of MaxBufferError');
      assert.equal(err.name, 'MaxBufferError', 'Error name must be MaxBufferError');
      assert.ok(Buffer.isBuffer(err.bufferedData), 'bufferedData on buffer stream error must be a Buffer');
      return true;
    },
    'getStream.buffer must reject with MaxBufferError when byte size exceeds maxBuffer'
  );

  // 4. Array / Object stream overflow behavior
  const objectItems = [{ id: 1 }, { id: 2 }, { id: 3 }, { id: 4 }];
  const objectStream = Readable.from(objectItems);
  await assert.rejects(
    async () => {
      await getStream.array(objectStream, { maxBuffer: 2 });
    },
    (err) => {
      assert.ok(err instanceof MaxBufferError, 'Rejected error must be an instance of MaxBufferError');
      assert.equal(err.name, 'MaxBufferError', 'Error name must be MaxBufferError');
      assert.ok(Array.isArray(err.bufferedData), 'bufferedData on array stream error must be an Array');
      assert.ok(err.bufferedData.length >= 2, 'bufferedData must include elements received during the overflow');
      return true;
    },
    'getStream.array must reject with MaxBufferError when item count exceeds maxBuffer'
  );

  // 5. Helper function tests with limits exceeded
  const helperStream1 = Readable.from(['quick brown fox']);
  await assert.rejects(
    async () => {
      await readStringWithLimit(helperStream1, 5);
    },
    (err) => isMaxBufferError(err) && err.name === 'MaxBufferError',
    'readStringWithLimit helper must reject with MaxBufferError when limit exceeded'
  );

  const helperStream2 = Readable.from([Buffer.from('sample buffer content')]);
  await assert.rejects(
    async () => {
      await readBufferWithLimit(helperStream2, 4);
    },
    (err) => isMaxBufferError(err) && Buffer.isBuffer(err.bufferedData),
    'readBufferWithLimit helper must reject with MaxBufferError when limit exceeded'
  );

  const helperStream3 = Readable.from(['item1', 'item2', 'item3']);
  await assert.rejects(
    async () => {
      await readArrayWithLimit(helperStream3, 1);
    },
    (err) => isMaxBufferError(err) && Array.isArray(err.bufferedData),
    'readArrayWithLimit helper must reject with MaxBufferError when limit exceeded'
  );

  // 6. Within limit / normal stream resolution (does not throw)
  const validStringStream = Readable.from(['hello', ' ', 'world']);
  const stringResult = await readStringWithLimit(validStringStream, 50);
  assert.equal(stringResult, 'hello world', 'readStringWithLimit must resolve correct content when within limit');

  const validBufferStream = Readable.from([Buffer.from('node stream test')]);
  const bufferResult = await readBufferWithLimit(validBufferStream, 50);
  assert.equal(bufferResult.toString('utf8'), 'node stream test', 'readBufferWithLimit must resolve correct Buffer when within limit');

  const validArrayStream = Readable.from([10, 20, 30]);
  const arrayResult = await readArrayWithLimit(validArrayStream, 10);
  assert.deepEqual(arrayResult, [10, 20, 30], 'readArrayWithLimit must resolve correct Array when within limit');

  // 7. isMaxBufferError utility verification
  assert.equal(isMaxBufferError(new MaxBufferError()), true, 'isMaxBufferError must return true for MaxBufferError');
  assert.equal(isMaxBufferError(new Error('general error')), false, 'isMaxBufferError must return false for standard Error');
  assert.equal(isMaxBufferError(null), false, 'isMaxBufferError must return false for null');
  assert.equal(isMaxBufferError({ name: 'MaxBufferError' }), false, 'isMaxBufferError must return false for plain object');

  console.log('All get-stream.MaxBufferError contract tests passed successfully.');
}

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

原始种子者

匿名