beavuck-time 2.1.21

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.
Files changed (53) hide show
  1. package/.env +21 -0
  2. package/.env.test +30 -0
  3. package/.gitlab/issue_templates/bug.md +53 -0
  4. package/.gitlab/issue_templates/enhancement.md +53 -0
  5. package/.gitlab/merge_request_templates/bug_fix.md +9 -0
  6. package/.gitlab/merge_request_templates/enhancement.md +9 -0
  7. package/.gitlab-ci.yml +149 -0
  8. package/.nvmrc +1 -0
  9. package/.sonarlint/connectedMode.json +4 -0
  10. package/CONTRIBUTING.md +57 -0
  11. package/DOCKERHUB_OVERVIEW.md +185 -0
  12. package/Dockerfile +25 -0
  13. package/HELP.md +53 -0
  14. package/README.md +215 -0
  15. package/UNLICENSE +24 -0
  16. package/api-tests/bruno/bruno.json +9 -0
  17. package/api-tests/bruno/collection.bru +7 -0
  18. package/api-tests/bruno/environments/local.bru +8 -0
  19. package/api-tests/bruno/now/nok/nok_no_origin.bru +19 -0
  20. package/api-tests/bruno/now/nok/nok_non_recognized_referrer.bru +23 -0
  21. package/api-tests/bruno/now/nok/nok_non_trusted_origin.bru +23 -0
  22. package/api-tests/bruno/now/nok/nok_non_truted_origin_nor_referrer.bru +24 -0
  23. package/api-tests/bruno/now/ok/ok_same_origin.bru +23 -0
  24. package/api-tests/bruno/now/ok/ok_same_referrer.bru +23 -0
  25. package/api-tests/bruno/now/ok/ok_trusted_origin.bru +23 -0
  26. package/build/api.js +20 -0
  27. package/eslint.config.mjs +28 -0
  28. package/openapi/swagger.json +75 -0
  29. package/package.json +79 -0
  30. package/sonar-project.properties +9 -0
  31. package/src/__tests__/api.test.ts +98 -0
  32. package/src/__tests__/middlewares/errorHandler.test.ts +93 -0
  33. package/src/__tests__/utils/urlUtil.test.ts +53 -0
  34. package/src/api.ts +19 -0
  35. package/src/config/corsOptions.ts +63 -0
  36. package/src/config/logger.ts +38 -0
  37. package/src/controllers/nowController.ts +19 -0
  38. package/src/errors/BeavuckTimeClientError.ts +12 -0
  39. package/src/errors/BeavuckTimeServerError.ts +18 -0
  40. package/src/errors/CorsError.ts +12 -0
  41. package/src/errors/base/BeavuckTimeError.ts +12 -0
  42. package/src/middlewares/corsMiddleware.ts +32 -0
  43. package/src/middlewares/errorHandler.ts +57 -0
  44. package/src/middlewares/notFoundHandler.ts +8 -0
  45. package/src/middlewares/rateLimiter.ts +15 -0
  46. package/src/models/now.ts +14 -0
  47. package/src/routes/routes.ts +84 -0
  48. package/src/server.ts +53 -0
  49. package/src/services/nowService.ts +9 -0
  50. package/src/types/isoTimestamp.ts +8 -0
  51. package/src/utils/urlUtil.ts +29 -0
  52. package/tsconfig.json +38 -0
  53. package/tsoa.json +12 -0
package/src/server.ts ADDED
@@ -0,0 +1,53 @@
1
+ // src/server.ts
2
+
3
+ import {api} from './api'
4
+ import {logger} from './config/logger'
5
+ import {tryParseUrl} from './utils/urlUtil'
6
+ import dotenvx from '@dotenvx/dotenvx'
7
+
8
+ dotenvx.config()
9
+
10
+ const API_PORT = process.env.API_PORT ?? 3000
11
+
12
+ const HOST_URL = process.env.HOST_URL ?? ''
13
+ const TRUSTED_ORIGINS = process.env.TRUSTED_ORIGINS ?? ''
14
+
15
+ const SIGTERM = 'SIGTERM'
16
+ const SIGINT = 'SIGINT'
17
+
18
+ if (!HOST_URL || !tryParseUrl(HOST_URL)) {
19
+ logger.error('HOST_URL not set in environment variables (or is not a valid URL)')
20
+ process.exit(1)
21
+ }
22
+
23
+ if (!TRUSTED_ORIGINS) {
24
+ logger.error('TRUSTED_ORIGINS not set in environment variables')
25
+ process.exit(1)
26
+ }
27
+
28
+ const server = api.listen(API_PORT, () => {
29
+ logger.info(
30
+ `
31
+ | | | | ,=.
32
+ | |__ ___ __ ___ ___ _ ___| | __ ,=""""==.__.=" o".___
33
+ | '_ \\ / _ \\/ _\` \\ \\ / / | | |/ __| |/ / ,=.==" ___/
34
+ | |_) | __/ (_| |\\ V /| |_| | (__| < ,==.," , , \\,===""
35
+ |_.__/ \\___|\\__,_| \\_/ \\__,_|\\___|_|\\_\\ < ,==) \\"'"=._.==) \\
36
+ \`=='' \`" \`"
37
+
38
+ Beavuck Time microservice started successfully
39
+
40
+ Ready on API port ${API_PORT} (if this is running in a container, this port number is internal to the container)`,
41
+ )
42
+ })
43
+
44
+ const gracefulShutdown = (signal: string) => {
45
+ logger.info(`${signal} signal received: closing HTTP server`)
46
+ server.close(() => {
47
+ logger.info('HTTP server closed')
48
+ process.exit(0)
49
+ })
50
+ }
51
+
52
+ process.on(SIGTERM, () => gracefulShutdown(SIGTERM))
53
+ process.on(SIGINT, () => gracefulShutdown(SIGINT))
@@ -0,0 +1,9 @@
1
+ // src/services/nowService.ts
2
+
3
+ import {Now} from '../models/now'
4
+
5
+ export class NowService {
6
+ public get(): Now {
7
+ return new Now()
8
+ }
9
+ }
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Stringified date-time in RFC 3339 format (often referred to as ISO 8601 format)
3
+ * See [RFC 3339 section 5.6](https://datatracker.ietf.org/doc/html/rfc3339#section-5.6)
4
+ * @example 2019-08-24T14:15:22Z
5
+ * @format date-time
6
+ */
7
+ export type IsoTimestamp = string
8
+ export const RFC_3339_FORMAT = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z$/
@@ -0,0 +1,29 @@
1
+ // src/utils/urlUtil.ts
2
+
3
+ import {URL} from 'url'
4
+ import {logger} from '../config/logger'
5
+
6
+ export function isSameOrigin(thisUrl?: URL, thatUrl?: URL): boolean {
7
+ logger.debug(`Comparing origins: ${thisUrl?.origin} and ${thatUrl?.origin}`)
8
+ if (!thisUrl || !thatUrl) return false
9
+ if (thisUrl.origin === thatUrl.origin) return true
10
+
11
+ const isSameProtocol = thisUrl.protocol === thatUrl.protocol
12
+ const isSameHost = thisUrl.hostname === thatUrl.hostname
13
+ const isSamePort = thisUrl.port === thatUrl.port
14
+
15
+ return isSameProtocol && isSameHost && isSamePort
16
+ }
17
+
18
+ export function tryParseUrl(urlString?: string): URL | undefined {
19
+ if (!urlString) {
20
+ logger.debug('Empty URL string')
21
+ return undefined
22
+ }
23
+ try {
24
+ return new URL(urlString)
25
+ } catch (error: unknown) {
26
+ logger.warn(`Invalid URL string: ${urlString} (${error})`)
27
+ }
28
+ return undefined
29
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,38 @@
1
+ {
2
+ "compilerOptions": {
3
+ /* Basic Options */
4
+ "incremental": true,
5
+ "target": "ES2023",
6
+ "module": "commonjs",
7
+ "outDir": "build",
8
+ "rootDir": "src",
9
+ /* Strict Type-Checking Options */
10
+ "strict": true,
11
+ "noImplicitAny": true,
12
+ "strictNullChecks": true,
13
+ "strictFunctionTypes": true,
14
+ "strictBindCallApply": true,
15
+ "strictPropertyInitialization": true,
16
+ "noImplicitThis": true,
17
+ "alwaysStrict": true,
18
+ /* Additional Checks */
19
+ "allowUnreachableCode": false,
20
+ "allowUnusedLabels": false,
21
+ "noEmitOnError": true,
22
+ "noUnusedLocals": true,
23
+ "noUnusedParameters": true,
24
+ "noImplicitReturns": true,
25
+ "noFallthroughCasesInSwitch": true,
26
+ /* Module Resolution Options */
27
+ "moduleResolution": "node",
28
+ "baseUrl": ".",
29
+ "esModuleInterop": true,
30
+ "resolveJsonModule": true,
31
+ /* Experimental Options */
32
+ "experimentalDecorators": true,
33
+ /* Advanced Options */
34
+ "forceConsistentCasingInFileNames": true
35
+ },
36
+ "include": ["src/**/*.ts"],
37
+ "exclude": ["node_modules"]
38
+ }
package/tsoa.json ADDED
@@ -0,0 +1,12 @@
1
+ {
2
+ "entryFile": "src/server.ts",
3
+ "noImplicitAdditionalProperties": "throw-on-extras",
4
+ "controllerPathGlobs": ["src/controllers/*Controller.ts"],
5
+ "spec": {
6
+ "outputDirectory": "openapi",
7
+ "specVersion": 3
8
+ },
9
+ "routes": {
10
+ "routesDir": "src/routes"
11
+ }
12
+ }