forked from remotestorage/api-test-suite
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.js
More file actions
76 lines (57 loc) · 2.56 KB
/
util.js
File metadata and controls
76 lines (57 loc) · 2.56 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
const mod = {
isEtag0: string => !string.match(/\D/i),
isEtag1: string => string.trim().length && string.match(/^([^']|\\')*/i),
validEtag: version => version === 0 ? mod.isEtag0 : mod.isEtag1,
isName0: string => string.match(/[a-zA-Z0-9%-_]/i),
isName1: string => string.trim().length && string.match(/[a-zA-Z0-9%-_\.\-\_]/i),
validName: version => version === 0 ? mod.isName0 : mod.isName1,
validDate: text => text.match(/^(Mon|Tue|Wed|Thu|Fri|Sat|Sun), \d{2} (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) \d{4} \d{2}:\d{2}:\d{2} GMT$/),
clone: object => Object.assign({}, object),
webfinger: {
async discover (server, account) {
const params = {
resource: `acct:${ account }@${ (new URL(server)).hostname }`,
};
const json = await (await mod._fetch(`${ server }/.well-known/webfinger?${ new URLSearchParams(params) }`)).json();
return json.links.filter(e => ['remotestorage', 'http://tools.ietf.org/id/draft-dejong-remotestorage'].includes(e.rel)).shift();
},
version: link => parseInt((link.type || link.properties['http://remotestorage.io/spec/version']).match(/draft-dejong-remotestorage-(\d+)/).pop()),
auth: link => link.properties['auth-endpoint'] || link.properties['http://tools.ietf.org/html/rfc6749#section-4.2'],
},
_fetch () {
// console.log([...arguments]);
return fetch(...arguments);
},
_url: (State, path) => `${ State.baseURL }${ `${ State.scope ? '/' + State.scope : '' }/${ path }`.replace(/\/+/g, '/') }`,
storage (State) {
const headers = {
'Content-Type': 'application/json',
};
if (State.token)
headers['Authorization'] = `Bearer ${ State.token }`;
return {
get: (path, _headers = {}) => mod._fetch(mod._url(State, path), {
headers: Object.assign(mod.clone(headers), _headers),
}),
put: (path, body, _headers = {}) => mod._fetch(mod._url(State, path), {
headers: Object.assign(mod.clone(headers), _headers),
method: 'PUT',
body: JSON.stringify(_headers).includes('charset=binary') ? body : JSON.stringify(body),
}),
delete: (path, _headers = {}) => mod._fetch(mod._url(State, path), {
headers: Object.assign(mod.clone(headers), _headers),
method: 'DELETE',
}),
head: path => mod._fetch(mod._url(State, path), {
headers,
method: 'HEAD',
}),
options: (path, _headers = {}) => mod._fetch(mod._url(State, path), {
headers: Object.assign(mod.clone(headers), _headers),
method: 'OPTIONS',
}),
};
},
byteLength: input => typeof Buffer !== 'undefined' ? Buffer.from(input).length : new TextEncoder().encode(input).byteLength,
};
export default mod;