@naturalcycles/backend-lib 9.14.0 → 9.16.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.js +7 -6
- package/dist/admin/base.admin.service.js +2 -2
- package/dist/admin/firebase.shared.service.js +1 -1
- package/dist/admin/index.d.ts +4 -0
- package/dist/admin/index.js +4 -0
- package/dist/admin/secureHeaderMiddleware.js +1 -1
- package/dist/db/httpDB.d.ts +3 -1
- package/dist/db/httpDB.js +1 -1
- package/dist/db/httpDBRequestHandler.d.ts +1 -1
- package/dist/db/httpDBRequestHandler.js +4 -3
- package/dist/deploy/backend.cfg.util.d.ts +1 -1
- package/dist/deploy/backend.cfg.util.js +3 -2
- package/dist/deploy/deploy.model.d.ts +1 -1
- package/dist/deploy/deploy.util.js +11 -6
- package/dist/deploy/deployGae.js +6 -3
- package/dist/deploy/deployHealthCheck.js +6 -2
- package/dist/deploy/deployInfo.util.d.ts +2 -0
- package/dist/{server → deploy}/deployInfo.util.js +3 -2
- package/dist/deploy/deployPrepare.js +3 -2
- package/dist/deploy/index.d.ts +1 -0
- package/dist/deploy/index.js +1 -0
- package/dist/env/env.shared.service.d.ts +1 -1
- package/dist/env/env.shared.service.js +3 -2
- package/dist/{server/createDefaultApp.model.d.ts → express/createDefaultApp.d.ts} +3 -2
- package/dist/{server → express}/createDefaultApp.js +10 -10
- package/dist/{server → express}/getDefaultRouter.d.ts +1 -1
- package/dist/{server/startServer.model.d.ts → express/startServer.d.ts} +17 -2
- package/dist/{server → express}/startServer.js +4 -3
- package/dist/index.d.ts +0 -15
- package/dist/index.js +0 -15
- package/dist/onFinished.d.ts +2 -0
- package/dist/onFinished.js +2 -0
- package/dist/sentry/sentry.shared.service.d.ts +2 -1
- package/dist/sentry/sentry.shared.service.js +2 -2
- package/dist/server/asyncLocalStorageMiddleware.d.ts +1 -1
- package/dist/server/basicAuthMiddleware.d.ts +1 -1
- package/dist/server/basicAuthMiddleware.js +1 -1
- package/dist/server/bodyParserTimeoutMiddleware.d.ts +1 -1
- package/dist/server/bodyParserTimeoutMiddleware.js +2 -2
- package/dist/server/genericErrorMiddleware.d.ts +1 -1
- package/dist/server/genericErrorMiddleware.js +2 -2
- package/dist/server/logMiddleware.d.ts +1 -1
- package/dist/server/logMiddleware.js +2 -1
- package/dist/server/request.log.util.js +1 -1
- package/dist/server/requestLoggerMiddleware.d.ts +1 -1
- package/dist/server/requestLoggerMiddleware.js +2 -2
- package/dist/server/requestTimeoutMiddleware.d.ts +2 -2
- package/dist/server/requestTimeoutMiddleware.js +5 -2
- package/dist/server/safeJsonMiddleware.js +1 -1
- package/dist/server/server.model.d.ts +2 -1
- package/dist/server/serverStatsMiddleware.d.ts +1 -1
- package/dist/server/serverStatsMiddleware.js +6 -2
- package/dist/server/serverStatusMiddleware.js +3 -2
- package/dist/server/simpleRequestLoggerMiddleware.d.ts +1 -1
- package/dist/server/simpleRequestLoggerMiddleware.js +3 -3
- package/dist/testing/express.test.service.d.ts +3 -3
- package/dist/testing/express.test.service.js +3 -2
- package/dist/{server/validation → validation/ajv}/ajvValidateRequest.d.ts +2 -2
- package/dist/{server/validation → validation/ajv}/ajvValidateRequest.js +2 -1
- package/dist/{server/validation/validateRequest.d.ts → validation/joi/joiValidateRequest.d.ts} +1 -1
- package/dist/{server/validation/validateRequest.js → validation/joi/joiValidateRequest.js} +2 -1
- package/dist/{server/validation → validation/zod}/zodValidateRequest.d.ts +2 -2
- package/dist/{server/validation → validation/zod}/zodValidateRequest.js +2 -1
- package/package.json +11 -2
- package/src/admin/adminMiddleware.ts +7 -6
- package/src/admin/base.admin.service.ts +2 -2
- package/src/admin/firebase.shared.service.ts +1 -1
- package/src/admin/index.ts +4 -0
- package/src/admin/secureHeaderMiddleware.ts +1 -1
- package/src/db/httpDB.ts +4 -7
- package/src/db/httpDBRequestHandler.ts +5 -4
- package/src/deploy/backend.cfg.util.ts +4 -3
- package/src/deploy/deploy.model.ts +1 -1
- package/src/deploy/deploy.util.ts +11 -6
- package/src/deploy/deployGae.ts +6 -3
- package/src/deploy/deployHealthCheck.ts +7 -3
- package/src/{server → deploy}/deployInfo.util.ts +4 -3
- package/src/deploy/deployPrepare.ts +3 -2
- package/src/deploy/index.ts +2 -0
- package/src/env/env.shared.service.ts +4 -3
- package/src/{server → express}/createDefaultApp.ts +54 -16
- package/src/{server → express}/getDefaultRouter.ts +1 -1
- package/src/{server → express}/startServer.ts +46 -4
- package/src/index.ts +0 -16
- package/src/onFinished.ts +3 -0
- package/src/sentry/sentry.shared.service.ts +4 -3
- package/src/server/asyncLocalStorageMiddleware.ts +1 -1
- package/src/server/basicAuthMiddleware.ts +2 -2
- package/src/server/bodyParserTimeoutMiddleware.ts +3 -3
- package/src/server/genericErrorMiddleware.ts +3 -6
- package/src/server/logMiddleware.ts +4 -2
- package/src/server/request.log.util.ts +2 -2
- package/src/server/requestLoggerMiddleware.ts +4 -4
- package/src/server/requestTimeoutMiddleware.ts +7 -4
- package/src/server/safeJsonMiddleware.ts +1 -1
- package/src/server/server.model.ts +2 -1
- package/src/server/server.util.ts +1 -1
- package/src/server/serverStatsMiddleware.ts +7 -15
- package/src/server/serverStatusMiddleware.ts +3 -2
- package/src/server/simpleRequestLoggerMiddleware.ts +5 -5
- package/src/testing/express.test.service.ts +9 -5
- package/src/{server/validation → validation/ajv}/ajvValidateRequest.ts +4 -3
- package/src/{server/validation/validateRequest.ts → validation/joi/joiValidateRequest.ts} +3 -2
- package/src/{server/validation → validation/zod}/zodValidateRequest.ts +4 -3
- package/dist/server/createDefaultApp.d.ts +0 -3
- package/dist/server/createDefaultApp.model.js +0 -1
- package/dist/server/deployInfo.util.d.ts +0 -2
- package/dist/server/startServer.d.ts +0 -17
- package/dist/server/startServer.model.js +0 -1
- package/src/server/createDefaultApp.model.ts +0 -41
- package/src/server/startServer.model.ts +0 -44
- /package/dist/{server → express}/getDefaultRouter.js +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { _memoFn
|
|
2
|
-
import {
|
|
3
|
-
import
|
|
1
|
+
import { _memoFn } from '@naturalcycles/js-lib/decorators';
|
|
2
|
+
import { AppError } from '@naturalcycles/js-lib/error';
|
|
3
|
+
import { fs2 } from '@naturalcycles/nodejs-lib/fs2';
|
|
4
4
|
import { srcDir } from '../paths.cnst.js';
|
|
5
5
|
export function createAdminMiddleware(adminService, cfgDefaults = {}) {
|
|
6
6
|
return (reqPermissions, cfg) => requireAdminPermissions(adminService, reqPermissions, {
|
|
@@ -37,17 +37,18 @@ export function requireAdminPermissions(adminService, reqPermissions = [], cfg =
|
|
|
37
37
|
}
|
|
38
38
|
export function loginHtml(firebaseServiceCfg) {
|
|
39
39
|
const { apiKey: firebaseApiKey, authDomain: firebaseAuthDomain, adminAuthProvider: firebaseAuthProvider = 'GoogleAuthProvider', } = firebaseServiceCfg;
|
|
40
|
-
return (_req, res) => {
|
|
41
|
-
res.send(getLoginHtml({
|
|
40
|
+
return async (_req, res) => {
|
|
41
|
+
res.send(await getLoginHtml({
|
|
42
42
|
firebaseApiKey,
|
|
43
43
|
firebaseAuthDomain,
|
|
44
44
|
firebaseAuthProvider,
|
|
45
45
|
}));
|
|
46
46
|
};
|
|
47
47
|
}
|
|
48
|
-
const getLoginHtml = _memoFn((cfg) => {
|
|
48
|
+
const getLoginHtml = _memoFn(async (cfg) => {
|
|
49
49
|
console.log(`reading login.html`);
|
|
50
50
|
const tmpl = fs2.readText(`${srcDir}/admin/login.html`);
|
|
51
|
+
const { default: ejs } = await import('ejs');
|
|
51
52
|
return ejs.render(tmpl, cfg);
|
|
52
53
|
});
|
|
53
54
|
export function getLoginHtmlRedirect(href) {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { _assert, AppError } from '@naturalcycles/js-lib';
|
|
2
|
-
import { dimGrey, green, red } from '@naturalcycles/nodejs-lib';
|
|
1
|
+
import { _assert, AppError } from '@naturalcycles/js-lib/error';
|
|
2
|
+
import { dimGrey, green, red } from '@naturalcycles/nodejs-lib/colors';
|
|
3
3
|
const adminInfoDisabled = () => ({
|
|
4
4
|
email: 'authDisabled',
|
|
5
5
|
permissions: [],
|
package/dist/db/httpDB.d.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import type { CommonDB, CommonDBOptions, CommonDBSaveOptions, CommonDBStreamOptions, CommonDBSupport, DBQuery, RunQueryResult } from '@naturalcycles/db-lib';
|
|
2
2
|
import { BaseCommonDB } from '@naturalcycles/db-lib';
|
|
3
|
-
import type { FetcherOptions
|
|
3
|
+
import type { FetcherOptions } from '@naturalcycles/js-lib/http';
|
|
4
|
+
import type { JsonSchemaRootObject } from '@naturalcycles/js-lib/json-schema';
|
|
5
|
+
import type { ObjectWithId } from '@naturalcycles/js-lib/types';
|
|
4
6
|
import type { ReadableTyped } from '@naturalcycles/nodejs-lib/stream';
|
|
5
7
|
export interface HttpDBCfg extends FetcherOptions {
|
|
6
8
|
baseUrl: string;
|
package/dist/db/httpDB.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Readable } from 'node:stream';
|
|
2
2
|
import { BaseCommonDB, commonDBFullSupport } from '@naturalcycles/db-lib';
|
|
3
|
-
import { getFetcher } from '@naturalcycles/js-lib';
|
|
3
|
+
import { getFetcher } from '@naturalcycles/js-lib/http';
|
|
4
4
|
/**
|
|
5
5
|
* Implementation of CommonDB that proxies all requests via HTTP to "httpDBRequestHandler".
|
|
6
6
|
*/
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { CommonDB, CommonDBOptions, CommonDBSaveOptions } from '@naturalcycles/db-lib';
|
|
2
2
|
import { DBQuery } from '@naturalcycles/db-lib';
|
|
3
|
-
import type { ObjectWithId } from '@naturalcycles/js-lib';
|
|
3
|
+
import type { ObjectWithId } from '@naturalcycles/js-lib/types';
|
|
4
4
|
import type { BackendRouter } from '../server/server.model.js';
|
|
5
5
|
export interface GetByIdsInput {
|
|
6
6
|
table: string;
|
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import { DBQuery
|
|
1
|
+
import { DBQuery } from '@naturalcycles/db-lib';
|
|
2
|
+
import { InMemoryDB } from '@naturalcycles/db-lib/inmemory';
|
|
2
3
|
import { commonDBOptionsSchema, commonDBSaveOptionsSchema, dbQuerySchema, } from '@naturalcycles/db-lib/validation';
|
|
3
4
|
import { anyObjectSchema, arraySchema, objectSchema, stringSchema, } from '@naturalcycles/nodejs-lib/joi';
|
|
4
|
-
import { getDefaultRouter } from '../
|
|
5
|
-
import { validateRequest } from '../
|
|
5
|
+
import { getDefaultRouter } from '../express/getDefaultRouter.js';
|
|
6
|
+
import { validateRequest } from '../validation/joi/joiValidateRequest.js';
|
|
6
7
|
const getByIdsInputSchema = objectSchema({
|
|
7
8
|
table: stringSchema,
|
|
8
9
|
ids: arraySchema(stringSchema),
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { AjvSchema } from '@naturalcycles/nodejs-lib/ajv';
|
|
2
|
-
import { fs2 } from '@naturalcycles/nodejs-lib/
|
|
2
|
+
import { fs2 } from '@naturalcycles/nodejs-lib/fs2';
|
|
3
|
+
import { yaml2 } from '@naturalcycles/nodejs-lib/yaml2';
|
|
3
4
|
import { resourcesDir } from '../paths.cnst.js';
|
|
4
5
|
const backendCfgSchema = AjvSchema.readJsonSync(`${resourcesDir}/backendCfg.schema.json`, {
|
|
5
6
|
objectName: 'backend.cfg.yaml',
|
|
@@ -8,7 +9,7 @@ export function getBackendCfg(projectDir = '.') {
|
|
|
8
9
|
const backendCfgYamlPath = `${projectDir}/backend.cfg.yaml`;
|
|
9
10
|
fs2.requireFileToExist(backendCfgYamlPath);
|
|
10
11
|
const backendCfg = {
|
|
11
|
-
...
|
|
12
|
+
...yaml2.readYaml(backendCfgYamlPath),
|
|
12
13
|
};
|
|
13
14
|
backendCfgSchema.validate(backendCfg);
|
|
14
15
|
return backendCfg;
|
|
@@ -1,6 +1,11 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
1
|
+
import { localTime } from '@naturalcycles/js-lib/datetime';
|
|
2
|
+
import { _assert } from '@naturalcycles/js-lib/error';
|
|
3
|
+
import { _mapValues, _merge } from '@naturalcycles/js-lib/object';
|
|
4
|
+
import { _truncate } from '@naturalcycles/js-lib/string';
|
|
5
|
+
import { sha256 } from '@naturalcycles/nodejs-lib';
|
|
6
|
+
import { dimGrey, white } from '@naturalcycles/nodejs-lib/colors';
|
|
7
|
+
import { fs2 } from '@naturalcycles/nodejs-lib/fs2';
|
|
8
|
+
import { yaml2 } from '@naturalcycles/nodejs-lib/yaml2';
|
|
4
9
|
const APP_YAML_DEFAULT = () => ({
|
|
5
10
|
runtime: 'nodejs22',
|
|
6
11
|
service: 'default',
|
|
@@ -77,7 +82,7 @@ export async function createDeployInfo(backendCfg, overrideBranch) {
|
|
|
77
82
|
export function createAndSaveAppYaml(backendCfg, deployInfo, projectDir, targetDir, appYamlPassEnv = '') {
|
|
78
83
|
const appYaml = createAppYaml(backendCfg, deployInfo, projectDir, appYamlPassEnv);
|
|
79
84
|
const appYamlPath = `${targetDir}/app.yaml`;
|
|
80
|
-
|
|
85
|
+
yaml2.writeYaml(appYamlPath, appYaml);
|
|
81
86
|
console.log(`saved ${dimGrey(appYamlPath)}`);
|
|
82
87
|
return appYaml;
|
|
83
88
|
}
|
|
@@ -94,12 +99,12 @@ export function createAppYaml(backendCfg, deployInfo, projectDir, appYamlPassEnv
|
|
|
94
99
|
const appYamlPath = `${projectDir}/app.yaml`;
|
|
95
100
|
if (fs2.pathExists(appYamlPath)) {
|
|
96
101
|
console.log(`merging-in ${dimGrey(appYamlPath)}`);
|
|
97
|
-
_merge(appYaml,
|
|
102
|
+
_merge(appYaml, yaml2.readYaml(appYamlPath));
|
|
98
103
|
}
|
|
99
104
|
const appEnvYamlPath = `${projectDir}/app.${APP_ENV}.yaml`;
|
|
100
105
|
if (fs2.pathExists(appEnvYamlPath)) {
|
|
101
106
|
console.log(`merging-in ${dimGrey(appEnvYamlPath)}`);
|
|
102
|
-
_merge(appYaml,
|
|
107
|
+
_merge(appYaml, yaml2.readYaml(appEnvYamlPath));
|
|
103
108
|
}
|
|
104
109
|
// appYamlPassEnv
|
|
105
110
|
const passEnv = appYamlPassEnv
|
package/dist/deploy/deployGae.js
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
|
-
import { _anyToError
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
1
|
+
import { _anyToError } from '@naturalcycles/js-lib/error';
|
|
2
|
+
import { pRetry } from '@naturalcycles/js-lib/promise';
|
|
3
|
+
import { _objectAssign } from '@naturalcycles/js-lib/types';
|
|
4
|
+
import { appendToGithubSummary } from '@naturalcycles/nodejs-lib/env';
|
|
5
|
+
import { exec2 } from '@naturalcycles/nodejs-lib/exec2';
|
|
6
|
+
import { fs2 } from '@naturalcycles/nodejs-lib/fs2';
|
|
4
7
|
import { getBackendCfg } from './backend.cfg.util.js';
|
|
5
8
|
import { createDeployInfo } from './deploy.util.js';
|
|
6
9
|
import { deployHealthCheck } from './deployHealthCheck.js';
|
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
import { inspect } from 'node:util';
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
2
|
+
import { _ms, _since } from '@naturalcycles/js-lib/datetime';
|
|
3
|
+
import { getFetcher } from '@naturalcycles/js-lib/http';
|
|
4
|
+
import { _filterFalsyValues } from '@naturalcycles/js-lib/object';
|
|
5
|
+
import { pDelay } from '@naturalcycles/js-lib/promise';
|
|
6
|
+
import { dimGrey, red } from '@naturalcycles/nodejs-lib/colors';
|
|
7
|
+
import { exec2 } from '@naturalcycles/nodejs-lib/exec2';
|
|
4
8
|
import { coloredHttpCode } from '../server/request.log.util.js';
|
|
5
9
|
export const deployHealthCheckYargsOptions = {
|
|
6
10
|
thresholdHealthy: {
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { localTime } from '@naturalcycles/js-lib/datetime';
|
|
2
|
+
import { _memoFn } from '@naturalcycles/js-lib/decorators';
|
|
3
|
+
import { fs2 } from '@naturalcycles/nodejs-lib/fs2';
|
|
3
4
|
export const getDeployInfo = _memoFn((projectDir) => {
|
|
4
5
|
const deployInfoPath = `${projectDir}/deployInfo.json`;
|
|
5
6
|
try {
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { dimGrey } from '@naturalcycles/nodejs-lib';
|
|
2
|
-
import { fs2
|
|
1
|
+
import { dimGrey } from '@naturalcycles/nodejs-lib/colors';
|
|
2
|
+
import { fs2 } from '@naturalcycles/nodejs-lib/fs2';
|
|
3
|
+
import { kpySync } from '@naturalcycles/nodejs-lib/kpy';
|
|
3
4
|
import { srcDir } from '../paths.cnst.js';
|
|
4
5
|
import { getBackendCfg } from './backend.cfg.util.js';
|
|
5
6
|
import { createAndSaveAppYaml, createAndSaveDeployInfo } from './deploy.util.js';
|
package/dist/deploy/index.d.ts
CHANGED
|
@@ -8,3 +8,4 @@ import { deployHealthCheck } from './deployHealthCheck.js';
|
|
|
8
8
|
import { deployPrepare } from './deployPrepare.js';
|
|
9
9
|
export type { BackendCfg, DeployHealthCheckOptions, DeployInfo };
|
|
10
10
|
export { createAppYaml, createDeployInfo, deployGae, deployHealthCheck, deployPrepare, getBackendCfg, };
|
|
11
|
+
export * from './deployInfo.util.js';
|
package/dist/deploy/index.js
CHANGED
|
@@ -4,3 +4,4 @@ import { deployGae } from './deployGae.js';
|
|
|
4
4
|
import { deployHealthCheck } from './deployHealthCheck.js';
|
|
5
5
|
import { deployPrepare } from './deployPrepare.js';
|
|
6
6
|
export { createAppYaml, createDeployInfo, deployGae, deployHealthCheck, deployPrepare, getBackendCfg, };
|
|
7
|
+
export * from './deployInfo.util.js';
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { _by } from '@naturalcycles/js-lib';
|
|
2
|
+
import { _assert } from '@naturalcycles/js-lib/error';
|
|
3
|
+
import { dimGrey } from '@naturalcycles/nodejs-lib/colors';
|
|
3
4
|
export class EnvSharedService {
|
|
4
5
|
constructor(cfg) {
|
|
5
6
|
this.envMap = _by(cfg.environments, e => e.name);
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import type { Options, OptionsJson, OptionsUrlencoded } from 'body-parser';
|
|
2
2
|
import type { CorsOptions } from 'cors';
|
|
3
3
|
import type { SentrySharedService } from '../sentry/sentry.shared.service.js';
|
|
4
|
-
import type
|
|
5
|
-
import type { BackendRequestHandler } from '
|
|
4
|
+
import { type GenericErrorMiddlewareCfg } from '../server/genericErrorMiddleware.js';
|
|
5
|
+
import type { BackendApplication, BackendRequestHandler } from '../server/server.model.js';
|
|
6
|
+
export declare function createDefaultApp(cfg: DefaultAppCfg): Promise<BackendApplication>;
|
|
6
7
|
/**
|
|
7
8
|
* Plain RequestHandler can be provided - then it's mounted to /
|
|
8
9
|
* Otherwise `path` can be provided to specify mounting point.
|
|
@@ -1,17 +1,17 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import { notFoundMiddleware } from './notFoundMiddleware.js';
|
|
9
|
-
import { requestTimeoutMiddleware } from './requestTimeoutMiddleware.js';
|
|
10
|
-
import { simpleRequestLoggerMiddleware } from './simpleRequestLoggerMiddleware.js';
|
|
1
|
+
import { asyncLocalStorageMiddleware } from '../server/asyncLocalStorageMiddleware.js';
|
|
2
|
+
import { genericErrorMiddleware, } from '../server/genericErrorMiddleware.js';
|
|
3
|
+
import { logMiddleware } from '../server/logMiddleware.js';
|
|
4
|
+
import { methodOverrideMiddleware } from '../server/methodOverrideMiddleware.js';
|
|
5
|
+
import { notFoundMiddleware } from '../server/notFoundMiddleware.js';
|
|
6
|
+
import { requestTimeoutMiddleware } from '../server/requestTimeoutMiddleware.js';
|
|
7
|
+
import { simpleRequestLoggerMiddleware } from '../server/simpleRequestLoggerMiddleware.js';
|
|
11
8
|
const isTest = process.env['APP_ENV'] === 'test';
|
|
12
9
|
const isDev = process.env['APP_ENV'] === 'dev';
|
|
13
10
|
export async function createDefaultApp(cfg) {
|
|
14
11
|
const { sentryService } = cfg;
|
|
12
|
+
const { default: express } = await import('express');
|
|
13
|
+
const { default: cors } = await import('cors');
|
|
14
|
+
const { default: cookieParser } = await import('cookie-parser');
|
|
15
15
|
const app = express();
|
|
16
16
|
app.disable('etag');
|
|
17
17
|
app.disable('x-powered-by');
|
|
@@ -1,7 +1,22 @@
|
|
|
1
1
|
import type { Server } from 'node:http';
|
|
2
2
|
import type { SentrySharedService } from '../sentry/sentry.shared.service.js';
|
|
3
|
-
import type {
|
|
4
|
-
import type
|
|
3
|
+
import type { BackendApplication } from '../server/server.model.js';
|
|
4
|
+
import { type DefaultAppCfg } from './createDefaultApp.js';
|
|
5
|
+
export declare class BackendServer {
|
|
6
|
+
private cfg;
|
|
7
|
+
constructor(cfg: StartServerCfg);
|
|
8
|
+
server?: Server;
|
|
9
|
+
start(): Promise<StartServerData>;
|
|
10
|
+
/**
|
|
11
|
+
* Gracefully shuts down the server.
|
|
12
|
+
* Does `process.exit()` in the end.
|
|
13
|
+
*/
|
|
14
|
+
stop(reason: string): Promise<void>;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Convenience function.
|
|
18
|
+
*/
|
|
19
|
+
export declare function startServer(cfg: StartServerCfg): Promise<StartServerData>;
|
|
5
20
|
/**
|
|
6
21
|
* If DefaultAppCfg.resources is passed and `expressApp` is not passed - it will call createDefaultApp(cfg).
|
|
7
22
|
*/
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { __decorate } from "tslib";
|
|
2
2
|
import os from 'node:os';
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
3
|
+
import { _ms } from '@naturalcycles/js-lib/datetime';
|
|
4
|
+
import { _Memo } from '@naturalcycles/js-lib/decorators';
|
|
5
|
+
import { boldGrey, dimGrey, white } from '@naturalcycles/nodejs-lib/colors';
|
|
6
|
+
import { createDefaultApp } from './createDefaultApp.js';
|
|
6
7
|
const { NODE_OPTIONS, APP_ENV } = process.env;
|
|
7
8
|
export class BackendServer {
|
|
8
9
|
cfg;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,18 +1,9 @@
|
|
|
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
1
|
export * from './env/env.shared.service.js';
|
|
7
2
|
export * from './sentry/sentry.shared.service.js';
|
|
8
3
|
export * from './server/asyncLocalStorageMiddleware.js';
|
|
9
4
|
export * from './server/basicAuthMiddleware.js';
|
|
10
5
|
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
6
|
export * from './server/genericErrorMiddleware.js';
|
|
15
|
-
export * from './server/getDefaultRouter.js';
|
|
16
7
|
export * from './server/logMiddleware.js';
|
|
17
8
|
export * from './server/methodOverrideMiddleware.js';
|
|
18
9
|
export * from './server/notFoundMiddleware.js';
|
|
@@ -26,10 +17,4 @@ export * from './server/server.model.js';
|
|
|
26
17
|
export * from './server/serverStatsMiddleware.js';
|
|
27
18
|
export * from './server/serverStatusMiddleware.js';
|
|
28
19
|
export * from './server/simpleRequestLoggerMiddleware.js';
|
|
29
|
-
export * from './server/startServer.js';
|
|
30
|
-
export * from './server/startServer.model.js';
|
|
31
|
-
export * from './server/validation/ajvValidateRequest.js';
|
|
32
|
-
export * from './server/validation/validateRequest.js';
|
|
33
|
-
export * from './server/validation/zodValidateRequest.js';
|
|
34
20
|
export * from './util.js';
|
|
35
|
-
export { onFinished };
|
package/dist/index.js
CHANGED
|
@@ -1,18 +1,9 @@
|
|
|
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
1
|
export * from './env/env.shared.service.js';
|
|
7
2
|
export * from './sentry/sentry.shared.service.js';
|
|
8
3
|
export * from './server/asyncLocalStorageMiddleware.js';
|
|
9
4
|
export * from './server/basicAuthMiddleware.js';
|
|
10
5
|
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
6
|
export * from './server/genericErrorMiddleware.js';
|
|
15
|
-
export * from './server/getDefaultRouter.js';
|
|
16
7
|
export * from './server/logMiddleware.js';
|
|
17
8
|
export * from './server/methodOverrideMiddleware.js';
|
|
18
9
|
export * from './server/notFoundMiddleware.js';
|
|
@@ -26,10 +17,4 @@ export * from './server/server.model.js';
|
|
|
26
17
|
export * from './server/serverStatsMiddleware.js';
|
|
27
18
|
export * from './server/serverStatusMiddleware.js';
|
|
28
19
|
export * from './server/simpleRequestLoggerMiddleware.js';
|
|
29
|
-
export * from './server/startServer.js';
|
|
30
|
-
export * from './server/startServer.model.js';
|
|
31
|
-
export * from './server/validation/ajvValidateRequest.js';
|
|
32
|
-
export * from './server/validation/validateRequest.js';
|
|
33
|
-
export * from './server/validation/zodValidateRequest.js';
|
|
34
20
|
export * from './util.js';
|
|
35
|
-
export { onFinished };
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import type { CommonLogger
|
|
1
|
+
import type { CommonLogger } from '@naturalcycles/js-lib/log';
|
|
2
|
+
import type { Primitive, StringMap } from '@naturalcycles/js-lib/types';
|
|
2
3
|
import type { Breadcrumb, SeverityLevel } from '@sentry/node';
|
|
3
4
|
import type * as SentryLib from '@sentry/node';
|
|
4
5
|
export interface SentrySharedServiceCfg {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { _anyToError, _isErrorObject } from '@naturalcycles/js-lib';
|
|
1
|
+
import { _anyToError, _isErrorObject } from '@naturalcycles/js-lib/error';
|
|
2
2
|
import { _inspect } from '@naturalcycles/nodejs-lib';
|
|
3
|
-
import { getRequestLogger } from '../
|
|
3
|
+
import { getRequestLogger } from '../server/asyncLocalStorageMiddleware.js';
|
|
4
4
|
const sentrySeverityMap = {
|
|
5
5
|
debug: 'log',
|
|
6
6
|
info: 'log',
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { AppError } from '@naturalcycles/js-lib';
|
|
2
|
-
import { respondWithError } from '
|
|
1
|
+
import { AppError } from '@naturalcycles/js-lib/error';
|
|
2
|
+
import { respondWithError } from './genericErrorMiddleware.js';
|
|
3
3
|
const code = 'BODY_PARSER_TIMEOUT';
|
|
4
4
|
/**
|
|
5
5
|
* Should be called BEFORE bodyParser
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { ErrorObject } from '@naturalcycles/js-lib/error';
|
|
2
2
|
import type { BackendErrorRequestHandler, BackendRequest, BackendResponse } from './server.model.js';
|
|
3
3
|
export interface GenericErrorMiddlewareCfg {
|
|
4
4
|
errorReportingService?: ErrorReportingService;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { _anyToError, _errorLikeToErrorObject } from '@naturalcycles/js-lib/error';
|
|
2
|
+
import { _objectAssign } from '@naturalcycles/js-lib/types';
|
|
3
3
|
const { APP_ENV } = process.env;
|
|
4
4
|
const includeErrorStack = APP_ENV === 'dev';
|
|
5
5
|
// Hacky way to store the errorService, so it's available to `respondWithError` function
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { inspect } from 'node:util';
|
|
2
|
-
import { _inspect
|
|
2
|
+
import { _inspect } from '@naturalcycles/nodejs-lib';
|
|
3
|
+
import { dimGrey } from '@naturalcycles/nodejs-lib/colors';
|
|
3
4
|
const { GOOGLE_CLOUD_PROJECT, GAE_INSTANCE, K_SERVICE, APP_ENV } = process.env;
|
|
4
5
|
const isGAE = !!GAE_INSTANCE;
|
|
5
6
|
const isCloudRun = !!K_SERVICE;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { boldGrey, green, red, yellow } from '@naturalcycles/nodejs-lib';
|
|
1
|
+
import { boldGrey, green, red, yellow } from '@naturalcycles/nodejs-lib/colors';
|
|
2
2
|
export function logRequestWithColors(req, statusCode, ...tokens) {
|
|
3
3
|
req[logLevel(statusCode)]([coloredHttpCode(statusCode), req.method, boldGrey(req.url), ...tokens].join(' '));
|
|
4
4
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { BackendRequest, BackendRequestHandler } from '
|
|
1
|
+
import type { BackendRequest, BackendRequestHandler } from './server.model.js';
|
|
2
2
|
export interface RequestLoggerMiddlewareCfg {
|
|
3
3
|
/**
|
|
4
4
|
* If set - this prefix will be removed from the request url before logging.
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { NumberOfSeconds } from '@naturalcycles/js-lib';
|
|
2
|
-
import type { BackendRequest, BackendRequestHandler, BackendResponse } from '
|
|
1
|
+
import type { NumberOfSeconds } from '@naturalcycles/js-lib/types';
|
|
2
|
+
import type { BackendRequest, BackendRequestHandler, BackendResponse } from './server.model.js';
|
|
3
3
|
export interface RequestTimeoutMiddlewareCfg {
|
|
4
4
|
/**
|
|
5
5
|
* @default 120
|
|
@@ -1,5 +1,8 @@
|
|
|
1
|
-
import { _ms
|
|
2
|
-
import {
|
|
1
|
+
import { _ms } from '@naturalcycles/js-lib/datetime';
|
|
2
|
+
import { AppError } from '@naturalcycles/js-lib/error';
|
|
3
|
+
import { onFinished } from '../onFinished.js';
|
|
4
|
+
import { respondWithError } from './genericErrorMiddleware.js';
|
|
5
|
+
import { getRequestEndpoint } from './request.util.js';
|
|
3
6
|
const code = 'REQUEST_TIMEOUT';
|
|
4
7
|
const REQUEST_TIMEOUT_QUERY_KEY = 'requestTimeout';
|
|
5
8
|
export function requestTimeoutMiddleware(cfg = {}) {
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import type { CommonLogFunction
|
|
1
|
+
import type { CommonLogFunction } from '@naturalcycles/js-lib/log';
|
|
2
|
+
import type { Promisable } from '@naturalcycles/js-lib/types';
|
|
2
3
|
import type { Application, IRouter, NextFunction, Request, Response } from 'express';
|
|
3
4
|
/**
|
|
4
5
|
* Use this interface instead of express.Request in cases when TypeScript gives an error, because it haven't "included" this very file.
|
|
@@ -1,5 +1,9 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { _mb, _sortBy, _sum } from '@naturalcycles/js-lib';
|
|
2
|
+
import { _ms } from '@naturalcycles/js-lib/datetime';
|
|
3
|
+
import { _percentile, NumberStack } from '@naturalcycles/js-lib/math';
|
|
4
|
+
import { _get, _mapValues } from '@naturalcycles/js-lib/object';
|
|
5
|
+
import { _stringMapEntries, _stringMapValues } from '@naturalcycles/js-lib/types';
|
|
6
|
+
import { onFinished } from '../onFinished.js';
|
|
3
7
|
import { getRequestEndpoint } from './request.util.js';
|
|
4
8
|
const { GAE_INSTANCE } = process.env;
|
|
5
9
|
const serverStatsMap = {};
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { localTime } from '@naturalcycles/js-lib/datetime';
|
|
2
|
+
import { _filterNullishValues } from '@naturalcycles/js-lib/object';
|
|
2
3
|
import { memoryUsageFull, processSharedUtil } from '@naturalcycles/nodejs-lib';
|
|
3
|
-
import { getDeployInfo } from '
|
|
4
|
+
import { getDeployInfo } from '../deploy/deployInfo.util.js';
|
|
4
5
|
const { versions, arch, platform } = process;
|
|
5
6
|
const { GAE_APPLICATION, GAE_SERVICE, GAE_VERSION, GOOGLE_CLOUD_PROJECT, K_SERVICE, K_REVISION, APP_ENV, NODE_OPTIONS, DEPLOY_BUILD_TIME, } = process.env;
|
|
6
7
|
export function serverStatusMiddleware(projectDir, extra) {
|