@thzero/library_server_fastify 0.17.8 → 0.17.9

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/openSource.js CHANGED
@@ -1,81 +1,81 @@
1
- export default () => {
2
- return [
3
- {
4
- category: 'server',
5
- name: '@fastify',
6
- url: 'https://github.com/fastify/fastify',
7
- licenseName: 'MIT',
8
- licenseUrl: 'https://github.com/fastify/fastify/blob/main/LICENSE'
9
- },
10
- {
11
- category: 'server',
12
- name: '@fastify/auth',
13
- url: 'https://github.com/fastify/fastify-auth',
14
- licenseName: 'MIT',
15
- licenseUrl: 'https://github.com/fastify/fastify-auth/blob/master/LICENSE'
16
- },
17
- {
18
- category: 'server',
19
- name: '@fastify/compress',
20
- url: 'https://github.com/fastify/compress',
21
- licenseName: 'MIT',
22
- licenseUrl: 'https://github.com/fastify/compress/blob/main/LICENSE'
23
- },
24
- {
25
- category: 'server',
26
- name: '@fastify/cors',
27
- url: 'https://github.com/fastify/cors',
28
- licenseName: 'MIT',
29
- licenseUrl: 'https://github.com/fastify/cors/blob/main/LICENSE'
30
- },
31
- {
32
- category: 'server',
33
- name: '@fastify/helmet',
34
- url: 'https://github.com/fastify/helmet',
35
- licenseName: 'MIT',
36
- licenseUrl: 'https://github.com/fastify/helmet/blob/main/LICENSE'
37
- },
38
- {
39
- category: 'server',
40
- name: '@fastify/routes',
41
- url: 'https://github.com/fastify/routes',
42
- licenseName: 'MIT',
43
- licenseUrl: 'https://github.com/fastify/routes/blob/main/LICENSE'
44
- },
45
- {
46
- category: 'server',
47
- name: '@fastify/static',
48
- url: 'https://github.com/fastify/static',
49
- licenseName: 'MIT',
50
- licenseUrl: 'https://github.com/fastify/static/blob/main/LICENSE'
51
- },
52
- {
53
- category: 'server',
54
- name: '@thzero/library_common',
55
- url: 'https://github.com/thzero/library_common',
56
- licenseName: 'MIT',
57
- licenseUrl: 'https://github.com/thzero/library_common/blob/master/license.md'
58
- },
59
- {
60
- category: 'server',
61
- name: '@thzero/library_common_service',
62
- url: 'https://github.com/thzero/library_common_service',
63
- licenseName: 'MIT',
64
- licenseUrl: 'https://github.com/thzero/library_common_service/blob/master/license.md'
65
- },
66
- {
67
- category: 'server',
68
- name: '@thzero/library_service',
69
- url: 'https://github.com/thzero/library_service',
70
- licenseName: 'MIT',
71
- licenseUrl: 'https://github.com/thzero/library_service/blob/master/license.md'
72
- },
73
- {
74
- category: 'server',
75
- name: '@thzero/library_server_fastify',
76
- url: 'https://github.com/thzero/library_server_fastify',
77
- licenseName: 'MIT',
78
- licenseUrl: 'https://github.com/thzero/library_server_fastify/blob/master/license.md'
79
- }
80
- ];
1
+ export default () => {
2
+ return [
3
+ {
4
+ category: 'server',
5
+ name: '@fastify',
6
+ url: 'https://github.com/fastify/fastify',
7
+ licenseName: 'MIT',
8
+ licenseUrl: 'https://github.com/fastify/fastify/blob/main/LICENSE'
9
+ },
10
+ {
11
+ category: 'server',
12
+ name: '@fastify/auth',
13
+ url: 'https://github.com/fastify/fastify-auth',
14
+ licenseName: 'MIT',
15
+ licenseUrl: 'https://github.com/fastify/fastify-auth/blob/master/LICENSE'
16
+ },
17
+ {
18
+ category: 'server',
19
+ name: '@fastify/compress',
20
+ url: 'https://github.com/fastify/compress',
21
+ licenseName: 'MIT',
22
+ licenseUrl: 'https://github.com/fastify/compress/blob/main/LICENSE'
23
+ },
24
+ {
25
+ category: 'server',
26
+ name: '@fastify/cors',
27
+ url: 'https://github.com/fastify/cors',
28
+ licenseName: 'MIT',
29
+ licenseUrl: 'https://github.com/fastify/cors/blob/main/LICENSE'
30
+ },
31
+ {
32
+ category: 'server',
33
+ name: '@fastify/helmet',
34
+ url: 'https://github.com/fastify/helmet',
35
+ licenseName: 'MIT',
36
+ licenseUrl: 'https://github.com/fastify/helmet/blob/main/LICENSE'
37
+ },
38
+ {
39
+ category: 'server',
40
+ name: '@fastify/routes',
41
+ url: 'https://github.com/fastify/routes',
42
+ licenseName: 'MIT',
43
+ licenseUrl: 'https://github.com/fastify/routes/blob/main/LICENSE'
44
+ },
45
+ {
46
+ category: 'server',
47
+ name: '@fastify/static',
48
+ url: 'https://github.com/fastify/static',
49
+ licenseName: 'MIT',
50
+ licenseUrl: 'https://github.com/fastify/static/blob/main/LICENSE'
51
+ },
52
+ {
53
+ category: 'server',
54
+ name: '@thzero/library_common',
55
+ url: 'https://github.com/thzero/library_common',
56
+ licenseName: 'MIT',
57
+ licenseUrl: 'https://github.com/thzero/library_common/blob/master/license.md'
58
+ },
59
+ {
60
+ category: 'server',
61
+ name: '@thzero/library_common_service',
62
+ url: 'https://github.com/thzero/library_common_service',
63
+ licenseName: 'MIT',
64
+ licenseUrl: 'https://github.com/thzero/library_common_service/blob/master/license.md'
65
+ },
66
+ {
67
+ category: 'server',
68
+ name: '@thzero/library_service',
69
+ url: 'https://github.com/thzero/library_service',
70
+ licenseName: 'MIT',
71
+ licenseUrl: 'https://github.com/thzero/library_service/blob/master/license.md'
72
+ },
73
+ {
74
+ category: 'server',
75
+ name: '@thzero/library_server_fastify',
76
+ url: 'https://github.com/thzero/library_server_fastify',
77
+ licenseName: 'MIT',
78
+ licenseUrl: 'https://github.com/thzero/library_server_fastify/blob/master/license.md'
79
+ }
80
+ ];
81
81
  }
package/package.json CHANGED
@@ -1,42 +1,42 @@
1
- {
2
- "name": "@thzero/library_server_fastify",
3
- "type": "module",
4
- "version": "0.17.8",
5
- "version_major": 0,
6
- "version_minor": 17,
7
- "version_patch": 8,
8
- "version_date": "04/15/2023",
9
- "description": "An opinionated library of common functionality to bootstrap a Fastify based API application.",
10
- "author": "thZero",
11
- "license": "MIT",
12
- "repository": {
13
- "type": "git",
14
- "url": "git+https://github.com/thzero/library_server_fastify.git"
15
- },
16
- "bugs": {
17
- "url": "https://github.com/thzero/library_server_fastify/issues"
18
- },
19
- "homepage": "https://github.com/thzero/library_server_fastify#readme",
20
- "engines": {
21
- "node": ">=12.8.3"
22
- },
23
- "scripts": {
24
- "cli-update": "library-cli --updateversion --pi",
25
- "test": "echo \"Error: no test specified\" && exit 1"
26
- },
27
- "dependencies": {
28
- "@fastify/auth": "^4.2.0",
29
- "@fastify/compress": "^6.2.0",
30
- "@fastify/cors": "^8.2.1",
31
- "@fastify/helmet": "^10.1.0",
32
- "@fastify/routes": "^5.1.0",
33
- "@fastify/static": "^6.9.0",
34
- "async-mutex": "^0.4.0",
35
- "fastify": "^4.14.1"
36
- },
37
- "peerDependencies": {
38
- "@thzero/library_common": "^0.17",
39
- "@thzero/library_common_service": "^0.17",
40
- "@thzero/library_server": "^0.17"
41
- }
1
+ {
2
+ "name": "@thzero/library_server_fastify",
3
+ "type": "module",
4
+ "version": "0.17.9",
5
+ "version_major": 0,
6
+ "version_minor": 17,
7
+ "version_patch": 9,
8
+ "version_date": "05/10/2023",
9
+ "description": "An opinionated library of common functionality to bootstrap a Fastify based API application.",
10
+ "author": "thZero",
11
+ "license": "MIT",
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "git+https://github.com/thzero/library_server_fastify.git"
15
+ },
16
+ "bugs": {
17
+ "url": "https://github.com/thzero/library_server_fastify/issues"
18
+ },
19
+ "homepage": "https://github.com/thzero/library_server_fastify#readme",
20
+ "engines": {
21
+ "node": ">=12.8.3"
22
+ },
23
+ "scripts": {
24
+ "cli-update": "library-cli --updateversion --pi",
25
+ "test": "echo \"Error: no test specified\" && exit 1"
26
+ },
27
+ "dependencies": {
28
+ "@fastify/auth": "^4.2.0",
29
+ "@fastify/compress": "^6.2.1",
30
+ "@fastify/cors": "^8.2.1",
31
+ "@fastify/helmet": "^10.1.1",
32
+ "@fastify/routes": "^5.1.0",
33
+ "@fastify/static": "^6.10.1",
34
+ "async-mutex": "^0.4.0",
35
+ "fastify": "^4.17.0"
36
+ },
37
+ "peerDependencies": {
38
+ "@thzero/library_common": "^0.17",
39
+ "@thzero/library_common_service": "^0.17",
40
+ "@thzero/library_server": "^0.17"
41
+ }
42
42
  }
package/plugins/apiKey.js CHANGED
@@ -1,49 +1,49 @@
1
- import fastifyPlugin from 'fastify-plugin';
2
-
3
- import LibraryServerConstants from '@thzero/library_server/constants.js';
4
-
5
- export default fastifyPlugin((instance, opts, done) => {
6
- instance.addHook('onRequest', (request, reply, next) => {
7
- if (request.originalUrl === '/favicon.ico') {
8
- next();
9
- return;
10
- }
11
-
12
- const key = request.headers[LibraryServerConstants.Headers.AuthKeys.API];
13
- // opts.logger.debug('KoaBootMain', 'start', 'auth-api-token.key', key);
14
- if (!String.isNullOrEmpty(key)) {
15
- const auth = request.config.get('auth');
16
- if (auth) {
17
- const apiKey = auth.apiKey;
18
- // this.loggerServiceI.debug('KoaBootMain', 'start', 'auth-api-token.apiKey', apiKey);
19
- // this.loggerServiceI.debug('KoaBootMain', 'start', 'auth-api-token.key===apiKey', (key === apiKey));
20
- if (key === apiKey) {
21
- request.apiKey = key;
22
- next();
23
- return;
24
- }
25
- }
26
- }
27
-
28
- (async () => {
29
- const usageMetrics = {
30
- url: request.routerPath,
31
- correlationId: request.correlationId,
32
- href: request.url,
33
- headers: request.headers,
34
- host: request.hostname,
35
- hostname: request.hostname,
36
- querystring: request.query,
37
- token: request.token
38
- };
39
- await opts.usageMetrics.register(usageMetrics).catch((err) => {
40
- opts.logger.error('FastifyBootMain', 'start', 'usageMetrics', err);
41
- });
42
- })();
43
-
44
- console.log('Unauthorized... auth-api-token failure');
45
- reply.status(401).send();
46
- });
47
-
48
- done();
1
+ import fastifyPlugin from 'fastify-plugin';
2
+
3
+ import LibraryServerConstants from '@thzero/library_server/constants.js';
4
+
5
+ export default fastifyPlugin((instance, opts, done) => {
6
+ instance.addHook('onRequest', (request, reply, next) => {
7
+ if (request.originalUrl === '/favicon.ico') {
8
+ next();
9
+ return;
10
+ }
11
+
12
+ const key = request.headers[LibraryServerConstants.Headers.AuthKeys.API];
13
+ // opts.logger.debug('KoaBootMain', 'start', 'auth-api-token.key', key);
14
+ if (!String.isNullOrEmpty(key)) {
15
+ const auth = request.config.get('auth');
16
+ if (auth) {
17
+ const apiKey = auth.apiKey;
18
+ // this.loggerServiceI.debug('KoaBootMain', 'start', 'auth-api-token.apiKey', apiKey);
19
+ // this.loggerServiceI.debug('KoaBootMain', 'start', 'auth-api-token.key===apiKey', (key === apiKey));
20
+ if (key === apiKey) {
21
+ request.apiKey = key;
22
+ next();
23
+ return;
24
+ }
25
+ }
26
+ }
27
+
28
+ (async () => {
29
+ const usageMetrics = {
30
+ url: request.routerPath,
31
+ correlationId: request.correlationId,
32
+ href: request.url,
33
+ headers: request.headers,
34
+ host: request.hostname,
35
+ hostname: request.hostname,
36
+ querystring: request.query,
37
+ token: request.token
38
+ };
39
+ await opts.usageMetrics.register(usageMetrics).catch((err) => {
40
+ opts.logger.error('FastifyBootMain', 'start', 'usageMetrics', err);
41
+ });
42
+ })();
43
+
44
+ console.log('Unauthorized... auth-api-token failure');
45
+ reply.status(401).send();
46
+ });
47
+
48
+ done();
49
49
  });
package/plugins/auth.js CHANGED
@@ -1,125 +1,125 @@
1
- 'use strict'
2
-
3
- // const fp = require('fastify-plugin')
4
- import fp from 'fastify-plugin'
5
- // const reusify = require('reusify')
6
- import reusify from 'reusify'
7
-
8
- function checkAuth (fastify, opts, next) {
9
- fastify.decorate('auth', auth)
10
- next()
11
- }
12
-
13
- function auth (functions, opts) {
14
- if (!Array.isArray(functions)) {
15
- throw new Error('You must give an array of functions to the auth function')
16
- }
17
- if (!functions.length) {
18
- throw new Error('Missing auth functions')
19
- }
20
-
21
- const options = Object.assign({
22
- relation: 'or',
23
- run: null
24
- }, opts)
25
-
26
- if (options.relation !== 'or' && options.relation !== 'and') {
27
- throw new Error('The value of options.relation should be one of [\'or\', \'and\']')
28
- }
29
- if (options.run && options.run !== 'all') {
30
- throw new Error('The value of options.run must be \'all\'')
31
- }
32
-
33
- /* eslint-disable-next-line no-var */
34
- for (var i = 0; i < functions.length; i++) {
35
- functions[i] = functions[i].bind(this)
36
- }
37
-
38
- const instance = reusify(Auth)
39
-
40
- function _auth (request, reply, done) {
41
- const obj = instance.get()
42
-
43
- obj.request = request
44
- obj.reply = reply
45
- obj.done = done
46
- obj.functions = this.functions
47
- obj.options = this.options
48
- obj.i = 0
49
- obj.start = true
50
- obj.firstResult = null
51
-
52
- obj.nextAuth()
53
- }
54
-
55
- return _auth.bind({ functions, options })
56
-
57
- function Auth () {
58
- this.next = null
59
- this.i = 0
60
- this.start = true
61
- this.functions = []
62
- this.options = {}
63
- this.request = null
64
- this.reply = null
65
- this.done = null
66
- this.firstResult = null
67
-
68
- const that = this
69
-
70
- this.nextAuth = function nextAuth (err) {
71
- const func = that.functions[that.i++]
72
-
73
- if (!func) {
74
- that.completeAuth(err)
75
- return
76
- }
77
-
78
- const maybePromise = func(that.request, that.reply, that.onAuth, that.options)
79
-
80
- if (maybePromise && typeof maybePromise.then === 'function') {
81
- maybePromise.then(results => that.onAuth(null, results), that.onAuth)
82
- }
83
- }
84
-
85
- this.onAuth = function onAuth (err, results) {
86
- if (that.options.relation === 'or') {
87
- if (err) {
88
- return that.nextAuth(err)
89
- }
90
-
91
- return that.completeAuth()
92
- } else {
93
- if (err) {
94
- return that.completeAuth(err)
95
- }
96
-
97
- return that.nextAuth(err)
98
- }
99
- }
100
-
101
- this.completeAuth = function (err) {
102
- if (that.start) {
103
- that.start = false
104
- that.firstResult = err
105
- }
106
-
107
- if (that.options.run === 'all' && that.i < that.functions.length) {
108
- return that.nextAuth(err)
109
- }
110
-
111
- if (that.firstResult && (!that.reply.raw.statusCode || that.reply.raw.statusCode < 400)) {
112
- that.reply.code(401)
113
- } else if (!that.firstResult && that.reply.raw.statusCode && that.reply.raw.statusCode >= 400) {
114
- that.reply.code(200)
115
- }
116
-
117
- that.done(that.firstResult)
118
- instance.release(that)
119
- }
120
- }
121
- }
122
-
123
- export default fp(checkAuth, {
124
- fastify: '4.x'
1
+ 'use strict'
2
+
3
+ // const fp = require('fastify-plugin')
4
+ import fp from 'fastify-plugin'
5
+ // const reusify = require('reusify')
6
+ import reusify from 'reusify'
7
+
8
+ function checkAuth (fastify, opts, next) {
9
+ fastify.decorate('auth', auth)
10
+ next()
11
+ }
12
+
13
+ function auth (functions, opts) {
14
+ if (!Array.isArray(functions)) {
15
+ throw new Error('You must give an array of functions to the auth function')
16
+ }
17
+ if (!functions.length) {
18
+ throw new Error('Missing auth functions')
19
+ }
20
+
21
+ const options = Object.assign({
22
+ relation: 'or',
23
+ run: null
24
+ }, opts)
25
+
26
+ if (options.relation !== 'or' && options.relation !== 'and') {
27
+ throw new Error('The value of options.relation should be one of [\'or\', \'and\']')
28
+ }
29
+ if (options.run && options.run !== 'all') {
30
+ throw new Error('The value of options.run must be \'all\'')
31
+ }
32
+
33
+ /* eslint-disable-next-line no-var */
34
+ for (var i = 0; i < functions.length; i++) {
35
+ functions[i] = functions[i].bind(this)
36
+ }
37
+
38
+ const instance = reusify(Auth)
39
+
40
+ function _auth (request, reply, done) {
41
+ const obj = instance.get()
42
+
43
+ obj.request = request
44
+ obj.reply = reply
45
+ obj.done = done
46
+ obj.functions = this.functions
47
+ obj.options = this.options
48
+ obj.i = 0
49
+ obj.start = true
50
+ obj.firstResult = null
51
+
52
+ obj.nextAuth()
53
+ }
54
+
55
+ return _auth.bind({ functions, options })
56
+
57
+ function Auth () {
58
+ this.next = null
59
+ this.i = 0
60
+ this.start = true
61
+ this.functions = []
62
+ this.options = {}
63
+ this.request = null
64
+ this.reply = null
65
+ this.done = null
66
+ this.firstResult = null
67
+
68
+ const that = this
69
+
70
+ this.nextAuth = function nextAuth (err) {
71
+ const func = that.functions[that.i++]
72
+
73
+ if (!func) {
74
+ that.completeAuth(err)
75
+ return
76
+ }
77
+
78
+ const maybePromise = func(that.request, that.reply, that.onAuth, that.options)
79
+
80
+ if (maybePromise && typeof maybePromise.then === 'function') {
81
+ maybePromise.then(results => that.onAuth(null, results), that.onAuth)
82
+ }
83
+ }
84
+
85
+ this.onAuth = function onAuth (err, results) {
86
+ if (that.options.relation === 'or') {
87
+ if (err) {
88
+ return that.nextAuth(err)
89
+ }
90
+
91
+ return that.completeAuth()
92
+ } else {
93
+ if (err) {
94
+ return that.completeAuth(err)
95
+ }
96
+
97
+ return that.nextAuth(err)
98
+ }
99
+ }
100
+
101
+ this.completeAuth = function (err) {
102
+ if (that.start) {
103
+ that.start = false
104
+ that.firstResult = err
105
+ }
106
+
107
+ if (that.options.run === 'all' && that.i < that.functions.length) {
108
+ return that.nextAuth(err)
109
+ }
110
+
111
+ if (that.firstResult && (!that.reply.raw.statusCode || that.reply.raw.statusCode < 400)) {
112
+ that.reply.code(401)
113
+ } else if (!that.firstResult && that.reply.raw.statusCode && that.reply.raw.statusCode >= 400) {
114
+ that.reply.code(200)
115
+ }
116
+
117
+ that.done(that.firstResult)
118
+ instance.release(that)
119
+ }
120
+ }
121
+ }
122
+
123
+ export default fp(checkAuth, {
124
+ fastify: '4.x'
125
125
  })