Skip to content

Commit f02dd3c

Browse files
committed
abort stream on exit
1 parent 96310a1 commit f02dd3c

File tree

3 files changed

+73
-32
lines changed

3 files changed

+73
-32
lines changed

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,15 @@
1616
"clean": "rm -rf dist"
1717
},
1818
"dependencies": {
19+
"abort-controller": "^3.0.0",
1920
"node-fetch": "^2.6.1"
2021
},
2122
"engines": {
2223
"node": ">=14"
2324
},
2425
"devDependencies": {
25-
"@types/node-fetch": "^2.6.1",
2626
"@types/jest": "^27.4.1",
27+
"@types/node-fetch": "^2.6.1",
2728
"jest": "^27.5.1",
2829
"nock": "^13.2.4",
2930
"ts-jest": "^27.0.3",

src/request.ts

+29-8
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,19 @@ import {
99
TwitterNextToken,
1010
TwitterPaginatedResponse,
1111
} from "./types";
12+
import type { AbortController as AbortControllerPolyfill } from "abort-controller";
13+
14+
let AbortController:
15+
| typeof globalThis.AbortController
16+
| typeof AbortControllerPolyfill;
17+
18+
if (!globalThis.AbortController) {
19+
AbortController = require("abort-controller");
20+
} else {
21+
// https://nodejs.org/api/globals.html#class-abortcontroller
22+
// AbortController available in v14.17.0 as experimental
23+
AbortController = globalThis.AbortController;
24+
}
1225

1326
export interface RequestOptions extends Omit<RequestInit, "body"> {
1427
auth?: AuthClient;
@@ -83,17 +96,25 @@ export async function request({
8396
}
8497

8598
export async function* stream<T>(args: RequestOptions): AsyncGenerator<T> {
86-
const { body } = await request(args);
99+
const controller = new AbortController();
100+
const { body } = await request({
101+
signal: controller.signal as RequestInit["signal"],
102+
...args,
103+
});
87104
if (body === null) throw new Error("No response returned from stream");
88105
let buf = "";
89-
for await (const chunk of body) {
90-
buf += chunk.toString();
91-
const lines = buf.split("\r\n");
92-
for (const [i, line] of lines.entries()) {
93-
if (i === lines.length - 1) {
94-
buf = line;
95-
} else if (line) yield JSON.parse(line);
106+
try {
107+
for await (const chunk of body) {
108+
buf += chunk.toString();
109+
const lines = buf.split("\r\n");
110+
for (const [i, line] of lines.entries()) {
111+
if (i === lines.length - 1) {
112+
buf = line;
113+
} else if (line) yield JSON.parse(line);
114+
}
96115
}
116+
} finally {
117+
controller.abort();
97118
}
98119
}
99120

yarn.lock

+42-23
Original file line numberDiff line numberDiff line change
@@ -550,9 +550,9 @@
550550
"@types/istanbul-lib-report" "*"
551551

552552
"@types/jest@^27.4.1":
553-
version "27.4.1"
554-
resolved "https://registry.npmjs.org/@types/jest/-/jest-27.4.1.tgz#185cbe2926eaaf9662d340cc02e548ce9e11ab6d"
555-
integrity sha512-23iPJADSmicDVrWk+HT58LMJtzLAnB2AgIzplQuq/bSrGaxCrlvRFjGbXmamnnk/mAmCdLStiGqggu28ocUyiw==
553+
version "27.5.2"
554+
resolved "https://registry.npmjs.org/@types/jest/-/jest-27.5.2.tgz#ec49d29d926500ffb9fd22b84262e862049c026c"
555+
integrity sha512-mpT8LJJ4CMeeahobofYWIjFo0xonRS/HfxnVEPMPFSQdGUt1uHCnoPT7Zhb+sjDU2wz0oKV0OLUR0WzrHNgfeA==
556556
dependencies:
557557
jest-matcher-utils "^27.0.0"
558558
pretty-format "^27.0.0"
@@ -566,9 +566,9 @@
566566
form-data "^3.0.0"
567567

568568
"@types/node@*":
569-
version "17.0.23"
570-
resolved "https://registry.npmjs.org/@types/node/-/node-17.0.23.tgz#3b41a6e643589ac6442bdbd7a4a3ded62f33f7da"
571-
integrity sha512-UxDxWn7dl97rKVeVS61vErvw086aCYhDLyvRQZ5Rk65rZKepaFdm53GeqXaKBuOhED4e9uWq34IC3TdSdJJ2Gw==
569+
version "17.0.38"
570+
resolved "https://registry.npmjs.org/@types/node/-/node-17.0.38.tgz#f8bb07c371ccb1903f3752872c89f44006132947"
571+
integrity sha512-5jY9RhV7c0Z4Jy09G+NIDTsCZ5G0L5n+Z+p+Y7t5VJHM30bgwzSjVtlcBxqAj+6L/swIlvtOSzr8rBk/aNyV2g==
572572

573573
"@types/prettier@^2.1.5":
574574
version "2.4.3"
@@ -597,6 +597,13 @@ abab@^2.0.3, abab@^2.0.5:
597597
resolved "https://registry.npmjs.org/abab/-/abab-2.0.5.tgz#c0b678fb32d60fc1219c784d6a826fe385aeb79a"
598598
integrity sha512-9IK9EadsbHo6jLWIpxpR6pL0sazTXV6+SQv25ZB+F7Bj9mJNaOc4nCRabwd5M/JwmUa8idz6Eci6eKfJryPs6Q==
599599

600+
abort-controller@^3.0.0:
601+
version "3.0.0"
602+
resolved "https://registry.npmjs.org/abort-controller/-/abort-controller-3.0.0.tgz#eaf54d53b62bae4138e809ca225c8439a6efb392"
603+
integrity sha512-h8lQ8tacZYnR3vNQTgibj+tODHI5/+l06Au2Pcriv/Gmet0eaj4TwWH41sO9wnHDiQsEj19q0drzdWdeAHtweg==
604+
dependencies:
605+
event-target-shim "^5.0.0"
606+
600607
acorn-globals@^6.0.0:
601608
version "6.0.0"
602609
resolved "https://registry.npmjs.org/acorn-globals/-/acorn-globals-6.0.0.tgz#46cdd39f0f8ff08a876619b55f5ac8a6dc770b45"
@@ -837,9 +844,9 @@ char-regex@^1.0.2:
837844
integrity sha512-kWWXztvZ5SBQV+eRgKFeh8q5sLuZY2+8WUIzlxWVTg+oGwY14qylx1KbKzHd8P6ZYkAg0xyIDU9JMHhyJMZ1jw==
838845

839846
ci-info@^3.2.0:
840-
version "3.3.0"
841-
resolved "https://registry.npmjs.org/ci-info/-/ci-info-3.3.0.tgz#b4ed1fb6818dea4803a55c623041f9165d2066b2"
842-
integrity sha512-riT/3vI5YpVH6/qomlDnJow6TBee2PBKSEpx3O32EGPYbWGIRsIlGRms3Sm74wYE1JMo8RnO04Hb12+v1J5ICw==
847+
version "3.3.1"
848+
resolved "https://registry.npmjs.org/ci-info/-/ci-info-3.3.1.tgz#58331f6f472a25fe3a50a351ae3052936c2c7f32"
849+
integrity sha512-SXgeMX9VwDe7iFFaEWkA5AstuER9YKqy4EhHqr4DVqkwmD9rpVimkMKWHdjn30Ja45txyjhSn63lVX69eVCckg==
843850

844851
cjs-module-lexer@^1.0.0:
845852
version "1.2.2"
@@ -1056,6 +1063,11 @@ esutils@^2.0.2:
10561063
resolved "https://registry.npmjs.org/esutils/-/esutils-2.0.3.tgz#74d2eb4de0b8da1293711910d50775b9b710ef64"
10571064
integrity sha512-kVscqXk4OCp68SZ0dkgEKVi6/8ij300KBWTJq32P/dYeWTSwK41WyTxalN1eRmA5Z9UU/LX9D7FWSmV9SAYx6g==
10581065

1066+
event-target-shim@^5.0.0:
1067+
version "5.0.1"
1068+
resolved "https://registry.npmjs.org/event-target-shim/-/event-target-shim-5.0.1.tgz#5d4d3ebdf9583d63a5333ce2deb7480ab2b05789"
1069+
integrity sha512-i/2XbnSz/uxRCU6+NdVJgKWDTM427+MqYbkQzD321DuCQJUqOuJKIA0IM2+W2xtYHdKOmZ4dR6fExsd4SXL+WQ==
1070+
10591071
execa@^5.0.0:
10601072
version "5.1.1"
10611073
resolved "https://registry.npmjs.org/execa/-/execa-5.1.1.tgz#f80ad9cbf4298f7bd1d4c9555c21e93741c411dd"
@@ -1180,9 +1192,9 @@ globals@^11.1.0:
11801192
integrity sha512-WOBp/EEGUiIsJSp7wcv/y6MO+lV9UoncWqxuFfm8eBwzWNgyfBd6Gz+IeKQ9jCmyhoH99g15M3T+QaVHFjizVA==
11811193

11821194
graceful-fs@^4.2.9:
1183-
version "4.2.9"
1184-
resolved "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.2.9.tgz#041b05df45755e587a24942279b9d113146e1c96"
1185-
integrity sha512-NtNxqUcXgpW2iMrfqSfR73Glt39K+BLwWsPs94yR63v45T0Wbej7eRmL5cWfwEgqXnmjQp3zaJTshdRW/qC2ZQ==
1195+
version "4.2.10"
1196+
resolved "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.2.10.tgz#147d3a006da4ca3ce14728c7aefc287c367d7a6c"
1197+
integrity sha512-9ByhssR2fPVsNZj478qUUbKfmL0+t5BDVyjShtyZZLiK7ZDAArFFfopyOTj0M05wE2tJPisA4iTnnXl2YoPvOA==
11861198

11871199
has-flag@^3.0.0:
11881200
version "3.0.0"
@@ -1868,7 +1880,7 @@ locate-path@^5.0.0:
18681880
18691881
version "4.1.2"
18701882
resolved "https://registry.npmjs.org/lodash.memoize/-/lodash.memoize-4.1.2.tgz#bcc6c49a42a2840ed997f323eada5ecd182e0bfe"
1871-
integrity sha1-vMbEmkKihA7Zl/Mj6tpezRguC/4=
1883+
integrity sha512-t7j+NzmgnQzTAYXcsHYLgimltOV1MXHtlOWf6GjL9Kj8GK5FInw5JotxvbOs+IvV1/Dzo04/fCGfLVs7aXb4Ag==
18721884

18731885
lodash.set@^4.3.2:
18741886
version "4.3.2"
@@ -2201,10 +2213,10 @@ saxes@^5.0.1:
22012213
dependencies:
22022214
xmlchars "^2.2.0"
22032215

2204-
[email protected], semver@^7.3.2:
2205-
version "7.3.5"
2206-
resolved "https://registry.npmjs.org/semver/-/semver-7.3.5.tgz#0b621c879348d8998e4b0e4be94b3f12e6018ef7"
2207-
integrity sha512-PoeGJYh8HK4BTO/a9Tf6ZG3veo/A7ZVsYrSA6J8ny9nb3B1VrpkuN+z9OE5wfE5p6H4LchYZsegiQgbJD94ZFQ==
2216+
2217+
version "7.3.7"
2218+
resolved "https://registry.npmjs.org/semver/-/semver-7.3.7.tgz#12c5b649afdbf9049707796e22a4028814ce523f"
2219+
integrity sha512-QlYTucUYOews+WeEujDoEGziz4K6c47V/Bd+LjSSYcA94p+DmINdf7ncaUinThfvZyu13lN9OY1XDxt8C0Tw0g==
22082220
dependencies:
22092221
lru-cache "^6.0.0"
22102222

@@ -2213,6 +2225,13 @@ semver@^6.0.0, semver@^6.3.0:
22132225
resolved "https://registry.npmjs.org/semver/-/semver-6.3.0.tgz#ee0a64c8af5e8ceea67687b133761e1becbd1d3d"
22142226
integrity sha512-b39TBaTSfV6yBrapU89p5fKekE2m/NwnDocOVruQFS1/veMgdzuPcnOM34M6CwxW8jH/lxEa5rBoDeUwu5HHTw==
22152227

2228+
semver@^7.3.2:
2229+
version "7.3.5"
2230+
resolved "https://registry.npmjs.org/semver/-/semver-7.3.5.tgz#0b621c879348d8998e4b0e4be94b3f12e6018ef7"
2231+
integrity sha512-PoeGJYh8HK4BTO/a9Tf6ZG3veo/A7ZVsYrSA6J8ny9nb3B1VrpkuN+z9OE5wfE5p6H4LchYZsegiQgbJD94ZFQ==
2232+
dependencies:
2233+
lru-cache "^6.0.0"
2234+
22162235
shebang-command@^2.0.0:
22172236
version "2.0.0"
22182237
resolved "https://registry.npmjs.org/shebang-command/-/shebang-command-2.0.0.tgz#ccd0af4f8835fbdc265b82461aaf0c36663f34ea"
@@ -2414,9 +2433,9 @@ tr46@~0.0.3:
24142433
integrity sha1-gYT9NH2snNwYWZLzpmIuFLnZq2o=
24152434

24162435
ts-jest@^27.0.3:
2417-
version "27.1.4"
2418-
resolved "https://registry.npmjs.org/ts-jest/-/ts-jest-27.1.4.tgz#84d42cf0f4e7157a52e7c64b1492c46330943e00"
2419-
integrity sha512-qjkZlVPWVctAezwsOD1OPzbZ+k7zA5z3oxII4dGdZo5ggX/PL7kvwTM0pXTr10fAtbiVpJaL3bWd502zAhpgSQ==
2436+
version "27.1.5"
2437+
resolved "https://registry.npmjs.org/ts-jest/-/ts-jest-27.1.5.tgz#0ddf1b163fbaae3d5b7504a1e65c914a95cff297"
2438+
integrity sha512-Xv6jBQPoBEvBq/5i2TeSG9tt/nqkbpcurrEG1b+2yfBrcJelOZF9Ml6dmyMh7bcW9JyFbRYpR5rxROSlBLTZHA==
24202439
dependencies:
24212440
bs-logger "0.x"
24222441
fast-json-stable-stringify "2.x"
@@ -2452,9 +2471,9 @@ typedarray-to-buffer@^3.1.5:
24522471
is-typedarray "^1.0.0"
24532472

24542473
typescript@^4.4.4:
2455-
version "4.6.3"
2456-
resolved "https://registry.npmjs.org/typescript/-/typescript-4.6.3.tgz#eefeafa6afdd31d725584c67a0eaba80f6fc6c6c"
2457-
integrity sha512-yNIatDa5iaofVozS/uQJEl3JRWLKKGJKh6Yaiv0GLGSuhpFJe7P3SbHZ8/yjAHRQwKRoA6YZqlfjXWmVzoVSMw==
2474+
version "4.7.2"
2475+
resolved "https://registry.npmjs.org/typescript/-/typescript-4.7.2.tgz#1f9aa2ceb9af87cca227813b4310fff0b51593c4"
2476+
integrity sha512-Mamb1iX2FDUpcTRzltPxgWMKy3fhg0TN378ylbktPGPK/99KbDtMQ4W1hwgsbPAsG3a0xKa1vmw4VKZQbkvz5A==
24582477

24592478
universalify@^0.1.2:
24602479
version "0.1.2"

0 commit comments

Comments
 (0)