CodeSampleX

Exemple

globalthis 1.0.4: get global this object across environments with globalthis

Échantillon vérifié pour npm globalthis 1.0.4: get global this object across environments with globalthis. Le contrat s'est exécuté sur node 22 · linux…

sha256:21d9723ea1881e3d70ad5437f7ca2d39ce150dc8daff8dff6d91875c075dd101

Ce réseau offre une seule chose : un échantillon qui compile. Il l'a exécuté dans un bac à sable et conservé le reçu signé. Il ne note rien et ne garantit rien : si le même code compile chez vous, il ne l'a pas mesuré. Combien de clés de signature distinctes ont déposé un reçu de contrat réussi. Une seule, c'est l'auteur ; plus d'une signifie que quelqu'un d'autre l'a compilé aussi. Une clé est auto-générée sans identité enregistrée derrière, donc on compte des clés, pas des personnes. MIT-0

Preuves d'exécution

L'environnement déclaré et les exécutions signées sont séparés, pour que vous voyiez exactement ce que cet échantillon a exécuté et où.

Base de preuve
Contrat signé réussi
Reçus de vérification
1
Clés de signature qui l’ont compilé
1
Environnement déclaré linux 24 · ubuntu · glibc 2.39 x64 npm

Environnements des exécutions de vérification

Environnement Contrat Étapes Exécution
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-27

Cas

HOW
Objectif
get global this object across environments with globalthis
Paquets
Symboles
  • globalthis
Créé
2026-08-27T22:00:09Z

Contrat

  1. getGlobalObject returns global object containing standard ECMAScript built-in globals
  2. getGlobalObject returns same reference as runtime global this
  3. getPolyfillImplementation returns polyfill resolution for global this
  4. shimGlobalThis installs globalThis property on global object and returns global reference
  5. hasGlobalProperty detects standard global built-ins and returns false for nonexistent keys
  6. setGlobalProperty writes and getGlobalProperty reads custom values on global scope
  7. deleteGlobalProperty removes custom properties from global scope
  8. getGlobal implementation exports fallback global resolution

Fichiers

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

Télécharger l’artefact source (tar.gz)

Code source

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: get global this object across environments with globalthis
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/globalthis@1.0.4
Demonstrate these symbols/APIs:
  - globalthis

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:a7db383441a37472e608b0da812d99715dcecad4c5d5603b8a7f00194b8a4e05","contract":["getGlobalObject returns global object containing standard ECMAScript built-in globals","getGlobalObject returns same reference as runtime global this","getPolyfillImplementation returns polyfill resolution for global this","shimGlobalThis installs globalThis property on global object and returns global reference","hasGlobalProperty detects standard global built-ins and returns false for nonexistent keys","setGlobalProperty writes and getGlobalProperty reads custom values on global scope","deleteGlobalProperty removes custom properties from global scope","getGlobal implementation exports fallback global resolution"],"goal":"get global this object across environments with globalthis","kind":"HOW","packages":["pkg:npm/globalthis@1.0.4"],"schemaVersion":1,"symbols":["globalthis"]},"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/globalthis@1.0.4"],"schemaVersion":1,"subject":"pkg:npm/globalthis@1.0.4","symbols":["globalthis"],"verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "sample-globalthis",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-globalthis",
      "version": "1.0.0",
      "dependencies": {
        "globalthis": "1.0.4"
      }
    },
    "node_modules/define-data-property": {
      "version": "1.1.4",
      "resolved": "https://registry.npmjs.org/define-data-property/-/define-data-property-1.1.4.tgz",
      "integrity": "sha512-rBMvIzlpA8v6E+SJZoo++HAYqsLrkg7MSfIinMPFhmkorw7X+dOXVJQs+QT69zGkzMyfDnIMN2Wid1+NbL3T+A==",
      "license": "MIT",
      "dependencies": {
        "es-define-property": "^1.0.0",
        "es-errors": "^1.3.0",
        "gopd": "^1.0.1"
      },
      "engines": {
        "node": ">= 0.4"
      },
      "funding": {
        "url": "https://github.com/sponsors/ljharb"
      }
    },
    "node_modules/define-properties": {
      "version": "1.2.1",
      "resolved": "https://registry.npmjs.org/define-properties/-/define-properties-1.2.1.tgz",
      "integrity": "sha512-8QmQKqEASLd5nx0U1B1okLElbUuuttJ/AnYmRXbbbGDWh6uS208EjD4Xqq/I9wK7u0v6O08XhTWnt5XtEbR6Dg==",
      "license": "MIT",
      "dependencies": {
        "define-data-property": "^1.0.1",
        "has-property-descriptors": "^1.0.0",
        "object-keys": "^1.1.1"
      },
      "engines": {
        "node": ">= 0.4"
      },
      "funding": {
        "url": "https://github.com/sponsors/ljharb"
      }
    },
    "node_modules/es-define-property": {
      "version": "1.0.1",
      "resolved": "https://registry.npmjs.org/es-define-property/-/es-define-property-1.0.1.tgz",
      "integrity": "sha512-e3nRfgfUZ4rNGL232gUgX06QNyyez04KdjFrF+LTRoOXmrOgFKDg4BCdsjW8EnT69eqdYGmRpJwiPVYNrCaW3g==",
      "license": "MIT",
      "engines": {
        "node": ">= 0.4"
      }
    },
    "node_modules/es-errors": {
      "version": "1.3.0",
      "resolved": "https://registry.npmjs.org/es-errors/-/es-errors-1.3.0.tgz",
      "integrity": "sha512-Zf5H2Kxt2xjTvbJvP2ZWLEICxA6j+hAmMzIlypy4xcBg1vKVnx89Wy0GbS+kf5cwCVFFzdCFh2XSCFNULS6csw==",
      "license": "MIT",
      "engines": {
        "node": ">= 0.4"
      }
    },
    "node_modules/globalthis": {
      "version": "1.0.4",
      "resolved": "https://registry.npmjs.org/globalthis/-/globalthis-1.0.4.tgz",
      "integrity": "sha512-DpLKbNU4WylpxJykQujfCcwYWiV/Jhm50Goo0wrVILAv5jOr9d+H+UR3PhSCD2rCCEIg0uc+G+muBTwD54JhDQ==",
      "license": "MIT",
      "dependencies": {
        "define-properties": "^1.2.1",
        "gopd": "^1.0.1"
      },
      "engines": {
        "node": ">= 0.4"
      },
      "funding": {
        "url": "https://github.com/sponsors/ljharb"
      }
    },
    "node_modules/gopd": {
      "version": "1.2.0",
      "resolved": "https://registry.npmjs.org/gopd/-/gopd-1.2.0.tgz",
      "integrity": "sha512-ZUKRh6/kUFoAiTAtTYPZJ3hw9wNxx+BIBOijnlG9PnrJsCcSjs1wyyD6vJpaYtgnzDrKYRSqf3OO6Rfa93xsRg==",
      "license": "MIT",
      "engines": {
        "node": ">= 0.4"
      },
      "funding": {
        "url": "https://github.com/sponsors/ljharb"
      }
    },
    "node_modules/has-property-descriptors": {
      "version": "1.0.2",
      "resolved": "https://registry.npmjs.org/has-property-descriptors/-/has-property-descriptors-1.0.2.tgz",
      "integrity": "sha512-55JNKuIW+vq4Ke1BjOTjM2YctQIvCT7GFzHwmfZPGo5wnrgkid0YQtnAleFSqumZm4az3n2BS+erby5ipJdgrg==",
      "license": "MIT",
      "dependencies": {
        "es-define-property": "^1.0.0"
      },
      "funding": {
        "url": "https://github.com/sponsors/ljharb"
      }
    },
    "node_modules/object-keys": {
      "version": "1.1.1",
      "resolved": "https://registry.npmjs.org/object-keys/-/object-keys-1.1.1.tgz",
      "integrity": "sha512-NuAESUOUMrlIXOfHKzD6bpPu3tYt3xvjNdRIQ+FeT0lNb4K8WR70CaDxhuNguS2XG+GjkyMwOzsN5ZktImfhLA==",
      "license": "MIT",
      "engines": {
        "node": ">= 0.4"
      }
    }
  }
}
package.json
{
  "name": "sample-globalthis",
  "version": "1.0.0",
  "type": "module",
  "private": true,
  "dependencies": {
    "globalthis": "1.0.4"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "get global this object across environments with globalthis",
  "kind": "HOW",
  "packages": [
    "pkg:npm/globalthis@1.0.4"
  ],
  "symbols": [
    "globalthis"
  ]
}
src/index.mjs
import getGlobal from "globalthis";

/**
 * Returns the environment's global this object using globalthis.
 * @returns {object} The global this object.
 */
export function getGlobalObject() {
  return getGlobal();
}

/**
 * Returns the polyfill implementation for globalThis.
 * @returns {object} The resolved global object or fallback implementation.
 */
export function getPolyfillImplementation() {
  return getGlobal.getPolyfill();
}

/**
 * Shims globalThis onto the global object if not already present.
 * @returns {object} The global object with globalThis guaranteed to exist.
 */
export function shimGlobalThis() {
  return getGlobal.shim();
}

/**
 * Checks if a property exists on the global object.
 * @param {string|symbol} key - The property key to check.
 * @returns {boolean} True if the property exists on global this.
 */
export function hasGlobalProperty(key) {
  const globalObj = getGlobal();
  return key in globalObj;
}

/**
 * Retrieves a property value from the global object.
 * @param {string|symbol} key - The property key to retrieve.
 * @returns {*} The property value.
 */
export function getGlobalProperty(key) {
  const globalObj = getGlobal();
  return globalObj[key];
}

/**
 * Sets a property on the global object and returns the assigned value.
 * @param {string|symbol} key - The property key to set.
 * @param {*} value - The value to assign.
 * @returns {*} The assigned value.
 */
export function setGlobalProperty(key, value) {
  const globalObj = getGlobal();
  globalObj[key] = value;
  return value;
}

/**
 * Removes a property from the global object.
 * @param {string|symbol} key - The property key to remove.
 * @returns {boolean} True if deleted successfully.
 */
export function deleteGlobalProperty(key) {
  const globalObj = getGlobal();
  return delete globalObj[key];
}

export default getGlobal;
export { getGlobal };
test/contract.mjs
import assert from "node:assert/strict";
import {
  getGlobal,
  getGlobalObject,
  getPolyfillImplementation,
  shimGlobalThis,
  hasGlobalProperty,
  getGlobalProperty,
  setGlobalProperty,
  deleteGlobalProperty,
} from "../src/index.mjs";

async function runContract() {
  // 1. getGlobalObject returns global object containing standard ECMAScript built-in globals
  const globalObj = getGlobalObject();
  assert.equal(typeof globalObj, "object");
  assert.notEqual(globalObj, null);
  assert.equal(globalObj.Array, Array);
  assert.equal(globalObj.Object, Object);
  assert.equal(globalObj.Math, Math);
  assert.equal(globalObj.JSON, JSON);
  assert.equal(globalObj.Date, Date);
  assert.equal(globalObj.Promise, Promise);

  // 2. getGlobalObject returns same reference as runtime global this
  assert.equal(globalObj, globalThis);

  // 3. getPolyfillImplementation returns polyfill resolution for global this
  const polyfill = getPolyfillImplementation();
  assert.equal(typeof polyfill, "object");
  assert.equal(polyfill.Array, Array);
  assert.equal(polyfill.Math, Math);

  // 4. shimGlobalThis installs globalThis property on global object and returns global reference
  const shimmed = shimGlobalThis();
  assert.equal(shimmed, globalObj);
  assert.equal(shimmed.globalThis, shimmed);

  // 5. hasGlobalProperty detects standard global built-ins and returns false for nonexistent keys
  assert.equal(hasGlobalProperty("Array"), true);
  assert.equal(hasGlobalProperty("Math"), true);
  assert.equal(hasGlobalProperty("nonExistentGlobalSymbol_12345"), false);

  // 6. setGlobalProperty writes and getGlobalProperty reads custom values on global scope
  const testKey = "__csx_sample_globalthis_prop__";
  const testValue = { sample: "verified", active: true };
  setGlobalProperty(testKey, testValue);
  assert.equal(hasGlobalProperty(testKey), true);
  assert.deepEqual(getGlobalProperty(testKey), testValue);

  // 7. deleteGlobalProperty removes custom properties from global scope
  const deleted = deleteGlobalProperty(testKey);
  assert.equal(deleted, true);
  assert.equal(hasGlobalProperty(testKey), false);
  assert.equal(getGlobalProperty(testKey), undefined);

  // 8. getGlobal implementation exports fallback global resolution
  assert.equal(typeof getGlobal.implementation, "object");
  assert.notEqual(getGlobal.implementation, null);

  console.log("All globalthis contract assertions passed successfully.");
}

runContract().catch((err) => {
  console.error("Contract test failed:", err);
  process.exit(1);
});

Seeder d'origine

anonyme