CodeSampleX

示例

hookified 2.2.0

已验证示例 — npm hookified 2.2.0. contract 在 node 22 · linux debian/x64 · docker 上运行并通过: Hookified registers lifecycle hooks with onHook and executes them during…

sha256:34208fe85521853c56f28db7c8f77722ee6a891601446ca4cf7e3f26448aceb3

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 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 debian/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1node:22@sha256:8a34c4ab3ea2…
2026-09-14

案例

HOW
目标
verify pkg:npm/hookified@2.2.0
包
环境
node 22.23.2
创建时间
2026-09-14T09:55:58Z

契约

  1. Hookified registers lifecycle hooks with onHook and executes them during hook calls
  2. Hookified supports beforeHook and afterHook convenience methods with prefixing
  3. Hookified onceHook executes registered handlers exactly once
  4. Hookified hookSync executes synchronous hook handlers
  5. Hookified prependHook executes registered handlers before previously registered handlers
  6. Hookified enforceBeforeAfter option rejects hook registration without before or after prefix
  7. Hookified throwOnHookError option causes hook invocation to reject when a handler throws
  8. Hookified inherits Eventified to register listeners with on and emit events
  9. ParallelHook executes hook functions concurrently and delivers settled results to final handler
  10. WaterfallHook executes hook functions sequentially passing accumulated context to final handler

文件

  • 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: verify pkg:npm/hookified@2.2.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/hookified@2.2.0
Constraints:
  - executionContext: node
Required runtime conditions:
  - ecosystem: npm
  - language: javascript
  - moduleSystem: cjs
  - packageManager: npm@10.9.8
  - runtime: node@22.23.2

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:be56285dedb71ab77654df54ba06ec055ffb3a35c2b6b5cd7a9607948b36ecfe","constraints":{"executionContext":"node"},"contract":["Hookified registers lifecycle hooks with onHook and executes them during hook calls","Hookified supports beforeHook and afterHook convenience methods with prefixing","Hookified onceHook executes registered handlers exactly once","Hookified hookSync executes synchronous hook handlers","Hookified prependHook executes registered handlers before previously registered handlers","Hookified enforceBeforeAfter option rejects hook registration without before or after prefix","Hookified throwOnHookError option causes hook invocation to reject when a handler throws","Hookified inherits Eventified to register listeners with on and emit events","ParallelHook executes hook functions concurrently and delivers settled results to final handler","WaterfallHook executes hook functions sequentially passing accumulated context to final handler"],"goal":"verify pkg:npm/hookified@2.2.0","kind":"HOW","packages":["pkg:npm/hookified@2.2.0"],"schemaVersion":1},"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/hookified@2.2.0"],"schemaVersion":1,"subject":"pkg:npm/hookified@2.2.0","verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "sample-hookified",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-hookified",
      "version": "1.0.0",
      "license": "MIT-0",
      "dependencies": {
        "hookified": "2.2.0"
      }
    },
    "node_modules/hookified": {
      "version": "2.2.0",
      "resolved": "https://registry.npmjs.org/hookified/-/hookified-2.2.0.tgz",
      "integrity": "sha512-p/LgFzRN5FeoD3DLS6bkUapeye6E4SI6yJs6KetENd18S+FBthqYq2amJUWpt5z0EQwwHemidjY5OqJGEKm5uA==",
      "license": "MIT"
    }
  }
}
package.json
{
  "name": "sample-hookified",
  "version": "1.0.0",
  "private": true,
  "description": "Clean-room code sample for hookified@2.2.0",
  "main": "src/index.js",
  "license": "MIT-0",
  "dependencies": {
    "hookified": "2.2.0"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify pkg:npm/hookified@2.2.0",
  "kind": "HOW",
  "packages": [
    "pkg:npm/hookified@2.2.0"
  ],
  "constraints": {
    "executionContext": "node"
  },
  "runtimeConditions": {
    "ecosystem": "npm",
    "language": "javascript",
    "moduleSystem": "cjs",
    "packageManager": "npm@10.9.8",
    "runtime": "node@22.23.2"
  }
}
src/index.js
'use strict';

const { Hookified, Hook, Eventified, ParallelHook, WaterfallHook } = require('hookified');

/**
 * Creates and configures a Hookified lifecycle manager instance.
 *
 * @param {object} [options] - Options for Hookified instance.
 * @returns {Hookified}
 */
function createHookManager(options) {
  return new Hookified(options);
}

/**
 * Executes a named hook with arguments on the provided Hookified instance.
 *
 * @param {Hookified} manager - Hookified instance.
 * @param {string} hookName - Name of the hook.
 * @param {...*} args - Arguments to pass to hook handlers.
 * @returns {Promise<void>}
 */
async function triggerHook(manager, hookName, ...args) {
  return manager.hook(hookName, ...args);
}

/**
 * Executes a named hook synchronously on the provided Hookified instance.
 *
 * @param {Hookified} manager - Hookified instance.
 * @param {string} hookName - Name of the hook.
 * @param {...*} args - Arguments to pass to hook handlers.
 * @returns {void}
 */
function triggerHookSync(manager, hookName, ...args) {
  return manager.hookSync(hookName, ...args);
}

module.exports = {
  Hookified,
  Hook,
  Eventified,
  ParallelHook,
  WaterfallHook,
  createHookManager,
  triggerHook,
  triggerHookSync
};
test/contract.mjs
import assert from 'node:assert/strict';
import { createRequire } from 'node:module';

const require = createRequire(import.meta.url);
const {
  Hookified,
  Hook,
  Eventified,
  ParallelHook,
  WaterfallHook,
  createHookManager,
  triggerHook,
  triggerHookSync
} = require('../src/index.js');

async function testAll() {
  // 1. Export verification
  assert.equal(typeof Hookified, 'function', 'Hookified export must be a constructor function');
  assert.equal(typeof Hook, 'function', 'Hook export must be a constructor function');
  assert.equal(typeof Eventified, 'function', 'Eventified export must be a constructor function');
  assert.equal(typeof ParallelHook, 'function', 'ParallelHook export must be a constructor function');
  assert.equal(typeof WaterfallHook, 'function', 'WaterfallHook export must be a constructor function');
  assert.equal(typeof createHookManager, 'function', 'createHookManager helper must be a function');
  assert.equal(typeof triggerHook, 'function', 'triggerHook helper must be a function');
  assert.equal(typeof triggerHookSync, 'function', 'triggerHookSync helper must be a function');

  // 2. Hookified registers lifecycle hooks with onHook and executes them during hook calls
  const h1 = createHookManager();
  let executedH1 = false;
  h1.onHook('lifecycle:init', (payload) => {
    assert.equal(payload.status, 'started');
    executedH1 = true;
  });
  await triggerHook(h1, 'lifecycle:init', { status: 'started' });
  assert.equal(executedH1, true, 'onHook handler must execute when hook is triggered');

  // 3. Hookified supports beforeHook and afterHook convenience methods with prefixing
  const h2 = createHookManager();
  const stages = [];
  h2.onHook('before:action', () => { stages.push('before'); });
  h2.onHook('after:action', () => { stages.push('after'); });
  await h2.beforeHook('action');
  await h2.afterHook('action');
  assert.deepEqual(stages, ['before', 'after'], 'beforeHook and afterHook should call prefixed handlers');

  // 4. Hookified onceHook executes registered handlers exactly once
  const h3 = createHookManager();
  let onceCalls = 0;
  h3.onceHook({
    event: 'single-run',
    handler: () => {
      onceCalls++;
    }
  });
  await triggerHook(h3, 'single-run');
  await triggerHook(h3, 'single-run');
  assert.equal(onceCalls, 1, 'onceHook handler must only execute once across multiple triggers');

  // 5. Hookified hookSync executes synchronous hook handlers
  const h4 = createHookManager();
  let syncTotal = 0;
  h4.onHook('compute', (delta) => {
    syncTotal += delta;
  });
  triggerHookSync(h4, 'compute', 15);
  assert.equal(syncTotal, 15, 'hookSync must synchronously execute registered handlers');

  // 6. Hookified prependHook executes registered handlers before previously registered handlers
  const h5 = createHookManager();
  const sequence = [];
  h5.onHook('step', () => { sequence.push('second'); });
  h5.prependHook({
    event: 'step',
    handler: () => { sequence.push('first'); }
  });
  await triggerHook(h5, 'step');
  assert.deepEqual(sequence, ['first', 'second'], 'prependHook handler must execute before normal handlers');

  // 7. Hookified enforceBeforeAfter option rejects hook registration without before or after prefix
  const h6 = createHookManager({ enforceBeforeAfter: true });
  assert.throws(
    () => {
      h6.onHook('customAction', () => {});
    },
    (err) => err instanceof Error,
    'enforceBeforeAfter must throw an error if hook name lacks before: or after: prefix'
  );
  assert.doesNotThrow(() => {
    h6.onHook('before:customAction', () => {});
    h6.onHook('after:customAction', () => {});
  });

  // 8. Hookified throwOnHookError option causes hook invocation to reject when a handler throws
  const h7 = createHookManager({ throwOnHookError: true });
  h7.onHook('failing-step', () => {
    throw new Error('step failed');
  });
  await assert.rejects(
    async () => {
      await triggerHook(h7, 'failing-step');
    },
    {
      name: 'Error',
      message: 'failing-step: step failed'
    },
    'throwOnHookError must cause hook trigger to reject when handler throws'
  );

  // 9. Hookified inherits Eventified to register listeners with on and emit events
  const h8 = createHookManager();
  let eventPayload = null;
  h8.on('state-change', (data) => {
    eventPayload = data;
  });
  const emitted = h8.emit('state-change', { active: true });
  assert.equal(emitted, true, 'emit must return true when listeners exist');
  assert.deepEqual(eventPayload, { active: true }, 'registered event listener must receive emitted data');

  // 10. ParallelHook executes hook functions concurrently and delivers settled results to final handler
  let parallelResults = null;
  const pHook = new ParallelHook('parallel:task', (context) => {
    parallelResults = context;
  });
  pHook.addHook(async (ctx) => ctx.initialArgs * 2);
  pHook.addHook(async (ctx) => ctx.initialArgs + 10);
  const h9 = createHookManager();
  h9.onHook(pHook);
  await triggerHook(h9, 'parallel:task', 5);
  assert.ok(parallelResults, 'ParallelHook final handler must be called');
  assert.equal(parallelResults.initialArgs, 5, 'ParallelHook must preserve initialArgs');

  // 11. WaterfallHook executes hook functions sequentially passing accumulated context to final handler
  let waterfallResults = null;
  const wHook = new WaterfallHook('waterfall:task', (context) => {
    waterfallResults = context;
  });
  wHook.addHook((ctx) => (ctx.initialArgs || 0) + 10);
  wHook.addHook((ctx) => ctx.results[0].result * 3);
  const h10 = createHookManager();
  h10.onHook(wHook);
  await triggerHook(h10, 'waterfall:task', 5);
  assert.ok(waterfallResults, 'WaterfallHook final handler must be called');
  assert.equal(waterfallResults.results[1].result, 45, 'WaterfallHook must chain intermediate results');

  console.log('All hookified contract tests passed successfully.');
}

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

原始种子者

匿名