CodeSampleX

샘플

node-addon-api 7.1.1: Napi::Function, Napi::Number, Napi::Object

검증된 샘플 — npm node-addon-api 7.1.1: Napi::Function, Napi::Number, Napi::Object. node 22 · linux alpine/x64 · docker에서 contract를 실행해 통과했습니다.

sha256:eefb6e92365d6aea6b771004fe25ff4bb21274caf48bec93d3c4ff2e3f5f05b6

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

실행 증거

선언된 환경과 서명된 실행을 분리해 두었습니다. 이 샘플이 무엇을 어디서 실행했는지 그대로 볼 수 있습니다.

증거 기준
서명된 컨트랙트 통과
검증 영수증
1
빌드한 서명 키
1
선언된 환경 node 22.23 linux 24 · ubuntu · glibc 2.39 x64 node 22.23 javascript npm 10

검증 실행 환경

환경 컨트랙트 단계 실행일
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-28

케이스

HOW
목표
verify pkg:npm/node-addon-api@7.1.1
패키지
심벌
  • Napi::Function
  • Napi::Number
  • Napi::Object
  • Napi::String
환경
node 22.23.2
생성일
2026-08-28T08:06:13Z

컨트랙트

  1. include directory exposes napi.h with C++ headers for Node-API addons
  2. napi.h declares Napi::String class inheriting from Name with New constructor and Utf8Value conversion
  3. napi.h declares Napi::Number class inheriting from Value with New constructor and DoubleValue/Int32Value conversion
  4. napi.h declares Napi::Object class inheriting from TypeTaggable with New constructor and Set/Get property methods
  5. napi.h declares Napi::Function class inheriting from Object with New constructor and Call method
  6. node-addon-api exports gyp path and targets file for build integration

파일

  • 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/node-addon-api@7.1.1
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/node-addon-api@7.1.1
Demonstrate these symbols/APIs:
  - Napi::Function
  - Napi::Number
  - Napi::Object
  - Napi::String

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:c3d112170eaebb22aac0d99424693da8b1c29aeb6ac42b8dd8fee22eae4a3277","contract":["include directory exposes napi.h with C++ headers for Node-API addons","napi.h declares Napi::String class inheriting from Name with New constructor and Utf8Value conversion","napi.h declares Napi::Number class inheriting from Value with New constructor and DoubleValue/Int32Value conversion","napi.h declares Napi::Object class inheriting from TypeTaggable with New constructor and Set/Get property methods","napi.h declares Napi::Function class inheriting from Object with New constructor and Call method","node-addon-api exports gyp path and targets file for build integration"],"goal":"verify pkg:npm/node-addon-api@7.1.1","kind":"HOW","packages":["pkg:npm/node-addon-api@7.1.1"],"schemaVersion":1,"symbols":["Napi::Function","Napi::Number","Napi::Object","Napi::String"]},"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/node-addon-api@7.1.1"],"schemaVersion":1,"subject":"pkg:npm/node-addon-api@7.1.1","symbols":["Napi::Function","Napi::Number","Napi::Object","Napi::String"],"verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "csx-sample-node-addon-api",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "csx-sample-node-addon-api",
      "version": "1.0.0",
      "license": "MIT-0",
      "dependencies": {
        "node-addon-api": "7.1.1"
      }
    },
    "node_modules/node-addon-api": {
      "version": "7.1.1",
      "resolved": "https://registry.npmjs.org/node-addon-api/-/node-addon-api-7.1.1.tgz",
      "integrity": "sha512-5m3bsyrjFWE1xf7nz7YXdN4udnVtXK6/Yfgn5qnahL6bCkf2yKt4k3nuTKAtT4r3IG8JNR2ncsIMdZuAzJjHQQ==",
      "license": "MIT"
    }
  }
}
package.json
{
  "name": "csx-sample-node-addon-api",
  "version": "1.0.0",
  "private": true,
  "type": "module",
  "license": "MIT-0",
  "scripts": {
    "test": "node test/contract.mjs"
  },
  "dependencies": {
    "node-addon-api": "7.1.1"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify pkg:npm/node-addon-api@7.1.1",
  "kind": "HOW",
  "packages": [
    "pkg:npm/node-addon-api@7.1.1"
  ],
  "symbols": [
    "Napi::Function",
    "Napi::Number",
    "Napi::Object",
    "Napi::String"
  ]
}
src/index.js
import fs from 'node:fs';
import path from 'node:path';
import { createRequire } from 'node:module';

const require = createRequire(import.meta.url);
const nodeAddonApi = require('node-addon-api');

/**
 * Returns the resolved include directory path for node-addon-api headers.
 * @returns {string} Path to headers.
 */
export function getIncludeDir() {
  const inc = nodeAddonApi.include;
  return inc ? inc.replace(/^"|"$/g, '') : '';
}

/**
 * Reads header file content from the include directory.
 * @param {string} headerName - e.g. 'napi.h' or 'napi-inl.h'
 * @returns {string} Header file content.
 */
export function readHeader(headerName) {
  const dir = getIncludeDir();
  const filePath = path.join(dir, headerName);
  return fs.readFileSync(filePath, 'utf8');
}

/**
 * Verifies that the C++ header exports core Node-API symbol class definitions and methods.
 * @returns {object} Map of verified symbol classes and their presence.
 */
export function verifyCoreSymbols() {
  const headerContent = readHeader('napi.h');
  return {
    hasString: /class String\s*:\s*public Name/.test(headerContent) &&
      /static String New\(napi_env env/.test(headerContent) &&
      /Utf8Value\(\)/.test(headerContent),
    hasNumber: /class Number\s*:\s*public Value/.test(headerContent) &&
      /static Number New\(napi_env env/.test(headerContent) &&
      /DoubleValue\(\)/.test(headerContent),
    hasObject: /class Object\s*:\s*public TypeTaggable/.test(headerContent) &&
      /static Object New\(napi_env env/.test(headerContent) &&
      headerContent.includes('Set(napi_value key') &&
      headerContent.includes('Get(napi_value key'),
    hasFunction: /class Function\s*:\s*public Object/.test(headerContent) &&
      /static Function New\(napi_env env/.test(headerContent) &&
      headerContent.includes('Call(const std::initializer_list<napi_value>& args)'),
  };
}

/**
 * Returns package configuration flags and paths.
 * @returns {object}
 */
export function getAddonConfig() {
  return {
    include: nodeAddonApi.include,
    include_dir: nodeAddonApi.include_dir,
    gyp: nodeAddonApi.gyp,
    targets: nodeAddonApi.targets,
    isNodeApiBuiltin: nodeAddonApi.isNodeApiBuiltin,
    needsFlag: nodeAddonApi.needsFlag,
  };
}

export default {
  getIncludeDir,
  readHeader,
  verifyCoreSymbols,
  getAddonConfig,
};
test/contract.mjs
import assert from 'node:assert/strict';
import fs from 'node:fs';
import { getIncludeDir, readHeader, verifyCoreSymbols, getAddonConfig } from '../src/index.js';

// 1. Verify include directory and header files
const includeDir = getIncludeDir();
assert.ok(includeDir && typeof includeDir === 'string', 'Include directory should be a non-empty string');
assert.ok(fs.existsSync(includeDir), 'Include directory should exist on disk');

const napiHeader = readHeader('napi.h');
assert.ok(napiHeader.length > 0, 'napi.h header should not be empty');

// 2. Napi::String verification in header
assert.ok(/class String\s*:\s*public Name/.test(napiHeader), 'napi.h must define Napi::String class');
assert.ok(/static String New\(napi_env env/.test(napiHeader), 'napi.h must declare Napi::String::New');
assert.ok(/Utf8Value\(\)/.test(napiHeader), 'napi.h must declare Utf8Value method');

// 3. Napi::Number verification in header
assert.ok(/class Number\s*:\s*public Value/.test(napiHeader), 'napi.h must define Napi::Number class');
assert.ok(/static Number New\(napi_env env/.test(napiHeader), 'napi.h must declare Napi::Number::New');
assert.ok(/DoubleValue\(\)/.test(napiHeader), 'napi.h must declare DoubleValue method');
assert.ok(/Int32Value\(\)/.test(napiHeader), 'napi.h must declare Int32Value method');

// 4. Napi::Object verification in header
assert.ok(/class Object\s*:\s*public TypeTaggable/.test(napiHeader), 'napi.h must define Napi::Object class');
assert.ok(/static Object New\(napi_env env/.test(napiHeader), 'napi.h must declare Napi::Object::New');
assert.ok(napiHeader.includes('Set(napi_value key'), 'napi.h must declare Set method on Object');
assert.ok(napiHeader.includes('Get(napi_value key'), 'napi.h must declare Get method on Object');

// 5. Napi::Function verification in header
assert.ok(/class Function\s*:\s*public Object/.test(napiHeader), 'napi.h must define Napi::Function class');
assert.ok(/static Function New\(napi_env env/.test(napiHeader), 'napi.h must declare Napi::Function::New');
assert.ok(napiHeader.includes('Call(const std::initializer_list<napi_value>& args)'), 'napi.h must declare Call method on Function');

// 6. Helper verification
const symbols = verifyCoreSymbols();
assert.deepStrictEqual(symbols, {
  hasString: true,
  hasNumber: true,
  hasObject: true,
  hasFunction: true,
});

// 7. Addon configuration and gyp integration exports
const config = getAddonConfig();
assert.ok(typeof config.include === 'string' && config.include.length > 0);
assert.ok(typeof config.include_dir === 'string' && config.include_dir.length > 0);
assert.ok(typeof config.gyp === 'string' && config.gyp.endsWith('nothing'));
assert.ok(typeof config.targets === 'string' && config.targets.endsWith('node_addon_api.gyp'));
assert.strictEqual(config.isNodeApiBuiltin, true);
assert.strictEqual(config.needsFlag, false);

console.log('All node-addon-api contract assertions passed successfully.');

오리진 시더

익명