@fastgpt-plugin/cli 0.1.0-alpha.6 → 0.1.0-alpha.8
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/dist/index.js +8 -103
- package/package.json +7 -6
- package/templates/tool/package.json +2 -1
- package/templates/tool/src/tool.test.ts +3 -3
- package/templates/tool-suite/package.json +2 -1
- package/dist/context-DoVwxr5H-B6IEtQ3M.js +0 -1
- package/dist/dist-Bvnjjx3Q.js +0 -109
- package/dist/dist-DHF5Otz1.js +0 -1
- package/dist/dist-mM4pkTG1.js +0 -27
- package/dist/prompt-ob7l3SPy.js +0 -45
- package/dist/resolver-o20sdYK5-D5TGopbk.js +0 -1
- package/dist/tsc-By9Scnlt.js +0 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fastgpt-plugin/cli",
|
|
3
|
-
"version": "0.1.0-alpha.
|
|
3
|
+
"version": "0.1.0-alpha.8",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"private": false,
|
|
6
6
|
"publishConfig": {
|
|
@@ -16,25 +16,26 @@
|
|
|
16
16
|
],
|
|
17
17
|
"scripts": {
|
|
18
18
|
"build": "tsdown",
|
|
19
|
+
"build:verbose": "tsdown",
|
|
19
20
|
"test": "vitest run",
|
|
20
21
|
"dev": "bun run src/index.ts",
|
|
21
22
|
"prepublishOnly": "npm run build"
|
|
22
23
|
},
|
|
23
24
|
"dependencies": {
|
|
24
25
|
"@oxc-parser/binding-wasm32-wasi": "^0.112.0",
|
|
25
|
-
"oxc-parser": "^0.112.0"
|
|
26
|
-
},
|
|
27
|
-
"devDependencies": {
|
|
26
|
+
"oxc-parser": "^0.112.0",
|
|
28
27
|
"@inquirer/prompts": "^8.2.0",
|
|
29
28
|
"commander": "^14.0.3",
|
|
30
29
|
"consola": "^3.4.2",
|
|
31
30
|
"es-toolkit": "^1.44.0",
|
|
32
|
-
"tsdown": "^0.20.1"
|
|
31
|
+
"tsdown": "^0.20.1"
|
|
32
|
+
},
|
|
33
|
+
"devDependencies": {
|
|
33
34
|
"typescript": "^5",
|
|
34
35
|
"vite-tsconfig-paths": "^6.0.5",
|
|
35
36
|
"vitest": "^4.0.18"
|
|
36
37
|
},
|
|
37
38
|
"engines": {
|
|
38
|
-
"node": "
|
|
39
|
+
"node": ">=22.12.0"
|
|
39
40
|
}
|
|
40
41
|
}
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
7
7
|
"scripts": {
|
|
8
|
-
"build": "
|
|
8
|
+
"build": "npx @fastgpt-plugin/cli build",
|
|
9
9
|
"test": "vitest run"
|
|
10
10
|
},
|
|
11
11
|
"dependencies": {
|
|
@@ -13,6 +13,7 @@
|
|
|
13
13
|
"zod": "catalog:"
|
|
14
14
|
},
|
|
15
15
|
"devDependencies": {
|
|
16
|
+
"@fastgpt-plugin/cli": "catalog:",
|
|
16
17
|
"typescript": "catalog:",
|
|
17
18
|
"vitest": "catalog:"
|
|
18
19
|
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { describe,
|
|
2
|
-
import { tool } from './tool';
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
3
2
|
import { InputType, OutputType } from './schemas';
|
|
3
|
+
import { tool } from './tool';
|
|
4
4
|
|
|
5
|
-
describe('
|
|
5
|
+
describe('get-time', () => {
|
|
6
6
|
it('should run with valid IO schemas', async () => {
|
|
7
7
|
const input = InputType.parse({});
|
|
8
8
|
const result = await tool(input, {
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
7
7
|
"scripts": {
|
|
8
|
-
"build": "
|
|
8
|
+
"build": "npx @fastgpt-plugin/cli build",
|
|
9
9
|
"test": "vitest run"
|
|
10
10
|
},
|
|
11
11
|
"dependencies": {
|
|
@@ -13,6 +13,7 @@
|
|
|
13
13
|
"zod": "catalog:"
|
|
14
14
|
},
|
|
15
15
|
"devDependencies": {
|
|
16
|
+
"@fastgpt-plugin/cli": "catalog:",
|
|
16
17
|
"typescript": "catalog:",
|
|
17
18
|
"vitest": "catalog:"
|
|
18
19
|
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import{y as e}from"./index.js";const t=globalThis.process.getBuiltinModule(`node:path`),n=e(`rolldown-plugin-dts:tsc-context`);function r(){return{programs:[],files:new Map,projects:new Map}}function i(e,r){r=t.resolve(r).replaceAll(`\\`,`/`),n(`invalidating context file: ${r}`),e.files.delete(r),e.programs=e.programs.filter(e=>!e.getSourceFiles().some(e=>e.fileName===r)),e.projects.clear()}const a=r();export{a as n,i as r,r as t};
|
package/dist/dist-Bvnjjx3Q.js
DELETED
|
@@ -1,109 +0,0 @@
|
|
|
1
|
-
import{r as e}from"./index.js";import{builtinModules as t,createRequire as n}from"node:module";import r from"node:path";import i from"node:process";import{spawn as a}from"node:child_process";import o,{existsSync as s}from"node:fs";import{tmpdir as c}from"node:os";import l from"node:crypto";import{fileURLToPath as u,pathToFileURL as d}from"node:url";import{Buffer as f}from"node:buffer";import.meta.url;function p(e,t){if(e.preset===`bundle-require`)return t;if(e.preset===`jiti`){let n=r.extname(e.path);if(t&&typeof t==`object`&&t[Symbol.toStringTag]===`Module`&&Object.keys(t).length===0)return n===`.mjs`?t:{}}return t&&typeof t==`object`&&`default`in t?t.default:t}function m(e){if(!e)return`index.ts`;if(e instanceof URL)return e.protocol===`file:`?u(e):e.href;if(typeof e==`string`){if(!e.startsWith(`file:`))return e;try{return u(e)}catch{try{return u(new URL(e))}catch{return e}}}return String(e)}function h(e={}){let t={path:r.resolve(i.cwd(),m(e.path)),debug:e.debug||!1,preset:e.preset||`none`,inputOptions:e.inputOptions,outputOptions:e.outputOptions};if(!o.existsSync(t.path))throw Error(`[unrun] File not found: ${t.path}`);if(!new Set([`none`,`jiti`,`bundle-require`]).has(t.preset))throw Error(`[unrun] Invalid preset "${t.preset}" (expected: none | jiti | bundle-require)`);return t}const g=new Set([...t,...t.map(e=>`node:${e}`)]);function _(e){return e?g.has(e)||e.startsWith(`node:`):!1}function v(e){let t=r.dirname(e.path),n=e=>{let n=y(e);if(!n)return!1;let i=t;for(;;){if(s(r.join(i,`node_modules`,n)))return!0;let e=r.dirname(i);if(e===i)break;i=e}return!1};return function(e){return!e||e.startsWith(`\0`)||e.startsWith(`.`)||e.startsWith(`#`)||r.isAbsolute(e)?!1:_(e)?!0:!!n(e)}}function y(e){if(!e)return;if(e.startsWith(`@`)){let t=e.split(`/`);return t.length>=2?`${t[0]}/${t[1]}`:void 0}let[t]=e.split(`/`);return t||void 0}const b=`(function(){
|
|
2
|
-
function __unrun__fmt(names, getter, np){
|
|
3
|
-
var onlyDefault = names.length === 1 && names[0] === "default";
|
|
4
|
-
var o = np ? Object.create(null) : {};
|
|
5
|
-
for (var i = 0; i < names.length; i++) {
|
|
6
|
-
var n = names[i];
|
|
7
|
-
try { o[n] = getter(n) } catch {}
|
|
8
|
-
}
|
|
9
|
-
if (onlyDefault) {
|
|
10
|
-
try {
|
|
11
|
-
var s = JSON.stringify(o.default);
|
|
12
|
-
if (s !== undefined) {
|
|
13
|
-
s = s.replace(/"([^"]+)":/g, "$1: ").replace(/,/g, ", ").replace(/{/g, "{ ").replace(/}/g, " }");
|
|
14
|
-
return "[Module: null prototype] { default: " + s + " }";
|
|
15
|
-
}
|
|
16
|
-
} catch {}
|
|
17
|
-
return "[Module: null prototype] { default: " + String(o.default) + " }";
|
|
18
|
-
}
|
|
19
|
-
return o;
|
|
20
|
-
}
|
|
21
|
-
function __unrun__setInspect(obj, names, getter, np){
|
|
22
|
-
try {
|
|
23
|
-
var __insp = Symbol.for('nodejs.util.inspect.custom');
|
|
24
|
-
Object.defineProperty(obj, __insp, {
|
|
25
|
-
value: function(){ return __unrun__fmt(names, getter, np) },
|
|
26
|
-
enumerable: false, configurable: true
|
|
27
|
-
});
|
|
28
|
-
} catch {}
|
|
29
|
-
return obj;
|
|
30
|
-
}
|
|
31
|
-
try {
|
|
32
|
-
Object.defineProperty(globalThis, "__unrun__setInspect", {
|
|
33
|
-
value: __unrun__setInspect,
|
|
34
|
-
enumerable: false,
|
|
35
|
-
});
|
|
36
|
-
} catch {}
|
|
37
|
-
})();`,x=/__unrun__setInspect\b/;function S(){return{name:`unrun-console-output-customizer`,generateBundle:{handler(e,t){for(let e of Object.values(t))e.type===`chunk`&&(C(e),w(e))}}}}function C(e){x.test(e.code)||(e.code=e.code.startsWith(`#!`)?E(e.code,`${b}\n`):`${b}\n${e.code}`)}function w(e){if(e.code.includes(`__unrun__wrapRolldownHelpers`))return;let t=T(e.code),n=`(function __unrun__wrapRolldownHelpers(){
|
|
38
|
-
if (typeof __unrun__setInspect !== "function") return;
|
|
39
|
-
if (typeof __export === "function" && !__export.__unrunPatched) {
|
|
40
|
-
var __unrun__origExport = __export;
|
|
41
|
-
var __unrun__patchedExport = (...__unrun__args) => {
|
|
42
|
-
var __unrun__target = __unrun__origExport(...__unrun__args);
|
|
43
|
-
if (__unrun__target && typeof __unrun__target === "object") {
|
|
44
|
-
try {
|
|
45
|
-
var __unrun__map = (__unrun__args[0] && typeof __unrun__args[0] === "object") ? __unrun__args[0] : {};
|
|
46
|
-
var __unrun__names = Object.keys(__unrun__map).filter(function(n){ return n !== "__esModule" });
|
|
47
|
-
__unrun__setInspect(
|
|
48
|
-
__unrun__target,
|
|
49
|
-
__unrun__names,
|
|
50
|
-
function(n){
|
|
51
|
-
var getter = __unrun__map[n];
|
|
52
|
-
return typeof getter === "function" ? getter() : getter;
|
|
53
|
-
},
|
|
54
|
-
false,
|
|
55
|
-
);
|
|
56
|
-
} catch {}
|
|
57
|
-
}
|
|
58
|
-
return __unrun__target;
|
|
59
|
-
};
|
|
60
|
-
__unrun__patchedExport.__unrunPatched = true;
|
|
61
|
-
__export = __unrun__patchedExport;
|
|
62
|
-
}
|
|
63
|
-
if (typeof __exportAll === "function" && !__exportAll.__unrunPatched) {
|
|
64
|
-
var __unrun__origExportAll = __exportAll;
|
|
65
|
-
var __unrun__patchedExportAll = (...__unrun__args) => {
|
|
66
|
-
var __unrun__target = __unrun__origExportAll(...__unrun__args);
|
|
67
|
-
if (__unrun__target && typeof __unrun__target === "object") {
|
|
68
|
-
try {
|
|
69
|
-
var __unrun__map = (__unrun__args[0] && typeof __unrun__args[0] === "object") ? __unrun__args[0] : {};
|
|
70
|
-
var __unrun__names = Object.keys(__unrun__map).filter(function(n){ return n !== "__esModule" });
|
|
71
|
-
__unrun__setInspect(
|
|
72
|
-
__unrun__target,
|
|
73
|
-
__unrun__names,
|
|
74
|
-
function(n){
|
|
75
|
-
var getter = __unrun__map[n];
|
|
76
|
-
return typeof getter === "function" ? getter() : getter;
|
|
77
|
-
},
|
|
78
|
-
false,
|
|
79
|
-
);
|
|
80
|
-
} catch {}
|
|
81
|
-
}
|
|
82
|
-
return __unrun__target;
|
|
83
|
-
};
|
|
84
|
-
__unrun__patchedExportAll.__unrunPatched = true;
|
|
85
|
-
__exportAll = __unrun__patchedExportAll;
|
|
86
|
-
}
|
|
87
|
-
if (typeof __copyProps === "function" && !__copyProps.__unrunPatched) {
|
|
88
|
-
var __unrun__origCopyProps = __copyProps;
|
|
89
|
-
var __unrun__patchedCopyProps = (...__unrun__args) => {
|
|
90
|
-
var __unrun__result = __unrun__origCopyProps(...__unrun__args);
|
|
91
|
-
if (__unrun__result && typeof __unrun__result === "object") {
|
|
92
|
-
try {
|
|
93
|
-
var __unrun__names = Object.keys(__unrun__result).filter(function(n){ return n !== "__esModule" });
|
|
94
|
-
__unrun__setInspect(__unrun__result, __unrun__names, function(n){ return __unrun__result[n] }, true);
|
|
95
|
-
} catch {}
|
|
96
|
-
}
|
|
97
|
-
return __unrun__result;
|
|
98
|
-
};
|
|
99
|
-
__unrun__patchedCopyProps.__unrunPatched = true;
|
|
100
|
-
__copyProps = __unrun__patchedCopyProps;
|
|
101
|
-
}
|
|
102
|
-
})();
|
|
103
|
-
`;if(t===-1){e.code=`${e.code}\n${n}`;return}e.code=`${e.code.slice(0,t)}${n}${e.code.slice(t)}`}function T(e){let t=e.indexOf(`//#endregion`);if(t===-1)return-1;let n=e.indexOf(`
|
|
104
|
-
`,t);return n===-1?e.length:n+1}function E(e,t){let n=e.indexOf(`
|
|
105
|
-
`);return n===-1?`${e}\n${t}`:`${e.slice(0,n+1)}${t}${e.slice(n+1)}`}function D(){return{name:`unrun-json-loader`,resolveId:{handler(e,t){if(!e.endsWith(`.json`))return null;let n=t?r.dirname(t):i.cwd(),a=r.resolve(n,e),s=!1;try{if(t){let n=o.readFileSync(t,`utf8`),r=e.replaceAll(/[.*+?^${}()|[\]\\]/g,e=>`\\${e}`),i=String.raw`\brequire\s*\(\s*['"]${r}['"]\s*\)`;s=new RegExp(i).test(n)}}catch{}return{id:`${a}?unrun-json.${s?`cjs`:`mjs`}`}}},load:{filter:{id:/\?unrun-json\.(?:mjs|cjs)$/},handler(e){try{let t=e.replace(/\?unrun-json\.(?:mjs|cjs)$/,``),n=o.readFileSync(t,`utf8`),r=JSON.parse(n),i=JSON.stringify(r);if(e.endsWith(`?unrun-json.cjs`))return{code:`const __data = ${i}\ntry { Object.defineProperty(__data, 'default', { value: __data, enumerable: false, configurable: true }) } catch {}\nmodule.exports = __data\n`};let a=Object.keys(r).filter(e=>/^[$A-Z_]\w*$/i.test(e)).map(e=>`export const ${e} = __data[${JSON.stringify(e)}]`).join(`
|
|
106
|
-
`);return{code:[`const __data = ${i}`,`try { Object.defineProperty(__data, 'default', { value: __data, enumerable: false, configurable: true }) } catch {}`,a,`export default __data`].filter(Boolean).join(`
|
|
107
|
-
`)}}catch{return null}}}}}function O(){return{name:`unrun-make-cjs-wrapper-async-friendly`,generateBundle:{handler(e,t){for(let e of Object.values(t)){if(e.type!==`chunk`)continue;let t=e.code,n=[`__commonJS({`,`__commonJSMin(`];if(!n.some(e=>t.includes(e)))continue;let r=e=>{let n=0;for(;;){let r=t.indexOf(e,n);if(r===-1)break;let i=t.indexOf(`(() => {`,r);if(i===-1){n=r+e.length;continue}let a=i+8,o=a,s=1;for(;o<t.length&&s>0;){let e=t[o++];e===`{`?s++:e===`}`&&s--}if(s!==0)break;let c=o-1,l=t.slice(a,c);if(/\bawait\b/.test(l)&&t.slice(i,i+14)!==`(async () => {`){t=`${t.slice(0,i+1)}async ${t.slice(i+1)}`,n=i+1+6;continue}n=c}};for(let e of n)r(e);t!==e.code&&(e.code=t)}}}}}function k(e){return{name:`unrun-require-resolve-fix`,generateBundle:{handler(t,n){for(let t of Object.values(n))t.type===`chunk`&&(t.code=t.code.replaceAll(/__require\.resolve\(["']([^"']+)["']\)/g,(t,n)=>{if(n.startsWith(`./`)||n.startsWith(`../`))try{let t=r.dirname(e.path);for(let e of[``,`.ts`,`.js`,`.mts`,`.mjs`,`.cts`,`.cjs`]){let i=r.resolve(t,n+e);if(o.existsSync(i))return JSON.stringify(i)}let i=r.resolve(t,n);return JSON.stringify(i)}catch{return t}return t}))}}}}function A(){return{name:`unrun-require-typeof-fix`,generateBundle:{handler(e,t){for(let e of Object.values(t))e.type===`chunk`&&(e.code=e.code.replaceAll(/\btypeof\s+__require\b/g,`typeof require`))}}}}function j(){return{name:`unrun-source-context-shims`,load:{filter:{id:/\.(?:m?[jt]s|c?tsx?)(?:$|\?)/},handler(e){let t=e.split(`?`)[0].split(`#`)[0],n=r.normalize(t),i;try{i=o.readFileSync(n,`utf8`)}catch{return null}if(n.replaceAll(`\\`,`/`).includes(`/node_modules/`))return null;let a=n,s=r.dirname(n),c=d(n).href,l=i.includes(`import.meta`),u=/\b__filename\b/.test(i),f=/\b(?:const|let|var)\s+__filename\b/.test(i),p=/\b__dirname\b/.test(i),m=/\b(?:const|let|var)\s+__dirname\b/.test(i),h=u&&!f,g=p&&!m;if(h||g||l){let e=[];h&&e.push(`const __filename = ${JSON.stringify(a)}`),g&&e.push(`const __dirname = ${JSON.stringify(s)}`);let t=i,o=!1;if(l){let e=/import\s*\.\s*meta\s*\.\s*resolve!?\s*\(\s*(["'])([^"']+)\1\s*\)/y,i=/import\s*\.\s*meta\s*\.\s*url\b/y,l=/import\s*\.\s*meta\s*\.\s*dirname\b/y,u=/import\s*\.\s*meta\s*\.\s*filename\b/y,f=``,p=`normal`,m=[],h=0,g=()=>{p=m.pop()??`normal`};for(let _=0;_<t.length;){let v=t[_],y=t[_+1];if(p===`lineComment`){f+=v,_+=1,v===`
|
|
108
|
-
`&&g();continue}if(p===`blockComment`){f+=v,_+=1,v===`*`&&y===`/`&&(f+=`/`,_+=1,g());continue}if(p===`single`){if(f+=v,_+=1,v===`\\`){f+=t[_]??``,_+=1;continue}v===`'`&&g();continue}if(p===`double`){if(f+=v,_+=1,v===`\\`){f+=t[_]??``,_+=1;continue}v===`"`&&g();continue}if(p===`template`){if(f+=v,_+=1,v===`\\`){f+=t[_]??``,_+=1;continue}if(v==="`"){g();continue}v===`$`&&y===`{`&&(f+=`{`,_+=1,m.push(p),p=`templateExpr`,h=1);continue}if(p===`templateExpr`){if(v===`{`)h+=1;else if(v===`}`&&(--h,h===0)){f+=v,_+=1,g();continue}}if(v===`/`&&y===`/`){f+=`//`,_+=2,m.push(p),p=`lineComment`;continue}if(v===`/`&&y===`*`){f+=`/*`,_+=2,m.push(p),p=`blockComment`;continue}if(v===`'`){f+=v,_+=1,m.push(p),p=`single`;continue}if(v===`"`){f+=v,_+=1,m.push(p),p=`double`;continue}if(v==="`"){f+=v,_+=1,m.push(p),p=`template`;continue}e.lastIndex=_;let b=e.exec(t);if(b){let t=b[2],i=d(r.resolve(r.dirname(n),t)).href;f+=JSON.stringify(i),_=e.lastIndex,o=!0;continue}if(i.lastIndex=_,i.test(t)){f+=JSON.stringify(c),_=i.lastIndex,o=!0;continue}if(l.lastIndex=_,l.test(t)){f+=JSON.stringify(s),_=l.lastIndex,o=!0;continue}if(u.lastIndex=_,u.test(t)){f+=JSON.stringify(a),_=u.lastIndex,o=!0;continue}f+=v,_+=1}o&&(t=f)}if(e.length>0&&(t=`${e.join(`
|
|
109
|
-
`)}\n${t}`),t!==i)return{code:t}}return null}}}}async function M(t){let n=r.resolve(i.cwd(),`tsconfig.json`),a=s(n)?n:void 0,o={input:t.path,platform:`node`,external:v(t),plugins:[O(),k(t),j(),...t.preset===`jiti`?[S(),D(),A()]:[]],transform:{define:{__dirname:JSON.stringify(r.dirname(t.path)),__filename:JSON.stringify(t.path),"import.meta.url":JSON.stringify(d(t.path).href),"import.meta.filename":JSON.stringify(t.path),"import.meta.dirname":JSON.stringify(r.dirname(t.path)),"import.meta.env":`process.env`}},logLevel:`silent`,...t.inputOptions};a&&(o.tsconfig=a);let c=await e(o),l={format:`esm`,codeSplitting:!1,keepNames:!0,...t.outputOptions},u=await c.generate(l);if(!u.output[0])throw Error(`[unrun] No output chunk found`);let f=await c.watchFiles;return{chunk:u.output[0],dependencies:f}}function N(e,t){if(!t.debug)try{if(e.startsWith(`file://`)){let t=new URL(e);o.unlinkSync(t)}}catch(e){if(e.code!==`ENOENT`)throw e}}function P(e){return e.replaceAll(/[^\w.-]/g,`_`)}function F(e,t){let n=r.basename(t.path),a=``;try{let t=l.randomBytes(16).toString(`hex`),s=`${n?`${P(n)}.`:``}${t}.mjs`,u=r.join(i.cwd(),`node_modules`),f=r.join(u,`.unrun`),p=r.join(f,s);if(!o.existsSync(p))try{o.mkdirSync(f,{recursive:!0}),o.writeFileSync(p,e,`utf8`)}catch{let t=r.join(c(),`unrun-cache`),n=r.join(t,s);o.mkdirSync(t,{recursive:!0}),o.writeFileSync(n,e,`utf8`),a=d(n).href}a||=d(p).href}catch{a=`data:text/javascript;base64,${f.from(e).toString(`base64`)}`}return a}async function I(e,t){let n=F(e,t),r;try{r=await import(n)}finally{N(n,t)}return r}async function L(e){let t=h(e),n=await M(t),r;try{r=await I(n.chunk.code,t)}catch(e){throw Error(`[unrun] Import failed (code length: ${n.chunk.code.length}): ${e.message}`)}return{module:p(t,r),dependencies:n.dependencies}}export{L as unrun};
|
package/dist/dist-DHF5Otz1.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
function e(){let e,t;return{promise:new Promise((n,r)=>{e=n,t=r}),resolve:e,reject:t}}const t=Math.random.bind(Math);function n(e=21){let n=``,r=e;for(;r--;)n+=`useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict`[t()*64|0];return n}const r=e=>e,i=r,{clearTimeout:a,setTimeout:o}=globalThis;function s(t,s){let{post:c,on:l,off:u=()=>{},eventNames:d=[],serialize:f=r,deserialize:p=i,resolver:m,bind:h=`rpc`,timeout:g=6e4,proxify:_=!0}=s,v=!1,y=new Map,b,x;async function S(t,r,i,l){if(v)throw Error(`[birpc] rpc is closed, cannot call "${t}"`);let u={m:t,a:r,t:`q`};l&&(u.o=!0);let d=async e=>c(f(e));if(i){await d(u);return}if(b)try{await b}finally{b=void 0}let{promise:p,resolve:m,reject:h}=e(),_=n();u.i=_;let S;async function C(e=u){return g>=0&&(S=o(()=>{try{if(s.onTimeoutError?.call(x,t,r)!==!0)throw Error(`[birpc] timeout on calling "${t}"`)}catch(e){h(e)}y.delete(_)},g),typeof S==`object`&&(S=S.unref?.())),y.set(_,{resolve:m,reject:h,timeoutId:S,method:t}),await d(e),p}try{s.onRequest?await s.onRequest.call(x,u,C,m):await C()}catch(e){if(s.onGeneralError?.call(x,e)!==!0)throw e;return}finally{a(S),y.delete(_)}return p}let C={$call:(e,...t)=>S(e,t,!1),$callOptional:(e,...t)=>S(e,t,!1,!0),$callEvent:(e,...t)=>S(e,t,!0),$callRaw:e=>S(e.method,e.args,e.event,e.optional),$rejectPendingCalls:T,get $closed(){return v},get $meta(){return s.meta},$close:w,$functions:t};x=_?new Proxy({},{get(e,n){if(Object.prototype.hasOwnProperty.call(C,n))return C[n];if(n===`then`&&!d.includes(`then`)&&!(`then`in t))return;let r=(...e)=>S(n,e,!0);if(d.includes(n))return r.asEvent=r,r;let i=(...e)=>S(n,e,!1);return i.asEvent=r,i}}):C;function w(e){v=!0,y.forEach(({reject:t,method:n})=>{let r=Error(`[birpc] rpc is closed, cannot call "${n}"`);if(e)return e.cause??=r,t(e);t(r)}),y.clear(),u(E)}function T(e){let t=Array.from(y.values()).map(({method:t,reject:n})=>e?e({method:t,reject:n}):n(Error(`[birpc]: rejected pending call "${t}".`)));return y.clear(),t}async function E(e,...n){let r;try{r=p(e)}catch(e){if(s.onGeneralError?.call(x,e)!==!0)throw e;return}if(r.t===`q`){let{m:e,a:i,o:a}=r,o,l,u=await(m?m.call(x,e,t[e]):t[e]);if(a&&(u||=()=>void 0),!u)l=Error(`[birpc] function "${e}" not found`);else try{o=await u.apply(h===`rpc`?x:t,i)}catch(e){l=e}if(r.i){if(l&&s.onFunctionError&&s.onFunctionError.call(x,l,e,i)===!0)return;if(!l)try{await c(f({t:`s`,i:r.i,r:o}),...n);return}catch(t){if(l=t,s.onGeneralError?.call(x,t,e,i)!==!0)throw t}try{await c(f({t:`s`,i:r.i,e:l}),...n)}catch(t){if(s.onGeneralError?.call(x,t,e,i)!==!0)throw t}}}else{let{i:e,r:t,e:n}=r,i=y.get(e);i&&(a(i.timeoutId),n?i.reject(n):i.resolve(t)),y.delete(e)}}return b=l(E),x}export{s as createBirpc};
|