Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/npm/cli.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'node_modules/@npmcli/fs/lib/common')
-rw-r--r--node_modules/@npmcli/fs/lib/common/file-url-to-path/index.js17
-rw-r--r--node_modules/@npmcli/fs/lib/common/file-url-to-path/polyfill.js120
-rw-r--r--node_modules/@npmcli/fs/lib/common/get-options.js20
-rw-r--r--node_modules/@npmcli/fs/lib/common/node.js9
-rw-r--r--node_modules/@npmcli/fs/lib/common/owner.js92
5 files changed, 258 insertions, 0 deletions
diff --git a/node_modules/@npmcli/fs/lib/common/file-url-to-path/index.js b/node_modules/@npmcli/fs/lib/common/file-url-to-path/index.js
new file mode 100644
index 000000000..7755d1c10
--- /dev/null
+++ b/node_modules/@npmcli/fs/lib/common/file-url-to-path/index.js
@@ -0,0 +1,17 @@
+const url = require('url')
+
+const node = require('../node.js')
+const polyfill = require('./polyfill.js')
+
+const useNative = node.satisfies('>=10.12.0')
+
+const fileURLToPath = (path) => {
+ // the polyfill is tested separately from this module, no need to hack
+ // process.version to try to trigger it just for coverage
+ // istanbul ignore next
+ return useNative
+ ? url.fileURLToPath(path)
+ : polyfill(path)
+}
+
+module.exports = fileURLToPath
diff --git a/node_modules/@npmcli/fs/lib/common/file-url-to-path/polyfill.js b/node_modules/@npmcli/fs/lib/common/file-url-to-path/polyfill.js
new file mode 100644
index 000000000..794d9bba4
--- /dev/null
+++ b/node_modules/@npmcli/fs/lib/common/file-url-to-path/polyfill.js
@@ -0,0 +1,120 @@
+const { URL, domainToUnicode } = require('url')
+
+const CHAR_LOWERCASE_A = 97
+const CHAR_LOWERCASE_Z = 122
+
+const isWindows = process.platform === 'win32'
+
+class ERR_INVALID_FILE_URL_HOST extends TypeError {
+ constructor (platform) {
+ super(`File URL host must be "localhost" or empty on ${platform}`)
+ this.code = 'ERR_INVALID_FILE_URL_HOST'
+ }
+
+ toString () {
+ return `${this.name} [${this.code}]: ${this.message}`
+ }
+}
+
+class ERR_INVALID_FILE_URL_PATH extends TypeError {
+ constructor (msg) {
+ super(`File URL path ${msg}`)
+ this.code = 'ERR_INVALID_FILE_URL_PATH'
+ }
+
+ toString () {
+ return `${this.name} [${this.code}]: ${this.message}`
+ }
+}
+
+class ERR_INVALID_ARG_TYPE extends TypeError {
+ constructor (name, actual) {
+ super(`The "${name}" argument must be one of type string or an instance of URL. Received type ${typeof actual} ${actual}`)
+ this.code = 'ERR_INVALID_ARG_TYPE'
+ }
+
+ toString () {
+ return `${this.name} [${this.code}]: ${this.message}`
+ }
+}
+
+class ERR_INVALID_URL_SCHEME extends TypeError {
+ constructor (expected) {
+ super(`The URL must be of scheme ${expected}`)
+ this.code = 'ERR_INVALID_URL_SCHEME'
+ }
+
+ toString () {
+ return `${this.name} [${this.code}]: ${this.message}`
+ }
+}
+
+const isURLInstance = (input) => {
+ return input != null && input.href && input.origin
+}
+
+const getPathFromURLWin32 = (url) => {
+ const hostname = url.hostname
+ let pathname = url.pathname
+ for (let n = 0; n < pathname.length; n++) {
+ if (pathname[n] === '%') {
+ const third = pathname.codePointAt(n + 2) | 0x20
+ if ((pathname[n + 1] === '2' && third === 102) ||
+ (pathname[n + 1] === '5' && third === 99)) {
+ throw new ERR_INVALID_FILE_URL_PATH('must not include encoded \\ or / characters')
+ }
+ }
+ }
+
+ pathname = pathname.replace(/\//g, '\\')
+ pathname = decodeURIComponent(pathname)
+ if (hostname !== '') {
+ return `\\\\${domainToUnicode(hostname)}${pathname}`
+ }
+
+ const letter = pathname.codePointAt(1) | 0x20
+ const sep = pathname[2]
+ if (letter < CHAR_LOWERCASE_A || letter > CHAR_LOWERCASE_Z ||
+ (sep !== ':')) {
+ throw new ERR_INVALID_FILE_URL_PATH('must be absolute')
+ }
+
+ return pathname.slice(1)
+}
+
+const getPathFromURLPosix = (url) => {
+ if (url.hostname !== '') {
+ throw new ERR_INVALID_FILE_URL_HOST(process.platform)
+ }
+
+ const pathname = url.pathname
+
+ for (let n = 0; n < pathname.length; n++) {
+ if (pathname[n] === '%') {
+ const third = pathname.codePointAt(n + 2) | 0x20
+ if (pathname[n + 1] === '2' && third === 102) {
+ throw new ERR_INVALID_FILE_URL_PATH('must not include encoded / characters')
+ }
+ }
+ }
+
+ return decodeURIComponent(pathname)
+}
+
+const fileURLToPath = (path) => {
+ if (typeof path === 'string') {
+ path = new URL(path)
+ } else if (!isURLInstance(path)) {
+ throw new ERR_INVALID_ARG_TYPE('path', ['string', 'URL'], path)
+ }
+
+ if (path.protocol !== 'file:') {
+ throw new ERR_INVALID_URL_SCHEME('file')
+ }
+
+ return isWindows
+ ? getPathFromURLWin32(path)
+ : getPathFromURLPosix(path)
+}
+
+module.exports = fileURLToPath
diff --git a/node_modules/@npmcli/fs/lib/common/get-options.js b/node_modules/@npmcli/fs/lib/common/get-options.js
new file mode 100644
index 000000000..cb5982f79
--- /dev/null
+++ b/node_modules/@npmcli/fs/lib/common/get-options.js
@@ -0,0 +1,20 @@
+// given an input that may or may not be an object, return an object that has
+// a copy of every defined property listed in 'copy'. if the input is not an
+// object, assign it to the property named by 'wrap'
+const getOptions = (input, { copy, wrap }) => {
+ const result = {}
+
+ if (input && typeof input === 'object') {
+ for (const prop of copy) {
+ if (input[prop] !== undefined) {
+ result[prop] = input[prop]
+ }
+ }
+ } else {
+ result[wrap] = input
+ }
+
+ return result
+}
+
+module.exports = getOptions
diff --git a/node_modules/@npmcli/fs/lib/common/node.js b/node_modules/@npmcli/fs/lib/common/node.js
new file mode 100644
index 000000000..4d13bc037
--- /dev/null
+++ b/node_modules/@npmcli/fs/lib/common/node.js
@@ -0,0 +1,9 @@
+const semver = require('semver')
+
+const satisfies = (range) => {
+ return semver.satisfies(process.version, range, { includePrerelease: true })
+}
+
+module.exports = {
+ satisfies,
+}
diff --git a/node_modules/@npmcli/fs/lib/common/owner.js b/node_modules/@npmcli/fs/lib/common/owner.js
new file mode 100644
index 000000000..e3468b077
--- /dev/null
+++ b/node_modules/@npmcli/fs/lib/common/owner.js
@@ -0,0 +1,92 @@
+const { dirname, resolve } = require('path')
+
+const fileURLToPath = require('./file-url-to-path/index.js')
+const fs = require('../fs.js')
+
+// given a path, find the owner of the nearest parent
+const find = async (path) => {
+ // if we have no getuid, permissions are irrelevant on this platform
+ if (!process.getuid) {
+ return {}
+ }
+
+ // fs methods accept URL objects with a scheme of file: so we need to unwrap
+ // those into an actual path string before we can resolve it
+ const resolved = path != null && path.href && path.origin
+ ? resolve(fileURLToPath(path))
+ : resolve(path)
+
+ let stat
+
+ try {
+ stat = await fs.lstat(resolved)
+ } finally {
+ // if we got a stat, return its contents
+ if (stat) {
+ return { uid: stat.uid, gid: stat.gid }
+ }
+
+ // try the parent directory
+ if (resolved !== dirname(resolved)) {
+ return find(dirname(resolved))
+ }
+
+ // no more parents, never got a stat, just return an empty object
+ return {}
+ }
+}
+
+// given a path, uid, and gid update the ownership of the path if necessary
+const update = async (path, uid, gid) => {
+ // nothing to update, just exit
+ if (uid === undefined && gid === undefined) {
+ return
+ }
+
+ try {
+ // see if the permissions are already the same, if they are we don't
+ // need to do anything, so return early
+ const stat = await fs.stat(path)
+ if (uid === stat.uid && gid === stat.gid) {
+ return
+ }
+ } catch (err) {}
+
+ try {
+ await fs.chown(path, uid, gid)
+ } catch (err) {}
+}
+
+// accepts a `path` and the `owner` property of an options object and normalizes
+// it into an object with numerical `uid` and `gid`
+const validate = async (path, input) => {
+ let uid
+ let gid
+
+ if (typeof input === 'string' || typeof input === 'number') {
+ uid = input
+ gid = input
+ } else if (input && typeof input === 'object') {
+ uid = input.uid
+ gid = input.gid
+ }
+
+ if (uid === 'inherit' || gid === 'inherit') {
+ const owner = await find(path)
+ if (uid === 'inherit') {
+ uid = owner.uid
+ }
+
+ if (gid === 'inherit') {
+ gid = owner.gid
+ }
+ }
+
+ return { uid, gid }
+}
+
+module.exports = {
+ find,
+ update,
+ validate,
+}