@kevisual/router 0.0.51 → 0.0.53

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/src/chat.ts DELETED
@@ -1,40 +0,0 @@
1
- import { QueryRouter } from "./route.ts";
2
-
3
- type RouterChatOptions = {
4
- router?: QueryRouter;
5
- }
6
- export class RouterChat {
7
- router: QueryRouter;
8
- prompt: string = '';
9
- constructor(opts?: RouterChatOptions) {
10
- this.router = opts?.router || new QueryRouter();
11
- }
12
- prefix(wrapperFn?: (routes: any[]) => string) {
13
- if (this.prompt) {
14
- return this.prompt;
15
- }
16
- let _prompt = `你是一个调用函数工具的助手,当用户询问时,如果拥有工具,请返回 JSON 数据,数据的值的内容是 id 和 payload 。如果有参数,请放到 payload 当中。
17
-
18
- 下面是你可以使用的工具列表:
19
-
20
- `;
21
- if (!wrapperFn) {
22
- _prompt += this.router.routes.map(r => `工具名称: ${r.id}\n描述: ${r.description}\n`).join('\n');
23
- } else {
24
- _prompt += wrapperFn(this.router.exportRoutes());
25
- }
26
- _prompt += `当你需要使用工具时,请严格按照以下格式返回:
27
- {
28
- "id": "工具名称",
29
- "payload": {
30
- // 参数列表
31
- }
32
- }
33
- 如果你不需要使用工具,直接返回用户想要的内容即可,不要返回任何多余的信息。`;
34
- return _prompt;
35
- }
36
- chat() {
37
- const prompt = this.prefix();
38
- return prompt;
39
- }
40
- }
package/src/io.ts DELETED
@@ -1,6 +0,0 @@
1
- // TODO: Implement IOApp
2
- export class IOApp {
3
- constructor() {
4
- console.log('IoApp');
5
- }
6
- }
@@ -1,3 +0,0 @@
1
- import { parseXml } from './server/parse-xml.ts';
2
-
3
- export { parseXml };
package/src/sign.ts DELETED
@@ -1,59 +0,0 @@
1
- import { generate } from 'selfsigned';
2
-
3
- export type Attributes = {
4
- name: string;
5
- value: string;
6
- };
7
- export type AltNames = {
8
- type: number;
9
- value?: string;
10
- ip?: string;
11
- };
12
- export const createCert = (attrs: Attributes[] = [], altNames: AltNames[] = []) => {
13
- let attributes = [
14
- { name: 'countryName', value: 'CN' }, // 国家代码
15
- { name: 'stateOrProvinceName', value: 'ZheJiang' }, // 州名
16
- { name: 'localityName', value: 'HangZhou' }, // 城市名
17
- { name: 'organizationName', value: 'kevisual' }, // 组织名
18
- { name: 'organizationalUnitName', value: 'kevisual' }, // 组织单位
19
- ...attrs,
20
- ];
21
- // attribute 根据name去重复, 后面的覆盖前面的
22
- attributes = Object.values(
23
- attributes.reduce(
24
- (acc, attr) => ({
25
- ...acc,
26
- [attr.name]: attr,
27
- }),
28
- {} as Record<string, Attributes>,
29
- ),
30
- );
31
-
32
- const options = {
33
- days: 365, // 证书有效期(天)
34
- extensions: [
35
- {
36
- name: 'subjectAltName',
37
- altNames: [
38
- { type: 2, value: '*' }, // DNS 名称
39
- { type: 2, value: 'localhost' }, // DNS
40
- {
41
- type: 2,
42
- value: '[::1]',
43
- },
44
- {
45
- type: 7,
46
- ip: 'fe80::1',
47
- },
48
- { type: 7, ip: '127.0.0.1' }, // IP 地址
49
- ...altNames,
50
- ],
51
- },
52
- ],
53
- };
54
- const pems = generate(attributes, options);
55
- return {
56
- key: pems.private,
57
- cert: pems.cert,
58
- };
59
- };
package/src/static.ts DELETED
@@ -1,97 +0,0 @@
1
- const http = require('http');
2
- const fs = require('fs').promises;
3
- const path = require('path');
4
- const fetch = require('node-fetch'); // 如果使用 Node.js 18 以上版本,可以改用内置 fetch
5
- const url = require('url');
6
-
7
- // 配置远端静态文件服务器和本地缓存目录
8
- const remoteServer = 'https://example.com/static'; // 远端服务器的 URL
9
- const cacheDir = path.join(__dirname, 'cache'); // 本地缓存目录
10
- const PORT = process.env.PORT || 3000;
11
-
12
- // 确保本地缓存目录存在
13
- fs.mkdir(cacheDir, { recursive: true }).catch(console.error);
14
-
15
- // 获取文件的 content-type
16
- function getContentType(filePath) {
17
- const extname = path.extname(filePath);
18
- const contentType = {
19
- '.html': 'text/html',
20
- '.js': 'text/javascript',
21
- '.css': 'text/css',
22
- '.json': 'application/json',
23
- '.png': 'image/png',
24
- '.jpg': 'image/jpg',
25
- '.gif': 'image/gif',
26
- '.svg': 'image/svg+xml',
27
- '.wav': 'audio/wav',
28
- '.mp4': 'video/mp4'
29
- };
30
- return contentType[extname] || 'application/octet-stream';
31
- }
32
-
33
- // 处理请求文件
34
- async function serveFile(filePath, remoteUrl, res) {
35
- try {
36
- // 检查文件是否存在于本地缓存中
37
- const fileContent = await fs.readFile(filePath);
38
- res.writeHead(200, { 'Content-Type': getContentType(filePath) });
39
- res.end(fileContent, 'utf-8');
40
- } catch (err) {
41
- if (err.code === 'ENOENT') {
42
- // 本地缓存中不存在,向远端服务器请求文件
43
- try {
44
- const response = await fetch(remoteUrl);
45
-
46
- if (response.ok) {
47
- // 远端请求成功,获取文件内容
48
- const data = await response.buffer();
49
-
50
- // 将文件缓存到本地
51
- await fs.writeFile(filePath, data);
52
-
53
- // 返回文件内容
54
- res.writeHead(200, { 'Content-Type': getContentType(filePath) });
55
- res.end(data, 'utf-8');
56
- } else {
57
- // 远端文件未找到或错误,返回 404
58
- res.writeHead(404, { 'Content-Type': 'text/plain' });
59
- res.end(`Error 404: File not found at ${remoteUrl}`);
60
- }
61
- } catch (fetchErr) {
62
- // 处理请求错误
63
- res.writeHead(500, { 'Content-Type': 'text/plain' });
64
- res.end(`Server Error: Unable to fetch ${remoteUrl}`);
65
- }
66
- } else {
67
- // 其他文件系统错误
68
- res.writeHead(500, { 'Content-Type': 'text/plain' });
69
- res.end(`Server Error: ${err.message}`);
70
- }
71
- }
72
- }
73
-
74
- // 创建 HTTP 服务器
75
- http.createServer(async (req, res) => {
76
- let reqPath = req.url;
77
-
78
- // 如果路径是根路径 `/`,将其设置为 `index.html`
79
- if (reqPath === '/') reqPath = '/index.html';
80
-
81
- // 构建本地缓存路径和远端 URL
82
- const localFilePath = path.join(cacheDir, reqPath); // 本地文件路径
83
- const remoteFileUrl = url.resolve(remoteServer, reqPath); // 远端文件 URL
84
-
85
- // 根据请求路径处理文件或返回 index.html(单页面应用处理)
86
- await serveFile(localFilePath, remoteFileUrl, res);
87
-
88
- // 单页面应用的路由处理
89
- if (res.headersSent) return; // 如果响应已发送,不再处理
90
-
91
- // 如果未匹配到任何文件,返回 index.html
92
- const indexFilePath = path.join(cacheDir, 'index.html');
93
- const indexRemoteUrl = url.resolve(remoteServer, '/index.html');
94
- await serveFile(indexFilePath, indexRemoteUrl, res);
95
- }).listen(PORT, () => {
96
- console.log(`Server running at http://localhost:${PORT}`);
97
- });