@syncbridge/common 0.9.3 → 0.9.5

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/constants.js CHANGED
@@ -1,4 +1,4 @@
1
- export const version = '0.9.3';
1
+ export const version = '0.9.5';
2
2
  export const CLASS_METADATA = Symbol.for('CLASS_METADATA');
3
3
  export const CLASS_TYPE = Symbol.for('CLASS_TYPE');
4
4
  export const VARIABLE_CONTAINER = Symbol.for('VARIABLE_CONTAINER');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@syncbridge/common",
3
- "version": "0.9.3",
3
+ "version": "0.9.5",
4
4
  "description": "SyncBridge Common utilities",
5
5
  "author": "Panates Inc",
6
6
  "license": "MIT",
@@ -11,8 +11,8 @@
11
11
  "caller-callsite": "^6.0.0",
12
12
  "reflect-metadata": "^0.2.2",
13
13
  "semver": "^7.7.4",
14
- "ts-gems": "^3.11.5",
15
- "valgen": "^5.19.5"
14
+ "ts-gems": "^3.11.6",
15
+ "valgen": "^6.0.3"
16
16
  },
17
17
  "peerDependencies": {
18
18
  "@opra/common": ">=1.17.1 <2"
@@ -26,8 +26,9 @@ export declare abstract class ExtensionBase {
26
26
  readonly pkg: ExtensionPackage;
27
27
  readonly exportName: string;
28
28
  readonly metadata: ElementMetadata;
29
- readonly name: string;
29
+ readonly organization: string;
30
30
  readonly packageName: string;
31
+ readonly className: string;
31
32
  protected constructor(pkg: ExtensionPackage, exportName: string, metadata: ElementMetadata, ctor?: Type<BaseElement>);
32
33
  get uri(): string;
33
34
  get displayName(): string;
@@ -17,15 +17,18 @@ export class ExtensionBase {
17
17
  pkg;
18
18
  exportName;
19
19
  metadata;
20
- name;
20
+ organization;
21
21
  packageName;
22
+ className;
22
23
  constructor(pkg, exportName, metadata, ctor) {
23
24
  this.pkg = pkg;
24
25
  this.exportName = exportName;
25
26
  this.metadata = metadata;
26
27
  this._ctor = ctor;
27
- const [packageName, name] = metadata.uri.split(':');
28
- this.name = name;
28
+ const [pn, className] = metadata.uri.split(':');
29
+ const [organization, packageName] = pn.split('/');
30
+ this.className = className;
31
+ this.organization = (organization || '').replace('@', '');
29
32
  this.packageName = packageName;
30
33
  }
31
34
  get uri() {
@@ -1,4 +1,5 @@
1
1
  import { ResponsiveMap } from '@opra/common';
2
+ import { AsyncEventEmitter } from 'node-events-async';
2
3
  import { Type } from 'ts-gems';
3
4
  import { ComponentBase } from '../classes/component-base.js';
4
5
  import { ProcessorBase } from '../classes/processor-base.js';
@@ -12,7 +13,7 @@ declare const instanceSymbol: unique symbol;
12
13
  *
13
14
  * @class ExtensionRegistry
14
15
  */
15
- export declare class ExtensionRegistry {
16
+ export declare class ExtensionRegistry extends AsyncEventEmitter {
16
17
  protected static [instanceSymbol]: ExtensionRegistry;
17
18
  static get packages(): ResponsiveMap<ExtensionPackage[]>;
18
19
  static get components(): ResponsiveMap<ComponentExtension[]>;
@@ -28,6 +29,10 @@ export declare class ExtensionRegistry {
28
29
  static registerClass<T extends ComponentBase>(ctor: Type<T>): Promise<ComponentExtension>;
29
30
  static registerClass<T extends ProcessorBase>(ctor: Type<T>): Promise<ProcessorExtension>;
30
31
  static storePackage(pkg: ExtensionPackage): void;
32
+ static onRegisterPackage(fn: (pkg: ExtensionPackage) => void): void;
33
+ static onRegisterComponent(fn: (pkg: ComponentExtension) => void): void;
34
+ static onRegisterProcessor(fn: (pkg: ProcessorExtension) => void): void;
35
+ constructor();
31
36
  protected _resolver: ExtensionRegistry.PackageResolver;
32
37
  readonly packages: ResponsiveMap<ExtensionPackage[]>;
33
38
  readonly components: ResponsiveMap<ComponentExtension[]>;
@@ -3,6 +3,7 @@ import path from 'node:path';
3
3
  import { pathToFileURL } from 'node:url';
4
4
  import { updateErrorMessage } from '@jsopen/objects';
5
5
  import { ResponsiveMap } from '@opra/common';
6
+ import { AsyncEventEmitter } from 'node-events-async';
6
7
  import semver from 'semver';
7
8
  import { SbError } from '../classes/sb-error.js';
8
9
  import { CLASS_METADATA, CLASS_TYPE } from '../constants.js';
@@ -19,7 +20,7 @@ const instanceSymbol = Symbol.for('instance');
19
20
  *
20
21
  * @class ExtensionRegistry
21
22
  */
22
- export class ExtensionRegistry {
23
+ export class ExtensionRegistry extends AsyncEventEmitter {
23
24
  static [instanceSymbol] = new ExtensionRegistry();
24
25
  static get packages() {
25
26
  return this[instanceSymbol].packages;
@@ -60,6 +61,19 @@ export class ExtensionRegistry {
60
61
  static storePackage(pkg) {
61
62
  return this[instanceSymbol].storePackage(pkg);
62
63
  }
64
+ static onRegisterPackage(fn) {
65
+ this[instanceSymbol].on('register-package', fn);
66
+ }
67
+ static onRegisterComponent(fn) {
68
+ this[instanceSymbol].on('register-component', fn);
69
+ }
70
+ static onRegisterProcessor(fn) {
71
+ this[instanceSymbol].on('register-processor', fn);
72
+ }
73
+ constructor() {
74
+ super();
75
+ this.setMaxListeners(1000);
76
+ }
63
77
  _resolver = import.meta.resolve;
64
78
  packages = new ResponsiveMap();
65
79
  components = new ResponsiveMap();
@@ -243,16 +257,17 @@ export class ExtensionRegistry {
243
257
  else
244
258
  items.push(cmp);
245
259
  /** Store to componentsByClassName */
246
- items = this.componentsByClassName.get(cmp.name);
260
+ items = this.componentsByClassName.get(cmp.className);
247
261
  if (!items) {
248
262
  items = [];
249
- this.componentsByClassName.set(cmp.name, items);
263
+ this.componentsByClassName.set(cmp.className, items);
250
264
  }
251
265
  i = items.findIndex(v => v.version === cmp.version);
252
266
  if (i >= 0)
253
267
  items[i] = cmp;
254
268
  else
255
269
  items.push(cmp);
270
+ this.emitSafe('register-component', cmp);
256
271
  }
257
272
  }
258
273
  if (pkg.processors?.length) {
@@ -272,17 +287,19 @@ export class ExtensionRegistry {
272
287
  else
273
288
  items.push(prc);
274
289
  /** Store to componentsByClassName */
275
- items = this.processors.get(prc.name);
290
+ items = this.processors.get(prc.className);
276
291
  if (!items) {
277
292
  items = [];
278
- this.processorsByClassName.set(prc.name, items);
293
+ this.processorsByClassName.set(prc.className, items);
279
294
  }
280
295
  i = items.findIndex(v => v.version === prc.version);
281
296
  if (i >= 0)
282
297
  items[i] = prc;
283
298
  else
284
299
  items.push(prc);
300
+ this.emitSafe('register-processor', prc);
285
301
  }
286
302
  }
303
+ this.emitSafe('register-package', pkg);
287
304
  }
288
305
  }
@@ -3,11 +3,11 @@ import { ProcessorMetadata, Profile } from '../models/index.js';
3
3
  /**
4
4
  * Materializes metadata from profile.
5
5
  */
6
- export declare function materializeMetadata(profile: Profile): ProcessorMetadata;
6
+ export declare function materializeMetadata(profile: Profile, returnUndefinedComponentsAsAbstract?: boolean): ProcessorMetadata;
7
7
  /**
8
8
  *
9
9
  */
10
- export declare function materializeMetadataSilent(profile: Profile): {
10
+ export declare function materializeMetadataSilent(profile: Profile, returnUndefinedComponentsAsAbstract?: boolean): {
11
11
  metadata: ProcessorMetadata;
12
12
  issues?: StackExecutor.Issue[];
13
13
  };
@@ -6,8 +6,8 @@ import { ExtensionRegistry } from '../registry/extension-registry.js';
6
6
  /**
7
7
  * Materializes metadata from profile.
8
8
  */
9
- export function materializeMetadata(profile) {
10
- const { metadata, issues } = materializeMetadataSilent(profile);
9
+ export function materializeMetadata(profile, returnUndefinedComponentsAsAbstract) {
10
+ const { metadata, issues } = materializeMetadataSilent(profile, returnUndefinedComponentsAsAbstract);
11
11
  if (issues?.length) {
12
12
  const msg = 'Worker profile validation error.\n ' +
13
13
  issues
@@ -27,7 +27,7 @@ export function materializeMetadata(profile) {
27
27
  /**
28
28
  *
29
29
  */
30
- export function materializeMetadataSilent(profile) {
30
+ export function materializeMetadataSilent(profile, returnUndefinedComponentsAsAbstract) {
31
31
  const stackExecutor = new StackExecutor();
32
32
  const processorRec = ExtensionRegistry.getProcessor(profile.processorUri);
33
33
  const metadata = omitUndefined(clone({
@@ -43,7 +43,7 @@ export function materializeMetadataSilent(profile) {
43
43
  }, { deep: 'full' }));
44
44
  if (metadata.components) {
45
45
  stackExecutor.execute('components', () => {
46
- metadata.components = _materializeComponents(stackExecutor, metadata.components, profile);
46
+ metadata.components = _materializeComponents(stackExecutor, metadata.components, profile, returnUndefinedComponentsAsAbstract);
47
47
  });
48
48
  }
49
49
  if (stackExecutor.issues?.length)
@@ -54,7 +54,7 @@ export function materializeMetadataSilent(profile) {
54
54
  *
55
55
  * @private
56
56
  */
57
- function _materializeComponents(stackExecutor, srcMetadata, profile) {
57
+ function _materializeComponents(stackExecutor, srcMetadata, profile, returnUndefinedComponentsAsAbstract) {
58
58
  const out = {};
59
59
  for (const [componentName, metadataComponent,] of Object.entries(srcMetadata)) {
60
60
  stackExecutor.execute(componentName, () => {
@@ -62,9 +62,18 @@ function _materializeComponents(stackExecutor, srcMetadata, profile) {
62
62
  let materializedComponent;
63
63
  const profileComponent = profile?.components?.[componentName];
64
64
  if (!profileComponent) {
65
- if (metadataComponent.required)
66
- throw new SbValidationError(`Component "${componentName}" should be configured`);
67
- return;
65
+ if (!returnUndefinedComponentsAsAbstract) {
66
+ if (metadataComponent.required)
67
+ throw new SbValidationError(`Component "${componentName}" should be configured`);
68
+ return;
69
+ }
70
+ materializedComponent = {
71
+ abstract: true,
72
+ uri: metadataComponent.uri,
73
+ description: metadataComponent.description,
74
+ required: metadataComponent.required,
75
+ };
76
+ out[componentName] = materializedComponent;
68
77
  }
69
78
  else {
70
79
  /** istanbul ignore next */
@@ -88,7 +97,7 @@ function _materializeComponents(stackExecutor, srcMetadata, profile) {
88
97
  }
89
98
  if (reg.metadata.components) {
90
99
  stackExecutor.execute('components', () => {
91
- materializedComponent.components = _materializeComponents(stackExecutor, reg.metadata.components, profileComponent);
100
+ materializedComponent.components = _materializeComponents(stackExecutor, reg.metadata.components, profileComponent, returnUndefinedComponentsAsAbstract);
92
101
  });
93
102
  }
94
103
  }