UNPKG

vite-plugin-mock-dev-server

Version:
4 lines (3 loc) 1.68 kB
import{deepClone as e,deepEqual as t,isArray as n,isFunction as r}from"@pengzhanbo/utils";import{Transform as i}from"node:stream";var a=class extends i{constructor(e){super({objectMode:!0}),e.socket.setKeepAlive(!0),e.socket.setNoDelay(!0),e.socket.setTimeout(0)}pipe(e,t){return e.writeHead&&(e.writeHead(200,{"Content-Type":`text/event-stream; charset=utf-8`,"Transfer-Encoding":`identity`,"Cache-Control":`no-cache`,Connection:`keep-alive`}),e.flushHeaders?.()),e.write(`:ok `),super.pipe(e,t)}_transform(e,t,n){e.comment&&this.push(`: ${e.comment}\n`),e.event&&this.push(`event: ${e.event}\n`),e.id&&this.push(`id: ${e.id}\n`),e.retry&&this.push(`retry: ${e.retry}\n`),e.data&&this.push(o(e.data)),this.push(` `),n()}write(e,...t){return super.write(e,...t)}destroy(e){return e&&this.write({event:`error`,data:e.message}),this.end(),this}};function o(e){return typeof e==`object`?o(JSON.stringify(e)):e.split(/\r\n|\r|\n/).map(e=>`data: ${e}\n`).join(``)}function s(e,t){let n=new a(e);return n.pipe(t),n}function c(e){return e}function l(e){return t=>(t=n(t)?t.map(t=>e(t)||t):e(t)||t,t)}const u=new Map,d=new WeakMap;var f=class{value;#e;#t;constructor(t){this.value=t,this.#e=e(t),this.#t=Date.now()}hotUpdate(n){Date.now()-this.#t<70||t(n,this.#e)||(this.value=n,this.#e=e(n),this.#t=Date.now())}};function p(e,t){let n=u.get(e);if(!n){let r=new f(t),i=u.get(e);i?n=i:(u.set(e,r),n=r)}if(n.hotUpdate(t),d.has(n))return d.get(n);let i=[()=>n.value,e=>{r(e)&&(e=e(n.value)??n.value),n.value=e}];return Object.defineProperty(i,`value`,{get(){return n.value},set(e){n.value=e}}),d.set(n,i),i}export{l as createDefineMock,s as createSSEStream,c as defineMock,p as defineMockData};