@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,8 @@
|
|
|
1
1
|
import { inspect } from 'node:util'
|
|
2
|
-
import type {
|
|
3
|
-
import {
|
|
2
|
+
import type { CommonLogger } from '@naturalcycles/js-lib/log'
|
|
3
|
+
import type { AnyObject } from '@naturalcycles/js-lib/types'
|
|
4
|
+
import { _inspect } from '@naturalcycles/nodejs-lib'
|
|
5
|
+
import { dimGrey } from '@naturalcycles/nodejs-lib/colors'
|
|
4
6
|
import type { BackendRequestHandler } from './server.model.js'
|
|
5
7
|
|
|
6
8
|
const { GOOGLE_CLOUD_PROJECT, GAE_INSTANCE, K_SERVICE, APP_ENV } = process.env
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { CommonLogLevel } from '@naturalcycles/js-lib'
|
|
2
|
-
import { boldGrey, green, red, yellow } from '@naturalcycles/nodejs-lib'
|
|
1
|
+
import type { CommonLogLevel } from '@naturalcycles/js-lib/log'
|
|
2
|
+
import { boldGrey, green, red, yellow } from '@naturalcycles/nodejs-lib/colors'
|
|
3
3
|
import type { BackendRequest } from './server.model.js'
|
|
4
4
|
|
|
5
5
|
export function logRequestWithColors(
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
import
|
|
4
|
-
import {
|
|
1
|
+
import { _since } from '@naturalcycles/js-lib/datetime'
|
|
2
|
+
import type { UnixTimestampMillis } from '@naturalcycles/js-lib/types'
|
|
3
|
+
import { onFinished } from '../onFinished.js'
|
|
4
|
+
import type { BackendRequest, BackendRequestHandler } from './server.model.js'
|
|
5
5
|
|
|
6
6
|
export interface RequestLoggerMiddlewareCfg {
|
|
7
7
|
/**
|
|
@@ -1,7 +1,10 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
import type {
|
|
4
|
-
import {
|
|
1
|
+
import { _ms } from '@naturalcycles/js-lib/datetime'
|
|
2
|
+
import { AppError } from '@naturalcycles/js-lib/error'
|
|
3
|
+
import type { NumberOfSeconds } from '@naturalcycles/js-lib/types'
|
|
4
|
+
import { onFinished } from '../onFinished.js'
|
|
5
|
+
import { respondWithError } from './genericErrorMiddleware.js'
|
|
6
|
+
import { getRequestEndpoint } from './request.util.js'
|
|
7
|
+
import type { BackendRequest, BackendRequestHandler, BackendResponse } from './server.model.js'
|
|
5
8
|
|
|
6
9
|
export interface RequestTimeoutMiddlewareCfg {
|
|
7
10
|
/**
|
|
@@ -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
|
/**
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { Server } from 'node:http'
|
|
2
2
|
import type { Socket } from 'node:net'
|
|
3
|
-
import type { StringMap } from '@naturalcycles/js-lib'
|
|
3
|
+
import type { StringMap } from '@naturalcycles/js-lib/types'
|
|
4
4
|
|
|
5
5
|
export interface DestroyableServer extends Server {
|
|
6
6
|
destroy: () => Promise<void>
|
|
@@ -1,19 +1,11 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
_percentile,
|
|
8
|
-
_sortBy,
|
|
9
|
-
_stringMapEntries,
|
|
10
|
-
_stringMapValues,
|
|
11
|
-
_sum,
|
|
12
|
-
NumberStack,
|
|
13
|
-
} from '@naturalcycles/js-lib'
|
|
14
|
-
import type { BackendRequestHandler } from '../index.js'
|
|
15
|
-
import { onFinished } from '../index.js'
|
|
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, type StringMap } from '@naturalcycles/js-lib/types'
|
|
6
|
+
import { onFinished } from '../onFinished.js'
|
|
16
7
|
import { getRequestEndpoint } from './request.util.js'
|
|
8
|
+
import type { BackendRequestHandler } from './server.model.js'
|
|
17
9
|
|
|
18
10
|
const { GAE_INSTANCE } = process.env
|
|
19
11
|
|
|
@@ -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
|
import type { BackendRequestHandler } from './server.model.js'
|
|
5
6
|
|
|
6
7
|
const { versions, arch, platform } = process
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
import { boldGrey, dimGrey } from '@naturalcycles/nodejs-lib'
|
|
4
|
-
import
|
|
5
|
-
import { onFinished } from '../index.js'
|
|
1
|
+
import { _since } from '@naturalcycles/js-lib/datetime'
|
|
2
|
+
import type { UnixTimestampMillis } from '@naturalcycles/js-lib/types'
|
|
3
|
+
import { boldGrey, dimGrey } from '@naturalcycles/nodejs-lib/colors'
|
|
4
|
+
import { onFinished } from '../onFinished.js'
|
|
6
5
|
import { logRequestWithColors } from './request.log.util.js'
|
|
6
|
+
import type { BackendRequestHandler } from './server.model.js'
|
|
7
7
|
|
|
8
8
|
export interface SimpleRequestLoggerMiddlewareCfg {
|
|
9
9
|
/**
|
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
import type { Server } from 'node:http'
|
|
2
2
|
import type { AddressInfo } from 'node:net'
|
|
3
|
-
import type { Fetcher, FetcherOptions, FetchFunction } from '@naturalcycles/js-lib'
|
|
4
|
-
import { getFetcher
|
|
5
|
-
import
|
|
6
|
-
import {
|
|
7
|
-
|
|
3
|
+
import type { Fetcher, FetcherOptions, FetchFunction } from '@naturalcycles/js-lib/http'
|
|
4
|
+
import { getFetcher } from '@naturalcycles/js-lib/http'
|
|
5
|
+
import { pDelay } from '@naturalcycles/js-lib/promise'
|
|
6
|
+
import {
|
|
7
|
+
type BackendRequestHandlerCfg,
|
|
8
|
+
createDefaultApp,
|
|
9
|
+
type DefaultAppCfg,
|
|
10
|
+
} from '../express/createDefaultApp.js'
|
|
11
|
+
import type { BackendApplication } from '../server/server.model.js'
|
|
8
12
|
|
|
9
13
|
const nativeFetchFn: FetchFunction = async (url, init) => await globalThis.fetch(url, init)
|
|
10
14
|
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { AppError } from '@naturalcycles/js-lib/error'
|
|
2
|
+
import { _get } from '@naturalcycles/js-lib/object'
|
|
2
3
|
import type { AjvSchema, AjvValidationError } from '@naturalcycles/nodejs-lib/ajv'
|
|
3
|
-
import type { BackendRequest } from '
|
|
4
|
-
import type { ReqValidationOptions } from '
|
|
4
|
+
import type { BackendRequest } from '../../server/server.model.js'
|
|
5
|
+
import type { ReqValidationOptions } from '../joi/joiValidateRequest.js'
|
|
5
6
|
|
|
6
7
|
class AjvValidateRequest {
|
|
7
8
|
body<T>(
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { AppError } from '@naturalcycles/js-lib/error'
|
|
2
|
+
import { _get } from '@naturalcycles/js-lib/object'
|
|
2
3
|
import type { AnySchema, JoiValidationError } from '@naturalcycles/nodejs-lib/joi'
|
|
3
4
|
import { getValidationResult } from '@naturalcycles/nodejs-lib/joi'
|
|
4
|
-
import type { BackendRequest } from '
|
|
5
|
+
import type { BackendRequest } from '../../server/server.model.js'
|
|
5
6
|
|
|
6
7
|
export interface ReqValidationOptions<ERR extends Error> {
|
|
7
8
|
/**
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { AppError } from '@naturalcycles/js-lib/error'
|
|
2
|
+
import { _get } from '@naturalcycles/js-lib/object'
|
|
2
3
|
import { type ZodType, type ZodValidationError, zSafeValidate } from '@naturalcycles/js-lib/zod'
|
|
3
|
-
import type { BackendRequest } from '
|
|
4
|
-
import type { ReqValidationOptions } from '
|
|
4
|
+
import type { BackendRequest } from '../../server/server.model.js'
|
|
5
|
+
import type { ReqValidationOptions } from '../joi/joiValidateRequest.js'
|
|
5
6
|
|
|
6
7
|
class ZodValidateRequest {
|
|
7
8
|
body<T>(
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import type { Server } from 'node:http';
|
|
2
|
-
import type { StartServerCfg, StartServerData } from './startServer.model.js';
|
|
3
|
-
export declare class BackendServer {
|
|
4
|
-
private cfg;
|
|
5
|
-
constructor(cfg: StartServerCfg);
|
|
6
|
-
server?: Server;
|
|
7
|
-
start(): Promise<StartServerData>;
|
|
8
|
-
/**
|
|
9
|
-
* Gracefully shuts down the server.
|
|
10
|
-
* Does `process.exit()` in the end.
|
|
11
|
-
*/
|
|
12
|
-
stop(reason: string): Promise<void>;
|
|
13
|
-
}
|
|
14
|
-
/**
|
|
15
|
-
* Convenience function.
|
|
16
|
-
*/
|
|
17
|
-
export declare function startServer(cfg: StartServerCfg): Promise<StartServerData>;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import type { Options, OptionsJson, OptionsUrlencoded } from 'body-parser'
|
|
2
|
-
import type { CorsOptions } from 'cors'
|
|
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
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Plain RequestHandler can be provided - then it's mounted to /
|
|
9
|
-
* Otherwise `path` can be provided to specify mounting point.
|
|
10
|
-
*/
|
|
11
|
-
export type BackendRequestHandlerCfg = BackendRequestHandler | BackendRequestHandlerWithPath
|
|
12
|
-
|
|
13
|
-
export interface BackendRequestHandlerWithPath {
|
|
14
|
-
path: string
|
|
15
|
-
handler: BackendRequestHandler
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Handlers are used in this order:
|
|
20
|
-
*
|
|
21
|
-
* 1. preHandlers
|
|
22
|
-
* 2. handlers
|
|
23
|
-
* 3. resources
|
|
24
|
-
* 4. postHandlers
|
|
25
|
-
*/
|
|
26
|
-
export interface DefaultAppCfg {
|
|
27
|
-
preHandlers?: BackendRequestHandlerCfg[]
|
|
28
|
-
handlers?: BackendRequestHandlerCfg[]
|
|
29
|
-
resources?: BackendRequestHandlerCfg[]
|
|
30
|
-
postHandlers?: BackendRequestHandlerCfg[]
|
|
31
|
-
|
|
32
|
-
sentryService?: SentrySharedService
|
|
33
|
-
|
|
34
|
-
bodyParserJsonOptions?: OptionsJson
|
|
35
|
-
bodyParserUrlEncodedOptions?: OptionsUrlencoded
|
|
36
|
-
bodyParserRawOptions?: Options
|
|
37
|
-
|
|
38
|
-
corsOptions?: CorsOptions
|
|
39
|
-
|
|
40
|
-
genericErrorMwCfg?: GenericErrorMiddlewareCfg
|
|
41
|
-
}
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
import type { Server } from 'node:http'
|
|
2
|
-
import type { SentrySharedService } from '../sentry/sentry.shared.service.js'
|
|
3
|
-
import type { DefaultAppCfg } from './createDefaultApp.model.js'
|
|
4
|
-
import type { BackendApplication } from './server.model.js'
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* If DefaultAppCfg.resources is passed and `expressApp` is not passed - it will call createDefaultApp(cfg).
|
|
8
|
-
*/
|
|
9
|
-
export interface StartServerCfg extends DefaultAppCfg {
|
|
10
|
-
/**
|
|
11
|
-
* @default process.env.PORT || 8080
|
|
12
|
-
*/
|
|
13
|
-
port?: number
|
|
14
|
-
|
|
15
|
-
expressApp?: BackendApplication
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Server will wait for promise to resolve until shutting down.
|
|
19
|
-
* (with a timeout)
|
|
20
|
-
*/
|
|
21
|
-
onShutdown?: () => Promise<void>
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* @default 3000
|
|
25
|
-
*/
|
|
26
|
-
forceShutdownTimeout?: number
|
|
27
|
-
|
|
28
|
-
sentryService?: SentrySharedService
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Defaults to true.
|
|
32
|
-
* Set to false if you already have your handlers elsewhere and don't need them here.
|
|
33
|
-
*/
|
|
34
|
-
registerUncaughtExceptionHandlers?: boolean
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
export interface StartServerData {
|
|
38
|
-
port: number
|
|
39
|
-
server: Server
|
|
40
|
-
/**
|
|
41
|
-
* "Processed" server.address() as a string, ready to Cmd+click in MacOS Terminal
|
|
42
|
-
*/
|
|
43
|
-
address: string
|
|
44
|
-
}
|
|
File without changes
|