tezx 1.0.78 → 2.0.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/README.md +2 -2
- package/bun/index.d.ts +2 -0
- package/bun/index.js +2 -0
- package/cjs/{adapter → bun}/index.js +2 -3
- package/cjs/core/config.js +1 -1
- package/cjs/core/context.js +2 -2
- package/cjs/core/environment.js +3 -3
- package/cjs/{helper/env-parser.js → deno/env.js} +12 -26
- package/cjs/deno/index.js +18 -0
- package/cjs/helper/index.js +9 -18
- package/cjs/index.js +1 -1
- package/cjs/node/adapter.js +105 -0
- package/cjs/node/env.js +50 -0
- package/cjs/node/index.js +18 -0
- package/cjs/utils/staticFile.js +1 -1
- package/cjs/ws/index.js +1 -1
- package/core/config.d.ts +1 -1
- package/core/config.js +2 -2
- package/core/context.js +3 -3
- package/core/environment.d.ts +1 -1
- package/core/environment.js +1 -1
- package/{helper/env-parser.js → deno/env.js} +13 -27
- package/deno/index.d.ts +2 -0
- package/deno/index.js +2 -0
- package/helper/index.d.ts +4 -2
- package/helper/index.js +4 -2
- package/index.d.ts +1 -0
- package/index.js +1 -1
- package/node/adapter.js +102 -0
- package/node/env.d.ts +5 -0
- package/node/env.js +47 -0
- package/node/index.d.ts +2 -0
- package/node/index.js +2 -0
- package/package.json +21 -6
- package/utils/staticFile.js +2 -2
- package/ws/index.js +2 -2
- package/adapter/index.d.ts +0 -4
- package/adapter/index.js +0 -3
- package/adapter/node.js +0 -107
- package/cjs/adapter/node.js +0 -109
- /package/{adapter/bun.d.ts → bun/adapter.d.ts} +0 -0
- /package/{adapter/bun.js → bun/adapter.js} +0 -0
- /package/cjs/{adapter/bun.js → bun/adapter.js} +0 -0
- /package/cjs/{adapter/deno.js → deno/adpater.js} +0 -0
- /package/{adapter/deno.d.ts → deno/adpater.d.ts} +0 -0
- /package/{adapter/deno.js → deno/adpater.js} +0 -0
- /package/{helper/env-parser.d.ts → deno/env.d.ts} +0 -0
- /package/{adapter/node.d.ts → node/adapter.d.ts} +0 -0
package/README.md
CHANGED
|
@@ -189,7 +189,7 @@ npm run dev
|
|
|
189
189
|
`src/index.ts`:
|
|
190
190
|
|
|
191
191
|
```ts
|
|
192
|
-
import { bunAdapter } from "tezx/
|
|
192
|
+
import { bunAdapter } from "tezx/bun";
|
|
193
193
|
bunAdapter(app).listen(3000, (message) => {
|
|
194
194
|
console.log(message);
|
|
195
195
|
});
|
|
@@ -206,7 +206,7 @@ bunAdapter(app).listen(3000, (message) => {
|
|
|
206
206
|
`src/index.ts`:
|
|
207
207
|
|
|
208
208
|
```ts
|
|
209
|
-
import { denoAdapter } from "tezx/
|
|
209
|
+
import { denoAdapter } from "tezx/deno";
|
|
210
210
|
denoAdapter(app).listen(3000, (message) => {
|
|
211
211
|
console.log(message);
|
|
212
212
|
});
|
package/bun/index.d.ts
ADDED
package/bun/index.js
ADDED
|
@@ -14,6 +14,5 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./
|
|
18
|
-
__exportStar(require("
|
|
19
|
-
__exportStar(require("./node.js"), exports);
|
|
17
|
+
__exportStar(require("./adapter.js"), exports);
|
|
18
|
+
__exportStar(require("../node/index.js"), exports);
|
package/cjs/core/config.js
CHANGED
|
@@ -15,7 +15,7 @@ let GlobalConfig = class {
|
|
|
15
15
|
static overwriteMethod = true;
|
|
16
16
|
static debugMode = false;
|
|
17
17
|
static server;
|
|
18
|
-
static adapter = environment_js_1.
|
|
18
|
+
static adapter = environment_js_1.Environment.getEnvironment;
|
|
19
19
|
static get debugging() {
|
|
20
20
|
return this.debugMode
|
|
21
21
|
? {
|
package/cjs/core/context.js
CHANGED
|
@@ -213,7 +213,7 @@ class Context {
|
|
|
213
213
|
async download(filePath, fileName) {
|
|
214
214
|
try {
|
|
215
215
|
let fileExists = false;
|
|
216
|
-
const runtime = environment_js_1.
|
|
216
|
+
const runtime = environment_js_1.Environment.getEnvironment;
|
|
217
217
|
if (runtime === "node") {
|
|
218
218
|
const { existsSync } = await Promise.resolve().then(() => require("node:fs"));
|
|
219
219
|
fileExists = existsSync(filePath);
|
|
@@ -261,7 +261,7 @@ class Context {
|
|
|
261
261
|
}
|
|
262
262
|
async sendFile(filePath, ...args) {
|
|
263
263
|
try {
|
|
264
|
-
const runtime = environment_js_1.
|
|
264
|
+
const runtime = environment_js_1.Environment.getEnvironment;
|
|
265
265
|
const resolvedPath = filePath;
|
|
266
266
|
let fileExists = false;
|
|
267
267
|
if (runtime === "node") {
|
package/cjs/core/environment.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
class
|
|
3
|
+
exports.Environment = void 0;
|
|
4
|
+
class Environment {
|
|
5
5
|
static get getEnvironment() {
|
|
6
6
|
if (typeof Bun !== "undefined")
|
|
7
7
|
return "bun";
|
|
@@ -12,4 +12,4 @@ class EnvironmentDetector {
|
|
|
12
12
|
return "unknown";
|
|
13
13
|
}
|
|
14
14
|
}
|
|
15
|
-
exports.
|
|
15
|
+
exports.Environment = Environment;
|
|
@@ -2,34 +2,25 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.loadEnv = loadEnv;
|
|
4
4
|
const environment_js_1 = require("../core/environment.js");
|
|
5
|
+
const colors_js_1 = require("../utils/colors.js");
|
|
5
6
|
function parseEnvFile(filePath, result) {
|
|
6
7
|
try {
|
|
8
|
+
let runtime = environment_js_1.Environment.getEnvironment;
|
|
9
|
+
if (runtime !== "deno") {
|
|
10
|
+
throw new Error(`Please use ${colors_js_1.COLORS.bgRed}import {loadEnv} from "tezx/${runtime}"${colors_js_1.COLORS.reset} environment`);
|
|
11
|
+
}
|
|
7
12
|
let fileExists = false;
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
fileExists = existsSync(filePath);
|
|
13
|
+
try {
|
|
14
|
+
Deno.statSync(filePath);
|
|
15
|
+
fileExists = true;
|
|
12
16
|
}
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
Deno.statSync(filePath);
|
|
16
|
-
fileExists = true;
|
|
17
|
-
}
|
|
18
|
-
catch {
|
|
19
|
-
fileExists = false;
|
|
20
|
-
}
|
|
17
|
+
catch {
|
|
18
|
+
fileExists = false;
|
|
21
19
|
}
|
|
22
20
|
if (!fileExists) {
|
|
23
21
|
return;
|
|
24
22
|
}
|
|
25
|
-
let fileContent = "";
|
|
26
|
-
if (runtime === "node" || runtime === "bun") {
|
|
27
|
-
const { readFileSync } = require("fs");
|
|
28
|
-
fileContent = readFileSync(filePath, "utf8");
|
|
29
|
-
}
|
|
30
|
-
else if (runtime === "deno") {
|
|
31
|
-
fileContent = new TextDecoder("utf-8").decode(Deno.readFileSync(filePath));
|
|
32
|
-
}
|
|
23
|
+
let fileContent = new TextDecoder("utf-8").decode(Deno.readFileSync(filePath));
|
|
33
24
|
const lines = fileContent.split("\n");
|
|
34
25
|
for (const line of lines) {
|
|
35
26
|
const trimmedLine = line.trim();
|
|
@@ -41,12 +32,7 @@ function parseEnvFile(filePath, result) {
|
|
|
41
32
|
.replace(/^"(.*)"$/, "$1")
|
|
42
33
|
.replace(/^'(.*)'$/, "$1");
|
|
43
34
|
result[key] = parsedValue;
|
|
44
|
-
|
|
45
|
-
process.env[key] = parsedValue;
|
|
46
|
-
}
|
|
47
|
-
else if (runtime === "deno") {
|
|
48
|
-
Deno.env.set(key, parsedValue);
|
|
49
|
-
}
|
|
35
|
+
Deno.env.set(key, parsedValue);
|
|
50
36
|
}
|
|
51
37
|
}
|
|
52
38
|
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./adpater.js"), exports);
|
|
18
|
+
__exportStar(require("./env.js"), exports);
|
package/cjs/helper/index.js
CHANGED
|
@@ -1,20 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
exports.
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
3
|
+
exports.generateID = exports.sanitizePathSplit = exports.GlobalConfig = exports.Environment = void 0;
|
|
4
|
+
var environment_js_1 = require("../core/environment.js");
|
|
5
|
+
Object.defineProperty(exports, "Environment", { enumerable: true, get: function () { return environment_js_1.Environment; } });
|
|
6
|
+
var config_js_1 = require("../core/config.js");
|
|
7
|
+
Object.defineProperty(exports, "GlobalConfig", { enumerable: true, get: function () { return config_js_1.GlobalConfig; } });
|
|
8
|
+
var url_js_1 = require("../utils/url.js");
|
|
9
|
+
Object.defineProperty(exports, "sanitizePathSplit", { enumerable: true, get: function () { return url_js_1.sanitizePathSplit; } });
|
|
10
|
+
var common_js_1 = require("./common.js");
|
|
11
|
+
Object.defineProperty(exports, "generateID", { enumerable: true, get: function () { return common_js_1.generateID; } });
|
package/cjs/index.js
CHANGED
|
@@ -7,4 +7,4 @@ var server_js_1 = require("./core/server.js");
|
|
|
7
7
|
Object.defineProperty(exports, "TezX", { enumerable: true, get: function () { return server_js_1.TezX; } });
|
|
8
8
|
var params_js_1 = require("./utils/params.js");
|
|
9
9
|
Object.defineProperty(exports, "useParams", { enumerable: true, get: function () { return params_js_1.useParams; } });
|
|
10
|
-
exports.version = "
|
|
10
|
+
exports.version = "2.0.0";
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.nodeAdapter = nodeAdapter;
|
|
4
|
+
const node_buffer_1 = require("node:buffer");
|
|
5
|
+
const node_http_1 = require("node:http");
|
|
6
|
+
const node_https_1 = require("node:https");
|
|
7
|
+
const config_js_1 = require("../core/config.js");
|
|
8
|
+
const context_js_1 = require("../core/context.js");
|
|
9
|
+
function nodeAdapter(TezX, options = {}) {
|
|
10
|
+
function listen(...arg) {
|
|
11
|
+
let ssl = options?.enableSSL;
|
|
12
|
+
let createServerFn = ssl ? node_https_1.createServer : node_http_1.createServer;
|
|
13
|
+
config_js_1.GlobalConfig.adapter = "node";
|
|
14
|
+
let server = createServerFn(options, async (req, res) => {
|
|
15
|
+
let address = {};
|
|
16
|
+
if (req.socket) {
|
|
17
|
+
address = {
|
|
18
|
+
remoteAddr: {
|
|
19
|
+
family: req.socket.remoteFamily,
|
|
20
|
+
address: req.socket.remoteAddress,
|
|
21
|
+
port: req.socket.remotePort,
|
|
22
|
+
},
|
|
23
|
+
localAddr: {
|
|
24
|
+
address: req.socket.localAddress,
|
|
25
|
+
port: req.socket.localPort,
|
|
26
|
+
family: req.socket.localFamily,
|
|
27
|
+
},
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
let options = {
|
|
31
|
+
connInfo: address,
|
|
32
|
+
};
|
|
33
|
+
const response = await TezX.serve(req, options);
|
|
34
|
+
if (typeof response?.websocket === "function" &&
|
|
35
|
+
response.ctx instanceof context_js_1.Context &&
|
|
36
|
+
response.ctx.wsProtocol) {
|
|
37
|
+
response.websocket(response.ctx, server);
|
|
38
|
+
return res.end();
|
|
39
|
+
}
|
|
40
|
+
const statusText = response?.statusText;
|
|
41
|
+
if (!(response instanceof Response)) {
|
|
42
|
+
throw new Error("Invalid response from TezX.serve");
|
|
43
|
+
}
|
|
44
|
+
if (statusText) {
|
|
45
|
+
res.statusMessage = statusText;
|
|
46
|
+
}
|
|
47
|
+
res.writeHead(response.status, [...response.headers.entries()]);
|
|
48
|
+
const { Readable } = await Promise.resolve().then(() => require("node:stream"));
|
|
49
|
+
const body = response.body;
|
|
50
|
+
if (response.headers.get("Content-Type") === "text/event-stream") {
|
|
51
|
+
req.socket.setTimeout(0);
|
|
52
|
+
}
|
|
53
|
+
if (body instanceof Readable) {
|
|
54
|
+
return body.pipe(res);
|
|
55
|
+
}
|
|
56
|
+
else if (body?.pipeTo || body?.getReader) {
|
|
57
|
+
try {
|
|
58
|
+
return Readable.fromWeb(body).pipe(res);
|
|
59
|
+
}
|
|
60
|
+
catch (err) {
|
|
61
|
+
config_js_1.GlobalConfig.debugging.warn("Failed to stream web body:", err);
|
|
62
|
+
return res.end();
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
else if (typeof body?.[Symbol.asyncIterator] === "function") {
|
|
66
|
+
const readable = Readable.from(body);
|
|
67
|
+
return readable.pipe(res);
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
try {
|
|
71
|
+
const buffer = await response.arrayBuffer?.();
|
|
72
|
+
if (buffer && buffer.byteLength > 0) {
|
|
73
|
+
return res.end(node_buffer_1.Buffer.from(buffer));
|
|
74
|
+
}
|
|
75
|
+
else {
|
|
76
|
+
return res.end();
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
catch (err) {
|
|
80
|
+
config_js_1.GlobalConfig.debugging.warn("Body extraction failed, trying text fallback...");
|
|
81
|
+
const text = await response.text?.();
|
|
82
|
+
return res.end(text ?? "");
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
const port = typeof arg[0] === "function" ? undefined : arg[0];
|
|
87
|
+
const callback = typeof arg[0] === "function" ? arg[0] : arg[1];
|
|
88
|
+
server.listen(options?.unix || port || 0, () => {
|
|
89
|
+
const protocol = ssl ? "\x1b[1;35mhttps\x1b[0m" : "\x1b[1;34mhttp\x1b[0m";
|
|
90
|
+
const address = server.address();
|
|
91
|
+
const message = typeof address === "string"
|
|
92
|
+
? `\x1b[1mNodeJS TezX Server running at unix://${address}\x1b[0m`
|
|
93
|
+
: `\x1b[1mNodeJS TezX Server running at ${protocol}://localhost:${address?.port}/\x1b[0m`;
|
|
94
|
+
config_js_1.GlobalConfig.server = server;
|
|
95
|
+
config_js_1.GlobalConfig.debugging.success(message);
|
|
96
|
+
if (typeof callback == "function")
|
|
97
|
+
callback();
|
|
98
|
+
return server;
|
|
99
|
+
});
|
|
100
|
+
return server;
|
|
101
|
+
}
|
|
102
|
+
return {
|
|
103
|
+
listen,
|
|
104
|
+
};
|
|
105
|
+
}
|
package/cjs/node/env.js
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.loadEnv = loadEnv;
|
|
4
|
+
const node_fs_1 = require("node:fs");
|
|
5
|
+
const environment_js_1 = require("../core/environment.js");
|
|
6
|
+
const colors_js_1 = require("../utils/colors.js");
|
|
7
|
+
function parseEnvFile(filePath, result) {
|
|
8
|
+
try {
|
|
9
|
+
let runtime = environment_js_1.Environment.getEnvironment;
|
|
10
|
+
if (runtime !== "bun" && runtime !== "node") {
|
|
11
|
+
throw new Error(`Please use ${colors_js_1.COLORS.bgRed}import {loadEnv} from "tezx/${runtime}"${colors_js_1.COLORS.reset} environment`);
|
|
12
|
+
}
|
|
13
|
+
let fileExists = (0, node_fs_1.existsSync)(filePath);
|
|
14
|
+
if (!fileExists) {
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
let fileContent = "";
|
|
18
|
+
fileContent = (0, node_fs_1.readFileSync)(filePath, "utf8");
|
|
19
|
+
const lines = fileContent.split("\n");
|
|
20
|
+
for (const line of lines) {
|
|
21
|
+
const trimmedLine = line.trim();
|
|
22
|
+
if (!trimmedLine || trimmedLine.startsWith("#"))
|
|
23
|
+
continue;
|
|
24
|
+
const [key, value] = trimmedLine.split("=", 2).map((part) => part.trim());
|
|
25
|
+
if (key && value) {
|
|
26
|
+
const parsedValue = value
|
|
27
|
+
.replace(/^"(.*)"$/, "$1")
|
|
28
|
+
.replace(/^'(.*)'$/, "$1");
|
|
29
|
+
result[key] = parsedValue;
|
|
30
|
+
process.env[key] = parsedValue;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
catch (error) {
|
|
35
|
+
console.error(`[dotenv] Error parsing file: ${filePath}`, error);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
function loadEnv(basePath = "./") {
|
|
39
|
+
const result = {};
|
|
40
|
+
const envFiles = [
|
|
41
|
+
".env",
|
|
42
|
+
".env.local",
|
|
43
|
+
`.env.${process?.env?.NODE_ENV || "development"}`,
|
|
44
|
+
`.env.${process?.env?.NODE_ENV || "development"}.local`,
|
|
45
|
+
];
|
|
46
|
+
for (const envFile of envFiles) {
|
|
47
|
+
parseEnvFile(`${basePath}${envFile}`, result);
|
|
48
|
+
}
|
|
49
|
+
return result;
|
|
50
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./adapter.js"), exports);
|
|
18
|
+
__exportStar(require("./env.js"), exports);
|
package/cjs/utils/staticFile.js
CHANGED
|
@@ -109,7 +109,7 @@ exports.mimeTypes = {
|
|
|
109
109
|
exports.defaultMimeType = "application/octet-stream";
|
|
110
110
|
async function getFiles(dir, basePath = "/", ref, option) {
|
|
111
111
|
const files = [];
|
|
112
|
-
const runtime = environment_js_1.
|
|
112
|
+
const runtime = environment_js_1.Environment.getEnvironment;
|
|
113
113
|
if (runtime == "deno") {
|
|
114
114
|
for await (const entry of Deno.readDir(dir)) {
|
|
115
115
|
const path = `${dir}/${entry.name}`;
|
package/cjs/ws/index.js
CHANGED
|
@@ -23,7 +23,7 @@ function upgradeWebSocket(callback, options = {}) {
|
|
|
23
23
|
}
|
|
24
24
|
ctx.wsProtocol = ctx.req.urlRef.protocol === "https:" ? "wss" : "ws";
|
|
25
25
|
try {
|
|
26
|
-
const env = environment_js_1.
|
|
26
|
+
const env = environment_js_1.Environment.getEnvironment;
|
|
27
27
|
if (!callback) {
|
|
28
28
|
throw new Error("WebSocket callback is missing. Please provide a valid callback function to handle the WebSocket events.");
|
|
29
29
|
}
|
package/core/config.d.ts
CHANGED
package/core/config.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { loggerOutput } from "../utils/debugging.js";
|
|
2
|
-
import {
|
|
2
|
+
import { Environment } from "./environment.js";
|
|
3
3
|
export let GlobalConfig = class {
|
|
4
4
|
static notFound = (ctx) => {
|
|
5
5
|
const { method, urlRef: { pathname }, } = ctx.req;
|
|
@@ -12,7 +12,7 @@ export let GlobalConfig = class {
|
|
|
12
12
|
static overwriteMethod = true;
|
|
13
13
|
static debugMode = false;
|
|
14
14
|
static server;
|
|
15
|
-
static adapter =
|
|
15
|
+
static adapter = Environment.getEnvironment;
|
|
16
16
|
static get debugging() {
|
|
17
17
|
return this.debugMode
|
|
18
18
|
? {
|
package/core/context.js
CHANGED
|
@@ -2,7 +2,7 @@ import { State } from "../utils/state.js";
|
|
|
2
2
|
import { defaultMimeType, mimeTypes } from "../utils/staticFile.js";
|
|
3
3
|
import { toWebRequest } from "../utils/toWebRequest.js";
|
|
4
4
|
import { GlobalConfig } from "./config.js";
|
|
5
|
-
import {
|
|
5
|
+
import { Environment } from "./environment.js";
|
|
6
6
|
import { Request as RequestParser } from "./request.js";
|
|
7
7
|
export class Context {
|
|
8
8
|
rawRequest;
|
|
@@ -210,7 +210,7 @@ export class Context {
|
|
|
210
210
|
async download(filePath, fileName) {
|
|
211
211
|
try {
|
|
212
212
|
let fileExists = false;
|
|
213
|
-
const runtime =
|
|
213
|
+
const runtime = Environment.getEnvironment;
|
|
214
214
|
if (runtime === "node") {
|
|
215
215
|
const { existsSync } = await import("node:fs");
|
|
216
216
|
fileExists = existsSync(filePath);
|
|
@@ -258,7 +258,7 @@ export class Context {
|
|
|
258
258
|
}
|
|
259
259
|
async sendFile(filePath, ...args) {
|
|
260
260
|
try {
|
|
261
|
-
const runtime =
|
|
261
|
+
const runtime = Environment.getEnvironment;
|
|
262
262
|
const resolvedPath = filePath;
|
|
263
263
|
let fileExists = false;
|
|
264
264
|
if (runtime === "node") {
|
package/core/environment.d.ts
CHANGED
package/core/environment.js
CHANGED
|
@@ -1,32 +1,23 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Environment } from "../core/environment.js";
|
|
2
|
+
import { COLORS } from "../utils/colors.js";
|
|
2
3
|
function parseEnvFile(filePath, result) {
|
|
3
4
|
try {
|
|
5
|
+
let runtime = Environment.getEnvironment;
|
|
6
|
+
if (runtime !== "deno") {
|
|
7
|
+
throw new Error(`Please use ${COLORS.bgRed}import {loadEnv} from "tezx/${runtime}"${COLORS.reset} environment`);
|
|
8
|
+
}
|
|
4
9
|
let fileExists = false;
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
fileExists = existsSync(filePath);
|
|
10
|
+
try {
|
|
11
|
+
Deno.statSync(filePath);
|
|
12
|
+
fileExists = true;
|
|
9
13
|
}
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
Deno.statSync(filePath);
|
|
13
|
-
fileExists = true;
|
|
14
|
-
}
|
|
15
|
-
catch {
|
|
16
|
-
fileExists = false;
|
|
17
|
-
}
|
|
14
|
+
catch {
|
|
15
|
+
fileExists = false;
|
|
18
16
|
}
|
|
19
17
|
if (!fileExists) {
|
|
20
18
|
return;
|
|
21
19
|
}
|
|
22
|
-
let fileContent = "";
|
|
23
|
-
if (runtime === "node" || runtime === "bun") {
|
|
24
|
-
const { readFileSync } = require("fs");
|
|
25
|
-
fileContent = readFileSync(filePath, "utf8");
|
|
26
|
-
}
|
|
27
|
-
else if (runtime === "deno") {
|
|
28
|
-
fileContent = new TextDecoder("utf-8").decode(Deno.readFileSync(filePath));
|
|
29
|
-
}
|
|
20
|
+
let fileContent = new TextDecoder("utf-8").decode(Deno.readFileSync(filePath));
|
|
30
21
|
const lines = fileContent.split("\n");
|
|
31
22
|
for (const line of lines) {
|
|
32
23
|
const trimmedLine = line.trim();
|
|
@@ -38,12 +29,7 @@ function parseEnvFile(filePath, result) {
|
|
|
38
29
|
.replace(/^"(.*)"$/, "$1")
|
|
39
30
|
.replace(/^'(.*)'$/, "$1");
|
|
40
31
|
result[key] = parsedValue;
|
|
41
|
-
|
|
42
|
-
process.env[key] = parsedValue;
|
|
43
|
-
}
|
|
44
|
-
else if (runtime === "deno") {
|
|
45
|
-
Deno.env.set(key, parsedValue);
|
|
46
|
-
}
|
|
32
|
+
Deno.env.set(key, parsedValue);
|
|
47
33
|
}
|
|
48
34
|
}
|
|
49
35
|
}
|
package/deno/index.d.ts
ADDED
package/deno/index.js
ADDED
package/helper/index.d.ts
CHANGED
|
@@ -1,2 +1,4 @@
|
|
|
1
|
-
export
|
|
2
|
-
export {
|
|
1
|
+
export { Environment } from "../core/environment.js";
|
|
2
|
+
export { GlobalConfig } from "../core/config.js";
|
|
3
|
+
export { sanitizePathSplit } from "../utils/url.js";
|
|
4
|
+
export { generateID } from "./common.js";
|
package/helper/index.js
CHANGED
|
@@ -1,2 +1,4 @@
|
|
|
1
|
-
export
|
|
2
|
-
export {
|
|
1
|
+
export { Environment } from "../core/environment.js";
|
|
2
|
+
export { GlobalConfig } from "../core/config.js";
|
|
3
|
+
export { sanitizePathSplit } from "../utils/url.js";
|
|
4
|
+
export { generateID } from "./common.js";
|
package/index.d.ts
CHANGED
|
@@ -3,6 +3,7 @@ export type { Callback, ctx as Context, Middleware, NextCallback, RouterConfig,
|
|
|
3
3
|
export type { CookieOptions, ResponseHeaders } from "./core/context.js";
|
|
4
4
|
export type { NetAddr as AddressType, ConnAddress, FormDataOptions, HTTPMethod, } from "./core/request.js";
|
|
5
5
|
export { TezX } from "./core/server.js";
|
|
6
|
+
export type { AdapterType } from "./core/config.js";
|
|
6
7
|
export type { TezXConfig } from "./core/server.js";
|
|
7
8
|
export { useParams } from "./utils/params.js";
|
|
8
9
|
export type { UrlRef } from "./utils/url.js";
|
package/index.js
CHANGED
package/node/adapter.js
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { Buffer } from "node:buffer";
|
|
2
|
+
import { createServer } from "node:http";
|
|
3
|
+
import { createServer as sslCreateServer } from "node:https";
|
|
4
|
+
import { GlobalConfig } from "../core/config.js";
|
|
5
|
+
import { Context } from "../core/context.js";
|
|
6
|
+
export function nodeAdapter(TezX, options = {}) {
|
|
7
|
+
function listen(...arg) {
|
|
8
|
+
let ssl = options?.enableSSL;
|
|
9
|
+
let createServerFn = ssl ? sslCreateServer : createServer;
|
|
10
|
+
GlobalConfig.adapter = "node";
|
|
11
|
+
let server = createServerFn(options, async (req, res) => {
|
|
12
|
+
let address = {};
|
|
13
|
+
if (req.socket) {
|
|
14
|
+
address = {
|
|
15
|
+
remoteAddr: {
|
|
16
|
+
family: req.socket.remoteFamily,
|
|
17
|
+
address: req.socket.remoteAddress,
|
|
18
|
+
port: req.socket.remotePort,
|
|
19
|
+
},
|
|
20
|
+
localAddr: {
|
|
21
|
+
address: req.socket.localAddress,
|
|
22
|
+
port: req.socket.localPort,
|
|
23
|
+
family: req.socket.localFamily,
|
|
24
|
+
},
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
let options = {
|
|
28
|
+
connInfo: address,
|
|
29
|
+
};
|
|
30
|
+
const response = await TezX.serve(req, options);
|
|
31
|
+
if (typeof response?.websocket === "function" &&
|
|
32
|
+
response.ctx instanceof Context &&
|
|
33
|
+
response.ctx.wsProtocol) {
|
|
34
|
+
response.websocket(response.ctx, server);
|
|
35
|
+
return res.end();
|
|
36
|
+
}
|
|
37
|
+
const statusText = response?.statusText;
|
|
38
|
+
if (!(response instanceof Response)) {
|
|
39
|
+
throw new Error("Invalid response from TezX.serve");
|
|
40
|
+
}
|
|
41
|
+
if (statusText) {
|
|
42
|
+
res.statusMessage = statusText;
|
|
43
|
+
}
|
|
44
|
+
res.writeHead(response.status, [...response.headers.entries()]);
|
|
45
|
+
const { Readable } = await import("node:stream");
|
|
46
|
+
const body = response.body;
|
|
47
|
+
if (response.headers.get("Content-Type") === "text/event-stream") {
|
|
48
|
+
req.socket.setTimeout(0);
|
|
49
|
+
}
|
|
50
|
+
if (body instanceof Readable) {
|
|
51
|
+
return body.pipe(res);
|
|
52
|
+
}
|
|
53
|
+
else if (body?.pipeTo || body?.getReader) {
|
|
54
|
+
try {
|
|
55
|
+
return Readable.fromWeb(body).pipe(res);
|
|
56
|
+
}
|
|
57
|
+
catch (err) {
|
|
58
|
+
GlobalConfig.debugging.warn("Failed to stream web body:", err);
|
|
59
|
+
return res.end();
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
else if (typeof body?.[Symbol.asyncIterator] === "function") {
|
|
63
|
+
const readable = Readable.from(body);
|
|
64
|
+
return readable.pipe(res);
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
try {
|
|
68
|
+
const buffer = await response.arrayBuffer?.();
|
|
69
|
+
if (buffer && buffer.byteLength > 0) {
|
|
70
|
+
return res.end(Buffer.from(buffer));
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
return res.end();
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
catch (err) {
|
|
77
|
+
GlobalConfig.debugging.warn("Body extraction failed, trying text fallback...");
|
|
78
|
+
const text = await response.text?.();
|
|
79
|
+
return res.end(text ?? "");
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
const port = typeof arg[0] === "function" ? undefined : arg[0];
|
|
84
|
+
const callback = typeof arg[0] === "function" ? arg[0] : arg[1];
|
|
85
|
+
server.listen(options?.unix || port || 0, () => {
|
|
86
|
+
const protocol = ssl ? "\x1b[1;35mhttps\x1b[0m" : "\x1b[1;34mhttp\x1b[0m";
|
|
87
|
+
const address = server.address();
|
|
88
|
+
const message = typeof address === "string"
|
|
89
|
+
? `\x1b[1mNodeJS TezX Server running at unix://${address}\x1b[0m`
|
|
90
|
+
: `\x1b[1mNodeJS TezX Server running at ${protocol}://localhost:${address?.port}/\x1b[0m`;
|
|
91
|
+
GlobalConfig.server = server;
|
|
92
|
+
GlobalConfig.debugging.success(message);
|
|
93
|
+
if (typeof callback == "function")
|
|
94
|
+
callback();
|
|
95
|
+
return server;
|
|
96
|
+
});
|
|
97
|
+
return server;
|
|
98
|
+
}
|
|
99
|
+
return {
|
|
100
|
+
listen,
|
|
101
|
+
};
|
|
102
|
+
}
|
package/node/env.d.ts
ADDED
package/node/env.js
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
2
|
+
import { Environment } from "../core/environment.js";
|
|
3
|
+
import { COLORS } from "../utils/colors.js";
|
|
4
|
+
function parseEnvFile(filePath, result) {
|
|
5
|
+
try {
|
|
6
|
+
let runtime = Environment.getEnvironment;
|
|
7
|
+
if (runtime !== "bun" && runtime !== "node") {
|
|
8
|
+
throw new Error(`Please use ${COLORS.bgRed}import {loadEnv} from "tezx/${runtime}"${COLORS.reset} environment`);
|
|
9
|
+
}
|
|
10
|
+
let fileExists = existsSync(filePath);
|
|
11
|
+
if (!fileExists) {
|
|
12
|
+
return;
|
|
13
|
+
}
|
|
14
|
+
let fileContent = "";
|
|
15
|
+
fileContent = readFileSync(filePath, "utf8");
|
|
16
|
+
const lines = fileContent.split("\n");
|
|
17
|
+
for (const line of lines) {
|
|
18
|
+
const trimmedLine = line.trim();
|
|
19
|
+
if (!trimmedLine || trimmedLine.startsWith("#"))
|
|
20
|
+
continue;
|
|
21
|
+
const [key, value] = trimmedLine.split("=", 2).map((part) => part.trim());
|
|
22
|
+
if (key && value) {
|
|
23
|
+
const parsedValue = value
|
|
24
|
+
.replace(/^"(.*)"$/, "$1")
|
|
25
|
+
.replace(/^'(.*)'$/, "$1");
|
|
26
|
+
result[key] = parsedValue;
|
|
27
|
+
process.env[key] = parsedValue;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
catch (error) {
|
|
32
|
+
console.error(`[dotenv] Error parsing file: ${filePath}`, error);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
export function loadEnv(basePath = "./") {
|
|
36
|
+
const result = {};
|
|
37
|
+
const envFiles = [
|
|
38
|
+
".env",
|
|
39
|
+
".env.local",
|
|
40
|
+
`.env.${process?.env?.NODE_ENV || "development"}`,
|
|
41
|
+
`.env.${process?.env?.NODE_ENV || "development"}.local`,
|
|
42
|
+
];
|
|
43
|
+
for (const envFile of envFiles) {
|
|
44
|
+
parseEnvFile(`${basePath}${envFile}`, result);
|
|
45
|
+
}
|
|
46
|
+
return result;
|
|
47
|
+
}
|
package/node/index.d.ts
ADDED
package/node/index.js
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tezx",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.1",
|
|
4
4
|
"description": "TezX is a high-performance, lightweight JavaScript framework designed for speed, scalability, and flexibility. It enables efficient routing, middleware management, and static file serving with minimal configuration. Fully compatible with Node.js, Deno, and Bun.",
|
|
5
5
|
"main": "cjs/index.js",
|
|
6
6
|
"module": "index.js",
|
|
@@ -28,11 +28,23 @@
|
|
|
28
28
|
"types": "./middleware/index.d.ts",
|
|
29
29
|
"default": "./middleware/index.js"
|
|
30
30
|
},
|
|
31
|
-
"./
|
|
32
|
-
"import": "./
|
|
33
|
-
"require": "./cjs/
|
|
34
|
-
"types": "./
|
|
35
|
-
"default": "./
|
|
31
|
+
"./node": {
|
|
32
|
+
"import": "./node/index.js",
|
|
33
|
+
"require": "./cjs/node/index.js",
|
|
34
|
+
"types": "./node/index.d.ts",
|
|
35
|
+
"default": "./node/index.js"
|
|
36
|
+
},
|
|
37
|
+
"./bun": {
|
|
38
|
+
"import": "./bun/index.js",
|
|
39
|
+
"require": "./cjs/bun/index.js",
|
|
40
|
+
"types": "./bun/index.d.ts",
|
|
41
|
+
"default": "./bun/index.js"
|
|
42
|
+
},
|
|
43
|
+
"./deno": {
|
|
44
|
+
"import": "./deno/index.js",
|
|
45
|
+
"require": "./cjs/deno/index.js",
|
|
46
|
+
"types": "./deno/index.d.ts",
|
|
47
|
+
"default": "./deno/index.js"
|
|
36
48
|
},
|
|
37
49
|
"./helper": {
|
|
38
50
|
"import": "./helper/index.js",
|
|
@@ -52,6 +64,9 @@
|
|
|
52
64
|
"cjs/",
|
|
53
65
|
"core/",
|
|
54
66
|
"helper/",
|
|
67
|
+
"bun/",
|
|
68
|
+
"deno/",
|
|
69
|
+
"node/",
|
|
55
70
|
"middleware/",
|
|
56
71
|
"utils/",
|
|
57
72
|
"ws/",
|
package/utils/staticFile.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Environment } from "../core/environment.js";
|
|
2
2
|
export const mimeTypes = {
|
|
3
3
|
html: "text/html",
|
|
4
4
|
htm: "text/html",
|
|
@@ -105,7 +105,7 @@ export const mimeTypes = {
|
|
|
105
105
|
export const defaultMimeType = "application/octet-stream";
|
|
106
106
|
export async function getFiles(dir, basePath = "/", ref, option) {
|
|
107
107
|
const files = [];
|
|
108
|
-
const runtime =
|
|
108
|
+
const runtime = Environment.getEnvironment;
|
|
109
109
|
if (runtime == "deno") {
|
|
110
110
|
for await (const entry of Deno.readDir(dir)) {
|
|
111
111
|
const path = `${dir}/${entry.name}`;
|
package/ws/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Environment } from "../core/environment.js";
|
|
2
2
|
import { DenoTransport } from "./deno.js";
|
|
3
3
|
import { NodeTransport } from "./node.js";
|
|
4
4
|
export function upgradeWebSocket(callback, options = {}) {
|
|
@@ -20,7 +20,7 @@ export function upgradeWebSocket(callback, options = {}) {
|
|
|
20
20
|
}
|
|
21
21
|
ctx.wsProtocol = ctx.req.urlRef.protocol === "https:" ? "wss" : "ws";
|
|
22
22
|
try {
|
|
23
|
-
const env =
|
|
23
|
+
const env = Environment.getEnvironment;
|
|
24
24
|
if (!callback) {
|
|
25
25
|
throw new Error("WebSocket callback is missing. Please provide a valid callback function to handle the WebSocket events.");
|
|
26
26
|
}
|
package/adapter/index.d.ts
DELETED
package/adapter/index.js
DELETED
package/adapter/node.js
DELETED
|
@@ -1,107 +0,0 @@
|
|
|
1
|
-
import { Buffer } from "node:buffer";
|
|
2
|
-
import { GlobalConfig } from "../core/config.js";
|
|
3
|
-
import { Context } from "../core/context.js";
|
|
4
|
-
export function nodeAdapter(TezX, options = {}) {
|
|
5
|
-
function listen(...arg) {
|
|
6
|
-
let ssl = options?.enableSSL;
|
|
7
|
-
return import(ssl ? "node:https" : "node:http")
|
|
8
|
-
.then((r) => {
|
|
9
|
-
GlobalConfig.adapter = "node";
|
|
10
|
-
let server = r.createServer(options, async (req, res) => {
|
|
11
|
-
let address = {};
|
|
12
|
-
if (req.socket) {
|
|
13
|
-
address = {
|
|
14
|
-
remoteAddr: {
|
|
15
|
-
family: req.socket.remoteFamily,
|
|
16
|
-
address: req.socket.remoteAddress,
|
|
17
|
-
port: req.socket.remotePort,
|
|
18
|
-
},
|
|
19
|
-
localAddr: {
|
|
20
|
-
address: req.socket.localAddress,
|
|
21
|
-
port: req.socket.localPort,
|
|
22
|
-
family: req.socket.localFamily,
|
|
23
|
-
},
|
|
24
|
-
};
|
|
25
|
-
}
|
|
26
|
-
let options = {
|
|
27
|
-
connInfo: address,
|
|
28
|
-
};
|
|
29
|
-
const response = await TezX.serve(req, options);
|
|
30
|
-
if (typeof response?.websocket === "function" &&
|
|
31
|
-
response.ctx instanceof Context &&
|
|
32
|
-
response.ctx.wsProtocol) {
|
|
33
|
-
response.websocket(response.ctx, server);
|
|
34
|
-
return res.end();
|
|
35
|
-
}
|
|
36
|
-
const statusText = response?.statusText;
|
|
37
|
-
if (!(response instanceof Response)) {
|
|
38
|
-
throw new Error("Invalid response from TezX.serve");
|
|
39
|
-
}
|
|
40
|
-
if (statusText) {
|
|
41
|
-
res.statusMessage = statusText;
|
|
42
|
-
}
|
|
43
|
-
res.writeHead(response.status, [...response.headers.entries()]);
|
|
44
|
-
const { Readable } = await import("node:stream");
|
|
45
|
-
const body = response.body;
|
|
46
|
-
if (response.headers.get("Content-Type") === "text/event-stream") {
|
|
47
|
-
req.socket.setTimeout(0);
|
|
48
|
-
}
|
|
49
|
-
if (body instanceof Readable) {
|
|
50
|
-
return body.pipe(res);
|
|
51
|
-
}
|
|
52
|
-
else if (body?.pipeTo || body?.getReader) {
|
|
53
|
-
try {
|
|
54
|
-
return Readable.fromWeb(body).pipe(res);
|
|
55
|
-
}
|
|
56
|
-
catch (err) {
|
|
57
|
-
GlobalConfig.debugging.warn("Failed to stream web body:", err);
|
|
58
|
-
return res.end();
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
else if (typeof body?.[Symbol.asyncIterator] === "function") {
|
|
62
|
-
const readable = Readable.from(body);
|
|
63
|
-
return readable.pipe(res);
|
|
64
|
-
}
|
|
65
|
-
else {
|
|
66
|
-
try {
|
|
67
|
-
const buffer = await response.arrayBuffer?.();
|
|
68
|
-
if (buffer && buffer.byteLength > 0) {
|
|
69
|
-
return res.end(Buffer.from(buffer));
|
|
70
|
-
}
|
|
71
|
-
else {
|
|
72
|
-
return res.end();
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
catch (err) {
|
|
76
|
-
GlobalConfig.debugging.warn("Body extraction failed, trying text fallback...");
|
|
77
|
-
const text = await response.text?.();
|
|
78
|
-
return res.end(text ?? "");
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
});
|
|
82
|
-
const port = typeof arg[0] === "function" ? undefined : arg[0];
|
|
83
|
-
const callback = typeof arg[0] === "function" ? arg[0] : arg[1];
|
|
84
|
-
server.listen(options?.unix || port || 0, () => {
|
|
85
|
-
const protocol = ssl
|
|
86
|
-
? "\x1b[1;35mhttps\x1b[0m"
|
|
87
|
-
: "\x1b[1;34mhttp\x1b[0m";
|
|
88
|
-
const address = server.address();
|
|
89
|
-
const message = typeof address === "string"
|
|
90
|
-
? `\x1b[1mNodeJS TezX Server running at unix://${address}\x1b[0m`
|
|
91
|
-
: `\x1b[1mNodeJS TezX Server running at ${protocol}://localhost:${address?.port}/\x1b[0m`;
|
|
92
|
-
GlobalConfig.server = server;
|
|
93
|
-
GlobalConfig.debugging.success(message);
|
|
94
|
-
if (typeof callback == "function")
|
|
95
|
-
callback();
|
|
96
|
-
return server;
|
|
97
|
-
});
|
|
98
|
-
return server;
|
|
99
|
-
})
|
|
100
|
-
.catch((r) => {
|
|
101
|
-
throw Error(r.message);
|
|
102
|
-
});
|
|
103
|
-
}
|
|
104
|
-
return {
|
|
105
|
-
listen,
|
|
106
|
-
};
|
|
107
|
-
}
|
package/cjs/adapter/node.js
DELETED
|
@@ -1,109 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.nodeAdapter = nodeAdapter;
|
|
4
|
-
const node_buffer_1 = require("node:buffer");
|
|
5
|
-
const config_js_1 = require("../core/config.js");
|
|
6
|
-
const context_js_1 = require("../core/context.js");
|
|
7
|
-
function nodeAdapter(TezX, options = {}) {
|
|
8
|
-
function listen(...arg) {
|
|
9
|
-
let ssl = options?.enableSSL;
|
|
10
|
-
return Promise.resolve(`${ssl ? "node:https" : "node:http"}`).then(s => require(s)).then((r) => {
|
|
11
|
-
config_js_1.GlobalConfig.adapter = "node";
|
|
12
|
-
let server = r.createServer(options, async (req, res) => {
|
|
13
|
-
let address = {};
|
|
14
|
-
if (req.socket) {
|
|
15
|
-
address = {
|
|
16
|
-
remoteAddr: {
|
|
17
|
-
family: req.socket.remoteFamily,
|
|
18
|
-
address: req.socket.remoteAddress,
|
|
19
|
-
port: req.socket.remotePort,
|
|
20
|
-
},
|
|
21
|
-
localAddr: {
|
|
22
|
-
address: req.socket.localAddress,
|
|
23
|
-
port: req.socket.localPort,
|
|
24
|
-
family: req.socket.localFamily,
|
|
25
|
-
},
|
|
26
|
-
};
|
|
27
|
-
}
|
|
28
|
-
let options = {
|
|
29
|
-
connInfo: address,
|
|
30
|
-
};
|
|
31
|
-
const response = await TezX.serve(req, options);
|
|
32
|
-
if (typeof response?.websocket === "function" &&
|
|
33
|
-
response.ctx instanceof context_js_1.Context &&
|
|
34
|
-
response.ctx.wsProtocol) {
|
|
35
|
-
response.websocket(response.ctx, server);
|
|
36
|
-
return res.end();
|
|
37
|
-
}
|
|
38
|
-
const statusText = response?.statusText;
|
|
39
|
-
if (!(response instanceof Response)) {
|
|
40
|
-
throw new Error("Invalid response from TezX.serve");
|
|
41
|
-
}
|
|
42
|
-
if (statusText) {
|
|
43
|
-
res.statusMessage = statusText;
|
|
44
|
-
}
|
|
45
|
-
res.writeHead(response.status, [...response.headers.entries()]);
|
|
46
|
-
const { Readable } = await Promise.resolve().then(() => require("node:stream"));
|
|
47
|
-
const body = response.body;
|
|
48
|
-
if (response.headers.get("Content-Type") === "text/event-stream") {
|
|
49
|
-
req.socket.setTimeout(0);
|
|
50
|
-
}
|
|
51
|
-
if (body instanceof Readable) {
|
|
52
|
-
return body.pipe(res);
|
|
53
|
-
}
|
|
54
|
-
else if (body?.pipeTo || body?.getReader) {
|
|
55
|
-
try {
|
|
56
|
-
return Readable.fromWeb(body).pipe(res);
|
|
57
|
-
}
|
|
58
|
-
catch (err) {
|
|
59
|
-
config_js_1.GlobalConfig.debugging.warn("Failed to stream web body:", err);
|
|
60
|
-
return res.end();
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
else if (typeof body?.[Symbol.asyncIterator] === "function") {
|
|
64
|
-
const readable = Readable.from(body);
|
|
65
|
-
return readable.pipe(res);
|
|
66
|
-
}
|
|
67
|
-
else {
|
|
68
|
-
try {
|
|
69
|
-
const buffer = await response.arrayBuffer?.();
|
|
70
|
-
if (buffer && buffer.byteLength > 0) {
|
|
71
|
-
return res.end(node_buffer_1.Buffer.from(buffer));
|
|
72
|
-
}
|
|
73
|
-
else {
|
|
74
|
-
return res.end();
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
catch (err) {
|
|
78
|
-
config_js_1.GlobalConfig.debugging.warn("Body extraction failed, trying text fallback...");
|
|
79
|
-
const text = await response.text?.();
|
|
80
|
-
return res.end(text ?? "");
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
});
|
|
84
|
-
const port = typeof arg[0] === "function" ? undefined : arg[0];
|
|
85
|
-
const callback = typeof arg[0] === "function" ? arg[0] : arg[1];
|
|
86
|
-
server.listen(options?.unix || port || 0, () => {
|
|
87
|
-
const protocol = ssl
|
|
88
|
-
? "\x1b[1;35mhttps\x1b[0m"
|
|
89
|
-
: "\x1b[1;34mhttp\x1b[0m";
|
|
90
|
-
const address = server.address();
|
|
91
|
-
const message = typeof address === "string"
|
|
92
|
-
? `\x1b[1mNodeJS TezX Server running at unix://${address}\x1b[0m`
|
|
93
|
-
: `\x1b[1mNodeJS TezX Server running at ${protocol}://localhost:${address?.port}/\x1b[0m`;
|
|
94
|
-
config_js_1.GlobalConfig.server = server;
|
|
95
|
-
config_js_1.GlobalConfig.debugging.success(message);
|
|
96
|
-
if (typeof callback == "function")
|
|
97
|
-
callback();
|
|
98
|
-
return server;
|
|
99
|
-
});
|
|
100
|
-
return server;
|
|
101
|
-
})
|
|
102
|
-
.catch((r) => {
|
|
103
|
-
throw Error(r.message);
|
|
104
|
-
});
|
|
105
|
-
}
|
|
106
|
-
return {
|
|
107
|
-
listen,
|
|
108
|
-
};
|
|
109
|
-
}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|