CodeSampleX

示例

proper-lockfile 4.1.2: check, lock, unlock

已验证示例 — npm proper-lockfile 4.1.2: check, lock, unlock. contract 在 node 22 · linux alpine/x64 · docker 上运行并通过: proper-lockfile.check returns false when the…

sha256:4448d5e84c7ec759a896b6d0be543af13e5c27580e2f0437ba41b640067ae0f0

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 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/proper-lockfile@4.1.2
包
符号
  • proper-lockfile.check
  • proper-lockfile.lock
  • proper-lockfile.unlock
创建时间
2026-08-27T20:18:03Z

契约

  1. proper-lockfile.check returns false when the file is not locked and true when locked
  2. proper-lockfile.lock acquires an exclusive lock and returns a release function
  3. proper-lockfile.lock rejects with ELOCKED error when file is already locked without retries
  4. proper-lockfile.unlock explicitly releases the lock on the target file
  5. proper-lockfile.lockSync and unlockSync synchronously acquire and release locks
  6. proper-lockfile.checkSync synchronously checks lock status on the target file

文件

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

下载源代码构件 (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/proper-lockfile@4.1.2
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/proper-lockfile@4.1.2
Demonstrate these symbols/APIs:
  - proper-lockfile.check
  - proper-lockfile.lock
  - proper-lockfile.unlock

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:38f782efbc45d9fe3ebbb52e23886afd3f3d2a4635e98c5b017cac493fbce347","contract":["proper-lockfile.check returns false when the file is not locked and true when locked","proper-lockfile.lock acquires an exclusive lock and returns a release function","proper-lockfile.lock rejects with ELOCKED error when file is already locked without retries","proper-lockfile.unlock explicitly releases the lock on the target file","proper-lockfile.lockSync and unlockSync synchronously acquire and release locks","proper-lockfile.checkSync synchronously checks lock status on the target file"],"goal":"verify pkg:npm/proper-lockfile@4.1.2","kind":"HOW","packages":["pkg:npm/proper-lockfile@4.1.2"],"schemaVersion":1,"symbols":["proper-lockfile.check","proper-lockfile.lock","proper-lockfile.unlock"]},"contractCommand":["node","test/contract.cjs"],"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/proper-lockfile@4.1.2"],"schemaVersion":1,"subject":"pkg:npm/proper-lockfile@4.1.2","symbols":["proper-lockfile.check","proper-lockfile.lock","proper-lockfile.unlock"],"verifierAdapter":"node-typescript@1"}
index.js
'use strict';

const lockfile = require('proper-lockfile');

/**
 * Acquire a lock on a file.
 * @param {string} file
 * @param {object} [options]
 * @returns {Promise<Function>} release function
 */
async function acquireLock(file, options) {
  return await lockfile.lock(file, options);
}

/**
 * Check if a file is locked.
 * @param {string} file
 * @param {object} [options]
 * @returns {Promise<boolean>}
 */
async function isLocked(file, options) {
  return await lockfile.check(file, options);
}

/**
 * Unlock a file.
 * @param {string} file
 * @param {object} [options]
 * @returns {Promise<void>}
 */
async function releaseLock(file, options) {
  return await lockfile.unlock(file, options);
}

/**
 * Synchronously acquire a lock on a file.
 * @param {string} file
 * @param {object} [options]
 * @returns {Function} release function
 */
function acquireLockSync(file, options) {
  return lockfile.lockSync(file, options);
}

/**
 * Synchronously check if a file is locked.
 * @param {string} file
 * @param {object} [options]
 * @returns {boolean}
 */
function isLockedSync(file, options) {
  return lockfile.checkSync(file, options);
}

/**
 * Synchronously unlock a file.
 * @param {string} file
 * @param {object} [options]
 */
function releaseLockSync(file, options) {
  return lockfile.unlockSync(file, options);
}

module.exports = {
  acquireLock,
  isLocked,
  releaseLock,
  acquireLockSync,
  isLockedSync,
  releaseLockSync,
  lockfile
};
package-lock.json
{
  "name": "sample-proper-lockfile",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-proper-lockfile",
      "version": "1.0.0",
      "license": "MIT-0",
      "dependencies": {
        "proper-lockfile": "4.1.2"
      }
    },
    "node_modules/graceful-fs": {
      "version": "4.2.11",
      "resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.2.11.tgz",
      "integrity": "sha512-RbJ5/jmFcNNCcDV5o9eTnBLJ/HszWV0P73bc+Ff4nS/rJj+YaS6IGyiOL0VoBYX+l1Wrl3k63h/KrH+nhJ0XvQ==",
      "license": "ISC"
    },
    "node_modules/proper-lockfile": {
      "version": "4.1.2",
      "resolved": "https://registry.npmjs.org/proper-lockfile/-/proper-lockfile-4.1.2.tgz",
      "integrity": "sha512-TjNPblN4BwAWMXU8s9AEz4JmQxnD1NNL7bNOY/AKUzyamc379FWASUhc/K1pL2noVb+XmZKLL68cjzLsiOAMaA==",
      "license": "MIT",
      "dependencies": {
        "graceful-fs": "^4.2.4",
        "retry": "^0.12.0",
        "signal-exit": "^3.0.2"
      }
    },
    "node_modules/retry": {
      "version": "0.12.0",
      "resolved": "https://registry.npmjs.org/retry/-/retry-0.12.0.tgz",
      "integrity": "sha512-9LkiTwjUh6rT555DtE9rTX+BKByPfrMzEAtnlEtdEwr3Nkffwiihqe2bWADg+OQRjt9gl6ICdmB/ZFDCGAtSow==",
      "license": "MIT",
      "engines": {
        "node": ">= 4"
      }
    },
    "node_modules/signal-exit": {
      "version": "3.0.7",
      "resolved": "https://registry.npmjs.org/signal-exit/-/signal-exit-3.0.7.tgz",
      "integrity": "sha512-wnD2ZE+l+SPC/uoS0vXeE9L1+0wuaMqKlfz9AMUo38JsyLSBWSFcHR1Rri62LZc12vLr1gb3jl7iwQhgwpAbGQ==",
      "license": "ISC"
    }
  }
}
package.json
{
  "name": "sample-proper-lockfile",
  "version": "1.0.0",
  "private": true,
  "description": "verify pkg:npm/proper-lockfile@4.1.2",
  "main": "index.js",
  "scripts": {
    "test": "node test/contract.cjs"
  },
  "dependencies": {
    "proper-lockfile": "4.1.2"
  },
  "license": "MIT-0"
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify pkg:npm/proper-lockfile@4.1.2",
  "kind": "HOW",
  "packages": [
    "pkg:npm/proper-lockfile@4.1.2"
  ],
  "symbols": [
    "proper-lockfile.check",
    "proper-lockfile.lock",
    "proper-lockfile.unlock"
  ]
}
test/contract.cjs
'use strict';

const assert = require('assert');
const fs = require('fs');
const path = require('path');
const os = require('os');
const lockfile = require('proper-lockfile');
const {
  acquireLock,
  isLocked,
  releaseLock,
  acquireLockSync,
  isLockedSync,
  releaseLockSync
} = require('../index.js');

async function run() {
  const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'lockfile-contract-test-'));
  const targetFile = path.join(tmpDir, 'data.txt');
  const targetFileSync = path.join(tmpDir, 'data-sync.txt');

  try {
    fs.writeFileSync(targetFile, 'initial data', 'utf8');
    fs.writeFileSync(targetFileSync, 'sync data', 'utf8');

    // 1. Initial check - file should not be locked
    const initialCheck = await lockfile.check(targetFile);
    assert.strictEqual(initialCheck, false, 'File must not be locked initially');

    const helperInitialCheck = await isLocked(targetFile);
    assert.strictEqual(helperInitialCheck, false, 'Helper isLocked must return false initially');

    // 2. Lock file asynchronously using proper-lockfile.lock
    const releaseAsync = await lockfile.lock(targetFile);
    assert.strictEqual(typeof releaseAsync, 'function', 'lock must return a release function');

    const lockedCheck = await lockfile.check(targetFile);
    assert.strictEqual(lockedCheck, true, 'check must return true after lock is acquired');

    // 3. Attempting to lock an already locked file with 0 retries must reject with ELOCKED
    let lockRejected = false;
    try {
      await lockfile.lock(targetFile, { retries: 0 });
    } catch (err) {
      lockRejected = true;
      assert.strictEqual(err.code, 'ELOCKED', 'Lock error code must be ELOCKED');
    }
    assert.strictEqual(lockRejected, true, 'lock must reject when file is already locked');

    // 4. Release lock using the returned release function
    await releaseAsync();
    const afterReleaseCheck = await lockfile.check(targetFile);
    assert.strictEqual(afterReleaseCheck, false, 'check must return false after release function is called');

    // 5. Lock and unlock using helper functions / proper-lockfile.unlock
    await acquireLock(targetFile);
    const helperLockedCheck = await isLocked(targetFile);
    assert.strictEqual(helperLockedCheck, true, 'acquireLock helper must lock the file');

    await releaseLock(targetFile);
    const afterUnlockCheck = await isLocked(targetFile);
    assert.strictEqual(afterUnlockCheck, false, 'releaseLock helper / unlock must release the lock');

    // 6. Synchronous lock and unlock
    const initialSyncCheck = lockfile.checkSync(targetFileSync);
    assert.strictEqual(initialSyncCheck, false, 'checkSync must return false initially');

    const releaseSync = acquireLockSync(targetFileSync);
    assert.strictEqual(typeof releaseSync, 'function', 'acquireLockSync must return a release function');

    const lockedSyncCheck = isLockedSync(targetFileSync);
    assert.strictEqual(lockedSyncCheck, true, 'isLockedSync must return true when locked');

    releaseSync();
    const afterSyncReleaseCheck = lockfile.checkSync(targetFileSync);
    assert.strictEqual(afterSyncReleaseCheck, false, 'checkSync must return false after sync release');

    // 7. Synchronous lock and unlockSync
    lockfile.lockSync(targetFileSync);
    assert.strictEqual(lockfile.checkSync(targetFileSync), true, 'lockSync must acquire lock');
    releaseLockSync(targetFileSync);
    assert.strictEqual(lockfile.checkSync(targetFileSync), false, 'releaseLockSync must unlock file');

    console.log('Contract tests passed successfully.');
  } finally {
    try {
      if (fs.existsSync(tmpDir)) {
        fs.rmSync(tmpDir, { recursive: true, force: true });
      }
    } catch (e) {
      // Ignore cleanup error
    }
  }
}

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

原始种子者

匿名