From fa06999c8f47f75a3b85d19243a94dada61bfd02 Mon Sep 17 00:00:00 2001 From: Mike Date: Fri, 14 Aug 2026 15:15:09 +0300 Subject: [PATCH] feat(codec): publish the decoder as @w3tech.io/torpc-decoder The package ships built JavaScript with type declarations. Node does not strip types inside node_modules, so a .ts entry point cannot be imported; tsc emits the JS and rewriteRelativeImportExtensions turns './x.ts' specifiers into './x.js', so no source changed. Checkouts still import the sources directly. Adds a CI workflow: typecheck, tests, lint and the build across the workspace. The publish workflow is dispatch-only, gated on the prod environment, and smoke-tests the packed tarball in a throwaway project. 83 tests green, tsc clean, and the tarball installs and maps a block with logsBloom dropped and 23 methods in the registry. --- .github/workflows/ci.yml | 38 +++++ .github/workflows/publish-decoder.yml | 66 +++++++++ README.md | 20 ++- codec/LICENSE | 202 ++++++++++++++++++++++++++ codec/README.md | 16 +- codec/package.json | 53 ++++--- codec/scripts/conformance-adapter.ts | 2 +- codec/src/index.ts | 2 +- codec/tsconfig.build.json | 22 +++ packages/torpc-toevm-rules/README.md | 4 +- 10 files changed, 396 insertions(+), 29 deletions(-) create mode 100644 .github/workflows/ci.yml create mode 100644 .github/workflows/publish-decoder.yml create mode 100644 codec/LICENSE create mode 100644 codec/tsconfig.build.json diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..35faf50 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,38 @@ +name: CI + +on: + push: + branches: [main] + pull_request: + +permissions: + contents: read + +jobs: + gate: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v7 + - name: Install pnpm + uses: pnpm/action-setup@v6 + # version comes from the root package.json "packageManager" + - uses: actions/setup-node@v7 + with: + # The root package.json asks for >=24 and the sources are imported as + # TypeScript locally, which needs a Node that strips types. + node-version: 24 + cache: "pnpm" + - run: pnpm install --frozen-lockfile + - name: Type-check + run: pnpm -r typecheck + - name: Test + run: pnpm -r test + - name: Lint + # Only the bench package carries an eslint config today; the root script + # says so explicitly rather than pretending the whole repo is linted. + run: pnpm lint + - name: Build the publishable package + # Not decoration: the published entry point is built JS, because Node + # refuses to strip types inside node_modules. If this breaks, the package + # is unusable however green the tests are. + run: pnpm --filter @w3tech.io/torpc-decoder build diff --git a/.github/workflows/publish-decoder.yml b/.github/workflows/publish-decoder.yml new file mode 100644 index 0000000..3f06fca --- /dev/null +++ b/.github/workflows/publish-decoder.yml @@ -0,0 +1,66 @@ +name: Publish decoder + +# Manual only: the `prod` environment permits `main`, so a tag cannot deploy. +# The version comes from codec/package.json, never from a ref. +on: + workflow_dispatch: {} + +permissions: + contents: read + # No `id-token: write` yet: provenance and trusted publishing come once a + # publisher is registered for the package on npm. + +jobs: + publish: + runs-on: ubuntu-latest + # Requires an approval from the environment's reviewers, and NPMJS_TOKEN is + # scoped to it, so the job cannot read the token without that approval. + environment: prod + defaults: + run: + working-directory: codec + + steps: + - uses: actions/checkout@v7 + - name: Install pnpm + uses: pnpm/action-setup@v6 + - uses: actions/setup-node@v7 + with: + node-version: 24 + registry-url: https://registry.npmjs.org + cache: "pnpm" + # One pnpm workspace: install from the root so the lockfile resolves. + - run: pnpm install --frozen-lockfile + working-directory: . + # Mirror CI before publishing. + - name: Type-check + run: pnpm typecheck + - name: Test + run: pnpm test + - name: Build + run: pnpm build + # Node does not strip types inside node_modules, so a package can compile and + # still be unimportable. Check the packed tarball, not the working tree. + - name: Smoke-test the packed tarball + run: | + set -euo pipefail + TARBALL="$(pnpm pack --pack-destination "$RUNNER_TEMP" | tail -1)" + mkdir -p "$RUNNER_TEMP/consume" && cd "$RUNNER_TEMP/consume" + echo '{"name":"consume","private":true,"type":"module"}' > package.json + npm install --silent "$TARBALL" + cat > use.mjs <<'JS' + import { forward, registry } from '@w3tech.io/torpc-decoder'; + const out = forward('eth_getBlockByNumber', { + jsonrpc: '2.0', id: 1, + result: { number: '0x1', gasUsed: '0x5208', hash: '0xabc', + logsBloom: '0x' + '00'.repeat(256) }, + }); + if (!out || typeof out.result !== 'object') throw new Error('forward returned nothing usable'); + if ('logsBloom' in out.result) throw new Error('logsBloom survived the mapping'); + if (registry.size === 0) throw new Error('method registry is empty'); + console.log('ok: importable,', registry.size, 'methods'); + JS + node use.mjs + - run: npm publish --access public + env: + NODE_AUTH_TOKEN: ${{ secrets.NPMJS_TOKEN }} diff --git a/README.md b/README.md index d5325ee..936ddec 100644 --- a/README.md +++ b/README.md @@ -14,15 +14,27 @@ spec wins. | Path | Package | What it is | | ---- | ------- | ---------- | -| [`codec/`](./codec/README.md) | `@torpc/decoder` | Bidirectional mapper between standard EVM JSON-RPC responses and the TORPC compact form. Structural only: it reshapes what it is given and does not resolve ABIs. | +| [`codec/`](./codec/README.md) | [`@w3tech.io/torpc-decoder`](https://www.npmjs.com/package/@w3tech.io/torpc-decoder) | Bidirectional mapper between standard EVM JSON-RPC responses and the TORPC compact form. Structural only: it reshapes what it is given and does not resolve ABIs. | | [`packages/torpc-toevm-rules/`](./packages/torpc-toevm-rules/README.md) | `@torpc/toevm-rules` | The individual transform rules the mapper composes: hex stripping, hex to decimal, service-field dropping. | | [`bench/`](./bench/README.md) | not a package | The benchmark harness: how many tokens the transform actually saves, and whether a model can still answer questions from the compressed payload. Measured on captured Ethereum mainnet responses, not on synthetic data. | ## Status -**Neither package is on npm yet.** Both are marked `private` in their `package.json`, and the names -above are what they will carry rather than something you can install today. Use them from a checkout, -importing the TypeScript sources directly. +The codec is published as **`@w3tech.io/torpc-decoder`**: + +``` +npm install @w3tech.io/torpc-decoder +``` + +The rules package is not published yet. It is still marked `private`, and the name above is what it +will carry rather than something you can install; use it from a checkout. + +The published package ships built JavaScript with type declarations, not the TypeScript in +`codec/src`: Node does not strip types inside `node_modules`, so a `.ts` entry point cannot be +imported. + +Version 0.1.0. The spec's EVM tier 1 and tier 2 rules are normative, the conformance suite is a +single-case scaffold, and no implementation claims TORPC conformance yet. ## Running it diff --git a/codec/LICENSE b/codec/LICENSE new file mode 100644 index 0000000..d645695 --- /dev/null +++ b/codec/LICENSE @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/codec/README.md b/codec/README.md index 84c9390..19842f8 100644 --- a/codec/README.md +++ b/codec/README.md @@ -1,13 +1,21 @@ -# @torpc/decoder +# @w3tech.io/torpc-decoder Reference **bidirectional mapper** between standard EVM JSON-RPC responses and the **TORPC (Token Optimized RPC)** compact form. Structural only: **no ABI resolution, no decoding of raw calldata or logs, no re-encoding.** Producing the decoded `{event, args}` / `{function, args}` shapes is the server's job (spec tier T2). This library only reshapes what it is given. -**Not published to npm.** The package is marked `private` in `package.json`; use it from a checkout -of this repository, importing the TypeScript sources directly (no build step, see Development below). -`@torpc/decoder` is the name it carries in `package.json`, not a name you can install today. +``` +npm install @w3tech.io/torpc-decoder +``` + +```ts +import { forward, backward } from '@w3tech.io/torpc-decoder'; +``` + +The published package is built JavaScript with type declarations, not the TypeScript in `src`: Node +does not strip types inside `node_modules`, so a `.ts` entry point cannot be imported. Inside a +checkout the sources are imported directly, no build step needed. ```ts import { forward, backward } from './src/index.ts'; diff --git a/codec/package.json b/codec/package.json index b3de333..fe7e5d7 100644 --- a/codec/package.json +++ b/codec/package.json @@ -1,18 +1,45 @@ { - "name": "@torpc/decoder", - "version": "0.0.0", - "private": true, - "description": "Reference bidirectional mapper between standard EVM JSON-RPC responses and the TORPC compact form. Structural only — no ABI, no decoding.", + "name": "@w3tech.io/torpc-decoder", + "version": "0.1.0", + "description": "Reference TORPC codec: maps standard EVM JSON-RPC responses to the TORPC compact form and back. Structural only, no ABI resolution.", + "license": "Apache-2.0", "type": "module", + "exports": { + ".": { + "types": "./dist/index.d.ts", + "import": "./dist/index.js" + } + }, + "main": "./dist/index.js", + "types": "./dist/index.d.ts", + "files": [ + "dist" + ], "engines": { - "node": ">=22.6.0" + "node": ">=20" }, - "exports": { - ".": "./src/index.ts" + "repository": { + "type": "git", + "url": "git+https://github.com/w3tech/torpc-js.git", + "directory": "codec" }, + "homepage": "https://github.com/w3tech/torpc-js/tree/main/codec#readme", + "bugs": "https://github.com/w3tech/torpc-js/issues", + "keywords": [ + "torpc", + "json-rpc", + "evm", + "ethereum", + "ai-agents", + "llm", + "token-optimization", + "codec" + ], "scripts": { + "build": "rm -rf dist && tsc -p tsconfig.build.json", "test": "node --test", - "typecheck": "tsc --noEmit" + "typecheck": "tsc --noEmit", + "prepublishOnly": "pnpm typecheck && pnpm test && pnpm build" }, "dependencies": { "@noble/hashes": "^1.5.0" @@ -20,13 +47,5 @@ "devDependencies": { "@types/node": "^22.5.0", "typescript": "^5.6.0" - }, - "keywords": [ - "torpc", - "ethereum", - "json-rpc", - "evm", - "compact" - ], - "license": "Apache-2.0" + } } diff --git a/codec/scripts/conformance-adapter.ts b/codec/scripts/conformance-adapter.ts index a69b74f..638e0c8 100644 --- a/codec/scripts/conformance-adapter.ts +++ b/codec/scripts/conformance-adapter.ts @@ -1,6 +1,6 @@ #!/usr/bin/env node --experimental-strip-types /** - * Conformance adapter for @torpc/decoder. + * Conformance adapter for @w3tech.io/torpc-decoder. * * Implements the TORPC_CMD protocol of the spec repository's conformance * runner: read a job on stdin, write the transformed JSON-RPC response on diff --git a/codec/src/index.ts b/codec/src/index.ts index 69f1d74..fb1d504 100644 --- a/codec/src/index.ts +++ b/codec/src/index.ts @@ -1,5 +1,5 @@ /** - * @torpc/codec — reference bidirectional mapper between standard EVM JSON-RPC + * @w3tech.io/torpc-decoder — reference bidirectional mapper between standard EVM JSON-RPC * responses and the TORPC compact form. Structural only: no ABI resolution, * no decoding of raw calldata/logs, no re-encoding. See the design spec and * torpc/specs/evm-v1.md. diff --git a/codec/tsconfig.build.json b/codec/tsconfig.build.json new file mode 100644 index 0000000..0519da3 --- /dev/null +++ b/codec/tsconfig.build.json @@ -0,0 +1,22 @@ +{ + // Emit config for the published artifact. tsconfig.json stays noEmit: inside a + // checkout the sources are imported as TypeScript. + // + // The build is required, not a convenience: Node does not strip types inside + // node_modules (ERR_UNSUPPORTED_NODE_MODULES_TYPE_STRIPPING), so a package whose + // entry point is a .ts file cannot be imported. + "extends": "./tsconfig.json", + "compilerOptions": { + "noEmit": false, + // Sources import each other as './x.ts'; tsc rewrites those to './x.js' on + // the way out (TS 5.7+), so no source change is needed. + "allowImportingTsExtensions": false, + "rewriteRelativeImportExtensions": true, + "declaration": true, + "declarationMap": true, + "sourceMap": true, + "outDir": "dist", + "rootDir": "src" + }, + "include": ["src"] +} diff --git a/packages/torpc-toevm-rules/README.md b/packages/torpc-toevm-rules/README.md index 1160606..1281b60 100644 --- a/packages/torpc-toevm-rules/README.md +++ b/packages/torpc-toevm-rules/README.md @@ -23,7 +23,7 @@ when it is `null`. Nothing here implements T2, tier negotiation, the `Accept-Tok `Token-Tier` headers, or method dispatch. If you want a complete, conformance-tested implementation of the spec, use -[`@torpc/decoder`](https://github.com/w3tech/torpc-js) (`codec/` in that repository), which maps +[`@w3tech.io/torpc-decoder`](https://www.npmjs.com/package/@w3tech.io/torpc-decoder) (`codec/` in that repository), which maps whole responses per method in both directions and runs against the conformance vectors. This package exists so the individual primitives can be named, read and reused in isolation. @@ -115,7 +115,7 @@ const topics = raw.logs[0].topics.map((t, i) => (i === 0 ? t : hexStrip(t))); `topics[0]` is kept verbatim: it is the event-signature hash, not a padded value. A `composeRules` entry point that applies a full per-method mapping in one call is not part of this -package and is not planned here, because `@torpc/decoder` already does that job. +package and is not planned here, because `@w3tech.io/torpc-decoder` already does that job. ## Development