@moneko/core 4.0.0 → 4.0.1
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/mock.d.mts +2 -2
- package/lib/dev/mock.mjs +1 -1
- package/package.json +1 -1
package/lib/dev/mock.d.mts
CHANGED
|
@@ -54,9 +54,9 @@ export type YApiOption = {
|
|
|
54
54
|
*/
|
|
55
55
|
export declare const yApiMock: (req: RequestFormData, yapi: YApiOption) => Promise<Any>;
|
|
56
56
|
export declare function containMockUrl(url: string): string | undefined;
|
|
57
|
-
declare const setupMock: () => (req: RequestFormData, res: ServerResponse<IncomingMessage> & {
|
|
57
|
+
declare const setupMock: () => ((req: RequestFormData, res: ServerResponse<IncomingMessage> & {
|
|
58
58
|
req: IncomingMessage;
|
|
59
59
|
}) => boolean | (http.ServerResponse<http.IncomingMessage> & {
|
|
60
60
|
req: IncomingMessage;
|
|
61
|
-
});
|
|
61
|
+
})) | undefined;
|
|
62
62
|
export default setupMock;
|
package/lib/dev/mock.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{watch as e}from"node:fs";import t from"node:http";import{join as r}from"node:path";import{StringDecoder as o}from"node:string_decoder";import{URL as n}from"node:url";import{directoryExists as a,ink as i,print as
|
|
1
|
+
import{watch as e}from"node:fs";import t from"node:http";import{join as r}from"node:path";import{StringDecoder as o}from"node:string_decoder";import{URL as n}from"node:url";import{directoryExists as a,ink as i,print as c,scanFolderSync as s}from"@moneko/utils";import{merge as m}from"webpack-merge";import d from"../commom/log.mjs";import l from"../commom/match-path.mjs";import p from"../commom/paths.mjs";import f,{METHOD_COLOR as u}from"../commom/print-log.mjs";import h from"../commom/require.mjs";import g from"../commom/sigint-exit.mjs";import{jsonSchema as y}from"./json-schema.mjs";function b(e){return!["GET","HEAD","OPTIONS"].includes(e.method?.toUpperCase()||"")}export function cacheBody(e){if(!e._bodyCached&&b(e)&&!e.readableEnded){e._bodyCached=!0;try{let t=[],r=e.on.bind(e),o=e.emit.bind(e),n=!1;e.on=function(e,o){return"data"!==e||n||(n=!0,r("data",e=>{t.push(e)})),r(e,o)};let a=!1;e.emit=function(r,...n){return"end"===r&&!a&&(a=!0,t.length>0&&(e._dataChunks=t)),o.call(this,r,...n)}}catch(e){d(e)}}}function k(e,t){try{let r=Buffer.concat(e);if(t.includes("application/json")){let e=new o("utf-8").write(r);return JSON.parse(e)}if(t.includes("application/octet-stream")||t.startsWith("image/"));else if(t.includes("text/"))return r.toString("utf-8");else if(t.includes("application/x-www-form-urlencoded"))return Object.fromEntries(new URLSearchParams(r.toString("utf8")));return r}catch(e){return d(e),null}}async function j(e){return new Promise(t=>{if(!b(e))return void t(null);let r=e.headers["content-type"]||"",o=e.headers["content-length"],n=e.headers["transfer-encoding"],a=[];void 0!==e._dataChunks?t(k(e._dataChunks,r)):o||n?(e.on("data",e=>{a.push(e)}),e.on("end",()=>{t(k(a,r))}),e.on("error",e=>{t(null),d(e)})):t(null)})}export const yApiSchemaMock=(e,r)=>new Promise(o=>{t.get(`${e.host}/api/interface/get?id=${e.id}&token=${e.token}`,e=>{e.setEncoding("utf8");let t="";e.on("data",e=>{t+=e}),e.on("end",()=>{try{let e=JSON.parse(t);if(e?.data?.res_body_is_json_schema){let t=JSON.parse(e.data.res_body),n=y(t);void 0!==r?o(m(n,r)):o(n)}}catch(e){d(e)}})})});export const yApiMock=(e,r)=>new Promise((o,a)=>{let i=new n(r.host),c={hostname:i.hostname,port:i.port,path:e.url?.replace(new RegExp(r.pathRewrite),`/mock/${r.projectId}/`),method:e.method,headers:e.headers,query:Object.fromEntries(i.searchParams)},s=t.request(c,e=>{e.setEncoding("utf8");let t="";e.on("data",e=>{t+=e}),e.on("end",()=>{try{o(JSON.parse(t))}catch(e){d(e)}})});s.on("error",e=>{a(e.message)}),s.write(JSON.stringify(e.body)),s.end()});let w=()=>{if(!a(p.mockPath)){O={};return}let e={},t=s(p.mockPath,["\\.(t|j)s$"]);c(i("Updating mock...","245"),!0),t.forEach(t=>{try{Object.assign(e,h(t).default)}catch(e){d(e)}}),c(i("Mock update successful","245"),!0),O=e},O={};w();export function containMockUrl(e){return Object.keys(O).find(t=>RegExp(`^${t.replace(/(:\w*)[^/]/g,"((?!/).)")}*$`).test(e))}export default(()=>{if(a(p.mockPath))return g(e(p.mockPath,(e,t)=>{if(t){let e=r(p.mockPath,t);delete h.cache[e]}w()}).close),(e,t)=>{if(t.writableEnded)return t;let r=new n(`http://localhost:3000${e.url}`),o=e.method||"GET",a=`${o} ${r.pathname}`,i=containMockUrl(a)||a,c=O[i];if(c){if(f(o,r.pathname,{note:"MOCK",labelAlign:"center",labelLen:8,labelForegroundColor:u[o].fg,labelBackgroundColor:u[o].bg}),t.send=e=>t.end("string"==typeof e?e:JSON.stringify(e)),t.writeHead(200,{"Content-Type":e.headers["content-type"]}),"function"==typeof c){let o=l(i.split(" ")[1],r.pathname);r.searchParams&&Object.assign(e,{query:Object.fromEntries(r.searchParams)}),o&&Object.assign(e,{params:o.params}),e.body=j(e);try{c(e,t)}catch(e){d(e)}return!0}return t.end("string"==typeof c?c:JSON.stringify(c)),!0}return!1}});
|