Skip to content

Commit

Permalink
avoid process.binding() where possible (#5)
Browse files Browse the repository at this point in the history
avoid process.binding() where possible

process.binding() is deprecated. When running recent versions of Node.js
with --pending-deprecation, the current code results in a printed
warning about process.binding() usage. process.binding() is used to
create writev(), but Node.js now exposes fs.writev(). Use that instead
when it exists. Otherwise, fall back to the polyfill. This will avoid
runtime deprecation messages for end users.

If the minimum supported engine is ever Node.js 12.9.0 or above, the
polyfill code should be removable at that time.
  • Loading branch information
Trott authored and ruyadorno committed Jan 20, 2020
1 parent 8b16337 commit e0b51ea
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,21 @@ const MiniPass = require('minipass')
const EE = require('events').EventEmitter
const fs = require('fs')

// for writev
const binding = process.binding('fs')
const writeBuffers = binding.writeBuffers
let writev = fs.writev
/* istanbul ignore next */
const FSReqWrap = binding.FSReqWrap || binding.FSReqCallback
if (!writev) {
// This entire block can be removed if support for earlier than Node.js
// 12.9.0 is not needed.
const binding = process.binding('fs')
const FSReqWrap = binding.FSReqWrap || binding.FSReqCallback

writev = (fd, iovec, pos, cb) => {
const done = (er, bw) => cb(er, bw, iovec)
const req = new FSReqWrap()
req.oncomplete = done
binding.writeBuffers(fd, iovec, pos, req)
}
}

const _autoClose = Symbol('_autoClose')
const _close = Symbol('_close')
Expand Down Expand Up @@ -373,13 +383,6 @@ class WriteStreamSync extends WriteStream {
}
}

const writev = (fd, iovec, pos, cb) => {
const done = (er, bw) => cb(er, bw, iovec)
const req = new FSReqWrap()
req.oncomplete = done
binding.writeBuffers(fd, iovec, pos, req)
}

exports.ReadStream = ReadStream
exports.ReadStreamSync = ReadStreamSync

Expand Down

0 comments on commit e0b51ea

Please sign in to comment.