mirror of
https://github.com/MercuryWorkshop/scramjet.git
synced 2025-05-13 22:40:01 -04:00
allow websockets in dedicated workers
This commit is contained in:
parent
70e0a684ae
commit
3b9017e464
8 changed files with 42 additions and 21 deletions
|
@ -48,7 +48,7 @@
|
||||||
},
|
},
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@mercuryworkshop/bare-mux": "^2.0.2",
|
"@mercuryworkshop/bare-mux": "^2.0.5",
|
||||||
"@webreflection/idb-map": "^0.3.1",
|
"@webreflection/idb-map": "^0.3.1",
|
||||||
"astravel": "^0.6.1",
|
"astravel": "^0.6.1",
|
||||||
"astring": "^1.8.6",
|
"astring": "^1.8.6",
|
||||||
|
|
10
pnpm-lock.yaml
generated
10
pnpm-lock.yaml
generated
|
@ -9,8 +9,8 @@ importers:
|
||||||
.:
|
.:
|
||||||
dependencies:
|
dependencies:
|
||||||
'@mercuryworkshop/bare-mux':
|
'@mercuryworkshop/bare-mux':
|
||||||
specifier: ^2.0.2
|
specifier: ^2.0.5
|
||||||
version: 2.0.2
|
version: 2.0.5
|
||||||
'@webreflection/idb-map':
|
'@webreflection/idb-map':
|
||||||
specifier: ^0.3.1
|
specifier: ^0.3.1
|
||||||
version: 0.3.1
|
version: 0.3.1
|
||||||
|
@ -230,8 +230,8 @@ packages:
|
||||||
'@mercuryworkshop/bare-as-module3@2.2.2':
|
'@mercuryworkshop/bare-as-module3@2.2.2':
|
||||||
resolution: {integrity: sha512-Ds8981SmI1DQp1mDCVax1x0I5mH2dR1tYKytW7xTjlnpsfwn3RzQDiVJHUCrpc7cWGCe+7mHCF3EoMjM3hFfvQ==}
|
resolution: {integrity: sha512-Ds8981SmI1DQp1mDCVax1x0I5mH2dR1tYKytW7xTjlnpsfwn3RzQDiVJHUCrpc7cWGCe+7mHCF3EoMjM3hFfvQ==}
|
||||||
|
|
||||||
'@mercuryworkshop/bare-mux@2.0.2':
|
'@mercuryworkshop/bare-mux@2.0.5':
|
||||||
resolution: {integrity: sha512-1rnqFfI29VXi3jrMbkotHV+NnafJ3tpTIuH3Ov/BtVAfZMNGcsCRr3dwEKRWc+mDb0tWdNWC0PnZghgfAlbr+A==}
|
resolution: {integrity: sha512-Uu0JGqqh4K0MuOGnz5QI8DvnW3SGqmdeIP5HET1JKFjMy3HqPpRrIb3UAETZ7TPYjWgR/YJxNp2DmCsPluohCg==}
|
||||||
|
|
||||||
'@mercuryworkshop/epoxy-tls@2.1.2-1':
|
'@mercuryworkshop/epoxy-tls@2.1.2-1':
|
||||||
resolution: {integrity: sha512-qt6UOEZcVMYblFwOKaNAZHoDHSYN3JZ5Z6BvRHKB6zo1DFMub+cPuYQ+Aauib7LmIabYWZi2cMyfO5VsNLDYoA==}
|
resolution: {integrity: sha512-qt6UOEZcVMYblFwOKaNAZHoDHSYN3JZ5Z6BvRHKB6zo1DFMub+cPuYQ+Aauib7LmIabYWZi2cMyfO5VsNLDYoA==}
|
||||||
|
@ -3183,7 +3183,7 @@ snapshots:
|
||||||
dependencies:
|
dependencies:
|
||||||
typescript: 5.5.3
|
typescript: 5.5.3
|
||||||
|
|
||||||
'@mercuryworkshop/bare-mux@2.0.2': {}
|
'@mercuryworkshop/bare-mux@2.0.5': {}
|
||||||
|
|
||||||
'@mercuryworkshop/epoxy-tls@2.1.2-1': {}
|
'@mercuryworkshop/epoxy-tls@2.1.2-1': {}
|
||||||
|
|
||||||
|
|
|
@ -6,7 +6,13 @@ import { createGlobalProxy } from "./global";
|
||||||
import { getOwnPropertyDescriptorHandler } from "./helpers";
|
import { getOwnPropertyDescriptorHandler } from "./helpers";
|
||||||
import { createLocationProxy } from "./location";
|
import { createLocationProxy } from "./location";
|
||||||
import { nativeGetOwnPropertyDescriptor } from "./natives";
|
import { nativeGetOwnPropertyDescriptor } from "./natives";
|
||||||
import { CookieStore, config, decodeUrl, encodeUrl } from "../shared";
|
import {
|
||||||
|
BareClient,
|
||||||
|
CookieStore,
|
||||||
|
config,
|
||||||
|
decodeUrl,
|
||||||
|
encodeUrl,
|
||||||
|
} from "../shared";
|
||||||
import { createWrapFn } from "./shared/wrap";
|
import { createWrapFn } from "./shared/wrap";
|
||||||
import { NavigateEvent } from "./events";
|
import { NavigateEvent } from "./events";
|
||||||
|
|
||||||
|
@ -53,6 +59,7 @@ export class ScramjetClient {
|
||||||
globalProxy: any;
|
globalProxy: any;
|
||||||
locationProxy: any;
|
locationProxy: any;
|
||||||
serviceWorker: ServiceWorkerContainer;
|
serviceWorker: ServiceWorkerContainer;
|
||||||
|
bare: any;
|
||||||
|
|
||||||
descriptors: Record<string, PropertyDescriptor> = {};
|
descriptors: Record<string, PropertyDescriptor> = {};
|
||||||
natives: Record<string, any> = {};
|
natives: Record<string, any> = {};
|
||||||
|
@ -83,7 +90,19 @@ export class ScramjetClient {
|
||||||
this.locationProxy = createLocationProxy(this, global);
|
this.locationProxy = createLocationProxy(this, global);
|
||||||
this.globalProxy = createGlobalProxy(this, global);
|
this.globalProxy = createGlobalProxy(this, global);
|
||||||
this.wrapfn = createWrapFn(this, global);
|
this.wrapfn = createWrapFn(this, global);
|
||||||
|
if (iswindow) {
|
||||||
|
this.bare = new BareClient();
|
||||||
|
} else {
|
||||||
|
this.bare = new BareClient(
|
||||||
|
new Promise((resolve) => {
|
||||||
|
addEventListener("message", (e) => {
|
||||||
|
if (e.data instanceof MessagePort) {
|
||||||
|
resolve(e.data);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
})
|
||||||
|
);
|
||||||
|
}
|
||||||
global[SCRAMJETCLIENT] = this;
|
global[SCRAMJETCLIENT] = this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,17 +1,10 @@
|
||||||
import { iswindow, isworker } from "../..";
|
|
||||||
import { ScramjetClient } from "../../client";
|
import { ScramjetClient } from "../../client";
|
||||||
import { BareClient } from "../../../shared";
|
|
||||||
|
|
||||||
const bare = iswindow && new BareClient();
|
|
||||||
|
|
||||||
export default function (client: ScramjetClient, self: typeof globalThis) {
|
export default function (client: ScramjetClient, self: typeof globalThis) {
|
||||||
// r58 please fix
|
|
||||||
if (isworker) return;
|
|
||||||
|
|
||||||
client.Proxy("WebSocket", {
|
client.Proxy("WebSocket", {
|
||||||
construct(ctx) {
|
construct(ctx) {
|
||||||
ctx.return(
|
ctx.return(
|
||||||
bare.createWebSocket(
|
client.bare.createWebSocket(
|
||||||
ctx.args[0],
|
ctx.args[0],
|
||||||
ctx.args[1],
|
ctx.args[1],
|
||||||
ctx.fn as typeof WebSocket,
|
ctx.fn as typeof WebSocket,
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
import { encodeUrl } from "../../shared";
|
import { encodeUrl, BareMuxConnection } from "../../shared";
|
||||||
import { ScramjetClient } from "../client";
|
import { ScramjetClient } from "../client";
|
||||||
|
|
||||||
export default function (client: ScramjetClient, self: typeof globalThis) {
|
export default function (client: ScramjetClient, self: typeof globalThis) {
|
||||||
client.Proxy("Worker", {
|
client.Proxy("Worker", {
|
||||||
construct({ args }) {
|
construct({ args, call }) {
|
||||||
if (args[0] instanceof URL) args[0] = args[0].href;
|
if (args[0] instanceof URL) args[0] = args[0].href;
|
||||||
if (args[0].startsWith("blob:") || args[0].startsWith("data:")) {
|
if (args[0].startsWith("blob:") || args[0].startsWith("data:")) {
|
||||||
if (args[0].startsWith("blob:")) {
|
if (args[0].startsWith("blob:")) {
|
||||||
|
@ -27,6 +27,13 @@ export default function (client: ScramjetClient, self: typeof globalThis) {
|
||||||
if (args[1] && args[1].type === "module") {
|
if (args[1] && args[1].type === "module") {
|
||||||
args[0] += "&type=module";
|
args[0] += "&type=module";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const worker = call();
|
||||||
|
const conn = new BareMuxConnection();
|
||||||
|
(async () => {
|
||||||
|
const port = await conn.getInnerPort();
|
||||||
|
worker.postMessage(port, [port]);
|
||||||
|
})();
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
export const {
|
export const {
|
||||||
util: { BareClient, ScramjetHeaders },
|
util: { BareClient, ScramjetHeaders, BareMuxConnection },
|
||||||
url: { encodeUrl, decodeUrl },
|
url: { encodeUrl, decodeUrl },
|
||||||
rewrite: {
|
rewrite: {
|
||||||
rewriteCss,
|
rewriteCss,
|
||||||
|
|
|
@ -4,7 +4,7 @@ import { rewriteHtml, rewriteSrcset } from "./rewriters/html";
|
||||||
import { rewriteJs } from "./rewriters/js";
|
import { rewriteJs } from "./rewriters/js";
|
||||||
import { rewriteHeaders } from "./rewriters/headers";
|
import { rewriteHeaders } from "./rewriters/headers";
|
||||||
import { rewriteWorkers } from "./rewriters/worker";
|
import { rewriteWorkers } from "./rewriters/worker";
|
||||||
import { BareClient } from "@mercuryworkshop/bare-mux";
|
import { BareClient, BareMuxConnection } from "@mercuryworkshop/bare-mux";
|
||||||
import { parseDomain } from "parse-domain";
|
import { parseDomain } from "parse-domain";
|
||||||
import { ScramjetHeaders } from "./headers";
|
import { ScramjetHeaders } from "./headers";
|
||||||
import { CookieStore } from "./cookie";
|
import { CookieStore } from "./cookie";
|
||||||
|
@ -14,6 +14,7 @@ self.$scramjet.shared = {
|
||||||
util: {
|
util: {
|
||||||
parseDomain,
|
parseDomain,
|
||||||
BareClient,
|
BareClient,
|
||||||
|
BareMuxConnection,
|
||||||
ScramjetHeaders,
|
ScramjetHeaders,
|
||||||
},
|
},
|
||||||
url: {
|
url: {
|
||||||
|
|
3
src/types.d.ts
vendored
3
src/types.d.ts
vendored
|
@ -11,7 +11,7 @@ import { rewriteJs } from "./shared/rewriters/js";
|
||||||
import { rewriteHeaders } from "./shared/rewriters/headers";
|
import { rewriteHeaders } from "./shared/rewriters/headers";
|
||||||
import { rewriteWorkers } from "./shared/rewriters/worker";
|
import { rewriteWorkers } from "./shared/rewriters/worker";
|
||||||
import type { Codec } from "./codecs";
|
import type { Codec } from "./codecs";
|
||||||
import { BareClient } from "@mercuryworkshop/bare-mux";
|
import { BareClient, BareMuxConnection } from "@mercuryworkshop/bare-mux";
|
||||||
import { parseDomain } from "parse-domain";
|
import { parseDomain } from "parse-domain";
|
||||||
import { ScramjetHeaders } from "./shared/headers";
|
import { ScramjetHeaders } from "./shared/headers";
|
||||||
import { CookieStore } from "./shared/cookie";
|
import { CookieStore } from "./shared/cookie";
|
||||||
|
@ -64,6 +64,7 @@ declare global {
|
||||||
};
|
};
|
||||||
util: {
|
util: {
|
||||||
BareClient: typeof BareClient;
|
BareClient: typeof BareClient;
|
||||||
|
BareMuxConnection: typeof BareMuxConnection;
|
||||||
ScramjetHeaders: typeof ScramjetHeaders;
|
ScramjetHeaders: typeof ScramjetHeaders;
|
||||||
parseDomain: typeof parseDomain;
|
parseDomain: typeof parseDomain;
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue