vaderjs 1.4.0 → 1.4.1-d560b9aa7f
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/@integrations/ssg.js +178 -0
- package/LICENSE +21 -0
- package/README.md +34 -57
- package/binaries/IPC/index.js +277 -0
- package/binaries/main.js +1318 -0
- package/binaries/watcher.js +70 -66
- package/binaries/win32/check.ps1 +7 -0
- package/client/index.js +15 -0
- package/config/index.js +36 -0
- package/package.json +8 -6
- package/runtime/router.js +1 -1
- package/runtime/vader.js +1 -1
- package/vader.js +212 -672
- package/binaries/generator.js +0 -201
|
@@ -0,0 +1,178 @@
|
|
|
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
|
+
console
|
|
52
|
+
|
|
53
|
+
let { path, file, isParam, params,kind, isCatchAll, query, pathname } = routeData;
|
|
54
|
+
|
|
55
|
+
let baseFolder = file.split('/').filter(f => f !== 'dist').join('/')
|
|
56
|
+
if (file.includes('./dist')) {
|
|
57
|
+
baseFolder = file.split('./dist')[1]
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
let html = `
|
|
62
|
+
<!DOCTYPE html>
|
|
63
|
+
<html>
|
|
64
|
+
<head>
|
|
65
|
+
|
|
66
|
+
<title>Vaderjs v1.3.3</title>
|
|
67
|
+
<script id="v">
|
|
68
|
+
window.$SERVER = true
|
|
69
|
+
</script>
|
|
70
|
+
</head>
|
|
71
|
+
<body>
|
|
72
|
+
|
|
73
|
+
<div id="app"></div>
|
|
74
|
+
<script id="router" type="module">
|
|
75
|
+
import Router from '/router.js'
|
|
76
|
+
|
|
77
|
+
const rt = new Router
|
|
78
|
+
${Object.keys(params).length > 0 ? Object.keys(params).map((param, i) => {
|
|
79
|
+
let first = pathname.split('/')[1]
|
|
80
|
+
first = '/' + first
|
|
81
|
+
let ranName = `_${Math.random().toString(36).substr(2, 9)}`
|
|
82
|
+
return `
|
|
83
|
+
import ${ranName} from '${baseFolder}'
|
|
84
|
+
rt.get($SERVER ? '/' : '${first}/:${param}', ${ranName})`
|
|
85
|
+
}): ''}
|
|
86
|
+
|
|
87
|
+
let c = await import('${baseFolder}')
|
|
88
|
+
if(Object.keys(c).includes('$prerender') && c.$prerender === false){
|
|
89
|
+
document.head.setAttribute('prerender', 'false')
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
${
|
|
93
|
+
!isCatchAll && !pathname.includes('[') ? `rt.get($SERVER ? '/' : '${pathname}', c)` : isCatchAll && !pathname.includes('[') ? `rt.get($SERVER ? '/' : '${pathname}/*', c)` : ``
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
rt.listen()
|
|
98
|
+
</script>
|
|
99
|
+
</body>
|
|
100
|
+
</html>`
|
|
101
|
+
return html;
|
|
102
|
+
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* @integration Server Side Generator
|
|
106
|
+
* @description Generate a static webapp from your Vader.js application
|
|
107
|
+
* @returns
|
|
108
|
+
*/
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
const ssg = async (config) => {
|
|
112
|
+
return new Promise(async (resolve, reject) => {
|
|
113
|
+
let { pages, output } = config;
|
|
114
|
+
if(!fs.existsSync(`${process.cwd()}/_dev/meta/routes.json`)){
|
|
115
|
+
process.exit()
|
|
116
|
+
}
|
|
117
|
+
let routes = JSON.parse(fs.readFileSync(`${process.cwd()}/_dev/meta/routes.json`).toString())
|
|
118
|
+
for (var i in routes) {
|
|
119
|
+
let route = routes[i];
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
let html = generateHTML(route);
|
|
123
|
+
globalThis.routeDocuments[routes[i].path] = html;
|
|
124
|
+
let browser = await playwright.chromium.launch({
|
|
125
|
+
headless: true,
|
|
126
|
+
executablePath: process.platform === 'win32' ? '' : '/usr/bin/chromium-browser'
|
|
127
|
+
});
|
|
128
|
+
globalThis.browser = browser;
|
|
129
|
+
let page = await browser.newPage();
|
|
130
|
+
await page.goto(`http://localhost:8700?folder=${routes[i].path}`, { waitUntil: 'load' });
|
|
131
|
+
await page.evaluate(() => {
|
|
132
|
+
document.querySelector('script#v').innerHTML = `window.$SERVER = false`
|
|
133
|
+
if(document.head.getAttribute('prerender') === 'false'){
|
|
134
|
+
document.querySelector("#app").innerHTML = "";
|
|
135
|
+
}
|
|
136
|
+
});
|
|
137
|
+
let content = await page.content();
|
|
138
|
+
if (output.includes('./dist')) {
|
|
139
|
+
output = output.split('./dist')[1]
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
if(routes[i].path.includes('[')){
|
|
143
|
+
routes[i].path = routes[i].path.split('[')[0]
|
|
144
|
+
}else if(routes[i].path.includes(':')){
|
|
145
|
+
routes[i].path = routes[i].path.split(':')[0]
|
|
146
|
+
}
|
|
147
|
+
let path = '/dist/' + output + routes[i].path + '/index.html';
|
|
148
|
+
|
|
149
|
+
fs.writeFileSync(process.cwd() + path, content);
|
|
150
|
+
hasGenerated.push(routes[i].path)
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
if (hasGenerated.length === routes.length) {
|
|
154
|
+
console.log(`Static site generation complete`)
|
|
155
|
+
browser.close()
|
|
156
|
+
server.close()
|
|
157
|
+
resolve()
|
|
158
|
+
process.exit()
|
|
159
|
+
}
|
|
160
|
+
})
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
if(context){
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
server.listen(8700);
|
|
167
|
+
await ssg({ pages: './pages', output: './dist' })
|
|
168
|
+
}
|
|
169
|
+
export default {
|
|
170
|
+
name: 'Vaderjs Static Site Generator',
|
|
171
|
+
version: '1.0.0',
|
|
172
|
+
useRuntime: 'node',
|
|
173
|
+
entryPoint: process.cwd() + '/node_modules/vaderjs/@integrations/ssg.js',
|
|
174
|
+
on: ['build', 'dev'],
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
// Path:integrations/ssg.js
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
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/README.md
CHANGED
|
@@ -17,14 +17,10 @@
|
|
|
17
17
|
> Do not use any alpha versions as these where changed multiple times any version under latest is considered lts and are deemed to be stable
|
|
18
18
|
## Get Started
|
|
19
19
|
|
|
20
|
-
1.
|
|
21
|
-
> Warning - do not use wsl version of bun with vader alot will not work due to path resolution use the experimental version :}
|
|
22
|
-
([Install Bun](https://bun.sh/docs/installation))
|
|
23
|
-
|
|
24
|
-
2. Install vaderjs
|
|
20
|
+
1. Install vaderjs
|
|
25
21
|
|
|
26
22
|
```bash
|
|
27
|
-
|
|
23
|
+
npx vaderjs@latest
|
|
28
24
|
```
|
|
29
25
|
|
|
30
26
|
4. Create Proper Folders
|
|
@@ -37,7 +33,7 @@ Tip: Each folder can be deep nested up to 4 levels!
|
|
|
37
33
|
/pages/index.jsx = /
|
|
38
34
|
/pages/home/[page].jsx = /home/:page
|
|
39
35
|
/pages/path/index.jsx = /path/
|
|
40
|
-
/pages/test/[...]/index.jsx = /path/test/*
|
|
36
|
+
/pages/test/[[...catchall]]/index.jsx = /path/test/*
|
|
41
37
|
/pages/route/[param1]/[param2].jsx = /path/route/:param1/:param2
|
|
42
38
|
```
|
|
43
39
|
Keyword folders - all files are passed from these folders to the `dist` folder
|
|
@@ -50,9 +46,29 @@ public - used for anything
|
|
|
50
46
|
|
|
51
47
|
```
|
|
52
48
|
|
|
49
|
+
Create a vader.config.js file
|
|
50
|
+
|
|
51
|
+
```js
|
|
52
|
+
import {defineConfig} from 'vaderjs/config/index.js'
|
|
53
|
+
import ssg from 'vaderjs/@integrations/ssg.js'
|
|
54
|
+
export default defineConfig({
|
|
55
|
+
host:{
|
|
56
|
+
provider:'vercel',
|
|
57
|
+
prod: {
|
|
58
|
+
port: process.env.PRODUCTION
|
|
59
|
+
}
|
|
60
|
+
},
|
|
61
|
+
dev:{
|
|
62
|
+
port: 3000
|
|
63
|
+
},
|
|
64
|
+
integrations:[ssg]
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
```
|
|
68
|
+
|
|
53
69
|
|
|
54
70
|
|
|
55
|
-
5. And your done - Run `
|
|
71
|
+
5. And your done - Run `npx vaderjs` and the compiled output is visible inside of the `/dist/` folder!
|
|
56
72
|
|
|
57
73
|
|
|
58
74
|
## Key Features & Examples
|
|
@@ -64,7 +80,7 @@ vader's compiler automatically handles routing so you wont need to! - it uses a
|
|
|
64
80
|
/pages/index.jsx = /
|
|
65
81
|
/pages/home/[page].jsx = /home/:page
|
|
66
82
|
/pages/path/index.jsx = /path/
|
|
67
|
-
/pages/path/[...].jsx = /path/*
|
|
83
|
+
/pages/path/[[...catchall]].jsx = /path/*
|
|
68
84
|
|
|
69
85
|
```
|
|
70
86
|
For pages that have [params] you can derive it using this.request
|
|
@@ -84,8 +100,8 @@ export default function(req, res){
|
|
|
84
100
|
let [count, setCount] = useState(0)
|
|
85
101
|
|
|
86
102
|
return <>
|
|
87
|
-
<h1>{count}</h1>
|
|
88
|
-
<button onClick={(event)=>{setCount(
|
|
103
|
+
<h1>{count()}</h1>
|
|
104
|
+
<button onClick={(event)=>{setCount(count() + 1)}
|
|
89
105
|
</>
|
|
90
106
|
}
|
|
91
107
|
|
|
@@ -95,7 +111,7 @@ export default function(req, res){
|
|
|
95
111
|
|
|
96
112
|
# ServerSide Site Generation (SSG)
|
|
97
113
|
|
|
98
|
-
Vader compiles all code to a static index.html page so your visitors will never have to wait for the page to load
|
|
114
|
+
Vader allows you to integrate ssg into your app via config file, it compiles all code to a static index.html page so your visitors will never have to wait for the page to load. Uppon rerender the page rehydrates reapplying functionality!
|
|
99
115
|
|
|
100
116
|
you can always opt out of ssg using:
|
|
101
117
|
|
|
@@ -128,11 +144,9 @@ export function Layout({title, keywords, description, children}){
|
|
|
128
144
|
)
|
|
129
145
|
}
|
|
130
146
|
|
|
131
|
-
// pages/index.jsx
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
export default function (req, res){
|
|
147
|
+
// pages/index.jsx
|
|
148
|
+
export default function Index(_,req, res){
|
|
149
|
+
//_ is props which you wont need on base route componnets
|
|
136
150
|
return (
|
|
137
151
|
<Layout {...{title:'home', description:'home page', keywords:'vader.js', logo:''}}>
|
|
138
152
|
<h1> Hello World</h1>
|
|
@@ -149,7 +163,7 @@ Vaderjs has two types of in javascript styling - css modules and inline jsx styl
|
|
|
149
163
|
```jsx
|
|
150
164
|
|
|
151
165
|
// inline
|
|
152
|
-
<button style={{color:'red'}}>Button</button>
|
|
166
|
+
<button style={{color:'red', "@mobile":{color:'green'}}}>Button</button>
|
|
153
167
|
|
|
154
168
|
// css module
|
|
155
169
|
|
|
@@ -189,12 +203,12 @@ export default class MyApp extends Component{
|
|
|
189
203
|
console.log(ref.current) // p tag
|
|
190
204
|
}, this)
|
|
191
205
|
return <>
|
|
192
|
-
<p ref={ref}>Count is {count}</p>
|
|
206
|
+
<p ref={ref}>Count is {count()}</p>
|
|
193
207
|
{/**
|
|
194
208
|
pass anything used from the toplevel render to the lowerlevel function params to be able to invoke!
|
|
195
209
|
**/}
|
|
196
210
|
<button onclick={()=>{
|
|
197
|
-
setCount(
|
|
211
|
+
setCount(count() + 1)
|
|
198
212
|
}}>Increment</button>
|
|
199
213
|
</>
|
|
200
214
|
|
|
@@ -202,43 +216,6 @@ export default class MyApp extends Component{
|
|
|
202
216
|
}
|
|
203
217
|
```
|
|
204
218
|
|
|
205
|
-
|
|
206
|
-
### Function Binding
|
|
207
|
-
|
|
208
|
-
Vaderjs allows you to bind functions directly to html elements just like react
|
|
209
|
-
there are two ways - top level invokes like below
|
|
210
|
-
|
|
211
|
-
```javascript
|
|
212
|
-
// vader uses params[0] as the event target object and other parameters resolve after
|
|
213
|
-
|
|
214
|
-
function click(event, otherparams){
|
|
215
|
-
console.log(event.target, otherparams)
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
const hello = function(event, otherparams){
|
|
219
|
-
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
return <>
|
|
223
|
-
<button onclick={()=>click()}>Click Me</button>
|
|
224
|
-
</>
|
|
225
|
-
```
|
|
226
|
-
|
|
227
|
-
Low level invokes are considered top level and can access - any value above the scope !!
|
|
228
|
-
|
|
229
|
-
```jsx
|
|
230
|
-
let car = {
|
|
231
|
-
model: 'tesla',
|
|
232
|
-
price: 'toomiuch'
|
|
233
|
-
}
|
|
234
|
-
return <>
|
|
235
|
-
<button onclick={(event)=>{
|
|
236
|
-
console.log(car.model)
|
|
237
|
-
}}>Log</button>
|
|
238
|
-
```
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
219
|
|
|
243
220
|
|
|
244
221
|
|
|
@@ -0,0 +1,277 @@
|
|
|
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;
|