Skip to content

Commit

Permalink
🔨 rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
nuintun committed Nov 6, 2017
1 parent d3ef7da commit d561a2c
Show file tree
Hide file tree
Showing 6 changed files with 553 additions and 33 deletions.
23 changes: 9 additions & 14 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ var http = require('http');
var Stream = require('stream');
var Events = require('events');
var mime = require('mime-types');
var ms = require('ms');
var path = require('path');
var ms = require('ms');
var etag = require('etag');
var fresh = require('fresh');
var destroy = require('destroy');
Expand Down Expand Up @@ -73,16 +73,11 @@ function typeIs(value, type) {
* @returns {boolean}
*/
function isOutBound(path$$1, root) {
if (path$$1.length < root.length) {
return true;
}
path$$1 = path.relative(root, path$$1);

if (process.platform === 'win32') {
path$$1 = path$$1.toLowerCase();
root = root.toLowerCase();
}
if (/\.\.(?:[\\/]|$)/.test(path$$1)) return true;

return path$$1.indexOf(root) !== 0;
return false;
}

/**
Expand Down Expand Up @@ -352,15 +347,15 @@ const MAX_MAX_AGE = 60 * 60 * 24 * 365;
* @param {string} charset
*/
function normalizeCharset(charset) {
return typeIs(charset, 'string') ? charset : null;
return charset && typeIs(charset, 'string') ? charset : null;
}

/**
* @function normalizeRoot
* @param {string} root
*/
function normalizeRoot(root) {
return posixURI(path.join(typeIs(root, 'string') ? path.resolve(root) : CWD));
return posixURI(typeIs(root, 'string') ? path.resolve(root) : CWD);
}

/**
Expand Down Expand Up @@ -801,7 +796,7 @@ class FileSend extends Events {
* @method get
*/
get charset() {
this[charset];
return this[charset];
}

/**
Expand Down Expand Up @@ -1026,7 +1021,7 @@ class FileSend extends Events {
*/
pipe(response$$1, options) {
if (this[response]) {
throw new RangeError('The pipe method can only be called once.');
throw new RangeError('The pipe method has been called more than once.');
}

if (!(response$$1 instanceof http.ServerResponse)) {
Expand Down Expand Up @@ -1394,7 +1389,7 @@ class FileSend extends Events {
}

// Cache-Control
if (this.cacheControl && this.maxAge > 0 && !(this.hasHeader('Cache-Control'))) {
if (this.cacheControl && !(this.hasHeader('Cache-Control'))) {
let cacheControl$$1 = `public, max-age=${ this.maxAge }`;

if (this.immutable) {
Expand Down
2 changes: 1 addition & 1 deletion dist/index.min.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ export default class FileSend extends Events {
* @method get
*/
get charset() {
this[symbol.charset];
return this[symbol.charset];
}

/**
Expand Down Expand Up @@ -449,7 +449,7 @@ export default class FileSend extends Events {
*/
pipe(response, options) {
if (this[symbol.response]) {
throw new RangeError('The pipe method can only be called once.');
throw new RangeError('The pipe method has been called more than once.');
}

if (!(response instanceof http.ServerResponse)) {
Expand Down Expand Up @@ -820,7 +820,7 @@ export default class FileSend extends Events {
}

// Cache-Control
if (this.cacheControl && this.maxAge > 0 && !(this.hasHeader('Cache-Control'))) {
if (this.cacheControl && !(this.hasHeader('Cache-Control'))) {
let cacheControl = `public, max-age=${ this.maxAge }`;

if (this.immutable) {
Expand Down
4 changes: 2 additions & 2 deletions lib/normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ const MAX_MAX_AGE = 60 * 60 * 24 * 365;
* @param {string} charset
*/
export function normalizeCharset(charset) {
return utils.typeIs(charset, 'string') ? charset : null;
return charset && utils.typeIs(charset, 'string') ? charset : null;
}

/**
* @function normalizeRoot
* @param {string} root
*/
export function normalizeRoot(root) {
return utils.posixURI(join(utils.typeIs(root, 'string') ? resolve(root) : CWD));
return utils.posixURI(utils.typeIs(root, 'string') ? resolve(root) : CWD);
}

/**
Expand Down
13 changes: 5 additions & 8 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* @version 2017/10/24
*/

import { relative } from 'path';

const toString = Object.prototype.toString;
const CHARS = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');

Expand Down Expand Up @@ -47,16 +49,11 @@ export function typeIs(value, type) {
* @returns {boolean}
*/
export function isOutBound(path, root) {
if (path.length < root.length) {
return true;
}
path = relative(root, path);

if (process.platform === 'win32') {
path = path.toLowerCase();
root = root.toLowerCase();
}
if (/\.\.(?:[\\/]|$)/.test(path)) return true;

return path.indexOf(root) !== 0;
return false;
}

/**
Expand Down
Loading

0 comments on commit d561a2c

Please sign in to comment.