{"name":"micro-ftch","version":"0.4.2","keywords":["fetch","network","killswitch","concurrent","parallel","jsonrpc","replay","ftch"],"author":{"url":"https://paulmillr.com","name":"Paul Miller"},"license":"MIT","_id":"micro-ftch@0.4.2","maintainers":[{"name":"paulmillr","email":"paul@paulmillr.com"}],"homepage":"https://github.com/paulmillr/micro-ftch","bugs":{"url":"https://github.com/paulmillr/micro-ftch/issues"},"dist":{"shasum":"79b993a9fe7fb9a5c456fa24de833fa80f8ef2dd","tarball":"http://registry-npm.ywwl.com/micro-ftch/download/micro-ftch-0.4.2.tgz","fileCount":13,"integrity":"sha512-7gXEYECF4DAoDUOViftg/rhUg/1R5n7tGHv/9Lc7xp9e32+FnLNcA6hMV+c2/gEnP+35CQVe09RWf6l4j7K6vA==","signatures":[{"sig":"MEQCIAd+sHqhNkV3jKUcEuQrOxgzRFW03V9REWnpt3/gb78vAiBLdMtomX7Z/m9OWcC8WzUnyAjKm+c9Xqglxbn5bO/rjA==","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"attestations":{"url":"https://registry.npmjs.org/-/npm/v1/attestations/micro-ftch@0.4.2","provenance":{"predicateType":"https://slsa.dev/provenance/v1"}},"unpackedSize":91606,"size":15596,"noattachment":false,"key":"/micro-ftch/-/micro-ftch-0.4.2.tgz"},"main":"./lib/index.js","types":"./lib/index.d.ts","module":"./lib/esm/index.js","exports":{".":{"types":"./lib/index.d.ts","import":"./lib/esm/index.js","default":"./lib/index.js"}},"funding":"https://paulmillr.com/funding/","gitHead":"0d77da01a7f2c10ad8adda240936cb5ab061808c","scripts":{"lint":"prettier --check index.ts","test":"node test/index.js","build":"tsc && tsc -p tsconfig.cjs.json","format":"prettier --write index.ts","test:bun":"bun test/index.js","test:deno":"deno --allow-env --allow-net test/index.js","build:release":"npx jsbt esbuild test/build"},"_npmUser":{"name":"paulmillr","email":"paul@paulmillr.com"},"repository":{"url":"git+https://github.com/paulmillr/micro-ftch.git","type":"git"},"_npmVersion":"10.9.2","description":"Wrappers for built-in fetch() enabling killswitch, logging, concurrency limit and other features","directories":{},"sideEffects":false,"_nodeVersion":"22.13.0","_hasShrinkwrap":false,"devDependencies":{"prettier":"3.3.2","typescript":"5.5.2","micro-bmark":"0.4.0","micro-should":"0.5.1","@paulmillr/jsbt":"0.3.1"},"_npmOperationalInternal":{"tmp":"tmp/micro-ftch_0.4.2_1737610854296_0.020131990920092324","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":1737610854480,"_cnpm_publish_time":1737610854480,"dist-tags":{"latest":"1.2.1"}}