Skip to content

Commit

Permalink
Rewrite and add tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
jjrv committed Jul 10, 2017
1 parent 45d290f commit 19a7b01
Show file tree
Hide file tree
Showing 15 changed files with 758 additions and 331 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
"name": "cget",
"version": "0.1.0",
"description": "Robust streaming parallel download manager with filesystem cache",
"main": "dist/cget.js",
"typings": "dist/cget.d.ts",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"scripts": {
"tsc": "tsc",
"prepublish": "tsc -p src",
Expand Down
54 changes: 23 additions & 31 deletions src/Address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,6 @@
import * as path from 'path';
import * as url from 'url';

export function sanitizeUrl(urlRemote: string) {
var urlParts = url.parse(urlRemote, false, true);
var origin = urlParts.host || '';

if((urlParts.pathname || '').charAt(0) != '/') origin += '/';

origin += urlParts.pathname;
return([
urlParts.protocol || 'http:',
'//',
url.resolve('', origin),
urlParts.search || ''
].join(''));
}

/** Last line of defence to filter malicious paths. */

export function sanitizePath(path: string) {
Expand All @@ -36,32 +21,39 @@ export function sanitizePath(path: string) {

export class Address {
constructor(uri: string, cwd?: string) {
var urn: string | null = null;
var url: string | null = null;
var cachePath: string;

if(uri.match(/^\.?\.?\//)) {
// The URI looks more like a local path.
cachePath = path.resolve(cwd || '.', uri);
url = 'file://' + cachePath;
this.path = path.resolve(cwd || '.', uri);
this.url = 'file://' + this.path;
this.isLocal = true;
} else if(uri.substr(0, 5) == 'file:') {
cachePath = path.resolve(uri.substr(5));
url = 'file://' + cachePath;
this.path = path.resolve(uri.substr(5));
this.url = 'file://' + this.path;
this.isLocal = true;
} else if(uri.substr(0, 4) == 'urn:') {
urn = uri;
cachePath = urn.substr(4).replace(/:/g, '/');
this.urn = uri;
this.path = sanitizePath(this.urn.substr(4).replace(/:/g, '/'));
} else {
// If the URI is not a URN address, interpret it as a URL address and clean it up.
url = sanitizeUrl(uri);
cachePath = uri.substr(uri.indexOf(':') + 1);

const parts = url.parse(uri, false, true);
const origin = parts.host || '';

const slash = ((parts.pathname || '').charAt(0) == '/') ? '' : '/';

this.url = (
(parts.protocol || 'http:') + '//' +
url.resolve('', origin + slash + parts.pathname) +
(parts.search || '')
);

this.path = sanitizePath(
url.resolve('', origin.replace(/:.*/, '') + slash + parts.pathname) +
(parts.search || '')
);
}

this.uri = (urn || url)!;
this.urn = urn;
this.url = url;
this.path = this.isLocal ? cachePath : sanitizePath(cachePath);
this.uri = (this.urn || this.url)!;
}

uri: string;
Expand Down
Loading

0 comments on commit 19a7b01

Please sign in to comment.