MCPcopy Create free account
hub / github.com/ZenNotes/zennotes / httpGetJson

Function httpGetJson

tooling/scripts/perf-web-runtime.mjs:144–169  ·  view source on GitHub ↗
(url)

Source from the content-addressed store, hash-verified

142}
143
144function httpGetJson(url) {
145 return new Promise((resolvePromise, rejectPromise) => {
146 const req = http.get(url, (res) => {
147 let body = ''
148 res.setEncoding('utf8')
149 res.on('data', (chunk) => {
150 body += chunk
151 })
152 res.on('end', () => {
153 if ((res.statusCode ?? 500) >= 400) {
154 rejectPromise(new Error(`${url} returned HTTP ${res.statusCode}: ${body}`))
155 return
156 }
157 try {
158 resolvePromise(JSON.parse(body))
159 } catch (err) {
160 rejectPromise(err)
161 }
162 })
163 })
164 req.on('error', rejectPromise)
165 req.setTimeout(1000, () => {
166 req.destroy(new Error(`Timed out requesting ${url}`))
167 })
168 })
169}
170
171async function waitForHttpOk(url, timeoutMs) {
172 const deadline = Date.now() + timeoutMs

Callers 2

waitForHttpOkFunction · 0.70
connectToPageFunction · 0.70

Calls 2

onMethod · 0.45
destroyMethod · 0.45

Tested by

no test coverage detected