CodeSampleX

示例

bare-events 2.9.1: on

已验证示例 — npm bare-events 2.9.1: on. contract 在 node 22 · linux alpine/x64 · docker 上运行并通过: emitter.on registers an event listener and returns the emitter…

sha256:8d7ab6ede3695b1749852c9ed05fa780132c59a451ebaad82e90023e584de458

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

执行证据

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

证据依据
签名契约通过
验证回执
2
构建过它的签名密钥
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-26
node 22 · linux debian/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1node:22@sha256:8a34c4ab3ea2…
2026-09-07

案例

HOW
目标
verify bare-events.on in pkg:npm/bare-events@2.9.1
包
符号
  • bare-events.on
创建时间
2026-08-26T11:29:16Z

契约

  1. emitter.on registers an event listener and returns the emitter instance for chaining
  2. emitter.on triggers the listener synchronously with all passed arguments when the event is emitted
  3. emitter.on preserves the EventEmitter instance as this context within listener functions
  4. emitter.on emits a newListener event before adding the listener to the emitter
  5. emitter.on appends listeners in registration order and supports multiple listeners for the same event
  6. EventEmitter.on static helper returns an async iterable yielding argument arrays for emitted events
  7. EventEmitter.on queues emitted events until consumed by next
  8. EventEmitter.on respects AbortSignal and rejects with OPERATION_ABORTED when aborted
  9. EventEmitter.on cleans up its internal listeners when return is called on the iterator
  10. EventEmitter.on rejects iterator promises when the emitter emits an error event

文件

  • PROMPT.md
  • csx.json
  • 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 bare-events.on in pkg:npm/bare-events@2.9.1
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/bare-events@2.9.1
Demonstrate these symbols/APIs:
  - bare-events.on

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:7f67a1b0835aad8e24131b9a8762f38910ec4bc8e4c73969ce0afd0695c9ff75","contract":["emitter.on registers an event listener and returns the emitter instance for chaining","emitter.on triggers the listener synchronously with all passed arguments when the event is emitted","emitter.on preserves the EventEmitter instance as this context within listener functions","emitter.on emits a newListener event before adding the listener to the emitter","emitter.on appends listeners in registration order and supports multiple listeners for the same event","EventEmitter.on static helper returns an async iterable yielding argument arrays for emitted events","EventEmitter.on queues emitted events until consumed by next","EventEmitter.on respects AbortSignal and rejects with OPERATION_ABORTED when aborted","EventEmitter.on cleans up its internal listeners when return is called on the iterator","EventEmitter.on rejects iterator promises when the emitter emits an error event"],"goal":"verify bare-events.on in pkg:npm/bare-events@2.9.1","kind":"HOW","packages":["pkg:npm/bare-events@2.9.1"],"schemaVersion":1,"symbols":["bare-events.on"]},"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/bare-events@2.9.1"],"schemaVersion":1,"subject":"pkg:npm/bare-events@2.9.1","symbols":["bare-events.on"],"verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "sample-bare-events-on",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-bare-events-on",
      "version": "1.0.0",
      "dependencies": {
        "bare-events": "2.9.1"
      }
    },
    "node_modules/bare-events": {
      "version": "2.9.1",
      "resolved": "https://registry.npmjs.org/bare-events/-/bare-events-2.9.1.tgz",
      "integrity": "sha512-Z0oHEHAFDZkffN8Qc39zNZjQlMDkPJRyyyZieU1VH7u8c5S+qHZ2S8ixdKIAxEjfHO7FJxXmJWgteOghVanIsg==",
      "license": "Apache-2.0",
      "peerDependencies": {
        "bare-abort-controller": "*"
      },
      "peerDependenciesMeta": {
        "bare-abort-controller": {
          "optional": true
        }
      }
    }
  }
}
package.json
{
  "name": "sample-bare-events-on",
  "version": "1.0.0",
  "type": "module",
  "private": true,
  "dependencies": {
    "bare-events": "2.9.1"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify bare-events.on in pkg:npm/bare-events@2.9.1",
  "kind": "HOW",
  "packages": [
    "pkg:npm/bare-events@2.9.1"
  ],
  "symbols": [
    "bare-events.on"
  ]
}
test/contract.mjs
import assert from 'node:assert/strict';
import EventEmitter from 'bare-events';

// 1. emitter.on registers an event listener and returns the emitter instance for chaining
{
  const ee = new EventEmitter();
  const res = ee.on('test', () => {});
  assert.equal(res, ee);
  assert.equal(ee.listenerCount('test'), 1);
}

// 2. emitter.on triggers the listener synchronously with all passed arguments when the event is emitted
{
  const ee = new EventEmitter();
  const calls = [];
  ee.on('greet', (greeting, target, extra) => {
    calls.push({ greeting, target, extra });
  });

  const emitted = ee.emit('greet', 'hello', 'world', 42);
  assert.equal(emitted, true);
  assert.deepEqual(calls, [{ greeting: 'hello', target: 'world', extra: 42 }]);
}

// 3. emitter.on preserves the EventEmitter instance as this context within listener functions
{
  const ee = new EventEmitter();
  let capturedThis = null;
  ee.on('context', function () {
    capturedThis = this;
  });

  ee.emit('context');
  assert.equal(capturedThis, ee);
}

// 4. emitter.on emits a newListener event before adding the listener to the emitter
{
  const ee = new EventEmitter();
  const events = [];
  ee.on('newListener', (eventName, listenerFn) => {
    events.push({
      eventName,
      fn: listenerFn,
      countBeforeAdd: ee.listenerCount(eventName)
    });
  });

  const customHandler = () => {};
  ee.on('custom', customHandler);

  assert.equal(events.length, 1);
  assert.equal(events[0].eventName, 'custom');
  assert.equal(events[0].fn, customHandler);
  assert.equal(events[0].countBeforeAdd, 0);
  assert.equal(ee.listenerCount('custom'), 1);
}

// 5. emitter.on appends listeners in registration order and supports multiple listeners for the same event
{
  const ee = new EventEmitter();
  const order = [];
  ee.on('order', () => order.push('first'));
  ee.on('order', () => order.push('second'));
  ee.on('order', () => order.push('third'));

  ee.emit('order');
  assert.deepEqual(order, ['first', 'second', 'third']);
}

// 6. EventEmitter.on static helper returns an async iterable yielding argument arrays for emitted events
{
  const ee = new EventEmitter();
  const iterator = EventEmitter.on(ee, 'stream');
  assert.equal(typeof iterator[Symbol.asyncIterator], 'function');

  const pendingNext = iterator.next();
  ee.emit('stream', 'alpha', 100);

  const res = await pendingNext;
  assert.deepEqual(res, { value: ['alpha', 100], done: false });
}

// 7. EventEmitter.on queues emitted events until consumed by next
{
  const ee = new EventEmitter();
  const iterator = EventEmitter.on(ee, 'batch');

  ee.emit('batch', 1);
  ee.emit('batch', 2);

  const res1 = await iterator.next();
  const res2 = await iterator.next();
  assert.deepEqual(res1.value, [1]);
  assert.deepEqual(res2.value, [2]);
}

// 8. EventEmitter.on respects AbortSignal and rejects with OPERATION_ABORTED when aborted
{
  const ee = new EventEmitter();
  const ac = new AbortController();
  const iterator = EventEmitter.on(ee, 'aborted-stream', { signal: ac.signal });

  ac.abort('stop');

  await assert.rejects(
    async () => {
      await iterator.next();
    },
    (err) => {
      assert.equal(err.code, 'OPERATION_ABORTED');
      return true;
    }
  );
}

// 9. EventEmitter.on cleans up its internal listeners when return is called on the iterator
{
  const ee = new EventEmitter();
  const iterator = EventEmitter.on(ee, 'cleanup');
  assert.equal(ee.listenerCount('cleanup'), 1);

  const closeRes = await iterator.return();
  assert.deepEqual(closeRes, { done: true });
  assert.equal(ee.listenerCount('cleanup'), 0);
}

// 10. EventEmitter.on rejects iterator promises when the emitter emits an error event
{
  const ee = new EventEmitter();
  const iterator = EventEmitter.on(ee, 'faulty');

  const pendingNext = iterator.next();
  const sampleErr = new Error('simulated failure');
  ee.emit('error', sampleErr);

  await assert.rejects(
    async () => {
      await pendingNext;
    },
    (err) => {
      assert.equal(err, sampleErr);
      return true;
    }
  );
}

原始种子者

匿名