@lakutata/core 1.0.29 → 1.0.33

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (69) hide show
  1. package/build/Application.d.ts +1 -0
  2. package/build/Application.js +5 -7
  3. package/build/Application.js.map +1 -1
  4. package/build/ApplicationManagement.js.map +1 -1
  5. package/build/base/BaseObject.js +1 -1
  6. package/build/base/BaseObject.js.map +1 -1
  7. package/build/base/Container.js +16 -0
  8. package/build/base/Container.js.map +1 -1
  9. package/build/base/ValidatorClass.d.ts +1 -0
  10. package/build/base/ValidatorClass.js +70 -53
  11. package/build/base/ValidatorClass.js.map +1 -1
  12. package/build/base/abstracts/Module.js +8 -8
  13. package/build/base/abstracts/Module.js.map +1 -1
  14. package/build/base/abstracts/Workflow.js +1 -1
  15. package/build/base/abstracts/Workflow.js.map +1 -1
  16. package/build/base/process/ProcessManager.js +1 -1
  17. package/build/base/process/ProcessManager.js.map +1 -1
  18. package/build/base/thread/ThreadManager.js +1 -1
  19. package/build/base/thread/ThreadManager.js.map +1 -1
  20. package/build/decorators/MethodValidation.js.map +1 -1
  21. package/build/interfaces/ShellJsInterfaces.d.ts +162 -0
  22. package/build/interfaces/ShellJsInterfaces.js +3 -0
  23. package/build/interfaces/ShellJsInterfaces.js.map +1 -0
  24. package/build/lib/minified/ShellJs.d.ts +2 -0
  25. package/build/lib/minified/ShellJs.js +3699 -0
  26. package/build/lib/minified/ShellJs.js.map +1 -0
  27. package/build/lib/multipleRunner/lib/Process.js +3 -3
  28. package/build/lib/multipleRunner/lib/Process.js.map +1 -1
  29. package/build/lib/multipleRunner/lib/Thread.js +4 -4
  30. package/build/lib/multipleRunner/lib/Thread.js.map +1 -1
  31. package/build/plugins/Crypto.js +2 -2
  32. package/build/plugins/Crypto.js.map +1 -1
  33. package/build/plugins/Formatter.js +1 -1
  34. package/build/plugins/Formatter.js.map +1 -1
  35. package/build/plugins/HttpRequest.js +8 -8
  36. package/build/plugins/HttpRequest.js.map +1 -1
  37. package/build/plugins/Logger.js +15 -6
  38. package/build/plugins/Logger.js.map +1 -1
  39. package/build/plugins/MessagePack.js +2 -2
  40. package/build/plugins/MessagePack.js.map +1 -1
  41. package/build/plugins/Security.js +24 -24
  42. package/build/plugins/Security.js.map +1 -1
  43. package/build/plugins/Shell.d.ts +39 -0
  44. package/build/plugins/Shell.js +58 -0
  45. package/build/plugins/Shell.js.map +1 -0
  46. package/build/types/TPluginsConfig.d.ts +1 -0
  47. package/build/utils/Delay.js +1 -1
  48. package/build/utils/Delay.js.map +1 -1
  49. package/build/utils/Timeout.js +1 -1
  50. package/build/utils/Timeout.js.map +1 -1
  51. package/build/utils/ValidDataUrl.d.ts +1 -0
  52. package/build/utils/ValidDataUrl.js +8 -0
  53. package/build/utils/ValidDataUrl.js.map +1 -0
  54. package/package.json +3 -3
  55. package/src/Application.ts +5 -11
  56. package/src/ApplicationManagement.ts +1 -1
  57. package/src/base/Container.ts +14 -1
  58. package/src/base/ValidatorClass.ts +44 -7
  59. package/src/base/abstracts/Exception.ts +1 -1
  60. package/src/base/abstracts/Module.ts +2 -1
  61. package/src/decorators/MethodValidation.ts +6 -6
  62. package/src/interfaces/ShellJsInterfaces.ts +858 -0
  63. package/src/lib/minified/ShellJs.js +1 -0
  64. package/src/plugins/Crypto.ts +3 -3
  65. package/src/plugins/Logger.ts +8 -2
  66. package/src/plugins/Shell.ts +371 -0
  67. package/src/tests/Main.spec.ts +6 -5
  68. package/src/types/TPluginsConfig.ts +1 -1
  69. package/src/utils/ValidDataUrl.ts +8 -0
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.validDataUrl = void 0;
4
+ function validDataUrl(candidate) {
5
+ return /^data:([a-z]+\/[a-z0-9-+.]+(;[a-z0-9-.!#$%*+.{}|~`]+=[a-z0-9-.!#$%*+.{}()|~`]+)*)?(;base64)?,([a-z0-9!$&',()*+;=\-._~:@\/?%\s<>]*?)$/i.test(candidate);
6
+ }
7
+ exports.validDataUrl = validDataUrl;
8
+ //# sourceMappingURL=ValidDataUrl.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ValidDataUrl.js","sourceRoot":"","sources":["../../src/utils/ValidDataUrl.ts"],"names":[],"mappings":";;;AAKA,SAAgB,YAAY,CAAC,SAAiB;IAC7C,OAAO,uIAAuI,CAAC,IAAI,CAAC,SAAS,CAAC,CAAA;AAC/J,CAAC;AAFD,oCAEC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lakutata/core",
3
- "version": "1.0.29",
3
+ "version": "1.0.33",
4
4
  "description": "Lakutata Framework Core",
5
5
  "main": "./build/Core.js",
6
6
  "types": "./build/Core.d.ts",
@@ -53,9 +53,9 @@
53
53
  "author": "alex.ma",
54
54
  "license": "MIT",
55
55
  "dependencies": {
56
- "@thiagodelgado111/yup-phone": "^0.0.2",
57
56
  "bcryptjs": "^2.4.3",
58
57
  "crypto-js": "^4.1.1",
58
+ "google-libphonenumber": "^3.2.25",
59
59
  "inversify": "^5.1.1",
60
60
  "is-ssh": "^1.3.3",
61
61
  "json-helpers": "^5.1.4",
@@ -73,7 +73,6 @@
73
73
  "random": "^3.0.6",
74
74
  "reflect-metadata": "^0.1.13",
75
75
  "string-random": "^0.1.3",
76
- "valid-data-url": "^3.0.1",
77
76
  "winston": "^3.3.3",
78
77
  "winston-transport": "^4.4.0",
79
78
  "yup": "^0.32.11"
@@ -82,6 +81,7 @@
82
81
  "@types/bcryptjs": "^2.4.2",
83
82
  "@types/chai": "^4.2.14",
84
83
  "@types/crypto-js": "^4.0.2",
84
+ "@types/google-libphonenumber": "^7.4.23",
85
85
  "@types/is-ssh": "^1.3.0",
86
86
  "@types/lodash.mapvalues": "^4.6.6",
87
87
  "@types/merge-deep": "^3.0.0",
@@ -10,10 +10,10 @@ import {MessagePack} from './plugins/MessagePack'
10
10
  import {JSON} from './plugins/JSON'
11
11
  import {TWorkflowsConfig} from './types/TWorkflowsConfig'
12
12
  import {Logger} from './plugins/Logger'
13
- import {format} from 'winston'
14
13
  import {TProcessesConfig} from './types/TProcessesConfig'
15
14
  import {TThreadsConfig} from './types/TThreadsConfig'
16
15
  import momentTimezone from 'moment-timezone'
16
+ import {Shell} from './plugins/Shell'
17
17
 
18
18
  export class Application extends Module {
19
19
  /**
@@ -65,6 +65,9 @@ export class Application extends Module {
65
65
  {
66
66
  class: JSON
67
67
  },
68
+ {
69
+ class: Shell
70
+ },
68
71
  {
69
72
  class: Formatter,
70
73
  dateFormat: 'YYYY-MM-DD HH:mm:ss'
@@ -77,16 +80,6 @@ export class Application extends Module {
77
80
  appId: process.env.appId,
78
81
  appName: process.env.appName
79
82
  },
80
- console: {
81
- silent: false,
82
- format: format.combine(
83
- format.timestamp({format: 'YYYY-MM-DD HH:mm:ss'}),
84
- format.colorize(),
85
- format.printf(packet => {
86
- return `${packet.timestamp} ${packet.level}: ${packet.message}`
87
- })
88
- )
89
- },
90
83
  transports: []
91
84
  }
92
85
  ]
@@ -163,6 +156,7 @@ export class Application extends Module {
163
156
  * @param {string} tz
164
157
  */
165
158
  public setTimezone(tz: string) {
159
+ process.env.TZ = tz
166
160
  this.tz = tz
167
161
  }
168
162
 
@@ -25,7 +25,7 @@ export async function createApp(config: IApplicationConfig): Promise<Application
25
25
  await coreContainer.initialize()
26
26
  return coreContainer.getModule(Application)
27
27
  } catch (e) {
28
- throw new FatalException(e.message)
28
+ throw new FatalException((e as Error).message)
29
29
  }
30
30
  }
31
31
 
@@ -83,12 +83,25 @@ export class Container<TObject extends BaseObject = BaseObject, TPlugin extends
83
83
  * @param {IConstructor<TPlugin>} pluginConstructor
84
84
  * @param {string} identifier
85
85
  * @param {object} config
86
- * @param {"Transient" | "Singleton"} scope
87
86
  */
88
87
  public bindPlugin(pluginConstructor: IConstructor<TPlugin>, identifier: string, config: object): void {
88
+ const scope = config['scope'] ? config['scope'] : 'Transient'
89
89
  const pluginConfiguration = config ? config : {}
90
90
  if (this.pluginIoC.isBound(identifier)) return
91
91
  const binding = this.pluginIoC.bind(identifier).to(pluginConstructor)
92
+ switch (scope) {
93
+ case 'Transient': {
94
+ binding.inTransientScope()
95
+ }
96
+ break
97
+ case 'Singleton': {
98
+ binding.inSingletonScope()
99
+ }
100
+ break
101
+ default: {
102
+ binding.inTransientScope()
103
+ }
104
+ }
92
105
  binding.onActivation((context, injectable) => {
93
106
  const onActivationFunc = function (this: TPlugin) {
94
107
  const pluginConfigurationKeys = Object.keys(pluginConfiguration)
@@ -25,11 +25,11 @@ import {OptionalArraySchema} from 'yup/lib/array'
25
25
  import Reference, {ReferenceOptions} from 'yup/lib/Reference'
26
26
  import mapValues from 'lodash/mapValues'
27
27
  import {ValidationException} from '../exceptions/ValidationException'
28
- import phoneValidationMethod, {YUP_METHOD as PHONE_METHOD} from '@thiagodelgado111/yup-phone'
29
28
  import isSsh from 'is-ssh'
30
- import validDataUrl from 'valid-data-url'
31
29
  import {MixedSchema} from 'yup/lib/mixed'
32
30
  import {BaseObject} from './BaseObject'
31
+ import gPhoneNumber from 'google-libphonenumber'
32
+ import {validDataUrl} from '../utils/ValidDataUrl'
33
33
 
34
34
  declare module 'yup' {
35
35
  interface StringSchema {
@@ -44,13 +44,15 @@ declare module 'yup' {
44
44
  dataUrl(): StringSchema
45
45
 
46
46
  url(option?: { acceptParams?: boolean; protocol?: string[] | string; blackHostnameList?: string[]; whiteHostnameList?: string[] }): StringSchema
47
+
48
+ phone(countryCode?: string, strict?: boolean): StringSchema
47
49
  }
48
50
 
49
51
  interface ObjectSchema<TShape extends ObjectShape, TContext extends AnyObject = AnyObject, TIn extends Maybe<TypeOfShape<TShape>> = TypeOfShape<TShape>, TOut extends Maybe<AssertsShape<TShape>> = AssertsShape<TShape> | Optionals<TIn>> extends BaseSchema<TIn, TContext, TOut> {
50
52
  structure(valueSchema: AnySchema): OptionalObjectSchema<TShape, Record<string, any>, TypeOfShape<TShape>>
51
53
  }
52
54
  }
53
-
55
+ const PHONE_METHOD = 'phone'
54
56
  const IPV4_METHOD = 'ipv4'
55
57
  const IPV6_METHOD = 'ipv6'
56
58
  const IP_ALL_METHOD = 'ip'
@@ -515,7 +517,42 @@ export class ValidatorClass extends BaseObject {
515
517
 
516
518
  constructor() {
517
519
  super()
518
- addMethod(string, PHONE_METHOD, phoneValidationMethod)
520
+ addMethod(string, PHONE_METHOD, function (this: StringSchema, countryCode?: string, strict: boolean = false) {
521
+ const phoneUtil = gPhoneNumber.PhoneNumberUtil.getInstance()
522
+ const CLDR_REGION_CODE_SIZE = 2
523
+ const isValidCountryCode = (countryCode: any): boolean => typeof countryCode === 'string' && countryCode.length === CLDR_REGION_CODE_SIZE
524
+ const errMsg = isValidCountryCode(countryCode)
525
+ ? `\${path} must be a valid phone number for region ${countryCode}`
526
+ : '${path} must be a valid phone number.'
527
+
528
+ return this.test(<any>{
529
+ name: PHONE_METHOD,
530
+ message: errMsg,
531
+ test(value: string) {
532
+ if (this.schema._nullable && value === null) {
533
+ return this.resolve(true)
534
+ }
535
+ if (!isValidCountryCode(countryCode)) {
536
+ countryCode = 'US'
537
+ strict = false
538
+ }
539
+ let isValid
540
+ try {
541
+ const phoneNumber = phoneUtil.parseAndKeepRawInput(value, countryCode)
542
+ if (!phoneUtil.isPossibleNumber(phoneNumber)) {
543
+ return false
544
+ }
545
+ const regionCodeFromPhoneNumber = phoneUtil.getRegionCodeForNumber(phoneNumber)
546
+ isValid = strict
547
+ ? phoneUtil.isValidNumberForRegion(phoneNumber, countryCode)
548
+ : phoneUtil.isValidNumberForRegion(phoneNumber, regionCodeFromPhoneNumber)
549
+ } catch (error) {
550
+ isValid = false
551
+ }
552
+ return this.resolve(isValid)
553
+ }
554
+ })
555
+ })
519
556
  addMethod(string, IPV4_METHOD, function (this: StringSchema, message = 'Invalid IP address') {
520
557
  return this.matches(/(^(\d{1,3}\.){3}(\d{1,3})$)/, {
521
558
  message,
@@ -668,7 +705,7 @@ export class ValidatorClass extends BaseObject {
668
705
  try {
669
706
  return await schema.validate(data, {strict: true})
670
707
  } catch (e) {
671
- throw this.generateException(ValidationException,e.message)
708
+ throw this.generateException(ValidationException, (e as Error).message)
672
709
  }
673
710
  }
674
711
 
@@ -682,7 +719,7 @@ export class ValidatorClass extends BaseObject {
682
719
  try {
683
720
  return schema.validateSync(data, {strict: true})
684
721
  } catch (e) {
685
- throw this.generateException(ValidationException,e.message)
722
+ throw this.generateException(ValidationException, (e as Error).message)
686
723
  }
687
724
  }
688
725
 
@@ -705,4 +742,4 @@ export class ValidatorClass extends BaseObject {
705
742
  public isValidSync<SchemaType extends AnySchema, DataType = any>(schema: SchemaType, data: DataType): boolean {
706
743
  return schema.isValidSync(data, {strict: true})
707
744
  }
708
- }
745
+ }
@@ -57,4 +57,4 @@ export abstract class Exception extends Error {
57
57
  protected templates(): string[] {
58
58
  return []
59
59
  }
60
- }
60
+ }
@@ -160,12 +160,13 @@ export abstract class Module extends BaseObject {
160
160
  * @protected
161
161
  */
162
162
  protected bindPlugins(): void {
163
+ //todo 可能需要合并一下多个相同插件的配置
163
164
  if (!this.plugins) return
164
165
  const reversedPlugins = this.plugins.reverse()
165
166
  const registeredPluginSet = new Set()
166
167
  for (const pluginConstructorOrConfig of reversedPlugins) {
167
168
  let pluginConstructor
168
- let pluginConfig: object = {}
169
+ let pluginConfig: object & { scope?: 'Transient' | 'Singleton' } = {}
169
170
  if (typeof pluginConstructorOrConfig === 'object') {
170
171
  pluginConstructor = pluginConstructorOrConfig.class
171
172
  for (const configKey of Object.keys(pluginConstructorOrConfig)) {
@@ -26,7 +26,7 @@ export const Return = (returnSchema: AnySchema, options: { strict?: boolean, str
26
26
  const methodResult = returnSchema.validateSync(originalAsyncMethodResult, options)
27
27
  resolve(methodResult)
28
28
  } catch (e) {
29
- reject(new InvalidMethodReturnException(e.message))
29
+ reject(new InvalidMethodReturnException((e as Error).message))
30
30
  }
31
31
  }).catch(reject)
32
32
  })
@@ -35,9 +35,9 @@ export const Return = (returnSchema: AnySchema, options: { strict?: boolean, str
35
35
  return returnSchema.validateSync(originalMethodResult, options)
36
36
  } catch (e) {
37
37
  if (target['generateException']) {
38
- throw target['generateException'](InvalidMethodReturnException, e.message)
38
+ throw target['generateException'](InvalidMethodReturnException, (e as Error).message)
39
39
  } else {
40
- throw new InvalidMethodReturnException(e.message)
40
+ throw new InvalidMethodReturnException((e as Error).message)
41
41
  }
42
42
  }
43
43
  }
@@ -68,13 +68,13 @@ export const Accept = (argumentSchemas: AnySchema | AnySchema[], options: { stri
68
68
  }
69
69
  } catch (e) {
70
70
  if (target['generateException']) {
71
- throw target['generateException'](InvalidMethodAcceptException, e.message)
71
+ throw target['generateException'](InvalidMethodAcceptException, (e as Error).message)
72
72
  } else {
73
- throw new InvalidMethodAcceptException(e.message)
73
+ throw new InvalidMethodAcceptException((e as Error).message)
74
74
  }
75
75
  }
76
76
  return originalMethod.apply(this, args)
77
77
  }
78
78
  return descriptor
79
79
  }
80
- }
80
+ }