CodeSampleX

示例

netmask 2.1.1: Parse IPv4 CIDR blocks and test IP containment and subnet iteration with Netmask

已验证示例 — npm netmask 2.1.1: Parse IPv4 CIDR blocks and test IP containment and subnet iteration with Netmask. contract 在 node 22 · linux alpine/x64 · docker…

sha256:415e82427893cbc87032611a9452a0c496b69db5875a564b79593c6f697730c5

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 1 表示只有作者;大于 1 表示还有其他人构建过。密钥是自行生成的,背后没有注册身份,因此计的是密钥而非人。 MIT-0

执行证据

声明的环境与签名的运行分开呈现,你可以看到这个样本究竟运行了什么、在哪里运行。

证据依据
签名契约通过
验证回执
2
构建过它的签名密钥
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-23
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-07

案例

HOW
目标
Parse IPv4 CIDR blocks and test IP containment and subnet iteration with Netmask
包
符号
  • Netmask
  • Netmask.prototype.contains
  • Netmask.prototype.forEach
环境
node 22.23.2
创建时间
2026-08-23T15:28:03Z

契约

  1. Netmask constructor parses CIDR notation and calculates base, mask, bitmask, hostmask, broadcast, size, first, and last properties
  2. Netmask constructor parses separate IP and netmask arguments correctly
  3. Netmask.prototype.contains validates whether an IPv4 address is within the subnet
  4. Netmask.prototype.contains validates whether a subnet block is entirely contained within another block
  5. Netmask.prototype.forEach iterates through all usable host IP addresses with index and integer representation
  6. Netmask.prototype.next navigates forward and backward to adjacent network blocks
  7. Netmask.prototype.toString returns the canonical CIDR block string
  8. Netmask constructor throws Error on invalid IP address or netmask format

文件

  • PROMPT.md
  • csx.json
  • 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: Parse IPv4 CIDR blocks and test IP containment and subnet iteration with Netmask
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/netmask@2.1.1
Demonstrate these symbols/APIs:
  - Netmask
  - Netmask.prototype.contains
  - Netmask.prototype.forEach

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:7cf2e0bae4f0e150ab90ee9aee5297bdef35570ce01dd5dc1213fc189d1a9eaf","contract":["Netmask constructor parses CIDR notation and calculates base, mask, bitmask, hostmask, broadcast, size, first, and last properties","Netmask constructor parses separate IP and netmask arguments correctly","Netmask.prototype.contains validates whether an IPv4 address is within the subnet","Netmask.prototype.contains validates whether a subnet block is entirely contained within another block","Netmask.prototype.forEach iterates through all usable host IP addresses with index and integer representation","Netmask.prototype.next navigates forward and backward to adjacent network blocks","Netmask.prototype.toString returns the canonical CIDR block string","Netmask constructor throws Error on invalid IP address or netmask format"],"goal":"Parse IPv4 CIDR blocks and test IP containment and subnet iteration with Netmask","kind":"HOW","packages":["pkg:npm/netmask@2.1.1"],"schemaVersion":1,"symbols":["Netmask","Netmask.prototype.contains","Netmask.prototype.forEach"]},"contractCommand":["node","test/contract.cjs"],"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.1"],"schemaVersion":1,"subject":"pkg:npm/netmask@2.1.1","symbols":["Netmask","Netmask.prototype.contains","Netmask.prototype.forEach"],"verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "netmask-sample",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "netmask-sample",
      "version": "1.0.0",
      "dependencies": {
        "netmask": "2.1.1"
      }
    },
    "node_modules/netmask": {
      "version": "2.1.1",
      "resolved": "https://registry.npmjs.org/netmask/-/netmask-2.1.1.tgz",
      "integrity": "sha512-eonl3sLUha+S1GzTPxychyhnUzKyeQkZ7jLjKrBagJgPla13F+uQ71HgpFefyHgqrjEbCPkDArxYsjY8/+gLKA==",
      "license": "MIT",
      "engines": {
        "node": ">= 0.4.0"
      }
    }
  }
}
package.json
{
  "name": "netmask-sample",
  "version": "1.0.0",
  "private": true,
  "description": "Verification sample for netmask 2.1.1",
  "dependencies": {
    "netmask": "2.1.1"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "Parse IPv4 CIDR blocks and test IP containment and subnet iteration with Netmask",
  "kind": "HOW",
  "packages": [
    "pkg:npm/netmask@2.1.1"
  ],
  "symbols": [
    "Netmask",
    "Netmask.prototype.contains",
    "Netmask.prototype.forEach"
  ]
}
test/contract.cjs
const assert = require("node:assert/strict");
const { Netmask } = require("netmask");

// 1. Netmask constructor parses CIDR notation and calculates base, mask, bitmask, hostmask, broadcast, size, first, and last properties
const block = new Netmask("192.168.1.0/24");
assert.equal(block.base, "192.168.1.0");
assert.equal(block.mask, "255.255.255.0");
assert.equal(block.bitmask, 24);
assert.equal(block.hostmask, "0.0.0.255");
assert.equal(block.broadcast, "192.168.1.255");
assert.equal(block.size, 256);
assert.equal(block.first, "192.168.1.1");
assert.equal(block.last, "192.168.1.254");

// 2. Netmask constructor parses separate IP and netmask arguments correctly
const blockWithMask = new Netmask("10.0.0.0", "255.255.0.0");
assert.equal(blockWithMask.base, "10.0.0.0");
assert.equal(blockWithMask.bitmask, 16);
assert.equal(blockWithMask.mask, "255.255.0.0");
assert.equal(blockWithMask.broadcast, "10.0.255.255");
assert.equal(blockWithMask.size, 65536);

// 3. Netmask.prototype.contains validates whether an IPv4 address is within the subnet
assert.equal(block.contains("192.168.1.1"), true);
assert.equal(block.contains("192.168.1.100"), true);
assert.equal(block.contains("192.168.1.254"), true);
assert.equal(block.contains("192.168.2.1"), false);
assert.equal(block.contains("10.0.0.1"), false);

// 4. Netmask.prototype.contains validates whether a subnet block is entirely contained within another block
const subBlock = new Netmask("192.168.1.64/26");
const outsideBlock = new Netmask("192.168.2.0/24");
const parentBlock = new Netmask("192.168.0.0/16");
assert.equal(block.contains(subBlock), true);
assert.equal(block.contains(outsideBlock), false);
assert.equal(block.contains(parentBlock), false);

// 5. Netmask.prototype.forEach iterates through all usable host IP addresses with index and integer representation
const smallBlock = new Netmask("172.16.0.0/29");
const visited = [];
smallBlock.forEach((ip, long, index) => {
  visited.push({ ip, index, isNumber: typeof long === "number" });
});

assert.equal(visited.length, 6);
assert.equal(visited[0].ip, "172.16.0.1");
assert.equal(visited[0].index, 0);
assert.equal(visited[0].isNumber, true);
assert.equal(visited[5].ip, "172.16.0.6");
assert.equal(visited[5].index, 5);

// 6. Netmask.prototype.next navigates forward and backward to adjacent network blocks
const nextBlock = block.next();
assert.equal(nextBlock.base, "192.168.2.0");
assert.equal(nextBlock.bitmask, 24);

const prevBlock = nextBlock.next(-1);
assert.equal(prevBlock.base, "192.168.1.0");
assert.equal(prevBlock.bitmask, 24);

// 7. Netmask.prototype.toString returns the canonical CIDR block string
assert.equal(block.toString(), "192.168.1.0/24");
assert.equal(smallBlock.toString(), "172.16.0.0/29");

// 8. Netmask constructor throws Error on invalid IP address or netmask format
assert.throws(
  () => new Netmask("999.999.999.999/24"),
  (err) => err instanceof Error && /Invalid/i.test(err.message)
);
assert.throws(
  () => new Netmask("192.168.1.0/35"),
  (err) => err instanceof Error && /Invalid/i.test(err.message)
);

console.log("Contract verified successfully.");

原始种子者

匿名