{"name":"micro-ftch","version":"1.2.0","keywords":["fetch","network","killswitch","concurrent","parallel","jsonrpc","micro","replay","ftch"],"author":{"url":"https://paulmillr.com","name":"Paul Miller"},"license":"MIT","_id":"micro-ftch@1.2.0","maintainers":[{"name":"paulmillr","email":"paul@paulmillr.com"}],"homepage":"https://github.com/paulmillr/micro-ftch#readme","bugs":{"url":"https://github.com/paulmillr/micro-ftch/issues"},"dist":{"shasum":"13dcd1e164bab277cd60f34d294d084035738b6a","tarball":"http://registry-npm.ywwl.com/micro-ftch/download/micro-ftch-1.2.0.tgz","fileCount":6,"integrity":"sha512-XvcEm9NJIZSXjz0As8/J5rBNYc4ZHKPaeBBttE0Gq0QEOdzPkogYUcXCh1SWLVmo/0BIAVZt3RdUyiRAYqns8g==","signatures":[{"sig":"MEYCIQDqbJUfPqRDHCZdl2xlrkF/tuE4giIWRgfLAX+zU4S4ngIhALw8Qfw+tXxmWXglObCOL8QmgAfvW3v3HzVggs3RD9iz","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"attestations":{"url":"https://registry.npmjs.org/-/npm/v1/attestations/micro-ftch@1.2.0","provenance":{"predicateType":"https://slsa.dev/provenance/v1"}},"unpackedSize":116283,"size":31185,"noattachment":false,"key":"/micro-ftch/-/micro-ftch-1.2.0.tgz"},"main":"index.js","type":"module","types":"index.d.ts","module":"index.js","gitHead":"63dc8e181201a98bd5515df04791d5d861c305f2","scripts":{"test":"node test/index.ts","build":"tsc","check":"jsbt-check","format":"prettier --write index.ts","test:deno":"deno --allow-env --allow-net test/index.ts","benchmark:size":"npx bismar@0.1 -bs"},"_npmUser":{"name":"GitHub Actions","email":"npm-oidc-no-reply@github.com","approver":{"name":"paulmillr","email":"paul@paulmillr.com"},"trustedPublisher":{"id":"github","oidcConfigId":"oidc:f1e9fb42-6d8a-4798-9603-8350bd30f3af"}},"repository":{"url":"git+https://github.com/paulmillr/micro-ftch.git","type":"git"},"_npmVersion":"12.0.2","description":"Wrappers for built-in fetch() enabling killswitch, logging, concurrency limit and other features","directories":{},"sideEffects":false,"_nodeVersion":"24.19.0","_hasShrinkwrap":false,"devDependencies":{"prettier":"3.6.2","typescript":"6.0.2","@types/node":"25.3.0","@paulmillr/jsbt":"0.6.5"},"_npmOperationalInternal":{"tmp":"tmp/micro-ftch_1.2.0_1786590122644_0.0007903839201184315","host":"s3://npm-registry-packages-npm-production"},"readme":"# micro-ftch\n\nPrivacy-first wrappers for built-in `fetch()`, enabling allowlists, killswitch, logging, concurrency control, and other features.\n\n## Usage\n\n> `npm install micro-ftch`\n\n> `jsr add jsr:@paulmillr/micro-ftch`\n\nA standalone file\n[micro-ftch.js](https://github.com/paulmillr/micro-ftch/releases) is also available.\n\nThere are four composable wrappers over `fetch()`:\n\n- [ftch](#ftch) — validation, allowlists, logging, timeouts, rate & size limits, basic auth\n- [jsonrpc](#jsonrpc) — batched JSON-RPC requests\n- [replayable](#replayable) — record & replay requests in tests, without network\n- [retry](#retry) — retries with exponential backoff and jitter\n\n## ftch\n\nAll options are optional:\n\n```ts\nimport { ftch } from 'micro-ftch';\n\nlet enabled = true;\nconst ctrl = new AbortController();\nconst f = ftch(fetch, {\n  isValidRequest: () => enabled, // killswitch: false makes requests throw\n  killswitchSignal: ctrl.signal, // one-shot hard killswitch, aborts in-flight requests\n  allowedHosts: ['rpc.example.com', 'http://localhost:8545'], // origin allowlist\n  log: (url, opts) => console.log(url, opts),\n  timeout: 5000, // ms; can also be set per-request: f(url, { timeout: 1000 })\n  concurrencyLimit: 10, // max in-flight requests, extras are queued\n  rps: 10, // max request starts per second\n  maxBodySize: 10 * 1024 * 1024, // bytes; default 1 GiB, bodies are buffered\n});\n\nawait f('https://rpc.example.com/');\nawait f('https://user:pwd@httpbin.org/basic-auth/user/pwd'); // basic auth auto-parsing\n```\n\n- `isValidRequest` can be toggled at any time and may return a `Promise<boolean>`;\n  it is checked before a request, after response headers, and after the body is buffered.\n- `killswitchSignal`, once aborted, cancels active requests and blocks all future ones;\n  construct a new wrapper to re-enable network access.\n- `referrerPolicy: 'no-referrer'` is set by default for privacy.\n\n## jsonrpc\n\nBatches multiple calls into one JSON-RPC HTTP request. Can massively speed things up\nwhen servers are single-threaded or have small per-user limits.\n\n```ts\nimport { jsonrpc } from 'micro-ftch';\n\nconst rpc = jsonrpc(fetch, 'http://rpc_node/', { batchSize: 20 });\nconst res = await rpc.call('method', 'arg0', 'arg1');\nconst res2 = await rpc.callNamed('method', { arg0: '0', arg1: '1' }); // named arguments\n```\n\n`jsonrpc` automatically restricts requests to the exact origin of `rpcUrl`, whether its\ntransport is native `fetch` or an `ftch` wrapper. Cross-origin redirects are blocked before the\nnew origin is contacted. Add trusted redirect origins explicitly with\n`{ allowedHosts: ['rpc-failover.example'] }`; the original RPC origin remains allowed.\nWhen the transport was not created by `ftch`, requests also default to a 240-second timeout and\n10 concurrent HTTP requests. Set `timeout` or `concurrencyLimit` here to override either value;\nan `ftch` transport keeps its existing operational limits unless these options are provided.\n\n## replayable\n\nRecords requests and replays them in tests, without calling network code.\n\n```ts\nimport { replayable } from 'micro-ftch';\n\nconst capture = replayable(fetch);\nawait capture('http://url/1'); // real network\nconst logs = capture.export();\n\nconst replay = replayable(fetch, JSON.parse(logs), { offline: true });\nawait replay('http://url/1'); // cached\nawait replay('http://url/2'); // throws: not captured, offline mode forbids network\n```\n\nCaptured entries include response status and headers, so error handling can be\nreplay-tested too. Logs store requests and responses verbatim, including any\nsecrets they contain — treat exported fixtures as sensitive.\n\n## retry\n\nRetries failed requests with exponential backoff, full jitter and `Retry-After` support.\nBy default only safe methods (GET/HEAD/OPTIONS) are retried — retrying a POST can duplicate\na non-idempotent action — and only on network errors or status 408/429/5xx.\nPass a custom `shouldRetry` to override the policy.\n\n```ts\nimport { ftch, jsonrpc, retry } from 'micro-ftch';\n\nconst net = retry(ftch(fetch), { attempts: 3, baseDelay: 100, maxDelay: 5000 });\nawait net('https://example.com'); // up to 3 attempts\n\n// JSON-RPC uses POST: opt in explicitly\nconst rpcNet = retry(ftch(fetch), {\n  shouldRetry: (ctx) => ctx.error !== undefined || ctx.status === 429 || ctx.status >= 500,\n});\nconst rpc = jsonrpc(rpcNet, 'http://rpc_node/', { batchSize: 20 });\n```\n\n## Security\n\n- `allowedHosts` entries are origins: `example.com` means exactly `https://example.com:443`,\n  a port (`example.com:8443`) means an alternate HTTPS origin, HTTP must be explicit\n  (`http://localhost:8545`). Paths, queries and credentials are not valid entries.\n- Redirects are followed by the wrapper one hop at a time, so the allowlist and\n  `isValidRequest` are checked before every hop's request is sent. Hops are capped at 20;\n  credentials, cookies and API-key headers (extend via `sensitiveHeaders`) are stripped\n  on cross-origin hops.\n- HTTPS→HTTP downgrades are rejected, even without an allowlist. Opt in with\n  `allowInsecureRedirects: true` if a trusted endpoint requires one.\n- `allowedHosts` is not a complete SSRF sandbox: it validates URL origins, but DNS may\n  still resolve a permitted hostname to an internal address. Enforce trusted DNS and\n  network-level egress rules when requests cross a security boundary.\n\n## License\n\nMIT (c) Paul Miller [(https://paulmillr.com)](https://paulmillr.com), see LICENSE file.\n","publish_time":1786590122779,"_cnpm_publish_time":1786590122779,"dist-tags":{"latest":"1.2.1"}}