CodeSampleX

示例

@hono/node-server 2.1.1: getConnInfo

已验证示例 — npm @hono/node-server 2.1.1: getConnInfo. contract 在 node 22 · linux debian/x64 · docker 上运行并通过: getConnInfo is an exported function from…

sha256:1628291b7e7ae82eca717952f3d6d95dbc5f28406b2c5df1667a25b232eebe9f

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

执行证据

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

证据依据
签名契约通过
验证回执
1
构建过它的签名密钥
1
声明的环境 linux 24 · ubuntu · glibc 2.39 x64 npm

验证运行环境

环境 契约 阶段 运行日期
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-06

案例

HOW
目标
verify @hono/node-server.getConnInfo in pkg:npm/%40hono/node-server@2.1.1
包
符号
  • @hono/node-server.getConnInfo
创建时间
2026-09-06T01:24:19Z

契约

  1. getConnInfo is an exported function from @hono/node-server/conninfo
  2. getConnInfo extracts remote address, port, and IPv4 addressType from c.env.incoming.socket
  3. getConnInfo extracts remote connection details when incoming socket is under c.env.server.incoming
  4. getConnInfo detects IPv6 addressType when remoteFamily is IPv6
  5. getConnInfo sets addressType to undefined when remoteFamily is neither IPv4 nor IPv6
  6. getConnInfo extracts live connection information from a running HTTP server request socket

文件

  • 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 @hono/node-server.getConnInfo in pkg:npm/%40hono/node-server@2.1.1
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/%40hono/node-server@2.1.1
Demonstrate these symbols/APIs:
  - @hono/node-server.getConnInfo

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:43ab6114a17ab044f150e20fb8fb52570f37247b9ba0b2a923e8399d9c8b3036","contract":["getConnInfo is an exported function from @hono/node-server/conninfo","getConnInfo extracts remote address, port, and IPv4 addressType from c.env.incoming.socket","getConnInfo extracts remote connection details when incoming socket is under c.env.server.incoming","getConnInfo detects IPv6 addressType when remoteFamily is IPv6","getConnInfo sets addressType to undefined when remoteFamily is neither IPv4 nor IPv6","getConnInfo extracts live connection information from a running HTTP server request socket"],"goal":"verify @hono/node-server.getConnInfo in pkg:npm/%40hono/node-server@2.1.1","kind":"HOW","packages":["pkg:npm/%40hono/node-server@2.1.1"],"schemaVersion":1,"symbols":["@hono/node-server.getConnInfo"]},"contractCommand":["node","test/contract.mjs"],"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/%40hono/node-server@2.1.1"],"schemaVersion":1,"subject":"pkg:npm/%40hono/node-server@2.1.1","symbols":["@hono/node-server.getConnInfo"],"verifierAdapter":"node-typescript@1"}
index.js
export { getConnInfo } from '@hono/node-server/conninfo';
package-lock.json
{
  "name": "sample-hono-node-server-getconninfo",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-hono-node-server-getconninfo",
      "version": "1.0.0",
      "license": "MIT-0",
      "dependencies": {
        "@hono/node-server": "2.1.1"
      }
    },
    "node_modules/@hono/node-server": {
      "version": "2.1.1",
      "resolved": "https://registry.npmjs.org/@hono/node-server/-/node-server-2.1.1.tgz",
      "integrity": "sha512-ELuehkj5VCBdgEw9zs+ivkKwyzzUCSQuE96YmiPvn1ECBoZCczbFXJLeEGMTYjphP6gydh4pHMqEYPVMYUVgQg==",
      "license": "MIT",
      "engines": {
        "node": ">=20"
      },
      "peerDependencies": {
        "hono": "^4"
      }
    },
    "node_modules/hono": {
      "version": "4.13.7",
      "resolved": "https://registry.npmjs.org/hono/-/hono-4.13.7.tgz",
      "integrity": "sha512-c8/gF9ac8Y78/agExVocyLevgR+JlpNB444Py0FSX8pJoPdYUfUzRcXtYEYGwt6l19qIlVZPN5Mfsw9jFShmQQ==",
      "license": "MIT",
      "peer": true,
      "engines": {
        "node": ">=16.9.0"
      }
    }
  }
}
package.json
{
  "name": "sample-hono-node-server-getconninfo",
  "version": "1.0.0",
  "private": true,
  "description": "verify @hono/node-server.getConnInfo in pkg:npm/%40hono/node-server@2.1.1",
  "type": "module",
  "main": "index.js",
  "scripts": {
    "test": "node test/contract.mjs"
  },
  "dependencies": {
    "@hono/node-server": "2.1.1"
  },
  "license": "MIT-0"
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify @hono/node-server.getConnInfo in pkg:npm/%40hono/node-server@2.1.1",
  "kind": "HOW",
  "packages": [
    "pkg:npm/%40hono/node-server@2.1.1"
  ],
  "symbols": [
    "@hono/node-server.getConnInfo"
  ]
}
test/contract.mjs
import assert from 'node:assert';
import http from 'node:http';
import { getConnInfo } from '../index.js';

// 1. getConnInfo is an exported function from @hono/node-server/conninfo
assert.strictEqual(typeof getConnInfo, 'function', 'getConnInfo must be an exported function');

// 2. getConnInfo extracts remote address, port, and IPv4 addressType from c.env.incoming.socket
const ctxDirect = {
  env: {
    incoming: {
      socket: {
        remoteAddress: '127.0.0.1',
        remotePort: 54321,
        remoteFamily: 'IPv4'
      }
    }
  }
};
const directInfo = getConnInfo(ctxDirect);
assert.deepStrictEqual(directInfo, {
  remote: {
    address: '127.0.0.1',
    port: 54321,
    addressType: 'IPv4'
  }
});

// 3. getConnInfo extracts remote connection details when incoming socket is under c.env.server.incoming
const ctxNested = {
  env: {
    server: {
      incoming: {
        socket: {
          remoteAddress: '192.168.1.100',
          remotePort: 8080,
          remoteFamily: 'IPv4'
        }
      }
    }
  }
};
const nestedInfo = getConnInfo(ctxNested);
assert.deepStrictEqual(nestedInfo, {
  remote: {
    address: '192.168.1.100',
    port: 8080,
    addressType: 'IPv4'
  }
});

// 4. getConnInfo detects IPv6 addressType when remoteFamily is IPv6
const ctxIPv6 = {
  env: {
    incoming: {
      socket: {
        remoteAddress: '::1',
        remotePort: 9000,
        remoteFamily: 'IPv6'
      }
    }
  }
};
const ipv6Info = getConnInfo(ctxIPv6);
assert.deepStrictEqual(ipv6Info, {
  remote: {
    address: '::1',
    port: 9000,
    addressType: 'IPv6'
  }
});

// 5. getConnInfo sets addressType to undefined when remoteFamily is neither IPv4 nor IPv6
const ctxUnknownFamily = {
  env: {
    incoming: {
      socket: {
        remoteAddress: 'custom-pipe',
        remotePort: undefined,
        remoteFamily: 'UnknownFamily'
      }
    }
  }
};
const unknownInfo = getConnInfo(ctxUnknownFamily);
assert.deepStrictEqual(unknownInfo, {
  remote: {
    address: 'custom-pipe',
    port: undefined,
    addressType: undefined
  }
});

// 6. getConnInfo extracts live connection information from a running HTTP server request socket
const server = http.createServer((req, res) => {
  const liveContext = {
    env: {
      incoming: req
    }
  };
  const liveInfo = getConnInfo(liveContext);
  assert(liveInfo && liveInfo.remote, 'remote property must exist');
  assert(typeof liveInfo.remote.address === 'string', 'address must be string');
  assert(typeof liveInfo.remote.port === 'number' && liveInfo.remote.port > 0, 'port must be positive number');
  assert(['IPv4', 'IPv6'].includes(liveInfo.remote.addressType), 'addressType must be IPv4 or IPv6');

  res.statusCode = 200;
  res.end('ok');
});

await new Promise((resolve, reject) => {
  server.listen(0, '127.0.0.1', () => {
    const addressInfo = server.address();
    const port = addressInfo.port;
    const clientReq = http.get(`http://127.0.0.1:${port}`, (res) => {
      res.resume();
      res.on('end', () => {
        server.close((err) => {
          if (err) reject(err);
          else resolve();
        });
      });
    });
    clientReq.on('error', reject);
  });
});

console.log('Contract tests passed successfully.');

原始种子者

匿名