eth-compress 0.3.0 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +30 -43
- package/_cjs/index.cjs +1 -1
- package/_cjs/index.cjs.map +3 -3
- package/_cjs/index.node.cjs +1 -1
- package/_cjs/index.node.cjs.map +3 -3
- package/_cjs/jit-compressor.cjs +1 -1
- package/_cjs/jit-compressor.cjs.map +3 -3
- package/_esm/index.js +1 -1
- package/_esm/index.js.map +3 -3
- package/_esm/index.node.js +1 -1
- package/_esm/index.node.js.map +3 -3
- package/_esm/jit-compressor.js +1 -1
- package/_esm/jit-compressor.js.map +3 -3
- package/_types/compiler/jit.d.ts +1 -0
- package/_types/compiler/jit.d.ts.map +1 -1
- package/_types/compiler/opcodes.d.ts +0 -1
- package/_types/compiler/opcodes.d.ts.map +1 -1
- package/_types/compiler/utils.d.ts +13 -24
- package/_types/compiler/utils.d.ts.map +1 -1
- package/_types/index.d.ts.map +1 -1
- package/_types/jit-compressor.d.ts.map +1 -1
- package/index.ts +2 -2
- package/jit-compressor.ts +11 -8
- package/package.json +8 -8
package/README.md
CHANGED
|
@@ -1,15 +1,16 @@
|
|
|
1
1
|
## eth-compress
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A compact client-side module for compressing Ethereum JSON-RPC requests, targeting **lower latency** and gas-efficient **read-only calls** with large calldata.
|
|
4
4
|
|
|
5
|
-
It combines [RFC 9110](https://www.rfc-editor.org/rfc/rfc9110.html#section-12.5.3)-compliant negotiation for client-to-server compression, with optional JIT-compiled calldata compression
|
|
5
|
+
It combines [RFC 9110](https://www.rfc-editor.org/rfc/rfc9110.html#section-12.5.3)-compliant negotiation for client-to-server compression, with optional JIT-compiled calldata compression.
|
|
6
6
|
|
|
7
|
-
_Plug'n
|
|
7
|
+
_Plug 'n' play with viem and a simple API_
|
|
8
8
|
|
|
9
9
|
### Scope
|
|
10
|
-
-
|
|
11
|
-
-
|
|
12
|
-
- HTTP
|
|
10
|
+
- **Read-only** `eth_call`s.
|
|
11
|
+
- Min. input threshold: > 1150 bytes.
|
|
12
|
+
- HTTP: Uses RFC 9110-compliant `Content-Encoding` negotiation (gzip/deflate).
|
|
13
|
+
- EVM/JIT: Routes `eth_call`s through a transient decompressor contract.
|
|
13
14
|
|
|
14
15
|
### Installation
|
|
15
16
|
|
|
@@ -17,9 +18,9 @@ _Plug'n Play with viem & with a simple API_
|
|
|
17
18
|
npm i eth-compress
|
|
18
19
|
```
|
|
19
20
|
---
|
|
20
|
-
### HTTP request compression
|
|
21
|
+
### HTTP request compression
|
|
21
22
|
|
|
22
|
-
|
|
23
|
+
Transparently compresses request bodies using the [CompressionStream API](https://developer.mozilla.org/en-US/docs/Web/API/CompressionStream).
|
|
23
24
|
|
|
24
25
|
```ts
|
|
25
26
|
import { compressModule } from 'eth-compress';
|
|
@@ -72,18 +73,6 @@ const client = createPublicClient({
|
|
|
72
73
|
});
|
|
73
74
|
```
|
|
74
75
|
|
|
75
|
-
Proactive:
|
|
76
|
-
```ts
|
|
77
|
-
import { compressModule } from 'eth-compress';
|
|
78
|
-
|
|
79
|
-
const client = createPublicClient({
|
|
80
|
-
chain: base,
|
|
81
|
-
transport: http(rpcUrl, {
|
|
82
|
-
fetchFn: (url, init) => compressModule(url, init, 'proactive'),
|
|
83
|
-
}),
|
|
84
|
-
});
|
|
85
|
-
```
|
|
86
|
-
|
|
87
76
|
JIT calldata compression:
|
|
88
77
|
```ts
|
|
89
78
|
import { compressModule } from 'eth-compress';
|
|
@@ -96,19 +85,19 @@ const client = createPublicClient({
|
|
|
96
85
|
}),
|
|
97
86
|
});
|
|
98
87
|
```
|
|
99
|
-
#### thats it.
|
|
100
88
|
----
|
|
101
89
|
### Compatibility
|
|
102
|
-
- Preserves viem semantics: responses and error handling are unchanged; only the request
|
|
103
|
-
- Works in Node and modern browsers that support the
|
|
90
|
+
- Preserves viem semantics: responses and error handling are unchanged; only the request body is compressed.
|
|
91
|
+
- Works in Node and modern browsers that support the CompressionStream API.
|
|
92
|
+
<br><a href="https://caniuse.com/mdn-api_compressionstream">Chrome/Edge ≥ 80; Firefox ≥ 113; Safari/iOS ≥ 16.4</a>
|
|
104
93
|
|
|
105
94
|
<br>
|
|
106
95
|
|
|
107
96
|
----
|
|
108
97
|
|
|
109
|
-
###
|
|
98
|
+
### JIT calldata compression for `eth_call`
|
|
110
99
|
|
|
111
|
-
|
|
100
|
+
Eligible `eth_call`s are compiled into a transient decompressor contract (passed via `stateDiff`).
|
|
112
101
|
|
|
113
102
|
```ts
|
|
114
103
|
import { compress_call } from 'eth-compress/compressor';
|
|
@@ -118,37 +107,35 @@ const payload = {
|
|
|
118
107
|
params: [
|
|
119
108
|
{
|
|
120
109
|
to: '0x…',
|
|
121
|
-
data: '0x…',
|
|
110
|
+
data: '0x…',
|
|
122
111
|
},
|
|
123
112
|
'latest',
|
|
124
113
|
],
|
|
125
114
|
};
|
|
126
115
|
|
|
127
|
-
const compressedPayload = compress_call(payload);
|
|
116
|
+
const compressedPayload = compress_call(payload);
|
|
128
117
|
```
|
|
129
118
|
|
|
130
|
-
`compress_call` can be passed directly to `compressModule` as a custom transform. For eligible `eth_call`s it chooses between:
|
|
119
|
+
`compress_call` can be passed directly to `compressModule` as a custom transform. For eligible `eth_call`s, it chooses between:
|
|
131
120
|
|
|
132
121
|
- **JIT**: Compiles just-in-time, a one-off decompressor contract that reconstructs calldata to forward the call.
|
|
133
|
-
- **FLZ / CD**: Uses `LibZip.flzCompress` and `LibZip.cdCompress` from `solady` for
|
|
134
|
-
|
|
135
|
-
Selection logic (subject to change, but current behaviour):
|
|
122
|
+
- **FLZ / CD**: Uses `LibZip.flzCompress` and `LibZip.cdCompress` from `solady` for FastLZ / RLE compression.
|
|
136
123
|
|
|
137
124
|
- **Size gating (JIT / EVM path)**:
|
|
138
125
|
- `< 1150 bytes (effective payload)`: no EVM-level compression.
|
|
139
126
|
- `≥ 1150 bytes`: compression considered.
|
|
140
127
|
- `size ≤ ~3000 bytes or > ~8000 bytes`: JIT is preferred.
|
|
141
|
-
- `~3000 ≤ size ≤ ~8000 bytes`: Best
|
|
128
|
+
- `~3000 ≤ size ≤ ~8000 bytes`: Best-of-3.
|
|
142
129
|
|
|
143
130
|
- **Algorithm choice**:
|
|
144
131
|
- For mid-sized payloads, FLZ and CD are tried and the smaller output is chosen.
|
|
145
|
-
- For larger
|
|
146
|
-
- The thresholds are chosen with
|
|
132
|
+
- For larger ones, JIT is used directly, prioritizing gas efficiency.
|
|
133
|
+
- The thresholds are chosen with request header overhead and latency in mind,
|
|
147
134
|
aiming to keep the total request size within the [Ethernet MTU](https://en.wikipedia.org/wiki/Maximum_transmission_unit).
|
|
148
135
|
|
|
149
136
|
### Important considerations
|
|
150
137
|
|
|
151
|
-
The JIT calldata compressor is **experimental** and intended for read-only `eth_call`s
|
|
138
|
+
The JIT calldata compressor is **experimental** and intended for auxiliary/bulk dApp read-only `eth_call`s. Use two viem clients to separate concerns.
|
|
152
139
|
|
|
153
140
|
### Compression Ratio & Gas
|
|
154
141
|
| Tx Size Range | # Txns | Avg. Tx Size| JIT Ratio | FLZ Ratio | CD Ratio | JIT Gas | FLZ Gas | CD Gas |
|
|
@@ -157,16 +144,16 @@ The JIT calldata compressor is **experimental** and intended for read-only `eth_
|
|
|
157
144
|
| **3–8 KB** | 260 | 4.82 kb | 2.77x | 2.59x | **2.81x** | **4.45k** | 138k | 88.9k |
|
|
158
145
|
| **1.15–3 KB** | 599 | 2.02 kb | **2.89x** | 1.91x | 2.58x | **3.35k** | 68.4k | 35.8k |
|
|
159
146
|
|
|
160
|
-
<sub>Excludes txns not compressible <70% of original size.</sub>
|
|
147
|
+
<sub>Excludes txns not compressible to <70% of original size.</sub>
|
|
161
148
|
|
|
162
|
-
### Compression
|
|
163
|
-
- **JIT calldata compiler (`compress_call` JIT mode)**: Views
|
|
149
|
+
### Compression flavors
|
|
150
|
+
- **JIT calldata compiler (`compress_call` JIT mode)**: Views calldata as a zero‑initialized memory image and synthesizes bytecode that rebuilds it word-by-word in-place.
|
|
164
151
|
|
|
165
152
|
In the first pass it walks the data in 32-byte slices, detects non-zero segments per word, and for each word chooses the cheapest of three strategies: store a literal tail, assemble segments using SHL/OR, or reuse an earlier word via MLOAD/MSTORE.
|
|
166
|
-
|
|
167
|
-
In the second pass it materialises this plan into concrete PUSH/MSTORE/SHL/OR/DUP opcodes, pre-seeds the stack with frequently used constants, and appends a small CALL/RETURNDATA stub that forwards the reconstructed calldata to the original `to` address.
|
|
168
|
-
|
|
169
|
-
The execution is realized through a `stateDiff` passed together with the `eth_call`. The 4‑byte selector is right‑aligned in the first 32‑byte slot so that the rest of the calldata can be reconstructed on mostly word‑aligned boundaries, with the decompressor stateDiff being placed at `0x00000000000000000000000000000000000000e0` such that `0xe0` can be obtained from `ADDRESS` with a single opcode instead of an explicit literal.
|
|
170
153
|
|
|
171
|
-
|
|
154
|
+
In the second pass it materializes this plan into concrete PUSH/MSTORE/SHL/OR/DUP opcodes, pre-seeds the stack with frequently used constants, and appends a small CALL/RETURNDATA stub that forwards the reconstructed calldata to the original `to` address.
|
|
155
|
+
|
|
156
|
+
The 4‑byte selector is right‑aligned in the first 32‑byte slot so that the rest of the calldata can be reconstructed on mostly word‑aligned boundaries, with the decompressor stateDiff being placed at `0xe0` to obtain this common offset from `ADDRESS` with a single opcode instead of PUSH1 + literal.
|
|
157
|
+
|
|
158
|
+
Both the FastLZ and calldata-RLE forwarders are minimally adapted from Solady's [`LibZip.sol`](https://github.com/Vectorized/solady/blob/main/src/utils/LibZip.sol) and inlined as raw bytecode. To avoid Solidity's wrapper overhead the code is compiled from pure yul.
|
|
172
159
|
|
package/_cjs/index.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var u=Object.defineProperty;var
|
|
1
|
+
var u=Object.defineProperty;var m=Object.getOwnPropertyDescriptor;var b=Object.getOwnPropertyNames;var w=Object.prototype.hasOwnProperty;var R=(n,e)=>{for(var o in e)u(n,o,{get:e[o],enumerable:!0})},C=(n,e,o,s)=>{if(e&&typeof e=="object"||typeof e=="function")for(let t of b(e))!w.call(n,t)&&t!==o&&u(n,t,{get:()=>e[t],enumerable:!(s=m(e,t))||s.enumerable});return n};var E=n=>C(u({},"__esModule",{value:!0}),n);var v={};R(v,{MIN_BODY_SIZE:()=>_,compressModule:()=>M});module.exports=E(v);var _=1150,q=typeof CompressionStream<"u",S=new Map,I=["gzip","deflate"];async function M(n,e,o){let s=n instanceof Request?n:null,t=typeof n=="string"?n:n instanceof Request?n.url:n.toString(),r=typeof e?.body=="string"?e.body:null;if(typeof o=="function"){if(s&&!e)return fetch(s);let f=e?.body;if(r)try{let a=o(JSON.parse(r));a!==void 0&&(f=JSON.stringify(a))}catch{}return fetch(s??t,{...e,body:f})}let c=S.get(t),g=o==="gzip"||o==="deflate",i=q?g?o:o==="proactive"?c===-1?null:c??"gzip":typeof c=="string"?c:null:null,y=!!i&&!!r&&r.length>=1150,p={...e,priority:"high"},h=new Headers(p.headers);y&&(p.body=await new Response(new Blob([r]).stream().pipeThrough(new CompressionStream(i))).blob(),h.set("Content-Encoding",i)),p.headers=h;let d=await fetch(s??t,p);if(!g&&c===void 0){let a=d.headers.get("Accept-Encoding")?.split(",").map(l=>l.trim()).find(l=>I.includes(l))??-1;S.set(t,o==="proactive"&&y&&d.ok?i:a)}return d}0&&(module.exports={MIN_BODY_SIZE,compressModule});
|
|
2
2
|
//# sourceMappingURL=index.cjs.map
|
package/_cjs/index.cjs.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/index.ts"],
|
|
4
|
-
"sourcesContent": ["export const MIN_BODY_SIZE = 1150;\n\nconst _cache = new Map<string, string | -1>();\nconst _enc = ['gzip', 'deflate'] as const;\ntype SupportedEncoding = (typeof _enc)[number];\n\nexport type PayloadTransform = (payload: unknown) => unknown;\nexport type CompressionMode = 'passive' | 'proactive' | 'gzip' | 'deflate' | PayloadTransform;\n\n/**\n * @param input - URL or Request\n * @param init - Request options\n * @param mode - Compression mode:\n * - 'passive' (default): discover support via Accept-Encoding header first\n * - 'proactive': compress with gzip first, adjust if server rejects\n * - 'gzip' | 'deflate': use specified encoding directly (known support)\n * - PayloadTransform function: transform payload, skip HTTP compression\n */\nexport async function compressModule(\n input: string | URL | Request,\n init?: RequestInit,\n mode?: CompressionMode,\n): Promise<Response> {\n const req = input instanceof Request ? input : null;\n const url =\n typeof input === 'string' ? input : input instanceof Request ? input.url : input.toString();\n const bodyStr = typeof init?.body === 'string' ? init.body : null;\n\n // Custom transform: apply and skip HTTP compression\n if (typeof mode === 'function') {\n if (req && !init) return fetch(req);\n let body = init?.body;\n if (bodyStr)\n try {\n const next = mode(JSON.parse(bodyStr));\n if (next !== undefined) body = JSON.stringify(next);\n } catch {}\n return fetch(req ?? url, { ...init, body });\n }\n\n const cached = _cache.get(url);\n const
|
|
5
|
-
"mappings": "4ZAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,mBAAAE,EAAA,mBAAAC,IAAA,eAAAC,EAAAJ,GAAO,IAAME,EAAgB,KAEvBG,EAAS,IAAI,IACbC,EAAO,CAAC,OAAQ,SAAS,EAe/B,
|
|
6
|
-
"names": ["index_exports", "__export", "MIN_BODY_SIZE", "compressModule", "__toCommonJS", "_cache", "_enc", "input", "init", "mode", "req", "url", "bodyStr", "body", "next", "cached", "
|
|
4
|
+
"sourcesContent": ["export const MIN_BODY_SIZE = 1150;\n\nconst _hasCS = typeof CompressionStream !== 'undefined';\nconst _cache = new Map<string, string | -1>();\nconst _enc = ['gzip', 'deflate'] as const;\ntype SupportedEncoding = (typeof _enc)[number];\n\nexport type PayloadTransform = (payload: unknown) => unknown;\nexport type CompressionMode = 'passive' | 'proactive' | 'gzip' | 'deflate' | PayloadTransform;\n\n/**\n * @param input - URL or Request\n * @param init - Request options\n * @param mode - Compression mode:\n * - 'passive' (default): discover support via Accept-Encoding header first\n * - 'proactive': compress with gzip first, adjust if server rejects\n * - 'gzip' | 'deflate': use specified encoding directly (known support)\n * - PayloadTransform function: transform payload, skip HTTP compression\n */\nexport async function compressModule(\n input: string | URL | Request,\n init?: RequestInit,\n mode?: CompressionMode,\n): Promise<Response> {\n const req = input instanceof Request ? input : null;\n const url =\n typeof input === 'string' ? input : input instanceof Request ? input.url : input.toString();\n const bodyStr = typeof init?.body === 'string' ? init.body : null;\n\n // Custom transform: apply and skip HTTP compression\n if (typeof mode === 'function') {\n if (req && !init) return fetch(req);\n let body = init?.body;\n if (bodyStr)\n try {\n const next = mode(JSON.parse(bodyStr));\n if (next !== undefined) body = JSON.stringify(next);\n } catch {}\n return fetch(req ?? url, { ...init, body });\n }\n\n const cached = _cache.get(url);\n const known = mode === 'gzip' || mode === 'deflate';\n const encoding = !_hasCS\n ? null\n : known\n ? (mode as SupportedEncoding)\n : mode === 'proactive'\n ? cached === -1\n ? null\n : (cached ?? 'gzip')\n : typeof cached === 'string'\n ? cached\n : null;\n\n const shouldCompress = !!encoding && !!bodyStr && bodyStr.length >= MIN_BODY_SIZE;\n const opts: RequestInit = { ...init, priority: 'high' as RequestPriority };\n const headers = new Headers(opts.headers);\n if (shouldCompress) {\n opts.body = await new Response(\n new Blob([bodyStr!])\n .stream()\n .pipeThrough(new CompressionStream(encoding as CompressionFormat)),\n ).blob();\n headers.set('Content-Encoding', encoding);\n }\n opts.headers = headers;\n\n const response = await fetch(req ?? url, opts);\n\n // Cache discovery for passive/proactive (not known modes)\n if (!known && cached === undefined) {\n const header = response.headers.get('Accept-Encoding');\n const discovered =\n header\n ?.split(',')\n .map((e) => e.trim())\n .find((e): e is SupportedEncoding => _enc.includes(e as SupportedEncoding)) ?? -1;\n _cache.set(\n url,\n mode === 'proactive' && shouldCompress ? (response.ok ? encoding! : discovered) : discovered,\n );\n }\n\n return response;\n}\n"],
|
|
5
|
+
"mappings": "4ZAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,mBAAAE,EAAA,mBAAAC,IAAA,eAAAC,EAAAJ,GAAO,IAAME,EAAgB,KAEvBG,EAAS,OAAO,kBAAsB,IACtCC,EAAS,IAAI,IACbC,EAAO,CAAC,OAAQ,SAAS,EAe/B,eAAsBJ,EACpBK,EACAC,EACAC,EACmB,CACnB,IAAMC,EAAMH,aAAiB,QAAUA,EAAQ,KACzCI,EACJ,OAAOJ,GAAU,SAAWA,EAAQA,aAAiB,QAAUA,EAAM,IAAMA,EAAM,SAAS,EACtFK,EAAU,OAAOJ,GAAM,MAAS,SAAWA,EAAK,KAAO,KAG7D,GAAI,OAAOC,GAAS,WAAY,CAC9B,GAAIC,GAAO,CAACF,EAAM,OAAO,MAAME,CAAG,EAClC,IAAIG,EAAOL,GAAM,KACjB,GAAII,EACF,GAAI,CACF,IAAME,EAAOL,EAAK,KAAK,MAAMG,CAAO,CAAC,EACjCE,IAAS,SAAWD,EAAO,KAAK,UAAUC,CAAI,EACpD,MAAQ,CAAC,CACX,OAAO,MAAMJ,GAAOC,EAAK,CAAE,GAAGH,EAAM,KAAAK,CAAK,CAAC,CAC5C,CAEA,IAAME,EAASV,EAAO,IAAIM,CAAG,EACvBK,EAAQP,IAAS,QAAUA,IAAS,UACpCQ,EAAYb,EAEdY,EACGP,EACDA,IAAS,YACPM,IAAW,GACT,KACCA,GAAU,OACb,OAAOA,GAAW,SAChBA,EACA,KATN,KAWEG,EAAiB,CAAC,CAACD,GAAY,CAAC,CAACL,GAAWA,EAAQ,QAAU,KAC9DO,EAAoB,CAAE,GAAGX,EAAM,SAAU,MAA0B,EACnEY,EAAU,IAAI,QAAQD,EAAK,OAAO,EACpCD,IACFC,EAAK,KAAO,MAAM,IAAI,SACpB,IAAI,KAAK,CAACP,CAAQ,CAAC,EAChB,OAAO,EACP,YAAY,IAAI,kBAAkBK,CAA6B,CAAC,CACrE,EAAE,KAAK,EACPG,EAAQ,IAAI,mBAAoBH,CAAQ,GAE1CE,EAAK,QAAUC,EAEf,IAAMC,EAAW,MAAM,MAAMX,GAAOC,EAAKQ,CAAI,EAG7C,GAAI,CAACH,GAASD,IAAW,OAAW,CAElC,IAAMO,EADSD,EAAS,QAAQ,IAAI,iBAAiB,GAG/C,MAAM,GAAG,EACV,IAAKE,GAAMA,EAAE,KAAK,CAAC,EACnB,KAAMA,GAA8BjB,EAAK,SAASiB,CAAsB,CAAC,GAAK,GACnFlB,EAAO,IACLM,EACAF,IAAS,aAAeS,GAAkBG,EAAS,GAAKJ,EAA0BK,CACpF,CACF,CAEA,OAAOD,CACT",
|
|
6
|
+
"names": ["index_exports", "__export", "MIN_BODY_SIZE", "compressModule", "__toCommonJS", "_hasCS", "_cache", "_enc", "input", "init", "mode", "req", "url", "bodyStr", "body", "next", "cached", "known", "encoding", "shouldCompress", "opts", "headers", "response", "discovered", "e"]
|
|
7
7
|
}
|
package/_cjs/index.node.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var
|
|
1
|
+
var C=Object.create;var d=Object.defineProperty;var R=Object.getOwnPropertyDescriptor;var z=Object.getOwnPropertyNames;var q=Object.getPrototypeOf,D=Object.prototype.hasOwnProperty;var E=(t,s)=>{for(var e in s)d(t,e,{get:s[e],enumerable:!0})},b=(t,s,e,n)=>{if(s&&typeof s=="object"||typeof s=="function")for(let o of z(s))!D.call(t,o)&&o!==e&&d(t,o,{get:()=>s[o],enumerable:!(n=R(s,o))||n.enumerable});return t};var I=(t,s,e)=>(e=t!=null?C(q(t)):{},b(s||!t||!t.__esModule?d(e,"default",{value:t,enumerable:!0}):e,t)),_=t=>b(d({},"__esModule",{value:!0}),t);var M={};E(M,{MIN_BODY_SIZE:()=>k,compressModule:()=>x});module.exports=_(M);var r=I(require("node:zlib"),1);var k=1150,v=typeof CompressionStream<"u",S=new Map,T=["gzip","deflate"];async function x(t,s,e){let n=t instanceof Request?t:null,o=typeof t=="string"?t:t instanceof Request?t.url:t.toString(),a=typeof s?.body=="string"?s.body:null;if(typeof e=="function"){if(n&&!s)return fetch(n);let g=s?.body;if(a)try{let c=e(JSON.parse(a));c!==void 0&&(g=JSON.stringify(c))}catch{}return fetch(n??o,{...s,body:g})}let i=S.get(o),y=e==="gzip"||e==="deflate",l=v?y?e:e==="proactive"?i===-1?null:i??"gzip":typeof i=="string"?i:null:null,m=!!l&&!!a&&a.length>=1150,p={...s,priority:"high"},h=new Headers(p.headers);m&&(p.body=await new Response(new Blob([a]).stream().pipeThrough(new CompressionStream(l))).blob(),h.set("Content-Encoding",l)),p.headers=h;let f=await fetch(n??o,p);if(!y&&i===void 0){let c=f.headers.get("Accept-Encoding")?.split(",").map(u=>u.trim()).find(u=>T.includes(u))??-1;S.set(o,e==="proactive"&&m&&f.ok?l:c)}return f}var w=(t,s)=>Object.assign(t,{writable:new WritableStream({write(e){return s.write(e),Promise.resolve()},close(){return s.end(),Promise.resolve()}}),readable:new ReadableStream({type:"bytes",start(e){s.on("data",n=>e.enqueue(n)),s.once("end",()=>e.close())}})});globalThis.CompressionStream||(globalThis.CompressionStream=class{constructor(s){let e;s==="deflate"?e=r.default.createDeflate():s==="gzip"?e=r.default.createGzip():s==="br"?e=r.default.createBrotliCompress():e=r.default.createDeflateRaw(),w(this,e)}});globalThis.DecompressionStream||(globalThis.DecompressionStream=class{constructor(s){let e;s==="deflate"?e=r.default.createInflate():s==="gzip"?e=r.default.createGunzip():s==="br"?e=r.default.createBrotliDecompress():e=r.default.createInflateRaw(),w(this,e)}});0&&(module.exports={MIN_BODY_SIZE,compressModule});
|
|
2
2
|
//# sourceMappingURL=index.node.cjs.map
|
package/_cjs/index.node.cjs.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/index.node.ts", "../../src/index.ts"],
|
|
4
|
-
"sourcesContent": ["import zlib from 'node:zlib';\n\n// Polyfill for CompressionStream/DecompressionStream in older versions of Node.js & Bun\nconst make = (ctx: any, handle: any) =>\n Object.assign(ctx, {\n writable: new WritableStream({\n write(chunk) {\n handle.write(chunk);\n return Promise.resolve();\n },\n close() {\n handle.end();\n return Promise.resolve();\n },\n }),\n readable: new ReadableStream({\n type: 'bytes',\n start(ctrl) {\n handle.on('data', (chunk: any) => ctrl.enqueue(chunk));\n handle.once('end', () => ctrl.close());\n },\n }),\n });\n\nif (!globalThis.CompressionStream) {\n globalThis.CompressionStream = class CompressionStream {\n constructor(format: string) {\n let handle;\n if (format === 'deflate') {\n handle = zlib.createDeflate();\n } else if (format === 'gzip') {\n handle = zlib.createGzip();\n } else if (format === 'br') {\n handle = zlib.createBrotliCompress();\n } else {\n handle = zlib.createDeflateRaw();\n }\n make(this, handle);\n }\n } as any;\n}\n\nif (!globalThis.DecompressionStream) {\n globalThis.DecompressionStream = class DecompressionStream {\n constructor(format: string) {\n let handle;\n if (format === 'deflate') {\n handle = zlib.createInflate();\n } else if (format === 'gzip') {\n handle = zlib.createGunzip();\n } else if (format === 'br') {\n handle = zlib.createBrotliDecompress();\n } else {\n handle = zlib.createInflateRaw();\n }\n make(this, handle);\n }\n } as any;\n}\n\nexport * from './index';\n", "export const MIN_BODY_SIZE = 1150;\n\nconst _cache = new Map<string, string | -1>();\nconst _enc = ['gzip', 'deflate'] as const;\ntype SupportedEncoding = (typeof _enc)[number];\n\nexport type PayloadTransform = (payload: unknown) => unknown;\nexport type CompressionMode = 'passive' | 'proactive' | 'gzip' | 'deflate' | PayloadTransform;\n\n/**\n * @param input - URL or Request\n * @param init - Request options\n * @param mode - Compression mode:\n * - 'passive' (default): discover support via Accept-Encoding header first\n * - 'proactive': compress with gzip first, adjust if server rejects\n * - 'gzip' | 'deflate': use specified encoding directly (known support)\n * - PayloadTransform function: transform payload, skip HTTP compression\n */\nexport async function compressModule(\n input: string | URL | Request,\n init?: RequestInit,\n mode?: CompressionMode,\n): Promise<Response> {\n const req = input instanceof Request ? input : null;\n const url =\n typeof input === 'string' ? input : input instanceof Request ? input.url : input.toString();\n const bodyStr = typeof init?.body === 'string' ? init.body : null;\n\n // Custom transform: apply and skip HTTP compression\n if (typeof mode === 'function') {\n if (req && !init) return fetch(req);\n let body = init?.body;\n if (bodyStr)\n try {\n const next = mode(JSON.parse(bodyStr));\n if (next !== undefined) body = JSON.stringify(next);\n } catch {}\n return fetch(req ?? url, { ...init, body });\n }\n\n const cached = _cache.get(url);\n const
|
|
5
|
-
"mappings": "6iBAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,mBAAAE,EAAA,mBAAAC,IAAA,eAAAC,EAAAJ,GAAA,IAAAK,EAAiB,0BCAV,IAAMC,EAAgB,KAEvBC,EAAS,IAAI,IACbC,EAAO,CAAC,OAAQ,SAAS,EAe/B,eAAsBC,EACpBC,EACAC,EACAC,EACmB,CACnB,IAAMC,EAAMH,aAAiB,QAAUA,EAAQ,KACzCI,EACJ,OAAOJ,GAAU,SAAWA,EAAQA,aAAiB,QAAUA,EAAM,IAAMA,EAAM,SAAS,EACtFK,EAAU,OAAOJ,GAAM,MAAS,SAAWA,EAAK,KAAO,KAG7D,GAAI,OAAOC,GAAS,WAAY,CAC9B,GAAIC,GAAO,CAACF,EAAM,OAAO,MAAME,CAAG,EAClC,IAAIG,EAAOL,GAAM,KACjB,GAAII,EACF,GAAI,CACF,IAAME,EAAOL,EAAK,KAAK,MAAMG,CAAO,CAAC,EACjCE,IAAS,SAAWD,EAAO,KAAK,UAAUC,CAAI,EACpD,MAAQ,CAAC,CACX,OAAO,MAAMJ,GAAOC,EAAK,CAAE,GAAGH,EAAM,KAAAK,CAAK,CAAC,CAC5C,CAEA,IAAME,EAASX,EAAO,IAAIO,CAAG,EACvBK,
|
|
6
|
-
"names": ["index_node_exports", "__export", "MIN_BODY_SIZE", "compressModule", "__toCommonJS", "import_node_zlib", "MIN_BODY_SIZE", "_cache", "_enc", "compressModule", "input", "init", "mode", "req", "url", "bodyStr", "body", "next", "cached", "
|
|
4
|
+
"sourcesContent": ["import zlib from 'node:zlib';\n\n// Polyfill for CompressionStream/DecompressionStream in older versions of Node.js & Bun\nconst make = (ctx: any, handle: any) =>\n Object.assign(ctx, {\n writable: new WritableStream({\n write(chunk) {\n handle.write(chunk);\n return Promise.resolve();\n },\n close() {\n handle.end();\n return Promise.resolve();\n },\n }),\n readable: new ReadableStream({\n type: 'bytes',\n start(ctrl) {\n handle.on('data', (chunk: any) => ctrl.enqueue(chunk));\n handle.once('end', () => ctrl.close());\n },\n }),\n });\n\nif (!globalThis.CompressionStream) {\n globalThis.CompressionStream = class CompressionStream {\n constructor(format: string) {\n let handle;\n if (format === 'deflate') {\n handle = zlib.createDeflate();\n } else if (format === 'gzip') {\n handle = zlib.createGzip();\n } else if (format === 'br') {\n handle = zlib.createBrotliCompress();\n } else {\n handle = zlib.createDeflateRaw();\n }\n make(this, handle);\n }\n } as any;\n}\n\nif (!globalThis.DecompressionStream) {\n globalThis.DecompressionStream = class DecompressionStream {\n constructor(format: string) {\n let handle;\n if (format === 'deflate') {\n handle = zlib.createInflate();\n } else if (format === 'gzip') {\n handle = zlib.createGunzip();\n } else if (format === 'br') {\n handle = zlib.createBrotliDecompress();\n } else {\n handle = zlib.createInflateRaw();\n }\n make(this, handle);\n }\n } as any;\n}\n\nexport * from './index';\n", "export const MIN_BODY_SIZE = 1150;\n\nconst _hasCS = typeof CompressionStream !== 'undefined';\nconst _cache = new Map<string, string | -1>();\nconst _enc = ['gzip', 'deflate'] as const;\ntype SupportedEncoding = (typeof _enc)[number];\n\nexport type PayloadTransform = (payload: unknown) => unknown;\nexport type CompressionMode = 'passive' | 'proactive' | 'gzip' | 'deflate' | PayloadTransform;\n\n/**\n * @param input - URL or Request\n * @param init - Request options\n * @param mode - Compression mode:\n * - 'passive' (default): discover support via Accept-Encoding header first\n * - 'proactive': compress with gzip first, adjust if server rejects\n * - 'gzip' | 'deflate': use specified encoding directly (known support)\n * - PayloadTransform function: transform payload, skip HTTP compression\n */\nexport async function compressModule(\n input: string | URL | Request,\n init?: RequestInit,\n mode?: CompressionMode,\n): Promise<Response> {\n const req = input instanceof Request ? input : null;\n const url =\n typeof input === 'string' ? input : input instanceof Request ? input.url : input.toString();\n const bodyStr = typeof init?.body === 'string' ? init.body : null;\n\n // Custom transform: apply and skip HTTP compression\n if (typeof mode === 'function') {\n if (req && !init) return fetch(req);\n let body = init?.body;\n if (bodyStr)\n try {\n const next = mode(JSON.parse(bodyStr));\n if (next !== undefined) body = JSON.stringify(next);\n } catch {}\n return fetch(req ?? url, { ...init, body });\n }\n\n const cached = _cache.get(url);\n const known = mode === 'gzip' || mode === 'deflate';\n const encoding = !_hasCS\n ? null\n : known\n ? (mode as SupportedEncoding)\n : mode === 'proactive'\n ? cached === -1\n ? null\n : (cached ?? 'gzip')\n : typeof cached === 'string'\n ? cached\n : null;\n\n const shouldCompress = !!encoding && !!bodyStr && bodyStr.length >= MIN_BODY_SIZE;\n const opts: RequestInit = { ...init, priority: 'high' as RequestPriority };\n const headers = new Headers(opts.headers);\n if (shouldCompress) {\n opts.body = await new Response(\n new Blob([bodyStr!])\n .stream()\n .pipeThrough(new CompressionStream(encoding as CompressionFormat)),\n ).blob();\n headers.set('Content-Encoding', encoding);\n }\n opts.headers = headers;\n\n const response = await fetch(req ?? url, opts);\n\n // Cache discovery for passive/proactive (not known modes)\n if (!known && cached === undefined) {\n const header = response.headers.get('Accept-Encoding');\n const discovered =\n header\n ?.split(',')\n .map((e) => e.trim())\n .find((e): e is SupportedEncoding => _enc.includes(e as SupportedEncoding)) ?? -1;\n _cache.set(\n url,\n mode === 'proactive' && shouldCompress ? (response.ok ? encoding! : discovered) : discovered,\n );\n }\n\n return response;\n}\n"],
|
|
5
|
+
"mappings": "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",
|
|
6
|
+
"names": ["index_node_exports", "__export", "MIN_BODY_SIZE", "compressModule", "__toCommonJS", "import_node_zlib", "MIN_BODY_SIZE", "_hasCS", "_cache", "_enc", "compressModule", "input", "init", "mode", "req", "url", "bodyStr", "body", "next", "cached", "known", "encoding", "shouldCompress", "opts", "headers", "response", "discovered", "e", "make", "ctx", "handle", "chunk", "ctrl", "format", "zlib"]
|
|
7
7
|
}
|
package/_cjs/jit-compressor.cjs
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
var
|
|
1
|
+
var it=Object.defineProperty;var Lt=Object.getOwnPropertyDescriptor;var Nt=Object.getOwnPropertyNames;var Ft=Object.prototype.hasOwnProperty;var Ot=(r,e)=>{for(var u in e)it(r,u,{get:e[u],enumerable:!0})},Tt=(r,e,u,y)=>{if(e&&typeof e=="object"||typeof e=="function")for(let a of Nt(e))!Ft.call(r,a)&&a!==u&&it(r,a,{get:()=>e[a],enumerable:!(y=Lt(e,a))||y.enumerable});return r};var jt=r=>Tt(it({},"__esModule",{value:!0}),r);var $t={};Ot($t,{compress_call:()=>Zt});module.exports=jt($t);var mt=require("solady");var St=(1n<<128n)-1n,k=(1n<<256n)-1n;var et=r=>~r&k;var _t=(r,e)=>r&e&k,Bt=(r,e)=>(r|e)&k,It=(r,e)=>(r^e)&k,Mt=(r,e)=>r+e&k,ct=(r,e)=>r-e&k,ft=(r,e)=>e<<r&k,at=(r,e)=>e>>r&k,lt=(r,e)=>{if(r>=31n)return e&k;let u=Number((r+1n)*8n);return BigInt.asUintN(256,BigInt.asIntN(u,e))};var j=r=>(r.charCodeAt(0)===48&&(r.charCodeAt(1)|32)===120?r.slice(2):r).toLowerCase(),Vt=Array.from({length:256},(r,e)=>e.toString(16).padStart(2,"0")),Ct=(()=>{let r=new Int8Array(103).fill(-1);for(let e=0;e<10;e++)r[48+e]=e;for(let e=0;e<6;e++)r[97+e]=10+e;return r})(),vt=r=>{let e=j(r),u=e.length,y=new Uint8Array(u/2);for(let a=0,H=0;a<u;a+=2)y[H++]=Ct[e.charCodeAt(a)]<<4|Ct[e.charCodeAt(a+1)];return y},ut=r=>{let e="";for(let u=0;u<r.length;u++)e+=Vt[r[u]];return e},Et=new Uint8Array(32),Pt=[],Ut=(r,e)=>{let u=j(r),y=vt(u),a=new Uint8Array(e+y.length);a.set(y,e);let H=a.length,A=Math.ceil(H/32),L=A*32;if(H!==L){let i=new Uint8Array(L);i.set(a,0),a=i}let M=new Array(A),C=new Array(A),m=new Map,d=null,b=i=>{let f=i<<5;return a.subarray(f,f+32)};for(let i=0;i<A;i++){let f=i<<5,x=b(i),W=ut(x);C[i]=W;let V=m.get(W),rt=d===W;if(V)V[0]+=1,rt||(V[1]+=1),V[3]=i,V[5]=f;else{let _=[1,1,i,i,f,f,-1];m.set(W,_)}d=W;let Y=[];for(let _=0;_<32;){for(;_<32&&x[_]===0;)++_;if(_>=32)break;let ot=_;for(;_<32&&x[_]!==0;)++_;Y.push(ot<<8|_-1)}M[i]=Y}for(let i of m.values()){let x=(i[4]===0?0:32-Math.clz32(i[4])+7>>3)+3;i[6]=32>x?x:-1}let U=i=>i<0||i>=A?Et:b(i),N=i=>i<0||i>=A?Pt:M[i],F=(i,f)=>{if(f<=0)return new Uint8Array(0);let x=new Uint8Array(f);if(i<0||i>=H)return x;let W=Math.min(i+f,H);return x.set(a.subarray(i,W),0),x},S=i=>F(i,32);return{hex:u,buffer:a,roundedLength:L,padding:e,dataLength:y.length,wordCount:A,wordHexes:C,wordStats:m,getWord:U,getSegments:N,mload:S,slice:F}};var bt="0x00000000000000000000000000000000000000e0",R=r=>r.padStart(64,"0"),qt=R("0"),Xt=R("20"),Rt=R("e0"),Kt="345f355af13d5f5f3e3d5ff3",gt=function(r,e,u){let y=u?j(u):null,a=28,H=null,A=0,L=j(e).padStart(16,"0"),M=y?BigInt("0x"+y):96n,C=2n,m=[],d=[],b=[],U=0,N=[],F=!0,S=Ut(r,a),{wordCount:i}=S,f=224n,x=new Map,W=Rt,V=R(y??M.toString(16)),rt=R(L);for(let[t,o]of S.wordStats){let n=o[0];t===qt||t===Xt||t===W||t===V||t===rt||n>A&&(A=n,H=t)}H&&(C=BigInt("0x"+H));let Y=t=>t+31&-32,_=t=>{for(let o=b.length-1,n=0;n<16&&o>=0;--o,++n)if(b[o]===t)return n;return-1},ot=(t,o,n)=>t.set(o,(t.get(o)||0)+n),O=(t,o)=>{m.push(t),d.push(o??null)},l=(t,o=1)=>{b.push(t),o!==0&&ot(x,t,o)},T=()=>[b.pop(),b.pop()],dt=(t,o)=>{U=Y(t+o)},P=(t,o)=>{if(t===128&&l(b[b.length-1],F?0:1),t===80&&b.pop(),t===71&&l(C,0),t===48&&l(f,0),t===51&&l(M,0),t===54&&l(32n,0),t===89&&l(BigInt(U),0),t===11){let[n,s]=T();l(lt(n,s),1)}if(t===25&&l(et(b.pop()),0),t===24){let[n,s]=T();l(It(n,s),1)}if(t===22){let[n,s]=T();l(_t(n,s),1)}if(t===23){let[n,s]=T();l(Bt(n,s),1)}if(t===1){let[n,s]=T();l(Mt(n,s),1)}if(t===3){let[n,s]=T();l(ct(s,n),1)}if(t===27){let[n,s]=T();l(ft(n,s),1)}if(t===28){let[n,s]=T();l(at(n,s),1)}if(t>=96&&t<=127||t===95){let n=0n;for(let g of o||[])n=n<<8n|BigInt(g);if(n===C){l(n,0),O(71);return}if(n===f){l(n,0),O(48);return}if(n===M){l(n,0),O(51);return}if(n===32n){l(n,0),O(54);return}if(n===BigInt(U)&&n!=0n){l(n,0),O(89);return}let s=_(n);if(s!=-1&&t!=95){l(n,F?1:0),O(128+s);return}if(n===k){l(n,0),O(95),O(25);return}l(n,1),O(t,o||null);return}if(t===81&&l(N[Number(b.pop())>>>5]??0n,0),t===82){let[n,s]=T(),g=Number(n);N[g>>>5]=s&k,dt(g,32)}if(t===83){let[n,s]=T();dt(Number(n),1)}O(t,o||null)},G=t=>P(t),xt=t=>{if(t===0n)return 0;let o=0,n=t<0n?-t:t;for(;n>0n;)++o,n>>=8n;return o},q=t=>t===0n?1:1+xt(t),K=t=>{let o=typeof t=="bigint"?t:BigInt(t);if(o>0n&&o===BigInt(U))return P(89);if(o===32n)return P(54);if(o===0n)return P(95);let n=o,s=xt(n),g=new Uint8Array(s);for(let D=s-1;D>=0;--D)g[D]=Number(n&0xffn),n>>=8n;return P(95+s,g)},ht=t=>P(95+t.length,t),J=[],B=t=>{J.push({t:"num",v:t}),K(t)},yt=t=>{J.push({t:"bytes",b:t}),ht(t)},p=t=>{J.push({t:"op",o:t}),G(t)},kt=t=>{let o=0,n=!0;for(let s=0;s<t.length;s++){let g=t[s],D=g>>>8,w=g&255;o+=1+(w-D+1),31-w>0&&(o+=3),n||(o+=1),n=!1}return o},pt=(t,o)=>{let n=t.subarray(o[0]>>>8),s=0n;for(let c=0;c<n.length;c++)s=s<<8n|BigInt(n[c]);let g=n.length+1,D=kt(o),w=g,v=()=>yt(n),tt=et(s),nt=q(tt)+1;nt<w&&(w=nt,v=()=>{B(tt),p(25)});let Z=ct(0n,s),$=q(Z)+2;$<w&&(w=$,v=()=>{B(0),B(Z),p(3)});for(let c=1;c<n.length;c++){let h=(1n<<BigInt(c*8))-1n,I=s&h;if(lt(BigInt(c-1),I)===s&&(I&1n<<BigInt(c*8-1))!==0n){let E=q(I)+3;E<w&&(w=E,v=()=>{B(I),B(c-1),p(11)});break}}for(let c=8;c<=248;c+=8){let h=at(BigInt(c),s);if(h===0n)break;let I=et(h);if(ft(BigInt(c),I)===s){let E=q(I)+q(BigInt(c))+2;E<w&&(w=E,v=()=>{B(I),B(c),p(27),p(25)})}}return D<w&&(w=D,v=()=>{let c=!0;for(let h=0;h<o.length;h++){let I=o[h],X=I>>>8,E=I&255,At=31-E;yt(t.subarray(X,E+1)),At>0&&(B(At*8),p(27)),c||p(23),c=!1}}),{literal:n,literalVal:s,bestCost:w,bestEmit:v,literalCost:g,shlCost:D}},z=0;for(;z<i;){let t=z*32,o=S.getWord(z),n=S.getSegments(z);if(!n.length){++z;continue}let s=S.wordHexes[z],g=z+1;for(;g<i&&!(!S.getSegments(g).length||S.wordHexes[g]!==s);)++g;let D=g-z;if(D>=2){let c=g-1,{bestEmit:h}=pt(o,n);h(),p(128),B(t),p(82);for(let X=1;X<D;X++)p(128),p(89),p(82);let I=S.wordStats.get(s);I&&I[3]<=c&&p(80),z=g;continue}let{literalCost:w,shlCost:v,bestCost:tt,bestEmit:nt}=pt(o,n);if(w>8){let c=S.wordStats.get(s);if(c&&c[6]!==-1&&z>c[2]){let h=c[4]===0?0:32-Math.clz32(c[4])+7>>3;if(w>c[6]+h){B(c[4]),p(81),B(t),p(82),++z;continue}}}let Z=!0;for(let c=0;c<n.length;c++){let h=n[c];if(h>>>8!==(h&255)){Z=!1;break}}let $=n.length*3;if(Z&&$<tt&&$<=v){for(let c=0;c<n.length;c++){let h=n[c]>>>8;B(o[h]),B(t+h),p(83)}++z;continue}nt(),B(t),p(82),++z}m=[],d=[],b=[],U=0,N=[],F=!1;let Q=[];for(let[t,o]of x)if(o>1&&t!==0n&&t!==32n&&t!==C&&t!==f&&t!==M&&t<=St){let n=q(t),s=o*(n-1)-n;s>0&&Q.push({val:t,uses:o,net:s,p:n})}Q.sort((t,o)=>o.net-t.net||o.uses-t.uses||t.p-o.p);for(let t=0;t<15&&t<Q.length;++t)K(Q[t].val);for(let t of J)t.t==="num"?K(t.v):t.t==="bytes"?ht(t.b):t.t==="op"&&G(t.o);G(95),G(95),K(S.dataLength),K(a);let wt=m.length;for(let t=0;t<m.length;++t)m[t]>=96&&m[t]<=127&&d[t]&&(wt+=d[t].length);let st=new Uint8Array(wt);for(let t=0,o=0;t<m.length;++t)st[o++]=m[t],m[t]>=96&&m[t]<=127&&d[t]&&(st.set(d[t],o),o+=d[t].length);let Ht="0x"+ut(st)+Kt,Dt="0x"+R(L);return{bytecode:Ht,calldata:Dt,to:bt,from:M.toString(16).padStart(40,"0"),balance:C.toString(16)}};var Wt=r=>`0x365f73${j(r)}815b838110602f575f80848134865af1503d5f803e3d5ff35b803590815f1a8060051c908115609857600190600783149285831a6007018118840218600201948383011a90601f1660081b0101808603906020811860208211021890815f5b80830151818a015201858110609257505050600201019201916018565b82906075565b6001929350829150019101925f5b82811060b3575001916018565b85851060c1575b60010160a6565b936001818192355f1a878501530194905060ba56`;var zt=r=>`0x5f5f5b368110602d575f8083813473${j(r)}5af1503d5f803e3d5ff35b600180820192909160031981019035185f1a8015604c57815301906002565b505f19815282820192607f9060031981019035185f1a818111156072575b160101906002565b838101368437606a56`;var Zt=function(r,e){let{method:u,params:y}=r;if(u&&u!=="eth_call")return r;let a=y?.[0]||r,H=y?.[1],A=y?.[2];if(!a?.to||!a?.data||(()=>{if(A)for(let f in A)return!0;for(let f in a)if(f!=="to"&&f!=="data"&&f!=="from")return!0;return!1})())return r;let L=a.data.length;if(L<1150)return r;let M=a.data,C=a.to,m=a.from,d,b,U,N,F;if(e==="jit"||!e&&(L<3e3||L>=8e3)){let f=gt(M,C,m);d=f.bytecode,b=f.calldata,U=f.to,N=f.from,F=f.balance}else{let f=e?null:gt(M,C,m),x=e==="flz"||!e?mt.LibZip.flzCompress(M):null,W=e==="cd"||!e&&x?mt.LibZip.cdCompress(M):null;e==="flz"||!e&&x&&(!W||x.length<W.length)?(b=x,d=Wt(C)):(b=W,d=zt(C)),U=bt,N=m?j(m).padStart(16,"0"):void 0,F="0",!e&&f&&f.bytecode.length+f.calldata.length<d.length+b.length&&(d=f.bytecode,b=f.calldata,U=f.to,N=f.from,F=f.balance)}if(d.length+b.length>=L)return r;let S={code:d,balance:"0x"+F},i={to:U,data:b};return N&&(i.from="0x"+N),{...r,params:[i,H,{...A,[U]:S}]}};
|
|
2
2
|
//! @__PURE__
|
|
3
3
|
//# sourceMappingURL=jit-compressor.cjs.map
|