Skip to content

Commit 588d770

Browse files
authored
BREAKING: Use bigint everywhere (#895)
Refs #886
1 parent c551140 commit 588d770

File tree

2 files changed

+13
-49
lines changed

2 files changed

+13
-49
lines changed

lib/util/__tests__/stat.test.js

+3-15
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,8 @@ const fs = require(process.cwd())
44
const os = require('os')
55
const path = require('path')
66
const assert = require('assert')
7-
const atLeastNode = require('at-least-node')
87
const stat = require('../stat.js')
98

10-
const NODE_VERSION_WITH_BIGINT = '10.5.0'
11-
129
/* global beforeEach, afterEach, describe, it */
1310

1411
describe('util/stat', () => {
@@ -21,20 +18,15 @@ describe('util/stat', () => {
2118

2219
afterEach(done => fs.remove(TEST_DIR, done))
2320

24-
describe('should use stats with bigint type for node versions >= 10.5.0 and number type for older versions', () => {
21+
describe('should use stats with bigint type', () => {
2522
it('stat.checkPaths()', () => {
2623
const src = path.join(TEST_DIR, 'src')
2724
const dest = path.join(TEST_DIR, 'dest')
2825
fs.ensureFileSync(src)
2926
fs.ensureFileSync(dest)
3027
stat.checkPaths(src, dest, 'copy', {}, (err, stats) => {
3128
assert.ifError(err)
32-
const { srcStat } = stats
33-
if (atLeastNode(NODE_VERSION_WITH_BIGINT)) {
34-
assert.strictEqual(typeof srcStat.ino, 'bigint')
35-
} else {
36-
assert.strictEqual(typeof srcStat.ino, 'number')
37-
}
29+
assert.strictEqual(typeof stats.srcStat.ino, 'bigint')
3830
})
3931
})
4032

@@ -44,11 +36,7 @@ describe('util/stat', () => {
4436
fs.ensureFileSync(src)
4537
fs.ensureFileSync(dest)
4638
const { srcStat } = stat.checkPathsSync(src, dest, 'copy', {})
47-
if (atLeastNode(NODE_VERSION_WITH_BIGINT)) {
48-
assert.strictEqual(typeof srcStat.ino, 'bigint')
49-
} else {
50-
assert.strictEqual(typeof srcStat.ino, 'number')
51-
}
39+
assert.strictEqual(typeof srcStat.ino, 'bigint')
5240
})
5341
})
5442

lib/util/stat.js

+10-34
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,11 @@
33
const fs = require('../fs')
44
const path = require('path')
55
const util = require('util')
6-
const atLeastNode = require('at-least-node')
7-
8-
const nodeSupportsBigInt = atLeastNode('10.5.0')
9-
const stat = (file) => nodeSupportsBigInt ? fs.stat(file, { bigint: true }) : fs.stat(file)
10-
const lstat = (file) => nodeSupportsBigInt ? fs.lstat(file, { bigint: true }) : fs.lstat(file)
11-
const statSync = (file) => nodeSupportsBigInt ? fs.statSync(file, { bigint: true }) : fs.statSync(file)
12-
const lstatSync = (file) => nodeSupportsBigInt ? fs.lstatSync(file, { bigint: true }) : fs.lstatSync(file)
136

147
function getStats (src, dest, opts) {
15-
const statFunc = opts.dereference ? stat : lstat
8+
const statFunc = opts.dereference
9+
? (file) => fs.stat(file, { bigint: true })
10+
: (file) => fs.lstat(file, { bigint: true })
1611
return Promise.all([
1712
statFunc(src),
1813
statFunc(dest).catch(err => {
@@ -24,7 +19,9 @@ function getStats (src, dest, opts) {
2419

2520
function getStatsSync (src, dest, opts) {
2621
let destStat
27-
const statFunc = opts.dereference ? statSync : lstatSync
22+
const statFunc = opts.dereference
23+
? (file) => fs.statSync(file, { bigint: true })
24+
: (file) => fs.lstatSync(file, { bigint: true })
2825
const srcStat = statFunc(src)
2926
try {
3027
destStat = statFunc(dest)
@@ -102,7 +99,7 @@ function checkParentPaths (src, srcStat, dest, funcName, cb) {
10299
const srcParent = path.resolve(path.dirname(src))
103100
const destParent = path.resolve(path.dirname(dest))
104101
if (destParent === srcParent || destParent === path.parse(destParent).root) return cb()
105-
const callback = (err, destStat) => {
102+
fs.stat(destParent, { bigint: true }, (err, destStat) => {
106103
if (err) {
107104
if (err.code === 'ENOENT') return cb()
108105
return cb(err)
@@ -111,9 +108,7 @@ function checkParentPaths (src, srcStat, dest, funcName, cb) {
111108
return cb(new Error(errMsg(src, dest, funcName)))
112109
}
113110
return checkParentPaths(src, srcStat, destParent, funcName, cb)
114-
}
115-
if (nodeSupportsBigInt) fs.stat(destParent, { bigint: true }, callback)
116-
else fs.stat(destParent, callback)
111+
})
117112
}
118113

119114
function checkParentPathsSync (src, srcStat, dest, funcName) {
@@ -122,7 +117,7 @@ function checkParentPathsSync (src, srcStat, dest, funcName) {
122117
if (destParent === srcParent || destParent === path.parse(destParent).root) return
123118
let destStat
124119
try {
125-
destStat = statSync(destParent)
120+
destStat = fs.statSync(destParent, { bigint: true })
126121
} catch (err) {
127122
if (err.code === 'ENOENT') return
128123
throw err
@@ -134,26 +129,7 @@ function checkParentPathsSync (src, srcStat, dest, funcName) {
134129
}
135130

136131
function areIdentical (srcStat, destStat) {
137-
if (destStat.ino && destStat.dev && destStat.ino === srcStat.ino && destStat.dev === srcStat.dev) {
138-
if (nodeSupportsBigInt || destStat.ino < Number.MAX_SAFE_INTEGER) {
139-
// definitive answer
140-
return true
141-
}
142-
// Use additional heuristics if we can't use 'bigint'.
143-
// Different 'ino' could be represented the same if they are >= Number.MAX_SAFE_INTEGER
144-
// See issue 657
145-
if (destStat.size === srcStat.size &&
146-
destStat.mode === srcStat.mode &&
147-
destStat.nlink === srcStat.nlink &&
148-
destStat.atimeMs === srcStat.atimeMs &&
149-
destStat.mtimeMs === srcStat.mtimeMs &&
150-
destStat.ctimeMs === srcStat.ctimeMs &&
151-
destStat.birthtimeMs === srcStat.birthtimeMs) {
152-
// heuristic answer
153-
return true
154-
}
155-
}
156-
return false
132+
return destStat.ino && destStat.dev && destStat.ino === srcStat.ino && destStat.dev === srcStat.dev
157133
}
158134

159135
// return true if dest is a subdir of src, otherwise false.

0 commit comments

Comments
 (0)