.
This commit is contained in:
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
Generated
Vendored
+53
@@ -0,0 +1,53 @@
|
||||
import path from 'path';
|
||||
import * as fs from 'fs/promises';
|
||||
import { constants } from 'fs';
|
||||
import * as Log from '../../../build/output/log';
|
||||
import { middlewareResponse } from '../middleware-response';
|
||||
const FONT_PREFIX = '/__nextjs_font/';
|
||||
const VALID_FONTS = [
|
||||
'geist-latin-ext.woff2',
|
||||
'geist-mono-latin-ext.woff2',
|
||||
'geist-latin.woff2',
|
||||
'geist-mono-latin.woff2'
|
||||
];
|
||||
const FONT_HEADERS = {
|
||||
'Content-Type': 'font/woff2',
|
||||
'Cache-Control': 'public, max-age=31536000, immutable'
|
||||
};
|
||||
export function getDevOverlayFontMiddleware() {
|
||||
return async function devOverlayFontMiddleware(req, res, next) {
|
||||
try {
|
||||
const { pathname } = new URL(`http://n${req.url}`);
|
||||
if (!pathname.startsWith(FONT_PREFIX)) {
|
||||
return next();
|
||||
}
|
||||
const fontFile = pathname.replace(FONT_PREFIX, '');
|
||||
if (!VALID_FONTS.includes(fontFile)) {
|
||||
return middlewareResponse.notFound(res);
|
||||
}
|
||||
const fontPath = path.resolve(__dirname, fontFile);
|
||||
const fileExists = await checkFileExists(fontPath);
|
||||
if (!fileExists) {
|
||||
return middlewareResponse.notFound(res);
|
||||
}
|
||||
const fontData = await fs.readFile(fontPath);
|
||||
Object.entries(FONT_HEADERS).forEach(([key, value])=>{
|
||||
res.setHeader(key, value);
|
||||
});
|
||||
res.end(fontData);
|
||||
} catch (err) {
|
||||
Log.error('Failed to serve font:', err instanceof Error ? err.message : err);
|
||||
return middlewareResponse.internalServerError(res);
|
||||
}
|
||||
};
|
||||
}
|
||||
async function checkFileExists(filePath) {
|
||||
try {
|
||||
await fs.access(filePath, constants.F_OK);
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
//# sourceMappingURL=get-dev-overlay-font-middleware.js.map
|
||||
Generated
Vendored
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"sources":["../../../../../src/next-devtools/server/font/get-dev-overlay-font-middleware.ts"],"sourcesContent":["import type { ServerResponse, IncomingMessage } from 'http'\nimport path from 'path'\nimport * as fs from 'fs/promises'\nimport { constants } from 'fs'\nimport * as Log from '../../../build/output/log'\nimport { middlewareResponse } from '../middleware-response'\n\nconst FONT_PREFIX = '/__nextjs_font/'\n\nconst VALID_FONTS = [\n 'geist-latin-ext.woff2',\n 'geist-mono-latin-ext.woff2',\n 'geist-latin.woff2',\n 'geist-mono-latin.woff2',\n]\n\nconst FONT_HEADERS = {\n 'Content-Type': 'font/woff2',\n 'Cache-Control': 'public, max-age=31536000, immutable',\n} as const\n\nexport function getDevOverlayFontMiddleware() {\n return async function devOverlayFontMiddleware(\n req: IncomingMessage,\n res: ServerResponse,\n next: () => void\n ): Promise<void> {\n try {\n const { pathname } = new URL(`http://n${req.url}`)\n\n if (!pathname.startsWith(FONT_PREFIX)) {\n return next()\n }\n\n const fontFile = pathname.replace(FONT_PREFIX, '')\n if (!VALID_FONTS.includes(fontFile)) {\n return middlewareResponse.notFound(res)\n }\n\n const fontPath = path.resolve(__dirname, fontFile)\n const fileExists = await checkFileExists(fontPath)\n\n if (!fileExists) {\n return middlewareResponse.notFound(res)\n }\n\n const fontData = await fs.readFile(fontPath)\n Object.entries(FONT_HEADERS).forEach(([key, value]) => {\n res.setHeader(key, value)\n })\n res.end(fontData)\n } catch (err) {\n Log.error(\n 'Failed to serve font:',\n err instanceof Error ? err.message : err\n )\n return middlewareResponse.internalServerError(res)\n }\n }\n}\n\nasync function checkFileExists(filePath: string): Promise<boolean> {\n try {\n await fs.access(filePath, constants.F_OK)\n return true\n } catch {\n return false\n }\n}\n"],"names":["path","fs","constants","Log","middlewareResponse","FONT_PREFIX","VALID_FONTS","FONT_HEADERS","getDevOverlayFontMiddleware","devOverlayFontMiddleware","req","res","next","pathname","URL","url","startsWith","fontFile","replace","includes","notFound","fontPath","resolve","__dirname","fileExists","checkFileExists","fontData","readFile","Object","entries","forEach","key","value","setHeader","end","err","error","Error","message","internalServerError","filePath","access","F_OK"],"mappings":"AACA,OAAOA,UAAU,OAAM;AACvB,YAAYC,QAAQ,cAAa;AACjC,SAASC,SAAS,QAAQ,KAAI;AAC9B,YAAYC,SAAS,4BAA2B;AAChD,SAASC,kBAAkB,QAAQ,yBAAwB;AAE3D,MAAMC,cAAc;AAEpB,MAAMC,cAAc;IAClB;IACA;IACA;IACA;CACD;AAED,MAAMC,eAAe;IACnB,gBAAgB;IAChB,iBAAiB;AACnB;AAEA,OAAO,SAASC;IACd,OAAO,eAAeC,yBACpBC,GAAoB,EACpBC,GAAmB,EACnBC,IAAgB;QAEhB,IAAI;YACF,MAAM,EAAEC,QAAQ,EAAE,GAAG,IAAIC,IAAI,CAAC,QAAQ,EAAEJ,IAAIK,GAAG,EAAE;YAEjD,IAAI,CAACF,SAASG,UAAU,CAACX,cAAc;gBACrC,OAAOO;YACT;YAEA,MAAMK,WAAWJ,SAASK,OAAO,CAACb,aAAa;YAC/C,IAAI,CAACC,YAAYa,QAAQ,CAACF,WAAW;gBACnC,OAAOb,mBAAmBgB,QAAQ,CAACT;YACrC;YAEA,MAAMU,WAAWrB,KAAKsB,OAAO,CAACC,WAAWN;YACzC,MAAMO,aAAa,MAAMC,gBAAgBJ;YAEzC,IAAI,CAACG,YAAY;gBACf,OAAOpB,mBAAmBgB,QAAQ,CAACT;YACrC;YAEA,MAAMe,WAAW,MAAMzB,GAAG0B,QAAQ,CAACN;YACnCO,OAAOC,OAAO,CAACtB,cAAcuB,OAAO,CAAC,CAAC,CAACC,KAAKC,MAAM;gBAChDrB,IAAIsB,SAAS,CAACF,KAAKC;YACrB;YACArB,IAAIuB,GAAG,CAACR;QACV,EAAE,OAAOS,KAAK;YACZhC,IAAIiC,KAAK,CACP,yBACAD,eAAeE,QAAQF,IAAIG,OAAO,GAAGH;YAEvC,OAAO/B,mBAAmBmC,mBAAmB,CAAC5B;QAChD;IACF;AACF;AAEA,eAAec,gBAAgBe,QAAgB;IAC7C,IAAI;QACF,MAAMvC,GAAGwC,MAAM,CAACD,UAAUtC,UAAUwC,IAAI;QACxC,OAAO;IACT,EAAE,OAAM;QACN,OAAO;IACT;AACF","ignoreList":[0]}
|
||||
Reference in New Issue
Block a user