Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. const {PassThrough} = require('stream');
  2. protocol.registerStreamProtocol('app', ((request, callback) => {
  3. let url = new murl.URL(request.url);
  4. let newUrlOptions = {
  5. auth: url.auth,
  6. hash: url.hash,
  7. host: webpackDevServerUrl.host,
  8. hostname: url.hostname,
  9. href: url.href,
  10. path: url.path,
  11. pathname: url.pathname,
  12. protocol: webpackDevServerUrl.protocol,
  13. search: url.search,
  14. slashes: url.slashes,
  15. port: webpackDevServerUrl.port,
  16. query: url.query,
  17. };
  18. let newUrl = murl.format(newUrlOptions);
  19. const netRequest = net.request({
  20. method: 'GET',
  21. url: newUrl
  22. });
  23.  
  24. netRequest.on('response', response => {
  25. console.log("netRequest response");
  26. const rv = new PassThrough();
  27. callback({
  28. statusCode: response.statusCode,
  29. headers: response.headers,
  30. data: rv
  31. });
  32. response.on('data', chunk => {
  33. rv.push(chunk);
  34. });
  35. response.on('end', () => {
  36. rv.push(null);
  37. });
  38. });
  39.  
  40. netRequest.on('abort', () => {
  41. console.log("netRequest abort");
  42. callback({
  43. statusCode: 501
  44. })
  45. });
  46.  
  47. netRequest.on('error', (error) => {
  48. console.log("netRequest error", error);
  49. callback({
  50. statusCode: error.code
  51. })
  52. });
  53.  
  54. netRequest.end();
  55. }));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement