This repository has been archived by the owner on Jun 10, 2022. It is now read-only.
/
adapter.ts
61 lines (53 loc) · 1.96 KB
/
adapter.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
55
56
57
58
59
60
61
/*!
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/
import events from 'events';
import * as Restify from 'restify';
import { Context, ParameterSet } from '..';
/**
* Adapter options
*/
export type AdapterOptions = {
/**
* @member {http.Server} server Provide an existing web server to use. Will create one otherwise
*/
server?: Restify.Server;
/**
* @member {string | number} port Optional. When options.server is not supplied and an internal web server is to be
* created, this is the port number it should listen on. If this value is not given, it will attempt to read the
* PORT environment variable, then default to 3901
*/
port?: string | number;
};
/**
* Base Adapter class. Adapters are where connections from hosts are accepted and mapped to Contexts. The host
* connection requests a Context from a sessionId. If no matching Context is found, a new one is created and
* the 'connection' event is raised.
*/
export abstract class Adapter {
protected emitter = new events.EventEmitter();
protected get options() { return this._options; }
public get server() { return this._options.server; }
public set server(value: Restify.Server) { this._options.server = value; }
public get port() { return this._options.port; }
// tslint:disable-next-line:variable-name
constructor(protected _options: AdapterOptions) {
this._options = { ..._options };
this._options.port =
this._options.port ||
process.env.port ||
process.env.PORT ||
3901;
}
public abstract listen(): Promise<Restify.Server>;
/**
* The onConnection event is raised when a new Context is created for an application session. This happens when the
* first client connects to your application.
* @event
*/
public onConnection(handler: (context: Context, params: ParameterSet) => void): this {
this.emitter.addListener('connection', handler);
return this;
}
}