@ticatec/dyna-js 0.1.0 → 0.1.2

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.
@@ -20,7 +20,7 @@ export type SaveModuleToLocalStorage = (data: any) => void;
20
20
  * @param data - Module identifier or data
21
21
  * @returns The module script text
22
22
  */
23
- export type LoadLocalModule = (data: any) => string;
23
+ export type LoadLocalModule = (data: any) => any;
24
24
  /**
25
25
  * Configuration options for ModuleLoader
26
26
  */
@@ -31,21 +31,12 @@ export interface ModuleLoaderOptions {
31
31
  saveModule?: SaveModuleToLocalStorage;
32
32
  /** Optional custom load local module function */
33
33
  loadLocalModule?: LoadLocalModule;
34
+ keyField?: string;
35
+ prefix?: string;
34
36
  }
35
37
  /**
36
38
  * ModuleLoader is a singleton class for managing dynamic module loading with caching
37
39
  * It supports checking for module updates, loading from remote sources, and caching in localStorage
38
- *
39
- * @example
40
- * ```typescript
41
- * const loader = ModuleLoader.initialize(
42
- * async (data) => fetch(`/api/modules/${data.code}`).then(r => r.json()),
43
- * {}
44
- * );
45
- *
46
- * await loader.checkFreshScripts([{ code: 'module1', digest: 'abc123' }]);
47
- * const module = loader.createModule('module1', { React, ReactDOM });
48
- * ```
49
40
  */
50
41
  export default class ModuleLoader {
51
42
  private static instance;
@@ -54,6 +45,8 @@ export default class ModuleLoader {
54
45
  private readonly saveModule;
55
46
  private readonly loadLocalModule;
56
47
  private modulesMap;
48
+ static keyField: string;
49
+ static prefix: string;
57
50
  /**
58
51
  * Private constructor for singleton pattern
59
52
  * @param loadModule - Function to load module from remote
@@ -69,6 +62,7 @@ export default class ModuleLoader {
69
62
  * @returns The singleton ModuleLoader instance
70
63
  */
71
64
  static initialize(loadModule: LoadModule, options: ModuleLoaderOptions): ModuleLoader;
65
+ static getInstance(): ModuleLoader;
72
66
  /**
73
67
  * Check and update modules that are not fresh
74
68
  * Iterates through the list, checks each module, and reloads if needed
@@ -89,5 +83,6 @@ export default class ModuleLoader {
89
83
  * @returns The instantiated module
90
84
  */
91
85
  createModule(key: string, imports: any): any;
86
+ getLayout(key: string): any;
92
87
  }
93
88
  //# sourceMappingURL=ModuleLoader.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"ModuleLoader.d.ts","sourceRoot":"","sources":["../src/ModuleLoader.ts"],"names":[],"mappings":"AAEA;;;;GAIG;AACH,MAAM,MAAM,WAAW,GAAG,CAAC,IAAI,EAAE,GAAG,KAAK,OAAO,CAAC;AAEjD;;;;GAIG;AACH,MAAM,MAAM,UAAU,GAAG,CAAC,IAAI,EAAE,GAAG,KAAK,OAAO,CAAC,GAAG,CAAC,CAAC;AAErD;;;GAGG;AACH,MAAM,MAAM,wBAAwB,GAAG,CAAC,IAAI,EAAE,GAAG,KAAK,IAAI,CAAC;AAE3D;;;;GAIG;AACH,MAAM,MAAM,eAAe,GAAG,CAAC,IAAI,EAAE,GAAG,KAAK,MAAM,CAAC;AA+BpD;;GAEG;AACH,MAAM,WAAW,mBAAmB;IAChC,4CAA4C;IAC5C,WAAW,CAAC,EAAE,WAAW,CAAC;IAC1B,2CAA2C;IAC3C,UAAU,CAAC,EAAE,wBAAwB,CAAC;IACtC,iDAAiD;IACjD,eAAe,CAAC,EAAE,eAAe,CAAA;CACpC;AAED;;;;;;;;;;;;;;GAcG;AACH,MAAM,CAAC,OAAO,OAAO,YAAY;IAE7B,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAe;IACtC,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAa;IACxC,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAc;IAC1C,OAAO,CAAC,QAAQ,CAAC,UAAU,CAA2B;IACtD,OAAO,CAAC,QAAQ,CAAC,eAAe,CAAkB;IAClD,OAAO,CAAC,UAAU,CAAmB;IAErC;;;;;;OAMG;IACH,OAAO;IAQP;;;;;OAKG;IACH,MAAM,CAAC,UAAU,CAAC,UAAU,EAAE,UAAU,EAAE,OAAO,EAAE,mBAAmB,GAAG,YAAY;IAQrF;;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M,CAAC;IACxB,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC;IAEtB;;;;;;OAMG;IACH,OAAO;IAQP;;;;;OAKG;IACH,MAAM,CAAC,UAAU,CAAC,UAAU,EAAE,UAAU,EAAE,OAAO,EAAE,mBAAmB,GAAG,YAAY;IAUrF,MAAM,CAAC,WAAW,IAAI,YAAY;IAIlC;;;;;;;;OAQG;IACG,iBAAiB,CAAC,IAAI,EAAE,KAAK,CAAC,GAAG,CAAC;IAWxC;;;;;;;;OAQG;IACH,YAAY,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,EAAE,GAAG,GAAG,GAAG;IAU5C,SAAS,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG;CAK9B"}
@@ -1,11 +1,20 @@
1
1
  import { getDynaJs } from "./index";
2
+ const getDigestKey = (key) => {
3
+ return `${ModuleLoader.prefix}:md:${key}`;
4
+ };
5
+ const getScriptKey = (key) => {
6
+ return `${ModuleLoader.prefix}:script:${key}`;
7
+ };
8
+ const getLayoutKey = (key) => {
9
+ return `${ModuleLoader.prefix}:layout:${key}`;
10
+ };
2
11
  /**
3
12
  * Default module check function that compares message digest
4
13
  * @param data - Module data containing code and digest
5
14
  * @returns true if the local digest matches the remote digest
6
15
  */
7
16
  const checkMessageDigest = (data) => {
8
- let localDigest = window.localStorage.getItem(`md:${data.code}`);
17
+ let localDigest = window.localStorage.getItem(getDigestKey(data.code));
9
18
  return localDigest == data.digest;
10
19
  };
11
20
  /**
@@ -14,8 +23,12 @@ const checkMessageDigest = (data) => {
14
23
  * @param data - Module data containing code, digest, and scriptText
15
24
  */
16
25
  const saveModule = (data) => {
17
- window.localStorage.setItem(`md:${data.code}`, data.digest);
18
- window.localStorage.setItem(`script:${data.code}`, data.scriptText);
26
+ const key = data[ModuleLoader.keyField];
27
+ window.localStorage.setItem(getDigestKey(key), data.digest);
28
+ window.localStorage.setItem(getScriptKey(key), data.scriptText ?? '');
29
+ if (data.uiLayout) {
30
+ window.localStorage.setItem(key, data.uiLayout);
31
+ }
19
32
  };
20
33
  /**
21
34
  * Default function to load module script from localStorage
@@ -23,22 +36,13 @@ const saveModule = (data) => {
23
36
  * @returns The module script text
24
37
  */
25
38
  const loadLocalModule = (key) => {
26
- return window.localStorage.getItem(`script:${key}`);
39
+ return {
40
+ scriptText: window.localStorage.getItem(getScriptKey(key))
41
+ };
27
42
  };
28
43
  /**
29
44
  * ModuleLoader is a singleton class for managing dynamic module loading with caching
30
45
  * It supports checking for module updates, loading from remote sources, and caching in localStorage
31
- *
32
- * @example
33
- * ```typescript
34
- * const loader = ModuleLoader.initialize(
35
- * async (data) => fetch(`/api/modules/${data.code}`).then(r => r.json()),
36
- * {}
37
- * );
38
- *
39
- * await loader.checkFreshScripts([{ code: 'module1', digest: 'abc123' }]);
40
- * const module = loader.createModule('module1', { React, ReactDOM });
41
- * ```
42
46
  */
43
47
  export default class ModuleLoader {
44
48
  /**
@@ -64,9 +68,14 @@ export default class ModuleLoader {
64
68
  static initialize(loadModule, options) {
65
69
  if (ModuleLoader.instance == null) {
66
70
  ModuleLoader.instance = new ModuleLoader(loadModule, options.moduleCheck ?? checkMessageDigest, options.saveModule ?? saveModule, options.loadLocalModule ?? loadLocalModule);
71
+ ModuleLoader.keyField = options.keyField ?? 'code';
72
+ ModuleLoader.prefix = options.prefix ?? "DynaJS";
67
73
  }
68
74
  return ModuleLoader.instance;
69
75
  }
76
+ static getInstance() {
77
+ return ModuleLoader.instance;
78
+ }
70
79
  /**
71
80
  * Check and update modules that are not fresh
72
81
  * Iterates through the list, checks each module, and reloads if needed
@@ -79,6 +88,7 @@ export default class ModuleLoader {
79
88
  async checkFreshScripts(list) {
80
89
  for (let item of list) {
81
90
  if (!this.moduleCheck(item)) {
91
+ this.modulesMap.delete(ModuleLoader.keyField);
82
92
  let data = await this.loadModule(item);
83
93
  this.saveModule(data);
84
94
  }
@@ -97,10 +107,14 @@ export default class ModuleLoader {
97
107
  createModule(key, imports) {
98
108
  let module = this.modulesMap.get(key);
99
109
  if (module == null) {
100
- let scriptText = this.loadLocalModule(key);
101
- module = getDynaJs().executeSync(scriptText, { imports });
110
+ let dynaCode = this.loadLocalModule(key);
111
+ module = getDynaJs().executeSync(dynaCode.scriptText, { imports });
102
112
  this.modulesMap.set(key, module);
103
113
  }
104
114
  return module;
105
115
  }
116
+ getLayout(key) {
117
+ let text = window.localStorage.getItem(getLayoutKey(key));
118
+ return text == null ? null : JSON.parse(text);
119
+ }
106
120
  }
@@ -1,13 +1,22 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const index_1 = require("./index");
4
+ const getDigestKey = (key) => {
5
+ return `${ModuleLoader.prefix}:md:${key}`;
6
+ };
7
+ const getScriptKey = (key) => {
8
+ return `${ModuleLoader.prefix}:script:${key}`;
9
+ };
10
+ const getLayoutKey = (key) => {
11
+ return `${ModuleLoader.prefix}:layout:${key}`;
12
+ };
4
13
  /**
5
14
  * Default module check function that compares message digest
6
15
  * @param data - Module data containing code and digest
7
16
  * @returns true if the local digest matches the remote digest
8
17
  */
9
18
  const checkMessageDigest = (data) => {
10
- let localDigest = window.localStorage.getItem(`md:${data.code}`);
19
+ let localDigest = window.localStorage.getItem(getDigestKey(data.code));
11
20
  return localDigest == data.digest;
12
21
  };
13
22
  /**
@@ -16,8 +25,13 @@ const checkMessageDigest = (data) => {
16
25
  * @param data - Module data containing code, digest, and scriptText
17
26
  */
18
27
  const saveModule = (data) => {
19
- window.localStorage.setItem(`md:${data.code}`, data.digest);
20
- window.localStorage.setItem(`script:${data.code}`, data.scriptText);
28
+ var _a;
29
+ const key = data[ModuleLoader.keyField];
30
+ window.localStorage.setItem(getDigestKey(key), data.digest);
31
+ window.localStorage.setItem(getScriptKey(key), (_a = data.scriptText) !== null && _a !== void 0 ? _a : '');
32
+ if (data.uiLayout) {
33
+ window.localStorage.setItem(key, data.uiLayout);
34
+ }
21
35
  };
22
36
  /**
23
37
  * Default function to load module script from localStorage
@@ -25,22 +39,13 @@ const saveModule = (data) => {
25
39
  * @returns The module script text
26
40
  */
27
41
  const loadLocalModule = (key) => {
28
- return window.localStorage.getItem(`script:${key}`);
42
+ return {
43
+ scriptText: window.localStorage.getItem(getScriptKey(key))
44
+ };
29
45
  };
30
46
  /**
31
47
  * ModuleLoader is a singleton class for managing dynamic module loading with caching
32
48
  * It supports checking for module updates, loading from remote sources, and caching in localStorage
33
- *
34
- * @example
35
- * ```typescript
36
- * const loader = ModuleLoader.initialize(
37
- * async (data) => fetch(`/api/modules/${data.code}`).then(r => r.json()),
38
- * {}
39
- * );
40
- *
41
- * await loader.checkFreshScripts([{ code: 'module1', digest: 'abc123' }]);
42
- * const module = loader.createModule('module1', { React, ReactDOM });
43
- * ```
44
49
  */
45
50
  class ModuleLoader {
46
51
  /**
@@ -64,12 +69,17 @@ class ModuleLoader {
64
69
  * @returns The singleton ModuleLoader instance
65
70
  */
66
71
  static initialize(loadModule, options) {
67
- var _a, _b, _c;
72
+ var _a, _b, _c, _d, _e;
68
73
  if (ModuleLoader.instance == null) {
69
74
  ModuleLoader.instance = new ModuleLoader(loadModule, (_a = options.moduleCheck) !== null && _a !== void 0 ? _a : checkMessageDigest, (_b = options.saveModule) !== null && _b !== void 0 ? _b : saveModule, (_c = options.loadLocalModule) !== null && _c !== void 0 ? _c : loadLocalModule);
75
+ ModuleLoader.keyField = (_d = options.keyField) !== null && _d !== void 0 ? _d : 'code';
76
+ ModuleLoader.prefix = (_e = options.prefix) !== null && _e !== void 0 ? _e : "DynaJS";
70
77
  }
71
78
  return ModuleLoader.instance;
72
79
  }
80
+ static getInstance() {
81
+ return ModuleLoader.instance;
82
+ }
73
83
  /**
74
84
  * Check and update modules that are not fresh
75
85
  * Iterates through the list, checks each module, and reloads if needed
@@ -82,6 +92,7 @@ class ModuleLoader {
82
92
  async checkFreshScripts(list) {
83
93
  for (let item of list) {
84
94
  if (!this.moduleCheck(item)) {
95
+ this.modulesMap.delete(ModuleLoader.keyField);
85
96
  let data = await this.loadModule(item);
86
97
  this.saveModule(data);
87
98
  }
@@ -100,11 +111,15 @@ class ModuleLoader {
100
111
  createModule(key, imports) {
101
112
  let module = this.modulesMap.get(key);
102
113
  if (module == null) {
103
- let scriptText = this.loadLocalModule(key);
104
- module = (0, index_1.getDynaJs)().executeSync(scriptText, { imports });
114
+ let dynaCode = this.loadLocalModule(key);
115
+ module = (0, index_1.getDynaJs)().executeSync(dynaCode.scriptText, { imports });
105
116
  this.modulesMap.set(key, module);
106
117
  }
107
118
  return module;
108
119
  }
120
+ getLayout(key) {
121
+ let text = window.localStorage.getItem(getLayoutKey(key));
122
+ return text == null ? null : JSON.parse(text);
123
+ }
109
124
  }
110
125
  exports.default = ModuleLoader;
package/dist/index.d.ts CHANGED
@@ -1,9 +1,10 @@
1
1
  import DynaJs from './DynaJs';
2
+ import ModuleLoader from "./ModuleLoader";
2
3
  export * from './types';
3
4
  export * from './utils';
4
5
  export declare function createDynaJs(config?: import('./types').DynaJsConfig): DynaJs;
5
6
  export declare function initializeDynaJs(config: import('./types').DynaJsConfig): DynaJs;
6
7
  export declare function getDynaJs(): DynaJs;
7
- export { DynaJs };
8
+ export { DynaJs, ModuleLoader };
8
9
  export default DynaJs;
9
10
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAO,UAAU,CAAC;AAC/B,cAAc,SAAS,CAAC;AACxB,cAAc,SAAS,CAAC;AAExB,wBAAgB,YAAY,CAAC,MAAM,CAAC,EAAE,OAAO,SAAS,EAAE,YAAY,UAEnE;AAED,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,OAAO,SAAS,EAAE,YAAY,UAEtE;AAED,wBAAgB,SAAS,WAExB;AAED,OAAO,EAAE,MAAM,EAAE,CAAC;AAClB,eAAe,MAAM,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAO,UAAU,CAAC;AAC/B,OAAO,YAAY,MAAM,gBAAgB,CAAC;AAC1C,cAAc,SAAS,CAAC;AACxB,cAAc,SAAS,CAAC;AAExB,wBAAgB,YAAY,CAAC,MAAM,CAAC,EAAE,OAAO,SAAS,EAAE,YAAY,UAEnE;AAED,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,OAAO,SAAS,EAAE,YAAY,UAEtE;AAED,wBAAgB,SAAS,WAExB;AAED,OAAO,EAAE,MAAM,EAAE,YAAY,EAAE,CAAC;AAChC,eAAe,MAAM,CAAC"}
package/dist/index.esm.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import DynaJs from './DynaJs';
2
+ import ModuleLoader from "./ModuleLoader";
2
3
  export * from './types';
3
4
  export * from './utils';
4
5
  export function createDynaJs(config) {
@@ -10,5 +11,5 @@ export function initializeDynaJs(config) {
10
11
  export function getDynaJs() {
11
12
  return DynaJs.getInstance();
12
13
  }
13
- export { DynaJs };
14
+ export { DynaJs, ModuleLoader };
14
15
  export default DynaJs;
package/dist/index.js CHANGED
@@ -17,12 +17,14 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
17
17
  return (mod && mod.__esModule) ? mod : { "default": mod };
18
18
  };
19
19
  Object.defineProperty(exports, "__esModule", { value: true });
20
- exports.DynaJs = void 0;
20
+ exports.ModuleLoader = exports.DynaJs = void 0;
21
21
  exports.createDynaJs = createDynaJs;
22
22
  exports.initializeDynaJs = initializeDynaJs;
23
23
  exports.getDynaJs = getDynaJs;
24
24
  const DynaJs_1 = __importDefault(require("./DynaJs"));
25
25
  exports.DynaJs = DynaJs_1.default;
26
+ const ModuleLoader_1 = __importDefault(require("./ModuleLoader"));
27
+ exports.ModuleLoader = ModuleLoader_1.default;
26
28
  __exportStar(require("./types"), exports);
27
29
  __exportStar(require("./utils"), exports);
28
30
  function createDynaJs(config) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ticatec/dyna-js",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "description": "A TypeScript library for dynamic code execution using new Function() that works in both Node.js and browser environments",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.esm.js",