@jcoreio/abortable 1.0.0

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/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 JCore Systems
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,25 @@
1
+ # @jcoreio/abortable
2
+
3
+ memory-leak-proof function to wrap a promise to reject when a signal is aborted
4
+
5
+ [![CircleCI](https://circleci.com/gh/jcoreio/abortable.svg?style=svg)](https://circleci.com/gh/jcoreio/abortable)
6
+ [![Coverage Status](https://codecov.io/gh/jcoreio/abortable/branch/master/graph/badge.svg)](https://codecov.io/gh/jcoreio/abortable)
7
+ [![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)
8
+ [![npm version](https://badge.fury.io/js/%40jcoreio%2Fabortable.svg)](https://badge.fury.io/js/%40jcoreio%2Fabortable)
9
+
10
+ ## `abortable(promise, signal)`
11
+
12
+ Creates a promise that fulfills when the given `promise` fulfills, or rejects when the given `signal` is aborted,
13
+ whichever comes first. If the signal is aborted, rejects with a `DOMException` with `name: 'AbortError'`.
14
+
15
+ Once the returned promise resolves or rejects, references to all promise and signal handlers will have been removed,
16
+ so that it doesn't unexpectedly retain any memory.
17
+
18
+ ```ts
19
+ import { abortable } from '@jcoreio/abortable'
20
+
21
+ const abortController = new AbortContorller()
22
+ const { signal } = abortController
23
+
24
+ const promise = abortable(new Promise((r) => setTimeout(r, 10000)), signal)
25
+ ```
package/index.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ export declare const newAbortError: () => DOMException;
2
+ export declare function abortable<T>(promise: Promise<T>, signal: AbortSignal | undefined): Promise<T>;
3
+ //# sourceMappingURL=index.d.ts.map
package/index.d.ts.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["src/index.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,aAAa,oBACoC,CAAA;AAI9D,wBAAgB,SAAS,CAAC,CAAC,EACzB,OAAO,EAAE,OAAO,CAAC,CAAC,CAAC,EACnB,MAAM,EAAE,WAAW,GAAG,SAAS,GAC9B,OAAO,CAAC,CAAC,CAAC,CA4BZ"}
package/index.js ADDED
@@ -0,0 +1,47 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.abortable = abortable;
7
+ exports.newAbortError = void 0;
8
+ var newAbortError = exports.newAbortError = function newAbortError() {
9
+ return new DOMException('This operation was aborted', 'AbortError');
10
+ };
11
+ var noop = function noop() {};
12
+ function abortable(promise, signal) {
13
+ if (!signal) return promise;
14
+ return new Promise(function (resolve, reject) {
15
+ if (signal.aborted) {
16
+ reject(newAbortError());
17
+ return;
18
+ }
19
+ var cleanup = function cleanup() {
20
+ var callbacks = {
21
+ resolve: resolve,
22
+ reject: reject
23
+ };
24
+ // Prevent memory leaks. If the input promise never resolves, then the handlers
25
+ // below would retain this enclosing Promise's resolve and reject callbacks,
26
+ // which would retain the enclosing Promise and anything waiting on it.
27
+ // By replacing references to these callbacks, we enable the enclosing Promise to
28
+ // be garbage collected
29
+ resolve = noop;
30
+ reject = noop;
31
+ // Memory could also leak if the signal never aborts, unless we remove the abort
32
+ // handler
33
+ signal.removeEventListener('abort', onAbort);
34
+ return callbacks;
35
+ };
36
+ var onAbort = function onAbort() {
37
+ return cleanup().reject(newAbortError());
38
+ };
39
+ signal.addEventListener('abort', onAbort);
40
+ promise.then(function (value) {
41
+ return cleanup().resolve(value);
42
+ }, function (error) {
43
+ return cleanup().reject(error);
44
+ });
45
+ });
46
+ }
47
+ //# sourceMappingURL=index.js.map
package/index.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","names":["newAbortError","exports","DOMException","noop","abortable","promise","signal","Promise","resolve","reject","aborted","cleanup","callbacks","removeEventListener","onAbort","addEventListener","then","value","error"],"sources":["src/index.ts"],"sourcesContent":["export const newAbortError = () =>\n new DOMException('This operation was aborted', 'AbortError')\n\nconst noop = () => {}\n\nexport function abortable<T>(\n promise: Promise<T>,\n signal: AbortSignal | undefined\n): Promise<T> {\n if (!signal) return promise\n return new Promise<T>((resolve, reject) => {\n if (signal.aborted) {\n reject(newAbortError())\n return\n }\n const cleanup = () => {\n const callbacks = { resolve, reject }\n // Prevent memory leaks. If the input promise never resolves, then the handlers\n // below would retain this enclosing Promise's resolve and reject callbacks,\n // which would retain the enclosing Promise and anything waiting on it.\n // By replacing references to these callbacks, we enable the enclosing Promise to\n // be garbage collected\n resolve = noop\n reject = noop\n // Memory could also leak if the signal never aborts, unless we remove the abort\n // handler\n signal.removeEventListener('abort', onAbort)\n return callbacks\n }\n const onAbort = () => cleanup().reject(newAbortError())\n signal.addEventListener('abort', onAbort)\n promise.then(\n (value) => cleanup().res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reList":[]}
package/index.mjs ADDED
@@ -0,0 +1,32 @@
1
+ export const newAbortError = () => new DOMException('This operation was aborted', 'AbortError');
2
+ const noop = () => {};
3
+ export function abortable(promise, signal) {
4
+ if (!signal) return promise;
5
+ return new Promise((resolve, reject) => {
6
+ if (signal.aborted) {
7
+ reject(newAbortError());
8
+ return;
9
+ }
10
+ const cleanup = () => {
11
+ const callbacks = {
12
+ resolve,
13
+ reject
14
+ };
15
+ // Prevent memory leaks. If the input promise never resolves, then the handlers
16
+ // below would retain this enclosing Promise's resolve and reject callbacks,
17
+ // which would retain the enclosing Promise and anything waiting on it.
18
+ // By replacing references to these callbacks, we enable the enclosing Promise to
19
+ // be garbage collected
20
+ resolve = noop;
21
+ reject = noop;
22
+ // Memory could also leak if the signal never aborts, unless we remove the abort
23
+ // handler
24
+ signal.removeEventListener('abort', onAbort);
25
+ return callbacks;
26
+ };
27
+ const onAbort = () => cleanup().reject(newAbortError());
28
+ signal.addEventListener('abort', onAbort);
29
+ promise.then(value => cleanup().resolve(value), error => cleanup().reject(error));
30
+ });
31
+ }
32
+ //# sourceMappingURL=index.mjs.map
package/index.mjs.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.mjs","names":["newAbortError","DOMException","noop","abortable","promise","signal","Promise","resolve","reject","aborted","cleanup","callbacks","removeEventListener","onAbort","addEventListener","then","value","error"],"sources":["src/index.ts"],"sourcesContent":["export const newAbortError = () =>\n new DOMException('This operation was aborted', 'AbortError')\n\nconst noop = () => {}\n\nexport function abortable<T>(\n promise: Promise<T>,\n signal: AbortSignal | undefined\n): Promise<T> {\n if (!signal) return promise\n return new Promise<T>((resolve, reject) => {\n if (signal.aborted) {\n reject(newAbortError())\n return\n }\n const cleanup = () => {\n const callbacks = { resolve, reject }\n // Prevent memory leaks. If the input promise never resolves, then the handlers\n // below would retain this enclosing Promise's resolve and reject callbacks,\n // which would retain the enclosing Promise and anything waiting on it.\n // By replacing references to these callbacks, we enable the enclosing Promise to\n // be garbage collected\n resolve = noop\n reject = noop\n // Memory could also leak if the signal never aborts, unless we remove the abort\n // handler\n signal.removeEventListener('abort', onAbort)\n return callbacks\n }\n const onAbort = () => cleanup().reject(newAbortError())\n signal.addEventListener('abort', onAbort)\n promise.then(\n (value) => cleanup().resolve(value),\n (error) => cleanup().reject(error)\n )\n })\n}\n"],"mappings":"AAAA,OAAO,MAAMA,aAAa,GAAGA,CAAA,KAC3B,IAAIC,YAAY,CAAC,4BAA4B,EAAE,YAAY,CAAC;AAE9D,MAAMC,IAAI,GAAGA,CAAA,KAAM,CAAC,CAAC;AAErB,OAAO,SAASC,SAASA,CACvBC,OAAmB,EACnBC,MAA+B,EACnB;EACZ,IAAI,CAACA,MAAM,EAAE,OAAOD,OAAO;EAC3B,OAAO,IAAIE,OAAO,CAAI,CAACC,OAAO,EAAEC,MAAM,KAAK;IACzC,IAAIH,MAAM,CAACI,OAAO,EAAE;MAClBD,MAAM,CAACR,aAAa,CAAC,CAAC,CAAC;MACvB;IACF;IACA,MAAMU,OAAO,GAAGA,CAAA,KAAM;MACpB,MAAMC,SAAS,GAAG;QAAEJ,OAAO;QAAEC;MAAO,CAAC;MACrC;MACA;MACA;MACA;MACA;MACAD,OAAO,GAAGL,IAAI;MACdM,MAAM,GAAGN,IAAI;MACb;MACA;MACAG,MAAM,CAACO,mBAAmB,CAAC,OAAO,EAAEC,OAAO,CAAC;MAC5C,OAAOF,SAAS;IAClB,CAAC;IACD,MAAME,OAAO,GAAGA,CAAA,KAAMH,OAAO,CAAC,CAAC,CAACF,MAAM,CAACR,aAAa,CAAC,CAAC,CAAC;IACvDK,MAAM,CAACS,gBAAgB,CAAC,OAAO,EAAED,OAAO,CAAC;IACzCT,OAAO,CAACW,IAAI,CACTC,KAAK,IAAKN,OAAO,CAAC,CAAC,CAACH,OAAO,CAACS,KAAK,CAAC,EAClCC,KAAK,IAAKP,OAAO,CAAC,CAAC,CAACF,MAAM,CAACS,KAAK,CACnC,CAAC;EACH,CAAC,CAAC;AACJ","ignoreList":[]}
package/package.json ADDED
@@ -0,0 +1,41 @@
1
+ {
2
+ "name": "@jcoreio/abortable",
3
+ "description": "memory-leak-proof function to wrap a promise to reject when a signal is aborted",
4
+ "repository": {
5
+ "type": "git",
6
+ "url": "https://github.com/jcoreio/abortable.git"
7
+ },
8
+ "homepage": "https://github.com/jcoreio/abortable",
9
+ "bugs": {
10
+ "url": "https://github.com/jcoreio/abortable/issues"
11
+ },
12
+ "author": "Andy Edwards",
13
+ "license": "MIT",
14
+ "keywords": [
15
+ "signal",
16
+ "abortsignal",
17
+ "abort",
18
+ "promise"
19
+ ],
20
+ "version": "1.0.0",
21
+ "sideEffects": false,
22
+ "engines": {
23
+ "node": ">=16"
24
+ },
25
+ "main": "./index.js",
26
+ "module": "./index.mjs",
27
+ "types": "./index.d.ts",
28
+ "exports": {
29
+ "./package.json": "./package.json",
30
+ ".": {
31
+ "types": "./index.d.ts",
32
+ "import": "./index.mjs",
33
+ "default": "./index.js"
34
+ }
35
+ },
36
+ "packageManager": "pnpm@8.11.0",
37
+ "dependencies": {},
38
+ "@jcoreio/toolchain": {
39
+ "migratedVersion": "4.12.3"
40
+ }
41
+ }
package/src/index.ts ADDED
@@ -0,0 +1,37 @@
1
+ export const newAbortError = () =>
2
+ new DOMException('This operation was aborted', 'AbortError')
3
+
4
+ const noop = () => {}
5
+
6
+ export function abortable<T>(
7
+ promise: Promise<T>,
8
+ signal: AbortSignal | undefined
9
+ ): Promise<T> {
10
+ if (!signal) return promise
11
+ return new Promise<T>((resolve, reject) => {
12
+ if (signal.aborted) {
13
+ reject(newAbortError())
14
+ return
15
+ }
16
+ const cleanup = () => {
17
+ const callbacks = { resolve, reject }
18
+ // Prevent memory leaks. If the input promise never resolves, then the handlers
19
+ // below would retain this enclosing Promise's resolve and reject callbacks,
20
+ // which would retain the enclosing Promise and anything waiting on it.
21
+ // By replacing references to these callbacks, we enable the enclosing Promise to
22
+ // be garbage collected
23
+ resolve = noop
24
+ reject = noop
25
+ // Memory could also leak if the signal never aborts, unless we remove the abort
26
+ // handler
27
+ signal.removeEventListener('abort', onAbort)
28
+ return callbacks
29
+ }
30
+ const onAbort = () => cleanup().reject(newAbortError())
31
+ signal.addEventListener('abort', onAbort)
32
+ promise.then(
33
+ (value) => cleanup().resolve(value),
34
+ (error) => cleanup().reject(error)
35
+ )
36
+ })
37
+ }