tsslang 1.0.2
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/package.json +13 -0
- package/tss.js +2 -0
- package/tss_runtime.js +132 -0
package/package.json
ADDED
package/tss.js
ADDED
package/tss_runtime.js
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
const fs = require("fs");
|
|
3
|
+
const { exec } = require("child_process");
|
|
4
|
+
const http = require("http");
|
|
5
|
+
const url = require("url");
|
|
6
|
+
|
|
7
|
+
function sleep(ms) {
|
|
8
|
+
return new Promise((r) => setTimeout(r, Number(ms) || 0));
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function run(cmd) {
|
|
12
|
+
return new Promise((resolve, reject) => {
|
|
13
|
+
exec(String(cmd), { shell: true }, (err, stdout, stderr) => {
|
|
14
|
+
if (stdout) process.stdout.write(stdout);
|
|
15
|
+
if (stderr) process.stderr.write(stderr);
|
|
16
|
+
if (err) reject(err);
|
|
17
|
+
else resolve(true);
|
|
18
|
+
});
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const file = {
|
|
23
|
+
read(p) {
|
|
24
|
+
return fs.readFileSync(String(p), "utf8");
|
|
25
|
+
},
|
|
26
|
+
write(p, content) {
|
|
27
|
+
fs.writeFileSync(String(p), String(content ?? ""), "utf8");
|
|
28
|
+
return true;
|
|
29
|
+
},
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
const json = {
|
|
33
|
+
parse(s) {
|
|
34
|
+
return JSON.parse(String(s));
|
|
35
|
+
},
|
|
36
|
+
stringify(v) {
|
|
37
|
+
return JSON.stringify(v);
|
|
38
|
+
},
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
// -------------------------
|
|
42
|
+
// Minimal server
|
|
43
|
+
// -------------------------
|
|
44
|
+
class TSSServer {
|
|
45
|
+
constructor() {
|
|
46
|
+
this.routes = { GET: [], POST: [] };
|
|
47
|
+
this.httpServer = null;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
get(route, handler) {
|
|
51
|
+
this.routes.GET.push([route, handler]);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
post(route, handler) {
|
|
55
|
+
this.routes.POST.push([route, handler]);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
_match(method, pathname) {
|
|
59
|
+
const list = this.routes[method] || [];
|
|
60
|
+
for (const [r, h] of list) {
|
|
61
|
+
if (String(r) === pathname) return h;
|
|
62
|
+
}
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
async listen(port) {
|
|
67
|
+
port = Number(port) || 3000;
|
|
68
|
+
|
|
69
|
+
if (this.httpServer) throw new Error("server already running");
|
|
70
|
+
|
|
71
|
+
this.httpServer = http.createServer(async (req, res) => {
|
|
72
|
+
const parsed = url.parse(req.url, true);
|
|
73
|
+
const pathname = parsed.pathname;
|
|
74
|
+
|
|
75
|
+
// Request helper
|
|
76
|
+
const reqObj = {
|
|
77
|
+
method: req.method,
|
|
78
|
+
path: pathname,
|
|
79
|
+
query: parsed.query || {},
|
|
80
|
+
headers: req.headers,
|
|
81
|
+
body: null,
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
// Read body for POST/PUT
|
|
85
|
+
if (req.method === "POST" || req.method === "PUT") {
|
|
86
|
+
reqObj.body = await new Promise((resolve) => {
|
|
87
|
+
let data = "";
|
|
88
|
+
req.on("data", (chunk) => (data += chunk));
|
|
89
|
+
req.on("end", () => resolve(data));
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// Response helper
|
|
94
|
+
const resObj = {
|
|
95
|
+
json(obj) {
|
|
96
|
+
const s = JSON.stringify(obj);
|
|
97
|
+
res.writeHead(200, { "Content-Type": "application/json" });
|
|
98
|
+
res.end(s);
|
|
99
|
+
},
|
|
100
|
+
text(txt) {
|
|
101
|
+
res.writeHead(200, { "Content-Type": "text/plain" });
|
|
102
|
+
res.end(String(txt));
|
|
103
|
+
},
|
|
104
|
+
status(code) {
|
|
105
|
+
res.statusCode = Number(code) || 200;
|
|
106
|
+
return resObj;
|
|
107
|
+
},
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
const handler = this._match(req.method, pathname);
|
|
111
|
+
if (!handler) {
|
|
112
|
+
res.writeHead(404, { "Content-Type": "text/plain" });
|
|
113
|
+
res.end("404 Not Found");
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
try {
|
|
118
|
+
await handler(reqObj, resObj);
|
|
119
|
+
} catch (e) {
|
|
120
|
+
res.writeHead(500, { "Content-Type": "text/plain" });
|
|
121
|
+
res.end("Server Error: " + e.message);
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
await new Promise((resolve) => this.httpServer.listen(port, resolve));
|
|
126
|
+
console.log(`✅ TSS Server running on http://127.0.0.1:${port}`);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const server = new TSSServer();
|
|
131
|
+
|
|
132
|
+
module.exports = { sleep, run, file, json, server };
|