@kbapp/market-partner-sdk 0.0.1 → 0.0.3

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/server.js DELETED
@@ -1,103 +0,0 @@
1
- // 简单的HTTP服务器,正确处理ES模块的MIME类型
2
- import http from 'http';
3
- import fs from 'fs';
4
- import path from 'path';
5
-
6
- const PORT = 8080;
7
- const BASE_DIR = process.cwd();
8
-
9
- const server = http.createServer((req, res) => {
10
- console.log(`请求路径: ${req.url}`);
11
- // 移除URL中的查询字符串
12
- const urlPath = req.url.split('?')[0];
13
-
14
- // 特殊路径重定向逻辑
15
- let redirectPath = null;
16
-
17
- // 重定向 /lib/bridge-code 到 /dist/lib/bridge-code
18
- if (urlPath === '/lib/bridge-code') {
19
- redirectPath = '/dist/lib/bridge-code';
20
- console.log(`重定向路径: ${urlPath} -\u003e ${redirectPath}`);
21
- }
22
- // 重定向 /dist/lib/flutter-ds-bridge 到 /dist/core/lib/flutter-ds-bridge
23
- else if (urlPath === '/dist/lib/flutter-ds-bridge') {
24
- redirectPath = '/dist/core/lib/flutter-ds-bridge';
25
- console.log(`重定向路径: ${urlPath} -\u003e ${redirectPath}`);
26
- }
27
-
28
- // 确定文件路径
29
- let filePath = path.join(BASE_DIR, redirectPath || urlPath);
30
-
31
- // 如果请求没有扩展名,尝试添加.js
32
- if (!path.extname(urlPath)) {
33
- console.log(`检测到无扩展名路径,尝试添加.js扩展名`);
34
- filePath = filePath + '.js';
35
- } else if (urlPath.endsWith('/')) {
36
- // 如果是目录,默认返回index.html
37
- filePath = path.join(filePath, 'index.html');
38
- }
39
-
40
- // 确定MIME类型
41
- const extname = path.extname(filePath);
42
- let contentType = 'text/html';
43
-
44
- switch (extname) {
45
- case '.js':
46
- contentType = 'application/javascript';
47
- break;
48
- case '.mjs':
49
- contentType = 'application/javascript';
50
- break;
51
- case '.css':
52
- contentType = 'text/css';
53
- break;
54
- case '.json':
55
- contentType = 'application/json';
56
- break;
57
- case '.png':
58
- contentType = 'image/png';
59
- break;
60
- case '.jpg':
61
- case '.jpeg':
62
- contentType = 'image/jpeg';
63
- break;
64
- }
65
-
66
- console.log(`尝试加载文件: ${filePath}`);
67
-
68
- // 读取并发送文件
69
- fs.readFile(filePath, (error, content) => {
70
- if (error) {
71
- console.error(`文件加载错误: ${error.message}`);
72
- if (error.code === 'ENOENT') {
73
- // 文件不存在,尝试备选路径/index.js
74
- const altFilePath = path.join(filePath.replace('.js', ''), 'index.js');
75
- console.log(`尝试备选路径: ${altFilePath}`);
76
- fs.readFile(altFilePath, (altError, altContent) => {
77
- if (altError) {
78
- res.writeHead(404);
79
- res.end(`文件不存在: ${filePath}`);
80
- } else {
81
- res.writeHead(200, { 'Content-Type': contentType });
82
- res.end(altContent, 'utf-8');
83
- }
84
- });
85
- return;
86
- }
87
-
88
- // 服务器错误
89
- res.writeHead(500);
90
- res.end('服务器错误: ' + error.code);
91
- } else {
92
- // 文件存在,发送内容
93
- res.writeHead(200, { 'Content-Type': contentType });
94
- res.end(content, 'utf-8');
95
- }
96
- });
97
- });
98
-
99
- server.listen(PORT, () => {
100
- console.log(`服务器运行在 http://localhost:${PORT}`);
101
- console.log(`当前工作目录: ${BASE_DIR}`);
102
- console.log(`访问示例: http://localhost:${PORT}/examples/index.html`);
103
- });