emr-bridge 1.2.0 → 1.2.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.
package/cjs/enums.d.ts ADDED
@@ -0,0 +1,15 @@
1
+ export declare enum Access {
2
+ get = "get",
3
+ set = "set"
4
+ }
5
+ export declare enum Scope {
6
+ renderer = "renderer",
7
+ preload = "preload"
8
+ }
9
+ export declare enum IPCChannel {
10
+ getPublicInfo = "GET_PUBLIC_INFO",
11
+ functionCall = "FUNCTION_CALL_",
12
+ propertyGet = "PROPERTY_GET_",
13
+ propertySet = "PROPERTY_SET_",
14
+ promisePostfix = "_PROMISE"
15
+ }
package/cjs/enums.js ADDED
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.IPCChannel = exports.Scope = exports.Access = void 0;
4
+ var Access;
5
+ (function (Access) {
6
+ Access["get"] = "get";
7
+ Access["set"] = "set";
8
+ })(Access = exports.Access || (exports.Access = {}));
9
+ var Scope;
10
+ (function (Scope) {
11
+ Scope["renderer"] = "renderer";
12
+ Scope["preload"] = "preload";
13
+ })(Scope = exports.Scope || (exports.Scope = {}));
14
+ var IPCChannel;
15
+ (function (IPCChannel) {
16
+ IPCChannel["getPublicInfo"] = "GET_PUBLIC_INFO";
17
+ IPCChannel["functionCall"] = "FUNCTION_CALL_";
18
+ IPCChannel["propertyGet"] = "PROPERTY_GET_";
19
+ IPCChannel["propertySet"] = "PROPERTY_SET_";
20
+ IPCChannel["promisePostfix"] = "_PROMISE";
21
+ })(IPCChannel = exports.IPCChannel || (exports.IPCChannel = {}));
package/cjs/index.d.ts ADDED
@@ -0,0 +1,5 @@
1
+ export { Access, Scope } from './enums';
2
+ export { publicGetter, publicMethod, publicProperty, publicSetter } from './scripts/newDecorators';
3
+ export { providePublic, publicMethod as publicMethodExp, publicProperty as publicPropertyExp, publicStaticMethod as publicStaticMethodExp, publicStaticProperty as publicStaticPropertyExp } from './scripts/oldDecorators';
4
+ export { publicFunction, publicVariable } from './scripts/publish';
5
+ export type { IPublishMethodArgs, IPublishPropertyArgs, PublicFunction, PublicProperty } from './types';
package/cjs/index.js ADDED
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.publicVariable = exports.publicFunction = exports.publicStaticPropertyExp = exports.publicStaticMethodExp = exports.publicPropertyExp = exports.publicMethodExp = exports.providePublic = exports.publicSetter = exports.publicProperty = exports.publicMethod = exports.publicGetter = exports.Scope = exports.Access = void 0;
4
+ var enums_1 = require("./enums");
5
+ Object.defineProperty(exports, "Access", { enumerable: true, get: function () { return enums_1.Access; } });
6
+ Object.defineProperty(exports, "Scope", { enumerable: true, get: function () { return enums_1.Scope; } });
7
+ var newDecorators_1 = require("./scripts/newDecorators");
8
+ Object.defineProperty(exports, "publicGetter", { enumerable: true, get: function () { return newDecorators_1.publicGetter; } });
9
+ Object.defineProperty(exports, "publicMethod", { enumerable: true, get: function () { return newDecorators_1.publicMethod; } });
10
+ Object.defineProperty(exports, "publicProperty", { enumerable: true, get: function () { return newDecorators_1.publicProperty; } });
11
+ Object.defineProperty(exports, "publicSetter", { enumerable: true, get: function () { return newDecorators_1.publicSetter; } });
12
+ var oldDecorators_1 = require("./scripts/oldDecorators");
13
+ Object.defineProperty(exports, "providePublic", { enumerable: true, get: function () { return oldDecorators_1.providePublic; } });
14
+ Object.defineProperty(exports, "publicMethodExp", { enumerable: true, get: function () { return oldDecorators_1.publicMethod; } });
15
+ Object.defineProperty(exports, "publicPropertyExp", { enumerable: true, get: function () { return oldDecorators_1.publicProperty; } });
16
+ Object.defineProperty(exports, "publicStaticMethodExp", { enumerable: true, get: function () { return oldDecorators_1.publicStaticMethod; } });
17
+ Object.defineProperty(exports, "publicStaticPropertyExp", { enumerable: true, get: function () { return oldDecorators_1.publicStaticProperty; } });
18
+ var publish_1 = require("./scripts/publish");
19
+ Object.defineProperty(exports, "publicFunction", { enumerable: true, get: function () { return publish_1.publicFunction; } });
20
+ Object.defineProperty(exports, "publicVariable", { enumerable: true, get: function () { return publish_1.publicVariable; } });
@@ -0,0 +1,2 @@
1
+ export { default as Main } from './scripts/Main';
2
+ export { default as provideFromMain } from './scripts/preloadProvider';
package/cjs/preload.js ADDED
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.provideFromMain = exports.Main = void 0;
7
+ var Main_1 = require("./scripts/Main");
8
+ Object.defineProperty(exports, "Main", { enumerable: true, get: function () { return __importDefault(Main_1).default; } });
9
+ var preloadProvider_1 = require("./scripts/preloadProvider");
10
+ Object.defineProperty(exports, "provideFromMain", { enumerable: true, get: function () { return __importDefault(preloadProvider_1).default; } });
@@ -0,0 +1 @@
1
+ export { default as Bridge } from './scripts/Bridge';
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.Bridge = void 0;
7
+ var Bridge_1 = require("./scripts/Bridge");
8
+ Object.defineProperty(exports, "Bridge", { enumerable: true, get: function () { return __importDefault(Bridge_1).default; } });
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Bridge between `renderer` and `main` processes.
3
+ *
4
+ * _only for renderer process_
5
+ */
6
+ declare const Bridge: import("../types").ITarget;
7
+ export default Bridge;
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const enums_1 = require("../enums");
7
+ const provider_1 = __importDefault(require("./provider"));
8
+ if (!window.__provider__)
9
+ throw new Error('Required methods not provided. Did you forget to call "provide" in preload?');
10
+ const provider = window.__provider__;
11
+ const info = provider.getInfo();
12
+ /**
13
+ * Bridge between `renderer` and `main` processes.
14
+ *
15
+ * _only for renderer process_
16
+ */
17
+ const Bridge = (0, provider_1.default)({
18
+ info,
19
+ scope: enums_1.Scope.renderer,
20
+ callFunction(name, ...args) {
21
+ return provider.provided.functions[name](...args);
22
+ },
23
+ getVariable(name) {
24
+ return provider.provided.properties[name];
25
+ },
26
+ setVariable(name, value) {
27
+ provider.provided.properties[name] = value;
28
+ return;
29
+ },
30
+ waitPromise(channel) {
31
+ return new Promise((resolve, reject) => {
32
+ provider.waitPromise(channel, resolve, reject);
33
+ });
34
+ }
35
+ });
36
+ exports.default = Bridge;
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Bridge between `preload` and `main` processes.
3
+ *
4
+ * _only for preload process_
5
+ */
6
+ declare const Main: import("../types").ITarget;
7
+ export default Main;
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const electron_1 = require("electron");
7
+ const enums_1 = require("../enums");
8
+ const provider_1 = __importDefault(require("./provider"));
9
+ const info = electron_1.ipcRenderer.sendSync(enums_1.IPCChannel.getPublicInfo);
10
+ if (!info)
11
+ throw new Error('Public methods from main is not provided');
12
+ /**
13
+ * Bridge between `preload` and `main` processes.
14
+ *
15
+ * _only for preload process_
16
+ */
17
+ const Main = (0, provider_1.default)({
18
+ info,
19
+ scope: enums_1.Scope.preload,
20
+ callFunction(name, ...args) {
21
+ return electron_1.ipcRenderer.sendSync(enums_1.IPCChannel.functionCall + name, ...args);
22
+ },
23
+ getVariable(name) {
24
+ return electron_1.ipcRenderer.sendSync(enums_1.IPCChannel.propertyGet + name);
25
+ },
26
+ setVariable(name, value) {
27
+ return electron_1.ipcRenderer.sendSync(enums_1.IPCChannel.propertySet + name, value);
28
+ },
29
+ waitPromise(channel) {
30
+ return new Promise((resolve, reject) => {
31
+ electron_1.ipcRenderer.once(channel, (_, result) => {
32
+ if (result.error)
33
+ reject(result.error);
34
+ else
35
+ resolve(result.value);
36
+ });
37
+ });
38
+ }
39
+ });
40
+ exports.default = Main;
@@ -0,0 +1,81 @@
1
+ import type { ClassGetterDecorator, ClassMethodDecorator, ClassPropertyDecorator, ClassSetterDecorator, IPublishMethodArgs, IPublishPropertyArgs, PublishGetterArgs, PublishSetterArgs } from '../types';
2
+ /**
3
+ * Makes the method available for **renderer** and **preload** processes.
4
+ *
5
+ * _only new decorators_
6
+ */
7
+ export declare function publicMethod(): ClassMethodDecorator;
8
+ /**
9
+ * Makes the method available for **renderer** and **preload** processes.
10
+ *
11
+ * _only new decorators_
12
+ * @param name - name by which the method will be accessed
13
+ */
14
+ export declare function publicMethod(name: string): ClassMethodDecorator;
15
+ /**
16
+ * Makes the method available for **renderer** and **preload** processes.
17
+ *
18
+ * _only new decorators_
19
+ * @param args - publish args
20
+ */
21
+ export declare function publicMethod(args: IPublishMethodArgs): ClassMethodDecorator;
22
+ /**
23
+ * Makes the property available for **renderer** and **preload** processes.
24
+ *
25
+ * _only new decorators_
26
+ */
27
+ export declare function publicProperty(): ClassPropertyDecorator;
28
+ /**
29
+ * Makes the property available for **renderer** and **preload** processes.
30
+ *
31
+ * _only new decorators_
32
+ * @param name - name by which the property will be accessed
33
+ */
34
+ export declare function publicProperty(name: string): ClassPropertyDecorator;
35
+ /**
36
+ * Makes the property available for **renderer** and **preload** processes.
37
+ *
38
+ * _only new decorators_
39
+ * @param args - publish args
40
+ */
41
+ export declare function publicProperty(args: IPublishPropertyArgs): ClassPropertyDecorator;
42
+ /**
43
+ * Makes the property getter available for **renderer** and **preload** processes.
44
+ *
45
+ * _only new decorators_
46
+ */
47
+ export declare function publicGetter(): ClassGetterDecorator;
48
+ /**
49
+ * Makes the property getter available for **renderer** and **preload** processes.
50
+ *
51
+ * _only new decorators_
52
+ * @param name - name by which the property will be accessed
53
+ */
54
+ export declare function publicGetter(name: string): ClassGetterDecorator;
55
+ /**
56
+ * Makes the property getter available for **renderer** and **preload** processes.
57
+ *
58
+ * _only new decorators_
59
+ * @param args - publish args
60
+ */
61
+ export declare function publicGetter(args: PublishGetterArgs): ClassGetterDecorator;
62
+ /**
63
+ * Makes the property setter available for **renderer** and **preload** processes.
64
+ *
65
+ * _only new decorators_
66
+ */
67
+ export declare function publicSetter(): ClassSetterDecorator;
68
+ /**
69
+ * Makes the property setter available for **renderer** and **preload** processes.
70
+ *
71
+ * _only new decorators_
72
+ * @param name - name by which the property will be accessed
73
+ */
74
+ export declare function publicSetter(name: string): ClassSetterDecorator;
75
+ /**
76
+ * Makes the property setter available for **renderer** and **preload** processes.
77
+ *
78
+ * _only new decorators_
79
+ * @param args - publish args
80
+ */
81
+ export declare function publicSetter(args: PublishSetterArgs): ClassSetterDecorator;
@@ -0,0 +1,96 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.publicSetter = exports.publicGetter = exports.publicProperty = exports.publicMethod = void 0;
4
+ const publish_1 = require("./publish");
5
+ function publicMethod(arg) {
6
+ return (method, context) => {
7
+ let name = String(context.name);
8
+ if (arg) {
9
+ if (typeof arg === 'string') {
10
+ name = arg;
11
+ }
12
+ else {
13
+ name = arg.name ?? name;
14
+ if (arg.scope)
15
+ (0, publish_1.addScope)(name, arg.scope);
16
+ }
17
+ }
18
+ context.addInitializer(function () {
19
+ (0, publish_1.publicFunction)(name, method.bind(this));
20
+ });
21
+ };
22
+ }
23
+ exports.publicMethod = publicMethod;
24
+ function publicProperty(arg) {
25
+ return (_, context) => {
26
+ const propName = String(context.name);
27
+ let name = propName;
28
+ if (arg) {
29
+ if (typeof arg === 'string') {
30
+ name = arg;
31
+ }
32
+ else {
33
+ name = arg.name ?? name;
34
+ if (arg.scope)
35
+ (0, publish_1.addScope)(name, arg.scope);
36
+ if (arg.access)
37
+ (0, publish_1.addAccess)(name, arg.access);
38
+ }
39
+ }
40
+ context.addInitializer(function () {
41
+ (0, publish_1.publicVariable)(name, {
42
+ get: () => {
43
+ // @ts-ignore
44
+ return this[propName];
45
+ },
46
+ set: (value) => {
47
+ // @ts-ignore
48
+ this[propName] = value;
49
+ }
50
+ });
51
+ });
52
+ };
53
+ }
54
+ exports.publicProperty = publicProperty;
55
+ function publicGetter(arg) {
56
+ return (getter, context) => {
57
+ let name = String(context.name);
58
+ if (arg) {
59
+ if (typeof arg === 'string') {
60
+ name = arg;
61
+ }
62
+ else {
63
+ name = arg.name ?? name;
64
+ if (arg.scope)
65
+ (0, publish_1.addScope)(name, arg.scope);
66
+ }
67
+ }
68
+ context.addInitializer(function () {
69
+ (0, publish_1.publicVariable)(name, {
70
+ get: getter.bind(this)
71
+ });
72
+ });
73
+ };
74
+ }
75
+ exports.publicGetter = publicGetter;
76
+ function publicSetter(arg) {
77
+ return (setter, context) => {
78
+ let name = String(context.name);
79
+ if (arg) {
80
+ if (typeof arg === 'string') {
81
+ name = arg;
82
+ }
83
+ else {
84
+ name = arg.name ?? name;
85
+ if (arg.scope)
86
+ (0, publish_1.addScope)(name, arg.scope);
87
+ }
88
+ }
89
+ context.addInitializer(function () {
90
+ (0, publish_1.publicVariable)(name, {
91
+ set: setter.bind(this)
92
+ });
93
+ });
94
+ };
95
+ }
96
+ exports.publicSetter = publicSetter;
@@ -0,0 +1,123 @@
1
+ import type { IPublishMethodArgs, IPublishPropertyArgs } from '../types';
2
+ /**
3
+ * Makes the method available for **renderer** and **preload** processes.
4
+ *
5
+ * _only for static_
6
+ *
7
+ * _experimental decorators_
8
+ */
9
+ export declare function publicStaticMethod(): MethodDecorator;
10
+ /**
11
+ * Makes the method available for **renderer** and **preload** processes.
12
+ *
13
+ * _only for static_
14
+ *
15
+ * _experimental decorators_
16
+ * @param name - name by which the method will be accessed
17
+ */
18
+ export declare function publicStaticMethod(name: string): MethodDecorator;
19
+ /**
20
+ * Makes the method available for **renderer** and **preload** processes.
21
+ *
22
+ * _only for static_
23
+ *
24
+ * _experimental decorators_
25
+ * @param args - publish args
26
+ */
27
+ export declare function publicStaticMethod(args: IPublishMethodArgs): MethodDecorator;
28
+ /**
29
+ * Makes the method available for **renderer** and **preload** processes.
30
+ *
31
+ * _only for non-static_
32
+ *
33
+ * _experimental decorators_
34
+ *
35
+ * _required `providePublic(classInstance)`_
36
+ */
37
+ export declare function publicMethod(): MethodDecorator;
38
+ /**
39
+ * Makes the method available for **renderer** and **preload** processes.
40
+ *
41
+ * _only for non-static_
42
+ *
43
+ * _experimental decorators_
44
+ *
45
+ * _required `providePublic(classInstance)`_
46
+ * @param name - name by which the method will be accessed
47
+ */
48
+ export declare function publicMethod(name: string): MethodDecorator;
49
+ /**
50
+ * Makes the method available for **renderer** and **preload** processes.
51
+ *
52
+ * _only for non-static_
53
+ *
54
+ * _experimental decorators_
55
+ *
56
+ * _required `providePublic(classInstance)`_
57
+ * @param args - publish args
58
+ */
59
+ export declare function publicMethod(args: IPublishMethodArgs): MethodDecorator;
60
+ /**
61
+ * Makes the property available for **renderer** and **preload** processes.
62
+ *
63
+ * _only for static_
64
+ *
65
+ * _experimental decorators_
66
+ */
67
+ export declare function publicStaticProperty(): PropertyDecorator;
68
+ /**
69
+ * Makes the property available for **renderer** and **preload** processes.
70
+ *
71
+ * _only for static_
72
+ *
73
+ * _experimental decorators_
74
+ * @param name - name by which the property will be accessed
75
+ */
76
+ export declare function publicStaticProperty(name: string): PropertyDecorator;
77
+ /**
78
+ * Makes the property available for **renderer** and **preload** processes.
79
+ *
80
+ * _only for static_
81
+ *
82
+ * _experimental decorators_
83
+ * @param args - publish args
84
+ */
85
+ export declare function publicStaticProperty(args: IPublishPropertyArgs): PropertyDecorator;
86
+ /**
87
+ * Makes the property available for **renderer** and **preload** processes.
88
+ *
89
+ * _only for non-static_
90
+ *
91
+ * _experimental decorators_
92
+ *
93
+ * _required `providePublic(classInstance)`_
94
+ */
95
+ export declare function publicProperty(): PropertyDecorator;
96
+ /**
97
+ * Makes the property available for **renderer** and **preload** processes.
98
+ *
99
+ * _only for non-static_
100
+ *
101
+ * _experimental decorators_
102
+ *
103
+ * _required `providePublic(classInstance)`_
104
+ * @param name - name by which the property will be accessed
105
+ */
106
+ export declare function publicProperty(name: string): PropertyDecorator;
107
+ /**
108
+ * Makes the property available for **renderer** and **preload** processes.
109
+ *
110
+ * _only for non-static_
111
+ *
112
+ * _experimental decorators_
113
+ *
114
+ * _required `providePublic(classInstance)`_
115
+ * @param args - publish args
116
+ */
117
+ export declare function publicProperty(args: IPublishPropertyArgs): PropertyDecorator;
118
+ /**
119
+ * Binds all public entities of a class to a specific instance.
120
+ *
121
+ * _only for class instance_
122
+ */
123
+ export declare function providePublic<InstanceType>(instance: InstanceType): InstanceType;
@@ -0,0 +1,83 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.providePublic = exports.publicProperty = exports.publicStaticProperty = exports.publicMethod = exports.publicStaticMethod = void 0;
4
+ const publish_1 = require("./publish");
5
+ function publicStaticMethod(arg) {
6
+ return (target, key) => {
7
+ let name = String(key);
8
+ if (arg) {
9
+ if (typeof arg === 'string') {
10
+ name = arg;
11
+ }
12
+ else {
13
+ name = arg.name ?? String(key);
14
+ if (arg.scope)
15
+ (0, publish_1.addScope)(name, arg.scope);
16
+ }
17
+ }
18
+ (0, publish_1.publicFunction)(name, (...args) => target[key](...args));
19
+ };
20
+ }
21
+ exports.publicStaticMethod = publicStaticMethod;
22
+ function publicMethod(arg) {
23
+ return (tgt, key, _) => {
24
+ const target = tgt;
25
+ const prevRegister = target.__register__ ?? (() => { });
26
+ target.__register__ = instance => {
27
+ prevRegister(instance);
28
+ publicStaticMethod(arg)(instance, key, _);
29
+ };
30
+ };
31
+ }
32
+ exports.publicMethod = publicMethod;
33
+ function publicStaticProperty(arg) {
34
+ return (target, key) => {
35
+ let name = String(key);
36
+ if (arg) {
37
+ if (typeof arg === 'string') {
38
+ name = arg;
39
+ }
40
+ else {
41
+ name = arg.name ?? String(key);
42
+ if (arg.scope)
43
+ (0, publish_1.addScope)(name, arg.scope);
44
+ if (arg.access)
45
+ (0, publish_1.addAccess)(name, arg.access);
46
+ }
47
+ }
48
+ (0, publish_1.publicVariable)(name, {
49
+ get() {
50
+ return target[key];
51
+ },
52
+ set(value) {
53
+ target[key] = value;
54
+ }
55
+ });
56
+ };
57
+ }
58
+ exports.publicStaticProperty = publicStaticProperty;
59
+ function publicProperty(arg) {
60
+ return (tgt, key) => {
61
+ const target = tgt;
62
+ const prevRegister = target.__register__ ?? (() => { });
63
+ target.__register__ = instance => {
64
+ prevRegister(instance);
65
+ publicStaticProperty(arg)(instance, key);
66
+ };
67
+ };
68
+ }
69
+ exports.publicProperty = publicProperty;
70
+ /**
71
+ * Binds all public entities of a class to a specific instance.
72
+ *
73
+ * _only for class instance_
74
+ */
75
+ function providePublic(instance) {
76
+ const target = instance;
77
+ if (!target.__register__)
78
+ throw new Error('Public methods not defined');
79
+ target.__register__(instance);
80
+ target.__register__ = undefined;
81
+ return instance;
82
+ }
83
+ exports.providePublic = providePublic;
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Sets access to main from renderer
3
+ *
4
+ * @param contextIsolation - _default_: `true`
5
+ */
6
+ declare function provideFromMain(contextIsolation?: boolean): void;
7
+ export default provideFromMain;
@@ -0,0 +1,53 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const electron_1 = require("electron");
4
+ const enums_1 = require("../enums");
5
+ const info = electron_1.ipcRenderer.sendSync(enums_1.IPCChannel.getPublicInfo);
6
+ /**
7
+ * Sets access to main from renderer
8
+ *
9
+ * @param contextIsolation - _default_: `true`
10
+ */
11
+ function provideFromMain(contextIsolation = true) {
12
+ const provider = {
13
+ getInfo() {
14
+ return info;
15
+ },
16
+ waitPromise(channel, resolve, reject) {
17
+ electron_1.ipcRenderer.once(channel, (_, result) => {
18
+ if (result.error)
19
+ reject?.(result.error);
20
+ else
21
+ resolve(result.value);
22
+ });
23
+ },
24
+ provided: {
25
+ properties: {},
26
+ functions: {}
27
+ }
28
+ };
29
+ info.functions.forEach(funcName => {
30
+ Object.defineProperty(provider.provided.functions, funcName, {
31
+ value(...args) {
32
+ return electron_1.ipcRenderer.sendSync(enums_1.IPCChannel.functionCall + funcName, ...args);
33
+ },
34
+ enumerable: true
35
+ });
36
+ });
37
+ info.properties.forEach(propName => {
38
+ Object.defineProperty(provider.provided.properties, propName, {
39
+ get() {
40
+ return electron_1.ipcRenderer.sendSync(enums_1.IPCChannel.propertyGet + propName);
41
+ },
42
+ set(value) {
43
+ electron_1.ipcRenderer.sendSync(enums_1.IPCChannel.propertySet + propName, value);
44
+ },
45
+ enumerable: true
46
+ });
47
+ });
48
+ if (contextIsolation)
49
+ electron_1.contextBridge.exposeInMainWorld('__provider__', provider);
50
+ else
51
+ window.__provider__ = provider;
52
+ }
53
+ exports.default = provideFromMain;
@@ -0,0 +1,3 @@
1
+ import type { ICreateProviderArgs, ITarget } from '../types';
2
+ declare function createProvider(args: ICreateProviderArgs): ITarget;
3
+ export default createProvider;
@@ -0,0 +1,102 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const enums_1 = require("../enums");
4
+ function errorHandler(error, channel) {
5
+ console.error(`Error on ${channel}.\n${error}`.replace('Error: ', ''));
6
+ }
7
+ function createProvider(args) {
8
+ const { info, scope, getVariable, setVariable, callFunction, waitPromise } = args;
9
+ const target = {
10
+ as: () => proxy
11
+ };
12
+ info.functions.forEach(funcName => {
13
+ if (!info.scopes[funcName].has(scope))
14
+ return;
15
+ Object.defineProperty(target, funcName, {
16
+ get() {
17
+ return (...args) => {
18
+ const result = callFunction(funcName, ...args);
19
+ if (result.error) {
20
+ errorHandler(result.error, funcName);
21
+ return;
22
+ }
23
+ if (result.promiseChannel)
24
+ return waitPromise(result.promiseChannel);
25
+ return result.value;
26
+ };
27
+ },
28
+ set() {
29
+ throw new Error(`Function '${funcName}' is readonly`);
30
+ },
31
+ enumerable: true
32
+ });
33
+ });
34
+ info.properties.forEach(propName => {
35
+ if (!info.scopes[propName].has(scope))
36
+ return;
37
+ let getter = () => {
38
+ const result = getVariable(propName);
39
+ if (result.error) {
40
+ errorHandler(result.error, propName);
41
+ return;
42
+ }
43
+ return result.value;
44
+ };
45
+ let setter = (value) => {
46
+ const result = setVariable(propName, value);
47
+ if (result?.error) {
48
+ errorHandler(result.error, propName);
49
+ return;
50
+ }
51
+ };
52
+ if (!info.accesses[propName].has(enums_1.Access.get))
53
+ getter = () => { throw new Error(`No access to getting the '${propName}' property`); };
54
+ if (!info.accesses[propName].has(enums_1.Access.set))
55
+ setter = () => { throw new Error(`No access to setting the '${propName}' property`); };
56
+ Object.defineProperty(target, propName, {
57
+ get: getter,
58
+ set: setter,
59
+ enumerable: true
60
+ });
61
+ });
62
+ const proxy = new Proxy(target, {
63
+ get(target, key) {
64
+ if (key in target)
65
+ return target[key];
66
+ const isFunction = info.functions.has(key);
67
+ const isProperty = info.properties.has(key);
68
+ if (!isFunction && !isProperty)
69
+ throw new Error(`'${key}' is not provided from main`);
70
+ if (!info.scopes[key].has(enums_1.Scope.renderer))
71
+ throw new Error(`'${key}' is not provided into renderer scope`);
72
+ if (isProperty && !info.accesses[key].has(enums_1.Access.get))
73
+ throw new Error(`No access to getting the '${key}' property`);
74
+ throw new Error(`Unknown error on get '${key}'`);
75
+ },
76
+ set(target, key, newValue) {
77
+ if (key in target) {
78
+ target[key] = newValue;
79
+ return true;
80
+ }
81
+ const isFunction = info.functions.has(key);
82
+ const isProperty = info.properties.has(key);
83
+ if (isFunction)
84
+ throw new Error(`Function '${key}' is readonly`);
85
+ if (!isProperty)
86
+ throw new Error(`Property '${key}' is not provided from main`);
87
+ if (!info.scopes[key].has(enums_1.Scope.renderer))
88
+ throw new Error(`'${key}' is not provided into renderer scope`);
89
+ if (!info.accesses[key].has(enums_1.Access.set))
90
+ throw new Error(`No access to setting the '${key}' property`);
91
+ throw new Error(`Unknown error on set '${key}'`);
92
+ },
93
+ ownKeys(target) {
94
+ return Object.keys(target);
95
+ },
96
+ has(target, key) {
97
+ return key in target;
98
+ }
99
+ });
100
+ return proxy;
101
+ }
102
+ exports.default = createProvider;
@@ -0,0 +1,22 @@
1
+ import { Access, Scope } from '../enums';
2
+ import type { PublicFunction, PublicProperty } from '../types';
3
+ /**
4
+ * Makes the function available for **renderer** and **preload** processes.
5
+ *
6
+ * _only for function_
7
+ * @param name - name by which the function will be accessed
8
+ * @param func - linked function
9
+ * @param scopes - function scopes
10
+ */
11
+ export declare function publicFunction(name: string, func: PublicFunction, scopes?: Scope[]): void;
12
+ /**
13
+ * Makes the function available for **renderer** and **preload** processes.
14
+ *
15
+ * _only for variables_
16
+ * @param name - name by which the variable will be accessed
17
+ * @param value - variable getter/setter
18
+ * @param scopes - variable scopes
19
+ */
20
+ export declare function publicVariable(name: string, value: PublicProperty, scopes?: Scope[]): void;
21
+ export declare function addScope(key: string, scope: Scope): void;
22
+ export declare function addAccess(key: string, access: Access): void;
@@ -0,0 +1,108 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.addAccess = exports.addScope = exports.publicVariable = exports.publicFunction = void 0;
4
+ const electron_1 = require("electron");
5
+ const enums_1 = require("../enums");
6
+ const publicInfo = {
7
+ properties: new Set(),
8
+ functions: new Set(),
9
+ accesses: {},
10
+ scopes: {}
11
+ };
12
+ electron_1.ipcMain.on(enums_1.IPCChannel.getPublicInfo, e => e.returnValue = publicInfo);
13
+ /**
14
+ * Makes the function available for **renderer** and **preload** processes.
15
+ *
16
+ * _only for function_
17
+ * @param name - name by which the function will be accessed
18
+ * @param func - linked function
19
+ * @param scopes - function scopes
20
+ */
21
+ function publicFunction(name, func, scopes = [enums_1.Scope.preload, enums_1.Scope.renderer]) {
22
+ const { functions } = publicInfo;
23
+ const channel = enums_1.IPCChannel.functionCall + name;
24
+ functions.add(name);
25
+ if (!publicInfo.scopes[name])
26
+ publicInfo.scopes[name] = new Set(scopes);
27
+ electron_1.ipcMain.removeAllListeners(channel);
28
+ electron_1.ipcMain.on(channel, (e, ...args) => {
29
+ try {
30
+ const result = func(...args);
31
+ if (result instanceof Promise) {
32
+ const promiseChannel = channel + enums_1.IPCChannel.promisePostfix;
33
+ result
34
+ .then(value => e.sender.send(promiseChannel, { value }))
35
+ .catch(reason => e.sender.send(promiseChannel, { error: String(reason) }));
36
+ e.returnValue = { promiseChannel };
37
+ }
38
+ else {
39
+ e.returnValue = { value: result };
40
+ }
41
+ }
42
+ catch (error) {
43
+ e.returnValue = { error: String(error) };
44
+ }
45
+ });
46
+ }
47
+ exports.publicFunction = publicFunction;
48
+ /**
49
+ * Makes the function available for **renderer** and **preload** processes.
50
+ *
51
+ * _only for variables_
52
+ * @param name - name by which the variable will be accessed
53
+ * @param value - variable getter/setter
54
+ * @param scopes - variable scopes
55
+ */
56
+ function publicVariable(name, value, scopes = [enums_1.Scope.renderer, enums_1.Scope.preload]) {
57
+ const { properties } = publicInfo;
58
+ const getChannel = enums_1.IPCChannel.propertyGet + name;
59
+ const setChannel = enums_1.IPCChannel.propertySet + name;
60
+ const { get: getter, set: setter } = value;
61
+ properties.add(name);
62
+ if (!publicInfo.scopes[name])
63
+ publicInfo.scopes[name] = new Set(scopes);
64
+ if (!publicInfo.accesses[name]) {
65
+ const accesses = [];
66
+ getter && accesses.push(enums_1.Access.get);
67
+ setter && accesses.push(enums_1.Access.set);
68
+ publicInfo.accesses[name] = new Set(accesses);
69
+ }
70
+ else {
71
+ const accesses = publicInfo.accesses[name];
72
+ getter && accesses.add(enums_1.Access.get);
73
+ setter && accesses.add(enums_1.Access.set);
74
+ }
75
+ if (getter) {
76
+ electron_1.ipcMain.removeAllListeners(getChannel);
77
+ electron_1.ipcMain.on(getChannel, event => {
78
+ try {
79
+ const result = getter();
80
+ event.returnValue = { value: result };
81
+ }
82
+ catch (error) {
83
+ event.returnValue = { error };
84
+ }
85
+ });
86
+ }
87
+ if (setter) {
88
+ electron_1.ipcMain.removeAllListeners(setChannel);
89
+ electron_1.ipcMain.on(setChannel, (event, ...args) => {
90
+ try {
91
+ setter(args[0]);
92
+ event.returnValue = { value: null };
93
+ }
94
+ catch (error) {
95
+ event.returnValue = { error };
96
+ }
97
+ });
98
+ }
99
+ }
100
+ exports.publicVariable = publicVariable;
101
+ function addScope(key, scope) {
102
+ publicInfo.scopes[key] = new Set([scope]);
103
+ }
104
+ exports.addScope = addScope;
105
+ function addAccess(key, access) {
106
+ publicInfo.accesses[key] = new Set([access]);
107
+ }
108
+ exports.addAccess = addAccess;
package/cjs/types.d.ts ADDED
@@ -0,0 +1,57 @@
1
+ import type { Scope } from './enums';
2
+ import { Access } from './enums';
3
+ export interface ITarget {
4
+ as<T>(): T;
5
+ }
6
+ export type ClassMethodDecorator = <This, Args extends any[], Return>(method: (this: This, ...args: Args) => Return, context: ClassMethodDecoratorContext<This, (this: This, ...args: Args) => Return>) => void;
7
+ export type ClassPropertyDecorator = <This, Type>(target: undefined, context: ClassFieldDecoratorContext<This, Type>) => void;
8
+ export type ClassGetterDecorator = <This, Type>(getter: () => Type, context: ClassGetterDecoratorContext<This, Type>) => void;
9
+ export type ClassSetterDecorator = <This, Type>(setter: (value: Type) => void, context: ClassSetterDecoratorContext<This, Type>) => void;
10
+ export type PublishSetterArgs = Omit<IPublishPropertyArgs, 'access'>;
11
+ export type PublishGetterArgs = Omit<IPublishPropertyArgs, 'access'>;
12
+ export interface IIPCResult {
13
+ value?: any;
14
+ error?: string;
15
+ promiseChannel?: string;
16
+ }
17
+ export interface IPublishMethodArgs {
18
+ name?: string;
19
+ scope?: Scope;
20
+ }
21
+ export interface IPublishPropertyArgs {
22
+ name?: string;
23
+ scope?: Scope;
24
+ access?: Access;
25
+ }
26
+ export interface ICreateProviderArgs {
27
+ info: IInfo;
28
+ scope: Scope;
29
+ callFunction(name: string, ...args: any[]): IIPCResult;
30
+ getVariable(name: string): IIPCResult;
31
+ setVariable(name: string, value: any): IIPCResult | undefined;
32
+ waitPromise(channel: string): Promise<any>;
33
+ }
34
+ export type Scopes = Record<string, Set<Scope>>;
35
+ export type Accesses = Record<string, Set<Access>>;
36
+ export interface IRendererPublic {
37
+ __register__?(instance: any): void;
38
+ }
39
+ export interface IProvider {
40
+ getInfo(): IInfo;
41
+ waitPromise(name: string, resolve: (value: any) => void, reject?: (reason?: any) => void): void;
42
+ provided: {
43
+ functions: Record<string, (...args: any[]) => IIPCResult>;
44
+ properties: Record<string, IIPCResult>;
45
+ };
46
+ }
47
+ export type PublicFunction = (...args: any[]) => any;
48
+ export type PublicProperty = {
49
+ get?(): any;
50
+ set?(value: any): void;
51
+ };
52
+ export interface IInfo {
53
+ properties: Set<string>;
54
+ functions: Set<string>;
55
+ scopes: Scopes;
56
+ accesses: Accesses;
57
+ }
package/cjs/types.js ADDED
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/package.json CHANGED
@@ -1,14 +1,15 @@
1
1
  {
2
2
  "name": "emr-bridge",
3
- "version": "1.2.0",
3
+ "version": "1.2.2",
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": {
7
- "compile": "tsc --project ./tsconfig.json"
7
+ "compile": "tsc --project ./tsconfig.json && tsc --project ./tsconfig.cjs.json"
8
8
  },
9
9
  "types": "lib/index.d.ts",
10
10
  "files": [
11
- "lib"
11
+ "lib",
12
+ "cjs"
12
13
  ],
13
14
  "typesVersions": {
14
15
  "*": {
@@ -23,11 +24,27 @@
23
24
  ],
24
25
  "main": [
25
26
  "./lib/index.d.ts"
27
+ ],
28
+ "cjs": [
29
+ "./cjs/index.d.ts"
30
+ ],
31
+ "cjs/renderer": [
32
+ "./cjs/renderer.d.ts"
33
+ ],
34
+ "cjs/preload": [
35
+ "./cjs/preload.d.ts"
36
+ ],
37
+ "cjs/main": [
38
+ "./cjs/index.d.ts"
26
39
  ]
27
40
  }
28
41
  },
29
42
  "exports": {
30
43
  ".": "./lib/index.js",
44
+ "./cjs": "./cjs/index.js",
45
+ "./cjs/renderer": "./cjs/renderer.js",
46
+ "./cjs/preload": "./cjs/preload.js",
47
+ "./cjs/main": "./cjs/index.js",
31
48
  "./renderer": "./lib/renderer.js",
32
49
  "./preload": "./lib/preload.js",
33
50
  "./main": "./lib/index.js",
@@ -47,6 +64,6 @@
47
64
  "electron": ">=20.0.0"
48
65
  },
49
66
  "devDependencies": {
50
- "typescript": "^5.0.2"
67
+ "typescript": "^5.0.4"
51
68
  }
52
69
  }