FileResponse: Pre-build range regex and re-use it for performance benefits
All checks were successful
continuous-integration/appveyor/branch AppVeyor build succeeded

This commit is contained in:
Jonatan Nilsson 2022-03-26 15:59:48 +00:00
parent 499cfa8ce0
commit d4bac4940e
2 changed files with 6 additions and 2 deletions

View file

@ -290,6 +290,8 @@ export class HttpError extends Error {
}
}
const RangeRegexTester = /bytes=(\d+)-(\d+)?/
export class FileResponse {
constructor(filepath, stat) {
this.filepath = filepath
@ -333,7 +335,8 @@ export class FileResponse {
let size = this.stat.size
if (ctx.req.headers['range']) {
let match = ctx.req.headers['range'].match(/bytes=(\d+)-(\d+)?/)
let match = RangeRegexTester.exec(ctx.req.headers['range'])
let ifRange = ctx.req.headers['if-range']
if (ifRange) {
if (ifRange[0] === '"' && ifRange !== etag) {
@ -345,6 +348,7 @@ export class FileResponse {
}
}
}
if (match) {
let start = Number(match[1])
let end = size - 1

View file

@ -1,6 +1,6 @@
{
"name": "flaska",
"version": "1.1.0",
"version": "1.1.1",
"description": "Flaska is a micro web-framework for node. It is designed to be fast, simple and lightweight, and is distributed as a single file module with no dependencies.",
"main": "flaska.mjs",
"scripts": {