-
Notifications
You must be signed in to change notification settings - Fork 300
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support mock CDN in dev-proxy for mock prod mode (#1627)
* feat: support mock CDN in dev-proxy for mock prod mode * back to mime 1
- Loading branch information
Showing
4 changed files
with
186 additions
and
20 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
"use strict"; | ||
|
||
/* eslint-disable no-console, no-magic-numbers, prefer-template */ | ||
|
||
/* | ||
* search all files under dist and generate a config/assets.json file for mocking CDN | ||
*/ | ||
|
||
const Path = require("path"); | ||
const filterScanDir = require("filter-scan-dir"); | ||
const Url = require("url"); | ||
const Fs = require("fs"); | ||
const chokidar = require("chokidar"); | ||
const mime = require("mime"); | ||
const LOADED_ASSETS = {}; | ||
|
||
const cdnMock = { | ||
generateMockAssets(baseUrl) { | ||
const watcher = chokidar.watch("dist"); | ||
let timer; | ||
const updateCdnMock = path => { | ||
LOADED_ASSETS[path] = undefined; | ||
if (timer) { | ||
clearTimeout(timer); | ||
} | ||
timer = setTimeout(() => { | ||
timer = undefined; | ||
console.log("Refreshing mock CDN mapping - please restart app"); | ||
cdnMock._generateMockAssets(baseUrl); | ||
}, 250).unref(); | ||
}; | ||
watcher.on("change", updateCdnMock); | ||
cdnMock._generateMockAssets(baseUrl); | ||
}, | ||
|
||
_generateMockAssets(baseUrl) { | ||
const files = filterScanDir.sync({ dir: "dist" }); | ||
|
||
const url = Url.parse(baseUrl); | ||
const noProtocolBase = Path.posix.join(`/`, url.host, url.path); | ||
const timestamp = Math.floor(Date.now() / 1000); | ||
|
||
const mockAssets = files.reduce((acc, file) => { | ||
acc[Path.basename(file)] = "/" + Path.posix.join(noProtocolBase, `${timestamp}`, file); | ||
return acc; | ||
}, {}); | ||
|
||
Fs.writeFileSync("config/assets.json", `${JSON.stringify(mockAssets, null, 2)}\n`); | ||
}, | ||
|
||
respondAsset(req, res) { | ||
try { | ||
const filePath = req.url.replace(/\/__mock-cdn\/[0-9]+/, "dist"); | ||
const fp = Path.resolve(filePath); | ||
let asset = LOADED_ASSETS[filePath]; | ||
if (!asset) { | ||
asset = LOADED_ASSETS[filePath] = Fs.readFileSync(fp); | ||
} | ||
const ext = Path.extname(filePath); | ||
const mimeType = mime.getType(ext); | ||
res.writeHead(200, { | ||
"Content-Type": mimeType, | ||
"Content-Length": Buffer.byteLength(asset) | ||
}); | ||
res.write(asset); | ||
res.end(); | ||
} catch (err) { | ||
res.statusCode = 404; | ||
res.write("Not Found"); | ||
res.end(); | ||
} | ||
} | ||
}; | ||
|
||
module.exports = cdnMock; |
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