node-miio 0.3.0-beta.2 → 0.3.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 (253) hide show
  1. package/AGENTS.md +117 -0
  2. package/README.md +41 -2
  3. package/dist/cli/commands/configure.js +60 -0
  4. package/dist/cli/commands/configure.js.map +1 -0
  5. package/dist/cli/commands/control.js +47 -0
  6. package/dist/cli/commands/control.js.map +1 -0
  7. package/dist/cli/commands/discover.js +32 -0
  8. package/dist/cli/commands/discover.js.map +1 -0
  9. package/dist/cli/commands/inspect.js +183 -0
  10. package/dist/cli/commands/inspect.js.map +1 -0
  11. package/dist/cli/commands/miot-spec.js +60 -0
  12. package/dist/cli/commands/miot-spec.js.map +1 -0
  13. package/dist/cli/commands/protocol/call.js +59 -0
  14. package/dist/cli/commands/protocol/call.js.map +1 -0
  15. package/dist/cli/commands/protocol/json-dump.js +56 -0
  16. package/dist/cli/commands/protocol/json-dump.js.map +1 -0
  17. package/dist/cli/commands/protocol/packet.js +30 -0
  18. package/dist/cli/commands/protocol/packet.js.map +1 -0
  19. package/dist/cli/commands/protocol.js +49 -0
  20. package/dist/cli/commands/protocol.js.map +1 -0
  21. package/dist/cli/commands/tokens/update.js +53 -0
  22. package/dist/cli/commands/tokens/update.js.map +1 -0
  23. package/dist/cli/commands/tokens.js +44 -0
  24. package/dist/cli/commands/tokens.js.map +1 -0
  25. package/dist/cli/device-finder.js +72 -0
  26. package/dist/cli/device-finder.js.map +1 -0
  27. package/dist/cli/index.js +64 -0
  28. package/dist/cli/index.js.map +1 -0
  29. package/dist/cli/log.js +69 -0
  30. package/dist/cli/log.js.map +1 -0
  31. package/dist/lib/check_result.js +18 -0
  32. package/dist/lib/check_result.js.map +1 -0
  33. package/dist/lib/connect_to_device.js +123 -0
  34. package/dist/lib/connect_to_device.js.map +1 -0
  35. package/dist/lib/device.js +259 -0
  36. package/dist/lib/device.js.map +1 -0
  37. package/dist/lib/device_info.js +297 -0
  38. package/dist/lib/device_info.js.map +1 -0
  39. package/dist/lib/devices/air-monitor.js +42 -0
  40. package/dist/lib/devices/air-monitor.js.map +1 -0
  41. package/dist/lib/devices/air-purifier.js +144 -0
  42. package/dist/lib/devices/air-purifier.js.map +1 -0
  43. package/dist/lib/devices/capabilities/battery-level.js +13 -0
  44. package/dist/lib/devices/capabilities/battery-level.js.map +1 -0
  45. package/dist/lib/devices/capabilities/buzzer.js +69 -0
  46. package/dist/lib/devices/capabilities/buzzer.js.map +1 -0
  47. package/dist/lib/devices/capabilities/changeable-led-brightness.js +44 -0
  48. package/dist/lib/devices/capabilities/changeable-led-brightness.js.map +1 -0
  49. package/dist/lib/devices/capabilities/colorable.js +14 -0
  50. package/dist/lib/devices/capabilities/colorable.js.map +1 -0
  51. package/dist/lib/devices/capabilities/dimmable.js +14 -0
  52. package/dist/lib/devices/capabilities/dimmable.js.map +1 -0
  53. package/dist/lib/devices/capabilities/mode.js +13 -0
  54. package/dist/lib/devices/capabilities/mode.js.map +1 -0
  55. package/dist/lib/devices/capabilities/power.js +13 -0
  56. package/dist/lib/devices/capabilities/power.js.map +1 -0
  57. package/dist/lib/devices/capabilities/sensor.js +46 -0
  58. package/dist/lib/devices/capabilities/sensor.js.map +1 -0
  59. package/dist/lib/devices/capabilities/switchable-led.js +48 -0
  60. package/dist/lib/devices/capabilities/switchable-led.js.map +1 -0
  61. package/dist/lib/devices/chuangmi.plug.v1.js +47 -0
  62. package/dist/lib/devices/chuangmi.plug.v1.js.map +1 -0
  63. package/dist/lib/devices/eyecare-lamp2.js +151 -0
  64. package/dist/lib/devices/eyecare-lamp2.js.map +1 -0
  65. package/dist/lib/devices/gateway/86sw1.js +20 -0
  66. package/dist/lib/devices/gateway/86sw1.js.map +1 -0
  67. package/dist/lib/devices/gateway/86sw2.js +36 -0
  68. package/dist/lib/devices/gateway/86sw2.js.map +1 -0
  69. package/dist/lib/devices/gateway/ctrl_ln1.js +37 -0
  70. package/dist/lib/devices/gateway/ctrl_ln1.js.map +1 -0
  71. package/dist/lib/devices/gateway/ctrl_ln2.js +45 -0
  72. package/dist/lib/devices/gateway/ctrl_ln2.js.map +1 -0
  73. package/dist/lib/devices/gateway/ctrl_neutral1.js +37 -0
  74. package/dist/lib/devices/gateway/ctrl_neutral1.js.map +1 -0
  75. package/dist/lib/devices/gateway/ctrl_neutral2.js +45 -0
  76. package/dist/lib/devices/gateway/ctrl_neutral2.js.map +1 -0
  77. package/dist/lib/devices/gateway/cube.js +49 -0
  78. package/dist/lib/devices/gateway/cube.js.map +1 -0
  79. package/dist/lib/devices/gateway/developer-api.js +104 -0
  80. package/dist/lib/devices/gateway/developer-api.js.map +1 -0
  81. package/dist/lib/devices/gateway/light-channel.js +24 -0
  82. package/dist/lib/devices/gateway/light-channel.js.map +1 -0
  83. package/dist/lib/devices/gateway/light-mixin.js +59 -0
  84. package/dist/lib/devices/gateway/light-mixin.js.map +1 -0
  85. package/dist/lib/devices/gateway/magnet.js +23 -0
  86. package/dist/lib/devices/gateway/magnet.js.map +1 -0
  87. package/dist/lib/devices/gateway/magnet2.js +9 -0
  88. package/dist/lib/devices/gateway/magnet2.js.map +1 -0
  89. package/dist/lib/devices/gateway/motion.js +21 -0
  90. package/dist/lib/devices/gateway/motion.js.map +1 -0
  91. package/dist/lib/devices/gateway/motion2.js +25 -0
  92. package/dist/lib/devices/gateway/motion2.js.map +1 -0
  93. package/dist/lib/devices/gateway/plug.js +34 -0
  94. package/dist/lib/devices/gateway/plug.js.map +1 -0
  95. package/dist/lib/devices/gateway/sensor_ht.js +13 -0
  96. package/dist/lib/devices/gateway/sensor_ht.js.map +1 -0
  97. package/dist/lib/devices/gateway/subdevice.js +203 -0
  98. package/dist/lib/devices/gateway/subdevice.js.map +1 -0
  99. package/dist/lib/devices/gateway/subdevices.js +33 -0
  100. package/dist/lib/devices/gateway/subdevices.js.map +1 -0
  101. package/dist/lib/devices/gateway/switch.js +24 -0
  102. package/dist/lib/devices/gateway/switch.js.map +1 -0
  103. package/dist/lib/devices/gateway/switch2.js +19 -0
  104. package/dist/lib/devices/gateway/switch2.js.map +1 -0
  105. package/dist/lib/devices/gateway/voltage.js +21 -0
  106. package/dist/lib/devices/gateway/voltage.js.map +1 -0
  107. package/dist/lib/devices/gateway/weather.js +16 -0
  108. package/dist/lib/devices/gateway/weather.js.map +1 -0
  109. package/dist/lib/devices/gateway.js +212 -0
  110. package/dist/lib/devices/gateway.js.map +1 -0
  111. package/dist/lib/devices/humidifier.js +141 -0
  112. package/dist/lib/devices/humidifier.js.map +1 -0
  113. package/dist/lib/devices/philips-light-bulb.js +63 -0
  114. package/dist/lib/devices/philips-light-bulb.js.map +1 -0
  115. package/dist/lib/devices/power-plug.js +22 -0
  116. package/dist/lib/devices/power-plug.js.map +1 -0
  117. package/dist/lib/devices/power-strip.js +27 -0
  118. package/dist/lib/devices/power-strip.js.map +1 -0
  119. package/dist/lib/devices/vacuums/dreamevacuum.js +312 -0
  120. package/dist/lib/devices/vacuums/dreamevacuum.js.map +1 -0
  121. package/dist/lib/devices/vacuums/mijiavacuum.js +21 -0
  122. package/dist/lib/devices/vacuums/mijiavacuum.js.map +1 -0
  123. package/dist/lib/devices/vacuums/miot-vacuum.js +444 -0
  124. package/dist/lib/devices/vacuums/miot-vacuum.js.map +1 -0
  125. package/dist/lib/devices/vacuums/vacuum.js +669 -0
  126. package/dist/lib/devices/vacuums/vacuum.js.map +1 -0
  127. package/dist/lib/devices/vacuums/vacuum_modes.js +171 -0
  128. package/dist/lib/devices/vacuums/vacuum_modes.js.map +1 -0
  129. package/dist/lib/devices/vacuums/viomivacuum.js +222 -0
  130. package/dist/lib/devices/vacuums/viomivacuum.js.map +1 -0
  131. package/dist/lib/devices/yeelight.color.js +5 -0
  132. package/dist/lib/devices/yeelight.color.js.map +1 -0
  133. package/dist/lib/devices/yeelight.js +242 -0
  134. package/dist/lib/devices/yeelight.js.map +1 -0
  135. package/dist/lib/devices/yeelight.mono.js +5 -0
  136. package/dist/lib/devices/yeelight.mono.js.map +1 -0
  137. package/dist/lib/discovery.js +147 -0
  138. package/dist/lib/discovery.js.map +1 -0
  139. package/dist/lib/index.js +89 -0
  140. package/dist/lib/index.js.map +1 -0
  141. package/dist/lib/info_from_hostname.js +27 -0
  142. package/dist/lib/info_from_hostname.js.map +1 -0
  143. package/dist/lib/management.js +101 -0
  144. package/dist/lib/management.js.map +1 -0
  145. package/dist/lib/miio_error.js +23 -0
  146. package/dist/lib/miio_error.js.map +1 -0
  147. package/dist/lib/miot/miot_api.js +73 -0
  148. package/dist/lib/miot/miot_api.js.map +1 -0
  149. package/dist/lib/miot/miot_spec.js +105 -0
  150. package/dist/lib/miot/miot_spec.js.map +1 -0
  151. package/dist/lib/miot/spec_client.js +290 -0
  152. package/dist/lib/miot/spec_client.js.map +1 -0
  153. package/dist/lib/models.js +69 -0
  154. package/dist/lib/models.js.map +1 -0
  155. package/dist/lib/network.js +194 -0
  156. package/dist/lib/network.js.map +1 -0
  157. package/dist/lib/packet.js +177 -0
  158. package/dist/lib/packet.js.map +1 -0
  159. package/dist/lib/placeholder.js +16 -0
  160. package/dist/lib/placeholder.js.map +1 -0
  161. package/dist/lib/protocol.js +3 -0
  162. package/dist/lib/protocol.js.map +1 -0
  163. package/dist/lib/safeish_json.js +22 -0
  164. package/dist/lib/safeish_json.js.map +1 -0
  165. package/dist/lib/tokens.js +30 -0
  166. package/dist/lib/tokens.js.map +1 -0
  167. package/docs/devices/vacuum.md +63 -3
  168. package/example.js +1 -1
  169. package/package.json +49 -16
  170. package/snapshots/vacuum-specs.json.gz +0 -0
  171. package/.editorconfig +0 -26
  172. package/.eslintrc +0 -32
  173. package/.github/release.yml +0 -20
  174. package/.github/workflows/build.yml +0 -50
  175. package/.github/workflows/codecov.yml +0 -35
  176. package/.github/workflows/codeql.yml +0 -24
  177. package/.github/workflows/publish-dev-from-main.yml +0 -92
  178. package/.github/workflows/publish-release-to-npm.yml +0 -45
  179. package/cli/commands/configure.js +0 -69
  180. package/cli/commands/control.js +0 -54
  181. package/cli/commands/discover.js +0 -36
  182. package/cli/commands/inspect.js +0 -197
  183. package/cli/commands/protocol/call.js +0 -66
  184. package/cli/commands/protocol/json-dump.js +0 -52
  185. package/cli/commands/protocol/packet.js +0 -29
  186. package/cli/commands/protocol.js +0 -8
  187. package/cli/commands/tokens/update.js +0 -58
  188. package/cli/commands/tokens.js +0 -8
  189. package/cli/device-finder.js +0 -69
  190. package/cli/index.js +0 -9
  191. package/cli/log.js +0 -89
  192. package/lib/checkResult.js +0 -13
  193. package/lib/connectToDevice.js +0 -66
  194. package/lib/device.js +0 -290
  195. package/lib/device_info.js +0 -329
  196. package/lib/device_info.test.js +0 -337
  197. package/lib/devices/air-monitor.js +0 -53
  198. package/lib/devices/air-purifier.js +0 -162
  199. package/lib/devices/capabilities/battery-level.js +0 -16
  200. package/lib/devices/capabilities/buzzer.js +0 -78
  201. package/lib/devices/capabilities/changeable-led-brightness.js +0 -48
  202. package/lib/devices/capabilities/colorable.js +0 -14
  203. package/lib/devices/capabilities/dimmable.js +0 -14
  204. package/lib/devices/capabilities/mode.js +0 -13
  205. package/lib/devices/capabilities/power.js +0 -14
  206. package/lib/devices/capabilities/sensor.js +0 -50
  207. package/lib/devices/capabilities/switchable-led.js +0 -49
  208. package/lib/devices/chuangmi.plug.v1.js +0 -61
  209. package/lib/devices/eyecare-lamp2.js +0 -179
  210. package/lib/devices/gateway/86sw1.js +0 -28
  211. package/lib/devices/gateway/86sw2.js +0 -44
  212. package/lib/devices/gateway/ctrl_ln1.js +0 -44
  213. package/lib/devices/gateway/ctrl_ln2.js +0 -53
  214. package/lib/devices/gateway/ctrl_neutral1.js +0 -44
  215. package/lib/devices/gateway/ctrl_neutral2.js +0 -53
  216. package/lib/devices/gateway/cube.js +0 -55
  217. package/lib/devices/gateway/developer-api.js +0 -120
  218. package/lib/devices/gateway/light-channel.js +0 -32
  219. package/lib/devices/gateway/light-mixin.js +0 -71
  220. package/lib/devices/gateway/magnet.js +0 -29
  221. package/lib/devices/gateway/magnet2.js +0 -13
  222. package/lib/devices/gateway/motion.js +0 -27
  223. package/lib/devices/gateway/motion2.js +0 -31
  224. package/lib/devices/gateway/plug.js +0 -45
  225. package/lib/devices/gateway/sensor_ht.js +0 -17
  226. package/lib/devices/gateway/subdevice.js +0 -227
  227. package/lib/devices/gateway/subdevices.js +0 -37
  228. package/lib/devices/gateway/switch.js +0 -29
  229. package/lib/devices/gateway/switch2.js +0 -27
  230. package/lib/devices/gateway/voltage.js +0 -28
  231. package/lib/devices/gateway/weather.js +0 -22
  232. package/lib/devices/gateway.js +0 -244
  233. package/lib/devices/humidifier.js +0 -162
  234. package/lib/devices/philips-light-bulb.js +0 -73
  235. package/lib/devices/power-plug.js +0 -26
  236. package/lib/devices/power-strip.js +0 -38
  237. package/lib/devices/vacuums/dreamevacuum.js +0 -350
  238. package/lib/devices/vacuums/mijiavacuum.js +0 -22
  239. package/lib/devices/vacuums/vacuum.js +0 -518
  240. package/lib/devices/vacuums/viomivacuum.js +0 -250
  241. package/lib/devices/yeelight.color.js +0 -6
  242. package/lib/devices/yeelight.js +0 -286
  243. package/lib/devices/yeelight.mono.js +0 -9
  244. package/lib/discovery.js +0 -192
  245. package/lib/index.js +0 -40
  246. package/lib/infoFromHostname.js +0 -24
  247. package/lib/management.js +0 -102
  248. package/lib/models.js +0 -76
  249. package/lib/network.js +0 -234
  250. package/lib/packet.js +0 -191
  251. package/lib/placeholder.js +0 -12
  252. package/lib/safeishJSON.js +0 -14
  253. package/lib/tokens.js +0 -129
package/lib/packet.js DELETED
@@ -1,191 +0,0 @@
1
- 'use strict';
2
-
3
- const crypto = require('crypto');
4
- const debug = require('debug')('miio:packet');
5
-
6
- class Packet {
7
- constructor(discovery = false) {
8
- this.discovery = discovery;
9
-
10
- this.header = Buffer.alloc(2 + 2 + 4 + 4 + 4 + 16);
11
- this.header[0] = 0x21;
12
- this.header[1] = 0x31;
13
-
14
- for (let i = 4; i < 32; i++) {
15
- this.header[i] = 0xff;
16
- }
17
-
18
- this._serverStampTime = 0;
19
- this._token = null;
20
- }
21
-
22
- handshake() {
23
- this.data = null;
24
- }
25
-
26
- handleHandshakeReply() {
27
- if (this._token === null) {
28
- const token = this.checksum;
29
- if (token.toString('hex').match(/^[fF0]+$/)) {
30
- // Device did not return its token so we set our token to null
31
- this._token = null;
32
- } else {
33
- this.token = this.checksum;
34
- }
35
- }
36
- }
37
-
38
- get needsHandshake() {
39
- /*
40
- * Handshake if we:
41
- * 1) do not have a token
42
- * 2) it has been longer then 120 seconds since last received message
43
- */
44
- return !this._token || Date.now() - this._serverStampTime > 120000;
45
- }
46
-
47
- get raw() {
48
- if (this.data) {
49
- // Send a command to the device
50
- if (!this._token) {
51
- throw new Error('Token is required to send commands');
52
- }
53
-
54
- for (let i = 4; i < 8; i++) {
55
- this.header[i] = 0x00;
56
- }
57
-
58
- // Update the stamp to match server
59
- if (this._serverStampTime) {
60
- const secondsPassed = Math.floor(
61
- (Date.now() - this._serverStampTime) / 1000
62
- );
63
- try {
64
- this.header.writeUInt32BE(this._serverStamp + secondsPassed, 12);
65
- } catch (err) {
66
- // If it fails to post the difference in seconds, just use the same stamp it previously had
67
- this.header.writeUInt32BE(this._serverStamp, 12);
68
- }
69
- }
70
-
71
- // Encrypt the data
72
- let cipher = crypto.createCipheriv(
73
- 'aes-128-cbc',
74
- this._tokenKey,
75
- this._tokenIV
76
- );
77
- let encrypted = Buffer.concat([cipher.update(this.data), cipher.final()]);
78
-
79
- // Set the length
80
- this.header.writeUInt16BE(32 + encrypted.length, 2);
81
-
82
- // Calculate the checksum
83
- let digest = crypto
84
- .createHash('md5')
85
- .update(this.header.slice(0, 16))
86
- .update(this._token)
87
- .update(encrypted)
88
- .digest();
89
- digest.copy(this.header, 16);
90
-
91
- debug('->', this.header);
92
- return Buffer.concat([this.header, encrypted]);
93
- } else {
94
- // Handshake
95
- this.header.writeUInt16BE(32, 2);
96
-
97
- for (let i = 4; i < 32; i++) {
98
- this.header[i] = 0xff;
99
- }
100
-
101
- debug('->', this.header);
102
- return this.header;
103
- }
104
- }
105
-
106
- set raw(msg) {
107
- msg.copy(this.header, 0, 0, 32);
108
- debug('<-', this.header);
109
-
110
- const stamp = this.stamp;
111
- if (stamp > 0) {
112
- // If the device returned a stamp, store it
113
- this._serverStamp = this.stamp;
114
- this._serverStampTime = Date.now();
115
- }
116
-
117
- const encrypted = msg.slice(32);
118
-
119
- if (this.discovery) {
120
- // This packet is only intended to be used for discovery
121
- this.data = encrypted.length > 0;
122
- } else {
123
- // Normal packet, decrypt data
124
- if (encrypted.length > 0) {
125
- if (!this._token) {
126
- debug('<- No token set, unable to handle packet');
127
- this.data = null;
128
- return;
129
- }
130
-
131
- const digest = crypto
132
- .createHash('md5')
133
- .update(this.header.slice(0, 16))
134
- .update(this._token)
135
- .update(encrypted)
136
- .digest();
137
-
138
- const checksum = this.checksum;
139
- if (!checksum.equals(digest)) {
140
- debug(
141
- '<- Invalid packet, checksum was',
142
- checksum,
143
- 'should be',
144
- digest
145
- );
146
- this.data = null;
147
- } else {
148
- let decipher = crypto.createDecipheriv(
149
- 'aes-128-cbc',
150
- this._tokenKey,
151
- this._tokenIV
152
- );
153
- this.data = Buffer.concat([
154
- decipher.update(encrypted),
155
- decipher.final(),
156
- ]);
157
- }
158
- } else {
159
- this.data = null;
160
- }
161
- }
162
- }
163
-
164
- get token() {
165
- return this._token;
166
- }
167
-
168
- set token(t) {
169
- this._token = Buffer.from(t);
170
- this._tokenKey = crypto.createHash('md5').update(t).digest();
171
- this._tokenIV = crypto
172
- .createHash('md5')
173
- .update(this._tokenKey)
174
- .update(t)
175
- .digest();
176
- }
177
-
178
- get checksum() {
179
- return this.header.slice(16);
180
- }
181
-
182
- get deviceId() {
183
- return this.header.readUInt32BE(8);
184
- }
185
-
186
- get stamp() {
187
- return this.header.readUInt32BE(12);
188
- }
189
- }
190
-
191
- module.exports = Packet;
@@ -1,12 +0,0 @@
1
- 'use strict';
2
-
3
- const { Thing } = require('abstract-things');
4
- const MiioApi = require('./device');
5
-
6
- module.exports = class extends Thing.with(MiioApi) {
7
-
8
- static get type() {
9
- return 'placeholder';
10
- }
11
-
12
- };
@@ -1,14 +0,0 @@
1
- 'use strict';
2
-
3
- module.exports = function(str) {
4
- try {
5
- return JSON.parse(str);
6
- } catch(ex) {
7
- // Case 1: Load for subdevices fail as they return empty values
8
- str = str.replace('[,]', '[null,null]');
9
- // for aqara body sensor (lumi.motion.aq2)
10
- str = str.replace('[,,]', '[null,null,null]');
11
-
12
- return JSON.parse(str);
13
- }
14
- };
package/lib/tokens.js DELETED
@@ -1,129 +0,0 @@
1
- 'use strict';
2
-
3
- const fs = require('fs');
4
- const path = require('path');
5
-
6
- const mkdirp = require('mkdirp');
7
- const AppDirectory = require('appdirectory');
8
- const dirs = new AppDirectory('miio');
9
-
10
- const CHECK_TIME = 1000;
11
- const MAX_STALE_TIME = 120000;
12
-
13
- const debug = require('debug')('miio:tokens');
14
-
15
- /**
16
- * Shared storage for tokens of devices. Keeps a simple JSON file synced
17
- * with tokens connected to device ids.
18
- */
19
- class Tokens {
20
- constructor() {
21
- this._file = path.join(dirs.userData(), 'tokens.json');
22
- this._data = {};
23
- this._lastSync = 0;
24
- }
25
-
26
- get(deviceId) {
27
- const now = Date.now();
28
- const diff = now - this._lastSync;
29
-
30
- if(diff > CHECK_TIME) {
31
- return this._loadAndGet(deviceId);
32
- }
33
-
34
- return Promise.resolve(this._get(deviceId));
35
- }
36
-
37
- _get(deviceId) {
38
- return this._data[deviceId];
39
- }
40
-
41
- _loadAndGet(deviceId) {
42
- return this._load()
43
- .then(() => this._get(deviceId))
44
- .catch(() => null);
45
- }
46
-
47
- _load() {
48
- if(this._loading) return this._loading;
49
-
50
- return this._loading = new Promise((resolve, reject) => {
51
- debug('Loading token storage from', this._file);
52
- fs.stat(this._file, (err, stat) => {
53
- if(err) {
54
- delete this._loading;
55
- if(err.code === 'ENOENT') {
56
- debug('Token storage does not exist');
57
- this._lastSync = Date.now();
58
- resolve(this._data);
59
- } else {
60
- reject(err);
61
- }
62
-
63
- return;
64
- }
65
-
66
- if(! stat.isFile()) {
67
- // tokens.json does not exist
68
- delete this._loading;
69
- reject(new Error('tokens.json exists but is not a file'));
70
- } else if(Date.now() - this._lastSync > MAX_STALE_TIME || stat.mtime.getTime() > this._lastSync) {
71
- debug('Loading tokens');
72
- fs.readFile(this._file, (err, result) => {
73
- this._data = JSON.parse(result.toString());
74
- this._lastSync = Date.now();
75
- delete this._loading;
76
- resolve(this._data);
77
- });
78
- } else {
79
- delete this._loading;
80
- this._lastSync = Date.now();
81
- resolve(this._data);
82
- }
83
- });
84
- });
85
- }
86
-
87
- update(deviceId, token) {
88
- return this._load()
89
- .then(() => {
90
- this._data[deviceId] = token;
91
-
92
- if(this._saving) {
93
- this._dirty = true;
94
- return this._saving;
95
- }
96
-
97
- return this._saving = new Promise((resolve, reject) => {
98
- const save = () => {
99
- debug('About to save tokens');
100
- fs.writeFile(this._file, JSON.stringify(this._data, null, 2), (err) => {
101
- if(err) {
102
- reject(err);
103
- } else {
104
- if(this._dirty) {
105
- debug('Redoing save due to multiple updates');
106
- this._dirty = false;
107
- save();
108
- } else {
109
- delete this._saving;
110
- resolve();
111
- }
112
- }
113
- });
114
- };
115
-
116
- mkdirp(dirs.userData(), (err) => {
117
- if(err) {
118
- reject(err);
119
- return;
120
- }
121
-
122
- save();
123
- });
124
- });
125
- });
126
- }
127
- }
128
-
129
- module.exports = new Tokens();