|
1 | 1 | "use strict";
|
2 | 2 |
|
3 |
| -const uuid = require('node-uuid'); |
4 |
| -const EventEmitter = require("events"); |
5 |
| - |
6 |
| -const Constants = require("./constants"); |
7 |
| - |
8 |
| -const IOSDeviceLibStdioHandler = require("./ios-device-lib-stdio-handler").IOSDeviceLibStdioHandler; |
9 |
| - |
10 |
| -const MethodNames = { |
11 |
| - install: "install", |
12 |
| - uninstall: "uninstall", |
13 |
| - list: "list", |
14 |
| - log: "log", |
15 |
| - upload: "upload", |
16 |
| - download: "download", |
17 |
| - read: "read", |
18 |
| - delete: "delete", |
19 |
| - postNotification: "postNotification", |
20 |
| - awaitNotificationResponse: "awaitNotificationResponse", |
21 |
| - start: "start", |
22 |
| - stop: "stop", |
23 |
| - apps: "apps", |
24 |
| - connectToPort: "connectToPort" |
25 |
| -}; |
26 |
| - |
27 |
| -const Events = { |
28 |
| - deviceLogData: "deviceLogData" |
29 |
| -}; |
30 |
| - |
31 |
| -class IOSDeviceLib extends EventEmitter { |
32 |
| - constructor(onDeviceFound, onDeviceLost, options) { |
33 |
| - super(); |
34 |
| - this._options = options || {}; |
35 |
| - this._iosDeviceLibStdioHandler = new IOSDeviceLibStdioHandler(this._options); |
36 |
| - this._iosDeviceLibStdioHandler.startReadingData(); |
37 |
| - this._iosDeviceLibStdioHandler.on(Constants.DeviceFoundEventName, onDeviceFound); |
38 |
| - this._iosDeviceLibStdioHandler.on(Constants.DeviceLostEventName, onDeviceLost); |
39 |
| - } |
40 |
| - |
41 |
| - install(ipaPath, deviceIdentifiers) { |
42 |
| - return deviceIdentifiers.map(di => this._getPromise(MethodNames.install, [ipaPath, [di]])); |
43 |
| - } |
44 |
| - |
45 |
| - uninstall(appId, deviceIdentifiers) { |
46 |
| - return deviceIdentifiers.map(di => this._getPromise(MethodNames.uninstall, [appId, [di]])); |
47 |
| - } |
48 |
| - |
49 |
| - list(listArray) { |
50 |
| - return listArray.map(listObject => this._getPromise(MethodNames.list, [listObject])); |
51 |
| - } |
52 |
| - |
53 |
| - upload(uploadArray) { |
54 |
| - return uploadArray.map(uploadObject => this._getPromise(MethodNames.upload, [uploadObject])); |
55 |
| - } |
56 |
| - |
57 |
| - download(downloadArray) { |
58 |
| - return downloadArray.map(downloadObject => this._getPromise(MethodNames.download, [downloadObject])); |
59 |
| - } |
60 |
| - |
61 |
| - read(readArray) { |
62 |
| - return readArray.map(readObject => this._getPromise(MethodNames.read, [readObject])); |
63 |
| - } |
64 |
| - |
65 |
| - delete(deleteArray) { |
66 |
| - return deleteArray.map(deleteObject => this._getPromise(MethodNames.delete, [deleteObject])); |
67 |
| - } |
68 |
| - |
69 |
| - postNotification(postNotificationArray) { |
70 |
| - return postNotificationArray.map(notificationObject => this._getPromise(MethodNames.postNotification, [notificationObject])); |
71 |
| - } |
72 |
| - |
73 |
| - awaitNotificationResponse(awaitNotificationResponseArray) { |
74 |
| - return awaitNotificationResponseArray.map(awaitNotificationObject => this._getPromise(MethodNames.awaitNotificationResponse, [awaitNotificationObject])); |
75 |
| - } |
76 |
| - |
77 |
| - apps(deviceIdentifiers) { |
78 |
| - return deviceIdentifiers.map(di => this._getPromise(MethodNames.apps, [di])); |
79 |
| - } |
80 |
| - |
81 |
| - start(startArray) { |
82 |
| - return startArray.map(startObject => this._getPromise(MethodNames.start, [startObject])); |
83 |
| - } |
84 |
| - |
85 |
| - stop(stopArray) { |
86 |
| - return stopArray.map(stopObject => this._getPromise(MethodNames.stop, [stopObject])); |
87 |
| - } |
88 |
| - |
89 |
| - startDeviceLog(deviceIdentifiers) { |
90 |
| - this._getPromise(MethodNames.log, deviceIdentifiers, { shouldEmit: true, disregardTimeout: true, doNotFailOnDeviceLost: true }); |
91 |
| - } |
92 |
| - |
93 |
| - connectToPort(connectToPortArray) { |
94 |
| - return connectToPortArray.map(connectToPortObject => this._getPromise(MethodNames.connectToPort, [connectToPortObject])); |
95 |
| - } |
96 |
| - |
97 |
| - dispose(signal) { |
98 |
| - this.removeAllListeners(); |
99 |
| - this._iosDeviceLibStdioHandler.dispose(signal); |
100 |
| - } |
101 |
| - |
102 |
| - _getPromise(methodName, args, options = {}) { |
103 |
| - return new Promise((resolve, reject) => { |
104 |
| - if (!args || !args.length) { |
105 |
| - return reject(new Error("No arguments provided")); |
106 |
| - } |
107 |
| - |
108 |
| - let timer = null; |
109 |
| - let eventHandler = null; |
110 |
| - let deviceLostHandler = null; |
111 |
| - const id = uuid.v4(); |
112 |
| - const removeListeners = () => { |
113 |
| - if (eventHandler) { |
114 |
| - this._iosDeviceLibStdioHandler.removeListener(Constants.DataEventName, eventHandler); |
115 |
| - } |
116 |
| - |
117 |
| - if (deviceLostHandler) { |
118 |
| - this._iosDeviceLibStdioHandler.removeListener(Constants.DeviceLostEventName, deviceLostHandler); |
119 |
| - } |
120 |
| - }; |
121 |
| - |
122 |
| - // In case device is lost during waiting for operation to complete |
123 |
| - // or in case we do not execute operation in the specified timeout |
124 |
| - // remove all handlers and reject the promise. |
125 |
| - // NOTE: This is not applicable for device logs, where the Promise is not awaited |
126 |
| - // Rejecting it results in Unhandled Rejection |
127 |
| - const handleMessage = (message) => { |
128 |
| - removeListeners(); |
129 |
| - message.error ? reject(message.error) : resolve(message); |
130 |
| - }; |
131 |
| - |
132 |
| - deviceLostHandler = (device) => { |
133 |
| - let message = `Device ${device.deviceId} lost during operation ${methodName} for message ${id}`; |
134 |
| - |
135 |
| - if (!options.doNotFailOnDeviceLost) { |
136 |
| - message = { error: new Error(message) }; |
137 |
| - } |
138 |
| - |
139 |
| - handleMessage(message); |
140 |
| - }; |
141 |
| - |
142 |
| - eventHandler = (message) => { |
143 |
| - if (message && message.id === id) { |
144 |
| - if (timer) { |
145 |
| - clearTimeout(timer); |
146 |
| - } |
147 |
| - |
148 |
| - delete message.id; |
149 |
| - if (options && options.shouldEmit) { |
150 |
| - this.emit(Events.deviceLogData, message); |
151 |
| - } else { |
152 |
| - handleMessage(message); |
153 |
| - } |
154 |
| - } |
155 |
| - }; |
156 |
| - |
157 |
| - if (this._options.timeout && !options.disregardTimeout) { |
158 |
| - // TODO: Check if we should clear the timers when dispose is called. |
159 |
| - timer = setTimeout(() => { |
160 |
| - handleMessage({ error: new Error(`Timeout waiting for ${methodName} response from ios-device-lib, message id: ${id}.`) }); |
161 |
| - }, this._options.timeout); |
162 |
| - } |
163 |
| - |
164 |
| - this._iosDeviceLibStdioHandler.on(Constants.DataEventName, eventHandler); |
165 |
| - this._iosDeviceLibStdioHandler.on(Constants.DeviceLostEventName, deviceLostHandler); |
166 |
| - |
167 |
| - this._iosDeviceLibStdioHandler.writeData(this._getMessage(id, methodName, args)); |
168 |
| - }); |
169 |
| - } |
170 |
| - |
171 |
| - _getMessage(id, name, args) { |
172 |
| - return JSON.stringify({ methods: [{ id: id, name: name, args: args }] }) + '\n'; |
173 |
| - } |
174 |
| -} |
| 3 | +const IOSDeviceLib = require("./ios-device-lib").IOSDeviceLib; |
| 4 | +const MessageUnpackStream = require("./message-unpack-stream").MessageUnpackStream; |
175 | 5 |
|
176 | 6 | exports.IOSDeviceLib = IOSDeviceLib;
|
| 7 | +exports.MessageUnpackStream = MessageUnpackStream; |
0 commit comments