UNPKG

11.3 kBTypeScriptView Raw
1/**
2 * HTTPS is the HTTP protocol over TLS/SSL. In Node.js this is implemented as a
3 * separate module.
4 * @see [source](https://github.com/nodejs/node/blob/v16.4.2/lib/https.js)
5 */
6declare module 'https' {
7 import * as tls from 'node:tls';
8 import * as http from 'node:http';
9 import { URL } from 'node:url';
10 type ServerOptions = tls.SecureContextOptions & tls.TlsOptions & http.ServerOptions;
11 type RequestOptions = http.RequestOptions &
12 tls.SecureContextOptions & {
13 rejectUnauthorized?: boolean | undefined; // Defaults to true
14 servername?: string | undefined; // SNI TLS Extension
15 };
16 interface AgentOptions extends http.AgentOptions, tls.ConnectionOptions {
17 rejectUnauthorized?: boolean | undefined;
18 maxCachedSessions?: number | undefined;
19 }
20 /**
21 * An `Agent` object for HTTPS similar to `http.Agent`. See {@link request} for more information.
22 * @since v0.4.5
23 */
24 class Agent extends http.Agent {
25 constructor(options?: AgentOptions);
26 options: AgentOptions;
27 }
28 interface Server extends http.HttpBase {}
29 /**
30 * See `http.Server` for more information.
31 * @since v0.3.4
32 */
33 class Server extends tls.Server {
34 constructor(requestListener?: http.RequestListener);
35 constructor(options: ServerOptions, requestListener?: http.RequestListener);
36 }
37 /**
38 * ```js
39 * // curl -k https://localhost:8000/
40 * const https = require('https');
41 * const fs = require('fs');
42 *
43 * const options = {
44 * key: fs.readFileSync('test/fixtures/keys/agent2-key.pem'),
45 * cert: fs.readFileSync('test/fixtures/keys/agent2-cert.pem')
46 * };
47 *
48 * https.createServer(options, (req, res) => {
49 * res.writeHead(200);
50 * res.end('hello world\n');
51 * }).listen(8000);
52 * ```
53 *
54 * Or
55 *
56 * ```js
57 * const https = require('https');
58 * const fs = require('fs');
59 *
60 * const options = {
61 * pfx: fs.readFileSync('test/fixtures/test_cert.pfx'),
62 * passphrase: 'sample'
63 * };
64 *
65 * https.createServer(options, (req, res) => {
66 * res.writeHead(200);
67 * res.end('hello world\n');
68 * }).listen(8000);
69 * ```
70 * @since v0.3.4
71 * @param options Accepts `options` from `createServer`, `createSecureContext` and `createServer`.
72 * @param requestListener A listener to be added to the `'request'` event.
73 */
74 function createServer(requestListener?: http.RequestListener): Server;
75 function createServer(options: ServerOptions, requestListener?: http.RequestListener): Server;
76 /**
77 * Makes a request to a secure web server.
78 *
79 * The following additional `options` from `tls.connect()` are also accepted:`ca`, `cert`, `ciphers`, `clientCertEngine`, `crl`, `dhparam`, `ecdhCurve`,`honorCipherOrder`, `key`, `passphrase`,
80 * `pfx`, `rejectUnauthorized`,`secureOptions`, `secureProtocol`, `servername`, `sessionIdContext`,`highWaterMark`.
81 *
82 * `options` can be an object, a string, or a `URL` object. If `options` is a
83 * string, it is automatically parsed with `new URL()`. If it is a `URL` object, it will be automatically converted to an ordinary `options` object.
84 *
85 * `https.request()` returns an instance of the `http.ClientRequest` class. The `ClientRequest` instance is a writable stream. If one needs to
86 * upload a file with a POST request, then write to the `ClientRequest` object.
87 *
88 * ```js
89 * const https = require('https');
90 *
91 * const options = {
92 * hostname: 'encrypted.google.com',
93 * port: 443,
94 * path: '/',
95 * method: 'GET'
96 * };
97 *
98 * const req = https.request(options, (res) => {
99 * console.log('statusCode:', res.statusCode);
100 * console.log('headers:', res.headers);
101 *
102 * res.on('data', (d) => {
103 * process.stdout.write(d);
104 * });
105 * });
106 *
107 * req.on('error', (e) => {
108 * console.error(e);
109 * });
110 * req.end();
111 * ```
112 *
113 * Example using options from `tls.connect()`:
114 *
115 * ```js
116 * const options = {
117 * hostname: 'encrypted.google.com',
118 * port: 443,
119 * path: '/',
120 * method: 'GET',
121 * key: fs.readFileSync('test/fixtures/keys/agent2-key.pem'),
122 * cert: fs.readFileSync('test/fixtures/keys/agent2-cert.pem')
123 * };
124 * options.agent = new https.Agent(options);
125 *
126 * const req = https.request(options, (res) => {
127 * // ...
128 * });
129 * ```
130 *
131 * Alternatively, opt out of connection pooling by not using an `Agent`.
132 *
133 * ```js
134 * const options = {
135 * hostname: 'encrypted.google.com',
136 * port: 443,
137 * path: '/',
138 * method: 'GET',
139 * key: fs.readFileSync('test/fixtures/keys/agent2-key.pem'),
140 * cert: fs.readFileSync('test/fixtures/keys/agent2-cert.pem'),
141 * agent: false
142 * };
143 *
144 * const req = https.request(options, (res) => {
145 * // ...
146 * });
147 * ```
148 *
149 * Example using a `URL` as `options`:
150 *
151 * ```js
152 * const options = new URL('https://abc:xyz@example.com');
153 *
154 * const req = https.request(options, (res) => {
155 * // ...
156 * });
157 * ```
158 *
159 * Example pinning on certificate fingerprint, or the public key (similar to`pin-sha256`):
160 *
161 * ```js
162 * const tls = require('tls');
163 * const https = require('https');
164 * const crypto = require('crypto');
165 *
166 * function sha256(s) {
167 * return crypto.createHash('sha256').update(s).digest('base64');
168 * }
169 * const options = {
170 * hostname: 'github.com',
171 * port: 443,
172 * path: '/',
173 * method: 'GET',
174 * checkServerIdentity: function(host, cert) {
175 * // Make sure the certificate is issued to the host we are connected to
176 * const err = tls.checkServerIdentity(host, cert);
177 * if (err) {
178 * return err;
179 * }
180 *
181 * // Pin the public key, similar to HPKP pin-sha25 pinning
182 * const pubkey256 = 'pL1+qb9HTMRZJmuC/bB/ZI9d302BYrrqiVuRyW+DGrU=';
183 * if (sha256(cert.pubkey) !== pubkey256) {
184 * const msg = 'Certificate verification error: ' +
185 * `The public key of '${cert.subject.CN}' ` +
186 * 'does not match our pinned fingerprint';
187 * return new Error(msg);
188 * }
189 *
190 * // Pin the exact certificate, rather than the pub key
191 * const cert256 = '25:FE:39:32:D9:63:8C:8A:FC:A1:9A:29:87:' +
192 * 'D8:3E:4C:1D:98:DB:71:E4:1A:48:03:98:EA:22:6A:BD:8B:93:16';
193 * if (cert.fingerprint256 !== cert256) {
194 * const msg = 'Certificate verification error: ' +
195 * `The certificate of '${cert.subject.CN}' ` +
196 * 'does not match our pinned fingerprint';
197 * return new Error(msg);
198 * }
199 *
200 * // This loop is informational only.
201 * // Print the certificate and public key fingerprints of all certs in the
202 * // chain. Its common to pin the public key of the issuer on the public
203 * // internet, while pinning the public key of the service in sensitive
204 * // environments.
205 * do {
206 * console.log('Subject Common Name:', cert.subject.CN);
207 * console.log(' Certificate SHA256 fingerprint:', cert.fingerprint256);
208 *
209 * hash = crypto.createHash('sha256');
210 * console.log(' Public key ping-sha256:', sha256(cert.pubkey));
211 *
212 * lastprint256 = cert.fingerprint256;
213 * cert = cert.issuerCertificate;
214 * } while (cert.fingerprint256 !== lastprint256);
215 *
216 * },
217 * };
218 *
219 * options.agent = new https.Agent(options);
220 * const req = https.request(options, (res) => {
221 * console.log('All OK. Server matched our pinned cert or public key');
222 * console.log('statusCode:', res.statusCode);
223 * // Print the HPKP values
224 * console.log('headers:', res.headers['public-key-pins']);
225 *
226 * res.on('data', (d) => {});
227 * });
228 *
229 * req.on('error', (e) => {
230 * console.error(e.message);
231 * });
232 * req.end();
233 * ```
234 *
235 * Outputs for example:
236 *
237 * ```text
238 * Subject Common Name: github.com
239 * Certificate SHA256 fingerprint: 25:FE:39:32:D9:63:8C:8A:FC:A1:9A:29:87:D8:3E:4C:1D:98:DB:71:E4:1A:48:03:98:EA:22:6A:BD:8B:93:16
240 * Public key ping-sha256: pL1+qb9HTMRZJmuC/bB/ZI9d302BYrrqiVuRyW+DGrU=
241 * Subject Common Name: DigiCert SHA2 Extended Validation Server CA
242 * Certificate SHA256 fingerprint: 40:3E:06:2A:26:53:05:91:13:28:5B:AF:80:A0:D4:AE:42:2C:84:8C:9F:78:FA:D0:1F:C9:4B:C5:B8:7F:EF:1A
243 * Public key ping-sha256: RRM1dGqnDFsCJXBTHky16vi1obOlCgFFn/yOhI/y+ho=
244 * Subject Common Name: DigiCert High Assurance EV Root CA
245 * Certificate SHA256 fingerprint: 74:31:E5:F4:C3:C1:CE:46:90:77:4F:0B:61:E0:54:40:88:3B:A9:A0:1E:D0:0B:A6:AB:D7:80:6E:D3:B1:18:CF
246 * Public key ping-sha256: WoiWRyIOVNa9ihaBciRSC7XHjliYS9VwUGOIud4PB18=
247 * All OK. Server matched our pinned cert or public key
248 * statusCode: 200
249 * headers: max-age=0; pin-sha256="WoiWRyIOVNa9ihaBciRSC7XHjliYS9VwUGOIud4PB18="; pin-sha256="RRM1dGqnDFsCJXBTHky16vi1obOlCgFFn/yOhI/y+ho=";
250 * pin-sha256="k2v657xBsOVe1PQRwOsHsw3bsGT2VzIqz5K+59sNQws="; pin-sha256="K87oWBWM9UZfyddvDfoxL+8lpNyoUB2ptGtn0fv6G2Q="; pin-sha256="IQBnNBEiFuhj+8x6X8XLgh01V9Ic5/V3IRQLNFFc7v4=";
251 * pin-sha256="iie1VXtL7HzAMF+/PVPR9xzT80kQxdZeJ+zduCB3uj0="; pin-sha256="LvRiGEjRqfzurezaWuj8Wie2gyHMrW5Q06LspMnox7A="; includeSubDomains
252 * ```
253 * @since v0.3.6
254 * @param options Accepts all `options` from `request`, with some differences in default values:
255 */
256 function request(options: RequestOptions | string | URL, callback?: (res: http.IncomingMessage) => void): http.ClientRequest;
257 function request(url: string | URL, options: RequestOptions, callback?: (res: http.IncomingMessage) => void): http.ClientRequest;
258 /**
259 * Like `http.get()` but for HTTPS.
260 *
261 * `options` can be an object, a string, or a `URL` object. If `options` is a
262 * string, it is automatically parsed with `new URL()`. If it is a `URL` object, it will be automatically converted to an ordinary `options` object.
263 *
264 * ```js
265 * const https = require('https');
266 *
267 * https.get('https://encrypted.google.com/', (res) => {
268 * console.log('statusCode:', res.statusCode);
269 * console.log('headers:', res.headers);
270 *
271 * res.on('data', (d) => {
272 * process.stdout.write(d);
273 * });
274 *
275 * }).on('error', (e) => {
276 * console.error(e);
277 * });
278 * ```
279 * @since v0.3.6
280 * @param options Accepts the same `options` as {@link request}, with the `method` always set to `GET`.
281 */
282 function get(options: RequestOptions | string | URL, callback?: (res: http.IncomingMessage) => void): http.ClientRequest;
283 function get(url: string | URL, options: RequestOptions, callback?: (res: http.IncomingMessage) => void): http.ClientRequest;
284 let globalAgent: Agent;
285}
286declare module 'node:https' {
287 export * from 'https';
288}
289
\No newline at end of file