@thzero/library_server_fastify 0.18.11 → 0.18.13

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.18.11",
5
- "version_major": 0,
6
- "version_minor": 18,
7
- "version_patch": 11,
8
- "version_date": "07/23/2024",
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.6.1",
29
- "@fastify/compress": "^7.0.3",
30
- "@fastify/cors": "^9.0.1",
31
- "@fastify/helmet": "^11.1.1",
32
- "@fastify/routes": "^5.1.0",
33
- "@fastify/static": "^7.0.4",
34
- "async-mutex": "^0.5.0",
35
- "fastify": "^4.28.1"
36
- },
37
- "peerDependencies": {
38
- "@thzero/library_common": "^0.18",
39
- "@thzero/library_common_service": "^0.18",
40
- "@thzero/library_server": "^0.18"
41
- }
1
+ {
2
+ "name": "@thzero/library_server_fastify",
3
+ "type": "module",
4
+ "version": "0.18.13",
5
+ "version_major": 0,
6
+ "version_minor": 18,
7
+ "version_patch": 13,
8
+ "version_date": "11/01/2024",
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": "^5.0.1",
29
+ "@fastify/compress": "^8.0.1",
30
+ "@fastify/cors": "^10.0.1",
31
+ "@fastify/helmet": "^12.0.1",
32
+ "@fastify/routes": "^6.0.1",
33
+ "@fastify/static": "^8.0.2",
34
+ "async-mutex": "^0.5.0",
35
+ "fastify": "^5.0.0"
36
+ },
37
+ "peerDependencies": {
38
+ "@thzero/library_common": "^0.18",
39
+ "@thzero/library_common_service": "^0.18",
40
+ "@thzero/library_server": "^0.18"
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.routeOptions.url,
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.routeOptions.url,
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,127 +1,127 @@
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
- import LibraryCommonConstants from '@thzero/library_common/constants.js';
9
-
10
- function checkAuth (fastify, opts, next) {
11
- fastify.decorate('auth', auth)
12
- next()
13
- }
14
-
15
- function auth (functions, opts) {
16
- if (!Array.isArray(functions)) {
17
- throw new Error('You must give an array of functions to the auth function')
18
- }
19
- if (!functions.length) {
20
- throw new Error('Missing auth functions')
21
- }
22
-
23
- const options = Object.assign({
24
- relation: LibraryCommonConstants.Security.logicalOr,
25
- run: null
26
- }, opts)
27
-
28
- if (options.relation !== LibraryCommonConstants.Security.logicalOr && options.relation !== LibraryCommonConstants.Security.logicalAnd) {
29
- throw new Error('The value of options.relation should be one of [\'or\', \'and\']')
30
- }
31
- if (options.run && options.run !== 'all') {
32
- throw new Error('The value of options.run must be \'all\'')
33
- }
34
-
35
- /* eslint-disable-next-line no-var */
36
- for (var i = 0; i < functions.length; i++) {
37
- functions[i] = functions[i].bind(this)
38
- }
39
-
40
- const instance = reusify(Auth)
41
-
42
- function _auth (request, reply, done) {
43
- const obj = instance.get()
44
-
45
- obj.request = request
46
- obj.reply = reply
47
- obj.done = done
48
- obj.functions = this.functions
49
- obj.options = this.options
50
- obj.i = 0
51
- obj.start = true
52
- obj.firstResult = null
53
-
54
- obj.nextAuth()
55
- }
56
-
57
- return _auth.bind({ functions, options })
58
-
59
- function Auth () {
60
- this.next = null
61
- this.i = 0
62
- this.start = true
63
- this.functions = []
64
- this.options = {}
65
- this.request = null
66
- this.reply = null
67
- this.done = null
68
- this.firstResult = null
69
-
70
- const that = this
71
-
72
- this.nextAuth = function nextAuth (err) {
73
- const func = that.functions[that.i++]
74
-
75
- if (!func) {
76
- that.completeAuth(err)
77
- return
78
- }
79
-
80
- const maybePromise = func(that.request, that.reply, that.onAuth, that.options)
81
-
82
- if (maybePromise && typeof maybePromise.then === 'function') {
83
- maybePromise.then(results => that.onAuth(null, results), that.onAuth)
84
- }
85
- }
86
-
87
- this.onAuth = function onAuth (err, results) {
88
- if (that.options.relation === LibraryCommonConstants.Security.logicalOr) {
89
- if (err) {
90
- return that.nextAuth(err)
91
- }
92
-
93
- return that.completeAuth()
94
- } else {
95
- if (err) {
96
- return that.completeAuth(err)
97
- }
98
-
99
- return that.nextAuth(err)
100
- }
101
- }
102
-
103
- this.completeAuth = function (err) {
104
- if (that.start) {
105
- that.start = false
106
- that.firstResult = err
107
- }
108
-
109
- if (that.options.run === 'all' && that.i < that.functions.length) {
110
- return that.nextAuth(err)
111
- }
112
-
113
- if (that.firstResult && (!that.reply.raw.statusCode || that.reply.raw.statusCode < 400)) {
114
- that.reply.code(401)
115
- } else if (!that.firstResult && that.reply.raw.statusCode && that.reply.raw.statusCode >= 400) {
116
- that.reply.code(200)
117
- }
118
-
119
- that.done(that.firstResult)
120
- instance.release(that)
121
- }
122
- }
123
- }
124
-
125
- export default fp(checkAuth, {
126
- 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
+ import LibraryCommonConstants from '@thzero/library_common/constants.js';
9
+
10
+ function checkAuth (fastify, opts, next) {
11
+ fastify.decorate('auth', auth)
12
+ next()
13
+ }
14
+
15
+ function auth (functions, opts) {
16
+ if (!Array.isArray(functions)) {
17
+ throw new Error('You must give an array of functions to the auth function')
18
+ }
19
+ if (!functions.length) {
20
+ throw new Error('Missing auth functions')
21
+ }
22
+
23
+ const options = Object.assign({
24
+ relation: LibraryCommonConstants.Security.logicalOr,
25
+ run: null
26
+ }, opts)
27
+
28
+ if (options.relation !== LibraryCommonConstants.Security.logicalOr && options.relation !== LibraryCommonConstants.Security.logicalAnd) {
29
+ throw new Error('The value of options.relation should be one of [\'or\', \'and\']')
30
+ }
31
+ if (options.run && options.run !== 'all') {
32
+ throw new Error('The value of options.run must be \'all\'')
33
+ }
34
+
35
+ /* eslint-disable-next-line no-var */
36
+ for (var i = 0; i < functions.length; i++) {
37
+ functions[i] = functions[i].bind(this)
38
+ }
39
+
40
+ const instance = reusify(Auth)
41
+
42
+ function _auth (request, reply, done) {
43
+ const obj = instance.get()
44
+
45
+ obj.request = request
46
+ obj.reply = reply
47
+ obj.done = done
48
+ obj.functions = this.functions
49
+ obj.options = this.options
50
+ obj.i = 0
51
+ obj.start = true
52
+ obj.firstResult = null
53
+
54
+ obj.nextAuth()
55
+ }
56
+
57
+ return _auth.bind({ functions, options })
58
+
59
+ function Auth () {
60
+ this.next = null
61
+ this.i = 0
62
+ this.start = true
63
+ this.functions = []
64
+ this.options = {}
65
+ this.request = null
66
+ this.reply = null
67
+ this.done = null
68
+ this.firstResult = null
69
+
70
+ const that = this
71
+
72
+ this.nextAuth = function nextAuth (err) {
73
+ const func = that.functions[that.i++]
74
+
75
+ if (!func) {
76
+ that.completeAuth(err)
77
+ return
78
+ }
79
+
80
+ const maybePromise = func(that.request, that.reply, that.onAuth, that.options)
81
+
82
+ if (maybePromise && typeof maybePromise.then === 'function') {
83
+ maybePromise.then(results => that.onAuth(null, results), that.onAuth)
84
+ }
85
+ }
86
+
87
+ this.onAuth = function onAuth (err, results) {
88
+ if (that.options.relation === LibraryCommonConstants.Security.logicalOr) {
89
+ if (err) {
90
+ return that.nextAuth(err)
91
+ }
92
+
93
+ return that.completeAuth()
94
+ } else {
95
+ if (err) {
96
+ return that.completeAuth(err)
97
+ }
98
+
99
+ return that.nextAuth(err)
100
+ }
101
+ }
102
+
103
+ this.completeAuth = function (err) {
104
+ if (that.start) {
105
+ that.start = false
106
+ that.firstResult = err
107
+ }
108
+
109
+ if (that.options.run === 'all' && that.i < that.functions.length) {
110
+ return that.nextAuth(err)
111
+ }
112
+
113
+ if (that.firstResult && (!that.reply.raw.statusCode || that.reply.raw.statusCode < 400)) {
114
+ that.reply.code(401)
115
+ } else if (!that.firstResult && that.reply.raw.statusCode && that.reply.raw.statusCode >= 400) {
116
+ that.reply.code(200)
117
+ }
118
+
119
+ that.done(that.firstResult)
120
+ instance.release(that)
121
+ }
122
+ }
123
+ }
124
+
125
+ export default fp(checkAuth, {
126
+ fastify: '5.x'
127
127
  })