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 { export class FileResponse {
constructor(filepath, stat) { constructor(filepath, stat) {
this.filepath = filepath this.filepath = filepath
@ -333,7 +335,8 @@ export class FileResponse {
let size = this.stat.size let size = this.stat.size
if (ctx.req.headers['range']) { 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'] let ifRange = ctx.req.headers['if-range']
if (ifRange) { if (ifRange) {
if (ifRange[0] === '"' && ifRange !== etag) { if (ifRange[0] === '"' && ifRange !== etag) {
@ -345,6 +348,7 @@ export class FileResponse {
} }
} }
} }
if (match) { if (match) {
let start = Number(match[1]) let start = Number(match[1])
let end = size - 1 let end = size - 1

View file

@ -1,6 +1,6 @@
{ {
"name": "flaska", "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.", "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", "main": "flaska.mjs",
"scripts": { "scripts": {