Skip to content

Commit

Permalink
Do not wait for ready event if the file descriptor is already opened (
Browse files Browse the repository at this point in the history
  • Loading branch information
Giotino authored and szmarczak committed Jul 6, 2020
1 parent 2f49800 commit 21286e5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
6 changes: 5 additions & 1 deletion source/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,12 @@ const waitForOpenFile = async (file: ReadStream): Promise<void> => new Promise((
reject(error);
};

if (!file.pending) {
resolve();
}

file.once('error', onError);
file.once('open', () => {
file.once('ready', () => {
file.off('error', onError);
resolve();
});
Expand Down
17 changes: 17 additions & 0 deletions test/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import fs = require('fs');
import path = require('path');
import test from 'ava';
import delay = require('delay');
import pEvent = require('p-event');
import {Handler} from 'express';
import getStream = require('get-stream');
import toReadableStream = require('to-readable-stream');
Expand Down Expand Up @@ -318,6 +319,22 @@ test('body - file read stream', withServer, async (t, server, got) => {
t.is(toSend, body);
});

test('body - file read stream, wait for `ready` event', withServer, async (t, server, got) => {
server.post('/', defaultEndpoint);

const fullPath = path.resolve('test/fixtures/ok');
const toSend = await getStream(fs.createReadStream(fullPath));
const ifStream = fs.createReadStream(fullPath);

await pEvent(ifStream, 'ready');

const body = await got.post({
body: ifStream
}).text();

t.is(toSend, body);
});

test('throws on upload error', withServer, async (t, server, got) => {
server.post('/', defaultEndpoint);

Expand Down

0 comments on commit 21286e5

Please sign in to comment.