mirror of
https://github.com/TriliumNext/Notes.git
synced 2025-07-27 10:02:59 +08:00
chore(express-partial-content): integrate more files
This commit is contained in:
parent
abede6239d
commit
d6736821b6
@ -1,9 +0,0 @@
|
|||||||
{
|
|
||||||
"parser": "@typescript-eslint/parser",
|
|
||||||
"plugins": ["@typescript-eslint"],
|
|
||||||
"extends": ["plugin:@typescript-eslint/recommended"],
|
|
||||||
"rules": {
|
|
||||||
"@typescript-eslint/indent": ["error", 2],
|
|
||||||
"@typescript-eslint/no-explicit-any": "off"
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,38 +0,0 @@
|
|||||||
---
|
|
||||||
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.
|
|
@ -1,20 +0,0 @@
|
|||||||
---
|
|
||||||
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.
|
|
@ -1,26 +0,0 @@
|
|||||||
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}}
|
|
@ -1,7 +0,0 @@
|
|||||||
coverage
|
|
||||||
.nyc_output
|
|
||||||
*.log
|
|
||||||
.env
|
|
||||||
node_modules
|
|
||||||
dist
|
|
||||||
.yarn
|
|
@ -1,11 +0,0 @@
|
|||||||
coverage
|
|
||||||
.nyc_output
|
|
||||||
node_modules
|
|
||||||
.travis.yml
|
|
||||||
yarn.lock
|
|
||||||
.vscode
|
|
||||||
tsconfig.*
|
|
||||||
.prettierrc.json
|
|
||||||
_config.yml
|
|
||||||
src
|
|
||||||
*.log
|
|
@ -1,5 +0,0 @@
|
|||||||
{
|
|
||||||
"printWidth": 120,
|
|
||||||
"semi": true,
|
|
||||||
"bracketSpacing": true
|
|
||||||
}
|
|
@ -1,6 +0,0 @@
|
|||||||
language: node_js
|
|
||||||
|
|
||||||
node_js:
|
|
||||||
- "8"
|
|
||||||
|
|
||||||
cache: yarn
|
|
@ -1,4 +0,0 @@
|
|||||||
{
|
|
||||||
"cSpell.words": ["Gujar", "Sukant", "copyfiles", "sukantgujar"],
|
|
||||||
"eslint.validate": ["javascript", "typescript"]
|
|
||||||
}
|
|
@ -1,76 +0,0 @@
|
|||||||
# 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
|
|
@ -1,8 +0,0 @@
|
|||||||
# 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.
|
|
@ -1,149 +0,0 @@
|
|||||||

|
|
||||||
|
|
||||||
# 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.
|
|
@ -1 +0,0 @@
|
|||||||
theme: jekyll-theme-slate
|
|
@ -1,52 +0,0 @@
|
|||||||
{
|
|
||||||
"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"
|
|
||||||
}
|
|
@ -1,16 +0,0 @@
|
|||||||
{
|
|
||||||
"compilerOptions": {
|
|
||||||
"module": "commonjs",
|
|
||||||
"esModuleInterop": true,
|
|
||||||
"target": "es6",
|
|
||||||
"noImplicitAny": true,
|
|
||||||
"moduleResolution": "node",
|
|
||||||
"sourceMap": true,
|
|
||||||
"outDir": "dist",
|
|
||||||
"declaration": true,
|
|
||||||
"baseUrl": "src",
|
|
||||||
"paths": {
|
|
||||||
"*": ["../node_modules/*", "./types/*"]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,7 +0,0 @@
|
|||||||
{
|
|
||||||
"extends": "./tsconfig.base.json",
|
|
||||||
"include": [
|
|
||||||
"src/**/*.ts",
|
|
||||||
"spec/**/*.ts"
|
|
||||||
]
|
|
||||||
}
|
|
@ -1,4 +0,0 @@
|
|||||||
{
|
|
||||||
"extends": "./tsconfig.base.json",
|
|
||||||
"include": ["src/index.ts"]
|
|
||||||
}
|
|
File diff suppressed because it is too large
Load Diff
@ -18,6 +18,9 @@
|
|||||||
"eslint.config.mjs"
|
"eslint.config.mjs"
|
||||||
],
|
],
|
||||||
"references": [
|
"references": [
|
||||||
|
{
|
||||||
|
"path": "../server/tsconfig.app.json"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"path": "../desktop/tsconfig.app.json"
|
"path": "../desktop/tsconfig.app.json"
|
||||||
},
|
},
|
||||||
|
@ -3,6 +3,9 @@
|
|||||||
"files": [],
|
"files": [],
|
||||||
"include": [],
|
"include": [],
|
||||||
"references": [
|
"references": [
|
||||||
|
{
|
||||||
|
"path": "../server"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"path": "../desktop"
|
"path": "../desktop"
|
||||||
},
|
},
|
||||||
|
@ -1,7 +1,149 @@
|
|||||||
# express-partial-content
|

|
||||||
|
|
||||||
This library was generated with [Nx](https://nx.dev).
|
# About
|
||||||
|
|
||||||
## Building
|
A HTTP 206 Partial Content handler to serve any readable stream partially in Express.
|
||||||
|
|
||||||
Run `nx build express-partial-content` to build the library.
|
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.
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
{
|
{
|
||||||
"name": "@triliumnext/express-partial-content",
|
"name": "@triliumnext/express-partial-content",
|
||||||
"version": "0.0.1",
|
"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.",
|
||||||
|
"license": "MIT",
|
||||||
|
"version": "1.1.0",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"private": true,
|
"private": true,
|
||||||
"main": "./dist/index.js",
|
"main": "./dist/index.js",
|
||||||
@ -15,6 +17,12 @@
|
|||||||
"default": "./dist/index.js"
|
"default": "./dist/index.js"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"keywords": [
|
||||||
|
"partial-content",
|
||||||
|
"206",
|
||||||
|
"stream",
|
||||||
|
"typescript"
|
||||||
|
],
|
||||||
"nx": {
|
"nx": {
|
||||||
"name": "express-partial-content",
|
"name": "express-partial-content",
|
||||||
"targets": {
|
"targets": {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user