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

@mux/upchunk

Package Overview
Dependencies
Maintainers
19
Versions
27
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@mux/upchunk - npm Package Compare versions

Comparing version 2.2.1 to 2.2.2

2

dist/upchunk.js

@@ -1,2 +0,2 @@

!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.UpChunk=t():e.UpChunk=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";var r=this&&this.__assign||function(){return(r=Object.assign||function(e){for(var t,n=1,r=arguments.length;n<r;n++)for(var o in t=arguments[n])Object.prototype.hasOwnProperty.call(t,o)&&(e[o]=t[o]);return e}).apply(this,arguments)};t.__esModule=!0,t.createUpload=t.UpChunk=void 0;var o=n(1),i=n(2),s=[200,201,202,204,308],u=[408,502,503,504],a=function(){function e(e){var t=this;this.endpoint=e.endpoint,this.file=e.file,this.headers=e.headers||{},this.method=e.method||"PUT",this.chunkSize=e.chunkSize||5120,this.attempts=e.attempts||5,this.delayBeforeAttempt=e.delayBeforeAttempt||1,this.maxFileBytes=1024*(e.maxFileSize||0),this.chunkCount=0,this.chunkByteSize=1024*this.chunkSize,this.totalChunks=Math.ceil(this.file.size/this.chunkByteSize),this.attemptCount=0,this.offline=!1,this.paused=!1,this.success=!1,this.reader=new FileReader,this.eventTarget=new o.EventTarget,this.validateOptions(),this.getEndpoint().then((function(){return t.sendChunks()})),"undefined"!=typeof window&&(window.addEventListener("online",(function(){t.offline&&(t.offline=!1,t.dispatch("online"),t.sendChunks())})),window.addEventListener("offline",(function(){t.offline=!0,t.dispatch("offline")})))}return e.prototype.on=function(e,t){this.eventTarget.addEventListener(e,t)},e.prototype.abort=function(){var e;this.pause(),null===(e=this.currentXhr)||void 0===e||e.abort()},e.prototype.pause=function(){this.paused=!0},e.prototype.resume=function(){this.paused&&(this.paused=!1,this.sendChunks())},e.prototype.dispatch=function(e,t){var n=new CustomEvent(e,{detail:t});this.eventTarget.dispatchEvent(n)},e.prototype.validateOptions=function(){if(!this.endpoint||"function"!=typeof this.endpoint&&"string"!=typeof this.endpoint)throw new TypeError("endpoint must be defined as a string or a function that returns a promise");if(!(this.file instanceof File))throw new TypeError("file must be a File object");if(this.headers&&"object"!=typeof this.headers)throw new TypeError("headers must be null or an object");if(this.chunkSize&&("number"!=typeof this.chunkSize||this.chunkSize<=0||this.chunkSize%256!=0))throw new TypeError("chunkSize must be a positive number in multiples of 256");if(this.maxFileBytes>0&&this.maxFileBytes<this.file.size)throw new Error("file size exceeds maximum ("+this.file.size+" > "+this.maxFileBytes+")");if(this.attempts&&("number"!=typeof this.attempts||this.attempts<=0))throw new TypeError("retries must be a positive number");if(this.delayBeforeAttempt&&("number"!=typeof this.delayBeforeAttempt||this.delayBeforeAttempt<0))throw new TypeError("delayBeforeAttempt must be a positive number")},e.prototype.getEndpoint=function(){var e=this;return"string"==typeof this.endpoint?(this.endpointValue=this.endpoint,Promise.resolve(this.endpoint)):this.endpoint(this.file).then((function(t){return e.endpointValue=t,e.endpointValue}))},e.prototype.getChunk=function(){var e=this;return new Promise((function(t){var n=1===e.totalChunks?e.file.size:e.chunkByteSize,r=n*e.chunkCount;e.reader.onload=function(){null!==e.reader.result&&(e.chunk=new Blob([e.reader.result],{type:"application/octet-stream"})),t()},e.reader.readAsArrayBuffer(e.file.slice(r,r+n))}))},e.prototype.xhrPromise=function(e){var t=this,n=function(e){e.upload.onprogress=function(e){var n=100/t.totalChunks*t.chunkCount,r=e.loaded/t.file.size*100;t.dispatch("progress",n+r)}};return new Promise((function(o,s){t.currentXhr=i.default(r(r({},e),{beforeSend:n}),(function(e,n){return t.currentXhr=void 0,e?s(e):o(n)}))}))},e.prototype.sendChunk=function(){var e=this.chunkCount*this.chunkByteSize,t=e+this.chunk.size-1,n=r(r({},this.headers),{"Content-Type":this.file.type,"Content-Range":"bytes "+e+"-"+t+"/"+this.file.size});return this.dispatch("attempt",{chunkNumber:this.chunkCount,chunkSize:this.chunk.size}),this.xhrPromise({headers:n,url:this.endpointValue,method:this.method,body:this.chunk})},e.prototype.manageRetries=function(){var e=this;if(this.attemptCount<this.attempts)return setTimeout((function(){return e.sendChunks()}),1e3*this.delayBeforeAttempt),void this.dispatch("attemptFailure",{message:"An error occured uploading chunk "+this.chunkCount+". "+(this.attempts-this.attemptCount)+" retries left.",chunkNumber:this.chunkCount,attemptsLeft:this.attempts-this.attemptCount});this.dispatch("error",{message:"An error occured uploading chunk "+this.chunkCount+". No more retries, stopping upload",chunk:this.chunkCount,attempts:this.attemptCount})},e.prototype.sendChunks=function(){var e=this;this.paused||this.offline||this.success||this.getChunk().then((function(){return e.sendChunk()})).then((function(t){if(e.attemptCount=e.attemptCount+1,s.includes(t.statusCode)){e.dispatch("chunkSuccess",{chunk:e.chunkCount,attempts:e.attemptCount,response:t}),e.attemptCount=0,e.chunkCount=e.chunkCount+1,e.chunkCount<e.totalChunks?e.sendChunks():(e.success=!0,e.dispatch("success"));var n=Math.round(100/e.totalChunks*e.chunkCount);e.dispatch("progress",n)}else if(u.includes(t.statusCode)){if(e.paused||e.offline)return;e.manageRetries()}else{if(e.paused||e.offline)return;e.dispatch("error",{message:"Server responded with "+t.statusCode+". Stopping upload.",chunkNumber:e.chunkCount,attempts:e.attemptCount})}})).catch((function(t){e.paused||e.offline||e.manageRetries()}))},e}();t.UpChunk=a,t.createUpload=function(e){return new a(e)}},function(e,t,n){"use strict";Object.defineProperty(t,"__esModule",{value:!0});const r=new WeakMap,o=new WeakMap;function i(e){const t=r.get(e);return console.assert(null!=t,"'this' is expected an Event object, but got",e),t}function s(e){null==e.passiveListener?e.event.cancelable&&(e.canceled=!0,"function"==typeof e.event.preventDefault&&e.event.preventDefault()):"undefined"!=typeof console&&"function"==typeof console.error&&console.error("Unable to preventDefault inside passive event listener invocation.",e.passiveListener)}function u(e,t){r.set(this,{eventTarget:e,event:t,eventPhase:2,currentTarget:e,canceled:!1,stopped:!1,immediateStopped:!1,passiveListener:null,timeStamp:t.timeStamp||Date.now()}),Object.defineProperty(this,"isTrusted",{value:!1,enumerable:!0});const n=Object.keys(t);for(let e=0;e<n.length;++e){const t=n[e];t in this||Object.defineProperty(this,t,a(t))}}function a(e){return{get(){return i(this).event[e]},set(t){i(this).event[e]=t},configurable:!0,enumerable:!0}}function p(e){return{value(){const t=i(this).event;return t[e].apply(t,arguments)},configurable:!0,enumerable:!0}}function c(e){if(null==e||e===Object.prototype)return u;let t=o.get(e);return null==t&&(t=function(e,t){const n=Object.keys(t);if(0===n.length)return e;function r(t,n){e.call(this,t,n)}r.prototype=Object.create(e.prototype,{constructor:{value:r,configurable:!0,writable:!0}});for(let o=0;o<n.length;++o){const i=n[o];if(!(i in e.prototype)){const e="function"==typeof Object.getOwnPropertyDescriptor(t,i).value;Object.defineProperty(r.prototype,i,e?p(i):a(i))}}return r}(c(Object.getPrototypeOf(e)),e),o.set(e,t)),t}function l(e){return i(e).immediateStopped}function f(e,t){i(e).passiveListener=t}u.prototype={get type(){return i(this).event.type},get target(){return i(this).eventTarget},get currentTarget(){return i(this).currentTarget},composedPath(){const e=i(this).currentTarget;return null==e?[]:[e]},get NONE(){return 0},get CAPTURING_PHASE(){return 1},get AT_TARGET(){return 2},get BUBBLING_PHASE(){return 3},get eventPhase(){return i(this).eventPhase},stopPropagation(){const e=i(this);e.stopped=!0,"function"==typeof e.event.stopPropagation&&e.event.stopPropagation()},stopImmediatePropagation(){const e=i(this);e.stopped=!0,e.immediateStopped=!0,"function"==typeof e.event.stopImmediatePropagation&&e.event.stopImmediatePropagation()},get bubbles(){return Boolean(i(this).event.bubbles)},get cancelable(){return Boolean(i(this).event.cancelable)},preventDefault(){s(i(this))},get defaultPrevented(){return i(this).canceled},get composed(){return Boolean(i(this).event.composed)},get timeStamp(){return i(this).timeStamp},get srcElement(){return i(this).eventTarget},get cancelBubble(){return i(this).stopped},set cancelBubble(e){if(!e)return;const t=i(this);t.stopped=!0,"boolean"==typeof t.event.cancelBubble&&(t.event.cancelBubble=!0)},get returnValue(){return!i(this).canceled},set returnValue(e){e||s(i(this))},initEvent(){}},Object.defineProperty(u.prototype,"constructor",{value:u,configurable:!0,writable:!0}),"undefined"!=typeof window&&void 0!==window.Event&&(Object.setPrototypeOf(u.prototype,window.Event.prototype),o.set(window.Event.prototype,u));const h=new WeakMap;function d(e){return null!==e&&"object"==typeof e}function y(e){const t=h.get(e);if(null==t)throw new TypeError("'this' is expected an EventTarget object, but got another value.");return t}function v(e,t){Object.defineProperty(e,"on"+t,function(e){return{get(){let t=y(this).get(e);for(;null!=t;){if(3===t.listenerType)return t.listener;t=t.next}return null},set(t){"function"==typeof t||d(t)||(t=null);const n=y(this);let r=null,o=n.get(e);for(;null!=o;)3===o.listenerType?null!==r?r.next=o.next:null!==o.next?n.set(e,o.next):n.delete(e):r=o,o=o.next;if(null!==t){const o={listener:t,listenerType:3,passive:!1,once:!1,next:null};null===r?n.set(e,o):r.next=o}},configurable:!0,enumerable:!0}}(t))}function m(e){function t(){b.call(this)}t.prototype=Object.create(b.prototype,{constructor:{value:t,configurable:!0,writable:!0}});for(let n=0;n<e.length;++n)v(t.prototype,e[n]);return t}function b(){if(!(this instanceof b)){if(1===arguments.length&&Array.isArray(arguments[0]))return m(arguments[0]);if(arguments.length>0){const e=new Array(arguments.length);for(let t=0;t<arguments.length;++t)e[t]=arguments[t];return m(e)}throw new TypeError("Cannot call a class as a function")}h.set(this,new Map)}b.prototype={addEventListener(e,t,n){if(null==t)return;if("function"!=typeof t&&!d(t))throw new TypeError("'listener' should be a function or an object.");const r=y(this),o=d(n),i=(o?Boolean(n.capture):Boolean(n))?1:2,s={listener:t,listenerType:i,passive:o&&Boolean(n.passive),once:o&&Boolean(n.once),next:null};let u=r.get(e);if(void 0===u)return void r.set(e,s);let a=null;for(;null!=u;){if(u.listener===t&&u.listenerType===i)return;a=u,u=u.next}a.next=s},removeEventListener(e,t,n){if(null==t)return;const r=y(this),o=(d(n)?Boolean(n.capture):Boolean(n))?1:2;let i=null,s=r.get(e);for(;null!=s;){if(s.listener===t&&s.listenerType===o)return void(null!==i?i.next=s.next:null!==s.next?r.set(e,s.next):r.delete(e));i=s,s=s.next}},dispatchEvent(e){if(null==e||"string"!=typeof e.type)throw new TypeError('"event.type" should be a string.');const t=y(this),n=e.type;let r=t.get(n);if(null==r)return!0;const o=function(e,t){return new(c(Object.getPrototypeOf(t)))(e,t)}(this,e);let s=null;for(;null!=r;){if(r.once?null!==s?s.next=r.next:null!==r.next?t.set(n,r.next):t.delete(n):s=r,f(o,r.passive?r.listener:null),"function"==typeof r.listener)try{r.listener.call(this,o)}catch(e){"undefined"!=typeof console&&"function"==typeof console.error&&console.error(e)}else 3!==r.listenerType&&"function"==typeof r.listener.handleEvent&&r.listener.handleEvent(o);if(l(o))break;r=r.next}return f(o,null),function(e,t){i(e).eventPhase=t}(o,0),function(e,t){i(e).currentTarget=t}(o,null),!o.defaultPrevented}},Object.defineProperty(b.prototype,"constructor",{value:b,configurable:!0,writable:!0}),"undefined"!=typeof window&&void 0!==window.EventTarget&&Object.setPrototypeOf(b.prototype,window.EventTarget.prototype),t.defineEventAttribute=v,t.EventTarget=b,t.default=b,e.exports=b,e.exports.EventTarget=e.exports.default=b,e.exports.defineEventAttribute=v},function(e,t,n){"use strict";var r=n(3),o=n(5),i=n(6),s=n(7);function u(e,t,n){var r=e;return o(t)?(n=t,"string"==typeof e&&(r={uri:e})):r=s(t,{uri:e}),r.callback=n,r}function a(e,t,n){return p(t=u(e,t,n))}function p(e){if(void 0===e.callback)throw new Error("callback argument missing");var t=!1,n=function(n,r,o){t||(t=!0,e.callback(n,r,o))};function r(){var e=void 0;if(e=c.response?c.response:c.responseText||function(e){try{if("document"===e.responseType)return e.responseXML;var t=e.responseXML&&"parsererror"===e.responseXML.documentElement.nodeName;if(""===e.responseType&&!t)return e.responseXML}catch(e){}return null}(c),m)try{e=JSON.parse(e)}catch(e){}return e}function o(e){return clearTimeout(l),e instanceof Error||(e=new Error(""+(e||"Unknown XMLHttpRequest Error"))),e.statusCode=0,n(e,b)}function s(){if(!p){var t;clearTimeout(l),t=e.useXDR&&void 0===c.status?200:1223===c.status?204:c.status;var o=b,s=null;return 0!==t?(o={body:r(),statusCode:t,method:h,headers:{},url:f,rawRequest:c},c.getAllResponseHeaders&&(o.headers=i(c.getAllResponseHeaders()))):s=new Error("Internal XMLHttpRequest Error"),n(s,o,o.body)}}var u,p,c=e.xhr||null;c||(c=e.cors||e.useXDR?new a.XDomainRequest:new a.XMLHttpRequest);var l,f=c.url=e.uri||e.url,h=c.method=e.method||"GET",d=e.body||e.data,y=c.headers=e.headers||{},v=!!e.sync,m=!1,b={body:void 0,headers:{},statusCode:0,method:h,url:f,rawRequest:c};if("json"in e&&!1!==e.json&&(m=!0,y.accept||y.Accept||(y.Accept="application/json"),"GET"!==h&&"HEAD"!==h&&(y["content-type"]||y["Content-Type"]||(y["Content-Type"]="application/json"),d=JSON.stringify(!0===e.json?d:e.json))),c.onreadystatechange=function(){4===c.readyState&&setTimeout(s,0)},c.onload=s,c.onerror=o,c.onprogress=function(){},c.onabort=function(){p=!0},c.ontimeout=o,c.open(h,f,!v,e.username,e.password),v||(c.withCredentials=!!e.withCredentials),!v&&e.timeout>0&&(l=setTimeout((function(){if(!p){p=!0,c.abort("timeout");var e=new Error("XMLHttpRequest timeout");e.code="ETIMEDOUT",o(e)}}),e.timeout)),c.setRequestHeader)for(u in y)y.hasOwnProperty(u)&&c.setRequestHeader(u,y[u]);else if(e.headers&&!function(e){for(var t in e)if(e.hasOwnProperty(t))return!1;return!0}(e.headers))throw new Error("Headers cannot be set on an XDomainRequest object");return"responseType"in e&&(c.responseType=e.responseType),"beforeSend"in e&&"function"==typeof e.beforeSend&&e.beforeSend(c),c.send(d||null),c}e.exports=a,e.exports.default=a,a.XMLHttpRequest=r.XMLHttpRequest||function(){},a.XDomainRequest="withCredentials"in new a.XMLHttpRequest?a.XMLHttpRequest:r.XDomainRequest,function(e,t){for(var n=0;n<e.length;n++)t(e[n])}(["get","put","post","patch","head","delete"],(function(e){a["delete"===e?"del":e]=function(t,n,r){return(n=u(t,n,r)).method=e.toUpperCase(),p(n)}}))},function(e,t,n){(function(t){var n;n="undefined"!=typeof window?window:void 0!==t?t:"undefined"!=typeof self?self:{},e.exports=n}).call(this,n(4))},function(e,t){var n;n=function(){return this}();try{n=n||new Function("return this")()}catch(e){"object"==typeof window&&(n=window)}e.exports=n},function(e,t){e.exports=function(e){if(!e)return!1;var t=n.call(e);return"[object Function]"===t||"function"==typeof e&&"[object RegExp]"!==t||"undefined"!=typeof window&&(e===window.setTimeout||e===window.alert||e===window.confirm||e===window.prompt)};var n=Object.prototype.toString},function(e,t){var n=function(e){return e.replace(/^\s+|\s+$/g,"")};e.exports=function(e){if(!e)return{};for(var t,r={},o=n(e).split("\n"),i=0;i<o.length;i++){var s=o[i],u=s.indexOf(":"),a=n(s.slice(0,u)).toLowerCase(),p=n(s.slice(u+1));void 0===r[a]?r[a]=p:(t=r[a],"[object Array]"===Object.prototype.toString.call(t)?r[a].push(p):r[a]=[r[a],p])}return r}},function(e,t){e.exports=function(){for(var e={},t=0;t<arguments.length;t++){var r=arguments[t];for(var o in r)n.call(r,o)&&(e[o]=r[o])}return e};var n=Object.prototype.hasOwnProperty}])}));
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.UpChunk=t():e.UpChunk=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";var r=this&&this.__assign||function(){return(r=Object.assign||function(e){for(var t,n=1,r=arguments.length;n<r;n++)for(var o in t=arguments[n])Object.prototype.hasOwnProperty.call(t,o)&&(e[o]=t[o]);return e}).apply(this,arguments)};t.__esModule=!0,t.createUpload=t.UpChunk=void 0;var o=n(1),i=n(2),s=[200,201,202,204,308],u=[408,502,503,504],a=function(){function e(e){var t=this;this.endpoint=e.endpoint,this.file=e.file,this.headers=e.headers||{},this.method=e.method||"PUT",this.chunkSize=e.chunkSize||5120,this.attempts=e.attempts||5,this.delayBeforeAttempt=e.delayBeforeAttempt||1,this.maxFileBytes=1024*(e.maxFileSize||0),this.chunkCount=0,this.chunkByteSize=1024*this.chunkSize,this.totalChunks=Math.ceil(this.file.size/this.chunkByteSize),this.attemptCount=0,this.offline=!1,this.paused=!1,this.success=!1,this.reader=new FileReader,this.eventTarget=new o.EventTarget,this.validateOptions(),this.getEndpoint().then((function(){return t.sendChunks()})),"undefined"!=typeof window&&(window.addEventListener("online",(function(){t.offline&&(t.offline=!1,t.dispatch("online"),t.sendChunks())})),window.addEventListener("offline",(function(){t.offline=!0,t.dispatch("offline")})))}return e.prototype.on=function(e,t){this.eventTarget.addEventListener(e,t)},e.prototype.abort=function(){var e;this.pause(),null===(e=this.currentXhr)||void 0===e||e.abort()},e.prototype.pause=function(){this.paused=!0},e.prototype.resume=function(){this.paused&&(this.paused=!1,this.sendChunks())},e.prototype.dispatch=function(e,t){var n=new CustomEvent(e,{detail:t});this.eventTarget.dispatchEvent(n)},e.prototype.validateOptions=function(){if(!this.endpoint||"function"!=typeof this.endpoint&&"string"!=typeof this.endpoint)throw new TypeError("endpoint must be defined as a string or a function that returns a promise");if(!(this.file instanceof File))throw new TypeError("file must be a File object");if(this.headers&&"object"!=typeof this.headers)throw new TypeError("headers must be null or an object");if(this.chunkSize&&("number"!=typeof this.chunkSize||this.chunkSize<=0||this.chunkSize%256!=0))throw new TypeError("chunkSize must be a positive number in multiples of 256");if(this.maxFileBytes>0&&this.maxFileBytes<this.file.size)throw new Error("file size exceeds maximum ("+this.file.size+" > "+this.maxFileBytes+")");if(this.attempts&&("number"!=typeof this.attempts||this.attempts<=0))throw new TypeError("retries must be a positive number");if(this.delayBeforeAttempt&&("number"!=typeof this.delayBeforeAttempt||this.delayBeforeAttempt<0))throw new TypeError("delayBeforeAttempt must be a positive number")},e.prototype.getEndpoint=function(){var e=this;return"string"==typeof this.endpoint?(this.endpointValue=this.endpoint,Promise.resolve(this.endpoint)):this.endpoint(this.file).then((function(t){return e.endpointValue=t,e.endpointValue}))},e.prototype.getChunk=function(){var e=this;return new Promise((function(t){var n=1===e.totalChunks?e.file.size:e.chunkByteSize,r=n*e.chunkCount;e.reader.onload=function(){null!==e.reader.result&&(e.chunk=new Blob([e.reader.result],{type:"application/octet-stream"})),t()},e.reader.readAsArrayBuffer(e.file.slice(r,r+n))}))},e.prototype.xhrPromise=function(e){var t=this,n=function(e){e.upload.onprogress=function(e){var n,r=100/t.totalChunks,o=r*t.file.size,i=r*t.chunkCount,s=e.loaded/(null!==(n=e.total)&&void 0!==n?n:o)*r;t.dispatch("progress",Math.min(i+s,100))}};return new Promise((function(o,s){t.currentXhr=i.default(r(r({},e),{beforeSend:n}),(function(e,n){return t.currentXhr=void 0,e?s(e):o(n)}))}))},e.prototype.sendChunk=function(){var e=this.chunkCount*this.chunkByteSize,t=e+this.chunk.size-1,n=r(r({},this.headers),{"Content-Type":this.file.type,"Content-Range":"bytes "+e+"-"+t+"/"+this.file.size});return this.dispatch("attempt",{chunkNumber:this.chunkCount,chunkSize:this.chunk.size}),this.xhrPromise({headers:n,url:this.endpointValue,method:this.method,body:this.chunk})},e.prototype.manageRetries=function(){var e=this;if(this.attemptCount<this.attempts)return setTimeout((function(){return e.sendChunks()}),1e3*this.delayBeforeAttempt),void this.dispatch("attemptFailure",{message:"An error occured uploading chunk "+this.chunkCount+". "+(this.attempts-this.attemptCount)+" retries left.",chunkNumber:this.chunkCount,attemptsLeft:this.attempts-this.attemptCount});this.dispatch("error",{message:"An error occured uploading chunk "+this.chunkCount+". No more retries, stopping upload",chunk:this.chunkCount,attempts:this.attemptCount})},e.prototype.sendChunks=function(){var e=this;this.paused||this.offline||this.success||this.getChunk().then((function(){return e.sendChunk()})).then((function(t){if(e.attemptCount=e.attemptCount+1,s.includes(t.statusCode)){e.dispatch("chunkSuccess",{chunk:e.chunkCount,attempts:e.attemptCount,response:t}),e.attemptCount=0,e.chunkCount=e.chunkCount+1,e.chunkCount<e.totalChunks?e.sendChunks():(e.success=!0,e.dispatch("success"));var n=100*(e.chunkCount/e.totalChunks*e.file.size)/e.file.size;e.dispatch("progress",n)}else if(u.includes(t.statusCode)){if(e.paused||e.offline)return;e.manageRetries()}else{if(e.paused||e.offline)return;e.dispatch("error",{message:"Server responded with "+t.statusCode+". Stopping upload.",chunkNumber:e.chunkCount,attempts:e.attemptCount})}})).catch((function(t){e.paused||e.offline||e.manageRetries()}))},e}();t.UpChunk=a,t.createUpload=function(e){return new a(e)}},function(e,t,n){"use strict";Object.defineProperty(t,"__esModule",{value:!0});const r=new WeakMap,o=new WeakMap;function i(e){const t=r.get(e);return console.assert(null!=t,"'this' is expected an Event object, but got",e),t}function s(e){null==e.passiveListener?e.event.cancelable&&(e.canceled=!0,"function"==typeof e.event.preventDefault&&e.event.preventDefault()):"undefined"!=typeof console&&"function"==typeof console.error&&console.error("Unable to preventDefault inside passive event listener invocation.",e.passiveListener)}function u(e,t){r.set(this,{eventTarget:e,event:t,eventPhase:2,currentTarget:e,canceled:!1,stopped:!1,immediateStopped:!1,passiveListener:null,timeStamp:t.timeStamp||Date.now()}),Object.defineProperty(this,"isTrusted",{value:!1,enumerable:!0});const n=Object.keys(t);for(let e=0;e<n.length;++e){const t=n[e];t in this||Object.defineProperty(this,t,a(t))}}function a(e){return{get(){return i(this).event[e]},set(t){i(this).event[e]=t},configurable:!0,enumerable:!0}}function p(e){return{value(){const t=i(this).event;return t[e].apply(t,arguments)},configurable:!0,enumerable:!0}}function c(e){if(null==e||e===Object.prototype)return u;let t=o.get(e);return null==t&&(t=function(e,t){const n=Object.keys(t);if(0===n.length)return e;function r(t,n){e.call(this,t,n)}r.prototype=Object.create(e.prototype,{constructor:{value:r,configurable:!0,writable:!0}});for(let o=0;o<n.length;++o){const i=n[o];if(!(i in e.prototype)){const e="function"==typeof Object.getOwnPropertyDescriptor(t,i).value;Object.defineProperty(r.prototype,i,e?p(i):a(i))}}return r}(c(Object.getPrototypeOf(e)),e),o.set(e,t)),t}function l(e){return i(e).immediateStopped}function f(e,t){i(e).passiveListener=t}u.prototype={get type(){return i(this).event.type},get target(){return i(this).eventTarget},get currentTarget(){return i(this).currentTarget},composedPath(){const e=i(this).currentTarget;return null==e?[]:[e]},get NONE(){return 0},get CAPTURING_PHASE(){return 1},get AT_TARGET(){return 2},get BUBBLING_PHASE(){return 3},get eventPhase(){return i(this).eventPhase},stopPropagation(){const e=i(this);e.stopped=!0,"function"==typeof e.event.stopPropagation&&e.event.stopPropagation()},stopImmediatePropagation(){const e=i(this);e.stopped=!0,e.immediateStopped=!0,"function"==typeof e.event.stopImmediatePropagation&&e.event.stopImmediatePropagation()},get bubbles(){return Boolean(i(this).event.bubbles)},get cancelable(){return Boolean(i(this).event.cancelable)},preventDefault(){s(i(this))},get defaultPrevented(){return i(this).canceled},get composed(){return Boolean(i(this).event.composed)},get timeStamp(){return i(this).timeStamp},get srcElement(){return i(this).eventTarget},get cancelBubble(){return i(this).stopped},set cancelBubble(e){if(!e)return;const t=i(this);t.stopped=!0,"boolean"==typeof t.event.cancelBubble&&(t.event.cancelBubble=!0)},get returnValue(){return!i(this).canceled},set returnValue(e){e||s(i(this))},initEvent(){}},Object.defineProperty(u.prototype,"constructor",{value:u,configurable:!0,writable:!0}),"undefined"!=typeof window&&void 0!==window.Event&&(Object.setPrototypeOf(u.prototype,window.Event.prototype),o.set(window.Event.prototype,u));const h=new WeakMap;function d(e){return null!==e&&"object"==typeof e}function y(e){const t=h.get(e);if(null==t)throw new TypeError("'this' is expected an EventTarget object, but got another value.");return t}function v(e,t){Object.defineProperty(e,"on"+t,function(e){return{get(){let t=y(this).get(e);for(;null!=t;){if(3===t.listenerType)return t.listener;t=t.next}return null},set(t){"function"==typeof t||d(t)||(t=null);const n=y(this);let r=null,o=n.get(e);for(;null!=o;)3===o.listenerType?null!==r?r.next=o.next:null!==o.next?n.set(e,o.next):n.delete(e):r=o,o=o.next;if(null!==t){const o={listener:t,listenerType:3,passive:!1,once:!1,next:null};null===r?n.set(e,o):r.next=o}},configurable:!0,enumerable:!0}}(t))}function m(e){function t(){b.call(this)}t.prototype=Object.create(b.prototype,{constructor:{value:t,configurable:!0,writable:!0}});for(let n=0;n<e.length;++n)v(t.prototype,e[n]);return t}function b(){if(!(this instanceof b)){if(1===arguments.length&&Array.isArray(arguments[0]))return m(arguments[0]);if(arguments.length>0){const e=new Array(arguments.length);for(let t=0;t<arguments.length;++t)e[t]=arguments[t];return m(e)}throw new TypeError("Cannot call a class as a function")}h.set(this,new Map)}b.prototype={addEventListener(e,t,n){if(null==t)return;if("function"!=typeof t&&!d(t))throw new TypeError("'listener' should be a function or an object.");const r=y(this),o=d(n),i=(o?Boolean(n.capture):Boolean(n))?1:2,s={listener:t,listenerType:i,passive:o&&Boolean(n.passive),once:o&&Boolean(n.once),next:null};let u=r.get(e);if(void 0===u)return void r.set(e,s);let a=null;for(;null!=u;){if(u.listener===t&&u.listenerType===i)return;a=u,u=u.next}a.next=s},removeEventListener(e,t,n){if(null==t)return;const r=y(this),o=(d(n)?Boolean(n.capture):Boolean(n))?1:2;let i=null,s=r.get(e);for(;null!=s;){if(s.listener===t&&s.listenerType===o)return void(null!==i?i.next=s.next:null!==s.next?r.set(e,s.next):r.delete(e));i=s,s=s.next}},dispatchEvent(e){if(null==e||"string"!=typeof e.type)throw new TypeError('"event.type" should be a string.');const t=y(this),n=e.type;let r=t.get(n);if(null==r)return!0;const o=function(e,t){return new(c(Object.getPrototypeOf(t)))(e,t)}(this,e);let s=null;for(;null!=r;){if(r.once?null!==s?s.next=r.next:null!==r.next?t.set(n,r.next):t.delete(n):s=r,f(o,r.passive?r.listener:null),"function"==typeof r.listener)try{r.listener.call(this,o)}catch(e){"undefined"!=typeof console&&"function"==typeof console.error&&console.error(e)}else 3!==r.listenerType&&"function"==typeof r.listener.handleEvent&&r.listener.handleEvent(o);if(l(o))break;r=r.next}return f(o,null),function(e,t){i(e).eventPhase=t}(o,0),function(e,t){i(e).currentTarget=t}(o,null),!o.defaultPrevented}},Object.defineProperty(b.prototype,"constructor",{value:b,configurable:!0,writable:!0}),"undefined"!=typeof window&&void 0!==window.EventTarget&&Object.setPrototypeOf(b.prototype,window.EventTarget.prototype),t.defineEventAttribute=v,t.EventTarget=b,t.default=b,e.exports=b,e.exports.EventTarget=e.exports.default=b,e.exports.defineEventAttribute=v},function(e,t,n){"use strict";var r=n(3),o=n(5),i=n(6),s=n(7);function u(e,t,n){var r=e;return o(t)?(n=t,"string"==typeof e&&(r={uri:e})):r=s(t,{uri:e}),r.callback=n,r}function a(e,t,n){return p(t=u(e,t,n))}function p(e){if(void 0===e.callback)throw new Error("callback argument missing");var t=!1,n=function(n,r,o){t||(t=!0,e.callback(n,r,o))};function r(){var e=void 0;if(e=c.response?c.response:c.responseText||function(e){try{if("document"===e.responseType)return e.responseXML;var t=e.responseXML&&"parsererror"===e.responseXML.documentElement.nodeName;if(""===e.responseType&&!t)return e.responseXML}catch(e){}return null}(c),m)try{e=JSON.parse(e)}catch(e){}return e}function o(e){return clearTimeout(l),e instanceof Error||(e=new Error(""+(e||"Unknown XMLHttpRequest Error"))),e.statusCode=0,n(e,b)}function s(){if(!p){var t;clearTimeout(l),t=e.useXDR&&void 0===c.status?200:1223===c.status?204:c.status;var o=b,s=null;return 0!==t?(o={body:r(),statusCode:t,method:h,headers:{},url:f,rawRequest:c},c.getAllResponseHeaders&&(o.headers=i(c.getAllResponseHeaders()))):s=new Error("Internal XMLHttpRequest Error"),n(s,o,o.body)}}var u,p,c=e.xhr||null;c||(c=e.cors||e.useXDR?new a.XDomainRequest:new a.XMLHttpRequest);var l,f=c.url=e.uri||e.url,h=c.method=e.method||"GET",d=e.body||e.data,y=c.headers=e.headers||{},v=!!e.sync,m=!1,b={body:void 0,headers:{},statusCode:0,method:h,url:f,rawRequest:c};if("json"in e&&!1!==e.json&&(m=!0,y.accept||y.Accept||(y.Accept="application/json"),"GET"!==h&&"HEAD"!==h&&(y["content-type"]||y["Content-Type"]||(y["Content-Type"]="application/json"),d=JSON.stringify(!0===e.json?d:e.json))),c.onreadystatechange=function(){4===c.readyState&&setTimeout(s,0)},c.onload=s,c.onerror=o,c.onprogress=function(){},c.onabort=function(){p=!0},c.ontimeout=o,c.open(h,f,!v,e.username,e.password),v||(c.withCredentials=!!e.withCredentials),!v&&e.timeout>0&&(l=setTimeout((function(){if(!p){p=!0,c.abort("timeout");var e=new Error("XMLHttpRequest timeout");e.code="ETIMEDOUT",o(e)}}),e.timeout)),c.setRequestHeader)for(u in y)y.hasOwnProperty(u)&&c.setRequestHeader(u,y[u]);else if(e.headers&&!function(e){for(var t in e)if(e.hasOwnProperty(t))return!1;return!0}(e.headers))throw new Error("Headers cannot be set on an XDomainRequest object");return"responseType"in e&&(c.responseType=e.responseType),"beforeSend"in e&&"function"==typeof e.beforeSend&&e.beforeSend(c),c.send(d||null),c}e.exports=a,e.exports.default=a,a.XMLHttpRequest=r.XMLHttpRequest||function(){},a.XDomainRequest="withCredentials"in new a.XMLHttpRequest?a.XMLHttpRequest:r.XDomainRequest,function(e,t){for(var n=0;n<e.length;n++)t(e[n])}(["get","put","post","patch","head","delete"],(function(e){a["delete"===e?"del":e]=function(t,n,r){return(n=u(t,n,r)).method=e.toUpperCase(),p(n)}}))},function(e,t,n){(function(t){var n;n="undefined"!=typeof window?window:void 0!==t?t:"undefined"!=typeof self?self:{},e.exports=n}).call(this,n(4))},function(e,t){var n;n=function(){return this}();try{n=n||new Function("return this")()}catch(e){"object"==typeof window&&(n=window)}e.exports=n},function(e,t){e.exports=function(e){if(!e)return!1;var t=n.call(e);return"[object Function]"===t||"function"==typeof e&&"[object RegExp]"!==t||"undefined"!=typeof window&&(e===window.setTimeout||e===window.alert||e===window.confirm||e===window.prompt)};var n=Object.prototype.toString},function(e,t){var n=function(e){return e.replace(/^\s+|\s+$/g,"")};e.exports=function(e){if(!e)return{};for(var t,r={},o=n(e).split("\n"),i=0;i<o.length;i++){var s=o[i],u=s.indexOf(":"),a=n(s.slice(0,u)).toLowerCase(),p=n(s.slice(u+1));void 0===r[a]?r[a]=p:(t=r[a],"[object Array]"===Object.prototype.toString.call(t)?r[a].push(p):r[a]=[r[a],p])}return r}},function(e,t){e.exports=function(){for(var e={},t=0;t<arguments.length;t++){var r=arguments[t];for(var o in r)n.call(r,o)&&(e[o]=r[o])}return e};var n=Object.prototype.hasOwnProperty}])}));
//# sourceMappingURL=upchunk.js.map
{
"name": "@mux/upchunk",
"version": "2.2.1",
"version": "2.2.2",
"description": "Dead simple chunked file uploads using Fetch",

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

# Release
1. Get branch approved and merged
1. Pull the updated master branch locally
1. Run `npm version` - bump the version appropriately (this will commit and tag master). ex: `npm version 1.0.8`
1. Push commit to github w/ tags `git push origin --tags`
1. Run `yarn build`
1. Run `npm publish` (your npm account will need to have publish access)
1. After publishing, there will be a release in github with this tagname. Edit the release notes with any changes and attach the `.tgz` file that was created by `yarn build`.
1. Merge feature branches, bug fixes, and whatever changes into master after CI passes and PRs are approved
2. Create a new branch off master when you're ready to release a new version
3. Run `npm version` which will bump the version in `package.json` and make a tag (for example `npm version patch -m "Bump for 3.1.2"`). Follow SemVer rules for patch/minor/major.
4. Push the version commit and the tag `git push` && `git push --tags origin`
5. Open Pull Request, merge after approved. Make sure to "rebase and merge".
6. Create a new release in the Github UI, give the release a name and add release notes (creating the release will kick off npm publish)

@@ -225,2 +225,69 @@ import * as nock from 'nock';

const isNumberArraySorted = (a: number[]): boolean => {
for (let i = 0; i < a.length - 1; i += 1) {
if (a[i] > a[i + 1]) {
return false;
}
}
return true;
};
test('progress event fires the correct upload percentage', (done) => {
const fileBytes = 1048576;
const upload = createUploadFixture(
{},
new File([new ArrayBuffer(fileBytes)], 'test.mp4')
);
const scopes = [
nock('https://example.com')
.matchHeader('content-range', `bytes 0-${fileBytes / 4 - 1}/${fileBytes}`)
.put('/upload/endpoint')
.reply(200),
nock('https://example.com')
.matchHeader(
'content-range',
`bytes ${fileBytes / 4}-${fileBytes / 2 - 1}/${fileBytes}`
)
.put('/upload/endpoint')
.reply(200),
nock('https://example.com')
.matchHeader(
'content-range',
`bytes ${fileBytes / 2}-${3 * fileBytes / 4 - 1}/${fileBytes}`
)
.put('/upload/endpoint')
.reply(200),
nock('https://example.com')
.matchHeader(
'content-range',
`bytes ${3 * fileBytes / 4}-${fileBytes - 1}/${fileBytes}`
)
.put('/upload/endpoint')
.reply(200),
];
const progressCallback = jest.fn((percentage) => percentage);
upload.on('error', (err) => {
done(err);
});
upload.on('progress', (progress) => {
progressCallback(progress.detail);
});
upload.on('success', () => {
scopes.forEach((scope) => {
if (!scope.isDone()) {
done('All scopes not completed');
}
});
expect(progressCallback).toHaveBeenCalledTimes(7);
const progressPercentageArray = progressCallback.mock.calls.map(([percentage]) => percentage);
expect(isNumberArraySorted(progressPercentageArray)).toBeTruthy();
done();
});
}, 10000);
test('abort pauses the upload and cancels the current XHR request', (done) => {

@@ -227,0 +294,0 @@ /*

@@ -223,5 +223,8 @@ import { EventTarget } from 'event-target-shim';

xhrObject.upload.onprogress = (event: ProgressEvent) => {
const successfulPercentage = (100 / this.totalChunks) * this.chunkCount;
const chunkPercentage = (event.loaded / this.file.size) * 100;
this.dispatch('progress', successfulPercentage + chunkPercentage);
const percentagePerChunk = 100 / this.totalChunks;
const sizePerChunk = percentagePerChunk * this.file.size;
const successfulPercentage = percentagePerChunk * this.chunkCount;
const currentChunkProgress = event.loaded / (event.total ?? sizePerChunk);
const chunkPercentage = currentChunkProgress * percentagePerChunk;
this.dispatch('progress', Math.min(successfulPercentage + chunkPercentage, 100));
};

@@ -321,6 +324,7 @@ };

const percentProgress = Math.round(
(100 / this.totalChunks) * this.chunkCount
);
const chunkFraction = this.chunkCount / this.totalChunks;
const uploadedBytes = chunkFraction * this.file.size;
const percentProgress = (100 * uploadedBytes) / this.file.size;
this.dispatch('progress', percentProgress);

@@ -327,0 +331,0 @@ } else if (TEMPORARY_ERROR_CODES.includes(res.statusCode)) {

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