@pollyjs/node-server
Version:
Standalone node server and express integration for @pollyjs
3 lines (2 loc) • 13.8 kB
JavaScript
import t from"path";import r from"fs-extra";import{assert as e}from"@pollyjs/utils";import n from"cors";import o from"morgan";import i from"express";import c from"http-graceful-shutdown";import u from"body-parser";import f from"nocache";class s{constructor(t={}){const{recordingsDir:r}=t;e(`Invalid recordings directory provided. Expected string, received: "${typeof r}".`,"string"==typeof r),this.recordingsDir=r}getRecording(t){const e=this.filenameFor(t);return r.existsSync(e)?this.respond(200,r.readJsonSync(e)):this.respond(204)}saveRecording(t,e){return r.outputJsonSync(this.filenameFor(t),e,{spaces:2}),this.respond(201)}deleteRecording(t){const e=this.filenameFor(t);return r.existsSync(e)&&r.removeSync(e),this.respond(200)}filenameFor(r){return t.join(this.recordingsDir,r,"recording.har")}respond(t,r){return{status:t,body:r}}}var a=function(t){if(null==t)throw TypeError("Can't call method on "+t);return t},p=function(t){return Object(a(t))},l={}.hasOwnProperty,y=function(t,r){return l.call(t,r)},h={}.toString,d=function(t){return h.call(t).slice(8,-1)},g=Object("z").propertyIsEnumerable(0)?Object:function(t){return"String"==d(t)?t.split(""):Object(t)},v=function(t){return g(a(t))},b=Math.ceil,m=Math.floor,O=function(t){return isNaN(t=+t)?0:(t>0?m:b)(t)},w=Math.min,j=Math.max,S=Math.min;function P(t,r){return t(r={exports:{}},r.exports),r.exports}var E,_=P((function(t){var r=t.exports={version:"2.6.9"};"number"==typeof __e&&(__e=r)})),x=(_.version,P((function(t){var r=t.exports="undefined"!=typeof window&&window.Math==Math?window:"undefined"!=typeof self&&self.Math==Math?self:Function("return this")();"number"==typeof __g&&(__g=r)}))),F=P((function(t){var r=x["__core-js_shared__"]||(x["__core-js_shared__"]={});(t.exports=function(t,e){return r[t]||(r[t]=void 0!==e?e:{})})("versions",[]).push({version:_.version,mode:"pure",copyright:"© 2019 Denis Pushkarev (zloirock.ru)"})})),D=0,N=Math.random(),M=function(t){return"Symbol(".concat(void 0===t?"":t,")_",(++D+N).toString(36))},R=F("keys"),k=function(t){return R[t]||(R[t]=M(t))},T=(E=!1,function(t,r,e){var n,o,i=v(t),c=(n=i.length)>0?w(O(n),9007199254740991):0,u=function(t,r){return(t=O(t))<0?j(t+r,0):S(t,r)}(e,c);if(E&&r!=r){for(;c>u;)if((o=i[u++])!=o)return!0}else for(;c>u;u++)if((E||u in i)&&i[u]===r)return E||u||0;return!E&&-1}),I=k("IE_PROTO"),W=function(t,r){var e,n=v(t),o=0,i=[];for(e in n)e!=I&&y(n,e)&&i.push(e);for(;r.length>o;)y(n,e=r[o++])&&(~T(i,e)||i.push(e));return i},A="constructor,hasOwnProperty,isPrototypeOf,propertyIsEnumerable,toLocaleString,toString,valueOf".split(","),z=Object.keys||function(t){return W(t,A)},J=function(t,r,e){if(function(t){if("function"!=typeof t)throw TypeError(t+" is not a function!")}(t),void 0===r)return t;switch(e){case 1:return function(e){return t.call(r,e)};case 2:return function(e,n){return t.call(r,e,n)};case 3:return function(e,n,o){return t.call(r,e,n,o)}}return function(){return t.apply(r,arguments)}},K=function(t){return"object"==typeof t?null!==t:"function"==typeof t},C=function(t){if(!K(t))throw TypeError(t+" is not an object!");return t},L=function(t){try{return!!t()}catch(t){return!0}},$=!L((function(){return 7!=Object.defineProperty({},"a",{get:function(){return 7}}).a})),q=x.document,G=K(q)&&K(q.createElement),Y=function(t){return G?q.createElement(t):{}},B=!$&&!L((function(){return 7!=Object.defineProperty(Y("div"),"a",{get:function(){return 7}}).a})),U=function(t,r){if(!K(t))return t;var e,n;if(r&&"function"==typeof(e=t.toString)&&!K(n=e.call(t)))return n;if("function"==typeof(e=t.valueOf)&&!K(n=e.call(t)))return n;if(!r&&"function"==typeof(e=t.toString)&&!K(n=e.call(t)))return n;throw TypeError("Can't convert object to primitive value")},Q=Object.defineProperty,H={f:$?Object.defineProperty:function(t,r,e){if(C(t),r=U(r,!0),C(e),B)try{return Q(t,r,e)}catch(t){}if("get"in e||"set"in e)throw TypeError("Accessors not supported!");return"value"in e&&(t[r]=e.value),t}},V=function(t,r){return{enumerable:!(1&t),configurable:!(2&t),writable:!(4&t),value:r}},X=$?function(t,r,e){return H.f(t,r,V(1,e))}:function(t,r,e){return t[r]=e,t},Z=function(t,r,e){var n,o,i,c=t&Z.F,u=t&Z.G,f=t&Z.S,s=t&Z.P,a=t&Z.B,p=t&Z.W,l=u?_:_[r]||(_[r]={}),h=l.prototype,d=u?x:f?x[r]:(x[r]||{}).prototype;for(n in u&&(e=r),e)(o=!c&&d&&void 0!==d[n])&&y(l,n)||(i=o?d[n]:e[n],l[n]=u&&"function"!=typeof d[n]?e[n]:a&&o?J(i,x):p&&d[n]==i?function(t){var r=function(r,e,n){if(this instanceof t){switch(arguments.length){case 0:return new t;case 1:return new t(r);case 2:return new t(r,e)}return new t(r,e,n)}return t.apply(this,arguments)};return r.prototype=t.prototype,r}(i):s&&"function"==typeof i?J(Function.call,i):i,s&&((l.virtual||(l.virtual={}))[n]=i,t&Z.R&&h&&!h[n]&&X(h,n,i)))};Z.F=1,Z.G=2,Z.S=4,Z.P=8,Z.B=16,Z.W=32,Z.U=64,Z.R=128;var tt=Z,rt=function(t,r){var e=(_.Object||{})[t]||Object[t],n={};n[t]=r(e),tt(tt.S+tt.F*L((function(){e(1)})),"Object",n)};rt("keys",(function(){return function(t){return z(p(t))}}));var et=_.Object.keys,nt=X,ot=P((function(t){var r=M("meta"),e=H.f,n=0,o=Object.isExtensible||function(){return!0},i=!L((function(){return o(Object.preventExtensions({}))})),c=function(t){e(t,r,{value:{i:"O"+ ++n,w:{}}})},u=t.exports={KEY:r,NEED:!1,fastKey:function(t,e){if(!K(t))return"symbol"==typeof t?t:("string"==typeof t?"S":"P")+t;if(!y(t,r)){if(!o(t))return"F";if(!e)return"E";c(t)}return t[r].i},getWeak:function(t,e){if(!y(t,r)){if(!o(t))return!0;if(!e)return!1;c(t)}return t[r].w},onFreeze:function(t){return i&&u.NEED&&o(t)&&!y(t,r)&&c(t),t}}})),it=(ot.KEY,ot.NEED,ot.fastKey,ot.getWeak,ot.onFreeze,P((function(t){var r=F("wks"),e=x.Symbol,n="function"==typeof e;(t.exports=function(t){return r[t]||(r[t]=n&&e[t]||(n?e:M)("Symbol."+t))}).store=r}))),ct=H.f,ut=it("toStringTag"),ft=function(t,r,e){t&&!y(t=e?t:t.prototype,ut)&&ct(t,ut,{configurable:!0,value:r})},st={f:it},at=H.f,pt=function(t){var r=_.Symbol||(_.Symbol={});"_"==t.charAt(0)||t in r||at(r,t,{value:st.f(t)})},lt={f:Object.getOwnPropertySymbols},yt={f:{}.propertyIsEnumerable},ht=Array.isArray||function(t){return"Array"==d(t)},dt=$?Object.defineProperties:function(t,r){C(t);for(var e,n=z(r),o=n.length,i=0;o>i;)H.f(t,e=n[i++],r[e]);return t},gt=x.document,vt=gt&>.documentElement,bt=k("IE_PROTO"),mt=function(){},Ot=function(){var t,r=Y("iframe"),e=A.length;for(r.style.display="none",vt.appendChild(r),r.src="javascript:",(t=r.contentWindow.document).open(),t.write("<script>document.F=Object<\/script>"),t.close(),Ot=t.F;e--;)delete Ot.prototype[A[e]];return Ot()},wt=Object.create||function(t,r){var e;return null!==t?(mt.prototype=C(t),e=new mt,mt.prototype=null,e[bt]=t):e=Ot(),void 0===r?e:dt(e,r)},jt=A.concat("length","prototype"),St={f:Object.getOwnPropertyNames||function(t){return W(t,jt)}},Pt=St.f,Et={}.toString,_t="object"==typeof window&&window&&Object.getOwnPropertyNames?Object.getOwnPropertyNames(window):[],xt={f:function(t){return _t&&"[object Window]"==Et.call(t)?function(t){try{return Pt(t)}catch(t){return _t.slice()}}(t):Pt(v(t))}},Ft=Object.getOwnPropertyDescriptor,Dt={f:$?Ft:function(t,r){if(t=v(t),r=U(r,!0),B)try{return Ft(t,r)}catch(t){}if(y(t,r))return V(!yt.f.call(t,r),t[r])}},Nt=ot.KEY,Mt=Dt.f,Rt=H.f,kt=xt.f,Tt=x.Symbol,It=x.JSON,Wt=It&&It.stringify,At=it("_hidden"),zt=it("toPrimitive"),Jt={}.propertyIsEnumerable,Kt=F("symbol-registry"),Ct=F("symbols"),Lt=F("op-symbols"),$t=Object.prototype,qt="function"==typeof Tt&&!!lt.f,Gt=x.QObject,Yt=!Gt||!Gt.prototype||!Gt.prototype.findChild,Bt=$&&L((function(){return 7!=wt(Rt({},"a",{get:function(){return Rt(this,"a",{value:7}).a}})).a}))?function(t,r,e){var n=Mt($t,r);n&&delete $t[r],Rt(t,r,e),n&&t!==$t&&Rt($t,r,n)}:Rt,Ut=function(t){var r=Ct[t]=wt(Tt.prototype);return r._k=t,r},Qt=qt&&"symbol"==typeof Tt.iterator?function(t){return"symbol"==typeof t}:function(t){return t instanceof Tt},Ht=function(t,r,e){return t===$t&&Ht(Lt,r,e),C(t),r=U(r,!0),C(e),y(Ct,r)?(e.enumerable?(y(t,At)&&t[At][r]&&(t[At][r]=!1),e=wt(e,{enumerable:V(0,!1)})):(y(t,At)||Rt(t,At,V(1,{})),t[At][r]=!0),Bt(t,r,e)):Rt(t,r,e)},Vt=function(t,r){C(t);for(var e,n=function(t){var r=z(t),e=lt.f;if(e)for(var n,o=e(t),i=yt.f,c=0;o.length>c;)i.call(t,n=o[c++])&&r.push(n);return r}(r=v(r)),o=0,i=n.length;i>o;)Ht(t,e=n[o++],r[e]);return t},Xt=function(t){var r=Jt.call(this,t=U(t,!0));return!(this===$t&&y(Ct,t)&&!y(Lt,t))&&(!(r||!y(this,t)||!y(Ct,t)||y(this,At)&&this[At][t])||r)},Zt=function(t,r){if(t=v(t),r=U(r,!0),t!==$t||!y(Ct,r)||y(Lt,r)){var e=Mt(t,r);return!e||!y(Ct,r)||y(t,At)&&t[At][r]||(e.enumerable=!0),e}},tr=function(t){for(var r,e=kt(v(t)),n=[],o=0;e.length>o;)y(Ct,r=e[o++])||r==At||r==Nt||n.push(r);return n},rr=function(t){for(var r,e=t===$t,n=kt(e?Lt:v(t)),o=[],i=0;n.length>i;)!y(Ct,r=n[i++])||e&&!y($t,r)||o.push(Ct[r]);return o};qt||(nt((Tt=function(){if(this instanceof Tt)throw TypeError("Symbol is not a constructor!");var t=M(arguments.length>0?arguments[0]:void 0),r=function(e){this===$t&&r.call(Lt,e),y(this,At)&&y(this[At],t)&&(this[At][t]=!1),Bt(this,t,V(1,e))};return $&&Yt&&Bt($t,t,{configurable:!0,set:r}),Ut(t)}).prototype,"toString",(function(){return this._k})),Dt.f=Zt,H.f=Ht,St.f=xt.f=tr,yt.f=Xt,lt.f=rr,st.f=function(t){return Ut(it(t))}),tt(tt.G+tt.W+tt.F*!qt,{Symbol:Tt});for(var er="hasInstance,isConcatSpreadable,iterator,match,replace,search,species,split,toPrimitive,toStringTag,unscopables".split(","),nr=0;er.length>nr;)it(er[nr++]);for(var or=z(it.store),ir=0;or.length>ir;)pt(or[ir++]);tt(tt.S+tt.F*!qt,"Symbol",{for:function(t){return y(Kt,t+="")?Kt[t]:Kt[t]=Tt(t)},keyFor:function(t){if(!Qt(t))throw TypeError(t+" is not a symbol!");for(var r in Kt)if(Kt[r]===t)return r},useSetter:function(){Yt=!0},useSimple:function(){Yt=!1}}),tt(tt.S+tt.F*!qt,"Object",{create:function(t,r){return void 0===r?wt(t):Vt(wt(t),r)},defineProperty:Ht,defineProperties:Vt,getOwnPropertyDescriptor:Zt,getOwnPropertyNames:tr,getOwnPropertySymbols:rr});var cr=L((function(){lt.f(1)}));tt(tt.S+tt.F*cr,"Object",{getOwnPropertySymbols:function(t){return lt.f(p(t))}}),It&&tt(tt.S+tt.F*(!qt||L((function(){var t=Tt();return"[null]"!=Wt([t])||"{}"!=Wt({a:t})||"{}"!=Wt(Object(t))}))),"JSON",{stringify:function(t){for(var r,e,n=[t],o=1;arguments.length>o;)n.push(arguments[o++]);if(e=r=n[1],(K(r)||void 0!==t)&&!Qt(t))return ht(r)||(r=function(t,r){if("function"==typeof e&&(r=e.call(this,t,r)),!Qt(r))return r}),n[1]=r,Wt.apply(It,n)}}),Tt.prototype[zt]||X(Tt.prototype,zt,Tt.prototype.valueOf),ft(Tt,"Symbol"),ft(Math,"Math",!0),ft(x.JSON,"JSON",!0);var ur=_.Object.getOwnPropertySymbols,fr=Dt.f;rt("getOwnPropertyDescriptor",(function(){return function(t,r){return fr(v(t),r)}}));var sr=_.Object,ar=function(t,r){return sr.getOwnPropertyDescriptor(t,r)},pr=x.Reflect,lr=pr&&pr.ownKeys||function(t){var r=St.f(C(t)),e=lt.f;return e?r.concat(e(t)):r},yr=function(t,r,e){r in t?H.f(t,r,V(0,e)):t[r]=e};tt(tt.S,"Object",{getOwnPropertyDescriptors:function(t){for(var r,e,n=v(t),o=Dt.f,i=lr(n),c={},u=0;i.length>u;)void 0!==(e=o(n,r=i[u++]))&&yr(c,r,e);return c}});var hr=_.Object.getOwnPropertyDescriptors;tt(tt.S+tt.F*!$,"Object",{defineProperties:dt});var dr=_.Object,gr=function(t,r){return dr.defineProperties(t,r)};tt(tt.S+tt.F*!$,"Object",{defineProperty:H.f});var vr,br=_.Object,mr=function(t,r,e){return br.defineProperty(t,r,e)},Or=P((function(t){t.exports=function(t,r,e){return r in t?mr(t,r,{value:e,enumerable:!0,configurable:!0,writable:!0}):t[r]=e,t},t.exports.default=t.exports,t.exports.__esModule=!0})),wr=(vr=Or)&&vr.__esModule&&Object.prototype.hasOwnProperty.call(vr,"default")?vr.default:vr,jr={port:3e3,quiet:!1,recordingSizeLimit:"50mb",recordingsDir:"recordings",apiNamespace:"/polly"};function Sr(t,r){var e=et(t);if(ur){var n=ur(t);r&&(n=n.filter((function(r){return ar(t,r).enumerable}))),e.push.apply(e,n)}return e}function Pr(t){for(var r=1;r<arguments.length;r++){var e=null!=arguments[r]?arguments[r]:{};r%2?Sr(Object(e),!0).forEach((function(r){wr(t,r,e[r])})):hr?gr(t,hr(e)):Sr(Object(e)).forEach((function(r){mr(t,r,ar(e,r))}))}return t}function Er(t,r){(r=Pr(Pr({},jr),r)).apiNamespace=function(t=""){return t.startsWith("/")?t:"/"+t}(r.apiNamespace);const e=i.Router(),n=new s({recordingsDir:r.recordingsDir});e.use(f()),e.get("/:recording",(function(t,r){const{recording:e}=t.params,{status:o,body:i}=n.getRecording(e);r.status(o),200===o?r.json(i):r.end()})),e.post("/:recording",u.json({limit:r.recordingSizeLimit}),(function(t,r){const{recording:e}=t.params,{status:o,body:i}=n.saveRecording(e,t.body);r.status(o).send(i)})),e.delete("/:recording",(function(t,r){const{recording:e}=t.params,{status:o,body:i}=n.deleteRecording(e);r.status(o).send(i)})),t.use(r.apiNamespace,e)}function _r(t,r){var e=et(t);if(ur){var n=ur(t);r&&(n=n.filter((function(r){return ar(t,r).enumerable}))),e.push.apply(e,n)}return e}function xr(t){for(var r=1;r<arguments.length;r++){var e=null!=arguments[r]?arguments[r]:{};r%2?_r(Object(e),!0).forEach((function(r){wr(t,r,e[r])})):hr?gr(t,hr(e)):_r(Object(e)).forEach((function(r){mr(t,r,ar(e,r))}))}return t}class Fr{constructor(t={}){this.config=xr(xr({},jr),t),this.app=i(),this.app.use(n(this.config.corsOptions)),this.config.quiet||this.app.use(o("dev")),this.app.get("/",(t,r)=>r.sendStatus(200)),this.app.head("/",(t,r)=>r.sendStatus(200)),Er(this.app,{recordingsDir:this.config.recordingsDir,apiNamespace:this.config.apiNamespace})}listen(t,r){if(!this.server)return t=t||this.config.port,r=r||this.config.host,this.server=this.app.listen(t,r).on("listening",()=>{this.config.quiet||console.log(`Listening on http://${r||"localhost"}:${t}/\n`)}).on("error",r=>{"EADDRINUSE"===r.code?(console.error(`Port ${t} already in use.`),process.exit(1)):console.error(r)}),c(this.server),this.server}}export{s as API,jr as Defaults,Fr as Server,Er as registerExpressAPI};
//# sourceMappingURL=pollyjs-node-server.min.js.map