emr-bridge 1.0.0 → 1.1.1

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.
@@ -1,89 +1,70 @@
1
1
  import { ipcMain } from 'electron';
2
- import { Access, IPCChannel, Scope } from './enums';
3
- const publicScopes = {};
4
- const publicAccesses = {};
2
+ import { Access, IPCChannel, Scope } from '../enums';
5
3
  const publicInfo = {
6
4
  properties: new Set(),
7
- functions: new Set()
5
+ functions: new Set(),
6
+ accesses: {},
7
+ scopes: {}
8
8
  };
9
9
  ipcMain.on(IPCChannel.getPublicInfo, e => e.returnValue = publicInfo);
10
- ipcMain.on(IPCChannel.getPublicScopes, e => e.returnValue = publicScopes);
11
- ipcMain.on(IPCChannel.getPublicAccesses, e => e.returnValue = publicAccesses);
12
- /**
13
- * Restricts the access scope
14
- */
15
- export function scope(scope) {
10
+ export function publicStaticMethod(arg) {
16
11
  return (target, key) => {
17
- publicScopes[key] = new Set([scope]);
18
- };
19
- }
20
- /**
21
- * Restricts access
22
- */
23
- export function access(access) {
24
- return (target, key) => {
25
- publicAccesses[key] = new Set([access]);
26
- };
27
- }
28
- /**
29
- * Makes the method available for **renderer** and **preload** processes.
30
- *
31
- * _only for static_
32
- * @param name - name by which the method will be accessed
33
- */
34
- export function publicStaticMethod(name) {
35
- return (target, key) => {
36
- publicFunction((...args) => target[key](...args), String(name ?? key));
12
+ let name = String(key);
13
+ if (arg) {
14
+ if (typeof arg === 'string') {
15
+ name = arg;
16
+ }
17
+ else {
18
+ name = arg.name ?? String(key);
19
+ if (arg.scope)
20
+ scope(name, arg.scope);
21
+ }
22
+ }
23
+ publicFunction(name, (...args) => target[key](...args));
37
24
  };
38
25
  }
39
- /**
40
- * Makes the method available for **renderer** and **preload** processes.
41
- *
42
- * _only for non-static_
43
- *
44
- * _required `providePublic(classInstance)`_
45
- * @param name - name by which the method will be accessed
46
- */
47
- export function publicMethod(name) {
26
+ export function publicMethod(arg) {
48
27
  return (tgt, key, _) => {
49
28
  const target = tgt;
50
29
  const prevRegister = target.__register__ ?? (() => { });
51
30
  target.__register__ = instance => {
52
31
  prevRegister(instance);
53
- publicStaticMethod(name)(instance, key, _);
32
+ publicStaticMethod(arg)(instance, key, _);
54
33
  };
55
34
  };
56
35
  }
57
- /**
58
- * Makes the property available for **renderer** and **preload** processes.
59
- *
60
- * _only for static_
61
- * @param name - name by which the property will be accessed
62
- */
63
- export function publicStaticProperty(name) {
64
- return (target, k) => {
65
- const key = String(name ?? k);
66
- publicVariable(key, {
67
- get: () => target[k],
68
- set: value => target[k] = value
36
+ export function publicStaticProperty(arg) {
37
+ return (target, key) => {
38
+ let name = String(key);
39
+ if (arg) {
40
+ if (typeof arg === 'string') {
41
+ name = arg;
42
+ }
43
+ else {
44
+ name = arg.name ?? String(key);
45
+ if (arg.scope)
46
+ scope(name, arg.scope);
47
+ if (arg.access)
48
+ access(name, arg.access);
49
+ }
50
+ }
51
+ publicVariable(name, {
52
+ get() {
53
+ return target[key];
54
+ },
55
+ set(value) {
56
+ target[key] = value;
57
+ }
69
58
  });
70
59
  };
71
60
  }
72
- /**
73
- * Makes the property available for **renderer** and **preload** processes.
74
- *
75
- * _only for non-static_
76
- *
77
- * _required `providePublic(classInstance)`_
78
- * @param name - name by which the property will be accessed
79
- */
80
- export function publicProperty(name) {
61
+ export function publicProperty(arg) {
81
62
  return (tgt, key) => {
82
63
  const target = tgt;
83
64
  const prevRegister = target.__register__ ?? (() => { });
84
65
  target.__register__ = instance => {
85
66
  prevRegister(instance);
86
- publicStaticProperty(name)(instance, key);
67
+ publicStaticProperty(arg)(instance, key);
87
68
  };
88
69
  };
89
70
  }
@@ -94,9 +75,8 @@ export function publicProperty(name) {
94
75
  */
95
76
  export function providePublic(instance) {
96
77
  const target = instance;
97
- if (!target.__register__) {
78
+ if (!target.__register__)
98
79
  throw new Error('Public methods not defined');
99
- }
100
80
  target.__register__(instance);
101
81
  target.__register__ = undefined;
102
82
  return instance;
@@ -105,23 +85,22 @@ export function providePublic(instance) {
105
85
  * Makes the function available for **renderer** and **preload** processes.
106
86
  *
107
87
  * _only for function_
108
- * @param func - linked function
109
88
  * @param name - name by which the function will be accessed
89
+ * @param func - linked function
110
90
  * @param scopes - function scopes
111
91
  */
112
- export function publicFunction(func, name, scopes = [Scope.preload, Scope.renderer]) {
92
+ export function publicFunction(name, func, scopes = [Scope.preload, Scope.renderer]) {
113
93
  const { functions } = publicInfo;
114
94
  const channel = IPCChannel.functionCall + name;
115
95
  functions.add(name);
116
- if (!publicScopes[name]) {
117
- publicScopes[name] = new Set(scopes);
118
- }
96
+ if (!publicInfo.scopes[name])
97
+ publicInfo.scopes[name] = new Set(scopes);
119
98
  ipcMain.removeAllListeners(channel);
120
99
  ipcMain.on(channel, (e, ...args) => {
121
100
  try {
122
101
  const result = func(...args);
123
102
  if (result instanceof Promise) {
124
- const promiseChannel = `${channel}${IPCChannel.promisePostfix}`;
103
+ const promiseChannel = channel + IPCChannel.promisePostfix;
125
104
  result
126
105
  .then(value => e.sender.send(promiseChannel, { value }))
127
106
  .catch(reason => e.sender.send(promiseChannel, { error: String(reason) }));
@@ -150,14 +129,13 @@ export function publicVariable(name, value, scopes = [Scope.renderer, Scope.prel
150
129
  const setChannel = IPCChannel.propertySet + name;
151
130
  const { get: getter, set: setter } = value;
152
131
  properties.add(name);
153
- if (!publicScopes[name]) {
154
- publicScopes[name] = new Set(scopes);
155
- }
156
- if (!publicAccesses[name]) {
132
+ if (!publicInfo.scopes[name])
133
+ publicInfo.scopes[name] = new Set(scopes);
134
+ if (!publicInfo.accesses[name]) {
157
135
  const accesses = [];
158
136
  getter && accesses.push(Access.get);
159
137
  setter && accesses.push(Access.set);
160
- publicAccesses[name] = new Set(accesses);
138
+ publicInfo.accesses[name] = new Set(accesses);
161
139
  }
162
140
  if (getter) {
163
141
  ipcMain.removeAllListeners(getChannel);
@@ -184,3 +162,9 @@ export function publicVariable(name, value, scopes = [Scope.renderer, Scope.prel
184
162
  });
185
163
  }
186
164
  }
165
+ function scope(key, scope) {
166
+ publicInfo.scopes[key] = new Set([scope]);
167
+ }
168
+ function access(key, access) {
169
+ publicInfo.accesses[key] = new Set([access]);
170
+ }
package/lib/types.d.ts CHANGED
@@ -1,39 +1,51 @@
1
1
  import type { Scope } from './enums';
2
2
  import { Access } from './enums';
3
+ export interface ITarget {
4
+ as<T>(): T;
5
+ }
3
6
  export interface IIPCResult {
4
7
  value?: any;
5
8
  error?: string;
6
9
  promiseChannel?: string;
7
10
  }
8
- export interface IScopes {
9
- [name: string]: Set<Scope>;
11
+ export interface IPublishMethodArgs {
12
+ name?: string;
13
+ scope?: Scope;
14
+ }
15
+ export interface IPublishPropertyArgs {
16
+ name?: string;
17
+ scope?: Scope;
18
+ access?: Access;
10
19
  }
11
- export interface IAccesses {
12
- [name: string]: Set<Access>;
20
+ export interface ICreateProviderArgs {
21
+ info: IInfo;
22
+ scope: Scope;
23
+ callFunction(name: string, ...args: any[]): IIPCResult;
24
+ getVariable(name: string): IIPCResult;
25
+ setVariable(name: string, value: any): IIPCResult | undefined;
26
+ waitPromise(channel: string): Promise<any>;
13
27
  }
28
+ export type Scopes = Record<string, Set<Scope>>;
29
+ export type Accesses = Record<string, Set<Access>>;
14
30
  export interface IRendererPublic {
15
31
  __register__?(instance: any): void;
16
32
  }
17
33
  export interface IProvider {
18
34
  getInfo(): IInfo;
19
- getScopes(): IScopes;
20
- getAccesses(): IAccesses;
21
35
  waitPromise(name: string, resolve: (value: any) => void, reject?: (reason?: any) => void): void;
22
36
  provided: {
23
- functions: {
24
- [key: string]: (...args: any[]) => IIPCResult;
25
- };
26
- properties: {
27
- [key: string]: IIPCResult;
28
- };
37
+ functions: Record<string, (...args: any[]) => IIPCResult>;
38
+ properties: Record<string, IIPCResult>;
29
39
  };
30
40
  }
31
- export declare type PublicFunction = (...args: any[]) => any;
32
- export declare type PublicProperty = {
41
+ export type PublicFunction = (...args: any[]) => any;
42
+ export type PublicProperty = {
33
43
  get?(): any;
34
44
  set?(value: any): void;
35
45
  };
36
46
  export interface IInfo {
37
47
  properties: Set<string>;
38
48
  functions: Set<string>;
49
+ scopes: Scopes;
50
+ accesses: Accesses;
39
51
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "emr-bridge",
3
- "version": "1.0.0",
3
+ "version": "1.1.1",
4
4
  "description": "Allows you to easily give access to main methods from the renderer process",
5
5
  "main": "lib/index.js",
6
6
  "scripts": {
@@ -43,8 +43,10 @@
43
43
  ],
44
44
  "author": "VerZsuT",
45
45
  "license": "MIT",
46
+ "dependencies": {
47
+ "electron": "^21.3.0"
48
+ },
46
49
  "devDependencies": {
47
- "electron": "^21.0.1",
48
- "typescript": "^4.8.4"
50
+ "typescript": "^4.9.3"
49
51
  }
50
52
  }
package/lib/bridge.d.ts DELETED
@@ -1,10 +0,0 @@
1
- declare const target: {
2
- as: <T>() => T;
3
- };
4
- /**
5
- * Bridge between `renderer` and `main` processes.
6
- *
7
- * _only for renderer process_
8
- */
9
- export declare const Bridge: typeof target;
10
- export {};
package/lib/bridge.js DELETED
@@ -1,98 +0,0 @@
1
- import { Access, Scope } from './enums';
2
- if (!window.__provider__) {
3
- throw new Error('Required methods not provided. Did you forget to call "provide" in preload?');
4
- }
5
- const provider = window.__provider__;
6
- const info = provider.getInfo();
7
- const scopes = provider.getScopes();
8
- const accesses = provider.getAccesses();
9
- function errorHandler(error, channel) {
10
- console.error(`Error on ${channel}.\n${error}`.replace('Error: ', ''));
11
- }
12
- const target = {
13
- as: () => Bridge
14
- };
15
- /**
16
- * Bridge between `renderer` and `main` processes.
17
- *
18
- * _only for renderer process_
19
- */
20
- export const Bridge = new Proxy(target, {
21
- get(target, key) {
22
- if (key in target)
23
- return target[key];
24
- const isFunction = info.functions.has(key);
25
- const isProperty = info.properties.has(key);
26
- if (!isFunction && !isProperty) {
27
- throw new Error(`'${key}' is not provided from main`);
28
- }
29
- if (!scopes[key].has(Scope.renderer)) {
30
- throw new Error(`'${key}' is not provided into renderer scope`);
31
- }
32
- if (isFunction) {
33
- return (...args) => {
34
- const result = provider.provided.functions[key](...args);
35
- if (result.error) {
36
- errorHandler(result.error, key);
37
- return;
38
- }
39
- if (result.promiseChannel) {
40
- return new Promise((resolve, reject) => {
41
- provider.waitPromise(key, resolve, reject);
42
- });
43
- }
44
- return result.value;
45
- };
46
- }
47
- if (isProperty) {
48
- if (!accesses[key].has(Access.get)) {
49
- throw new Error(`No access to getting the '${key}' property`);
50
- }
51
- const result = provider.provided.properties[key];
52
- if (result.error) {
53
- errorHandler(result.error, key);
54
- return;
55
- }
56
- return result.value;
57
- }
58
- },
59
- set(target, key, newValue) {
60
- const isFunction = info.functions.has(key);
61
- const isProperty = info.properties.has(key);
62
- if (isFunction) {
63
- throw new Error(`Function '${key}' is readonly`);
64
- }
65
- if (!isProperty) {
66
- throw new Error(`Property '${key}' is not provided from main`);
67
- }
68
- if (!scopes[key].has(Scope.renderer)) {
69
- throw new Error(`'${key}' is not provided into renderer scope`);
70
- }
71
- if (!accesses[key].has(Access.set)) {
72
- throw new Error(`No access to setting the '${key}' property`);
73
- }
74
- provider.provided.properties[key] = newValue;
75
- return true;
76
- },
77
- ownKeys(target) {
78
- const keys = [];
79
- for (const key of [...info.functions, ...info.properties]) {
80
- if (scopes[key].has(Scope.renderer)) {
81
- keys.push(key);
82
- }
83
- }
84
- for (const key in target) {
85
- keys.push(key);
86
- }
87
- return keys;
88
- },
89
- has(target, key) {
90
- const keys = [];
91
- for (const key of [...info.functions, ...info.properties]) {
92
- if (scopes[key].has(Scope.renderer)) {
93
- keys.push(key);
94
- }
95
- }
96
- return (key in target) || keys.includes(key);
97
- }
98
- });
package/lib/main.d.ts DELETED
@@ -1,10 +0,0 @@
1
- declare const target: {
2
- as: <T>() => T;
3
- };
4
- /**
5
- * Bridge between `preload` and `main` processes.
6
- *
7
- * _only for preload process_
8
- */
9
- export declare const Main: typeof target;
10
- export {};
package/lib/main.js DELETED
@@ -1,110 +0,0 @@
1
- import { ipcRenderer } from 'electron';
2
- import { Access, IPCChannel, Scope } from './enums';
3
- const info = ipcRenderer.sendSync(IPCChannel.getPublicInfo);
4
- const scopes = ipcRenderer.sendSync(IPCChannel.getPublicScopes);
5
- const accesses = ipcRenderer.sendSync(IPCChannel.getPublicAccesses);
6
- if (!info) {
7
- throw new Error('Public methods from main is not provided');
8
- }
9
- function errorHandler(error, channel) {
10
- console.error(`Error on ${channel}.\n${error}`.replace('Error: ', ''));
11
- }
12
- const target = {
13
- as: () => Main
14
- };
15
- /**
16
- * Bridge between `preload` and `main` processes.
17
- *
18
- * _only for preload process_
19
- */
20
- export const Main = new Proxy(target, {
21
- get(target, key) {
22
- if (key in target)
23
- return target[key];
24
- const isFunction = info.functions.has(key);
25
- const isProperty = info.properties.has(key);
26
- if (!isFunction && !isProperty) {
27
- throw new Error(`'${key}' is not provided from main`);
28
- }
29
- if (!scopes[key].has(Scope.preload)) {
30
- throw new Error(`'${key}' is not provided into preload scope`);
31
- }
32
- if (isFunction) {
33
- return (...args) => {
34
- const channel = IPCChannel.functionCall + key;
35
- const result = ipcRenderer.sendSync(channel, ...args);
36
- if (result.error) {
37
- errorHandler(result.error, key);
38
- return;
39
- }
40
- if (result.promiseChannel) {
41
- return new Promise((resolve, reject) => {
42
- ipcRenderer.once(result.promiseChannel, (_, result) => {
43
- if (result.error)
44
- reject(result.error);
45
- else
46
- resolve(result.value);
47
- });
48
- });
49
- }
50
- return result.value;
51
- };
52
- }
53
- if (isProperty) {
54
- if (!accesses[key].has(Access.get)) {
55
- throw new Error(`No access to getting the '${key}' property`);
56
- }
57
- const channel = IPCChannel.propertyGet + key;
58
- const getterResult = ipcRenderer.sendSync(channel);
59
- if (getterResult.error) {
60
- errorHandler(getterResult.error, key);
61
- return;
62
- }
63
- return getterResult.value;
64
- }
65
- },
66
- set(target, key, newValue) {
67
- const isFunction = info.functions.has(key);
68
- const isProperty = info.properties.has(key);
69
- if (isFunction) {
70
- throw new Error(`Function '${key}' is readonly`);
71
- }
72
- if (!isProperty) {
73
- throw new Error(`Property '${key}' is not provided from main`);
74
- }
75
- if (!scopes[key].has(Scope.preload)) {
76
- throw new Error(`'${key}' is not provided into preload scope`);
77
- }
78
- if (!accesses[key].has(Access.set)) {
79
- throw new Error(`No access to setting the '${key}' property`);
80
- }
81
- const channel = IPCChannel.propertySet + key;
82
- const setterResult = ipcRenderer.sendSync(channel, newValue);
83
- if (setterResult.error) {
84
- errorHandler(setterResult.error, key);
85
- return false;
86
- }
87
- return true;
88
- },
89
- ownKeys(target) {
90
- const keys = [];
91
- for (const key of [...info.functions, ...info.properties]) {
92
- if (scopes[key].has(Scope.preload)) {
93
- keys.push(key);
94
- }
95
- }
96
- for (const key in target) {
97
- keys.push(key);
98
- }
99
- return keys;
100
- },
101
- has(target, key) {
102
- const keys = [];
103
- for (const key of [...info.functions, ...info.properties]) {
104
- if (scopes[key].has(Scope.preload)) {
105
- keys.push(key);
106
- }
107
- }
108
- return (key in target) || keys.includes(key);
109
- }
110
- });
package/lib/provider.js DELETED
@@ -1,63 +0,0 @@
1
- import { contextBridge, ipcRenderer } from 'electron';
2
- import { Main } from './main';
3
- import { IPCChannel } from './enums';
4
- const info = ipcRenderer.sendSync(IPCChannel.getPublicInfo);
5
- const scopes = ipcRenderer.sendSync(IPCChannel.getPublicScopes);
6
- const accesses = ipcRenderer.sendSync(IPCChannel.getPublicAccesses);
7
- const main = Main.as();
8
- /**
9
- * Sets access to main from renderer
10
- */
11
- export function provideFromMain(contextIsolation = true) {
12
- const promises = {};
13
- const provider = {
14
- getInfo: () => info,
15
- getScopes: () => scopes,
16
- getAccesses: () => accesses,
17
- waitPromise(name, resolve, reject) {
18
- promises[name] = { resolve, reject };
19
- },
20
- provided: {
21
- properties: {},
22
- functions: {}
23
- }
24
- };
25
- info.functions.forEach(funcName => {
26
- Object.defineProperty(provider.provided.functions, funcName, {
27
- value: (...args) => {
28
- const result = main[funcName](...args);
29
- if (result instanceof Promise) {
30
- result
31
- .then(value => {
32
- if (!promises[funcName])
33
- return;
34
- promises[funcName].resolve(value);
35
- })
36
- .catch(reason => {
37
- if (!promises[funcName])
38
- return;
39
- promises[funcName].reject?.(reason);
40
- });
41
- return { promiseChannel: funcName };
42
- }
43
- else {
44
- return { value: result };
45
- }
46
- },
47
- enumerable: true
48
- });
49
- });
50
- info.properties.forEach(propName => {
51
- Object.defineProperty(provider.provided.properties, propName, {
52
- get: () => ({ value: main[propName] }),
53
- set: value => main[propName] = value,
54
- enumerable: true
55
- });
56
- });
57
- if (contextIsolation) {
58
- contextBridge.exposeInMainWorld('__provider__', provider);
59
- }
60
- else {
61
- window.__provider__ = provider;
62
- }
63
- }
package/lib/publish.d.ts DELETED
@@ -1,66 +0,0 @@
1
- import { Access, Scope } from './enums';
2
- import type { PublicFunction, PublicProperty } from './types';
3
- /**
4
- * Restricts the access scope
5
- */
6
- export declare function scope(scope: Scope): (target: any, key: string) => void;
7
- /**
8
- * Restricts access
9
- */
10
- export declare function access(access: Access): (target: any, key: string) => void;
11
- /**
12
- * Makes the method available for **renderer** and **preload** processes.
13
- *
14
- * _only for static_
15
- * @param name - name by which the method will be accessed
16
- */
17
- export declare function publicStaticMethod(name?: string): MethodDecorator;
18
- /**
19
- * Makes the method available for **renderer** and **preload** processes.
20
- *
21
- * _only for non-static_
22
- *
23
- * _required `providePublic(classInstance)`_
24
- * @param name - name by which the method will be accessed
25
- */
26
- export declare function publicMethod(name?: string): MethodDecorator;
27
- /**
28
- * Makes the property available for **renderer** and **preload** processes.
29
- *
30
- * _only for static_
31
- * @param name - name by which the property will be accessed
32
- */
33
- export declare function publicStaticProperty(name?: string): PropertyDecorator;
34
- /**
35
- * Makes the property available for **renderer** and **preload** processes.
36
- *
37
- * _only for non-static_
38
- *
39
- * _required `providePublic(classInstance)`_
40
- * @param name - name by which the property will be accessed
41
- */
42
- export declare function publicProperty(name?: string): PropertyDecorator;
43
- /**
44
- * Binds all public entities of a class to a specific instance.
45
- *
46
- * _only for class instance_
47
- */
48
- export declare function providePublic<InstanceType>(instance: InstanceType): InstanceType;
49
- /**
50
- * Makes the function available for **renderer** and **preload** processes.
51
- *
52
- * _only for function_
53
- * @param func - linked function
54
- * @param name - name by which the function will be accessed
55
- * @param scopes - function scopes
56
- */
57
- export declare function publicFunction(func: PublicFunction, name: string, scopes?: Scope[]): void;
58
- /**
59
- * Makes the function available for **renderer** and **preload** processes.
60
- *
61
- * _only for variables_
62
- * @param name - name by which the variable will be accessed
63
- * @param value - variable getter/setter
64
- * @param scopes - variable scopes
65
- */
66
- export declare function publicVariable(name: string, value: PublicProperty, scopes?: Scope[]): void;