CodeSampleX

示例

react-refresh 0.17.0: performReactRefresh

已验证示例 — npm react-refresh 0.17.0: performReactRefresh. contract 在 node 22 · linux debian/x64 · docker 上运行并通过: performReactRefresh returns null when no…

sha256:584669bd42ce55c26843b6a6aa15b3bf1d9dacd7ce7cd491b85d0d74936aa1c9

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

案例

HOW
目标
verify react-refresh.performReactRefresh in pkg:npm/react-refresh@0.17.0
包
符号
  • react-refresh.performReactRefresh
创建时间
2026-09-04T19:25:03Z

契约

  1. performReactRefresh returns null when no pending component updates exist
  2. register queues pending updates when registering a new component type with an existing ID
  3. performReactRefresh applies pending updates to component families and returns an update object with updatedFamilies
  4. performReactRefresh clears pending updates so subsequent calls return null
  5. performReactRefresh notifies injected DevTools renderer refresh handlers of updated family resolutions

文件

  • 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 react-refresh.performReactRefresh in pkg:npm/react-refresh@0.17.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/react-refresh@0.17.0
Demonstrate these symbols/APIs:
  - react-refresh.performReactRefresh

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:6848f78713956fb384b4862a1be7c99b21daf3de7750b9fd1ff0610a721f5e37","contract":["performReactRefresh returns null when no pending component updates exist","register queues pending updates when registering a new component type with an existing ID","performReactRefresh applies pending updates to component families and returns an update object with updatedFamilies","performReactRefresh clears pending updates so subsequent calls return null","performReactRefresh notifies injected DevTools renderer refresh handlers of updated family resolutions"],"goal":"verify react-refresh.performReactRefresh in pkg:npm/react-refresh@0.17.0","kind":"HOW","packages":["pkg:npm/react-refresh@0.17.0"],"schemaVersion":1,"symbols":["react-refresh.performReactRefresh"]},"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/react-refresh@0.17.0"],"schemaVersion":1,"subject":"pkg:npm/react-refresh@0.17.0","symbols":["react-refresh.performReactRefresh"],"verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "sample-react-refresh-perform-react-refresh",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-react-refresh-perform-react-refresh",
      "version": "1.0.0",
      "dependencies": {
        "react-refresh": "0.17.0"
      }
    },
    "node_modules/react-refresh": {
      "version": "0.17.0",
      "resolved": "https://registry.npmjs.org/react-refresh/-/react-refresh-0.17.0.tgz",
      "integrity": "sha512-z6F7K9bV85EfseRCp2bzrpyQ0Gkw1uLoCel9XBVWPg/TjRj94SkJzUTGfOa4bs7iJvBWtQG0Wq7wnI0syw3EBQ==",
      "license": "MIT",
      "engines": {
        "node": ">=0.10.0"
      }
    }
  }
}
package.json
{
  "name": "sample-react-refresh-perform-react-refresh",
  "version": "1.0.0",
  "private": true,
  "description": "Clean-room verification of react-refresh.performReactRefresh in react-refresh",
  "type": "module",
  "main": "src/index.js",
  "dependencies": {
    "react-refresh": "0.17.0"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify react-refresh.performReactRefresh in pkg:npm/react-refresh@0.17.0",
  "kind": "HOW",
  "packages": [
    "pkg:npm/react-refresh@0.17.0"
  ],
  "symbols": [
    "react-refresh.performReactRefresh"
  ]
}
src/index.js
import RefreshRuntime from 'react-refresh/runtime';

const {
  performReactRefresh,
  register,
  setSignature,
  injectIntoGlobalHook,
  getFamilyByID,
  getFamilyByType,
  isLikelyComponentType,
} = RefreshRuntime;

/**
 * Executes a pending React Refresh pass.
 *
 * @returns {object|null} The refresh update object containing updatedFamilies and staleFamilies, or null if no updates.
 */
export function executeReactRefresh() {
  return performReactRefresh();
}

/**
 * Registers a component type with an identifier for hot reloading.
 *
 * @param {Function|object} type - Component type.
 * @param {string} id - Component identifier.
 */
export function registerComponent(type, id) {
  register(type, id);
}

/**
 * Inspects the registered family for a component identifier.
 *
 * @param {string} id - Component identifier.
 * @returns {object|undefined} Component family object.
 */
export function getComponentFamily(id) {
  return getFamilyByID(id);
}

/**
 * Injects React DevTools hook helpers for refresh testing.
 *
 * @param {object} globalObject - Target global object.
 */
export function setupRefreshHook(globalObject) {
  injectIntoGlobalHook(globalObject);
}

export {
  performReactRefresh,
  register,
  setSignature,
  injectIntoGlobalHook,
  getFamilyByID,
  getFamilyByType,
  isLikelyComponentType,
};
test/contract.mjs
import assert from 'node:assert/strict';
import {
  executeReactRefresh,
  registerComponent,
  getComponentFamily,
  setupRefreshHook,
  performReactRefresh,
  register,
  getFamilyByID,
  getFamilyByType,
} from '../src/index.js';

// 1. Symbol and function export verification
assert.equal(typeof performReactRefresh, 'function', 'performReactRefresh must be exported as a function');
assert.equal(typeof register, 'function', 'register must be exported as a function');
assert.equal(typeof executeReactRefresh, 'function', 'executeReactRefresh wrapper must be a function');
assert.equal(typeof registerComponent, 'function', 'registerComponent wrapper must be a function');

// 2. Initial state returns null when no updates are queued
const initialResult = performReactRefresh();
assert.equal(initialResult, null, 'performReactRefresh returns null when no pending component updates exist');

const wrapperNoopResult = executeReactRefresh();
assert.equal(wrapperNoopResult, null, 'executeReactRefresh returns null when no pending component updates exist');

// 3. Register first version of a component family
function HeaderV1() {
  return 'Header version 1';
}
registerComponent(HeaderV1, 'HeaderComponent');

const headerFamily = getComponentFamily('HeaderComponent');
assert.ok(headerFamily, 'Component family should be created for HeaderComponent');
assert.equal(headerFamily.current, HeaderV1, 'Family current pointer should initially reference HeaderV1');
assert.equal(getFamilyByType(HeaderV1), headerFamily, 'getFamilyByType should map HeaderV1 to the created family');

// Registering initial version does not queue a pending update
assert.equal(performReactRefresh(), null, 'performReactRefresh returns null when only initial registration occurred');

// 4. Register updated version with the same ID - queues pending update
function HeaderV2() {
  return 'Header version 2';
}
registerComponent(HeaderV2, 'HeaderComponent');

// 5. performReactRefresh applies update and returns update object
const updateResult = performReactRefresh();
assert.ok(updateResult, 'performReactRefresh must return an update object when updates are pending');
assert.ok(updateResult.updatedFamilies instanceof Set, 'updatedFamilies must be an instance of Set');
assert.ok(updateResult.staleFamilies instanceof Set, 'staleFamilies must be an instance of Set');
assert.equal(updateResult.updatedFamilies.has(headerFamily), true, 'updatedFamilies must contain the modified HeaderComponent family');
assert.equal(headerFamily.current, HeaderV2, 'Component family current pointer must update to HeaderV2');
assert.equal(getFamilyByType(HeaderV2), headerFamily, 'getFamilyByType should resolve HeaderV2 to the updated family');

// 6. performReactRefresh clears pending updates
assert.equal(performReactRefresh(), null, 'performReactRefresh clears pending updates so subsequent calls return null');

// 7. DevTools global hook integration and renderer refresh handler notification
const mockRenderer = {
  scheduleRefresh: (root, update) => {},
  setRefreshHandler: (handler) => {
    mockRenderer.refreshHandler = handler;
  },
};

const fakeGlobal = {};
setupRefreshHook(fakeGlobal);
assert.ok(fakeGlobal.__REACT_DEVTOOLS_GLOBAL_HOOK__, 'DevTools global hook should be created');
assert.equal(typeof fakeGlobal.__REACT_DEVTOOLS_GLOBAL_HOOK__.inject, 'function', 'Global hook inject must be a function');

fakeGlobal.__REACT_DEVTOOLS_GLOBAL_HOOK__.inject(mockRenderer);

function HeaderV3() {
  return 'Header version 3';
}
register(HeaderV3, 'HeaderComponent');

const rendererUpdate = executeReactRefresh();
assert.ok(rendererUpdate, 'executeReactRefresh should return update object');
assert.equal(typeof mockRenderer.refreshHandler, 'function', 'setRefreshHandler must be called on registered renderer');
assert.equal(mockRenderer.refreshHandler(HeaderV1), headerFamily, 'refreshHandler must resolve original HeaderV1 to family');
assert.equal(mockRenderer.refreshHandler(HeaderV2), headerFamily, 'refreshHandler must resolve intermediate HeaderV2 to family');
assert.equal(mockRenderer.refreshHandler(HeaderV3), headerFamily, 'refreshHandler must resolve current HeaderV3 to family');
assert.equal(headerFamily.current, HeaderV3, 'Family current pointer must point to HeaderV3');

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

原始种子者

匿名