-
Notifications
You must be signed in to change notification settings - Fork 7
/
lib.browser.mjs
143 lines (130 loc) · 3.67 KB
/
lib.browser.mjs
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/* global XMLHttpRequest, localStorage */
import * as utf8Codec from 'utf8-codec'
import { base64URL } from '@leichtgewicht/base64-codec'
import {
AbortError,
HTTPStatusError,
TimeoutError,
URL
} from './common.mjs'
const contentType = 'application/dns-message'
function noop () { }
export function queryDns () {
throw new Error('Only "doh" endpoints are supported in the browser')
}
export async function loadJSON (url, cache, timeout, abortSignal) {
const cacheKey = cache ? cache.localStoragePrefix + cache.name : null
if (cacheKey) {
try {
const cached = JSON.parse(localStorage.getItem(cacheKey))
if (cached && cached.time > cache.maxTime) {
return cached
}
} catch (err) {}
}
const { data } = await requestRaw(url, 'GET', null, timeout, abortSignal)
const result = {
time: Date.now(),
data: JSON.parse(utf8Codec.decode(data))
}
if (cacheKey) {
try {
localStorage.setItem(cacheKey, JSON.stringify(result))
} catch (err) {
result.time = null
}
}
return result
}
function requestRaw (url, method, data, timeout, abortSignal) {
return new Promise((resolve, reject) => {
const target = new URL(url)
if (method === 'GET' && data) {
target.search = '?dns=' + base64URL.decode(data)
}
const uri = target.toString()
const xhr = new XMLHttpRequest()
xhr.open(method, uri, true)
xhr.setRequestHeader('Accept', contentType)
if (method === 'POST') {
xhr.setRequestHeader('Content-Type', contentType)
}
xhr.responseType = 'arraybuffer'
xhr.timeout = timeout
xhr.ontimeout = ontimeout
xhr.onreadystatechange = onreadystatechange
xhr.onerror = onerror
xhr.onload = onload
if (method === 'POST') {
xhr.send(data)
} else {
xhr.send()
}
if (abortSignal) {
abortSignal.addEventListener('abort', onabort)
}
function ontimeout () {
finish(new TimeoutError(timeout))
try {
xhr.abort()
} catch (e) { }
}
function onload () {
if (xhr.status !== 200) {
finish(new HTTPStatusError(uri, xhr.status, method))
} else {
let buf
if (typeof xhr.response === 'string') {
buf = utf8Codec.encode(xhr.response)
} else if (xhr.response instanceof Uint8Array) {
buf = xhr.response
} else if (Array.isArray(xhr.response) || xhr.response instanceof ArrayBuffer) {
buf = new Uint8Array(xhr.response)
} else {
throw new Error('Unprocessable response ' + xhr.response)
}
finish(null, buf)
}
}
function onreadystatechange () {
if (xhr.readyState > 1 && xhr.status !== 200 && xhr.status !== 0) {
finish(new HTTPStatusError(uri, xhr.status, method))
try {
xhr.abort()
} catch (e) { }
}
}
let finish = function (error, data) {
finish = noop
if (abortSignal) {
abortSignal.removeEventListener('abort', onabort)
}
if (error) {
resolve({
error,
response: xhr
})
} else {
resolve({
data,
response: xhr
})
}
}
function onerror () {
finish(xhr.status === 200 ? new Error('Inexplicable XHR Error') : new HTTPStatusError(uri, xhr.status, method))
}
function onabort () {
finish(new AbortError())
try {
xhr.abort()
} catch (e) { }
}
})
}
export function request (url, method, packet, timeout, abortSignal) {
return requestRaw(url, method, packet, timeout, abortSignal)
}
export function processResolvers (resolvers) {
return resolvers.filter(resolver => resolver.cors || resolver.endpoint.cors)
}