-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
160 lines (127 loc) · 4.18 KB
/
index.js
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
'use strict';
const url = require('url');
const http = require('http');
const https = require('https');
const fs = require('fs');
const httpRequestOptions = [
'protocol',
'host',
'hostname',
'family',
'port',
'localAddress',
'socketPath',
'method',
'path',
'headers',
'auth',
'agent',
'createConnection',
'timeout',
'encoding', // Xtra ...
'content' // Xtra - http://stackoverflow.com/questions/2445276/how-to-post-data-in-php-using-file-get-contents
];
const fsReadOptions = [
'encoding',
'flag'
];
/**
* Get content from a file or a URL
*
* @async
*
* @param {!string} path - Path or a URL
*
* @param {object} [options] - Options to get content
*
* @param {number} [options.family] - Same as [http.request.options.family]{@link https://nodejs.org/api/http.html#http_http_request_options_callback}
*
* @param {string} [options.socketPath] - Same as [http.request.options.socketPath]{@link https://nodejs.org/api/http.html#http_http_request_options_callback}
*
* @param {string} [options.method=GET] - Same as [http.request.options.method]{@link https://nodejs.org/api/http.html#http_http_request_options_callback}
*
* @param {object} [options.headers] - Same as [http.request.options.headers]{@link https://nodejs.org/api/http.html#http_http_request_options_callback}
*
* @param {string} [options.auth] - Same as [http.request.options.auth]{@link https://nodejs.org/api/http.html#http_http_request_options_callback}
*
* @param {(http.Agent|boolean)} [options.agent] - Same as [http.request.options.agent]{@link https://nodejs.org/api/http.html#http_http_request_options_callback}
*
* @param {function} [options.createConnection] - Same as [http.request.options.createConnection]{@link https://nodejs.org/api/http.html#http_http_request_options_callback}
*
* @param {number} [options.timeout] - Same as [http.request.options.timeout]{@link https://nodejs.org/api/http.html#http_http_request_options_callback}
*
* @param {string} [options.encoding=latin1] - Same as [fs.readFile.options.encoding]{@link https://nodejs.org/api/fs.html#fs_fs_readfile_path_options_callback}
*
* @param {string} [options.content] - Used to send content
*
* @param {string} [options.flag] - Same as [fs.readFile.options.flag]{@link https://nodejs.org/api/fs.html#fs_fs_readfile_path_options_callback}
*
* @return {Promise<string>}
*/
const fileGetContents = function (path, options) {
const sanitizeOptions = function (indexes, options) {
const sanitizedOptions = {};
for (let x = 0, y = indexes.length; x < y; x++) {
if (typeof options[indexes[x]] !== 'undefined') {
let option = options[indexes[x]];
if (typeof option === 'object') {
option = Object.assign({}, option);
}
sanitizedOptions[indexes[x]] = option;
}
}
return sanitizedOptions;
};
const httpRequest = function (options, resolve, reject) {
const client = options.protocol === 'https:' ? https : http;
const request = client.request(options, response => {
let data = '';
response.on('data', chunk => {
data += chunk.toString(options.encoding || 'latin1');
});
response.on('end', () => {
resolve(data);
});
});
request.on('error', error => {
reject(error);
});
request.end(options.content || undefined);
};
const fileSystemRequest = function (path, options, resolve, reject) {
fs.readFile(path, options, (error, data) => {
if (error) {
reject(error);
} else {
resolve(data.toString());
}
});
};
return new Promise((resolve, reject) => {
if (arguments.length === 0) {
reject(new Error('Required path value'));
return;
}
try {
if (typeof options !== 'object') {
options = {};
}
const uri = url.parse(path);
if (!uri.host) {
const fsOptions = sanitizeOptions(fsReadOptions, options);
fileSystemRequest(path, fsOptions, resolve, reject);
return;
}
const httpOptions = sanitizeOptions(httpRequestOptions, options);
httpOptions.protocol = uri.protocol;
httpOptions.host = uri.host;
httpOptions.port = uri.port;
httpOptions.hostname = uri.hostname;
httpOptions.path = uri.path;
httpRequest(httpOptions, resolve, reject);
} catch (err) {
reject(err);
}
});
};
module.exports = fileGetContents;