示例
eventsource-parser 3.1.1
已验证示例 — npm eventsource-parser 3.1.1. contract 在 node 22 · linux alpine/x64 · docker 上运行并通过: assert createParser parses single-line and multiline data fields…
sha256:9041007dffbc5e94ed01f18ab87cbef5065683b0e2c4792f6aa517f51307dafb
本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。
提交了通过的契约回执的不同签名密钥数量。为 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- 目标
- verify pkg:npm/eventsource-parser@3.1.1
- 创建时间
- 2026-08-27T22:16:35Z
契约
- assert createParser parses single-line and multiline data fields into EventSourceMessage objects when fed chunks
- assert createParser extracts optional id and event type fields along with message data
- assert createParser invokes onRetry callback when parsing retry interval directives and onComment for comment lines
- assert createParser emits ParseError with invalid-retry or unknown-field types when encountering malformed SSE fields
- assert createParser enforces maxBufferSize limit by emitting ParseError with max-buffer-size-exceeded and terminating feed
- assert EventSourceParserStream transforms a stream of chunked strings into a stream of EventSourceMessage events
文件
- PROMPT.md
- csx.json
- index.mjs
- package-lock.json
- package.json
- spec.json
- test/contract.mjs
源代码
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 pkg:npm/eventsource-parser@3.1.1
Kind: HOW
Use EXACTLY these public packages and versions:
- pkg:npm/eventsource-parser@3.1.1
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.
{"case":{"caseId":"case:sha256:514ff6fa387a790563675e2c5cd7659b5477a038f6ce273838523d2e0c937633","contract":["assert createParser parses single-line and multiline data fields into EventSourceMessage objects when fed chunks","assert createParser extracts optional id and event type fields along with message data","assert createParser invokes onRetry callback when parsing retry interval directives and onComment for comment lines","assert createParser emits ParseError with invalid-retry or unknown-field types when encountering malformed SSE fields","assert createParser enforces maxBufferSize limit by emitting ParseError with max-buffer-size-exceeded and terminating feed","assert EventSourceParserStream transforms a stream of chunked strings into a stream of EventSourceMessage events"],"goal":"verify pkg:npm/eventsource-parser@3.1.1","kind":"HOW","packages":["pkg:npm/eventsource-parser@3.1.1"],"schemaVersion":1},"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/eventsource-parser@3.1.1"],"schemaVersion":1,"subject":"pkg:npm/eventsource-parser@3.1.1","verifierAdapter":"node-typescript@1"}
import { createParser, ParseError } from 'eventsource-parser';
import { EventSourceParserStream } from 'eventsource-parser/stream';
/**
* Parse an SSE string directly into an array of parsed messages.
* @param {string} input - Raw SSE stream text
* @param {import('eventsource-parser').ParserConfig} [config]
* @returns {import('eventsource-parser').EventSourceMessage[]}
*/
export function parseSSE(input, config = {}) {
const events = [];
const parser = createParser({
...config,
onEvent: (event) => {
events.push(event);
config.onEvent?.(event);
},
});
parser.feed(input);
return events;
}
export { createParser, ParseError, EventSourceParserStream };
export default createParser;
{
"name": "sample-eventsource-parser-verify",
"version": "1.0.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "sample-eventsource-parser-verify",
"version": "1.0.0",
"dependencies": {
"eventsource-parser": "3.1.1"
}
},
"node_modules/eventsource-parser": {
"version": "3.1.1",
"resolved": "https://registry.npmjs.org/eventsource-parser/-/eventsource-parser-3.1.1.tgz",
"integrity": "sha512-EKN1vKAMcZ8MlYMpaNuxN6R9yakzH6uajHcHVTqWJzvu5pWw9DyhbP35HH8MVBQ+dZjAfDxk+A8NiR9KWaXiyQ==",
"license": "MIT",
"engines": {
"node": ">=18.0.0"
}
}
}
}
{
"name": "sample-eventsource-parser-verify",
"version": "1.0.0",
"private": true,
"type": "module",
"dependencies": {
"eventsource-parser": "3.1.1"
}
}
{
"schemaVersion": 1,
"goal": "verify pkg:npm/eventsource-parser@3.1.1",
"kind": "HOW",
"packages": [
"pkg:npm/eventsource-parser@3.1.1"
]
}
import assert from 'node:assert/strict';
import { createParser, ParseError, EventSourceParserStream, parseSSE } from '../index.mjs';
// 1. assert createParser parses single-line and multiline data fields into EventSourceMessage objects when fed chunks
{
const events = [];
const parser = createParser({
onEvent: (event) => events.push(event),
});
parser.feed('data: hel');
parser.feed('lo world\n\n');
parser.feed('data: line 1\n');
parser.feed('data: line 2\n\n');
assert.strictEqual(events.length, 2);
assert.deepStrictEqual(events[0], {
id: undefined,
event: undefined,
data: 'hello world',
});
assert.deepStrictEqual(events[1], {
id: undefined,
event: undefined,
data: 'line 1\nline 2',
});
const batchEvents = parseSSE('data: batch test\n\n');
assert.strictEqual(batchEvents.length, 1);
assert.strictEqual(batchEvents[0].data, 'batch test');
}
// 2. assert createParser extracts optional id and event type fields along with message data
{
const events = [];
const parser = createParser({
onEvent: (event) => events.push(event),
});
parser.feed('id: msg-001\nevent: custom-event\ndata: payload body\n\n');
assert.strictEqual(events.length, 1);
assert.strictEqual(events[0].id, 'msg-001');
assert.strictEqual(events[0].event, 'custom-event');
assert.strictEqual(events[0].data, 'payload body');
}
// 3. assert createParser invokes onRetry callback when parsing retry interval directives and onComment for comment lines
{
const retries = [];
const comments = [];
const parser = createParser({
onRetry: (interval) => retries.push(interval),
onComment: (comment) => comments.push(comment),
});
parser.feed('retry: 5000\n\n');
parser.feed(': keep-alive comment\n\n');
assert.strictEqual(retries.length, 1);
assert.strictEqual(retries[0], 5000);
assert.strictEqual(comments.length, 1);
assert.strictEqual(comments[0], 'keep-alive comment');
}
// 4. assert createParser emits ParseError with invalid-retry or unknown-field types when encountering malformed SSE fields
{
const errors = [];
const parser = createParser({
onError: (err) => errors.push(err),
});
parser.feed('unknown_directive: value\n\n');
parser.feed('retry: not_a_number\n\n');
assert.strictEqual(errors.length, 2);
assert.ok(errors[0] instanceof ParseError);
assert.strictEqual(errors[0].type, 'unknown-field');
assert.strictEqual(errors[0].field, 'unknown_directive');
assert.strictEqual(errors[0].value, 'value');
assert.ok(errors[1] instanceof ParseError);
assert.strictEqual(errors[1].type, 'invalid-retry');
assert.strictEqual(errors[1].value, 'not_a_number');
}
// 5. assert createParser enforces maxBufferSize limit by emitting ParseError with max-buffer-size-exceeded and terminating feed
{
const errors = [];
const parser = createParser({
maxBufferSize: 20,
onError: (err) => errors.push(err),
});
parser.feed('data: this is an overly long stream message that exceeds buffer limit');
assert.strictEqual(errors.length, 1);
assert.strictEqual(errors[0].type, 'max-buffer-size-exceeded');
assert.throws(
() => parser.feed('data: next chunk'),
/terminated after exceeding the configured max buffer size/
);
parser.reset();
const resetEvents = [];
const resetParser = createParser({
maxBufferSize: 50,
onEvent: (e) => resetEvents.push(e),
});
resetParser.feed('data: valid\n\n');
assert.strictEqual(resetEvents.length, 1);
assert.strictEqual(resetEvents[0].data, 'valid');
}
// 6. assert EventSourceParserStream transforms a stream of chunked strings into a stream of EventSourceMessage events
{
const stream = new ReadableStream({
start(controller) {
controller.enqueue('event: notice\n');
controller.enqueue('data: chunk1\n\n');
controller.enqueue('id: 99\ndata: chunk2\n\n');
controller.close();
},
});
const parsedStream = stream.pipeThrough(new EventSourceParserStream());
const reader = parsedStream.getReader();
const msg1 = await reader.read();
assert.strictEqual(msg1.done, false);
assert.deepStrictEqual(msg1.value, {
id: undefined,
event: 'notice',
data: 'chunk1',
});
const msg2 = await reader.read();
assert.strictEqual(msg2.done, false);
assert.deepStrictEqual(msg2.value, {
id: '99',
event: undefined,
data: 'chunk2',
});
const done = await reader.read();
assert.strictEqual(done.done, true);
}
console.log('All contracts passed successfully.');
原始种子者
匿名