UNPKG

1.84 kBJavaScriptView Raw
1"use strict";
2
3const debug = require("debug")("motif:context");
4
5class Context {
6 get port() {
7 return this._port;
8 }
9
10 get config() {
11 return this._config;
12 }
13
14 get appPath() {
15 return this._appPath;
16 }
17
18 get path() {
19 return this._path;
20 }
21
22 get createDoc() {
23 return this._createDoc;
24 }
25
26 get useCookie() {
27 return this._useCookie;
28 }
29
30 get useProxy() {
31 return this._useProxy;
32 }
33
34 get session() {
35 return this._session;
36 }
37
38 get prefix() {
39 return this._prefix;
40 }
41
42 _normalizePath(val) {
43 if(val && val.indexOf("/") === 0) {
44 return val.substr(1);
45 }
46
47 return val;
48 }
49
50 _normalizePort(val) {
51 let port = parseInt(val, 10);
52 if(isNaN(port)) {
53 return val;
54 }
55
56 if(port >= 0) {
57 return port;
58 }
59
60 return false;
61 }
62
63 constructor(options) {
64 let {
65 appPath,
66 config,
67 path,
68 port,
69 createDoc,
70 useFavicon,
71 useProxy,
72 useCookie,
73 session,
74 prefix
75 } = options;
76
77 debug("context", options);
78
79 this._appPath = appPath;
80 this._config = config;
81 this._path = this._normalizePath(path || "");
82 this._port = this._normalizePort(port || 3000);
83 this._createDoc = createDoc === true ? true : false;
84 this._useFavicon = useFavicon === true ? true : false;
85 this._useCookie = useCookie === true ? true : false;
86 this._useProxy = useProxy === true ? true : false;
87 this._session = session || null;
88 this._prefix = prefix || 'motif';
89
90 this._awsConfig = (this._config && this._config.aws) || {};
91
92 if(this._awsConfig.accessKeyId) {
93 process.env.AWS_ACCESS_KEY_ID = this._awsConfig.accessKeyId;
94 }
95 if(this._awsConfig.secretAccessKey) {
96 process.env.AWS_SECRET_ACCESS_KEY = this._awsConfig.secretAccessKey;
97 }
98 }
99}
100
101module.exports = Context;