emr-bridge 1.0.0 → 1.1.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/README.md CHANGED
@@ -13,6 +13,7 @@ JS library for easily providing access to the **main** from the **renderer** pro
13
13
  - [scopes](#scopes)
14
14
  - [access](#restricting-access-to-variables)
15
15
  - [usage in renderer](#access-from-renderer-and-preload)
16
+ - [promises](#promises)
16
17
 
17
18
  ## Installation
18
19
 
@@ -41,14 +42,14 @@ Use **publicStaticMethod**/**publicStaticProperty** decorators
41
42
 
42
43
  ```ts
43
44
  // Main process
44
- import { publicStaticMethod, publicStaticProperty, access, Access } from 'emr-bridge'
45
+ import { publicStaticMethod, publicStaticProperty, Access } from 'emr-bridge'
45
46
 
46
47
  class User {
47
48
  private static name = 'Name'
48
49
  private static age = 20
49
50
  private static money = 1000
50
51
 
51
- @publicStaticProperty()
52
+ @publicStaticProperty('userBalance')
52
53
  static get balance(): string {
53
54
  return `${this.money}$`
54
55
  }
@@ -57,12 +58,14 @@ class User {
57
58
  this.money = Number(newBalance.split('$')[0])
58
59
  }
59
60
 
60
- @publicStaticProperty('userInfo')
61
+ @publicStaticProperty({
62
+ name: 'userInfo',
63
+ access: Access.get
64
+ })
61
65
  static get info(): string {
62
66
  return `Name: '${this.name}'; Age: '${this.age}'; Money: '${this.money}';`
63
67
  }
64
68
 
65
- @access(Access.get)
66
69
  @publicStaticMethod()
67
70
  static setName(newName: string): void {
68
71
  this.name = newName
@@ -79,9 +82,11 @@ class User {
79
82
 
80
83
  Use **providePublic** and **publicMethod**/**publicProperty** decorators.
81
84
 
85
+ Without calling **providePublic**, public methods will not be accessible from renderer.
86
+
82
87
  ```ts
83
88
  // Main process
84
- import { providePublic, publicMethod, publicProperty, access, Access } from 'emr-bridge'
89
+ import { providePublic, publicMethod, publicProperty, Access } from 'emr-bridge'
85
90
 
86
91
  class User {
87
92
  private name = 'Name'
@@ -97,8 +102,10 @@ class User {
97
102
  this.money = Number(newBalance.split('$')[0])
98
103
  }
99
104
 
100
- @access(Access.get)
101
- @publicProperty('userInfo')
105
+ @publicProperty({
106
+ name: 'userInfo',
107
+ access: Access.get
108
+ })
102
109
  get info(): string {
103
110
  return `Name: '${this.name}'; Age: '${this.age}'; Money: '${this.money}';`
104
111
  }
@@ -125,8 +132,8 @@ Use **publicFunction** and **publicVariable**.
125
132
  // Main process
126
133
  import { publicFunction, publicVariable } from 'emr-bridge'
127
134
 
128
- publicFunction(sayHello, 'sayHello')
129
- publicFunction(getName, 'getUserName')
135
+ publicFunction('sayHello', sayHello)
136
+ publicFunction('getUserName', getName)
130
137
  publicVariable('count', {
131
138
  get: () => count,
132
139
  set: (value: number) => count = value
@@ -150,32 +157,37 @@ function getName(): string {
150
157
 
151
158
  ### Scopes
152
159
 
153
- In order to set a specific scope, use the **scope** decorator
160
+ In order to set a specific scope, use the **scope** property.
161
+
162
+ Accessing an entity outside the specified scope will result in an error being thrown.
154
163
 
155
164
  ```ts
156
165
  // Main process
157
166
 
158
167
  // static and non-static
159
- import { scope, Scope, providePublic, publicStaticMethod, publicMethod, publicProperty } from 'emr-bridge'
168
+ import { Scope, Access, providePublic, publicStaticMethod, publicMethod, publicProperty } from 'emr-bridge'
160
169
 
161
170
  class User {
162
171
  private static age = 20
163
172
  private name = 'Name'
164
-
165
- @scope(Scope.preload)
166
- @publicProperty()
173
+
174
+ @publicProperty({
175
+ scope: Scope.preload,
176
+ access: Access.get
177
+ })
167
178
  get count(): number {
168
179
  return 30
169
180
  }
170
-
171
- @scope(Scope.preload)
172
- @publicStaticMethod()
181
+
182
+ @publicStaticMethod({ scope: Scope.preload })
173
183
  static getAge(): number {
174
184
  return this.age
175
185
  }
176
-
177
- @scope(Scope.renderer)
178
- @publicMethod('getUserName')
186
+
187
+ @publicMethod({
188
+ name: 'getUserName',
189
+ scope: Scope.renderer
190
+ })
179
191
  getName(): string {
180
192
  return this.name
181
193
  }
@@ -186,7 +198,7 @@ providePublic(new User())
186
198
  // functions and variables
187
199
  import { publicFunction, publicVariable, Scope } from 'emr-bridge'
188
200
 
189
- publicFunction(getName, 'getName', [Scope.preload])
201
+ publicFunction('getName', getName, [Scope.preload])
190
202
  publicVariable('count', {
191
203
  get: () => count,
192
204
  set: (value: number) => count = value
@@ -208,20 +220,21 @@ Using an entity outside the specified scope _will cause an error_.
208
220
  // Main process
209
221
 
210
222
  // static and non-static
211
- import { publicStaticProperty, publicProperty, providePublic, access, Access } from 'emr-bridge'
223
+ import { publicStaticProperty, publicProperty, providePublic, Access } from 'emr-bridge'
212
224
 
213
225
  class User {
214
226
  private static name = 'Name'
215
227
  private static surname = 'Surname'
216
228
 
217
- @access(Access.get)
218
- @publicStaticProperty()
229
+ @publicStaticProperty({ access: Access.get })
219
230
  static get fullName(): string {
220
231
  return `${this.name} ${this.surname}`
221
232
  }
222
233
 
223
- @access(Access.get)
224
- @publicProperty('userAge')
234
+ @publicProperty({
235
+ name: 'userAge',
236
+ access: Access.get
237
+ })
225
238
  get age(): number {
226
239
  return 20
227
240
  }
@@ -281,3 +294,31 @@ bridge.getUserName()
281
294
  bridge.count--
282
295
  bridge.setUserAge(30)
283
296
  ```
297
+
298
+ ### Promises
299
+
300
+ The library also allows you to pass Promises from main to renderer
301
+
302
+ ```ts
303
+ // Main process
304
+ import { publicFunction } from 'emr-bridge'
305
+
306
+ publicFunction('delay1s', () => {
307
+ return new Promise<void>(resolve => {
308
+ setTimeout(resolve, 1000)
309
+ })
310
+ })
311
+ ```
312
+
313
+ ```ts
314
+ // Renderer process
315
+ import { Bridge } from 'emr-bridge/renderer'
316
+
317
+ interface IPublic {
318
+ delay1s(): Promise<void>
319
+ }
320
+
321
+ const bridge = Bridge.as<IPublic>()
322
+
323
+ bridge.delay1s().then(() => console.log('After 1 second'))
324
+ ```
package/lib/enums.d.ts CHANGED
@@ -7,9 +7,7 @@ export declare enum Scope {
7
7
  preload = "preload"
8
8
  }
9
9
  export declare enum IPCChannel {
10
- getPublicScopes = "GET_PUBLIC_SCOPES",
11
10
  getPublicInfo = "GET_PUBLIC_INFO",
12
- getPublicAccesses = "GET_PUBLIC_ACCESSES",
13
11
  functionCall = "FUNCTION_CALL_",
14
12
  propertyGet = "PROPERTY_GET_",
15
13
  propertySet = "PROPERTY_SET_",
package/lib/enums.js CHANGED
@@ -10,9 +10,7 @@ export var Scope;
10
10
  })(Scope || (Scope = {}));
11
11
  export var IPCChannel;
12
12
  (function (IPCChannel) {
13
- IPCChannel["getPublicScopes"] = "GET_PUBLIC_SCOPES";
14
13
  IPCChannel["getPublicInfo"] = "GET_PUBLIC_INFO";
15
- IPCChannel["getPublicAccesses"] = "GET_PUBLIC_ACCESSES";
16
14
  IPCChannel["functionCall"] = "FUNCTION_CALL_";
17
15
  IPCChannel["propertyGet"] = "PROPERTY_GET_";
18
16
  IPCChannel["propertySet"] = "PROPERTY_SET_";
package/lib/index.d.ts CHANGED
@@ -1,3 +1,3 @@
1
- export { scope, access, publicMethod, publicProperty, publicFunction, publicVariable, publicStaticMethod, publicStaticProperty, providePublic } from './publish';
1
+ export { publicMethod, publicProperty, publicFunction, publicVariable, publicStaticMethod, publicStaticProperty, providePublic } from './scripts/publish';
2
2
  export { Access, Scope } from './enums';
3
- export type { PublicFunction, PublicProperty } from './types';
3
+ export type { PublicFunction, PublicProperty, IPublishPropertyArgs, IPublishMethodArgs } from './types';
package/lib/index.js CHANGED
@@ -1,2 +1,2 @@
1
- export { scope, access, publicMethod, publicProperty, publicFunction, publicVariable, publicStaticMethod, publicStaticProperty, providePublic } from './publish';
1
+ export { publicMethod, publicProperty, publicFunction, publicVariable, publicStaticMethod, publicStaticProperty, providePublic } from './scripts/publish';
2
2
  export { Access, Scope } from './enums';
package/lib/preload.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- export { Main } from './main';
2
- export { provideFromMain } from './provider';
1
+ export { Main } from './scripts/Main';
2
+ export { provideFromMain } from './scripts/preloadProvider';
package/lib/preload.js CHANGED
@@ -1,2 +1,2 @@
1
- export { Main } from './main';
2
- export { provideFromMain } from './provider';
1
+ export { Main } from './scripts/Main';
2
+ export { provideFromMain } from './scripts/preloadProvider';
package/lib/renderer.d.ts CHANGED
@@ -1 +1 @@
1
- export { Bridge } from './bridge';
1
+ export { Bridge } from './scripts/Bridge';
package/lib/renderer.js CHANGED
@@ -1 +1 @@
1
- export { Bridge } from './bridge';
1
+ export { Bridge } from './scripts/Bridge';
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Bridge between `renderer` and `main` processes.
3
+ *
4
+ * _only for renderer process_
5
+ */
6
+ export declare const Bridge: import("./provider").ITarget;
@@ -0,0 +1,31 @@
1
+ import { createProvider } from './provider';
2
+ import { Scope } from '../enums';
3
+ if (!window.__provider__) {
4
+ throw new Error('Required methods not provided. Did you forget to call "provide" in preload?');
5
+ }
6
+ const provider = window.__provider__;
7
+ const info = provider.getInfo();
8
+ /**
9
+ * Bridge between `renderer` and `main` processes.
10
+ *
11
+ * _only for renderer process_
12
+ */
13
+ export const Bridge = createProvider({
14
+ info,
15
+ scope: Scope.renderer,
16
+ callFunction(name, ...args) {
17
+ return provider.provided.functions[name](...args);
18
+ },
19
+ getVariable(name) {
20
+ return provider.provided.properties[name];
21
+ },
22
+ setVariable(name, value) {
23
+ provider.provided.properties[name] = value;
24
+ return;
25
+ },
26
+ waitPromise(channel) {
27
+ return new Promise((resolve, reject) => {
28
+ provider.waitPromise(channel, resolve, reject);
29
+ });
30
+ }
31
+ });
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Bridge between `preload` and `main` processes.
3
+ *
4
+ * _only for preload process_
5
+ */
6
+ export declare const Main: import("./provider").ITarget;
@@ -0,0 +1,38 @@
1
+ import { ipcRenderer } from 'electron';
2
+ import { IPCChannel, Scope } from '../enums';
3
+ import { createProvider } from './provider';
4
+ const info = ipcRenderer.sendSync(IPCChannel.getPublicInfo);
5
+ if (!info) {
6
+ throw new Error('Public methods from main is not provided');
7
+ }
8
+ /**
9
+ * Bridge between `preload` and `main` processes.
10
+ *
11
+ * _only for preload process_
12
+ */
13
+ export const Main = createProvider({
14
+ info,
15
+ scope: Scope.preload,
16
+ callFunction(name, ...args) {
17
+ const channel = IPCChannel.functionCall + name;
18
+ return ipcRenderer.sendSync(channel, ...args);
19
+ },
20
+ getVariable(name) {
21
+ const channel = IPCChannel.propertyGet + name;
22
+ return ipcRenderer.sendSync(channel);
23
+ },
24
+ setVariable(name, value) {
25
+ const channel = IPCChannel.propertySet + name;
26
+ return ipcRenderer.sendSync(channel, value);
27
+ },
28
+ waitPromise(channel) {
29
+ return new Promise((resolve, reject) => {
30
+ ipcRenderer.once(channel, (_, result) => {
31
+ if (result.error)
32
+ reject(result.error);
33
+ else
34
+ resolve(result.value);
35
+ });
36
+ });
37
+ }
38
+ });
@@ -0,0 +1,51 @@
1
+ import { contextBridge, ipcRenderer } from 'electron';
2
+ import { IPCChannel } from '../enums';
3
+ const info = ipcRenderer.sendSync(IPCChannel.getPublicInfo);
4
+ /**
5
+ * Sets access to main from renderer
6
+ */
7
+ export function provideFromMain(contextIsolation = true) {
8
+ const provider = {
9
+ getInfo: () => info,
10
+ waitPromise(channel, resolve, reject) {
11
+ ipcRenderer.once(channel, (_, result) => {
12
+ if (result.error)
13
+ reject?.(result.error);
14
+ else
15
+ resolve(result.value);
16
+ });
17
+ },
18
+ provided: {
19
+ properties: {},
20
+ functions: {}
21
+ }
22
+ };
23
+ info.functions.forEach(funcName => {
24
+ Object.defineProperty(provider.provided.functions, funcName, {
25
+ value: (...args) => {
26
+ const channel = IPCChannel.functionCall + funcName;
27
+ return ipcRenderer.sendSync(channel, ...args);
28
+ },
29
+ enumerable: true
30
+ });
31
+ });
32
+ info.properties.forEach(propName => {
33
+ Object.defineProperty(provider.provided.properties, propName, {
34
+ get: () => {
35
+ const channel = IPCChannel.propertyGet + propName;
36
+ return ipcRenderer.sendSync(channel);
37
+ },
38
+ set: value => {
39
+ const channel = IPCChannel.propertySet + propName;
40
+ ipcRenderer.sendSync(channel, value);
41
+ },
42
+ enumerable: true
43
+ });
44
+ });
45
+ if (contextIsolation) {
46
+ contextBridge.exposeInMainWorld('__provider__', provider);
47
+ }
48
+ else {
49
+ window.__provider__ = provider;
50
+ }
51
+ }
@@ -0,0 +1,5 @@
1
+ import type { ICreateProviderArgs } from '../types';
2
+ export interface ITarget {
3
+ as<T>(): T;
4
+ }
5
+ export declare function createProvider(args: ICreateProviderArgs): ITarget;
@@ -0,0 +1,111 @@
1
+ import { Access, Scope } from '../enums';
2
+ function errorHandler(error, channel) {
3
+ console.error(`Error on ${channel}.\n${error}`.replace('Error: ', ''));
4
+ }
5
+ export function createProvider(args) {
6
+ const { info, scope, getVariable, setVariable, callFunction, waitPromise } = args;
7
+ const target = {
8
+ as: () => proxy
9
+ };
10
+ info.functions.forEach(funcName => {
11
+ if (info.scopes[funcName].has(scope)) {
12
+ Object.defineProperty(target, funcName, {
13
+ get: () => (...args) => {
14
+ const result = callFunction(funcName, ...args);
15
+ if (result.error) {
16
+ errorHandler(result.error, funcName);
17
+ return;
18
+ }
19
+ if (result.promiseChannel) {
20
+ return waitPromise(result.promiseChannel);
21
+ }
22
+ return result.value;
23
+ },
24
+ set: () => {
25
+ throw new Error(`Function '${funcName}' is readonly`);
26
+ },
27
+ enumerable: true
28
+ });
29
+ }
30
+ });
31
+ info.properties.forEach(propName => {
32
+ if (info.scopes[propName].has(scope)) {
33
+ let getter = () => {
34
+ const result = getVariable(propName);
35
+ if (result.error) {
36
+ errorHandler(result.error, propName);
37
+ return;
38
+ }
39
+ return result.value;
40
+ };
41
+ let setter = (value) => {
42
+ const result = setVariable(propName, value);
43
+ if (result?.error) {
44
+ errorHandler(result.error, propName);
45
+ return;
46
+ }
47
+ };
48
+ if (!info.accesses[propName].has(Access.get)) {
49
+ getter = () => {
50
+ throw new Error(`No access to getting the '${propName}' property`);
51
+ };
52
+ }
53
+ if (!info.accesses[propName].has(Access.set)) {
54
+ setter = () => {
55
+ throw new Error(`No access to setting the '${propName}' property`);
56
+ };
57
+ }
58
+ Object.defineProperty(target, propName, {
59
+ get: getter,
60
+ set: setter,
61
+ enumerable: true
62
+ });
63
+ }
64
+ });
65
+ const proxy = new Proxy(target, {
66
+ get(target, key) {
67
+ if (key in target)
68
+ return target[key];
69
+ const isFunction = info.functions.has(key);
70
+ const isProperty = info.properties.has(key);
71
+ if (!isFunction && !isProperty) {
72
+ throw new Error(`'${key}' is not provided from main`);
73
+ }
74
+ if (!info.scopes[key].has(Scope.renderer)) {
75
+ throw new Error(`'${key}' is not provided into renderer scope`);
76
+ }
77
+ if (isProperty && !info.accesses[key].has(Access.get)) {
78
+ throw new Error(`No access to getting the '${key}' property`);
79
+ }
80
+ throw new Error(`Unknown error on get '${key}'`);
81
+ },
82
+ set(target, key, newValue) {
83
+ if (key in target) {
84
+ target[key] = newValue;
85
+ return true;
86
+ }
87
+ const isFunction = info.functions.has(key);
88
+ const isProperty = info.properties.has(key);
89
+ if (isFunction) {
90
+ throw new Error(`Function '${key}' is readonly`);
91
+ }
92
+ if (!isProperty) {
93
+ throw new Error(`Property '${key}' is not provided from main`);
94
+ }
95
+ if (!info.scopes[key].has(Scope.renderer)) {
96
+ throw new Error(`'${key}' is not provided into renderer scope`);
97
+ }
98
+ if (!info.accesses[key].has(Access.set)) {
99
+ throw new Error(`No access to setting the '${key}' property`);
100
+ }
101
+ throw new Error(`Unknown error on set '${key}'`);
102
+ },
103
+ ownKeys(target) {
104
+ return Object.keys(target);
105
+ },
106
+ has(target, key) {
107
+ return key in target;
108
+ }
109
+ });
110
+ return proxy;
111
+ }
@@ -0,0 +1,118 @@
1
+ import { Scope } from '../enums';
2
+ import type { IPublishMethodArgs, IPublishPropertyArgs, PublicFunction, PublicProperty } from '../types';
3
+ /**
4
+ * Makes the method available for **renderer** and **preload** processes.
5
+ *
6
+ * _only for static_
7
+ */
8
+ export declare function publicStaticMethod(): MethodDecorator;
9
+ /**
10
+ * Makes the method available for **renderer** and **preload** processes.
11
+ *
12
+ * _only for static_
13
+ * @param name - name by which the method will be accessed
14
+ */
15
+ export declare function publicStaticMethod(name: string): MethodDecorator;
16
+ /**
17
+ * Makes the method available for **renderer** and **preload** processes.
18
+ *
19
+ * _only for static_
20
+ * @param args - publish args
21
+ */
22
+ export declare function publicStaticMethod(args: IPublishMethodArgs): MethodDecorator;
23
+ /**
24
+ * Makes the method available for **renderer** and **preload** processes.
25
+ *
26
+ * _only for non-static_
27
+ *
28
+ * _required `providePublic(classInstance)`_
29
+ */
30
+ export declare function publicMethod(): MethodDecorator;
31
+ /**
32
+ * Makes the method available for **renderer** and **preload** processes.
33
+ *
34
+ * _only for non-static_
35
+ *
36
+ * _required `providePublic(classInstance)`_
37
+ * @param name - name by which the method will be accessed
38
+ */
39
+ export declare function publicMethod(name: string): MethodDecorator;
40
+ /**
41
+ * Makes the method available for **renderer** and **preload** processes.
42
+ *
43
+ * _only for non-static_
44
+ *
45
+ * _required `providePublic(classInstance)`_
46
+ * @param args - publish args
47
+ */
48
+ export declare function publicMethod(args: IPublishMethodArgs): MethodDecorator;
49
+ /**
50
+ * Makes the property available for **renderer** and **preload** processes.
51
+ *
52
+ * _only for static_
53
+ */
54
+ export declare function publicStaticProperty(): PropertyDecorator;
55
+ /**
56
+ * Makes the property available for **renderer** and **preload** processes.
57
+ *
58
+ * _only for static_
59
+ * @param name - name by which the property will be accessed
60
+ */
61
+ export declare function publicStaticProperty(name: string): PropertyDecorator;
62
+ /**
63
+ * Makes the property available for **renderer** and **preload** processes.
64
+ *
65
+ * _only for static_
66
+ * @param args - publish args
67
+ */
68
+ export declare function publicStaticProperty(args: IPublishPropertyArgs): PropertyDecorator;
69
+ /**
70
+ * Makes the property available for **renderer** and **preload** processes.
71
+ *
72
+ * _only for non-static_
73
+ *
74
+ * _required `providePublic(classInstance)`_
75
+ */
76
+ export declare function publicProperty(): PropertyDecorator;
77
+ /**
78
+ * Makes the property available for **renderer** and **preload** processes.
79
+ *
80
+ * _only for non-static_
81
+ *
82
+ * _required `providePublic(classInstance)`_
83
+ * @param name - name by which the property will be accessed
84
+ */
85
+ export declare function publicProperty(name: string): PropertyDecorator;
86
+ /**
87
+ * Makes the property available for **renderer** and **preload** processes.
88
+ *
89
+ * _only for non-static_
90
+ *
91
+ * _required `providePublic(classInstance)`_
92
+ * @param args - publish args
93
+ */
94
+ export declare function publicProperty(args: IPublishPropertyArgs): PropertyDecorator;
95
+ /**
96
+ * Binds all public entities of a class to a specific instance.
97
+ *
98
+ * _only for class instance_
99
+ */
100
+ export declare function providePublic<InstanceType>(instance: InstanceType): InstanceType;
101
+ /**
102
+ * Makes the function available for **renderer** and **preload** processes.
103
+ *
104
+ * _only for function_
105
+ * @param name - name by which the function will be accessed
106
+ * @param func - linked function
107
+ * @param scopes - function scopes
108
+ */
109
+ export declare function publicFunction(name: string, func: PublicFunction, scopes?: Scope[]): void;
110
+ /**
111
+ * Makes the function available for **renderer** and **preload** processes.
112
+ *
113
+ * _only for variables_
114
+ * @param name - name by which the variable will be accessed
115
+ * @param value - variable getter/setter
116
+ * @param scopes - variable scopes
117
+ */
118
+ export declare function publicVariable(name: string, value: PublicProperty, scopes?: Scope[]): void;