-
Notifications
You must be signed in to change notification settings - Fork 0
/
Query.js
124 lines (110 loc) · 4.14 KB
/
Query.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
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
/* eslint-env amd, webworker, browser */
/* global sjcl */
'use strict';
(function () {
var global = Function('return this')() // eslint-disable-line
if (typeof global.sjcl === 'undefined') {
throw new Error('Rely on "Stanford Javascript Crypto Library" defined as a global "sjcl"\nFind it at https://bitwiseshiftleft.github.io/sjcl/')
}
if (typeof global.Artnum === 'undefined') {
global.Artnum = {}
}
global.Artnum.Query = (function () {
var my = {}
var id = new Uint8Array(8)
global.crypto.getRandomValues(id)
id = id.join('')
var count = 0
my.exec = function (url, options = {}) {
return new Promise(function (resolve, reject) {
count++
if (!options.credentials) {
options.credentials = 'same-origin'
}
if (options.headers) {
if (!options.headers['Content-Type']) {
options.headers['Content-Type'] = 'application/json; charset=utf-8'
}
} else {
options.headers = {'Content-Type': 'application/json; charset=utf-8'}
}
var requestId = id + String(count)
if (options.reqid) {
requestId = options.reqid
}
options.headers['X-Artnum-Reqid'] = requestId
options.headers['X-Request-ID'] = requestId
if (options.body) {
if (typeof options.body !== 'string') {
options.body = JSON.stringify(options.body)
}
}
fetch(url, options).then(function (response) {
if (options.method && options.method.toLowerCase() === 'head') {
var r = /^x-artnum-(.+)$/i // eslint-disable-line
var results = {}
for (var h of response.headers) {
if (r.test(h[0])) {
results[r.exec(h[0])[1].toLowerCase()] = h[1]
}
}
resolve({success: true, type: 'results', 'message': 'OK', data: results, length: 1})
} else {
switch (Math.round(response.status / 100)) {
default:
case 2:
response.text().then(function (body) {
var hash = {}
var sign = {}
for (var h of response.headers) {
switch (h[0].toLowerCase()) {
case 'x-artnum-hash': hash.value = h[1]; break
case 'x-artnum-hash-algo': hash.algo = h[1]; break
case 'x-artnum-sign': sign.value = h[1]; break
case 'x-artnum-sign-algo': sign.algo = h[1]; break
}
}
var valid = true
if (hash.value && hash.algo) {
if (sjcl.hash[hash.algo]) {
var value = sjcl.hash[hash.algo].hash(body)
if (sjcl.codec.base64.fromBits(value) !== hash.value) {
valid = false
}
}
}
if (sign.value && sign.algo) {
// Not implemented
}
if (valid) {
try {
let object = JSON.parse(body)
resolve(object)
} catch (e) {
console.log(e)
resolve({success: false, type: 'error', message: 'JSON error', data: [], length: 0})
}
} else {
resolve({success: false, type: 'error', message: 'Invalid data from server', data: [], length: 0})
}
})
break
case 4:
resolve({success: false, type: 'error', message: 'Cannot access requested data', data: [], length: 0})
break
case 5:
resolve({success: false, type: 'error', message: 'Server failure', data: [], length: 0})
break
}
}
}, function (body) {
console.log(body)
})
})
}
return my
}())
if (typeof define === 'function' && define.amd) {
define(['artnum/Query'], function () { return global.Artnum.Query })
}
}())