eddev 2.0.0-beta.70 → 2.0.0-beta.72

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.
@@ -5,7 +5,6 @@ import { ServerContext } from "./server-context.js";
5
5
  import { renderErrorPage } from "./render-ssr-page.js";
6
6
  export async function proxyWpAdmin(event) {
7
7
  const serverContext = ServerContext.main;
8
- const replaceUrls = serverContext.replaceUrls;
9
8
  const req = getWebRequest(event);
10
9
  const reqUrl = getRequestURL(event);
11
10
  const serverlessConfig = serverContext.config.serverless;
@@ -46,8 +45,8 @@ export async function proxyWpAdmin(event) {
46
45
  cookies.push(...splitSetCookieString(value));
47
46
  return;
48
47
  }
49
- if (key === "location" && replaceUrls) {
50
- value = replaceUrls(value);
48
+ if (key === "location") {
49
+ value = serverContext.replaceUrls(value);
51
50
  }
52
51
  res.headers.set(key, value);
53
52
  });
@@ -61,9 +60,7 @@ export async function proxyWpAdmin(event) {
61
60
  // Replace URLs pointing to the origin server from the response itself
62
61
  // This is only done for text-based content types, like application/json, text/plain, text/html etc
63
62
  if (contentType?.match(/(application|text)\//)) {
64
- if (replaceUrls) {
65
- body = replaceUrls((await response.text()) ?? "");
66
- }
63
+ body = serverContext.replaceUrls((await response.text()) ?? "", reqUrl.origin);
67
64
  // If the content type is text/html, inject the Vite assets into the response — assuming the placeholder comments are found
68
65
  if (contentType.startsWith("text/html")) {
69
66
  const clientManifest = serverContext.runtime.getManifest("admin");
@@ -7,6 +7,7 @@ export declare function getSsrStream(args: SSRArgs): Promise<ReadableStream<Uint
7
7
  type RenderArgs = {
8
8
  pathname: string;
9
9
  statusCode?: number;
10
+ newOrigin?: string;
10
11
  };
11
12
  export declare function renderPage(args: RenderArgs): Promise<Response>;
12
13
  type RenderErrorPageArgs = {
@@ -60,6 +60,7 @@ export async function renderPage(args) {
60
60
  withAppData: false,
61
61
  headers: {},
62
62
  query: {},
63
+ newOrigin: args.newOrigin,
63
64
  }),
64
65
  ]);
65
66
  let data;
@@ -23,22 +23,25 @@ type ServerAppData = {
23
23
  export declare class ServerContext {
24
24
  dev: boolean;
25
25
  origin: string;
26
- replaceUrls?: (text: string) => string;
26
+ _urlReplacer?: (text: string, origin?: string) => string;
27
27
  rpcBases: string[];
28
28
  config: EDConfig;
29
29
  static main: ServerContext;
30
30
  constructor(conf: ServerContextArgs);
31
+ replaceUrls(text: string, origin?: string): string;
31
32
  get runtime(): ServerContextRuntime;
32
33
  static setRuntime(rt: ServerContextRuntime): void;
33
34
  getOriginUrl(url: string): string;
34
35
  fetchOrigin(url: string, opts?: RequestInit & {
35
36
  replaceUrls?: boolean;
37
+ newOrigin?: string;
36
38
  }): Promise<Response>;
37
39
  fetchRouteData(req: {
38
40
  pathname: string;
39
41
  query?: Record<string, any>;
40
42
  headers?: RequestHeaders;
41
43
  withAppData?: boolean;
44
+ newOrigin?: string;
42
45
  }): Promise<Response>;
43
46
  fetchAppData(): Promise<ServerAppData>;
44
47
  extractRequestHeaders(req?: RequestHeaders): Partial<Record<import("vinxi/http").HTTPHeaderName, string | undefined>>;
@@ -17,7 +17,7 @@ let runtime;
17
17
  export class ServerContext {
18
18
  dev;
19
19
  origin;
20
- replaceUrls;
20
+ _urlReplacer;
21
21
  rpcBases = [];
22
22
  config;
23
23
  static main;
@@ -27,10 +27,16 @@ export class ServerContext {
27
27
  this.rpcBases = conf.rpcBases ?? [];
28
28
  this.config = conf.config;
29
29
  if (conf.replaceUrls) {
30
- this.replaceUrls = createUrlReplacer(conf.replaceUrls);
30
+ this._urlReplacer = createUrlReplacer(conf.replaceUrls);
31
31
  }
32
32
  ServerContext.main = this;
33
33
  }
34
+ replaceUrls(text, origin) {
35
+ if (this._urlReplacer) {
36
+ return this._urlReplacer(text, origin);
37
+ }
38
+ return text;
39
+ }
34
40
  get runtime() {
35
41
  return runtime;
36
42
  }
@@ -66,8 +72,8 @@ export class ServerContext {
66
72
  });
67
73
  // console.log("Returning headers", headers)
68
74
  let text = await response.text();
69
- if (opts?.replaceUrls && this.replaceUrls) {
70
- text = this.replaceUrls(text);
75
+ if (opts?.replaceUrls) {
76
+ text = this.replaceUrls(text, opts.newOrigin);
71
77
  }
72
78
  return new Response(text, {
73
79
  status: response.status,
@@ -86,6 +92,7 @@ export class ServerContext {
86
92
  const result = await this.fetchOrigin(fetchUrl, {
87
93
  cache: "no-cache",
88
94
  replaceUrls: true,
95
+ newOrigin: req.newOrigin,
89
96
  headers: {
90
97
  "Content-Type": "application/json",
91
98
  Accept: "application/json",
@@ -112,7 +119,6 @@ export class ServerContext {
112
119
  return result;
113
120
  }
114
121
  async fetchAppData() {
115
- // console.log("Fetching app data")
116
122
  const response = await this.fetchOrigin("/_appdata", {
117
123
  cache: "no-cache",
118
124
  replaceUrls: true,
@@ -121,9 +127,7 @@ export class ServerContext {
121
127
  Accept: "application/json",
122
128
  },
123
129
  });
124
- // console.log("Decoding app data")
125
130
  const result = await response.json();
126
- // console.log("Done with app done")
127
131
  return result;
128
132
  }
129
133
  extractRequestHeaders(req) {
@@ -7,4 +7,4 @@ export type UrlReplacerConf = {
7
7
  replace: boolean;
8
8
  }[];
9
9
  };
10
- export declare function createUrlReplacer(conf: UrlReplacerConf): (text: string) => string;
10
+ export declare function createUrlReplacer(conf: UrlReplacerConf): (text: string, newOrigin?: string) => string;
@@ -2,7 +2,7 @@ export function createUrlReplacer(conf) {
2
2
  // Create a regular expression to match the URL
3
3
  // Note that that the regex doesn't necessarily match the entire URL
4
4
  const lookup = new RegExp(conf.from.replace(/https?[:\\\/]+/, "https?[:\\/\\\\]+") + "([a-z0-9\\-_/\\\\]+)", "ig");
5
- return (text) => {
5
+ return (text, newOrigin) => {
6
6
  return text.replace(lookup, (url) => {
7
7
  // Strip out the origin, to create a relative path
8
8
  const path = url.replace(/https?:[\/\\]+[^\/\\]+/, "");
@@ -18,10 +18,19 @@ export function createUrlReplacer(conf) {
18
18
  return url;
19
19
  }
20
20
  else {
21
- return path;
21
+ break;
22
22
  }
23
23
  }
24
24
  }
25
+ console.log("Replacing", url, "with", newOrigin);
26
+ if (newOrigin) {
27
+ if (isEscaped) {
28
+ return newOrigin.replace(/\//g, "\\/") + path;
29
+ }
30
+ else {
31
+ return newOrigin + path;
32
+ }
33
+ }
25
34
  return path;
26
35
  // if (path.startsWith("/wp-content/uploads/")) {
27
36
  // if (!conf.ignoreUploads) {
@@ -0,0 +1,7 @@
1
+ import { Cache } from "@epic-web/cachified";
2
+ export declare function swr<T>(args: {
3
+ key: string;
4
+ fetcher: () => Promise<T>;
5
+ ttl?: number;
6
+ cache?: Cache;
7
+ }): Promise<T>;
@@ -0,0 +1,29 @@
1
+ import { LRUCache } from "lru-cache";
2
+ import { cachified, totalTtl } from "@epic-web/cachified";
3
+ /* lru cache is not part of this package but a simple non-persistent cache */
4
+ const lruInstance = new LRUCache({ max: 1000 });
5
+ const lru = {
6
+ set(key, value) {
7
+ const ttl = totalTtl(value?.metadata);
8
+ return lruInstance.set(key, value, {
9
+ ttl: ttl === Infinity ? undefined : ttl,
10
+ start: value?.metadata?.createdTime,
11
+ });
12
+ },
13
+ get(key) {
14
+ return lruInstance.get(key);
15
+ },
16
+ delete(key) {
17
+ return lruInstance.delete(key);
18
+ },
19
+ };
20
+ export function swr(args) {
21
+ return cachified({
22
+ key: args.key,
23
+ cache: args.cache ?? lru,
24
+ getFreshValue: args.fetcher,
25
+ ttl: args.ttl,
26
+ swr: 3600_000,
27
+ fallbackToCache: 15_000,
28
+ });
29
+ }
@@ -1 +1 @@
1
- export declare const VERSION = "2.0.0-beta.70";
1
+ export declare const VERSION = "2.0.0-beta.72";
@@ -1 +1 @@
1
- export const VERSION = "2.0.0-beta.70";
1
+ export const VERSION = "2.0.0-beta.72";
@@ -33,6 +33,20 @@ export function createVinxiApp(args) {
33
33
  varies: [],
34
34
  },
35
35
  },
36
+ "/robots.txt": {
37
+ proxy: joinURL(args.origin, "robots.txt"),
38
+ static: true,
39
+ cache: {
40
+ maxAge: 3600,
41
+ varies: [],
42
+ },
43
+ },
44
+ "/sitemap*.xml": {
45
+ cache: {
46
+ maxAge: 3600,
47
+ swr: true,
48
+ },
49
+ },
36
50
  "/wp-content/plugins/**": {
37
51
  proxy: joinURL(args.origin, "wp-content/plugins/**"),
38
52
  headers: {
@@ -116,8 +130,7 @@ export function createVinxiApp(args) {
116
130
  handler: `${folder}/handler.wp-proxy.ts`,
117
131
  target: "server",
118
132
  plugins: () => [
119
- tsconfigPaths(),
120
- envPlugin({
133
+ ...corePlugins({
121
134
  rootDir: args.rootDir,
122
135
  console: log,
123
136
  mode: args.mode,
@@ -20,7 +20,7 @@ export function createVinxiCodegen(opts) {
20
20
  name: "context.ts",
21
21
  generate: async () => {
22
22
  const args = {
23
- dev: true,
23
+ dev: opts.mode === "development",
24
24
  origin: project.origin,
25
25
  replaceUrls: {
26
26
  from: project.origin,
@@ -181,7 +181,7 @@ export function createVinxiCodegen(opts) {
181
181
  name: "handler.data-api.ts",
182
182
  generate: code /* tsx */ `
183
183
  /// <reference types="vinxi/types/server" />
184
- import { createRouter, eventHandler, getRouterParam, getQuery, getWebRequest, getRequestHeaders } from "vinxi/http"
184
+ import { createRouter, eventHandler, getRouterParam, getQuery, getWebRequest, getRequestHeaders, getRequestURL } from "vinxi/http"
185
185
  import { serverContext } from "./context.js"
186
186
 
187
187
  const router = createRouter()
@@ -189,10 +189,12 @@ export function createVinxiCodegen(opts) {
189
189
  "/route/",
190
190
  eventHandler(async (event) => {
191
191
  const id = "/"
192
+ const url = getRequestURL(event);
192
193
 
193
194
  return await serverContext.fetchRouteData({
194
195
  pathname: id,
195
196
  withAppData: false,
197
+ newOrigin: url.origin,
196
198
  })
197
199
  }),
198
200
  )
@@ -200,10 +202,12 @@ export function createVinxiCodegen(opts) {
200
202
  "/route/**:name",
201
203
  eventHandler(async (event) => {
202
204
  const id = "/" + getRouterParam(event, "name")
205
+ const url = getRequestURL(event);
203
206
 
204
207
  return await serverContext.fetchRouteData({
205
208
  pathname: id,
206
209
  withAppData: false,
210
+ newOrigin: url.origin,
207
211
  })
208
212
  }),
209
213
  )
@@ -268,7 +272,7 @@ export function createVinxiCodegen(opts) {
268
272
  return proxyWpAdmin(event)
269
273
  }
270
274
 
271
- return renderPage({ pathname: url.pathname })
275
+ return renderPage({ pathname: url.pathname, newOrigin: url.origin })
272
276
  }
273
277
  })
274
278
  `,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eddev",
3
- "version": "2.0.0-beta.70",
3
+ "version": "2.0.0-beta.72",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -77,6 +77,7 @@
77
77
  "dependencies": {
78
78
  "@babel/types": "^7.25.2",
79
79
  "@clack/prompts": "^0.7.0",
80
+ "@epic-web/cachified": "^5.2.0",
80
81
  "@graphql-codegen/core": "^4.0.0",
81
82
  "@graphql-codegen/typescript": "^4.0.1",
82
83
  "@graphql-codegen/typescript-operations": "^4.0.1",
@@ -105,6 +106,7 @@
105
106
  "ink-spinner": "^5.0.0",
106
107
  "ink-text-input": "^6.0.0",
107
108
  "listhen": "^1.6.0",
109
+ "lru-cache": "^11.0.2",
108
110
  "mkcert": "^3.2.0",
109
111
  "obj-console": "^1.0.2",
110
112
  "object-code": "^1.3.3",