node-wget-fetch
Advanced tools
Comparing version 1.0.4 to 1.0.5
{ | ||
"name": "node-wget-fetch", | ||
"version": "1.0.4", | ||
"version": "1.0.5", | ||
"description": "Ultra simple async retrieval of resources or remote files over http or https, an cli tool, and convenience wrapper of node-fetch.", | ||
@@ -5,0 +5,0 @@ "main": "wget-fetch.js", |
@@ -30,3 +30,2 @@ # node-wget-fetch | ||
- '`text`' for **text()** | ||
- '`converted`' for **textConverted()** | ||
- '`stream`' for **NodeJs.readableStream()** | ||
@@ -47,3 +46,2 @@ | ||
- '`array`' = 'application/octet' | ||
- '`converted`' = 'application/x-www-form-urlencoded' | ||
@@ -100,3 +98,2 @@ ## Convenience Request Methods | ||
// 'text' for text() | ||
// 'converted' for textConverted() | ||
// 'stream' for NodeJs.readableStream() | ||
@@ -103,0 +100,0 @@ // default is 'download' |
'use strict'; | ||
const fs = require('fs'), | ||
fetch = require('node-fetch'); | ||
fetch = require('node-fetch'); | ||
let content_types = {}; | ||
content_types['json'] = 'application/json; charset=utf-8'; | ||
content_types['text'] = 'application/x-www-form-urlencoded'; | ||
content_types['blob'] = 'application/octet'; | ||
content_types['buffer'] = 'application/octet'; | ||
content_types['header'] = 'text/plain'; | ||
content_types['object'] = 'application/json; charset=utf-8'; | ||
content_types['stream'] = 'application/octet'; | ||
content_types['array'] = 'application/octet'; | ||
content_types['converted'] = 'application/x-www-form-urlencoded'; | ||
let content_types = { | ||
json: 'application/json; charset=utf-8', | ||
text: 'application/x-www-form-urlencoded', | ||
blob: 'application/octet', | ||
buffer: 'application/octet', | ||
header: 'text/plain', | ||
object: 'application/json; charset=utf-8', | ||
stream: 'application/octet', | ||
array: 'application/octet' | ||
} | ||
@@ -31,3 +31,2 @@ /** | ||
* - '`text`' for **text()** | ||
* - '`converted`' for **textConverted()** | ||
* - '`stream`' for **NodeJs.readableStream()** | ||
@@ -40,96 +39,94 @@ * | ||
function fetching(url, action = '', options = {}) { | ||
let src = url, | ||
destination = action || './', | ||
parts = src.split('/'), | ||
file = parts[parts.length - 1]; | ||
let src = url, | ||
destination = action || './', | ||
parts = src.split('/'), | ||
file = parts[parts.length - 1]; | ||
parts = file.split('?'); | ||
file = parts[0]; | ||
parts = file.split('#'); | ||
file = parts[0]; | ||
parts = file.split('?'); | ||
file = parts[0]; | ||
parts = file.split('#'); | ||
file = parts[0]; | ||
if (isString(action) && ['header', 'object', 'array', 'buffer', 'blob', 'json', 'text', 'converted', 'stream'].includes(action)) { | ||
destination = './'; | ||
} else if (isObject(action)) { | ||
options = Object.assign(options, action); | ||
destination = './'; | ||
action = 'stream'; | ||
} else { | ||
action = 'download'; | ||
} | ||
if (isString(action) && ['header', 'object', 'array', 'buffer', 'blob', 'json', 'text', 'stream'].includes(action)) { | ||
destination = './'; | ||
} else if (isObject(action)) { | ||
options = Object.assign(options, action); | ||
destination = './'; | ||
action = 'stream'; | ||
} else { | ||
action = 'download'; | ||
} | ||
if (options.action) { | ||
action = options.action; | ||
delete options.action; | ||
} | ||
if (options.action) { | ||
action = options.action; | ||
delete options.action; | ||
} | ||
if (destination.substr(destination.length - 1, 1) == '/') { | ||
destination = destination + file; | ||
} | ||
if (destination.substr(destination.length - 1, 1) == '/') { | ||
destination = destination + file; | ||
} | ||
if (options.dry) { | ||
return new Promise((resolve) => resolve({ | ||
filepath: destination | ||
})); | ||
} else { | ||
return fetch(src, options) | ||
.then(res => { | ||
if (res.statusText === 'OK') { | ||
switch (action) { | ||
case 'header': | ||
return new Promise((resolve) => resolve(res.headers.raw())); | ||
case 'object': | ||
return new Promise((resolve) => resolve(res)); | ||
case 'array': | ||
return res.arrayBuffer(); | ||
case 'buffer': | ||
return res.buffer(); | ||
case 'blob': | ||
return res.blob(); | ||
case 'json': | ||
return res.json(); | ||
case 'text': | ||
return res.text(); | ||
case 'converted': | ||
return res.textConverted(); | ||
case 'stream': | ||
return new Promise((resolve) => resolve(res.body)); | ||
default: | ||
return new Promise((resolve, reject) => { | ||
const fileSize = Number.isInteger(res.headers.get('content-length') - 0) ? | ||
parseInt(res.headers.get('content-length')) : | ||
0; | ||
let downloadedSize = 0; | ||
const writer = fs.createWriteStream(destination, { | ||
flags: 'w+', | ||
encoding: 'binary' | ||
}); | ||
res.body.pipe(writer); | ||
if (options.dry) { | ||
return new Promise((resolve) => resolve({ | ||
filepath: destination | ||
})); | ||
} else { | ||
return fetch(src, options) | ||
.then(res => { | ||
if (res.statusText === 'OK' || res.ok) { | ||
switch (action) { | ||
case 'header': | ||
return new Promise((resolve) => resolve(res.headers.raw())); | ||
case 'object': | ||
return new Promise((resolve) => resolve(res)); | ||
case 'array': | ||
return res.arrayBuffer(); | ||
case 'buffer': | ||
return res.buffer(); | ||
case 'blob': | ||
return res.blob(); | ||
case 'json': | ||
return res.json(); | ||
case 'text': | ||
return res.text(); | ||
case 'stream': | ||
return new Promise((resolve) => resolve(res.body)); | ||
default: | ||
return new Promise((resolve, reject) => { | ||
const fileSize = Number.isInteger(res.headers.get('content-length') - 0) | ||
? parseInt(res.headers.get('content-length')) | ||
: 0; | ||
let downloadedSize = 0; | ||
const writer = fs.createWriteStream(destination, { | ||
flags: 'w+', | ||
encoding: 'binary' | ||
}); | ||
res.body.pipe(writer); | ||
res.body.on('data', function (chunk) { | ||
downloadedSize += chunk.length; | ||
}); | ||
res.body.on('data', function (chunk) { | ||
downloadedSize += chunk.length; | ||
}); | ||
writer.on('finish', () => { | ||
writer.end(); | ||
let info = { | ||
filepath: destination, | ||
fileSize: downloadedSize, | ||
fileSizeMatch: (fileSize === downloadedSize) | ||
}; | ||
writer.on('finish', () => { | ||
writer.end(); | ||
let info = { | ||
filepath: destination, | ||
fileSize: downloadedSize, | ||
fileSizeMatch: (fileSize === downloadedSize) | ||
}; | ||
info.headers = res.headers.raw(); | ||
return resolve(info); | ||
}); | ||
writer.on('error', reject); | ||
}); | ||
} | ||
} else { | ||
throw ("Fetch to " + src + " failed, with status text: " + res.statusText); | ||
} | ||
}) | ||
.catch(err => { | ||
return new Promise((resolve, reject) => reject(err)); | ||
}); | ||
} | ||
info.headers = res.headers.raw(); | ||
return resolve(info); | ||
}); | ||
writer.on('error', reject); | ||
}); | ||
} | ||
} else { | ||
throw ("Fetch to " + src + " failed, with status text: " + res.statusText); | ||
} | ||
}) | ||
.catch(err => { | ||
return new Promise((resolve, reject) => reject(err)); | ||
}); | ||
} | ||
} | ||
@@ -146,3 +143,3 @@ | ||
function isArray(val) { | ||
return toString.call(val) === '[object Array]'; | ||
return toString.call(val) === '[object Array]'; | ||
} | ||
@@ -157,3 +154,3 @@ | ||
function isUndefined(val) { | ||
return typeof val === 'undefined'; | ||
return typeof val === 'undefined'; | ||
} | ||
@@ -168,4 +165,4 @@ | ||
function isBuffer(val) { | ||
return val !== null && !isUndefined(val) && val.constructor !== null && !isUndefined(val.constructor) && | ||
typeof val.constructor.isBuffer === 'function' && val.constructor.isBuffer(val); | ||
return val !== null && !isUndefined(val) && val.constructor !== null && !isUndefined(val.constructor) && | ||
typeof val.constructor.isBuffer === 'function' && val.constructor.isBuffer(val); | ||
} | ||
@@ -180,3 +177,3 @@ | ||
function isArrayBuffer(val) { | ||
return toString.call(val) === '[object ArrayBuffer]'; | ||
return toString.call(val) === '[object ArrayBuffer]'; | ||
} | ||
@@ -191,3 +188,3 @@ | ||
function isString(val) { | ||
return typeof val === 'string'; | ||
return typeof val === 'string'; | ||
} | ||
@@ -202,3 +199,3 @@ | ||
function isNumber(val) { | ||
return typeof val === 'number'; | ||
return typeof val === 'number'; | ||
} | ||
@@ -213,3 +210,3 @@ | ||
function isObject(val) { | ||
return val !== null && typeof val === 'object'; | ||
return val !== null && typeof val === 'object'; | ||
} | ||
@@ -224,8 +221,8 @@ | ||
function isPlainObject(val) { | ||
if (toString.call(val) !== '[object Object]') { | ||
return false; | ||
} | ||
if (toString.call(val) !== '[object Object]') { | ||
return false; | ||
} | ||
var prototype = Object.getPrototypeOf(val); | ||
return prototype === null || prototype === Object.prototype; | ||
var prototype = Object.getPrototypeOf(val); | ||
return prototype === null || prototype === Object.prototype; | ||
} | ||
@@ -240,3 +237,3 @@ | ||
function isBlob(val) { | ||
return toString.call(val) === '[object Blob]'; | ||
return toString.call(val) === '[object Blob]'; | ||
} | ||
@@ -251,3 +248,3 @@ | ||
function isFunction(val) { | ||
return toString.call(val) === '[object Function]'; | ||
return toString.call(val) === '[object Function]'; | ||
} | ||
@@ -262,3 +259,3 @@ | ||
function isDate(val) { | ||
return toString.call(val) === '[object Date]'; | ||
return toString.call(val) === '[object Date]'; | ||
} | ||
@@ -273,3 +270,3 @@ | ||
function isStream(val) { | ||
return isObject(val) && isFunction(val.pipe); | ||
return isObject(val) && isFunction(val.pipe); | ||
} | ||
@@ -290,3 +287,2 @@ | ||
- 'text' for text() | ||
- 'converted' for textConverted() | ||
- 'stream' for NodeJs.readableStream() | ||
@@ -297,16 +293,16 @@ * @param options optional `Fetch` options. | ||
function verbFuncBody(verb) { | ||
let method = verb.toUpperCase(); | ||
return function (uri, body = null, responseType = 'text', options = {}) { | ||
let params = { | ||
headers: { | ||
'Content-Type': null | ||
} | ||
}; | ||
params.method = method; | ||
params.action = responseType; | ||
params.headers['Content-Type'] = content_types[responseType] || 'application/x-www-form-urlencoded'; | ||
params.body = ((isString(body) && body.includes('=')) || (isObject(body))) ? new URLSearchParams(body) : body; | ||
params = Object.assign(params, options); | ||
return fetching(uri, params); | ||
} | ||
let method = verb.toUpperCase(); | ||
return function (uri, body = null, responseType = 'text', options = {}) { | ||
let params = { | ||
headers: { | ||
'Content-Type': null | ||
} | ||
}; | ||
params.method = method; | ||
params.action = responseType; | ||
params.headers['Content-Type'] = content_types[responseType] || 'application/x-www-form-urlencoded'; | ||
params.body = ((isString(body) && body.includes('=', '&')) || (isObject(body))) ? new URLSearchParams(body) : body; | ||
params = Object.assign(params, options); | ||
return fetching(uri, params); | ||
} | ||
} | ||
@@ -326,3 +322,2 @@ | ||
- 'text' for text() | ||
- 'converted' for textConverted() | ||
- 'stream' for NodeJs.readableStream() | ||
@@ -333,9 +328,9 @@ * @param options optional `Fetch` options. | ||
function verbFunc(verb) { | ||
let method = verb.toUpperCase(); | ||
return function (uri, responseType = 'header', options = {}) { | ||
let params = options; | ||
params.action = responseType; | ||
params.method = method; | ||
return fetching(uri, params); | ||
} | ||
let method = verb.toUpperCase(); | ||
return function (uri, responseType = 'header', options = {}) { | ||
let params = options; | ||
params.action = responseType; | ||
params.method = method; | ||
return fetching(uri, params); | ||
} | ||
} | ||
@@ -354,9 +349,9 @@ | ||
function wget(url, folderFilename = './', options = {}) { | ||
let params = options; | ||
if (isObject(folderFilename)) { | ||
params = Object.assign(params, folderFilename); | ||
folderFilename = './'; | ||
} | ||
params.action = 'download'; | ||
return fetching(url, folderFilename, params); | ||
let params = options; | ||
if (isObject(folderFilename)) { | ||
params = Object.assign(params, folderFilename); | ||
folderFilename = './'; | ||
} | ||
params.action = 'download'; | ||
return fetching(url, folderFilename, params); | ||
} | ||
@@ -388,3 +383,2 @@ | ||
- 'text' for text() | ||
- 'converted' for textConverted() | ||
- 'stream' for NodeJs.readableStream() | ||
@@ -408,3 +402,2 @@ * @param options optional `Fetch` options. | ||
- 'text' for text() | ||
- 'converted' for textConverted() | ||
- 'stream' for NodeJs.readableStream() | ||
@@ -428,3 +421,2 @@ * @param options optional `Fetch` options. | ||
- 'text' for text() | ||
- 'converted' for textConverted() | ||
- 'stream' for NodeJs.readableStream() | ||
@@ -450,3 +442,2 @@ * @param options optional `Fetch` options. | ||
- 'text' for text() | ||
- 'converted' for textConverted() | ||
- 'stream' for NodeJs.readableStream() | ||
@@ -472,3 +463,2 @@ * @param options optional `Fetch` options. | ||
- 'text' for text() | ||
- 'converted' for textConverted() | ||
- 'stream' for NodeJs.readableStream() | ||
@@ -494,3 +484,2 @@ * @param options optional `Fetch` options. | ||
- 'text' for text() | ||
- 'converted' for textConverted() | ||
- 'stream' for NodeJs.readableStream() | ||
@@ -516,3 +505,2 @@ * @param options optional `Fetch` options. | ||
- 'text' for text() | ||
- 'converted' for textConverted() | ||
- 'stream' for NodeJs.readableStream() | ||
@@ -538,3 +526,2 @@ * @param options optional `Fetch` options. | ||
- 'text' for text() | ||
- 'converted' for textConverted() | ||
- 'stream' for NodeJs.readableStream() | ||
@@ -557,3 +544,3 @@ * @param options optional `Fetch` options. | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
value: true | ||
}); | ||
@@ -560,0 +547,0 @@ |
28894
579
181