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

@mux/upchunk

Package Overview
Dependencies
Maintainers
17
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 1.0.8 to 1.0.9

README_INTERNAL.md

2

dist/upchunk.d.ts

@@ -31,3 +31,3 @@ declare type EventName = 'attempt' | 'attemptFailure' | 'error' | 'offline' | 'online' | 'progress' | 'success';

*/
on(eventName: EventName, fn: (event: CustomEvent) => void): void;
on(eventName: EventName, fn: (event: Event) => void): void;
pause(): void;

@@ -34,0 +34,0 @@ resume(): void;

@@ -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;var o=n(1),i=[200,201,202,204,308],s=[408,502,503,504],u=function(){function e(e){var t=this;this.endpoint=e.endpoint,this.file=e.file,this.headers=e.headers||{},this.chunkSize=e.chunkSize||5120,this.attempts=e.attempts||5,this.delayBeforeAttempt=e.delayBeforeAttempt||1,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.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.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.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.sendChunk=function(){var e=this.chunkCount*this.chunkByteSize,t=e+this.chunk.size-1,n=r({},this.headers,{"Content-Type":this.file.type,"Content-Length":this.chunk.size,"Content-Range":"bytes "+e+"-"+t+"/"+this.file.size});return this.dispatch("attempt",{chunkNumber:this.chunkCount,chunkSize:this.chunk.size}),fetch(this.endpointValue,{headers:n,method:"PUT",body:this.chunk})},e.prototype.manageRetries=function(){var e=this;if(this.attemptCount<this.attempts)return this.attemptCount=this.attemptCount+1,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.getChunk().then(function(){return e.sendChunk()}).then(function(t){if(i.includes(t.status)){e.chunkCount=e.chunkCount+1,e.chunkCount<e.totalChunks?e.sendChunks():e.dispatch("success");var n=Math.round(100/e.totalChunks*e.chunkCount);e.dispatch("progress",n)}else if(s.includes(t.status)){if(e.paused||e.offline)return;e.manageRetries()}else{if(e.paused||e.offline)return;e.dispatch("error",{message:"Server responded with "+t.status+". Stopping upload.",chunkNumber:e.chunkCount,attempts:e.attemptCount})}}).catch(function(t){e.paused||e.offline||e.manageRetries()})},e}();t.UpChunk=u,t.createUpload=function(e){return new u(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 l(e){return{value(){const t=i(this).event;return t[e].apply(t,arguments)},configurable:!0,enumerable:!0}}function p(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?l(i):a(i))}}return r}(p(Object.getPrototypeOf(e)),e),o.set(e,t)),t}function c(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,d=3;function y(e){return null!==e&&"object"==typeof e}function v(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 b(e,t){Object.defineProperty(e,`on${t}`,function(e){return{get(){let t=v(this).get(e);for(;null!=t;){if(t.listenerType===d)return t.listener;t=t.next}return null},set(t){"function"==typeof t||y(t)||(t=null);const n=v(this);let r=null,o=n.get(e);for(;null!=o;)o.listenerType===d?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:d,passive:!1,once:!1,next:null};null===r?n.set(e,o):r.next=o}},configurable:!0,enumerable:!0}}(t))}function g(e){function t(){m.call(this)}t.prototype=Object.create(m.prototype,{constructor:{value:t,configurable:!0,writable:!0}});for(let n=0;n<e.length;++n)b(t.prototype,e[n]);return t}function m(){if(!(this instanceof m)){if(1===arguments.length&&Array.isArray(arguments[0]))return g(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 g(e)}throw new TypeError("Cannot call a class as a function")}h.set(this,new Map)}m.prototype={addEventListener(e,t,n){if(null==t)return;if("function"!=typeof t&&!y(t))throw new TypeError("'listener' should be a function or an object.");const r=v(this),o=y(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=v(this),o=(y(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=v(this),n=e.type;let r=t.get(n);if(null==r)return!0;const o=function(e,t){return new(p(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 r.listenerType!==d&&"function"==typeof r.listener.handleEvent&&r.listener.handleEvent(o);if(c(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(m.prototype,"constructor",{value:m,configurable:!0,writable:!0}),"undefined"!=typeof window&&void 0!==window.EventTarget&&Object.setPrototypeOf(m.prototype,window.EventTarget.prototype),t.defineEventAttribute=b,t.EventTarget=m,t.default=m,e.exports=m,e.exports.EventTarget=e.exports.default=m,e.exports.defineEventAttribute=b}])});
!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()}(window,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;var o=n(1),i=[200,201,202,204,308],s=[408,502,503,504];console.log("oh hai there");var u=function(){function e(e){var t=this;this.endpoint=e.endpoint,this.file=e.file,this.headers=e.headers||{},this.chunkSize=e.chunkSize||5120,this.attempts=e.attempts||5,this.delayBeforeAttempt=e.delayBeforeAttempt||1,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.reader=new FileReader,this.eventTarget=new o.EventTarget,this.validateOptions(),this.getEndpoint().then(function(){return t.sendChunks()}),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.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.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;n=1===e.totalChunks?e.file.size:e.chunkCount+1===e.totalChunks?e.file.size-e.chunkCount*e.chunkByteSize:e.chunkByteSize;var r=e.chunkByteSize*e.chunkCount;e.reader.onload=function(){null!==e.reader.result&&(e.chunk=new Blob([e.reader.result],{type:"application/octet-stream"})),t({start:r,length:n})},e.reader.readAsArrayBuffer(e.file.slice(r,r+n))})},e.prototype.sendChunk=function(e){var t=e.start,n=t,o=t+e.length-1,i=r({},this.headers,{"Content-Type":this.file.type,"Content-Length":this.chunk.size,"Content-Range":"bytes "+n+"-"+o+"/"+this.file.size});return this.dispatch("attempt",{chunkNumber:this.chunkCount,chunkSize:this.chunk.size}),fetch(this.endpointValue,{headers:i,method:"PUT",mode:"cors",body:this.chunk})},e.prototype.manageRetries=function(){var e=this;if(this.attemptCount<this.attempts)return this.attemptCount=this.attemptCount+1,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.getChunk().then(function(t){return e.sendChunk(t)}).then(function(t){if(i.includes(t.status)){console.log(t.status),e.chunkCount=e.chunkCount+1,e.chunkCount<e.totalChunks?e.sendChunks():e.dispatch("success");var n=Math.round(100/e.totalChunks*e.chunkCount);e.dispatch("progress",n)}else if(s.includes(t.status)){if(e.paused||e.offline)return;e.manageRetries()}else{if(e.paused||e.offline)return;e.dispatch("error",{message:"Server responded with "+t.status+". Stopping upload.",chunkNumber:e.chunkCount,attempts:e.attemptCount})}}).catch(function(t){e.paused||e.offline||e.manageRetries()})},e}();t.UpChunk=u,t.createUpload=function(e){return new u(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 l(e){return{value(){const t=i(this).event;return t[e].apply(t,arguments)},configurable:!0,enumerable:!0}}function p(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?l(i):a(i))}}return r}(p(Object.getPrototypeOf(e)),e),o.set(e,t)),t}function c(e){return i(e).immediateStopped}function h(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 f=new WeakMap,d=3;function y(e){return null!==e&&"object"==typeof e}function v(e){const t=f.get(e);if(null==t)throw new TypeError("'this' is expected an EventTarget object, but got another value.");return t}function g(e,t){Object.defineProperty(e,`on${t}`,function(e){return{get(){let t=v(this).get(e);for(;null!=t;){if(t.listenerType===d)return t.listener;t=t.next}return null},set(t){"function"==typeof t||y(t)||(t=null);const n=v(this);let r=null,o=n.get(e);for(;null!=o;)o.listenerType===d?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:d,passive:!1,once:!1,next:null};null===r?n.set(e,o):r.next=o}},configurable:!0,enumerable:!0}}(t))}function b(e){function t(){m.call(this)}t.prototype=Object.create(m.prototype,{constructor:{value:t,configurable:!0,writable:!0}});for(let n=0;n<e.length;++n)g(t.prototype,e[n]);return t}function m(){if(!(this instanceof m)){if(1===arguments.length&&Array.isArray(arguments[0]))return b(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 b(e)}throw new TypeError("Cannot call a class as a function")}f.set(this,new Map)}m.prototype={addEventListener(e,t,n){if(null==t)return;if("function"!=typeof t&&!y(t))throw new TypeError("'listener' should be a function or an object.");const r=v(this),o=y(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=v(this),o=(y(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=v(this),n=e.type;let r=t.get(n);if(null==r)return!0;const o=function(e,t){return new(p(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,h(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 r.listenerType!==d&&"function"==typeof r.listener.handleEvent&&r.listener.handleEvent(o);if(c(o))break;r=r.next}return h(o,null),function(e,t){i(e).eventPhase=t}(o,0),function(e,t){i(e).currentTarget=t}(o,null),!o.defaultPrevented}},Object.defineProperty(m.prototype,"constructor",{value:m,configurable:!0,writable:!0}),"undefined"!=typeof window&&void 0!==window.EventTarget&&Object.setPrototypeOf(m.prototype,window.EventTarget.prototype),t.defineEventAttribute=g,t.EventTarget=m,t.default=m,e.exports=m,e.exports.EventTarget=e.exports.default=m,e.exports.defineEventAttribute=g}])});
//# sourceMappingURL=upchunk.js.map
{
"name": "@mux/upchunk",
"version": "1.0.8",
"version": "1.0.9",
"description": "Dead simple chunked file uploads using Fetch",

@@ -32,18 +32,22 @@ "main": "dist/upchunk.js",

"devDependencies": {
"@types/jest": "^23.3.10",
"jest": "^23.6.0",
"ts-jest": "^23.10.5",
"ts-loader": "^5.3.1",
"tslint": "^5.11.0",
"tslint-config-airbnb": "^5.11.1",
"tslint-config-prettier": "^1.17.0",
"typescript": "^3.2.2",
"webpack": "^4.27.1",
"webpack-cli": "^3.1.2",
"webpack-dev-server": "^3.1.10",
"webpack-merge": "^4.1.5"
"@types/jest": "^25.2.3",
"jest": "^26.0.1",
"ts-jest": "^26.1.0",
"ts-loader": "^7.0.5",
"tslint": "^6.1.2",
"tslint-config-airbnb": "^5.11.2",
"tslint-config-prettier": "^1.18.0",
"typescript": "^3.9.5",
"webpack": "^4.43.0",
"webpack-cli": "^3.3.11",
"webpack-dev-server": "^3.11.0",
"webpack-merge": "^4.2.2"
},
"dependencies": {
"event-target-shim": "^4.0.3"
"event-target-shim": "^4.0.3",
"xhr": "^2.5.0"
},
"volta": {
"node": "10.21.0"
}
}

@@ -32,3 +32,3 @@ ![UpChunk](banner.png)

```
<script src="https://unpkg.com/@mux/upchunk@1"></script>
<script src="https://unpkg.com/@mux/upchunk@2"></script>
```

@@ -163,3 +163,3 @@

Fired after successful chunk uploads and returns the current percentage of the file that's been uploaded (in terms of chunks).
Fired continuously with incremental upload progress. This returns the current percentage of the file that's been uploaded.

@@ -166,0 +166,0 @@ - `success`

/**
* This is more of an integration test. We can't test these in TS, because
* our handy dandy typechecks will make it so we can't compile with invalid
* our handy dandy typechecks will make it so we can't compile with invalid
* parameters. We don't have that luxury in normal JS, however, so make sure

@@ -5,0 +5,0 @@ * we still yell when we're supposed to.

import { EventTarget } from 'event-target-shim';
import xhr, { XhrUrlConfig, XhrHeaders, XhrResponse } from 'xhr';

@@ -18,3 +19,3 @@ const SUCCESSFUL_CHUNK_UPLOAD_CODES = [200, 201, 202, 204, 308];

file: File;
headers?: Headers;
headers?: XhrHeaders;
chunkSize?: number;

@@ -28,3 +29,3 @@ attempts?: number;

public file: File;
public headers: Headers;
public headers: XhrHeaders;
public chunkSize: number;

@@ -49,3 +50,3 @@ public attempts: number;

this.file = options.file;
this.headers = options.headers || ({} as Headers);
this.headers = options.headers || ({} as XhrHeaders);
this.chunkSize = options.chunkSize || 5120;

@@ -168,3 +169,3 @@ this.attempts = options.attempts || 5;

return this.endpoint(this.file).then(value => {
return this.endpoint(this.file).then((value) => {
this.endpointValue = value;

@@ -179,3 +180,3 @@ return this.endpointValue;

private getChunk() {
return new Promise(resolve => {
return new Promise((resolve) => {
// Since we start with 0-chunkSize for the range, we need to subtract 1.

@@ -199,2 +200,22 @@ const length =

private xhrPromise(options: XhrUrlConfig): Promise<XhrResponse> {
const beforeSend = (xhrObject: XMLHttpRequest) => {
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);
};
};
return new Promise((resolve, reject) => {
xhr({ ...options, beforeSend }, (err, resp) => {
if (err) {
return reject(err);
}
return resolve(resp);
});
});
}
/**

@@ -209,3 +230,2 @@ * Send chunk of the file with appropriate headers and add post parameters if it's last chunk

'Content-Type': this.file.type,
'Content-Length': this.chunk.size,
'Content-Range': `bytes ${rangeStart}-${rangeEnd}/${this.file.size}`,

@@ -219,4 +239,5 @@ };

return fetch(this.endpointValue, {
return this.xhrPromise({
headers,
url: this.endpointValue,
method: 'PUT',

@@ -235,4 +256,5 @@ body: this.chunk,

this.dispatch('attemptFailure', {
message: `An error occured uploading chunk ${this.chunkCount}. ${this
.attempts - this.attemptCount} retries left.`,
message: `An error occured uploading chunk ${this.chunkCount}. ${
this.attempts - this.attemptCount
} retries left.`,
chunkNumber: this.chunkCount,

@@ -245,5 +267,3 @@ attemptsLeft: this.attempts - this.attemptCount,

this.dispatch('error', {
message: `An error occured uploading chunk ${
this.chunkCount
}. No more retries, stopping upload`,
message: `An error occured uploading chunk ${this.chunkCount}. No more retries, stopping upload`,
chunk: this.chunkCount,

@@ -265,4 +285,4 @@ attempts: this.attemptCount,

.then(() => this.sendChunk())
.then(res => {
if (SUCCESSFUL_CHUNK_UPLOAD_CODES.includes(res.status)) {
.then((res) => {
if (SUCCESSFUL_CHUNK_UPLOAD_CODES.includes(res.statusCode)) {
this.chunkCount = this.chunkCount + 1;

@@ -280,3 +300,3 @@ if (this.chunkCount < this.totalChunks) {

this.dispatch('progress', percentProgress);
} else if (TEMPORARY_ERROR_CODES.includes(res.status)) {
} else if (TEMPORARY_ERROR_CODES.includes(res.statusCode)) {
if (this.paused || this.offline) {

@@ -292,3 +312,3 @@ return;

this.dispatch('error', {
message: `Server responded with ${res.status}. Stopping upload.`,
message: `Server responded with ${res.statusCode}. Stopping upload.`,
chunkNumber: this.chunkCount,

@@ -299,3 +319,3 @@ attempts: this.attemptCount,

})
.catch(err => {
.catch((err) => {
if (this.paused || this.offline) {

@@ -302,0 +322,0 @@ return;

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