Skip to content

Commit

Permalink
🔨 rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
nuintun committed Nov 3, 2017
1 parent f4ebe8a commit 74fc636
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 27 deletions.
43 changes: 27 additions & 16 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,15 @@ function typeIs(value, type) {
* @returns {boolean}
*/
function isOutBound(path$$1, root) {
if (path$$1.length < root.length) {
return true;
}

if (process.platform === 'win32') {
path$$1 = path$$1.toLowerCase();
root = root.toLowerCase();
}

if (path$$1.length < root.length) {
return true;
}

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

Expand Down Expand Up @@ -313,6 +313,7 @@ const maxAge = Symbol('maxAge');
const charset = Symbol('charset');
const request = Symbol('request');
const isFresh = Symbol('isFresh');
const realpath = Symbol('realpath');
const response = Symbol('response');
const isIgnore = Symbol('isIgnore');
const sendFile = Symbol('sendFile');
Expand Down Expand Up @@ -659,7 +660,7 @@ class FileSend extends Events {
const response$$1 = this[response];

if (!response$$1) {
throw new Error('Can\'t get http response before called pipe method.');
throw new ReferenceError('Can\'t get http response before called pipe method.');
}

return response$$1;
Expand All @@ -678,7 +679,12 @@ class FileSend extends Events {
* @method set
*/
set path(path$$1) {
this[path$1] = normalizePath(path$$1);
const root$$1 = this.root;

path$$1 = normalizePath(path$$1);

this[path$1] = path$$1;
this[realpath] = root$$1 ? normalizeRealpath(root$$1, path$$1) : path$$1;
}

/**
Expand All @@ -694,7 +700,12 @@ class FileSend extends Events {
* @method set
*/
set root(root$$1) {
this[root] = normalizeRoot(root$$1);
const path$$1 = this.path;

root$$1 = normalizeRoot(root$$1);

this[root] = root$$1;
this[realpath] = path$$1 ? normalizeRealpath(root$$1, path$$1) : root$$1;
}

/**
Expand All @@ -710,7 +721,7 @@ class FileSend extends Events {
* @method get
*/
get realpath() {
return normalizeRealpath(this.root, this.path);
return this[realpath];
}

/**
Expand Down Expand Up @@ -1015,11 +1026,11 @@ class FileSend extends Events {
*/
pipe(response$$1, options) {
if (this[response]) {
throw new TypeError('There already have a http response alive.');
throw new RangeError('The pipe method can only be called once.');
}

if (!(response$$1 instanceof http.ServerResponse)) {
throw new TypeError('The param response must be a http response.');
throw new TypeError('The response must be a http response.');
}

// Set response
Expand Down Expand Up @@ -1462,7 +1473,7 @@ class FileSend extends Events {
* @private
*/
[sendFile](ranges) {
const realpath = this.realpath;
const realpath$$1 = this.realpath;
const stdin$$1 = this[stdin];

// Iterator ranges
Expand All @@ -1471,7 +1482,7 @@ class FileSend extends Events {
range.open && stdin$$1.write(range.open);

// Create file stream
const file = fs.createReadStream(realpath, range);
const file = fs.createReadStream(realpath$$1, range);

// Error handling code-smell
file.on('error', (error$$1) => {
Expand Down Expand Up @@ -1511,7 +1522,7 @@ class FileSend extends Events {
*/
[bootstrap]() {
const response$$1 = this.response;
const realpath = this.realpath;
const realpath$$1 = this.realpath;

// Set status
this.status(response$$1.statusCode || 200);
Expand All @@ -1522,7 +1533,7 @@ class FileSend extends Events {
}

// Malicious path
if (isOutBound(realpath, this.root)) {
if (isOutBound(realpath$$1, this.root)) {
return this[error](403);
}

Expand All @@ -1537,7 +1548,7 @@ class FileSend extends Events {
}

// Read file
fs.stat(realpath, (error$$1, stats) => {
fs.stat(realpath$$1, (error$$1, stats) => {
// Stat error
if (error$$1) {
return this[statError](error$$1);
Expand Down Expand Up @@ -1597,7 +1608,7 @@ class FileSend extends Events {
} else {
// Emit file event
if (this.hasListeners('file')) {
this.emit('file', realpath, stats);
this.emit('file', realpath$$1, stats);
}

// Read file
Expand Down
2 changes: 1 addition & 1 deletion dist/index.min.js

Large diffs are not rendered by default.

22 changes: 16 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export default class FileSend extends Events {
const response = this[symbol.response];

if (!response) {
throw new Error('Can\'t get http response before called pipe method.');
throw new ReferenceError('Can\'t get http response before called pipe method.');
}

return response;
Expand All @@ -102,7 +102,12 @@ export default class FileSend extends Events {
* @method set
*/
set path(path) {
this[symbol.path] = normalize.normalizePath(path);
const root = this.root;

path = normalize.normalizePath(path);

this[symbol.path] = path;
this[symbol.realpath] = root ? normalize.normalizeRealpath(root, path) : path;
}

/**
Expand All @@ -118,7 +123,12 @@ export default class FileSend extends Events {
* @method set
*/
set root(root) {
this[symbol.root] = normalize.normalizeRoot(root);
const path = this.path;

root = normalize.normalizeRoot(root);

this[symbol.root] = root;
this[symbol.realpath] = path ? normalize.normalizeRealpath(root, path) : root;
}

/**
Expand All @@ -134,7 +144,7 @@ export default class FileSend extends Events {
* @method get
*/
get realpath() {
return normalize.normalizeRealpath(this.root, this.path);
return this[symbol.realpath];
}

/**
Expand Down Expand Up @@ -439,11 +449,11 @@ export default class FileSend extends Events {
*/
pipe(response, options) {
if (this[symbol.response]) {
throw new TypeError('There already have a http response alive.');
throw new RangeError('The pipe method can only be called once.');
}

if (!(response instanceof http.ServerResponse)) {
throw new TypeError('The param response must be a http response.');
throw new TypeError('The response must be a http response.');
}

// Set response
Expand Down
1 change: 1 addition & 0 deletions lib/symbol.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export const maxAge = Symbol('maxAge');
export const charset = Symbol('charset');
export const request = Symbol('request');
export const isFresh = Symbol('isFresh');
export const realpath = Symbol('realpath');
export const response = Symbol('response');
export const isIgnore = Symbol('isIgnore');
export const sendFile = Symbol('sendFile');
Expand Down
8 changes: 4 additions & 4 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ export function typeIs(value, type) {
* @returns {boolean}
*/
export function isOutBound(path, root) {
if (path.length < root.length) {
return true;
}

if (process.platform === 'win32') {
path = path.toLowerCase();
root = root.toLowerCase();
}

if (path.length < root.length) {
return true;
}

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

Expand Down

0 comments on commit 74fc636

Please sign in to comment.