ranuts 0.1.0-alpha.9 → 0.2.0-alpha.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/CLAUDE.md +164 -0
- package/README.md +116 -0
- package/{readme.md → README.zh-CN.md} +43 -12
- package/dist/bin/generate-api-docs.d.ts +1 -0
- package/dist/build/build.es.d.ts +2 -0
- package/dist/build/build.umd.d.ts +2 -0
- package/dist/build/build.umd.node.d.ts +2 -0
- package/dist/build/build.umd.utils.d.ts +2 -0
- package/dist/color-CRdlaDqQ.js +1 -0
- package/dist/examples/clone-deep-example.d.ts +1 -0
- package/dist/examples/is-equal-example.d.ts +1 -0
- package/dist/index.d.ts +5 -21
- package/dist/index.js +1 -2484
- package/dist/plugins/vite-plugins-banner.d.ts +2 -0
- package/dist/rolldown-runtime-I3TIJKkN.js +1 -0
- package/dist/src/node/appendFile.d.ts +7 -0
- package/dist/src/node/body.d.ts +9 -0
- package/dist/src/node/color.d.ts +7 -0
- package/dist/src/node/command.d.ts +8 -0
- package/dist/src/node/ctx2req.d.ts +5 -0
- package/dist/src/node/fileInfo.d.ts +7 -0
- package/dist/src/node/fs.d.ts +7 -0
- package/dist/src/node/get.d.ts +10 -0
- package/dist/src/node/getIPAdress.d.ts +1 -0
- package/dist/src/node/index.d.ts +25 -0
- package/dist/src/node/index.js +1 -0
- package/dist/src/node/isColorSupported.d.ts +2 -0
- package/dist/src/node/paresUrl.d.ts +19 -0
- package/dist/src/node/readDir.d.ts +6 -0
- package/dist/src/node/readFile.d.ts +9 -0
- package/dist/src/node/router.d.ts +42 -0
- package/dist/src/node/send.d.ts +7 -0
- package/dist/src/node/server.d.ts +18 -0
- package/dist/src/node/startTask.d.ts +2 -0
- package/dist/src/node/stream.d.ts +15 -0
- package/dist/src/node/taskEnd.d.ts +2 -0
- package/dist/src/node/traverse.d.ts +17 -0
- package/dist/src/node/watchFile.d.ts +8 -0
- package/dist/src/node/writeFile.d.ts +8 -0
- package/dist/src/node/ws.d.ts +8 -0
- package/dist/src/utils/audioRecorder.d.ts +16 -0
- package/dist/src/utils/behavior.d.ts +15 -0
- package/dist/src/utils/bom.d.ts +294 -0
- package/dist/src/utils/color.d.ts +71 -0
- package/dist/src/utils/compose.d.ts +10 -0
- package/dist/src/utils/console.d.ts +1 -0
- package/dist/src/utils/debounce.d.ts +7 -0
- package/dist/src/utils/device.d.ts +25 -0
- package/dist/src/utils/dom.d.ts +105 -0
- package/dist/src/utils/error.d.ts +1 -0
- package/dist/src/utils/func.d.ts +1 -0
- package/dist/src/utils/img.d.ts +16 -0
- package/dist/src/utils/index.d.ts +35 -0
- package/dist/src/utils/index.js +1 -0
- package/dist/src/utils/memoize.d.ts +7 -0
- package/dist/src/utils/mimeType.d.ts +9 -0
- package/dist/src/utils/monitor.d.ts +38 -0
- package/dist/src/utils/network.d.ts +39 -0
- package/dist/src/utils/noop.d.ts +2 -0
- package/dist/src/utils/number.d.ts +45 -0
- package/dist/src/utils/obj.d.ts +83 -0
- package/dist/src/utils/performance.d.ts +21 -0
- package/dist/src/utils/queue.d.ts +33 -0
- package/dist/src/utils/report.d.ts +8 -0
- package/dist/src/utils/request.d.ts +17 -0
- package/dist/src/utils/script.d.ts +7 -0
- package/dist/src/utils/signal.d.ts +6 -0
- package/dist/src/utils/storage.d.ts +2 -0
- package/dist/src/utils/str.d.ts +212 -0
- package/dist/src/utils/subscribe.d.ts +46 -0
- package/dist/src/utils/throttle.d.ts +15 -0
- package/dist/src/utils/time.d.ts +20 -0
- package/dist/src/utils/totp/sha/common.d.ts +151 -0
- package/dist/src/utils/totp/sha/converters.d.ts +88 -0
- package/dist/src/utils/totp/sha/custom_types.d.ts +60 -0
- package/dist/src/utils/totp/sha/primitives_32.d.ts +98 -0
- package/dist/src/utils/totp/sha/primitives_64.d.ts +116 -0
- package/dist/src/utils/totp/sha/sha.d.ts +103 -0
- package/dist/src/utils/totp/sha/sha1.d.ts +18 -0
- package/dist/src/utils/totp/sha/sha256.d.ts +20 -0
- package/dist/src/utils/totp/sha/sha3.d.ts +53 -0
- package/dist/src/utils/totp/sha/sha512.d.ts +21 -0
- package/dist/src/utils/totp/totp.d.ts +23 -0
- package/dist/src/utils/visual/application.d.ts +27 -0
- package/dist/src/utils/visual/enums.d.ts +27 -0
- package/dist/src/utils/visual/event/boundary.d.ts +22 -0
- package/dist/src/utils/visual/event/event.d.ts +14 -0
- package/dist/src/utils/visual/event/index.d.ts +6 -0
- package/dist/src/utils/visual/event/types.d.ts +32 -0
- package/dist/src/utils/visual/graphics/graphics.d.ts +81 -0
- package/dist/src/utils/visual/graphics/graphicsData.d.ts +13 -0
- package/dist/src/utils/visual/graphics/graphicsGeometry.d.ts +35 -0
- package/dist/src/utils/visual/graphics/index.d.ts +4 -0
- package/dist/src/utils/visual/index.d.ts +7 -0
- package/dist/src/utils/visual/index.js +1 -0
- package/dist/src/utils/visual/math/bezier.d.ts +2 -0
- package/dist/src/utils/visual/math/enums.d.ts +3 -0
- package/dist/src/utils/visual/math/index.d.ts +5 -0
- package/dist/src/utils/visual/math/matrix.d.ts +113 -0
- package/dist/src/utils/visual/math/transform.d.ts +29 -0
- package/dist/src/utils/visual/render/batchRenderer.d.ts +78 -0
- package/dist/src/utils/visual/render/canvasRenderer.d.ts +10 -0
- package/dist/src/utils/visual/render/index.d.ts +3 -0
- package/dist/src/utils/visual/render/render.d.ts +11 -0
- package/dist/src/utils/visual/render/utils/batch/index.d.ts +90 -0
- package/dist/src/utils/visual/render/utils/batch/index.test.d.ts +1 -0
- package/dist/src/utils/visual/render/utils/float.d.ts +63 -0
- package/dist/src/utils/visual/render/utils/index.d.ts +3 -0
- package/dist/src/utils/visual/render/utils/index.test.d.ts +1 -0
- package/dist/src/utils/visual/render/utils/verticy.d.ts +13 -0
- package/dist/src/utils/visual/render/utils/webgl/initShader.d.ts +7 -0
- package/dist/src/utils/visual/render/utils/webgl/shaders.d.ts +2 -0
- package/dist/src/utils/visual/render/utils/webgpu/shaders.d.ts +8 -0
- package/dist/src/utils/visual/render/webGPURenderer.d.ts +49 -0
- package/dist/src/utils/visual/render/webGlRenderer.d.ts +14 -0
- package/dist/src/utils/visual/shape/circle.d.ts +11 -0
- package/dist/src/utils/visual/shape/ellipse.d.ts +12 -0
- package/dist/src/utils/visual/shape/index.d.ts +7 -0
- package/dist/src/utils/visual/shape/polygon.d.ts +11 -0
- package/dist/src/utils/visual/shape/rectangle.d.ts +12 -0
- package/dist/src/utils/visual/shape/roundedRectangle.d.ts +13 -0
- package/dist/src/utils/visual/shape/shape.d.ts +6 -0
- package/dist/src/utils/visual/style/fill.d.ts +8 -0
- package/dist/src/utils/visual/style/index.d.ts +3 -0
- package/dist/src/utils/visual/style/line.d.ts +10 -0
- package/dist/src/utils/visual/types.d.ts +18 -0
- package/dist/src/utils/visual/vertex/container.d.ts +74 -0
- package/dist/src/utils/visual/vertex/point.d.ts +18 -0
- package/dist/src/utils/visual/vertex/vertex.d.ts +36 -0
- package/dist/src/vnode/chainDom.d.ts +47 -0
- package/dist/src/vnode/h.d.ts +6 -0
- package/dist/src/vnode/hooks.d.ts +23 -0
- package/dist/src/vnode/htmlDomApi.d.ts +33 -0
- package/dist/src/vnode/index.d.ts +18 -0
- package/dist/src/vnode/index.js +1 -0
- package/dist/src/vnode/init.d.ts +2 -0
- package/dist/src/vnode/is.d.ts +5 -0
- package/dist/src/vnode/modules/attributes.d.ts +8 -0
- package/dist/src/vnode/modules/class.d.ts +8 -0
- package/dist/src/vnode/modules/index.d.ts +3 -0
- package/dist/src/vnode/modules/listeners.d.ts +12 -0
- package/dist/src/vnode/modules/props.d.ts +8 -0
- package/dist/src/vnode/modules/style.d.ts +14 -0
- package/dist/src/vnode/vnode.d.ts +31 -0
- package/dist/subscribe-CINWsor3.js +1 -0
- package/dist/test/bridge.test.d.ts +1 -0
- package/dist/test/clearBr.test.d.ts +1 -0
- package/dist/test/cloneDeep.test.d.ts +1 -0
- package/dist/test/command.test.d.ts +1 -0
- package/dist/test/encodeUrl.test.d.ts +1 -0
- package/dist/test/escapeHtml.test.d.ts +1 -0
- package/dist/test/is-equal.test.d.ts +1 -0
- package/dist/test/mimeType.test.d.ts +1 -0
- package/dist/test/package-exports.test.d.ts +1 -0
- package/dist/test/server.test.d.ts +1 -0
- package/dist/test/status.test.d.ts +1 -0
- package/dist/test/totp.test.d.ts +1 -0
- package/dist/test/utils/compose.test.d.ts +1 -0
- package/dist/test/utils/mergeObj.test.d.ts +1 -0
- package/dist/test/visual/math.test.d.ts +1 -0
- package/dist/test/visual-application.test.d.ts +1 -0
- package/dist/test/visual-batch.test.d.ts +1 -0
- package/dist/test/visual-math.test.d.ts +1 -0
- package/dist/test/vnode/index.d.ts +1 -0
- package/dist/test/vnode/server.d.ts +2 -0
- package/dist/test/vnode/vnode.d.ts +1 -0
- package/dist/test/vnode.test.d.ts +1 -0
- package/dist/test/websocket.test.d.ts +1 -0
- package/dist/test/writeFile.test.d.ts +1 -0
- package/dist/umd/index.umd.cjs +1 -0
- package/dist/umd/node/node.umd.cjs +1 -0
- package/dist/umd/utils/utils.umd.cjs +1 -0
- package/dist/utils-_OxsVZmz.js +1 -0
- package/dist/vite.config.d.ts +8 -0
- package/dist/vitest.config.d.ts +2 -0
- package/docs/API.md +305 -0
- package/package.json +67 -15
- package/typings.d.ts +28 -0
- package/dist/index.umd.cjs +0 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
var e=Object.defineProperty,r=(e,r)=>()=>(r||(e((r={exports:{}}).exports,r),e=null),r.exports),t=(r,t)=>{let o={};for(var a in r)e(o,a,{get:r[a],enumerable:!0});return t||e(o,Symbol.toStringTag,{value:"Module"}),o};export{t as n,r as t};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { MiddlewareFunction } from '@/node/server';
|
|
2
|
+
interface ServerBody {
|
|
3
|
+
uploadDir: string;
|
|
4
|
+
encoding: BufferEncoding;
|
|
5
|
+
urlencoded: boolean;
|
|
6
|
+
json: boolean;
|
|
7
|
+
}
|
|
8
|
+
declare const bodyMiddleware: (options?: Partial<ServerBody>) => MiddlewareFunction;
|
|
9
|
+
export default bodyMiddleware;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export declare const runCommand: (command: string, args: string[]) => Promise<void>;
|
|
2
|
+
interface PromptOption {
|
|
3
|
+
message: string;
|
|
4
|
+
defaultResponse: string;
|
|
5
|
+
stream: NodeJS.ReadWriteStream;
|
|
6
|
+
}
|
|
7
|
+
export declare const prompt: ({ message, stream, defaultResponse }: PromptOption) => Promise<boolean>;
|
|
8
|
+
export {};
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { IncomingMessage, ServerResponse } from 'node:http';
|
|
2
|
+
import type { MiddlewareFunction } from '@/node/server';
|
|
3
|
+
type ConnectMiddleware = (req: IncomingMessage, res: ServerResponse, next?: Function) => void;
|
|
4
|
+
declare function connect(connectMiddleware: ConnectMiddleware): MiddlewareFunction;
|
|
5
|
+
export default connect;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const getIPAdress: () => string | undefined;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import Server from './server';
|
|
2
|
+
import { appendFile } from './appendFile';
|
|
3
|
+
import colors from './color';
|
|
4
|
+
import body from './body';
|
|
5
|
+
import { prompt, runCommand } from './command';
|
|
6
|
+
import connect from './ctx2req';
|
|
7
|
+
import queryFileInfo from './fileInfo';
|
|
8
|
+
import get from './get';
|
|
9
|
+
import { getIPAdress } from './getIPAdress';
|
|
10
|
+
import isColorSupported from './isColorSupported';
|
|
11
|
+
import paresUrl from './paresUrl';
|
|
12
|
+
import readDir from './readDir';
|
|
13
|
+
import readFile from './readFile';
|
|
14
|
+
import Router from './router';
|
|
15
|
+
import staticMiddleware from './send';
|
|
16
|
+
import startTask from './startTask';
|
|
17
|
+
import { readStream, writeStream } from './stream';
|
|
18
|
+
import taskEnd from './taskEnd';
|
|
19
|
+
import { traverse, traverseSync } from './traverse';
|
|
20
|
+
import watchFile from './watchFile';
|
|
21
|
+
import WSS from './ws';
|
|
22
|
+
import writeFile from './writeFile';
|
|
23
|
+
import type { Context } from './server';
|
|
24
|
+
export { appendFile, colors, body, prompt, runCommand, connect, queryFileInfo, get, getIPAdress, isColorSupported, paresUrl, readDir, Router, readFile, staticMiddleware, Server, startTask, readStream, writeStream, taskEnd, traverse, traverseSync, watchFile, WSS, writeFile, };
|
|
25
|
+
export type { Context };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{P as e,S as t,r as n,x as r}from"../../color-CRdlaDqQ.js";import{Socket as o}from"node:net";import s from"node:http";import i from"node:os";import a,{createReadStream as c,createWriteStream as d,readdir as l,readdirSync as u,stat as f,statSync as h}from"node:fs";import p from"node:tty";import m,{hrtime as w}from"node:process";import g from"node:child_process";import y from"node:readline";import b from"node:https";import E,{join as v,resolve as x}from"node:path";import{promisify as S}from"node:util";import{Buffer as T}from"node:buffer";import P from"node:crypto";function O(){const e=i.networkInterfaces();for(const t in e){const n=e[t];if(n)for(let e=0;e<n.length;e++){const t=n[e];if("IPv4"===t.family&&"127.0.0.1"!==t.address&&!t.internal)return t.address}}}var q=class{middleware;ctx;constructor(){const e=new s.IncomingMessage(new o),t=new s.ServerResponse(e);this.ctx={ipv4:O,req:e,res:t},this.middleware=[]}use(e){if(!e)throw new Error("the use function has an incorrect argument");this.middleware.push(e)}listen(...e){const t=function(e){if(!Array.isArray(e))throw new TypeError("Middleware stack must be an array!");for(const t of e)if("function"!=typeof t)throw new TypeError("Middleware must be composed of functions!");return function(t,n){let r=-1;function o(s){if(s<=r)return Promise.reject(/* @__PURE__ */new Error("next() called multiple times"));r=s;let i=e[s];if(s===e.length&&n&&(i=n),!i)return Promise.resolve();try{return Promise.resolve(i(t,o.bind(null,s+1)))}catch(a){return Promise.reject(a)}}return o(0)}}(this.middleware);return s.createServer((e,n)=>{this.ctx.req=e,this.ctx.res=n,t(this.ctx).catch(C)}).listen(...e)}};function C(e){console.error(e.stack||e.toString())}var H=a,M=(e,t="utf-8")=>{const n=new AbortController,r=n.signal,o=new Promise((o,s)=>{H.readFile(e,{encoding:t,signal:r},(e,t)=>{e?(n.abort(),s({success:!1,_identification:!1,data:e})):o({success:!0,_identification:!0,data:t})})});return o.abort=n.abort,o},_=(e,t)=>new Promise((n,r)=>{H.appendFile(e,t,t=>{t?r({success:!1,_identification:!1,data:t}):M(e).then(e=>{n(e)})})}),F=m?.env&&!("NO_COLOR"in m.env||m.argv.includes("--no-color"))&&("FORCE_COLOR"in m.env||m.argv.includes("--color")||"win32"===m.platform||p.isatty(1)&&"dumb"!==m.env.TERM||"CI"in m.env),U=(e,t,n=e)=>r=>{const o=""+r,s=o.indexOf(t,e.length);return~s?e+j(o,t,n,s)+t:e+o+t},j=(e,t,n,r)=>{const o=e.substring(0,r)+n,s=e.substring(r+t.length),i=s.indexOf(t);return~i?o+j(s,t,n,i):o+s},k=Object.assign({},((e=F)=>{const t={};return Object.keys(n).forEach(r=>t[r]=e?U(n[r][0],n[r][1]):String),t})(),((e=F)=>({isColorSupported:e,reset:e?e=>`${n.reset[0]}${e}${n.reset[1]}`:String,bold:e?U(n.bold[0],n.bold[1],`${n.bold[1]}${n.bold[0]}`):String,dim:e?U(n.dim[0],n.dim[1],`${n.dim[1]}${n.dim[0]}`):String}))()),$=(t={})=>{const{uploadDir:n=".",encoding:r="",json:o=!0}=t;return(t,s)=>{const{req:i,res:c}=t,{url:d,method:l}=i,[u="",f=""]=d?.split("?")||[],h=f?new URLSearchParams(f):{};t.request||(t.request={}),t.request.method=l,t.request.path=u,t.request.url=d,t.request.query=h;const p=i.headers["content-type"];if("application/json"===p){r?i.setEncoding(r):i.setEncoding("utf-8");let e="";i.on("data",t=>{e+=t}),i.on("end",()=>{c.setHeader("content-type","application/json;charset=UTF-8");try{t.request.body=o?JSON.parse(e):e}catch{t.request.body=e}s()})}if(p?.includes("multipart/form-data;")){const[t,o]=i.headers["content-type"]?.split(";").map(e=>e.trim())||[],[d,l]=o.split("=");r?i.setEncoding(r):i.setEncoding("binary");let u="";i.on("data",e=>{u+=e}),i.on("end",()=>{const t={};u.split("\r\n").slice(0,4).forEach(n=>{Object.assign(t,e(n,";",/=|:/))});const r=t["Content-Type"],o=Buffer.from(t.filename,"latin1").toString("utf8"),i=u.indexOf(r)+r.length,d=u.substring(i).replace(/^\s+/,""),f=d.substring(0,d.indexOf(`--${l}--`));a.writeFile(`${n}/${o}`,f,{encoding:"binary"},e=>{if(e)throw e;c.setHeader("content-type","application/json;charset=UTF-8"),s()})})}}},I=(e,t)=>new Promise((n,r)=>{const o=g.spawn(e,t,{stdio:"inherit",shell:!1});o.on("error",e=>r(e)),o.on("exit",e=>0===e?n():r(/* @__PURE__ */new Error(`Command exited with code ${e}`)))}),A=({message:e,stream:t=process.stderr,defaultResponse:n="Y"})=>{const r=y.createInterface({input:process.stdin,output:t});return new Promise(t=>{r.question(`${e} `,e=>{r.close();const o=(e||n).toLowerCase();t("y"===o||"yes"===o)})})},B=()=>{};function D(e,t,n){return t(e.req,e.res,B),n()}function L(e,t,n){return new Promise((r,o)=>{t(e.req,e.res,e=>{e?o(e):r(n())})})}function R(e){const t=e.length<3?D:L;return function(n,r){return t(n,e,r)}}var N=e=>new Promise((t,n)=>{H.stat(e,(e,r)=>{e?n({success:!1,_identification:!1,data:e}):t({success:!0,_identification:!0,data:r})})}),K=({url:e})=>new Promise((t,n)=>{b.get(e,e=>{const n=[];e.on("data",e=>{n.push(e)}),e.on("end",()=>{const{data:e=[]}=JSON.parse(Buffer.concat(n).toString());e.forEach(e=>{}),t({success:!0,data:e,message:"request success"})})}).on("error",e=>{e.message,n({success:!1,data:e,message:e.message})})}),z=()=>{const e=i.networkInterfaces();for(const t in e){const n=e[t];for(let e=0;e<n.length;e++){const t=n[e];if("IPv4"===t.family&&"127.0.0.1"!==t.address&&!t.internal)return t.address}}};function J(e){const t=e.url;if(void 0===t)return;let n=e._parsedUrl;if(n&&n._raw===t)return n;n={},n.query=n.search=void 0,n.href=n.path=n.pathname=t;const r=t.indexOf("?",1);return-1!==r&&(n.search=t.substring(r),n.query=n.search.substring(1),n.pathname=t.substring(0,r)),n._raw=t,e._parsedUrl=n}var W=e=>{const{dirPath:t}=e;try{return H.readdirSync(t)}catch(n){throw n}},G=class{ctx;map;methods;paths;constructor(){this.map=/* @__PURE__ */new Map,this.methods=/* @__PURE__ */new Set,this.paths=/* @__PURE__ */new Set}get(e,t){this.addHandlerToMap("GET")(e,t)}post(e,t){this.addHandlerToMap("POST")(e,t)}put(e,t){this.addHandlerToMap("PUT")(e,t)}patch(e,t){this.addHandlerToMap("PATCH")(e,t)}del(e,t){this.addHandlerToMap("DELETE")(e,t)}head(e,t){this.addHandlerToMap("HEAD")(e,t)}options(e,t){this.addHandlerToMap("OPTIONS")(e,t)}addHandlerToMap(e){return this.methods.add(e),(t,n)=>{this.paths.add(t);let r=this.map.get(e);r||(r=/* @__PURE__ */new Map,this.map.set(e,r)),r.set(t,n)}}routes(){return(e,t)=>{this.ctx=e;const{path:n,method:r}=e.request,o=this.map.get(r),s=o?o.get(n):t;s&&s(e,t)}}allowedMethods(){return e=>{const{res:t}=e,{path:n,method:r}=e.request;!this.methods.has(r)&&this.paths.has(n)&&(t.statusCode=405,t.end("405, method is not allowed")),this.methods.has(r)&&!this.paths.has(n)&&(t.statusCode=404,t.end("404, the request address does not exist")),this.methods.has(r)||this.paths.has(n)||(t.statusCode=501,t.end("501, not support the functionality needed to satisfy the request"))}}},Y=(e={})=>{const{pathname:n,fileTypes:o={}}=e;return async(e,s)=>{try{const{req:i,res:c}=e;if(i.url){const e="text/html",s=n||process.cwd(),d=E.normalize(E.resolve(s)),l=E.extname(i.url).slice(1);Object.keys(o).forEach(e=>t(e,o[e]));const u=l?r(l):e;if(!Boolean(u))return c.writeHead(404,{"Content-Type":"text/html"}),void c.end("404: File not found");let f=i.url;if("/"===i.url)f="index.html";else if(!l)try{a.accessSync(E.join(d,i.url+".html"),a.constants.F_OK),f=i.url+".html"}catch{f=E.join(i.url,"index.html")}const h=E.join(d,f);if(!E.normalize(E.resolve(h)).startsWith(d))return c.writeHead(404,{"Content-Type":"text/html"}),void c.end("404: File not found");a.readFile(h,(e,t)=>{e?(c.writeHead(404,{"Content-Type":"text/html"}),c.end("404: File not found")):(c.writeHead(200,{"Content-Type":u}),c.end(t))})}await s()}catch{}}},Q=()=>{if("undefined"!=typeof window){const e=window.performance.now();return Symbol.for(`${e}`)}if("undefined"!=typeof process){const e=w.bigint();return Symbol.for(`${e}`)}return Symbol.for(`${Date.now()}`)},V=e=>{const{path:t}=e;if(t)return c(t);throw new Error("path is not defined")},X=e=>{const{path:t}=e;if(t)return d(t);throw new Error("path is not defined")},Z=e=>{const t=Symbol.keyFor(e);return"undefined"!=typeof window&&t?window.performance.now()-Number(t):"undefined"!=typeof process&&t?w.bigint()-BigInt(t):Date.now()-Number(t)},ee=S(f),te=S(l);async function ne(e,t,n=""){e=x(".",e);const r=await te(e);await Promise.all(r.map(async r=>{const o=v(e,r),s=await ee(o);return s.isDirectory()?ne(o,t,v(n,r)):t(v(n,r),o,s)}))}function re(e,t,n=""){e=x(".",e);const r=u(e);let o,s,i=0;for(;i<r.length;i++)o=v(e,r[i]),s=h(o),s.isDirectory()?re(o,t,v(n,r[i])):t(v(n,r[i]),o,s)}var oe=(e,t=20)=>new Promise(n=>{H.watchFile(e,{interval:t},(t,r)=>{t.mtime!==r.mtime?(H.unwatchFile(e),n({success:!0,_identification:!0,data:{},message:"file is changed"})):n({success:!1,_identification:!1,data:{},message:"file is not changed"})})});function se(e,t){const n=T.byteLength(e);let r=null;if(n>65535?(r=T.alloc(10+n),r[1]=127,r.writeUInt32BE(n,6),r.write(e,10)):n>125?(r=T.alloc(4+n),r[1]=126,r.writeUInt16BE(n,2),r.write(e,4)):(r=T.alloc(2+n),r[1]=n,r.write(e,2)),t){const e=t.opcode;e&&e<15&&e>=0&&(r[0]=128|e)}else r[0]=129;return r}function ie(e,t){if(!t)return e;const n=T.from(e);for(let r=0;r<n.length;++r)n[r]=n[r]^t[r%4];return n}function ae(){const e={};this.on=(t,n)=>{e[t]?e[t].push(n):e[t]=[n]},this.off=(t,n)=>{const r=e[t],o=r.indexOf(n);r&&-1!==o&&r.splice(o,1)},this.emit=(t,n)=>{e[t]&&e[t].forEach(e=>e(n))}}function ce(e){ae.call(this);const t=[];this.server=e,this.clients=t,this.broadcast=e=>{t.forEach(t=>{t.send(e)})},e.on("upgrade",(e,n)=>{n.write(["HTTP/1.1 101 Switching Protocols","Upgrade: websocket","Connection: Upgrade","Sec-WebSocket-Accept: "+P.createHash("sha1").update(e.headers["sec-websocket-key"]+"258EAFA5-E914-47DA-95CA-C5AB0DC85B11").digest("base64")].join("\n")+"\n\n");const r={send(e,t){n.write(se(e,t))},ping(){n.write(se("",{opcode:9}))},pong(){n.write(se("",{opcode:10}))},close(){if(-1===t.indexOf(r))return!1;t.splice(t.indexOf(r),1),n.write(se("",{opcode:8})),r.emit("close"),n.destroy()},socket:n};ae.call(r),this.emit("connect",r),t.push(r);let o=T.allocUnsafe(0),s=T.allocUnsafe(0);const i=[];let a=!0;function c(){const e=function(e){const t=T.from(e),n=127&t[1];let r=0,o=0;const s=t[1]>=128;return 127===n?(r=t.readUInt32BE(6),o=10):126===n?(r=t.readUInt16BE(2),o=4):(r=n,o=2),{fin:t[0]>=128,opcode:15&t[0],mask:s,maskKey:s?t.subarray(o,o+(s?4:0)):null,len7:n,len:r,lenMeta:o+(s?4:0)}}(o);i.push({l:e.lenMeta,f:()=>{a=!1,i.push({l:e.len,f:t=>{if(s=T.concat([s,ie(t,e.maskKey)]),a=!0,e.fin){if(8===e.opcode)return r.close();if(9===e.opcode)return r.pong();r.emit("message",s),s=T.allocUnsafe(0)}}})}})}n.on("data",e=>{for(o=T.concat([o,e]),a&&c();i[0]&&o.byteLength>=i[0].l;){const e=i[0].l;i[0].f(o.subarray(0,e)),o=o.subarray(e),i.splice(0,1)}}),n.on("end",()=>{r.close()}),n.on("error",e=>{r.emit("error",e),r.close()})})}var de=(e,t)=>new Promise((n,r)=>{H.writeFile(e,t,{mode:438,flag:"w+",encoding:"utf-8"},o=>{if(o)throw r({success:!1,_identification:!1,data:o}),o;n({success:!0,_identification:!1,data:{path:e,content:t}})})});export{G as Router,q as Server,ce as WSS,_ as appendFile,$ as body,k as colors,R as connect,K as get,z as getIPAdress,F as isColorSupported,J as paresUrl,A as prompt,N as queryFileInfo,W as readDir,M as readFile,V as readStream,I as runCommand,Q as startTask,Y as staticMiddleware,Z as taskEnd,ne as traverse,re as traverseSync,oe as watchFile,de as writeFile,X as writeStream};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { IncomingMessage } from 'node:http';
|
|
2
|
+
interface ParseUrl {
|
|
3
|
+
search?: string;
|
|
4
|
+
query?: string;
|
|
5
|
+
pathname?: string;
|
|
6
|
+
path?: string;
|
|
7
|
+
href?: string;
|
|
8
|
+
_raw?: string;
|
|
9
|
+
}
|
|
10
|
+
interface Req extends IncomingMessage {
|
|
11
|
+
_parsedUrl?: ParseUrl;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* @description: 解析 IncomingMessage 类型的请求url,返回的类型永远是 ParseUrl
|
|
15
|
+
* @param {Req} req
|
|
16
|
+
* @return {ParseUrl}
|
|
17
|
+
*/
|
|
18
|
+
export default function (req: Req): ParseUrl | undefined;
|
|
19
|
+
export {};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { FilePromiseResult } from '@/node/fs';
|
|
2
|
+
/**
|
|
3
|
+
* @description: 读取一个文件,读取成功返回状态码和文件内容
|
|
4
|
+
* @param {string} path 文件路径
|
|
5
|
+
* @param {string} format 读取格式,默认utf-8
|
|
6
|
+
* @return {Promise}
|
|
7
|
+
*/
|
|
8
|
+
declare const readFile: (path: string, format?: BufferEncoding) => FilePromiseResult;
|
|
9
|
+
export default readFile;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { Context, MiddlewareFunction, Next } from '@/node/server';
|
|
2
|
+
/**
|
|
3
|
+
* @description:
|
|
4
|
+
*
|
|
5
|
+
* Basic usage:
|
|
6
|
+
*
|
|
7
|
+
* ```javascript
|
|
8
|
+
* import Server from '@/server/server'
|
|
9
|
+
* import Router from '@/server/router'
|
|
10
|
+
*
|
|
11
|
+
* const app = new Server();
|
|
12
|
+
* const router = new Router();
|
|
13
|
+
*
|
|
14
|
+
* router.get('/', (ctx, next) => {
|
|
15
|
+
* // ctx.router available
|
|
16
|
+
* });
|
|
17
|
+
*
|
|
18
|
+
* app
|
|
19
|
+
* .use(router.routes())
|
|
20
|
+
* .use(router.allowedMethods());
|
|
21
|
+
* ```
|
|
22
|
+
* @return {*}
|
|
23
|
+
*/
|
|
24
|
+
type Handler = (ctx: Context, next: Next) => void;
|
|
25
|
+
declare class Router {
|
|
26
|
+
ctx?: Context;
|
|
27
|
+
map: Map<string, Map<string, Handler>>;
|
|
28
|
+
methods: Set<string>;
|
|
29
|
+
paths: Set<string>;
|
|
30
|
+
constructor();
|
|
31
|
+
get(url: string, handler: Handler): void;
|
|
32
|
+
post(url: string, handler: Handler): void;
|
|
33
|
+
put(url: string, handler: Handler): void;
|
|
34
|
+
patch(url: string, handler: Handler): void;
|
|
35
|
+
del(url: string, handler: Handler): void;
|
|
36
|
+
head(url: string, handler: Handler): void;
|
|
37
|
+
options(url: string, handler: Handler): void;
|
|
38
|
+
private addHandlerToMap;
|
|
39
|
+
routes(): MiddlewareFunction;
|
|
40
|
+
allowedMethods(): MiddlewareFunction;
|
|
41
|
+
}
|
|
42
|
+
export default Router;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { IncomingMessage, ServerResponse } from 'node:http';
|
|
2
|
+
import http from 'node:http';
|
|
3
|
+
export type Next = () => Promise<never> | Promise<void>;
|
|
4
|
+
export type MiddlewareFunction = (ctx: Context, next: Next) => void | Promise<void>;
|
|
5
|
+
export interface Context {
|
|
6
|
+
[x: string]: any;
|
|
7
|
+
ipv4: () => string | undefined;
|
|
8
|
+
req: IncomingMessage;
|
|
9
|
+
res: ServerResponse;
|
|
10
|
+
}
|
|
11
|
+
declare class Server {
|
|
12
|
+
middleware: Array<MiddlewareFunction>;
|
|
13
|
+
ctx: Context;
|
|
14
|
+
constructor();
|
|
15
|
+
use(handle: MiddlewareFunction): void;
|
|
16
|
+
listen(...args: any): http.Server;
|
|
17
|
+
}
|
|
18
|
+
export default Server;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { ReadStream, WriteStream } from 'node:fs';
|
|
2
|
+
interface ReadOption {
|
|
3
|
+
path: string;
|
|
4
|
+
encoding: string;
|
|
5
|
+
start: number;
|
|
6
|
+
end: number;
|
|
7
|
+
highWaterMark: number;
|
|
8
|
+
}
|
|
9
|
+
interface WriteOption {
|
|
10
|
+
path: string;
|
|
11
|
+
encoding: string;
|
|
12
|
+
}
|
|
13
|
+
export declare const readStream: (option: ReadOption) => ReadStream;
|
|
14
|
+
export declare const writeStream: (option: WriteOption) => WriteStream;
|
|
15
|
+
export {};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { Stats } from 'node:fs';
|
|
2
|
+
type Caller = (relPath: string, absPath: string, stats: Stats) => any;
|
|
3
|
+
/**
|
|
4
|
+
* @description: 递归遍历每一个目录,为找到的文件都执行一个函数
|
|
5
|
+
* @param {string} dir
|
|
6
|
+
* @param {Caller} callback
|
|
7
|
+
* @param {*} pre
|
|
8
|
+
*/
|
|
9
|
+
export declare function traverse(dir: string, callback: Caller, pre?: string): Promise<any>;
|
|
10
|
+
/**
|
|
11
|
+
* @description: 同步方法,递归遍历每一个目录,为找到的文件都执行一个函数
|
|
12
|
+
* @param {string} dir
|
|
13
|
+
* @param {Caller} callback
|
|
14
|
+
* @param {*} pre
|
|
15
|
+
*/
|
|
16
|
+
export declare function traverseSync(dir: string, callback: Caller, pre?: string): void;
|
|
17
|
+
export {};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @description: 观察一个文件是否被改变,返回状态
|
|
3
|
+
* @param {string} path 监听的文件路径
|
|
4
|
+
* @param {number} interval 监听的时间,单位毫秒,默认 20 毫秒
|
|
5
|
+
* @return {Promise}
|
|
6
|
+
*/
|
|
7
|
+
declare const watchFile: (path: string, interval?: number) => Promise<Ranuts.Identification>;
|
|
8
|
+
export default watchFile;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @description: 根据文件路径创建文件,如果文件存在会清空再写入,如果不存在会创建
|
|
3
|
+
* @param {string} path 文件路径
|
|
4
|
+
* @param {string} content 文件内容
|
|
5
|
+
* @return {Promise}
|
|
6
|
+
*/
|
|
7
|
+
declare const writeFile: (path: string, content: string) => Promise<Ranuts.Identification>;
|
|
8
|
+
export default writeFile;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export declare const webmFixDuration: (blob: Blob, duration: number, type?: string) => Promise<Blob>;
|
|
2
|
+
/**
|
|
3
|
+
* @description: 录音
|
|
4
|
+
* @return {*}
|
|
5
|
+
*/
|
|
6
|
+
export declare class AudioRecorder {
|
|
7
|
+
startTime: number;
|
|
8
|
+
chunks: Blob[];
|
|
9
|
+
mediaRecorder?: MediaRecorder;
|
|
10
|
+
blob?: Blob;
|
|
11
|
+
constructor();
|
|
12
|
+
private init;
|
|
13
|
+
start(): MediaRecorder | undefined;
|
|
14
|
+
pause(): MediaRecorder | undefined;
|
|
15
|
+
stop(): Blob | undefined;
|
|
16
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export declare const handleClick: (hooks?: (event: MouseEvent) => void) => void;
|
|
2
|
+
/**
|
|
3
|
+
* @description: 获取地区经纬度(会弹出提示让用户授权)
|
|
4
|
+
* @return {*}
|
|
5
|
+
*/
|
|
6
|
+
export declare const getRegionalLatitudeAndLongitude: () => Promise<GeolocationPosition>;
|
|
7
|
+
export declare const webglVendor: () => {
|
|
8
|
+
vendor: string;
|
|
9
|
+
renderer: string;
|
|
10
|
+
} | null;
|
|
11
|
+
export declare const canvasVendor: () => string | null;
|
|
12
|
+
/**
|
|
13
|
+
* @description: 音频指纹,1.生成音频信息流 (三角波),对其进行 FFT 变换,计算 SHA 值作为指纹。2.生成音频信息流(正弦波),进行动态压缩处理,计算 MD5 值。
|
|
14
|
+
*/
|
|
15
|
+
export declare const audioVendor: () => Promise<string>;
|
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @description: 覆盖浏览器的后退事件
|
|
3
|
+
* @param {*} callback
|
|
4
|
+
* @return {*}
|
|
5
|
+
*/
|
|
6
|
+
export declare const retain: (callback?: () => void) => void;
|
|
7
|
+
/**
|
|
8
|
+
* @description: 获取指定的 cookie
|
|
9
|
+
* @param {string} objName
|
|
10
|
+
* @return {*}
|
|
11
|
+
*/
|
|
12
|
+
export declare const getCookie: (objName: string) => string;
|
|
13
|
+
export interface RequestUrlToArraybufferOption {
|
|
14
|
+
responseType: XMLHttpRequestResponseType;
|
|
15
|
+
method: string;
|
|
16
|
+
withCredentials: boolean;
|
|
17
|
+
headers: Record<string, string>;
|
|
18
|
+
body: string;
|
|
19
|
+
onProgress?: Function;
|
|
20
|
+
}
|
|
21
|
+
export interface requestUrlToArraybufferReturn extends BaseReturn {
|
|
22
|
+
data: Blob;
|
|
23
|
+
}
|
|
24
|
+
export interface BaseReturn {
|
|
25
|
+
success: boolean;
|
|
26
|
+
message?: string;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* @description: url 转 arrayBuffer
|
|
30
|
+
* @param {string} src
|
|
31
|
+
* @param {RequestUrlToArraybufferOption} options
|
|
32
|
+
* @return {*}
|
|
33
|
+
*/
|
|
34
|
+
export declare const requestUrlToBuffer: (src: string, options: Partial<RequestUrlToArraybufferOption>) => Promise<requestUrlToArraybufferReturn>;
|
|
35
|
+
export interface Context {
|
|
36
|
+
backingStorePixelRatio: number;
|
|
37
|
+
webkitBackingStorePixelRatio: number;
|
|
38
|
+
mozBackingStorePixelRatio: number;
|
|
39
|
+
msBackingStorePixelRatio: number;
|
|
40
|
+
oBackingStorePixelRatio: number;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* @description: 获取分辨率
|
|
44
|
+
* @param {CanvasRenderingContext2D} context
|
|
45
|
+
* @return {*}
|
|
46
|
+
*/
|
|
47
|
+
export declare const getPixelRatio: (context: CanvasRenderingContext2D & Partial<Context>) => number;
|
|
48
|
+
export declare const createObjectURL: (src: Blob | ArrayBuffer | Response) => Promise<string>;
|
|
49
|
+
/**
|
|
50
|
+
* @description: 计算每毫秒的帧率,每秒的帧率需要乘 1000
|
|
51
|
+
* @return {*}
|
|
52
|
+
*/
|
|
53
|
+
export declare const getFrame: (n?: number) => Promise<number>;
|
|
54
|
+
/**
|
|
55
|
+
* @description: Gets the current environment configuration
|
|
56
|
+
* @return {string}
|
|
57
|
+
*/
|
|
58
|
+
export declare const getHost: (env?: string) => string | undefined;
|
|
59
|
+
/**
|
|
60
|
+
* @description: 将 url 上的字符串转换成对象
|
|
61
|
+
* @param {string} url
|
|
62
|
+
* @param {*} string
|
|
63
|
+
* @return {*}
|
|
64
|
+
*/
|
|
65
|
+
export declare const getAllQueryString: (url?: string) => Record<string, string>;
|
|
66
|
+
/**
|
|
67
|
+
* @description: 将 url 上的字符串转换成对象
|
|
68
|
+
* @param {string} url
|
|
69
|
+
* @param {*} string
|
|
70
|
+
* @return {*}
|
|
71
|
+
*/
|
|
72
|
+
export declare const getQuery: (url?: string) => Record<string, string>;
|
|
73
|
+
/**
|
|
74
|
+
* @description: 将一个对象转换成 querystring,拼接到 url 后面
|
|
75
|
+
* @return {*}
|
|
76
|
+
*/
|
|
77
|
+
export declare function appendUrl(url: string, params?: Record<string, string>): string;
|
|
78
|
+
/**
|
|
79
|
+
* @description: 移除拖拽事件的阴影
|
|
80
|
+
* @param {DragEvent} event
|
|
81
|
+
* @return {*}
|
|
82
|
+
*/
|
|
83
|
+
export declare const removeGhosting: (event: DragEvent) => void;
|
|
84
|
+
export declare function getCookieByName(name: string): string;
|
|
85
|
+
interface ClientRatio {
|
|
86
|
+
width: number;
|
|
87
|
+
height: number;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* 跨浏览器获取可视窗口大小
|
|
91
|
+
*/
|
|
92
|
+
export declare const getWindow: () => ClientRatio;
|
|
93
|
+
/**
|
|
94
|
+
* @description: 返回当前网络状态,当前吞吐量,是否切换网络
|
|
95
|
+
*/
|
|
96
|
+
export declare const connection: () => number | undefined;
|
|
97
|
+
/**
|
|
98
|
+
* Encode a URL to a percent-encoded form, excluding already-encoded sequences.
|
|
99
|
+
*
|
|
100
|
+
* This function will take an already-encoded URL and encode all the non-URL
|
|
101
|
+
* code points. This function will not encode the "%" character unless it is
|
|
102
|
+
* not part of a valid sequence (`%20` will be left as-is, but `%foo` will
|
|
103
|
+
* be encoded as `%25foo`).
|
|
104
|
+
*
|
|
105
|
+
* This encode is meant to be "safe" and does not throw errors. It will try as
|
|
106
|
+
* hard as it can to properly encode the given URL, including replacing any raw,
|
|
107
|
+
* unpaired surrogate pairs with the Unicode replacement character prior to
|
|
108
|
+
* encoding.
|
|
109
|
+
*
|
|
110
|
+
* @param {string} url
|
|
111
|
+
* @return {string}
|
|
112
|
+
* @public
|
|
113
|
+
*/
|
|
114
|
+
export declare function encodeUrl(url: string): string;
|
|
115
|
+
interface Options {
|
|
116
|
+
url?: string;
|
|
117
|
+
duration?: number;
|
|
118
|
+
count?: number;
|
|
119
|
+
}
|
|
120
|
+
interface ReturnType {
|
|
121
|
+
ping: number;
|
|
122
|
+
jitter: number;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* @description: 图片请求
|
|
126
|
+
* @param {string} url
|
|
127
|
+
* @return {Promise<ImageLoadError | number>}
|
|
128
|
+
*/
|
|
129
|
+
export declare const imageRequest: (url?: string) => Promise<number>;
|
|
130
|
+
/**
|
|
131
|
+
* @description: 间隔一定时间,执行指定的函数
|
|
132
|
+
* @param {HandlerFunction} handler
|
|
133
|
+
* @param {array} params
|
|
134
|
+
*/
|
|
135
|
+
export declare const durationHandler: <T, U>(handler: (...args: T[]) => U, ...params: T[]) => ((a: number) => Promise<U>);
|
|
136
|
+
/**
|
|
137
|
+
* @description: 通过请求来测试当前网络的 ping 值
|
|
138
|
+
* @param {*} options
|
|
139
|
+
*/
|
|
140
|
+
export declare const networkSpeed: (options: Options) => Promise<ReturnType>;
|
|
141
|
+
export declare const isSafari: () => boolean | undefined | string;
|
|
142
|
+
export interface MessageHandler<T = unknown, R = unknown> {
|
|
143
|
+
(payload: T): Promise<R> | R;
|
|
144
|
+
}
|
|
145
|
+
export interface MessageData<T = unknown> {
|
|
146
|
+
type: string;
|
|
147
|
+
payload: T;
|
|
148
|
+
id?: string;
|
|
149
|
+
isResponse?: boolean;
|
|
150
|
+
isError?: boolean;
|
|
151
|
+
/** 通道标识:用于隔离同一窗口上的多个 bridge(默认 DEFAULT_CHANNEL) */
|
|
152
|
+
channel?: string;
|
|
153
|
+
/** 发送方实例 id:用于避免 bridge 处理自己发出的请求(自答自问) */
|
|
154
|
+
senderId?: string;
|
|
155
|
+
}
|
|
156
|
+
export interface PendingRequest<R = unknown> {
|
|
157
|
+
resolve: (value: R) => void;
|
|
158
|
+
reject: (error: unknown) => void;
|
|
159
|
+
}
|
|
160
|
+
export declare const BRIDGE_MARKER = "__ranuts_bridge__";
|
|
161
|
+
export declare const DEFAULT_CHANNEL = "default";
|
|
162
|
+
/**
|
|
163
|
+
* Bridge 注册事件,供 client 消费
|
|
164
|
+
*/
|
|
165
|
+
export declare class PostMessageBridge {
|
|
166
|
+
private targetWindow;
|
|
167
|
+
private targetOrigin;
|
|
168
|
+
private messageHandlers;
|
|
169
|
+
private pendingRequests;
|
|
170
|
+
private channel;
|
|
171
|
+
private senderId;
|
|
172
|
+
private available;
|
|
173
|
+
constructor(targetWindow?: Window, targetOrigin?: string, channel?: string);
|
|
174
|
+
private post;
|
|
175
|
+
receive: (event: MessageEvent) => void;
|
|
176
|
+
on: <T = unknown, R = unknown>(type: string, handler: MessageHandler<T, R>) => void;
|
|
177
|
+
off: (type: string) => void;
|
|
178
|
+
send: <T = unknown, R = unknown>(type: string, payload: T) => Promise<R>;
|
|
179
|
+
broadcast: <T = unknown>(data: {
|
|
180
|
+
type: string;
|
|
181
|
+
payload: T;
|
|
182
|
+
}) => void;
|
|
183
|
+
destroy: () => void;
|
|
184
|
+
}
|
|
185
|
+
export interface BridgeManagerOptions {
|
|
186
|
+
id?: string;
|
|
187
|
+
targetOrigin?: string;
|
|
188
|
+
targetWindow?: Window;
|
|
189
|
+
/** 通道标识:需要隔离同一窗口上的多个连接时显式指定,两端须一致 */
|
|
190
|
+
channel?: string;
|
|
191
|
+
}
|
|
192
|
+
export declare class BridgeManager {
|
|
193
|
+
private static instance;
|
|
194
|
+
private bridges;
|
|
195
|
+
private constructor();
|
|
196
|
+
static getInstance(): BridgeManager;
|
|
197
|
+
connectClient: ({ id, targetOrigin, targetWindow, channel, }: BridgeManagerOptions) => {
|
|
198
|
+
bridge: PostMessageBridge;
|
|
199
|
+
id: string;
|
|
200
|
+
};
|
|
201
|
+
getClient: (id: string) => PostMessageBridge | undefined;
|
|
202
|
+
removeClient: (id: string) => void;
|
|
203
|
+
removeAllClient: () => void;
|
|
204
|
+
broadcast: <T = unknown>(payload: {
|
|
205
|
+
type: string;
|
|
206
|
+
payload: T;
|
|
207
|
+
}) => void;
|
|
208
|
+
sendTo: <T = unknown, R = unknown>(id: string, type: string, payload: T) => Promise<R>;
|
|
209
|
+
}
|
|
210
|
+
export declare const bridgeManager: BridgeManager;
|
|
211
|
+
export interface BroadcastPayload {
|
|
212
|
+
type: string;
|
|
213
|
+
payload: unknown;
|
|
214
|
+
}
|
|
215
|
+
export interface CallToPayload<T = unknown> {
|
|
216
|
+
id: string;
|
|
217
|
+
type: string;
|
|
218
|
+
payload: T;
|
|
219
|
+
}
|
|
220
|
+
export declare const Client: {
|
|
221
|
+
connect: ({ id, targetWindow, targetOrigin, channel, }: BridgeManagerOptions) => {
|
|
222
|
+
bridge: PostMessageBridge;
|
|
223
|
+
id: string;
|
|
224
|
+
};
|
|
225
|
+
remove: (id: string) => void;
|
|
226
|
+
removeAll: () => void;
|
|
227
|
+
/** 广播消息到所有 Platform */
|
|
228
|
+
broadcast: (payload: BroadcastPayload) => void;
|
|
229
|
+
/** 发送消息到指定 Platform */
|
|
230
|
+
call: <T = unknown, R = unknown>({ id, type, payload }: CallToPayload<T>) => Promise<R>;
|
|
231
|
+
/** 广播消息到所有所有可以接收消息的窗口 (为了安全考虑,不建议使用) */
|
|
232
|
+
broadcastToAll: (payload: BroadcastPayload) => void;
|
|
233
|
+
};
|
|
234
|
+
export declare const initPlatform: <T = unknown, R = unknown>(events: Record<string, MessageHandler<T, R>>) => {
|
|
235
|
+
destroy: () => void;
|
|
236
|
+
};
|
|
237
|
+
export declare const Platform: {
|
|
238
|
+
init: <T = unknown, R = unknown>(events: Record<string, MessageHandler<T, R>>) => {
|
|
239
|
+
destroy: () => void;
|
|
240
|
+
};
|
|
241
|
+
};
|
|
242
|
+
/**
|
|
243
|
+
* 基于 MessagePort 的点对点桥接(方案 B,作为新 API 提供)。
|
|
244
|
+
*
|
|
245
|
+
* 与 PostMessageBridge 的「全局广播 + 过滤」不同,MessagePort 是浏览器提供的
|
|
246
|
+
* 私有点对点信道:只有握手时拿到 port 的双方能通信。因此天然规避了
|
|
247
|
+
* 跨窗口串消息、来源伪造、同窗口多桥串台、自答自问等问题,也无需 origin 过滤、
|
|
248
|
+
* 无需协议标记、无需 base64——payload 直接走结构化克隆。
|
|
249
|
+
*
|
|
250
|
+
* 典型用法:
|
|
251
|
+
* // A 窗口(发起方)
|
|
252
|
+
* const bridge = openPortBridge({ targetWindow: iframe.contentWindow, targetOrigin });
|
|
253
|
+
* const res = await bridge.send('ping', { n: 1 });
|
|
254
|
+
*
|
|
255
|
+
* // B 窗口(接收方)
|
|
256
|
+
* const bridge = await acceptPortBridge({ targetOrigin });
|
|
257
|
+
* bridge.on('ping', ({ n }) => n + 1);
|
|
258
|
+
*
|
|
259
|
+
* 也可用于已有 port 的场景(Web Worker / SharedWorker):createPortBridge(port)。
|
|
260
|
+
*/
|
|
261
|
+
export interface PortBridge {
|
|
262
|
+
on: <T = unknown, R = unknown>(type: string, handler: MessageHandler<T, R>) => void;
|
|
263
|
+
off: (type: string) => void;
|
|
264
|
+
send: <T = unknown, R = unknown>(type: string, payload: T) => Promise<R>;
|
|
265
|
+
broadcast: <T = unknown>(data: {
|
|
266
|
+
type: string;
|
|
267
|
+
payload: T;
|
|
268
|
+
}) => void;
|
|
269
|
+
destroy: () => void;
|
|
270
|
+
}
|
|
271
|
+
export interface OpenPortBridgeOptions {
|
|
272
|
+
targetWindow: Window;
|
|
273
|
+
targetOrigin?: string;
|
|
274
|
+
/** 连接名:一个页面里区分多个独立 port 连接,两端须一致(默认 'default') */
|
|
275
|
+
name?: string;
|
|
276
|
+
}
|
|
277
|
+
export interface AcceptPortBridgeOptions {
|
|
278
|
+
targetOrigin?: string;
|
|
279
|
+
name?: string;
|
|
280
|
+
}
|
|
281
|
+
/**
|
|
282
|
+
* 在任意 MessagePort 上构建 bridge(Web Worker / SharedWorker 或已握手的 port)。
|
|
283
|
+
*/
|
|
284
|
+
export declare const createPortBridge: (port: MessagePort) => PortBridge;
|
|
285
|
+
/**
|
|
286
|
+
* 发起方:创建 MessageChannel,把一端交给目标窗口,自己持有另一端。
|
|
287
|
+
*/
|
|
288
|
+
export declare const openPortBridge: ({ targetWindow, targetOrigin, name, }: OpenPortBridgeOptions) => PortBridge;
|
|
289
|
+
/**
|
|
290
|
+
* 接收方:等待发起方递来的 port,握手完成后返回 bridge。
|
|
291
|
+
* 返回的 Promise 在收到匹配 name 的握手消息后 resolve。
|
|
292
|
+
*/
|
|
293
|
+
export declare const acceptPortBridge: ({ targetOrigin, name, }?: AcceptPortBridgeOptions) => Promise<PortBridge>;
|
|
294
|
+
export {};
|