-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutil.ts
128 lines (120 loc) · 3.7 KB
/
util.ts
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
128
import * as rdf from "rdflib";
import { IndexedFormula } from "rdflib";
import AuthFetcher from "solid-auth-fetcher/dist/AuthFetcher";
function isContainer(url) {
return url.substr(-1) === "/";
}
export function getStore(
authFetcher: AuthFetcher | { fetch: () => any }
): IndexedFormula {
if (!authFetcher) {
throw new Error("please pass authFetcher to getStore!");
}
const store = (module.exports = rdf.graph()); // Make a Quad store
rdf.fetcher(store, { fetch: authFetcher.fetch.bind(authFetcher) }); // Attach a web I/O module, store.fetcher
store.updater = new rdf.UpdateManager(store); // Add real-time live updates store.updater
return store;
}
export async function getContainerMembers(
containerUrl: string,
authFetcher: AuthFetcher | { fetch: () => any }
): Promise<string[]> {
if (!authFetcher) {
throw new Error("please pass authFetcher to getContainerMembers!");
}
const store = getStore(authFetcher);
await store.fetcher.load(store.sym(containerUrl));
return store
.statementsMatching(
store.sym(containerUrl),
store.sym("http://www.w3.org/ns/ldp#contains")
)
.map((st) => st.object.value);
}
export async function recursiveDelete(
url: string,
authFetcher: AuthFetcher
): Promise<Response> {
if (!authFetcher) {
throw new Error("please pass authFetcher to recursiveDelete!");
}
if (isContainer(url)) {
const containerMembers = await getContainerMembers(url, authFetcher);
await Promise.all(
containerMembers.map((url) => recursiveDelete(url, authFetcher))
);
}
return authFetcher.fetch(url, { method: "DELETE" });
}
export function ifWps(name: string, runner: () => any): any {
const level = "WPS";
const id = "";
if (process.env.SKIP_WPS) {
return it.skip(`${level} ${id} ${name}`, runner);
}
return it(`${level} ${id} ${name}`, runner);
}
export function ifSecureWebsockets(name: string, runner: () => any): any {
const level = "SECURE_WEBSOCKETS";
const id = "";
if (process.env.SKIP_SECURE_WEBSOCKETS) {
return it.skip(`${level} ${id} ${name}`, runner);
}
return it(`${level} ${id} ${name}`, runner);
}
export function ifWebhooks(name: string, runner: () => any): any {
const level = "WEBHOOKS";
const id = "";
if (process.env.SKIP_WEBHOOKS) {
return it.skip(`${level} ${id} ${name}`, runner);
}
return it(`${level} ${id} ${name}`, runner);
}
export function responseCodeGroup(code: number): string {
return `${Math.floor(code / 100)}xx`;
}
// env parameters are SKIP for MUST, SHOULD and INCLUDE for MAY
export function itIs(
level = "",
id = ""
): (name: string, runner: () => any) => any {
switch (level) {
case "SKIP":
return (name, runner) => {
it.skip(`${level} ${id} ${name}`, runner);
};
case "MUST":
if (process.env.SKIP_MUST || process.env["SKIP_MUST_" + id]) {
return (name, runner) => {
it.skip(`${level} ${id} ${name}`, runner);
};
} else {
return (name, runner) => {
it(`${level} ${id} ${name}`, runner);
};
}
case "SHOULD":
if (process.env.SKIP_SHOULD || process.env["SKIP_SHOULD_" + id]) {
return (name, runner) => {
it.skip(`${level} ${id} ${name}`, runner);
};
} else {
return (name, runner) => {
it(`${level} ${id} ${name}`, runner);
};
}
case "MAY":
if (process.env.INCLUDE_MAY || process.env["INCLUDE_MAY_" + id]) {
return (name, runner) => {
it(`${level} ${id} ${name}`, runner);
};
} else {
return (name, runner) => {
it.skip(`${level} ${id} ${name}`, runner);
};
}
}
return (name, runner) => {
it(`${level} ${id} ${name}`, runner);
};
}