Remove express from deps, move out fCP in separate file.

This commit is contained in:
SukantGujar 2019-03-11 17:34:44 +05:30
parent 33a4b1aea4
commit ca0b97b471
3 changed files with 37 additions and 38 deletions

View File

@ -1,8 +1,8 @@
{ {
"name": "node-partial-stream", "name": "express-partial-content",
"version": "0.1.0", "version": "0.1.0",
"main": "dist/index.js", "main": "dist/index.js",
"repository": "ssh://git@github.com-sukantgujar/SukantGujar/node-partial-stream", "repository": "ssh://git@github.com-sukantgujar/SukantGujar/express-partial-content",
"author": "sukantgujar <sukantgujar@yahoo.com>", "author": "sukantgujar <sukantgujar@yahoo.com>",
"license": "MIT", "license": "MIT",
"scripts": { "scripts": {
@ -19,7 +19,5 @@
"peerDependencies": { "peerDependencies": {
"express": "^4.16.4" "express": "^4.16.4"
}, },
"dependencies": { "dependencies": {}
"express": "^4.16.4"
}
} }

View File

@ -0,0 +1,28 @@
import { Request } from "express";
import fs from "fs";
import { Range, ContentDoesNotExistError, ContentProvider } from "../../index";
import { existsAsync, statAsync, logger } from "./index";
export const fileContentProvider: ContentProvider = async (req: Request) => {
const fileName = req.params.name;
const file = `${__dirname}/files/${fileName}`;
if (!(await existsAsync(file))) {
throw new ContentDoesNotExistError(`File doesn't exists: ${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

@ -1,40 +1,13 @@
import express, { Request } from "express"; import express from "express";
import { promisify } from "util"; import { promisify } from "util";
import fs from "fs"; import fs from "fs";
import { Range, createPartialStreamHandler, ContentDoesNotExistError, ContentProvider } from "../../index"; import { createPartialStreamHandler } from "../../index";
import { fileContentProvider } from "./fileContentProvider";
const statAsync = promisify(fs.stat); export const statAsync = promisify(fs.stat);
const existsAsync = promisify(fs.exists); export const existsAsync = promisify(fs.exists);
const fileContentProvider: ContentProvider = async (req: Request) => { export const logger = {
const fileName = req.params.name;
const file = `${__dirname}/files/${fileName}`;
if (!(await existsAsync(file))) {
throw new ContentDoesNotExistError(`File doesn't exists: ${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
};
};
const logger = {
debug(message: string, extra?: any) { debug(message: string, extra?: any) {
if (extra) { if (extra) {
console.log(`[debug]: ${message}`, extra); console.log(`[debug]: ${message}`, extra);