File: httpUtils.ts

package info (click to toggle)
node-corepack 0.24.0-5
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 262,916 kB
  • sloc: javascript: 94; makefile: 18; sh: 12
file content (71 lines) | stat: -rw-r--r-- 2,362 bytes parent folder | download
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
import {UsageError}                     from 'clipanion';
import {RequestOptions}                 from 'https';
import {IncomingMessage, ClientRequest} from 'http';

export async function fetchUrlStream(url: string, options: RequestOptions = {}) {
  if (process.env.COREPACK_ENABLE_NETWORK === `0`)
    throw new UsageError(`Network access disabled by the environment; can't reach ${url}`);

  const {default: https} = await import(`https`);

  const {ProxyAgent} = await import(`proxy-agent`);

  const proxyAgent = new ProxyAgent();

  return new Promise<IncomingMessage>((resolve, reject) => {
    const createRequest = (url: string) => {
      const request: ClientRequest = https.get(url, {...options, agent: proxyAgent}, response => {
        const statusCode = response.statusCode;

        if ([301, 302, 307, 308].includes(statusCode as number) && response.headers.location)
          return createRequest(response.headers.location as string);

        if (statusCode != null && statusCode >= 200 && statusCode < 300)
          return resolve(response);

        return reject(new Error(`Server answered with HTTP ${statusCode} when performing the request to ${url}; for troubleshooting help, see https://github.com/nodejs/corepack#troubleshooting`));
      });

      request.on(`error`, err => {
        reject(new Error(`Error when performing the request to ${url}; for troubleshooting help, see https://github.com/nodejs/corepack#troubleshooting`));
      });
    };

    createRequest(url);
  });
}

export async function fetchAsBuffer(url: string, options?: RequestOptions) {
  const response = await fetchUrlStream(url, options);

  return new Promise<Buffer>((resolve, reject) => {
    const chunks: Array<Buffer> = [];

    response.on(`data`, chunk => {
      chunks.push(chunk);
    });

    response.on(`error`, error => {
      reject(error);
    });

    response.on(`end`, () => {
      resolve(Buffer.concat(chunks));
    });
  });
}

export async function fetchAsJson(url: string, options?: RequestOptions) {
  const buffer = await fetchAsBuffer(url, options);
  const asText = buffer.toString();

  try {
    return JSON.parse(asText);
  } catch (error) {
    const truncated = asText.length > 30
      ? `${asText.slice(0, 30)}...`
      : asText;

    throw new Error(`Couldn't parse JSON data: ${JSON.stringify(truncated)}`);
  }
}