Skip to content

Instantly share code, notes, and snippets.

@jc-lab
Created October 2, 2019 00:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jc-lab/86814143807ccdf7388b19825883613c to your computer and use it in GitHub Desktop.
Save jc-lab/86814143807ccdf7388b19825883613c to your computer and use it in GitHub Desktop.
app protocol to http protocol
const {PassThrough} = require('stream');
protocol.registerStreamProtocol('app', ((request, callback) => {
let url = new murl.URL(request.url);
let newUrlOptions = {
auth: url.auth,
hash: url.hash,
host: webpackDevServerUrl.host,
hostname: url.hostname,
href: url.href,
path: url.path,
pathname: url.pathname,
protocol: webpackDevServerUrl.protocol,
search: url.search,
slashes: url.slashes,
port: webpackDevServerUrl.port,
query: url.query,
};
let newUrl = murl.format(newUrlOptions);
const netRequest = net.request({
method: 'GET',
url: newUrl
});
netRequest.on('response', response => {
console.log("netRequest response");
const rv = new PassThrough();
callback({
statusCode: response.statusCode,
headers: response.headers,
data: rv
});
response.on('data', chunk => {
rv.push(chunk);
});
response.on('end', () => {
rv.push(null);
});
});
netRequest.on('abort', () => {
console.log("netRequest abort");
callback({
statusCode: 501
})
});
netRequest.on('error', (error) => {
console.log("netRequest error", error);
callback({
statusCode: error.code
})
});
netRequest.end();
}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment