@mercuryworkshop/epoxy-transport 1.0.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/dist/main.d.ts ADDED
@@ -0,0 +1,15 @@
1
+ import { BareHeaders, TransferrableResponse, type BareTransport } from "@mercuryworkshop/bare-mux";
2
+ export declare class TLSClient implements BareTransport {
3
+ canstart: boolean;
4
+ epxclient: Awaited<ReturnType<any>>["EpoxyClient"]["prototype"];
5
+ wsproxy: string | null;
6
+ constructor({ wsproxy, mux }: {
7
+ wsproxy: any;
8
+ mux: any;
9
+ });
10
+ init(): Promise<void>;
11
+ ready: boolean;
12
+ meta(): Promise<void>;
13
+ request(remote: URL, method: string, body: BodyInit | null, headers: BareHeaders, signal: AbortSignal | undefined): Promise<TransferrableResponse>;
14
+ connect(url: URL, origin: string, protocols: string[], requestHeaders: BareHeaders, onopen: (protocol: string) => void, onmessage: (data: Blob | ArrayBuffer | string) => void, onclose: (code: number, reason: string) => void, onerror: (error: string) => void): (data: Blob | ArrayBuffer | string) => void;
15
+ }
@@ -0,0 +1,94 @@
1
+ // import { dtsPlugin } from "esbuild-plugin-d.ts";
2
+ import { build } from "esbuild";
3
+ import path from 'node:path'
4
+ import fs from 'node:fs'
5
+ import { umdWrapper } from "esbuild-plugin-umd-wrapper";
6
+
7
+ const umdWrapperOptions = {
8
+ libraryName: "EpxMod", // default is unset
9
+ external: "inherit", // <= default
10
+ amdLoaderName: "define" // <= default
11
+ }
12
+
13
+ let wasmPlugin = {
14
+ name: 'wasm',
15
+ setup(build) {
16
+ // Resolve ".wasm" files to a path with a namespace
17
+ build.onResolve({ filter: /\.wasm$/ }, args => {
18
+ // If this is the import inside the stub module, import the
19
+ // binary itself. Put the path in the "wasm-binary" namespace
20
+ // to tell our binary load callback to load the binary file.
21
+ if (args.namespace === 'wasm-stub') {
22
+ return {
23
+ path: args.path,
24
+ namespace: 'wasm-binary',
25
+ }
26
+ }
27
+
28
+ // Otherwise, generate the JavaScript stub module for this
29
+ // ".wasm" file. Put it in the "wasm-stub" namespace to tell
30
+ // our stub load callback to fill it with JavaScript.
31
+ //
32
+ // Resolve relative paths to absolute paths here since this
33
+ // resolve callback is given "resolveDir", the directory to
34
+ // resolve imports against.
35
+ if (args.resolveDir === '') {
36
+ return // Ignore unresolvable paths
37
+ }
38
+ return {
39
+ path: path.isAbsolute(args.path) ? args.path : path.join(args.resolveDir, args.path),
40
+ namespace: 'wasm-stub',
41
+ }
42
+ })
43
+
44
+ // Virtual modules in the "wasm-stub" namespace are filled with
45
+ // the JavaScript code for compiling the WebAssembly binary. The
46
+ // binary itself is imported from a second virtual module.
47
+ build.onLoad({ filter: /.*/, namespace: 'wasm-stub' }, async (args) => ({
48
+ contents: `import wasm from ${JSON.stringify(args.path)}
49
+ export default (imports) =>
50
+ WebAssembly.instantiate(wasm, imports).then(
51
+ result => result.instance.exports)`,
52
+ }))
53
+
54
+ // Virtual modules in the "wasm-binary" namespace contain the
55
+ // actual bytes of the WebAssembly file. This uses esbuild's
56
+ // built-in "binary" loader instead of manually embedding the
57
+ // binary data inside JavaScript code ourselves.
58
+ build.onLoad({ filter: /.*/, namespace: 'wasm-binary' }, async (args) => ({
59
+ contents: await fs.promises.readFile(args.path),
60
+ loader: 'binary',
61
+ }))
62
+ },
63
+ }
64
+ let makeAllPackagesExternalPlugin = {
65
+ name: 'make-all-packages-external',
66
+ setup(build) {
67
+
68
+ // build.onResolve({ filter: /protocol/ }, args => ({ external: false }))
69
+ let filter = /^[^.\/]|^\.[^.\/]|^\.\.[^\/]/ // Must not start with "/" or "./" or "../"
70
+ build.onResolve({ filter }, args => ({ path: args.path, external: true }))
71
+ },
72
+ }
73
+
74
+ build({
75
+ bundle: true,
76
+ format: "esm",
77
+ entryPoints: [`./src/main.ts`],
78
+ outfile: `./dist/index.mjs`,
79
+ plugins: [wasmPlugin],
80
+ external: ["fs", "ws", "path"],
81
+ })
82
+
83
+ build({
84
+ bundle: true,
85
+ format: "cjs",
86
+ entryPoints: [`./src/main.ts`],
87
+ outfile: `./dist/index.cjs`,
88
+ external: ["fs", "ws", "path"],
89
+ plugins: [wasmPlugin, umdWrapper(umdWrapperOptions)]
90
+ // plugins: [dtsPlugin({
91
+ // outDir: `./dist/`,
92
+ // tsconfig: "tsconfig.json"
93
+ // })]
94
+ })
package/lib/index.cjs ADDED
@@ -0,0 +1,7 @@
1
+ 'use strict';
2
+
3
+ const { resolve } = require('node:path');
4
+
5
+ const epoxyPath = resolve(__dirname, '..', 'dist');
6
+
7
+ exports.epoxyPath = epoxyPath;
package/package.json ADDED
@@ -0,0 +1,36 @@
1
+ {
2
+ "name": "@mercuryworkshop/epoxy-transport",
3
+ "version": "1.0.0",
4
+ "description": "a bare transport that implements end-to-end encryption with epoxy-tls and wisp",
5
+ "main": "./dist/index.mjs",
6
+ "keywords": [],
7
+ "author": "",
8
+ "type": "module",
9
+ "license": "LGPL",
10
+ "scripts": {
11
+ "build": "node esbuild.bundle.mjs"
12
+ },
13
+ "dependencies": {
14
+ "@mercuryworkshop/epoxy-tls": "^1.1.1",
15
+ "esbuild-plugin-umd-wrapper": "^2.0.0",
16
+ "rollup": "^4.12.0",
17
+ "rollup-plugin-node-resolve": "^5.2.0",
18
+ "rollup-plugin-typescript2": "^0.36.0",
19
+ "ws": "8.16.0"
20
+ },
21
+ "devDependencies": {
22
+ "esbuild": "^0.19.11",
23
+ "esbuild-plugin-d.ts": "^1.2.2"
24
+ },
25
+ "exports": {
26
+ ".": {
27
+ "browser": {
28
+ "import": "./dist/index.mjs",
29
+ "require": "./dist/index.cjs"
30
+ },
31
+ "node": {
32
+ "require": "./lib/index.cjs"
33
+ }
34
+ }
35
+ }
36
+ }
package/src/main.ts ADDED
@@ -0,0 +1,63 @@
1
+ import { BareHeaders, BareResponse, TransferrableResponse, type BareTransport } from "@mercuryworkshop/bare-mux";
2
+ import epoxy from "@mercuryworkshop/epoxy-tls";
3
+ export class EpoxyClient implements BareTransport {
4
+ canstart = true;
5
+ epxclient: Awaited<ReturnType<any>>["EpoxyClient"]["prototype"] = null!;
6
+ wisp: string;
7
+
8
+ constructor({ wisp }) {
9
+ this.wisp = wisp;
10
+ }
11
+ async init() {
12
+ let { EpoxyClient } = await epoxy();
13
+ this.epxclient = await new EpoxyClient(this.wisp, navigator.userAgent, 10);
14
+
15
+ this.ready = true;
16
+ }
17
+ ready = false;
18
+ async meta() { }
19
+
20
+ async request(
21
+ remote: URL,
22
+ method: string,
23
+ body: BodyInit | null,
24
+ headers: BareHeaders,
25
+ signal: AbortSignal | undefined
26
+ ): Promise<TransferrableResponse> {
27
+ if (body instanceof Blob)
28
+ body = await body.arrayBuffer();
29
+ let payload = await this.epxclient.fetch(remote.href, { method, body, headers, redirect: "manual" });
30
+
31
+ return {
32
+ body: payload.body!,
33
+ headers: (payload as any).rawHeaders,
34
+ status: payload.status,
35
+ statusText: payload.statusText,
36
+ };
37
+ }
38
+
39
+ connect(
40
+ url: URL,
41
+ origin: string,
42
+ protocols: string[],
43
+ requestHeaders: BareHeaders,
44
+ onopen: (protocol: string) => void,
45
+ onmessage: (data: Blob | ArrayBuffer | string) => void,
46
+ onclose: (code: number, reason: string) => void,
47
+ onerror: (error: string) => void,
48
+ ): (data: Blob | ArrayBuffer | string) => void {
49
+ let epsocket = this.epxclient.connect_ws(
50
+ onopen,
51
+ onclose,
52
+ onerror,
53
+ (data: Uint8Array | string) => data instanceof Uint8Array ? onmessage(data.buffer) : onmessage(data),
54
+ url.href,
55
+ protocols,
56
+ origin,
57
+ );
58
+
59
+ return async (data) => {
60
+ (await epsocket).send(data);
61
+ };
62
+ }
63
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,16 @@
1
+ {
2
+ "compilerOptions": {
3
+ "outDir": "build",
4
+ "sourceMap": true,
5
+ "target": "ES2022",
6
+ "lib": ["DOM", "DOM.Iterable", "ES2019"],
7
+ "strict": true,
8
+ "stripInternal": true,
9
+ "module": "ES6",
10
+ "moduleResolution": "node",
11
+ "allowSyntheticDefaultImports": true,
12
+ "declaration": true,
13
+ "noImplicitAny": false
14
+ },
15
+ "include": ["src"]
16
+ }