This repository has been archived by the owner on Jun 20, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use local server to host offline extensions, 3.0.22
- Loading branch information
Showing
9 changed files
with
80 additions
and
10 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
const {app} = require('electron'); | ||
var http = require('http'); | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
|
||
const mimes = { | ||
'.ico': 'image/x-icon', | ||
'.html': 'text/html', | ||
'.js': 'text/javascript', | ||
'.json': 'application/json', | ||
'.css': 'text/css', | ||
'.png': 'image/png', | ||
'.jpg': 'image/jpeg' | ||
}; | ||
|
||
let instance = null; | ||
|
||
class ExtensionsServer { | ||
|
||
static instance() { | ||
if(instance == null) { | ||
instance = new ExtensionsServer(); | ||
} | ||
return instance; | ||
} | ||
|
||
constructor() { | ||
this.port = 45653; | ||
} | ||
|
||
getHost() { | ||
return `http://localhost:${this.port}/`; | ||
} | ||
|
||
createServer() { | ||
function handleRequest(req, res) { | ||
const extensionsFolder = "Extensions"; | ||
const extensionsDir = path.join(app.getPath('userData'), extensionsFolder); | ||
const modifiedReqUrl = req.url.replace(extensionsFolder, ""); | ||
const filePath = path.join(extensionsDir, modifiedReqUrl); | ||
|
||
fs.exists(filePath, function(exists) { | ||
if(exists && fs.lstatSync(filePath).isFile()) { | ||
const ext = path.parse(filePath).ext; | ||
res.setHeader("Content-Type", mimes[ext] || 'text/plain'); | ||
res.writeHead(200, { | ||
'Access-Control-Allow-Origin': '*' | ||
}); | ||
fs.createReadStream(filePath).pipe(res); | ||
return; | ||
} | ||
|
||
res.writeHead(404); | ||
res.write('404 Not Found'); | ||
res.end(); | ||
}); | ||
} | ||
|
||
var server = http.createServer(handleRequest); | ||
server.listen(this.port, () => { | ||
console.log(`Extensions server started at http://localhost:${this.port}`); | ||
}); | ||
} | ||
} | ||
|
||
module.exports = ExtensionsServer; |
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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