10000 Fix destructure error on `promsie.json()` · sindresorhus/got@c97ce7c · GitHub
[go: up one dir, main page]

Skip to content

Commit c97ce7c

Browse files
committed
Fix destructure error on promsie.json()
1 parent 1b208df commit c97ce7c

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

source/core/index.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1426,17 +1426,25 @@ export default class Request extends Duplex implements RequestEvents<Request> {
14261426
this._lockWrite();
14271427
}
14281428

1429-
(async (nonNormalizedOptions: Options) => {
1429+
if (kIsNormalizedAlready in options) {
1430+
this.options = options as NormalizedOptions;
1431+
} else {
14301432
try {
1431-
if (nonNormalizedOptions.body instanceof ReadStream) {
1432-
await waitForOpenFile(nonNormalizedOptions.body);
1433+
// @ts-expect-error Common TypeScript bug saying that `this.constructor` is not accessible
1434+
this.options = this.constructor.normalizeArguments(url, options, defaults);
1435+
} catch (error) {
1436+
if (is.nodeStream(options.body)) {
1437+
options.body.destroy();
14331438
}
14341439

1435-
if (kIsNormalizedAlready in nonNormalizedOptions) {
1436-
this.options = nonNormalizedOptions as NormalizedOptions;
1437-
} else {
1438-
// @ts-expect-error Common TypeScript bug saying that `this.constructor` is not accessible
1439-
this.options = this.constructor.normalizeArguments(url, nonNormalizedOptions, defaults);
1440+
throw error;
1441+
}
1442+
}
1443+
1444+
(async () => {
1445+
try {
1446+
if (this.options.body instanceof ReadStream) {
1447+
await waitForOpenFile(this.options.body);
14401448
}
14411449

14421450
const {url: normalizedURL} = this.options;
@@ -1476,7 +1484,7 @@ export default class Request extends Duplex implements RequestEvents<Request> {
14761484
this.destroy(error);
14771485
}
14781486
}
1479-
})(options);
1487+
})();
14801488
}
14811489

14821490
static normalizeArguments(url?: string | URL, options?: Options, defaults?: Defaults): NormalizedOptions {

test/stream.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ test.failing('no unhandled body stream errors', async t => {
328328
body.append('upload', fs.createReadStream('/bin/sh'));
329329

330330
await t.throwsAsync(got.post(`https://offlinesite${Date.now()}.com`, {
331-
form: body
331+
body
332332
}), {
333333
code: 'ENOTFOUND'
334334
});

0 commit comments

Comments
 (0)
0