@witchcraft/nuxt-electron 0.2.8 → 0.3.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/module.json +2 -2
- package/dist/module.mjs +12 -4
- package/dist/runtime/electron/createProxiedProtocolHandler.d.ts +18 -2
- package/dist/runtime/electron/createProxiedProtocolHandler.js +28 -2
- package/package.json +5 -6
- package/src/module.ts +13 -4
- package/src/runtime/electron/createProxiedProtocolHandler.ts +43 -3
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
1
|
import type { Protocol } from "electron";
|
|
2
|
-
|
|
3
|
-
|
|
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,
|
|
@@ -50,3 +65,4 @@ routeProxies: Record<string, string | {
|
|
|
50
65
|
*/
|
|
51
66
|
errorPage?: string;
|
|
52
67
|
}): void;
|
|
68
|
+
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);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@witchcraft/nuxt-electron",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
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 = [
|
|
@@ -6,9 +6,48 @@ 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) {
|