chemicaljs 2.2.0 → 2.4.0

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/index.js DELETED
@@ -1,597 +0,0 @@
1
- import { resolve, dirname } from "node:path";
2
- import {
3
- readFileSync,
4
- cpSync,
5
- writeFileSync,
6
- copyFileSync,
7
- existsSync,
8
- mkdirSync,
9
- rmSync,
10
- readdirSync,
11
- } from "node:fs";
12
- import { createServer } from "node:http";
13
- import { fileURLToPath } from "node:url";
14
- import express from "express";
15
- import { server as wisp, logging } from "@mercuryworkshop/wisp-js/server";
16
- import { baremuxPath } from "@mercuryworkshop/bare-mux/node";
17
- import { libcurlPath } from "@mercuryworkshop/libcurl-transport";
18
- import { epoxyPath } from "@mercuryworkshop/epoxy-transport";
19
- import { uvPath } from "@titaniumnetwork-dev/ultraviolet";
20
- import { scramjetPath } from "@mercuryworkshop/scramjet";
21
- import { meteorPath } from "meteorproxy";
22
- import createRammerhead from "rammerhead/src/server/index.js";
23
- const __dirname = dirname(fileURLToPath(import.meta.url));
24
-
25
- logging.set_level(logging.ERROR);
26
-
27
- class ChemicalServer {
28
- constructor(options) {
29
- if (options) {
30
- if (typeof options !== "object" || Array.isArray(options)) {
31
- options = {};
32
- console.error("Error: ChemicalServer options invalid.");
33
- }
34
- } else {
35
- options = {};
36
- }
37
-
38
- if (options.uv === undefined) {
39
- options.uv = true;
40
- }
41
-
42
- if (options.experimental === undefined) {
43
- options.experimental = {};
44
- }
45
-
46
- if (options.experimental.scramjet === undefined) {
47
- options.experimental.scramjet = false;
48
- }
49
-
50
- if (options.experimental.meteor === undefined) {
51
- options.experimental.meteor = false;
52
- }
53
-
54
- if (options.rammerhead === undefined) {
55
- options.rammerhead = true;
56
- }
57
-
58
- if (options.demoMode === undefined) {
59
- options.demoMode = false;
60
- }
61
-
62
- this.options = options;
63
- this.server = createServer();
64
- this.app = express();
65
- this.app.serveChemical = this.serveChemical;
66
-
67
- return [this.app, this.listen];
68
- }
69
- serveChemical = () => {
70
- const rh = createRammerhead();
71
- const rammerheadScopes = [
72
- "/rammerhead.js",
73
- "/hammerhead.js",
74
- "/transport-worker.js",
75
- "/task.js",
76
- "/iframe-task.js",
77
- "/worker-hammerhead.js",
78
- "/messaging",
79
- "/sessionexists",
80
- "/deletesession",
81
- "/newsession",
82
- "/editsession",
83
- "/needpassword",
84
- "/syncLocalStorage",
85
- "/api/shuffleDict",
86
- ];
87
- const rammerheadSession = /^\/[a-z0-9]{32}/;
88
- const shouldRouteRh = (req) => {
89
- const url = new URL(req.url, "http://0.0.0.0");
90
- return (
91
- rammerheadScopes.includes(url.pathname) ||
92
- rammerheadSession.test(url.pathname)
93
- );
94
- };
95
- const routeRhRequest = (req, res) => {
96
- rh.emit("request", req, res);
97
- };
98
- const routeRhUpgrade = (req, socket, head) => {
99
- rh.emit("upgrade", req, socket, head);
100
- };
101
-
102
- this.app.get("/chemical.js", async (req, res) => {
103
- let chemicalMain = await readFileSync(
104
- resolve(__dirname, "client/chemical.js"),
105
- "utf8"
106
- );
107
-
108
- if (this.options.default) {
109
- if (
110
- ["uv", "rammerhead", "scramjet", "meteor"].includes(
111
- this.options.default
112
- )
113
- ) {
114
- chemicalMain =
115
- `const defaultService = "${this.options.default}";\n\n` +
116
- chemicalMain;
117
- } else {
118
- chemicalMain = `const defaultService = "uv";\n\n` + chemicalMain;
119
- console.error("Error: Chemical default option invalid.");
120
- }
121
- } else {
122
- chemicalMain = `const defaultService = "uv";\n\n` + chemicalMain;
123
- }
124
-
125
- chemicalMain =
126
- "const uvEnabled = " + String(this.options.uv) + ";\n" + chemicalMain;
127
- chemicalMain =
128
- "const scramjetEnabled = " +
129
- String(this.options.experimental.scramjet) +
130
- ";\n" +
131
- chemicalMain;
132
- chemicalMain =
133
- "const meteorEnabled = " +
134
- String(this.options.experimental.meteor) +
135
- ";\n" +
136
- chemicalMain;
137
- chemicalMain =
138
- "const rammerheadEnabled = " +
139
- String(this.options.rammerhead) +
140
- ";\n" +
141
- chemicalMain;
142
- chemicalMain =
143
- "const demoMode = " +
144
- String(this.options.demoMode) +
145
- ";\n" +
146
- chemicalMain;
147
-
148
- chemicalMain = "(async () => {\n" + chemicalMain + "\n})();";
149
-
150
- res.type("application/javascript");
151
- return res.send(chemicalMain);
152
- });
153
- this.app.get("/chemical.sw.js", async (req, res) => {
154
- let chemicalSW = await readFileSync(
155
- resolve(__dirname, "client/chemical.sw.js"),
156
- "utf8"
157
- );
158
-
159
- chemicalSW =
160
- "const uvEnabled = " + String(this.options.uv) + ";\n" + chemicalSW;
161
- chemicalSW =
162
- "const scramjetEnabled = " +
163
- String(this.options.experimental.scramjet) +
164
- ";\n" +
165
- chemicalSW;
166
- chemicalSW =
167
- "const meteorEnabled = " +
168
- String(this.options.experimental.meteor) +
169
- ";\n" +
170
- chemicalSW;
171
- chemicalSW =
172
- "const rammerheadEnabled = " +
173
- String(this.options.rammerhead) +
174
- ";\n" +
175
- chemicalSW;
176
-
177
- res.type("application/javascript");
178
- return res.send(chemicalSW);
179
- });
180
- this.app.use(express.static(resolve(__dirname, "client")));
181
- this.app.use("/baremux/", express.static(baremuxPath));
182
- this.app.use("/libcurl/", express.static(libcurlPath));
183
- this.app.use("/epoxy/", express.static(epoxyPath));
184
- if (this.options.uv) {
185
- this.app.use("/uv/", express.static(resolve(__dirname, "config/uv")));
186
- this.app.use("/uv/", express.static(uvPath));
187
- }
188
- if (this.options.experimental.scramjet) {
189
- this.app.use(
190
- "/scramjet/",
191
- express.static(resolve(__dirname, "config/scramjet"))
192
- );
193
- this.app.use("/scramjet/", express.static(scramjetPath));
194
- }
195
- if (this.options.experimental.meteor) {
196
- this.app.use(
197
- "/meteor/",
198
- express.static(resolve(__dirname, "config/meteor"))
199
- );
200
- this.app.use("/meteor/", express.static(meteorPath));
201
- }
202
- this.server.on("request", (req, res) => {
203
- if (this.options.rammerhead && shouldRouteRh(req)) {
204
- routeRhRequest(req, res);
205
- } else {
206
- this.app(req, res);
207
- }
208
- });
209
- this.server.on("upgrade", (req, socket, head) => {
210
- if (req.url && req.url.endsWith("/wisp/")) {
211
- if (this.options.wispOptions) {
212
- for (let option in this.options.wispOptions) {
213
- wisp.options[option] = this.options.wispOptions[option];
214
- }
215
- }
216
- wisp.routeRequest(req, socket, head);
217
- } else if (this.options.rammerhead && shouldRouteRh(req)) {
218
- routeRhUpgrade(req, socket, head);
219
- } else {
220
- socket.end();
221
- }
222
- });
223
- };
224
- listen = (port, callback) => {
225
- this.server.listen(port, callback);
226
- };
227
- }
228
-
229
- const ChemicalVitePlugin = (options) => ({
230
- name: "chemical-vite-plugin",
231
- configureServer(server) {
232
- if (options) {
233
- if (typeof options !== "object" || Array.isArray(options)) {
234
- options = {};
235
- console.error("Error: ChemicalServer options invalid.");
236
- }
237
- } else {
238
- options = {};
239
- }
240
-
241
- if (options.uv === undefined) {
242
- options.uv = true;
243
- }
244
-
245
- if (options.experimental === undefined) {
246
- options.experimental = {};
247
- }
248
-
249
- if (options.experimental.scramjet === undefined) {
250
- options.experimental.scramjet = false;
251
- }
252
-
253
- if (options.experimental.meteor === undefined) {
254
- options.experimental.meteor = false;
255
- }
256
-
257
- if (options.rammerhead === undefined) {
258
- options.rammerhead = true;
259
- }
260
-
261
- if (options.demoMode === undefined) {
262
- options.demoMode = false;
263
- }
264
-
265
- const rh = createRammerhead();
266
- const rammerheadScopes = [
267
- "/rammerhead.js",
268
- "/hammerhead.js",
269
- "/transport-worker.js",
270
- "/task.js",
271
- "/iframe-task.js",
272
- "/worker-hammerhead.js",
273
- "/messaging",
274
- "/sessionexists",
275
- "/deletesession",
276
- "/newsession",
277
- "/editsession",
278
- "/needpassword",
279
- "/syncLocalStorage",
280
- "/api/shuffleDict",
281
- ];
282
- const rammerheadSession = /^\/[a-z0-9]{32}/;
283
- const shouldRouteRh = (req) => {
284
- const url = new URL(req.url, "http://0.0.0.0");
285
- return (
286
- rammerheadScopes.includes(url.pathname) ||
287
- rammerheadSession.test(url.pathname)
288
- );
289
- };
290
- const routeRhRequest = (req, res) => {
291
- rh.emit("request", req, res);
292
- };
293
- const routeRhUpgrade = (req, socket, head) => {
294
- rh.emit("upgrade", req, socket, head);
295
- };
296
-
297
- const app = express();
298
- app.get("/chemical.js", async function (req, res) {
299
- let chemicalMain = await readFileSync(
300
- resolve(__dirname, "client/chemical.js"),
301
- "utf8"
302
- );
303
-
304
- if (options.default) {
305
- if (
306
- ["uv", "rammerhead", "scramjet", "meteor"].includes(options.default)
307
- ) {
308
- chemicalMain =
309
- `const defaultService = "${options.default}";\n\n` + chemicalMain;
310
- } else {
311
- chemicalMain = `const defaultService = "uv";\n\n` + chemicalMain;
312
- console.error("Error: Chemical default option invalid.");
313
- }
314
- } else {
315
- chemicalMain = `const defaultService = "uv";\n\n` + chemicalMain;
316
- }
317
-
318
- chemicalMain =
319
- "const uvEnabled = " + String(options.uv) + ";\n" + chemicalMain;
320
- chemicalMain =
321
- "const scramjetEnabled = " +
322
- String(options.experimental.scramjet) +
323
- ";\n" +
324
- chemicalMain;
325
- chemicalMain =
326
- "const meteorEnabled = " +
327
- String(options.experimental.meteor) +
328
- ";\n" +
329
- chemicalMain;
330
- chemicalMain =
331
- "const rammerheadEnabled = " +
332
- String(options.rammerhead) +
333
- ";\n" +
334
- chemicalMain;
335
- chemicalMain =
336
- "const demoMode = " + String(options.demoMode) + ";\n" + chemicalMain;
337
-
338
- chemicalMain = "(async () => {\n" + chemicalMain + "\n})();";
339
-
340
- res.type("application/javascript");
341
- return res.send(chemicalMain);
342
- });
343
- app.get("/chemical.sw.js", async function (req, res) {
344
- let chemicalSW = await readFileSync(
345
- resolve(__dirname, "client/chemical.sw.js"),
346
- "utf8"
347
- );
348
-
349
- chemicalSW =
350
- "const uvEnabled = " + String(options.uv) + ";\n" + chemicalSW;
351
- chemicalSW =
352
- "const scramjetEnabled = " +
353
- String(options.experimental.scramjet) +
354
- ";\n" +
355
- chemicalSW;
356
- chemicalSW =
357
- "const meteorEnabled = " +
358
- String(options.experimental.meteor) +
359
- ";\n" +
360
- chemicalSW;
361
- chemicalSW =
362
- "const rammerheadEnabled = " +
363
- String(options.rammerhead) +
364
- ";\n" +
365
- chemicalSW;
366
-
367
- res.type("application/javascript");
368
- return res.send(chemicalSW);
369
- });
370
- app.use(express.static(resolve(__dirname, "client")));
371
- app.use("/baremux/", express.static(baremuxPath));
372
- app.use("/libcurl/", express.static(libcurlPath));
373
- app.use("/epoxy/", express.static(epoxyPath));
374
- if (options.uv) {
375
- app.use("/uv/", express.static(resolve(__dirname, "config/uv")));
376
- app.use("/uv/", express.static(uvPath));
377
- }
378
- if (options.experimental.scramjet) {
379
- app.use(
380
- "/scramjet/",
381
- express.static(resolve(__dirname, "config/scramjet"))
382
- );
383
- app.use("/scramjet/", express.static(scramjetPath));
384
- }
385
- if (options.experimental.meteor) {
386
- app.use("/meteor/", express.static(resolve(__dirname, "config/meteor")));
387
- app.use("/meteor/", express.static(meteorPath));
388
- }
389
-
390
- server.middlewares.use(app);
391
-
392
- server.middlewares.use((req, res, next) => {
393
- if (options.rammerhead && shouldRouteRh(req)) {
394
- routeRhRequest(req, res);
395
- } else {
396
- next();
397
- }
398
- });
399
-
400
- const upgraders = server.httpServer.listeners("upgrade");
401
-
402
- for (const upgrader of upgraders) {
403
- server.httpServer.off("upgrade", upgrader);
404
- }
405
-
406
- server.httpServer.on("upgrade", (req, socket, head) => {
407
- if (req.url && req.url.endsWith("/wisp/")) {
408
- if (options.wispOptions) {
409
- for (let option in options.wispOptions) {
410
- wisp.options[option] = options.wispOptions[option];
411
- }
412
- }
413
- wisp.routeRequest(req, socket, head);
414
- } else if (options.rammerhead && shouldRouteRh(req)) {
415
- routeRhUpgrade(req, socket, head);
416
- } else {
417
- for (const upgrader of upgraders) {
418
- upgrader(req, socket, head);
419
- }
420
- }
421
- });
422
- },
423
- });
424
-
425
- class ChemicalBuild {
426
- constructor(options) {
427
- if (options) {
428
- if (typeof options !== "object" || Array.isArray(options)) {
429
- options = {};
430
- console.error("Error: ChemicalBuild options invalid.");
431
- }
432
- } else {
433
- options = {};
434
- }
435
-
436
- if (options.path === undefined) {
437
- options.path = "dist";
438
- }
439
-
440
- if (options.path.startsWith("/")) {
441
- options.path = options.path.substring(1);
442
- }
443
-
444
- if (options.path.endsWith("/")) {
445
- options.path = options.path.slice(0, -1);
446
- }
447
-
448
- if (options.uv === undefined) {
449
- options.uv = true;
450
- }
451
-
452
- if (options.experimental === undefined) {
453
- options.experimental = {};
454
- }
455
-
456
- if (options.experimental.scramjet === undefined) {
457
- options.experimental.scramjet = false;
458
- }
459
-
460
- if (options.experimental.meteor === undefined) {
461
- options.experimental.meteor = false;
462
- }
463
-
464
- if (options.rammerhead === undefined) {
465
- options.rammerhead = true;
466
- }
467
-
468
- if (options.demoMode === undefined) {
469
- options.demoMode = false;
470
- }
471
-
472
- this.options = options;
473
- }
474
- async write(deletePath = false) {
475
- if (!existsSync(resolve(this.options.path))) {
476
- mkdirSync(resolve(this.options.path), { recursive: true });
477
- } else {
478
- if (deletePath) {
479
- readdirSync(resolve(this.options.path)).forEach((file) =>
480
- rmSync(resolve(this.options.path, file), { recursive: true })
481
- );
482
- }
483
- }
484
-
485
- let chemicalMain = await readFileSync(
486
- resolve(__dirname, "client/chemical.js"),
487
- "utf8"
488
- );
489
-
490
- if (this.options.default) {
491
- if (
492
- ["uv", "rammerhead", "scramjet", "meteor"].includes(
493
- this.options.default
494
- )
495
- ) {
496
- chemicalMain =
497
- `const defaultService = "${this.options.default}";\n\n` +
498
- chemicalMain;
499
- } else {
500
- chemicalMain = `const defaultService = "uv";\n\n` + chemicalMain;
501
- console.error("Error: Chemical default option invalid.");
502
- }
503
- } else {
504
- chemicalMain = `const defaultService = "uv";\n\n` + chemicalMain;
505
- }
506
-
507
- chemicalMain =
508
- "const uvEnabled = " + String(this.options.uv) + ";\n" + chemicalMain;
509
- chemicalMain =
510
- "const scramjetEnabled = " +
511
- String(this.options.experimental.scramjet) +
512
- ";\n" +
513
- chemicalMain;
514
- chemicalMain =
515
- "const meteorEnabled = " +
516
- String(this.options.experimental.meteor) +
517
- ";\n" +
518
- chemicalMain;
519
- chemicalMain =
520
- "const rammerheadEnabled = " +
521
- String(this.options.rammerhead) +
522
- ";\n" +
523
- chemicalMain;
524
- chemicalMain =
525
- "const demoMode = " +
526
- String(this.options.demoMode) +
527
- ";\n" +
528
- chemicalMain;
529
-
530
- chemicalMain = "(async () => {\n" + chemicalMain + "\n})();";
531
-
532
- writeFileSync(resolve(this.options.path, "chemical.js"), chemicalMain);
533
-
534
- let chemicalSW = await readFileSync(
535
- resolve(__dirname, "client/chemical.sw.js"),
536
- "utf8"
537
- );
538
-
539
- chemicalSW =
540
- "const uvEnabled = " + String(this.options.uv) + ";\n" + chemicalSW;
541
- chemicalSW =
542
- "const scramjetEnabled = " +
543
- String(this.options.experimental.scramjet) +
544
- ";\n" +
545
- chemicalSW;
546
- chemicalSW =
547
- "const meteorEnabled = " +
548
- String(this.options.experimental.meteor) +
549
- ";\n" +
550
- chemicalSW;
551
- chemicalSW =
552
- "const rammerheadEnabled = " +
553
- String(this.options.rammerhead) +
554
- ";\n" +
555
- chemicalSW;
556
-
557
- writeFileSync(resolve(this.options.path, "chemical.sw.js"), chemicalSW);
558
-
559
- cpSync(baremuxPath, resolve(this.options.path, "baremux"), {
560
- recursive: true,
561
- });
562
- cpSync(libcurlPath, resolve(this.options.path, "libcurl"), {
563
- recursive: true,
564
- });
565
- cpSync(epoxyPath, resolve(this.options.path, "epoxy"), { recursive: true });
566
- cpSync(libcurlPath, resolve(this.options.path, "libcurl"), {
567
- recursive: true,
568
- });
569
- if (this.options.uv) {
570
- cpSync(uvPath, resolve(this.options.path, "uv"), { recursive: true });
571
- copyFileSync(
572
- resolve(__dirname, "config/uv/uv.config.js"),
573
- resolve(this.options.path, "uv/uv.config.js")
574
- );
575
- }
576
- if (this.options.experimental.scramjet) {
577
- cpSync(scramjetPath, resolve(this.options.path, "scramjet"), {
578
- recursive: true,
579
- });
580
- copyFileSync(
581
- resolve(__dirname, "config/scramjet/scramjet.config.js"),
582
- resolve(this.options.path, "scramjet/scramjet.config.js")
583
- );
584
- }
585
- if (this.options.experimental.meteor) {
586
- cpSync(meteorPath, resolve(this.options.path, "meteor"), {
587
- recursive: true,
588
- });
589
- copyFileSync(
590
- resolve(__dirname, "config/meteor/meteor.config.js"),
591
- resolve(this.options.path, "meteor/meteor.config.js")
592
- );
593
- }
594
- }
595
- }
596
-
597
- export { ChemicalServer, ChemicalBuild, ChemicalVitePlugin };
package/rammerhead.tgz DELETED
Binary file