particle-api-js 11.1.7 → 12.0.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/README.md +90 -10
- package/dist/particle.min.js +1 -1
- package/dist/particle.min.js.map +1 -1
- package/fs.d.ts +2 -0
- package/lib/fs.d.ts +2 -0
- package/lib/fs.js +3 -0
- package/lib/package.json +110 -0
- package/lib/src/Agent.d.ts +40 -0
- package/lib/src/Agent.d.ts.map +1 -0
- package/lib/src/Agent.js +233 -0
- package/lib/src/Agent.js.map +1 -0
- package/lib/src/Client.d.ts +80 -0
- package/lib/src/Client.d.ts.map +1 -0
- package/lib/src/Client.js +104 -0
- package/lib/src/Client.js.map +1 -0
- package/lib/src/Defaults.d.ts +6 -0
- package/lib/src/Defaults.d.ts.map +1 -0
- package/lib/src/Defaults.js +12 -0
- package/lib/src/Defaults.js.map +1 -0
- package/lib/src/EventStream.d.ts +31 -0
- package/lib/src/EventStream.d.ts.map +1 -0
- package/lib/src/EventStream.js +275 -0
- package/lib/src/EventStream.js.map +1 -0
- package/lib/src/Library.d.ts +33 -0
- package/lib/src/Library.d.ts.map +1 -0
- package/lib/src/Library.js +19 -0
- package/lib/src/Library.js.map +1 -0
- package/{src/Particle.js → lib/src/Particle.d.ts} +623 -1779
- package/lib/src/Particle.d.ts.map +1 -0
- package/lib/src/Particle.js +2578 -0
- package/lib/src/Particle.js.map +1 -0
- package/lib/src/types/common.d.ts +73 -0
- package/lib/src/types/common.d.ts.map +1 -0
- package/lib/src/types/common.js +3 -0
- package/lib/src/types/common.js.map +1 -0
- package/lib/src/types/index.d.ts +4 -0
- package/lib/src/types/index.d.ts.map +1 -0
- package/lib/src/types/index.js +20 -0
- package/lib/src/types/index.js.map +1 -0
- package/lib/src/types/requests.d.ts +568 -0
- package/lib/src/types/requests.d.ts.map +1 -0
- package/lib/src/types/requests.js +3 -0
- package/lib/src/types/requests.js.map +1 -0
- package/lib/src/types/responses.d.ts +449 -0
- package/lib/src/types/responses.d.ts.map +1 -0
- package/lib/src/types/responses.js +3 -0
- package/lib/src/types/responses.js.map +1 -0
- package/package.json +35 -14
- package/scripts/postprocess-docs.js +306 -0
- package/typedoc.json +20 -0
- package/.circleci/config.yml +0 -104
- package/.nvmrc +0 -1
- package/CHANGELOG.md +0 -404
- package/EventStream-e2e-browser.html +0 -39
- package/EventStream-e2e-node.js +0 -34
- package/RELEASE.md +0 -12
- package/bower.json +0 -30
- package/docs/api.md +0 -2594
- package/eslint.config.mjs +0 -7
- package/examples/login/login.html +0 -17
- package/karma.conf.js +0 -80
- package/src/Agent.js +0 -397
- package/src/Client.js +0 -171
- package/src/Defaults.js +0 -8
- package/src/EventStream.js +0 -269
- package/src/Library.js +0 -33
- package/test/Agent.integration.js +0 -23
- package/test/Agent.spec.js +0 -488
- package/test/Client.spec.js +0 -216
- package/test/Defaults.spec.js +0 -30
- package/test/EventStream.feature +0 -65
- package/test/EventStream.spec.js +0 -263
- package/test/FakeAgent.js +0 -27
- package/test/Library.spec.js +0 -40
- package/test/Particle.integration.js +0 -38
- package/test/Particle.spec.js +0 -3198
- package/test/fixtures/index.js +0 -15
- package/test/fixtures/libraries.json +0 -33
- package/test/fixtures/library.json +0 -31
- package/test/fixtures/libraryVersions.json +0 -211
- package/test/out.tmp +0 -0
- package/test/support/FixtureHttpServer.js +0 -28
- package/test/test-setup.js +0 -17
- package/tsconfig.json +0 -17
- package/webpack.config.js +0 -46
package/src/EventStream.js
DELETED
|
@@ -1,269 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
const http = require('http');
|
|
3
|
-
const https = require('https');
|
|
4
|
-
const url = require('url');
|
|
5
|
-
const { EventEmitter } = require('events');
|
|
6
|
-
|
|
7
|
-
class EventStream extends EventEmitter {
|
|
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 nonce = global.performance ? global.performance.now() : 0;
|
|
30
|
-
const req = requestor.request({
|
|
31
|
-
hostname,
|
|
32
|
-
protocol,
|
|
33
|
-
// Firefox has issues making multiple fetch requests with the same parameters so add a nonce
|
|
34
|
-
path: `${path}?nonce=${nonce}`,
|
|
35
|
-
headers: {
|
|
36
|
-
'Authorization': `Bearer ${this.token}`
|
|
37
|
-
},
|
|
38
|
-
method: 'get',
|
|
39
|
-
// @ts-ignore
|
|
40
|
-
port: parseInt(port, 10) || (isSecure ? 443 : 80),
|
|
41
|
-
// @ts-ignore
|
|
42
|
-
mode: 'prefer-streaming'
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
this.req = req;
|
|
46
|
-
|
|
47
|
-
let connected = false;
|
|
48
|
-
const connectionTimeout = setTimeout(() => {
|
|
49
|
-
if (this.req) {
|
|
50
|
-
this.req.abort();
|
|
51
|
-
}
|
|
52
|
-
reject({ error: new Error('Timeout'), errorDescription: `Timeout connecting to ${this.uri}` });
|
|
53
|
-
}, this.timeout);
|
|
54
|
-
|
|
55
|
-
req.on('error', e => {
|
|
56
|
-
clearTimeout(connectionTimeout);
|
|
57
|
-
|
|
58
|
-
if (connected) {
|
|
59
|
-
this.end();
|
|
60
|
-
} else {
|
|
61
|
-
reject({ error: e, errorDescription: `Network error from ${this.uri}` });
|
|
62
|
-
}
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
req.on('response', res => {
|
|
66
|
-
clearTimeout(connectionTimeout);
|
|
67
|
-
|
|
68
|
-
const statusCode = res.statusCode;
|
|
69
|
-
if (statusCode !== 200) {
|
|
70
|
-
let body = '';
|
|
71
|
-
res.on('data', chunk => body += chunk);
|
|
72
|
-
res.on('end', () => {
|
|
73
|
-
try {
|
|
74
|
-
body = JSON.parse(body);
|
|
75
|
-
} catch (_err) {
|
|
76
|
-
// don't bother doing anything special if the JSON.parse fails
|
|
77
|
-
// since we are already about to reject the promise anyway
|
|
78
|
-
} finally {
|
|
79
|
-
let errorDescription = `HTTP error ${statusCode} from ${this.uri}`;
|
|
80
|
-
// @ts-ignore
|
|
81
|
-
if (body && body.error_description) {
|
|
82
|
-
// @ts-ignore
|
|
83
|
-
errorDescription += ' - ' + body.error_description;
|
|
84
|
-
}
|
|
85
|
-
reject({ statusCode, errorDescription, body });
|
|
86
|
-
this.req = undefined;
|
|
87
|
-
}
|
|
88
|
-
});
|
|
89
|
-
return;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
this.data = '';
|
|
93
|
-
this.buf = '';
|
|
94
|
-
|
|
95
|
-
connected = true;
|
|
96
|
-
res.on('data', this.parse);
|
|
97
|
-
res.once('end', this.end);
|
|
98
|
-
this.startIdleTimeout();
|
|
99
|
-
resolve(this);
|
|
100
|
-
});
|
|
101
|
-
req.end();
|
|
102
|
-
});
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
abort() {
|
|
106
|
-
if (this.req) {
|
|
107
|
-
this.req.abort();
|
|
108
|
-
this.req = undefined;
|
|
109
|
-
}
|
|
110
|
-
this.removeAllListeners();
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
/* Private methods */
|
|
114
|
-
|
|
115
|
-
emitSafe(event, param) {
|
|
116
|
-
try {
|
|
117
|
-
this.emit(event, param);
|
|
118
|
-
} catch (error) {
|
|
119
|
-
if (event !== 'error') {
|
|
120
|
-
this.emitSafe('error', error);
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
end() {
|
|
126
|
-
this.stopIdleTimeout();
|
|
127
|
-
|
|
128
|
-
if (!this.req) {
|
|
129
|
-
// request was ended intentionally by abort
|
|
130
|
-
// do not auto reconnect.
|
|
131
|
-
return;
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
this.req = undefined;
|
|
135
|
-
this.emitSafe('disconnect');
|
|
136
|
-
this.reconnect();
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
reconnect() {
|
|
140
|
-
setTimeout(() => {
|
|
141
|
-
if (this.isOffline()) {
|
|
142
|
-
this.reconnect();
|
|
143
|
-
return;
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
this.emitSafe('reconnect');
|
|
147
|
-
this.connect().then(() => {
|
|
148
|
-
this.emitSafe('reconnect-success');
|
|
149
|
-
}).catch(err => {
|
|
150
|
-
this.emitSafe('reconnect-error', err);
|
|
151
|
-
this.reconnect();
|
|
152
|
-
});
|
|
153
|
-
}, this.reconnectInterval);
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
isOffline() {
|
|
157
|
-
if (typeof navigator === 'undefined' || Object.hasOwnProperty.call(navigator, 'onLine')) {
|
|
158
|
-
return false;
|
|
159
|
-
}
|
|
160
|
-
return !navigator.onLine;
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
startIdleTimeout() {
|
|
164
|
-
this.stopIdleTimeout();
|
|
165
|
-
this.idleTimeout = setTimeout(this.idleTimeoutExpired, this.timeout);
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
stopIdleTimeout() {
|
|
169
|
-
if (this.idleTimeout) {
|
|
170
|
-
clearTimeout(this.idleTimeout);
|
|
171
|
-
this.idleTimeout = null;
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
idleTimeoutExpired() {
|
|
176
|
-
if (this.req) {
|
|
177
|
-
this.req.abort();
|
|
178
|
-
this.end();
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
parse(chunk) {
|
|
183
|
-
this.startIdleTimeout();
|
|
184
|
-
|
|
185
|
-
this.buf += chunk;
|
|
186
|
-
let pos = 0;
|
|
187
|
-
const length = this.buf.length;
|
|
188
|
-
let discardTrailingNewline = false;
|
|
189
|
-
|
|
190
|
-
while (pos < length) {
|
|
191
|
-
if (discardTrailingNewline) {
|
|
192
|
-
if (this.buf[pos] === '\n') {
|
|
193
|
-
++pos;
|
|
194
|
-
}
|
|
195
|
-
discardTrailingNewline = false;
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
let lineLength = -1;
|
|
199
|
-
let fieldLength = -1;
|
|
200
|
-
|
|
201
|
-
for (let i = pos; lineLength < 0 && i < length; ++i) {
|
|
202
|
-
const c = this.buf[i];
|
|
203
|
-
if (c === ':') {
|
|
204
|
-
if (fieldLength < 0) {
|
|
205
|
-
fieldLength = i - pos;
|
|
206
|
-
}
|
|
207
|
-
} else if (c === '\r') {
|
|
208
|
-
discardTrailingNewline = true;
|
|
209
|
-
lineLength = i - pos;
|
|
210
|
-
} else if (c === '\n') {
|
|
211
|
-
lineLength = i - pos;
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
if (lineLength < 0) {
|
|
216
|
-
break;
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
this.parseEventStreamLine(pos, fieldLength, lineLength);
|
|
220
|
-
|
|
221
|
-
pos += lineLength + 1;
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
if (pos === length) {
|
|
225
|
-
this.buf = '';
|
|
226
|
-
} else if (pos > 0) {
|
|
227
|
-
this.buf = this.buf.slice(pos);
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
parseEventStreamLine(pos, fieldLength, lineLength) {
|
|
232
|
-
if (lineLength === 0) {
|
|
233
|
-
try {
|
|
234
|
-
if (this.data.length > 0 && this.event) {
|
|
235
|
-
const event = JSON.parse(this.data);
|
|
236
|
-
event.name = this.eventName || '';
|
|
237
|
-
this.emitSafe('event', event);
|
|
238
|
-
}
|
|
239
|
-
} catch (_err) {
|
|
240
|
-
// do nothing if JSON.parse fails
|
|
241
|
-
} finally {
|
|
242
|
-
this.data = '';
|
|
243
|
-
this.eventName = undefined;
|
|
244
|
-
this.event = false;
|
|
245
|
-
}
|
|
246
|
-
} else if (fieldLength > 0) {
|
|
247
|
-
const field = this.buf.slice(pos, pos + fieldLength);
|
|
248
|
-
let step = 0;
|
|
249
|
-
|
|
250
|
-
if (this.buf[pos + fieldLength + 1] !== ' ') {
|
|
251
|
-
step = fieldLength + 1;
|
|
252
|
-
} else {
|
|
253
|
-
step = fieldLength + 2;
|
|
254
|
-
}
|
|
255
|
-
pos += step;
|
|
256
|
-
const valueLength = lineLength - step;
|
|
257
|
-
const value = this.buf.slice(pos, pos + valueLength);
|
|
258
|
-
|
|
259
|
-
if (field === 'data') {
|
|
260
|
-
this.data += value + '\n';
|
|
261
|
-
} else if (field === 'event') {
|
|
262
|
-
this.eventName = value;
|
|
263
|
-
this.event = true;
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
module.exports = EventStream;
|
package/src/Library.js
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
/* Library
|
|
2
|
-
* Represents a version of a library contributed in the cloud.
|
|
3
|
-
*/
|
|
4
|
-
'use strict';
|
|
5
|
-
class Library {
|
|
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
|
-
|
|
13
|
-
_assignAttributes(data) {
|
|
14
|
-
Object.assign(this, data.attributes);
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
|
|
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
|
-
|
|
30
|
-
/* TODO: add a versions() method to fetch an array of library objects */
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
module.exports = Library;
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Tests for real the Agent class using an external service.
|
|
3
|
-
*/
|
|
4
|
-
'use strict';
|
|
5
|
-
const { expect } = require('./test-setup');
|
|
6
|
-
const Agent = require('../src/Agent');
|
|
7
|
-
|
|
8
|
-
describe('Agent', () => {
|
|
9
|
-
if (!process.env.SKIP_AGENT_TEST){
|
|
10
|
-
it('can fetch a webpage', function cb() {
|
|
11
|
-
this.retries(5);
|
|
12
|
-
this.timeout(6000);
|
|
13
|
-
const agent = new Agent();
|
|
14
|
-
const query = { a: '1', b: '2' };
|
|
15
|
-
const result = agent.get({ uri: 'https://postman-echo.com/get', query });
|
|
16
|
-
return result.then((res) => {
|
|
17
|
-
expect(res.statusCode).to.equal(200);
|
|
18
|
-
expect(res).has.property('body');
|
|
19
|
-
expect(res.body.args).to.deep.equal(query);
|
|
20
|
-
});
|
|
21
|
-
});
|
|
22
|
-
}
|
|
23
|
-
});
|