particle-api-js 10.2.0 → 10.3.1
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/.eslintrc.js +23 -21
- package/CHANGELOG.md +13 -0
- package/EventStream-e2e-browser.html +7 -7
- package/EventStream-e2e-node.js +14 -14
- package/dist/particle.min.js +1 -1
- package/dist/particle.min.js.map +1 -1
- package/docs/api.md +3544 -911
- package/karma.conf.js +59 -59
- package/package.json +1 -1
- package/src/Agent.js +387 -363
- package/src/Client.js +162 -162
- package/src/Defaults.js +5 -5
- package/src/EventStream.js +254 -253
- package/src/Library.js +21 -21
- package/src/Particle.js +2685 -2614
- package/test/.eslintrc +5 -5
- package/test/Agent.integration.js +14 -14
- package/test/Agent.spec.js +495 -495
- package/test/Client.spec.js +203 -203
- package/test/Defaults.spec.js +20 -20
- package/test/EventStream.spec.js +231 -231
- package/test/FakeAgent.js +18 -18
- package/test/Library.spec.js +29 -29
- package/test/Particle.integration.js +29 -29
- package/test/Particle.spec.js +3127 -3090
- package/test/fixtures/index.js +7 -7
- package/test/support/FixtureHttpServer.js +16 -16
- package/test/test-setup.js +3 -3
- package/tsconfig.json +3 -1
- package/webpack.config.js +39 -39
package/src/EventStream.js
CHANGED
|
@@ -5,259 +5,260 @@ const url = require('url');
|
|
|
5
5
|
const { EventEmitter } = require('events');
|
|
6
6
|
|
|
7
7
|
class EventStream extends EventEmitter {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
8
|
+
constructor(uri, token) {
|
|
9
|
+
super();
|
|
10
|
+
this.uri = uri;
|
|
11
|
+
this.token = token;
|
|
12
|
+
this.reconnectInterval = 2000;
|
|
13
|
+
this.timeout = 13000; // keep alive can be sent up to 12 seconds after last event
|
|
14
|
+
this.data = '';
|
|
15
|
+
this.buf = '';
|
|
16
|
+
|
|
17
|
+
this.parse = this.parse.bind(this);
|
|
18
|
+
this.end = this.end.bind(this);
|
|
19
|
+
this.idleTimeoutExpired = this.idleTimeoutExpired.bind(this);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
connect() {
|
|
23
|
+
return new Promise((resolve, reject) => {
|
|
24
|
+
const { hostname, protocol, port, path } = url.parse(this.uri);
|
|
25
|
+
this.origin = `${protocol}//${hostname}${port ? (':' + port) : ''}`;
|
|
26
|
+
|
|
27
|
+
const isSecure = protocol === 'https:';
|
|
28
|
+
const requestor = isSecure ? https : http;
|
|
29
|
+
const req = requestor.request({
|
|
30
|
+
hostname,
|
|
31
|
+
protocol,
|
|
32
|
+
path: `${path}?access_token=${this.token}`,
|
|
33
|
+
method: 'get',
|
|
34
|
+
// @ts-ignore
|
|
35
|
+
port: parseInt(port, 10) || (isSecure ? 443 : 80),
|
|
36
|
+
// @ts-ignore
|
|
37
|
+
mode: 'prefer-streaming'
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
this.req = req;
|
|
41
|
+
|
|
42
|
+
let connected = false;
|
|
43
|
+
let connectionTimeout = setTimeout(() => {
|
|
44
|
+
if (this.req) {
|
|
45
|
+
this.req.abort();
|
|
46
|
+
}
|
|
47
|
+
reject({ error: new Error('Timeout'), errorDescription: `Timeout connecting to ${this.uri}` });
|
|
48
|
+
}, this.timeout);
|
|
49
|
+
|
|
50
|
+
req.on('error', e => {
|
|
51
|
+
clearTimeout(connectionTimeout);
|
|
52
|
+
|
|
53
|
+
if (connected) {
|
|
54
|
+
this.end();
|
|
55
|
+
} else {
|
|
56
|
+
reject({ error: e, errorDescription: `Network error from ${this.uri}` });
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
req.on('response', res => {
|
|
61
|
+
clearTimeout(connectionTimeout);
|
|
62
|
+
|
|
63
|
+
const statusCode = res.statusCode;
|
|
64
|
+
if (statusCode !== 200) {
|
|
65
|
+
let body = '';
|
|
66
|
+
res.on('data', chunk => body += chunk);
|
|
67
|
+
res.on('end', () => {
|
|
68
|
+
try {
|
|
69
|
+
body = JSON.parse(body);
|
|
70
|
+
} catch (e) {
|
|
71
|
+
// don't bother doing anything special if the JSON.parse fails
|
|
72
|
+
// since we are already about to reject the promise anyway
|
|
73
|
+
} finally {
|
|
74
|
+
let errorDescription = `HTTP error ${statusCode} from ${this.uri}`;
|
|
75
|
+
// @ts-ignore
|
|
76
|
+
if (body && body.error_description) {
|
|
77
|
+
// @ts-ignore
|
|
78
|
+
errorDescription += ' - ' + body.error_description;
|
|
79
|
+
}
|
|
80
|
+
reject({ statusCode, errorDescription, body });
|
|
81
|
+
this.req = undefined;
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
this.data = '';
|
|
88
|
+
this.buf = '';
|
|
89
|
+
|
|
90
|
+
connected = true;
|
|
91
|
+
res.on('data', this.parse);
|
|
92
|
+
res.once('end', this.end);
|
|
93
|
+
this.startIdleTimeout();
|
|
94
|
+
resolve(this);
|
|
95
|
+
});
|
|
96
|
+
req.end();
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
abort() {
|
|
101
|
+
if (this.req) {
|
|
102
|
+
this.req.abort();
|
|
103
|
+
this.req = undefined;
|
|
104
|
+
}
|
|
105
|
+
this.removeAllListeners();
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/* Private methods */
|
|
109
|
+
|
|
110
|
+
emitSafe(event, param) {
|
|
111
|
+
try {
|
|
112
|
+
this.emit(event, param);
|
|
113
|
+
} catch (error) {
|
|
114
|
+
if (event !== 'error') {
|
|
115
|
+
this.emitSafe('error', error);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
end() {
|
|
121
|
+
this.stopIdleTimeout();
|
|
122
|
+
|
|
123
|
+
if (!this.req) {
|
|
124
|
+
// request was ended intentionally by abort
|
|
125
|
+
// do not auto reconnect.
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
this.req = undefined;
|
|
130
|
+
this.emitSafe('disconnect');
|
|
131
|
+
this.reconnect();
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
reconnect() {
|
|
135
|
+
setTimeout(() => {
|
|
136
|
+
if (this.isOffline()) {
|
|
137
|
+
this.reconnect();
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
this.emitSafe('reconnect');
|
|
142
|
+
this.connect().then(() => {
|
|
143
|
+
this.emitSafe('reconnect-success');
|
|
144
|
+
}).catch(err => {
|
|
145
|
+
this.emitSafe('reconnect-error', err);
|
|
146
|
+
this.reconnect();
|
|
147
|
+
});
|
|
148
|
+
}, this.reconnectInterval);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
isOffline() {
|
|
152
|
+
if (typeof navigator === 'undefined' || navigator.hasOwnProperty('onLine')) {
|
|
153
|
+
return false;
|
|
154
|
+
}
|
|
155
|
+
return !navigator.onLine;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
startIdleTimeout() {
|
|
159
|
+
this.stopIdleTimeout();
|
|
160
|
+
this.idleTimeout = setTimeout(this.idleTimeoutExpired, this.timeout);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
stopIdleTimeout() {
|
|
164
|
+
if (this.idleTimeout) {
|
|
165
|
+
clearTimeout(this.idleTimeout);
|
|
166
|
+
this.idleTimeout = null;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
idleTimeoutExpired() {
|
|
171
|
+
if (this.req) {
|
|
172
|
+
this.req.abort();
|
|
173
|
+
this.end();
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
parse(chunk) {
|
|
178
|
+
this.startIdleTimeout();
|
|
179
|
+
|
|
180
|
+
this.buf += chunk;
|
|
181
|
+
let pos = 0;
|
|
182
|
+
let length = this.buf.length;
|
|
183
|
+
let discardTrailingNewline = false;
|
|
184
|
+
|
|
185
|
+
while (pos < length) {
|
|
186
|
+
if (discardTrailingNewline) {
|
|
187
|
+
if (this.buf[pos] === '\n') {
|
|
188
|
+
++pos;
|
|
189
|
+
}
|
|
190
|
+
discardTrailingNewline = false;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
let lineLength = -1;
|
|
194
|
+
let fieldLength = -1;
|
|
195
|
+
|
|
196
|
+
for (let i = pos; lineLength < 0 && i < length; ++i) {
|
|
197
|
+
const c = this.buf[i];
|
|
198
|
+
if (c === ':') {
|
|
199
|
+
if (fieldLength < 0) {
|
|
200
|
+
fieldLength = i - pos;
|
|
201
|
+
}
|
|
202
|
+
} else if (c === '\r') {
|
|
203
|
+
discardTrailingNewline = true;
|
|
204
|
+
lineLength = i - pos;
|
|
205
|
+
} else if (c === '\n') {
|
|
206
|
+
lineLength = i - pos;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
if (lineLength < 0) {
|
|
211
|
+
break;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
this.parseEventStreamLine(pos, fieldLength, lineLength);
|
|
215
|
+
|
|
216
|
+
pos += lineLength + 1;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
if (pos === length) {
|
|
220
|
+
this.buf = '';
|
|
221
|
+
} else if (pos > 0) {
|
|
222
|
+
this.buf = this.buf.slice(pos);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
parseEventStreamLine(pos, fieldLength, lineLength) {
|
|
227
|
+
if (lineLength === 0) {
|
|
228
|
+
try {
|
|
229
|
+
if (this.data.length > 0 && this.event) {
|
|
230
|
+
const event = JSON.parse(this.data);
|
|
231
|
+
event.name = this.eventName || '';
|
|
232
|
+
this.emitSafe('event', event);
|
|
233
|
+
}
|
|
234
|
+
} catch (e) {
|
|
235
|
+
// do nothing if JSON.parse fails
|
|
236
|
+
} finally {
|
|
237
|
+
this.data = '';
|
|
238
|
+
this.eventName = undefined;
|
|
239
|
+
this.event = false;
|
|
240
|
+
}
|
|
241
|
+
} else if (fieldLength > 0) {
|
|
242
|
+
const field = this.buf.slice(pos, pos + fieldLength);
|
|
243
|
+
let step = 0;
|
|
244
|
+
|
|
245
|
+
if (this.buf[pos + fieldLength + 1] !== ' ') {
|
|
246
|
+
step = fieldLength + 1;
|
|
247
|
+
} else {
|
|
248
|
+
step = fieldLength + 2;
|
|
249
|
+
}
|
|
250
|
+
pos += step;
|
|
251
|
+
const valueLength = lineLength - step;
|
|
252
|
+
const value = this.buf.slice(pos, pos + valueLength);
|
|
253
|
+
|
|
254
|
+
if (field === 'data') {
|
|
255
|
+
this.data += value + '\n';
|
|
256
|
+
} else if (field === 'event') {
|
|
257
|
+
this.eventName = value;
|
|
258
|
+
this.event = true;
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
}
|
|
261
262
|
}
|
|
262
263
|
|
|
263
264
|
module.exports = EventStream;
|
package/src/Library.js
CHANGED
|
@@ -3,31 +3,31 @@
|
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
5
|
class Library {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
6
|
+
constructor(client, data) {
|
|
7
|
+
// Make client non-enumerable so it doesn't show up in Object.keys, JSON.stringify, etc
|
|
8
|
+
Object.defineProperty(this, 'client', { value: client });
|
|
9
|
+
this._assignAttributes(data);
|
|
10
|
+
this.downloadUrl = data.links && data.links.download;
|
|
11
|
+
}
|
|
12
12
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
_assignAttributes(data) {
|
|
14
|
+
Object.assign(this, data.attributes);
|
|
15
|
+
}
|
|
16
16
|
|
|
17
17
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
18
|
+
/**
|
|
19
|
+
* Download the compressed file containing the source code for this library version.
|
|
20
|
+
* @return {Promise} Resolves to the .tar.gz compressed source code
|
|
21
|
+
*/
|
|
22
|
+
download() {
|
|
23
|
+
if (!this.downloadUrl) {
|
|
24
|
+
return Promise.reject(new Error('No download URL for this library'));
|
|
25
|
+
}
|
|
26
|
+
// @ts-ignore
|
|
27
|
+
return this.client.downloadFile(this.downloadUrl);
|
|
28
|
+
}
|
|
29
29
|
|
|
30
|
-
|
|
30
|
+
/* TODO: add a versions() method to fetch an array of library objects */
|
|
31
31
|
}
|
|
32
32
|
|
|
33
33
|
module.exports = Library;
|