alp-node 6.2.0 → 7.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/CHANGELOG.md +14 -0
- package/dist/AlpNodeApp-node18.mjs +303 -12
- package/dist/AlpNodeApp-node18.mjs.map +1 -1
- package/dist/definitions/AlpNodeApp.d.ts +15 -7
- package/dist/definitions/AlpNodeApp.d.ts.map +1 -1
- package/dist/definitions/config.d.ts +18 -0
- package/dist/definitions/config.d.ts.map +1 -0
- package/dist/definitions/errors.d.ts +4 -0
- package/dist/definitions/errors.d.ts.map +1 -0
- package/dist/definitions/index.d.ts +5 -3
- package/dist/definitions/index.d.ts.map +1 -1
- package/dist/definitions/language.d.ts +7 -0
- package/dist/definitions/language.d.ts.map +1 -0
- package/dist/definitions/listen.d.ts +7 -0
- package/dist/definitions/listen.d.ts.map +1 -0
- package/dist/definitions/params/ParamValid.d.ts +9 -0
- package/dist/definitions/params/ParamValid.d.ts.map +1 -0
- package/dist/definitions/params/ParamValidationResult.d.ts +9 -0
- package/dist/definitions/params/ParamValidationResult.d.ts.map +1 -0
- package/dist/definitions/params/ParamValidationResult.test.d.ts +2 -0
- package/dist/definitions/params/ParamValidationResult.test.d.ts.map +1 -0
- package/dist/definitions/params/ParamValueFromContext.d.ts +13 -0
- package/dist/definitions/params/ParamValueFromContext.d.ts.map +1 -0
- package/dist/definitions/params/ParamValueModelValidator.d.ts +4 -0
- package/dist/definitions/params/ParamValueModelValidator.d.ts.map +1 -0
- package/dist/definitions/params/ParamValueStringValidator.d.ts +5 -0
- package/dist/definitions/params/ParamValueStringValidator.d.ts.map +1 -0
- package/dist/definitions/params/ParamValueValidator.d.ts +10 -0
- package/dist/definitions/params/ParamValueValidator.d.ts.map +1 -0
- package/dist/definitions/params/index.d.ts +15 -0
- package/dist/definitions/params/index.d.ts.map +1 -0
- package/dist/definitions/router.d.ts +14 -0
- package/dist/definitions/router.d.ts.map +1 -0
- package/dist/definitions/translate/index.d.ts +11 -0
- package/dist/definitions/translate/index.d.ts.map +1 -0
- package/dist/definitions/translate/load.d.ts +4 -0
- package/dist/definitions/translate/load.d.ts.map +1 -0
- package/dist/definitions/types.d.ts +53 -0
- package/dist/definitions/types.d.ts.map +1 -0
- package/dist/index-node18.mjs +402 -15
- package/dist/index-node18.mjs.map +1 -1
- package/package.json +8 -9
- package/src/AlpNodeApp.ts +42 -21
- package/src/config.ts +121 -0
- package/src/errors.ts +70 -0
- package/src/index.ts +18 -3
- package/src/language.ts +27 -0
- package/src/listen.ts +68 -0
- package/src/params/ParamValid.ts +15 -0
- package/src/params/ParamValidationResult.test.ts +65 -0
- package/src/params/ParamValidationResult.ts +38 -0
- package/src/params/ParamValueFromContext.ts +42 -0
- package/src/params/ParamValueModelValidator.ts +36 -0
- package/src/params/ParamValueStringValidator.ts +13 -0
- package/src/params/ParamValueValidator.ts +23 -0
- package/src/params/index.ts +66 -0
- package/src/router.ts +64 -0
- package/src/translate/index.ts +45 -0
- package/src/translate/load.ts +30 -0
- package/src/types.ts +67 -0
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index-node18.mjs","sources":["../src/AlpNodeApp.ts","../src/index.ts"],"sourcesContent":["import type { IncomingMessage, Server, ServerResponse } from 'node:http';\nimport path from 'node:path';\nimport { deprecate } from 'node:util';\nimport _listen from 'alp-listen';\nimport type { Config } from 'alp-node-config';\nimport _config from 'alp-node-config';\nimport errors from 'alp-node-errors';\nimport language from 'alp-node-language';\nimport params from 'alp-params';\nimport translate from 'alp-translate';\nimport type {\n NodeApplication,\n NodeConfig,\n Context as AlpContext,\n ContextState,\n ContextSanitizedState,\n ContextRequest,\n} from 'alp-types';\nimport Koa from 'koa';\nimport type { ParameterizedContext, DefaultState, BaseRequest } from 'koa';\nimport compress from 'koa-compress';\nimport serve from 'koa-static';\nimport { Logger } from 'nightingale-logger';\n\nconst logger = new Logger('alp');\n\nexport interface AlpNodeAppOptions {\n appDirname: string;\n packageDirname: string;\n config: Config & NodeConfig;\n certPath?: string;\n publicPath?: string;\n}\n\ndeclare module 'koa' {\n // eslint-disable-next-line @typescript-eslint/no-empty-interface, @typescript-eslint/no-shadow\n interface DefaultState extends ContextState {}\n // eslint-disable-next-line @typescript-eslint/no-empty-interface\n interface DefaultContext extends AlpContext {}\n // eslint-disable-next-line @typescript-eslint/no-empty-interface\n interface BaseContext extends AlpContext {}\n // eslint-disable-next-line @typescript-eslint/no-empty-interface, @typescript-eslint/no-shadow\n interface BaseRequest extends ContextRequest {}\n}\n\nexport class AlpNodeApp extends Koa<ContextState> implements NodeApplication {\n dirname: string;\n\n certPath: string;\n\n publicPath: string;\n\n config: Config & NodeConfig;\n\n declare request: BaseRequest & ContextRequest;\n\n _server?: Server;\n\n /**\n * @param {Object} [options]\n * @param {string} [options.certPath] directory of the ssl certificates\n * @param {string} [options.publicPath] directory of public files\n */\n constructor({\n appDirname,\n packageDirname,\n config,\n certPath,\n publicPath,\n }: AlpNodeAppOptions) {\n super();\n\n this.dirname = path.normalize(appDirname);\n\n Object.defineProperty(this, 'packageDirname', {\n get: deprecate(() => packageDirname, 'packageDirname'),\n configurable: false,\n enumerable: false,\n });\n\n this.certPath = certPath || `${packageDirname}/config/cert`;\n this.publicPath = publicPath || `${packageDirname}/public/`;\n\n this.config = _config(this, config);\n this.context.config = this.config;\n\n params(this);\n language(this);\n translate('locales')(this);\n\n this.use(compress());\n }\n\n existsConfigSync(name: string): ReturnType<Config['existsConfigSync']> {\n return this.config.existsConfigSync(name);\n }\n\n loadConfigSync(name: string): ReturnType<Config['loadConfigSync']> {\n return this.config.loadConfigSync(name);\n }\n\n override createContext<StateT = DefaultState>(\n req: IncomingMessage,\n res: ServerResponse,\n ): ParameterizedContext<StateT> {\n const ctx = super.createContext<StateT>(req, res);\n ctx.sanitizedState = {} as ContextSanitizedState;\n return ctx;\n }\n\n servePublic(): void {\n this.use(serve(this.publicPath)); // static files\n }\n\n catchErrors(): void {\n this.use(errors);\n }\n\n // eslint-disable-next-line @typescript-eslint/class-methods-use-this\n override listen(): never {\n throw new Error('Use start instead');\n }\n\n /**\n * Close server and emit close event\n */\n close(): void {\n if (this._server) {\n this._server.close();\n this.emit('close');\n }\n }\n\n async start(fn: () => Promise<void> | void): Promise<Server> {\n await fn();\n try {\n const server = await _listen(this.config, this.callback(), this.certPath);\n this._server = server;\n logger.success('started');\n if (process.send) process.send('ready');\n return server;\n } catch (error: unknown) {\n logger.error('start fail', { err: error });\n throw error;\n }\n }\n}\n\nexport type { Context } from 'koa';\n","import { existsSync, readFileSync } from 'node:fs';\nimport path from 'node:path';\nimport { Config } from 'alp-node-config';\nimport { Logger } from 'nightingale-logger';\nimport type { AlpNodeAppOptions } from './AlpNodeApp';\nimport { AlpNodeApp } from './AlpNodeApp';\n\nexport type { Context } from './AlpNodeApp';\nexport { Config } from 'alp-node-config';\n\nconst logger = new Logger('alp');\n\nexport const appDirname = path.resolve('build');\n\nconst packagePath = path.resolve('package.json');\nif (!packagePath) {\n throw new Error(`Could not find package.json: \"${String(packagePath)}\"`);\n}\nexport const packageDirname = path.dirname(packagePath);\n\nlogger.debug('init', { appDirname, packageDirname });\n\nexport const packageConfig: Record<string, unknown> = JSON.parse(\n readFileSync(packagePath, 'utf8'),\n) as Record<string, unknown>;\n\nconst buildedConfigPath = `${appDirname}/build/config/`;\nconst configPath = existsSync(buildedConfigPath)\n ? buildedConfigPath\n : `${appDirname}/config/`;\n\nexport const config = new Config(configPath).loadSync({ packageConfig });\n\nexport type AppOptions = Omit<\n AlpNodeAppOptions,\n 'appDirname' | 'config' | 'packageDirname'\n>;\n\nexport default class App extends AlpNodeApp {\n constructor(options?: AppOptions) {\n super({\n ...options,\n appDirname,\n packageDirname,\n config,\n });\n }\n}\n"],"names":["logger","Logger","AlpNodeApp","Koa","constructor","appDirname","packageDirname","config","certPath","publicPath","dirname","path","normalize","Object","defineProperty","get","deprecate","configurable","enumerable","_config","context","params","language","translate","use","compress","existsConfigSync","name","loadConfigSync","createContext","req","res","ctx","sanitizedState","servePublic","serve","catchErrors","errors","listen","Error","close","_server","emit","start","fn","server","_listen","callback","success","process","send","error","err","resolve","packagePath","String","debug","packageConfig","JSON","parse","readFileSync","buildedConfigPath","configPath","existsSync","Config","loadSync","App","options"],"mappings":";;;;;;;;;;;;;;;AAwBA,MAAMA,QAAM,GAAG,IAAIC,MAAM,CAAC,KAAK,CAAC,CAAA;AAqBzB,MAAMC,UAAU,SAASC,GAAG,CAA0C;AAa3E;AACF;AACA;AACA;AACA;AACEC,EAAAA,WAAWA,CAAC;IACVC,UAAU;IACVC,cAAc;IACdC,MAAM;IACNC,QAAQ;AACRC,IAAAA,UAAAA;AACiB,GAAC,EAAE;AACpB,IAAA,KAAK,EAAE,CAAA;IAEP,IAAI,CAACC,OAAO,GAAGC,IAAI,CAACC,SAAS,CAACP,UAAU,CAAC,CAAA;AAEzCQ,IAAAA,MAAM,CAACC,cAAc,CAAC,IAAI,EAAE,gBAAgB,EAAE;AAC5CC,MAAAA,GAAG,EAAEC,SAAS,CAAC,MAAMV,cAAc,EAAE,gBAAgB,CAAC;AACtDW,MAAAA,YAAY,EAAE,KAAK;AACnBC,MAAAA,UAAU,EAAE,KAAA;AACd,KAAC,CAAC,CAAA;AAEF,IAAA,IAAI,CAACV,QAAQ,GAAGA,QAAQ,IAAK,CAAA,EAAEF,cAAe,CAAa,YAAA,CAAA,CAAA;AAC3D,IAAA,IAAI,CAACG,UAAU,GAAGA,UAAU,IAAK,CAAA,EAAEH,cAAe,CAAS,QAAA,CAAA,CAAA;IAE3D,IAAI,CAACC,MAAM,GAAGY,OAAO,CAAC,IAAI,EAAEZ,MAAM,CAAC,CAAA;AACnC,IAAA,IAAI,CAACa,OAAO,CAACb,MAAM,GAAG,IAAI,CAACA,MAAM,CAAA;IAEjCc,MAAM,CAAC,IAAI,CAAC,CAAA;IACZC,QAAQ,CAAC,IAAI,CAAC,CAAA;AACdC,IAAAA,SAAS,CAAC,SAAS,CAAC,CAAC,IAAI,CAAC,CAAA;AAE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AiB,CAAC,GAC5CA,iBAAiB,GAChB,CAAExD,EAAAA,UAAW,CAAS,QAAA,CAAA,CAAA;AAEpB,MAAME,MAAM,GAAG,IAAIyD,MAAM,CAACF,UAAU,CAAC,CAACG,QAAQ,CAAC;AAAER,EAAAA,aAAAA;AAAc,CAAC,EAAC;AAOzD,MAAMS,GAAG,SAAShE,UAAU,CAAC;EAC1CE,WAAWA,CAAC+D,OAAoB,EAAE;AAChC,IAAA,KAAK,CAAC;AACJ,MAAA,GAAGA,OAAO;MACV9D,UAAU;MACVC,cAAc;AACdC,MAAAA,MAAAA;AACF,KAAC,CAAC,CAAA;AACJ,GAAA;AACF;;;;"}
|
|
1
|
+
{"version":3,"file":"index-node18.mjs","sources":["../src/config.ts","../src/errors.ts","../src/language.ts","../src/listen.ts","../src/params/ParamValidationResult.ts","../src/params/ParamValid.ts","../src/params/ParamValueValidator.ts","../src/params/ParamValueStringValidator.ts","../src/params/ParamValueFromContext.ts","../src/params/index.ts","../src/translate/load.ts","../src/translate/index.ts","../src/AlpNodeApp.ts","../src/router.ts","../src/index.ts"],"sourcesContent":["import { existsSync, readFileSync } from 'node:fs';\nimport deepFreeze from 'deep-freeze-es6';\nimport minimist from 'minimist';\nimport parseJSON from 'parse-json-object-as-map';\nimport type { NodeApplication, NodeConfig, PackageConfig } from './types';\n\nconst argv = minimist(process.argv.slice(2));\n\nfunction _existsConfigSync(dirname: string, name: string): boolean {\n return existsSync(`${dirname}${name}.json`);\n}\n\nfunction _loadConfigSync(dirname: string, name: string): Map<string, unknown> {\n const content = readFileSync(`${dirname}${name}.json`, 'utf8');\n return parseJSON(content) as Map<string, unknown>;\n}\n\nexport interface ConfigOptions {\n argv?: string[];\n packageConfig?: PackageConfig;\n version?: string;\n}\n\nexport class Config {\n packageConfig?: PackageConfig;\n\n private _map: Map<string, unknown>;\n\n private readonly _dirname: string;\n\n constructor(dirname: string, options?: ConfigOptions) {\n this._map = new Map<string, unknown>();\n this._dirname = dirname.replace(/\\/*$/, '/');\n if (options) {\n this.loadSync(options);\n }\n }\n\n loadSync(options: ConfigOptions = {}): Config & NodeConfig {\n const env = process.env.CONFIG_ENV || process.env.NODE_ENV || 'development';\n const { argv: argvOverrides = [], packageConfig, version } = options;\n this.packageConfig = packageConfig;\n\n const config = this.loadConfigSync('common') as Map<string, unknown>;\n for (const [key, value] of this.loadConfigSync(env)) {\n config.set(key, value);\n }\n\n if (this.existsConfigSync('local')) {\n for (const [key, value] of this.loadConfigSync('local')) {\n config.set(key, value);\n }\n }\n\n if (config.has('version')) {\n throw new Error('Cannot have \"version\", in config.');\n }\n\n config.set(\n 'version',\n String(version || argv.version || packageConfig?.version),\n );\n\n const socketPath: string | undefined = (argv.socket ||\n argv['socket-path'] ||\n argv.socketPath) as string | undefined;\n if (socketPath) {\n config.set('socketPath', socketPath);\n } else if (argv.port) {\n config.set('port', argv.port);\n config.delete('socketPath');\n } else if (process.env.PORT) {\n config.set('port', Number(process.env.PORT));\n config.delete('socketPath');\n }\n\n argvOverrides.forEach((key) => {\n const splitted = key.split('.');\n const value =\n splitted.length > 0 &&\n // eslint-disable-next-line @typescript-eslint/no-unsafe-return,unicorn/no-array-reduce, @typescript-eslint/no-shadow\n splitted.reduce((config, partialKey) => config?.[partialKey], argv);\n if (value !== undefined) {\n const last = splitted.pop()!;\n const map =\n splitted.length === 0\n ? config\n : // eslint-disable-next-line unicorn/no-array-reduce\n splitted.reduce(\n // eslint-disable-next-line @typescript-eslint/no-shadow\n (config, partialKey) =>\n config.get(partialKey) as Map<string, unknown>,\n config,\n );\n map.set(last, value);\n }\n });\n\n this._map = deepFreeze(config);\n return this as Config & NodeConfig;\n }\n\n get<T>(key: string): T {\n return this._map.get(key) as T;\n }\n\n existsConfigSync(name: string): boolean {\n return _existsConfigSync(this._dirname, name);\n }\n\n loadConfigSync(name: string): ReadonlyMap<string, unknown> {\n return _loadConfigSync(this._dirname, name);\n }\n}\n\nexport default function getConfig(\n app: NodeApplication,\n config: Config & NodeConfig,\n): Config & NodeConfig {\n return config;\n}\n","/* eslint-disable complexity */\nimport { STATUS_CODES } from 'node:http';\nimport ErrorHtmlRenderer from 'error-html';\nimport { Logger } from 'nightingale-logger';\nimport type { Context } from './AlpNodeApp';\nimport type { HtmlError } from './types';\n\nconst logger = new Logger('alp:errors');\nconst errorHtmlRenderer = new ErrorHtmlRenderer({\n appPath: `${process.cwd()}/`,\n});\n\nexport default async function alpNodeErrors(\n ctx: Context,\n next: () => Promise<void> | void,\n): Promise<void> {\n try {\n await next();\n } catch (error: unknown) {\n // eslint-disable-next-line no-ex-assign\n if (!error) error = new Error('Unknown error');\n // eslint-disable-next-line no-ex-assign\n if (typeof error === 'string') error = new Error(error);\n\n ctx.status = (error as HtmlError).status || 500;\n // eslint-disable-next-line @typescript-eslint/no-unsafe-argument\n logger.error(error as any);\n\n switch (ctx.request.accepts('html', 'text', 'json')) {\n case 'json':\n ctx.type = 'application/json';\n if (\n process.env.NODE_ENV !== 'production' ||\n (error as HtmlError).expose\n ) {\n ctx.body = { error: (error as Error).message };\n } else {\n ctx.body = { error: STATUS_CODES[ctx.status] };\n }\n\n break;\n\n case 'html':\n ctx.type = 'text/html';\n if (process.env.NODE_ENV !== 'production') {\n ctx.body = errorHtmlRenderer.render(error as Error);\n } else if ((error as HtmlError).expose) {\n ctx.body = (error as Error).message;\n } else {\n throw error;\n }\n\n break;\n\n case 'text':\n default:\n ctx.type = 'text/plain';\n if (\n process.env.NODE_ENV !== 'production' ||\n (error as HtmlError).expose\n ) {\n ctx.body = (error as Error).message;\n } else {\n throw error;\n }\n\n break;\n }\n }\n}\n","import type { Context } from 'koa';\nimport { defineLazyProperty } from 'object-properties';\nimport type { AlpNodeApp } from './AlpNodeApp';\n\nexport interface AlpLanguageContext {\n readonly firstAcceptedLanguage: string;\n readonly language: string;\n}\nexport default function alpLanguage(app: AlpNodeApp): void {\n const config = app.context.config;\n const availableLanguages: string[] = config.get('availableLanguages');\n if (!availableLanguages) {\n throw new Error('Missing config \"availableLanguages\"');\n }\n\n defineLazyProperty(app.context, 'language', function (this: Context): string {\n return this.acceptsLanguages(availableLanguages) || availableLanguages[0];\n });\n\n defineLazyProperty(\n app.context,\n 'firstAcceptedLanguage',\n function (this: Context): string {\n return this.acceptsLanguages()[0] || availableLanguages[0];\n },\n );\n}\n","import { chmodSync, unlinkSync, readFileSync } from 'node:fs';\nimport { createServer as createServerHttp } from 'node:http';\nimport type { Server, IncomingMessage, ServerResponse } from 'node:http';\nimport { createServer as createServerHttps } from 'node:https';\nimport { Logger } from 'nightingale-logger';\nimport type { Config } from './config';\n\nconst logger = new Logger('alp:listen');\n\ntype RequestListener = (req: IncomingMessage, res: ServerResponse) => void;\n\nconst createServer = (\n callback: RequestListener,\n socketPath?: string,\n tls?: boolean,\n dirname = '',\n // eslint-disable-next-line @typescript-eslint/max-params\n): Server => {\n const createHttpServer =\n !socketPath && tls ? createServerHttps : createServerHttp;\n\n if (!tls) {\n return (createHttpServer as typeof createServerHttps)(callback);\n }\n\n const options = {\n key: readFileSync(`${dirname}/server.key`),\n cert: readFileSync(`${dirname}/server.crt`),\n };\n\n return (createHttpServer as typeof createServerHttps)(options, callback);\n};\n\nexport default function alpListen(\n config: Config,\n callback: RequestListener,\n dirname?: string,\n): Promise<Server> {\n return new Promise((resolve) => {\n const socketPath = config.get<string>('socketPath');\n const port = config.get<number>('port');\n const hostname = config.get<string>('hostname');\n const tls = config.get<boolean>('tls');\n\n logger.info('Creating server', socketPath ? { socketPath } : { port });\n const server = createServer(callback, socketPath, tls, dirname);\n\n if (socketPath) {\n try {\n unlinkSync(socketPath);\n } catch {}\n\n server.listen(socketPath, () => {\n if (socketPath) {\n chmodSync(socketPath, '777');\n }\n\n logger.info('Server listening', { socketPath });\n resolve(server);\n });\n } else {\n server.listen(port, hostname, () => {\n logger.info('Server listening', { port });\n resolve(server);\n });\n }\n });\n}\n","export type Errors = Record<string, any>;\n\nexport class ParamValidationResult {\n _errors?: Errors;\n\n _error(name: string, key: string, value: unknown): void {\n if (!this._errors) {\n this._errors = {};\n }\n\n this._errors[name] = { error: key, value };\n }\n\n getErrors(): Errors | undefined {\n return this._errors;\n }\n\n hasErrors(): boolean {\n return this._errors !== undefined;\n }\n\n isValid(): boolean {\n return this._errors === undefined;\n }\n\n // string(name: string): ParamValueStringValidator {\n // return new ParamValueStringValidator(this, name, this.context.param(name));\n // }\n /* int(name, position) {\n return new ParamValueIntValidator(this, name, this.context.param(name, position));\n }\n model(modelName, name) {\n name = name || S.string.lcFirst(modelName);\n console.log('paramvalidator model', modelName, M[modelName]);\n let data = this.context.getOrPostParam(name);\n return new ParamValueModelValidator(this, name, !data ? null : new M[modelName](data));\n } */\n}\n","import type { Context } from '../AlpNodeApp';\nimport { ParamValidationResult } from './ParamValidationResult';\n\nexport default class ParamValid extends ParamValidationResult {\n context: Context;\n\n constructor(context: Context) {\n super();\n this.context = context;\n }\n\n override _error(): void {\n this.context.throw(400, 'Invalid params', { validator: this });\n }\n}\n","import type { ParamValidationResult } from './ParamValidationResult';\n\nexport default class ParamValueValidator<T> {\n readonly validationResult: ParamValidationResult;\n\n readonly name: string;\n\n readonly value: T;\n\n constructor(validationResult: ParamValidationResult, name: string, value: T) {\n this.validationResult = validationResult;\n this.name = name;\n this.value = value;\n }\n\n isValid(): boolean {\n return this.validationResult.isValid();\n }\n\n _error(key: string): void {\n this.validationResult._error(this.name, key, this.value);\n }\n}\n","import ParamValueValidator from './ParamValueValidator';\n\nexport default class ParamValueStringValidator<\n T extends string = string,\n> extends ParamValueValidator<T | null | undefined> {\n notEmpty(): ParamValueValidator<T> {\n if (this.value == null || this.value.trim() === '') {\n this._error('notEmpty');\n }\n\n return this as ParamValueValidator<T>;\n }\n}\n","import type { Context } from '../AlpNodeApp';\nimport type { ParamValidationResult } from './ParamValidationResult';\nimport ParamValueStringValidator from './ParamValueStringValidator';\n\nexport class ParamValueFromContext {\n readonly validationResult: ParamValidationResult;\n\n readonly context: Context;\n\n constructor(context: Context, validationResult: ParamValidationResult) {\n this.validationResult = validationResult;\n this.context = context;\n }\n\n namedParam(name: string): ParamValueStringValidator {\n return new ParamValueStringValidator(\n this.validationResult,\n name,\n this.context.namedParam(name),\n );\n }\n\n otherParam(position: number): ParamValueStringValidator {\n return new ParamValueStringValidator(\n this.validationResult,\n String(position),\n this.context.otherParam(position),\n );\n }\n\n queryParam(name: string): ParamValueStringValidator {\n return new ParamValueStringValidator(\n this.validationResult,\n name,\n this.context.queryParam(name),\n );\n }\n\n // bodyParam: <T>(name: string): ParamValueValidator<string | undefined> {\n\n // }\n}\n","import { defineLazyProperty } from 'object-properties';\nimport type { AlpNodeApp, Context } from '../AlpNodeApp';\nimport ParamValid from './ParamValid';\nimport { ParamValidationResult } from './ParamValidationResult';\nimport { ParamValueFromContext } from './ParamValueFromContext';\n\nexport interface AlpParamsContext {\n params: ParamValueFromContext;\n validParams: ParamValueFromContext;\n namedParam: (name: string) => string | undefined;\n otherParam: (position: number) => string | undefined;\n queryParam: (name: string) => string | undefined;\n bodyParam: <T>(name: string) => T | undefined;\n}\n\nexport interface AlpParamsRequest {\n searchParams: URLSearchParams;\n}\n\nexport default function alpParams(app: AlpNodeApp): void {\n Object.assign(app.context, {\n namedRouteParam(this: Context, name: string): string | undefined {\n const namedParams = this.route.namedParams;\n return namedParams?.get(name);\n },\n\n otherRouteParam(this: Context, position: number): string | undefined {\n const otherParams = this.route.otherParams;\n return otherParams?.[position - 1];\n },\n\n queryParam(this: Context, name: string): string | undefined {\n const searchParams = this.request.searchParams;\n return searchParams.get(name) ?? undefined;\n },\n\n bodyParam<T>(this: Context, name: string): T | undefined {\n // eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-member-access\n return (this.body as any)[name];\n },\n });\n\n defineLazyProperty(\n app.request,\n 'searchParams',\n function (this: Context['request']): URLSearchParams {\n return new URLSearchParams(this.search);\n },\n );\n\n defineLazyProperty(\n app.context,\n 'params',\n function (this: Context): ParamValueFromContext {\n return new ParamValueFromContext(this, new ParamValidationResult());\n },\n );\n\n defineLazyProperty(\n app.context,\n 'validParams',\n function (this: Context): ParamValueFromContext {\n return new ParamValueFromContext(this, new ParamValid(this));\n },\n );\n}\n","import IntlMessageFormatDefault from 'intl-messageformat';\n\n// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment\nconst IntlMessageFormat: typeof IntlMessageFormatDefault =\n // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access\n (IntlMessageFormatDefault as any).default || IntlMessageFormatDefault;\n\nexport type Translations = ReadonlyMap<string, IntlMessageFormatDefault>;\n\nexport default function load(\n translations: ReadonlyMap<string, unknown>,\n language: string,\n): Translations {\n const result = new Map();\n\n (function loadMap(map, prefix) {\n map.forEach((value: any, key) => {\n if (typeof value === 'object') {\n // eslint-disable-next-line @typescript-eslint/no-unsafe-argument\n loadMap(value, `${prefix}${key}.`);\n return;\n }\n\n // eslint-disable-next-line @typescript-eslint/no-unsafe-argument\n result.set(`${prefix}${key}`, new IntlMessageFormat(value, language));\n });\n })(translations, '');\n\n return result;\n}\n","import { Logger } from 'nightingale-logger';\nimport type { AlpNodeApp, Context } from '../AlpNodeApp';\nimport type { Translations } from './load';\nimport load from './load';\n\nconst logger = new Logger('alp:translate');\n\ntype Args = Record<string, any>;\n\nexport interface TranslateBaseContext {\n t: (id: string, args: Args) => string;\n}\nexport interface TranslateContext {\n readonly language: string;\n}\n\nexport default function alpTranslate(\n dirname: string,\n): (app: AlpNodeApp) => void {\n dirname = dirname.replace(/\\/*$/, '/');\n return (app: AlpNodeApp) => {\n const appTranslations = new Map<string, Translations>();\n\n Object.assign(app.context, {\n t(this: Context, id: string, args: Args): string {\n const msg = appTranslations.get(this.language)!.get(id);\n if (!msg) {\n logger.warn('invalid msg', { language: this.language, id });\n return id;\n }\n\n return msg.format(args) as string;\n },\n });\n\n const config = app.config;\n\n config.get<string[]>('availableLanguages').forEach((language) => {\n const translations = app.loadConfigSync(dirname + language);\n appTranslations.set(language, load(translations, language));\n });\n\n return appTranslations;\n };\n}\n","import type { IncomingMessage, Server, ServerResponse } from 'node:http';\nimport path from 'node:path';\nimport { deprecate } from 'node:util';\nimport Koa from 'koa';\nimport type { ParameterizedContext, DefaultState } from 'koa';\nimport compress from 'koa-compress';\nimport serve from 'koa-static';\nimport { Logger } from 'nightingale-logger';\nimport type { Router } from 'router-segments';\nimport type { Config } from './config';\nimport _config from './config';\nimport errors from './errors';\nimport type { AlpLanguageContext } from './language';\nimport language from './language';\nimport _listen from './listen';\nimport type { AlpParamsContext, AlpParamsRequest } from './params';\nimport params from './params';\nimport type {\n AlpRouteRef,\n RouterContext as AlpRouterContext,\n UrlGenerator,\n} from './router';\nimport type { TranslateBaseContext, TranslateContext } from './translate';\nimport translate from './translate';\nimport type {\n NodeApplication,\n NodeConfig,\n Context as AlpContext,\n ContextState,\n ContextSanitizedState,\n} from './types';\n\nconst logger = new Logger('alp');\n\nexport interface AlpNodeAppOptions {\n appDirname: string;\n packageDirname: string;\n config: Config & NodeConfig;\n certPath?: string;\n publicPath?: string;\n}\n\ndeclare module 'koa' {\n // eslint-disable-next-line @typescript-eslint/no-empty-interface, @typescript-eslint/no-shadow\n interface DefaultState extends ContextState {}\n\n interface DefaultContext\n extends AlpContext,\n AlpParamsContext,\n AlpRouterContext,\n AlpLanguageContext,\n TranslateContext {}\n\n interface BaseContext extends AlpContext, TranslateBaseContext {\n urlGenerator: UrlGenerator;\n redirectTo: <P extends Record<string, unknown>>(\n to: string,\n params?: P,\n ) => void;\n }\n // eslint-disable-next-line @typescript-eslint/no-empty-interface\n interface BaseRequest extends AlpParamsRequest {}\n}\n\nexport class AlpNodeApp extends Koa<ContextState> implements NodeApplication {\n dirname: string;\n\n certPath: string;\n\n publicPath: string;\n\n config: Config & NodeConfig;\n\n _server?: Server;\n\n router?: Router<any, AlpRouteRef>;\n\n /**\n * @param {Object} [options]\n * @param {string} [options.certPath] directory of the ssl certificates\n * @param {string} [options.publicPath] directory of public files\n */\n constructor({\n appDirname,\n packageDirname,\n config,\n certPath,\n publicPath,\n }: AlpNodeAppOptions) {\n super();\n\n this.dirname = path.normalize(appDirname);\n\n Object.defineProperty(this, 'packageDirname', {\n get: deprecate(() => packageDirname, 'packageDirname'),\n configurable: false,\n enumerable: false,\n });\n\n this.certPath = certPath || `${packageDirname}/config/cert`;\n this.publicPath = publicPath || `${packageDirname}/public/`;\n\n this.config = _config(this, config);\n this.context.config = this.config;\n\n params(this);\n language(this);\n translate('locales')(this);\n\n this.use(compress());\n }\n\n existsConfigSync(name: string): ReturnType<Config['existsConfigSync']> {\n return this.config.existsConfigSync(name);\n }\n\n loadConfigSync(name: string): ReturnType<Config['loadConfigSync']> {\n return this.config.loadConfigSync(name);\n }\n\n override createContext<StateT = DefaultState>(\n req: IncomingMessage,\n res: ServerResponse,\n ): ParameterizedContext<StateT> {\n const ctx = super.createContext<StateT>(req, res);\n ctx.sanitizedState = {} as ContextSanitizedState;\n return ctx;\n }\n\n servePublic(): void {\n this.use(serve(this.publicPath)); // static files\n }\n\n catchErrors(): void {\n this.use(errors);\n }\n\n // eslint-disable-next-line @typescript-eslint/class-methods-use-this\n override listen(): never {\n throw new Error('Use start instead');\n }\n\n /**\n * Close server and emit close event\n */\n close(): void {\n if (this._server) {\n this._server.close();\n this.emit('close');\n }\n }\n\n async start(fn: () => Promise<void> | void): Promise<Server> {\n await fn();\n try {\n const server = await _listen(this.config, this.callback(), this.certPath);\n this._server = server;\n logger.success('started');\n if (process.send) process.send('ready');\n return server;\n } catch (error: unknown) {\n logger.error('start fail', { err: error });\n throw error;\n }\n }\n}\n\nexport type { Context } from 'koa';\n\nexport { type NodeApplication } from './types';\n","import type {\n Router,\n LocaleType,\n RouterBuilder,\n RouteMatch,\n} from 'router-segments';\nimport { createRouterBuilder } from 'router-segments';\nimport type { AlpNodeApp, Context } from './AlpNodeApp';\n\nexport type AlpRouter<Locales extends LocaleType> = Router<\n Locales,\n AlpRouteRef\n>;\nexport type AlpRouteRef = (ctx: Context) => Promise<void> | void;\ntype ReturnType = (app: AlpNodeApp) => AlpRouteRef;\n\nexport interface RouterContext {\n route: RouteMatch<any, AlpRouteRef>;\n}\nexport const createAlpRouterBuilder = <\n Locales extends LocaleType,\n>(): RouterBuilder<Locales, AlpRouteRef> =>\n createRouterBuilder<Locales, AlpRouteRef>();\n\nexport type UrlGenerator = <P extends Record<string, unknown> | undefined>(\n routeKey: string,\n params?: P,\n) => string;\n\nexport default function alpRouter<Locales extends string>(\n router: Router<Locales, AlpRouteRef>,\n): ReturnType {\n return (app: AlpNodeApp) => {\n app.router = router;\n\n app.context.urlGenerator = function <\n P extends Record<string, unknown> | undefined,\n >(this: Context, routeKey: string, params?: P): string {\n return router.toLocalizedPath(this.language as Locales, routeKey, params);\n };\n\n app.context.redirectTo = function <\n P extends Record<string, unknown> | undefined,\n >(this: Context, to: string, params?: P): void {\n this.redirect(\n router.toLocalizedPath(this.language as Locales, to, params),\n );\n };\n\n return async (ctx: Context): Promise<void> => {\n // eslint-disable-next-line unicorn/no-array-method-this-argument\n const routeMatch = router.find(ctx.request.path, ctx.language as Locales);\n\n if (!routeMatch) {\n ctx.status = 404;\n throw new Error(`Route not found: ${ctx.request.path}`);\n }\n\n ctx.route = routeMatch;\n\n await routeMatch.ref(ctx);\n };\n };\n}\n","import { existsSync, readFileSync } from 'node:fs';\nimport path from 'node:path';\nimport { Logger } from 'nightingale-logger';\nimport type { AlpNodeAppOptions } from './AlpNodeApp';\nimport { AlpNodeApp } from './AlpNodeApp';\nimport { Config } from './config';\n\nexport type {\n BaseContext,\n NodeApplication,\n NodeConfig,\n ContextState,\n ContextSanitizedState,\n} from './types';\nexport type { Context } from './AlpNodeApp';\n\nconst logger = new Logger('alp');\n\nexport const appDirname = path.resolve('build');\n\nconst packagePath = path.resolve('package.json');\nif (!packagePath) {\n throw new Error(`Could not find package.json: \"${String(packagePath)}\"`);\n}\nexport const packageDirname = path.dirname(packagePath);\n\nlogger.debug('init', { appDirname, packageDirname });\n\nexport const packageConfig: Record<string, unknown> = JSON.parse(\n readFileSync(packagePath, 'utf8'),\n) as Record<string, unknown>;\n\nconst buildedConfigPath = `${appDirname}/build/config/`;\nconst configPath = existsSync(buildedConfigPath)\n ? buildedConfigPath\n : `${appDirname}/config/`;\n\nexport const config = new Config(configPath).loadSync({ packageConfig });\n\nexport type AppOptions = Omit<\n AlpNodeAppOptions,\n 'appDirname' | 'config' | 'packageDirname'\n>;\n\nexport default class App extends AlpNodeApp {\n constructor(options?: AppOptions) {\n super({\n ...options,\n appDirname,\n packageDirname,\n config,\n });\n }\n}\n\nexport { Config } from './config';\n\nexport {\n default as router,\n createAlpRouterBuilder,\n type AlpRouteRef,\n type AlpRouter,\n} from './router';\n"],"names":["argv","minimist","process","slice","_existsConfigSync","dirname","name","existsSync","_loadConfigSync","content","readFileSync","parseJSON","Config","constructor","options","_map","Map","_dirname","replace","loadSync","env","CONFIG_ENV","NODE_ENV","argvOverrides","packageConfig","version","config","loadConfigSync","key","value","set","existsConfigSync","has","Error","String","socketPath","socket","port","delete","PORT","Number","forEach","splitted","split","length","reduce","partialKey","undefined","last","pop","map","get","deepFreeze","getConfig","app","logger","Logger","errorHtmlRenderer","ErrorHtmlRenderer","appPath","cwd","alpNodeErrors","ctx","next","error","status","request","accepts","type","expose","body","message","STATUS_CODES","render","alpLanguage","context","availableLanguages","defineLazyProperty","acceptsLanguages","createServer","callback","tls","createHttpServer","createServerHttps","createServerHttp","cert","alpListen","Promise","resolve","hostname","info","server","unlinkSync","listen","chmodSync","ParamValidationResult","_error","_errors","getErrors","hasErrors","isValid","ParamValid","throw","validator","ParamValueValidator","validationResult","ParamValueStringValidator","notEmpty","trim","ParamValueFromContext","namedParam","otherParam","position","queryParam","alpParams","Object","assign","namedRouteParam","namedParams","route","otherRouteParam","otherParams","searchParams","bodyParam","URLSearchParams","search","IntlMessageFormat","IntlMessageFormatDefault","default","load","translations","language","result","loadMap","prefix","alpTranslate","appTranslations","t","id","args","msg","warn","format","AlpNodeApp","Koa","appDirname","packageDirname","certPath","publicPath","path","normalize","defineProperty","deprecate","configurable","enumerable","_config","params","translate","use","compress","createContext","req","res","sanitizedState","servePublic","serve","catchErrors","errors","close","_server","emit","start","fn","_listen","success","send","err","createAlpRouterBuilder","createRouterBuilder","alpRouter","router","urlGenerator","routeKey","toLocalizedPath","redirectTo","to","redirect","routeMatch","find","ref","packagePath","debug","JSON","parse","buildedConfigPath","configPath","App"],"mappings":";;;;;;;;;;;;;;;;;AAMA,MAAMA,IAAI,GAAGC,QAAQ,CAACC,OAAO,CAACF,IAAI,CAACG,KAAK,CAAC,CAAC,CAAC,CAAC,CAAA;AAE5C,SAASC,iBAAiBA,CAACC,OAAe,EAAEC,IAAY,EAAW;AACjE,EAAA,OAAOC,UAAU,CAAE,CAAA,EAAEF,OAAQ,CAAEC,EAAAA,IAAK,OAAM,CAAC,CAAA;AAC7C,CAAA;AAEA,SAASE,eAAeA,CAACH,OAAe,EAAEC,IAAY,EAAwB;EAC5E,MAAMG,OAAO,GAAGC,YAAY,CAAE,CAAA,EAAEL,OAAQ,CAAA,EAAEC,IAAK,CAAA,KAAA,CAAM,EAAE,MAAM,CAAC,CAAA;EAC9D,OAAOK,SAAS,CAACF,OAAO,CAAC,CAAA;AAC3B,CAAA;AAQO,MAAMG,MAAM,CAAC;AAOlBC,EAAAA,WAAWA,CAACR,OAAe,EAAES,OAAuB,EAAE;AACpD,IAAA,IAAI,CAACC,IAAI,GAAG,IAAIC,GAAG,EAAmB,CAAA;IACtC,IAAI,CAACC,QAAQ,GAAGZ,OAAO,CAACa,OAAO,CAAC,MAAM,EAAE,GAAG,CAAC,CAAA;AAC5C,IAAA,IAAIJ,OAAO,EAAE;AACX,MAAA,IAAI,CAACK,QAAQ,CAACL,OAAO,CAAC,CAAA;AACxB,KAAA;AACF,GAAA;AAEAK,EAAAA,QAAQA,CAACL,OAAsB,GAAG,EAAE,EAAuB;AACzD,IAAA,MAAMM,GAAG,GAAGlB,OAAO,CAACkB,GAAG,CAACC,UAAU,IAAInB,OAAO,CAACkB,GAAG,CAACE,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AAiB,CAAC,GAC5CA,iBAAiB,GAChB,CAAEjD,EAAAA,UAAW,CAAS,QAAA,CAAA,CAAA;AAEpB,MAAMvH,MAAM,GAAG,IAAId,MAAM,CAACuL,UAAU,CAAC,CAAChL,QAAQ,CAAC;AAAEK,EAAAA,aAAAA;AAAc,CAAC,EAAC;AAOzD,MAAM4K,GAAG,SAASrD,UAAU,CAAC;EAC1ClI,WAAWA,CAACC,OAAoB,EAAE;AAChC,IAAA,KAAK,CAAC;AACJ,MAAA,GAAGA,OAAO;MACVmI,UAAU;MACVC,cAAc;AACdxH,MAAAA,MAAAA;AACF,KAAC,CAAC,CAAA;AACJ,GAAA;AACF;;;;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "alp-node",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "7.0.0",
|
|
4
4
|
"description": "framework based on koa 2",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"springbokjs",
|
|
@@ -81,17 +81,16 @@
|
|
|
81
81
|
"dependencies": {
|
|
82
82
|
"@types/koa": "^2.13.1",
|
|
83
83
|
"@types/node": ">=18.0.0",
|
|
84
|
-
"
|
|
85
|
-
"
|
|
86
|
-
"
|
|
87
|
-
"alp-node-language": "7.1.1",
|
|
88
|
-
"alp-params": "6.2.0",
|
|
89
|
-
"alp-translate": "8.1.1",
|
|
90
|
-
"alp-types": "3.1.1",
|
|
84
|
+
"deep-freeze-es6": "^3.0.2",
|
|
85
|
+
"error-html": "^0.3.5",
|
|
86
|
+
"intl-messageformat": "^10.0.0",
|
|
91
87
|
"koa": "^2.13.1",
|
|
92
88
|
"koa-compress": "^5.0.0",
|
|
93
89
|
"koa-static": "^5.0.0",
|
|
94
|
-
"
|
|
90
|
+
"minimist": "^1.2.8",
|
|
91
|
+
"nightingale-logger": "^15.0.0",
|
|
92
|
+
"object-properties": "^8.1.0",
|
|
93
|
+
"parse-json-object-as-map": "^1.2.0"
|
|
95
94
|
},
|
|
96
95
|
"devDependencies": {
|
|
97
96
|
"@babel/core": "7.23.7",
|
package/src/AlpNodeApp.ts
CHANGED
|
@@ -1,26 +1,34 @@
|
|
|
1
1
|
import type { IncomingMessage, Server, ServerResponse } from 'node:http';
|
|
2
2
|
import path from 'node:path';
|
|
3
3
|
import { deprecate } from 'node:util';
|
|
4
|
-
import
|
|
5
|
-
import type {
|
|
6
|
-
import
|
|
7
|
-
import
|
|
8
|
-
import
|
|
9
|
-
import
|
|
10
|
-
import
|
|
4
|
+
import Koa from 'koa';
|
|
5
|
+
import type { ParameterizedContext, DefaultState } from 'koa';
|
|
6
|
+
import compress from 'koa-compress';
|
|
7
|
+
import serve from 'koa-static';
|
|
8
|
+
import { Logger } from 'nightingale-logger';
|
|
9
|
+
import type { Router } from 'router-segments';
|
|
10
|
+
import type { Config } from './config';
|
|
11
|
+
import _config from './config';
|
|
12
|
+
import errors from './errors';
|
|
13
|
+
import type { AlpLanguageContext } from './language';
|
|
14
|
+
import language from './language';
|
|
15
|
+
import _listen from './listen';
|
|
16
|
+
import type { AlpParamsContext, AlpParamsRequest } from './params';
|
|
17
|
+
import params from './params';
|
|
18
|
+
import type {
|
|
19
|
+
AlpRouteRef,
|
|
20
|
+
RouterContext as AlpRouterContext,
|
|
21
|
+
UrlGenerator,
|
|
22
|
+
} from './router';
|
|
23
|
+
import type { TranslateBaseContext, TranslateContext } from './translate';
|
|
24
|
+
import translate from './translate';
|
|
11
25
|
import type {
|
|
12
26
|
NodeApplication,
|
|
13
27
|
NodeConfig,
|
|
14
28
|
Context as AlpContext,
|
|
15
29
|
ContextState,
|
|
16
30
|
ContextSanitizedState,
|
|
17
|
-
|
|
18
|
-
} from 'alp-types';
|
|
19
|
-
import Koa from 'koa';
|
|
20
|
-
import type { ParameterizedContext, DefaultState, BaseRequest } from 'koa';
|
|
21
|
-
import compress from 'koa-compress';
|
|
22
|
-
import serve from 'koa-static';
|
|
23
|
-
import { Logger } from 'nightingale-logger';
|
|
31
|
+
} from './types';
|
|
24
32
|
|
|
25
33
|
const logger = new Logger('alp');
|
|
26
34
|
|
|
@@ -35,12 +43,23 @@ export interface AlpNodeAppOptions {
|
|
|
35
43
|
declare module 'koa' {
|
|
36
44
|
// eslint-disable-next-line @typescript-eslint/no-empty-interface, @typescript-eslint/no-shadow
|
|
37
45
|
interface DefaultState extends ContextState {}
|
|
46
|
+
|
|
47
|
+
interface DefaultContext
|
|
48
|
+
extends AlpContext,
|
|
49
|
+
AlpParamsContext,
|
|
50
|
+
AlpRouterContext,
|
|
51
|
+
AlpLanguageContext,
|
|
52
|
+
TranslateContext {}
|
|
53
|
+
|
|
54
|
+
interface BaseContext extends AlpContext, TranslateBaseContext {
|
|
55
|
+
urlGenerator: UrlGenerator;
|
|
56
|
+
redirectTo: <P extends Record<string, unknown>>(
|
|
57
|
+
to: string,
|
|
58
|
+
params?: P,
|
|
59
|
+
) => void;
|
|
60
|
+
}
|
|
38
61
|
// eslint-disable-next-line @typescript-eslint/no-empty-interface
|
|
39
|
-
interface
|
|
40
|
-
// eslint-disable-next-line @typescript-eslint/no-empty-interface
|
|
41
|
-
interface BaseContext extends AlpContext {}
|
|
42
|
-
// eslint-disable-next-line @typescript-eslint/no-empty-interface, @typescript-eslint/no-shadow
|
|
43
|
-
interface BaseRequest extends ContextRequest {}
|
|
62
|
+
interface BaseRequest extends AlpParamsRequest {}
|
|
44
63
|
}
|
|
45
64
|
|
|
46
65
|
export class AlpNodeApp extends Koa<ContextState> implements NodeApplication {
|
|
@@ -52,10 +71,10 @@ export class AlpNodeApp extends Koa<ContextState> implements NodeApplication {
|
|
|
52
71
|
|
|
53
72
|
config: Config & NodeConfig;
|
|
54
73
|
|
|
55
|
-
declare request: BaseRequest & ContextRequest;
|
|
56
|
-
|
|
57
74
|
_server?: Server;
|
|
58
75
|
|
|
76
|
+
router?: Router<any, AlpRouteRef>;
|
|
77
|
+
|
|
59
78
|
/**
|
|
60
79
|
* @param {Object} [options]
|
|
61
80
|
* @param {string} [options.certPath] directory of the ssl certificates
|
|
@@ -147,3 +166,5 @@ export class AlpNodeApp extends Koa<ContextState> implements NodeApplication {
|
|
|
147
166
|
}
|
|
148
167
|
|
|
149
168
|
export type { Context } from 'koa';
|
|
169
|
+
|
|
170
|
+
export { type NodeApplication } from './types';
|
package/src/config.ts
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
2
|
+
import deepFreeze from 'deep-freeze-es6';
|
|
3
|
+
import minimist from 'minimist';
|
|
4
|
+
import parseJSON from 'parse-json-object-as-map';
|
|
5
|
+
import type { NodeApplication, NodeConfig, PackageConfig } from './types';
|
|
6
|
+
|
|
7
|
+
const argv = minimist(process.argv.slice(2));
|
|
8
|
+
|
|
9
|
+
function _existsConfigSync(dirname: string, name: string): boolean {
|
|
10
|
+
return existsSync(`${dirname}${name}.json`);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function _loadConfigSync(dirname: string, name: string): Map<string, unknown> {
|
|
14
|
+
const content = readFileSync(`${dirname}${name}.json`, 'utf8');
|
|
15
|
+
return parseJSON(content) as Map<string, unknown>;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface ConfigOptions {
|
|
19
|
+
argv?: string[];
|
|
20
|
+
packageConfig?: PackageConfig;
|
|
21
|
+
version?: string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export class Config {
|
|
25
|
+
packageConfig?: PackageConfig;
|
|
26
|
+
|
|
27
|
+
private _map: Map<string, unknown>;
|
|
28
|
+
|
|
29
|
+
private readonly _dirname: string;
|
|
30
|
+
|
|
31
|
+
constructor(dirname: string, options?: ConfigOptions) {
|
|
32
|
+
this._map = new Map<string, unknown>();
|
|
33
|
+
this._dirname = dirname.replace(/\/*$/, '/');
|
|
34
|
+
if (options) {
|
|
35
|
+
this.loadSync(options);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
loadSync(options: ConfigOptions = {}): Config & NodeConfig {
|
|
40
|
+
const env = process.env.CONFIG_ENV || process.env.NODE_ENV || 'development';
|
|
41
|
+
const { argv: argvOverrides = [], packageConfig, version } = options;
|
|
42
|
+
this.packageConfig = packageConfig;
|
|
43
|
+
|
|
44
|
+
const config = this.loadConfigSync('common') as Map<string, unknown>;
|
|
45
|
+
for (const [key, value] of this.loadConfigSync(env)) {
|
|
46
|
+
config.set(key, value);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (this.existsConfigSync('local')) {
|
|
50
|
+
for (const [key, value] of this.loadConfigSync('local')) {
|
|
51
|
+
config.set(key, value);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (config.has('version')) {
|
|
56
|
+
throw new Error('Cannot have "version", in config.');
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
config.set(
|
|
60
|
+
'version',
|
|
61
|
+
String(version || argv.version || packageConfig?.version),
|
|
62
|
+
);
|
|
63
|
+
|
|
64
|
+
const socketPath: string | undefined = (argv.socket ||
|
|
65
|
+
argv['socket-path'] ||
|
|
66
|
+
argv.socketPath) as string | undefined;
|
|
67
|
+
if (socketPath) {
|
|
68
|
+
config.set('socketPath', socketPath);
|
|
69
|
+
} else if (argv.port) {
|
|
70
|
+
config.set('port', argv.port);
|
|
71
|
+
config.delete('socketPath');
|
|
72
|
+
} else if (process.env.PORT) {
|
|
73
|
+
config.set('port', Number(process.env.PORT));
|
|
74
|
+
config.delete('socketPath');
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
argvOverrides.forEach((key) => {
|
|
78
|
+
const splitted = key.split('.');
|
|
79
|
+
const value =
|
|
80
|
+
splitted.length > 0 &&
|
|
81
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return,unicorn/no-array-reduce, @typescript-eslint/no-shadow
|
|
82
|
+
splitted.reduce((config, partialKey) => config?.[partialKey], argv);
|
|
83
|
+
if (value !== undefined) {
|
|
84
|
+
const last = splitted.pop()!;
|
|
85
|
+
const map =
|
|
86
|
+
splitted.length === 0
|
|
87
|
+
? config
|
|
88
|
+
: // eslint-disable-next-line unicorn/no-array-reduce
|
|
89
|
+
splitted.reduce(
|
|
90
|
+
// eslint-disable-next-line @typescript-eslint/no-shadow
|
|
91
|
+
(config, partialKey) =>
|
|
92
|
+
config.get(partialKey) as Map<string, unknown>,
|
|
93
|
+
config,
|
|
94
|
+
);
|
|
95
|
+
map.set(last, value);
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
this._map = deepFreeze(config);
|
|
100
|
+
return this as Config & NodeConfig;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
get<T>(key: string): T {
|
|
104
|
+
return this._map.get(key) as T;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
existsConfigSync(name: string): boolean {
|
|
108
|
+
return _existsConfigSync(this._dirname, name);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
loadConfigSync(name: string): ReadonlyMap<string, unknown> {
|
|
112
|
+
return _loadConfigSync(this._dirname, name);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export default function getConfig(
|
|
117
|
+
app: NodeApplication,
|
|
118
|
+
config: Config & NodeConfig,
|
|
119
|
+
): Config & NodeConfig {
|
|
120
|
+
return config;
|
|
121
|
+
}
|
package/src/errors.ts
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/* eslint-disable complexity */
|
|
2
|
+
import { STATUS_CODES } from 'node:http';
|
|
3
|
+
import ErrorHtmlRenderer from 'error-html';
|
|
4
|
+
import { Logger } from 'nightingale-logger';
|
|
5
|
+
import type { Context } from './AlpNodeApp';
|
|
6
|
+
import type { HtmlError } from './types';
|
|
7
|
+
|
|
8
|
+
const logger = new Logger('alp:errors');
|
|
9
|
+
const errorHtmlRenderer = new ErrorHtmlRenderer({
|
|
10
|
+
appPath: `${process.cwd()}/`,
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
export default async function alpNodeErrors(
|
|
14
|
+
ctx: Context,
|
|
15
|
+
next: () => Promise<void> | void,
|
|
16
|
+
): Promise<void> {
|
|
17
|
+
try {
|
|
18
|
+
await next();
|
|
19
|
+
} catch (error: unknown) {
|
|
20
|
+
// eslint-disable-next-line no-ex-assign
|
|
21
|
+
if (!error) error = new Error('Unknown error');
|
|
22
|
+
// eslint-disable-next-line no-ex-assign
|
|
23
|
+
if (typeof error === 'string') error = new Error(error);
|
|
24
|
+
|
|
25
|
+
ctx.status = (error as HtmlError).status || 500;
|
|
26
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
|
27
|
+
logger.error(error as any);
|
|
28
|
+
|
|
29
|
+
switch (ctx.request.accepts('html', 'text', 'json')) {
|
|
30
|
+
case 'json':
|
|
31
|
+
ctx.type = 'application/json';
|
|
32
|
+
if (
|
|
33
|
+
process.env.NODE_ENV !== 'production' ||
|
|
34
|
+
(error as HtmlError).expose
|
|
35
|
+
) {
|
|
36
|
+
ctx.body = { error: (error as Error).message };
|
|
37
|
+
} else {
|
|
38
|
+
ctx.body = { error: STATUS_CODES[ctx.status] };
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
break;
|
|
42
|
+
|
|
43
|
+
case 'html':
|
|
44
|
+
ctx.type = 'text/html';
|
|
45
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
46
|
+
ctx.body = errorHtmlRenderer.render(error as Error);
|
|
47
|
+
} else if ((error as HtmlError).expose) {
|
|
48
|
+
ctx.body = (error as Error).message;
|
|
49
|
+
} else {
|
|
50
|
+
throw error;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
break;
|
|
54
|
+
|
|
55
|
+
case 'text':
|
|
56
|
+
default:
|
|
57
|
+
ctx.type = 'text/plain';
|
|
58
|
+
if (
|
|
59
|
+
process.env.NODE_ENV !== 'production' ||
|
|
60
|
+
(error as HtmlError).expose
|
|
61
|
+
) {
|
|
62
|
+
ctx.body = (error as Error).message;
|
|
63
|
+
} else {
|
|
64
|
+
throw error;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
break;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,12 +1,18 @@
|
|
|
1
1
|
import { existsSync, readFileSync } from 'node:fs';
|
|
2
2
|
import path from 'node:path';
|
|
3
|
-
import { Config } from 'alp-node-config';
|
|
4
3
|
import { Logger } from 'nightingale-logger';
|
|
5
4
|
import type { AlpNodeAppOptions } from './AlpNodeApp';
|
|
6
5
|
import { AlpNodeApp } from './AlpNodeApp';
|
|
7
|
-
|
|
6
|
+
import { Config } from './config';
|
|
7
|
+
|
|
8
|
+
export type {
|
|
9
|
+
BaseContext,
|
|
10
|
+
NodeApplication,
|
|
11
|
+
NodeConfig,
|
|
12
|
+
ContextState,
|
|
13
|
+
ContextSanitizedState,
|
|
14
|
+
} from './types';
|
|
8
15
|
export type { Context } from './AlpNodeApp';
|
|
9
|
-
export { Config } from 'alp-node-config';
|
|
10
16
|
|
|
11
17
|
const logger = new Logger('alp');
|
|
12
18
|
|
|
@@ -46,3 +52,12 @@ export default class App extends AlpNodeApp {
|
|
|
46
52
|
});
|
|
47
53
|
}
|
|
48
54
|
}
|
|
55
|
+
|
|
56
|
+
export { Config } from './config';
|
|
57
|
+
|
|
58
|
+
export {
|
|
59
|
+
default as router,
|
|
60
|
+
createAlpRouterBuilder,
|
|
61
|
+
type AlpRouteRef,
|
|
62
|
+
type AlpRouter,
|
|
63
|
+
} from './router';
|
package/src/language.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { Context } from 'koa';
|
|
2
|
+
import { defineLazyProperty } from 'object-properties';
|
|
3
|
+
import type { AlpNodeApp } from './AlpNodeApp';
|
|
4
|
+
|
|
5
|
+
export interface AlpLanguageContext {
|
|
6
|
+
readonly firstAcceptedLanguage: string;
|
|
7
|
+
readonly language: string;
|
|
8
|
+
}
|
|
9
|
+
export default function alpLanguage(app: AlpNodeApp): void {
|
|
10
|
+
const config = app.context.config;
|
|
11
|
+
const availableLanguages: string[] = config.get('availableLanguages');
|
|
12
|
+
if (!availableLanguages) {
|
|
13
|
+
throw new Error('Missing config "availableLanguages"');
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
defineLazyProperty(app.context, 'language', function (this: Context): string {
|
|
17
|
+
return this.acceptsLanguages(availableLanguages) || availableLanguages[0];
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
defineLazyProperty(
|
|
21
|
+
app.context,
|
|
22
|
+
'firstAcceptedLanguage',
|
|
23
|
+
function (this: Context): string {
|
|
24
|
+
return this.acceptsLanguages()[0] || availableLanguages[0];
|
|
25
|
+
},
|
|
26
|
+
);
|
|
27
|
+
}
|
package/src/listen.ts
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { chmodSync, unlinkSync, readFileSync } from 'node:fs';
|
|
2
|
+
import { createServer as createServerHttp } from 'node:http';
|
|
3
|
+
import type { Server, IncomingMessage, ServerResponse } from 'node:http';
|
|
4
|
+
import { createServer as createServerHttps } from 'node:https';
|
|
5
|
+
import { Logger } from 'nightingale-logger';
|
|
6
|
+
import type { Config } from './config';
|
|
7
|
+
|
|
8
|
+
const logger = new Logger('alp:listen');
|
|
9
|
+
|
|
10
|
+
type RequestListener = (req: IncomingMessage, res: ServerResponse) => void;
|
|
11
|
+
|
|
12
|
+
const createServer = (
|
|
13
|
+
callback: RequestListener,
|
|
14
|
+
socketPath?: string,
|
|
15
|
+
tls?: boolean,
|
|
16
|
+
dirname = '',
|
|
17
|
+
// eslint-disable-next-line @typescript-eslint/max-params
|
|
18
|
+
): Server => {
|
|
19
|
+
const createHttpServer =
|
|
20
|
+
!socketPath && tls ? createServerHttps : createServerHttp;
|
|
21
|
+
|
|
22
|
+
if (!tls) {
|
|
23
|
+
return (createHttpServer as typeof createServerHttps)(callback);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const options = {
|
|
27
|
+
key: readFileSync(`${dirname}/server.key`),
|
|
28
|
+
cert: readFileSync(`${dirname}/server.crt`),
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
return (createHttpServer as typeof createServerHttps)(options, callback);
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export default function alpListen(
|
|
35
|
+
config: Config,
|
|
36
|
+
callback: RequestListener,
|
|
37
|
+
dirname?: string,
|
|
38
|
+
): Promise<Server> {
|
|
39
|
+
return new Promise((resolve) => {
|
|
40
|
+
const socketPath = config.get<string>('socketPath');
|
|
41
|
+
const port = config.get<number>('port');
|
|
42
|
+
const hostname = config.get<string>('hostname');
|
|
43
|
+
const tls = config.get<boolean>('tls');
|
|
44
|
+
|
|
45
|
+
logger.info('Creating server', socketPath ? { socketPath } : { port });
|
|
46
|
+
const server = createServer(callback, socketPath, tls, dirname);
|
|
47
|
+
|
|
48
|
+
if (socketPath) {
|
|
49
|
+
try {
|
|
50
|
+
unlinkSync(socketPath);
|
|
51
|
+
} catch {}
|
|
52
|
+
|
|
53
|
+
server.listen(socketPath, () => {
|
|
54
|
+
if (socketPath) {
|
|
55
|
+
chmodSync(socketPath, '777');
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
logger.info('Server listening', { socketPath });
|
|
59
|
+
resolve(server);
|
|
60
|
+
});
|
|
61
|
+
} else {
|
|
62
|
+
server.listen(port, hostname, () => {
|
|
63
|
+
logger.info('Server listening', { port });
|
|
64
|
+
resolve(server);
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { Context } from '../AlpNodeApp';
|
|
2
|
+
import { ParamValidationResult } from './ParamValidationResult';
|
|
3
|
+
|
|
4
|
+
export default class ParamValid extends ParamValidationResult {
|
|
5
|
+
context: Context;
|
|
6
|
+
|
|
7
|
+
constructor(context: Context) {
|
|
8
|
+
super();
|
|
9
|
+
this.context = context;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
override _error(): void {
|
|
13
|
+
this.context.throw(400, 'Invalid params', { validator: this });
|
|
14
|
+
}
|
|
15
|
+
}
|