-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathwebrepl.js
489 lines (465 loc) · 17.3 KB
/
webrepl.js
1
2
3
4
5
6
7
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
import { EventEmitter } from './event-emitter.js';
/**
* WebSocket connection is opened.
* @event WebREPL#connected
*/
/**
* WebSocket connection is closed.
* @event WebREPL#disconnected
*/
/**
* Incoming WebSocket connection error or internal WebREPL error.
* @event WebREPL#error
* @type {ErrorEvent}
*/
/**
* WebREPL authenticated session with success.
* @event WebREPL#authenticated
*/
/**
* Every message coming from the webrepl to be printed on the terminal.
* @event WebREPL#output
* @type {string}
*/
/**
* `ArrayBuffer` coming from the webrepl.
* @event WebREPL#data
* @type {ArrayBuffer}
*/
class WebREPL extends EventEmitter {
/**
* Represents a WebREPL connection.
* @constructor
* @param {object} [opts] - Initialization option
* @param {string} [opts.ip] - Ip address to connect to.
* @param {string} [opts.password] - Password to authenticate webrepl session.
* @param {boolean} [opts.autoConnect] - Flags if should connect automatically when instantiating WebREPL class.
* @param {boolean} [opts.autoAuth] - Flags if should authenticate the webrepl session automatically.
* @param {number} [opts.timeout] - How long, in milliseconds, should wait for response from webrepl.
* @property {string} [ip='192.168.4.1'] - Ip address to connect to.
* @property {string} [password='micropythoN'] - Password to authenticate webrepl session.
* @property {boolean} [autoConnect=false] - Flags if should connect automatically when instantiating WebREPL class.
* @property {boolean} [autoAuth=false] - Flags if should authenticate the webrepl session automatically.
* @property {number} [timeout=5000] - How long, in milliseconds, should wait for response from webrepl.
* @property {Uint8Array} fileBuffer - Buffer for files being requested by `loadFile`.
* @property {WebSocket} ws - WebSocket connection with board.
* @example
* let repl = new WebREPL({
* ip: '192.168.1.4',
* password: 'micropythoN',
* autoConnect: true,
* autoAuth: true,
* timeout: 5000
* })
*/
constructor(opts) {
super()
this.STOP = '\r\x03' // CTRL-C
this.RESET = '\r\x04' // CTRL-D
this.ENTER_RAW_REPL = '\r\x01' // CTRL-A
this.EXECUTE_RAW_REPL = '\r\x04' // CTRL-D
this.EXIT_RAW_REPL = '\r\x02' // CTRL-B
opts = opts || {}
this.ip = opts.ip || '192.168.4.1'
this.password = opts.password || 'micropythoN'
this.autoAuth = !!opts.autoAuth
this.autoConnect = !!opts.autoConnect
this.timeout = opts.timeout || 5000
this.fileBuffer = new Uint8Array()
if (this.autoConnect) {
this.connect()
}
}
/**
* Creates a websocket connection following the WebREPL standards on the
* ip set while instantiating `WebREPL` and binds events for its opening
* and for parsing incoming messages.
* @example
* let repl = new WebREPL()
* repl.connect()
*/
connect() {
try {
this.ws = new WebSocket(`ws://${this.ip}:8266`)
this.ws.binaryType = 'arraybuffer'
this.ws.onopen = this._onOpen.bind(this)
this.ws.onerror = this._onError.bind(this)
this.ws.onclose = this._onClose.bind(this)
} catch(err) {
this._onError(err)
}
}
_onOpen() {
this.emit('connected')
this.ws.onmessage = this._handleMessage.bind(this)
}
_onError(err) {
this.emit('error', err)
}
_onClose() {
this.emit('disconnected')
}
/**
* Close the current websocket connection.
* @example
* let repl = new WebREPL()
* repl.disconnect()
*/
disconnect() {
this.ws.close()
}
/**
* Sends a keyboard interrupt character (CTRL-C).
* @example
* let repl = new WebREPL({ autoConnect: true })
* let stopButton = document.querySelector('#stop-code')
* repl.on('authenticated', () => {
* stopButton.addEventListener('click', repl.sendStop.bind(repl))
* })
*/
sendStop() {
this.eval(this.STOP)
}
/**
* Sends a keyboard interruption (sendStop) and then the character to
* perform a software reset on the board (CTRL-D).
* @example
* let repl = new WebREPL({ autoConnect: true })
* let resetButton = document.querySelector('#reset-button')
* repl.on('authenticated', function() {
* resetButton.addEventListener('click', (e) => repl.softReset())
* })
*/
softReset() {
this.sendStop()
this.eval(this.RESET)
}
/**
* Sends character to enter RAW Repl mode (CTRL-A).
* @return {Promise} - Resolves when board enters in raw repl mode, rejects if timeout.
* @example
* let repl = new WebREPL({ autoConnect: true })
* repl.on('authenticated', function() {
* repl.enterRawRepl()
* .then(() => {
* // RAW REPL
* })
* })
* })
*/
enterRawRepl() {
return new Promise((resolve, reject) => {
let timeout = setTimeout(() => {
reject(new Error('Timeout: Could not enter raw repl mode.'))
}, this.timeout)
let onOutput = (output) => {
if (output.indexOf('raw REPL; CTRL-B to exit') != -1) {
this.removeListener('output', onOutput)
resolve()
}
}
this.on('output', onOutput)
this.eval(this.ENTER_RAW_REPL)
})
}
/**
* Evaluate the code on raw repl line by line and resolve promise when it
* gets executed (prints "OK"). If `interval` is passed, wait that amount
* of time before evaluating next line (important for ESP32 WebREPL).
* @param {string} code - String containing lines of code separated by `\n`.
* @param {number} interval - Interval in milliseconds between the lines of code.
* @return {Promise} Resolves when code is pasted and executed (got `OK` from repl) on raw repl mode.
* @example
* let repl = new WebREPL({ autoConnect: true })
* let code = `for i in range(0, 10):\n print(i)`
* repl.on('authenticated', function() {
* this.enterRawRepl()
* .then(() => this.execRaw(code, 30))
* .then(() => this.exitRawRepl(code))
* })
*/
execRaw(code, interval) {
return new Promise((resolve, reject) => {
let buffer = ''
let onOutput = (output) => {
if (output.indexOf('OK') != -1) {
this.removeListener('output', onOutput)
resolve()
}
}
this.on('output', onOutput)
if(interval) {
code.split('\n').forEach((line, i) => {
setTimeout(() => {
this.eval(`${line}\r`)
this.emit('output', '.')
}, i*interval)
})
setTimeout(() => {
this.eval(this.EXECUTE_RAW_REPL)
}, (code.split('\n').length + 1) * interval )
} else {
this.eval(code)
this.eval(this.EXECUTE_RAW_REPL)
}
})
}
/**
* Sends character to enter RAW Repl mode (CTRL-D + CTRL-B).
* @return {Promise} Resolves when exits raw repl mode (Gets MicroPython booting message).
* @example
* let repl = new WebREPL({ autoConnect: true })
* repl.on('authenticated', function() {
* repl.enterRawRepl()
* .then(() => repl.execRaw('print("hello world!")'))
* .then(() => repl.exitRawRepl())
* })
*/
exitRawRepl() {
return new Promise((resolve, reject) => {
let onOutput = (output) => {
let endRaw = 'Type "help()" for more information.'
if (output.indexOf(endRaw) != -1) {
this.removeListener('output', onOutput)
resolve()
}
}
this.on('output', onOutput)
this.eval(this.EXIT_RAW_REPL)
})
}
/**
* Execute a string containing lines of code separated by `\n` in raw repl
* mode. It will send a keyboard interrupt before entering RAW REPL mode.
* @param {string} code - String containing lines of code separated by `\n`.
* @param {number} interval - Interval in milliseconds between the lines of code.
* @return {Promise} Resolves when exits raw repl mode.
* @example
* let repl = new WebREPL({ autoConnect: true })
* let code = `for i in range(0, 10):\n print(i)`
* repl.on('authenticated', function() {
* this.execFromString(code)
* .then(() => console.log('code executed'))
*
* })
*/
execFromString(code, interval) {
this.sendStop()
return this.enterRawRepl()
.then(() => this.execRaw(code, interval))
.then(() => this.exitRawRepl())
}
/**
* Send command to websocket connection.
* @param {string} command - Command to be sent.
* @example
* let repl = new WebREPL({ autoConnect: true })
* repl.on('authenticated', function() {
* this.eval('print("hello world!")\r')
* })
*/
eval(command) {
this.ws.send(command)
}
/**
* Evaluate command to the board followed by a line break (\r).
* @param {string} command - Command to be executed by WebREPL.
* @example
* let repl = new WebREPL({ autoConnect: true })
* repl.on('authenticated', function() {
* repl.exec('print("hello world!")')
* })
*/
exec(command) {
this.eval(command + '\r')
}
/**
* Save file to MicroPython's filesystem. Will use the filename from the
* `file` argument object as the filesystem path.
* @param {string} filename - Name of file to be sent
* @param {Uint8Array} putFileData - Typed array buffer with content of file to be sent.
* @return {Promise} Resolves when file is sent.
* @example
* let repl = new WebREPL({ autoConnect: true })
* let filename = 'foo.py'
* let buffer = new TextEncoder("utf-8").encode('print("hello world!")');
* repl.on('authenticated', function() {
* repl.sendFile(filename, buffer)
* })
*/
sendFile(filename, putFileData) {
return new Promise((resolve, reject) => {
let timeout = setTimeout(() => {
reject(new Error('Timeout: Could not send file.'))
}, this.timeout)
let initialResponse = (data) => {
clearTimeout(timeout)
let response = new Uint8Array(data)
if (this._decode_response(response) == 0) {
this.removeListener('data', initialResponse)
// Register listener for final response
this.on('data', finalResponse)
// Send file in chunks
for (let offset = 0; offset < putFileData.length; offset += 1024) {
this.ws.send(putFileData.slice(offset, offset + 1024))
}
}
}
let finalResponse = (data) => {
let response = new Uint8Array(data)
if (this._decode_response(response) == 0) {
this.removeListener('data', finalResponse)
resolve()
}
}
// Register listener for initial response
this.on('data', initialResponse)
// Send request to open file
let rec = this._getPutBinary(filename, putFileData.length)
this.ws.send(rec)
})
}
/*
* Given a filename and the file size, get a `Uint8Array` with fixed length
* and specific bits set to send a "put" request to MicroPython.
* @param {string} filename - File name
* @param {number} filesize - Length of ArrayBuffer containing file data
* @returns {Uint8Array}
*/
_getPutBinary(filename, filesize) {
// WEBREPL_FILE = "<2sBBQLH64s"
let rec = new Uint8Array(2 + 1 + 1 + 8 + 4 + 2 + 64)
rec[0] = 'W'.charCodeAt(0)
rec[1] = 'A'.charCodeAt(0)
rec[2] = 1 // put
rec[3] = 0
rec[4] = 0; rec[5] = 0; rec[6] = 0; rec[7] = 0; rec[8] = 0; rec[9] = 0; rec[10] = 0; rec[11] = 0;
rec[12] = filesize & 0xff; rec[13] = (filesize >> 8) & 0xff; rec[14] = (filesize >> 16) & 0xff; rec[15] = (filesize >> 24) & 0xff;
rec[16] = filename.length & 0xff; rec[17] = (filename.length >> 8) & 0xff;
for (let i = 0; i < 64; ++i) {
if (i < filename.length) {
rec[18 + i] = filename.charCodeAt(i)
} else {
rec[18 + i] = 0
}
}
return rec
}
/**
* Load file from MicroPython's filesystem.
* @param {string} filename - File name
* @return {Promise} Resolves with `Uint8Array` containing the data from requested file.
* @example
* let repl = new WebREPL({ autoConnect: true })
* let filename = 'foo.py'
* repl.saveAs = (blob) => {
* console.log('File content', blob)
* }
* repl.loadFile(filename)
*/
loadFile(filename) {
return new Promise((resolve, reject) => {
this.fileBuffer = new Uint8Array()
let timeout = setTimeout(() => {
reject(new Error('Timeout: Could not get file.'))
}, this.timeout)
let initialResponse = (data) => {
let response = new Uint8Array(data)
if (this._decode_response(response) == 0) {
this.removeListener('data', initialResponse)
this.on('data', onFileData)
let nextRec = new Uint8Array(1)
nextRec[0] = 0
this.ws.send(nextRec)
}
}
let onFileData = (data) => {
let response = new Uint8Array(data)
var sz = response[0] | (response[1] << 8);
if (response.length == 2 + sz) {
// we assume that the data comes in single chunks
if (sz != 0) {
// accumulate incoming data to fileBuffer
let newBuffer = new Uint8Array(this.fileBuffer.length + sz)
newBuffer.set(this.fileBuffer)
newBuffer.set(response.slice(2), this.fileBuffer.length)
this.fileBuffer = newBuffer
let rec = new Uint8Array(1)
rec[0] = 0
this.ws.send(rec)
}
} else {
// Done receiving
this.removeListener('data', onFileData)
resolve(this.fileBuffer)
}
}
let rec = this._getGetBinary(filename)
this.on('data', initialResponse)
this.ws.send(rec)
})
}
/*
* Given a filename, get a `Uint8Array` with fixed length and specific bits
* set to send a "get" request to MicroPython.
* @param {string} filename - File name
* @returns {Uint8Array}
*/
_getGetBinary(filename) {
// WEBREPL_FILE = "<2sBBQLH64s"
let rec = new Uint8Array(2 + 1 + 1 + 8 + 4 + 2 + 64);
rec[0] = 'W'.charCodeAt(0);
rec[1] = 'A'.charCodeAt(0);
rec[2] = 2; // get
rec[3] = 0;
rec[4] = 0; rec[5] = 0; rec[6] = 0; rec[7] = 0; rec[8] = 0; rec[9] = 0; rec[10] = 0; rec[11] = 0;
rec[12] = 0; rec[13] = 0; rec[14] = 0; rec[15] = 0;
rec[16] = filename.length & 0xff; rec[17] = (filename.length >> 8) & 0xff;
for (let i = 0; i < 64; ++i) {
if (i < filename.length) {
rec[18 + i] = filename.charCodeAt(i);
} else {
rec[18 + i] = 0;
}
}
return rec
}
/*
* Makes sure incoming data is valid.
* @param {ArrayBuffer} - Incoming data from webrepl.
* @return {number} Returns `0` if valid and `-1` otherwise.
*/
_decode_response(data) {
if (data[0] == 'W'.charCodeAt(0) && data[1] == 'B'.charCodeAt(0)) {
var code = data[2] | (data[3] << 8);
return code;
} else {
return -1;
}
}
/*
* Handles incoming data from websocket based on the data and current
* binaryState the WebREPL currently is set to.
* @param {object} event - Incoming event object from websocket connection.
* @param {ArrayBuffer|String} event.data - Data sent by MicroPython
* through websocket.
*/
_handleMessage(event) {
if (event.data instanceof ArrayBuffer) {
this.emit('data', event.data)
} else if (typeof event.data === 'string') {
this.emit('output', event.data)
// If is asking for password, send password
if (event.data == 'Password: ' && this.autoAuth) {
this.ws.send(`${this.password}\r`)
}
if (event.data.indexOf('WebREPL connected') != -1) {
this.emit('authenticated')
}
} else {
this._onError(new Error('Unrecognized data format.'))
}
}
}
export default WebREPL
export { WebREPL }