1 | 'use strict';
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | const util = require('util');
|
16 | const execChildProcess = require('child_process').exec;
|
17 | const spawnChildProcess = require('child_process').spawn;
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 | module.exports.fetch = function (url) {
|
27 | return new Promise((resolve, reject) => {
|
28 | const lib = url.startsWith('https') ? require('https') : require('http');
|
29 | const request = lib.get(url, response => {
|
30 | if (response.statusCode < 200 || response.statusCode > 299) {
|
31 | reject(new Error('Failed to load page, status code: ' + response.statusCode));
|
32 | }
|
33 | const body = [];
|
34 | response.on('data', chunk => body.push(chunk));
|
35 | response.on('end', () => resolve(body.join('')));
|
36 | });
|
37 | request.on('error', err => reject(err));
|
38 | });
|
39 | };
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 | module.exports.exec = util.promisify(execChildProcess);
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 | module.exports.spawn = util.promisify(spawnChildProcess); |
\ | No newline at end of file |