forked from googleapis/cloud-trace-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpress-secure.ts
54 lines (48 loc) · 1.64 KB
/
express-secure.ts
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
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import {AddressInfo} from 'net';
import * as fs from 'fs';
import * as path from 'path';
import {Express4} from './express';
import * as httpModule from 'http';
import * as httpsModule from 'https';
/**
* A modification of the Express4 test server that uses HTTPS instead.
*/
export class Express4Secure extends Express4 {
static key = fs.readFileSync(
path.join(__dirname, '..', 'fixtures', 'key.pem')
);
static cert = fs.readFileSync(
path.join(__dirname, '..', 'fixtures', 'cert.pem')
);
private https: typeof httpsModule;
constructor() {
super();
this.https = require('https');
}
listen(port: number): number {
// The types of (http|https).Server are not compatible, but we don't
// access any properties that aren't present on both in the test.
this.server = this.https.createServer(
{key: Express4Secure.key, cert: Express4Secure.cert},
this.app
) as {} as httpModule.Server;
this.server.listen(port);
return (this.server.address() as AddressInfo).port;
}
shutdown() {
this.server!.close();
}
}