From 0ff4a558afeb0ec051041e8f1f9c45d876ae0530 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Fri, 4 Oct 2019 20:37:51 +0200 Subject: http2: allow passing FileHandle to respondWithFD MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This seems to make sense if we want to promote the use of `fs.promises`, although it’s not strictly necessary. PR-URL: https://github.com/nodejs/node/pull/29876 Reviewed-By: Luigi Pinca Reviewed-By: Colin Ihrig Reviewed-By: James M Snell Reviewed-By: Minwoo Jung Reviewed-By: Ruben Bridgewater --- lib/fs.js | 2 +- lib/internal/fs/promises.js | 56 ++++++++++++++++++++++++--------------------- lib/internal/http2/core.js | 6 ++++- 3 files changed, 36 insertions(+), 28 deletions(-) (limited to 'lib') diff --git a/lib/fs.js b/lib/fs.js index 34517a17da..d5c7ea70d8 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -1964,7 +1964,7 @@ Object.defineProperties(fs, { enumerable: true, get() { if (promises === null) - promises = require('internal/fs/promises'); + promises = require('internal/fs/promises').exports; return promises; } } diff --git a/lib/internal/fs/promises.js b/lib/internal/fs/promises.js index 7660ff66be..31613780a7 100644 --- a/lib/internal/fs/promises.js +++ b/lib/internal/fs/promises.js @@ -46,7 +46,7 @@ const { const pathModule = require('path'); const { promisify } = require('internal/util'); -const kHandle = Symbol('handle'); +const kHandle = Symbol('kHandle'); const { kUsePromises } = binding; const getDirectoryEntriesPromise = promisify(getDirents); @@ -507,29 +507,33 @@ async function readFile(path, options) { } module.exports = { - access, - copyFile, - open, - opendir: promisify(opendir), - rename, - truncate, - rmdir, - mkdir, - readdir, - readlink, - symlink, - lstat, - stat, - link, - unlink, - chmod, - lchmod, - lchown, - chown, - utimes, - realpath, - mkdtemp, - writeFile, - appendFile, - readFile + exports: { + access, + copyFile, + open, + opendir: promisify(opendir), + rename, + truncate, + rmdir, + mkdir, + readdir, + readlink, + symlink, + lstat, + stat, + link, + unlink, + chmod, + lchmod, + lchown, + chown, + utimes, + realpath, + mkdtemp, + writeFile, + appendFile, + readFile, + }, + + FileHandle }; diff --git a/lib/internal/http2/core.js b/lib/internal/http2/core.js index d2701dfb84..11d677c5f6 100644 --- a/lib/internal/http2/core.js +++ b/lib/internal/http2/core.js @@ -82,6 +82,7 @@ const { hideStackFrames } = require('internal/errors'); const { validateNumber, validateString } = require('internal/validators'); +const fsPromisesInternal = require('internal/fs/promises'); const { utcDate } = require('internal/http'); const { onServerStream, Http2ServerRequest, @@ -2523,7 +2524,10 @@ class ServerHttp2Stream extends Http2Stream { this[kState].flags |= STREAM_FLAGS_HAS_TRAILERS; } - validateNumber(fd, 'fd'); + if (fd instanceof fsPromisesInternal.FileHandle) + fd = fd.fd; + else if (typeof fd !== 'number') + throw new ERR_INVALID_ARG_TYPE('fd', ['number', 'FileHandle'], fd); debugStreamObj(this, 'initiating response from fd'); this[kUpdateTimer](); -- cgit v1.2.3