Created
October 2, 2019 00:19
-
-
Save jc-lab/86814143807ccdf7388b19825883613c to your computer and use it in GitHub Desktop.
app protocol to http protocol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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