From e0b51ea4080a8803d32082be3a0902e6cf19286d Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Mon, 20 Jan 2020 10:14:09 -0800 Subject: [PATCH] avoid process.binding() where possible (#5) 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. --- index.js | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/index.js b/index.js index a43c260..2402890 100644 --- a/index.js +++ b/index.js @@ -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') @@ -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