CodeSampleX

Exemple

body-parser 2.4.0

Échantillon vérifié pour npm body-parser 2.4.0. Le contrat s'est exécuté sur node 22 · linux debian/x64 · docker et a réussi.

sha256:7c0ef0c3d340f1eabba9704c33b26f8a8dfb8f2800b075bcd1118522dc7eaabe

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é node 22.23 linux 24 · ubuntu · glibc 2.39 x64 node 22.23 javascript npm 10

Environnements des exécutions de vérification

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

Cas

HOW
Objectif
verify pkg:npm/body-parser@2.4.0
Paquets
Environnement
node 22.23.2
Créé
2026-11-28T02:46:11Z

Contrat

  1. subpath exports body-parser/json, body-parser/raw, body-parser/text, and body-parser/urlencoded match top-level bodyParser exports, while calling bodyParser() directly throws
  2. bodyParser.json parses application/json with reviver, leaves req.body undefined when Content-Type does not match or no body is present, rejects primitives in default strict mode with 400 entity.parse.failed, and accepts primitives with strict: false
  3. bodyParser.json enforces byte limit with 413 entity.too.large, passes raw Buffer and encoding to verify, and aborts with 403 entity.verify.failed when verify throws
  4. bodyParser.json automatically inflates gzip and zstd Content-Encoding payloads and rejects compressed payloads with 415 encoding.unsupported when inflate is false
  5. bodyParser.urlencoded defaults to extended: false for flat pairs, parses nested structures with extended: true, enforces depth with status 400, and enforces parameterLimit with 413 parameters.too.many
  6. bodyParser.text parses text/plain into a string on req.body and bodyParser.raw parses application/octet-stream into a Buffer on req.body

Fichiers

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

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: verify pkg:npm/body-parser@2.4.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/body-parser@2.4.0
Constraints:
  - executionContext: node
Required runtime conditions:
  - ecosystem: npm
  - language: javascript
  - moduleSystem: cjs
  - packageManager: npm@10.9.8
  - runtime: node@22.23.2

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:6714a969bc19525cc30f2b21590bf64f2bbb5961cc8972b5d0897fbae9bf3fb5","constraints":{"executionContext":"node"},"contract":["subpath exports body-parser/json, body-parser/raw, body-parser/text, and body-parser/urlencoded match top-level bodyParser exports, while calling bodyParser() directly throws","bodyParser.json parses application/json with reviver, leaves req.body undefined when Content-Type does not match or no body is present, rejects primitives in default strict mode with 400 entity.parse.failed, and accepts primitives with strict: false","bodyParser.json enforces byte limit with 413 entity.too.large, passes raw Buffer and encoding to verify, and aborts with 403 entity.verify.failed when verify throws","bodyParser.json automatically inflates gzip and zstd Content-Encoding payloads and rejects compressed payloads with 415 encoding.unsupported when inflate is false","bodyParser.urlencoded defaults to extended: false for flat pairs, parses nested structures with extended: true, enforces depth with status 400, and enforces parameterLimit with 413 parameters.too.many","bodyParser.text parses text/plain into a string on req.body and bodyParser.raw parses application/octet-stream into a Buffer on req.body"],"goal":"verify pkg:npm/body-parser@2.4.0","kind":"HOW","packages":["pkg:npm/body-parser@2.4.0"],"schemaVersion":1},"contractCommand":["node","test/contract.js"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"npm","executionContext":"node","language":"javascript","libc":"glibc","libcVersion":"2.39","moduleSystem":"cjs","os":"linux","osVersionBucket":"24","packageManager":"npm","packageManagerVersion":"10.9.8","runtime":"node","runtimeVersion":"22.23.2","schemaVersion":1},"license":"MIT-0","packages":["pkg:npm/body-parser@2.4.0"],"schemaVersion":1,"subject":"pkg:npm/body-parser@2.4.0","verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "body-parser-sample",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "body-parser-sample",
      "version": "1.0.0",
      "dependencies": {
        "body-parser": "2.4.0"
      }
    },
    "node_modules/body-parser": {
      "version": "2.4.0",
      "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-2.4.0.tgz",
      "integrity": "sha512-Ifk1KcLrxaGt1occQagQNFNNOqRt1LFuehEnHHzB8Z6cRgIxRU1TBaiIsUiCdA+gy5PL7Y1IX31DNKkRmxtXjQ==",
      "license": "MIT",
      "dependencies": {
        "bytes": "^3.1.2",
        "content-type": "^2.1.0",
        "debug": "^4.4.3",
        "http-errors": "^2.0.1",
        "iconv-lite": "^0.7.3",
        "on-finished": "^2.4.1",
        "qs": "^6.16.1",
        "raw-body": "^3.0.2",
        "type-is": "^2.1.0"
      },
      "engines": {
        "node": ">=18"
      },
      "funding": {
        "type": "opencollective",
        "url": "https://opencollective.com/express"
      }
    },
    "node_modules/bytes": {
      "version": "3.1.2",
      "resolved": "https://registry.npmjs.org/bytes/-/bytes-3.1.2.tgz",
      "integrity": "sha512-/Nf7TyzTx6S3yRJObOAV7956r8cr2+Oj8AC5dt8wSP3BQAoeX58NoHyCU8P8zGkNXStjTSi6fzO6F0pBdcYbEg==",
      "license": "MIT",
      "engines": {
        "node": ">= 0.8"
      }
    },
    "node_modules/call-bind-apply-helpers": {
      "version": "1.0.2",
      "resolved": "https://registry.npmjs.org/call-bind-apply-helpers/-/call-bind-apply-helpers-1.0.2.tgz",
      "integrity": "sha512-Sp1ablJ0ivDkSzjcaJdxEunN5/XvksFJ2sMBFfq6x0ryhQV/2b/KwFe21cMpmHtPOSij8K99/wSfoEuTObmuMQ==",
      "license": "MIT",
      "dependencies": {
        "es-errors": "^1.3.0",
        "function-bind": "^1.1.2"
      },
      "engines": {
        "node": ">= 0.4"
      }
    },
    "node_modules/call-bound": {
      "version": "1.0.4",
      "resolved": "https://registry.npmjs.org/call-bound/-/call-bound-1.0.4.tgz",
      "integrity": "sha512-+ys997U96po4Kx/ABpBCqhA9EuxJaQWDQg7295H4hBphv3IZg0boBKuwYpt4YXp6MZ5AmZQnU/tyMTlRpaSejg==",
      "license": "MIT",
      "dependencies": {
        "call-bind-apply-helpers": "^1.0.2",
        "get-intrinsic": "^1.3.0"
      },
      "engines": {
        "node": ">= 0.4"
      },
      "funding": {
        "url": "https://github.com/sponsors/ljharb"
      }
    },
    "node_modules/content-type": {
      "version": "2.1.0",
      "resolved": "https://registry.npmjs.org/content-type/-/content-type-2.1.0.tgz",
      "integrity": "sha512-mj7UPXE0jaqaOsukNZRUEfEi2AcL7C/vwmwcHV0O97eO1E1pxBZuyjlZrx5seTaNBg1U6+o35wpa35Qfcc+7ag==",
      "license": "MIT",
      "engines": {
        "node": ">=18"
      },
      "funding": {
        "type": "opencollective",
        "url": "https://opencollective.com/express"
      }
    },
    "node_modules/debug": {
      "version": "4.4.3",
      "resolved": "https://registry.npmjs.org/debug/-/debug-4.4.3.tgz",
      "integrity": "sha512-RGwwWnwQvkVfavKVt22FGLw+xYSdzARwm0ru6DhTVA3umU5hZc28V3kO4stgYryrTlLpuvgI9GiijltAjNbcqA==",
      "license": "MIT",
      "dependencies": {
        "ms": "^2.1.3"
      },
      "engines": {
        "node": ">=6.0"
      },
      "peerDependenciesMeta": {
        "supports-color": {
          "optional": true
        }
      }
    },
    "node_modules/depd": {
      "version": "2.0.0",
      "resolved": "https://registry.npmjs.org/depd/-/depd-2.0.0.tgz",
      "integrity": "sha512-g7nH6P6dyDioJogAAGprGpCtVImJhpPk/roCzdb3fIh61/s/nPsfR6onyMwkCAR/OlC3yBC0lESvUoQEAssIrw==",
      "license": "MIT",
      "engines": {
        "node": ">= 0.8"
      }
    },
    "node_modules/dunder-proto": {
      "version": "1.0.1",
      "resolved": "https://registry.npmjs.org/dunder-proto/-/dunder-proto-1.0.1.tgz",
      "integrity": "sha512-KIN/nDJBQRcXw0MLVhZE9iQHmG68qAVIBg9CqmUYjmQIhgij9U5MFvrqkUL5FbtyyzZuOeOt0zdeRe4UY7ct+A==",
      "license": "MIT",
      "dependencies": {
        "call-bind-apply-helpers": "^1.0.1",
        "es-errors": "^1.3.0",
        "gopd": "^1.2.0"
      },
      "engines": {
        "node": ">= 0.4"
      }
    },
    "node_modules/ee-first": {
      "version": "1.1.1",
      "resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz",
      "integrity": "sha512-WMwm9LhRUo+WUaRN+vRuETqG89IgZphVSNkdFgeb6sS/E4OrDIN7t48CAewSHXc6C8lefD8KKfr5vY61brQlow==",
      "license": "MIT"
    },
    "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/es-object-atoms": {
      "version": "1.1.2",
      "resolved": "https://registry.npmjs.org/es-object-atoms/-/es-object-atoms-1.1.2.tgz",
      "integrity": "sha512-HWcBoN6NileqtSydK2FqHbS/LoDd2pqrnQHLyJzBj4kOp/ky2MWMN694xOfkK8/SnUsW2DH7EfyVlydKCsm1Zw==",
      "license": "MIT",
      "dependencies": {
        "es-errors": "^1.3.0"
      },
      "engines": {
        "node": ">= 0.4"
      }
    },
    "node_modules/function-bind": {
      "version": "1.1.2",
      "resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.2.tgz",
      "integrity": "sha512-7XHNxH7qX9xG5mIwxkhumTox/MIRNcOgDrxWsMt2pAr23WHp6MrRlN7FBSFpCpr+oVO0F744iUgR82nJMfG2SA==",
      "license": "MIT",
      "funding": {
        "url": "https://github.com/sponsors/ljharb"
      }
    },
    "node_modules/get-intrinsic": {
      "version": "1.3.0",
      "resolved": "https://registry.npmjs.org/get-intrinsic/-/get-intrinsic-1.3.0.tgz",
      "integrity": "sha512-9fSjSaos/fRIVIp+xSJlE6lfwhES7LNtKaCBIamHsjr2na1BiABJPo0mOjjz8GJDURarmCPGqaiVg5mfjb98CQ==",
      "license": "MIT",
      "dependencies": {
        "call-bind-apply-helpers": "^1.0.2",
        "es-define-property": "^1.0.1",
        "es-errors": "^1.3.0",
        "es-object-atoms": "^1.1.1",
        "function-bind": "^1.1.2",
        "get-proto": "^1.0.1",
        "gopd": "^1.2.0",
        "has-symbols": "^1.1.0",
        "hasown": "^2.0.2",
        "math-intrinsics": "^1.1.0"
      },
      "engines": {
        "node": ">= 0.4"
      },
      "funding": {
        "url": "https://github.com/sponsors/ljharb"
      }
    },
    "node_modules/get-proto": {
      "version": "1.0.1",
      "resolved": "https://registry.npmjs.org/get-proto/-/get-proto-1.0.1.tgz",
      "integrity": "sha512-sTSfBjoXBp89JvIKIefqw7U2CCebsc74kiY6awiGogKtoSGbgjYE/G/+l9sF3MWFPNc9IcoOC4ODfKHfxFmp0g==",
      "license": "MIT",
      "dependencies": {
        "dunder-proto": "^1.0.1",
        "es-object-atoms": "^1.0.0"
      },
      "engines": {
        "node": ">= 0.4"
      }
    },
    "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-symbols": {
      "version": "1.1.0",
      "resolved": "https://registry.npmjs.org/has-symbols/-/has-symbols-1.1.0.tgz",
      "integrity": "sha512-1cDNdwJ2Jaohmb3sg4OmKaMBwuC48sYni5HUw2DvsC8LjGTLK9h+eb1X6RyuOHe4hT0ULCW68iomhjUoKUqlPQ==",
      "license": "MIT",
      "engines": {
        "node": ">= 0.4"
      },
      "funding": {
        "url": "https://github.com/sponsors/ljharb"
      }
    },
    "node_modules/hasown": {
      "version": "2.0.4",
      "resolved": "https://registry.npmjs.org/hasown/-/hasown-2.0.4.tgz",
      "integrity": "sha512-T2UbfbBEF32wiepXIsMlTW9+dDYC6wMh/t/vYA4tuOMKqWz/n3vr1NFSxQiyP+zk2mXsoMA/i/7qV6LKut1t1A==",
      "license": "MIT",
      "dependencies": {
        "function-bind": "^1.1.2"
      },
      "engines": {
        "node": ">= 0.4"
      }
    },
    "node_modules/http-errors": {
      "version": "2.0.1",
      "resolved": "https://registry.npmjs.org/http-errors/-/http-errors-2.0.1.tgz",
      "integrity": "sha512-4FbRdAX+bSdmo4AUFuS0WNiPz8NgFt+r8ThgNWmlrjQjt1Q7ZR9+zTlce2859x4KSXrwIsaeTqDoKQmtP8pLmQ==",
      "license": "MIT",
      "dependencies": {
        "depd": "~2.0.0",
        "inherits": "~2.0.4",
        "setprototypeof": "~1.2.0",
        "statuses": "~2.0.2",
        "toidentifier": "~1.0.1"
      },
      "engines": {
        "node": ">= 0.8"
      },
      "funding": {
        "type": "opencollective",
        "url": "https://opencollective.com/express"
      }
    },
    "node_modules/iconv-lite": {
      "version": "0.7.3",
      "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.7.3.tgz",
      "integrity": "sha512-IKXpvIzjnC9XTAUbVBcMfGS0EPaIXtW6v+zr+RRp+hqULEpo0owZax6wyRwPOJbWbzjYspQwusTsfVr0ifh4uQ==",
      "license": "MIT",
      "dependencies": {
        "safer-buffer": ">= 2.1.2 < 3.0.0"
      },
      "engines": {
        "node": ">=0.10.0"
      },
      "funding": {
        "type": "opencollective",
        "url": "https://opencollective.com/express"
      }
    },
    "node_modules/inherits": {
      "version": "2.0.4",
      "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz",
      "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==",
      "license": "ISC"
    },
    "node_modules/math-intrinsics": {
      "version": "1.1.0",
      "resolved": "https://registry.npmjs.org/math-intrinsics/-/math-intrinsics-1.1.0.tgz",
      "integrity": "sha512-/IXtbwEk5HTPyEwyKX6hGkYXxM9nbj64B+ilVJnC/R6B0pH5G4V3b0pVbL7DBj4tkhBAppbQUlf6F6Xl9LHu1g==",
      "license": "MIT",
      "engines": {
        "node": ">= 0.4"
      }
    },
    "node_modules/media-typer": {
      "version": "1.1.1",
      "resolved": "https://registry.npmjs.org/media-typer/-/media-typer-1.1.1.tgz",
      "integrity": "sha512-yz3xRaG20c6/BOzvYoDaGtPmGscs7YivItZEEqe6GbwNfHuxu9YNmvnEkMzKldAGY4/80pRcQRZSEnhquk9XuQ==",
      "license": "MIT",
      "engines": {
        "node": ">= 0.8"
      },
      "funding": {
        "type": "opencollective",
        "url": "https://opencollective.com/express"
      }
    },
    "node_modules/mime-db": {
      "version": "1.54.0",
      "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.54.0.tgz",
      "integrity": "sha512-aU5EJuIN2WDemCcAp2vFBfp/m4EAhWJnUNSSw0ixs7/kXbd6Pg64EmwJkNdFhB8aWt1sH2CTXrLxo/iAGV3oPQ==",
      "license": "MIT",
      "engines": {
        "node": ">= 0.6"
      }
    },
    "node_modules/mime-types": {
      "version": "3.0.2",
      "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-3.0.2.tgz",
      "integrity": "sha512-Lbgzdk0h4juoQ9fCKXW4by0UJqj+nOOrI9MJ1sSj4nI8aI2eo1qmvQEie4VD1glsS250n15LsWsYtCugiStS5A==",
      "license": "MIT",
      "dependencies": {
        "mime-db": "^1.54.0"
      },
      "engines": {
        "node": ">=18"
      },
      "funding": {
        "type": "opencollective",
        "url": "https://opencollective.com/express"
      }
    },
    "node_modules/ms": {
      "version": "2.1.3",
      "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz",
      "integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==",
      "license": "MIT"
    },
    "node_modules/object-inspect": {
      "version": "1.14.0",
      "resolved": "https://registry.npmjs.org/object-inspect/-/object-inspect-1.14.0.tgz",
      "integrity": "sha512-IVgHNlp/jwQX271+Xzoq9LZNTWXxZK+cEOrGBOBxXNbn7fapnZ+Ts6TVMgNOuAezwG2wdtnMX97tTPK6VEnuOQ==",
      "license": "MIT",
      "engines": {
        "node": ">= 0.4"
      },
      "funding": {
        "url": "https://github.com/sponsors/ljharb"
      }
    },
    "node_modules/on-finished": {
      "version": "2.4.2",
      "resolved": "https://registry.npmjs.org/on-finished/-/on-finished-2.4.2.tgz",
      "integrity": "sha512-ATWUv387i+nLD49EiDoOft5scf32Vsdz3ktrQJG9bi7jRFmaXTssvyvj1EmeKVVgvesbqUtvQQvaS2TpTukAZQ==",
      "license": "MIT",
      "dependencies": {
        "ee-first": "1.1.1"
      },
      "engines": {
        "node": ">= 0.8"
      },
      "funding": {
        "type": "opencollective",
        "url": "https://opencollective.com/express"
      }
    },
    "node_modules/qs": {
      "version": "6.17.1",
      "resolved": "https://registry.npmjs.org/qs/-/qs-6.17.1.tgz",
      "integrity": "sha512-jd3COkaKVgGUW+ZHyEHCG9bH7RoaaiWuMgzsIBjUMVjLAaWGWpoUAVwljVvBHxRu/2qEHnadLGZrkutYUBPQJA==",
      "license": "BSD-3-Clause",
      "dependencies": {
        "es-define-property": "^1.0.1",
        "side-channel": "^1.1.1"
      },
      "engines": {
        "node": ">=0.6"
      },
      "funding": {
        "url": "https://github.com/sponsors/ljharb"
      }
    },
    "node_modules/raw-body": {
      "version": "3.0.2",
      "resolved": "https://registry.npmjs.org/raw-body/-/raw-body-3.0.2.tgz",
      "integrity": "sha512-K5zQjDllxWkf7Z5xJdV0/B0WTNqx6vxG70zJE4N0kBs4LovmEYWJzQGxC9bS9RAKu3bgM40lrd5zoLJ12MQ5BA==",
      "license": "MIT",
      "dependencies": {
        "bytes": "~3.1.2",
        "http-errors": "~2.0.1",
        "iconv-lite": "~0.7.0",
        "unpipe": "~1.0.0"
      },
      "engines": {
        "node": ">= 0.10"
      }
    },
    "node_modules/safer-buffer": {
      "version": "2.1.2",
      "resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz",
      "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==",
      "license": "MIT"
    },
    "node_modules/setprototypeof": {
      "version": "1.2.0",
      "resolved": "https://registry.npmjs.org/setprototypeof/-/setprototypeof-1.2.0.tgz",
      "integrity": "sha512-E5LDX7Wrp85Kil5bhZv46j8jOeboKq5JMmYM3gVGdGH8xFpPWXUMsNrlODCrkoxMEeNi/XZIwuRvY4XNwYMJpw==",
      "license": "ISC"
    },
    "node_modules/side-channel": {
      "version": "1.1.1",
      "resolved": "https://registry.npmjs.org/side-channel/-/side-channel-1.1.1.tgz",
      "integrity": "sha512-6x6dK6zJdpTzF4sQeNYxwtvBzf6Eg4GtlesS94HOvTudUeyK2WXAaIfmDgsyslYrRBeFIlsi54AYsFGUuhmvrQ==",
      "license": "MIT",
      "dependencies": {
        "es-errors": "^1.3.0",
        "object-inspect": "^1.13.4",
        "side-channel-list": "^1.0.1",
        "side-channel-map": "^1.0.1",
        "side-channel-weakmap": "^1.0.2"
      },
      "engines": {
        "node": ">= 0.4"
      },
      "funding": {
        "url": "https://github.com/sponsors/ljharb"
      }
    },
    "node_modules/side-channel-list": {
      "version": "1.0.1",
      "resolved": "https://registry.npmjs.org/side-channel-list/-/side-channel-list-1.0.1.tgz",
      "integrity": "sha512-mjn/0bi/oUURjc5Xl7IaWi/OJJJumuoJFQJfDDyO46+hBWsfaVM65TBHq2eoZBhzl9EchxOijpkbRC8SVBQU0w==",
      "license": "MIT",
      "dependencies": {
        "es-errors": "^1.3.0",
        "object-inspect": "^1.13.4"
      },
      "engines": {
        "node": ">= 0.4"
      },
      "funding": {
        "url": "https://github.com/sponsors/ljharb"
      }
    },
    "node_modules/side-channel-map": {
      "version": "1.0.1",
      "resolved": "https://registry.npmjs.org/side-channel-map/-/side-channel-map-1.0.1.tgz",
      "integrity": "sha512-VCjCNfgMsby3tTdo02nbjtM/ewra6jPHmpThenkTYh8pG9ucZ/1P8So4u4FGBek/BjpOVsDCMoLA/iuBKIFXRA==",
      "license": "MIT",
      "dependencies": {
        "call-bound": "^1.0.2",
        "es-errors": "^1.3.0",
        "get-intrinsic": "^1.2.5",
        "object-inspect": "^1.13.3"
      },
      "engines": {
        "node": ">= 0.4"
      },
      "funding": {
        "url": "https://github.com/sponsors/ljharb"
      }
    },
    "node_modules/side-channel-weakmap": {
      "version": "1.0.2",
      "resolved": "https://registry.npmjs.org/side-channel-weakmap/-/side-channel-weakmap-1.0.2.tgz",
      "integrity": "sha512-WPS/HvHQTYnHisLo9McqBHOJk2FkHO/tlpvldyrnem4aeQp4hai3gythswg6p01oSoTl58rcpiFAjF2br2Ak2A==",
      "license": "MIT",
      "dependencies": {
        "call-bound": "^1.0.2",
        "es-errors": "^1.3.0",
        "get-intrinsic": "^1.2.5",
        "object-inspect": "^1.13.3",
        "side-channel-map": "^1.0.1"
      },
      "engines": {
        "node": ">= 0.4"
      },
      "funding": {
        "url": "https://github.com/sponsors/ljharb"
      }
    },
    "node_modules/statuses": {
      "version": "2.0.2",
      "resolved": "https://registry.npmjs.org/statuses/-/statuses-2.0.2.tgz",
      "integrity": "sha512-DvEy55V3DB7uknRo+4iOGT5fP1slR8wQohVdknigZPMpMstaKJQWhwiYBACJE3Ul2pTnATihhBYnRhZQHGBiRw==",
      "license": "MIT",
      "engines": {
        "node": ">= 0.8"
      }
    },
    "node_modules/toidentifier": {
      "version": "1.0.1",
      "resolved": "https://registry.npmjs.org/toidentifier/-/toidentifier-1.0.1.tgz",
      "integrity": "sha512-o5sSPKEkg/DIQNmH43V0/uerLrpzVedkUh8tGNvaeXpfpuwjKenlSox/2O/BTlZUtEe+JG7s5YhEz608PlAHRA==",
      "license": "MIT",
      "engines": {
        "node": ">=0.6"
      }
    },
    "node_modules/type-is": {
      "version": "2.1.0",
      "resolved": "https://registry.npmjs.org/type-is/-/type-is-2.1.0.tgz",
      "integrity": "sha512-faYHw0anBbc/kWF3zFTEnxSFOAGUX9GFbOBthvDdLsIlEoWOFOtS0zgCiQYwIskL9iGXZL3kAXD8OoZ4GmMATA==",
      "license": "MIT",
      "dependencies": {
        "content-type": "^2.0.0",
        "media-typer": "^1.1.0",
        "mime-types": "^3.0.0"
      },
      "engines": {
        "node": ">= 18"
      },
      "funding": {
        "type": "opencollective",
        "url": "https://opencollective.com/express"
      }
    },
    "node_modules/unpipe": {
      "version": "1.0.0",
      "resolved": "https://registry.npmjs.org/unpipe/-/unpipe-1.0.0.tgz",
      "integrity": "sha512-pjy2bYhSsufwWlKwPc+l3cN7+wuJlK6uz0YdJEOlQDbl6jo/YlPi4mb8agUkVC8BF7V8NuzeyPNqRksA3hztKQ==",
      "license": "MIT",
      "engines": {
        "node": ">= 0.8"
      }
    }
  }
}
package.json
{
  "name": "body-parser-sample",
  "version": "1.0.0",
  "private": true,
  "description": "Clean-room verification sample for body-parser 2.4.0",
  "main": "src/index.js",
  "scripts": {
    "test": "node test/contract.js"
  },
  "dependencies": {
    "body-parser": "2.4.0"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify pkg:npm/body-parser@2.4.0",
  "kind": "HOW",
  "packages": [
    "pkg:npm/body-parser@2.4.0"
  ],
  "constraints": {
    "executionContext": "node"
  },
  "runtimeConditions": {
    "ecosystem": "npm",
    "language": "javascript",
    "moduleSystem": "cjs",
    "packageManager": "npm@10.9.8",
    "runtime": "node@22.23.2"
  }
}
src/index.js
'use strict';

const { Readable } = require('node:stream');
const bodyParser = require('body-parser');
const jsonParserFactory = require('body-parser/json');
const rawParserFactory = require('body-parser/raw');
const textParserFactory = require('body-parser/text');
const urlencodedParserFactory = require('body-parser/urlencoded');

/**
 * Create an offline mock IncomingMessage stream for testing body-parser middleware.
 *
 * @param {Buffer|string|null} payload
 * @param {Record<string, string>} headers
 * @returns {Readable & { headers: Record<string, string> }}
 */
function createMockRequest(payload, headers = {}) {
  const normalizedHeaders = {};
  for (const [key, value] of Object.entries(headers)) {
    normalizedHeaders[key.toLowerCase()] = value;
  }

  let chunks = [];
  if (payload !== null && payload !== undefined) {
    const buf = Buffer.isBuffer(payload) ? payload : Buffer.from(payload, 'utf8');
    chunks = [buf];
    if (
      normalizedHeaders['content-length'] === undefined &&
      normalizedHeaders['transfer-encoding'] === undefined
    ) {
      normalizedHeaders['content-length'] = String(buf.length);
    }
  }

  const req = Readable.from(chunks);
  req.headers = normalizedHeaders;
  return req;
}

/**
 * Execute a body-parser middleware against a mock request and return { req, err }.
 *
 * @param {Function} middleware
 * @param {Buffer|string|null} payload
 * @param {Record<string, string>} headers
 * @returns {Promise<{ req: Readable & { headers: Record<string, string>, body?: unknown }, err: Error | null }>}
 */
function runMiddleware(middleware, payload, headers = {}) {
  const req = createMockRequest(payload, headers);
  const res = {};

  return new Promise((resolve) => {
    middleware(req, res, (err) => {
      resolve({ req, err: err || null });
    });
  });
}

module.exports = {
  bodyParser,
  jsonParserFactory,
  rawParserFactory,
  textParserFactory,
  urlencodedParserFactory,
  createMockRequest,
  runMiddleware,
};
test/contract.js
'use strict';

const assert = require('node:assert/strict');
const zlib = require('node:zlib');
const {
  bodyParser,
  jsonParserFactory,
  rawParserFactory,
  textParserFactory,
  urlencodedParserFactory,
  runMiddleware,
} = require('../src/index.js');

async function main() {
  // 1. Subpath exports match top-level exports, and calling bodyParser() throws.
  assert.strictEqual(bodyParser.json, jsonParserFactory);
  assert.strictEqual(bodyParser.raw, rawParserFactory);
  assert.strictEqual(bodyParser.text, textParserFactory);
  assert.strictEqual(bodyParser.urlencoded, urlencodedParserFactory);
  assert.throws(
    () => bodyParser(),
    /split into individual middleware/i
  );

  // 2. JSON parsing, reviver, strict mode, and v2 undefined req.body when unmatched.
  {
    const middleware = bodyParser.json({
      reviver: (key, value) => (key === 'count' ? value * 2 : value),
    });
    const { req, err } = await runMiddleware(
      middleware,
      JSON.stringify({ name: 'widget', count: 3 }),
      { 'content-type': 'application/json' }
    );
    assert.strictEqual(err, null);
    assert.deepStrictEqual(req.body, { name: 'widget', count: 6 });

    const unmatched = await runMiddleware(middleware, 'plain', {
      'content-type': 'text/plain',
    });
    assert.strictEqual(unmatched.err, null);
    assert.strictEqual(unmatched.req.body, undefined);

    const noBody = await runMiddleware(middleware, null, {
      'content-type': 'application/json',
    });
    assert.strictEqual(noBody.err, null);
    assert.strictEqual(noBody.req.body, undefined);
  }

  // 3. JSON strict mode rejects primitives by default and allows them with strict: false.
  {
    const strictMiddleware = bodyParser.json();
    const strictRes = await runMiddleware(strictMiddleware, '"scalar"', {
      'content-type': 'application/json',
    });
    assert.ok(strictRes.err);
    assert.strictEqual(strictRes.err.status, 400);
    assert.strictEqual(strictRes.err.type, 'entity.parse.failed');

    const nonStrictMiddleware = bodyParser.json({ strict: false });
    const nonStrictRes = await runMiddleware(nonStrictMiddleware, '"scalar"', {
      'content-type': 'application/json',
    });
    assert.strictEqual(nonStrictRes.err, null);
    assert.strictEqual(nonStrictRes.req.body, 'scalar');
  }

  // 4. JSON limit enforcement and verify callback (pass and abort with 403).
  {
    const limited = bodyParser.json({ limit: 10 });
    const tooLarge = await runMiddleware(limited, JSON.stringify({ key: '0123456789' }), {
      'content-type': 'application/json',
    });
    assert.ok(tooLarge.err);
    assert.strictEqual(tooLarge.err.status, 413);
    assert.strictEqual(tooLarge.err.type, 'entity.too.large');

    let capturedRaw = null;
    const verifiedOk = bodyParser.json({
      verify: (_req, _res, buf, encoding) => {
        capturedRaw = { text: buf.toString('utf8'), encoding };
      },
    });
    const okRes = await runMiddleware(verifiedOk, '{"ok":true}', {
      'content-type': 'application/json',
    });
    assert.strictEqual(okRes.err, null);
    assert.deepStrictEqual(okRes.req.body, { ok: true });
    assert.deepStrictEqual(capturedRaw, { text: '{"ok":true}', encoding: 'utf-8' });

    const verifiedReject = bodyParser.json({
      verify: () => {
        throw new Error('signature mismatch');
      },
    });
    const rejectRes = await runMiddleware(verifiedReject, '{"ok":true}', {
      'content-type': 'application/json',
    });
    assert.ok(rejectRes.err);
    assert.strictEqual(rejectRes.err.status, 403);
    assert.strictEqual(rejectRes.err.type, 'entity.verify.failed');
  }

  // 5. Automatic inflation (gzip and zstd) and rejection when inflate: false.
  {
    const jsonPayload = Buffer.from(JSON.stringify({ compressed: true }), 'utf8');
    const gzipped = zlib.gzipSync(jsonPayload);
    const zstdCompressed = zlib.zstdCompressSync(jsonPayload);

    const inflating = bodyParser.json();
    const gzipRes = await runMiddleware(inflating, gzipped, {
      'content-type': 'application/json',
      'content-encoding': 'gzip',
    });
    assert.strictEqual(gzipRes.err, null);
    assert.deepStrictEqual(gzipRes.req.body, { compressed: true });

    const zstdRes = await runMiddleware(inflating, zstdCompressed, {
      'content-type': 'application/json',
      'content-encoding': 'zstd',
    });
    assert.strictEqual(zstdRes.err, null);
    assert.deepStrictEqual(zstdRes.req.body, { compressed: true });

    const noInflate = bodyParser.json({ inflate: false });
    const rejectedRes = await runMiddleware(noInflate, gzipped, {
      'content-type': 'application/json',
      'content-encoding': 'gzip',
    });
    assert.ok(rejectedRes.err);
    assert.strictEqual(rejectedRes.err.status, 415);
    assert.strictEqual(rejectedRes.err.type, 'encoding.unsupported');
  }

  // 6. URL-encoded parser: default extended: false, extended: true, depth limit, parameterLimit.
  {
    const flatParser = bodyParser.urlencoded();
    const flatRes = await runMiddleware(flatParser, 'a=1&b=two', {
      'content-type': 'application/x-www-form-urlencoded',
    });
    assert.strictEqual(flatRes.err, null);
    assert.deepStrictEqual({ ...flatRes.req.body }, { a: '1', b: 'two' });

    const nestedParser = bodyParser.urlencoded({ extended: true, depth: 2 });
    const nestedOk = await runMiddleware(nestedParser, 'item[label]=alpha&tags[]=js&tags[]=node', {
      'content-type': 'application/x-www-form-urlencoded',
    });
    assert.strictEqual(nestedOk.err, null);
    assert.deepStrictEqual(nestedOk.req.body, {
      item: { label: 'alpha' },
      tags: ['js', 'node'],
    });

    const tooDeep = await runMiddleware(nestedParser, 'a[b][c][d]=1', {
      'content-type': 'application/x-www-form-urlencoded',
    });
    assert.ok(tooDeep.err);
    assert.strictEqual(tooDeep.err.status, 400);

    const paramLimited = bodyParser.urlencoded({ extended: false, parameterLimit: 2 });
    const tooManyParams = await runMiddleware(paramLimited, 'a=1&b=2&c=3', {
      'content-type': 'application/x-www-form-urlencoded',
    });
    assert.ok(tooManyParams.err);
    assert.strictEqual(tooManyParams.err.status, 413);
    assert.strictEqual(tooManyParams.err.type, 'parameters.too.many');
  }

  // 7. Text and raw parsers.
  {
    const textMiddleware = bodyParser.text({ type: 'text/plain' });
    const textRes = await runMiddleware(textMiddleware, 'hello body-parser 2.4', {
      'content-type': 'text/plain; charset=utf-8',
    });
    assert.strictEqual(textRes.err, null);
    assert.strictEqual(textRes.req.body, 'hello body-parser 2.4');

    const rawMiddleware = bodyParser.raw({ type: 'application/octet-stream' });
    const bytes = Buffer.from([0x01, 0x02, 0x03, 0xff]);
    const rawRes = await runMiddleware(rawMiddleware, bytes, {
      'content-type': 'application/octet-stream',
    });
    assert.strictEqual(rawRes.err, null);
    assert.ok(Buffer.isBuffer(rawRes.req.body));
    assert.deepStrictEqual(rawRes.req.body, bytes);
  }

  console.log('body-parser@2.4.0 contract passed');
}

main().catch((err) => {
  console.error(err);
  process.exit(1);
});

Seeder d'origine

anonyme