@witchcraft/nuxt-electron 0.3.0 → 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 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 renderer files you can also now use nuxt's public runtime config (only the public, you don't want to expose your server secrets to the electron app at all).
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
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "electron",
3
3
  "configKey": "electron",
4
- "version": "0.3.0",
4
+ "version": "0.3.1",
5
5
  "builder": {
6
6
  "@nuxt/module-builder": "1.0.3",
7
7
  "unbuild": "3.6.1"
@@ -1,4 +1,4 @@
1
- import type { Protocol } from "electron";
1
+ import type { Protocol, Session } from "electron";
2
2
  /**
3
3
  * Stores file paths and proxy route URLs with an expiration time.
4
4
  * Expired entries are lazily deleted on read and periodically cleaned on write.
@@ -50,7 +50,7 @@ routeProxies: Record<string, string | {
50
50
  url: never;
51
51
  convertPath: never;
52
52
  ignore: true;
53
- }>, { logger, errorPage }?: {
53
+ }>, { logger, errorPage, session }?: {
54
54
  /**
55
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.
56
56
  */
@@ -64,5 +64,11 @@ routeProxies: Record<string, string | {
64
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.
65
65
  */
66
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;
67
73
  }): void;
68
74
  export {};
@@ -67,12 +67,14 @@ async function getFromCacheOr(cache2, key, fn, logger) {
67
67
  }
68
68
  export function createProxiedProtocolHandler(protocol, protocolName = "app", basePath, routeProxies, {
69
69
  logger,
70
- errorPage = "404.html"
70
+ errorPage = "404.html",
71
+ session
71
72
  } = {}) {
72
73
  const routeProxyKeys = keys(routeProxies);
73
74
  if (protocol.isProtocolHandled(protocolName)) {
74
75
  throw new Error(`Protocol ${protocolName} is already handled.`);
75
76
  }
77
+ const netFetch = session ? (input, init) => session.fetch(input, init) : net.fetch;
76
78
  let errorPage404;
77
79
  protocol.handle(protocolName, async (request) => {
78
80
  errorPage404 ??= await getFromCacheOr(cache, "404.html", (key) => getPathToServe(path.join(basePath, errorPage), "404.html", key, logger), logger);
@@ -111,7 +113,7 @@ export function createProxiedProtocolHandler(protocol, protocolName = "app", bas
111
113
  finalPath: cachedProxyResult
112
114
  });
113
115
  }
114
- return net.fetch(cachedProxyResult, {
116
+ return netFetch(cachedProxyResult, {
115
117
  ...pick(request, proxiedProperties)
116
118
  });
117
119
  }
@@ -165,7 +167,7 @@ export function createProxiedProtocolHandler(protocol, protocolName = "app", bas
165
167
  errorPath: pathToFileURL(errorPage404).toString()
166
168
  });
167
169
  }
168
- const res = await net.fetch(pathToFileURL(errorPage404).toString(), {
170
+ const res = await netFetch(pathToFileURL(errorPage404).toString(), {
169
171
  // see below
170
172
  bypassCustomProtocolHandlers: true
171
173
  }).catch((err) => err);
@@ -181,7 +183,7 @@ export function createProxiedProtocolHandler(protocol, protocolName = "app", bas
181
183
  originalPath
182
184
  });
183
185
  }
184
- const response = await net.fetch(finalPath, {
186
+ const response = await netFetch(finalPath, {
185
187
  // avoid infinite loop if protocolName protocol is "file"
186
188
  // or file is otherwise handled
187
189
  bypassCustomProtocolHandlers: true
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@witchcraft/nuxt-electron",
3
- "version": "0.3.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",
@@ -1,6 +1,6 @@
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"
@@ -146,7 +146,8 @@ export function createProxiedProtocolHandler(
146
146
  >,
147
147
  {
148
148
  logger,
149
- errorPage = "404.html"
149
+ errorPage = "404.html",
150
+ session
150
151
  }: {
151
152
  /**
152
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.
@@ -163,12 +164,23 @@ export function createProxiedProtocolHandler(
163
164
  */
164
165
  errorPage?: string
165
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
+
166
174
  } = {}
167
175
  ): void {
168
176
  const routeProxyKeys = keys(routeProxies)
169
177
  if (protocol.isProtocolHandled(protocolName)) {
170
178
  throw new Error(`Protocol ${protocolName} is already handled.`)
171
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
172
184
 
173
185
  let errorPage404: string | undefined
174
186
  // note that while it would be nice to do protocol.isProtocolRegistered
@@ -222,7 +234,7 @@ export function createProxiedProtocolHandler(
222
234
  finalPath: cachedProxyResult
223
235
  })
224
236
  }
225
- return net.fetch(cachedProxyResult, {
237
+ return netFetch(cachedProxyResult, {
226
238
  ...pick(request, proxiedProperties)
227
239
  })
228
240
  }
@@ -276,7 +288,7 @@ export function createProxiedProtocolHandler(
276
288
  errorPath: pathToFileURL(errorPage404).toString()
277
289
  })
278
290
  }
279
- const res = await net.fetch(pathToFileURL(errorPage404).toString(), {
291
+ const res = await netFetch(pathToFileURL(errorPage404).toString(), {
280
292
  // see below
281
293
  bypassCustomProtocolHandlers: true
282
294
  })
@@ -295,7 +307,7 @@ export function createProxiedProtocolHandler(
295
307
  })
296
308
  }
297
309
 
298
- const response = await net.fetch(finalPath, {
310
+ const response = await netFetch(finalPath, {
299
311
  // avoid infinite loop if protocolName protocol is "file"
300
312
  // or file is otherwise handled
301
313
  bypassCustomProtocolHandlers: true