chemicaljs 1.1.0 → 1.2.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/README.md +12 -2
- package/client/chemical.js +27 -17
- package/client/chemical.sw.js +23 -7
- package/config/scramjet/scramjet.config.js +9 -0
- package/{client → config}/uv/uv.config.js +1 -1
- package/index.js +70 -5
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -19,6 +19,17 @@ import { ChemicalServer } from "chemicaljs";
|
|
|
19
19
|
const chemical = new ChemicalServer();
|
|
20
20
|
```
|
|
21
21
|
|
|
22
|
+
You can pass options to disable proxy services and set the default service.
|
|
23
|
+
|
|
24
|
+
```js
|
|
25
|
+
const chemical = new ChemicalServer({
|
|
26
|
+
default: "rammerhead",
|
|
27
|
+
uv: true,
|
|
28
|
+
scramjet: false,
|
|
29
|
+
rammerhead: true
|
|
30
|
+
});
|
|
31
|
+
```
|
|
32
|
+
|
|
22
33
|
3. Use `chemical.app` which is an express app. You may need to import express for certain APIs.
|
|
23
34
|
|
|
24
35
|
```js
|
|
@@ -74,7 +85,7 @@ If you want to set the wisp server to an external server just change the `wisp`
|
|
|
74
85
|
await window.chemicalEncode("https://example.com")
|
|
75
86
|
```
|
|
76
87
|
|
|
77
|
-
Optional: Change service to `ultraviolet`, `scramjet`, or `rammerhead`.
|
|
88
|
+
Optional: Change service to `ultraviolet`, `scramjet`, or `rammerhead`. Defaults to `uv` or server option.
|
|
78
89
|
|
|
79
90
|
```js
|
|
80
91
|
await window.chemicalEncode("https://example.com", "rammerhead")
|
|
@@ -116,7 +127,6 @@ Below is a simple example of a simple input that redirects to the encoded URL wh
|
|
|
116
127
|
|
|
117
128
|
Some features that may come in the future are:
|
|
118
129
|
|
|
119
|
-
- Server options to disable services.
|
|
120
130
|
- Easy client components.
|
|
121
131
|
- Easy tab cloaking components.
|
|
122
132
|
|
package/client/chemical.js
CHANGED
|
@@ -174,24 +174,30 @@ function rammerheadEncode(baseUrl) {
|
|
|
174
174
|
})
|
|
175
175
|
}
|
|
176
176
|
|
|
177
|
-
async function chemicalEncode(url, service =
|
|
177
|
+
async function chemicalEncode(url, service = defaultService) {
|
|
178
178
|
switch (service) {
|
|
179
|
-
case "
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
179
|
+
case "uv":
|
|
180
|
+
if (uvEnabled) {
|
|
181
|
+
return (
|
|
182
|
+
window.location.origin +
|
|
183
|
+
window.__uv$config.prefix +
|
|
184
|
+
window.__uv$config.encodeUrl(url)
|
|
185
|
+
);
|
|
186
|
+
}
|
|
185
187
|
break;
|
|
186
188
|
case "rammerhead":
|
|
187
|
-
|
|
189
|
+
if (rammerheadEnabled) {
|
|
190
|
+
return window.location.origin + (await rammerheadEncode(url));
|
|
191
|
+
}
|
|
188
192
|
break;
|
|
189
193
|
case "scramjet":
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
194
|
+
if (scramjetEnabled) {
|
|
195
|
+
return (
|
|
196
|
+
window.location.origin +
|
|
197
|
+
window.__scramjet$config.prefix +
|
|
198
|
+
window.__scramjet$config.codec.encode(url)
|
|
199
|
+
);
|
|
200
|
+
}
|
|
195
201
|
break;
|
|
196
202
|
}
|
|
197
203
|
}
|
|
@@ -225,10 +231,14 @@ async function loadScript(src) {
|
|
|
225
231
|
|
|
226
232
|
(async () => {
|
|
227
233
|
await loadScript("/baremux/index.js");
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
234
|
+
if (uvEnabled) {
|
|
235
|
+
await loadScript("/uv/uv.bundle.js");
|
|
236
|
+
await loadScript("/uv/uv.config.js");
|
|
237
|
+
}
|
|
238
|
+
if (scramjetEnabled) {
|
|
239
|
+
await loadScript("/scramjet/scramjet.codecs.js");
|
|
240
|
+
await loadScript("/scramjet/scramjet.config.js");
|
|
241
|
+
}
|
|
232
242
|
await registerSW();
|
|
233
243
|
chemicalLoaded = true;
|
|
234
244
|
window.dispatchEvent(new Event("chemicalLoaded"));
|
package/client/chemical.sw.js
CHANGED
|
@@ -1,19 +1,35 @@
|
|
|
1
|
-
|
|
2
|
-
importScripts("/uv/uv.bundle.js");
|
|
3
|
-
importScripts("/uv/uv.config.js");
|
|
4
|
-
importScripts(__uv$config.sw || "/uv/uv.sw.js");
|
|
1
|
+
if (uvEnabled) {
|
|
2
|
+
importScripts("/uv/uv.bundle.js");
|
|
3
|
+
importScripts("/uv/uv.config.js");
|
|
4
|
+
importScripts(__uv$config.sw || "/uv/uv.sw.js");
|
|
5
|
+
}
|
|
6
|
+
if (scramjetEnabled) {
|
|
7
|
+
importScripts("/scramjet/scramjet.codecs.js");
|
|
8
|
+
importScripts("/scramjet/scramjet.config.js");
|
|
9
|
+
importScripts(__scramjet$config.bundle || "/scramjet/scramjet.bundle.js");
|
|
10
|
+
importScripts(__scramjet$config.worker || "/scramjet/scramjet.worker.js");
|
|
11
|
+
}
|
|
5
12
|
|
|
6
13
|
Object.defineProperty(self, "crossOriginIsolated", { value: true }); // Firefox fix
|
|
7
14
|
|
|
8
|
-
|
|
15
|
+
let uv, scramjet;
|
|
16
|
+
|
|
17
|
+
if (uvEnabled) {
|
|
18
|
+
uv = new UVServiceWorker();
|
|
19
|
+
}
|
|
20
|
+
if (scramjetEnabled) {
|
|
21
|
+
scramjet = new ScramjetServiceWorker();
|
|
22
|
+
}
|
|
9
23
|
|
|
10
24
|
self.addEventListener("fetch", (event) => {
|
|
11
25
|
event.respondWith(
|
|
12
26
|
(async () => {
|
|
13
|
-
if (uv.route(event)) {
|
|
27
|
+
if (uvEnabled && uv.route(event)) {
|
|
14
28
|
return await uv.fetch(event);
|
|
29
|
+
} else if (scramjetEnabled && scramjet.route(event)) {
|
|
30
|
+
return await scramjet.fetch(event);
|
|
15
31
|
}
|
|
16
32
|
return await fetch(event.request);
|
|
17
33
|
})(),
|
|
18
34
|
);
|
|
19
|
-
});
|
|
35
|
+
});
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
self.__scramjet$config = {
|
|
2
|
+
prefix: "/~/sj/",
|
|
3
|
+
codec: self.__scramjet$codecs.plain,
|
|
4
|
+
config: "/scramjet/scramjet.config.js",
|
|
5
|
+
bundle: "/scramjet/scramjet.bundle.js",
|
|
6
|
+
worker: "/scramjet/scramjet.worker.js",
|
|
7
|
+
client: "/scramjet/scramjet.client.js",
|
|
8
|
+
codecs: "/scramjet/scramjet.codecs.js",
|
|
9
|
+
};
|
package/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { resolve } from "node:path";
|
|
2
|
+
import { readFileSync } from "node:fs";
|
|
2
3
|
import { createServer } from "node:http";
|
|
3
4
|
import express from "express";
|
|
4
5
|
import wisp from "wisp-server-node";
|
|
@@ -9,7 +10,12 @@ import { scramjetPath } from "@mercuryworkshop/scramjet";
|
|
|
9
10
|
import createRammerhead from "rammerhead/src/server/index.js";
|
|
10
11
|
|
|
11
12
|
class ChemicalServer {
|
|
12
|
-
constructor() {
|
|
13
|
+
constructor(options) {
|
|
14
|
+
if (typeof options !== "object" || Array.isArray(options)) {
|
|
15
|
+
options = {}
|
|
16
|
+
console.error("Error: ChemicalServer options invalid.")
|
|
17
|
+
}
|
|
18
|
+
|
|
13
19
|
const rh = createRammerhead()
|
|
14
20
|
|
|
15
21
|
const rammerheadScopes = [
|
|
@@ -49,13 +55,72 @@ class ChemicalServer {
|
|
|
49
55
|
|
|
50
56
|
this.server = createServer();
|
|
51
57
|
this.app = express();
|
|
58
|
+
this.app.get("/chemical.js", async function (req, res) {
|
|
59
|
+
let chemicalMain = await readFileSync(resolve(import.meta.dirname, "client/chemical.js"), "utf8");
|
|
60
|
+
|
|
61
|
+
if (options.default) {
|
|
62
|
+
if (["uv", "rammerhead", "scramjet"].includes(options.default)) {
|
|
63
|
+
chemicalMain = `const defaultService = "${options.default}";\n\n` + chemicalMain
|
|
64
|
+
} else {
|
|
65
|
+
chemicalMain = `const defaultService = "uv";\n\n` + chemicalMain
|
|
66
|
+
console.error("Error: Chemical default option invalid.")
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (options.uv == false) {
|
|
71
|
+
chemicalMain = "const uvEnabled = false;\n" + chemicalMain
|
|
72
|
+
} else {
|
|
73
|
+
chemicalMain = "const uvEnabled = true;\n" + chemicalMain
|
|
74
|
+
}
|
|
75
|
+
if (options.scramjet == false) {
|
|
76
|
+
chemicalMain = "const scramjetEnabled = false;\n" + chemicalMain
|
|
77
|
+
} else {
|
|
78
|
+
chemicalMain = "const scramjetEnabled = true;\n" + chemicalMain
|
|
79
|
+
}
|
|
80
|
+
if (options.rammerhead == false) {
|
|
81
|
+
chemicalMain = "const rammerheadEnabled = false;\n" + chemicalMain
|
|
82
|
+
} else {
|
|
83
|
+
chemicalMain = "const rammerheadEnabled = true;\n" + chemicalMain
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
res.type("application/javascript");
|
|
87
|
+
return res.send(chemicalMain);
|
|
88
|
+
});
|
|
89
|
+
this.app.get("/chemical.sw.js", async function (req, res) {
|
|
90
|
+
let chemicalSW = await readFileSync(resolve(import.meta.dirname, "client/chemical.sw.js"), "utf8");
|
|
91
|
+
|
|
92
|
+
if (options.uv == false) {
|
|
93
|
+
chemicalSW = "const uvEnabled = false;\n\n" + chemicalSW
|
|
94
|
+
} else {
|
|
95
|
+
chemicalSW = "const uvEnabled = true;\n\n" + chemicalSW
|
|
96
|
+
}
|
|
97
|
+
if (options.scramjet == false) {
|
|
98
|
+
chemicalSW = "const scramjetEnabled = false;\n" + chemicalSW
|
|
99
|
+
} else {
|
|
100
|
+
chemicalSW = "const scramjetEnabled = true;\n" + chemicalSW
|
|
101
|
+
}
|
|
102
|
+
if (options.rammerhead == false) {
|
|
103
|
+
chemicalSW = "const rammerheadEnabled = false;\n" + chemicalSW
|
|
104
|
+
} else {
|
|
105
|
+
chemicalSW = "const rammerheadEnabled = true;\n" + chemicalSW
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
res.type("application/javascript");
|
|
109
|
+
return res.send(chemicalSW);
|
|
110
|
+
});
|
|
52
111
|
this.app.use(express.static(resolve(import.meta.dirname, "client")));
|
|
53
112
|
this.app.use("/baremux/", express.static(baremuxPath));
|
|
54
113
|
this.app.use("/epoxy/", express.static(epoxyPath));
|
|
55
|
-
|
|
56
|
-
|
|
114
|
+
if (options.uv !== false) {
|
|
115
|
+
this.app.use("/uv/", express.static(resolve(import.meta.dirname, "config/uv")));
|
|
116
|
+
this.app.use("/uv/", express.static(uvPath));
|
|
117
|
+
}
|
|
118
|
+
if (options.scramjet !== false) {
|
|
119
|
+
this.app.use("/scramjet/", express.static(resolve(import.meta.dirname, "config/scramjet")));
|
|
120
|
+
this.app.use("/scramjet/", express.static(scramjetPath));
|
|
121
|
+
}
|
|
57
122
|
this.server.on("request", (req, res) => {
|
|
58
|
-
if (shouldRouteRh(req)) {
|
|
123
|
+
if (options.rammerhead !== false && shouldRouteRh(req)) {
|
|
59
124
|
routeRhRequest(req, res);
|
|
60
125
|
} else {
|
|
61
126
|
this.app(req, res);
|
|
@@ -64,7 +129,7 @@ class ChemicalServer {
|
|
|
64
129
|
this.server.on("upgrade", (req, socket, head) => {
|
|
65
130
|
if (req.url && req.url.endsWith("/wisp/")) {
|
|
66
131
|
wisp.routeRequest(req, socket, head);
|
|
67
|
-
} else if (shouldRouteRh(req)) {
|
|
132
|
+
} else if (options.rammerhead !== false && shouldRouteRh(req)) {
|
|
68
133
|
routeRhUpgrade(req, socket, head);
|
|
69
134
|
} else {
|
|
70
135
|
socket.end();
|