@mattduffy/banner 1.2.0 → 1.3.0
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 +152 -68
- package/test/test.js +47 -26
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.
|
|
3
|
+
"version": "1.3.0",
|
|
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,29 +272,30 @@ 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
|
|
278
296
|
case 'post':
|
|
279
297
|
_g = gPOST
|
|
298
|
+
console.info(ctx.request.body)
|
|
280
299
|
break
|
|
281
300
|
case 'put':
|
|
282
301
|
_g = gPUT
|
|
@@ -288,52 +307,117 @@ export class Banner {
|
|
|
288
307
|
_g = gGET
|
|
289
308
|
}
|
|
290
309
|
if (!ctx) {
|
|
291
|
-
throw new Error('Missing required ctx object.')
|
|
310
|
+
throw new Error('Missing required ctx object.')
|
|
292
311
|
}
|
|
293
312
|
if (!ctx.request.header.host) {
|
|
294
|
-
throw new Error('Missing required request header.host value.')
|
|
313
|
+
throw new Error('Missing required request header.host value.')
|
|
295
314
|
}
|
|
296
315
|
if (!ctx.request.method) {
|
|
297
|
-
throw new Error('Missing required request method value.')
|
|
316
|
+
throw new Error('Missing required request method value.')
|
|
298
317
|
}
|
|
299
318
|
if (!ctx.request.url) {
|
|
300
|
-
throw new Error('Missing required request url value.')
|
|
319
|
+
throw new Error('Missing required request url value.')
|
|
320
|
+
}
|
|
321
|
+
const labels = []
|
|
322
|
+
const values = []
|
|
323
|
+
const lines = []
|
|
324
|
+
const _urlLabel = `${ctx.request.method}:`
|
|
325
|
+
let _url = `${ctx.request.protocol}://${ctx.request.header.host}${ctx.request.url}`
|
|
326
|
+
const _queryStringIndex = _url.indexOf('?')
|
|
327
|
+
const _queryStringLabel = 'Query Params:'
|
|
328
|
+
let _queryString = ''
|
|
329
|
+
let _queryStringLine = ''
|
|
330
|
+
if (_queryStringIndex !== -1) {
|
|
331
|
+
_queryString = _url.slice(_url.indexOf('?'))
|
|
332
|
+
_queryStringLine = `${_queryStringLabel} ${_queryString}`
|
|
333
|
+
labels.push(_queryStringLabel)
|
|
334
|
+
values.push(_queryString)
|
|
335
|
+
lines.push(_queryStringLine)
|
|
336
|
+
// console.log('adding query string to request banner')
|
|
301
337
|
}
|
|
302
|
-
|
|
303
|
-
|
|
338
|
+
_url = (_queryString.length > 0) ? _url.slice(0, _url.indexOf('?')) : _url
|
|
339
|
+
values.push(_url)
|
|
304
340
|
let _urlLine = `${_urlLabel} ${_url}`
|
|
305
341
|
const _refLabel = 'Referer:'
|
|
306
342
|
const _ref = ctx.request.header.referer ?? '<emtpy header field>'
|
|
343
|
+
values.push(_ref)
|
|
307
344
|
let _refLine = `${_refLabel} ${_ref}`
|
|
308
345
|
const _ipLabel = 'From IP:'
|
|
309
346
|
const _ip = ctx.request.ip
|
|
347
|
+
values.push(_ip)
|
|
310
348
|
let _ipLine = `${_ipLabel} ${_ip}`
|
|
311
|
-
const
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
349
|
+
const _timestampLabel = 'Timestamp:'
|
|
350
|
+
const _timestamp = new Date().toLocaleString()
|
|
351
|
+
values.push(_timestamp)
|
|
352
|
+
let _timestampLine = `${_timestampLabel} ${_timestamp}`
|
|
353
|
+
labels.push(_urlLabel)
|
|
354
|
+
labels.push(_refLabel)
|
|
355
|
+
labels.push(_ipLabel)
|
|
356
|
+
labels.push(_timestampLabel)
|
|
357
|
+
// console.log(labels)
|
|
358
|
+
|
|
359
|
+
// console.log(values)
|
|
360
|
+
const _longestValue = values
|
|
361
|
+
.reduce((a, c) => {
|
|
362
|
+
if (a > c.length) return a
|
|
363
|
+
return c.length
|
|
364
|
+
}, '')
|
|
365
|
+
// console.log('longest value', _longestValue)
|
|
366
|
+
|
|
367
|
+
lines.push(_ipLine)
|
|
368
|
+
lines.push(_timestampLine)
|
|
369
|
+
lines.push(_urlLine)
|
|
370
|
+
lines.push(_refLine)
|
|
371
|
+
// console.log(lines)
|
|
372
|
+
const _longestLabel = labels
|
|
373
|
+
.reduce((a, c) => {
|
|
374
|
+
// console.log(a, c.indexOf(':') + 1)
|
|
375
|
+
if (a > (c.indexOf(':') + 1)) {
|
|
376
|
+
return a
|
|
377
|
+
}
|
|
378
|
+
return (c.indexOf(':') + 1)
|
|
379
|
+
}, '')
|
|
380
|
+
// console.log('longest label', _longestLabel)
|
|
317
381
|
_refLine = _refLine.padStart(
|
|
318
|
-
(_longestLabel - _refLine.indexOf(':')) + _refLine.length,
|
|
382
|
+
(_longestLabel - _refLine.indexOf(':')) + _refLine.length,
|
|
383
|
+
' ',
|
|
319
384
|
)
|
|
320
385
|
_urlLine = _urlLine.padStart(
|
|
321
|
-
(_longestLabel - _urlLine.indexOf(':')) + _urlLine.length,
|
|
386
|
+
(_longestLabel - _urlLine.indexOf(':')) + _urlLine.length,
|
|
387
|
+
' ',
|
|
322
388
|
)
|
|
389
|
+
if (_queryStringLine.length > 0) {
|
|
390
|
+
_queryStringLine = _queryStringLine.padStart(
|
|
391
|
+
(_longestLabel - _queryStringLine.indexOf(':')) + _queryStringLine.length,
|
|
392
|
+
' ',
|
|
393
|
+
)
|
|
394
|
+
}
|
|
323
395
|
_ipLine = _ipLine.padStart(
|
|
324
|
-
(_longestLabel - _ipLine.indexOf(':')) + _ipLine.length,
|
|
396
|
+
(_longestLabel - _ipLine.indexOf(':')) + _ipLine.length,
|
|
397
|
+
' ',
|
|
398
|
+
)
|
|
399
|
+
_timestampLine = _timestampLine.padStart(
|
|
400
|
+
(_longestLabel - _timestampLine.indexOf(':')) + _timestampLine.length,
|
|
401
|
+
' ',
|
|
325
402
|
)
|
|
326
|
-
const _longestLine =
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
//
|
|
331
|
-
|
|
332
|
-
|
|
403
|
+
const _longestLine = _longestLabel + _longestValue
|
|
404
|
+
// const _longestLine = lines
|
|
405
|
+
// .reduce((a, c) => {
|
|
406
|
+
// // console.log(a, c.length)
|
|
407
|
+
// if (a > c.length) return a
|
|
408
|
+
// return c.length
|
|
409
|
+
// }, '')
|
|
410
|
+
// console.log('longest line', _longestLine)
|
|
411
|
+
/* eslint-disable prefer-template */
|
|
412
|
+
const endDangler = 6
|
|
413
|
+
_requestBanner = `${_g.padEnd(_longestLine + endDangler, _g)}\n`
|
|
333
414
|
+ `${_g} ${_urlLine}\n`
|
|
415
|
+
+ `${(_queryStringLine.length > 0) ? _g + ' ' + _queryStringLine + '\n' : ''}`
|
|
334
416
|
+ `${_g} ${_refLine}\n`
|
|
335
417
|
+ `${_g} ${_ipLine}\n`
|
|
336
|
-
+ `${_g
|
|
418
|
+
+ `${_g} ${_timestampLine}\n`
|
|
419
|
+
+ `${_g.padEnd(_longestLine + endDangler, _g)}`
|
|
420
|
+
/* eslint-enable prefer-template */
|
|
337
421
|
_log(_requestBanner)
|
|
338
422
|
if (next) {
|
|
339
423
|
await next(ctx.request.method)
|
package/test/test.js
CHANGED
|
@@ -1,15 +1,17 @@
|
|
|
1
1
|
// import path from 'node:path'
|
|
2
2
|
// import { randomBytes } from 'node:crypto'
|
|
3
3
|
import {
|
|
4
|
-
after,
|
|
4
|
+
// after,
|
|
5
5
|
before,
|
|
6
6
|
describe,
|
|
7
7
|
it,
|
|
8
8
|
} from 'node:test'
|
|
9
9
|
import assert from 'node:assert/strict'
|
|
10
|
-
import fs from 'node:fs/promises'
|
|
10
|
+
// import fs from 'node:fs/promises'
|
|
11
11
|
import os from 'node:os'
|
|
12
12
|
import { Banner } from '../src/index.js'
|
|
13
|
+
|
|
14
|
+
/* eslint-disable camelcase */
|
|
13
15
|
const skip = { skip: true }
|
|
14
16
|
console.log(skip)
|
|
15
17
|
let cfg
|
|
@@ -22,23 +24,25 @@ let next
|
|
|
22
24
|
describe('First test suite for banner package', async () => {
|
|
23
25
|
before(() => {
|
|
24
26
|
function getLocalIpAddress() {
|
|
25
|
-
const networkInterfaces = os.networkInterfaces()
|
|
26
|
-
let localIpAddress = null
|
|
27
|
-
|
|
27
|
+
const networkInterfaces = os.networkInterfaces()
|
|
28
|
+
let localIpAddress = null
|
|
29
|
+
|
|
30
|
+
/* eslint-disable no-restricted-syntax */
|
|
31
|
+
/* eslint-disable guard-for-in */
|
|
28
32
|
for (const interfaceName in networkInterfaces) {
|
|
29
|
-
const networkInterface = networkInterfaces[interfaceName]
|
|
33
|
+
const networkInterface = networkInterfaces[interfaceName]
|
|
30
34
|
for (const details of networkInterface) {
|
|
31
35
|
// Check for IPv4, not internal (loopback), and a valid address
|
|
32
36
|
if (details.family === 'IPv4' && !details.internal) {
|
|
33
|
-
localIpAddress = details.address
|
|
34
|
-
break
|
|
37
|
+
localIpAddress = details.address
|
|
38
|
+
break // Found a suitable IPv4 address, exit inner loop
|
|
35
39
|
}
|
|
36
40
|
}
|
|
37
41
|
if (localIpAddress) {
|
|
38
|
-
break
|
|
42
|
+
break // Found a suitable IPv4 address, exit outer loop
|
|
39
43
|
}
|
|
40
44
|
}
|
|
41
|
-
return localIpAddress
|
|
45
|
+
return localIpAddress
|
|
42
46
|
}
|
|
43
47
|
ctx = {
|
|
44
48
|
request: {
|
|
@@ -71,21 +75,22 @@ describe('First test suite for banner package', async () => {
|
|
|
71
75
|
})
|
|
72
76
|
|
|
73
77
|
it('Should create a start-up banner instance, with constructor param.', () => {
|
|
74
|
-
const banner = new Banner(cfg)
|
|
78
|
+
const banner = new Banner(cfg)
|
|
75
79
|
assert(banner.bannerText.length > 0)
|
|
76
80
|
assert(typeof banner.bannerText === 'string')
|
|
77
81
|
})
|
|
78
|
-
|
|
82
|
+
|
|
79
83
|
it('Should fail', () => {
|
|
80
84
|
const banner = new Banner()
|
|
81
85
|
assert(!banner.print())
|
|
82
86
|
})
|
|
83
87
|
|
|
84
88
|
it('Should work as a koajs middleware function - POST method.', async () => {
|
|
85
|
-
ctx_POST = Object.assign({}, ctx)
|
|
89
|
+
// ctx_POST = Object.assign({}, ctx)
|
|
90
|
+
ctx_POST = { ...ctx }
|
|
86
91
|
ctx_POST.request.method = 'POST'
|
|
87
|
-
ctx_POST.throw = (code, err
|
|
88
|
-
throw new Error(`${code}, ${
|
|
92
|
+
ctx_POST.throw = (code, err) => {
|
|
93
|
+
throw new Error(`${code}, ${err}`)
|
|
89
94
|
}
|
|
90
95
|
console.log(ctx_POST)
|
|
91
96
|
const post = new Banner(ctx_POST)
|
|
@@ -93,10 +98,11 @@ describe('First test suite for banner package', async () => {
|
|
|
93
98
|
})
|
|
94
99
|
|
|
95
100
|
it('Should work as a koajs middleware function - GET method.', async () => {
|
|
96
|
-
ctx_GET = Object.assign({}, ctx)
|
|
101
|
+
// ctx_GET = Object.assign({}, ctx)
|
|
102
|
+
ctx_GET = { ...ctx }
|
|
97
103
|
ctx_GET.request.method = 'GET'
|
|
98
|
-
ctx_GET.throw = (code, err
|
|
99
|
-
throw new Error(`${code}, ${
|
|
104
|
+
ctx_GET.throw = (code, err) => {
|
|
105
|
+
throw new Error(`${code}, ${err}`)
|
|
100
106
|
}
|
|
101
107
|
console.log(ctx_GET)
|
|
102
108
|
const get = new Banner(ctx_GET)
|
|
@@ -104,10 +110,11 @@ describe('First test suite for banner package', async () => {
|
|
|
104
110
|
})
|
|
105
111
|
|
|
106
112
|
it('Should work as a koajs middleware function - PUT method.', async () => {
|
|
107
|
-
ctx_PUT = Object.assign({}, ctx)
|
|
113
|
+
// ctx_PUT = Object.assign({}, ctx)
|
|
114
|
+
ctx_PUT = { ...ctx }
|
|
108
115
|
ctx_PUT.request.method = 'PUT'
|
|
109
|
-
ctx_PUT.throw = (code, err
|
|
110
|
-
throw new Error(`${code}, ${
|
|
116
|
+
ctx_PUT.throw = (code, err) => {
|
|
117
|
+
throw new Error(`${code}, ${err}`)
|
|
111
118
|
}
|
|
112
119
|
console.log(ctx_PUT)
|
|
113
120
|
const put = new Banner(ctx_PUT)
|
|
@@ -115,22 +122,36 @@ describe('First test suite for banner package', async () => {
|
|
|
115
122
|
})
|
|
116
123
|
|
|
117
124
|
it('Should work as a koajs middleware function - DELETE method.', async () => {
|
|
118
|
-
ctx_DEL = Object.assign({}, ctx)
|
|
125
|
+
// ctx_DEL = Object.assign({}, ctx)
|
|
126
|
+
ctx_DEL = { ...ctx }
|
|
119
127
|
ctx_DEL.request.method = 'DELETE'
|
|
120
|
-
ctx_DEL.throw = (code, err
|
|
121
|
-
throw new Error(`${code}, ${
|
|
128
|
+
ctx_DEL.throw = (code, err) => {
|
|
129
|
+
throw new Error(`${code}, ${err}`)
|
|
122
130
|
}
|
|
123
131
|
console.log(ctx_DEL)
|
|
124
132
|
const del = new Banner(ctx_DEL)
|
|
125
133
|
assert(await del.use()(ctx_DEL, next))
|
|
126
134
|
})
|
|
127
135
|
|
|
128
|
-
it('Should
|
|
136
|
+
it('Should work as a koajs middleware function - GET method, with query string.', async () => {
|
|
137
|
+
// ctx_GET = Object.assign({}, ctx)
|
|
138
|
+
ctx_GET = { ...ctx }
|
|
139
|
+
ctx_GET.request.method = 'GET'
|
|
140
|
+
ctx_GET.request.url += '?param1=querty¶m2=12345¶m3=true'
|
|
141
|
+
ctx_GET.throw = (code, err) => {
|
|
142
|
+
throw new Error(`${code}, ${err}`)
|
|
143
|
+
}
|
|
144
|
+
console.log(ctx_GET)
|
|
145
|
+
const get = new Banner(ctx_GET)
|
|
146
|
+
assert(await get.use()(ctx_GET, next))
|
|
147
|
+
})
|
|
148
|
+
|
|
149
|
+
it('Should fail as a koajs middleware function, missing input parameters.', async () => {
|
|
129
150
|
ctx.request.header.host = null
|
|
130
151
|
ctx.request.url = null
|
|
131
152
|
ctx.throw = (code, err) => {
|
|
132
153
|
// console.log(err.message)
|
|
133
|
-
throw err
|
|
154
|
+
throw err
|
|
134
155
|
}
|
|
135
156
|
console.log(ctx)
|
|
136
157
|
const req = new Banner()
|