This repository has been archived by the owner on Aug 15, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
writeFileSystemNodePermissions.js
86 lines (77 loc) · 2.94 KB
/
writeFileSystemNodePermissions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import { chmod } from "fs"
import { permissionsToBinaryFlags } from "./internal/permissions.js"
import { assertAndNormalizeFileUrl } from "./assertAndNormalizeFileUrl.js"
import { urlToFileSystemPath } from "./urlToFileSystemPath.js"
export const writeFileSystemNodePermissions = async (source, permissions) => {
const sourceUrl = assertAndNormalizeFileUrl(source)
const sourcePath = urlToFileSystemPath(sourceUrl)
let binaryFlags
if (typeof permissions === "object") {
permissions = {
owner: {
read: getPermissionOrComputeDefault("read", "owner", permissions),
write: getPermissionOrComputeDefault("write", "owner", permissions),
execute: getPermissionOrComputeDefault("execute", "owner", permissions),
},
group: {
read: getPermissionOrComputeDefault("read", "group", permissions),
write: getPermissionOrComputeDefault("write", "group", permissions),
execute: getPermissionOrComputeDefault("execute", "group", permissions),
},
others: {
read: getPermissionOrComputeDefault("read", "others", permissions),
write: getPermissionOrComputeDefault("write", "others", permissions),
execute: getPermissionOrComputeDefault("execute", "others", permissions),
},
}
binaryFlags = permissionsToBinaryFlags(permissions)
} else {
binaryFlags = permissions
}
return chmodNaive(sourcePath, binaryFlags)
}
const chmodNaive = (fileSystemPath, binaryFlags) => {
return new Promise((resolve, reject) => {
chmod(fileSystemPath, binaryFlags, (error) => {
if (error) {
reject(error)
} else {
resolve()
}
})
})
}
const actionLevels = { read: 0, write: 1, execute: 2 }
const subjectLevels = { others: 0, group: 1, owner: 2 }
const getPermissionOrComputeDefault = (action, subject, permissions) => {
if (subject in permissions) {
const subjectPermissions = permissions[subject]
if (action in subjectPermissions) {
return subjectPermissions[action]
}
const actionLevel = actionLevels[action]
const actionFallback = Object.keys(actionLevels).find(
(actionFallbackCandidate) =>
actionLevels[actionFallbackCandidate] > actionLevel &&
actionFallbackCandidate in subjectPermissions,
)
if (actionFallback) {
return subjectPermissions[actionFallback]
}
}
const subjectLevel = subjectLevels[subject]
// do we have a subject with a stronger level (group or owner)
// where we could read the action permission ?
const subjectFallback = Object.keys(subjectLevels).find(
(subjectFallbackCandidate) =>
subjectLevels[subjectFallbackCandidate] > subjectLevel &&
subjectFallbackCandidate in permissions,
)
if (subjectFallback) {
const subjectPermissions = permissions[subjectFallback]
return action in subjectPermissions
? subjectPermissions[action]
: getPermissionOrComputeDefault(action, subjectFallback, permissions)
}
return false
}