@naturalcycles/backend-lib 8.0.4 → 9.0.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/dist/admin/adminMiddleware.d.ts +3 -3
- package/dist/admin/adminMiddleware.js +11 -18
- package/dist/admin/base.admin.service.d.ts +1 -1
- package/dist/admin/base.admin.service.js +9 -13
- package/dist/admin/firebase.shared.service.d.ts +3 -3
- package/dist/admin/firebase.shared.service.js +12 -15
- package/dist/admin/secureHeaderMiddleware.d.ts +2 -2
- package/dist/admin/secureHeaderMiddleware.js +7 -10
- package/dist/bin/deploy-gae.js +9 -11
- package/dist/bin/deploy-health-check.js +6 -8
- package/dist/bin/deploy-prepare.js +5 -7
- package/dist/bin/undeploy-gae.js +5 -7
- package/dist/db/httpDB.js +7 -11
- package/dist/db/httpDBRequestHandler.d.ts +1 -1
- package/dist/db/httpDBRequestHandler.js +27 -29
- package/dist/db/index.d.ts +3 -3
- package/dist/db/index.js +3 -7
- package/dist/deploy/backend.cfg.util.js +6 -9
- package/dist/deploy/deploy.model.js +1 -2
- package/dist/deploy/deploy.util.d.ts +2 -2
- package/dist/deploy/deploy.util.js +25 -33
- package/dist/deploy/deployGae.d.ts +2 -2
- package/dist/deploy/deployGae.js +23 -27
- package/dist/deploy/deployHealthCheck.js +16 -20
- package/dist/deploy/deployPrepare.d.ts +1 -1
- package/dist/deploy/deployPrepare.js +17 -21
- package/dist/deploy/index.d.ts +8 -8
- package/dist/deploy/index.js +6 -14
- package/dist/env/env.shared.service.js +8 -12
- package/dist/index.d.ts +33 -33
- package/dist/index.js +34 -38
- package/dist/paths.cnst.js +5 -9
- package/dist/sentry/sentry.shared.service.js +11 -15
- package/dist/server/asyncLocalStorageMiddleware.d.ts +1 -1
- package/dist/server/asyncLocalStorageMiddleware.js +9 -15
- package/dist/server/basicAuthMiddleware.d.ts +1 -1
- package/dist/server/basicAuthMiddleware.js +5 -8
- package/dist/server/bodyParserTimeoutMiddleware.d.ts +1 -1
- package/dist/server/bodyParserTimeoutMiddleware.js +5 -9
- package/dist/server/createDefaultApp.d.ts +3 -3
- package/dist/server/createDefaultApp.js +22 -23
- package/dist/server/createDefaultApp.model.d.ts +3 -3
- package/dist/server/createDefaultApp.model.js +1 -2
- package/dist/server/deployInfo.util.d.ts +1 -1
- package/dist/server/deployInfo.util.js +5 -8
- package/dist/server/genericErrorMiddleware.d.ts +2 -2
- package/dist/server/genericErrorMiddleware.js +6 -10
- package/dist/server/getDefaultRouter.d.ts +1 -1
- package/dist/server/getDefaultRouter.js +3 -6
- package/dist/server/logMiddleware.d.ts +1 -1
- package/dist/server/logMiddleware.js +12 -16
- package/dist/server/methodOverrideMiddleware.d.ts +1 -1
- package/dist/server/methodOverrideMiddleware.js +1 -4
- package/dist/server/notFoundMiddleware.d.ts +1 -1
- package/dist/server/notFoundMiddleware.js +3 -6
- package/dist/server/okMiddleware.d.ts +1 -1
- package/dist/server/okMiddleware.js +1 -4
- package/dist/server/request.log.util.d.ts +1 -1
- package/dist/server/request.log.util.js +7 -11
- package/dist/server/request.util.d.ts +1 -1
- package/dist/server/request.util.js +1 -4
- package/dist/server/requestTimeoutMiddleware.d.ts +1 -1
- package/dist/server/requestTimeoutMiddleware.js +9 -13
- package/dist/server/safeJsonMiddleware.d.ts +1 -1
- package/dist/server/safeJsonMiddleware.js +3 -6
- package/dist/server/server.model.js +1 -2
- package/dist/server/server.util.js +1 -4
- package/dist/server/serverStatsMiddleware.d.ts +1 -1
- package/dist/server/serverStatsMiddleware.js +19 -24
- package/dist/server/serverStatusMiddleware.d.ts +1 -1
- package/dist/server/serverStatusMiddleware.js +12 -16
- package/dist/server/simpleRequestLoggerMiddleware.d.ts +1 -1
- package/dist/server/simpleRequestLoggerMiddleware.js +8 -11
- package/dist/server/startServer.d.ts +1 -1
- package/dist/server/startServer.js +18 -22
- package/dist/server/startServer.model.d.ts +3 -3
- package/dist/server/startServer.model.js +1 -2
- package/dist/server/validation/validateMiddleware.d.ts +2 -2
- package/dist/server/validation/validateMiddleware.js +9 -15
- package/dist/server/validation/validateRequest.d.ts +1 -1
- package/dist/server/validation/validateRequest.js +6 -9
- package/dist/server/validation/zodValidateMiddleware.d.ts +2 -2
- package/dist/server/validation/zodValidateMiddleware.js +6 -9
- package/dist/testing/express.test.service.d.ts +5 -5
- package/dist/testing/express.test.service.js +16 -14
- package/dist/testing/index.d.ts +2 -2
- package/dist/testing/index.js +2 -5
- package/dist/util.js +2 -6
- package/package.json +7 -7
- package/src/admin/adminMiddleware.ts +3 -3
- package/src/admin/base.admin.service.ts +1 -1
- package/src/admin/firebase.shared.service.ts +7 -6
- package/src/admin/secureHeaderMiddleware.ts +4 -4
- package/src/bin/deploy-gae.ts +3 -3
- package/src/bin/deploy-health-check.ts +1 -1
- package/src/bin/deploy-prepare.ts +1 -1
- package/src/bin/undeploy-gae.ts +1 -1
- package/src/db/httpDBRequestHandler.ts +4 -3
- package/src/db/index.ts +3 -3
- package/src/deploy/backend.cfg.util.ts +1 -1
- package/src/deploy/deploy.util.ts +3 -4
- package/src/deploy/deployGae.ts +6 -6
- package/src/deploy/deployHealthCheck.ts +1 -1
- package/src/deploy/deployPrepare.ts +6 -5
- package/src/deploy/index.ts +8 -8
- package/src/index.ts +33 -33
- package/src/sentry/sentry.shared.service.ts +1 -1
- package/src/server/asyncLocalStorageMiddleware.ts +2 -2
- package/src/server/basicAuthMiddleware.ts +1 -1
- package/src/server/bodyParserTimeoutMiddleware.ts +2 -2
- package/src/server/createDefaultApp.model.ts +3 -3
- package/src/server/createDefaultApp.ts +16 -14
- package/src/server/deployInfo.util.ts +1 -1
- package/src/server/genericErrorMiddleware.ts +2 -2
- package/src/server/getDefaultRouter.ts +1 -1
- package/src/server/logMiddleware.ts +1 -1
- package/src/server/methodOverrideMiddleware.ts +1 -1
- package/src/server/notFoundMiddleware.ts +2 -2
- package/src/server/okMiddleware.ts +1 -1
- package/src/server/request.log.util.ts +1 -1
- package/src/server/request.util.ts +1 -1
- package/src/server/requestTimeoutMiddleware.ts +2 -2
- package/src/server/safeJsonMiddleware.ts +1 -1
- package/src/server/serverStatsMiddleware.ts +3 -3
- package/src/server/serverStatusMiddleware.ts +2 -2
- package/src/server/simpleRequestLoggerMiddleware.ts +3 -3
- package/src/server/startServer.model.ts +3 -3
- package/src/server/startServer.ts +4 -7
- package/src/server/validation/validateMiddleware.ts +2 -2
- package/src/server/validation/validateRequest.ts +1 -1
- package/src/server/validation/zodValidateMiddleware.ts +2 -2
- package/src/testing/express.test.service.ts +20 -11
- package/src/testing/index.ts +2 -2
|
@@ -1,8 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.expressTestService = void 0;
|
|
4
|
-
const js_lib_1 = require("@naturalcycles/js-lib");
|
|
5
|
-
const index_1 = require("../index");
|
|
1
|
+
import { getFetcher, pDelay } from '@naturalcycles/js-lib';
|
|
2
|
+
import { createDefaultApp } from '../index.js';
|
|
6
3
|
const nativeFetchFn = async (url, init) => await globalThis.fetch(url, init);
|
|
7
4
|
// Example:
|
|
8
5
|
// const app = expressTestService.createApp([ debugResource ])
|
|
@@ -10,14 +7,14 @@ const nativeFetchFn = async (url, init) => await globalThis.fetch(url, init);
|
|
|
10
7
|
// await app.close()
|
|
11
8
|
// })
|
|
12
9
|
class ExpressTestService {
|
|
13
|
-
createAppFromResource(resource, opt, defaultAppCfg) {
|
|
14
|
-
return this.createApp(
|
|
10
|
+
async createAppFromResource(resource, opt, defaultAppCfg) {
|
|
11
|
+
return this.createApp(await createDefaultApp({
|
|
15
12
|
...defaultAppCfg,
|
|
16
13
|
resources: [resource],
|
|
17
14
|
}), opt);
|
|
18
15
|
}
|
|
19
|
-
createAppFromResources(resources, opt) {
|
|
20
|
-
return this.createApp(
|
|
16
|
+
async createAppFromResources(resources, opt) {
|
|
17
|
+
return this.createApp(await createDefaultApp({
|
|
21
18
|
resources,
|
|
22
19
|
}), opt);
|
|
23
20
|
}
|
|
@@ -25,7 +22,7 @@ class ExpressTestService {
|
|
|
25
22
|
const server = this.createTestServer(app);
|
|
26
23
|
const { port } = server.address();
|
|
27
24
|
const baseUrl = `http://127.0.0.1:${port}`;
|
|
28
|
-
const fetcher =
|
|
25
|
+
const fetcher = getFetcher({
|
|
29
26
|
baseUrl,
|
|
30
27
|
responseType: 'json',
|
|
31
28
|
retry: { count: 0 },
|
|
@@ -39,7 +36,7 @@ class ExpressTestService {
|
|
|
39
36
|
// so tests using it can reply on `void someAnalyticsService.doSmth()` promises
|
|
40
37
|
// to be done by that time.
|
|
41
38
|
// Smart, huh?
|
|
42
|
-
await
|
|
39
|
+
await pDelay();
|
|
43
40
|
});
|
|
44
41
|
fetcher.close = async () => {
|
|
45
42
|
// const started = Date.now()
|
|
@@ -47,9 +44,14 @@ class ExpressTestService {
|
|
|
47
44
|
// console.log(`close took ${_since(started)}`) // todo: investigate why it takes ~5 seconds!
|
|
48
45
|
// Kirill: not awaiting the server-close, otherwise it takes significant waiting time
|
|
49
46
|
// to "teardown" server after it's been hit by Fetcher
|
|
50
|
-
server.close()
|
|
51
|
-
// server.
|
|
47
|
+
// server.close()
|
|
48
|
+
// 2024-08-31: server.close is no longer slow, so now we're back at awaiting it
|
|
49
|
+
await new Promise(resolve => {
|
|
50
|
+
server.unref().close(resolve);
|
|
51
|
+
});
|
|
52
|
+
await pDelay();
|
|
52
53
|
};
|
|
54
|
+
fetcher[Symbol.asyncDispose] = async () => await fetcher.close();
|
|
53
55
|
return fetcher;
|
|
54
56
|
}
|
|
55
57
|
/**
|
|
@@ -70,4 +72,4 @@ class ExpressTestService {
|
|
|
70
72
|
return server;
|
|
71
73
|
}
|
|
72
74
|
}
|
|
73
|
-
|
|
75
|
+
export const expressTestService = new ExpressTestService();
|
package/dist/testing/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { ExpressApp } from './express.test.service';
|
|
2
|
-
import { expressTestService } from './express.test.service';
|
|
1
|
+
import type { ExpressApp } from './express.test.service.js';
|
|
2
|
+
import { expressTestService } from './express.test.service.js';
|
|
3
3
|
export type { ExpressApp };
|
|
4
4
|
export { expressTestService };
|
package/dist/testing/index.js
CHANGED
|
@@ -1,5 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.expressTestService = void 0;
|
|
4
|
-
const express_test_service_1 = require("./express.test.service");
|
|
5
|
-
Object.defineProperty(exports, "expressTestService", { enumerable: true, get: function () { return express_test_service_1.expressTestService; } });
|
|
1
|
+
import { expressTestService } from './express.test.service.js';
|
|
2
|
+
export { expressTestService };
|
package/dist/util.js
CHANGED
|
@@ -1,11 +1,7 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.isGAE = isGAE;
|
|
4
|
-
exports.isCloudRun = isCloudRun;
|
|
5
1
|
const { GAE_INSTANCE, K_SERVICE } = process.env;
|
|
6
|
-
function isGAE() {
|
|
2
|
+
export function isGAE() {
|
|
7
3
|
return !!GAE_INSTANCE;
|
|
8
4
|
}
|
|
9
|
-
function isCloudRun() {
|
|
5
|
+
export function isCloudRun() {
|
|
10
6
|
return !!K_SERVICE;
|
|
11
7
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@naturalcycles/backend-lib",
|
|
3
|
-
"
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "9.0.0",
|
|
4
5
|
"scripts": {
|
|
5
6
|
"prepare": "husky",
|
|
6
7
|
"build": "dev-lib build",
|
|
@@ -11,11 +12,11 @@
|
|
|
11
12
|
"dev": "APP_ENV=dev tsx watch src/test/server/server.ts",
|
|
12
13
|
"docs-serve": "NODE_OPTIONS=--openssl-legacy-provider vuepress dev docs",
|
|
13
14
|
"docs-build": "NODE_OPTIONS=--openssl-legacy-provider vuepress build docs",
|
|
14
|
-
"deploy-gae": "yarn tsx
|
|
15
|
-
"deploy-prepare": "yarn tsx
|
|
16
|
-
"deploy-prepare-debug": "AA=AA1 BB=BB1 yarn tsx
|
|
17
|
-
"deploy-health-check-debug1": "yarn tsx
|
|
18
|
-
"deploy-health-check-debug2": "yarn tsx
|
|
15
|
+
"deploy-gae": "yarn tsx src/bin/deploy-gae.ts",
|
|
16
|
+
"deploy-prepare": "yarn tsx src/bin/deploy-prepare.ts",
|
|
17
|
+
"deploy-prepare-debug": "AA=AA1 BB=BB1 yarn tsx src/bin/deploy-prepare.ts --projectDir ./src/test/project",
|
|
18
|
+
"deploy-health-check-debug1": "yarn tsx src/bin/deploy-health-check.ts --url https://api-master.naturalcycles.com",
|
|
19
|
+
"deploy-health-check-debug2": "yarn tsx src/bin/deploy-health-check.ts --url https://api-master2.naturalcycles.com --thresholdUnhealthy 5"
|
|
19
20
|
},
|
|
20
21
|
"peerDependencies": {
|
|
21
22
|
"@sentry/node": "^9"
|
|
@@ -77,7 +78,6 @@
|
|
|
77
78
|
"engines": {
|
|
78
79
|
"node": ">=22.12.0"
|
|
79
80
|
},
|
|
80
|
-
"type": "commonjs",
|
|
81
81
|
"description": "Standard library for making Express.js / AppEngine based backend services",
|
|
82
82
|
"author": "Natural Cycles Team",
|
|
83
83
|
"license": "MIT"
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { _memoFn, AppError } from '@naturalcycles/js-lib'
|
|
2
2
|
import { fs2 } from '@naturalcycles/nodejs-lib'
|
|
3
3
|
import ejs from 'ejs'
|
|
4
|
-
import type { BackendRequestHandler } from '../server/server.model'
|
|
5
|
-
import type { BaseAdminService } from './base.admin.service'
|
|
6
|
-
import type { FirebaseSharedServiceCfg } from './firebase.shared.service'
|
|
4
|
+
import type { BackendRequestHandler } from '../server/server.model.js'
|
|
5
|
+
import type { BaseAdminService } from './base.admin.service.js'
|
|
6
|
+
import type { FirebaseSharedServiceCfg } from './firebase.shared.service.js'
|
|
7
7
|
|
|
8
8
|
export interface RequireAdminCfg {
|
|
9
9
|
/**
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { _assert, AppError } from '@naturalcycles/js-lib'
|
|
2
2
|
import { dimGrey, green, red } from '@naturalcycles/nodejs-lib'
|
|
3
3
|
import type FirebaseAdmin from 'firebase-admin'
|
|
4
|
-
import type { BackendRequest, BackendRequestHandler } from '../server/server.model'
|
|
4
|
+
import type { BackendRequest, BackendRequestHandler } from '../server/server.model.js'
|
|
5
5
|
|
|
6
6
|
export interface AdminServiceCfg {
|
|
7
7
|
/**
|
|
@@ -39,16 +39,16 @@ export interface FirebaseSharedServiceCfg {
|
|
|
39
39
|
export class FirebaseSharedService {
|
|
40
40
|
constructor(public cfg: FirebaseSharedServiceCfg) {}
|
|
41
41
|
|
|
42
|
-
init(): void {
|
|
43
|
-
this.admin()
|
|
42
|
+
async init(): Promise<void> {
|
|
43
|
+
await this.admin()
|
|
44
44
|
}
|
|
45
45
|
|
|
46
46
|
@_Memo()
|
|
47
|
-
admin(): FirebaseAdmin.app.App {
|
|
47
|
+
async admin(): Promise<FirebaseAdmin.app.App> {
|
|
48
48
|
const { serviceAccount } = this.cfg
|
|
49
49
|
|
|
50
50
|
// lazy loading
|
|
51
|
-
const admin =
|
|
51
|
+
const admin = await import('firebase-admin')
|
|
52
52
|
|
|
53
53
|
const credential = serviceAccount
|
|
54
54
|
? admin.credential.cert(serviceAccount)
|
|
@@ -63,7 +63,8 @@ export class FirebaseSharedService {
|
|
|
63
63
|
)
|
|
64
64
|
}
|
|
65
65
|
|
|
66
|
-
auth(): FirebaseAdmin.auth.Auth {
|
|
67
|
-
|
|
66
|
+
async auth(): Promise<FirebaseAdmin.auth.Auth> {
|
|
67
|
+
const admin = await this.admin()
|
|
68
|
+
return admin.auth()
|
|
68
69
|
}
|
|
69
70
|
}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { AppError } from '@naturalcycles/js-lib'
|
|
2
2
|
import { timingSafeStringEqual } from '@naturalcycles/nodejs-lib'
|
|
3
|
-
import type { BackendRequestHandler } from '../server/server.model'
|
|
4
|
-
import type { AdminMiddleware, RequireAdminCfg } from './adminMiddleware'
|
|
5
|
-
import { requireAdminPermissions } from './adminMiddleware'
|
|
6
|
-
import type { BaseAdminService } from './base.admin.service'
|
|
3
|
+
import type { BackendRequestHandler } from '../server/server.model.js'
|
|
4
|
+
import type { AdminMiddleware, RequireAdminCfg } from './adminMiddleware.js'
|
|
5
|
+
import { requireAdminPermissions } from './adminMiddleware.js'
|
|
6
|
+
import type { BaseAdminService } from './base.admin.service.js'
|
|
7
7
|
|
|
8
8
|
export interface SecureHeaderMiddlewareCfg extends RequireAdminCfg {
|
|
9
9
|
adminService: BaseAdminService
|
package/src/bin/deploy-gae.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
3
|
import { _yargs, runScript } from '@naturalcycles/nodejs-lib'
|
|
4
|
-
import { deployGae } from '../deploy/deployGae'
|
|
5
|
-
import { deployHealthCheckYargsOptions } from '../deploy/deployHealthCheck'
|
|
6
|
-
import { deployPrepareYargsOptions } from '../deploy/deployPrepare'
|
|
4
|
+
import { deployGae } from '../deploy/deployGae.js'
|
|
5
|
+
import { deployHealthCheckYargsOptions } from '../deploy/deployHealthCheck.js'
|
|
6
|
+
import { deployPrepareYargsOptions } from '../deploy/deployPrepare.js'
|
|
7
7
|
|
|
8
8
|
runScript(async () => {
|
|
9
9
|
const opt = _yargs().options({
|
|
@@ -10,7 +10,7 @@ yarn deploy-health-check --url https://service-dot-yourproject.appspot.com
|
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
12
|
import { _yargs, runScript } from '@naturalcycles/nodejs-lib'
|
|
13
|
-
import { deployHealthCheck, deployHealthCheckYargsOptions } from '../deploy/deployHealthCheck'
|
|
13
|
+
import { deployHealthCheck, deployHealthCheckYargsOptions } from '../deploy/deployHealthCheck.js'
|
|
14
14
|
|
|
15
15
|
runScript(async () => {
|
|
16
16
|
const { url, ...opt } = _yargs().options({
|
|
@@ -7,7 +7,7 @@ yarn deploy-prepare
|
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
9
|
import { _yargs, runScript } from '@naturalcycles/nodejs-lib'
|
|
10
|
-
import { deployPrepare, deployPrepareYargsOptions } from '../deploy/deployPrepare'
|
|
10
|
+
import { deployPrepare, deployPrepareYargsOptions } from '../deploy/deployPrepare.js'
|
|
11
11
|
|
|
12
12
|
runScript(async () => {
|
|
13
13
|
const opt = _yargs().options(deployPrepareYargsOptions).argv
|
package/src/bin/undeploy-gae.ts
CHANGED
|
@@ -4,11 +4,12 @@ import {
|
|
|
4
4
|
commonDBOptionsSchema,
|
|
5
5
|
commonDBSaveOptionsSchema,
|
|
6
6
|
dbQuerySchema,
|
|
7
|
-
} from '@naturalcycles/db-lib/dist/validation'
|
|
7
|
+
} from '@naturalcycles/db-lib/dist/validation/index.js'
|
|
8
8
|
import type { ObjectWithId } from '@naturalcycles/js-lib'
|
|
9
9
|
import { anyObjectSchema, arraySchema, objectSchema, stringSchema } from '@naturalcycles/nodejs-lib'
|
|
10
|
-
import
|
|
11
|
-
import {
|
|
10
|
+
import { getDefaultRouter } from '../server/getDefaultRouter.js'
|
|
11
|
+
import type { BackendRouter } from '../server/server.model.js'
|
|
12
|
+
import { validateRequest } from '../server/validation/validateRequest.js'
|
|
12
13
|
|
|
13
14
|
export interface GetByIdsInput {
|
|
14
15
|
table: string
|
package/src/db/index.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type { HttpDBCfg } from './httpDB'
|
|
2
|
-
import { HttpDB } from './httpDB'
|
|
3
|
-
import { httpDBRequestHandler } from './httpDBRequestHandler'
|
|
1
|
+
import type { HttpDBCfg } from './httpDB.js'
|
|
2
|
+
import { HttpDB } from './httpDB.js'
|
|
3
|
+
import { httpDBRequestHandler } from './httpDBRequestHandler.js'
|
|
4
4
|
|
|
5
5
|
export type { HttpDBCfg }
|
|
6
6
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { StringMap } from '@naturalcycles/js-lib'
|
|
2
2
|
import { AjvSchema, fs2, requireFileToExist } from '@naturalcycles/nodejs-lib'
|
|
3
|
-
import { resourcesDir } from '../paths.cnst'
|
|
3
|
+
import { resourcesDir } from '../paths.cnst.js'
|
|
4
4
|
|
|
5
5
|
export interface BackendCfg {
|
|
6
6
|
gaeProject: string
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { _assert, _mapValues, _merge, _truncate, localTime } from '@naturalcycles/js-lib'
|
|
2
2
|
import { dimGrey, fs2, sha256, white } from '@naturalcycles/nodejs-lib'
|
|
3
|
-
import type { BackendCfg } from './backend.cfg.util'
|
|
4
|
-
import type { AppYaml, DeployInfo } from './deploy.model'
|
|
3
|
+
import type { BackendCfg } from './backend.cfg.util.js'
|
|
4
|
+
import type { AppYaml, DeployInfo } from './deploy.model.js'
|
|
5
5
|
|
|
6
6
|
const APP_YAML_DEFAULT = (): AppYaml => ({
|
|
7
7
|
runtime: 'nodejs22',
|
|
@@ -37,7 +37,7 @@ export async function createDeployInfo(
|
|
|
37
37
|
backendCfg: BackendCfg,
|
|
38
38
|
overrideBranch?: string,
|
|
39
39
|
): Promise<DeployInfo> {
|
|
40
|
-
const simpleGit =
|
|
40
|
+
const { simpleGit } = await import('simple-git') // lazy load
|
|
41
41
|
const git = simpleGit('.')
|
|
42
42
|
|
|
43
43
|
const now = localTime.now()
|
|
@@ -153,7 +153,6 @@ export function createAppYaml(
|
|
|
153
153
|
}
|
|
154
154
|
|
|
155
155
|
// appYamlPassEnv
|
|
156
|
-
require('dotenv').config() // ensure .env is read
|
|
157
156
|
const passEnv = appYamlPassEnv
|
|
158
157
|
.split(',')
|
|
159
158
|
.filter(Boolean)
|
package/src/deploy/deployGae.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { _anyToError, _objectAssign, pRetry } from '@naturalcycles/js-lib'
|
|
2
2
|
import { appendToGithubSummary, exec2 } from '@naturalcycles/nodejs-lib'
|
|
3
|
-
import { getBackendCfg } from './backend.cfg.util'
|
|
4
|
-
import { createDeployInfo } from './deploy.util'
|
|
5
|
-
import type { DeployHealthCheckOptions } from './deployHealthCheck'
|
|
6
|
-
import { deployHealthCheck } from './deployHealthCheck'
|
|
7
|
-
import type { DeployPrepareOptions } from './deployPrepare'
|
|
8
|
-
import { deployPrepare } from './deployPrepare'
|
|
3
|
+
import { getBackendCfg } from './backend.cfg.util.js'
|
|
4
|
+
import { createDeployInfo } from './deploy.util.js'
|
|
5
|
+
import type { DeployHealthCheckOptions } from './deployHealthCheck.js'
|
|
6
|
+
import { deployHealthCheck } from './deployHealthCheck.js'
|
|
7
|
+
import type { DeployPrepareOptions } from './deployPrepare.js'
|
|
8
|
+
import { deployPrepare } from './deployPrepare.js'
|
|
9
9
|
|
|
10
10
|
export interface DeployGaeOptions extends DeployPrepareOptions, DeployHealthCheckOptions {}
|
|
11
11
|
|
|
@@ -3,7 +3,7 @@ import { inspect } from 'node:util'
|
|
|
3
3
|
import type { UnixTimestampMillis } from '@naturalcycles/js-lib'
|
|
4
4
|
import { _filterFalsyValues, _ms, _since, getFetcher, pDelay } from '@naturalcycles/js-lib'
|
|
5
5
|
import { dimGrey, exec2, red } from '@naturalcycles/nodejs-lib'
|
|
6
|
-
import { coloredHttpCode } from '../server/request.log.util'
|
|
6
|
+
import { coloredHttpCode } from '../server/request.log.util.js'
|
|
7
7
|
|
|
8
8
|
export interface DeployHealthCheckOptions {
|
|
9
9
|
thresholdHealthy?: number
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { dimGrey, fs2, kpySync } from '@naturalcycles/nodejs-lib'
|
|
2
|
-
import { srcDir } from '../paths.cnst'
|
|
3
|
-
import { getBackendCfg } from './backend.cfg.util'
|
|
4
|
-
import type { DeployInfo } from './deploy.model'
|
|
5
|
-
import { createAndSaveAppYaml, createAndSaveDeployInfo } from './deploy.util'
|
|
2
|
+
import { srcDir } from '../paths.cnst.js'
|
|
3
|
+
import { getBackendCfg } from './backend.cfg.util.js'
|
|
4
|
+
import type { DeployInfo } from './deploy.model.js'
|
|
5
|
+
import { createAndSaveAppYaml, createAndSaveDeployInfo } from './deploy.util.js'
|
|
6
6
|
|
|
7
7
|
export interface DeployPrepareOptions {
|
|
8
8
|
projectDir?: string
|
|
@@ -65,7 +65,8 @@ const DEFAULT_FILES = [
|
|
|
65
65
|
const defaultFilesDir = `${srcDir}/deploy/files-default`
|
|
66
66
|
|
|
67
67
|
export async function deployPrepare(opt: DeployPrepareOptions = {}): Promise<DeployInfo> {
|
|
68
|
-
|
|
68
|
+
await import('dotenv/config') // ensure .env is loaded
|
|
69
|
+
|
|
69
70
|
const { projectDir = '.', targetDir = './tmp/deploy', createNpmrc = true } = opt
|
|
70
71
|
|
|
71
72
|
const backendCfg = getBackendCfg(projectDir)
|
package/src/deploy/index.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import type { BackendCfg } from './backend.cfg.util'
|
|
2
|
-
import { getBackendCfg } from './backend.cfg.util'
|
|
3
|
-
import type { DeployInfo } from './deploy.model'
|
|
4
|
-
import { createAppYaml, createDeployInfo } from './deploy.util'
|
|
5
|
-
import { deployGae } from './deployGae'
|
|
6
|
-
import type { DeployHealthCheckOptions } from './deployHealthCheck'
|
|
7
|
-
import { deployHealthCheck } from './deployHealthCheck'
|
|
8
|
-
import { deployPrepare } from './deployPrepare'
|
|
1
|
+
import type { BackendCfg } from './backend.cfg.util.js'
|
|
2
|
+
import { getBackendCfg } from './backend.cfg.util.js'
|
|
3
|
+
import type { DeployInfo } from './deploy.model.js'
|
|
4
|
+
import { createAppYaml, createDeployInfo } from './deploy.util.js'
|
|
5
|
+
import { deployGae } from './deployGae.js'
|
|
6
|
+
import type { DeployHealthCheckOptions } from './deployHealthCheck.js'
|
|
7
|
+
import { deployHealthCheck } from './deployHealthCheck.js'
|
|
8
|
+
import { deployPrepare } from './deployPrepare.js'
|
|
9
9
|
|
|
10
10
|
export type { BackendCfg, DeployHealthCheckOptions, DeployInfo }
|
|
11
11
|
|
package/src/index.ts
CHANGED
|
@@ -1,35 +1,35 @@
|
|
|
1
|
-
import onFinished
|
|
2
|
-
export * from './admin/adminMiddleware'
|
|
3
|
-
export * from './admin/base.admin.service'
|
|
4
|
-
export * from './admin/firebase.shared.service'
|
|
5
|
-
export * from './admin/secureHeaderMiddleware'
|
|
6
|
-
export * from './env/env.shared.service'
|
|
7
|
-
export * from './sentry/sentry.shared.service'
|
|
8
|
-
export * from './server/asyncLocalStorageMiddleware'
|
|
9
|
-
export * from './server/basicAuthMiddleware'
|
|
10
|
-
export * from './server/bodyParserTimeoutMiddleware'
|
|
11
|
-
export * from './server/createDefaultApp'
|
|
12
|
-
export * from './server/createDefaultApp.model'
|
|
13
|
-
export * from './server/deployInfo.util'
|
|
14
|
-
export * from './server/genericErrorMiddleware'
|
|
15
|
-
export * from './server/getDefaultRouter'
|
|
16
|
-
export * from './server/logMiddleware'
|
|
17
|
-
export * from './server/methodOverrideMiddleware'
|
|
18
|
-
export * from './server/notFoundMiddleware'
|
|
19
|
-
export * from './server/okMiddleware'
|
|
20
|
-
export * from './server/request.log.util'
|
|
21
|
-
export * from './server/request.util'
|
|
22
|
-
export * from './server/requestTimeoutMiddleware'
|
|
23
|
-
export * from './server/safeJsonMiddleware'
|
|
24
|
-
export * from './server/server.model'
|
|
25
|
-
export * from './server/serverStatsMiddleware'
|
|
26
|
-
export * from './server/serverStatusMiddleware'
|
|
27
|
-
export * from './server/simpleRequestLoggerMiddleware'
|
|
28
|
-
export * from './server/startServer'
|
|
29
|
-
export * from './server/startServer.model'
|
|
30
|
-
export * from './server/validation/validateMiddleware'
|
|
31
|
-
export * from './server/validation/validateRequest'
|
|
32
|
-
export * from './server/validation/zodValidateMiddleware'
|
|
33
|
-
export * from './util'
|
|
1
|
+
import onFinished from 'on-finished'
|
|
2
|
+
export * from './admin/adminMiddleware.js'
|
|
3
|
+
export * from './admin/base.admin.service.js'
|
|
4
|
+
export * from './admin/firebase.shared.service.js'
|
|
5
|
+
export * from './admin/secureHeaderMiddleware.js'
|
|
6
|
+
export * from './env/env.shared.service.js'
|
|
7
|
+
export * from './sentry/sentry.shared.service.js'
|
|
8
|
+
export * from './server/asyncLocalStorageMiddleware.js'
|
|
9
|
+
export * from './server/basicAuthMiddleware.js'
|
|
10
|
+
export * from './server/bodyParserTimeoutMiddleware.js'
|
|
11
|
+
export * from './server/createDefaultApp.js'
|
|
12
|
+
export * from './server/createDefaultApp.model.js'
|
|
13
|
+
export * from './server/deployInfo.util.js'
|
|
14
|
+
export * from './server/genericErrorMiddleware.js'
|
|
15
|
+
export * from './server/getDefaultRouter.js'
|
|
16
|
+
export * from './server/logMiddleware.js'
|
|
17
|
+
export * from './server/methodOverrideMiddleware.js'
|
|
18
|
+
export * from './server/notFoundMiddleware.js'
|
|
19
|
+
export * from './server/okMiddleware.js'
|
|
20
|
+
export * from './server/request.log.util.js'
|
|
21
|
+
export * from './server/request.util.js'
|
|
22
|
+
export * from './server/requestTimeoutMiddleware.js'
|
|
23
|
+
export * from './server/safeJsonMiddleware.js'
|
|
24
|
+
export * from './server/server.model.js'
|
|
25
|
+
export * from './server/serverStatsMiddleware.js'
|
|
26
|
+
export * from './server/serverStatusMiddleware.js'
|
|
27
|
+
export * from './server/simpleRequestLoggerMiddleware.js'
|
|
28
|
+
export * from './server/startServer.js'
|
|
29
|
+
export * from './server/startServer.model.js'
|
|
30
|
+
export * from './server/validation/validateMiddleware.js'
|
|
31
|
+
export * from './server/validation/validateRequest.js'
|
|
32
|
+
export * from './server/validation/zodValidateMiddleware.js'
|
|
33
|
+
export * from './util.js'
|
|
34
34
|
|
|
35
35
|
export { onFinished }
|
|
@@ -4,7 +4,7 @@ import type { InspectAnyOptions } from '@naturalcycles/nodejs-lib'
|
|
|
4
4
|
import { _inspect } from '@naturalcycles/nodejs-lib'
|
|
5
5
|
import type { Breadcrumb, SeverityLevel } from '@sentry/node'
|
|
6
6
|
import type * as SentryLib from '@sentry/node'
|
|
7
|
-
import { getRequestLogger } from '../index'
|
|
7
|
+
import { getRequestLogger } from '../index.js'
|
|
8
8
|
|
|
9
9
|
export interface SentrySharedServiceCfg {
|
|
10
10
|
sentry: typeof SentryLib
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { AsyncLocalStorage } from 'node:async_hooks'
|
|
2
2
|
import type { CommonLogger } from '@naturalcycles/js-lib'
|
|
3
3
|
import { _lazyValue } from '@naturalcycles/js-lib'
|
|
4
|
-
import { ciLogger, devLogger, gaeLogger } from './logMiddleware'
|
|
5
|
-
import type { BackendRequest, BackendRequestHandler } from './server.model'
|
|
4
|
+
import { ciLogger, devLogger, gaeLogger } from './logMiddleware.js'
|
|
5
|
+
import type { BackendRequest, BackendRequestHandler } from './server.model.js'
|
|
6
6
|
|
|
7
7
|
const { GAE_INSTANCE, CI } = process.env
|
|
8
8
|
const isGAE = !!GAE_INSTANCE
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { StringMap } from '@naturalcycles/js-lib'
|
|
2
2
|
import { _split } from '@naturalcycles/js-lib'
|
|
3
3
|
import { base64ToString, timingSafeStringEqual } from '@naturalcycles/nodejs-lib'
|
|
4
|
-
import type { BackendRequestHandler } from './server.model'
|
|
4
|
+
import type { BackendRequestHandler } from './server.model.js'
|
|
5
5
|
|
|
6
6
|
export interface BasicAuthMiddlewareCfg {
|
|
7
7
|
/**
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { AppError } from '@naturalcycles/js-lib'
|
|
2
|
-
import type { BackendRequestHandler } from '../index'
|
|
3
|
-
import { respondWithError } from '../index'
|
|
2
|
+
import type { BackendRequestHandler } from '../index.js'
|
|
3
|
+
import { respondWithError } from '../index.js'
|
|
4
4
|
|
|
5
5
|
export interface BodyParserTimeoutMiddlewareCfg {
|
|
6
6
|
/**
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import type { Options, OptionsJson, OptionsUrlencoded } from 'body-parser'
|
|
2
2
|
import type { CorsOptions } from 'cors'
|
|
3
|
-
import type { SentrySharedService } from '../sentry/sentry.shared.service'
|
|
4
|
-
import type { GenericErrorMiddlewareCfg } from './genericErrorMiddleware'
|
|
5
|
-
import type { BackendRequestHandler } from './server.model'
|
|
3
|
+
import type { SentrySharedService } from '../sentry/sentry.shared.service.js'
|
|
4
|
+
import type { GenericErrorMiddlewareCfg } from './genericErrorMiddleware.js'
|
|
5
|
+
import type { BackendRequestHandler } from './server.model.js'
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* Plain RequestHandler can be provided - then it's mounted to /
|
|
@@ -1,23 +1,24 @@
|
|
|
1
|
-
import cookieParser
|
|
2
|
-
import cors
|
|
3
|
-
import express
|
|
4
|
-
import
|
|
5
|
-
import {
|
|
6
|
-
import { asyncLocalStorageMiddleware } from './asyncLocalStorageMiddleware'
|
|
1
|
+
import cookieParser from 'cookie-parser'
|
|
2
|
+
import cors from 'cors'
|
|
3
|
+
import express from 'express'
|
|
4
|
+
import { isGAE } from '../util.js'
|
|
5
|
+
import { asyncLocalStorageMiddleware } from './asyncLocalStorageMiddleware.js'
|
|
7
6
|
import type {
|
|
8
7
|
BackendRequestHandlerCfg,
|
|
9
8
|
BackendRequestHandlerWithPath,
|
|
10
9
|
DefaultAppCfg,
|
|
11
|
-
} from './createDefaultApp.model'
|
|
12
|
-
import { genericErrorMiddleware } from './genericErrorMiddleware'
|
|
13
|
-
import { logMiddleware } from './logMiddleware'
|
|
14
|
-
import {
|
|
15
|
-
import {
|
|
16
|
-
import {
|
|
10
|
+
} from './createDefaultApp.model.js'
|
|
11
|
+
import { genericErrorMiddleware } from './genericErrorMiddleware.js'
|
|
12
|
+
import { logMiddleware } from './logMiddleware.js'
|
|
13
|
+
import { methodOverrideMiddleware } from './methodOverrideMiddleware.js'
|
|
14
|
+
import { notFoundMiddleware } from './notFoundMiddleware.js'
|
|
15
|
+
import { requestTimeoutMiddleware } from './requestTimeoutMiddleware.js'
|
|
16
|
+
import type { BackendApplication } from './server.model.js'
|
|
17
|
+
import { simpleRequestLoggerMiddleware } from './simpleRequestLoggerMiddleware.js'
|
|
17
18
|
|
|
18
19
|
const isTest = process.env['APP_ENV'] === 'test'
|
|
19
20
|
|
|
20
|
-
export function createDefaultApp(cfg: DefaultAppCfg): BackendApplication {
|
|
21
|
+
export async function createDefaultApp(cfg: DefaultAppCfg): Promise<BackendApplication> {
|
|
21
22
|
const { sentryService } = cfg
|
|
22
23
|
|
|
23
24
|
const app = express()
|
|
@@ -75,8 +76,9 @@ export function createDefaultApp(cfg: DefaultAppCfg): BackendApplication {
|
|
|
75
76
|
|
|
76
77
|
if (!isTest) {
|
|
77
78
|
// leaks, load lazily
|
|
79
|
+
const { default: helmet } = await import('helmet')
|
|
78
80
|
app.use(
|
|
79
|
-
|
|
81
|
+
helmet({
|
|
80
82
|
contentSecurityPolicy: false, // to allow "admin 401 auto-redirect"
|
|
81
83
|
}),
|
|
82
84
|
)
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { _memoFn, localTime } from '@naturalcycles/js-lib'
|
|
2
2
|
import { fs2 } from '@naturalcycles/nodejs-lib'
|
|
3
|
-
import type { DeployInfo } from '../deploy'
|
|
3
|
+
import type { DeployInfo } from '../deploy/index.js'
|
|
4
4
|
|
|
5
5
|
export const getDeployInfo = _memoFn((projectDir: string): DeployInfo => {
|
|
6
6
|
const deployInfoPath = `${projectDir}/deployInfo.json`
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { AppError, BackendErrorResponseObject, ErrorObject } from '@naturalcycles/js-lib'
|
|
2
2
|
import { _anyToError, _errorLikeToErrorObject, _filterUndefinedValues } from '@naturalcycles/js-lib'
|
|
3
|
-
import type { SentrySharedService } from '../sentry/sentry.shared.service'
|
|
4
|
-
import type { BackendErrorRequestHandler, BackendRequest, BackendResponse } from './server.model'
|
|
3
|
+
import type { SentrySharedService } from '../sentry/sentry.shared.service.js'
|
|
4
|
+
import type { BackendErrorRequestHandler, BackendRequest, BackendResponse } from './server.model.js'
|
|
5
5
|
|
|
6
6
|
export interface GenericErrorMiddlewareCfg {
|
|
7
7
|
sentryService?: SentrySharedService
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { inspect } from 'node:util'
|
|
2
2
|
import type { AnyObject, CommonLogger } from '@naturalcycles/js-lib'
|
|
3
3
|
import { _inspect, dimGrey } from '@naturalcycles/nodejs-lib'
|
|
4
|
-
import type { BackendRequestHandler } from './server.model'
|
|
4
|
+
import type { BackendRequestHandler } from './server.model.js'
|
|
5
5
|
|
|
6
6
|
const { GOOGLE_CLOUD_PROJECT, GAE_INSTANCE, APP_ENV } = process.env
|
|
7
7
|
const isGAE = !!GAE_INSTANCE
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { getRequestEndpoint } from './request.util'
|
|
2
|
-
import type { BackendRequestHandler } from './server.model'
|
|
1
|
+
import { getRequestEndpoint } from './request.util.js'
|
|
2
|
+
import type { BackendRequestHandler } from './server.model.js'
|
|
3
3
|
|
|
4
4
|
export function notFoundMiddleware(): BackendRequestHandler {
|
|
5
5
|
return (req, res) => {
|