@itwin/core-mobile 4.0.0-dev.10 → 4.0.0-dev.100

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.
Files changed (64) hide show
  1. package/CHANGELOG.md +58 -1
  2. package/README.md +1 -1
  3. package/lib/cjs/MobileBackend.d.ts +7 -7
  4. package/lib/cjs/MobileBackend.js +28 -24
  5. package/lib/cjs/MobileBackend.js.map +1 -1
  6. package/lib/cjs/MobileFrontend.d.ts +5 -5
  7. package/lib/cjs/MobileFrontend.js +25 -21
  8. package/lib/cjs/MobileFrontend.js.map +1 -1
  9. package/lib/cjs/__DOC_ONLY__.d.ts +33 -33
  10. package/lib/cjs/__DOC_ONLY__.js +56 -52
  11. package/lib/cjs/__DOC_ONLY__.js.map +1 -1
  12. package/lib/cjs/backend/MobileAuthorizationBackend.d.ts +17 -17
  13. package/lib/cjs/backend/MobileAuthorizationBackend.d.ts.map +1 -1
  14. package/lib/cjs/backend/MobileAuthorizationBackend.js +54 -46
  15. package/lib/cjs/backend/MobileAuthorizationBackend.js.map +1 -1
  16. package/lib/cjs/backend/MobileFileHandler.d.ts +112 -112
  17. package/lib/cjs/backend/MobileFileHandler.d.ts.map +1 -1
  18. package/lib/cjs/backend/MobileFileHandler.js +263 -263
  19. package/lib/cjs/backend/MobileFileHandler.js.map +1 -1
  20. package/lib/cjs/backend/MobileHost.d.ts +72 -72
  21. package/lib/cjs/backend/MobileHost.d.ts.map +1 -1
  22. package/lib/cjs/backend/MobileHost.js +151 -156
  23. package/lib/cjs/backend/MobileHost.js.map +1 -1
  24. package/lib/cjs/backend/MobileRpcServer.d.ts +21 -19
  25. package/lib/cjs/backend/MobileRpcServer.d.ts.map +1 -1
  26. package/lib/cjs/backend/MobileRpcServer.js +179 -156
  27. package/lib/cjs/backend/MobileRpcServer.js.map +1 -1
  28. package/lib/cjs/backend/Request.d.ts +117 -149
  29. package/lib/cjs/backend/Request.d.ts.map +1 -1
  30. package/lib/cjs/backend/Request.js +255 -267
  31. package/lib/cjs/backend/Request.js.map +1 -1
  32. package/lib/cjs/common/MobileAppChannel.d.ts +4 -4
  33. package/lib/cjs/common/MobileAppChannel.js +11 -11
  34. package/lib/cjs/common/MobileAppProps.d.ts +35 -35
  35. package/lib/cjs/common/MobileAppProps.d.ts.map +1 -1
  36. package/lib/cjs/common/MobileAppProps.js +26 -26
  37. package/lib/cjs/common/MobileEventLoop.d.ts +11 -11
  38. package/lib/cjs/common/MobileEventLoop.js +30 -30
  39. package/lib/cjs/common/MobileEventLoop.js.map +1 -1
  40. package/lib/cjs/common/MobileIpc.d.ts +17 -17
  41. package/lib/cjs/common/MobileIpc.js +70 -70
  42. package/lib/cjs/common/MobilePush.d.ts +20 -20
  43. package/lib/cjs/common/MobilePush.js +53 -53
  44. package/lib/cjs/common/MobilePush.js.map +1 -1
  45. package/lib/cjs/common/MobileRpcManager.d.ts +40 -40
  46. package/lib/cjs/common/MobileRpcManager.js +110 -110
  47. package/lib/cjs/common/MobileRpcManager.js.map +1 -1
  48. package/lib/cjs/common/MobileRpcProtocol.d.ts +58 -58
  49. package/lib/cjs/common/MobileRpcProtocol.d.ts.map +1 -1
  50. package/lib/cjs/common/MobileRpcProtocol.js +277 -277
  51. package/lib/cjs/common/MobileRpcProtocol.js.map +1 -1
  52. package/lib/cjs/common/MobileRpcRequest.d.ts +20 -20
  53. package/lib/cjs/common/MobileRpcRequest.js +50 -50
  54. package/lib/cjs/frontend/MobileApp.d.ts +23 -23
  55. package/lib/cjs/frontend/MobileApp.d.ts.map +1 -1
  56. package/lib/cjs/frontend/MobileApp.js +79 -80
  57. package/lib/cjs/frontend/MobileApp.js.map +1 -1
  58. package/lib/cjs/frontend/MobileAuthorizationFrontend.d.ts +17 -17
  59. package/lib/cjs/frontend/MobileAuthorizationFrontend.d.ts.map +1 -1
  60. package/lib/cjs/frontend/MobileAuthorizationFrontend.js +54 -46
  61. package/lib/cjs/frontend/MobileAuthorizationFrontend.js.map +1 -1
  62. package/lib/cjs/test/ios/MobilePlatform.test.d.ts +1 -1
  63. package/lib/cjs/test/ios/MobilePlatform.test.js +84 -84
  64. package/package.json +18 -27
@@ -1,157 +1,180 @@
1
- "use strict";
2
- /*---------------------------------------------------------------------------------------------
3
- * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
4
- * See LICENSE.md in the project root for license terms and full copyright notice.
5
- *--------------------------------------------------------------------------------------------*/
6
- Object.defineProperty(exports, "__esModule", { value: true });
7
- exports.setupMobileRpc = exports.MobileRpcServer = void 0;
8
- const ws = require("ws");
9
- const core_common_1 = require("@itwin/core-common");
10
- const MobileRpcProtocol_1 = require("../common/MobileRpcProtocol");
11
- const MobileRpcManager_1 = require("../common/MobileRpcManager");
12
- const MobileHost_1 = require("./MobileHost");
13
- const core_bentley_1 = require("@itwin/core-bentley");
14
- let addon;
15
- /** @internal */
16
- class MobileRpcServer {
17
- constructor() {
18
- /* _pingTime is a fix for ios/mobile case where when the app moves into foreground from
19
- * background backend restart ws.Server and then notify frontend to reconnect. But ws.Server
20
- * listening event is not fired as node yield to kevent and wait for some io event to happen.
21
- * This causes a delay in reconnection which may be as long a 40 secs. To solve the issue we
22
- * create _pingTimer which causes kevent to yield back to uv poll so timer event can be fired.
23
- * This allow listening event to go through quickly (max 5ms). Once the listening event occur we
24
- * clear the timer. Here we use setInterval() just to make sure otherwise setTimeout() could equally
25
- * be effective
26
- */
27
- this._pingTimer = setInterval(() => { }, 5);
28
- this._port = MobileRpcManager_1.MobileRpcConfiguration.setup.obtainPort();
29
- this._server = new ws.Server({ port: this._port });
30
- this._connectionId = ++MobileRpcServer._nextId;
31
- MobileRpcServer.interop.connectionId = this._connectionId;
32
- this._onListening();
33
- this._onConnection();
34
- }
35
- _onListening() {
36
- this._server.on("listening", () => {
37
- const address = this._server.address();
38
- this._port = address.port;
39
- clearInterval(this._pingTimer);
40
- this._notifyListening();
41
- });
42
- }
43
- _notifyListening() {
44
- MobileRpcServer.interop.port = this._port;
45
- if (addon) {
46
- addon.notifyListening(this._port);
47
- }
48
- if (this._connectionId !== 0) {
49
- MobileHost_1.MobileHost.reconnect(this._port);
50
- }
51
- }
52
- _onConnection() {
53
- this._server.on("connection", (connection) => {
54
- this._connection = connection;
55
- this._connection.on("message", (data) => this._onConnectionMessage(data));
56
- this._createSender();
57
- global.__iTwinJsRpcReady = true;
58
- });
59
- }
60
- _createSender() {
61
- const sender = (message, connectionId) => {
62
- if (connectionId !== this._connectionId) {
63
- return;
64
- }
65
- this._connection.send(message, (err) => {
66
- if (err) {
67
- throw err;
68
- }
69
- });
70
- };
71
- MobileRpcServer.interop.sendString = sender;
72
- MobileRpcServer.interop.sendBinary = sender;
73
- }
74
- _onConnectionMessage(data) {
75
- let message = data;
76
- if (Array.isArray(message)) {
77
- throw new core_common_1.IModelError(core_common_1.BentleyStatus.ERROR, "Unsupported data type");
78
- }
79
- if (Buffer.isBuffer(message)) {
80
- if (message.byteOffset !== 0 || message.byteLength !== message.buffer.byteLength) {
81
- message = new Uint8Array(message.buffer, message.byteOffset, message.byteLength);
82
- }
83
- else {
84
- message = message.buffer;
85
- }
86
- }
87
- MobileRpcServer.interop.handler(message, this._connectionId);
88
- }
89
- dispose() {
90
- clearInterval(this._pingTimer);
91
- if (this._connection) {
92
- MobileRpcServer.interop.sendString = () => { };
93
- MobileRpcServer.interop.sendBinary = () => { };
94
- this._connection.close();
95
- }
96
- this._server.close();
97
- }
98
- }
99
- exports.MobileRpcServer = MobileRpcServer;
100
- MobileRpcServer._nextId = -1;
101
- MobileRpcServer.interop = {
102
- handler: (_payload) => { throw new core_common_1.IModelError(core_common_1.BentleyStatus.ERROR, "Not implemented."); },
103
- sendString: (_message, _connectionId) => { throw new core_common_1.IModelError(core_common_1.BentleyStatus.ERROR, "No connection."); },
104
- sendBinary: (_message, _connectionId) => { throw new core_common_1.IModelError(core_common_1.BentleyStatus.ERROR, "No connection."); },
105
- port: 0,
106
- connectionId: 0,
107
- };
108
- let mobileReady = false;
109
- let hasSuspended = false;
110
- function setupMobileRpc() {
111
- if (mobileReady) {
112
- return;
113
- }
114
- if (core_bentley_1.ProcessDetector.isMobileAppBackend) {
115
- addon = process._linkedBinding("iModelJsMobile");
116
- addon === null || addon === void 0 ? void 0 : addon.registerDeviceImpl();
117
- }
118
- let server = new MobileRpcServer();
119
- /* The UV event loop (internal to node) is retained by handles,
120
- such as those created for setInterval/setTimeout and client/server connections.
121
- In a simple app, the RPC server may be the only handle retaining the UV loop.
122
- Thus, we install a temporary timer on suspend to prevent the loop from exiting prematurely.
123
- */
124
- let retainUvLoop;
125
- MobileHost_1.MobileHost.onEnterBackground.addListener(() => {
126
- hasSuspended = true;
127
- if (server === null) {
128
- return;
129
- }
130
- retainUvLoop = setInterval(() => { }, 1000);
131
- server.dispose();
132
- server = null;
133
- });
134
- MobileHost_1.MobileHost.onEnterForeground.addListener(() => {
135
- if (!hasSuspended) {
136
- return;
137
- }
138
- server = new MobileRpcServer();
139
- clearInterval(retainUvLoop);
140
- retainUvLoop = undefined;
141
- });
142
- MobileHost_1.MobileHost.onWillTerminate.addListener(() => {
143
- if (typeof (retainUvLoop) !== "undefined") {
144
- clearInterval(retainUvLoop);
145
- retainUvLoop = undefined;
146
- }
147
- if (server === null) {
148
- return;
149
- }
150
- server.dispose();
151
- server = null;
152
- });
153
- MobileRpcProtocol_1.MobileRpcProtocol.obtainInterop = () => MobileRpcServer.interop;
154
- mobileReady = true;
155
- }
156
- exports.setupMobileRpc = setupMobileRpc;
1
+ "use strict";
2
+ /*---------------------------------------------------------------------------------------------
3
+ * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
4
+ * See LICENSE.md in the project root for license terms and full copyright notice.
5
+ *--------------------------------------------------------------------------------------------*/
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.setupMobileRpc = exports.MobileRpcServer = void 0;
8
+ const ws = require("ws");
9
+ const core_common_1 = require("@itwin/core-common");
10
+ const MobileRpcProtocol_1 = require("../common/MobileRpcProtocol");
11
+ const MobileRpcManager_1 = require("../common/MobileRpcManager");
12
+ const MobileHost_1 = require("./MobileHost");
13
+ const core_bentley_1 = require("@itwin/core-bentley");
14
+ let addon;
15
+ /** @internal */
16
+ class MobileRpcServer {
17
+ constructor(_pendingMessages = undefined) {
18
+ this._pendingMessages = _pendingMessages;
19
+ /* _pingTime is a fix for ios/mobile case where when the app moves into foreground from
20
+ * background backend restart ws.Server and then notify frontend to reconnect. But ws.Server
21
+ * listening event is not fired as node yield to kevent and wait for some io event to happen.
22
+ * This causes a delay in reconnection which may be as long a 40 secs. To solve the issue we
23
+ * create _pingTimer which causes kevent to yield back to uv poll so timer event can be fired.
24
+ * This allow listening event to go through quickly (max 5ms). Once the listening event occur we
25
+ * clear the timer. Here we use setInterval() just to make sure otherwise setTimeout() could equally
26
+ * be effective
27
+ */
28
+ this._pingTimer = setInterval(() => { }, 5);
29
+ this._port = MobileRpcManager_1.MobileRpcConfiguration.setup.obtainPort();
30
+ this._server = new ws.Server({ port: this._port });
31
+ this._connectionId = ++MobileRpcServer._nextId;
32
+ MobileRpcServer.interop.connectionId = this._connectionId;
33
+ this._onListening();
34
+ this._onConnection();
35
+ }
36
+ _onListening() {
37
+ this._server.on("listening", () => {
38
+ const address = this._server.address();
39
+ this._port = address.port;
40
+ clearInterval(this._pingTimer);
41
+ this._notifyListening();
42
+ });
43
+ }
44
+ _notifyListening() {
45
+ MobileRpcServer.interop.port = this._port;
46
+ if (addon) {
47
+ addon.notifyListening(this._port);
48
+ }
49
+ if (this._connectionId !== 0) {
50
+ MobileHost_1.MobileHost.reconnect(this._port);
51
+ }
52
+ }
53
+ _onConnection() {
54
+ this._server.on("connection", (connection) => {
55
+ this._connection = connection;
56
+ this._connection.on("message", (data) => this._onConnectionMessage(data));
57
+ this._createSender();
58
+ this._sendPending();
59
+ global.__iTwinJsRpcReady = true;
60
+ });
61
+ }
62
+ _createSender() {
63
+ const sender = (message, connectionId) => {
64
+ if (connectionId !== this._connectionId) {
65
+ return;
66
+ }
67
+ this._connection.send(message, (err) => {
68
+ if (err) {
69
+ throw err;
70
+ }
71
+ });
72
+ };
73
+ MobileRpcServer.interop.sendString = sender;
74
+ MobileRpcServer.interop.sendBinary = sender;
75
+ }
76
+ _sendPending() {
77
+ if (this._pendingMessages === undefined)
78
+ return;
79
+ for (const message of this._pendingMessages) {
80
+ this._connection.send(message, (err) => {
81
+ if (err) {
82
+ throw err;
83
+ }
84
+ });
85
+ }
86
+ this._pendingMessages.length = 0;
87
+ }
88
+ _onConnectionMessage(data) {
89
+ let message = data;
90
+ if (Array.isArray(message)) {
91
+ throw new core_common_1.IModelError(core_common_1.BentleyStatus.ERROR, "Unsupported data type");
92
+ }
93
+ if (Buffer.isBuffer(message)) {
94
+ if (message.byteOffset !== 0 || message.byteLength !== message.buffer.byteLength) {
95
+ message = new Uint8Array(message.buffer, message.byteOffset, message.byteLength);
96
+ }
97
+ else {
98
+ message = message.buffer;
99
+ }
100
+ }
101
+ MobileRpcServer.interop.handler(message, this._connectionId);
102
+ }
103
+ dispose() {
104
+ clearInterval(this._pingTimer);
105
+ if (this._connection) {
106
+ MobileRpcServer.interop.sendString = () => { };
107
+ MobileRpcServer.interop.sendBinary = () => { };
108
+ this._connection.close();
109
+ }
110
+ this._server.close();
111
+ }
112
+ }
113
+ MobileRpcServer._nextId = -1;
114
+ MobileRpcServer.interop = {
115
+ handler: (_payload) => { throw new core_common_1.IModelError(core_common_1.BentleyStatus.ERROR, "Not implemented."); },
116
+ sendString: (_message, _connectionId) => { throw new core_common_1.IModelError(core_common_1.BentleyStatus.ERROR, "No connection."); },
117
+ sendBinary: (_message, _connectionId) => { throw new core_common_1.IModelError(core_common_1.BentleyStatus.ERROR, "No connection."); },
118
+ port: 0,
119
+ connectionId: 0,
120
+ };
121
+ exports.MobileRpcServer = MobileRpcServer;
122
+ let mobileReady = false;
123
+ let hasSuspended = false;
124
+ function setupMobileRpc() {
125
+ if (mobileReady) {
126
+ return;
127
+ }
128
+ if (core_bentley_1.ProcessDetector.isMobileAppBackend) {
129
+ addon = process._linkedBinding("iModelJsMobile");
130
+ addon?.registerDeviceImpl();
131
+ }
132
+ let server = new MobileRpcServer();
133
+ /* The UV event loop (internal to node) is retained by handles,
134
+ such as those created for setInterval/setTimeout and client/server connections.
135
+ In a simple app, the RPC server may be the only handle retaining the UV loop.
136
+ Thus, we install a temporary timer on suspend to prevent the loop from exiting prematurely.
137
+ */
138
+ let retainUvLoop;
139
+ const pendingMessages = [];
140
+ function usePendingSender() {
141
+ const sender = (message, _connectionId) => {
142
+ pendingMessages.push(message);
143
+ };
144
+ MobileRpcServer.interop.sendString = sender;
145
+ MobileRpcServer.interop.sendBinary = sender;
146
+ }
147
+ MobileHost_1.MobileHost.onEnterBackground.addListener(() => {
148
+ hasSuspended = true;
149
+ if (server === null) {
150
+ return;
151
+ }
152
+ retainUvLoop = setInterval(() => { }, 1000);
153
+ server.dispose();
154
+ usePendingSender();
155
+ server = null;
156
+ });
157
+ MobileHost_1.MobileHost.onEnterForeground.addListener(() => {
158
+ if (!hasSuspended) {
159
+ return;
160
+ }
161
+ server = new MobileRpcServer(pendingMessages);
162
+ clearInterval(retainUvLoop);
163
+ retainUvLoop = undefined;
164
+ });
165
+ MobileHost_1.MobileHost.onWillTerminate.addListener(() => {
166
+ if (typeof (retainUvLoop) !== "undefined") {
167
+ clearInterval(retainUvLoop);
168
+ retainUvLoop = undefined;
169
+ }
170
+ if (server === null) {
171
+ return;
172
+ }
173
+ server.dispose();
174
+ server = null;
175
+ });
176
+ MobileRpcProtocol_1.MobileRpcProtocol.obtainInterop = () => MobileRpcServer.interop;
177
+ mobileReady = true;
178
+ }
179
+ exports.setupMobileRpc = setupMobileRpc;
157
180
  //# sourceMappingURL=MobileRpcServer.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"MobileRpcServer.js","sourceRoot":"","sources":["../../../src/backend/MobileRpcServer.ts"],"names":[],"mappings":";AAAA;;;+FAG+F;;;AAE/F,yBAAyB;AACzB,oDAAgE;AAChE,mEAAkF;AAClF,iE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GAAG,GAAG,EAAE,CAAC,eAAe,CAAC,OAAO,CAAC;IAChE,WAAW,GAAG,IAAI,CAAC;AACrB,CAAC;AAzDD,wCAyDC","sourcesContent":["/*---------------------------------------------------------------------------------------------\r\n* Copyright (c) Bentley Systems, Incorporated. All rights reserved.\r\n* See LICENSE.md in the project root for license terms and full copyright notice.\r\n*--------------------------------------------------------------------------------------------*/\r\n\r\nimport * as ws from \"ws\";\r\nimport { BentleyStatus, IModelError } from \"@itwin/core-common\";\r\nimport { MobileRpcGateway, MobileRpcProtocol } from \"../common/MobileRpcProtocol\";\r\nimport { MobileRpcConfiguration } from \"../common/MobileRpcManager\";\r\nimport { MobileHost } from \"./MobileHost\";\r\nimport { ProcessDetector } from \"@itwin/core-bentley\";\r\n\r\n/* eslint-disable deprecation/deprecation */\r\n\r\ninterface MobileAddon {\r\n notifyListening: (port: number) => void;\r\n registerDeviceImpl: () => void;\r\n}\r\n\r\nlet addon: MobileAddon | undefined;\r\n\r\n/** @internal */\r\nexport class MobileRpcServer {\r\n private static _nextId = -1;\r\n\r\n public static interop: MobileRpcGateway = {\r\n handler: (_payload: ArrayBuffer | string) => { throw new IModelError(BentleyStatus.ERROR, \"Not implemented.\"); },\r\n sendString: (_message: string, _connectionId: number) => { throw new IModelError(BentleyStatus.ERROR, \"No connection.\"); },\r\n sendBinary: (_message: Uint8Array, _connectionId: number) => { throw new IModelError(BentleyStatus.ERROR, \"No connection.\"); },\r\n port: 0,\r\n connectionId: 0,\r\n };\r\n\r\n private _server: ws.Server;\r\n private _connection: ws | undefined;\r\n private _port: number;\r\n private _connectionId: number;\r\n private _pingTimer: NodeJS.Timeout;\r\n public constructor() {\r\n /* _pingTime is a fix for ios/mobile case where when the app moves into foreground from\r\n * background backend restart ws.Server and then notify frontend to reconnect. But ws.Server\r\n * listening event is not fired as node yield to kevent and wait for some io event to happen.\r\n * This causes a delay in reconnection which may be as long a 40 secs. To solve the issue we\r\n * create _pingTimer which causes kevent to yield back to uv poll so timer event can be fired.\r\n * This allow listening event to go through quickly (max 5ms). Once the listening event occur we\r\n * clear the timer. Here we use setInterval() just to make sure otherwise setTimeout() could equally\r\n * be effective\r\n */\r\n this._pingTimer = setInterval(() => { }, 5);\r\n this._port = MobileRpcConfiguration.setup.obtainPort();\r\n this._server = new ws.Server({ port: this._port });\r\n this._connectionId = ++MobileRpcServer._nextId;\r\n MobileRpcServer.interop.connectionId = this._connectionId;\r\n this._onListening();\r\n this._onConnection();\r\n }\r\n\r\n private _onListening() {\r\n this._server.on(\"listening\", () => {\r\n const address = this._server.address() as ws.AddressInfo;\r\n this._port = address.port;\r\n clearInterval(this._pingTimer);\r\n this._notifyListening();\r\n });\r\n }\r\n\r\n private _notifyListening() {\r\n MobileRpcServer.interop.port = this._port;\r\n\r\n if (addon) {\r\n addon.notifyListening(this._port);\r\n }\r\n\r\n if (this._connectionId !== 0) {\r\n MobileHost.reconnect(this._port);\r\n }\r\n }\r\n\r\n private _onConnection() {\r\n this._server.on(\"connection\", (connection) => {\r\n this._connection = connection;\r\n this._connection.on(\"message\", (data) => this._onConnectionMessage(data));\r\n this._createSender();\r\n (global as any).__iTwinJsRpcReady = true;\r\n });\r\n }\r\n\r\n private _createSender() {\r\n const sender = (message: string | Uint8Array, connectionId: number) => {\r\n if (connectionId !== this._connectionId) {\r\n return;\r\n }\r\n\r\n this._connection!.send(message, (err) => {\r\n if (err) {\r\n throw err;\r\n }\r\n });\r\n };\r\n\r\n MobileRpcServer.interop.sendString = sender;\r\n MobileRpcServer.interop.sendBinary = sender;\r\n }\r\n\r\n private _onConnectionMessage(data: ws.Data) {\r\n let message = data;\r\n if (Array.isArray(message)) {\r\n throw new IModelError(BentleyStatus.ERROR, \"Unsupported data type\");\r\n }\r\n\r\n if (Buffer.isBuffer(message)) {\r\n if (message.byteOffset !== 0 || message.byteLength !== message.buffer.byteLength) {\r\n message = new Uint8Array(message.buffer, message.byteOffset, message.byteLength);\r\n } else {\r\n message = message.buffer;\r\n }\r\n }\r\n\r\n MobileRpcServer.interop.handler(message, this._connectionId);\r\n }\r\n\r\n public dispose() {\r\n clearInterval(this._pingTimer);\r\n if (this._connection) {\r\n MobileRpcServer.interop.sendString = () => { };\r\n MobileRpcServer.interop.sendBinary = () => { };\r\n this._connection.close();\r\n }\r\n\r\n this._server.close();\r\n }\r\n}\r\n\r\nlet mobileReady = false;\r\nlet hasSuspended = false;\r\n\r\nexport function setupMobileRpc() {\r\n if (mobileReady) {\r\n return;\r\n }\r\n\r\n if (ProcessDetector.isMobileAppBackend) {\r\n addon = (process as any)._linkedBinding(\"iModelJsMobile\");\r\n addon?.registerDeviceImpl();\r\n }\r\n\r\n let server: MobileRpcServer | null = new MobileRpcServer();\r\n\r\n /* The UV event loop (internal to node) is retained by handles,\r\n such as those created for setInterval/setTimeout and client/server connections.\r\n In a simple app, the RPC server may be the only handle retaining the UV loop.\r\n Thus, we install a temporary timer on suspend to prevent the loop from exiting prematurely.\r\n */\r\n let retainUvLoop: NodeJS.Timer | undefined;\r\n\r\n MobileHost.onEnterBackground.addListener(() => {\r\n hasSuspended = true;\r\n\r\n if (server === null) {\r\n return;\r\n }\r\n\r\n retainUvLoop = setInterval(() => { }, 1000);\r\n server.dispose();\r\n server = null;\r\n });\r\n\r\n MobileHost.onEnterForeground.addListener(() => {\r\n if (!hasSuspended) {\r\n return;\r\n }\r\n\r\n server = new MobileRpcServer();\r\n clearInterval(retainUvLoop);\r\n retainUvLoop = undefined;\r\n });\r\n\r\n MobileHost.onWillTerminate.addListener(() => {\r\n if (typeof (retainUvLoop) !== \"undefined\") {\r\n clearInterval(retainUvLoop);\r\n retainUvLoop = undefined;\r\n }\r\n\r\n if (server === null) {\r\n return;\r\n }\r\n\r\n server.dispose();\r\n server = null;\r\n });\r\n\r\n MobileRpcProtocol.obtainInterop = () => MobileRpcServer.interop;\r\n mobileReady = true;\r\n}\r\n"]}
1
+ {"version":3,"file":"MobileRpcServer.js","sourceRoot":"","sources":["../../../src/backend/MobileRpcServer.ts"],"names":[],"m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IAAI,MAAM,KAAK,IAAI,EAAE;YACnB,OAAO;SACR;QAED,YAAY,GAAG,WAAW,CAAC,GAAG,EAAE,GAAG,CAAC,EAAE,IAAI,CAAC,CAAC;QAC5C,MAAM,CAAC,OAAO,EAAE,CAAC;QACjB,gBAAgB,EAAE,CAAC;QACnB,MAAM,GAAG,IAAI,CAAC;IAChB,CAAC,CAAC,CAAC;IAEH,uBAAU,CAAC,iBAAiB,CAAC,WAAW,CAAC,GAAG,EAAE;QAC5C,IAAI,CAAC,YAAY,EAAE;YACjB,OAAO;SACR;QAED,MAAM,GAAG,IAAI,eAAe,CAAC,eAAe,CAAC,CAAC;QAC9C,aAAa,CAAC,YAAY,CAAC,CAAC;QAC5B,YAAY,GAAG,SAAS,CAAC;IAC3B,CAAC,CAAC,CAAC;IAEH,uBAAU,CAAC,eAAe,CAAC,WAAW,CAAC,GAAG,EAAE;QAC1C,IAAI,OAAO,CAAC,YAAY,CAAC,KAAK,WAAW,EAAE;YACzC,aAAa,CAAC,YAAY,CAAC,CAAC;YAC5B,YAAY,GAAG,SAAS,CAAC;SAC1B;QAED,IAAI,MAAM,KAAK,IAAI,EAAE;YACnB,OAAO;SACR;QAED,MAAM,CAAC,OAAO,EAAE,CAAC;QACjB,MAAM,GAAG,IAAI,CAAC;IAChB,CAAC,CAAC,CAAC;IAEH,qCAAiB,CAAC,aAAa,GAAG,GAAG,EAAE,CAAC,eAAe,CAAC,OAAO,CAAC;IAChE,WAAW,GAAG,IAAI,CAAC;AACrB,CAAC;AAnED,wCAmEC","sourcesContent":["/*---------------------------------------------------------------------------------------------\r\n* Copyright (c) Bentley Systems, Incorporated. All rights reserved.\r\n* See LICENSE.md in the project root for license terms and full copyright notice.\r\n*--------------------------------------------------------------------------------------------*/\r\n\r\nimport * as ws from \"ws\";\r\nimport { BentleyStatus, IModelError } from \"@itwin/core-common\";\r\nimport { MobileRpcGateway, MobileRpcProtocol } from \"../common/MobileRpcProtocol\";\r\nimport { MobileRpcConfiguration } from \"../common/MobileRpcManager\";\r\nimport { MobileHost } from \"./MobileHost\";\r\nimport { ProcessDetector } from \"@itwin/core-bentley\";\r\n\r\n/* eslint-disable deprecation/deprecation */\r\n\r\ninterface MobileAddon {\r\n notifyListening: (port: number) => void;\r\n registerDeviceImpl: () => void;\r\n}\r\n\r\nlet addon: MobileAddon | undefined;\r\n\r\n/** @internal */\r\nexport class MobileRpcServer {\r\n private static _nextId = -1;\r\n\r\n public static interop: MobileRpcGateway = {\r\n handler: (_payload: ArrayBuffer | string) => { throw new IModelError(BentleyStatus.ERROR, \"Not implemented.\"); },\r\n sendString: (_message: string, _connectionId: number) => { throw new IModelError(BentleyStatus.ERROR, \"No connection.\"); },\r\n sendBinary: (_message: Uint8Array, _connectionId: number) => { throw new IModelError(BentleyStatus.ERROR, \"No connection.\"); },\r\n port: 0,\r\n connectionId: 0,\r\n };\r\n\r\n private _server: ws.Server;\r\n private _connection: ws | undefined;\r\n private _port: number;\r\n private _connectionId: number;\r\n private _pingTimer: NodeJS.Timeout;\r\n public constructor(private _pendingMessages: Array<string | Uint8Array> | undefined = undefined) {\r\n /* _pingTime is a fix for ios/mobile case where when the app moves into foreground from\r\n * background backend restart ws.Server and then notify frontend to reconnect. But ws.Server\r\n * listening event is not fired as node yield to kevent and wait for some io event to happen.\r\n * This causes a delay in reconnection which may be as long a 40 secs. To solve the issue we\r\n * create _pingTimer which causes kevent to yield back to uv poll so timer event can be fired.\r\n * This allow listening event to go through quickly (max 5ms). Once the listening event occur we\r\n * clear the timer. Here we use setInterval() just to make sure otherwise setTimeout() could equally\r\n * be effective\r\n */\r\n this._pingTimer = setInterval(() => { }, 5);\r\n this._port = MobileRpcConfiguration.setup.obtainPort();\r\n this._server = new ws.Server({ port: this._port });\r\n this._connectionId = ++MobileRpcServer._nextId;\r\n MobileRpcServer.interop.connectionId = this._connectionId;\r\n this._onListening();\r\n this._onConnection();\r\n }\r\n\r\n private _onListening() {\r\n this._server.on(\"listening\", () => {\r\n const address = this._server.address() as ws.AddressInfo;\r\n this._port = address.port;\r\n clearInterval(this._pingTimer);\r\n this._notifyListening();\r\n });\r\n }\r\n\r\n private _notifyListening() {\r\n MobileRpcServer.interop.port = this._port;\r\n\r\n if (addon) {\r\n addon.notifyListening(this._port);\r\n }\r\n\r\n if (this._connectionId !== 0) {\r\n MobileHost.reconnect(this._port);\r\n }\r\n }\r\n\r\n private _onConnection() {\r\n this._server.on(\"connection\", (connection) => {\r\n this._connection = connection;\r\n this._connection.on(\"message\", (data) => this._onConnectionMessage(data));\r\n this._createSender();\r\n this._sendPending();\r\n (global as any).__iTwinJsRpcReady = true;\r\n });\r\n }\r\n\r\n private _createSender() {\r\n const sender = (message: string | Uint8Array, connectionId: number) => {\r\n if (connectionId !== this._connectionId) {\r\n return;\r\n }\r\n\r\n this._connection!.send(message, (err) => {\r\n if (err) {\r\n throw err;\r\n }\r\n });\r\n };\r\n\r\n MobileRpcServer.interop.sendString = sender;\r\n MobileRpcServer.interop.sendBinary = sender;\r\n }\r\n\r\n private _sendPending() {\r\n if (this._pendingMessages === undefined)\r\n return;\r\n\r\n for (const message of this._pendingMessages) {\r\n this._connection!.send(message, (err) => {\r\n if (err) {\r\n throw err;\r\n }\r\n });\r\n }\r\n this._pendingMessages.length = 0;\r\n }\r\n\r\n private _onConnectionMessage(data: ws.Data) {\r\n let message = data;\r\n if (Array.isArray(message)) {\r\n throw new IModelError(BentleyStatus.ERROR, \"Unsupported data type\");\r\n }\r\n\r\n if (Buffer.isBuffer(message)) {\r\n if (message.byteOffset !== 0 || message.byteLength !== message.buffer.byteLength) {\r\n message = new Uint8Array(message.buffer, message.byteOffset, message.byteLength);\r\n } else {\r\n message = message.buffer;\r\n }\r\n }\r\n\r\n MobileRpcServer.interop.handler(message, this._connectionId);\r\n }\r\n\r\n public dispose() {\r\n clearInterval(this._pingTimer);\r\n if (this._connection) {\r\n MobileRpcServer.interop.sendString = () => { };\r\n MobileRpcServer.interop.sendBinary = () => { };\r\n this._connection.close();\r\n }\r\n\r\n this._server.close();\r\n }\r\n}\r\n\r\nlet mobileReady = false;\r\nlet hasSuspended = false;\r\n\r\nexport function setupMobileRpc() {\r\n if (mobileReady) {\r\n return;\r\n }\r\n\r\n if (ProcessDetector.isMobileAppBackend) {\r\n addon = (process as any)._linkedBinding(\"iModelJsMobile\");\r\n addon?.registerDeviceImpl();\r\n }\r\n\r\n let server: MobileRpcServer | null = new MobileRpcServer();\r\n\r\n /* The UV event loop (internal to node) is retained by handles,\r\n such as those created for setInterval/setTimeout and client/server connections.\r\n In a simple app, the RPC server may be the only handle retaining the UV loop.\r\n Thus, we install a temporary timer on suspend to prevent the loop from exiting prematurely.\r\n */\r\n let retainUvLoop: NodeJS.Timer | undefined;\r\n const pendingMessages: Array<string | Uint8Array> = [];\r\n\r\n function usePendingSender() {\r\n const sender = (message: string | Uint8Array, _connectionId: number) => {\r\n pendingMessages.push(message);\r\n };\r\n MobileRpcServer.interop.sendString = sender;\r\n MobileRpcServer.interop.sendBinary = sender;\r\n }\r\n\r\n MobileHost.onEnterBackground.addListener(() => {\r\n hasSuspended = true;\r\n\r\n if (server === null) {\r\n return;\r\n }\r\n\r\n retainUvLoop = setInterval(() => { }, 1000);\r\n server.dispose();\r\n usePendingSender();\r\n server = null;\r\n });\r\n\r\n MobileHost.onEnterForeground.addListener(() => {\r\n if (!hasSuspended) {\r\n return;\r\n }\r\n\r\n server = new MobileRpcServer(pendingMessages);\r\n clearInterval(retainUvLoop);\r\n retainUvLoop = undefined;\r\n });\r\n\r\n MobileHost.onWillTerminate.addListener(() => {\r\n if (typeof (retainUvLoop) !== \"undefined\") {\r\n clearInterval(retainUvLoop);\r\n retainUvLoop = undefined;\r\n }\r\n\r\n if (server === null) {\r\n return;\r\n }\r\n\r\n server.dispose();\r\n server = null;\r\n });\r\n\r\n MobileRpcProtocol.obtainInterop = () => MobileRpcServer.interop;\r\n mobileReady = true;\r\n}\r\n"]}