@witchcraft/nuxt-electron 0.2.8 → 0.3.1
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 +1 -1
- package/dist/module.json +2 -2
- package/dist/module.mjs +12 -4
- package/dist/runtime/electron/createProxiedProtocolHandler.d.ts +26 -4
- package/dist/runtime/electron/createProxiedProtocolHandler.js +34 -6
- package/package.json +5 -6
- package/src/module.ts +13 -4
- package/src/runtime/electron/createProxiedProtocolHandler.ts +60 -8
package/README.md
CHANGED
|
@@ -247,7 +247,7 @@ Proxying server only page routes seems possible but complicated because each rou
|
|
|
247
247
|
|
|
248
248
|
### Runtime Config
|
|
249
249
|
|
|
250
|
-
Anywhere in electron's
|
|
250
|
+
Anywhere in electron's main files you can also now use nuxt's public runtime config using the special version from the module (only the public part, you don't want to expose your server secrets to the electron app at all). Module does some magic to strinfigy it into the `ELECTRON_RUNTIME_CONFIG` env variable and read it back out:
|
|
251
251
|
|
|
252
252
|
```ts
|
|
253
253
|
import { useNuxtRuntimeConfig } from "@witchcraft/nuxt-electron/electron"
|
package/dist/module.json
CHANGED
package/dist/module.mjs
CHANGED
|
@@ -7,7 +7,6 @@ import fs from 'node:fs/promises';
|
|
|
7
7
|
import path from 'node:path';
|
|
8
8
|
import { startup, build } from 'vite-plugin-electron';
|
|
9
9
|
import { notBundle } from 'vite-plugin-electron/plugin';
|
|
10
|
-
import { externalizeDeps } from 'vite-plugin-externalize-deps';
|
|
11
10
|
|
|
12
11
|
startup.exit = async () => {
|
|
13
12
|
if ("electronApp" in process) {
|
|
@@ -153,6 +152,13 @@ const module$1 = defineNuxtModule({
|
|
|
153
152
|
if (maybeWatchers || started) return;
|
|
154
153
|
started = true;
|
|
155
154
|
const viteConfig = await viteConfigPromise;
|
|
155
|
+
const externalDeps = [];
|
|
156
|
+
const appPackageJson = JSON.parse(await fs.readFile(path.join(nuxt.options.rootDir, "package.json"), "utf8"));
|
|
157
|
+
for (const depSection of ["dependencies", "optionalDependencies", "peerDependencies"]) {
|
|
158
|
+
for (const dep of Object.keys(appPackageJson[depSection] ?? {})) {
|
|
159
|
+
externalDeps.push(dep, new RegExp(`^${dep}(?:/.+)?$`));
|
|
160
|
+
}
|
|
161
|
+
}
|
|
156
162
|
const electronRuntimeConfig = defu(
|
|
157
163
|
options.electronOnlyRuntimeConfig,
|
|
158
164
|
nuxt.options.runtimeConfig.public
|
|
@@ -210,14 +216,16 @@ const module$1 = defineNuxtModule({
|
|
|
210
216
|
{
|
|
211
217
|
build: {
|
|
212
218
|
outDir: electronBuildDir,
|
|
213
|
-
minify: false
|
|
219
|
+
minify: false,
|
|
220
|
+
rollupOptions: {
|
|
221
|
+
external: externalDeps
|
|
222
|
+
}
|
|
214
223
|
},
|
|
215
224
|
define: electronVariables,
|
|
216
225
|
resolve: {
|
|
217
226
|
alias: nuxt.options.alias,
|
|
218
227
|
extensions: [".mjs", ".js", ".ts", ".jsx", ".tsx", ".json", ".vue"]
|
|
219
|
-
}
|
|
220
|
-
plugins: [externalizeDeps()]
|
|
228
|
+
}
|
|
221
229
|
}
|
|
222
230
|
);
|
|
223
231
|
const electronCliArgs = [
|
|
@@ -1,6 +1,21 @@
|
|
|
1
|
-
import type { Protocol } from "electron";
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import type { Protocol, Session } from "electron";
|
|
2
|
+
/**
|
|
3
|
+
* Stores file paths and proxy route URLs with an expiration time.
|
|
4
|
+
* Expired entries are lazily deleted on read and periodically cleaned on write.
|
|
5
|
+
*/
|
|
6
|
+
declare class TtlCache<TK, TV> {
|
|
7
|
+
#private;
|
|
8
|
+
map: Map<TK, {
|
|
9
|
+
value: TV;
|
|
10
|
+
expiry: number;
|
|
11
|
+
}>;
|
|
12
|
+
ttlMs: number;
|
|
13
|
+
constructor(ttlMs?: number);
|
|
14
|
+
get(key: TK): TV | undefined;
|
|
15
|
+
set(key: TK, value: TV): void;
|
|
16
|
+
}
|
|
17
|
+
export declare const cache: TtlCache<string, string | undefined>;
|
|
18
|
+
export declare const proxyCache: TtlCache<string, string | undefined>;
|
|
4
19
|
export declare function createProxiedProtocolHandler(
|
|
5
20
|
/** `protocol` or `session.protocol` depending on if you're using paritions with your windows or not, regular `protocol` only registers the handler for the default parition. */
|
|
6
21
|
protocol: Protocol, protocolName: string | undefined, basePath: string,
|
|
@@ -35,7 +50,7 @@ routeProxies: Record<string, string | {
|
|
|
35
50
|
url: never;
|
|
36
51
|
convertPath: never;
|
|
37
52
|
ignore: true;
|
|
38
|
-
}>, { logger, errorPage }?: {
|
|
53
|
+
}>, { logger, errorPage, session }?: {
|
|
39
54
|
/**
|
|
40
55
|
* Optional logger. It's suggested you not pass this unless you're traceging in dev mode as a lot of requests can be made.
|
|
41
56
|
*/
|
|
@@ -49,4 +64,11 @@ routeProxies: Record<string, string | {
|
|
|
49
64
|
* The module forces nuxt to generate it as it's not rendered in build mode. This is only used for 404 errors. The 400 error for bad requests still just returns Bad Request for now. This might become more flexible in the future.
|
|
50
65
|
*/
|
|
51
66
|
errorPage?: string;
|
|
67
|
+
/**
|
|
68
|
+
* The window session the requests should be made from, i.e. the same session whose `protocol` you passed first.
|
|
69
|
+
*
|
|
70
|
+
* `net.fetch` always uses the default session, so without this, cookies, cache and proxy settings of a partitioned window session are bypassed, and requests for a partitioned window go out with an empty cookie jar.
|
|
71
|
+
*/
|
|
72
|
+
session?: Session;
|
|
52
73
|
}): void;
|
|
74
|
+
export {};
|
|
@@ -4,8 +4,34 @@ import { net } from "electron";
|
|
|
4
4
|
import fs from "node:fs/promises";
|
|
5
5
|
import path from "node:path";
|
|
6
6
|
import { pathToFileURL } from "node:url";
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
class TtlCache {
|
|
8
|
+
map = /* @__PURE__ */ new Map();
|
|
9
|
+
ttlMs;
|
|
10
|
+
constructor(ttlMs = 5 * 60 * 1e3) {
|
|
11
|
+
this.ttlMs = ttlMs;
|
|
12
|
+
}
|
|
13
|
+
get(key) {
|
|
14
|
+
const entry = this.map.get(key);
|
|
15
|
+
if (!entry) return void 0;
|
|
16
|
+
if (Date.now() > entry.expiry) {
|
|
17
|
+
this.map.delete(key);
|
|
18
|
+
return void 0;
|
|
19
|
+
}
|
|
20
|
+
return entry.value;
|
|
21
|
+
}
|
|
22
|
+
set(key, value) {
|
|
23
|
+
this.map.set(key, { value, expiry: Date.now() + this.ttlMs });
|
|
24
|
+
if (this.map.size % 100 === 0) this.#cleanup();
|
|
25
|
+
}
|
|
26
|
+
#cleanup() {
|
|
27
|
+
const now = Date.now();
|
|
28
|
+
for (const [key, entry] of this.map) {
|
|
29
|
+
if (now > entry.expiry) this.map.delete(key);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
export const cache = new TtlCache();
|
|
34
|
+
export const proxyCache = new TtlCache();
|
|
9
35
|
const proxiedProperties = ["headers", "destination", "referrer", "referrerPolicy", "mode", "credentials", "cache", "redirect", "integrity", "keepalive"];
|
|
10
36
|
async function getPathToServe(originalPath, pathWithIndexHtml, requestUrl, logger) {
|
|
11
37
|
const stats = await fs.stat(originalPath).catch(() => void 0);
|
|
@@ -41,12 +67,14 @@ async function getFromCacheOr(cache2, key, fn, logger) {
|
|
|
41
67
|
}
|
|
42
68
|
export function createProxiedProtocolHandler(protocol, protocolName = "app", basePath, routeProxies, {
|
|
43
69
|
logger,
|
|
44
|
-
errorPage = "404.html"
|
|
70
|
+
errorPage = "404.html",
|
|
71
|
+
session
|
|
45
72
|
} = {}) {
|
|
46
73
|
const routeProxyKeys = keys(routeProxies);
|
|
47
74
|
if (protocol.isProtocolHandled(protocolName)) {
|
|
48
75
|
throw new Error(`Protocol ${protocolName} is already handled.`);
|
|
49
76
|
}
|
|
77
|
+
const netFetch = session ? (input, init) => session.fetch(input, init) : net.fetch;
|
|
50
78
|
let errorPage404;
|
|
51
79
|
protocol.handle(protocolName, async (request) => {
|
|
52
80
|
errorPage404 ??= await getFromCacheOr(cache, "404.html", (key) => getPathToServe(path.join(basePath, errorPage), "404.html", key, logger), logger);
|
|
@@ -85,7 +113,7 @@ export function createProxiedProtocolHandler(protocol, protocolName = "app", bas
|
|
|
85
113
|
finalPath: cachedProxyResult
|
|
86
114
|
});
|
|
87
115
|
}
|
|
88
|
-
return
|
|
116
|
+
return netFetch(cachedProxyResult, {
|
|
89
117
|
...pick(request, proxiedProperties)
|
|
90
118
|
});
|
|
91
119
|
}
|
|
@@ -139,7 +167,7 @@ export function createProxiedProtocolHandler(protocol, protocolName = "app", bas
|
|
|
139
167
|
errorPath: pathToFileURL(errorPage404).toString()
|
|
140
168
|
});
|
|
141
169
|
}
|
|
142
|
-
const res = await
|
|
170
|
+
const res = await netFetch(pathToFileURL(errorPage404).toString(), {
|
|
143
171
|
// see below
|
|
144
172
|
bypassCustomProtocolHandlers: true
|
|
145
173
|
}).catch((err) => err);
|
|
@@ -155,7 +183,7 @@ export function createProxiedProtocolHandler(protocol, protocolName = "app", bas
|
|
|
155
183
|
originalPath
|
|
156
184
|
});
|
|
157
185
|
}
|
|
158
|
-
const response = await
|
|
186
|
+
const response = await netFetch(finalPath, {
|
|
159
187
|
// avoid infinite loop if protocolName protocol is "file"
|
|
160
188
|
// or file is otherwise handled
|
|
161
189
|
bypassCustomProtocolHandlers: true
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@witchcraft/nuxt-electron",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.1",
|
|
4
4
|
"description": "Nuxt module for working with electron.",
|
|
5
5
|
"repository": "https://github.com/witchcraftjs/nuxt-electron",
|
|
6
6
|
"license": "MIT",
|
|
@@ -25,15 +25,14 @@
|
|
|
25
25
|
"genDevDesktop.js"
|
|
26
26
|
],
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"@alanscodelog/utils": "^6.
|
|
28
|
+
"@alanscodelog/utils": "^6.3.0",
|
|
29
29
|
"@nuxt/kit": "^4.3.1",
|
|
30
|
-
"@witchcraft/nuxt-utils": "^0.3.
|
|
30
|
+
"@witchcraft/nuxt-utils": "^0.3.10",
|
|
31
31
|
"@witchcraft/ui": "^0.5.0",
|
|
32
32
|
"defu": "^6.1.4",
|
|
33
33
|
"electron": "^37.10.3",
|
|
34
34
|
"es-toolkit": "^1.44.0",
|
|
35
|
-
"vite-plugin-electron": "^0.29.0"
|
|
36
|
-
"vite-plugin-externalize-deps": "^0.9.0"
|
|
35
|
+
"vite-plugin-electron": "^0.29.0"
|
|
37
36
|
},
|
|
38
37
|
"peerDependencies": {
|
|
39
38
|
"@witchcraft/ui": "^0.5.0",
|
|
@@ -52,7 +51,7 @@
|
|
|
52
51
|
"@alanscodelog/semantic-release-config": "^6.0.2",
|
|
53
52
|
"@alanscodelog/tsconfigs": "^6.3.0",
|
|
54
53
|
"@nuxt/eslint-config": "^1.15.1",
|
|
55
|
-
"@nuxt/module-builder": "^1.0.
|
|
54
|
+
"@nuxt/module-builder": "^1.0.3",
|
|
56
55
|
"@nuxt/schema": "^4.3.1",
|
|
57
56
|
"@nuxt/test-utils": "^3.23.0",
|
|
58
57
|
"@types/node": "latest",
|
package/src/module.ts
CHANGED
|
@@ -18,7 +18,6 @@ import type { RollupOutput, RollupWatcher } from "rollup"
|
|
|
18
18
|
import type { ViteDevServer } from "vite"
|
|
19
19
|
import { build, type ElectronOptions, startup } from "vite-plugin-electron"
|
|
20
20
|
import { notBundle } from "vite-plugin-electron/plugin"
|
|
21
|
-
import { externalizeDeps } from "vite-plugin-externalize-deps"
|
|
22
21
|
|
|
23
22
|
|
|
24
23
|
// https://github.com/electron-vite/vite-plugin-electron/issues/251#issuecomment-2360153184
|
|
@@ -308,6 +307,14 @@ export default defineNuxtModule<ModuleOptions>({
|
|
|
308
307
|
|
|
309
308
|
const viteConfig = await viteConfigPromise
|
|
310
309
|
|
|
310
|
+
const externalDeps: (string | RegExp)[] = []
|
|
311
|
+
const appPackageJson = JSON.parse(await fs.readFile(path.join(nuxt.options.rootDir, "package.json"), "utf8"))
|
|
312
|
+
for (const depSection of ["dependencies", "optionalDependencies", "peerDependencies"] as const) {
|
|
313
|
+
for (const dep of Object.keys(appPackageJson[depSection] ?? {})) {
|
|
314
|
+
externalDeps.push(dep, new RegExp(`^${dep}(?:/.+)?$`))
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
|
|
311
318
|
const electronRuntimeConfig = defu(
|
|
312
319
|
options.electronOnlyRuntimeConfig,
|
|
313
320
|
nuxt.options.runtimeConfig.public
|
|
@@ -368,14 +375,16 @@ export default defineNuxtModule<ModuleOptions>({
|
|
|
368
375
|
{
|
|
369
376
|
build: {
|
|
370
377
|
outDir: electronBuildDir,
|
|
371
|
-
minify: false
|
|
378
|
+
minify: false,
|
|
379
|
+
rollupOptions: {
|
|
380
|
+
external: externalDeps
|
|
381
|
+
}
|
|
372
382
|
},
|
|
373
383
|
define: electronVariables,
|
|
374
384
|
resolve: {
|
|
375
385
|
alias: nuxt.options.alias,
|
|
376
386
|
extensions: [".mjs", ".js", ".ts", ".jsx", ".tsx", ".json", ".vue"]
|
|
377
|
-
}
|
|
378
|
-
plugins: [externalizeDeps() as any]
|
|
387
|
+
}
|
|
379
388
|
})
|
|
380
389
|
|
|
381
390
|
const electronCliArgs = [
|
|
@@ -1,14 +1,53 @@
|
|
|
1
1
|
import { keys } from "@alanscodelog/utils/keys"
|
|
2
2
|
import { pick } from "@alanscodelog/utils/pick"
|
|
3
|
-
import type { Protocol } from "electron"
|
|
3
|
+
import type { Protocol, Session } from "electron"
|
|
4
4
|
import { net } from "electron"
|
|
5
5
|
import fs from "node:fs/promises"
|
|
6
6
|
import path from "node:path"
|
|
7
7
|
import { pathToFileURL } from "node:url"
|
|
8
8
|
|
|
9
|
+
// what we're storing is pretty small (Record<string, path>) but just to be safe
|
|
10
|
+
/**
|
|
11
|
+
* Stores file paths and proxy route URLs with an expiration time.
|
|
12
|
+
* Expired entries are lazily deleted on read and periodically cleaned on write.
|
|
13
|
+
*/
|
|
14
|
+
class TtlCache<TK, TV> {
|
|
15
|
+
map = new Map<TK, { value: TV, expiry: number }>()
|
|
16
|
+
ttlMs: number
|
|
9
17
|
|
|
10
|
-
|
|
11
|
-
|
|
18
|
+
constructor(
|
|
19
|
+
/* 5 minutes by default */
|
|
20
|
+
ttlMs: number = 5 * 60 * 1000
|
|
21
|
+
) {
|
|
22
|
+
this.ttlMs = ttlMs
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
get(key: TK): TV | undefined {
|
|
26
|
+
const entry = this.map.get(key)
|
|
27
|
+
if (!entry) return undefined
|
|
28
|
+
if (Date.now() > entry.expiry) {
|
|
29
|
+
this.map.delete(key)
|
|
30
|
+
return undefined
|
|
31
|
+
}
|
|
32
|
+
return entry.value
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
set(key: TK, value: TV) {
|
|
36
|
+
this.map.set(key, { value, expiry: Date.now() + this.ttlMs })
|
|
37
|
+
// clean up expired entries (every 100 writes) to prevent unbounded growth
|
|
38
|
+
if (this.map.size % 100 === 0) this.#cleanup()
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
#cleanup() {
|
|
42
|
+
const now = Date.now()
|
|
43
|
+
for (const [key, entry] of this.map) {
|
|
44
|
+
if (now > entry.expiry) this.map.delete(key)
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export const cache = new TtlCache<string, string | undefined>()
|
|
50
|
+
export const proxyCache = new TtlCache<string, string | undefined>()
|
|
12
51
|
const proxiedProperties: (keyof Request)[] = ["headers", "destination", "referrer", "referrerPolicy", "mode", "credentials", "cache", "redirect", "integrity", "keepalive"]
|
|
13
52
|
|
|
14
53
|
async function getPathToServe(
|
|
@@ -43,7 +82,8 @@ async function getPathToServe(
|
|
|
43
82
|
return pathToServe
|
|
44
83
|
}
|
|
45
84
|
|
|
46
|
-
|
|
85
|
+
type Cacheable = { get(key: string): string | undefined, set(key: string, value: string | undefined): void }
|
|
86
|
+
async function getFromCacheOr(cache: Cacheable, key: string, fn: (key: string) => Promise<string | undefined> | string | undefined, logger?: { trace: (...args: any[]) => void }): Promise<string | undefined> {
|
|
47
87
|
let result = cache.get(key)
|
|
48
88
|
if (result) {
|
|
49
89
|
if (logger) {
|
|
@@ -106,7 +146,8 @@ export function createProxiedProtocolHandler(
|
|
|
106
146
|
>,
|
|
107
147
|
{
|
|
108
148
|
logger,
|
|
109
|
-
errorPage = "404.html"
|
|
149
|
+
errorPage = "404.html",
|
|
150
|
+
session
|
|
110
151
|
}: {
|
|
111
152
|
/**
|
|
112
153
|
* Optional logger. It's suggested you not pass this unless you're traceging in dev mode as a lot of requests can be made.
|
|
@@ -123,12 +164,23 @@ export function createProxiedProtocolHandler(
|
|
|
123
164
|
*/
|
|
124
165
|
errorPage?: string
|
|
125
166
|
|
|
167
|
+
/**
|
|
168
|
+
* The window session the requests should be made from, i.e. the same session whose `protocol` you passed first.
|
|
169
|
+
*
|
|
170
|
+
* `net.fetch` always uses the default session, so without this, cookies, cache and proxy settings of a partitioned window session are bypassed, and requests for a partitioned window go out with an empty cookie jar.
|
|
171
|
+
*/
|
|
172
|
+
session?: Session
|
|
173
|
+
|
|
126
174
|
} = {}
|
|
127
175
|
): void {
|
|
128
176
|
const routeProxyKeys = keys(routeProxies)
|
|
129
177
|
if (protocol.isProtocolHandled(protocolName)) {
|
|
130
178
|
throw new Error(`Protocol ${protocolName} is already handled.`)
|
|
131
179
|
}
|
|
180
|
+
// net.fetch is hardcoded to the default session, a partitioned window needs its own
|
|
181
|
+
const netFetch: typeof net.fetch = session
|
|
182
|
+
? (input, init) => session.fetch(input as Parameters<Session["fetch"]>[0], init)
|
|
183
|
+
: net.fetch
|
|
132
184
|
|
|
133
185
|
let errorPage404: string | undefined
|
|
134
186
|
// note that while it would be nice to do protocol.isProtocolRegistered
|
|
@@ -182,7 +234,7 @@ export function createProxiedProtocolHandler(
|
|
|
182
234
|
finalPath: cachedProxyResult
|
|
183
235
|
})
|
|
184
236
|
}
|
|
185
|
-
return
|
|
237
|
+
return netFetch(cachedProxyResult, {
|
|
186
238
|
...pick(request, proxiedProperties)
|
|
187
239
|
})
|
|
188
240
|
}
|
|
@@ -236,7 +288,7 @@ export function createProxiedProtocolHandler(
|
|
|
236
288
|
errorPath: pathToFileURL(errorPage404).toString()
|
|
237
289
|
})
|
|
238
290
|
}
|
|
239
|
-
const res = await
|
|
291
|
+
const res = await netFetch(pathToFileURL(errorPage404).toString(), {
|
|
240
292
|
// see below
|
|
241
293
|
bypassCustomProtocolHandlers: true
|
|
242
294
|
})
|
|
@@ -255,7 +307,7 @@ export function createProxiedProtocolHandler(
|
|
|
255
307
|
})
|
|
256
308
|
}
|
|
257
309
|
|
|
258
|
-
const response = await
|
|
310
|
+
const response = await netFetch(finalPath, {
|
|
259
311
|
// avoid infinite loop if protocolName protocol is "file"
|
|
260
312
|
// or file is otherwise handled
|
|
261
313
|
bypassCustomProtocolHandlers: true
|