MCPcopy Index your code
hub / github.com/VSpaceCode/VSpaceCode / _get

Function _get

src/test/runTest-web.ts:11–55  ·  view source on GitHub ↗
(
        url: string,
        resolve: (b: Buffer) => void,
        reject: (e: Error) => void
    )

Source from the content-addressed store, hash-verified

9
10function httpsGetBuffer(url: string) {
11 const _get = (
12 url: string,
13 resolve: (b: Buffer) => void,
14 reject: (e: Error) => void
15 ) => {
16 // User-Agent is needed for GitHub request
17 const options: https.RequestOptions = {
18 headers: {
19 "User-Agent": "node-https",
20 "Content-Type": "application/json; charset=utf-8",
21 },
22 };
23 https
24 .get(url, options, function (res) {
25 if (
26 (res.statusCode === 301 || res.statusCode === 302) &&
27 res.headers.location
28 ) {
29 _get(res.headers.location, resolve, reject);
30 return;
31 }
32
33 const data: Uint8Array[] = [];
34 res.on("data", function (chunk: Uint8Array) {
35 data.push(chunk);
36 });
37 res.on("end", function () {
38 const buffer = Buffer.concat(data);
39 if (res.statusCode === 200) {
40 resolve(buffer);
41 } else {
42 reject(
43 new Error(
44 `Status code: ${
45 res.statusCode
46 }\nResponse: ${buffer.toString()}`
47 )
48 );
49 }
50 });
51 })
52 .on("error", function (err) {
53 reject(new Error(`Error: ${err}`));
54 });
55 };
56
57 return new Promise<Buffer>((resolve, reject) => {
58 _get(url, resolve, reject);

Callers 1

httpsGetBufferFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected