CodeSampleX

示例

tslib 2.9.1: __addDisposableResource

已验证示例 — npm tslib 2.9.1: __addDisposableResource. contract 在 node 22 · linux debian/x64 · docker 上运行并通过: sync registration (async=false) accepts object and…

sha256:1b2231fdbf8a17308f694faec59651182202fce6a5ec7b1859444c7ab8444423

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 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-11-23

案例

HOW
目标
verify tslib.__addDisposableResource in pkg:npm/tslib@2.9.1
包
符号
  • tslib.__addDisposableResource
环境
node 22.23.2
创建时间
2026-11-23T15:57:05Z

契约

  1. sync registration (async=false) accepts object and function values with Symbol.dispose, pushes { value, dispose, async: false } to env.stack, and returns value
  2. sync registration (async=false) throws TypeError('Object not disposable.') for objects defining only Symbol.asyncDispose without mutating env.stack
  3. async registration (async=true) prefers Symbol.asyncDispose over Symbol.dispose when both exist and pushes { value, dispose, async: true }
  4. async registration (async=true) falls back to Symbol.dispose when Symbol.asyncDispose is undefined and wraps synchronous disposal errors into a rejected Promise
  5. null and undefined return unchanged without pushing when async=false, and push { async: true } when async=true
  6. non-nullish primitives throw TypeError('Object expected.') and non-disposable objects throw TypeError('Object not disposable.') without mutating env.stack

文件

  • PROMPT.md
  • csx.json
  • package-lock.json
  • package.json
  • spec.json
  • src/index.js
  • test/contract.js

下载源代码构件 (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 tslib.__addDisposableResource in pkg:npm/tslib@2.9.1
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/tslib@2.9.1
Demonstrate these symbols/APIs:
  - tslib.__addDisposableResource
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:a0cf190560e73a81c0978db8e3f4791b7a6349eee61750e75845878e7e529d92","constraints":{"executionContext":"node"},"contract":["sync registration (async=false) accepts object and function values with Symbol.dispose, pushes { value, dispose, async: false } to env.stack, and returns value","sync registration (async=false) throws TypeError('Object not disposable.') for objects defining only Symbol.asyncDispose without mutating env.stack","async registration (async=true) prefers Symbol.asyncDispose over Symbol.dispose when both exist and pushes { value, dispose, async: true }","async registration (async=true) falls back to Symbol.dispose when Symbol.asyncDispose is undefined and wraps synchronous disposal errors into a rejected Promise","null and undefined return unchanged without pushing when async=false, and push { async: true } when async=true","non-nullish primitives throw TypeError('Object expected.') and non-disposable objects throw TypeError('Object not disposable.') without mutating env.stack"],"goal":"verify tslib.__addDisposableResource in pkg:npm/tslib@2.9.1","kind":"HOW","packages":["pkg:npm/tslib@2.9.1"],"schemaVersion":1,"symbols":["tslib.__addDisposableResource"]},"contractCommand":["node","test/contract.js"],"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/tslib@2.9.1"],"schemaVersion":1,"subject":"pkg:npm/tslib@2.9.1","symbols":["tslib.__addDisposableResource"],"verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "tslib-add-disposable-resource-sample",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "tslib-add-disposable-resource-sample",
      "version": "1.0.0",
      "dependencies": {
        "tslib": "2.9.1"
      }
    },
    "node_modules/tslib": {
      "version": "2.9.1",
      "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.9.1.tgz",
      "integrity": "sha512-a4Ux9VKuIMHsOS7ENbEq6hK2ZGYIT27Rtl0E4eD9KqwxhKiQeMZE/9xb3IdYwWaSOyIBQLExhQgXDTYoiCxnWA==",
      "license": "0BSD"
    }
  }
}
package.json
{
  "name": "tslib-add-disposable-resource-sample",
  "version": "1.0.0",
  "private": true,
  "description": "Minimal contract sample verifying tslib.__addDisposableResource in tslib 2.9.1",
  "main": "src/index.js",
  "scripts": {
    "test": "node test/contract.js"
  },
  "dependencies": {
    "tslib": "2.9.1"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify tslib.__addDisposableResource in pkg:npm/tslib@2.9.1",
  "kind": "HOW",
  "packages": [
    "pkg:npm/tslib@2.9.1"
  ],
  "symbols": [
    "tslib.__addDisposableResource"
  ],
  "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 tslib = require("tslib");

function createDisposableEnv() {
  return {
    stack: [],
    error: undefined,
    hasError: false,
  };
}

function registerDisposable(env, value, isAsync) {
  return tslib.__addDisposableResource(env, value, Boolean(isAsync));
}

module.exports = {
  createDisposableEnv,
  registerDisposable,
};
test/contract.js
"use strict";

const assert = require("node:assert/strict");
const { createDisposableEnv, registerDisposable } = require("../src/index.js");

async function main() {
  // 1. Sync registration of an object and a function implementing Symbol.dispose
  {
    const env = createDisposableEnv();
    const calls = [];

    const objResource = {
      id: "obj",
      [Symbol.dispose]() {
        calls.push(this.id);
      },
    };
    const fnResource = function resourceFn() {};
    fnResource.id = "fn";
    fnResource[Symbol.dispose] = function disposeFn() {
      calls.push(this.id);
    };

    assert.equal(registerDisposable(env, objResource, false), objResource);
    assert.equal(registerDisposable(env, fnResource, false), fnResource);
    assert.equal(env.stack.length, 2);

    assert.equal(env.stack[0].value, objResource);
    assert.equal(env.stack[0].dispose, objResource[Symbol.dispose]);
    assert.equal(env.stack[0].async, false);

    assert.equal(env.stack[1].value, fnResource);
    assert.equal(env.stack[1].dispose, fnResource[Symbol.dispose]);
    assert.equal(env.stack[1].async, false);

    env.stack[0].dispose.call(env.stack[0].value);
    env.stack[1].dispose.call(env.stack[1].value);
    assert.deepEqual(calls, ["obj", "fn"]);
  }

  // 2. Sync registration ignores Symbol.asyncDispose and rejects async-only resources
  {
    const env = createDisposableEnv();
    const asyncOnly = {
      async [Symbol.asyncDispose]() {},
    };
    assert.throws(
      () => registerDisposable(env, asyncOnly, false),
      { name: "TypeError", message: "Object not disposable." }
    );
    assert.equal(env.stack.length, 0);
  }

  // 3. Async registration prefers Symbol.asyncDispose when both symbols exist
  {
    const env = createDisposableEnv();
    const log = [];
    const dualResource = {
      [Symbol.dispose]() {
        log.push("sync");
      },
      async [Symbol.asyncDispose]() {
        log.push("async");
      },
    };

    assert.equal(registerDisposable(env, dualResource, true), dualResource);
    assert.equal(env.stack.length, 1);
    assert.equal(env.stack[0].value, dualResource);
    assert.equal(env.stack[0].dispose, dualResource[Symbol.asyncDispose]);
    assert.equal(env.stack[0].async, true);

    await env.stack[0].dispose.call(env.stack[0].value);
    assert.deepEqual(log, ["async"]);
  }

  // 4. Async registration falls back to Symbol.dispose and wraps sync throws into rejected Promises
  {
    const env = createDisposableEnv();
    const boom = new Error("sync dispose failure");
    const syncFallback = {
      label: "fallback",
      [Symbol.dispose]() {
        assert.equal(this.label, "fallback");
        throw boom;
      },
    };

    assert.equal(registerDisposable(env, syncFallback, true), syncFallback);
    assert.equal(env.stack.length, 1);
    assert.equal(env.stack[0].value, syncFallback);
    assert.equal(env.stack[0].async, true);
    assert.notEqual(env.stack[0].dispose, syncFallback[Symbol.dispose]);

    const result = env.stack[0].dispose.call(env.stack[0].value);
    assert.ok(result instanceof Promise);
    await assert.rejects(result, (err) => err === boom);
  }

  // 5. Null and undefined handling for sync vs async
  {
    const syncEnv = createDisposableEnv();
    assert.equal(registerDisposable(syncEnv, null, false), null);
    assert.equal(registerDisposable(syncEnv, undefined, false), undefined);
    assert.equal(syncEnv.stack.length, 0);

    const asyncEnv = createDisposableEnv();
    assert.equal(registerDisposable(asyncEnv, null, true), null);
    assert.equal(registerDisposable(asyncEnv, undefined, true), undefined);
    assert.deepEqual(asyncEnv.stack, [{ async: true }, { async: true }]);
  }

  // 6. Primitive and non-disposable error handling
  {
    const env = createDisposableEnv();
    for (const primitive of [42, "text", true]) {
      assert.throws(
        () => registerDisposable(env, primitive, false),
        { name: "TypeError", message: "Object expected." }
      );
      assert.throws(
        () => registerDisposable(env, primitive, true),
        { name: "TypeError", message: "Object expected." }
      );
    }

    for (const invalid of [{}, { [Symbol.dispose]: 123 }, { [Symbol.asyncDispose]: "bad" }]) {
      assert.throws(
        () => registerDisposable(env, invalid, false),
        { name: "TypeError", message: "Object not disposable." }
      );
      assert.throws(
        () => registerDisposable(env, invalid, true),
        { name: "TypeError", message: "Object not disposable." }
      );
    }
    assert.equal(env.stack.length, 0);
  }
}

main().catch((err) => {
  console.error(err);
  process.exit(1);
});

原始种子者

匿名