Add '_regroup_monorepo/express-partial-content/' from commit 'b80bfb7ca44c16bd5b747362ac2f53ece911b225'

git-subtree-dir: _regroup_monorepo/express-partial-content
git-subtree-mainline: 7349176c988df849f7f1cf46f22d48d6b04597e0
git-subtree-split: b80bfb7ca44c16bd5b747362ac2f53ece911b225
This commit is contained in:
Elian Doran 2025-05-03 01:19:52 +03:00
commit 5dc060f49c
37 changed files with 4442 additions and 0 deletions

View File

@ -0,0 +1,9 @@
{
"parser": "@typescript-eslint/parser",
"plugins": ["@typescript-eslint"],
"extends": ["plugin:@typescript-eslint/recommended"],
"rules": {
"@typescript-eslint/indent": ["error", 2],
"@typescript-eslint/no-explicit-any": "off"
}
}

View File

@ -0,0 +1,38 @@
---
name: Bug report
about: Create a report to help us improve
title: ''
labels: ''
assignees: ''
---
**Describe the bug**
A clear and concise description of what the bug is.
**To Reproduce**
Steps to reproduce the behavior:
1. Go to '...'
2. Click on '....'
3. Scroll down to '....'
4. See error
**Expected behavior**
A clear and concise description of what you expected to happen.
**Screenshots**
If applicable, add screenshots to help explain your problem.
**Desktop (please complete the following information):**
- OS: [e.g. iOS]
- Browser [e.g. chrome, safari]
- Version [e.g. 22]
**Smartphone (please complete the following information):**
- Device: [e.g. iPhone6]
- OS: [e.g. iOS8.1]
- Browser [e.g. stock browser, safari]
- Version [e.g. 22]
**Additional context**
Add any other context about the problem here.

View File

@ -0,0 +1,20 @@
---
name: Feature request
about: Suggest an idea for this project
title: ''
labels: ''
assignees: ''
---
**Is your feature request related to a problem? Please describe.**
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
**Describe the solution you'd like**
A clear and concise description of what you want to happen.
**Describe alternatives you've considered**
A clear and concise description of any alternative solutions or features you've considered.
**Additional context**
Add any other context or screenshots about the feature request here.

View File

@ -0,0 +1,26 @@
name: Release
on:
push:
tags:
- 'v*'
jobs:
publish-package:
name: Publish package
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Node
uses: actions/setup-node@v4
with:
node-version: 22
registry-url: 'https://registry.npmjs.org'
- name: Set up yarn with corepack
run: |
corepack enable &&
corepack install
- name: Install dependencies
run: yarn install --frozen-lockfile
- name: Publish package
run: yarn push
env:
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}

View File

@ -0,0 +1,7 @@
coverage
.nyc_output
*.log
.env
node_modules
dist
.yarn

View File

@ -0,0 +1,11 @@
coverage
.nyc_output
node_modules
.travis.yml
yarn.lock
.vscode
tsconfig.*
.prettierrc.json
_config.yml
src
*.log

View File

@ -0,0 +1,5 @@
{
"printWidth": 120,
"semi": true,
"bracketSpacing": true
}

View File

@ -0,0 +1,6 @@
language: node_js
node_js:
- "8"
cache: yarn

View File

@ -0,0 +1,4 @@
{
"cSpell.words": ["Gujar", "Sukant", "copyfiles", "sukantgujar"],
"eslint.validate": ["javascript", "typescript"]
}

View File

@ -0,0 +1,76 @@
# Contributor Covenant Code of Conduct
## Our Pledge
In the interest of fostering an open and welcoming environment, we as
contributors and maintainers pledge to making participation in our project and
our community a harassment-free experience for everyone, regardless of age, body
size, disability, ethnicity, sex characteristics, gender identity and expression,
level of experience, education, socio-economic status, nationality, personal
appearance, race, religion, or sexual identity and orientation.
## Our Standards
Examples of behavior that contributes to creating a positive environment
include:
* Using welcoming and inclusive language
* Being respectful of differing viewpoints and experiences
* Gracefully accepting constructive criticism
* Focusing on what is best for the community
* Showing empathy towards other community members
Examples of unacceptable behavior by participants include:
* The use of sexualized language or imagery and unwelcome sexual attention or
advances
* Trolling, insulting/derogatory comments, and personal or political attacks
* Public or private harassment
* Publishing others' private information, such as a physical or electronic
address, without explicit permission
* Other conduct which could reasonably be considered inappropriate in a
professional setting
## Our Responsibilities
Project maintainers are responsible for clarifying the standards of acceptable
behavior and are expected to take appropriate and fair corrective action in
response to any instances of unacceptable behavior.
Project maintainers have the right and responsibility to remove, edit, or
reject comments, commits, code, wiki edits, issues, and other contributions
that are not aligned to this Code of Conduct, or to ban temporarily or
permanently any contributor for other behaviors that they deem inappropriate,
threatening, offensive, or harmful.
## Scope
This Code of Conduct applies both within project spaces and in public spaces
when an individual is representing the project or its community. Examples of
representing a project or community include using an official project e-mail
address, posting via an official social media account, or acting as an appointed
representative at an online or offline event. Representation of a project may be
further defined and clarified by project maintainers.
## Enforcement
Instances of abusive, harassing, or otherwise unacceptable behavior may be
reported by contacting the project team at sukantgujar@yahoo.com. All
complaints will be reviewed and investigated and will result in a response that
is deemed necessary and appropriate to the circumstances. The project team is
obligated to maintain confidentiality with regard to the reporter of an incident.
Further details of specific enforcement policies may be posted separately.
Project maintainers who do not follow or enforce the Code of Conduct in good
faith may face temporary or permanent repercussions as determined by other
members of the project's leadership.
## Attribution
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html
[homepage]: https://www.contributor-covenant.org
For answers to common questions about this code of conduct, see
https://www.contributor-covenant.org/faq

View File

@ -0,0 +1,8 @@
# About
This file documents the contribution guidelines for this project. This list is by no means complete and the final decision to accept contribution in form of PRs is reserved by the owner of this project.
# Code quality
- This project uses Prettier to take care of some of the code styling hassles. Ensure that the commited code is pre-formatted per the included Prettier configuration file.
- All tests must pass before the code is pushed.
- Fix any tests which are broken by the new changes.
- Add new tests along with your changes before submitting PRs.

View File

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2019 Sukant Gujar
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@ -0,0 +1,149 @@
![Travis CI Status](https://travis-ci.com/SukantGujar/express-partial-content.svg?branch=master)
# About
A HTTP 206 Partial Content handler to serve any readable stream partially in Express.
Based on this blog post: https://www.codeproject.com/Articles/813480/HTTP-Partial-Content-In-Node-js.
# Installation
`yarn add express-partial-content`
OR
`npm install express-partial-content`
> Note: `Express` package is a peer dependency for `express-partial-content` and must be present in dependencies of the host package.
# Usage
From the `express-file-server` example:
1. Implement a `ContentProvider` function which prepares and returns a `Content` object:
import { promisify } from "util";
import fs from "fs";
import { Range, ContentDoesNotExistError, ContentProvider } from "express-partial-content";
import {logger} from "./logger";
const statAsync = promisify(fs.stat);
const existsAsync = promisify(fs.exists);
export const fileContentProvider: ContentProvider = async (req: Request) => {
// Read file name from route params.
const fileName = req.params.name;
const file = `${__dirname}/files/${fileName}`;
if (!(await existsAsync(file))) {
throw new ContentDoesNotExistError(`File doesn't exist: ${file}`);
}
const stats = await statAsync(file);
const totalSize = stats.size;
const mimeType = "application/octet-stream";
const getStream = (range?: Range) => {
if (!range) {
// Request if for complete content.
return fs.createReadStream(file);
}
// Partial content request.
const { start, end } = range;
logger.debug(`start: ${start}, end: ${end}`);
return fs.createReadStream(file, { start, end });
};
return {
fileName,
totalSize,
mimeType,
getStream
};
};
2. In your express code, use `createPartialContentHandler` factory method to generate an express handler for serving partial content for the route of your choice:
import {createPartialContentHandler} from "express-partial-content";
import {logger} from "./logger";
const handler = createPartialContentHandler(fileContentProvider, logger);
const app = express();
const port = 8080;
// File name is a route param.
app.get("/files/:name", handler);
app.listen(port, () => {
logger.debug("Server started!");
});
3. Run your server and use a multi-part/multi-connection download utility like [aria2c](https://aria2.github.io/) to test it:
aria -x5 -k1M http://localhost:8080/files/readme.txt
# Examples
There one examples in the `src/examples` folder:
1. `express-file-server`: Implements a file based `ContentProvider`.
## Running the examples:
1. `express-file-server`: Run the following commands, the server will listen on http://localhost:8080/.
yarn build:dev
yarn copy-assets
yarn run:examples:file
## Connecting to the running server:
Browse to `https://localhost:8080/files/readme.txt`
# Reference
## createPartialContentHandler function:
This is a factory method which generates a partial content handler for express routes.
### Arguments:
- `contentProvider`: An `async` function which returns a Promise resolved to a `Content` object (see below).
- `logger`: Any logging implementation which has a `debug(message:string, extra: any)` method. Either `winston` or `bunyan` loggers should work.
### Returns:
- Express Route Handler: `createPartialContentHandler` returns an express handler which can be mapped to an Express route to serve partial content.
## ContentProvider function:
This function _needs to be implemented by you_. It's purpose is to fetch and return `Content` object containing necessary metadata and methods to stream the content partially. This method is invoked by the express handler (returned by `createPartialContentHandler`) on each request.
### Arguments:
- `Request`: It receives the `Request` object as it's only input. Use the information available in `Request` to find the requested content, e.g. through `Request.params` or query string, headers etc.
### Returns:
- `Promise<Content>`: See below.
### Throws:
- `ContentDoesNotExistError`: Throw this to indicate that the content doesn't exist. The generated express handler will return a 404 in this case.
> Note: Any message provided to the `ContentDoesNotExistError` object is returned to the client.
## Content object:
This object contains metadata and methods which describe the content. The `ContentProvider` method builds and returns it.
### Properties:
All the properties of this object are used to return content metadata to the client as various `Response` headers.
- `fileName`: Used as the `Content-Disposition` header's `filename` value.
- `mimeType`: Used as the `Content-Type` header value.
- `totalSize`: Used as the `Content-Length` header value.
### Methods:
- `getStream(range?: Range)`: This method should return a readable stream initialized to the provided `range` (optional). You need to handle two cases:
- range is `null`: When `range` is not-specified, the client is requesting the full content. In this case, return the stream as it is.
- range is `{start, end}`: When client requests partial content, the `start` and `end` values will point to the corresponding byte positions (0 based and inclusive) of the content. You need to return stream limited to these positions.

View File

@ -0,0 +1 @@
theme: jekyll-theme-slate

View File

@ -0,0 +1,52 @@
{
"name": "@triliumnext/express-partial-content",
"description": "A partial content handler implementation for any readable stream with Express. Based on this blog post: https://www.codeproject.com/Articles/813480/HTTP-Partial-Content-In-Node-js.",
"version": "1.1.0",
"main": "dist/index.js",
"repository": "https://github.com/TriliumNext/express-partial-content",
"author": "Elian Doran <contact@eliandoran.me>",
"homepage": "https://github.com/TriliumNext/express-partial-content",
"license": "MIT",
"scripts": {
"clean": "rimraf ./dist",
"copy-assets": "copyfiles -E -u 1 src/examples/sample-files/* dist/",
"test": "tsc && nyc --reporter=text mocha dist/**/*.spec.js",
"build:watch": "npx tsc -w",
"build:dev": "npx tsc && yarn copy-assets",
"build:prod": "yarn clean && cross-env NODE_ENV=production tsc -p ./tsconfig.production.json",
"push": "yarn test && yarn build:prod && yarn publish --access public",
"run:examples:file": "node ./dist/examples/express-file-server/index.js"
},
"bugs": {
"email": "contact@eliandoran.me",
"url": "https://github.com/TriliumNext/express-partial-content/issues"
},
"keywords": [
"partial-content",
"206",
"stream",
"typescript"
],
"devDependencies": {
"@types/chai": "^4.1.7",
"@types/express": "^4.16.1",
"@types/mocha": "^5.2.6",
"@types/sinon": "^7.0.9",
"@typescript-eslint/eslint-plugin": "^1.5.0",
"@typescript-eslint/parser": "^1.5.0",
"chai": "^4.2.0",
"copyfiles": "^2.1.0",
"cross-env": "^5.2.0",
"eslint": "^5.15.3",
"express": "^5.1.0",
"mocha": "^6.0.2",
"nyc": "^17.1.0",
"rimraf": "^2.6.3",
"sinon": "^7.2.7",
"typescript": "^5.7.2"
},
"peerDependencies": {
"express": "^4.16.4 || ^5.1.0"
},
"packageManager": "yarn@1.22.22"
}

View File

@ -0,0 +1,182 @@
import * as utils from "../src/utils";
import * as ParseRangeHeaderExports from "../src/parseRangeHeader";
import { ContentDoesNotExistError } from "../src/ContentDoesNotExistError";
import { SinonSandbox, createSandbox, SinonStub, SinonSpy } from "sinon";
import { createPartialContentHandler } from "../src/createPartialContentHandler";
import { ContentProvider } from "../src/ContentProvider";
import { Logger } from "../src/Logger";
import { expect } from "chai";
import { Request, Response } from "express";
import { Content } from "../src/Content";
import { Stream } from "stream";
import { Range } from "../src/Range";
describe("createPartialContentHandler tests", () => {
let sandbox: SinonSandbox;
let logger: Logger;
beforeEach(() => {
sandbox = createSandbox();
logger = {
debug: sandbox.stub() as (message: string, extra?: any) => void
};
});
afterEach(() => {
sandbox.restore();
});
it("returns a handler", () => {
const contentProvider = sandbox.stub().resolves({}) as ContentProvider;
const handler = createPartialContentHandler(contentProvider, logger);
expect(typeof handler === "function");
});
describe("handler tests", () => {
let req: Request;
let res: Response;
let statusSpy: SinonSpy;
let sendSpy: SinonSpy;
let sendStatusSpy: SinonSpy;
beforeEach(() => {
req = {} as Request;
res = {
status: (code: number) => res,
send: (message: string) => res,
sendStatus: (code: number) => res,
setHeader: sandbox.stub() as (name: string, value: string) => void
} as Response;
statusSpy = sandbox.spy(res, "status");
sendSpy = sandbox.spy(res, "send");
sendStatusSpy = sandbox.spy(res, "sendStatus");
});
it("invokes contentProvider with the specified request", async () => {
const contentProvider = sandbox.stub().resolves({}) as ContentProvider;
const handler = createPartialContentHandler(contentProvider, logger);
try {
await handler(req, res);
} catch {}
expect((contentProvider as SinonStub).calledOnceWith(req));
});
it("returns 404 if contentProvider throws ContentDoesNotExistError error", async () => {
const error = new ContentDoesNotExistError("404-File not found!");
const contentProvider = sandbox.stub().rejects(error) as ContentProvider;
const handler = createPartialContentHandler(contentProvider, logger);
try {
await handler(req, res);
expect(statusSpy.calledOnceWith(404));
expect(sendSpy.calledOnceWith(error.message));
} catch {
expect(false);
}
});
it("returns 500 if contentProvider throws any other error", async () => {
const error = new Error("Something went wrong!");
const contentProvider = sandbox.stub().rejects(error) as ContentProvider;
const handler = createPartialContentHandler(contentProvider, logger);
try {
await handler(req, res);
expect(sendStatusSpy.calledOnceWith(500));
} catch {
expect(false);
}
});
it("returns 416 if parseRangeHeader throws RangeParserError error", async () => {
const contentProvider = sandbox.stub().resolves({}) as ContentProvider;
const handler = createPartialContentHandler(contentProvider, logger);
req.headers = { range: "bytes=30-10" };
try {
await handler(req, res);
expect(statusSpy.calledOnceWith(416));
} catch {
expect(false);
}
});
it("returns 500 if parseRangeHeader throws other errors", async () => {
const parseRangeHeaderStub = sandbox
.stub(ParseRangeHeaderExports, "parseRangeHeader")
.throws(new Error("Something went wrong!"));
const contentProvider = sandbox.stub().resolves({}) as ContentProvider;
const handler = createPartialContentHandler(contentProvider, logger);
try {
await handler(req, res);
expect(sendStatusSpy.calledOnceWith(500));
} catch {
expect(false);
}
});
it("returns correct response if range is not specified", async () => {
const result = ({
pipe() {
return result;
}
} as any) as Stream;
const content: Content = {
fileName: "file.txt",
totalSize: 10,
mimeType: "text/plain",
getStream(range?: Range) {
return result;
}
};
const pipeSpy = sandbox.spy(result, "pipe");
const getStreamSpy = sandbox.spy(content, "getStream");
const contentProvider = sandbox.stub().resolves(content) as ContentProvider;
const handler = createPartialContentHandler(contentProvider, logger);
const setContentTypeHeaderSpy = sandbox.spy(utils, "setContentTypeHeader");
const setContentDispositionHeaderSpy = sandbox.spy(utils, "setContentDispositionHeader");
const setAcceptRangesHeaderSpy = sandbox.spy(utils, "setAcceptRangesHeader");
const setContentLengthHeaderSpy = sandbox.spy(utils, "setContentLengthHeader");
const setContentRangeHeaderSpy = sandbox.spy(utils, "setContentRangeHeader");
try {
await handler(req, res);
expect(setContentTypeHeaderSpy.calledOnceWith(content.mimeType, res));
expect(setContentDispositionHeaderSpy.calledOnceWith(content.fileName, res));
expect(setAcceptRangesHeaderSpy.calledOnceWith(res));
expect(setContentLengthHeaderSpy.calledOnceWith(content.totalSize, res));
expect(getStreamSpy.calledOnceWith());
expect(pipeSpy.calledOnceWith(res));
expect(setContentRangeHeaderSpy.notCalled);
} catch {
expect(false);
}
});
it("returns correct partial response if range is specified", async () => {
req.headers = {
range: "bytes=0-5"
};
const result = ({
pipe() {
return result;
}
} as any) as Stream;
const content: Content = {
fileName: "file.txt",
totalSize: 10,
mimeType: "text/plain",
getStream(range?: Range) {
return result;
}
};
const range = { start: 0, end: 5 };
const pipeSpy = sandbox.spy(result, "pipe");
const getStreamSpy = sandbox.spy(content, "getStream");
const contentProvider = sandbox.stub().resolves(content) as ContentProvider;
const handler = createPartialContentHandler(contentProvider, logger);
const setContentTypeHeaderSpy = sandbox.spy(utils, "setContentTypeHeader");
const setContentDispositionHeaderSpy = sandbox.spy(utils, "setContentDispositionHeader");
const setAcceptRangesHeaderSpy = sandbox.spy(utils, "setAcceptRangesHeader");
const setContentLengthHeaderSpy = sandbox.spy(utils, "setContentLengthHeader");
const setContentRangeHeaderSpy = sandbox.spy(utils, "setContentRangeHeader");
try {
await handler(req, res);
expect(setContentTypeHeaderSpy.calledOnceWith(content.mimeType, res));
expect(setContentDispositionHeaderSpy.calledOnceWith(content.fileName, res));
expect(setAcceptRangesHeaderSpy.calledOnceWith(res));
expect(setContentRangeHeaderSpy.calledOnceWith(range, content.totalSize, res));
expect(setContentLengthHeaderSpy.calledOnceWith(6, res));
expect(getStreamSpy.calledOnceWith(range));
expect(pipeSpy.calledOnceWith(res));
} catch {
expect(false);
}
});
});
});

View File

@ -0,0 +1,58 @@
import { parseRangeHeader } from "../src/parseRangeHeader";
import { SinonSandbox, createSandbox } from "sinon";
import { Logger } from "../src/Logger";
import { expect } from "chai";
import { RangeParserError } from "../src/RangeParserError";
describe("parseRangeHeader tests", () => {
let sandbox: SinonSandbox;
let logger: Logger;
beforeEach(() => {
sandbox = createSandbox();
logger = {
debug: sandbox.stub() as (message: string, extra?: any) => void
};
});
afterEach(() => {
sandbox.restore();
});
it("returns null if range is not specified", () => {
let value = parseRangeHeader("", 10, logger);
expect(value).to.be.equal(null);
value = parseRangeHeader(null, 10, logger);
expect(value).to.be.equal(null);
});
it("returns null if total size is zero", () => {
let value = parseRangeHeader("bytes=0-5", 0, logger);
expect(value).to.be.equal(null);
});
it("if end is not provided, sets end to the last byte (totalSize - 1).", () => {
let value = parseRangeHeader("bytes=0-", 10, logger);
expect(value).to.be.deep.equal({ start: 0, end: 9 });
});
it('if start is not provided, set it to the offset of last "end" bytes from the end of the file.', () => {
let value = parseRangeHeader("bytes=-5", 10, logger);
expect(value).to.be.deep.equal({ start: 5, end: 9 });
});
it("handles invalid ranges", () => {
try {
parseRangeHeader("bytes=6-5", 10, logger);
} catch (error) {
expect(error).that.be.instanceOf(RangeParserError);
}
try {
parseRangeHeader("bytes=6-7", 10, logger);
} catch (error) {
expect(error).that.be.instanceOf(RangeParserError);
}
try {
parseRangeHeader("bytes=6-11", 10, logger);
} catch (error) {
expect(error).that.be.instanceOf(RangeParserError);
}
});
it("returns a valid parsed range.", () => {
let value = parseRangeHeader("bytes=0-5", 10, logger);
expect(value).to.be.deep.equal({ start: 0, end: 5 });
});
});

View File

@ -0,0 +1,110 @@
import { Request, Response } from "express";
import { expect } from "chai";
import sinon, { SinonStub, SinonSpy } from "sinon";
import {
getHeader,
setHeader,
getRangeHeader,
setContentTypeHeader,
setContentLengthHeader,
setAcceptRangesHeader,
setContentDispositionHeader,
setContentRangeHeader,
setCacheControlHeaderNoCache
} from "../src/utils";
describe("utils tests", () => {
let req: Request;
let res: Response;
beforeEach(() => {
req = {
headers: {
"content-type": "application/octet-stream",
range: "*"
}
} as Request;
res = {
setHeader: sinon.stub() as (name: string, value: string) => void
} as Response;
});
describe("getHeader tests", () => {
it("gets the specified header value if present", () => {
const value = getHeader("content-type", req);
expect(value).to.equal("application/octet-stream");
});
it("returns undefined if the specified header value is absent", () => {
const value = getHeader("mime-type", req);
expect(value).to.be.undefined;
});
});
describe("setHeader tests", () => {
it("invokes res.setHeader API with the specified name and value args", () => {
const name = "Content-Type";
const value = "application/octet-stream";
setHeader(name, value, res);
expect((res.setHeader as SinonStub).calledOnceWith(name, value));
});
});
describe("getRangeHeader tests", () => {
it("gets range header value", () => {
const value = getRangeHeader(req);
expect(value).to.equal("*");
});
});
describe("setContentTypeHeader tests", () => {
it("sets Content-Type header with specified value", () => {
const value = "application/octet-stream";
setContentTypeHeader(value, res);
expect((res.setHeader as SinonStub).calledOnceWith("Content-Type", value));
});
});
describe("setContentLengthHeader tests", () => {
it("sets Content-Length header with specified value", () => {
const value = 100;
setContentLengthHeader(value, res);
expect((res.setHeader as SinonStub).calledOnceWith("Content-Length", value));
});
});
describe("setAcceptRangesHeader tests", () => {
it("sets Accept-Ranges header with specified value", () => {
const value = "bytes";
setAcceptRangesHeader(res);
expect((res.setHeader as SinonStub).calledOnceWith("Accept-Ranges", value));
});
});
describe("setContentRangeHeader tests", () => {
it("sets Content-Range header with specified value", () => {
let range = { start: 10, end: 100 };
const size = 1000;
let value = `bytes ${range.start}-${range.end}/${size}`;
setContentRangeHeader(range, size, res);
expect((res.setHeader as SinonStub).calledOnceWith("Content-Range", value));
range = null;
value = `bytes */${size}`;
setContentRangeHeader(range, size, res);
expect((res.setHeader as SinonStub).calledOnceWith("Content-Range", value));
});
});
describe("setContentDispositionHeader tests", () => {
it("sets Content-Disposition header with specified value", () => {
const fileName = "file.txt";
const value = `attachment; filename*=utf-8''${fileName}`;
setContentDispositionHeader(fileName, res);
expect((res.setHeader as SinonStub).calledOnceWith("Content-Disposition", value)).to.be.true;
});
it("sets Content-Disposition header with specified unicode", () => {
const fileName = "file.txt";
const value = `attachment; filename*=utf-8''${encodeURIComponent(fileName)}`;
setContentDispositionHeader(fileName, res);
expect((res.setHeader as SinonStub).calledOnceWith("Content-Disposition", value)).to.be.true;
});
});
describe("setCacheControlHeaderNoCache tests", () => {
it("sets Cache-Control header with specified value", () => {
const value = "no-cache";
setCacheControlHeaderNoCache(res);
expect((res.setHeader as SinonStub).calledOnceWith("Cache-Control", value));
});
});
});

View File

@ -0,0 +1,22 @@
import { Range } from "./Range";
import { Stream } from "stream";
export interface Content {
/**
* Returns a readable stream based on the provided range (optional).
* @param {Range} range The start-end range of stream data.
* @returns {Stream} A readable stream
*/
getStream(range?: Range): Stream;
/**
* Total size of the content
*/
readonly totalSize: number;
/**
* Mime type to be sent in Content-Type header
*/
readonly mimeType: string;
/**
* File name to be sent in Content-Disposition header
*/
readonly fileName: string;
};

View File

@ -0,0 +1,2 @@
export class ContentDoesNotExistError extends Error {
}

View File

@ -0,0 +1,6 @@
import { Request } from "express";
import { Content } from "./Content";
/**
* @type {function (Request): Promise<Content>}
*/
export type ContentProvider = (req: Request) => Promise<Content>;

View File

@ -0,0 +1,3 @@
export interface Logger {
debug(message: string, extra?: any): void;
}

View File

@ -0,0 +1,4 @@
export type Range = {
start: number;
end: number;
};

View File

@ -0,0 +1,5 @@
export class RangeParserError extends Error {
constructor(start: any, end: any) {
super(`Invalid start and end values: ${start}-${end}.`);
}
}

View File

@ -0,0 +1,60 @@
import { Request, Response } from "express";
import { parseRangeHeader } from "./parseRangeHeader";
import { RangeParserError } from "./RangeParserError";
import { Logger } from "./Logger";
import { ContentProvider } from "./ContentProvider";
import { ContentDoesNotExistError } from "./ContentDoesNotExistError";
import {
getRangeHeader,
setContentRangeHeader,
setContentTypeHeader,
setContentDispositionHeader,
setAcceptRangesHeader,
setContentLengthHeader,
setCacheControlHeaderNoCache
} from "./utils";
export function createPartialContentHandler(contentProvider: ContentProvider, logger: Logger) {
return async function handler(req: Request, res: Response) {
let content;
try {
content = await contentProvider(req);
} catch (error) {
logger.debug("createPartialContentHandler: ContentProvider threw exception: ", error);
if (error instanceof ContentDoesNotExistError) {
return res.status(404).send(error.message);
}
return res.sendStatus(500);
}
let { getStream, mimeType, fileName, totalSize } = content;
const rangeHeader = getRangeHeader(req);
let range;
try {
range = parseRangeHeader(rangeHeader, totalSize, logger);
} catch (error) {
logger.debug(`createPartialContentHandler: parseRangeHeader error: `, error);
if (error instanceof RangeParserError) {
setContentRangeHeader(null, totalSize, res);
return res.status(416).send(`Invalid value for Range: ${rangeHeader}`);
}
return res.sendStatus(500);
}
setContentTypeHeader(mimeType, res);
setContentDispositionHeader(fileName, res);
setAcceptRangesHeader(res);
// If range is not specified, or the file is empty, return the full stream
if (range === null) {
logger.debug("createPartialContentHandler: No range found, returning full content.");
setContentLengthHeader(totalSize, res);
return getStream().pipe(res);
}
setContentRangeHeader(range, totalSize, res);
let { start, end } = range;
setContentLengthHeader(start === end ? 0 : end - start + 1, res);
setCacheControlHeaderNoCache(res);
// Return 206 Partial Content status
logger.debug("createPartialContentHandler: Returning partial content for range: ", JSON.stringify(range));
res.status(206);
return getStream(range).pipe(res);
};
}

View File

@ -0,0 +1,31 @@
import { Request } from "express";
import fs from "fs";
import { Range, ContentDoesNotExistError, ContentProvider } from "../../index";
import { existsAsync, statAsync } from "./utils";
import { logger } from "./logger";
export const fileContentProvider: ContentProvider = async (req: Request) => {
const fileName = req.params.name;
const file = `${__dirname}/../sample-files/${fileName}`;
if (!(await existsAsync(file))) {
throw new ContentDoesNotExistError(`File doesn't exist: ${file}`);
}
const stats = await statAsync(file);
const totalSize = stats.size;
const mimeType = "application/octet-stream";
const getStream = (range?: Range) => {
if (!range) {
return fs.createReadStream(file);
}
const { start, end } = range;
logger.debug(`start: ${start}, end: ${end}`);
return fs.createReadStream(file, { start, end });
};
return {
fileName,
totalSize,
mimeType,
getStream
};
};

View File

@ -0,0 +1,15 @@
import express from "express";
import { createPartialContentHandler } from "../../index";
import { fileContentProvider } from "./fileContentProvider";
import { logger } from "./logger";
const handler = createPartialContentHandler(fileContentProvider, logger);
const app = express();
const port = 8080;
app.get("/files/:name", handler);
app.listen(port, () => {
logger.debug("Server started!");
});

View File

@ -0,0 +1,9 @@
export const logger = {
debug(message: string, extra?: any) {
if (extra) {
console.log(`[debug]: ${message}`, extra);
} else {
console.log(`[debug]: ${message}`);
}
}
};

View File

@ -0,0 +1,5 @@
import { promisify } from "util";
import fs from "fs";
export const statAsync = promisify(fs.stat);
export const existsAsync = promisify(fs.exists);

View File

@ -0,0 +1,7 @@
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Magna etiam tempor orci eu lobortis elementum nibh. In egestas erat imperdiet sed euismod. Amet consectetur adipiscing elit pellentesque habitant.
Vel quam elementum pulvinar etiam non quam lacus suspendisse.
Nibh sit amet commodo nulla facilisi. Vel risus commodo viverra maecenas accumsan lacus. Ornare arcu dui vivamus arcu felis bibendum ut tristique et.
Vitae semper quis lectus nulla at volutpat diam. Mauris vitae ultricies leo integer malesuada nunc. Donec massa sapien faucibus et. Senectus et netus et malesuada. Vitae tortor condimentum lacinia quis vel. Sagittis id consectetur purus ut faucibus pulvinar elementum. Nisi est sit amet facilisis magna etiam tempor orci eu.
Dictum varius duis at consectetur lorem donec massa sapien. Odio pellentesque diam volutpat commodo. Egestas dui id ornare arcu odio ut sem nulla. Consequat id porta nibh venenatis cras sed felis eget. Placerat in egestas erat imperdiet. Dui nunc mattis enim ut tellus elementum sagittis vitae. Aliquet bibendum enim facilisis gravida neque convallis a cras. Id semper risus in hendrerit gravida. Tempor orci eu lobortis elementum nibh tellus molestie. Semper auctor neque vitae tempus quam pellentesque. Vitae proin sagittis nisl rhoncus mattis rhoncus urna neque. Bibendum at varius vel pharetra vel turpis. Tellus integer feugiat scelerisque varius morbi enim nunc. Volutpat commodo sed egestas egestas fringilla. Congue eu consequat ac felis donec et odio. Venenatis cras sed felis eget velit aliquet. Urna neque viverra justo nec. Dictum non consectetur a erat nam. Lacinia quis vel eros donec.

View File

@ -0,0 +1,6 @@
export * from "./Content";
export * from "./ContentDoesNotExistError";
export * from "./ContentProvider";
export * from "./createPartialContentHandler";
export * from "./Logger";
export * from "./Range";

View File

@ -0,0 +1,57 @@
import { Logger } from "./Logger";
import { RangeParserError } from "./RangeParserError";
import { Range } from "./Range";
const rangeRegEx = /bytes=([0-9]*)-([0-9]*)/;
export function parseRangeHeader(range: string, totalSize: number, logger: Logger): Range | null {
logger.debug("Un-parsed range is: ", range);
// 1. If range is not specified or the file is empty, return null.
if (!range || range === null || range.length === 0 || totalSize === 0) {
return null;
}
const splitRange = range.split(rangeRegEx);
const [, startValue, endValue] = splitRange;
let start = Number.parseInt(startValue);
let end = Number.parseInt(endValue);
// 2. Parse start and end values and ensure they are within limits.
// 2.1. start: >= 0.
// 2.2. end: >= 0, <= totalSize - 1
let result = {
start: Number.isNaN(start) ? 0 : Math.max(start, 0),
end: Number.isNaN(end) ? totalSize - 1 : Math.min(Math.max(end, 0), totalSize - 1)
};
// 3.1. If end is not provided, set end to the last byte (totalSize - 1).
if (!Number.isNaN(start) && Number.isNaN(end)) {
logger.debug("End is not provided.");
result.start = start;
result.end = totalSize - 1;
}
// 3.2. If start is not provided, set it to the offset of last "end" bytes from the end of the file.
// And set end to the last byte.
// This way we return the last "end" bytes.
if (Number.isNaN(start) && !Number.isNaN(end)) {
logger.debug(`Start is not provided, "end" will be treated as last "end" bytes of the content.`);
result.start = Math.max(totalSize - end, 0);
result.end = totalSize - 1;
}
// 4. Handle invalid ranges.
if (start < 0 || start > end || end > totalSize) {
throw new RangeParserError(start, end);
}
logRange(logger, result);
return result;
}
function logRange(logger: Logger, range: Range) {
logger.debug("Range is: ", JSON.stringify(range));
}

View File

@ -0,0 +1,13 @@
import { Request, Response } from "express";
import { Range } from "./Range";
export const getHeader = (name: string, req: Request) => req.headers[name];
export const getRangeHeader = getHeader.bind(null, "range");
export const setHeader = (name: string, value: string, res: Response) => res.setHeader(name, value);
export const setContentTypeHeader = setHeader.bind(null, "Content-Type");
export const setContentLengthHeader = setHeader.bind(null, "Content-Length");
export const setAcceptRangesHeader = setHeader.bind(null, "Accept-Ranges", "bytes");
export const setContentRangeHeader = (range: Range | null, size: number, res: Response) =>
setHeader("Content-Range", `bytes ${range ? `${range.start}-${range.end}` : "*"}/${size}`, res);
export const setContentDispositionHeader = (fileName: string, res: Response) =>
setHeader("Content-Disposition", `attachment; filename*=utf-8''${encodeURIComponent(fileName)}`, res);
export const setCacheControlHeaderNoCache = setHeader.bind(null, "Cache-Control", "no-cache");

View File

@ -0,0 +1,16 @@
{
"compilerOptions": {
"module": "commonjs",
"esModuleInterop": true,
"target": "es6",
"noImplicitAny": true,
"moduleResolution": "node",
"sourceMap": true,
"outDir": "dist",
"declaration": true,
"baseUrl": "src",
"paths": {
"*": ["../node_modules/*", "./types/*"]
}
}
}

View File

@ -0,0 +1,7 @@
{
"extends": "./tsconfig.base.json",
"include": [
"src/**/*.ts",
"spec/**/*.ts"
]
}

View File

@ -0,0 +1,4 @@
{
"extends": "./tsconfig.base.json",
"include": ["src/index.ts"]
}

File diff suppressed because it is too large Load Diff