
Security News
Browserslist-rs Gets Major Refactor, Cutting Binary Size by Over 1MB
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
express-partial-content
Advanced tools
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.
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.
yarn add express-partial-content
OR
npm install express-partial-content
Note:
Express
package is a peer dependency forexpress-partial-content
and must be present in dependencies of the host package.
From the express-file-server
example:
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
};
};
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!");
});
Run your server and use a multi-part/multi-connection download utility like aria2c to test it:
aria -x5 -k1M http://localhost:8080/files/readme.txt
There are two examples in the src/examples
folder:
express-file-server
: Implements a file based ContentProvider
.express-mongo-server
: Implements a mongodb based ContentProvider
.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
express-mongo-server
:
Run your own Mongo instance first or create one in https://mlab.com.
Setup the connection string in MongoUrl
environment variable:
export MongoUrl=mongodb+srv://username:password@mongoserver/dbname?retryWrites=true
Build and run the example, the server will listen on http://localhost:8080/.
yarn build:dev
yarn copy-assets
yarn run:examples:file
Browse to https://localhost:8080/files/readme.txt
This is a factory method which generates a partial content handler for express routes.
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.createPartialContentHandler
returns an express handler which can be mapped to an Express route to serve partial content.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.
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.Promise<Content>
: See below.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.
This object contains metadata and methods which describe the content. The ContentProvider
method builds and returns it.
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.getStream(range?: Range)
: This method should return a readable stream initialized to the provided range
(optional). You need to handle two cases:
null
: When range
is not-specified, the client is requesting the full content. In this case, return the stream as it 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.FAQs
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.
The npm package express-partial-content receives a total of 10 weekly downloads. As such, express-partial-content popularity was classified as not popular.
We found that express-partial-content demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
Research
Security News
Eight new malicious Firefox extensions impersonate games, steal OAuth tokens, hijack sessions, and exploit browser permissions to spy on users.
Security News
The official Go SDK for the Model Context Protocol is in development, with a stable, production-ready release expected by August 2025.