From dd0381fe4e4ded48d6d9b92dfeb6b7b7d4991b60 Mon Sep 17 00:00:00 2001 From: Masashi Hirano Date: Thu, 23 Aug 2018 23:29:40 +0900 Subject: [PATCH] fs: make process.binding('fs') internal MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Refs: https://github.com/nodejs/node/issues/22160 PR-URL: https://github.com/nodejs/node/pull/22478 Reviewed-By: Ruben Bridgewater Reviewed-By: James M Snell Reviewed-By: Anna Henningsen Reviewed-By: Michaƫl Zasso Reviewed-By: Joyee Cheung Reviewed-By: Denys Otrishko Backport-PR-URL: https://github.com/nodejs/node/pull/25446 --- lib/fs.js | 2 +- lib/internal/bootstrap/loaders.js | 1 + lib/internal/fs/promises.js | 2 +- lib/internal/fs/read_file_context.js | 3 ++- lib/internal/fs/streams.js | 2 +- lib/internal/fs/watchers.js | 2 +- lib/internal/http2/core.js | 2 +- lib/internal/modules/cjs/loader.js | 2 +- lib/internal/net.js | 2 +- src/node_file.cc | 2 +- test/parallel/test-assert.js | 4 +++- test/parallel/test-fs-filehandle.js | 18 ++++++++++++------ test/parallel/test-fs-sync-fd-leak.js | 2 +- test/parallel/test-module-binding.js | 4 +++- ...rocess-binding-internalbinding-whitelist.js | 1 + test/sequential/test-async-wrap-getasyncid.js | 2 +- 16 files changed, 32 insertions(+), 19 deletions(-) diff --git a/lib/fs.js b/lib/fs.js index ad0a498e639521..fcedb4eb07844f 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -41,7 +41,7 @@ const { const pathModule = require('path'); const { isArrayBufferView } = require('internal/util/types'); -const binding = process.binding('fs'); +const binding = internalBinding('fs'); const { Buffer, kMaxLength } = require('buffer'); const errors = require('internal/errors'); const { diff --git a/lib/internal/bootstrap/loaders.js b/lib/internal/bootstrap/loaders.js index 02abc683072ae2..88227f2927c05a 100644 --- a/lib/internal/bootstrap/loaders.js +++ b/lib/internal/bootstrap/loaders.js @@ -81,6 +81,7 @@ 'constants', 'contextify', 'crypto', + 'fs', 'fs_event_wrap', 'http_parser', 'icu', diff --git a/lib/internal/fs/promises.js b/lib/internal/fs/promises.js index b6b4d6605d3387..33c510ed5ca702 100644 --- a/lib/internal/fs/promises.js +++ b/lib/internal/fs/promises.js @@ -7,7 +7,7 @@ const { S_IFMT, S_IFREG } = internalBinding('constants').fs; -const binding = process.binding('fs'); +const binding = internalBinding('fs'); const { Buffer, kMaxLength } = require('buffer'); const { ERR_FS_FILE_TOO_LARGE, diff --git a/lib/internal/fs/read_file_context.js b/lib/internal/fs/read_file_context.js index 62d2ed9e7d91ca..d7543ffa5aafed 100644 --- a/lib/internal/fs/read_file_context.js +++ b/lib/internal/fs/read_file_context.js @@ -1,7 +1,8 @@ 'use strict'; const { Buffer } = require('buffer'); -const { FSReqCallback, close, read } = process.binding('fs'); + +const { FSReqCallback, close, read } = internalBinding('fs'); const kReadFileBufferLength = 8 * 1024; diff --git a/lib/internal/fs/streams.js b/lib/internal/fs/streams.js index 224752b1011c24..dc79a5b79abff1 100644 --- a/lib/internal/fs/streams.js +++ b/lib/internal/fs/streams.js @@ -3,7 +3,7 @@ const { FSReqCallback, writeBuffers -} = process.binding('fs'); +} = internalBinding('fs'); const { ERR_INVALID_ARG_TYPE, ERR_OUT_OF_RANGE diff --git a/lib/internal/fs/watchers.js b/lib/internal/fs/watchers.js index f542a1eab1d177..e026aa8192c3cf 100644 --- a/lib/internal/fs/watchers.js +++ b/lib/internal/fs/watchers.js @@ -4,7 +4,7 @@ const errors = require('internal/errors'); const { kFsStatsFieldsNumber, StatWatcher: _StatWatcher -} = process.binding('fs'); +} = internalBinding('fs'); const { FSEvent } = internalBinding('fs_event_wrap'); const { UV_ENOSPC } = internalBinding('uv'); const { EventEmitter } = require('events'); diff --git a/lib/internal/http2/core.js b/lib/internal/http2/core.js index c0ad0ae3529a75..400db84b56f466 100644 --- a/lib/internal/http2/core.js +++ b/lib/internal/http2/core.js @@ -119,7 +119,7 @@ const { } = require('internal/timers'); const { isArrayBufferView } = require('internal/util/types'); -const { FileHandle } = process.binding('fs'); +const { FileHandle } = internalBinding('fs'); const binding = internalBinding('http2'); const { ShutdownWrap, diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js index 83ca0699773fa8..3f0a27137d96ba 100644 --- a/lib/internal/modules/cjs/loader.js +++ b/lib/internal/modules/cjs/loader.js @@ -33,7 +33,7 @@ const { URL } = require('url'); const { internalModuleReadJSON, internalModuleStat -} = process.binding('fs'); +} = internalBinding('fs'); const { safeGetenv } = internalBinding('util'); const { makeRequireFunction, diff --git a/lib/internal/net.js b/lib/internal/net.js index d2b097573b918e..e8b67ed0fd397a 100644 --- a/lib/internal/net.js +++ b/lib/internal/net.js @@ -1,7 +1,7 @@ 'use strict'; const Buffer = require('buffer').Buffer; -const { writeBuffer } = process.binding('fs'); +const { writeBuffer } = internalBinding('fs'); const errors = require('internal/errors'); // IPv4 Segment diff --git a/src/node_file.cc b/src/node_file.cc index ae34d90a2d026a..b66c0fe6d20915 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -2236,4 +2236,4 @@ void Initialize(Local target, } // end namespace node -NODE_BUILTIN_MODULE_CONTEXT_AWARE(fs, node::fs::Initialize) +NODE_MODULE_CONTEXT_AWARE_INTERNAL(fs, node::fs::Initialize) diff --git a/test/parallel/test-assert.js b/test/parallel/test-assert.js index 172a55959ec97b..db00ed2836327f 100644 --- a/test/parallel/test-assert.js +++ b/test/parallel/test-assert.js @@ -1,3 +1,4 @@ +// Flags: --expose-internals // Copyright Joyent, Inc. and other Node contributors. // // Permission is hereby granted, free of charge, to any person obtaining a @@ -24,6 +25,7 @@ const common = require('../common'); const assert = require('assert'); const { inspect } = require('util'); +const { internalBinding } = require('internal/test/binding'); const a = assert; // Disable colored output to prevent color codes from breaking assertion @@ -656,7 +658,7 @@ common.expectsError( { // Test caching. - const fs = process.binding('fs'); + const fs = internalBinding('fs'); const tmp = fs.close; fs.close = common.mustCall(tmp, 1); function throwErr() { diff --git a/test/parallel/test-fs-filehandle.js b/test/parallel/test-fs-filehandle.js index 0d1dffa2b5ff78..a56b39f7c92526 100644 --- a/test/parallel/test-fs-filehandle.js +++ b/test/parallel/test-fs-filehandle.js @@ -4,7 +4,8 @@ const common = require('../common'); const assert = require('assert'); const path = require('path'); -const fs = process.binding('fs'); +const { internalBinding } = require('internal/test/binding'); +const fs = internalBinding('fs'); const { stringToFlags } = require('internal/fs/utils'); // Verifies that the FileHandle object is garbage collected and that a @@ -18,11 +19,16 @@ let fdnum; assert.strictEqual(ctx.errno, undefined); } -common.expectWarning( - 'Warning', - `Closing file descriptor ${fdnum} on garbage collection`, - common.noWarnCode -); +common.expectWarning({ + 'internal/test/binding': [ + 'These APIs are for internal testing only. Do not use them.', + common.noWarnCode + ], + 'Warning': [ + `Closing file descriptor ${fdnum} on garbage collection`, + common.noWarnCode + ] +}); global.gc(); diff --git a/test/parallel/test-fs-sync-fd-leak.js b/test/parallel/test-fs-sync-fd-leak.js index 30016e02845298..52f6f38f9c0ea0 100644 --- a/test/parallel/test-fs-sync-fd-leak.js +++ b/test/parallel/test-fs-sync-fd-leak.js @@ -42,7 +42,7 @@ fs.writeSync = function() { throw new Error('BAM'); }; -process.binding('fs').fstat = function(fd, bigint, _, ctx) { +internalBinding('fs').fstat = function(fd, bigint, _, ctx) { ctx.errno = UV_EBADF; ctx.syscall = 'fstat'; }; diff --git a/test/parallel/test-module-binding.js b/test/parallel/test-module-binding.js index a3ebaf9e7266d2..07c4965f2204f3 100644 --- a/test/parallel/test-module-binding.js +++ b/test/parallel/test-module-binding.js @@ -1,7 +1,9 @@ +// Flags: --expose-internals 'use strict'; require('../common'); const fixtures = require('../common/fixtures'); -const { internalModuleReadJSON } = process.binding('fs'); +const { internalBinding } = require('internal/test/binding'); +const { internalModuleReadJSON } = internalBinding('fs'); const { readFileSync } = require('fs'); const { strictEqual } = require('assert'); diff --git a/test/parallel/test-process-binding-internalbinding-whitelist.js b/test/parallel/test-process-binding-internalbinding-whitelist.js index 9cd12b54ea0f8a..3eb87e2f0fac7c 100644 --- a/test/parallel/test-process-binding-internalbinding-whitelist.js +++ b/test/parallel/test-process-binding-internalbinding-whitelist.js @@ -16,3 +16,4 @@ assert(process.binding('url')); assert(process.binding('spawn_sync')); assert(process.binding('js_stream')); assert(process.binding('buffer')); +assert(process.binding('fs')); diff --git a/test/sequential/test-async-wrap-getasyncid.js b/test/sequential/test-async-wrap-getasyncid.js index 3a59ade9a950a4..2ad602e482df57 100644 --- a/test/sequential/test-async-wrap-getasyncid.js +++ b/test/sequential/test-async-wrap-getasyncid.js @@ -132,7 +132,7 @@ if (common.hasCrypto) { // eslint-disable-line node-core/crypto-check { - const binding = process.binding('fs'); + const binding = internalBinding('fs'); const path = require('path'); const FSReqCallback = binding.FSReqCallback;