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

Revert check for fs.realpath.native (#887) #953

Merged
merged 6 commits into from Apr 16, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
66 changes: 65 additions & 1 deletion lib/fs/__tests__/realpath.test.js
@@ -1,11 +1,75 @@
'use strict'

const fse = require('../..')
const fs = require('fs')
const path = require('path')
const assert = require('assert')

/* eslint-env mocha */

describe('realpath.native does not exist', () => {
let warning
const originalListener = process.listeners('warning')[process.listeners('warning').length - 1]
const warningListener = error => {
if (error.name === 'Warning') {
if (error.code.startsWith('fs-extra-WARN0003')) {
warning = error
return
}
}
originalListener(error)
}

const realpathNativeBackup = fs.realpath.native
const clearFseCache = () => {
const fsePath = path.dirname(require.resolve('../../..'))
for (const entry in require.cache) {
if (entry.startsWith(fsePath)) {
delete require.cache[entry]
}
}
}

before(() => {
process.off('warning', originalListener)
lamweili marked this conversation as resolved.
Show resolved Hide resolved
process.on('warning', warningListener)

// clear existing require.cache
clearFseCache()

// mock stub
delete fs.realpath.native
})

after(() => {
process.off('warning', warningListener)
process.on('warning', originalListener)

// clear stubbed require.cache
clearFseCache()

// reinstate
fs.realpath.native = realpathNativeBackup
require('../..')
lamweili marked this conversation as resolved.
Show resolved Hide resolved
})

it('fse fallback to realpath internally', () => {
const fse = require(require.resolve('../..'))
lamweili marked this conversation as resolved.
Show resolved Hide resolved

// next event loop to allow event emitter/listener to happen
setImmediate(() => {
assert(warning, 'fs-extra-WARN0003 should be emitted')
lamweili marked this conversation as resolved.
Show resolved Hide resolved
})

fse.realpath.native(__dirname, (err, path) => {
assert.ifError(err)
assert.strictEqual(path, __dirname)
})
})
})

describe('realpath.native', () => {
const fse = require('../..')

it('works with callbacks', () => {
fse.realpath.native(__dirname, (err, path) => {
assert.ifError(err)
Expand Down
23 changes: 22 additions & 1 deletion lib/fs/index.js
Expand Up @@ -54,7 +54,6 @@ Object.assign(exports, fs)
api.forEach(method => {
exports[method] = u(fs[method])
})
exports.realpath.native = u(fs.realpath.native)

// We differ from mz/fs in that we still ship the old, broken, fs.exists()
// since we are a drop-in replacement for the native module
Expand Down Expand Up @@ -117,3 +116,25 @@ if (typeof fs.writev === 'function') {
})
}
}

// fs.realpath.native only available in Node v9.2+
lamweili marked this conversation as resolved.
Show resolved Hide resolved
if (typeof fs.realpath.native === 'function') {
exports.realpath.native = u(fs.realpath.native)
} else {
// fallback to a similar method signature that is present before Node v9.2
exports.realpath.native = u(fs.realpath)

// emit warning that fs.realpath.native is not a function (or undefined)
const nodeVer = process.version.match(/(\d+)\.(\d+)\.(\d+)/)
lamweili marked this conversation as resolved.
Show resolved Hide resolved
const [major, minor] = nodeVer.slice(1).map(_ => parseInt(_))
process.emitWarning(
`fs.realpath.native is not a function: ${fs.realpath.native}.\n` +
'\tfs.realpath.native is now redirected to fs.realpath as a fallback measure.\n\n' +
(
(major <= 9 || (major === 9 && minor < 2))
? `\tEnsure that NodeJS is at least v9.2.0. Detected: ${process.version}.`
: '\tEnsure that fs-extra is imported before other dependencies that can change fs.\n\n' +
'\tsee https://github.com/jprichardson/node-fs-extra/pull/953'
),
'Warning', 'fs-extra-WARN0003')
}