CodeSampleX

サンプル

netmask 2.1.2: Netmask

検証済みサンプル — npm netmask 2.1.2: Netmask. node 22 · linux debian/x64 · docker で contract を実行し、成功しました: new Netmask('192.0.2.42/24') normalizes base to…

sha256:86980da99e7564399ff947226eb1b0c4b75ff7f8cdefe8822421952236ace6ea

このネットワークが提供するのは一つだけです。ビルドされるサンプル。サンドボックスで実行し、署名済みの受領証を保管します。等級はつけず、何も保証しません — 同じコードがあなたの環境でビルドされるかは測定していません。 合格した契約受領証を提出した異なる署名鍵の数です。1 なら作者だけ、2 以上なら他の誰かもビルドしています。鍵は自己生成で背後に登録された身元がないため、数えているのは人ではなく鍵です。 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-22

ケース

HOW
ゴール
verify netmask.Netmask in pkg:npm/netmask@2.1.2
パッケージ
シンボル
  • netmask.Netmask
環境
node 22.23.2
作成日
2026-11-22T19:56:17Z

コントラクト

  1. new Netmask('192.0.2.42/24') normalizes base to '192.0.2.0', mask '255.255.255.0', hostmask '0.0.0.255', bitmask 24, size 256, first '192.0.2.1', last '192.0.2.254', broadcast '192.0.2.255', and toString() '192.0.2.0/24'
  2. new Netmask(net, mask) accepts separate dotted-quad or numeric bitmask arguments and produces identical CIDR blocks
  3. IPv4 /31 and /32 (including omitted mask defaulting to /32) set broadcast to undefined and include base in first/last
  4. contains() returns true for in-range single IPs, enclosed CIDR strings, and enclosed Netmask instances, and false for out-of-range or wider blocks
  5. next(count) returns adjacent Netmask instances for default (1), positive, and negative offsets
  6. forEach() iterates usable host addresses from first through last with sequential indices
  7. IPv6 CIDR inputs (e.g. '2001:db8::2/126') parse base, bitmask, size, first, last, set broadcast to undefined and maskLong/netLong to 0, and support contains() and next()
  8. Missing net parameter, out-of-range IPv4 (>32) or IPv6 (>128) bitmasks, and invalid addresses throw Error

ファイル

  • 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 netmask.Netmask in pkg:npm/netmask@2.1.2
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/netmask@2.1.2
Demonstrate these symbols/APIs:
  - netmask.Netmask
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:db9b080b8d846ebc97a485cc8aa6c83fcd711ca8a6fe496e5b5eec6d98ef6755","constraints":{"executionContext":"node"},"contract":["new Netmask('192.0.2.42/24') normalizes base to '192.0.2.0', mask '255.255.255.0', hostmask '0.0.0.255', bitmask 24, size 256, first '192.0.2.1', last '192.0.2.254', broadcast '192.0.2.255', and toString() '192.0.2.0/24'","new Netmask(net, mask) accepts separate dotted-quad or numeric bitmask arguments and produces identical CIDR blocks","IPv4 /31 and /32 (including omitted mask defaulting to /32) set broadcast to undefined and include base in first/last","contains() returns true for in-range single IPs, enclosed CIDR strings, and enclosed Netmask instances, and false for out-of-range or wider blocks","next(count) returns adjacent Netmask instances for default (1), positive, and negative offsets","forEach() iterates usable host addresses from first through last with sequential indices","IPv6 CIDR inputs (e.g. '2001:db8::2/126') parse base, bitmask, size, first, last, set broadcast to undefined and maskLong/netLong to 0, and support contains() and next()","Missing net parameter, out-of-range IPv4 (\u003e32) or IPv6 (\u003e128) bitmasks, and invalid addresses throw Error"],"goal":"verify netmask.Netmask in pkg:npm/netmask@2.1.2","kind":"HOW","packages":["pkg:npm/netmask@2.1.2"],"schemaVersion":1,"symbols":["netmask.Netmask"]},"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/netmask@2.1.2"],"schemaVersion":1,"subject":"pkg:npm/netmask@2.1.2","symbols":["netmask.Netmask"],"verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "netmask-netmask-sample",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "netmask-netmask-sample",
      "version": "1.0.0",
      "license": "MIT-0",
      "dependencies": {
        "netmask": "2.1.2"
      }
    },
    "node_modules/netmask": {
      "version": "2.1.2",
      "resolved": "https://registry.npmjs.org/netmask/-/netmask-2.1.2.tgz",
      "integrity": "sha512-In/qjEgPWP3EEKRBrATKSHRCcempKN/gUl97rrB1mTreVQfvPGthTJVwNXwbQHtIzzLf1bYyh8M50xpeYk0r6Q==",
      "license": "MIT",
      "engines": {
        "node": ">= 0.4.0"
      }
    }
  }
}
package.json
{
  "name": "netmask-netmask-sample",
  "version": "1.0.0",
  "private": true,
  "license": "MIT-0",
  "scripts": {
    "test": "node test/contract.js"
  },
  "dependencies": {
    "netmask": "2.1.2"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify netmask.Netmask in pkg:npm/netmask@2.1.2",
  "kind": "HOW",
  "packages": [
    "pkg:npm/netmask@2.1.2"
  ],
  "symbols": [
    "netmask.Netmask"
  ],
  "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 { Netmask } = require('netmask');

function inspectBlock(net, mask) {
  const block = mask === undefined ? new Netmask(net) : new Netmask(net, mask);
  return {
    cidr: block.toString(),
    base: block.base,
    mask: block.mask,
    hostmask: block.hostmask,
    bitmask: block.bitmask,
    size: block.size,
    first: block.first,
    last: block.last,
    broadcast: block.broadcast,
    block,
  };
}

function listHosts(block) {
  const hosts = [];
  block.forEach((ip, _long, index) => {
    hosts.push({ index, ip });
  });
  return hosts;
}

module.exports = {
  inspectBlock,
  listHosts,
};
test/contract.js
'use strict';

const assert = require('node:assert/strict');
const { Netmask } = require('netmask');
const { inspectBlock, listHosts } = require('../src/index');

// 1. IPv4 CIDR parsing normalizes host bits and computes network metadata
const v4 = inspectBlock('192.0.2.42/24');
assert.equal(v4.cidr, '192.0.2.0/24');
assert.equal(v4.base, '192.0.2.0');
assert.equal(v4.mask, '255.255.255.0');
assert.equal(v4.hostmask, '0.0.0.255');
assert.equal(v4.bitmask, 24);
assert.equal(v4.size, 256);
assert.equal(v4.first, '192.0.2.1');
assert.equal(v4.last, '192.0.2.254');
assert.equal(v4.broadcast, '192.0.2.255');

// 2. Separate dotted-quad and numeric mask arguments produce identical blocks
const fromDotted = inspectBlock('192.0.2.19', '255.255.255.240');
const fromNumber = inspectBlock('192.0.2.19', 28);
assert.equal(fromDotted.cidr, '192.0.2.16/28');
assert.equal(fromNumber.cidr, '192.0.2.16/28');
assert.equal(fromDotted.size, 16);
assert.equal(fromDotted.first, '192.0.2.17');
assert.equal(fromDotted.last, '192.0.2.30');
assert.equal(fromDotted.broadcast, '192.0.2.31');

// 3. /31 and /32 (default mask when omitted) set broadcast to undefined
const p2p = new Netmask('192.0.2.4/31');
assert.equal(p2p.size, 2);
assert.equal(p2p.first, '192.0.2.4');
assert.equal(p2p.last, '192.0.2.5');
assert.equal(p2p.broadcast, undefined);

const singleHost = new Netmask('192.0.2.9');
assert.equal(singleHost.bitmask, 32);
assert.equal(singleHost.size, 1);
assert.equal(singleHost.first, '192.0.2.9');
assert.equal(singleHost.last, '192.0.2.9');
assert.equal(singleHost.broadcast, undefined);

// 4. contains() accepts single IP strings, CIDR strings, and Netmask instances
assert.equal(v4.block.contains('192.0.2.0'), true);
assert.equal(v4.block.contains('192.0.2.255'), true);
assert.equal(v4.block.contains('192.0.3.1'), false);
assert.equal(v4.block.contains('192.0.2.16/28'), true);
assert.equal(v4.block.contains('192.0.0.0/22'), false);
assert.equal(v4.block.contains(fromDotted.block), true);
assert.equal(fromDotted.block.contains(v4.block), false);

// 5. next() returns adjacent Netmask blocks for default, positive, and negative offsets
const nextOne = fromDotted.block.next();
assert.ok(nextOne instanceof Netmask);
assert.equal(nextOne.toString(), '192.0.2.32/28');
assert.equal(fromDotted.block.next(2).toString(), '192.0.2.48/28');
assert.equal(nextOne.next(-1).toString(), '192.0.2.16/28');

// 6. forEach() iterates host addresses from first to last with sequential indices
const small = new Netmask('192.0.2.0/30');
assert.deepEqual(listHosts(small), [
  { index: 0, ip: '192.0.2.1' },
  { index: 1, ip: '192.0.2.2' },
]);

// 7. IPv6 CIDR parsing, containment, and next() with maskLong/netLong zeroed and broadcast undefined
const v6 = inspectBlock('2001:db8::2/126');
assert.equal(v6.cidr, '2001:db8::/126');
assert.equal(v6.base, '2001:db8::');
assert.equal(v6.bitmask, 126);
assert.equal(v6.size, 4);
assert.equal(v6.first, '2001:db8::');
assert.equal(v6.last, '2001:db8::3');
assert.equal(v6.broadcast, undefined);
assert.equal(v6.block.maskLong, 0);
assert.equal(v6.block.netLong, 0);
assert.equal(v6.block.contains('2001:db8::3'), true);
assert.equal(v6.block.contains('2001:db8::4'), false);
assert.equal(v6.block.contains('2001:db8::/127'), true);
assert.equal(v6.block.next().toString(), '2001:db8::4/126');

// 8. Invalid inputs throw Error
assert.throws(() => new Netmask(), /Missing `net' parameter/);
assert.throws(() => new Netmask('192.0.2.0/33'), /Invalid mask/);
assert.throws(() => new Netmask('2001:db8::/129'), /Invalid mask for IPv6/);
assert.throws(() => new Netmask('999.0.0.1/24'), /Invalid net address/);

console.log('netmask.Netmask contract passed');

オリジンシーダー

匿名