vaderjs 1.4.1-ui7iuy47 → 1.4.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/.editorconfig +11 -0
- package/.vscode/c_cpp_properties.json +21 -0
- package/.vscode/settings.json +12 -0
- package/README.md +36 -199
- package/binaries/Kalix/index.js +665 -0
- package/binaries/compiler/main.js +461 -0
- package/binaries/vader.js +74 -0
- package/binaries/watcher/hmr.js +36 -0
- package/client/index.d.ts +226 -0
- package/client/runtime/index.js +417 -0
- package/client/runtime/router.js +235 -0
- package/config/index.ts +68 -0
- package/index.ts +344 -0
- package/package.json +14 -25
- package/plugins/cloudflare/functions/index.js +98 -0
- package/plugins/cloudflare/toCopy/@server/Kalix/index.js +625 -0
- package/plugins/cloudflare/toCopy/@server/cloudflare_ssr/index.js +85 -0
- package/plugins/cloudflare/toCopy/node_modules/vaderjs/server/index.js +99 -0
- package/plugins/cloudflare/toCopy/src/client.js +432 -0
- package/plugins/cloudflare/toCopy/src/router.js +235 -0
- package/plugins/ssg/index.js +124 -0
- package/plugins/vercel/functions/index.ts +8 -0
- package/router/index.ts +181 -0
- package/server/index.js +129 -0
- package/vader.js +59 -112
- package/@integrations/ssg.js +0 -189
- package/LICENSE +0 -21
- package/binaries/IPC/index.js +0 -277
- package/binaries/main.js +0 -1464
- package/binaries/readme.md +0 -4
- package/binaries/watcher.js +0 -74
- package/binaries/win32/check.ps1 +0 -7
- package/client/index.js +0 -426
- package/config/index.js +0 -36
- package/logo.png +0 -0
- package/runtime/router.js +0 -1
- package/runtime/vader.js +0 -1
package/vader.js
CHANGED
|
@@ -3,7 +3,7 @@ import { exec } from "child_process";
|
|
|
3
3
|
import fs from "fs";
|
|
4
4
|
globalThis.currentCommand = null;
|
|
5
5
|
globalThis.isRunning = false;
|
|
6
|
-
let vaderisInstalled = process.cwd() + "/node_modules/vaderjs/binaries/
|
|
6
|
+
let vaderisInstalled = process.cwd() + "/node_modules/vaderjs/binaries/vader.js";
|
|
7
7
|
if (!fs.existsSync(process.cwd() + "/_dev")) {
|
|
8
8
|
fs.mkdirSync(process.cwd() + "/_dev");
|
|
9
9
|
!fs.existsSync(process.cwd() + "/_dev/readme.md") && fs.writeFileSync(process.cwd() + "/_dev/readme.md", `This folder is used by vader.js to store important files, deletables include: Bun, Chrome - These should only be uninstalled if you need to reinstall them.`);
|
|
@@ -36,6 +36,15 @@ function checkIFBundleIsInstalled() {
|
|
|
36
36
|
}
|
|
37
37
|
|
|
38
38
|
function run() {
|
|
39
|
+
if(!fs.existsSync(porcess.cwd() + "/pages")) {
|
|
40
|
+
fs.mkdirSync(process.cwd() + "/pages");
|
|
41
|
+
}
|
|
42
|
+
if(!fs.existsSync(porcess.cwd() + "/public")) {
|
|
43
|
+
fs.mkdirSync(process.cwd() + "/public");
|
|
44
|
+
}
|
|
45
|
+
if(!fs.existsSync(process.cwd() + "/src")) {
|
|
46
|
+
fs.mkdirSync(process.cwd() + "/src");
|
|
47
|
+
}
|
|
39
48
|
if (!fs.existsSync(process.cwd() + "/package.json")) {
|
|
40
49
|
fs.writeFileSync(process.cwd() + "/package.json", JSON.stringify({ name: "my_app", version: "1.0.0" }, null, 2));
|
|
41
50
|
return;
|
|
@@ -86,59 +95,7 @@ function run() {
|
|
|
86
95
|
|
|
87
96
|
`);
|
|
88
97
|
}
|
|
89
|
-
|
|
90
|
-
function checkIFChromeIumIsInstalled() {
|
|
91
|
-
let platform = process.platform;
|
|
92
|
-
let findChrome = {
|
|
93
|
-
windows: `powershell -c "${process.cwd() + "\\node_modules\\vaderjs\\binaries\\win32\\check.ps1"}"`,
|
|
94
|
-
others: "/usr/bin/chromium-browser --version",
|
|
95
|
-
};
|
|
96
|
-
let installCommands = {
|
|
97
|
-
windows: `winget install Google.Chrome`,
|
|
98
|
-
others: "sudo apt-get install chromium-browser -y",
|
|
99
|
-
};
|
|
100
|
-
findChrome.windows = findChrome.windows.replace(/\n/g, " ");
|
|
101
|
-
let commandToRun = platform === "win32" ? findChrome.windows : findChrome.others;
|
|
102
|
-
|
|
103
|
-
return new Promise((resolve, reject) => {
|
|
104
|
-
if (fs.existsSync(process.cwd() + "/_dev/chrome")) {
|
|
105
|
-
resolve(true);
|
|
106
|
-
} else {
|
|
107
|
-
exec(commandToRun, (err, stdout, stderr) => {
|
|
108
|
-
let hasError = false;
|
|
109
|
-
if (err) {
|
|
110
|
-
console.log(err);
|
|
111
|
-
hasError = true;
|
|
112
|
-
console.log(`Attempting to install DEPENDENCY: ${platform === "win32" ? "Google Chrome" : "Chromium"}`);
|
|
113
|
-
}
|
|
114
|
-
if (stdout && !hasError) {
|
|
115
|
-
resolve(`${platform === "win32" ? "Google Chrome" : "Chromium"} is installed: ${stdout}`);
|
|
116
|
-
fs.writeFileSync(process.cwd() + "/_dev/chrome", `Installed: ${stdout}`);
|
|
117
|
-
}
|
|
118
|
-
if (stderr) {
|
|
119
|
-
console.log(stderr);
|
|
120
|
-
console.log(`Installing DEPENDENCY: ${platform === "win32" ? "Google Chrome" : "Chromium"}`);
|
|
121
|
-
let installCommand = platform === "win32" ? installCommands.windows : installCommands.others;
|
|
122
|
-
return new Promise((resolve, reject) => {
|
|
123
|
-
exec(installCommand, (err, stdout, stderr) => {
|
|
124
|
-
if (err) {
|
|
125
|
-
reject(err);
|
|
126
|
-
}
|
|
127
|
-
if (stdout) {
|
|
128
|
-
resolve(`${platform === "win32" ? "Google Chrome" : "Chromium"} installed successfully: ${stdout}`);
|
|
129
|
-
fs.writeFileSync(process.cwd() + "/_dev/chrome", `Installed: ${stdout}`);
|
|
130
|
-
run();
|
|
131
|
-
}
|
|
132
|
-
if (stderr) {
|
|
133
|
-
reject(`${platform === "win32" ? "Google Chrome" : "Chromium"} failed to install: ${stderr}`);
|
|
134
|
-
}
|
|
135
|
-
});
|
|
136
|
-
});
|
|
137
|
-
}
|
|
138
|
-
});
|
|
139
|
-
}
|
|
140
|
-
});
|
|
141
|
-
}
|
|
98
|
+
|
|
142
99
|
let Commands = {
|
|
143
100
|
dev: `bun run dev`,
|
|
144
101
|
build: `bun run build`,
|
|
@@ -159,72 +116,62 @@ switch (true) {
|
|
|
159
116
|
currentCommand = null;
|
|
160
117
|
break;
|
|
161
118
|
}
|
|
162
|
-
|
|
163
|
-
|
|
119
|
+
checkIFBundleIsInstalled()
|
|
120
|
+
.then((stdout) => {
|
|
121
|
+
if (stdout && !isRunning) {
|
|
122
|
+
if (!fs.existsSync(process.cwd() + "/_dev/bun")) {
|
|
123
|
+
fs.writeFileSync(process.cwd() + "/_dev/bun", `Installed: ${stdout}`);
|
|
124
|
+
}
|
|
125
|
+
run();
|
|
126
|
+
globalThis.isRunning = true;
|
|
127
|
+
}
|
|
128
|
+
})
|
|
129
|
+
.catch(async (err) => {
|
|
130
|
+
console.log("Bun.js is not installed. Installing....");
|
|
131
|
+
let installScipt = {
|
|
132
|
+
windows: 'powershell -c "irm bun.sh/install.ps1|iex',
|
|
133
|
+
others: "curl -fsSL https://bun.sh/install.sh | bash",
|
|
134
|
+
};
|
|
135
|
+
let scriptotRun = process.platform === "win32" ? installScipt.windows : installScipt.others;
|
|
136
|
+
exec(scriptotRun, async (err, stdout, stderr) => {
|
|
137
|
+
if (err) {
|
|
138
|
+
console.log("Error installing bun.js, may want to install manually");
|
|
139
|
+
process.exit(1);
|
|
140
|
+
}
|
|
164
141
|
if (stdout) {
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
fs.writeFileSync(process.cwd() + "/_dev/bun", `Installed: ${stdout}`);
|
|
170
|
-
}
|
|
171
|
-
run();
|
|
172
|
-
globalThis.isRunning = true;
|
|
173
|
-
}
|
|
174
|
-
})
|
|
175
|
-
.catch(async (err) => {
|
|
176
|
-
console.log("Bun.js is not installed. Installing....");
|
|
177
|
-
let installScipt = {
|
|
178
|
-
windows: 'powershell -c "irm bun.sh/install.ps1|iex',
|
|
179
|
-
others: "curl -fsSL https://bun.sh/install.sh | bash",
|
|
180
|
-
};
|
|
181
|
-
let scriptotRun = process.platform === "win32" ? installScipt.windows : installScipt.others;
|
|
182
|
-
exec(scriptotRun, async (err, stdout, stderr) => {
|
|
142
|
+
if (!process.platform === "win32") {
|
|
143
|
+
await new Promise((resolve, reject) => {
|
|
144
|
+
console.log(`Adding bun.js to path...`);
|
|
145
|
+
exec("source ~/.bashrc", (err, stdout, stderr) => {
|
|
183
146
|
if (err) {
|
|
184
147
|
console.log("Error installing bun.js");
|
|
185
|
-
|
|
148
|
+
return;
|
|
186
149
|
}
|
|
187
150
|
if (stdout) {
|
|
188
|
-
if (!process.platform === "win32") {
|
|
189
|
-
await new Promise((resolve, reject) => {
|
|
190
|
-
console.log(`Adding bun.js to path...`);
|
|
191
|
-
let shell = null;
|
|
192
|
-
exec("source ~/.bashrc", (err, stdout, stderr) => {
|
|
193
|
-
if (err) {
|
|
194
|
-
console.log("Error installing bun.js");
|
|
195
|
-
return;
|
|
196
|
-
}
|
|
197
|
-
if (stdout) {
|
|
198
|
-
run();
|
|
199
|
-
}
|
|
200
|
-
if (stderr) {
|
|
201
|
-
console.log("Error installing bun.js");
|
|
202
|
-
process.exit(1);
|
|
203
|
-
}
|
|
204
|
-
});
|
|
205
|
-
});
|
|
206
|
-
exec("chmod +x bun.sh/install.sh", (err, stdout, stderr) => {
|
|
207
|
-
if (err) {
|
|
208
|
-
console.log("Error installing bun.js");
|
|
209
|
-
return;
|
|
210
|
-
}
|
|
211
|
-
if (stdout) {
|
|
212
|
-
console.log("Bun.js installed successfully");
|
|
213
|
-
run();
|
|
214
|
-
}
|
|
215
|
-
if (stderr) {
|
|
216
|
-
console.log("Error installing bun.js");
|
|
217
|
-
process.exit(1);
|
|
218
|
-
}
|
|
219
|
-
});
|
|
220
|
-
}
|
|
221
151
|
run();
|
|
222
152
|
}
|
|
153
|
+
if (stderr) {
|
|
154
|
+
console.log("Error installing bun.js");
|
|
155
|
+
process.exit(1);
|
|
156
|
+
}
|
|
223
157
|
});
|
|
224
158
|
});
|
|
159
|
+
exec("chmod +x bun.sh/install.sh", (err, stdout, stderr) => {
|
|
160
|
+
if (err) {
|
|
161
|
+
console.log("Error installing bun.js");
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
if (stdout) {
|
|
165
|
+
console.log("Bun.js installed successfully");
|
|
166
|
+
run();
|
|
167
|
+
}
|
|
168
|
+
if (stderr) {
|
|
169
|
+
console.log("Error installing bun.js");
|
|
170
|
+
process.exit(1);
|
|
171
|
+
}
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
run();
|
|
225
175
|
}
|
|
226
|
-
})
|
|
227
|
-
.catch(async (err) => {
|
|
228
|
-
console.error(err);
|
|
229
|
-
process.exit(1);
|
|
230
176
|
});
|
|
177
|
+
});
|
package/@integrations/ssg.js
DELETED
|
@@ -1,189 +0,0 @@
|
|
|
1
|
-
import fs from 'fs';
|
|
2
|
-
import playwright from 'playwright';
|
|
3
|
-
globalThis.routeDocuments = {}
|
|
4
|
-
let context = process.env.isVader === 'true' ? true : false
|
|
5
|
-
/**
|
|
6
|
-
*
|
|
7
|
-
* @param {Object} config
|
|
8
|
-
* @param {string} config.pages - The directory where the pages are located
|
|
9
|
-
* @param {string} config.output - The directory where the pages are to be outputted
|
|
10
|
-
*/
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
import http from 'http';
|
|
14
|
-
let hasGenerated = []
|
|
15
|
-
let server = http.createServer((req, res) => {
|
|
16
|
-
if (!req.url.includes(".")) {
|
|
17
|
-
let params = new URLSearchParams(req.url.split("?")[1]);
|
|
18
|
-
let folder = params.get("folder");
|
|
19
|
-
|
|
20
|
-
let content = globalThis.routeDocuments[folder]
|
|
21
|
-
res.writeHead(200, { "Content-Type": "text/html" });
|
|
22
|
-
|
|
23
|
-
res.end(content);
|
|
24
|
-
} else {
|
|
25
|
-
if (req.url.includes('./')) {
|
|
26
|
-
req.url = req.url.replace('./', '/')
|
|
27
|
-
}
|
|
28
|
-
const filePath = process.cwd() + "/dist/" + req.url;
|
|
29
|
-
|
|
30
|
-
fs.readFile(filePath, (err, data) => {
|
|
31
|
-
if (err) {
|
|
32
|
-
res.writeHead(404, {
|
|
33
|
-
"Content-Type": filePath.includes("js")
|
|
34
|
-
? "text/javascript"
|
|
35
|
-
: "text/html",
|
|
36
|
-
});
|
|
37
|
-
res.end("File not found");
|
|
38
|
-
} else {
|
|
39
|
-
res.writeHead(200, {
|
|
40
|
-
"Content-Type": filePath.includes("js")
|
|
41
|
-
? "text/javascript"
|
|
42
|
-
: "text/html",
|
|
43
|
-
});
|
|
44
|
-
res.end(data);
|
|
45
|
-
}
|
|
46
|
-
});
|
|
47
|
-
}
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
const generateHTML = (routeData) => {
|
|
51
|
-
|
|
52
|
-
let { path, file, isParam, params, query, pathname } = routeData;
|
|
53
|
-
let baseFolder = file.split('/').filter(f => f !== 'dist').join('/')
|
|
54
|
-
if (file.includes('./dist')) {
|
|
55
|
-
baseFolder = file.split('./dist')[1]
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
let isCatchAll = false;
|
|
59
|
-
|
|
60
|
-
let html = `
|
|
61
|
-
<!DOCTYPE html>
|
|
62
|
-
<html>
|
|
63
|
-
<head>
|
|
64
|
-
|
|
65
|
-
<title>Vaderjs v1.3.3</title>
|
|
66
|
-
<meta name="description" content="Vader.js is a modern web framework for building web applications.">
|
|
67
|
-
<link rel="shortcut icon" href="https://raw.githubusercontent.com/Postr-Inc/Vader.js/main/logo.png" type="image/x-icon">
|
|
68
|
-
</head>
|
|
69
|
-
<body>
|
|
70
|
-
<script id="v">
|
|
71
|
-
window.$SERVER = true
|
|
72
|
-
</script>
|
|
73
|
-
<div id="app"></div>
|
|
74
|
-
<script id="router" type="module">
|
|
75
|
-
import Router from '/router.js'
|
|
76
|
-
|
|
77
|
-
const rt = new Router
|
|
78
|
-
${Array.isArray(params)
|
|
79
|
-
? params
|
|
80
|
-
.map((param) => {
|
|
81
|
-
if (!param.jsFile) return "";
|
|
82
|
-
let ranName = Math.random()
|
|
83
|
-
.toString(36)
|
|
84
|
-
.substring(7)
|
|
85
|
-
.replace(".", "");
|
|
86
|
-
let pd = Object.keys(param.paramData)[0];
|
|
87
|
-
let baseFolder = path
|
|
88
|
-
.split("/")
|
|
89
|
-
.filter((f) => f !== "pages")
|
|
90
|
-
.join("/");
|
|
91
|
-
|
|
92
|
-
if(pd === '$catchall'){
|
|
93
|
-
isCatchAll = true;
|
|
94
|
-
console.log('catchall')
|
|
95
|
-
return `
|
|
96
|
-
import c${ranName} from '${param.jsFile}'
|
|
97
|
-
rt.get($SERVER ? '/' : '/${baseFolder}/*', c${ranName})
|
|
98
|
-
`
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
return `
|
|
102
|
-
|
|
103
|
-
import c${ranName} from '${param.jsFile}'
|
|
104
|
-
rt.get('/${baseFolder}/:${pd}', c${ranName})
|
|
105
|
-
`;
|
|
106
|
-
})
|
|
107
|
-
.join("")
|
|
108
|
-
: ""
|
|
109
|
-
}
|
|
110
|
-
let c = await import('${baseFolder}')
|
|
111
|
-
if(Object.keys(c).includes('$prerender') && c.$prerender === false){
|
|
112
|
-
document.head.setAttribute('prerender', 'false')
|
|
113
|
-
}
|
|
114
|
-
${
|
|
115
|
-
!isCatchAll ? `rt.get($SERVER? '/' : '${pathname}', c)` : ``
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
rt.listen()
|
|
119
|
-
</script>
|
|
120
|
-
</body>
|
|
121
|
-
</html>`
|
|
122
|
-
return html;
|
|
123
|
-
|
|
124
|
-
}
|
|
125
|
-
/**
|
|
126
|
-
* @integration Server Side Generator
|
|
127
|
-
* @description Generate a static webapp from your Vader.js application
|
|
128
|
-
* @returns
|
|
129
|
-
*/
|
|
130
|
-
const ssg = async (config) => {
|
|
131
|
-
return new Promise(async (resolve, reject) => {
|
|
132
|
-
let { pages, output } = config;
|
|
133
|
-
let routes = JSON.parse(fs.readFileSync(`${process.cwd()}/_dev/meta/routes.json`).toString())
|
|
134
|
-
for (var i in routes) {
|
|
135
|
-
let route = routes[i];
|
|
136
|
-
|
|
137
|
-
if(route.path.includes('[')){
|
|
138
|
-
continue
|
|
139
|
-
}
|
|
140
|
-
let html = generateHTML(route);
|
|
141
|
-
globalThis.routeDocuments[routes[i].path] = html;
|
|
142
|
-
let browser = await playwright.chromium.launch({
|
|
143
|
-
headless: true,
|
|
144
|
-
executablePath: process.platform === 'win32' ? '' : '/usr/bin/chromium-browser'
|
|
145
|
-
});
|
|
146
|
-
globalThis.browser = browser;
|
|
147
|
-
let page = await browser.newPage();
|
|
148
|
-
await page.goto(`http://localhost:8700?folder=${routes[i].path}`);
|
|
149
|
-
await page.evaluate(() => {
|
|
150
|
-
document.querySelector('script#v').innerHTML = `window.$SERVER = false`
|
|
151
|
-
if(document.head.getAttribute('prerender') === 'false'){
|
|
152
|
-
document.querySelector("#app").innerHTML = "";
|
|
153
|
-
}
|
|
154
|
-
});
|
|
155
|
-
let content = await page.content();
|
|
156
|
-
console.log(`Writing ${routes[i].path} to ${output}/${routes[i].path}/index.html`)
|
|
157
|
-
if (output.includes('./dist')) {
|
|
158
|
-
output = output.split('./dist')[1]
|
|
159
|
-
}
|
|
160
|
-
let path = '/dist/' + output + routes[i].path + '/index.html';
|
|
161
|
-
fs.writeFileSync(process.cwd() + path, content)
|
|
162
|
-
hasGenerated.push(routes[i].path)
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
if (hasGenerated.length === routes.length) {
|
|
166
|
-
console.log(`Static site generation complete`)
|
|
167
|
-
browser.close()
|
|
168
|
-
server.close()
|
|
169
|
-
resolve()
|
|
170
|
-
process.exit()
|
|
171
|
-
}
|
|
172
|
-
})
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
if(context){
|
|
176
|
-
server.listen(8700);
|
|
177
|
-
await ssg({ pages: './pages', output: './dist' })
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
export default {
|
|
182
|
-
name: 'Vaderjs Static Site Generator',
|
|
183
|
-
version: '1.0.0',
|
|
184
|
-
useRuntime: 'node',
|
|
185
|
-
entryPoint: process.cwd() + '/node_modules/vaderjs/@integrations/ssg.js',
|
|
186
|
-
on: ['build', 'dev:change'],
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
|
package/LICENSE
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2024 Pascal
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
package/binaries/IPC/index.js
DELETED
|
@@ -1,277 +0,0 @@
|
|
|
1
|
-
import { WebSocketServer } from "ws";
|
|
2
|
-
import WebSocket from "ws";
|
|
3
|
-
/**
|
|
4
|
-
* @class IPC
|
|
5
|
-
* @description Inter-Process Communication - allows you to communicate with vaderjs using Bun.js
|
|
6
|
-
*/
|
|
7
|
-
class IPC {
|
|
8
|
-
constructor(config = { port: 3434 }) {
|
|
9
|
-
this.server = {
|
|
10
|
-
port: config.port,
|
|
11
|
-
/**
|
|
12
|
-
* @property {IPC} instance - The instance of the IPC server
|
|
13
|
-
*/
|
|
14
|
-
instance: null,
|
|
15
|
-
clients: [],
|
|
16
|
-
/**
|
|
17
|
-
*
|
|
18
|
-
* @private
|
|
19
|
-
*/
|
|
20
|
-
sendMsg: (msg) => {
|
|
21
|
-
this.server.clients.forEach((client) => {
|
|
22
|
-
client.send(JSON.stringify({ IPC_TYPE: this.type, msg }));
|
|
23
|
-
});
|
|
24
|
-
},
|
|
25
|
-
};
|
|
26
|
-
/**
|
|
27
|
-
* @enum {Object} typeEnums - The type of process to spawn
|
|
28
|
-
*/
|
|
29
|
-
this.typeEnums = {
|
|
30
|
-
/**
|
|
31
|
-
* @enum {Number} WATCHER - The file watching process
|
|
32
|
-
*/
|
|
33
|
-
WATCHER: 1,
|
|
34
|
-
/**
|
|
35
|
-
* @enum {Number} PAGEGENERATOR - The page generator process
|
|
36
|
-
*/
|
|
37
|
-
PAGEGENERATOR: 2,
|
|
38
|
-
/**
|
|
39
|
-
* @enum {Number} DFT - The default process
|
|
40
|
-
*/
|
|
41
|
-
DFT: 3,
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
this.listeners = [];
|
|
45
|
-
this.type = this.typeEnums.DFT;
|
|
46
|
-
}
|
|
47
|
-
/**
|
|
48
|
-
* @property {Boolean} NOLISTEN - Whether to listen for incoming messages or not
|
|
49
|
-
*/
|
|
50
|
-
NOLISTEN = false;
|
|
51
|
-
/**
|
|
52
|
-
* @Object Console
|
|
53
|
-
* @description The console object to use for logging - Bun.spawn has 0 way of reading logs from the spawned process so this is a workaround
|
|
54
|
-
*/
|
|
55
|
-
Console = {
|
|
56
|
-
log: (msg, IPC_TYPE) => {
|
|
57
|
-
this.server.clients.forEach((client) => {
|
|
58
|
-
client.send(JSON.stringify({ IPC_TYPE: PROTOCOL, CONSOLE: true, LOG: true, msg }));
|
|
59
|
-
});
|
|
60
|
-
},
|
|
61
|
-
error: (msg, errorLevel, IPC_TYPE) => {
|
|
62
|
-
this.server.clients.forEach((client) => {
|
|
63
|
-
client.send(JSON.stringify({ IPC_TYPE: PROTOCOL, CONSOLE: true, ERROR: true, errorLevel, msg }));
|
|
64
|
-
});
|
|
65
|
-
},
|
|
66
|
-
warn: (msg, IPC_TYPE) => {
|
|
67
|
-
this.server.clients.forEach((client) => {
|
|
68
|
-
client.send(JSON.stringify({ IPC_TYPE: PROTOCOL, CONSOLE: true, WARN: true, msg }));
|
|
69
|
-
});
|
|
70
|
-
},
|
|
71
|
-
info: (msg, IPC_TYPE) => {
|
|
72
|
-
this.server.clients.forEach((client) => {
|
|
73
|
-
client.send(JSON.stringify({ IPC_TYPE: PROTOCOL, CONSOLE: true, INFO: true, msg }));
|
|
74
|
-
});
|
|
75
|
-
},
|
|
76
|
-
debug: (msg, IPC_TYPE) => {
|
|
77
|
-
this.server.clients.forEach((client) => {
|
|
78
|
-
client.send(JSON.stringify({ IPC_TYPE: PROTOCOl, CONSOLE: true, DEBUG: true, msg }));
|
|
79
|
-
});
|
|
80
|
-
},
|
|
81
|
-
};
|
|
82
|
-
|
|
83
|
-
spawnServer(type = this.typeEnums.DFT) {
|
|
84
|
-
this.isntSpawned = false;
|
|
85
|
-
if (!this.server.instance) {
|
|
86
|
-
// if used in achildProcess reconnect to the main running server
|
|
87
|
-
this.server.instance = new WebSocketServer({ port: this.server.port });
|
|
88
|
-
} else {
|
|
89
|
-
// switch type
|
|
90
|
-
this.server.instance.clients.forEach((client) => {
|
|
91
|
-
client.send(JSON.stringify({ PROTO_CHANGE: true, IPC_TYPE: type }));
|
|
92
|
-
});
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
this.server.instance.on("connection", (ws) => {
|
|
96
|
-
this.server.clients.push(ws);
|
|
97
|
-
|
|
98
|
-
this.server.instance.clients.forEach((client) => {
|
|
99
|
-
client.send(JSON.stringify({ ESTABLISHED: true, IPC_TYPE: type }));
|
|
100
|
-
});
|
|
101
|
-
ws.on("message", (msg) => {
|
|
102
|
-
msg = JSON.parse(msg);
|
|
103
|
-
if (msg.CLIENTINIT) {
|
|
104
|
-
let port = msg.port;
|
|
105
|
-
ws.send(JSON.stringify({ ESTABLISHED: true, IPC_TYPE: type }));
|
|
106
|
-
return;
|
|
107
|
-
} else if (msg.SERVERINIT) {
|
|
108
|
-
ws.send(JSON.stringify({ ESTABLISHED: true, IPC_TYPE: type }));
|
|
109
|
-
return;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
this.server.clients.forEach((client) => {
|
|
113
|
-
client.send(JSON.stringify(msg));
|
|
114
|
-
});
|
|
115
|
-
});
|
|
116
|
-
});
|
|
117
|
-
|
|
118
|
-
this.server.instance.on("close", () => {
|
|
119
|
-
this.server.clients = [];
|
|
120
|
-
});
|
|
121
|
-
|
|
122
|
-
// spawn if nolisten is false
|
|
123
|
-
this.isntSpawned = true;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
/**
|
|
127
|
-
* @method newServer
|
|
128
|
-
* @description Creates a new IPC server instance
|
|
129
|
-
* @param {IPC.typeEnums} proto
|
|
130
|
-
* @param {Object} config
|
|
131
|
-
* @returns
|
|
132
|
-
*/
|
|
133
|
-
async newServer(proto = 0, config = { port: 3434 }) {
|
|
134
|
-
const client = new WebSocket(`ws://localhost:${config.port}`);
|
|
135
|
-
|
|
136
|
-
client.on("open", () => {
|
|
137
|
-
client.send(JSON.stringify({ SERVERINIT: true, IPC_TYPE: proto }));
|
|
138
|
-
});
|
|
139
|
-
client.on("message", (msg) => {
|
|
140
|
-
if (msg.ESTABLISHED && msg.IPC_TYPE === proto) {
|
|
141
|
-
config?.onMessage(msg);
|
|
142
|
-
}
|
|
143
|
-
});
|
|
144
|
-
let port = config.port;
|
|
145
|
-
|
|
146
|
-
const sendMsg = (msg) => {
|
|
147
|
-
if (!client.readyState) {
|
|
148
|
-
let i = setInterval(() => {
|
|
149
|
-
if (client.readyState) {
|
|
150
|
-
client.send(JSON.stringify({ IPC_TYPE: proto, msg, port, isServer: true }));
|
|
151
|
-
clearInterval(i);
|
|
152
|
-
}
|
|
153
|
-
}, 1000);
|
|
154
|
-
return;
|
|
155
|
-
}
|
|
156
|
-
client.send(JSON.stringify({ IPC_TYPE: proto, msg, port, isServer: true }));
|
|
157
|
-
};
|
|
158
|
-
|
|
159
|
-
sendMsg({ ESTABLISHED: true, IPC_TYPE: proto, port });
|
|
160
|
-
return {
|
|
161
|
-
sendMsg: (msg) => {
|
|
162
|
-
sendMsg(msg);
|
|
163
|
-
},
|
|
164
|
-
/**
|
|
165
|
-
* @method Console
|
|
166
|
-
* @description The console object to use for logging - Bun.spawn has 0 way of reading logs from the spawned process so this is a workaround
|
|
167
|
-
*/
|
|
168
|
-
Console: {
|
|
169
|
-
log: (msg) => {
|
|
170
|
-
sendMsg({ CONSOLE: true, data: msg });
|
|
171
|
-
},
|
|
172
|
-
error: (msg, errorLevel) => {
|
|
173
|
-
sendMsg({ CONSOLE: true, ERROR: true, errorLevel, msg });
|
|
174
|
-
},
|
|
175
|
-
warn: (msg) => {
|
|
176
|
-
sendMsg({ CONSOLE: true, WARN: true, msg });
|
|
177
|
-
},
|
|
178
|
-
info: (msg) => {
|
|
179
|
-
sendMsg({ CONSOLE: true, INFO: true, msg });
|
|
180
|
-
},
|
|
181
|
-
},
|
|
182
|
-
listen: (callback) => {
|
|
183
|
-
client.on("message", (msg) => {
|
|
184
|
-
callback(msg);
|
|
185
|
-
});
|
|
186
|
-
},
|
|
187
|
-
onError: (callback) => {
|
|
188
|
-
client.on("error", (err) => {
|
|
189
|
-
callback(err);
|
|
190
|
-
});
|
|
191
|
-
},
|
|
192
|
-
};
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
listen(callback) {
|
|
196
|
-
this.server.instance.on("message", (msg) => {
|
|
197
|
-
callback(msg);
|
|
198
|
-
});
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
client(config = { use: this.typeEnums.DFT, port: 0, callback: () => {} }) {
|
|
202
|
-
if (!this.server.instance) {
|
|
203
|
-
this.spawnServer();
|
|
204
|
-
}
|
|
205
|
-
const ws = new WebSocket(`ws://localhost:${config.port}`);
|
|
206
|
-
ws.on("open", () => {
|
|
207
|
-
console.log(config);
|
|
208
|
-
});
|
|
209
|
-
ws.send(JSON.stringify({ CLIENTINIT: true, IPC_TYPE: config.use, port: config.port }));
|
|
210
|
-
|
|
211
|
-
function listen(callback) {
|
|
212
|
-
ws.on("message", (msg) => {
|
|
213
|
-
callback(msg);
|
|
214
|
-
});
|
|
215
|
-
}
|
|
216
|
-
return {
|
|
217
|
-
Console: {
|
|
218
|
-
read: (callback) => {
|
|
219
|
-
listen((msg) => {
|
|
220
|
-
msg = JSON.parse(msg.toString());
|
|
221
|
-
if (msg.isServer && msg.IPC_TYPE === config.use && msg.port === config.port && msg?.msg?.CONSOLE) {
|
|
222
|
-
callback(msg);
|
|
223
|
-
}
|
|
224
|
-
});
|
|
225
|
-
},
|
|
226
|
-
write: (msg) => {
|
|
227
|
-
ws.send(JSON.stringify({ CONSOLE_LOG: true, msg }));
|
|
228
|
-
},
|
|
229
|
-
},
|
|
230
|
-
sendMsg: (msg) => {
|
|
231
|
-
ws.send(JSON.stringify({ IPC_TYPE: config.use, msg }));
|
|
232
|
-
},
|
|
233
|
-
listen: (callback) => {
|
|
234
|
-
ws.on("message", (msg) => {
|
|
235
|
-
callback(msg);
|
|
236
|
-
});
|
|
237
|
-
},
|
|
238
|
-
close: () => {
|
|
239
|
-
ws.close();
|
|
240
|
-
},
|
|
241
|
-
onError: (callback) => {
|
|
242
|
-
ws.on("error", (err) => {
|
|
243
|
-
callback(err);
|
|
244
|
-
});
|
|
245
|
-
},
|
|
246
|
-
};
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
sendMsg(batch, callback) {
|
|
250
|
-
switch (true) {
|
|
251
|
-
case Array.isArray(batch):
|
|
252
|
-
console.log(`Using incremental sending`);
|
|
253
|
-
let timeBetween = 1000;
|
|
254
|
-
batch.forEach((msg, i) => {
|
|
255
|
-
setTimeout(() => {
|
|
256
|
-
this.server.sendMsg(msg);
|
|
257
|
-
}, i * timeBetween);
|
|
258
|
-
});
|
|
259
|
-
break;
|
|
260
|
-
case typeof batch === "string":
|
|
261
|
-
this.server.sendMsg(batch);
|
|
262
|
-
break;
|
|
263
|
-
default:
|
|
264
|
-
this.server.sendMsg(batch.toString());
|
|
265
|
-
break;
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
/**
|
|
271
|
-
* @module IPCServer
|
|
272
|
-
* @description The IPC server instance
|
|
273
|
-
|
|
274
|
-
*/
|
|
275
|
-
let IPCServer = new IPC({ port: 3434 });
|
|
276
|
-
|
|
277
|
-
export default IPCServer;
|