This repository was archived by the owner on Oct 12, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathloader.js
More file actions
128 lines (112 loc) · 5.2 KB
/
loader.js
File metadata and controls
128 lines (112 loc) · 5.2 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
function _getBehaviorSrc(name, path, description = "") {
return fetch(path)
.then(response => response.text())
.then(xml => {return {
"description":description || name, // work around LUNA bug that both requires description but cannot be empty
"name":name,
"sharingLevel": "ACCOUNT",
"xml": xml.trim(),
"approvedByUser": "ftl"
}});
}
function _addBehavior(name, path, isCustomOverride = false, dryrun = false) {
console.log(`${dryrun? "// TODO: " : ""}addBehavior("${name}");`);
return _getBehaviorSrc(name, path)
.then(behaviorSrc => {
if (dryrun) return true;
let apiPrefix = isCustomOverride ? "custom-overrides" : "custom-behaviors";
return fetch(`https://control.akamai.com/papi/v0/${apiPrefix}`,
{
method: 'POST',
credentials: "same-origin",
body: JSON.stringify(behaviorSrc),
headers: new Headers({"Content-Type": "application/json",
"PAPI-Use-Prefixes": "false"})
})
});
}
function _deleteBehavior(behavior, isCustomOverride = false, dryrun = false) {
console.log(`${dryrun? "// TODO: " : ""}deleteBehavior("${behavior.name}");`);
if (dryrun) return true;
let apiPrefix = isCustomOverride ? "custom-overrides" : "custom-behaviors";
return fetch(`https://control.akamai.com/papi/v0/${apiPrefix}/${behavior.behaviorId}`,
{
method: 'DELETE',
credentials: "same-origin",
headers: new Headers({"PAPI-Use-Prefixes": "false"})
})
}
function excludeExistingBehaviors(behaviors, isCustomOverride = false) {
let apiPrefix = isCustomOverride ? "custom-overrides" : "custom-behaviors";
return fetch(`https://control.akamai.com/papi/v0/${apiPrefix}`, {credentials: "same-origin"})
.then(response => response.json())
.then(data => {
for (const existingBehavior of (data.customBehaviors || data.customOverrides).items) {
behaviors.delete(existingBehavior.name);
}
return behaviors;
})
}
function _matchExistingBehaviors(behaviors, isCustomOverride = false) {
let apiPrefix = isCustomOverride ? "custom-overrides" : "custom-behaviors";
return fetch(`https://control.akamai.com/papi/v0/${apiPrefix}`, {credentials: "same-origin"})
.then(response => response.json())
.then(data => {
let existingBehaviors = [];
for (const existingBehavior of (data.customBehaviors || data.customOverrides).items) {
if (behaviors.has(existingBehavior.name))
existingBehaviors.push(existingBehavior);
}
return existingBehaviors;
})
}
function _addAllCustomBehaviors(dryrun = false, filter = null) {
return Promise.all([true, false].map(isCustomOverride =>
fetch(`https://api.github.com/repos/colinbendell/pm-custom-behaviors/contents${isCustomOverride ? "/overrides" : ""}`)
.then(response => response.status !== 200 ? Promise.reject(response) : response.json())
.then(data => {
let behaviorList = new Map();
data.forEach(behaviorFile => {
if (/\.xml$/.test(behaviorFile.name)
&& (!filter || new RegExp(filter).test(behaviorFile.name))
&& behaviorFile.size > 0)
behaviorList.set(behaviorFile.name.replace('.xml', ''), behaviorFile.download_url)
});
return behaviorList;
})
.then(behaviors => excludeExistingBehaviors(behaviors, isCustomOverride))
.then(behaviors => Promise.all([...behaviors].map(val => {_addBehavior(val[0], val[1], isCustomOverride, dryrun)})))
)
);
}
function _deleteAllCustomBehaviors(dryrun = false, filter = null) {
return Promise.all([true, false].map(isCustomOverride =>
fetch(`https://api.github.com/repos/colinbendell/pm-custom-behaviors/contents${isCustomOverride ? "/overrides" : ""}/deprecated`)
.then(response => response.status !== 200 ? Promise.reject(response) : response.json())
.then(data => {
let behaviorSet = new Set();
data.forEach(behaviorFile => {
if (/\.xml$/.test(behaviorFile.name)
&& (!filter || new RegExp(filter).test(behaviorFile.name)))
behaviorSet.add(behaviorFile.name.replace('.xml', ''))
});
return behaviorSet;
})
.then(behaviors => _matchExistingBehaviors(behaviors, isCustomOverride))
.then(behaviors => Promise.all(behaviors.map(o => {_deleteBehavior(o, isCustomOverride, dryrun)})))
)
);
}
function sync(dryrun = false) {
return _addAllCustomBehaviors(dryrun)
.then (() => _deleteAllCustomBehaviors(dryrun))
.then(() => dryrun ? console.log('//TODO: run `sync();` to automatically perform the above') : '')
.catch(e => console.error(e))
}
function addBehavior(name) {
return _addAllCustomBehaviors(false, name);
}
function deleteBehavior(name) {
return _deleteAllCustomBehaviors(false, name);
}
sync(true);