Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid ERR_INVALID_ARG_TYPE error on Node.js >= 9 #23

Merged
merged 1 commit into from
Nov 16, 2017
Merged

Avoid ERR_INVALID_ARG_TYPE error on Node.js >= 9 #23

merged 1 commit into from
Nov 16, 2017

Conversation

shinnn
Copy link
Contributor

@shinnn shinnn commented Nov 16, 2017

Problem

From nodejs/node@e5c290b that is shipped with Node.js 9, fs.WriteStream#close() requires a callback function as its first argument and throws an error when no arguments are provided to it.

Here is a small example to reproduce the error:

'use strict';

const {createWriteStream} = require('fs');
const {PassThrough} = require('stream');
const pump = require('pump');

const firstStream = new PassThrough();
firstStream.end(Buffer.alloc(99999999));

pump([
  firstStream,
  // Writing contents to `__dirname` should always fail with EISDIR error.
  createWriteStream(__dirname)
], err => {
  console.log(err.message);
});

With Node.js 8.9.1, it works as expected.

$ node --version
v8.9.1

$ node exmaple.js
EISDIR: illegal operation on a directory, open '/Users/shinnn/github/pump'

But with Node.js 9.2.0, it throws a TypeError like below:

$ node --version
v9.2.0

$ node exmaple.js
events.js:180
    throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'listener', 'Function');
    ^

TypeError [ERR_INVALID_ARG_TYPE]: The "listener" argument must be of type Function
    at _addListener (events.js:180:11)
    at WriteStream.addListener (events.js:240:10)
    at WriteStream.close (fs.js:2302:10)
    at /Users/shinnn/github/pump/index.js:40:37
    at call (/Users/shinnn/github/pump/index.js:50:3)
    at Array.forEach (<anonymous>)
    at /Users/shinnn/github/pump/index.js:70:25
    at f (/Users/shinnn/github/pump/node_modules/once/once.js:25:25)
    at WriteStream.<anonymous> (/Users/shinnn/github/pump/index.js:29:21)
    at WriteStream.f (/Users/shinnn/github/pump/node_modules/once/once.js:25:25)

Solution

I added a noop function to the fs.WriteStream#close() argument to avoid the ERR_INVALID_ARG_TYPE error.

From nodejs/node@e5c290b included in Node.js >= 9, fs.WriteStream#close() requires a callback function and throws an error when no arguments are provided.

This patch makes pump compatible with Node.js >= 9 by passing a noop function to fs.WriteStream#close().
@mafintosh mafintosh merged commit a430c27 into mafintosh:master Nov 16, 2017
@mafintosh
Copy link
Owner

YUUUUS, 1.0.3

@shinnn
Copy link
Contributor Author

shinnn commented Nov 16, 2017

Glad of your quick release :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants