@ceicc/range
Advanced tools
Comparing version 3.0.0-beta.7 to 3.0.0-beta.8
@@ -1,36 +0,14 @@ | ||
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { | ||
Object.defineProperty(o, "default", { enumerable: true, value: v }); | ||
}) : function(o, v) { | ||
o["default"] = v; | ||
}); | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); | ||
__setModuleDefault(result, mod); | ||
return result; | ||
}; | ||
const fs_1 = require("fs"); | ||
const stream_1 = require("stream"); | ||
const util_1 = require("util"); | ||
const mime_types_1 = require("mime-types"); | ||
const optionsChecker = require("@ceicc/options-checker"); | ||
const url_1 = require("url"); | ||
const path_1 = require("path"); | ||
const utils = __importStar(require("./utils.js")); | ||
import { createReadStream, promises } from "node:fs"; | ||
import { pipeline } from "node:stream"; | ||
import { promisify } from "node:util"; | ||
import { URL } from "node:url"; | ||
import { extname } from "node:path"; | ||
import { contentType } from "mime-types"; | ||
import optionsChecker from "@ceicc/options-checker"; | ||
import * as utils from "./utils.js"; | ||
// Im not going to use `stream/promises` library because it was added in | ||
// version 15.0, Not all hosting providers support that version (including mine) | ||
const pipelinePromised = (0, util_1.promisify)(stream_1.pipeline); | ||
const pipelinePromised = promisify(pipeline); | ||
export { range }; | ||
export default range; | ||
function range(options = {}) { | ||
@@ -52,6 +30,6 @@ optionsChecker(options, { | ||
return async function rangeMiddleware(req, res, next) { | ||
const { pathname } = new url_1.URL(`https://example.com${req.url}`); | ||
const { pathname } = new URL(`https://example.com${req.url}`); | ||
try { | ||
// Using `var` to get function scope | ||
var stat = await fs_1.promises.stat(options.baseDir + pathname); | ||
var stat = await promises.stat(options.baseDir + pathname); | ||
} | ||
@@ -85,3 +63,3 @@ catch (error) { | ||
extensions.add("html"); | ||
const directory = await fs_1.promises.readdir(options.baseDir + pathname); | ||
const directory = await promises.readdir(options.baseDir + pathname); | ||
for (const extension of extensions) { | ||
@@ -96,4 +74,4 @@ if (!directory.includes(`index.${extension}`)) | ||
const etag = options.etag && utils.getEtag(stat.mtime, stat.size); | ||
const extension = (0, path_1.extname)(pathname); | ||
const fileContentType = (0, mime_types_1.contentType)(extension); | ||
const extension = extname(pathname); | ||
const fileContentType = contentType(extension); | ||
etag && res.setHeader("etag", etag); | ||
@@ -174,3 +152,3 @@ options.lastModified && res.setHeader("last-modified", stat.mtime.toUTCString()); | ||
async function streamIt({ path, res, range, transformStream }) { | ||
const readableFile = (0, fs_1.createReadStream)(path, range ? { start: range.start, end: range.end } : undefined); | ||
const readableFile = createReadStream(path, range ? { start: range.start, end: range.end } : undefined); | ||
if (transformStream) { | ||
@@ -215,4 +193,4 @@ return pipelinePromised(readableFile, transformStream, res).catch(catchError); | ||
res.setHeader("content-range", `bytes ${start}-${end}/${size}`); | ||
res.setHeader("content-length", end - start + 1); | ||
return streamIt({ path, res, range: { start, end } }); | ||
} | ||
module.exports = range; |
@@ -1,20 +0,13 @@ | ||
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.hush = exports.hasTrailingSlash = exports.getPossibleEncoding = exports.forgetAboutIt = exports.getEtag = void 0; | ||
const zlib_1 = require("zlib"); | ||
const compressible_1 = __importDefault(require("compressible")); | ||
const negotiator_1 = __importDefault(require("negotiator")); | ||
import { createBrotliCompress, createGzip, createDeflate } from "node:zlib"; | ||
import compressible from "compressible"; | ||
import Negotiator from "negotiator"; | ||
const ENCODINGS_MAP = new Map([ | ||
["br", zlib_1.createBrotliCompress], | ||
["gzip", zlib_1.createGzip], | ||
["deflate", zlib_1.createDeflate] | ||
["br", createBrotliCompress], | ||
["gzip", createGzip], | ||
["deflate", createDeflate] | ||
]); | ||
function getEtag(mtime, size) { | ||
export function getEtag(mtime, size) { | ||
return `W/"${size.toString(16)}-${mtime.getTime().toString(16)}"`; | ||
} | ||
exports.getEtag = getEtag; | ||
function forgetAboutIt(res, status) { | ||
export function forgetAboutIt(res, status) { | ||
res.removeHeader("content-type"); | ||
@@ -26,9 +19,8 @@ res.removeHeader("content-length"); | ||
} | ||
exports.forgetAboutIt = forgetAboutIt; | ||
function getCompressionStream(encoding) { | ||
return ENCODINGS_MAP.get(encoding)?.(); | ||
} | ||
function getPossibleEncoding({ headers, availableEncodings, contentType }) { | ||
const encoding = new negotiator_1.default({ headers }).encoding(availableEncodings); | ||
const isComressible = (0, compressible_1.default)(contentType); | ||
export function getPossibleEncoding({ headers, availableEncodings, contentType }) { | ||
const encoding = new Negotiator({ headers }).encoding(availableEncodings); | ||
const isComressible = compressible(contentType); | ||
if (!encoding || !isComressible) | ||
@@ -39,8 +31,6 @@ return { encoding, stream: null }; | ||
} | ||
exports.getPossibleEncoding = getPossibleEncoding; | ||
function hasTrailingSlash(url) { | ||
export function hasTrailingSlash(url) { | ||
return url[url.length - 1] === "/"; | ||
} | ||
exports.hasTrailingSlash = hasTrailingSlash; | ||
function hush(res) { | ||
export function hush(res) { | ||
if (!res.headersSent) { | ||
@@ -51,2 +41,1 @@ res.statusCode = 500; | ||
} | ||
exports.hush = hush; |
{ | ||
"name": "@ceicc/range", | ||
"version": "3.0.0-beta.7", | ||
"version": "3.0.0-beta.8", | ||
"description": "http range request handler", | ||
"main": "lib/index.js", | ||
"exports": "./lib/index.js", | ||
"type": "module", | ||
"scripts": { | ||
"test": "node test/server.test.js", | ||
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js", | ||
"dev": "nodemon test/server.js" | ||
@@ -38,2 +39,5 @@ }, | ||
"types": "types/", | ||
"engines": { | ||
"node": ">=14.16" | ||
}, | ||
"dependencies": { | ||
@@ -40,0 +44,0 @@ "@ceicc/options-checker": "^1.0.2", |
import type { IncomingMessage, ServerResponse } from "http"; | ||
import type { NextFunction } from "express"; | ||
export = range; | ||
export { range }; | ||
export default range; | ||
declare type options = { | ||
@@ -5,0 +6,0 @@ baseDir?: string; |
@@ -1,2 +0,2 @@ | ||
/// <reference types="node" /> | ||
/// <reference types="node" resolution-mode="require"/> | ||
import type { IncomingHttpHeaders, ServerResponse } from "http"; | ||
@@ -3,0 +3,0 @@ import type { BrotliCompress, Gzip, Deflate } from "zlib"; |
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
0
Yes
17204
268