FsConnectionDelegate.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. 'use strict';
  2. const fs = require('fs'),
  3. {ConnectionDelegate} = require('../../src/node-process');
  4. /**
  5. * Handle the requests of a connection to control the "fs" module.
  6. */
  7. class FsConnectionDelegate extends ConnectionDelegate
  8. {
  9. async handleInstruction(instruction, responseHandler, errorHandler)
  10. {
  11. instruction.setDefaultResource(this.extendFsModule(fs));
  12. let value = null;
  13. try {
  14. value = await instruction.execute();
  15. } catch (error) {
  16. if (instruction.shouldCatchErrors()) {
  17. return errorHandler(error);
  18. }
  19. throw error;
  20. }
  21. responseHandler(value);
  22. }
  23. extendFsModule(fs)
  24. {
  25. fs.multipleStatSync = (...paths) => paths.map(fs.statSync);
  26. fs.multipleResourcesIsFile = resources => resources.map(resource => resource.isFile());
  27. fs.getHeavyPayloadWithNonAsciiChars = () => {
  28. let payload = '';
  29. for (let i = 0 ; i < 1024 ; i++) {
  30. payload += 'a';
  31. }
  32. return `😘${payload}😘`;
  33. };
  34. fs.wait = ms => new Promise(resolve => setTimeout(resolve, ms));
  35. fs.runCallback = cb => cb(fs);
  36. fs.getOption = name => this.options[name];
  37. return fs;
  38. }
  39. }
  40. module.exports = FsConnectionDelegate;