CodeSampleX

示例

socks 2.8.9: SocksClientError

已验证示例 — npm socks 2.8.9: SocksClientError. contract 在 node 22 · linux alpine/x64 · docker 上运行并通过: assert SocksClientError is an instance of Error

sha256:c9bb71b18a2a435767fa1e9feac89a549666e034faf875908410b0e873176836

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

执行证据

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

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

验证运行环境

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

案例

HOW
目标
verify socks.SocksClientError in pkg:npm/socks@2.8.9
包
符号
  • socks.SocksClientError
创建时间
2026-08-23T13:40:31Z

契约

  1. assert SocksClientError is an instance of Error
  2. assert SocksClientError retains error message and client options
  3. assert SocksClient emits or rejects with SocksClientError on connection failure

文件

  • PROMPT.md
  • csx.json
  • index.mjs
  • 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 socks.SocksClientError in pkg:npm/socks@2.8.9
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/socks@2.8.9
Demonstrate these symbols/APIs:
  - socks.SocksClientError

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:4c9dd4f2f0dcf92d91dd95e1b1541a006d44dadefef1643ebcb2947127599199","contract":["assert SocksClientError is an instance of Error","assert SocksClientError retains error message and client options","assert SocksClient emits or rejects with SocksClientError on connection failure"],"goal":"verify socks.SocksClientError in pkg:npm/socks@2.8.9","kind":"HOW","packages":["pkg:npm/socks@2.8.9"],"schemaVersion":1,"symbols":["socks.SocksClientError"]},"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/socks@2.8.9"],"schemaVersion":1,"subject":"pkg:npm/socks@2.8.9","symbols":["socks.SocksClientError"],"verifierAdapter":"node-typescript@1"}
index.mjs
import { SocksClient, SocksClientError } from 'socks';

/**
 * Creates a SocksClient error with custom options.
 * @param {string} message
 * @param {object} options
 * @returns {SocksClientError}
 */
export function createClientError(message, options) {
  return new SocksClientError(message, options);
}

/**
 * Attempts to connect through a SOCKS proxy, catching any SocksClientError.
 * @param {object} options
 * @returns {Promise<object>}
 */
export async function attemptConnection(options) {
  try {
    const info = await SocksClient.createConnection(options);
    return { success: true, info };
  } catch (err) {
    if (err instanceof SocksClientError) {
      return { success: false, error: err };
    }
    throw err;
  }
}

export { SocksClient, SocksClientError };
package-lock.json
{
  "name": "socks-client-error-sample",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "socks-client-error-sample",
      "version": "1.0.0",
      "license": "MIT-0",
      "dependencies": {
        "socks": "2.8.9"
      }
    },
    "node_modules/ip-address": {
      "version": "10.5.0",
      "resolved": "https://registry.npmjs.org/ip-address/-/ip-address-10.5.0.tgz",
      "integrity": "sha512-R5SnVLJmgYYvf2F2ZgwSBnelz5G4q5AxIC277GDfUaNbrZKNANcBC7RHqYYePlszf4kBolVkJauG0ZjHHFh55g==",
      "license": "MIT",
      "engines": {
        "node": ">= 12"
      }
    },
    "node_modules/smart-buffer": {
      "version": "4.2.0",
      "resolved": "https://registry.npmjs.org/smart-buffer/-/smart-buffer-4.2.0.tgz",
      "integrity": "sha512-94hK0Hh8rPqQl2xXc3HsaBoOXKV20MToPkcXvwbISWLEs+64sBq5kFgn2kJDHb1Pry9yrP0dxrCI9RRci7RXKg==",
      "license": "MIT",
      "engines": {
        "node": ">= 6.0.0",
        "npm": ">= 3.0.0"
      }
    },
    "node_modules/socks": {
      "version": "2.8.9",
      "resolved": "https://registry.npmjs.org/socks/-/socks-2.8.9.tgz",
      "integrity": "sha512-LJhUYUvItdQ0LkJTmPeaEObWXAqFyfmP85x0tch/ez9cahmhlBBLbIqDFnvBnUJGagb0JbIQrkBs1wJ+yRYpEw==",
      "license": "MIT",
      "dependencies": {
        "ip-address": "^10.1.1",
        "smart-buffer": "^4.2.0"
      },
      "engines": {
        "node": ">= 10.0.0",
        "npm": ">= 3.0.0"
      }
    }
  }
}
package.json
{
  "name": "socks-client-error-sample",
  "version": "1.0.0",
  "description": "Demonstrate socks.SocksClientError usage and behavior",
  "main": "index.mjs",
  "type": "module",
  "scripts": {
    "test": "node test/contract.mjs"
  },
  "dependencies": {
    "socks": "2.8.9"
  },
  "license": "MIT-0"
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify socks.SocksClientError in pkg:npm/socks@2.8.9",
  "kind": "HOW",
  "packages": [
    "pkg:npm/socks@2.8.9"
  ],
  "symbols": [
    "socks.SocksClientError"
  ]
}
test/contract.mjs
import assert from 'node:assert/strict';
import { SocksClient, SocksClientError } from 'socks';
import { createClientError, attemptConnection } from '../index.mjs';

// 1. Assert SocksClientError class exists and extends Error
assert.equal(typeof SocksClientError, 'function', 'SocksClientError should be a constructor function');
const sampleOptions = {
  proxy: {
    ipaddress: '127.0.0.1',
    port: 1080,
    type: 5
  },
  command: 'connect',
  destination: {
    host: 'localhost',
    port: 80
  }
};

const customError = createClientError('connection failed to proxy', sampleOptions);
assert(customError instanceof Error, 'customError must be an instance of Error');
assert(customError instanceof SocksClientError, 'customError must be an instance of SocksClientError');
assert.equal(customError.message, 'connection failed to proxy', 'error message must match');
assert.deepEqual(customError.options, sampleOptions, 'error options must match');

// 2. Assert SocksClient.createConnection emits/rejects with SocksClientError on unreachable proxy
const unreachableOptions = {
  proxy: {
    ipaddress: '127.0.0.1',
    port: 59998,
    type: 5
  },
  command: 'connect',
  destination: {
    host: '127.0.0.1',
    port: 8080
  },
  timeout: 1000
};

const result = await attemptConnection(unreachableOptions);
assert.equal(result.success, false, 'connection should fail on closed port');
assert(result.error instanceof SocksClientError, 'error should be an instance of SocksClientError');
assert(result.error instanceof Error, 'error should be an instance of Error');
assert(result.error.message.length > 0, 'error message should not be empty');
assert.equal(result.error.options.proxy.port, 59998, 'error options proxy port should match');

console.log('All contract tests passed.');

原始种子者

匿名