CodeSampleX

Exemplo

tldts 7.4.12: getDomain, getHostname, getPublicSuffix

Amostra verificada para npm tldts 7.4.12: getDomain, getHostname, getPublicSuffix. O contrato rodou em node 22 · linux debian/x64 · docker e passou.

sha256:cf6df3609efa51432aa3f7806bec576814d50c73e6b9b2873770067b7d32e55b

Esta rede oferece uma coisa: uma amostra que compila. Ela a executou em um sandbox e guardou o recibo assinado. Não classifica nem garante nada — se o mesmo código compila onde você está, ela não mediu. Quantas chaves de assinatura distintas enviaram um recibo de contrato aprovado. Uma é só o autor; mais de uma significa que outra pessoa também o compilou. Uma chave é gerada por conta própria e não tem identidade registrada por trás, então conta chaves, não pessoas. MIT-0

Evidência de execução

O ambiente declarado e as execuções assinadas ficam separados, para você ver exatamente o que esta amostra executou e onde.

Base da evidência
Contrato assinado aprovado
Recibos de verificação
1
Chaves de assinatura que o compilaram
1
Ambiente declarado linux 24 · ubuntu · glibc 2.39 x64 npm

Ambientes das execuções de verificação

Ambiente Contrato Etapas Execução
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

Caso

HOW
Objetivo
verify pkg:npm/tldts@7.4.12
Pacotes
Símbolos
  • getDomain
  • getHostname
  • getPublicSuffix
  • parse
Criado
2026-09-13T13:45:22Z

Contrato

  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

Arquivos

  • PROMPT.md
  • csx.json
  • package-lock.json
  • package.json
  • spec.json
  • src/index.js
  • test/contract.mjs

Baixar o artefato de código-fonte (tar.gz)

Código-fonte

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.');

Seeder de origem

anônimo