create-byan-agent 2.11.0 → 2.11.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (58) hide show
  1. package/install/templates/_byan/mcp/byan-mcp-server/server.js +122 -9
  2. package/node_modules/byan-platform-config/README.md +107 -0
  3. package/node_modules/byan-platform-config/index.js +16 -0
  4. package/node_modules/byan-platform-config/lib/env-config.js +128 -0
  5. package/node_modules/byan-platform-config/lib/mcp-config.js +95 -0
  6. package/node_modules/byan-platform-config/lib/token-prompt.js +61 -0
  7. package/node_modules/byan-platform-config/lib/url-utils.js +27 -0
  8. package/node_modules/byan-platform-config/lib/validate.js +44 -0
  9. package/node_modules/byan-platform-config/package.json +42 -0
  10. package/node_modules/fs-extra/LICENSE +15 -0
  11. package/node_modules/fs-extra/README.md +294 -0
  12. package/node_modules/fs-extra/lib/copy/copy-sync.js +176 -0
  13. package/node_modules/fs-extra/lib/copy/copy.js +180 -0
  14. package/node_modules/fs-extra/lib/copy/index.js +7 -0
  15. package/node_modules/fs-extra/lib/empty/index.js +39 -0
  16. package/node_modules/fs-extra/lib/ensure/file.js +66 -0
  17. package/node_modules/fs-extra/lib/ensure/index.js +23 -0
  18. package/node_modules/fs-extra/lib/ensure/link.js +64 -0
  19. package/node_modules/fs-extra/lib/ensure/symlink-paths.js +101 -0
  20. package/node_modules/fs-extra/lib/ensure/symlink-type.js +34 -0
  21. package/node_modules/fs-extra/lib/ensure/symlink.js +92 -0
  22. package/node_modules/fs-extra/lib/esm.mjs +68 -0
  23. package/node_modules/fs-extra/lib/fs/index.js +146 -0
  24. package/node_modules/fs-extra/lib/index.js +16 -0
  25. package/node_modules/fs-extra/lib/json/index.js +16 -0
  26. package/node_modules/fs-extra/lib/json/jsonfile.js +11 -0
  27. package/node_modules/fs-extra/lib/json/output-json-sync.js +12 -0
  28. package/node_modules/fs-extra/lib/json/output-json.js +12 -0
  29. package/node_modules/fs-extra/lib/mkdirs/index.js +14 -0
  30. package/node_modules/fs-extra/lib/mkdirs/make-dir.js +27 -0
  31. package/node_modules/fs-extra/lib/mkdirs/utils.js +21 -0
  32. package/node_modules/fs-extra/lib/move/index.js +7 -0
  33. package/node_modules/fs-extra/lib/move/move-sync.js +55 -0
  34. package/node_modules/fs-extra/lib/move/move.js +59 -0
  35. package/node_modules/fs-extra/lib/output-file/index.js +31 -0
  36. package/node_modules/fs-extra/lib/path-exists/index.js +12 -0
  37. package/node_modules/fs-extra/lib/remove/index.js +17 -0
  38. package/node_modules/fs-extra/lib/util/async.js +29 -0
  39. package/node_modules/fs-extra/lib/util/stat.js +159 -0
  40. package/node_modules/fs-extra/lib/util/utimes.js +36 -0
  41. package/node_modules/fs-extra/package.json +71 -0
  42. package/node_modules/graceful-fs/LICENSE +15 -0
  43. package/node_modules/graceful-fs/README.md +143 -0
  44. package/node_modules/graceful-fs/clone.js +23 -0
  45. package/node_modules/graceful-fs/graceful-fs.js +448 -0
  46. package/node_modules/graceful-fs/legacy-streams.js +118 -0
  47. package/node_modules/graceful-fs/package.json +53 -0
  48. package/node_modules/graceful-fs/polyfills.js +355 -0
  49. package/node_modules/jsonfile/LICENSE +15 -0
  50. package/node_modules/jsonfile/README.md +230 -0
  51. package/node_modules/jsonfile/index.js +88 -0
  52. package/node_modules/jsonfile/package.json +40 -0
  53. package/node_modules/jsonfile/utils.js +18 -0
  54. package/node_modules/universalify/LICENSE +20 -0
  55. package/node_modules/universalify/README.md +76 -0
  56. package/node_modules/universalify/index.js +24 -0
  57. package/node_modules/universalify/package.json +34 -0
  58. package/package.json +4 -1
@@ -0,0 +1,146 @@
1
+ 'use strict'
2
+ // This is adapted from https://github.com/normalize/mz
3
+ // Copyright (c) 2014-2016 Jonathan Ong me@jongleberry.com and Contributors
4
+ const u = require('universalify').fromCallback
5
+ const fs = require('graceful-fs')
6
+
7
+ const api = [
8
+ 'access',
9
+ 'appendFile',
10
+ 'chmod',
11
+ 'chown',
12
+ 'close',
13
+ 'copyFile',
14
+ 'cp',
15
+ 'fchmod',
16
+ 'fchown',
17
+ 'fdatasync',
18
+ 'fstat',
19
+ 'fsync',
20
+ 'ftruncate',
21
+ 'futimes',
22
+ 'glob',
23
+ 'lchmod',
24
+ 'lchown',
25
+ 'lutimes',
26
+ 'link',
27
+ 'lstat',
28
+ 'mkdir',
29
+ 'mkdtemp',
30
+ 'open',
31
+ 'opendir',
32
+ 'readdir',
33
+ 'readFile',
34
+ 'readlink',
35
+ 'realpath',
36
+ 'rename',
37
+ 'rm',
38
+ 'rmdir',
39
+ 'stat',
40
+ 'statfs',
41
+ 'symlink',
42
+ 'truncate',
43
+ 'unlink',
44
+ 'utimes',
45
+ 'writeFile'
46
+ ].filter(key => {
47
+ // Some commands are not available on some systems. Ex:
48
+ // fs.cp was added in Node.js v16.7.0
49
+ // fs.statfs was added in Node v19.6.0, v18.15.0
50
+ // fs.glob was added in Node.js v22.0.0
51
+ // fs.lchown is not available on at least some Linux
52
+ return typeof fs[key] === 'function'
53
+ })
54
+
55
+ // Export cloned fs:
56
+ Object.assign(exports, fs)
57
+
58
+ // Universalify async methods:
59
+ api.forEach(method => {
60
+ exports[method] = u(fs[method])
61
+ })
62
+
63
+ // We differ from mz/fs in that we still ship the old, broken, fs.exists()
64
+ // since we are a drop-in replacement for the native module
65
+ exports.exists = function (filename, callback) {
66
+ if (typeof callback === 'function') {
67
+ return fs.exists(filename, callback)
68
+ }
69
+ return new Promise(resolve => {
70
+ return fs.exists(filename, resolve)
71
+ })
72
+ }
73
+
74
+ // fs.read(), fs.write(), fs.readv(), & fs.writev() need special treatment due to multiple callback args
75
+
76
+ exports.read = function (fd, buffer, offset, length, position, callback) {
77
+ if (typeof callback === 'function') {
78
+ return fs.read(fd, buffer, offset, length, position, callback)
79
+ }
80
+ return new Promise((resolve, reject) => {
81
+ fs.read(fd, buffer, offset, length, position, (err, bytesRead, buffer) => {
82
+ if (err) return reject(err)
83
+ resolve({ bytesRead, buffer })
84
+ })
85
+ })
86
+ }
87
+
88
+ // Function signature can be
89
+ // fs.write(fd, buffer[, offset[, length[, position]]], callback)
90
+ // OR
91
+ // fs.write(fd, string[, position[, encoding]], callback)
92
+ // We need to handle both cases, so we use ...args
93
+ exports.write = function (fd, buffer, ...args) {
94
+ if (typeof args[args.length - 1] === 'function') {
95
+ return fs.write(fd, buffer, ...args)
96
+ }
97
+
98
+ return new Promise((resolve, reject) => {
99
+ fs.write(fd, buffer, ...args, (err, bytesWritten, buffer) => {
100
+ if (err) return reject(err)
101
+ resolve({ bytesWritten, buffer })
102
+ })
103
+ })
104
+ }
105
+
106
+ // Function signature is
107
+ // s.readv(fd, buffers[, position], callback)
108
+ // We need to handle the optional arg, so we use ...args
109
+ exports.readv = function (fd, buffers, ...args) {
110
+ if (typeof args[args.length - 1] === 'function') {
111
+ return fs.readv(fd, buffers, ...args)
112
+ }
113
+
114
+ return new Promise((resolve, reject) => {
115
+ fs.readv(fd, buffers, ...args, (err, bytesRead, buffers) => {
116
+ if (err) return reject(err)
117
+ resolve({ bytesRead, buffers })
118
+ })
119
+ })
120
+ }
121
+
122
+ // Function signature is
123
+ // s.writev(fd, buffers[, position], callback)
124
+ // We need to handle the optional arg, so we use ...args
125
+ exports.writev = function (fd, buffers, ...args) {
126
+ if (typeof args[args.length - 1] === 'function') {
127
+ return fs.writev(fd, buffers, ...args)
128
+ }
129
+
130
+ return new Promise((resolve, reject) => {
131
+ fs.writev(fd, buffers, ...args, (err, bytesWritten, buffers) => {
132
+ if (err) return reject(err)
133
+ resolve({ bytesWritten, buffers })
134
+ })
135
+ })
136
+ }
137
+
138
+ // fs.realpath.native sometimes not available if fs is monkey-patched
139
+ if (typeof fs.realpath.native === 'function') {
140
+ exports.realpath.native = u(fs.realpath.native)
141
+ } else {
142
+ process.emitWarning(
143
+ 'fs.realpath.native is not a function. Is fs being monkey-patched?',
144
+ 'Warning', 'fs-extra-WARN0003'
145
+ )
146
+ }
@@ -0,0 +1,16 @@
1
+ 'use strict'
2
+
3
+ module.exports = {
4
+ // Export promiseified graceful-fs:
5
+ ...require('./fs'),
6
+ // Export extra methods:
7
+ ...require('./copy'),
8
+ ...require('./empty'),
9
+ ...require('./ensure'),
10
+ ...require('./json'),
11
+ ...require('./mkdirs'),
12
+ ...require('./move'),
13
+ ...require('./output-file'),
14
+ ...require('./path-exists'),
15
+ ...require('./remove')
16
+ }
@@ -0,0 +1,16 @@
1
+ 'use strict'
2
+
3
+ const u = require('universalify').fromPromise
4
+ const jsonFile = require('./jsonfile')
5
+
6
+ jsonFile.outputJson = u(require('./output-json'))
7
+ jsonFile.outputJsonSync = require('./output-json-sync')
8
+ // aliases
9
+ jsonFile.outputJSON = jsonFile.outputJson
10
+ jsonFile.outputJSONSync = jsonFile.outputJsonSync
11
+ jsonFile.writeJSON = jsonFile.writeJson
12
+ jsonFile.writeJSONSync = jsonFile.writeJsonSync
13
+ jsonFile.readJSON = jsonFile.readJson
14
+ jsonFile.readJSONSync = jsonFile.readJsonSync
15
+
16
+ module.exports = jsonFile
@@ -0,0 +1,11 @@
1
+ 'use strict'
2
+
3
+ const jsonFile = require('jsonfile')
4
+
5
+ module.exports = {
6
+ // jsonfile exports
7
+ readJson: jsonFile.readFile,
8
+ readJsonSync: jsonFile.readFileSync,
9
+ writeJson: jsonFile.writeFile,
10
+ writeJsonSync: jsonFile.writeFileSync
11
+ }
@@ -0,0 +1,12 @@
1
+ 'use strict'
2
+
3
+ const { stringify } = require('jsonfile/utils')
4
+ const { outputFileSync } = require('../output-file')
5
+
6
+ function outputJsonSync (file, data, options) {
7
+ const str = stringify(data, options)
8
+
9
+ outputFileSync(file, str, options)
10
+ }
11
+
12
+ module.exports = outputJsonSync
@@ -0,0 +1,12 @@
1
+ 'use strict'
2
+
3
+ const { stringify } = require('jsonfile/utils')
4
+ const { outputFile } = require('../output-file')
5
+
6
+ async function outputJson (file, data, options = {}) {
7
+ const str = stringify(data, options)
8
+
9
+ await outputFile(file, str, options)
10
+ }
11
+
12
+ module.exports = outputJson
@@ -0,0 +1,14 @@
1
+ 'use strict'
2
+ const u = require('universalify').fromPromise
3
+ const { makeDir: _makeDir, makeDirSync } = require('./make-dir')
4
+ const makeDir = u(_makeDir)
5
+
6
+ module.exports = {
7
+ mkdirs: makeDir,
8
+ mkdirsSync: makeDirSync,
9
+ // alias
10
+ mkdirp: makeDir,
11
+ mkdirpSync: makeDirSync,
12
+ ensureDir: makeDir,
13
+ ensureDirSync: makeDirSync
14
+ }
@@ -0,0 +1,27 @@
1
+ 'use strict'
2
+ const fs = require('../fs')
3
+ const { checkPath } = require('./utils')
4
+
5
+ const getMode = options => {
6
+ const defaults = { mode: 0o777 }
7
+ if (typeof options === 'number') return options
8
+ return ({ ...defaults, ...options }).mode
9
+ }
10
+
11
+ module.exports.makeDir = async (dir, options) => {
12
+ checkPath(dir)
13
+
14
+ return fs.mkdir(dir, {
15
+ mode: getMode(options),
16
+ recursive: true
17
+ })
18
+ }
19
+
20
+ module.exports.makeDirSync = (dir, options) => {
21
+ checkPath(dir)
22
+
23
+ return fs.mkdirSync(dir, {
24
+ mode: getMode(options),
25
+ recursive: true
26
+ })
27
+ }
@@ -0,0 +1,21 @@
1
+ // Adapted from https://github.com/sindresorhus/make-dir
2
+ // Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
3
+ // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+ // The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
5
+ // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
6
+ 'use strict'
7
+ const path = require('path')
8
+
9
+ // https://github.com/nodejs/node/issues/8987
10
+ // https://github.com/libuv/libuv/pull/1088
11
+ module.exports.checkPath = function checkPath (pth) {
12
+ if (process.platform === 'win32') {
13
+ const pathHasInvalidWinCharacters = /[<>:"|?*]/.test(pth.replace(path.parse(pth).root, ''))
14
+
15
+ if (pathHasInvalidWinCharacters) {
16
+ const error = new Error(`Path contains invalid characters: ${pth}`)
17
+ error.code = 'EINVAL'
18
+ throw error
19
+ }
20
+ }
21
+ }
@@ -0,0 +1,7 @@
1
+ 'use strict'
2
+
3
+ const u = require('universalify').fromPromise
4
+ module.exports = {
5
+ move: u(require('./move')),
6
+ moveSync: require('./move-sync')
7
+ }
@@ -0,0 +1,55 @@
1
+ 'use strict'
2
+
3
+ const fs = require('graceful-fs')
4
+ const path = require('path')
5
+ const copySync = require('../copy').copySync
6
+ const removeSync = require('../remove').removeSync
7
+ const mkdirpSync = require('../mkdirs').mkdirpSync
8
+ const stat = require('../util/stat')
9
+
10
+ function moveSync (src, dest, opts) {
11
+ opts = opts || {}
12
+ const overwrite = opts.overwrite || opts.clobber || false
13
+
14
+ const { srcStat, isChangingCase = false } = stat.checkPathsSync(src, dest, 'move', opts)
15
+ stat.checkParentPathsSync(src, srcStat, dest, 'move')
16
+ if (!isParentRoot(dest)) mkdirpSync(path.dirname(dest))
17
+ return doRename(src, dest, overwrite, isChangingCase)
18
+ }
19
+
20
+ function isParentRoot (dest) {
21
+ const parent = path.dirname(dest)
22
+ const parsedPath = path.parse(parent)
23
+ return parsedPath.root === parent
24
+ }
25
+
26
+ function doRename (src, dest, overwrite, isChangingCase) {
27
+ if (isChangingCase) return rename(src, dest, overwrite)
28
+ if (overwrite) {
29
+ removeSync(dest)
30
+ return rename(src, dest, overwrite)
31
+ }
32
+ if (fs.existsSync(dest)) throw new Error('dest already exists.')
33
+ return rename(src, dest, overwrite)
34
+ }
35
+
36
+ function rename (src, dest, overwrite) {
37
+ try {
38
+ fs.renameSync(src, dest)
39
+ } catch (err) {
40
+ if (err.code !== 'EXDEV') throw err
41
+ return moveAcrossDevice(src, dest, overwrite)
42
+ }
43
+ }
44
+
45
+ function moveAcrossDevice (src, dest, overwrite) {
46
+ const opts = {
47
+ overwrite,
48
+ errorOnExist: true,
49
+ preserveTimestamps: true
50
+ }
51
+ copySync(src, dest, opts)
52
+ return removeSync(src)
53
+ }
54
+
55
+ module.exports = moveSync
@@ -0,0 +1,59 @@
1
+ 'use strict'
2
+
3
+ const fs = require('../fs')
4
+ const path = require('path')
5
+ const { copy } = require('../copy')
6
+ const { remove } = require('../remove')
7
+ const { mkdirp } = require('../mkdirs')
8
+ const { pathExists } = require('../path-exists')
9
+ const stat = require('../util/stat')
10
+
11
+ async function move (src, dest, opts = {}) {
12
+ const overwrite = opts.overwrite || opts.clobber || false
13
+
14
+ const { srcStat, isChangingCase = false } = await stat.checkPaths(src, dest, 'move', opts)
15
+
16
+ await stat.checkParentPaths(src, srcStat, dest, 'move')
17
+
18
+ // If the parent of dest is not root, make sure it exists before proceeding
19
+ const destParent = path.dirname(dest)
20
+ const parsedParentPath = path.parse(destParent)
21
+ if (parsedParentPath.root !== destParent) {
22
+ await mkdirp(destParent)
23
+ }
24
+
25
+ return doRename(src, dest, overwrite, isChangingCase)
26
+ }
27
+
28
+ async function doRename (src, dest, overwrite, isChangingCase) {
29
+ if (!isChangingCase) {
30
+ if (overwrite) {
31
+ await remove(dest)
32
+ } else if (await pathExists(dest)) {
33
+ throw new Error('dest already exists.')
34
+ }
35
+ }
36
+
37
+ try {
38
+ // Try w/ rename first, and try copy + remove if EXDEV
39
+ await fs.rename(src, dest)
40
+ } catch (err) {
41
+ if (err.code !== 'EXDEV') {
42
+ throw err
43
+ }
44
+ await moveAcrossDevice(src, dest, overwrite)
45
+ }
46
+ }
47
+
48
+ async function moveAcrossDevice (src, dest, overwrite) {
49
+ const opts = {
50
+ overwrite,
51
+ errorOnExist: true,
52
+ preserveTimestamps: true
53
+ }
54
+
55
+ await copy(src, dest, opts)
56
+ return remove(src)
57
+ }
58
+
59
+ module.exports = move
@@ -0,0 +1,31 @@
1
+ 'use strict'
2
+
3
+ const u = require('universalify').fromPromise
4
+ const fs = require('../fs')
5
+ const path = require('path')
6
+ const mkdir = require('../mkdirs')
7
+ const pathExists = require('../path-exists').pathExists
8
+
9
+ async function outputFile (file, data, encoding = 'utf-8') {
10
+ const dir = path.dirname(file)
11
+
12
+ if (!(await pathExists(dir))) {
13
+ await mkdir.mkdirs(dir)
14
+ }
15
+
16
+ return fs.writeFile(file, data, encoding)
17
+ }
18
+
19
+ function outputFileSync (file, ...args) {
20
+ const dir = path.dirname(file)
21
+ if (!fs.existsSync(dir)) {
22
+ mkdir.mkdirsSync(dir)
23
+ }
24
+
25
+ fs.writeFileSync(file, ...args)
26
+ }
27
+
28
+ module.exports = {
29
+ outputFile: u(outputFile),
30
+ outputFileSync
31
+ }
@@ -0,0 +1,12 @@
1
+ 'use strict'
2
+ const u = require('universalify').fromPromise
3
+ const fs = require('../fs')
4
+
5
+ function pathExists (path) {
6
+ return fs.access(path).then(() => true).catch(() => false)
7
+ }
8
+
9
+ module.exports = {
10
+ pathExists: u(pathExists),
11
+ pathExistsSync: fs.existsSync
12
+ }
@@ -0,0 +1,17 @@
1
+ 'use strict'
2
+
3
+ const fs = require('graceful-fs')
4
+ const u = require('universalify').fromCallback
5
+
6
+ function remove (path, callback) {
7
+ fs.rm(path, { recursive: true, force: true }, callback)
8
+ }
9
+
10
+ function removeSync (path) {
11
+ fs.rmSync(path, { recursive: true, force: true })
12
+ }
13
+
14
+ module.exports = {
15
+ remove: u(remove),
16
+ removeSync
17
+ }
@@ -0,0 +1,29 @@
1
+ 'use strict'
2
+
3
+ // https://github.com/jprichardson/node-fs-extra/issues/1056
4
+ // Performing parallel operations on each item of an async iterator is
5
+ // surprisingly hard; you need to have handlers in place to avoid getting an
6
+ // UnhandledPromiseRejectionWarning.
7
+ // NOTE: This function does not presently handle return values, only errors
8
+ async function asyncIteratorConcurrentProcess (iterator, fn) {
9
+ const promises = []
10
+ for await (const item of iterator) {
11
+ promises.push(
12
+ fn(item).then(
13
+ () => null,
14
+ (err) => err ?? new Error('unknown error')
15
+ )
16
+ )
17
+ }
18
+ await Promise.all(
19
+ promises.map((promise) =>
20
+ promise.then((possibleErr) => {
21
+ if (possibleErr !== null) throw possibleErr
22
+ })
23
+ )
24
+ )
25
+ }
26
+
27
+ module.exports = {
28
+ asyncIteratorConcurrentProcess
29
+ }
@@ -0,0 +1,159 @@
1
+ 'use strict'
2
+
3
+ const fs = require('../fs')
4
+ const path = require('path')
5
+ const u = require('universalify').fromPromise
6
+
7
+ function getStats (src, dest, opts) {
8
+ const statFunc = opts.dereference
9
+ ? (file) => fs.stat(file, { bigint: true })
10
+ : (file) => fs.lstat(file, { bigint: true })
11
+ return Promise.all([
12
+ statFunc(src),
13
+ statFunc(dest).catch(err => {
14
+ if (err.code === 'ENOENT') return null
15
+ throw err
16
+ })
17
+ ]).then(([srcStat, destStat]) => ({ srcStat, destStat }))
18
+ }
19
+
20
+ function getStatsSync (src, dest, opts) {
21
+ let destStat
22
+ const statFunc = opts.dereference
23
+ ? (file) => fs.statSync(file, { bigint: true })
24
+ : (file) => fs.lstatSync(file, { bigint: true })
25
+ const srcStat = statFunc(src)
26
+ try {
27
+ destStat = statFunc(dest)
28
+ } catch (err) {
29
+ if (err.code === 'ENOENT') return { srcStat, destStat: null }
30
+ throw err
31
+ }
32
+ return { srcStat, destStat }
33
+ }
34
+
35
+ async function checkPaths (src, dest, funcName, opts) {
36
+ const { srcStat, destStat } = await getStats(src, dest, opts)
37
+ if (destStat) {
38
+ if (areIdentical(srcStat, destStat)) {
39
+ const srcBaseName = path.basename(src)
40
+ const destBaseName = path.basename(dest)
41
+ if (funcName === 'move' &&
42
+ srcBaseName !== destBaseName &&
43
+ srcBaseName.toLowerCase() === destBaseName.toLowerCase()) {
44
+ return { srcStat, destStat, isChangingCase: true }
45
+ }
46
+ throw new Error('Source and destination must not be the same.')
47
+ }
48
+ if (srcStat.isDirectory() && !destStat.isDirectory()) {
49
+ throw new Error(`Cannot overwrite non-directory '${dest}' with directory '${src}'.`)
50
+ }
51
+ if (!srcStat.isDirectory() && destStat.isDirectory()) {
52
+ throw new Error(`Cannot overwrite directory '${dest}' with non-directory '${src}'.`)
53
+ }
54
+ }
55
+
56
+ if (srcStat.isDirectory() && isSrcSubdir(src, dest)) {
57
+ throw new Error(errMsg(src, dest, funcName))
58
+ }
59
+
60
+ return { srcStat, destStat }
61
+ }
62
+
63
+ function checkPathsSync (src, dest, funcName, opts) {
64
+ const { srcStat, destStat } = getStatsSync(src, dest, opts)
65
+
66
+ if (destStat) {
67
+ if (areIdentical(srcStat, destStat)) {
68
+ const srcBaseName = path.basename(src)
69
+ const destBaseName = path.basename(dest)
70
+ if (funcName === 'move' &&
71
+ srcBaseName !== destBaseName &&
72
+ srcBaseName.toLowerCase() === destBaseName.toLowerCase()) {
73
+ return { srcStat, destStat, isChangingCase: true }
74
+ }
75
+ throw new Error('Source and destination must not be the same.')
76
+ }
77
+ if (srcStat.isDirectory() && !destStat.isDirectory()) {
78
+ throw new Error(`Cannot overwrite non-directory '${dest}' with directory '${src}'.`)
79
+ }
80
+ if (!srcStat.isDirectory() && destStat.isDirectory()) {
81
+ throw new Error(`Cannot overwrite directory '${dest}' with non-directory '${src}'.`)
82
+ }
83
+ }
84
+
85
+ if (srcStat.isDirectory() && isSrcSubdir(src, dest)) {
86
+ throw new Error(errMsg(src, dest, funcName))
87
+ }
88
+ return { srcStat, destStat }
89
+ }
90
+
91
+ // recursively check if dest parent is a subdirectory of src.
92
+ // It works for all file types including symlinks since it
93
+ // checks the src and dest inodes. It starts from the deepest
94
+ // parent and stops once it reaches the src parent or the root path.
95
+ async function checkParentPaths (src, srcStat, dest, funcName) {
96
+ const srcParent = path.resolve(path.dirname(src))
97
+ const destParent = path.resolve(path.dirname(dest))
98
+ if (destParent === srcParent || destParent === path.parse(destParent).root) return
99
+
100
+ let destStat
101
+ try {
102
+ destStat = await fs.stat(destParent, { bigint: true })
103
+ } catch (err) {
104
+ if (err.code === 'ENOENT') return
105
+ throw err
106
+ }
107
+
108
+ if (areIdentical(srcStat, destStat)) {
109
+ throw new Error(errMsg(src, dest, funcName))
110
+ }
111
+
112
+ return checkParentPaths(src, srcStat, destParent, funcName)
113
+ }
114
+
115
+ function checkParentPathsSync (src, srcStat, dest, funcName) {
116
+ const srcParent = path.resolve(path.dirname(src))
117
+ const destParent = path.resolve(path.dirname(dest))
118
+ if (destParent === srcParent || destParent === path.parse(destParent).root) return
119
+ let destStat
120
+ try {
121
+ destStat = fs.statSync(destParent, { bigint: true })
122
+ } catch (err) {
123
+ if (err.code === 'ENOENT') return
124
+ throw err
125
+ }
126
+ if (areIdentical(srcStat, destStat)) {
127
+ throw new Error(errMsg(src, dest, funcName))
128
+ }
129
+ return checkParentPathsSync(src, srcStat, destParent, funcName)
130
+ }
131
+
132
+ function areIdentical (srcStat, destStat) {
133
+ // stat.dev can be 0n on windows when node version >= 22.x.x
134
+ return destStat.ino !== undefined && destStat.dev !== undefined && destStat.ino === srcStat.ino && destStat.dev === srcStat.dev
135
+ }
136
+
137
+ // return true if dest is a subdir of src, otherwise false.
138
+ // It only checks the path strings.
139
+ function isSrcSubdir (src, dest) {
140
+ const srcArr = path.resolve(src).split(path.sep).filter(i => i)
141
+ const destArr = path.resolve(dest).split(path.sep).filter(i => i)
142
+ return srcArr.every((cur, i) => destArr[i] === cur)
143
+ }
144
+
145
+ function errMsg (src, dest, funcName) {
146
+ return `Cannot ${funcName} '${src}' to a subdirectory of itself, '${dest}'.`
147
+ }
148
+
149
+ module.exports = {
150
+ // checkPaths
151
+ checkPaths: u(checkPaths),
152
+ checkPathsSync,
153
+ // checkParent
154
+ checkParentPaths: u(checkParentPaths),
155
+ checkParentPathsSync,
156
+ // Misc
157
+ isSrcSubdir,
158
+ areIdentical
159
+ }