-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #830 from openSUSE/devserver_manifests
Development server - handle also the manifests.js file request
- Loading branch information
Showing
3 changed files
with
64 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
const fs = require("fs"); | ||
const path = require("path"); | ||
|
||
const manifestFile = path.join(__dirname, "..", "manifest.json"); | ||
|
||
// this function is injected as a string into the resulting JS file | ||
const updateAgamaManifest = (data) => { | ||
if (typeof cockpit === "object" && cockpit.manifests) { | ||
cockpit.manifests.agama = data; | ||
} | ||
}; | ||
|
||
// This function processes the webpack HTTP proxy request for manifests.js file. | ||
// | ||
// Patching the original JS code is difficult so rather inject code | ||
// which rewrites the Agama manifest data with new content. | ||
// | ||
// @see https://github.com/http-party/node-http-proxy#modify-response | ||
// | ||
// @param proxyRes HTTP proxy resource | ||
// @param req HTTP request | ||
// @param res HTTP response | ||
module.exports = function (proxyRes, req, res) { | ||
// collect parts of the original response | ||
const body = []; | ||
|
||
proxyRes.on("data", function (chunk) { | ||
body.push(chunk); | ||
}); | ||
|
||
proxyRes.on("end", function () { | ||
// forward the original status code | ||
res.statusCode = proxyRes.statusCode; | ||
|
||
// patch the response only on success otherwise there | ||
// might be some unexpected content (HTML error page) | ||
if (proxyRes.statusCode === 200 && fs.existsSync(manifestFile)) { | ||
const manifest = fs.readFileSync(manifestFile); | ||
// use an immediately-invoked function expression to inject the new | ||
// manifest content | ||
res.end(Buffer.concat(body).toString() + "((" + | ||
updateAgamaManifest.toString() + ")(" + manifest + "));"); | ||
} else { | ||
// otherwise just return the original content | ||
res.end(Buffer.concat(body).toString()); | ||
} | ||
}); | ||
}; |
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