Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

js-file-downloader

Package Overview
Dependencies
Maintainers
1
Versions
30
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

js-file-downloader - npm Package Compare versions

Comparing version 1.1.22 to 1.1.23

12

dist/js-file-downloader.js

@@ -172,3 +172,3 @@ (function webpackUniversalModuleDefinition(root, factory) {

/*!
* JS File Downloader v 1.1.21
* JS File Downloader v 1.1.23
* https://github.com/AleeeKoi/js-file-downloader

@@ -233,8 +233,8 @@ *

return new Promise(function (resolve, reject) {
_this.initDonwload(resolve, reject);
_this.initDownload(resolve, reject);
});
}
}, {
key: "initDonwload",
value: function initDonwload(resolve, reject) {
key: "initDownload",
value: function initDownload(resolve, reject) {
var _this2 = this;

@@ -311,2 +311,6 @@

if (this.params.onloadstart && typeof this.params.onloadstart === 'function') {
request.onloadstart = this.params.onloadstart;
}
request.timeout = this.params.timeout;

@@ -313,0 +317,0 @@ request.withCredentials = !!this.params.withCredentials || !!this.params.includeCredentials;

!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.jsFileDownloader=t():e.jsFileDownloader=t()}(this,(function(){return function(e){var t={};function n(r){if(t[r])return t[r].exports;var o=t[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}return n.m=e,n.c=t,n.d=function(e,t,r){n.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:r})},n.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},n.t=function(e,t){if(1&t&&(e=n(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var o in e)n.d(r,o,function(t){return e[t]}.bind(null,o));return r},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,"a",t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p="",n(n.s=0)}([function(e,t,n){"use strict";function r(e){return(r="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}function o(e,t){if(t&&("object"===r(t)||"function"==typeof t))return t;if(void 0!==t)throw new TypeError("Derived constructors may only return object or undefined");return function(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}(e)}function i(e){var t="function"==typeof Map?new Map:void 0;return(i=function(e){if(null===e||(n=e,-1===Function.toString.call(n).indexOf("[native code]")))return e;var n;if("function"!=typeof e)throw new TypeError("Super expression must either be null or a function");if(void 0!==t){if(t.has(e))return t.get(e);t.set(e,r)}function r(){return a(e,arguments,c(this).constructor)}return r.prototype=Object.create(e.prototype,{constructor:{value:r,enumerable:!1,writable:!0,configurable:!0}}),s(r,e)})(e)}function a(e,t,n){return(a=u()?Reflect.construct:function(e,t,n){var r=[null];r.push.apply(r,t);var o=new(Function.bind.apply(e,r));return n&&s(o,n.prototype),o}).apply(null,arguments)}function u(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){}))),!0}catch(e){return!1}}function s(e,t){return(s=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e})(e,t)}function c(e){return(c=Object.setPrototypeOf?Object.getPrototypeOf:function(e){return e.__proto__||Object.getPrototypeOf(e)})(e)}n.r(t);var l=function(e){!function(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function");e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,writable:!0,configurable:!0}}),t&&s(e,t)}(i,e);var t,n,r=(t=i,n=u(),function(){var e,r=c(t);if(n){var i=c(this).constructor;e=Reflect.construct(r,arguments,i)}else e=r.apply(this,arguments);return o(this,e)});function i(e,t){var n;return function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,i),(n=r.call(this,"Downloader error: ".concat(e))).request=t,n.name="DownloadException",n}return i}(i(Error));
/*!
* JS File Downloader v 1.1.21
* JS File Downloader v 1.1.23
* https://github.com/AleeeKoi/js-file-downloader

@@ -10,3 +10,3 @@ *

*/
function f(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function p(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}var d={timeout:4e4,headers:[],forceDesktopMode:!1,autoStart:!0,withCredentials:!1,method:"GET",nameCallback:function(e){return e},contentType:"application/x-www-form-urlencoded",body:null,nativeFallbackOnError:!1},h=function(){function e(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};return f(this,e),this.params=Object.assign({},d,t),this.link=this.createLink(),this.request=null,this.params.autoStart?this.downloadFile():this}var t,n,r;return t=e,(n=[{key:"start",value:function(){return this.downloadFile()}},{key:"downloadFile",value:function(){var e=this;return new Promise((function(t,n){e.initDonwload(t,n)}))}},{key:"initDonwload",value:function(e,t){var n=this,r=function(){n.link.target="_blank",n.link.href=n.params.url,n.clickLink()};return!("download"in this.link)||this.isMobile()?(r(),e()):(this.request=this.createRequest(),this.params.url?(this.request.onload=function(){return 200!==parseInt(n.request.status,10)?t(new l("status code [".concat(n.request.status,"]"),n.request)):(n.startDownload(),e(n))},this.request.ontimeout=function(){t(new l("request timeout",n.request))},this.request.onerror=function(){n.params.nativeFallbackOnError?(r(),e(n)):t(new l("network error",n.request))},this.request.send(this.params.body),this):t(new l("url param not defined!",this.request)))}},{key:"isMobile",value:function(){return!this.params.forceDesktopMode&&/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)}},{key:"createRequest",value:function(){var e=new XMLHttpRequest;return e.open(this.params.method,this.params.url,!0),!1!==this.params.contentType&&e.setRequestHeader("Content-type",this.params.contentType),this.params.headers.forEach((function(t){e.setRequestHeader(t.name,t.value)})),e.responseType="arraybuffer",this.params.process&&"function"==typeof this.params.process&&e.addEventListener("progress",this.params.process),e.timeout=this.params.timeout,e.withCredentials=!!this.params.withCredentials||!!this.params.includeCredentials,e}},{key:"getFileName",value:function(){if("string"==typeof this.params.filename)return this.params.filename;var e=this.request.getResponseHeader("Content-Disposition"),t=[];e&&(t=e.replace(/["']/g,"").match(/filename\*?=([^;]+)/));var n=t&&t.length>=1?t[1]:this.params.url.split("/").pop().split("?")[0];return this.params.nameCallback(n)}},{key:"createLink",value:function(){var e=document.createElement("a");return e.style.display="none",e}},{key:"clickLink",value:function(){var e;try{e=new MouseEvent("click")}catch(t){(e=document.createEvent("MouseEvent")).initMouseEvent("click",!0,!0,window,0,0,0,0,0,!1,!1,!1,!1,0,null)}this.link.dispatchEvent(e)}},{key:"getFile",value:function(e,t){var n=null,r={type:"application/octet-stream"};try{n=new File([e],t,r)}catch(o){(n=new Blob([e],r)).name=t,n.lastModifiedDate=new Date}return n}},{key:"startDownload",value:function(){var e=this.getFileName(),t=this.getFile(this.request.response,e);if("msSaveOrOpenBlob"in window.navigator)return window.navigator.msSaveOrOpenBlob(t,e);var n=window.URL.createObjectURL(t);return this.link.href=n,this.link.download=e,this.clickLink(),setTimeout((function(){(window.URL||window.webkitURL||window).revokeObjectURL(n)}),4e4),this}}])&&p(t.prototype,n),r&&p(t,r),e}();t.default=h}]).default}));
function f(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function p(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}var d={timeout:4e4,headers:[],forceDesktopMode:!1,autoStart:!0,withCredentials:!1,method:"GET",nameCallback:function(e){return e},contentType:"application/x-www-form-urlencoded",body:null,nativeFallbackOnError:!1},h=function(){function e(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};return f(this,e),this.params=Object.assign({},d,t),this.link=this.createLink(),this.request=null,this.params.autoStart?this.downloadFile():this}var t,n,r;return t=e,(n=[{key:"start",value:function(){return this.downloadFile()}},{key:"downloadFile",value:function(){var e=this;return new Promise((function(t,n){e.initDownload(t,n)}))}},{key:"initDownload",value:function(e,t){var n=this,r=function(){n.link.target="_blank",n.link.href=n.params.url,n.clickLink()};return!("download"in this.link)||this.isMobile()?(r(),e()):(this.request=this.createRequest(),this.params.url?(this.request.onload=function(){return 200!==parseInt(n.request.status,10)?t(new l("status code [".concat(n.request.status,"]"),n.request)):(n.startDownload(),e(n))},this.request.ontimeout=function(){t(new l("request timeout",n.request))},this.request.onerror=function(){n.params.nativeFallbackOnError?(r(),e(n)):t(new l("network error",n.request))},this.request.send(this.params.body),this):t(new l("url param not defined!",this.request)))}},{key:"isMobile",value:function(){return!this.params.forceDesktopMode&&/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)}},{key:"createRequest",value:function(){var e=new XMLHttpRequest;return e.open(this.params.method,this.params.url,!0),!1!==this.params.contentType&&e.setRequestHeader("Content-type",this.params.contentType),this.params.headers.forEach((function(t){e.setRequestHeader(t.name,t.value)})),e.responseType="arraybuffer",this.params.process&&"function"==typeof this.params.process&&e.addEventListener("progress",this.params.process),this.params.onloadstart&&"function"==typeof this.params.onloadstart&&(e.onloadstart=this.params.onloadstart),e.timeout=this.params.timeout,e.withCredentials=!!this.params.withCredentials||!!this.params.includeCredentials,e}},{key:"getFileName",value:function(){if("string"==typeof this.params.filename)return this.params.filename;var e=this.request.getResponseHeader("Content-Disposition"),t=[];e&&(t=e.replace(/["']/g,"").match(/filename\*?=([^;]+)/));var n=t&&t.length>=1?t[1]:this.params.url.split("/").pop().split("?")[0];return this.params.nameCallback(n)}},{key:"createLink",value:function(){var e=document.createElement("a");return e.style.display="none",e}},{key:"clickLink",value:function(){var e;try{e=new MouseEvent("click")}catch(t){(e=document.createEvent("MouseEvent")).initMouseEvent("click",!0,!0,window,0,0,0,0,0,!1,!1,!1,!1,0,null)}this.link.dispatchEvent(e)}},{key:"getFile",value:function(e,t){var n=null,r={type:"application/octet-stream"};try{n=new File([e],t,r)}catch(o){(n=new Blob([e],r)).name=t,n.lastModifiedDate=new Date}return n}},{key:"startDownload",value:function(){var e=this.getFileName(),t=this.getFile(this.request.response,e);if("msSaveOrOpenBlob"in window.navigator)return window.navigator.msSaveOrOpenBlob(t,e);var n=window.URL.createObjectURL(t);return this.link.href=n,this.link.download=e,this.clickLink(),setTimeout((function(){(window.URL||window.webkitURL||window).revokeObjectURL(n)}),4e4),this}}])&&p(t.prototype,n),r&&p(t,r),e}();t.default=h}]).default}));
//# sourceMappingURL=js-file-downloader.min.js.map

@@ -15,2 +15,3 @@ declare module 'js-file-downloader' {

nativeFallbackOnError?: boolean;
onloadstart?: () => void;
}

@@ -26,3 +27,4 @@

interface JsFileDownloaderContructor {
new (data?: Params): JsFileDownloaderBase;
new (data?: Params & { autoStart: false }): JsFileDownloaderBase;
new (data?: Params): Promise<void>;
}

@@ -29,0 +31,0 @@

{
"name": "js-file-downloader",
"version": "1.1.22",
"version": "1.1.23",
"description": "Small lib for a cool download experience",

@@ -5,0 +5,0 @@ "main": "dist/js-file-downloader.js",

@@ -96,2 +96,18 @@ <p align="center">

#### onloadstart ('loadstart' event listener)
A function to call when a 'loadstart' event is triggered.
```js
function onloadstart () {
// what to do ...
}
new JsFileDownloader({
url: '...',
onloadstart
})
```
#### headers (of request)

@@ -98,0 +114,0 @@ If you need to customize request header data you can pass an array of objects like following example:

@@ -0,0 +0,0 @@ export class DownloadException extends Error {

@@ -50,7 +50,7 @@ /*!

return new Promise((resolve, reject) => {
this.initDonwload(resolve, reject);
this.initDownload(resolve, reject);
});
}
initDonwload (resolve, reject) {
initDownload (resolve, reject) {

@@ -120,2 +120,5 @@ const fallback = () => {

}
if (this.params.onloadstart && typeof this.params.onloadstart === 'function') {
request.onloadstart = this.params.onloadstart;
}
request.timeout = this.params.timeout;

@@ -122,0 +125,0 @@ request.withCredentials = !!this.params.withCredentials || !!this.params.includeCredentials;

import chai from 'chai';
import Downloader from '../dist/js-file-downloader.min';
chai.expect();
const expect = chai.expect;
chai.should();

@@ -117,2 +117,33 @@

describe('Passing onloadstart', () => {
let DownloaderPromise;
let loadingStarted = false;
before(() => {
DownloaderPromise = new Downloader({
url: 'https://cdn.apedesign.net/github/logo.png',
onloadstart () { loadingStarted = true; }
});
});
it('should not be rejected', done => {
DownloaderPromise
.then(DL => {
done();
describe('Checking if loadstart listener has been called', () => {
it('"loadingStarted" flag should be set to true', () => {
expect(loadingStarted).to.equal(true);
});
});
})
.catch(e => { done(e); });
});
});
describe('Rejection test', () => {

@@ -119,0 +150,0 @@

@@ -0,0 +0,0 @@ const path = require('path');

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc