@getanthill/event-source
Advanced tools
Comparing version 0.1.0 to 0.1.1
@@ -1,1 +0,1 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("assert");var e=function(){this.__data__=[],this.size=0};var r=function(t,e){return t===e||t!=t&&e!=e};var n=function(t,e){for(var n=t.length;n--;)if(r(t[n][0],e))return n;return-1},o=Array.prototype.splice;var a=function(t){var e=this.__data__,r=n(e,t);return!(r<0)&&(r==e.length-1?e.pop():o.call(e,r,1),--this.size,!0)};var i=function(t){var e=this.__data__,r=n(e,t);return r<0?void 0:e[r][1]};var c=function(t){return n(this.__data__,t)>-1};var s=function(t,e){var r=this.__data__,o=n(r,t);return o<0?(++this.size,r.push([t,e])):r[o][1]=e,this};function u(t){var e=-1,r=null==t?0:t.length;for(this.clear();++e<r;){var n=t[e];this.set(n[0],n[1])}}u.prototype.clear=e,u.prototype.delete=a,u.prototype.get=i,u.prototype.has=c,u.prototype.set=s;var l=u;var f=function(){this.__data__=new l,this.size=0};var p=function(t){var e=this.__data__,r=e.delete(t);return this.size=e.size,r};var v=function(t){return this.__data__.get(t)};var b=function(t){return this.__data__.has(t)},d="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{};function h(t,e,r){return t(r={path:e,exports:{},require:function(t,e){return function(){throw new Error("Dynamic requires are not currently supported by @rollup/plugin-commonjs")}(null==e&&r.path)}},r.exports),r.exports}var y="object"==typeof d&&d&&d.Object===Object&&d,_="object"==typeof self&&self&&self.Object===Object&&self,j=y||_||Function("return this")(),g=j.Symbol,O=Object.prototype,E=O.hasOwnProperty,I=O.toString,S=g?g.toStringTag:void 0;var A=function(t){var e=E.call(t,S),r=t[S];try{t[S]=void 0;var n=!0}catch(t){}var o=I.call(t);return n&&(e?t[S]=r:delete t[S]),o},w=Object.prototype.toString;var R=function(t){return w.call(t)},m=g?g.toStringTag:void 0;var T=function(t){return null==t?void 0===t?"[object Undefined]":"[object Null]":m&&m in Object(t)?A(t):R(t)};var N=function(t){var e=typeof t;return null!=t&&("object"==e||"function"==e)};var C,L=function(t){if(!N(t))return!1;var e=T(t);return"[object Function]"==e||"[object GeneratorFunction]"==e||"[object AsyncFunction]"==e||"[object Proxy]"==e},x=j["__core-js_shared__"],D=(C=/[^.]+$/.exec(x&&x.keys&&x.keys.IE_PROTO||""))?"Symbol(src)_1."+C:"";var F=function(t){return!!D&&D in t},P=Function.prototype.toString;var M=function(t){if(null!=t){try{return P.call(t)}catch(t){}try{return t+""}catch(t){}}return""},z=/^\[object .+?Constructor\]$/,V=Function.prototype,U=Object.prototype,q=V.toString,B=U.hasOwnProperty,H=RegExp("^"+q.call(B).replace(/[\\^$.*+?()[\]{}|]/g,"\\$&").replace(/hasOwnProperty|(function).*?(?=\\\()| for .+?(?=\\\])/g,"$1.*?")+"$");var $=function(t){return!(!N(t)||F(t))&&(L(t)?H:z).test(M(t))};var k=function(t,e){return null==t?void 0:t[e]};var W=function(t,e){var r=k(t,e);return $(r)?r:void 0},G=W(j,"Map"),Y=W(Object,"create");var J=function(){this.__data__=Y?Y(null):{},this.size=0};var K=function(t){var e=this.has(t)&&delete this.__data__[t];return this.size-=e?1:0,e},Q=Object.prototype.hasOwnProperty;var X=function(t){var e=this.__data__;if(Y){var r=e[t];return"__lodash_hash_undefined__"===r?void 0:r}return Q.call(e,t)?e[t]:void 0},Z=Object.prototype.hasOwnProperty;var tt=function(t){var e=this.__data__;return Y?void 0!==e[t]:Z.call(e,t)};var et=function(t,e){var r=this.__data__;return this.size+=this.has(t)?0:1,r[t]=Y&&void 0===e?"__lodash_hash_undefined__":e,this};function rt(t){var e=-1,r=null==t?0:t.length;for(this.clear();++e<r;){var n=t[e];this.set(n[0],n[1])}}rt.prototype.clear=J,rt.prototype.delete=K,rt.prototype.get=X,rt.prototype.has=tt,rt.prototype.set=et;var nt=rt;var ot=function(){this.size=0,this.__data__={hash:new nt,map:new(G||l),string:new nt}};var at=function(t){var e=typeof t;return"string"==e||"number"==e||"symbol"==e||"boolean"==e?"__proto__"!==t:null===t};var it=function(t,e){var r=t.__data__;return at(e)?r["string"==typeof e?"string":"hash"]:r.map};var ct=function(t){var e=it(this,t).delete(t);return this.size-=e?1:0,e};var st=function(t){return it(this,t).get(t)};var ut=function(t){return it(this,t).has(t)};var lt=function(t,e){var r=it(this,t),n=r.size;return r.set(t,e),this.size+=r.size==n?0:1,this};function ft(t){var e=-1,r=null==t?0:t.length;for(this.clear();++e<r;){var n=t[e];this.set(n[0],n[1])}}ft.prototype.clear=ot,ft.prototype.delete=ct,ft.prototype.get=st,ft.prototype.has=ut,ft.prototype.set=lt;var pt=ft;var vt=function(t,e){var r=this.__data__;if(r instanceof l){var n=r.__data__;if(!G||n.length<199)return n.push([t,e]),this.size=++r.size,this;r=this.__data__=new pt(n)}return r.set(t,e),this.size=r.size,this};function bt(t){var e=this.__data__=new l(t);this.size=e.size}bt.prototype.clear=f,bt.prototype.delete=p,bt.prototype.get=v,bt.prototype.has=b,bt.prototype.set=vt;var dt=bt;var ht=function(t,e){for(var r=-1,n=null==t?0:t.length;++r<n&&!1!==e(t[r],r,t););return t},yt=function(){try{var t=W(Object,"defineProperty");return t({},"",{}),t}catch(t){}}();var _t=function(t,e,r){"__proto__"==e&&yt?yt(t,e,{configurable:!0,enumerable:!0,value:r,writable:!0}):t[e]=r},jt=Object.prototype.hasOwnProperty;var gt=function(t,e,n){var o=t[e];jt.call(t,e)&&r(o,n)&&(void 0!==n||e in t)||_t(t,e,n)};var Ot=function(t,e,r,n){var o=!r;r||(r={});for(var a=-1,i=e.length;++a<i;){var c=e[a],s=n?n(r[c],t[c],c,r,t):void 0;void 0===s&&(s=t[c]),o?_t(r,c,s):gt(r,c,s)}return r};var Et=function(t,e){for(var r=-1,n=Array(t);++r<t;)n[r]=e(r);return n};var It=function(t){return null!=t&&"object"==typeof t};var St=function(t){return It(t)&&"[object Arguments]"==T(t)},At=Object.prototype,wt=At.hasOwnProperty,Rt=At.propertyIsEnumerable,mt=St(function(){return arguments}())?St:function(t){return It(t)&&wt.call(t,"callee")&&!Rt.call(t,"callee")},Tt=Array.isArray;var Nt=function(){return!1},Ct=h((function(t,e){var r=e&&!e.nodeType&&e,n=r&&t&&!t.nodeType&&t,o=n&&n.exports===r?j.Buffer:void 0,a=(o?o.isBuffer:void 0)||Nt;t.exports=a})),Lt=/^(?:0|[1-9]\d*)$/;var xt=function(t,e){var r=typeof t;return!!(e=null==e?9007199254740991:e)&&("number"==r||"symbol"!=r&&Lt.test(t))&&t>-1&&t%1==0&&t<e};var Dt=function(t){return"number"==typeof t&&t>-1&&t%1==0&&t<=9007199254740991},Ft={};Ft["[object Float32Array]"]=Ft["[object Float64Array]"]=Ft["[object Int8Array]"]=Ft["[object Int16Array]"]=Ft["[object Int32Array]"]=Ft["[object Uint8Array]"]=Ft["[object Uint8ClampedArray]"]=Ft["[object Uint16Array]"]=Ft["[object Uint32Array]"]=!0,Ft["[object Arguments]"]=Ft["[object Array]"]=Ft["[object ArrayBuffer]"]=Ft["[object Boolean]"]=Ft["[object DataView]"]=Ft["[object Date]"]=Ft["[object Error]"]=Ft["[object Function]"]=Ft["[object Map]"]=Ft["[object Number]"]=Ft["[object Object]"]=Ft["[object RegExp]"]=Ft["[object Set]"]=Ft["[object String]"]=Ft["[object WeakMap]"]=!1;var Pt=function(t){return It(t)&&Dt(t.length)&&!!Ft[T(t)]};var Mt=function(t){return function(e){return t(e)}},zt=h((function(t,e){var r=e&&!e.nodeType&&e,n=r&&t&&!t.nodeType&&t,o=n&&n.exports===r&&y.process,a=function(){try{var t=n&&n.require&&n.require("util").types;return t||o&&o.binding&&o.binding("util")}catch(t){}}();t.exports=a})),Vt=zt&&zt.isTypedArray,Ut=Vt?Mt(Vt):Pt,qt=Object.prototype.hasOwnProperty;var Bt=function(t,e){var r=Tt(t),n=!r&&mt(t),o=!r&&!n&&Ct(t),a=!r&&!n&&!o&&Ut(t),i=r||n||o||a,c=i?Et(t.length,String):[],s=c.length;for(var u in t)!e&&!qt.call(t,u)||i&&("length"==u||o&&("offset"==u||"parent"==u)||a&&("buffer"==u||"byteLength"==u||"byteOffset"==u)||xt(u,s))||c.push(u);return c},Ht=Object.prototype;var $t=function(t){var e=t&&t.constructor;return t===("function"==typeof e&&e.prototype||Ht)};var kt=function(t,e){return function(r){return t(e(r))}},Wt=kt(Object.keys,Object),Gt=Object.prototype.hasOwnProperty;var Yt=function(t){if(!$t(t))return Wt(t);var e=[];for(var r in Object(t))Gt.call(t,r)&&"constructor"!=r&&e.push(r);return e};var Jt=function(t){return null!=t&&Dt(t.length)&&!L(t)};var Kt=function(t){return Jt(t)?Bt(t):Yt(t)};var Qt=function(t,e){return t&&Ot(e,Kt(e),t)};var Xt=function(t){var e=[];if(null!=t)for(var r in Object(t))e.push(r);return e},Zt=Object.prototype.hasOwnProperty;var te=function(t){if(!N(t))return Xt(t);var e=$t(t),r=[];for(var n in t)("constructor"!=n||!e&&Zt.call(t,n))&&r.push(n);return r};var ee=function(t){return Jt(t)?Bt(t,!0):te(t)};var re=function(t,e){return t&&Ot(e,ee(e),t)},ne=h((function(t,e){var r=e&&!e.nodeType&&e,n=r&&t&&!t.nodeType&&t,o=n&&n.exports===r?j.Buffer:void 0,a=o?o.allocUnsafe:void 0;t.exports=function(t,e){if(e)return t.slice();var r=t.length,n=a?a(r):new t.constructor(r);return t.copy(n),n}}));var oe=function(t,e){var r=-1,n=t.length;for(e||(e=Array(n));++r<n;)e[r]=t[r];return e};var ae=function(t,e){for(var r=-1,n=null==t?0:t.length,o=0,a=[];++r<n;){var i=t[r];e(i,r,t)&&(a[o++]=i)}return a};var ie=function(){return[]},ce=Object.prototype.propertyIsEnumerable,se=Object.getOwnPropertySymbols,ue=se?function(t){return null==t?[]:(t=Object(t),ae(se(t),(function(e){return ce.call(t,e)})))}:ie;var le=function(t,e){return Ot(t,ue(t),e)};var fe=function(t,e){for(var r=-1,n=e.length,o=t.length;++r<n;)t[o+r]=e[r];return t},pe=kt(Object.getPrototypeOf,Object),ve=Object.getOwnPropertySymbols?function(t){for(var e=[];t;)fe(e,ue(t)),t=pe(t);return e}:ie;var be=function(t,e){return Ot(t,ve(t),e)};var de=function(t,e,r){var n=e(t);return Tt(t)?n:fe(n,r(t))};var he=function(t){return de(t,Kt,ue)};var ye=function(t){return de(t,ee,ve)},_e=W(j,"DataView"),je=W(j,"Promise"),ge=W(j,"Set"),Oe=W(j,"WeakMap"),Ee=M(_e),Ie=M(G),Se=M(je),Ae=M(ge),we=M(Oe),Re=T;(_e&&"[object DataView]"!=Re(new _e(new ArrayBuffer(1)))||G&&"[object Map]"!=Re(new G)||je&&"[object Promise]"!=Re(je.resolve())||ge&&"[object Set]"!=Re(new ge)||Oe&&"[object WeakMap]"!=Re(new Oe))&&(Re=function(t){var e=T(t),r="[object Object]"==e?t.constructor:void 0,n=r?M(r):"";if(n)switch(n){case Ee:return"[object DataView]";case Ie:return"[object Map]";case Se:return"[object Promise]";case Ae:return"[object Set]";case we:return"[object WeakMap]"}return e});var me=Re,Te=Object.prototype.hasOwnProperty;var Ne=function(t){var e=t.length,r=new t.constructor(e);return e&&"string"==typeof t[0]&&Te.call(t,"index")&&(r.index=t.index,r.input=t.input),r},Ce=j.Uint8Array;var Le=function(t){var e=new t.constructor(t.byteLength);return new Ce(e).set(new Ce(t)),e};var xe=function(t,e){var r=e?Le(t.buffer):t.buffer;return new t.constructor(r,t.byteOffset,t.byteLength)},De=/\w*$/;var Fe=function(t){var e=new t.constructor(t.source,De.exec(t));return e.lastIndex=t.lastIndex,e},Pe=g?g.prototype:void 0,Me=Pe?Pe.valueOf:void 0;var ze=function(t){return Me?Object(Me.call(t)):{}};var Ve=function(t,e){var r=e?Le(t.buffer):t.buffer;return new t.constructor(r,t.byteOffset,t.length)};var Ue=function(t,e,r){var n=t.constructor;switch(e){case"[object ArrayBuffer]":return Le(t);case"[object Boolean]":case"[object Date]":return new n(+t);case"[object DataView]":return xe(t,r);case"[object Float32Array]":case"[object Float64Array]":case"[object Int8Array]":case"[object Int16Array]":case"[object Int32Array]":case"[object Uint8Array]":case"[object Uint8ClampedArray]":case"[object Uint16Array]":case"[object Uint32Array]":return Ve(t,r);case"[object Map]":return new n;case"[object Number]":case"[object String]":return new n(t);case"[object RegExp]":return Fe(t);case"[object Set]":return new n;case"[object Symbol]":return ze(t)}},qe=Object.create,Be=function(){function t(){}return function(e){if(!N(e))return{};if(qe)return qe(e);t.prototype=e;var r=new t;return t.prototype=void 0,r}}();var He=function(t){return"function"!=typeof t.constructor||$t(t)?{}:Be(pe(t))};var $e=function(t){return It(t)&&"[object Map]"==me(t)},ke=zt&&zt.isMap,We=ke?Mt(ke):$e;var Ge=function(t){return It(t)&&"[object Set]"==me(t)},Ye=zt&&zt.isSet,Je=Ye?Mt(Ye):Ge,Ke={};Ke["[object Arguments]"]=Ke["[object Array]"]=Ke["[object ArrayBuffer]"]=Ke["[object DataView]"]=Ke["[object Boolean]"]=Ke["[object Date]"]=Ke["[object Float32Array]"]=Ke["[object Float64Array]"]=Ke["[object Int8Array]"]=Ke["[object Int16Array]"]=Ke["[object Int32Array]"]=Ke["[object Map]"]=Ke["[object Number]"]=Ke["[object Object]"]=Ke["[object RegExp]"]=Ke["[object Set]"]=Ke["[object String]"]=Ke["[object Symbol]"]=Ke["[object Uint8Array]"]=Ke["[object Uint8ClampedArray]"]=Ke["[object Uint16Array]"]=Ke["[object Uint32Array]"]=!0,Ke["[object Error]"]=Ke["[object Function]"]=Ke["[object WeakMap]"]=!1;var Qe=function t(e,r,n,o,a,i){var c,s=1&r,u=2&r,l=4&r;if(n&&(c=a?n(e,o,a,i):n(e)),void 0!==c)return c;if(!N(e))return e;var f=Tt(e);if(f){if(c=Ne(e),!s)return oe(e,c)}else{var p=me(e),v="[object Function]"==p||"[object GeneratorFunction]"==p;if(Ct(e))return ne(e,s);if("[object Object]"==p||"[object Arguments]"==p||v&&!a){if(c=u||v?{}:He(e),!s)return u?be(e,re(c,e)):le(e,Qt(c,e))}else{if(!Ke[p])return a?e:{};c=Ue(e,p,s)}}i||(i=new dt);var b=i.get(e);if(b)return b;i.set(e,c),Je(e)?e.forEach((function(o){c.add(t(o,r,n,o,e,i))})):We(e)&&e.forEach((function(o,a){c.set(a,t(o,r,n,a,e,i))}));var d=f?void 0:(l?u?ye:he:u?ee:Kt)(e);return ht(d||e,(function(o,a){d&&(o=e[a=o]),gt(c,a,t(o,r,n,a,e,i))})),c};var Xe=function(t){return Qe(t,5)};function Ze(t){return Object.getOwnPropertyNames(t||{}).forEach((e=>{const r=t[e];r instanceof Buffer||"object"==typeof r&&null!==r&&Ze(r)})),Object.freeze(t)}class tr{static initIndexes(t,e){return Promise.all(tr.getRequiredIndexes(e).map((([e,r])=>t.createIndex(e,r))))}static getRequiredIndexes(t){return[[{[t]:1,version:1},{unique:!0,name:"correlation_id_version_unicity"}]]}static getEvents(e,r,n,o){return t.strict(r,"[Events#getEvents] Missing correlationField"),t.strict(n,"[Events#getEvents] Missing correlationId"),t.strict("number"==typeof o,"[Events#getEvents] Invalid version (must be a number)"),e.find({[r]:n,version:{$gt:o}},{projection:{_id:0,[r]:0,created_at:0}}).sort({version:1})}static store(t,e){const r=e.map((t=>({...t,created_at:new Date})));return t.insertMany(r)}}class er{static initIndexes(t,e){return Promise.all(er.getRequiredIndexes(e).map((([e,r])=>t.createIndex(e,r))))}static getRequiredIndexes(t){return[[{[t]:1,version:1},{unique:!0,name:"correlation_id_version_unicity"}]]}static getSnapshot(e,r,n,o){return t.strict(r,"[Snapshots#getSnapshot] Missing correlationField"),t.strict(n,"[Snapshots#getSnapshot] Missing correlationId"),t.strict("number"==typeof o,"[Snapshots#getSnapshot] Invalid version (must be a number)"),e.findOne({[r]:n,version:{$lte:o}},{sort:{version:-1},fields:{_id:0}})}static create(t,e,r){return t.findOneAndUpdate({[e]:r[e],version:r.version},{$setOnInsert:r},{projection:{_id:0},upsert:!0})}}const rr=new Error("An event must not have a version before its reduction"),nr=new Error("Retry duration reached");var or=Object.freeze({__proto__:null,INCOMPATIBLE_EVENT_VERSION_WITH_STATE_VERSION:rr,HANDLER_RETRY_TIMEOUT:nr});exports.ERRORS=or,exports.EventSourcedFactory=function(e,r,n={}){return class o{constructor({db:t,logger:e},r){this.logger=null,this.correlationId=null,this.db=t,this.logger=e,this.correlationId=r,this.defaultStoreStateErrorHandler=this.defaultStoreStateErrorHandler.bind(this)}static get options(){return Object.assign({CORRELATION_FIELD:"correlation_id",EVENTS_COLLECTION_NAME:e+"_events",SNAPSHOTS_COLLECTION_NAME:e+"_snapshots"},n)}static get ERRORS(){return or}static getCollection(t,e){return t.collection(e)}static getStatesCollection(t){return o.getCollection(t,e)}static getEventsCollection(t){return o.getCollection(t,o.options.EVENTS_COLLECTION_NAME)}static getSnapshotsCollection(t){return o.getCollection(t,o.options.SNAPSHOTS_COLLECTION_NAME)}static initIndexes(t){return Promise.all([...o.getRequiredIndexes()[e].map((([e,r])=>o.getStatesCollection(t).createIndex(e,r))),tr.initIndexes(o.getEventsCollection(t),o.options.CORRELATION_FIELD),er.initIndexes(o.getSnapshotsCollection(t),o.options.CORRELATION_FIELD)])}static getRequiredIndexes(){return{[e]:[[{[o.options.CORRELATION_FIELD]:1},{unique:!0,name:"correlation_id_unicity"}]],[o.options.EVENTS_COLLECTION_NAME]:tr.getRequiredIndexes(o.options.CORRELATION_FIELD),[o.options.SNAPSHOTS_COLLECTION_NAME]:er.getRequiredIndexes(o.options.CORRELATION_FIELD)}}static getVersion(t){return t&&void 0!==t.version?t.version:-1}static getEvents(t,e,r){return tr.getEvents(o.getEventsCollection(t),o.options.CORRELATION_FIELD,e,r)}static reduce(e,n,a){t.strict(a,"[EventSourced#reduce] correlationId must be defined");const i=Xe(e);return[i.reduce(((t,e)=>{const n=o.getVersion(e),i=o.getVersion(t);if(-1!==n&&n-i!=1)throw o.ERRORS.INCOMPATIBLE_EVENT_VERSION_WITH_STATE_VERSION;return(t=r(t,e)).version=i+1,e.version=o.getVersion(t),t[o.options.CORRELATION_FIELD]=a,e[o.options.CORRELATION_FIELD]=a,Ze(t)}),Ze(Xe(n))),i]}static async getState(t,e){const r=await o.getStatesCollection(t).findOne({[o.options.CORRELATION_FIELD]:e},{projection:{_id:0}}),n=await o.getEvents(t,e,o.getVersion(r));let a=Xe(r);for(;await n.hasNext();){const t=await n.next(),[r]=o.reduce([t],a,e);a=r}return{stateInDb:Ze(r),currentState:Ze(a)}}static storeEvents(t,e){return tr.store(o.getEventsCollection(t),e)}static storeState(t,e,r){return o.getStatesCollection(t).findOneAndReplace({[o.options.CORRELATION_FIELD]:e[o.options.CORRELATION_FIELD],version:r},e,{upsert:!0})}static async createSnapshot(t,e){const{stateInDb:r}=await o.getState(t,e);return await er.create(o.getSnapshotsCollection(t),o.options.CORRELATION_FIELD,r),r}defaultStoreStateErrorHandler(t,e){this.logger.info({err:t,updatedState:e},"Error occured when attempted to save the state")}async handle(e,r){r=r||this.defaultStoreStateErrorHandler,t.strict("function"==typeof r,"storeStateErrorHandler must be a function");const{stateInDb:n,currentState:a}=await o.getState(this.db,this.correlationId),i=await e(a),[c,s]=o.reduce(i,a,this.correlationId);await o.storeEvents(this.db,s);try{await o.storeState(this.db,c,o.getVersion(n))}catch(t){r(t,c)}return c}async handleWithRetry(e,r,n){t.strict("number"==typeof r,"retryDuration must be a number");const a=Date.now();for(;Date.now()-a<r;)try{return await this.handle(e,n)}catch(t){this.logger.error({err:t},"Error while handling the command")}throw o.ERRORS.HANDLER_RETRY_TIMEOUT}createSnapshot(){return o.createSnapshot(this.db,this.correlationId)}}},exports.Events=tr,exports.Snapshots=er; | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.ERRORS=exports.Snapshots=exports.Events=exports.EventSourcedFactory=void 0;var e=require("./EventSourced");Object.defineProperty(exports,"EventSourcedFactory",{enumerable:!0,get:function(){return e.EventSourcedFactory}});var r=require("./Events");Object.defineProperty(exports,"Events",{enumerable:!0,get:function(){return r.Events}});var t=require("./Snapshots");Object.defineProperty(exports,"Snapshots",{enumerable:!0,get:function(){return t.Snapshots}});const o=require("./errors");exports.ERRORS=o; |
@@ -1,1 +0,1 @@ | ||
import{strict as t}from"assert";var e=function(){this.__data__=[],this.size=0};var r=function(t,e){return t===e||t!=t&&e!=e};var n=function(t,e){for(var n=t.length;n--;)if(r(t[n][0],e))return n;return-1},o=Array.prototype.splice;var a=function(t){var e=this.__data__,r=n(e,t);return!(r<0)&&(r==e.length-1?e.pop():o.call(e,r,1),--this.size,!0)};var i=function(t){var e=this.__data__,r=n(e,t);return r<0?void 0:e[r][1]};var c=function(t){return n(this.__data__,t)>-1};var s=function(t,e){var r=this.__data__,o=n(r,t);return o<0?(++this.size,r.push([t,e])):r[o][1]=e,this};function u(t){var e=-1,r=null==t?0:t.length;for(this.clear();++e<r;){var n=t[e];this.set(n[0],n[1])}}u.prototype.clear=e,u.prototype.delete=a,u.prototype.get=i,u.prototype.has=c,u.prototype.set=s;var l=u;var f=function(){this.__data__=new l,this.size=0};var p=function(t){var e=this.__data__,r=e.delete(t);return this.size=e.size,r};var v=function(t){return this.__data__.get(t)};var b=function(t){return this.__data__.has(t)},d="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{};function h(t,e,r){return t(r={path:e,exports:{},require:function(t,e){return function(){throw new Error("Dynamic requires are not currently supported by @rollup/plugin-commonjs")}(null==e&&r.path)}},r.exports),r.exports}var y="object"==typeof d&&d&&d.Object===Object&&d,_="object"==typeof self&&self&&self.Object===Object&&self,j=y||_||Function("return this")(),g=j.Symbol,O=Object.prototype,E=O.hasOwnProperty,I=O.toString,S=g?g.toStringTag:void 0;var A=function(t){var e=E.call(t,S),r=t[S];try{t[S]=void 0;var n=!0}catch(t){}var o=I.call(t);return n&&(e?t[S]=r:delete t[S]),o},w=Object.prototype.toString;var R=function(t){return w.call(t)},m=g?g.toStringTag:void 0;var T=function(t){return null==t?void 0===t?"[object Undefined]":"[object Null]":m&&m in Object(t)?A(t):R(t)};var N=function(t){var e=typeof t;return null!=t&&("object"==e||"function"==e)};var C,L=function(t){if(!N(t))return!1;var e=T(t);return"[object Function]"==e||"[object GeneratorFunction]"==e||"[object AsyncFunction]"==e||"[object Proxy]"==e},x=j["__core-js_shared__"],D=(C=/[^.]+$/.exec(x&&x.keys&&x.keys.IE_PROTO||""))?"Symbol(src)_1."+C:"";var F=function(t){return!!D&&D in t},P=Function.prototype.toString;var M=function(t){if(null!=t){try{return P.call(t)}catch(t){}try{return t+""}catch(t){}}return""},z=/^\[object .+?Constructor\]$/,V=Function.prototype,U=Object.prototype,q=V.toString,B=U.hasOwnProperty,H=RegExp("^"+q.call(B).replace(/[\\^$.*+?()[\]{}|]/g,"\\$&").replace(/hasOwnProperty|(function).*?(?=\\\()| for .+?(?=\\\])/g,"$1.*?")+"$");var $=function(t){return!(!N(t)||F(t))&&(L(t)?H:z).test(M(t))};var k=function(t,e){return null==t?void 0:t[e]};var W=function(t,e){var r=k(t,e);return $(r)?r:void 0},G=W(j,"Map"),Y=W(Object,"create");var J=function(){this.__data__=Y?Y(null):{},this.size=0};var K=function(t){var e=this.has(t)&&delete this.__data__[t];return this.size-=e?1:0,e},Q=Object.prototype.hasOwnProperty;var X=function(t){var e=this.__data__;if(Y){var r=e[t];return"__lodash_hash_undefined__"===r?void 0:r}return Q.call(e,t)?e[t]:void 0},Z=Object.prototype.hasOwnProperty;var tt=function(t){var e=this.__data__;return Y?void 0!==e[t]:Z.call(e,t)};var et=function(t,e){var r=this.__data__;return this.size+=this.has(t)?0:1,r[t]=Y&&void 0===e?"__lodash_hash_undefined__":e,this};function rt(t){var e=-1,r=null==t?0:t.length;for(this.clear();++e<r;){var n=t[e];this.set(n[0],n[1])}}rt.prototype.clear=J,rt.prototype.delete=K,rt.prototype.get=X,rt.prototype.has=tt,rt.prototype.set=et;var nt=rt;var ot=function(){this.size=0,this.__data__={hash:new nt,map:new(G||l),string:new nt}};var at=function(t){var e=typeof t;return"string"==e||"number"==e||"symbol"==e||"boolean"==e?"__proto__"!==t:null===t};var it=function(t,e){var r=t.__data__;return at(e)?r["string"==typeof e?"string":"hash"]:r.map};var ct=function(t){var e=it(this,t).delete(t);return this.size-=e?1:0,e};var st=function(t){return it(this,t).get(t)};var ut=function(t){return it(this,t).has(t)};var lt=function(t,e){var r=it(this,t),n=r.size;return r.set(t,e),this.size+=r.size==n?0:1,this};function ft(t){var e=-1,r=null==t?0:t.length;for(this.clear();++e<r;){var n=t[e];this.set(n[0],n[1])}}ft.prototype.clear=ot,ft.prototype.delete=ct,ft.prototype.get=st,ft.prototype.has=ut,ft.prototype.set=lt;var pt=ft;var vt=function(t,e){var r=this.__data__;if(r instanceof l){var n=r.__data__;if(!G||n.length<199)return n.push([t,e]),this.size=++r.size,this;r=this.__data__=new pt(n)}return r.set(t,e),this.size=r.size,this};function bt(t){var e=this.__data__=new l(t);this.size=e.size}bt.prototype.clear=f,bt.prototype.delete=p,bt.prototype.get=v,bt.prototype.has=b,bt.prototype.set=vt;var dt=bt;var ht=function(t,e){for(var r=-1,n=null==t?0:t.length;++r<n&&!1!==e(t[r],r,t););return t},yt=function(){try{var t=W(Object,"defineProperty");return t({},"",{}),t}catch(t){}}();var _t=function(t,e,r){"__proto__"==e&&yt?yt(t,e,{configurable:!0,enumerable:!0,value:r,writable:!0}):t[e]=r},jt=Object.prototype.hasOwnProperty;var gt=function(t,e,n){var o=t[e];jt.call(t,e)&&r(o,n)&&(void 0!==n||e in t)||_t(t,e,n)};var Ot=function(t,e,r,n){var o=!r;r||(r={});for(var a=-1,i=e.length;++a<i;){var c=e[a],s=n?n(r[c],t[c],c,r,t):void 0;void 0===s&&(s=t[c]),o?_t(r,c,s):gt(r,c,s)}return r};var Et=function(t,e){for(var r=-1,n=Array(t);++r<t;)n[r]=e(r);return n};var It=function(t){return null!=t&&"object"==typeof t};var St=function(t){return It(t)&&"[object Arguments]"==T(t)},At=Object.prototype,wt=At.hasOwnProperty,Rt=At.propertyIsEnumerable,mt=St(function(){return arguments}())?St:function(t){return It(t)&&wt.call(t,"callee")&&!Rt.call(t,"callee")},Tt=Array.isArray;var Nt=function(){return!1},Ct=h((function(t,e){var r=e&&!e.nodeType&&e,n=r&&t&&!t.nodeType&&t,o=n&&n.exports===r?j.Buffer:void 0,a=(o?o.isBuffer:void 0)||Nt;t.exports=a})),Lt=/^(?:0|[1-9]\d*)$/;var xt=function(t,e){var r=typeof t;return!!(e=null==e?9007199254740991:e)&&("number"==r||"symbol"!=r&&Lt.test(t))&&t>-1&&t%1==0&&t<e};var Dt=function(t){return"number"==typeof t&&t>-1&&t%1==0&&t<=9007199254740991},Ft={};Ft["[object Float32Array]"]=Ft["[object Float64Array]"]=Ft["[object Int8Array]"]=Ft["[object Int16Array]"]=Ft["[object Int32Array]"]=Ft["[object Uint8Array]"]=Ft["[object Uint8ClampedArray]"]=Ft["[object Uint16Array]"]=Ft["[object Uint32Array]"]=!0,Ft["[object Arguments]"]=Ft["[object Array]"]=Ft["[object ArrayBuffer]"]=Ft["[object Boolean]"]=Ft["[object DataView]"]=Ft["[object Date]"]=Ft["[object Error]"]=Ft["[object Function]"]=Ft["[object Map]"]=Ft["[object Number]"]=Ft["[object Object]"]=Ft["[object RegExp]"]=Ft["[object Set]"]=Ft["[object String]"]=Ft["[object WeakMap]"]=!1;var Pt=function(t){return It(t)&&Dt(t.length)&&!!Ft[T(t)]};var Mt=function(t){return function(e){return t(e)}},zt=h((function(t,e){var r=e&&!e.nodeType&&e,n=r&&t&&!t.nodeType&&t,o=n&&n.exports===r&&y.process,a=function(){try{var t=n&&n.require&&n.require("util").types;return t||o&&o.binding&&o.binding("util")}catch(t){}}();t.exports=a})),Vt=zt&&zt.isTypedArray,Ut=Vt?Mt(Vt):Pt,qt=Object.prototype.hasOwnProperty;var Bt=function(t,e){var r=Tt(t),n=!r&&mt(t),o=!r&&!n&&Ct(t),a=!r&&!n&&!o&&Ut(t),i=r||n||o||a,c=i?Et(t.length,String):[],s=c.length;for(var u in t)!e&&!qt.call(t,u)||i&&("length"==u||o&&("offset"==u||"parent"==u)||a&&("buffer"==u||"byteLength"==u||"byteOffset"==u)||xt(u,s))||c.push(u);return c},Ht=Object.prototype;var $t=function(t){var e=t&&t.constructor;return t===("function"==typeof e&&e.prototype||Ht)};var kt=function(t,e){return function(r){return t(e(r))}},Wt=kt(Object.keys,Object),Gt=Object.prototype.hasOwnProperty;var Yt=function(t){if(!$t(t))return Wt(t);var e=[];for(var r in Object(t))Gt.call(t,r)&&"constructor"!=r&&e.push(r);return e};var Jt=function(t){return null!=t&&Dt(t.length)&&!L(t)};var Kt=function(t){return Jt(t)?Bt(t):Yt(t)};var Qt=function(t,e){return t&&Ot(e,Kt(e),t)};var Xt=function(t){var e=[];if(null!=t)for(var r in Object(t))e.push(r);return e},Zt=Object.prototype.hasOwnProperty;var te=function(t){if(!N(t))return Xt(t);var e=$t(t),r=[];for(var n in t)("constructor"!=n||!e&&Zt.call(t,n))&&r.push(n);return r};var ee=function(t){return Jt(t)?Bt(t,!0):te(t)};var re=function(t,e){return t&&Ot(e,ee(e),t)},ne=h((function(t,e){var r=e&&!e.nodeType&&e,n=r&&t&&!t.nodeType&&t,o=n&&n.exports===r?j.Buffer:void 0,a=o?o.allocUnsafe:void 0;t.exports=function(t,e){if(e)return t.slice();var r=t.length,n=a?a(r):new t.constructor(r);return t.copy(n),n}}));var oe=function(t,e){var r=-1,n=t.length;for(e||(e=Array(n));++r<n;)e[r]=t[r];return e};var ae=function(t,e){for(var r=-1,n=null==t?0:t.length,o=0,a=[];++r<n;){var i=t[r];e(i,r,t)&&(a[o++]=i)}return a};var ie=function(){return[]},ce=Object.prototype.propertyIsEnumerable,se=Object.getOwnPropertySymbols,ue=se?function(t){return null==t?[]:(t=Object(t),ae(se(t),(function(e){return ce.call(t,e)})))}:ie;var le=function(t,e){return Ot(t,ue(t),e)};var fe=function(t,e){for(var r=-1,n=e.length,o=t.length;++r<n;)t[o+r]=e[r];return t},pe=kt(Object.getPrototypeOf,Object),ve=Object.getOwnPropertySymbols?function(t){for(var e=[];t;)fe(e,ue(t)),t=pe(t);return e}:ie;var be=function(t,e){return Ot(t,ve(t),e)};var de=function(t,e,r){var n=e(t);return Tt(t)?n:fe(n,r(t))};var he=function(t){return de(t,Kt,ue)};var ye=function(t){return de(t,ee,ve)},_e=W(j,"DataView"),je=W(j,"Promise"),ge=W(j,"Set"),Oe=W(j,"WeakMap"),Ee=M(_e),Ie=M(G),Se=M(je),Ae=M(ge),we=M(Oe),Re=T;(_e&&"[object DataView]"!=Re(new _e(new ArrayBuffer(1)))||G&&"[object Map]"!=Re(new G)||je&&"[object Promise]"!=Re(je.resolve())||ge&&"[object Set]"!=Re(new ge)||Oe&&"[object WeakMap]"!=Re(new Oe))&&(Re=function(t){var e=T(t),r="[object Object]"==e?t.constructor:void 0,n=r?M(r):"";if(n)switch(n){case Ee:return"[object DataView]";case Ie:return"[object Map]";case Se:return"[object Promise]";case Ae:return"[object Set]";case we:return"[object WeakMap]"}return e});var me=Re,Te=Object.prototype.hasOwnProperty;var Ne=function(t){var e=t.length,r=new t.constructor(e);return e&&"string"==typeof t[0]&&Te.call(t,"index")&&(r.index=t.index,r.input=t.input),r},Ce=j.Uint8Array;var Le=function(t){var e=new t.constructor(t.byteLength);return new Ce(e).set(new Ce(t)),e};var xe=function(t,e){var r=e?Le(t.buffer):t.buffer;return new t.constructor(r,t.byteOffset,t.byteLength)},De=/\w*$/;var Fe=function(t){var e=new t.constructor(t.source,De.exec(t));return e.lastIndex=t.lastIndex,e},Pe=g?g.prototype:void 0,Me=Pe?Pe.valueOf:void 0;var ze=function(t){return Me?Object(Me.call(t)):{}};var Ve=function(t,e){var r=e?Le(t.buffer):t.buffer;return new t.constructor(r,t.byteOffset,t.length)};var Ue=function(t,e,r){var n=t.constructor;switch(e){case"[object ArrayBuffer]":return Le(t);case"[object Boolean]":case"[object Date]":return new n(+t);case"[object DataView]":return xe(t,r);case"[object Float32Array]":case"[object Float64Array]":case"[object Int8Array]":case"[object Int16Array]":case"[object Int32Array]":case"[object Uint8Array]":case"[object Uint8ClampedArray]":case"[object Uint16Array]":case"[object Uint32Array]":return Ve(t,r);case"[object Map]":return new n;case"[object Number]":case"[object String]":return new n(t);case"[object RegExp]":return Fe(t);case"[object Set]":return new n;case"[object Symbol]":return ze(t)}},qe=Object.create,Be=function(){function t(){}return function(e){if(!N(e))return{};if(qe)return qe(e);t.prototype=e;var r=new t;return t.prototype=void 0,r}}();var He=function(t){return"function"!=typeof t.constructor||$t(t)?{}:Be(pe(t))};var $e=function(t){return It(t)&&"[object Map]"==me(t)},ke=zt&&zt.isMap,We=ke?Mt(ke):$e;var Ge=function(t){return It(t)&&"[object Set]"==me(t)},Ye=zt&&zt.isSet,Je=Ye?Mt(Ye):Ge,Ke={};Ke["[object Arguments]"]=Ke["[object Array]"]=Ke["[object ArrayBuffer]"]=Ke["[object DataView]"]=Ke["[object Boolean]"]=Ke["[object Date]"]=Ke["[object Float32Array]"]=Ke["[object Float64Array]"]=Ke["[object Int8Array]"]=Ke["[object Int16Array]"]=Ke["[object Int32Array]"]=Ke["[object Map]"]=Ke["[object Number]"]=Ke["[object Object]"]=Ke["[object RegExp]"]=Ke["[object Set]"]=Ke["[object String]"]=Ke["[object Symbol]"]=Ke["[object Uint8Array]"]=Ke["[object Uint8ClampedArray]"]=Ke["[object Uint16Array]"]=Ke["[object Uint32Array]"]=!0,Ke["[object Error]"]=Ke["[object Function]"]=Ke["[object WeakMap]"]=!1;var Qe=function t(e,r,n,o,a,i){var c,s=1&r,u=2&r,l=4&r;if(n&&(c=a?n(e,o,a,i):n(e)),void 0!==c)return c;if(!N(e))return e;var f=Tt(e);if(f){if(c=Ne(e),!s)return oe(e,c)}else{var p=me(e),v="[object Function]"==p||"[object GeneratorFunction]"==p;if(Ct(e))return ne(e,s);if("[object Object]"==p||"[object Arguments]"==p||v&&!a){if(c=u||v?{}:He(e),!s)return u?be(e,re(c,e)):le(e,Qt(c,e))}else{if(!Ke[p])return a?e:{};c=Ue(e,p,s)}}i||(i=new dt);var b=i.get(e);if(b)return b;i.set(e,c),Je(e)?e.forEach((function(o){c.add(t(o,r,n,o,e,i))})):We(e)&&e.forEach((function(o,a){c.set(a,t(o,r,n,a,e,i))}));var d=f?void 0:(l?u?ye:he:u?ee:Kt)(e);return ht(d||e,(function(o,a){d&&(o=e[a=o]),gt(c,a,t(o,r,n,a,e,i))})),c};var Xe=function(t){return Qe(t,5)};function Ze(t){return Object.getOwnPropertyNames(t||{}).forEach((e=>{const r=t[e];r instanceof Buffer||"object"==typeof r&&null!==r&&Ze(r)})),Object.freeze(t)}class tr{static initIndexes(t,e){return Promise.all(tr.getRequiredIndexes(e).map((([e,r])=>t.createIndex(e,r))))}static getRequiredIndexes(t){return[[{[t]:1,version:1},{unique:!0,name:"correlation_id_version_unicity"}]]}static getEvents(e,r,n,o){return t(r,"[Events#getEvents] Missing correlationField"),t(n,"[Events#getEvents] Missing correlationId"),t("number"==typeof o,"[Events#getEvents] Invalid version (must be a number)"),e.find({[r]:n,version:{$gt:o}},{projection:{_id:0,[r]:0,created_at:0}}).sort({version:1})}static store(t,e){const r=e.map((t=>({...t,created_at:new Date})));return t.insertMany(r)}}class er{static initIndexes(t,e){return Promise.all(er.getRequiredIndexes(e).map((([e,r])=>t.createIndex(e,r))))}static getRequiredIndexes(t){return[[{[t]:1,version:1},{unique:!0,name:"correlation_id_version_unicity"}]]}static getSnapshot(e,r,n,o){return t(r,"[Snapshots#getSnapshot] Missing correlationField"),t(n,"[Snapshots#getSnapshot] Missing correlationId"),t("number"==typeof o,"[Snapshots#getSnapshot] Invalid version (must be a number)"),e.findOne({[r]:n,version:{$lte:o}},{sort:{version:-1},fields:{_id:0}})}static create(t,e,r){return t.findOneAndUpdate({[e]:r[e],version:r.version},{$setOnInsert:r},{projection:{_id:0},upsert:!0})}}const rr=new Error("An event must not have a version before its reduction"),nr=new Error("Retry duration reached");var or=Object.freeze({__proto__:null,INCOMPATIBLE_EVENT_VERSION_WITH_STATE_VERSION:rr,HANDLER_RETRY_TIMEOUT:nr});function ar(e,r,n={}){return class o{constructor({db:t,logger:e},r){this.logger=null,this.correlationId=null,this.db=t,this.logger=e,this.correlationId=r,this.defaultStoreStateErrorHandler=this.defaultStoreStateErrorHandler.bind(this)}static get options(){return Object.assign({CORRELATION_FIELD:"correlation_id",EVENTS_COLLECTION_NAME:e+"_events",SNAPSHOTS_COLLECTION_NAME:e+"_snapshots"},n)}static get ERRORS(){return or}static getCollection(t,e){return t.collection(e)}static getStatesCollection(t){return o.getCollection(t,e)}static getEventsCollection(t){return o.getCollection(t,o.options.EVENTS_COLLECTION_NAME)}static getSnapshotsCollection(t){return o.getCollection(t,o.options.SNAPSHOTS_COLLECTION_NAME)}static initIndexes(t){return Promise.all([...o.getRequiredIndexes()[e].map((([e,r])=>o.getStatesCollection(t).createIndex(e,r))),tr.initIndexes(o.getEventsCollection(t),o.options.CORRELATION_FIELD),er.initIndexes(o.getSnapshotsCollection(t),o.options.CORRELATION_FIELD)])}static getRequiredIndexes(){return{[e]:[[{[o.options.CORRELATION_FIELD]:1},{unique:!0,name:"correlation_id_unicity"}]],[o.options.EVENTS_COLLECTION_NAME]:tr.getRequiredIndexes(o.options.CORRELATION_FIELD),[o.options.SNAPSHOTS_COLLECTION_NAME]:er.getRequiredIndexes(o.options.CORRELATION_FIELD)}}static getVersion(t){return t&&void 0!==t.version?t.version:-1}static getEvents(t,e,r){return tr.getEvents(o.getEventsCollection(t),o.options.CORRELATION_FIELD,e,r)}static reduce(e,n,a){t(a,"[EventSourced#reduce] correlationId must be defined");const i=Xe(e);return[i.reduce(((t,e)=>{const n=o.getVersion(e),i=o.getVersion(t);if(-1!==n&&n-i!=1)throw o.ERRORS.INCOMPATIBLE_EVENT_VERSION_WITH_STATE_VERSION;return(t=r(t,e)).version=i+1,e.version=o.getVersion(t),t[o.options.CORRELATION_FIELD]=a,e[o.options.CORRELATION_FIELD]=a,Ze(t)}),Ze(Xe(n))),i]}static async getState(t,e){const r=await o.getStatesCollection(t).findOne({[o.options.CORRELATION_FIELD]:e},{projection:{_id:0}}),n=await o.getEvents(t,e,o.getVersion(r));let a=Xe(r);for(;await n.hasNext();){const t=await n.next(),[r]=o.reduce([t],a,e);a=r}return{stateInDb:Ze(r),currentState:Ze(a)}}static storeEvents(t,e){return tr.store(o.getEventsCollection(t),e)}static storeState(t,e,r){return o.getStatesCollection(t).findOneAndReplace({[o.options.CORRELATION_FIELD]:e[o.options.CORRELATION_FIELD],version:r},e,{upsert:!0})}static async createSnapshot(t,e){const{stateInDb:r}=await o.getState(t,e);return await er.create(o.getSnapshotsCollection(t),o.options.CORRELATION_FIELD,r),r}defaultStoreStateErrorHandler(t,e){this.logger.info({err:t,updatedState:e},"Error occured when attempted to save the state")}async handle(e,r){r=r||this.defaultStoreStateErrorHandler,t("function"==typeof r,"storeStateErrorHandler must be a function");const{stateInDb:n,currentState:a}=await o.getState(this.db,this.correlationId),i=await e(a),[c,s]=o.reduce(i,a,this.correlationId);await o.storeEvents(this.db,s);try{await o.storeState(this.db,c,o.getVersion(n))}catch(t){r(t,c)}return c}async handleWithRetry(e,r,n){t("number"==typeof r,"retryDuration must be a number");const a=Date.now();for(;Date.now()-a<r;)try{return await this.handle(e,n)}catch(t){this.logger.error({err:t},"Error while handling the command")}throw o.ERRORS.HANDLER_RETRY_TIMEOUT}createSnapshot(){return o.createSnapshot(this.db,this.correlationId)}}}export{or as ERRORS,ar as EventSourcedFactory,tr as Events,er as Snapshots}; | ||
Object.defineProperty(exports,"__esModule",{value:!0}),exports.ERRORS=exports.Snapshots=exports.Events=exports.EventSourcedFactory=void 0;var e=require("./EventSourced");Object.defineProperty(exports,"EventSourcedFactory",{enumerable:!0,get:function(){return e.EventSourcedFactory}});var r=require("./Events");Object.defineProperty(exports,"Events",{enumerable:!0,get:function(){return r.Events}});var t=require("./Snapshots");Object.defineProperty(exports,"Snapshots",{enumerable:!0,get:function(){return t.Snapshots}});const o=require("./errors");exports.ERRORS=o; |
@@ -0,6 +1,9 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.HANDLER_RETRY_TIMEOUT = exports.INCOMPATIBLE_EVENT_VERSION_WITH_STATE_VERSION = void 0; | ||
/** | ||
* EventSourced errors | ||
*/ | ||
export const INCOMPATIBLE_EVENT_VERSION_WITH_STATE_VERSION = new Error('An event must not have a version before its reduction'); | ||
export const HANDLER_RETRY_TIMEOUT = new Error('Retry duration reached'); | ||
exports.INCOMPATIBLE_EVENT_VERSION_WITH_STATE_VERSION = new Error('An event must not have a version before its reduction'); | ||
exports.HANDLER_RETRY_TIMEOUT = new Error('Retry duration reached'); | ||
//# sourceMappingURL=errors.js.map |
@@ -1,3 +0,6 @@ | ||
import { strict as assert } from 'assert'; | ||
import * as date from './utils/date'; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Events = void 0; | ||
const assert_1 = require("assert"); | ||
const date = require("./utils/date"); | ||
/** | ||
@@ -10,3 +13,3 @@ * Events class to manage events for EventSourced instances | ||
*/ | ||
export class Events { | ||
class Events { | ||
/** Initialize the indexes | ||
@@ -50,5 +53,5 @@ * Mainly for testing purposes | ||
static getEvents(collection, correlationField, correlationId, version) { | ||
assert(correlationField, '[Events#getEvents] Missing correlationField'); | ||
assert(correlationId, '[Events#getEvents] Missing correlationId'); | ||
assert(typeof version === 'number', '[Events#getEvents] Invalid version (must be a number)'); | ||
assert_1.strict(correlationField, '[Events#getEvents] Missing correlationField'); | ||
assert_1.strict(correlationId, '[Events#getEvents] Missing correlationId'); | ||
assert_1.strict(typeof version === 'number', '[Events#getEvents] Invalid version (must be a number)'); | ||
return collection | ||
@@ -83,2 +86,3 @@ .find({ | ||
} | ||
exports.Events = Events; | ||
//# sourceMappingURL=Events.js.map |
import * as ERRORS from './errors'; | ||
import { MongoDbConnector } from 'chpr-mongodb'; | ||
import type { MongoDbConnector } from 'chpr-mongodb'; | ||
interface Logger { | ||
@@ -4,0 +4,0 @@ trace: Function; |
@@ -1,7 +0,10 @@ | ||
import { strict as assert } from 'assert'; | ||
import cloneDeep from 'lodash/cloneDeep'; | ||
import { deepFreeze } from './utils/deep-freeze'; | ||
import { Events } from './Events'; | ||
import { Snapshots } from './Snapshots'; | ||
import * as ERRORS from './errors'; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.EventSourcedFactory = void 0; | ||
const assert_1 = require("assert"); | ||
const lodash_1 = require("lodash"); | ||
const deep_freeze_1 = require("./utils/deep-freeze"); | ||
const Events_1 = require("./Events"); | ||
const Snapshots_1 = require("./Snapshots"); | ||
const ERRORS = require("./errors"); | ||
/** | ||
@@ -19,3 +22,3 @@ * Factory of the EventSourced class | ||
*/ | ||
export function EventSourcedFactory(collectionName, reducer, options = {}) { | ||
function EventSourcedFactory(collectionName, reducer, options = {}) { | ||
return class EventSourced { | ||
@@ -86,4 +89,4 @@ /** | ||
...EventSourced.getRequiredIndexes()[collectionName].map(([fields, opts]) => EventSourced.getStatesCollection(db).createIndex(fields, opts)), | ||
Events.initIndexes(EventSourced.getEventsCollection(db), EventSourced.options.CORRELATION_FIELD), | ||
Snapshots.initIndexes(EventSourced.getSnapshotsCollection(db), EventSourced.options.CORRELATION_FIELD), | ||
Events_1.Events.initIndexes(EventSourced.getEventsCollection(db), EventSourced.options.CORRELATION_FIELD), | ||
Snapshots_1.Snapshots.initIndexes(EventSourced.getSnapshotsCollection(db), EventSourced.options.CORRELATION_FIELD), | ||
]); | ||
@@ -104,5 +107,5 @@ } | ||
[EventSourced.options | ||
.EVENTS_COLLECTION_NAME]: Events.getRequiredIndexes(EventSourced.options.CORRELATION_FIELD), | ||
.EVENTS_COLLECTION_NAME]: Events_1.Events.getRequiredIndexes(EventSourced.options.CORRELATION_FIELD), | ||
[EventSourced.options | ||
.SNAPSHOTS_COLLECTION_NAME]: Snapshots.getRequiredIndexes(EventSourced.options.CORRELATION_FIELD), | ||
.SNAPSHOTS_COLLECTION_NAME]: Snapshots_1.Snapshots.getRequiredIndexes(EventSourced.options.CORRELATION_FIELD), | ||
}; | ||
@@ -129,3 +132,3 @@ } | ||
static getEvents(db, correlationId, version) { | ||
return Events.getEvents(EventSourced.getEventsCollection(db), EventSourced.options.CORRELATION_FIELD, correlationId, version); | ||
return Events_1.Events.getEvents(EventSourced.getEventsCollection(db), EventSourced.options.CORRELATION_FIELD, correlationId, version); | ||
} | ||
@@ -140,4 +143,4 @@ /** | ||
static reduce(events, state, correlationId) { | ||
assert(correlationId, '[EventSourced#reduce] correlationId must be defined'); | ||
const copiedEvents = cloneDeep(events); | ||
assert_1.strict(correlationId, '[EventSourced#reduce] correlationId must be defined'); | ||
const copiedEvents = lodash_1.cloneDeep(events); | ||
const updatedState = copiedEvents.reduce((updated, event) => { | ||
@@ -157,4 +160,4 @@ const eventVersion = EventSourced.getVersion(event); | ||
event[EventSourced.options.CORRELATION_FIELD] = correlationId; | ||
return deepFreeze(updated); | ||
}, deepFreeze(cloneDeep(state))); | ||
return deep_freeze_1.deepFreeze(updated); | ||
}, deep_freeze_1.deepFreeze(lodash_1.cloneDeep(state))); | ||
return [updatedState, copiedEvents]; | ||
@@ -178,3 +181,3 @@ } | ||
const cursor = await EventSourced.getEvents(db, correlationId, EventSourced.getVersion(stateInDb)); | ||
let currentState = cloneDeep(stateInDb); | ||
let currentState = lodash_1.cloneDeep(stateInDb); | ||
while (await cursor.hasNext()) { | ||
@@ -187,4 +190,4 @@ // eslint-disable-line no-await-in-loop | ||
return { | ||
stateInDb: deepFreeze(stateInDb), | ||
currentState: deepFreeze(currentState), | ||
stateInDb: deep_freeze_1.deepFreeze(stateInDb), | ||
currentState: deep_freeze_1.deepFreeze(currentState), | ||
}; | ||
@@ -199,3 +202,3 @@ } | ||
static storeEvents(db, events) { | ||
return Events.store(EventSourced.getEventsCollection(db), events); | ||
return Events_1.Events.store(EventSourced.getEventsCollection(db), events); | ||
} | ||
@@ -226,3 +229,3 @@ /** | ||
const { stateInDb } = await EventSourced.getState(db, correlationId); | ||
await Snapshots.create(EventSourced.getSnapshotsCollection(db), EventSourced.options.CORRELATION_FIELD, stateInDb); | ||
await Snapshots_1.Snapshots.create(EventSourced.getSnapshotsCollection(db), EventSourced.options.CORRELATION_FIELD, stateInDb); | ||
return stateInDb; | ||
@@ -249,3 +252,3 @@ } | ||
storeStateErrorHandler || this.defaultStoreStateErrorHandler; | ||
assert(typeof storeStateErrorHandler === 'function', 'storeStateErrorHandler must be a function'); | ||
assert_1.strict(typeof storeStateErrorHandler === 'function', 'storeStateErrorHandler must be a function'); | ||
// Retrieve last available state and current state with potential missing events reduced | ||
@@ -261,3 +264,3 @@ const { stateInDb, currentState } = await EventSourced.getState(this.db, this.correlationId); | ||
try { | ||
const stored = await EventSourced.storeState(this.db, updatedState, EventSourced.getVersion(stateInDb)); | ||
await EventSourced.storeState(this.db, updatedState, EventSourced.getVersion(stateInDb)); | ||
} | ||
@@ -279,3 +282,3 @@ catch (err) { | ||
async handleWithRetry(handler, retryDuration, storeStateErrorHandler) { | ||
assert(typeof retryDuration === 'number', 'retryDuration must be a number'); | ||
assert_1.strict(typeof retryDuration === 'number', 'retryDuration must be a number'); | ||
const tic = Date.now(); | ||
@@ -302,2 +305,3 @@ // eslint-disable-next-line no-await-in-loop | ||
} | ||
exports.EventSourcedFactory = EventSourcedFactory; | ||
//# sourceMappingURL=EventSourced.js.map |
@@ -0,7 +1,13 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.ERRORS = exports.Snapshots = exports.Events = exports.EventSourcedFactory = void 0; | ||
/* istanbul ignore file */ | ||
export { EventSourcedFactory } from './EventSourced'; | ||
export { Events } from './Events'; | ||
export { Snapshots } from './Snapshots'; | ||
import * as ERRORS from './errors'; | ||
export { ERRORS }; | ||
var EventSourced_1 = require("./EventSourced"); | ||
Object.defineProperty(exports, "EventSourcedFactory", { enumerable: true, get: function () { return EventSourced_1.EventSourcedFactory; } }); | ||
var Events_1 = require("./Events"); | ||
Object.defineProperty(exports, "Events", { enumerable: true, get: function () { return Events_1.Events; } }); | ||
var Snapshots_1 = require("./Snapshots"); | ||
Object.defineProperty(exports, "Snapshots", { enumerable: true, get: function () { return Snapshots_1.Snapshots; } }); | ||
const ERRORS = require("./errors"); | ||
exports.ERRORS = ERRORS; | ||
//# sourceMappingURL=index.js.map |
@@ -1,3 +0,6 @@ | ||
import { strict as assert } from 'assert'; | ||
export class Snapshots { | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Snapshots = void 0; | ||
const assert_1 = require("assert"); | ||
class Snapshots { | ||
/** Initialize the indexes | ||
@@ -41,5 +44,5 @@ * Mainly for testing purposes | ||
static getSnapshot(collection, correlationField, correlationId, version) { | ||
assert(correlationField, '[Snapshots#getSnapshot] Missing correlationField'); | ||
assert(correlationId, '[Snapshots#getSnapshot] Missing correlationId'); | ||
assert(typeof version === 'number', '[Snapshots#getSnapshot] Invalid version (must be a number)'); | ||
assert_1.strict(correlationField, '[Snapshots#getSnapshot] Missing correlationField'); | ||
assert_1.strict(correlationId, '[Snapshots#getSnapshot] Missing correlationId'); | ||
assert_1.strict(typeof version === 'number', '[Snapshots#getSnapshot] Invalid version (must be a number)'); | ||
return collection.findOne({ | ||
@@ -72,2 +75,3 @@ [correlationField]: correlationId, | ||
} | ||
exports.Snapshots = Snapshots; | ||
//# sourceMappingURL=Snapshots.js.map |
@@ -0,1 +1,4 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.getNow = void 0; | ||
/** | ||
@@ -5,5 +8,6 @@ * Return actual date, creating to be able to stub the date in the tests | ||
*/ | ||
export function getNow() { | ||
function getNow() { | ||
return new Date(); | ||
} | ||
exports.getNow = getNow; | ||
//# sourceMappingURL=date.js.map |
@@ -0,1 +1,4 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.deepFreeze = void 0; | ||
/** | ||
@@ -7,3 +10,3 @@ * Deep freeze an object | ||
*/ | ||
export function deepFreeze(obj) { | ||
function deepFreeze(obj) { | ||
// Retrieve the property names defined on obj | ||
@@ -25,2 +28,3 @@ const propNames = Object.getOwnPropertyNames(obj || {}); | ||
} | ||
exports.deepFreeze = deepFreeze; | ||
//# sourceMappingURL=deep-freeze.js.map |
{ | ||
"name": "@getanthill/event-source", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "Event-sourced data store handling library", | ||
"main": "dist/index.js", | ||
"browser": "dist/bundle.umd.js", | ||
"module": "dist/bundle.esm.js", | ||
@@ -8,0 +7,0 @@ "types": "dist/index.d.ts", |
import { strict as assert } from 'assert'; | ||
import cloneDeep from 'lodash/cloneDeep'; | ||
import { cloneDeep } from 'lodash'; | ||
@@ -10,4 +10,5 @@ import { deepFreeze } from './utils/deep-freeze'; | ||
import * as ERRORS from './errors'; | ||
import { MongoDbConnector } from 'chpr-mongodb'; | ||
import type { MongoDbConnector } from 'chpr-mongodb'; | ||
interface Logger { | ||
@@ -14,0 +15,0 @@ trace: Function; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
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
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
0
68813
1486
3