• This is my module saver program. It works now and can be used to save modules stored on a webserver onto esp8266 flash memory. Start the program and then key in on the left side:
    s.save('MyModule');

    class StModule {
      constructor () {
        this.st = require('Storage');
        this.wifi = require('Wifi');
        this.http = require('http');
        this.server = null;
      }
      
      init (ssid, pw, server) {
        this.wifi.connect(ssid, {password: pw}, (err) => {
          if (err) {
            console.log('Connection error, cannot connect to ', ssid);
          } else {
            console.log('Connected to ', ssid);
            this.server = server;
          }
        });
      }
      
      save(name) {
        var url = 'http://'+this.server+'/'+name+'.js';
        this.st.erase(name);
        this.http.get(url, (res) => {
          var offset = 0;
          var sz = res.headers['Content-Length'];
          res.on('data', (data) => {
            this.st.write(name, data, offset, sz);
            offset += data.length;
          });
          res.on('close', () => {
            console.log('Module '+name+' saved, size: '+this.st.read(name).length);
            console.log(this.st.list());
          });
        });
      }
      
      show(name) {
        print(this.st.read(name));
      }
    }
    
    exports = StModule;
    
    var s = new StModule();
    s.init('MyWlan', 'mypassword', '192.168.88.117:8081');
    //        SSID        Password       webserver's url
    

    I had to find out the size of the module. It was stored in the header of the received code.
    then it needs to specify the total module size for each chunk to be written to the flash:

    this.st.write(name, data, offset, sz);
    

    Altough some times i get an error which might be from a buffer overrun in the http.get module.

About

Avatar for fanThomas @fanThomas started