From 25b287702b5f906c21b94925590b055b68390182 Mon Sep 17 00:00:00 2001 From: Joel Chen Date: Fri, 17 Apr 2020 16:41:06 -0700 Subject: [PATCH] fix HTML log viewer fetching logs across different runs --- packages/xarc-app-dev/lib/dev-admin/log.html | 224 +++++++++++++----- .../xarc-app-dev/lib/dev-admin/middleware.js | 30 ++- 2 files changed, 188 insertions(+), 66 deletions(-) diff --git a/packages/xarc-app-dev/lib/dev-admin/log.html b/packages/xarc-app-dev/lib/dev-admin/log.html index 405c31be0..20b07edb3 100644 --- a/packages/xarc-app-dev/lib/dev-admin/log.html +++ b/packages/xarc-app-dev/lib/dev-admin/log.html @@ -55,7 +55,7 @@ diff --git a/packages/xarc-app-dev/lib/dev-admin/middleware.js b/packages/xarc-app-dev/lib/dev-admin/middleware.js index 523ac5e85..245a13c43 100644 --- a/packages/xarc-app-dev/lib/dev-admin/middleware.js +++ b/packages/xarc-app-dev/lib/dev-admin/middleware.js @@ -8,6 +8,7 @@ const Fs = require("fs"); const webpack = require("webpack"); const hotHelpers = require("webpack-hot-middleware/helpers"); const Url = require("url"); +const queryStr = require("querystring"); const { getWebpackStartConfig } = require("@xarc/webpack/lib/util/custom-check"); const { getLogs, getLogEventAsHtml } = require("./log-reader"); const { fullDevServer, controlPaths } = require("../../config/dev-proxy"); @@ -70,6 +71,7 @@ class Middleware { constructor(options) { this._options = options; this.canContinue = Symbol("webpack dev middleware continue"); + this._instanceId = Date.now(); } setup() { @@ -349,11 +351,29 @@ ${jumpToError} }; const serveLogEvents = async () => { - const htmlLogs = (await getLogs()).map(event => ({ - ...event, - message: getLogEventAsHtml(event) - })); - return Promise.resolve(cycle.replyStaticData(JSON.stringify(htmlLogs))); + const query = queryStr.parse(Url.parse(req.url).query); + let start = parseInt(query.start, 10) || 0; + const id = parseInt(query.id, 10); + if (id !== this._instanceId) { + start = 0; + } + + const htmlLogs = []; + const allLogs = await getLogs(); + for (let ix = start; ix < allLogs.length; ix++) { + const event = allLogs[ix]; + htmlLogs.push({ + ...event, + message: getLogEventAsHtml(event) + }); + } + const data = { + start, + logs: htmlLogs, + total: allLogs.length, + instanceId: this._instanceId + }; + return Promise.resolve(cycle.replyStaticData(JSON.stringify(data))); }; const serveLog = () => {