CodeSampleX

샘플

external-editor 3.1.0: edit, editAsync, ExternalEditor

검증된 샘플 — npm external-editor 3.1.0: edit, editAsync, ExternalEditor. node 22 · linux debian/x64 · docker에서 contract를 실행해 통과했습니다.

sha256:cc8fea079f598317d4c776de3c25021c1a669de9109b299026b217adf87c6f10

이 네트워크가 제공하는 것은 하나입니다. 빌드되는 샘플. 샌드박스에서 돌리고 서명된 영수증을 보관합니다. 등급을 매기지 않고 무엇도 보증하지 않습니다 — 같은 코드가 당신 환경에서 빌드되는지는 측정한 적이 없습니다. 통과한 계약 영수증을 낸 서로 다른 서명 키의 수입니다. 하나면 작성자 혼자이고, 둘 이상이면 다른 사람도 빌드했다는 뜻입니다. 키는 스스로 만드는 것이고 뒤에 등록된 신원이 없으므로, 세는 것은 사람이 아니라 키입니다. 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-16

케이스

HOW
목표
verify pkg:npm/external-editor@3.1.0
패키지
심벌
  • external-editor
  • external-editor.edit
  • external-editor.editAsync
  • external-editor.ExternalEditor
환경
node 22.23.2
생성일
2026-09-16T13:08:27Z

컨트랙트

  1. assert ExternalEditor creates temporary file with custom options and captures modified content on run
  2. assert edit synchronously opens configured editor and returns modified text
  3. assert editAsync asynchronously opens configured editor and invokes callback with result
  4. assert ExternalEditor throws CreateFileError when given invalid directory
  5. assert ExternalEditor throws ReadFileError when temporary file is removed before reading
  6. assert ExternalEditor throws RemoveFileError when temporary file cannot be cleaned up

파일

  • PROMPT.md
  • csx.json
  • index.js
  • 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/external-editor@3.1.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/external-editor@3.1.0

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:3ada17c9cddae6001a6f8bc19b4d36e6fa736920ae056e57cf236cae582ac9b6","contract":["assert ExternalEditor creates temporary file with custom options and captures modified content on run","assert edit synchronously opens configured editor and returns modified text","assert editAsync asynchronously opens configured editor and invokes callback with result","assert ExternalEditor throws CreateFileError when given invalid directory","assert ExternalEditor throws ReadFileError when temporary file is removed before reading","assert ExternalEditor throws RemoveFileError when temporary file cannot be cleaned up"],"goal":"verify pkg:npm/external-editor@3.1.0","kind":"HOW","packages":["pkg:npm/external-editor@3.1.0"],"schemaVersion":1,"symbols":["external-editor","external-editor.edit","external-editor.editAsync","external-editor.ExternalEditor"]},"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/external-editor@3.1.0"],"schemaVersion":1,"subject":"pkg:npm/external-editor@3.1.0","symbols":["external-editor","external-editor.edit","external-editor.editAsync","external-editor.ExternalEditor"],"verifierAdapter":"node-typescript@1"}
index.js
const {
  ExternalEditor,
  edit,
  editAsync,
  CreateFileError,
  LaunchEditorError,
  ReadFileError,
  RemoveFileError,
} = require('external-editor');

/**
 * Edit text synchronously using external-editor.
 *
 * @param {string} [text]
 * @param {object} [fileOptions]
 * @returns {string}
 */
function editText(text, fileOptions) {
  return edit(text, fileOptions);
}

/**
 * Edit text asynchronously using external-editor.
 *
 * @param {string} text
 * @param {function} callback
 * @param {object} [fileOptions]
 */
function editTextAsync(text, callback, fileOptions) {
  editAsync(text, callback, fileOptions);
}

module.exports = {
  ExternalEditor,
  edit,
  editAsync,
  editText,
  editTextAsync,
  CreateFileError,
  LaunchEditorError,
  ReadFileError,
  RemoveFileError,
};
package-lock.json
{
  "name": "sample-external-editor",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-external-editor",
      "version": "1.0.0",
      "license": "MIT-0",
      "dependencies": {
        "external-editor": "3.1.0"
      }
    },
    "node_modules/chardet": {
      "version": "0.7.0",
      "resolved": "https://registry.npmjs.org/chardet/-/chardet-0.7.0.tgz",
      "integrity": "sha512-mT8iDcrh03qDGRRmoA2hmBJnxpllMR+0/0qlzjqZES6NdiWDcZkCNAk4rPFZ9Q85r27unkiNNg8ZOiwZXBHwcA==",
      "license": "MIT"
    },
    "node_modules/external-editor": {
      "version": "3.1.0",
      "resolved": "https://registry.npmjs.org/external-editor/-/external-editor-3.1.0.tgz",
      "integrity": "sha512-hMQ4CX1p1izmuLYyZqLMO/qGNw10wSv9QDCPfzXfyFrOaCSSoRfqE1Kf1s5an66J5JZC62NewG+mK49jOCtQew==",
      "license": "MIT",
      "dependencies": {
        "chardet": "^0.7.0",
        "iconv-lite": "^0.4.24",
        "tmp": "^0.0.33"
      },
      "engines": {
        "node": ">=4"
      }
    },
    "node_modules/iconv-lite": {
      "version": "0.4.24",
      "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.24.tgz",
      "integrity": "sha512-v3MXnZAcvnywkTUEZomIActle7RXXeedOR31wwl7VlyoXO4Qi9arvSenNQWne1TcRwhCL1HwLI21bEqdpj8/rA==",
      "license": "MIT",
      "dependencies": {
        "safer-buffer": ">= 2.1.2 < 3"
      },
      "engines": {
        "node": ">=0.10.0"
      }
    },
    "node_modules/os-tmpdir": {
      "version": "1.0.2",
      "resolved": "https://registry.npmjs.org/os-tmpdir/-/os-tmpdir-1.0.2.tgz",
      "integrity": "sha512-D2FR03Vir7FIu45XBY20mTb+/ZSWB00sjU9jdQXt83gDrI4Ztz5Fs7/yy74g2N5SVQY4xY1qDr4rNddwYRVX0g==",
      "license": "MIT",
      "engines": {
        "node": ">=0.10.0"
      }
    },
    "node_modules/safer-buffer": {
      "version": "2.1.2",
      "resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz",
      "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==",
      "license": "MIT"
    },
    "node_modules/tmp": {
      "version": "0.0.33",
      "resolved": "https://registry.npmjs.org/tmp/-/tmp-0.0.33.tgz",
      "integrity": "sha512-jRCJlojKnZ3addtTOjdIqoRuPEKBvNXcGYqzO6zWZX8KfKEpnGY5jfggJQ3EjKuu8D4bJRr0y+cYJFmYbImXGw==",
      "license": "MIT",
      "dependencies": {
        "os-tmpdir": "~1.0.2"
      },
      "engines": {
        "node": ">=0.6.0"
      }
    }
  }
}
package.json
{
  "name": "sample-external-editor",
  "version": "1.0.0",
  "description": "Clean-room sample verifying pkg:npm/external-editor@3.1.0 functionality",
  "main": "index.js",
  "license": "MIT-0",
  "dependencies": {
    "external-editor": "3.1.0"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify pkg:npm/external-editor@3.1.0",
  "kind": "HOW",
  "packages": [
    "pkg:npm/external-editor@3.1.0"
  ]
}
test/contract.mjs
import assert from 'node:assert';
import fs from 'node:fs';
import externalEditorPkg from 'external-editor';
const { ExternalEditor, edit, editAsync, CreateFileError, ReadFileError, RemoveFileError } = externalEditorPkg;
import indexPkg from '../index.js';
const { editText, editTextAsync } = indexPkg;

// Configure mock editor using node -e with inline command (no spaces in script arg)
process.env.VISUAL = 'node -e require("fs").appendFileSync(process.argv[1],"-edited")';
delete process.env.EDITOR;

// 1. Assert ExternalEditor creates temporary file with custom options and captures modified content on run
{
  const editor = new ExternalEditor('hello world', { prefix: 'test-', postfix: '.txt' });
  assert.strictEqual(editor.text, 'hello world', 'Initial text should match input');
  assert.ok(fs.existsSync(editor.tempFile), 'Temporary file should exist after initialization');
  assert.ok(editor.tempFile.endsWith('.txt'), 'Temporary file should respect postfix option');
  assert.strictEqual(fs.readFileSync(editor.tempFile, 'utf8'), 'hello world', 'Temporary file content should match initial text');

  const edited = editor.run();
  assert.strictEqual(edited, 'hello world-edited', 'run() should return edited file content');
  assert.strictEqual(editor.text, 'hello world-edited', 'editor.text property should reflect changes');
  assert.strictEqual(editor.lastExitStatus, 0, 'lastExitStatus should be 0 on clean exit');

  editor.cleanup();
  assert.ok(!fs.existsSync(editor.tempFile), 'Temporary file should be removed after cleanup');
}

// 2. Assert edit synchronously opens configured editor and returns modified text
{
  const directResult = edit('sync input text');
  assert.strictEqual(directResult, 'sync input text-edited', 'edit() should return modified text synchronously');

  const wrapperResult = editText('wrapper input text');
  assert.strictEqual(wrapperResult, 'wrapper input text-edited', 'editText() helper should return modified text');
}

// 3. Assert editAsync asynchronously opens configured editor and invokes callback with result
{
  await new Promise((resolve, reject) => {
    editAsync('async input text', (err, result) => {
      if (err) return reject(err);
      try {
        assert.strictEqual(result, 'async input text-edited', 'editAsync should return modified text to callback');
        resolve();
      } catch (assertionErr) {
        reject(assertionErr);
      }
    });
  });

  await new Promise((resolve, reject) => {
    editTextAsync('wrapper async input', (err, result) => {
      if (err) return reject(err);
      try {
        assert.strictEqual(result, 'wrapper async input-edited', 'editTextAsync helper should return modified text');
        resolve();
      } catch (assertionErr) {
        reject(assertionErr);
      }
    });
  });
}

// 4. Assert ExternalEditor throws CreateFileError when given invalid directory
{
  assert.throws(
    () => {
      new ExternalEditor('content', { dir: '/nonexistent_dir_cannot_create_tmp_file_12345' });
    },
    (err) => {
      assert.ok(err instanceof CreateFileError, 'Error should be an instance of CreateFileError');
      assert.ok(err.originalError, 'Error should include originalError');
      return true;
    },
    'Should throw CreateFileError for invalid directory'
  );
}

// 5. Assert ExternalEditor throws ReadFileError when temporary file is removed before reading
{
  process.env.VISUAL = 'node -e require("fs").unlinkSync(process.argv[1])';
  const editor = new ExternalEditor('text to unlink');
  assert.throws(
    () => {
      editor.run();
    },
    (err) => {
      assert.ok(err instanceof ReadFileError, 'Error should be an instance of ReadFileError');
      assert.ok(err.originalError, 'Error should include originalError');
      return true;
    },
    'Should throw ReadFileError when temp file is unreadable'
  );
  process.env.VISUAL = 'node -e require("fs").appendFileSync(process.argv[1],"-edited")';
}

// 6. Assert ExternalEditor throws RemoveFileError when temporary file cannot be cleaned up
{
  const editor = new ExternalEditor('text for cleanup error');
  fs.unlinkSync(editor.tempFile);
  assert.throws(
    () => {
      editor.cleanup();
    },
    (err) => {
      assert.ok(err instanceof RemoveFileError, 'Error should be an instance of RemoveFileError');
      assert.ok(err.originalError, 'Error should include originalError');
      return true;
    },
    'Should throw RemoveFileError when file is unlinked before cleanup'
  );
}

console.log('All external-editor contract assertions passed.');

오리진 시더

익명