upload-js
Advanced tools
Comparing version 1.18.0 to 1.19.0
@@ -1,1 +0,1 @@ | ||
module.exports=(()=>{"use strict";var e={675:(e,t,n)=>{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){return!t||"object"!==r(t)&&"function"!=typeof t?i(e):t}function i(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}function a(e){var t="function"==typeof Map?new Map:void 0;return(a=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 u(e,arguments,l(this).constructor)}return r.prototype=Object.create(e.prototype,{constructor:{value:r,enumerable:!1,writable:!0,configurable:!0}}),s(r,e)})(e)}function u(e,t,n){return(u=c()?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 c(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Date.prototype.toString.call(Reflect.construct(Date,[],(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 l(e){return(l=Object.setPrototypeOf?Object.getPrototypeOf:function(e){return e.__proto__||Object.getPrototypeOf(e)})(e)}n.r(t),n.d(t,{Upload:()=>v});var p=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)}(a,e);var t,n,r=(t=a,n=c(),function(){var e,r=l(t);if(n){var i=l(this).constructor;e=Reflect.construct(r,arguments,i)}else e=r.apply(this,arguments);return o(this,e)});function a(e){var t;return function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,a),t=r.call(this,e),Object.setPrototypeOf(i(t),a.prototype),t}return a}(a(Error));const f=require("upload-api-client-upload-js");function d(e,t){(null==t||t>e.length)&&(t=e.length);for(var n=0,r=new Array(t);n<t;n++)r[n]=e[n];return r}function h(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 y=function(e,t,n,r){return new(n||(n=Promise))((function(o,i){function a(e){try{c(r.next(e))}catch(e){i(e)}}function u(e){try{c(r.throw(e))}catch(e){i(e)}}function c(e){var t;e.done?o(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(a,u)}c((r=r.apply(e,t||[])).next())}))},v=function(){function e(t){var n,r,o,i,a,u,c,s;if(function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),this.config=t,this.accountIdLength=7,this.apiKeyPrefix="public_",this.maxUploadConcurrency=5,!0===t.debug&&console.log("[upload-js] Initialized with API key '".concat(t.apiKey,"'")),this.apiUrl=null!==(r=null===(n=t.internal)||void 0===n?void 0:n.apiUrl)&&void 0!==r?r:"https://api.upload.io",this.cdnUrl=null!==(i=null===(o=t.internal)||void 0===o?void 0:o.cdnUrl)&&void 0!==i?i:"https://cdn.upload.io",this.authenticateWithApiKey=null===(u=null===(a=t.internal)||void 0===a?void 0:a.authenticateWithApiKey)||void 0===u||u,this.headers=null===(c=t.internal)||void 0===c?void 0:c.headers,this.debugMode=!0===t.debug,t.apiKey.trim()!==t.apiKey)throw new Error("[upload-js] Invalid API key. Whitespace detected: please remove whitespace from the API key and try again.");if(!1===(null===(s=t.internal)||void 0===s?void 0:s.authenticateWithApiKey))this.accountId=t.internal.accountId;else{if(!t.apiKey.startsWith(this.apiKeyPrefix))throw new Error("[upload-js] Invalid API key. API keys must start with '".concat(this.apiKeyPrefix,"'"));if(this.accountId=t.apiKey.substr(this.apiKeyPrefix.length,this.accountIdLength),this.accountId.length!==this.accountIdLength)throw new Error("[upload-js] Invalid API key. API keys must be at least ".concat(this.apiKeyPrefix.length+this.accountIdLength," characters long, but the API key you provided is ").concat(this.apiKeyPrefix.length+this.accountId.length,"."))}}var t,n;return t=e,(n=[{key:"createFileInputHandler",value:function(e){var t=this;return function(n){var r=n.target;if(void 0===r.files||null===r.files)throw new Error("No property 'files' on input element: ensure 'createFileInputHandler' is set to the 'onchange' attribute on an input of type 'file'.");if(void 0===r.files[0])throw new Error("No file selected.");t.uploadFile(Object.assign(Object.assign({},e),{file:r.files[0]})).then(e.onUploaded,(function(t){void 0!==e.onError?e.onError(t):console.error("Cannot upload file. To remove this console message, handle the error explicitly by providing an 'onError' parameter: upload.createFileInputHandler({onUploaded, onError})",t)}))}}},{key:"uploadFile",value:function(e){return y(this,void 0,void 0,regeneratorRuntime.mark((function t(){var n,r,o;return regeneratorRuntime.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=[],r=function(e){n.push(e)},o=function(){return n.forEach((function(e){return e()}))},void 0!==e.onBegin&&e.onBegin({cancel:o}),t.prev=4,t.next=7,this.beginFileUpload(e.file,e,r);case 7:return t.abrupt("return",t.sent);case 10:throw t.prev=10,t.t0=t.catch(4),o(),t.t0;case 14:case"end":return t.stop()}}),t,this,[[4,10]])})))}},{key:"url",value:function(e){return"".concat(this.cdnUrl,"/").concat(e)}},{key:"beginFileUpload",value:function(e,t,n){return y(this,void 0,void 0,regeneratorRuntime.mark((function r(){var o,i,a,u,c,s,l,p,h,v=this;return regeneratorRuntime.wrap((function(r){for(;;)switch(r.prev=r.next){case 0:return void 0!==t.onProgress&&t.onProgress({bytesSent:0,bytesTotal:e.size}),o={accountId:this.accountId,fileSize:e.size,fileName:e.name,mime:this.normalizeMimeType(e.type),tag:t.tag,userId:t.userId},this.debug("Initiating file upload. Params = ".concat(JSON.stringify(o))),this.preflight(),r.next=6,f.FilesService.beginUpload(o);case 6:return i=r.sent,a=i.uploadParts.count>1,this.debug("Initiated file upload. Metadata = ".concat(JSON.stringify(i))),u=function(){var e=0;return function(){if(e!==i.uploadParts.count-1)return++e}}(),c=[i.uploadParts.first],s=function(){return y(v,void 0,void 0,regeneratorRuntime.mark((function e(){var t,n;return regeneratorRuntime.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(void 0===(t=c.pop())){e.next=4;break}return this.debug("Dequeued part ".concat(t.uploadPartIndex)),e.abrupt("return",t);case 4:if(void 0!==(n=u())){e.next=8;break}return this.debug("No parts remaining."),e.abrupt("return",void 0);case 8:return this.preflight(),this.debug("Fetching metadata for part ".concat(n,".")),e.next=12,f.FilesService.getUploadPart(i.file.fileId,n);case 12:return e.abrupt("return",e.sent);case 13:case"end":return e.stop()}}),e,this)})))},l=[],p=function r(o){return y(v,void 0,void 0,regeneratorRuntime.mark((function u(){var c,p,f;return regeneratorRuntime.wrap((function(u){for(;;)switch(u.prev=u.next){case 0:return u.next=2,s();case 2:if(void 0===(c=u.sent)){u.next=10;break}return p=0,f=function(n){var r=n.bytesSent;if(void 0===l[o]?l[o]=r:(l[o]-=p,l[o]+=r),p=r,void 0!==t.onProgress){var i=l.reduce((function(e,t){return e+t}));t.onProgress({bytesSent:i,bytesTotal:e.size})}},u.next=8,this.uploadPart(e,i.file,a,c,f,n);case 8:return u.next=10,r(o);case 10:case"end":return u.stop()}}),u,this)})))},r.next=16,Promise.all((g=Array(this.maxUploadConcurrency).keys(),function(e){if(Array.isArray(e))return d(e)}(g)||function(e){if("undefined"!=typeof Symbol&&Symbol.iterator in Object(e))return Array.from(e)}(g)||function(e,t){if(e){if("string"==typeof e)return d(e,t);var n=Object.prototype.toString.call(e).slice(8,-1);return"Object"===n&&e.constructor&&(n=e.constructor.name),"Map"===n||"Set"===n?Array.from(e):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?d(e,t):void 0}}(g)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()).map((function(e){return y(v,void 0,void 0,regeneratorRuntime.mark((function t(){return regeneratorRuntime.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,p(e);case 2:return t.abrupt("return",t.sent);case 3:case"end":return t.stop()}}),t)})))})));case 16:return h={accountId:o.accountId,file:e,fileId:i.file.fileId,fileSize:i.file.size,fileUrl:this.url(i.file.fileId),tag:o.tag,userId:o.userId,mime:i.file.mime},this.debug("File upload completed. File = ".concat(JSON.stringify(h))),r.abrupt("return",h);case 19:case"end":return r.stop()}var g}),r,this)})))}},{key:"debug",value:function(e){this.debugMode&&console.log("[upload-js] ".concat(e))}},{key:"normalizeMimeType",value:function(e){var t=e.toLowerCase();return/^[a-z0-9]+\/[a-z0-9+\-._]+$/.test(t)?t:void 0}},{key:"preflight",value:function(){f.OpenAPI.BASE=this.apiUrl,this.authenticateWithApiKey?(f.OpenAPI.WITH_CREDENTIALS=!0,f.OpenAPI.USERNAME="apikey",f.OpenAPI.PASSWORD=this.config.apiKey):(f.OpenAPI.WITH_CREDENTIALS=!1,delete f.OpenAPI.USERNAME,delete f.OpenAPI.PASSWORD),void 0!==this.headers?f.OpenAPI.HEADERS=this.headers:delete f.OpenAPI.HEADERS}},{key:"putUploadPart",value:function(e,t,n,r,o,i){return y(this,void 0,void 0,regeneratorRuntime.mark((function a(){var u,c;return regeneratorRuntime.wrap((function(a){for(;;)switch(a.prev=a.next){case 0:return u=new XMLHttpRequest,c=!0,i((function(){c&&u.abort()})),a.prev=3,a.next=6,new Promise((function(i,a){u.upload.addEventListener("progress",(function(e){e.lengthComputable&&o({bytesSent:e.loaded,bytesTotal:e.total})}),!1),u.addEventListener("load",(function(){if(o({bytesSent:r.size,bytesTotal:r.size}),2===Math.floor(u.status/100)){var e=u.getResponseHeader("etag");null==e?a(new Error("File upload error: no etag header in upload response.")):i({etag:e})}else a(new Error("File upload error: status code ".concat(u.status)))})),u.onabort=function(){return a(new p("File upload cancelled."))},u.onerror=function(){return a(new Error("File upload error."))},u.ontimeout=function(){return a(new Error("File upload timeout."))},u.open("PUT",e),n||(u.setRequestHeader("content-type",t.mime),null!==t.name&&u.setRequestHeader("content-disposition",'attachment; filename="'.concat(encodeURIComponent(t.name),"\"; filename*=UTF-8''").concat(encodeURIComponent(t.name)))),u.send(r)}));case 6:return a.abrupt("return",a.sent);case 7:return a.prev=7,c=!1,a.finish(7);case 10:case"end":return a.stop()}}),a,null,[[3,,7,10]])})))}},{key:"uploadPart",value:function(e,t,n,r,o,i){return y(this,void 0,void 0,regeneratorRuntime.mark((function a(){var u,c,s;return regeneratorRuntime.wrap((function(a){for(;;)switch(a.prev=a.next){case 0:return u=-1===r.range.inclusiveEnd?new Blob:e.slice(r.range.inclusiveStart,r.range.inclusiveEnd+1),this.debug("Uploading part ".concat(r.uploadPartIndex,".")),a.next=4,this.putUploadPart(r.uploadUrl,t,n,u,o,i);case 4:return c=a.sent,s=c.etag,this.preflight(),a.next=9,f.FilesService.completeUploadPart(r.fileId,r.uploadPartIndex,{etag:s});case 9:this.debug("Uploaded part ".concat(r.uploadPartIndex,"."));case 10:case"end":return a.stop()}}),a,this)})))}}])&&h(t.prototype,n),e}()}},t={};function n(r){if(t[r])return t[r].exports;var o=t[r]={exports:{}};return e[r](o,o.exports,n),o.exports}return n.d=(e,t)=>{for(var r in t)n.o(t,r)&&!n.o(e,r)&&Object.defineProperty(e,r,{enumerable:!0,get:t[r]})},n.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),n.r=e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},n(675)})(); | ||
module.exports=(()=>{"use strict";var e={675:(e,t,n)=>{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){return!t||"object"!==r(t)&&"function"!=typeof t?i(e):t}function i(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}function a(e){var t="function"==typeof Map?new Map:void 0;return(a=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 u(e,arguments,l(this).constructor)}return r.prototype=Object.create(e.prototype,{constructor:{value:r,enumerable:!1,writable:!0,configurable:!0}}),s(r,e)})(e)}function u(e,t,n){return(u=c()?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 c(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Date.prototype.toString.call(Reflect.construct(Date,[],(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 l(e){return(l=Object.setPrototypeOf?Object.getPrototypeOf:function(e){return e.__proto__||Object.getPrototypeOf(e)})(e)}n.r(t),n.d(t,{Upload:()=>v});var p=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)}(a,e);var t,n,r=(t=a,n=c(),function(){var e,r=l(t);if(n){var i=l(this).constructor;e=Reflect.construct(r,arguments,i)}else e=r.apply(this,arguments);return o(this,e)});function a(e){var t;return function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,a),t=r.call(this,e),Object.setPrototypeOf(i(t),a.prototype),t}return a}(a(Error));const f=require("upload-api-client-upload-js");function d(e,t){(null==t||t>e.length)&&(t=e.length);for(var n=0,r=new Array(t);n<t;n++)r[n]=e[n];return r}function h(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 y=function(e,t,n,r){return new(n||(n=Promise))((function(o,i){function a(e){try{c(r.next(e))}catch(e){i(e)}}function u(e){try{c(r.throw(e))}catch(e){i(e)}}function c(e){var t;e.done?o(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(a,u)}c((r=r.apply(e,t||[])).next())}))},v=function(){function e(t){var n,r,o,i,a,u,c,s;if(function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),this.config=t,this.accountIdLength=7,this.apiKeyPrefix="public_",this.maxUploadConcurrency=5,!0===t.debug&&console.log("[upload-js] Initialized with API key '".concat(t.apiKey,"'")),this.apiUrl=null!==(r=null===(n=t.internal)||void 0===n?void 0:n.apiUrl)&&void 0!==r?r:"https://api.upload.io",this.cdnUrl=null!==(i=null===(o=t.internal)||void 0===o?void 0:o.cdnUrl)&&void 0!==i?i:"https://cdn.upload.io",this.authenticateWithApiKey=null===(u=null===(a=t.internal)||void 0===a?void 0:a.authenticateWithApiKey)||void 0===u||u,this.headers=null===(c=t.internal)||void 0===c?void 0:c.headers,this.debugMode=!0===t.debug,t.apiKey.trim()!==t.apiKey)throw new Error("[upload-js] Invalid API key. Whitespace detected: please remove whitespace from the API key and try again.");if(!1===(null===(s=t.internal)||void 0===s?void 0:s.authenticateWithApiKey))this.accountId=t.internal.accountId;else{if(!t.apiKey.startsWith(this.apiKeyPrefix))throw new Error("[upload-js] Invalid API key. API keys must start with '".concat(this.apiKeyPrefix,"'"));if(this.accountId=t.apiKey.substr(this.apiKeyPrefix.length,this.accountIdLength),this.accountId.length!==this.accountIdLength)throw new Error("[upload-js] Invalid API key. API keys must be at least ".concat(this.apiKeyPrefix.length+this.accountIdLength," characters long, but the API key you provided is ").concat(this.apiKeyPrefix.length+this.accountId.length,"."))}}var t,n;return t=e,(n=[{key:"createFileInputHandler",value:function(e){var t=this;return function(n){var r=n.target;if(void 0===r.files||null===r.files)throw new Error("No property 'files' on input element: ensure 'createFileInputHandler' is set to the 'onchange' attribute on an input of type 'file'.");if(void 0===r.files[0])throw new Error("No file selected.");t.uploadFile(Object.assign(Object.assign({},e),{file:r.files[0]})).then(e.onUploaded,(function(t){void 0!==e.onError?e.onError(t):console.error("Cannot upload file. To remove this console message, handle the error explicitly by providing an 'onError' parameter: upload.createFileInputHandler({onUploaded, onError})",t)}))}}},{key:"uploadFile",value:function(e){return y(this,void 0,void 0,regeneratorRuntime.mark((function t(){var n,r,o;return regeneratorRuntime.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=[],r=function(e){n.push(e)},o=function(){return n.forEach((function(e){return e()}))},void 0!==e.onBegin&&e.onBegin({cancel:o}),t.prev=4,t.next=7,this.beginFileUpload(e.file,e,r);case 7:return t.abrupt("return",t.sent);case 10:throw t.prev=10,t.t0=t.catch(4),o(),t.t0;case 14:case"end":return t.stop()}}),t,this,[[4,10]])})))}},{key:"url",value:function(e){return"".concat(this.cdnUrl,"/").concat(e)}},{key:"beginFileUpload",value:function(e,t,n){return y(this,void 0,void 0,regeneratorRuntime.mark((function r(){var o,i,a,u,c,s,l,p,h,v=this;return regeneratorRuntime.wrap((function(r){for(;;)switch(r.prev=r.next){case 0:return void 0!==t.onProgress&&t.onProgress({bytesSent:0,bytesTotal:e.size}),o={accountId:this.accountId,fileSize:e.size,fileName:e.name,mime:this.normalizeMimeType(e.type),tag:t.tag,userId:t.userId},this.debug("Initiating file upload. Params = ".concat(JSON.stringify(o))),this.preflight(),r.next=6,f.FilesService.beginUpload(o);case 6:return i=r.sent,a=i.uploadParts.count>1,this.debug("Initiated file upload. Metadata = ".concat(JSON.stringify(i))),u=function(){var e=0;return function(){if(e!==i.uploadParts.count-1)return++e}}(),c=[i.uploadParts.first],s=function(e){return y(v,void 0,void 0,regeneratorRuntime.mark((function t(){var n,r;return regeneratorRuntime.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(void 0===(n=c.pop())){t.next=4;break}return this.debug("Dequeued part ".concat(n.uploadPartIndex,"."),e),t.abrupt("return",n);case 4:if(void 0!==(r=u())){t.next=8;break}return this.debug("No parts remaining.",e),t.abrupt("return",void 0);case 8:return this.preflight(),this.debug("Fetching metadata for part ".concat(r,"."),e),t.next=12,f.FilesService.getUploadPart(i.file.fileId,r);case 12:return t.abrupt("return",t.sent);case 13:case"end":return t.stop()}}),t,this)})))},l=[],p=function r(o){return y(v,void 0,void 0,regeneratorRuntime.mark((function u(){var c,p,f;return regeneratorRuntime.wrap((function(u){for(;;)switch(u.prev=u.next){case 0:return u.next=2,s(o);case 2:if(void 0===(c=u.sent)){u.next=10;break}return p=0,f=function(n){var r=n.bytesSent;if(void 0===l[o]?l[o]=r:(l[o]-=p,l[o]+=r),p=r,void 0!==t.onProgress){var i=l.reduce((function(e,t){return e+t}));t.onProgress({bytesSent:i,bytesTotal:e.size})}},u.next=8,this.uploadPart(e,i.file,a,c,f,n,o);case 8:return u.next=10,r(o);case 10:case"end":return u.stop()}}),u,this)})))},r.next=16,Promise.all((g=Array(this.maxUploadConcurrency).keys(),function(e){if(Array.isArray(e))return d(e)}(g)||function(e){if("undefined"!=typeof Symbol&&Symbol.iterator in Object(e))return Array.from(e)}(g)||function(e,t){if(e){if("string"==typeof e)return d(e,t);var n=Object.prototype.toString.call(e).slice(8,-1);return"Object"===n&&e.constructor&&(n=e.constructor.name),"Map"===n||"Set"===n?Array.from(e):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?d(e,t):void 0}}(g)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()).map((function(e){return y(v,void 0,void 0,regeneratorRuntime.mark((function t(){return regeneratorRuntime.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,p(e);case 2:return t.abrupt("return",t.sent);case 3:case"end":return t.stop()}}),t)})))})));case 16:return h={accountId:o.accountId,file:e,fileId:i.file.fileId,fileSize:i.file.size,fileUrl:this.url(i.file.fileId),tag:o.tag,userId:o.userId,mime:i.file.mime},this.debug("File upload completed. File = ".concat(JSON.stringify(h))),r.abrupt("return",h);case 19:case"end":return r.stop()}var g}),r,this)})))}},{key:"debug",value:function(e,t){this.debugMode&&console.log("[upload-js] ".concat(e).concat(void 0!==t?" (Worker ".concat(t,".)"):""))}},{key:"normalizeMimeType",value:function(e){var t=e.toLowerCase();return/^[a-z0-9]+\/[a-z0-9+\-._]+$/.test(t)?t:void 0}},{key:"preflight",value:function(){f.OpenAPI.BASE=this.apiUrl,this.authenticateWithApiKey?(f.OpenAPI.WITH_CREDENTIALS=!0,f.OpenAPI.USERNAME="apikey",f.OpenAPI.PASSWORD=this.config.apiKey):(f.OpenAPI.WITH_CREDENTIALS=!1,delete f.OpenAPI.USERNAME,delete f.OpenAPI.PASSWORD),void 0!==this.headers?f.OpenAPI.HEADERS=this.headers:delete f.OpenAPI.HEADERS}},{key:"putUploadPart",value:function(e,t,n,r,o,i){return y(this,void 0,void 0,regeneratorRuntime.mark((function a(){var u,c;return regeneratorRuntime.wrap((function(a){for(;;)switch(a.prev=a.next){case 0:return u=new XMLHttpRequest,c=!0,i((function(){c&&u.abort()})),a.prev=3,a.next=6,new Promise((function(i,a){u.upload.addEventListener("progress",(function(e){e.lengthComputable&&o({bytesSent:e.loaded,bytesTotal:e.total})}),!1),u.addEventListener("load",(function(){if(o({bytesSent:r.size,bytesTotal:r.size}),2===Math.floor(u.status/100)){var e=u.getResponseHeader("etag");null==e?a(new Error("File upload error: no etag header in upload response.")):i({etag:e})}else a(new Error("File upload error: status code ".concat(u.status)))})),u.onabort=function(){return a(new p("File upload cancelled."))},u.onerror=function(){return a(new Error("File upload error."))},u.ontimeout=function(){return a(new Error("File upload timeout."))},u.open("PUT",e),n||(u.setRequestHeader("content-type",t.mime),null!==t.name&&u.setRequestHeader("content-disposition",'attachment; filename="'.concat(encodeURIComponent(t.name),"\"; filename*=UTF-8''").concat(encodeURIComponent(t.name)))),u.send(r)}));case 6:return a.abrupt("return",a.sent);case 7:return a.prev=7,c=!1,a.finish(7);case 10:case"end":return a.stop()}}),a,null,[[3,,7,10]])})))}},{key:"uploadPart",value:function(e,t,n,r,o,i,a){return y(this,void 0,void 0,regeneratorRuntime.mark((function u(){var c,s,l;return regeneratorRuntime.wrap((function(u){for(;;)switch(u.prev=u.next){case 0:return c=-1===r.range.inclusiveEnd?new Blob:e.slice(r.range.inclusiveStart,r.range.inclusiveEnd+1),this.debug("Uploading part ".concat(r.uploadPartIndex,"."),a),u.next=4,this.putUploadPart(r.uploadUrl,t,n,c,o,i);case 4:return s=u.sent,l=s.etag,this.preflight(),u.next=9,f.FilesService.completeUploadPart(r.fileId,r.uploadPartIndex,{etag:l});case 9:this.debug("Uploaded part ".concat(r.uploadPartIndex,"."),a);case 10:case"end":return u.stop()}}),u,this)})))}}])&&h(t.prototype,n),e}()}},t={};function n(r){if(t[r])return t[r].exports;var o=t[r]={exports:{}};return e[r](o,o.exports,n),o.exports}return n.d=(e,t)=>{for(var r in t)n.o(t,r)&&!n.o(e,r)&&Object.defineProperty(e,r,{enumerable:!0,get:t[r]})},n.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),n.r=e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},n(675)})(); |
{ | ||
"name": "upload-js", | ||
"version": "1.18.0", | ||
"version": "1.19.0", | ||
"author": "Upload <hello@upload.io> (https://upload.io)", | ||
@@ -5,0 +5,0 @@ "description": "Upload.js JavaScript Library", |
@@ -153,6 +153,6 @@ import { Cancellation } from "upload-js/Errors"; | ||
const nextPartQueue = [uploadMetadata.uploadParts.first]; | ||
const getNextPart: () => Promise<UploadPart | undefined> = async () => { | ||
const getNextPart: (workerIndex: number) => Promise<UploadPart | undefined> = async workerIndex => { | ||
const nextPart = nextPartQueue.pop(); | ||
if (nextPart !== undefined) { | ||
this.debug(`Dequeued part ${nextPart.uploadPartIndex}`); | ||
this.debug(`Dequeued part ${nextPart.uploadPartIndex}.`, workerIndex); | ||
return nextPart; | ||
@@ -162,7 +162,7 @@ } | ||
if (uploadPartIndex === undefined) { | ||
this.debug("No parts remaining."); | ||
this.debug("No parts remaining.", workerIndex); | ||
return undefined; | ||
} | ||
this.preflight(); | ||
this.debug(`Fetching metadata for part ${uploadPartIndex}.`); | ||
this.debug(`Fetching metadata for part ${uploadPartIndex}.`, workerIndex); | ||
return await FilesService.getUploadPart(uploadMetadata.file.fileId, uploadPartIndex); | ||
@@ -173,3 +173,3 @@ }; | ||
const uploadNextPart: (workerIndex: number) => Promise<void> = async workerIndex => { | ||
const nextPart = await getNextPart(); | ||
const nextPart = await getNextPart(workerIndex); | ||
if (nextPart !== undefined) { | ||
@@ -192,3 +192,11 @@ let lastBytesSent = 0; | ||
}; | ||
await this.uploadPart(file, uploadMetadata.file, isMultipart, nextPart, progress, addCancellationHandler); | ||
await this.uploadPart( | ||
file, | ||
uploadMetadata.file, | ||
isMultipart, | ||
nextPart, | ||
progress, | ||
addCancellationHandler, | ||
workerIndex | ||
); | ||
await uploadNextPart(workerIndex); | ||
@@ -218,5 +226,5 @@ } | ||
private debug(message: string): void { | ||
private debug(message: string, workerIndex?: number): void { | ||
if (this.debugMode) { | ||
console.log(`[upload-js] ${message}`); | ||
console.log(`[upload-js] ${message}${workerIndex !== undefined ? ` (Worker ${workerIndex}.)` : ""}`); | ||
} | ||
@@ -330,3 +338,4 @@ } | ||
progress: (status: { bytesSent: number; bytesTotal: number }) => void, | ||
addCancellationHandler: AddCancellationHandler | ||
addCancellationHandler: AddCancellationHandler, | ||
workerIndex: number | ||
): Promise<void> { | ||
@@ -336,3 +345,3 @@ const content: Blob = | ||
this.debug(`Uploading part ${part.uploadPartIndex}.`); | ||
this.debug(`Uploading part ${part.uploadPartIndex}.`, workerIndex); | ||
@@ -353,4 +362,4 @@ const { etag } = await this.putUploadPart( | ||
this.debug(`Uploaded part ${part.uploadPartIndex}.`); | ||
this.debug(`Uploaded part ${part.uploadPartIndex}.`, workerIndex); | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
313734
7585