{"name":"abortable-iterator","version":"3.0.2","description":"Make any iterator or iterable abortable via an AbortSignal","main":"index.js","scripts":{"test":"npm run lint && nyc --reporter=lcov --reporter=text ava --verbose","lint":"standard"},"keywords":["async","iterator","abort","abortable","cancel","stop","AbortController","AbortSignal","signal"],"author":{"name":"Alan Shaw"},"license":"MIT","devDependencies":{"abort-controller":"^3.0.0","ava":"^3.15.0","nyc":"^15.1.0","standard":"^16.0.4"},"dependencies":{"get-iterator":"^1.0.2"},"repository":{"type":"git","url":"git+https://github.com/alanshaw/abortable-iterator.git"},"bugs":{"url":"https://github.com/alanshaw/abortable-iterator/issues"},"homepage":"https://github.com/alanshaw/abortable-iterator#readme","types":"./index.d.ts","gitHead":"e8727f8c72ce48716f197bff28122cfd7f8f2fc0","_id":"abortable-iterator@3.0.2","_nodeVersion":"16.13.0","_npmVersion":"8.1.0","dist":{"integrity":"sha512-qVP8HFfTpUQI2F+f1tpTriKDIZ4XrmwCrBCrQeRKO7DKWF3kgoT6NXiNDv2krrGcHxPwmI63eGQiec81sEaWIw==","shasum":"6f5af0f36e86db4607fc95251bffe2dee261d6a2","tarball":"http://registry-npm.ywwl.com/abortable-iterator/download/abortable-iterator-3.0.2.tgz","fileCount":9,"unpackedSize":17942,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJh4TKxCRA9TVsSAnZWagAABHEP/2jwmcPQRihOAnLOBiQe\nbjmQQnnX9lv78a7X2OsiDsOK5wOmxAQBRLfxw9SaOCk8RVHyRiFTkbpQNWQC\nR5OwZ3Uc+EvvGr2GLIcMHDPKWbBy4I2CtwwWJvs2q802+Tv570FnG4qqRJ17\n7p4s5xXTm3MaZxMCOF7Ruz9CE8n9AQSecfJeQdkOBBuYpUvG1vv6lRfAiT2+\noRAoN4HK8x88tINc3Cu/xqcsWsVFNgQC+eBKO0uBKZXXfidK2AqAo4y2EbJs\nGuJhrEf3PSZjz1PMnoRi8LMlRViDqaLQ8/5YvjGHHo6Py9y6w4BkGVjek4Zj\nikERuZjrqQ+Zh/NDz7FuQS5UllHxEvr+L+c4t27QU94VXgCMe3aJgFRCNi0E\nhNIbI3XerkvrnZnbqMJScoNW0ExNY731q4349vgyE5/zQpB80xQa/Qz5q4gu\n/tjCHnW16qr0hpfXLerudC4vScX6JfVaZefSJ3bPZsfBfZuidCeA/yfzylHm\njJiTxFIbXPPnSmGrFWiurpTKTwYO1wn65pA33n/CrOhzqHZL2ibohkSnnFkg\nGWMTG/tac6Q0DNwn4rD2BraW13GUmjRfiA1J69vRyzMEXmjqt4+duT4ZS+v9\nneDb2O+N0fHocNKt4uiFnMTgo5d5vlR38Tlz0u+WxxHjMSsuq4ub33SI7gPl\n2oXI\r\n=Tozc\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIEVhlqxcU+L+H1d3HXLH8oAI6NRpQ5q903gkY+UnoGR3AiEA9bNFWeetnJISp1kHnu/ODYdqC0QMV3d3OnWOAFp7m20="}],"size":4875,"noattachment":false,"key":"/abortable-iterator/-/abortable-iterator-3.0.2.tgz"},"_npmUser":{"name":"achingbrain","email":"alex@achingbrain.net"},"directories":{},"maintainers":[{"name":"achingbrain","email":"alex@achingbrain.net"},{"name":"alanshaw","email":"alan138@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/abortable-iterator_3.0.2_1642148529245_0.22214070058425772"},"_hasShrinkwrap":false,"readme":"# abortable-iterator <!-- omit in toc -->\n\n[![codecov](https://img.shields.io/codecov/c/github/alanshaw/abortable-iterator.svg?style=flat-square)](https://codecov.io/gh/alanshaw/abortable-iterator)\n[![CI](https://img.shields.io/github/actions/workflow/status/alanshaw/abortable-iterator/js-test-and-release.yml?branch=master\\&style=flat-square)](https://github.com/alanshaw/abortable-iterator/actions/workflows/js-test-and-release.yml?query=branch%3Amaster)\n\n> Make any iterator or iterable abortable via an AbortSignal\n\n## Table of contents <!-- omit in toc -->\n\n- [Install](#install)\n  - [Browser `<script>` tag](#browser-script-tag)\n- [Usage](#usage)\n- [API](#api)\n  - [`abortableSource(source, signal, [options])`](#abortablesourcesource-signal-options)\n    - [Parameters](#parameters)\n    - [Returns](#returns)\n  - [`abortableSink(sink, signal, [options])`](#abortablesinksink-signal-options)\n  - [`abortableTransform(transform, signal, [options])`](#abortabletransformtransform-signal-options)\n  - [`abortableDuplex(duplex, signal, [options])`](#abortableduplexduplex-signal-options)\n- [Related](#related)\n- [Contribute](#contribute)\n- [API Docs](#api-docs)\n- [License](#license)\n- [Contribution](#contribution)\n\n## Install\n\n```console\n$ npm i abortable-iterator\n```\n\n### Browser `<script>` tag\n\nLoading this module through a script tag will make it's exports available as `AbortableIterator` in the global namespace.\n\n```html\n<script src=\"https://unpkg.com/abortable-iterator/dist/index.min.js\"></script>\n```\n\nThe [`AbortController`](https://developer.mozilla.org/en-US/docs/Web/API/AbortController) is used in the fetch API to abort in flight requests from, for example, a timeout or user action. The same concept is used here to halt iteration of an async iterator.\n\n## Usage\n\n```js\nimport { abortableSource } from 'abortable-iterator'\n\n// An example function that creates an async iterator that yields an increasing\n// number every x milliseconds and NEVER ENDS!\nconst asyncCounter = async function * (start, delay) {\n  let i = start\n  while (true) {\n    yield new Promise(resolve => setTimeout(() => resolve(i++), delay))\n  }\n}\n\n// Create a counter that'll yield numbers from 0 upwards every second\nconst everySecond = asyncCounter(0, 1000)\n\n// Make everySecond abortable!\nconst controller = new AbortController()\nconst abortableEverySecond = abortableSource(everySecond, controller.signal)\n\n// Abort after 5 seconds\nsetTimeout(() => controller.abort(), 5000)\n\ntry {\n  // Start the iteration, which will throw after 5 seconds when it is aborted\n  for await (const n of abortableEverySecond) {\n    console.log(n)\n  }\n} catch (err) {\n  if (err.code === 'ERR_ABORTED') {\n    // Expected - all ok :D\n  } else {\n    throw err\n  }\n}\n```\n\n## API\n\n```js\nimport {\n  abortableSource,\n  abortableSink,\n  abortableTransform,\n  abortableDuplex\n} from 'abortable-iterator'\n```\n\n- [`abortableSource(source, signal, [options])`](#abortablesource-signal-options)\n- [`abortableSink(sink, signal, [options])`](#abortablesinksink-signal-options)\n- [`abortableTransform(transform, signal, [options])`](#abortabletransformtransform-signal-options)\n- [`abortableDuplex(duplex, signal, [options])`](#abortableduplexduplex-signal-options)\n\n### `abortableSource(source, signal, [options])`\n\n**(alias for `abortable.source(source, signal, [options])`)**\n\nMake any iterator or iterable abortable via an `AbortSignal`.\n\n#### Parameters\n\n| Name                  | Type                                                                                                                                                                                                                                                 | Description                                                                                                                                                              |\n| --------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |\n| source                | [`Iterable`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterable_protocol)\\|[`Iterator`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterator_protocol) | The iterator or iterable object to make abortable                                                                                                                        |\n| signal                | [`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal)                                                                                                                                                                        | Signal obtained from `AbortController.signal` which is used to abort the iterator.                                                                                       |\n| options               | `Object`                                                                                                                                                                                                                                             | (optional) options                                                                                                                                                       |\n| options.onAbort       | `Function`                                                                                                                                                                                                                                           | An (async) function called when the iterator is being aborted, before the abort error is thrown. Default `null`                                                          |\n| options.abortMessage  | `String`                                                                                                                                                                                                                                             | The message that the error will have if the iterator is aborted. Default \"The operation was aborted\"                                                                     |\n| options.abortCode     | `String`\\|`Number`                                                                                                                                                                                                                                   | The value assigned to the `code` property of the error that is thrown if the iterator is aborted. Default \"ABORT\\_ERR\"                                                   |\n| options.returnOnAbort | `Boolean`                                                                                                                                                                                                                                            | Instead of throwing the abort error, just return from iterating over the source stream.                                                                                  |\n| options.onReturnError | `Function`                                                                                                                                                                                                                                           | When a generator is aborted, we call `.return` on it - if this function errors the error value will be passed to the `.onReturnError` callback if passed. Default `null` |\n\n#### Returns\n\n| Type                                                                                                                      | Description                                                                                                     |\n| ------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------- |\n| [`Iterable`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterator_protocol) | An iterator that wraps the passed `source` parameter that makes it abortable via the passed `signal` parameter. |\n\nThe returned iterator will `throw` an `AbortError` when it is aborted that has a `type` with the value `aborted` and `code` property with the value `ABORT_ERR` by default.\n\n### `abortableSink(sink, signal, [options])`\n\nThe same as [`abortable.source`](#abortablesource-signal-options) except this makes the passed [`sink`](https://gist.github.com/alanshaw/591dc7dd54e4f99338a347ef568d6ee9#sink-it) abortable. Returns a new sink that wraps the passed `sink` and makes it abortable via the passed `signal` parameter.\n\n### `abortableTransform(transform, signal, [options])`\n\nThe same as [`abortable.source`](#abortablesource-signal-options) except this makes the passed [`transform`](https://gist.github.com/alanshaw/591dc7dd54e4f99338a347ef568d6ee9#transform-it) abortable. Returns a new transform that wraps the passed `transform` and makes it abortable via the passed `signal` parameter.\n\n### `abortableDuplex(duplex, signal, [options])`\n\nThe same as [`abortable.source`](#abortablesource-signal-options) except this makes the passed [`duplex`](https://gist.github.com/alanshaw/591dc7dd54e4f99338a347ef568d6ee9#duplex-it) abortable. Returns a new duplex that wraps the passed `duplex` and makes it abortable via the passed `signal` parameter.\n\nNote that this will abort *both* sides of the duplex. Use `duplex.sink = abortable.sink(duplex.sink)` or `duplex.source = abortable.source(duplex.source)` to abort just the sink or the source.\n\n## Related\n\n- [`it-pipe`](https://www.npmjs.com/package/it-pipe) Utility to \"pipe\" async iterables together\n\n## Contribute\n\nFeel free to dive in! [Open an issue](https://github.com/alanshaw/abortable-iterator/issues/new) or submit PRs.\n\n## API Docs\n\n- <https://alanshaw.github.io/abortable-iterator>\n\n## License\n\nLicensed under either of\n\n- Apache 2.0, ([LICENSE-APACHE](LICENSE-APACHE) / <http://www.apache.org/licenses/LICENSE-2.0>)\n- MIT ([LICENSE-MIT](LICENSE-MIT) / <http://opensource.org/licenses/MIT>)\n\n## Contribution\n\nUnless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.\n","publish_time":1642148529915,"_cnpm_publish_time":1642148529915,"dist-tags":{"latest":"5.1.0"}}