@mattduffy/banner 1.2.0 → 1.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc.cjs +5 -3
- package/Readme.md +1 -1
- package/package.json +3 -2
- package/src/index.js +75 -55
package/.eslintrc.cjs
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
const restrictedGlobals = require('eslint-restricted-globals')
|
|
2
|
+
|
|
1
3
|
module.exports = {
|
|
2
4
|
globals: {
|
|
3
5
|
window: true,
|
|
@@ -15,9 +17,9 @@ module.exports = {
|
|
|
15
17
|
extends: 'airbnb-base',
|
|
16
18
|
overrides: [
|
|
17
19
|
{
|
|
18
|
-
files: [
|
|
20
|
+
files: ['public/j/worker.js'],
|
|
19
21
|
rules: {
|
|
20
|
-
'no-restricted-globals': ['error', 'isFinite', 'isNaN'].concat(restrictedGlobals),
|
|
22
|
+
'no-restricted-globals': ['error', 'isFinite', 'isNaN'].concat(restrictedGlobals),
|
|
21
23
|
},
|
|
22
24
|
},
|
|
23
25
|
],
|
|
@@ -31,7 +33,7 @@ module.exports = {
|
|
|
31
33
|
'no-underscore-dangle': 'off',
|
|
32
34
|
'import/extensions': 'off',
|
|
33
35
|
'import/prefer-default-export': 'off',
|
|
34
|
-
'max-len': ['error', {
|
|
36
|
+
'max-len': ['error', { code: 100 }],
|
|
35
37
|
'new-cap': 'off',
|
|
36
38
|
},
|
|
37
39
|
}
|
package/Readme.md
CHANGED
|
@@ -31,7 +31,7 @@ app.use(banner.use())
|
|
|
31
31
|
Emits at the beginning of each client request.
|
|
32
32
|
#################################################################
|
|
33
33
|
# GET: https://dev.example.com/map/getToken?debug=verbose
|
|
34
|
-
# Referer: https://dev.
|
|
34
|
+
# Referer: https://dev.example.com/?debug=verbose
|
|
35
35
|
# From IP: 192.168.1.254
|
|
36
36
|
#################################################################
|
|
37
37
|
*/
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mattduffy/banner",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.1",
|
|
4
4
|
"description": "Displays server start-up information.",
|
|
5
5
|
"author": "Matthew Duffy",
|
|
6
6
|
"license": "ISC",
|
|
@@ -28,6 +28,7 @@
|
|
|
28
28
|
},
|
|
29
29
|
"keywords": [],
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"debug": "4.4.1"
|
|
31
|
+
"debug": "4.4.1",
|
|
32
|
+
"eslint-restricted-globals": "0.2.0"
|
|
32
33
|
}
|
|
33
34
|
}
|
package/src/index.js
CHANGED
|
@@ -7,8 +7,8 @@
|
|
|
7
7
|
import Debug from 'debug'
|
|
8
8
|
|
|
9
9
|
Debug.log = console.log.bind(console)
|
|
10
|
-
const log = Debug('banner')
|
|
11
|
-
const error = log.extend('ERROR')
|
|
10
|
+
// const log = Debug('banner')
|
|
11
|
+
// const error = log.extend('ERROR')
|
|
12
12
|
|
|
13
13
|
/**
|
|
14
14
|
* A class to create and emit a start-up banner of Koajs based apps.
|
|
@@ -18,24 +18,43 @@ const error = log.extend('ERROR')
|
|
|
18
18
|
*/
|
|
19
19
|
export class Banner {
|
|
20
20
|
#appName
|
|
21
|
+
|
|
21
22
|
#arch
|
|
23
|
+
|
|
22
24
|
#bannerText
|
|
25
|
+
|
|
23
26
|
#borderGlyphGET = '#'
|
|
27
|
+
|
|
24
28
|
#borderGlyphPUT = '&'
|
|
29
|
+
|
|
25
30
|
#borderGlyphPOST = '@'
|
|
31
|
+
|
|
26
32
|
#borderGlyphDELETE = '*'
|
|
33
|
+
|
|
27
34
|
#ipAddress
|
|
35
|
+
|
|
28
36
|
#lineStarts = []
|
|
37
|
+
|
|
29
38
|
#lines = []
|
|
39
|
+
|
|
30
40
|
#local
|
|
41
|
+
|
|
31
42
|
#localPort
|
|
43
|
+
|
|
32
44
|
#nodejs
|
|
45
|
+
|
|
33
46
|
#nodeLts
|
|
47
|
+
|
|
34
48
|
#nodeName
|
|
49
|
+
|
|
35
50
|
#nodeVersion
|
|
51
|
+
|
|
36
52
|
#platform
|
|
53
|
+
|
|
37
54
|
#public
|
|
55
|
+
|
|
38
56
|
#startingup
|
|
57
|
+
|
|
39
58
|
/**
|
|
40
59
|
* Create an instance of the Banner class.
|
|
41
60
|
* @param { object } [strings] - An object literal of strings to display in the banner.
|
|
@@ -46,7 +65,7 @@ export class Banner {
|
|
|
46
65
|
* @param { string } strings.public - The public web address the app is accesssible at.
|
|
47
66
|
* @returns { Banner}
|
|
48
67
|
*/
|
|
49
|
-
constructor(strings=null) {
|
|
68
|
+
constructor(strings = null) {
|
|
50
69
|
this.#arch = process.arch
|
|
51
70
|
this.#nodeLts = process.release?.lts ?? null
|
|
52
71
|
this.#nodeName = process.release.name
|
|
@@ -64,7 +83,7 @@ export class Banner {
|
|
|
64
83
|
this.#borderGlyphGET = strings?.borderGlyph ?? this.#borderGlyphGET
|
|
65
84
|
this.#localPort = strings?.localPort ?? null
|
|
66
85
|
this.#ipAddress = strings?.ip ?? null
|
|
67
|
-
this.#local = `${strings.local}${(this.#localPort) ?
|
|
86
|
+
this.#local = `${strings.local}${(this.#localPort) ? `: ${this.#localPort}` : ''}`
|
|
68
87
|
this.#public = strings.public
|
|
69
88
|
this.#startingup = strings.name
|
|
70
89
|
}
|
|
@@ -79,63 +98,62 @@ export class Banner {
|
|
|
79
98
|
* @throws { Error } - Throws an exception if name, public, local values are missing.
|
|
80
99
|
* @return { void }
|
|
81
100
|
*/
|
|
82
|
-
#compose
|
|
101
|
+
#compose() {
|
|
83
102
|
if (!this.#appName || !this.#local || !this.#public) {
|
|
84
103
|
throw new Error('Missing required inputs.')
|
|
85
104
|
}
|
|
86
105
|
this.startingupLine = `${this.startingupLabel}: ${this.#startingup}`
|
|
87
106
|
this.#lineStarts.push(this.startingupLine)
|
|
88
107
|
this.localLine = `${this.localLabel}: `
|
|
89
|
-
+ `${/^https?:\/\//.test(this.#local) ? this.#local :
|
|
108
|
+
+ `${/^https?:\/\//.test(this.#local) ? `${this.#local}` : `http://${this.#local}`}`
|
|
90
109
|
this.#lineStarts.push(this.localLine)
|
|
91
110
|
this.publicLine = `${this.publicLabel}: `
|
|
92
|
-
+ `${/^https?:\/\//.test(this.#public) ? this.#public :
|
|
111
|
+
+ `${/^https?:\/\//.test(this.#public) ? `${this.#public}` : `https://${this.#public}`}`
|
|
93
112
|
this.#lineStarts.push(this.publicLine)
|
|
94
113
|
this.archLine = `${this.archLabel}: ${this.#arch} ${this.#platform}`
|
|
95
114
|
this.#lineStarts.push(this.archLine)
|
|
96
115
|
this.nodejsLine = `${this.nodejsLabel}: ${this.#nodeName} ${this.#nodeVersion} `
|
|
97
|
-
+ `${(this.#nodeLts) ?
|
|
116
|
+
+ `${(this.#nodeLts) ? `(${this.#nodeLts})` : ''}`
|
|
98
117
|
this.#lineStarts.push(this.nodejsLine)
|
|
99
118
|
|
|
100
|
-
this.startingupLine = this.startingupLine
|
|
101
|
-
(this.longestLabel - this.startingupLine.indexOf(':'))
|
|
102
|
-
|
|
119
|
+
this.startingupLine = this.startingupLine
|
|
120
|
+
.padStart((this.longestLabel - this.startingupLine.indexOf(':'))
|
|
121
|
+
+ this.startingupLine.length, ' ')
|
|
103
122
|
this.#lines[0] = this.startingupLine
|
|
104
123
|
|
|
105
|
-
this.localLine = this.localLine
|
|
106
|
-
(this.longestLabel - this.localLine.indexOf(':'))
|
|
107
|
-
|
|
124
|
+
this.localLine = this.localLine
|
|
125
|
+
.padStart((this.longestLabel - this.localLine.indexOf(':'))
|
|
126
|
+
+ this.localLine.length, ' ')
|
|
108
127
|
this.#lines[1] = this.localLine
|
|
109
128
|
|
|
110
|
-
this.publicLine = this.publicLine
|
|
111
|
-
(this.longestLabel - this.publicLine.indexOf(':'))
|
|
112
|
-
|
|
129
|
+
this.publicLine = this.publicLine
|
|
130
|
+
.padStart((this.longestLabel - this.publicLine.indexOf(':'))
|
|
131
|
+
+ this.publicLine.length, ' ')
|
|
113
132
|
this.#lines[2] = this.publicLine
|
|
114
133
|
|
|
115
|
-
this.nodejsLine = this.nodejsLine
|
|
116
|
-
(this.longestLabel - this.nodejsLine.indexOf(':'))
|
|
117
|
-
|
|
134
|
+
this.nodejsLine = this.nodejsLine
|
|
135
|
+
.padStart((this.longestLabel - this.nodejsLine.indexOf(':'))
|
|
136
|
+
+ this.nodejsLine.length, ' ')
|
|
118
137
|
this.#lines[3] = this.nodejsLine
|
|
119
138
|
|
|
120
|
-
this.archLine = this.archLine
|
|
121
|
-
(this.longestLabel - this.archLine.indexOf(':'))
|
|
122
|
-
|
|
139
|
+
this.archLine = this.archLine
|
|
140
|
+
.padStart((this.longestLabel - this.archLine.indexOf(':'))
|
|
141
|
+
+ this.archLine.length, ' ')
|
|
123
142
|
this.#lines[4] = this.archLine
|
|
124
143
|
|
|
125
144
|
const g = this.#borderGlyphGET
|
|
126
|
-
this.#bannerText =
|
|
127
|
-
`${g}${g.padEnd(this.longestLine + 5, g)}${g}\n`
|
|
145
|
+
this.#bannerText = `${g}${g.padEnd(this.longestLine + 5, g)}${g}\n`
|
|
128
146
|
+ `${g} ${' '.padEnd(this.longestLine + 2, ' ')} ${g}\n`
|
|
129
|
-
+ `${g} ${this.startingupLine}${' '
|
|
130
|
-
(this.longestLine - this.startingupLine.length) + 3, ' ')} ${g}\n`
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
147
|
+
+ `${g} ${this.startingupLine}${' '
|
|
148
|
+
.padEnd((this.longestLine - this.startingupLine.length) + 3, ' ')} ${g}\n`
|
|
149
|
+
+ `${g} ${this.localLine}${' '
|
|
150
|
+
.padEnd((this.longestLine - this.localLine.length) + 3, ' ')} ${g}\n`
|
|
151
|
+
+ `${g} ${this.publicLine}${' '
|
|
152
|
+
.padEnd((this.longestLine - this.publicLine.length) + 3, ' ')} ${g}\n`
|
|
153
|
+
+ `${g} ${this.nodejsLine}${' '
|
|
154
|
+
.padEnd((this.longestLine - this.nodejsLine.length) + 3, ' ')} ${g}\n`
|
|
155
|
+
+ `${g} ${this.archLine}${' '
|
|
156
|
+
.padEnd((this.longestLine - this.archLine.length) + 3, ' ')} ${g}\n`
|
|
139
157
|
+ `${g} ${' '.padEnd(this.longestLine + 2, ' ')} ${g}\n`
|
|
140
158
|
+ `${g}${g.padEnd(this.longestLine + 5, g)}${g}\n`
|
|
141
159
|
}
|
|
@@ -166,13 +184,13 @@ export class Banner {
|
|
|
166
184
|
}
|
|
167
185
|
}
|
|
168
186
|
|
|
169
|
-
|
|
187
|
+
/**
|
|
170
188
|
* Set the local address displayed in the banner.
|
|
171
189
|
* @type { string }
|
|
172
190
|
* @param { string } local - The local address displayed in the banner.
|
|
173
191
|
*/
|
|
174
192
|
set local(local) {
|
|
175
|
-
this.#local = `${local}${(this.#localPort) ?
|
|
193
|
+
this.#local = `${local}${(this.#localPort) ? `:${this.#localPort}` : ''}`
|
|
176
194
|
if (this.#appName && this.#public) {
|
|
177
195
|
this.#compose()
|
|
178
196
|
}
|
|
@@ -254,24 +272,24 @@ export class Banner {
|
|
|
254
272
|
|
|
255
273
|
/**
|
|
256
274
|
* Create a middleware method that generates a banner for each request.
|
|
257
|
-
* @param { function } [
|
|
258
|
-
* @param { function } [
|
|
275
|
+
* @param { function } [Log] - an optional reference to the app level logging function.
|
|
276
|
+
* @param { function } [Error] - an optional reference to the app level error logging function.
|
|
259
277
|
* @returns { (ctx:object, next:function) => mixed } - Koa middleware function.
|
|
260
278
|
*/
|
|
261
|
-
use(
|
|
262
|
-
const _log =
|
|
263
|
-
const _error =
|
|
279
|
+
use(Log = null, Error = null) {
|
|
280
|
+
const _log = Log ?? console.log
|
|
281
|
+
// const _error = Error ?? console.error
|
|
264
282
|
_log('adding request banner to the app.')
|
|
265
283
|
const gGET = this.#borderGlyphGET
|
|
266
284
|
const gPUT = this.#borderGlyphPUT
|
|
267
285
|
const gPOST = this.#borderGlyphPOST
|
|
268
286
|
const gDEL = this.#borderGlyphDELETE
|
|
269
|
-
const n = this.#appName
|
|
270
|
-
return async function banner(ctx, next = null){
|
|
287
|
+
// const n = this.#appName
|
|
288
|
+
return async function banner(ctx, next = null) {
|
|
271
289
|
let _requestBanner
|
|
272
290
|
try {
|
|
273
291
|
let _g
|
|
274
|
-
switch(ctx.request.method.toLowerCase()) {
|
|
292
|
+
switch (ctx.request.method.toLowerCase()) {
|
|
275
293
|
case 'get':
|
|
276
294
|
_g = gGET
|
|
277
295
|
break
|
|
@@ -288,18 +306,18 @@ export class Banner {
|
|
|
288
306
|
_g = gGET
|
|
289
307
|
}
|
|
290
308
|
if (!ctx) {
|
|
291
|
-
throw new Error('Missing required ctx object.')
|
|
309
|
+
throw new Error('Missing required ctx object.')
|
|
292
310
|
}
|
|
293
311
|
if (!ctx.request.header.host) {
|
|
294
|
-
throw new Error('Missing required request header.host value.')
|
|
312
|
+
throw new Error('Missing required request header.host value.')
|
|
295
313
|
}
|
|
296
314
|
if (!ctx.request.method) {
|
|
297
|
-
throw new Error('Missing required request method value.')
|
|
315
|
+
throw new Error('Missing required request method value.')
|
|
298
316
|
}
|
|
299
317
|
if (!ctx.request.url) {
|
|
300
|
-
throw new Error('Missing required request url value.')
|
|
318
|
+
throw new Error('Missing required request url value.')
|
|
301
319
|
}
|
|
302
|
-
const _urlLabel = `${ctx.request.method}:`
|
|
320
|
+
const _urlLabel = `${ctx.request.method}:`
|
|
303
321
|
const _url = `${ctx.request.protocol}://${ctx.request.header.host}${ctx.request.url}`
|
|
304
322
|
let _urlLine = `${_urlLabel} ${_url}`
|
|
305
323
|
const _refLabel = 'Referer:'
|
|
@@ -315,21 +333,23 @@ export class Banner {
|
|
|
315
333
|
return (c.indexOf(':') + 1)
|
|
316
334
|
}, '')
|
|
317
335
|
_refLine = _refLine.padStart(
|
|
318
|
-
(_longestLabel - _refLine.indexOf(':')) + _refLine.length,
|
|
336
|
+
(_longestLabel - _refLine.indexOf(':')) + _refLine.length,
|
|
337
|
+
' ',
|
|
319
338
|
)
|
|
320
339
|
_urlLine = _urlLine.padStart(
|
|
321
|
-
(_longestLabel - _urlLine.indexOf(':')) + _urlLine.length,
|
|
340
|
+
(_longestLabel - _urlLine.indexOf(':')) + _urlLine.length,
|
|
341
|
+
' ',
|
|
322
342
|
)
|
|
323
343
|
_ipLine = _ipLine.padStart(
|
|
324
|
-
(_longestLabel - _ipLine.indexOf(':')) + _ipLine.length,
|
|
344
|
+
(_longestLabel - _ipLine.indexOf(':')) + _ipLine.length,
|
|
345
|
+
' ',
|
|
325
346
|
)
|
|
326
347
|
const _longestLine = [_urlLine, _refLine, _ipLine].reduce((a, c) => {
|
|
327
348
|
if (a > c.length) return a
|
|
328
349
|
return c.length
|
|
329
350
|
}, '')
|
|
330
351
|
// _log('request banner _longestLine', _longestLine)
|
|
331
|
-
_requestBanner =
|
|
332
|
-
`${_g.padEnd(_longestLine + 5, _g)}\n`
|
|
352
|
+
_requestBanner = `${_g.padEnd(_longestLine + 5, _g)}\n`
|
|
333
353
|
+ `${_g} ${_urlLine}\n`
|
|
334
354
|
+ `${_g} ${_refLine}\n`
|
|
335
355
|
+ `${_g} ${_ipLine}\n`
|