From d4bac4940eed92a0ccbc2cf050e1fff6fce64a4e Mon Sep 17 00:00:00 2001 From: Jonatan Nilsson Date: Sat, 26 Mar 2022 15:59:48 +0000 Subject: [PATCH] FileResponse: Pre-build range regex and re-use it for performance benefits --- flaska.mjs | 6 +++++- package.json | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/flaska.mjs b/flaska.mjs index 796f1b0..33958e7 100644 --- a/flaska.mjs +++ b/flaska.mjs @@ -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 diff --git a/package.json b/package.json index 581a749..6b75325 100644 --- a/package.json +++ b/package.json @@ -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": {