@witchcraft/nuxt-electron 0.4.0 → 0.4.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "electron",
3
3
  "configKey": "electron",
4
- "version": "0.4.0",
4
+ "version": "0.4.2",
5
5
  "builder": {
6
6
  "@nuxt/module-builder": "1.0.3",
7
7
  "unbuild": "3.6.1"
@@ -44,13 +44,13 @@ protocol: Protocol, protocolName: string | undefined, basePath: string,
44
44
  routeProxies: Record<string, string | {
45
45
  url: string;
46
46
  /** Override how the path is merged with the url. The default literally just concatenates url + path. */
47
- convertPath?: (proxyKey: string, url: string, requestPath: string) => string;
47
+ convertPath?: (proxyKey: string, url: string, requestPath: string, query: string) => string;
48
48
  ignore?: false;
49
49
  } | {
50
50
  url: never;
51
51
  convertPath: never;
52
52
  ignore: true;
53
- }>, { logger, errorPage, session }?: {
53
+ }>, { logger, errorPage, session, modifyRequest, proxiedProperties }?: {
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
  */
@@ -70,5 +70,8 @@ routeProxies: Record<string, string | {
70
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
71
  */
72
72
  session?: Session;
73
+ /** Request properties forwarded to the proxied request. The url is always replaced. */
74
+ proxiedProperties?: (keyof Request)[];
75
+ modifyRequest?: (init: RequestInit, url: string) => RequestInit;
73
76
  }): void;
74
77
  export {};
@@ -32,7 +32,6 @@ class TtlCache {
32
32
  }
33
33
  export const cache = new TtlCache();
34
34
  export const proxyCache = new TtlCache();
35
- const proxiedProperties = ["headers", "destination", "referrer", "referrerPolicy", "mode", "credentials", "cache", "redirect", "integrity", "keepalive"];
36
35
  async function getPathToServe(originalPath, pathWithIndexHtml, requestUrl, logger) {
37
36
  const stats = await fs.stat(originalPath).catch(() => void 0);
38
37
  if (logger) {
@@ -68,7 +67,22 @@ async function getFromCacheOr(cache2, key, fn, logger) {
68
67
  export function createProxiedProtocolHandler(protocol, protocolName = "app", basePath, routeProxies, {
69
68
  logger,
70
69
  errorPage = "404.html",
71
- session
70
+ session,
71
+ modifyRequest,
72
+ proxiedProperties = [
73
+ "method",
74
+ "headers",
75
+ "body",
76
+ "destination",
77
+ "referrer",
78
+ "referrerPolicy",
79
+ "mode",
80
+ "credentials",
81
+ "cache",
82
+ "redirect",
83
+ "integrity",
84
+ "keepalive"
85
+ ]
72
86
  } = {}) {
73
87
  const routeProxyKeys = keys(routeProxies);
74
88
  if (protocol.isProtocolHandled(protocolName)) {
@@ -89,7 +103,7 @@ export function createProxiedProtocolHandler(protocol, protocolName = "app", bas
89
103
  const proxyEntry = proxyKey && routeProxies[proxyKey];
90
104
  if (proxyKey && proxyEntry && !(typeof proxyEntry === "object" && proxyEntry?.ignore)) {
91
105
  const proxyUrl = typeof proxyEntry === "string" ? proxyEntry : proxyEntry?.url;
92
- const finalPath2 = typeof proxyEntry === "object" && proxyEntry?.convertPath ? proxyEntry.convertPath(proxyKey, proxyUrl, requestPath) : proxyUrl + requestPath;
106
+ const finalPath2 = typeof proxyEntry === "object" && proxyEntry?.convertPath ? proxyEntry.convertPath(proxyKey, proxyUrl, requestPath, parsedUrl.search) : proxyUrl + requestPath;
93
107
  if (logger) {
94
108
  logger.trace({
95
109
  ns: "main:createProxiedProtocolHandler:fetchingViaProxy",
@@ -113,9 +127,16 @@ export function createProxiedProtocolHandler(protocol, protocolName = "app", bas
113
127
  finalPath: cachedProxyResult
114
128
  });
115
129
  }
116
- return netFetch(cachedProxyResult, {
117
- ...pick(request, proxiedProperties)
118
- });
130
+ let init = pick(request, proxiedProperties);
131
+ if (init.body) init.duplex = "half";
132
+ const finalUrl = cachedProxyResult + parsedUrl.search;
133
+ const headers = new Headers(init.headers);
134
+ headers.set("origin", new URL(finalUrl).origin);
135
+ headers.set("referer", finalUrl);
136
+ headers.set("sec-fetch-site", "same-origin");
137
+ init.headers = headers;
138
+ if (modifyRequest) init = modifyRequest(init, finalUrl);
139
+ return netFetch(finalUrl, init);
119
140
  }
120
141
  const originalPath = path.join(basePath, requestPath);
121
142
  const pathWithIndexHtml = path.join(originalPath, "index.html");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@witchcraft/nuxt-electron",
3
- "version": "0.4.0",
3
+ "version": "0.4.2",
4
4
  "description": "Nuxt module for working with electron.",
5
5
  "repository": "https://github.com/witchcraftjs/nuxt-electron",
6
6
  "license": "MIT",
@@ -48,7 +48,6 @@ class TtlCache<TK, TV> {
48
48
 
49
49
  export const cache = new TtlCache<string, string | undefined>()
50
50
  export const proxyCache = new TtlCache<string, string | undefined>()
51
- const proxiedProperties: (keyof Request)[] = ["headers", "destination", "referrer", "referrerPolicy", "mode", "credentials", "cache", "redirect", "integrity", "keepalive"]
52
51
 
53
52
  async function getPathToServe(
54
53
  originalPath: string,
@@ -135,7 +134,7 @@ export function createProxiedProtocolHandler(
135
134
  | {
136
135
  url: string
137
136
  /** Override how the path is merged with the url. The default literally just concatenates url + path. */
138
- convertPath?: (proxyKey: string, url: string, requestPath: string) => string
137
+ convertPath?: (proxyKey: string, url: string, requestPath: string, query: string) => string
139
138
  ignore?: false
140
139
  }
141
140
  | {
@@ -147,7 +146,22 @@ export function createProxiedProtocolHandler(
147
146
  {
148
147
  logger,
149
148
  errorPage = "404.html",
150
- session
149
+ session,
150
+ modifyRequest,
151
+ proxiedProperties = [
152
+ "method",
153
+ "headers",
154
+ "body",
155
+ "destination",
156
+ "referrer",
157
+ "referrerPolicy",
158
+ "mode",
159
+ "credentials",
160
+ "cache",
161
+ "redirect",
162
+ "integrity",
163
+ "keepalive"
164
+ ] as (keyof Request)[]
151
165
  }: {
152
166
  /**
153
167
  * Optional logger. It's suggested you not pass this unless you're traceging in dev mode as a lot of requests can be made.
@@ -170,6 +184,9 @@ export function createProxiedProtocolHandler(
170
184
  * `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
185
  */
172
186
  session?: Session
187
+ /** Request properties forwarded to the proxied request. The url is always replaced. */
188
+ proxiedProperties?: (keyof Request)[]
189
+ modifyRequest?: (init: RequestInit, url: string) => RequestInit
173
190
 
174
191
  } = {}
175
192
  ): void {
@@ -208,7 +225,7 @@ export function createProxiedProtocolHandler(
208
225
  const proxyUrl = typeof proxyEntry === "string" ? proxyEntry : proxyEntry?.url
209
226
 
210
227
  const finalPath = typeof proxyEntry === "object" && proxyEntry?.convertPath
211
- ? proxyEntry.convertPath(proxyKey, proxyUrl, requestPath)
228
+ ? proxyEntry.convertPath(proxyKey, proxyUrl, requestPath, parsedUrl.search)
212
229
  : proxyUrl + requestPath
213
230
 
214
231
  if (logger) {
@@ -234,9 +251,17 @@ export function createProxiedProtocolHandler(
234
251
  finalPath: cachedProxyResult
235
252
  })
236
253
  }
237
- return netFetch(cachedProxyResult, {
238
- ...pick(request, proxiedProperties)
239
- })
254
+ let init = pick(request, proxiedProperties) as RequestInit
255
+ // fetch rejects a streamed body unless the stream mode is declared
256
+ if (init.body) (init as any).duplex = "half"
257
+ const finalUrl = cachedProxyResult + parsedUrl.search
258
+ const headers = new Headers(init.headers)
259
+ headers.set("origin", new URL(finalUrl).origin)
260
+ headers.set("referer", finalUrl)
261
+ headers.set("sec-fetch-site", "same-origin")
262
+ init.headers = headers
263
+ if (modifyRequest) init = modifyRequest(init, finalUrl)
264
+ return netFetch(finalUrl, init)
240
265
  }
241
266
 
242
267
  const originalPath = path.join(basePath, requestPath)