appium-remote-debugger 12.2.9 → 13.0.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.
Files changed (61) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/build/lib/mixins/connect.d.ts.map +1 -1
  3. package/build/lib/mixins/connect.js +32 -37
  4. package/build/lib/mixins/connect.js.map +1 -1
  5. package/build/lib/mixins/execute.d.ts.map +1 -1
  6. package/build/lib/mixins/execute.js +14 -12
  7. package/build/lib/mixins/execute.js.map +1 -1
  8. package/build/lib/mixins/navigate.d.ts.map +1 -1
  9. package/build/lib/mixins/navigate.js +9 -96
  10. package/build/lib/mixins/navigate.js.map +1 -1
  11. package/build/lib/protocol/index.d.ts +3 -1
  12. package/build/lib/protocol/index.d.ts.map +1 -1
  13. package/build/lib/protocol/index.js +22 -23
  14. package/build/lib/protocol/index.js.map +1 -1
  15. package/build/lib/rpc/index.d.ts +1 -2
  16. package/build/lib/rpc/index.d.ts.map +1 -1
  17. package/build/lib/rpc/index.js +1 -36
  18. package/build/lib/rpc/index.js.map +1 -1
  19. package/build/lib/rpc/remote-messages.d.ts +24 -18
  20. package/build/lib/rpc/remote-messages.d.ts.map +1 -1
  21. package/build/lib/rpc/remote-messages.js +24 -11
  22. package/build/lib/rpc/remote-messages.js.map +1 -1
  23. package/build/lib/rpc/rpc-client-real-device.d.ts.map +1 -1
  24. package/build/lib/rpc/rpc-client-real-device.js +1 -3
  25. package/build/lib/rpc/rpc-client-real-device.js.map +1 -1
  26. package/build/lib/rpc/rpc-client-simulator.d.ts.map +1 -1
  27. package/build/lib/rpc/rpc-client-simulator.js +1 -3
  28. package/build/lib/rpc/rpc-client-simulator.js.map +1 -1
  29. package/build/lib/rpc/rpc-client.d.ts +63 -44
  30. package/build/lib/rpc/rpc-client.d.ts.map +1 -1
  31. package/build/lib/rpc/rpc-client.js +209 -122
  32. package/build/lib/rpc/rpc-client.js.map +1 -1
  33. package/build/lib/rpc/rpc-message-handler.d.ts +14 -6
  34. package/build/lib/rpc/rpc-message-handler.d.ts.map +1 -1
  35. package/build/lib/rpc/rpc-message-handler.js +70 -68
  36. package/build/lib/rpc/rpc-message-handler.js.map +1 -1
  37. package/build/lib/types.d.ts +28 -3
  38. package/build/lib/types.d.ts.map +1 -1
  39. package/build/lib/utils.d.ts +4 -3
  40. package/build/lib/utils.d.ts.map +1 -1
  41. package/build/lib/utils.js +4 -2
  42. package/build/lib/utils.js.map +1 -1
  43. package/build/tsconfig.tsbuildinfo +1 -1
  44. package/lib/mixins/connect.js +34 -38
  45. package/lib/mixins/execute.js +15 -12
  46. package/lib/mixins/navigate.js +11 -99
  47. package/lib/protocol/index.js +22 -24
  48. package/lib/rpc/index.js +1 -3
  49. package/lib/rpc/remote-messages.js +28 -11
  50. package/lib/rpc/rpc-client-real-device.js +1 -3
  51. package/lib/rpc/rpc-client-simulator.js +1 -3
  52. package/lib/rpc/rpc-client.js +244 -133
  53. package/lib/rpc/rpc-message-handler.js +71 -67
  54. package/lib/types.ts +33 -3
  55. package/lib/utils.js +4 -2
  56. package/package.json +2 -1
  57. package/build/lib/rpc/constants.d.ts +0 -2
  58. package/build/lib/rpc/constants.d.ts.map +0 -1
  59. package/build/lib/rpc/constants.js +0 -5
  60. package/build/lib/rpc/constants.js.map +0 -1
  61. package/lib/rpc/constants.js +0 -1
@@ -107,18 +107,17 @@ export default class RpcMessageHandler extends EventEmitters {
107
107
  /**
108
108
  * Dispatch a data message.
109
109
  *
110
- * @param {string | undefined} msgId
111
- * @param {string} method
112
- * @param {import('@appium/types').StringRecord} params
110
+ * @param {string} msgId If not empty then the following event is going to be emitted:
111
+ * - <msgId, error, result>
112
+ * If empty then the following event is going to be emitted:
113
+ * - <name, error, ..args>
114
+ * @param {string | undefined} method
115
+ * @param {import('@appium/types').StringRecord | undefined} params
113
116
  * @param {any} result
114
- * @param {Error} [error]
117
+ * @param {Error | undefined} error
115
118
  * @returns {Promise<void>}
116
119
  */
117
120
  async dispatchDataMessage (msgId, method, params, result, error) {
118
- if (!_.isEmpty(msgId)) {
119
- log.debug(`Handling message (id: '${msgId}')`);
120
- }
121
-
122
121
  if (msgId) {
123
122
  if (this.listenerCount(msgId)) {
124
123
  if (_.has(result?.result, 'value')) {
@@ -134,7 +133,7 @@ export default class RpcMessageHandler extends EventEmitters {
134
133
  return;
135
134
  }
136
135
 
137
- /** @type {string[]} */
136
+ /** @type {any[]} */
138
137
  const eventNames = [method];
139
138
  /** @type {any[]} */
140
139
  let args = [params];
@@ -152,10 +151,10 @@ export default class RpcMessageHandler extends EventEmitters {
152
151
  args = [params || params.record];
153
152
  break;
154
153
  case 'Console.messageAdded':
155
- args = [params.message];
154
+ args = [params?.message];
156
155
  break;
157
156
  case 'Runtime.executionContextCreated':
158
- args = [params.context];
157
+ args = [params?.context];
159
158
  break;
160
159
  default:
161
160
  // pass
@@ -190,67 +189,65 @@ export default class RpcMessageHandler extends EventEmitters {
190
189
  let result = dataKey.result;
191
190
 
192
191
  let method = dataKey.method;
193
- let params;
192
+ let params = dataKey.params;
194
193
 
195
- if (method === 'Target.targetCreated') {
196
- // this is in response to a `_rpc_forwardSocketSetup:` call
197
- // targetInfo: { targetId: 'page-1', type: 'page' }
198
- const app = plist.__argument.WIRApplicationIdentifierKey;
199
- const targetInfo = dataKey.params.targetInfo;
200
- this.emit('Target.targetCreated', null, app, targetInfo);
201
- return;
202
- } else if (method === 'Target.didCommitProvisionalTarget') {
203
- const app = plist.__argument.WIRApplicationIdentifierKey;
204
- const oldTargetId = dataKey.params.oldTargetId;
205
- const newTargetId = dataKey.params.newTargetId;
206
- this.emit('Target.didCommitProvisionalTarget', null, app, oldTargetId, newTargetId);
207
- return;
208
- } else if (method === 'Target.targetDestroyed') {
209
- const app = plist.__argument.WIRApplicationIdentifierKey;
210
- const targetInfo = dataKey.params.targetInfo || {targetId: dataKey.params.targetId};
211
- this.emit('Target.targetDestroyed', null, app, targetInfo);
212
- return;
213
- }
194
+ const parseError = () => {
195
+ const defaultMessage = 'Error occurred in handling data message';
196
+ if (result?.wasThrown) {
197
+ const message = (result?.result?.value || result?.result?.description)
198
+ ? (result?.result?.value || result?.result?.description)
199
+ : (dataKey.error ?? defaultMessage);
200
+ return new Error(message);
201
+ }
202
+ if (dataKey.error) {
203
+ if (_.isPlainObject(dataKey.error)) {
204
+ const dataKeyError = /** @type {DataErrorMessage} */ (dataKey.error);
205
+ let error = new Error(defaultMessage);
206
+ for (const key of Object.keys(dataKeyError)) {
207
+ error[key] = dataKeyError[key];
208
+ }
209
+ return error;
210
+ }
211
+ return new Error(String(dataKey.error || defaultMessage));
212
+ }
213
+ return undefined;
214
+ };
214
215
 
215
- if (!dataKey.error && this.isTargetBased) {
216
- if (dataKey.method !== 'Target.dispatchMessageFromTarget') {
217
- // this sort of message, at this point, is just an acknowledgement
218
- // that the original message was received
216
+ switch (method) {
217
+ case 'Target.targetCreated':
218
+ case 'Target.targetDestroyed':
219
+ case 'Target.didCommitProvisionalTarget': {
220
+ const app = plist.__argument.WIRApplicationIdentifierKey;
221
+ const args = method === 'Target.didCommitProvisionalTarget'
222
+ ? params
223
+ : (params.targetInfo ?? {targetId: params.targetId});
224
+ this.emit(method, null, app, args);
219
225
  return;
220
226
  }
227
+ case 'Target.dispatchMessageFromTarget': {
228
+ if (!dataKey.error && this.isTargetBased) {
229
+ try {
230
+ const message = JSON.parse(dataKey.params.message);
231
+ msgId = _.isUndefined(message.id) ? '' : String(message.id);
232
+ method = message.method;
233
+ result = message.result || message;
234
+ params = result.params;
235
+ } catch (err) {
236
+ // if this happens then some aspect of the protocol is missing to us
237
+ // so print the entire message to get visibiity into what is going on
238
+ log.error(`Unexpected message format from Web Inspector: ${util.jsonStringify(plist, null)}`);
239
+ throw err;
240
+ }
241
+ }
221
242
 
222
- // at this point, we have a Target-based message wrapping a protocol message
223
- let message;
224
- try {
225
- message = JSON.parse(dataKey.params.message);
226
- msgId = message.id;
227
- method = message.method;
228
- result = message.result || message;
229
- params = result.params;
230
- } catch (err) {
231
- // if this happens then some aspect of the protocol is missing to us
232
- // so print the entire message to get visibiity into what is going on
233
- log.error(`Unexpected message format from Web Inspector:`);
234
- log.warn(util.jsonStringify(plist, null));
235
- throw err;
243
+ await this.dispatchDataMessage(msgId, method, params, result, parseError());
244
+ return;
236
245
  }
237
- } else {
238
- params = dataKey.params;
239
- }
240
-
241
- // we can get an error, or we can get a response that is an error
242
- let error;
243
- if (result?.wasThrown) {
244
- const message = (result?.result?.value || result?.result?.description)
245
- ? (result?.result?.value || result?.result?.description)
246
- : (dataKey.error ?? 'Error occurred in handling data message');
247
- error = new Error(message);
248
- } else if (dataKey.error) {
249
- error = new Error(dataKey.error);
250
- }
251
-
252
- await this.dispatchDataMessage(msgId, method, params, result, error);
253
- }
246
+ default: {
247
+ await this.dispatchDataMessage(msgId, method, params, result, parseError());
248
+ }
249
+ } // switch
250
+ } // function
254
251
  }
255
252
 
256
253
  /**
@@ -259,5 +256,12 @@ export default class RpcMessageHandler extends EventEmitters {
259
256
  * @property {string} method
260
257
  * @property {import('@appium/types').StringRecord} params
261
258
  * @property {any} result
262
- * @property {string} [error]
259
+ * @property {string | DataErrorMessage} [error]
260
+ */
261
+
262
+ /**
263
+ * @typedef {Object} DataErrorMessage
264
+ * @property {string} message
265
+ * @property {number} code
266
+ * @property {any} data
263
267
  */
package/lib/types.ts CHANGED
@@ -83,6 +83,7 @@ export type RemoteDebuggerRealDeviceOptions = RemoteDebuggerRealDeviceSpecificOp
83
83
 
84
84
  export type AppIdKey = string | number;
85
85
  export type PageIdKey = string | number;
86
+ export type TargetId = string;
86
87
 
87
88
  export interface RemoteCommandOpts {
88
89
  appIdKey?: AppIdKey;
@@ -90,7 +91,7 @@ export interface RemoteCommandOpts {
90
91
  id?: string;
91
92
  connId?: string;
92
93
  senderId?: string;
93
- targetId?: string;
94
+ targetId?: TargetId;
94
95
  bundleId?: string;
95
96
  enabled?: boolean;
96
97
  [key: string]: any;
@@ -102,7 +103,36 @@ export interface ProtocolCommandOpts {
102
103
  params: StringRecord;
103
104
  }
104
105
 
105
- export interface RemoteCommand {
106
- __argument: StringRecord;
106
+ type SocketDataKey = Buffer | StringRecord;
107
+
108
+ interface RemoteCommandArgument<T extends SocketDataKey> {
109
+ WIRSocketDataKey?: T;
110
+ WIRConnectionIdentifierKey?: string;
111
+ WIRSenderKey?: string;
112
+ WIRApplicationIdentifierKey?: AppIdKey;
113
+ WIRPageIdentifierKey?: PageIdKey;
114
+ WIRMessageDataTypeKey?: string;
115
+ WIRDestinationKey?: string;
116
+ WIRMessageDataKey?: string;
117
+ [key: string]: any;
118
+ }
119
+
120
+ interface RemoteCommandTemplated<T extends SocketDataKey> {
121
+ __argument: RemoteCommandArgument<T>;
107
122
  __selector: string;
108
123
  }
124
+
125
+ export type RawRemoteCommand = RemoteCommandTemplated<StringRecord>;
126
+ export type RemoteCommand = RemoteCommandTemplated<Buffer>;
127
+
128
+ export interface TargetInfo {
129
+ targetId: string;
130
+ type: 'page' | 'service-worker' | 'worker';
131
+ isProvisional: boolean;
132
+ isPaused: boolean;
133
+ }
134
+
135
+ export interface ProvisionalTargetInfo {
136
+ oldTargetId: string;
137
+ newTargetId: string;
138
+ }
package/lib/utils.js CHANGED
@@ -96,8 +96,9 @@ export function appIdsForBundle (bundleId, appDict) {
96
96
  }
97
97
 
98
98
  /**
99
- * @param {import('@appium/types').StringRecord} params
100
- * @returns {void}
99
+ * @template {import('@appium/types').StringRecord} T
100
+ * @param {T} params
101
+ * @returns {T}
101
102
  */
102
103
  export function checkParams (params) {
103
104
  // check if all parameters have a value
@@ -107,6 +108,7 @@ export function checkParams (params) {
107
108
  if (errors.length) {
108
109
  throw new Error(`Missing ${util.pluralize('parameter', errors.length)}: ${errors.join(', ')}`);
109
110
  }
111
+ return params;
110
112
  }
111
113
 
112
114
  /**
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "keywords": [
5
5
  "appium"
6
6
  ],
7
- "version": "12.2.9",
7
+ "version": "13.0.0",
8
8
  "author": "Appium Contributors",
9
9
  "license": "Apache-2.0",
10
10
  "repository": {
@@ -37,6 +37,7 @@
37
37
  "@appium/support": "^6.0.0",
38
38
  "appium-ios-device": "^2.0.0",
39
39
  "asyncbox": "^3.0.0",
40
+ "async-lock": "^1.4.1",
40
41
  "bluebird": "^3.4.7",
41
42
  "glob": "^10.3.3",
42
43
  "lodash": "^4.17.11",
@@ -1,2 +0,0 @@
1
- export const ON_TARGET_PROVISIONED_EVENT: "onTargetProvisioned";
2
- //# sourceMappingURL=constants.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../../../lib/rpc/constants.js"],"names":[],"mappings":"AAAA,0CAA2C,qBAAqB,CAAC"}
@@ -1,5 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ON_TARGET_PROVISIONED_EVENT = void 0;
4
- exports.ON_TARGET_PROVISIONED_EVENT = 'onTargetProvisioned';
5
- //# sourceMappingURL=constants.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"constants.js","sourceRoot":"","sources":["../../../lib/rpc/constants.js"],"names":[],"mappings":";;;AAAa,QAAA,2BAA2B,GAAG,qBAAqB,CAAC"}
@@ -1 +0,0 @@
1
- export const ON_TARGET_PROVISIONED_EVENT = 'onTargetProvisioned';