fix(core): update

This commit is contained in:
2024-01-07 14:50:14 +01:00
parent a710473d33
commit c9688159e5
8 changed files with 97 additions and 17 deletions

View File

@@ -2,8 +2,10 @@ import * as plugins from '../typedserver.plugins.js';
import * as interfaces from '../interfaces/index.js';
import { Handler } from './classes.handler.js';
import { Compressor } from './classes.compressor.js';
export class HandlerStatic extends Handler {
public compressor = new Compressor();
constructor(
pathArg: string,
optionsArg?: {
@@ -62,11 +64,9 @@ export class HandlerStatic extends Handler {
}
// lets actually care about serving, if security checks pass
let fileString: string;
let fileEncoding: 'binary' | 'utf8';
let fileBuffer: Buffer;
try {
fileString = plugins.smartfile.fs.toStringSync(joinedPath);
fileEncoding = plugins.smartmime.getEncoding(joinedPath);
fileBuffer = plugins.smartfile.fs.toBufferSync(joinedPath);
usedPath = joinedPath;
} catch (err) {
// try serving index.html instead
@@ -75,8 +75,7 @@ export class HandlerStatic extends Handler {
console.log(`serving default path ${defaultPath} instead of ${joinedPath}`);
try {
parsedPath = plugins.path.parse(defaultPath);
fileString = plugins.smartfile.fs.toStringSync(defaultPath);
fileEncoding = plugins.smartmime.getEncoding(defaultPath);
fileBuffer = plugins.smartfile.fs.toBufferSync(defaultPath);
usedPath = defaultPath;
} catch (err) {
res.writeHead(500);
@@ -99,7 +98,7 @@ export class HandlerStatic extends Handler {
const modifiedResponse = await optionsArg.responseModifier({
headers: res.getHeaders(),
path: usedPath,
responseContent: fileString,
responseContent: fileBuffer,
travelData,
});
@@ -115,11 +114,15 @@ export class HandlerStatic extends Handler {
}
// responseContent
fileString = modifiedResponse.responseContent;
fileBuffer = modifiedResponse.responseContent;
}
// lets finally deal with compression
const compressionResult = await this.compressor.maybeCompress(requestHeaders, fileBuffer);
res.status(200);
res.write(Buffer.from(fileString, fileEncoding));
res.header('Content-Encoding', compressionResult.compressionMethod);
res.write(compressionResult.result);
res.end();
});
}