更新
This commit is contained in:
Generated
Vendored
+9
@@ -0,0 +1,9 @@
|
||||
/// <reference types="node" />
|
||||
import type { IncomingMessage, ServerResponse } from 'http';
|
||||
interface UniStaticMiddlewareOptions {
|
||||
etag: boolean;
|
||||
resolve: (pathname: string) => string | void;
|
||||
}
|
||||
export type NextHandler = () => void | Promise<void>;
|
||||
export declare function uniStaticMiddleware(opts: UniStaticMiddlewareOptions): (req: IncomingMessage, res: ServerResponse, next: NextHandler) => void | Promise<void> | ServerResponse<IncomingMessage>;
|
||||
export {};
|
||||
Generated
Vendored
+80
@@ -0,0 +1,80 @@
|
||||
"use strict";
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.uniStaticMiddleware = void 0;
|
||||
const fs_1 = __importDefault(require("fs"));
|
||||
const url_1 = __importDefault(require("url"));
|
||||
const lite_1 = __importDefault(require("mime/lite"));
|
||||
function normalizeFile(filename, isEtag) {
|
||||
const stats = fs_1.default.statSync(filename);
|
||||
return {
|
||||
stats,
|
||||
headers: normalizeHeaders(filename, stats, isEtag),
|
||||
};
|
||||
}
|
||||
function normalizeHeaders(filename, stats, isEtag) {
|
||||
const headers = {
|
||||
'Content-Length': stats.size,
|
||||
'Content-Type': lite_1.default.getType(filename) || '',
|
||||
'Last-Modified': stats.mtime.toUTCString(),
|
||||
};
|
||||
if (isEtag) {
|
||||
headers['ETag'] = `W/"${stats.size}-${stats.mtime.getTime()}"`;
|
||||
}
|
||||
return headers;
|
||||
}
|
||||
function send(req, res, filename, stats, headers) {
|
||||
let code = 200;
|
||||
headers = { ...headers };
|
||||
const opts = {};
|
||||
for (const key in headers) {
|
||||
const value = res.getHeader(key);
|
||||
if (value) {
|
||||
headers[key] = value;
|
||||
}
|
||||
}
|
||||
if (res.getHeader('content-type')) {
|
||||
headers['Content-Type'] = res.getHeader('content-type');
|
||||
}
|
||||
if (req.headers.range) {
|
||||
code = 206;
|
||||
const [x, y] = req.headers.range.replace('bytes=', '').split('-');
|
||||
const end = (opts.end = parseInt(y, 10) || stats.size - 1);
|
||||
const start = (opts.start = parseInt(x, 10) || 0);
|
||||
if (start >= stats.size || end >= stats.size) {
|
||||
res.setHeader('Content-Range', `bytes */${stats.size}`);
|
||||
res.statusCode = 416;
|
||||
return res.end();
|
||||
}
|
||||
headers['Content-Range'] = `bytes ${start}-${end}/${stats.size}`;
|
||||
headers['Content-Length'] = end - start + 1;
|
||||
headers['Accept-Ranges'] = 'bytes';
|
||||
}
|
||||
res.writeHead(code, headers);
|
||||
fs_1.default.createReadStream(filename, opts).pipe(res);
|
||||
}
|
||||
function uniStaticMiddleware(opts) {
|
||||
const isEtag = !!opts.etag;
|
||||
return function staticMiddleware(req, res, next) {
|
||||
const pathname = url_1.default.parse(req.url).pathname;
|
||||
if (!pathname) {
|
||||
return next();
|
||||
}
|
||||
const filename = opts.resolve(pathname);
|
||||
if (!filename) {
|
||||
return next();
|
||||
}
|
||||
const data = normalizeFile(filename, isEtag);
|
||||
if (!data) {
|
||||
return next();
|
||||
}
|
||||
if (isEtag && req.headers['if-none-match'] === data.headers['ETag']) {
|
||||
res.writeHead(304);
|
||||
return res.end();
|
||||
}
|
||||
return send(req, res, filename, data.stats, data.headers);
|
||||
};
|
||||
}
|
||||
exports.uniStaticMiddleware = uniStaticMiddleware;
|
||||
Generated
Vendored
+5
@@ -0,0 +1,5 @@
|
||||
/// <reference types="node" />
|
||||
import type { IncomingMessage, ServerResponse } from 'http';
|
||||
import type { ViteDevServer } from 'vite';
|
||||
import type { NextHandler } from './static';
|
||||
export declare function uniTimestampMiddleware(server: ViteDevServer): (req: IncomingMessage, _: ServerResponse, next: NextHandler) => Promise<void>;
|
||||
TongjiUniApp/node_modules/@dcloudio/uni-h5-vite/dist/plugin/configureServer/middlewares/timestamp.js
Generated
Vendored
+36
@@ -0,0 +1,36 @@
|
||||
"use strict";
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.uniTimestampMiddleware = void 0;
|
||||
const url_1 = require("url");
|
||||
const path_1 = __importDefault(require("path"));
|
||||
const uni_cli_shared_1 = require("@dcloudio/uni-cli-shared");
|
||||
function uniTimestampMiddleware(server) {
|
||||
return async function timestampMiddleware(req, _, next) {
|
||||
// 当页面被作为组件引用时,会导致history刷新该页面直接显示js代码,因为该页面已被缓存为了module,
|
||||
// https://github.com/vitejs/vite/blob/702d50315535c189151c67d33e4a22124f926bed/packages/vite/src/node/server/transformRequest.ts#L52
|
||||
// /pages/tabBar/API/API
|
||||
let { url } = req;
|
||||
if (url) {
|
||||
const base = server.config.base;
|
||||
const parsed = (0, url_1.parse)(url);
|
||||
let newUrl = url;
|
||||
if ((parsed.pathname || '/').startsWith(base)) {
|
||||
newUrl = newUrl.replace(base, '/');
|
||||
}
|
||||
if (!path_1.default.extname(newUrl) &&
|
||||
!newUrl.endsWith('/') &&
|
||||
!newUrl.includes('?')) {
|
||||
const module = await server.moduleGraph.getModuleByUrl(newUrl);
|
||||
if (module && module.file && uni_cli_shared_1.EXTNAME_VUE_RE.test(module.file)) {
|
||||
// /pages/tabBar/API/API => /pages/tabBar/API/API?__t__=time
|
||||
req.url = url + '?__t__=' + Date.now();
|
||||
}
|
||||
}
|
||||
}
|
||||
next();
|
||||
};
|
||||
}
|
||||
exports.uniTimestampMiddleware = uniTimestampMiddleware;
|
||||
Reference in New Issue
Block a user