CodeSampleX

示例

@hono/node-server 2.1.0: serve

已验证示例 — npm @hono/node-server 2.1.0: serve. contract 在 node 22 · linux debian/x64 · docker 上运行并通过: @hono/node-server exports serve function

sha256:0c294e68ac37d664840045ec5b18ee4bc7788c70633258f6a720c64e6451fc4f

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

案例

HOW
目标
verify serve in pkg:npm/%40hono/node-server@2.1.0
包
符号
  • serve
环境
node 22.23.2
创建时间
2026-09-01T22:29:32Z

契约

  1. @hono/node-server exports serve function
  2. serve starts an HTTP server and returns an http.Server instance
  3. serve invokes listeningListener with server AddressInfo including port and address
  4. serve dispatches HTTP GET requests to fetch handler and responds with status, headers, and body
  5. serve dispatches HTTP POST requests with JSON payload to fetch handler
  6. serve supports custom createServer factory function in options
  7. serve HTTP server can be closed via server.close()

文件

  • PROMPT.md
  • csx.json
  • 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 serve in pkg:npm/%40hono/node-server@2.1.0
Kind: HOW

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

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:8a42e6ed0f241dda40e3986de00c20659340b2a05b51b261a0454271cf71921c","contract":["@hono/node-server exports serve function","serve starts an HTTP server and returns an http.Server instance","serve invokes listeningListener with server AddressInfo including port and address","serve dispatches HTTP GET requests to fetch handler and responds with status, headers, and body","serve dispatches HTTP POST requests with JSON payload to fetch handler","serve supports custom createServer factory function in options","serve HTTP server can be closed via server.close()"],"goal":"verify serve in pkg:npm/%40hono/node-server@2.1.0","kind":"HOW","packages":["pkg:npm/%40hono/node-server@2.1.0"],"schemaVersion":1,"symbols":["serve"]},"contractCommand":["node","test/contract.mjs"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"npm","language":"javascript","libc":"glibc","libcVersion":"2.39","moduleSystem":"esm","os":"linux","osVersionBucket":"24","packageManager":"npm","packageManagerVersion":"10.9.8","runtime":"node","runtimeVersion":"22.23.2","schemaVersion":1},"license":"MIT-0","packages":["pkg:npm/%40hono/node-server@2.1.0"],"schemaVersion":1,"subject":"pkg:npm/%40hono/node-server@2.1.0","symbols":["serve"],"verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "sample-hono-node-server-serve",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-hono-node-server-serve",
      "version": "1.0.0",
      "license": "MIT-0",
      "dependencies": {
        "@hono/node-server": "2.1.0"
      }
    },
    "node_modules/@hono/node-server": {
      "version": "2.1.0",
      "resolved": "https://registry.npmjs.org/@hono/node-server/-/node-server-2.1.0.tgz",
      "integrity": "sha512-XovyyCCnBzW+zKu+z/zq8hwNs4KOR5rEMAOxo2f40Q5xoOI37IMm6MIg2COOUtUApo0i6850MTBKH2u4QLGIqg==",
      "license": "MIT",
      "engines": {
        "node": ">=20"
      },
      "peerDependencies": {
        "hono": "^4"
      }
    },
    "node_modules/hono": {
      "version": "4.13.5",
      "resolved": "https://registry.npmjs.org/hono/-/hono-4.13.5.tgz",
      "integrity": "sha512-O6+/eCYRkzzzy0rPWwKLiGBR1nFuUPZynnwjxN1MBA62NNqbT0wQEzQyK2gSO5yDIDB336sXQleAhOHrzlYyKw==",
      "license": "MIT",
      "peer": true,
      "engines": {
        "node": ">=16.9.0"
      }
    }
  }
}
package.json
{
  "name": "sample-hono-node-server-serve",
  "version": "1.0.0",
  "type": "module",
  "private": true,
  "license": "MIT-0",
  "dependencies": {
    "@hono/node-server": "2.1.0"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify serve in pkg:npm/%40hono/node-server@2.1.0",
  "kind": "HOW",
  "packages": [
    "pkg:npm/%40hono/node-server@2.1.0"
  ],
  "symbols": [
    "serve"
  ]
}
test/contract.mjs
import assert from 'node:assert/strict';
import http from 'node:http';
import { serve, createAdaptorServer } from '@hono/node-server';

// Contract 1: @hono/node-server exports serve function
{
  assert.strictEqual(typeof serve, 'function');
  assert.strictEqual(typeof createAdaptorServer, 'function');
}

// Contract 2: serve starts an HTTP server and returns an http.Server instance
// Contract 3: serve invokes listeningListener with server AddressInfo including port and address
// Contract 4: serve dispatches HTTP GET requests to fetch handler and responds with status, headers, and body
// Contract 5: serve dispatches HTTP POST requests with JSON payload to fetch handler
{
  let listeningInfo = null;
  const server = serve(
    {
      fetch: async (req) => {
        const url = new URL(req.url);
        if (url.pathname === '/test') {
          return new Response(JSON.stringify({ ok: true, method: req.method }), {
            status: 200,
            headers: { 'content-type': 'application/json' },
          });
        }
        if (url.pathname === '/echo' && req.method === 'POST') {
          const body = await req.json();
          return new Response(JSON.stringify({ received: body }), {
            status: 201,
            headers: { 'content-type': 'application/json' },
          });
        }
        return new Response('Not Found', { status: 404 });
      },
      port: 0,
      hostname: '127.0.0.1',
    },
    (info) => {
      listeningInfo = info;
    }
  );

  assert.ok(server instanceof http.Server);

  await new Promise((resolve) => {
    if (server.listening) resolve();
    else server.once('listening', resolve);
  });

  const address = server.address();
  assert.ok(address && typeof address === 'object');
  assert.strictEqual(typeof address.port, 'number');
  assert.strictEqual(address.address, '127.0.0.1');
  assert.deepStrictEqual(listeningInfo, address);

  const baseUrl = `http://127.0.0.1:${address.port}`;

  // GET request
  const getRes = await fetch(`${baseUrl}/test`);
  assert.strictEqual(getRes.status, 200);
  assert.strictEqual(getRes.headers.get('content-type'), 'application/json');
  const getData = await getRes.json();
  assert.deepStrictEqual(getData, { ok: true, method: 'GET' });

  // POST request
  const postRes = await fetch(`${baseUrl}/echo`, {
    method: 'POST',
    headers: { 'content-type': 'application/json' },
    body: JSON.stringify({ message: 'hono-node-server' }),
  });
  assert.strictEqual(postRes.status, 201);
  const postData = await postRes.json();
  assert.deepStrictEqual(postData, { received: { message: 'hono-node-server' } });

  // 404 Not Found
  const notFoundRes = await fetch(`${baseUrl}/missing`);
  assert.strictEqual(notFoundRes.status, 404);

  // Contract 7: serve HTTP server can be closed via server.close()
  await new Promise((resolve, reject) => {
    server.close((err) => (err ? reject(err) : resolve()));
  });
  assert.strictEqual(server.listening, false);
}

// Contract 6: serve supports custom createServer factory function in options
{
  let customFactoryCalled = false;
  const server = serve({
    fetch: () => new Response('custom factory response'),
    port: 0,
    hostname: '127.0.0.1',
    createServer: (options, listener) => {
      customFactoryCalled = true;
      return http.createServer(options, listener);
    },
  });

  assert.strictEqual(customFactoryCalled, true);
  assert.ok(server instanceof http.Server);

  await new Promise((resolve) => {
    if (server.listening) resolve();
    else server.once('listening', resolve);
  });

  const address = server.address();
  const res = await fetch(`http://127.0.0.1:${address.port}/`);
  assert.strictEqual(res.status, 200);
  const text = await res.text();
  assert.strictEqual(text, 'custom factory response');

  await new Promise((resolve, reject) => {
    server.close((err) => (err ? reject(err) : resolve()));
  });
  assert.strictEqual(server.listening, false);
}

console.log('All @hono/node-server serve contract tests passed.');

原始种子者

匿名