aboutsummaryrefslogtreecommitdiff
path: root/sandbox/testAppNevena/Front/node_modules/infer-owner/index.js
diff options
context:
space:
mode:
authorDanijel Andjelkovic <adanijel99@gmail.com>2022-03-01 21:54:41 +0100
committerDanijel Andjelkovic <adanijel99@gmail.com>2022-03-01 21:54:41 +0100
commit6c8128f9fd5a5d0be115806c35a21b3d683df8d6 (patch)
treef46c2f6b3b9b294ff32bd75c08ccdc9e7a8cc4ef /sandbox/testAppNevena/Front/node_modules/infer-owner/index.js
parent2400b84e95913665da6279114168148444b8f9ab (diff)
parent7d3640f824f46490b47bd95f1c5a16644f712068 (diff)
Merge branch 'dev' of http://gitlab.pmf.kg.ac.rs/igrannonica/neuronstellar into logo
Diffstat (limited to 'sandbox/testAppNevena/Front/node_modules/infer-owner/index.js')
-rw-r--r--sandbox/testAppNevena/Front/node_modules/infer-owner/index.js71
1 files changed, 71 insertions, 0 deletions
diff --git a/sandbox/testAppNevena/Front/node_modules/infer-owner/index.js b/sandbox/testAppNevena/Front/node_modules/infer-owner/index.js
new file mode 100644
index 00000000..a7bddcbd
--- /dev/null
+++ b/sandbox/testAppNevena/Front/node_modules/infer-owner/index.js
@@ -0,0 +1,71 @@
+const cache = new Map()
+const fs = require('fs')
+const { dirname, resolve } = require('path')
+
+
+const lstat = path => new Promise((res, rej) =>
+ fs.lstat(path, (er, st) => er ? rej(er) : res(st)))
+
+const inferOwner = path => {
+ path = resolve(path)
+ if (cache.has(path))
+ return Promise.resolve(cache.get(path))
+
+ const statThen = st => {
+ const { uid, gid } = st
+ cache.set(path, { uid, gid })
+ return { uid, gid }
+ }
+ const parent = dirname(path)
+ const parentTrap = parent === path ? null : er => {
+ return inferOwner(parent).then((owner) => {
+ cache.set(path, owner)
+ return owner
+ })
+ }
+ return lstat(path).then(statThen, parentTrap)
+}
+
+const inferOwnerSync = path => {
+ path = resolve(path)
+ if (cache.has(path))
+ return cache.get(path)
+
+ const parent = dirname(path)
+
+ // avoid obscuring call site by re-throwing
+ // "catch" the error by returning from a finally,
+ // only if we're not at the root, and the parent call works.
+ let threw = true
+ try {
+ const st = fs.lstatSync(path)
+ threw = false
+ const { uid, gid } = st
+ cache.set(path, { uid, gid })
+ return { uid, gid }
+ } finally {
+ if (threw && parent !== path) {
+ const owner = inferOwnerSync(parent)
+ cache.set(path, owner)
+ return owner // eslint-disable-line no-unsafe-finally
+ }
+ }
+}
+
+const inflight = new Map()
+module.exports = path => {
+ path = resolve(path)
+ if (inflight.has(path))
+ return Promise.resolve(inflight.get(path))
+ const p = inferOwner(path).then(owner => {
+ inflight.delete(path)
+ return owner
+ })
+ inflight.set(path, p)
+ return p
+}
+module.exports.sync = inferOwnerSync
+module.exports.clearCache = () => {
+ cache.clear()
+ inflight.clear()
+}