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

Function httpsGetBuffer

src/test/runTest-web.ts:10–60  ·  view source on GitHub ↗
(url: string)

Source from the content-addressed store, hash-verified

8const AdmZip = require("adm-zip");
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);
59 });
60}
61
62async function httpsGetJson(url: string) {
63 const buffer = await httpsGetBuffer(url);

Callers 2

httpsGetJsonFunction · 0.85
httpsGetZipFunction · 0.85

Calls 1

_getFunction · 0.85

Tested by

no test coverage detected