@moneko/core 4.7.3-beta.1 → 4.7.3-beta.2
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 +1 -1
- package/package.json +1 -1
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 o}from"node:url";import r from"../commom/log.mjs";import{containMockUrl as
|
|
1
|
+
import{request as e}from"node:http";import{request as t}from"node:https";import{URL as o}from"node:url";import{updateFile as r}from"@moneko/utils";import n from"../commom/log.mjs";import{containMockUrl as p}from"./mock.mjs";export async function setupProxy(i,a,s){if(a.writableEnded)return a;let m=i.url,h=Object.keys(s).find(e=>new RegExp(e).test(m));if(!h)return!1;let c="string"==typeof s[h]?{target:s[h],pathRewrite:void 0,changeOrigin:!0,secure:!0,ws:!1}:s[h],f=new o(c.target),l=m;if(c.pathRewrite&&"object"==typeof c.pathRewrite)for(let e in c.pathRewrite)Object.prototype.hasOwnProperty.call(c.pathRewrite,e)&&(l=m.replace(new RegExp(e),c.pathRewrite[e]));let d="https:"===f.protocol,u={method:i.method,hostname:f.hostname,port:f.port||(d?443:80),path:l,headers:{...i.headers},protocol:f.protocol};c.changeOrigin&&(u.headers.host=f.host);let g=d?t:e;return new Promise(e=>{let t=g(u,t=>{let n=t.statusCode||404,s=n>=400,m=`${i.method||"GET"} ${new o(`${f.origin}${i.url}`).pathname}`,h=!!p(m);if(s&&h)return e(!1);if(a.writeHead(n,t.headers),!s&&!h&&c.harvester){let e=[];t.on("data",t=>e.push(t)),t.on("end",()=>{let t={" ":"➯","/":">",":":"◍"};r(`mock/${m.replace(/[\s/:]/g,e=>t[e]||e)}.ts`,`import type { MockConfiguration } from '@moneko/core';const conf: MockConfiguration = {'${m}': ${Buffer.concat(e).toString()}};export default conf;`)})}t.pipe(a),e(a)});t.on("error",t=>(n(t),a.headersSent?a.end():(a.writeHead(502,{"Content-Type":"text/plain"}),a.end(`Proxy Error: ${t.message}`)),e(!1))),i.pipe(t)})}
|