Skip to content

Commit

Permalink
feature: use httpStatusCode enum (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
levivilet authored Dec 20, 2024
1 parent fa47fbd commit 214265c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
15 changes: 7 additions & 8 deletions src/parts/HandleRangeRequest/HandleRangeRequest.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createReadStream } from 'node:fs'
import { stat } from 'node:fs/promises'
import { ServerResponse } from 'node:http'
import { OutgoingHttpHeaders, ServerResponse } from 'node:http'
import { pipeline } from 'node:stream/promises'
import * as HttpHeader from '../HttpHeader/HttpHeader.ts'
import * as HttpStatusCode from '../HttpStatusCode/HttpStatusCode.ts'
Expand All @@ -9,7 +9,7 @@ import * as IsStreamPrematureCloseError from '../IsStreamPrematureCloseError/IsS
// TODO add lots of tests for this
export const handleRangeRequest = async (filePath: string, range: string, res: ServerResponse) => {
const stats = await stat(filePath)
let code = 206
let code = HttpStatusCode.PartialContent
let [x, y] = range.replace('bytes=', '').split('-')
let end = parseInt(y, 10) || stats.size - 1
let start = parseInt(x, 10) || 0
Expand All @@ -28,12 +28,11 @@ export const handleRangeRequest = async (filePath: string, range: string, res: S
res.statusCode = HttpStatusCode.OtherError
return res.end()
}
const headers: any = {}

headers[HttpHeader.ContentRange] = `bytes ${start}-${end}/${stats.size}`
headers[HttpHeader.ContentLength] = end - start + 1
headers[HttpHeader.AcceptRanges] = 'bytes'

const headers: OutgoingHttpHeaders = {
[HttpHeader.ContentRange]: `bytes ${start}-${end}/${stats.size}`,
[HttpHeader.ContentLength]: end - start + 1,
[HttpHeader.AcceptRanges]: 'bytes',
}
res.writeHead(code, headers)
const readStream = createReadStream(filePath, options)
try {
Expand Down
3 changes: 2 additions & 1 deletion src/parts/HttpStatusCode/HttpStatusCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ export const MultipleChoices = 300
export const NotFound = 404
export const NotModifed = 304
export const Ok = 200
export const ServerError = 500
export const OtherError = 416
export const PartialContent = 206
export const ServerError = 500

0 comments on commit 214265c

Please sign in to comment.