Skip to content

Commit

Permalink
v2.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
andris9 committed Aug 22, 2017
1 parent 44bb64f commit a55c97c
Show file tree
Hide file tree
Showing 12 changed files with 505 additions and 689 deletions.
3 changes: 3 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "nodemailer"
}
56 changes: 0 additions & 56 deletions .eslintrc.js

This file was deleted.

9 changes: 2 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
language: node_js
sudo: false
node_js:
- "0.10"
- 0.12
- iojs
- 4
- 5
before_install:
- npm install -g grunt-cli
- 6
- 8
notifications:
email:
- [email protected]
5 changes: 2 additions & 3 deletions Gruntfile.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
'use strict';

module.exports = function (grunt) {

module.exports = function(grunt) {
// Project configuration.
grunt.initConfig({
eslint: {
all: ['lib/*.js', 'test/*.js', 'Gruntfile.js', '.eslintrc.js']
all: ['lib/*.js', 'test/*.js', 'example/*.js', 'Gruntfile.js']
},

mochaTest: {
Expand Down
7 changes: 2 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
## DEPRECATED

This module is available under the name `fetch` in npm up to v1.x. Higher version numbers indicate another module

# fetch

Fetch url contents. Supports gzipped content for quicker download, redirects (with automatic cookie handling, so no eternal redirect loops), streaming and piping etc.
Expand Down Expand Up @@ -88,7 +84,8 @@ Possible option values
* **agentHttp** pass-through http.request agent parameter for http
* **agent** pass-through http.request agent parameter as fallback, if agentHttps or agentHttp are not specified
* **rejectUnauthorized** whether to reject self-signed certificates (`true`, default behavior), or ignore and allow them (`false`)

* **user** is the username for Basic auth
* **pass** is the password for Basic auth

## Meta object

Expand Down
16 changes: 9 additions & 7 deletions example/fetchstream.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
/* eslint no-console:0 */

'use strict';

var FetchStream = require('../lib/fetch').FetchStream;
const FetchStream = require('../lib/fetch').FetchStream;

var fetch = new FetchStream('http://google.com', {
let fetch = new FetchStream('http://google.com', {
headers: {}
});

fetch.on('data', function (chunk) {
fetch.on('data', chunk => {
console.log(chunk);
});

fetch.on('meta', function (meta) {
fetch.on('meta', meta => {
console.log(meta);
});

fetch.on('end', function () {
fetch.on('end', () => {
console.log('END');
});

fetch.on('error', function (e) {
console.log('ERROR: ' + (e && e.message || e));
fetch.on('error', e => {
console.log('ERROR: ' + ((e && e.message) || e));
});
9 changes: 5 additions & 4 deletions example/fetchurl.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
/* eslint no-console:0 */

'use strict';

// fetch url and update charset to utf-8
var fetchUrl = require('../lib/fetch').fetchUrl;

const fetchUrl = require('../lib/fetch').fetchUrl;

fetchUrl('http://kreata.ee/iso-8859-15.php', function(error, meta, body){
if(error){
fetchUrl('http://kreata.ee/iso-8859-15.php', (error, meta, body) => {
if (error) {
return console.log('ERROR', error.message || error);
}

Expand Down
13 changes: 7 additions & 6 deletions example/pipe.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
/* eslint no-console:0 */

'use strict';

// pipe to file

var FetchStream = require('../lib/fetch').FetchStream,
fs = require('fs'),
inp, out;
const FetchStream = require('../lib/fetch').FetchStream;
const fs = require('fs');

inp = new FetchStream('http://google.com');
out = fs.createWriteStream('google.html');
const inp = new FetchStream('http://google.com');
const out = fs.createWriteStream('google.html');

inp.on('end', function(){
inp.on('end', () => {
console.log('downloaded!');
});

Expand Down
198 changes: 14 additions & 184 deletions lib/cookiejar.js
Original file line number Diff line number Diff line change
@@ -1,193 +1,23 @@
'use strict';

var urllib = require('url');
const Biskviit = require('biskviit');

module.exports.CookieJar = CookieJar;

function CookieJar(options) {
this.options = options || {};
this.options.sessionTimeout = this.options.sessionTimeout || 1800; // 30min

this.cookies = {};
}

CookieJar.prototype.addCookie = function (cookie) {

if (!cookie || !cookie.name) {
return;
// Thin layer around biskviit to keep API compatibility
class CookieJar {
constructor(options) {
this.options = options || {};
this.biskviit = new Biskviit({
sessionTimeout: this.options.sessionTimeout || 1800 // expire cookies after 30 minutes by default
});
}

var lcookie;

if (!this.cookies[cookie.name]) {
this.cookies[cookie.name] = [];
}

// overwrite if has same params
for (var i = 0, len = this.cookies[cookie.name].length; i < len; i++) {
lcookie = this.cookies[cookie.name][i];
if (
lcookie.path === cookie.path &&
lcookie.domain === cookie.domain &&
lcookie.secure === cookie.secure &&
lcookie.httponly === cookie.httponly
) {
this.cookies[cookie.name][i] = cookie;
return;
}
getCookies(url) {
return this.biskviit.get(url);
}

this.cookies[cookie.name].push(cookie);
};

CookieJar.prototype.getCookies = function (url) {
var keys = Object.keys(this.cookies),
cookie, cookies = [];

for (var i = 0, len = keys.length; i < len; i++) {
if (Array.isArray(this.cookies[keys[i]])) {
for (var j = 0, lenj = this.cookies[keys[i]].length; j < lenj; j++) {
cookie = this.cookies[keys[i]][j];
if (this.matchCookie(cookie, url)) {
cookies.push(cookie.name + '=' + cookie.value);
}
}
}
}
return cookies.join('; ');
};

CookieJar.prototype.matchCookie = function (cookie, url) {
var urlparts = urllib.parse(url || '', false, true),
path;

// check expire
if (cookie.expire) {
if (cookie.expire.getTime() < Date.now()) {
return;
}
}

// check if hostname matches
if (urlparts.hostname && cookie._domain) {
if (!(urlparts.hostname === cookie._domain || urlparts.hostname.substr(-(cookie._domain.length + 1)) === '.' + cookie._domain)) {
return false;
}
}

// check if path matches
if (cookie.path && urlparts.pathname) {

path = (urlparts.pathname || '/').split('/');
path.pop();
path = path.join('/').trim();
if (path.substr(0, 1) !== '/') {
path = '/' + path;
}
if (path.substr(-1) !== '/') {
path += '/';
}

if (path.substr(0, cookie.path.length) !== cookie.path) {
return false;
}
}

// check secure
if (cookie.secure && urlparts.protocol) {
if (urlparts.protocol !== 'https:') {
return false;
}
}

// check httponly
if (cookie.httponly && urlparts.protocol) {
if (urlparts.protocol !== 'http:') {
return false;
}
}

return true;
};

CookieJar.prototype.setCookie = function (cookie_str, url) {
var parts = (cookie_str || '').split(';'),
cookie = {},
urlparts = urllib.parse(url || '', false, true),
path;

parts.forEach((function (part) {
var key, val;
part = part.split('=');
key = part.shift().trim();
val = part.join('=').trim();

if (!key) {
return;
}

switch (key.toLowerCase()) {

case 'expires':

cookie.expires = new Date(val);
break;

case 'path':
cookie.path = val.trim();
break;

case 'domain':
cookie.domain = val.toLowerCase();
break;

case 'max-age':
cookie.expires = new Date(Date.now() + (Number(val) || 0) * 1000);
break;

case 'secure':
cookie.secure = true;
break;

case 'httponly':
cookie.httponly = true;
break;

default:
if (!cookie.name) {
cookie.name = key;
cookie.value = val;
}
}
}).bind(this));

// use current path when path is not specified
if (!cookie.path) {
path = (urlparts.pathname || '/').split('/');
path.pop();
cookie.path = path.join('/').trim();
if (cookie.path.substr(0, 1) !== '/') {
cookie.path = '/' + cookie.path;
}
if (cookie.path.substr(-1) !== '/') {
cookie.path += '/';
}
}

// if no expire date, then use sessionTimeout value
if (!cookie.expires) {
cookie._expires = new Date(Date.now() + (Number(this.options.sessionTimeout) || 0) * 1000);
} else {
cookie._expires = cookie.expires;
}

if (!cookie.domain) {
if (urlparts.hostname) {
cookie._domain = urlparts.hostname;
}
} else {
cookie._domain = cookie.domain;
setCookie(cookieStr, url) {
this.biskviit.set(cookieStr, url);
}
}

this.addCookie(cookie);
};
module.exports.CookieJar = CookieJar;
Loading

0 comments on commit a55c97c

Please sign in to comment.