E.openFile failures

Posted on
  • Hello everybody,

    I am experimenting a bit with the http server (espruino versions from 1.99) and this snippet works most of the time.

    First time the page is opened it shows the files in the system as hyperlinks. Downloading a file many times breaks the code.

    I´ve found that calling E.openFile many times in the left webide pane makes it fail too. No examples show a close method after openFile, so I believe that it is not needed.

    Am I doing it wrong?

    >fs.readdir()
    =[
      "xx.txt"
     ]
    >E.openFile("xx.txt");
    =File: {  }
    >process.memory()
    ={ "free": 1857, "usage": 643, "total": 2500, "history": 239,
      "gc": 0, "gctime": 1.839 }
    >E.openFile("xx.txt");
    =File: {  }
    >process.memory()
    ={ "free": 1592, "usage": 908, "total": 2500, "history": 242,
      "gc": 0, "gctime": 1.669 }
    >E.openFile("xx.txt");
    =File: {  }
    >process.memory()
    ={ "free": 1327, "usage": 1173, "total": 2500, "history": 242,
      "gc": 0, "gctime": 1.5 }
    >E.openFile("xx.txt");
    =File: {  }
    >process.memory()
    ={ "free": 1062, "usage": 1438, "total": 2500, "history": 242,
      "gc": 0, "gctime": 1.345 }
    >E.openFile("xx.txt");
    =File: {  }
    >process.memory()
    ={ "free": 797, "usage": 1703, "total": 2500, "history": 242,
      "gc": 0, "gctime": 1.159 }
    >E.openFile("xx.txt");
    =File: {  }
    >process.memory()
    ={ "free": 532, "usage": 1968, "total": 2500, "history": 242,
      "gc": 0, "gctime": 0.997 }
    >E.openFile("xx.txt");
    =File: {  }
    >process.memory()
    ={ "free": 267, "usage": 2233, "total": 2500, "history": 242,
      "gc": 0, "gctime": 0.828 }
    >E.openFile("xx.txt");
    =undefined
    >E.openFile("xx.txt");
    =undefined
    > 
    
    var fs = require("fs");
    try {
      fs.readdirSync();
     } catch (e) {
      console.log('Formatting FS - only need to do once');
      E.flashFatFS({ format: true });
      fs.writeFileSync("xx.txt", "..");
    }
    
    function onPageRequest(req, res) { 
      var a = url.parse(req.url, true);
      print(a);
      if (a.pathname.substr(-1)=="/") { // a slash at the end, list the directory
        res.writeHead(200, {'Content-Type': 'text/html'});
        res.write("<html><body><p>Contents of "+a.pathname+"</p><ul>");
        fs.readdir().map(function(f) {
          res.write('<a href="'+f+'">'+f+'</a>');
        });
        res.end("</ul></body></html>");
      } else { // No slash, try and open file
        var f = E.openFile(a.pathname, "r");
        if (f !== undefined) { // File open succeeded - send it!
          res.writeHead(200, {'Content-Type': 'text/plain'});
          f.pipe(res); // streams the file to the HTTP response
        } else { // couldn't open file
          res.writeHead(404, {'Content-Type': 'text/plain'});
          res.end("404: Page "+a.pathname+" not found");
        }
      }
    }
    
  • Sat 2019.01.12

    Performing a Google search, remember Google is your friend,

    Google:    E.openFile   site:espruino.com

    http://forum.espruino.com/conversations/327385/

    a lengthy discussion and solution was previously proposed

  • Ha! Thank you. I used the forum search for "openfile" without results ;(

    Here is one solution to help other users.

    It fixes the heap leak, but still eventually fails to open the file.

        var f = E.openFile(a.pathname, "r");
        if (f !== undefined) { // File open succeeded - send it!
          res.writeHead(200, {'Content-Type': 'text/plain'});
          f.pipe(res, {chunkSize: 512,  complete: f.close});
        } else { // couldn't open file
          res.writeHead(404, {'Content-Type': 'text/plain'});
          res.end("404: Page "+a.pathname+" not found");
        }
    
  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

E.openFile failures

Posted by Avatar for barbiani @barbiani

Actions