@moneko/core 4.7.6 → 4.7.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/dev/proxy.mjs CHANGED
@@ -1 +1 @@
1
- import{request as e}from"node:http";import{request as t}from"node:https";import{URL as r}from"node:url";import{brotliDecompressSync as o,gunzipSync as n,inflateSync as i}from"node:zlib";import{updateFile as a}from"@moneko/utils";import p from"../commom/log.mjs";import{isFunction as s,isObject as m}from"../utils/index.mjs";import{containMockUrl as l}from"./mock.mjs";export function convertUriToFileName(e){let t={" ":"➯","/":">",":":"◍"};return e.replace(/[\s/:]/g,e=>t[e]||e)}export async function setupProxy(c,h,d){if(h.writableEnded)return h;let f=c.url,u=Object.keys(d).find(e=>new RegExp(e).test(f));if(!u)return!1;let g="string"==typeof d[u]?{target:d[u],pathRewrite:void 0,changeOrigin:!0,secure:!0,ws:!1}:d[u],w=new r(g.target),y=f;if(g.pathRewrite&&"object"==typeof g.pathRewrite)for(let e in g.pathRewrite)Object.prototype.hasOwnProperty.call(g.pathRewrite,e)&&(y=f.replace(new RegExp(e),g.pathRewrite[e]));let v="https:"===w.protocol,x={method:c.method,hostname:w.hostname,port:w.port||(v?443:80),path:y,headers:{...c.headers},protocol:w.protocol};g.changeOrigin&&(x.headers.host=w.host);let R=v?t:e;return new Promise(e=>{let t=R(x,t=>{let p=c.method||"GET",d=new r(`${w.origin}${c.url}`).pathname,f=`${p} ${d}`,u=t.statusCode||404,y=u>=400,v=!!l(f);if(y&&v)return e(!1);if(h.writeHead(u,t.headers),!y&&!v&&g.harvester){let e=m(g.harvester)&&s(g.harvester.generate)?g.harvester.generate({method:p,pathname:d,convert:convertUriToFileName}):convertUriToFileName(f);if(e){let r=[];t.on("data",e=>r.push(e)),t.on("end",()=>{let p=Buffer.concat(r),s=t.headers["content-encoding"]||"",m=null;(m=s.includes("gzip")?n(p).toString():s.includes("br")?o(p).toString():s.includes("deflate")?i(p).toString():p.toString())&&a(`mock/${e}.ts`,`import type { MockConfiguration } from '@moneko/core';const conf: MockConfiguration = {'${f}': ${m}};export default conf;`)})}}t.pipe(h),e(h)});t.on("error",t=>(p(t),h.headersSent?h.end():(h.writeHead(502,{"Content-Type":"text/plain"}),h.end(`Proxy Error: ${t.message}`)),e(!1))),c.pipe(t)})}
1
+ import{request as e}from"node:http";import{request as t}from"node:https";import{URL as r}from"node:url";import{brotliDecompressSync as o,gunzipSync as n,inflateSync as i}from"node:zlib";import{updateFile as a}from"@moneko/utils";import p from"../commom/log.mjs";import{isFunction as s,isObject as m}from"../utils/index.mjs";import{containMockUrl as l}from"./mock.mjs";export function convertUriToFileName(e){let t={" ":"➯","/":">",":":"◍"};return e.replace(/[\s/:]/g,e=>t[e]||e)}function c(e){return{target:e,pathRewrite:void 0,changeOrigin:!0,secure:!0,ws:!1}}export async function setupProxy(h,d,f){if(d.writableEnded)return d;let u=h.url,g=Object.keys(f).find(e=>new RegExp(e).test(u));if(!g)return!1;let w="string"==typeof f[g]?c(f[g]):{...c(f[g].target),...f[g]},y=new r(w.target),v=u;if(w.pathRewrite&&"object"==typeof w.pathRewrite)for(let e in w.pathRewrite)Object.prototype.hasOwnProperty.call(w.pathRewrite,e)&&(v=u.replace(new RegExp(e),w.pathRewrite[e]));let x="https:"===y.protocol,R={method:h.method,hostname:y.hostname,port:y.port||(x?443:80),path:v,headers:{...h.headers},protocol:y.protocol};w.changeOrigin&&(R.headers.host=y.host);let $=x?t:e;return new Promise(e=>{let t=$(R,t=>{let p=h.method||"GET",c=new r(`${y.origin}${h.url}`).pathname,f=`${p} ${c}`,u=t.statusCode||404,g=u>=400,v=!!l(f);if(g&&v)return e(!1);if(d.writeHead(u,t.headers),!g&&!v&&w.harvester){let e=m(w.harvester)&&s(w.harvester.generate)?w.harvester.generate({method:p,pathname:c,convert:convertUriToFileName}):convertUriToFileName(f);if(e){let r=[];t.on("data",e=>r.push(e)),t.on("end",()=>{let p=Buffer.concat(r),s=t.headers["content-encoding"]||"",m=null;(m=s.includes("gzip")?n(p).toString():s.includes("br")?o(p).toString():s.includes("deflate")?i(p).toString():p.toString())&&a(`mock/${e}.ts`,`import type { MockConfiguration } from '@moneko/core';const conf: MockConfiguration = {'${f}': ${m}};export default conf;`)})}}t.pipe(d),e(d)});t.on("error",t=>(p(t),d.headersSent?d.end():(d.writeHead(502,{"Content-Type":"text/plain"}),d.end(`Proxy Error: ${t.message}`)),e(!1))),h.pipe(t)})}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@moneko/core",
3
- "version": "4.7.6",
3
+ "version": "4.7.7",
4
4
  "description": "core",
5
5
  "main": "lib/index.mjs",
6
6
  "type": "module",