CodeSampleX

示例

tldts 7.4.12: getDomain, getHostname, getPublicSuffix

已验证示例 — npm tldts 7.4.12: getDomain, getHostname, getPublicSuffix. contract 在 node 22 · linux debian/x64 · docker 上运行并通过: getHostname extracts hostname from…

sha256:cf6df3609efa51432aa3f7806bec576814d50c73e6b9b2873770067b7d32e55b

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 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-13

案例

HOW
目标
verify pkg:npm/tldts@7.4.12
包
符号
  • getDomain
  • getHostname
  • getPublicSuffix
  • parse
创建时间
2026-09-13T13:45:22Z

契约

  1. getHostname extracts hostname from full URLs, URLs with ports, and bare domain strings
  2. getDomain extracts registrable domain from hostname and URLs
  3. getPublicSuffix extracts top-level domain suffix from domain and URLs
  4. parse extracts structured hostname, domain, publicSuffix, and subdomain metadata
  5. parse and getHostname identify IP addresses and set isIp flag with null domain
  6. parse supports validHosts option to treat custom hostnames like localhost as valid domains
  7. getDomain, getHostname, and getPublicSuffix return null for empty or non-string inputs

文件

  • 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/tldts@7.4.12
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/tldts@7.4.12
Demonstrate these symbols/APIs:
  - getDomain
  - getHostname
  - getPublicSuffix
  - parse

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:b98553f923b21ed084aac4303875e68dcf82c76911560d1c9aaa020c5e3baa3e","contract":["getHostname extracts hostname from full URLs, URLs with ports, and bare domain strings","getDomain extracts registrable domain from hostname and URLs","getPublicSuffix extracts top-level domain suffix from domain and URLs","parse extracts structured hostname, domain, publicSuffix, and subdomain metadata","parse and getHostname identify IP addresses and set isIp flag with null domain","parse supports validHosts option to treat custom hostnames like localhost as valid domains","getDomain, getHostname, and getPublicSuffix return null for empty or non-string inputs"],"goal":"verify pkg:npm/tldts@7.4.12","kind":"HOW","packages":["pkg:npm/tldts@7.4.12"],"schemaVersion":1,"symbols":["getDomain","getHostname","getPublicSuffix","parse"]},"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/tldts@7.4.12"],"schemaVersion":1,"subject":"pkg:npm/tldts@7.4.12","symbols":["getDomain","getHostname","getPublicSuffix","parse"],"verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "sample-tldts",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-tldts",
      "version": "1.0.0",
      "license": "MIT-0",
      "dependencies": {
        "tldts": "7.4.12"
      }
    },
    "node_modules/tldts": {
      "version": "7.4.12",
      "resolved": "https://registry.npmjs.org/tldts/-/tldts-7.4.12.tgz",
      "integrity": "sha512-WylhSDKVeYnWXL3a+vKTaOxjnOeEGw938hImY8zoRWJjRRK/Jp1K+IihBzIONpUmW4e3WmXT6q5FW6vlESVZCA==",
      "license": "MIT",
      "dependencies": {
        "tldts-core": "^7.4.12"
      },
      "bin": {
        "tldts": "bin/cli.js"
      }
    },
    "node_modules/tldts-core": {
      "version": "7.4.12",
      "resolved": "https://registry.npmjs.org/tldts-core/-/tldts-core-7.4.12.tgz",
      "integrity": "sha512-nYNzS2WRf4QJmjzFFgAxLOBjyBxAGRbCy9PVBPaglcYyYajh40VBn+v5Ngr96ZMc7oM0+aCJdtQnNejvdBnXMQ==",
      "license": "MIT"
    }
  }
}
package.json
{
  "name": "sample-tldts",
  "version": "1.0.0",
  "description": "Clean-room sample verifying pkg:npm/tldts@7.4.12",
  "type": "module",
  "main": "src/index.js",
  "license": "MIT-0",
  "dependencies": {
    "tldts": "7.4.12"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify pkg:npm/tldts@7.4.12",
  "kind": "HOW",
  "packages": [
    "pkg:npm/tldts@7.4.12"
  ],
  "symbols": [
    "getDomain",
    "getHostname",
    "getPublicSuffix",
    "parse"
  ]
}
src/index.js
import { getDomain, getHostname, getPublicSuffix, parse } from 'tldts';

/**
 * Extracts the hostname from a given URL or domain string.
 * @param {string} url
 * @param {object} [options]
 * @returns {string|null}
 */
export function extractHostname(url, options) {
  return getHostname(url, options);
}

/**
 * Extracts the registered domain from a given URL or domain string.
 * @param {string} url
 * @param {object} [options]
 * @returns {string|null}
 */
export function extractDomain(url, options) {
  return getDomain(url, options);
}

/**
 * Extracts the public suffix from a given URL or domain string.
 * @param {string} url
 * @param {object} [options]
 * @returns {string|null}
 */
export function extractPublicSuffix(url, options) {
  return getPublicSuffix(url, options);
}

/**
 * Parses a given URL or domain string into its structured components.
 * @param {string} url
 * @param {object} [options]
 * @returns {object}
 */
export function parseUrl(url, options) {
  return parse(url, options);
}

export { getDomain, getHostname, getPublicSuffix, parse };
test/contract.mjs
import assert from 'node:assert/strict';
import { getDomain, getHostname, getPublicSuffix, parse } from 'tldts';
import {
  extractDomain,
  extractHostname,
  extractPublicSuffix,
  parseUrl,
} from '../src/index.js';

// 1. getHostname extracts hostname from full URLs, URLs with ports, and bare domain strings
{
  assert.equal(getHostname('https://sub.example.com/path?arg=1'), 'sub.example.com');
  assert.equal(getHostname('https://sub.example.com:8080/path'), 'sub.example.com');
  assert.equal(getHostname('sub.example.com'), 'sub.example.com');
  assert.equal(extractHostname('https://example.com:3000'), 'example.com');
}

// 2. getDomain extracts registrable domain from hostname and URLs
{
  assert.equal(getDomain('https://sub.example.com'), 'example.com');
  assert.equal(getDomain('https://a.b.c.example.com:443/test'), 'example.com');
  assert.equal(getDomain('sub.example.com'), 'example.com');
  assert.equal(extractDomain('https://example.com/path'), 'example.com');
}

// 3. getPublicSuffix extracts top-level domain suffix from domain and URLs
{
  assert.equal(getPublicSuffix('example.com'), 'com');
  assert.equal(getPublicSuffix('sub.example.com'), 'com');
  assert.equal(getPublicSuffix('https://sub.example.com/path'), 'com');
  assert.equal(extractPublicSuffix('https://example.com'), 'com');
}

// 4. parse extracts structured hostname, domain, publicSuffix, and subdomain metadata
{
  const res = parse('https://sub.example.com:8080/path?query=1');
  assert.equal(res.hostname, 'sub.example.com');
  assert.equal(res.domain, 'example.com');
  assert.equal(res.publicSuffix, 'com');
  assert.equal(res.domainWithoutSuffix, 'example');
  assert.equal(res.subdomain, 'sub');
  assert.equal(res.isIcann, true);
  assert.equal(res.isIp, false);

  const parsed = parseUrl('https://alpha.beta.example.com');
  assert.equal(parsed.domain, 'example.com');
  assert.equal(parsed.subdomain, 'alpha.beta');
}

// 5. parse and getHostname identify IP addresses and set isIp flag with null domain
{
  const ipResult = parse('http://127.0.0.1:3000/api');
  assert.equal(ipResult.hostname, '127.0.0.1');
  assert.equal(ipResult.domain, null);
  assert.equal(ipResult.publicSuffix, null);
  assert.equal(ipResult.isIp, true);
  assert.equal(getHostname('http://127.0.0.1:8080'), '127.0.0.1');
  assert.equal(getDomain('127.0.0.1'), null);
}

// 6. parse supports validHosts option to treat custom hostnames like localhost as valid domains
{
  const defaultLocal = parse('http://localhost:3000');
  assert.equal(defaultLocal.hostname, 'localhost');
  assert.equal(defaultLocal.domain, null);

  const customLocal = parse('http://localhost:3000', { validHosts: ['localhost'] });
  assert.equal(customLocal.hostname, 'localhost');
  assert.equal(customLocal.domain, 'localhost');
  assert.equal(customLocal.publicSuffix, 'localhost');
}

// 7. getDomain, getHostname, and getPublicSuffix return null for empty or non-string inputs
{
  assert.equal(getHostname(''), null);
  assert.equal(getDomain(''), null);
  assert.equal(getPublicSuffix(''), null);
  const emptyParse = parse('');
  assert.equal(emptyParse.hostname, null);
  assert.equal(emptyParse.domain, null);
  assert.equal(emptyParse.publicSuffix, null);
}

console.log('All tldts contract assertions passed.');

原始种子者

匿名