@mattduffy/banner 1.0.0 → 1.1.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/package.json +1 -1
- package/src/index.js +43 -12
- package/test/test.js +26 -3
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -20,7 +20,10 @@ export class Banner {
|
|
|
20
20
|
#appName
|
|
21
21
|
#arch
|
|
22
22
|
#bannerText
|
|
23
|
-
#
|
|
23
|
+
#borderGlyphGET = '#'
|
|
24
|
+
#borderGlyphPUT = '&'
|
|
25
|
+
#borderGlyphPOST = '@'
|
|
26
|
+
#borderGlyphDELETE = '*'
|
|
24
27
|
#lineStarts = []
|
|
25
28
|
#lines = []
|
|
26
29
|
#local
|
|
@@ -56,7 +59,7 @@ export class Banner {
|
|
|
56
59
|
|
|
57
60
|
if (strings) {
|
|
58
61
|
this.#appName = strings.name
|
|
59
|
-
this.#
|
|
62
|
+
this.#borderGlyphGET = strings?.borderGlyph ?? this.#borderGlyphGET
|
|
60
63
|
this.#localPort = strings?.localPort ?? null
|
|
61
64
|
this.#local = `${strings.local}${(this.#localPort) ? ':' + this.#localPort : ''}`
|
|
62
65
|
this.#public = strings.public
|
|
@@ -116,7 +119,7 @@ export class Banner {
|
|
|
116
119
|
+ this.archLine.length, ' ')
|
|
117
120
|
this.#lines[4] = this.archLine
|
|
118
121
|
|
|
119
|
-
const g = this.#
|
|
122
|
+
const g = this.#borderGlyphGET
|
|
120
123
|
this.#bannerText =
|
|
121
124
|
`${g}${g.padEnd(this.longestLine + 5, g)}${g}\n`
|
|
122
125
|
+ `${g} ${' '.padEnd(this.longestLine + 2, ' ')} ${g}\n`
|
|
@@ -203,7 +206,7 @@ export class Banner {
|
|
|
203
206
|
* @param { string } glyph - The border glyph character of the banner.
|
|
204
207
|
*/
|
|
205
208
|
set borderGlyph(glyph) {
|
|
206
|
-
this.#
|
|
209
|
+
this.#borderGlyphGET = glyph
|
|
207
210
|
if (this.#appName && this.#local && this.#public) {
|
|
208
211
|
this.#compose()
|
|
209
212
|
}
|
|
@@ -256,29 +259,57 @@ export class Banner {
|
|
|
256
259
|
const _log = log ?? console.log
|
|
257
260
|
const _error = error ?? console.error
|
|
258
261
|
_log('adding request banner to the app.')
|
|
259
|
-
const
|
|
262
|
+
const gGET = this.#borderGlyphGET
|
|
263
|
+
const gPUT = this.#borderGlyphPUT
|
|
264
|
+
const gPOST = this.#borderGlyphPOST
|
|
265
|
+
const gDEL = this.#borderGlyphDELETE
|
|
260
266
|
const n = this.#appName
|
|
261
267
|
return async function banner(ctx, next){
|
|
262
268
|
try {
|
|
263
|
-
const _g = (/post/i.test(ctx.request.method)) ? '@' : g
|
|
269
|
+
// const _g = (/post/i.test(ctx.request.method)) ? '@' : g
|
|
270
|
+
let _g
|
|
271
|
+
switch(ctx.request.method.toLowerCase()) {
|
|
272
|
+
case 'get':
|
|
273
|
+
_g = gGET
|
|
274
|
+
break
|
|
275
|
+
case 'post':
|
|
276
|
+
_g = gPOST
|
|
277
|
+
break
|
|
278
|
+
case 'put':
|
|
279
|
+
_g = gPUT
|
|
280
|
+
break
|
|
281
|
+
case 'delete':
|
|
282
|
+
_g = gDEL
|
|
283
|
+
break
|
|
284
|
+
default:
|
|
285
|
+
_g = gGET
|
|
286
|
+
}
|
|
287
|
+
_log('ctx.request.header.host', ctx.request.header.host)
|
|
264
288
|
const _urlLabel = `${ctx.request.method}:`
|
|
265
|
-
const _url = `${ctx.request.header.host}${ctx.request.url}`
|
|
266
|
-
|
|
289
|
+
const _url = `${ctx.request.protocol}://${ctx.request.header.host}${ctx.request.url}`
|
|
290
|
+
let _urlLine = `${_urlLabel} ${_url}`
|
|
267
291
|
const _refLabel = 'Referer:'
|
|
268
292
|
const _ref = ctx.request.header.referer ?? '<emtpy header field>'
|
|
269
|
-
|
|
293
|
+
let _refLine = `${_refLabel} ${_ref}`
|
|
270
294
|
const _longestLabel = [_urlLabel, _refLabel].reduce((a, c) => {
|
|
271
|
-
if (a > (c.indexOf(':') + 1)) {
|
|
295
|
+
if (a.length > (c.indexOf(':') + 1)) {
|
|
272
296
|
return a
|
|
273
297
|
}
|
|
274
|
-
return (c.indexOf(':' + 1)
|
|
298
|
+
return (c.indexOf(':') + 1)
|
|
275
299
|
}, '')
|
|
300
|
+
_refLine = _refLine.padStart(
|
|
301
|
+
(_longestLabel - _refLine.indexOf(':')) + _refLine.length, ' '
|
|
302
|
+
)
|
|
303
|
+
_urlLine = _urlLine.padStart(
|
|
304
|
+
(_longestLabel - _urlLine.indexOf(':')) + _urlLine.length, ' '
|
|
305
|
+
)
|
|
276
306
|
const _longestLine = [_urlLine, _refLine].reduce((a, c) => {
|
|
277
307
|
if (a > c.length) return a
|
|
278
308
|
return c.length
|
|
279
309
|
}, '')
|
|
280
310
|
// _log('request banner _longestLine', _longestLine)
|
|
281
|
-
const _requestBanner =
|
|
311
|
+
const _requestBanner =
|
|
312
|
+
`${_g.padEnd(_longestLine + 5, _g)}\n`
|
|
282
313
|
+ `${_g} ${_urlLine}\n`
|
|
283
314
|
+ `${_g} ${_refLine}\n`
|
|
284
315
|
+ `${_g.padEnd(_longestLine + 5, _g)}`
|
package/test/test.js
CHANGED
|
@@ -14,16 +14,19 @@ let cfg
|
|
|
14
14
|
let ctx
|
|
15
15
|
let ctx_POST
|
|
16
16
|
let ctx_GET
|
|
17
|
+
let ctx_PUT
|
|
18
|
+
let ctx_DEL
|
|
17
19
|
let next
|
|
18
20
|
console.log(skip)
|
|
19
21
|
describe('First test suite for banner package', async () => {
|
|
20
22
|
before(() => {
|
|
21
23
|
ctx = {
|
|
22
24
|
request: {
|
|
25
|
+
protocol: 'https',
|
|
23
26
|
method: '',
|
|
24
27
|
url: '/a/really/long/url/to/a/special/page',
|
|
25
28
|
header: {
|
|
26
|
-
host: '
|
|
29
|
+
host: 'banner.test',
|
|
27
30
|
referer: 'https://googoogle.com',
|
|
28
31
|
},
|
|
29
32
|
},
|
|
@@ -31,6 +34,8 @@ describe('First test suite for banner package', async () => {
|
|
|
31
34
|
throw new Error(`Error code ${code}: ${msg}`)
|
|
32
35
|
}
|
|
33
36
|
}
|
|
37
|
+
ctx_DEL = { ...ctx }
|
|
38
|
+
ctx_PUT = { ...ctx }
|
|
34
39
|
ctx_POST = { ...ctx }
|
|
35
40
|
ctx_GET = { ...ctx }
|
|
36
41
|
next = async () => {
|
|
@@ -62,13 +67,15 @@ describe('First test suite for banner package', async () => {
|
|
|
62
67
|
assert(!banner.print())
|
|
63
68
|
})
|
|
64
69
|
|
|
65
|
-
it('Should work as a koajs middleware function.', async () => {
|
|
70
|
+
it('Should work as a koajs middleware function - POST method.', async () => {
|
|
66
71
|
ctx_POST = Object.assign(ctx_POST, ctx)
|
|
67
72
|
ctx_POST.request.method = 'POST'
|
|
68
73
|
console.log(ctx_POST)
|
|
69
74
|
const post = new Banner(ctx_POST)
|
|
70
75
|
assert(await post.use()(ctx, next))
|
|
76
|
+
})
|
|
71
77
|
|
|
78
|
+
it('Should work as a koajs middleware function - GET method.', async () => {
|
|
72
79
|
ctx_GET = Object.assign(ctx_GET, ctx)
|
|
73
80
|
ctx_GET.request.method = 'GET'
|
|
74
81
|
console.log(ctx_GET)
|
|
@@ -76,7 +83,23 @@ describe('First test suite for banner package', async () => {
|
|
|
76
83
|
assert(await get.use()(ctx, next))
|
|
77
84
|
})
|
|
78
85
|
|
|
79
|
-
it('Should
|
|
86
|
+
it('Should work as a koajs middleware function - PUT method.', async () => {
|
|
87
|
+
ctx_PUT = Object.assign(ctx_PUT, ctx)
|
|
88
|
+
ctx_PUT.request.method = 'PUT'
|
|
89
|
+
console.log(ctx_PUT)
|
|
90
|
+
const put = new Banner(ctx_PUT)
|
|
91
|
+
assert(await put.use()(ctx, next))
|
|
92
|
+
})
|
|
93
|
+
|
|
94
|
+
it('Should work as a koajs middleware function - DELETE method.', async () => {
|
|
95
|
+
ctx_DEL = Object.assign(ctx_DEL, ctx)
|
|
96
|
+
ctx_DEL.request.method = 'DELETE'
|
|
97
|
+
console.log(ctx_DEL)
|
|
98
|
+
const del = new Banner(ctx_DEL)
|
|
99
|
+
assert(await del.use()(ctx, next))
|
|
100
|
+
})
|
|
101
|
+
|
|
102
|
+
it('Should fail as a koajs middleware function, missing input parameters.', async () => {
|
|
80
103
|
ctx.url= ''
|
|
81
104
|
ctx.request.header.host = undefined
|
|
82
105
|
ctx.request.url = undefined
|