elit 3.1.6 → 3.1.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/build.d.mts +3 -1
- package/dist/cli.js +442 -218
- package/dist/database.d.mts +31 -0
- package/dist/database.d.ts +30 -0
- package/dist/database.d.ts.map +1 -0
- package/dist/database.js +440 -0
- package/dist/database.mjs +406 -0
- package/dist/hmr.d.ts.map +1 -1
- package/dist/hmr.js +4 -5
- package/dist/hmr.mjs +4 -5
- package/dist/http.d.mts +3 -0
- package/dist/http.d.ts +3 -0
- package/dist/http.d.ts.map +1 -1
- package/dist/http.js +23 -0
- package/dist/http.mjs +23 -0
- package/dist/https.js +23 -0
- package/dist/https.mjs +23 -0
- package/dist/index.js +4 -5
- package/dist/index.mjs +4 -5
- package/dist/{server-CkRUWELa.d.ts → server-BFOHbYb6.d.ts} +32 -14
- package/dist/{server-D8ktU14v.d.mts → server-BPVoq5Xi.d.mts} +32 -14
- package/dist/server.d.mts +3 -1
- package/dist/server.d.ts +28 -13
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +431 -234
- package/dist/server.mjs +429 -234
- package/dist/types.d.mts +35 -13
- package/dist/types.d.ts +7 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/wss.js +23 -0
- package/dist/wss.mjs +23 -0
- package/package.json +8 -2
- package/src/cli.ts +2 -1
- package/src/database.ts +246 -0
- package/src/hmr.ts +7 -6
- package/src/http.ts +26 -0
- package/src/server.ts +90 -65
- package/src/types.ts +7 -0
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import vm from 'node:vm';
|
|
2
|
+
|
|
3
|
+
interface DatabaseConfig {
|
|
4
|
+
dir?: string;
|
|
5
|
+
language?: 'ts' | 'js';
|
|
6
|
+
registerModules?: {
|
|
7
|
+
[key: string]: any;
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
declare class Database {
|
|
11
|
+
private _ctx;
|
|
12
|
+
private _registerModules;
|
|
13
|
+
private _config;
|
|
14
|
+
constructor(config: DatabaseConfig);
|
|
15
|
+
set config(config: DatabaseConfig);
|
|
16
|
+
private register;
|
|
17
|
+
plugin(moduleName: string, moduleContent: any): void;
|
|
18
|
+
private resolvePath;
|
|
19
|
+
private moduleLinker;
|
|
20
|
+
private vmRun;
|
|
21
|
+
/**
|
|
22
|
+
* Execute database code and return results
|
|
23
|
+
*/
|
|
24
|
+
execute(code: string | Function, options?: vm.RunningCodeOptions | string): Promise<{
|
|
25
|
+
namespace: Object;
|
|
26
|
+
logs: any[];
|
|
27
|
+
}>;
|
|
28
|
+
}
|
|
29
|
+
declare function database(): Database;
|
|
30
|
+
|
|
31
|
+
export { Database, type DatabaseConfig, database, database as default };
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import vm from "node:vm";
|
|
2
|
+
export interface DatabaseConfig {
|
|
3
|
+
dir?: string;
|
|
4
|
+
language?: 'ts' | 'js';
|
|
5
|
+
registerModules?: {
|
|
6
|
+
[key: string]: any;
|
|
7
|
+
};
|
|
8
|
+
}
|
|
9
|
+
export declare class Database {
|
|
10
|
+
private _ctx;
|
|
11
|
+
private _registerModules;
|
|
12
|
+
private _config;
|
|
13
|
+
constructor(config: DatabaseConfig);
|
|
14
|
+
set config(config: DatabaseConfig);
|
|
15
|
+
private register;
|
|
16
|
+
plugin(moduleName: string, moduleContent: any): void;
|
|
17
|
+
private resolvePath;
|
|
18
|
+
private moduleLinker;
|
|
19
|
+
private vmRun;
|
|
20
|
+
/**
|
|
21
|
+
* Execute database code and return results
|
|
22
|
+
*/
|
|
23
|
+
execute(code: string | Function, options?: vm.RunningCodeOptions | string): Promise<{
|
|
24
|
+
namespace: Object;
|
|
25
|
+
logs: any[];
|
|
26
|
+
}>;
|
|
27
|
+
}
|
|
28
|
+
export declare function database(): Database;
|
|
29
|
+
export default database;
|
|
30
|
+
//# sourceMappingURL=database.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"database.d.ts","sourceRoot":"","sources":["../src/database.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,SAAS,CAAC;AAMzB,MAAM,WAAW,cAAc;IAC3B,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,QAAQ,CAAC,EAAE,IAAI,GAAG,IAAI,CAAC;IACvB,eAAe,CAAC,EAAE;QAAE,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAA;KAAE,CAAC;CAC5C;AAED,qBAAa,QAAQ;IACjB,OAAO,CAAC,IAAI,CAAa;IACzB,OAAO,CAAC,gBAAgB,CAAyB;IACjD,OAAO,CAAC,OAAO,CAEb;gBAEU,MAAM,EAAE,cAAc;IAMlC,IAAI,MAAM,CAAC,MAAM,EAAE,cAAc,EAEhC;IAED,OAAO,CAAC,QAAQ;IAMhB,MAAM,CAAC,UAAU,EAAE,MAAM,EAAE,aAAa,EAAE,GAAG;IAI7C,OAAO,CAAC,WAAW;YAwBL,YAAY;YA8BZ,KAAK;IAuInB;;OAEG;IACG,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,QAAQ,EAAE,OAAO,CAAC,EAAE,EAAE,CAAC,kBAAkB,GAAG,MAAM;;;;CAIlF;AAID,wBAAgB,QAAQ,aAIvB;AAED,eAAe,QAAQ,CAAC"}
|
package/dist/database.js
ADDED
|
@@ -0,0 +1,440 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/database.ts
|
|
31
|
+
var database_exports = {};
|
|
32
|
+
__export(database_exports, {
|
|
33
|
+
Database: () => Database,
|
|
34
|
+
database: () => database,
|
|
35
|
+
default: () => database_default
|
|
36
|
+
});
|
|
37
|
+
module.exports = __toCommonJS(database_exports);
|
|
38
|
+
var import_node_vm = __toESM(require("vm"));
|
|
39
|
+
|
|
40
|
+
// src/runtime.ts
|
|
41
|
+
var runtime = (() => {
|
|
42
|
+
if (typeof Deno !== "undefined") return "deno";
|
|
43
|
+
if (typeof Bun !== "undefined") return "bun";
|
|
44
|
+
return "node";
|
|
45
|
+
})();
|
|
46
|
+
var isNode = runtime === "node";
|
|
47
|
+
var isBun = runtime === "bun";
|
|
48
|
+
var isDeno = runtime === "deno";
|
|
49
|
+
|
|
50
|
+
// src/path.ts
|
|
51
|
+
function getSeparator(isWin) {
|
|
52
|
+
return isWin ? "\\" : "/";
|
|
53
|
+
}
|
|
54
|
+
function getCwd() {
|
|
55
|
+
if (isNode || isBun) {
|
|
56
|
+
return process.cwd();
|
|
57
|
+
} else if (isDeno) {
|
|
58
|
+
return Deno.cwd();
|
|
59
|
+
}
|
|
60
|
+
return "/";
|
|
61
|
+
}
|
|
62
|
+
function findLastSeparator(path2) {
|
|
63
|
+
return Math.max(path2.lastIndexOf("/"), path2.lastIndexOf("\\"));
|
|
64
|
+
}
|
|
65
|
+
function createPathOps(isWin) {
|
|
66
|
+
return {
|
|
67
|
+
sep: getSeparator(isWin),
|
|
68
|
+
delimiter: isWin ? ";" : ":",
|
|
69
|
+
normalize: (path2) => normalizePath(path2, isWin),
|
|
70
|
+
join: (...paths) => joinPaths(paths, isWin),
|
|
71
|
+
resolve: (...paths) => resolvePaths(paths, isWin),
|
|
72
|
+
isAbsolute: (path2) => isWin ? isAbsoluteWin(path2) : isAbsolutePosix(path2),
|
|
73
|
+
relative: (from, to) => relativePath(from, to, isWin),
|
|
74
|
+
dirname: (path2) => getDirname(path2, isWin),
|
|
75
|
+
basename: (path2, ext) => getBasename(path2, ext, isWin),
|
|
76
|
+
extname: (path2) => getExtname(path2),
|
|
77
|
+
parse: (path2) => parsePath(path2, isWin),
|
|
78
|
+
format: (pathObject) => formatPath(pathObject, isWin)
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
function isAbsolutePosix(path2) {
|
|
82
|
+
return path2.length > 0 && path2[0] === "/";
|
|
83
|
+
}
|
|
84
|
+
function isAbsoluteWin(path2) {
|
|
85
|
+
const len = path2.length;
|
|
86
|
+
if (len === 0) return false;
|
|
87
|
+
const code = path2.charCodeAt(0);
|
|
88
|
+
if (code === 47 || code === 92) {
|
|
89
|
+
return true;
|
|
90
|
+
}
|
|
91
|
+
if (code >= 65 && code <= 90 || code >= 97 && code <= 122) {
|
|
92
|
+
if (len > 2 && path2.charCodeAt(1) === 58) {
|
|
93
|
+
const code2 = path2.charCodeAt(2);
|
|
94
|
+
if (code2 === 47 || code2 === 92) {
|
|
95
|
+
return true;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return false;
|
|
100
|
+
}
|
|
101
|
+
var isWindows = (() => {
|
|
102
|
+
if (isNode) {
|
|
103
|
+
return process.platform === "win32";
|
|
104
|
+
} else if (isDeno) {
|
|
105
|
+
return Deno.build.os === "windows";
|
|
106
|
+
}
|
|
107
|
+
return typeof process !== "undefined" && process.platform === "win32";
|
|
108
|
+
})();
|
|
109
|
+
var posix = createPathOps(false);
|
|
110
|
+
var win32 = createPathOps(true);
|
|
111
|
+
function normalizePath(path2, isWin) {
|
|
112
|
+
if (path2.length === 0) return ".";
|
|
113
|
+
const separator = getSeparator(isWin);
|
|
114
|
+
const isAbsolute = isWin ? isAbsoluteWin(path2) : isAbsolutePosix(path2);
|
|
115
|
+
const trailingSeparator = path2[path2.length - 1] === separator || isWin && path2[path2.length - 1] === "/";
|
|
116
|
+
let normalized = path2.replace(isWin ? /[\/\\]+/g : /\/+/g, separator);
|
|
117
|
+
const parts = normalized.split(separator);
|
|
118
|
+
const result = [];
|
|
119
|
+
for (let i = 0; i < parts.length; i++) {
|
|
120
|
+
const part = parts[i];
|
|
121
|
+
if (part === "" || part === ".") {
|
|
122
|
+
if (i === 0 && isAbsolute) result.push("");
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
125
|
+
if (part === "..") {
|
|
126
|
+
if (result.length > 0 && result[result.length - 1] !== "..") {
|
|
127
|
+
if (!(result.length === 1 && result[0] === "")) {
|
|
128
|
+
result.pop();
|
|
129
|
+
}
|
|
130
|
+
} else if (!isAbsolute) {
|
|
131
|
+
result.push("..");
|
|
132
|
+
}
|
|
133
|
+
} else {
|
|
134
|
+
result.push(part);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
let final = result.join(separator);
|
|
138
|
+
if (final.length === 0) {
|
|
139
|
+
return isAbsolute ? separator : ".";
|
|
140
|
+
}
|
|
141
|
+
if (trailingSeparator && final[final.length - 1] !== separator) {
|
|
142
|
+
final += separator;
|
|
143
|
+
}
|
|
144
|
+
return final;
|
|
145
|
+
}
|
|
146
|
+
function joinPaths(paths, isWin) {
|
|
147
|
+
if (paths.length === 0) return ".";
|
|
148
|
+
const separator = getSeparator(isWin);
|
|
149
|
+
let joined = "";
|
|
150
|
+
for (let i = 0; i < paths.length; i++) {
|
|
151
|
+
const path2 = paths[i];
|
|
152
|
+
if (path2 && path2.length > 0) {
|
|
153
|
+
if (joined.length === 0) {
|
|
154
|
+
joined = path2;
|
|
155
|
+
} else {
|
|
156
|
+
joined += separator + path2;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
if (joined.length === 0) return ".";
|
|
161
|
+
return normalizePath(joined, isWin);
|
|
162
|
+
}
|
|
163
|
+
function resolvePaths(paths, isWin) {
|
|
164
|
+
const separator = getSeparator(isWin);
|
|
165
|
+
let resolved = "";
|
|
166
|
+
let isAbsolute = false;
|
|
167
|
+
for (let i = paths.length - 1; i >= 0 && !isAbsolute; i--) {
|
|
168
|
+
const path2 = paths[i];
|
|
169
|
+
if (path2 && path2.length > 0) {
|
|
170
|
+
resolved = path2 + (resolved.length > 0 ? separator + resolved : "");
|
|
171
|
+
isAbsolute = isWin ? isAbsoluteWin(resolved) : isAbsolutePosix(resolved);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
if (!isAbsolute) {
|
|
175
|
+
const cwd = getCwd();
|
|
176
|
+
resolved = cwd + (resolved.length > 0 ? separator + resolved : "");
|
|
177
|
+
}
|
|
178
|
+
return normalizePath(resolved, isWin);
|
|
179
|
+
}
|
|
180
|
+
function relativePath(from, to, isWin) {
|
|
181
|
+
from = resolvePaths([from], isWin);
|
|
182
|
+
to = resolvePaths([to], isWin);
|
|
183
|
+
if (from === to) return "";
|
|
184
|
+
const separator = getSeparator(isWin);
|
|
185
|
+
const fromParts = from.split(separator).filter((p) => p.length > 0);
|
|
186
|
+
const toParts = to.split(separator).filter((p) => p.length > 0);
|
|
187
|
+
let commonLength = 0;
|
|
188
|
+
const minLength = Math.min(fromParts.length, toParts.length);
|
|
189
|
+
for (let i = 0; i < minLength; i++) {
|
|
190
|
+
if (fromParts[i] === toParts[i]) {
|
|
191
|
+
commonLength++;
|
|
192
|
+
} else {
|
|
193
|
+
break;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
const upCount = fromParts.length - commonLength;
|
|
197
|
+
const result = [];
|
|
198
|
+
for (let i = 0; i < upCount; i++) {
|
|
199
|
+
result.push("..");
|
|
200
|
+
}
|
|
201
|
+
for (let i = commonLength; i < toParts.length; i++) {
|
|
202
|
+
result.push(toParts[i]);
|
|
203
|
+
}
|
|
204
|
+
return result.join(separator) || ".";
|
|
205
|
+
}
|
|
206
|
+
function getDirname(path2, isWin) {
|
|
207
|
+
if (path2.length === 0) return ".";
|
|
208
|
+
const separator = getSeparator(isWin);
|
|
209
|
+
const normalized = normalizePath(path2, isWin);
|
|
210
|
+
const lastSepIndex = normalized.lastIndexOf(separator);
|
|
211
|
+
if (lastSepIndex === -1) return ".";
|
|
212
|
+
if (lastSepIndex === 0) return separator;
|
|
213
|
+
return normalized.slice(0, lastSepIndex);
|
|
214
|
+
}
|
|
215
|
+
function getBasename(path2, ext, isWin) {
|
|
216
|
+
if (path2.length === 0) return "";
|
|
217
|
+
const lastSepIndex = isWin ? findLastSeparator(path2) : path2.lastIndexOf("/");
|
|
218
|
+
let base = lastSepIndex === -1 ? path2 : path2.slice(lastSepIndex + 1);
|
|
219
|
+
if (ext && base.endsWith(ext)) {
|
|
220
|
+
base = base.slice(0, base.length - ext.length);
|
|
221
|
+
}
|
|
222
|
+
return base;
|
|
223
|
+
}
|
|
224
|
+
function getExtname(path2) {
|
|
225
|
+
const lastDotIndex = path2.lastIndexOf(".");
|
|
226
|
+
const lastSepIndex = findLastSeparator(path2);
|
|
227
|
+
if (lastDotIndex === -1 || lastDotIndex < lastSepIndex || lastDotIndex === path2.length - 1) {
|
|
228
|
+
return "";
|
|
229
|
+
}
|
|
230
|
+
return path2.slice(lastDotIndex);
|
|
231
|
+
}
|
|
232
|
+
function parsePath(path2, isWin) {
|
|
233
|
+
let root = "";
|
|
234
|
+
if (isWin) {
|
|
235
|
+
if (path2.length >= 2 && path2[1] === ":") {
|
|
236
|
+
root = path2.slice(0, 2);
|
|
237
|
+
if (path2.length > 2 && (path2[2] === "\\" || path2[2] === "/")) {
|
|
238
|
+
root += "\\";
|
|
239
|
+
}
|
|
240
|
+
} else if (path2[0] === "\\" || path2[0] === "/") {
|
|
241
|
+
root = "\\";
|
|
242
|
+
}
|
|
243
|
+
} else {
|
|
244
|
+
if (path2[0] === "/") {
|
|
245
|
+
root = "/";
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
const dir = getDirname(path2, isWin);
|
|
249
|
+
const base = getBasename(path2, void 0, isWin);
|
|
250
|
+
const ext = getExtname(path2);
|
|
251
|
+
const name = ext ? base.slice(0, base.length - ext.length) : base;
|
|
252
|
+
return { root, dir, base, ext, name };
|
|
253
|
+
}
|
|
254
|
+
function formatPath(pathObject, isWin) {
|
|
255
|
+
const separator = getSeparator(isWin);
|
|
256
|
+
const dir = pathObject.dir || pathObject.root || "";
|
|
257
|
+
const base = pathObject.base || (pathObject.name || "") + (pathObject.ext || "");
|
|
258
|
+
if (!dir) return base;
|
|
259
|
+
if (dir === pathObject.root) return dir + base;
|
|
260
|
+
return dir + separator + base;
|
|
261
|
+
}
|
|
262
|
+
function resolve(...paths) {
|
|
263
|
+
return resolvePaths(paths, isWindows);
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
// src/database.ts
|
|
267
|
+
var import_node_path = __toESM(require("path"));
|
|
268
|
+
var import_node_fs = __toESM(require("fs"));
|
|
269
|
+
var esbuild = __toESM(require("esbuild"));
|
|
270
|
+
var Database = class {
|
|
271
|
+
constructor(config) {
|
|
272
|
+
this._config = {
|
|
273
|
+
dir: resolve(process.cwd(), "databases")
|
|
274
|
+
};
|
|
275
|
+
this._config = { ...this._config, ...config };
|
|
276
|
+
this._registerModules = config.registerModules || {};
|
|
277
|
+
this._ctx = import_node_vm.default.createContext(this._registerModules);
|
|
278
|
+
}
|
|
279
|
+
set config(config) {
|
|
280
|
+
this._config = { ...this._config, ...config };
|
|
281
|
+
}
|
|
282
|
+
register(context) {
|
|
283
|
+
this._registerModules = { ...this._registerModules, ...context };
|
|
284
|
+
this._ctx = import_node_vm.default.createContext(this._registerModules);
|
|
285
|
+
}
|
|
286
|
+
plugin(moduleName, moduleContent) {
|
|
287
|
+
this.register({ [moduleName]: moduleContent });
|
|
288
|
+
}
|
|
289
|
+
resolvePath(fileList, query) {
|
|
290
|
+
const aliases = { "@db": this._config.dir || resolve(process.cwd(), "databases") };
|
|
291
|
+
let resolvedPath = query;
|
|
292
|
+
for (const [alias, target] of Object.entries(aliases)) {
|
|
293
|
+
if (resolvedPath.startsWith(alias + "/")) {
|
|
294
|
+
resolvedPath = resolvedPath.replace(alias, target);
|
|
295
|
+
break;
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
resolvedPath = import_node_path.default.normalize(resolvedPath);
|
|
299
|
+
return fileList.find((file) => {
|
|
300
|
+
const normalizedFile = import_node_path.default.normalize(file);
|
|
301
|
+
const fileWithoutExt = normalizedFile.replace(/\.[^/.]+$/, "");
|
|
302
|
+
return normalizedFile === resolvedPath || fileWithoutExt === resolvedPath || normalizedFile === resolvedPath + ".ts" || normalizedFile === resolvedPath + ".js";
|
|
303
|
+
});
|
|
304
|
+
}
|
|
305
|
+
async moduleLinker(specifier, referencingModule) {
|
|
306
|
+
const dbFiles = import_node_fs.default.readdirSync(this._config.dir || resolve(process.cwd(), "databases")).filter((f) => f.endsWith(".ts")).map((f) => import_node_path.default.join(this._config.dir || resolve(process.cwd(), "databases"), f));
|
|
307
|
+
const dbResult = this.resolvePath(dbFiles, specifier);
|
|
308
|
+
if (dbResult) {
|
|
309
|
+
try {
|
|
310
|
+
const actualModule = await import(dbResult);
|
|
311
|
+
const exportNames = Object.keys(actualModule);
|
|
312
|
+
return new import_node_vm.default.SyntheticModule(
|
|
313
|
+
exportNames,
|
|
314
|
+
function() {
|
|
315
|
+
exportNames.forEach((key) => {
|
|
316
|
+
this.setExport(key, actualModule[key]);
|
|
317
|
+
});
|
|
318
|
+
},
|
|
319
|
+
{ identifier: specifier, context: referencingModule.context }
|
|
320
|
+
);
|
|
321
|
+
} catch (err) {
|
|
322
|
+
console.error(`Failed to load database module ${specifier}:`, err);
|
|
323
|
+
throw err;
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
throw new Error(`Module ${specifier} is not allowed or not found.`);
|
|
327
|
+
}
|
|
328
|
+
async vmRun(code, _options) {
|
|
329
|
+
const logs = [];
|
|
330
|
+
const customConsole = ["log", "error", "warn", "info", "debug", "trace"].reduce((acc, type) => {
|
|
331
|
+
acc[type] = (...args) => logs.push({ type, args });
|
|
332
|
+
return acc;
|
|
333
|
+
}, {});
|
|
334
|
+
this.register({
|
|
335
|
+
console: customConsole
|
|
336
|
+
});
|
|
337
|
+
let stringCode;
|
|
338
|
+
if (typeof code === "function") {
|
|
339
|
+
const funcStr = code.toString();
|
|
340
|
+
if (funcStr.includes("=>")) {
|
|
341
|
+
const arrowIndex = funcStr.indexOf("=>");
|
|
342
|
+
let start = arrowIndex + 2;
|
|
343
|
+
while (start < funcStr.length && funcStr[start] === " ") start++;
|
|
344
|
+
if (funcStr[start] === "{") start++;
|
|
345
|
+
let end = funcStr.lastIndexOf("}");
|
|
346
|
+
if (start < end) {
|
|
347
|
+
stringCode = funcStr.substring(start, end);
|
|
348
|
+
} else {
|
|
349
|
+
stringCode = funcStr.substring(start);
|
|
350
|
+
}
|
|
351
|
+
} else if (funcStr.includes("function")) {
|
|
352
|
+
const funcIndex = funcStr.indexOf("function");
|
|
353
|
+
let start = funcIndex + 8;
|
|
354
|
+
while (start < funcStr.length && funcStr[start] === " ") start++;
|
|
355
|
+
if (funcStr[start] === "(") start++;
|
|
356
|
+
if (start < funcStr.length && funcStr[start] !== "(") {
|
|
357
|
+
while (start < funcStr.length && funcStr[start] !== " " && funcStr[start] !== "(") start++;
|
|
358
|
+
}
|
|
359
|
+
if (funcStr[start] === "(") start++;
|
|
360
|
+
while (start < funcStr.length && funcStr[start] === " ") start++;
|
|
361
|
+
if (funcStr[start] === "{") start++;
|
|
362
|
+
const end = funcStr.lastIndexOf("}");
|
|
363
|
+
if (start < end) {
|
|
364
|
+
stringCode = funcStr.substring(start, end);
|
|
365
|
+
} else {
|
|
366
|
+
stringCode = funcStr.substring(start);
|
|
367
|
+
}
|
|
368
|
+
} else {
|
|
369
|
+
stringCode = funcStr;
|
|
370
|
+
}
|
|
371
|
+
stringCode = stringCode.trim();
|
|
372
|
+
let importPos = 0;
|
|
373
|
+
while ((importPos = stringCode.indexOf("import(", importPos)) !== -1) {
|
|
374
|
+
const fromPos = stringCode.indexOf(".from(", importPos);
|
|
375
|
+
if (fromPos === -1) break;
|
|
376
|
+
const quoteStart = stringCode.indexOf("(", fromPos + 7) + 1;
|
|
377
|
+
if (quoteStart === -1) break;
|
|
378
|
+
const quoteChar = stringCode[quoteStart];
|
|
379
|
+
if (quoteChar !== '"' && quoteChar !== "'") break;
|
|
380
|
+
const quoteEnd = stringCode.indexOf(quoteChar, quoteStart + 1);
|
|
381
|
+
if (quoteEnd === -1) break;
|
|
382
|
+
const modulePath = stringCode.substring(quoteStart + 1, quoteEnd);
|
|
383
|
+
const importArgEnd = fromPos - 1;
|
|
384
|
+
const importArgStart = importPos + 7;
|
|
385
|
+
const trimmed = stringCode.substring(importArgStart, importArgEnd).trim();
|
|
386
|
+
let replacement;
|
|
387
|
+
if (trimmed.startsWith("{") && trimmed.endsWith("}")) {
|
|
388
|
+
const inner = trimmed.slice(1, -1).trim();
|
|
389
|
+
replacement = `import { ${inner} } from "${modulePath}"`;
|
|
390
|
+
} else {
|
|
391
|
+
replacement = `import ${trimmed} from "${modulePath}"`;
|
|
392
|
+
}
|
|
393
|
+
const before = stringCode.substring(0, importPos);
|
|
394
|
+
const after = stringCode.substring(quoteEnd + 2);
|
|
395
|
+
stringCode = before + replacement + after;
|
|
396
|
+
}
|
|
397
|
+
const lines = stringCode.split("\n");
|
|
398
|
+
const trimmedLines = lines.map((line) => line.trim());
|
|
399
|
+
stringCode = trimmedLines.join("\n").trim();
|
|
400
|
+
} else {
|
|
401
|
+
stringCode = code;
|
|
402
|
+
}
|
|
403
|
+
const result = await esbuild.build({
|
|
404
|
+
stdin: {
|
|
405
|
+
contents: stringCode,
|
|
406
|
+
loader: this._config.language || "ts"
|
|
407
|
+
},
|
|
408
|
+
format: "esm",
|
|
409
|
+
target: "es2020",
|
|
410
|
+
write: false,
|
|
411
|
+
bundle: false,
|
|
412
|
+
sourcemap: false
|
|
413
|
+
});
|
|
414
|
+
const js = result.outputFiles[0].text;
|
|
415
|
+
const mod = new import_node_vm.default.SourceTextModule(js, { context: this._ctx, identifier: import_node_path.default.join(this._config.dir || resolve(process.cwd(), "databases"), "virtual-entry.js") });
|
|
416
|
+
await mod.link(this.moduleLinker.bind(this));
|
|
417
|
+
await mod.evaluate();
|
|
418
|
+
return {
|
|
419
|
+
namespace: mod.namespace,
|
|
420
|
+
logs
|
|
421
|
+
};
|
|
422
|
+
}
|
|
423
|
+
/**
|
|
424
|
+
* Execute database code and return results
|
|
425
|
+
*/
|
|
426
|
+
async execute(code, options) {
|
|
427
|
+
return await this.vmRun(code, options);
|
|
428
|
+
}
|
|
429
|
+
};
|
|
430
|
+
function database() {
|
|
431
|
+
return new Database({
|
|
432
|
+
dir: resolve(process.cwd(), "databases")
|
|
433
|
+
});
|
|
434
|
+
}
|
|
435
|
+
var database_default = database;
|
|
436
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
437
|
+
0 && (module.exports = {
|
|
438
|
+
Database,
|
|
439
|
+
database
|
|
440
|
+
});
|