CodeSampleX

示例

wrappy 1.0.2: wrappy

已验证示例 — npm wrappy 1.0.2: wrappy. contract 在 node 22 · linux alpine/x64 · docker 上运行并通过: wrappy returns a wrapper generator function when passed a wrapper…

sha256:fe88c89d35d48a347a6dcaeb0bb8ca0058a40c22fd24b3d754a9d7a8cc669ea9

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

执行证据

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

证据依据
签名契约通过
验证回执
1
构建过它的签名密钥
1
声明的环境 windows 11 x64 npm

验证运行环境

环境 契约 阶段 运行日期
node 22 · linux alpine/x64 · docker ed25519:2175b912ea1c23b1 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1
2026-08-19

案例

HOW
目标
verify pkg:npm/wrappy@1.0.2
包
符号
  • wrappy
创建时间
2026-08-18T18:16:16Z

契约

  1. wrappy returns a wrapper generator function when passed a wrapper function
  2. wrappy immediately wraps a target callback when passed both wrapper and target
  3. wrappy copies own enumerable properties from wrapper function to generated wrapper
  4. wrappy copies own enumerable properties from target callback to the returned wrapped function
  5. wrappy does not copy properties if returned function is identical to target callback
  6. wrappy preserves receiver and arguments forwarding to wrapper function
  7. wrappy throws TypeError with need wrapper function when first argument is not a function
  8. wrapFunction helper provides wrapper generator and immediate wrapping interfaces

文件

  • PROMPT.md
  • csx.json
  • index.mjs
  • 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 pkg:npm/wrappy@1.0.2
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/wrappy@1.0.2
Demonstrate these symbols/APIs:
  - wrappy

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:975d92c4a134441a71709898631e648d603e08c40c7f9ab2d934213faf640d01","contract":["wrappy returns a wrapper generator function when passed a wrapper function","wrappy immediately wraps a target callback when passed both wrapper and target","wrappy copies own enumerable properties from wrapper function to generated wrapper","wrappy copies own enumerable properties from target callback to the returned wrapped function","wrappy does not copy properties if returned function is identical to target callback","wrappy preserves receiver and arguments forwarding to wrapper function","wrappy throws TypeError with need wrapper function when first argument is not a function","wrapFunction helper provides wrapper generator and immediate wrapping interfaces"],"goal":"verify pkg:npm/wrappy@1.0.2","kind":"HOW","packages":["pkg:npm/wrappy@1.0.2"],"schemaVersion":1,"symbols":["wrappy"]},"contractCommand":["node","test/contract.mjs"],"environment":{"arch":"x64","ecosystem":"npm","os":"windows","osVersionBucket":"11","packageManager":"npm","schemaVersion":1},"license":"MIT-0","packages":["pkg:npm/wrappy@1.0.2"],"schemaVersion":1,"symbols":["wrappy"],"verifierAdapter":"node-typescript@1"}
index.mjs
import wrappy from 'wrappy';

/**
 * Wraps a transformation function so that it can be applied to a callback
 * while preserving callback identity, properties, prototype, and name.
 *
 * @param {Function} fn - The wrapper function.
 * @param {Function} [cb] - Optional target callback to wrap immediately.
 * @returns {Function} The wrapped function or wrapper generator.
 */
export function wrapFunction(fn, cb) {
  if (cb !== undefined) {
    return wrappy(fn, cb);
  }
  return wrappy(fn);
}

export default wrappy;
package-lock.json
{
  "name": "sample",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample",
      "version": "1.0.0",
      "dependencies": {
        "wrappy": "1.0.2"
      }
    },
    "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",
  "version": "1.0.0",
  "type": "module",
  "dependencies": {
    "wrappy": "1.0.2"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify pkg:npm/wrappy@1.0.2",
  "kind": "HOW",
  "packages": [
    "pkg:npm/wrappy@1.0.2"
  ],
  "symbols": [
    "wrappy"
  ]
}
test/contract.mjs
import assert from 'node:assert/strict';
import wrappy from 'wrappy';
import { wrapFunction } from '../index.mjs';

// Case 1: wrappy returns a wrapper generator function when passed a wrapper function
{
  const makeMultiplier = wrappy((cb) => {
    return function (...args) {
      return cb(...args) * 2;
    };
  });
  assert.equal(typeof makeMultiplier, 'function', 'wrappy(fn) should return a wrapper generator function');

  const add = (a, b) => a + b;
  const wrappedAdd = makeMultiplier(add);
  assert.equal(typeof wrappedAdd, 'function', 'wrapper generator should return a function');
  assert.equal(wrappedAdd(2, 3), 10, 'wrapped function should apply transformation: (2 + 3) * 2 = 10');
}

// Case 2: wrappy immediately wraps a target callback when passed both wrapper and target
{
  function prependHello(cb) {
    return function (name) {
      return `Hello, ${cb(name)}`;
    };
  }

  function getTarget(name) {
    return name.toUpperCase();
  }

  const greeted = wrappy(prependHello, getTarget);
  assert.equal(typeof greeted, 'function', 'wrappy(fn, cb) should return wrapped callback');
  assert.equal(greeted('world'), 'Hello, WORLD', 'wrappy(fn, cb) should immediately wrap the callback');
}

// Case 3: wrappy copies own enumerable properties from wrapper function to generated wrapper
{
  function myWrapper(cb) {
    return function (...args) {
      return cb(...args);
    };
  }
  myWrapper.customMetadata = 'wrapper-v1';
  myWrapper.version = 42;

  const generator = wrappy(myWrapper);
  assert.equal(generator.customMetadata, 'wrapper-v1', 'wrapper generator should inherit wrapper custom properties');
  assert.equal(generator.version, 42, 'wrapper generator should inherit wrapper version property');
}

// Case 4: wrappy copies own enumerable properties from target callback to the returned wrapped function
{
  const once = wrappy((cb) => {
    let called = false;
    let result;
    return function (...args) {
      if (called) return result;
      called = true;
      result = cb.apply(this, args);
      return result;
    };
  });

  function originalWorker(x) {
    return x * 3;
  }
  originalWorker.role = 'worker';
  originalWorker.taskCount = 5;

  const wrappedWorker = once(originalWorker);

  assert.equal(wrappedWorker.role, 'worker', 'wrapped function should inherit callback own property role');
  assert.equal(wrappedWorker.taskCount, 5, 'wrapped function should inherit callback own property taskCount');
  assert.equal(wrappedWorker(4), 12, 'first call should compute 4 * 3 = 12');
  assert.equal(wrappedWorker(10), 12, 'subsequent calls should return cached result 12');
}

// Case 5: wrappy does not copy properties if returned function is identical to target callback
{
  const identityWrapper = wrappy((cb) => cb);

  function noop() {}
  noop.flag = true;

  const result = identityWrapper(noop);
  assert.equal(result, noop, 'identity wrapper returns the original callback unchanged');
}

// Case 6: wrappy preserves receiver and arguments forwarding to wrapper function
{
  function withContext(cb) {
    return function (...args) {
      return `${this.prefix}: ${cb.apply(this, args)}`;
    };
  }

  const wrapWithCtx = wrappy(withContext);
  const formatter = wrapWithCtx(function (val) {
    return val + this.suffix;
  });

  const context = { prefix: 'PRE', suffix: 'POST' };
  const res = formatter.call(context, 'DATA');
  assert.equal(res, 'PRE: DATAPOST', 'this context and arguments should be preserved');
}

// Case 7: wrappy throws TypeError with need wrapper function when first argument is not a function
{
  assert.throws(
    () => {
      wrappy(null);
    },
    {
      name: 'TypeError',
      message: 'need wrapper function'
    },
    'wrappy(null) should throw TypeError with message "need wrapper function"'
  );

  assert.throws(
    () => {
      wrappy('not-a-function');
    },
    {
      name: 'TypeError',
      message: 'need wrapper function'
    },
    'wrappy non-function should throw TypeError'
  );
}

// Case 8: wrapFunction helper provides wrapper generator and immediate wrapping interfaces
{
  const helperGen = wrapFunction((cb) => (x) => cb(x) + 1);
  const inc = helperGen((x) => x * 10);
  assert.equal(inc(5), 51, 'wrapFunction with 1 arg should act as wrapper generator');

  const direct = wrapFunction((cb) => (x) => cb(x) - 1, (x) => x * 2);
  assert.equal(direct(5), 9, 'wrapFunction with 2 args should immediately wrap callback');
}

console.log('PASS: all wrappy contract assertions passed');

原始种子者

匿名