{"_id":"micro-ftch@1.2.1","bugs":{"url":"https://github.com/paulmillr/micro-ftch/issues"},"dist":{"shasum":"984486ef2b28bd3a89e6dfd21d1bd8d4d676ce4c","tarball":"http://registry-npm.ywwl.com/micro-ftch/download/micro-ftch-1.2.1.tgz","integrity":"sha512-ZNFTR0At/Gyr4i3Qtwj6rva2j2rGcapmFg97nNLzbXG6s6iNiwQHNYmAoJ4RVhC5dErJfxZasbULaUYepoFeIQ==","fileCount":6,"unpackedSize":122256,"attestations":{"url":"https://registry.npmjs.org/-/npm/v1/attestations/micro-ftch@1.2.1","provenance":{"predicateType":"https://slsa.dev/provenance/v1"}},"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEUCIA+RV0Dj3AHGYxMjJ+W3umz1XvXn+2UzTuRSy/QoRgfLAiEA/Iem/Cwc7mZcd4yuF7Xb4bHyQcVH+OI2aa+ZzGX8U1Y="}],"size":32357,"noattachment":false,"key":"/micro-ftch/-/micro-ftch-1.2.1.tgz"},"main":"index.js","name":"micro-ftch","type":"module","types":"index.d.ts","author":{"url":"https://paulmillr.com","name":"Paul Miller"},"module":"index.js","gitHead":"6b50b324c846458f3fa40456fbb97379203b6f10","license":"MIT","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":"bismar -bsm"},"version":"1.2.1","_npmUser":{"name":"GitHub Actions","email":"npm-oidc-no-reply@github.com","trustedPublisher":{"id":"github","oidcConfigId":"oidc:f1e9fb42-6d8a-4798-9603-8350bd30f3af"},"approver":{"name":"paulmillr","email":"paul@paulmillr.com"}},"homepage":"https://github.com/paulmillr/micro-ftch#readme","keywords":["fetch","network","killswitch","concurrent","parallel","jsonrpc","micro","replay","ftch"],"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":{},"maintainers":[{"name":"paulmillr","email":"paul@paulmillr.com"}],"sideEffects":false,"_nodeVersion":"24.19.0","devDependencies":{"bismar":"0.1.8","prettier":"3.9.6","typescript":"6.0.3","@types/node":"26.2.0","@paulmillr/jsbt":"0.7.1"},"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/micro-ftch_1.2.1_1788012783252_0.5068349463724493"},"_hasShrinkwrap":false,"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":1788012783328,"_cnpm_publish_time":1788012783328,"dist-tags":{"latest":"1.2.1"}}