@cedarjs/vite 4.1.1-next.1 → 4.1.1-next.55

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.
Files changed (52) hide show
  1. package/dist/apiDevMiddleware.d.ts +15 -0
  2. package/dist/apiDevMiddleware.d.ts.map +1 -0
  3. package/dist/{apiDevServer.js → apiDevMiddleware.js} +95 -107
  4. package/dist/build/build.d.ts +1 -1
  5. package/dist/build/build.d.ts.map +1 -1
  6. package/dist/build/build.js +2 -0
  7. package/dist/buildApp.d.ts +15 -0
  8. package/dist/buildApp.d.ts.map +1 -0
  9. package/dist/buildApp.js +150 -0
  10. package/dist/buildUDApiServer.d.ts +7 -5
  11. package/dist/buildUDApiServer.d.ts.map +1 -1
  12. package/dist/buildUDApiServer.js +8 -26
  13. package/dist/cedar-unified-dev.js +59 -5
  14. package/dist/cjs/{apiDevServer.js → apiDevMiddleware.js} +102 -110
  15. package/dist/cjs/build/build.js +3 -0
  16. package/dist/cjs/buildApp.js +181 -0
  17. package/dist/cjs/buildUDApiServer.js +8 -26
  18. package/dist/cjs/cedar-unified-dev.js +59 -5
  19. package/dist/cjs/index.js +3 -3
  20. package/dist/cjs/plugins/vite-plugin-cedar-cjs-compat.js +341 -0
  21. package/dist/cjs/plugins/vite-plugin-cedar-universal-deploy.js +114 -19
  22. package/dist/cjs/plugins/vite-plugin-cedar-wait-for-api-server.js +2 -1
  23. package/dist/cjs/ud-handlers/catch-all.js +65 -0
  24. package/dist/cjs/ud-handlers/function.js +47 -0
  25. package/dist/cjs/ud-handlers/graphql.js +59 -0
  26. package/dist/index.d.ts +1 -1
  27. package/dist/index.d.ts.map +1 -1
  28. package/dist/index.js +2 -2
  29. package/dist/plugins/vite-plugin-cedar-cjs-compat.d.ts +23 -0
  30. package/dist/plugins/vite-plugin-cedar-cjs-compat.d.ts.map +1 -0
  31. package/dist/plugins/vite-plugin-cedar-cjs-compat.js +307 -0
  32. package/dist/plugins/vite-plugin-cedar-universal-deploy.d.ts.map +1 -1
  33. package/dist/plugins/vite-plugin-cedar-universal-deploy.js +105 -20
  34. package/dist/plugins/vite-plugin-cedar-wait-for-api-server.d.ts.map +1 -1
  35. package/dist/plugins/vite-plugin-cedar-wait-for-api-server.js +2 -1
  36. package/dist/ud-handlers/catch-all.d.ts +15 -0
  37. package/dist/ud-handlers/catch-all.d.ts.map +1 -0
  38. package/dist/ud-handlers/catch-all.js +41 -0
  39. package/dist/ud-handlers/function.d.ts +5 -0
  40. package/dist/ud-handlers/function.d.ts.map +1 -0
  41. package/dist/ud-handlers/function.js +23 -0
  42. package/dist/ud-handlers/graphql.d.ts +7 -0
  43. package/dist/ud-handlers/graphql.d.ts.map +1 -0
  44. package/dist/ud-handlers/graphql.js +35 -0
  45. package/package.json +52 -23
  46. package/LICENSE +0 -21
  47. package/dist/apiDevServer.d.ts +0 -18
  48. package/dist/apiDevServer.d.ts.map +0 -1
  49. package/dist/cjs/plugins/vite-plugin-cedar-dev-dispatcher.js +0 -223
  50. package/dist/plugins/vite-plugin-cedar-dev-dispatcher.d.ts +0 -3
  51. package/dist/plugins/vite-plugin-cedar-dev-dispatcher.d.ts.map +0 -1
  52. package/dist/plugins/vite-plugin-cedar-dev-dispatcher.js +0 -189
@@ -1,223 +0,0 @@
1
- "use strict";
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __export = (target, all) => {
9
- for (var name in all)
10
- __defProp(target, name, { get: all[name], enumerable: true });
11
- };
12
- var __copyProps = (to, from, except, desc) => {
13
- if (from && typeof from === "object" || typeof from === "function") {
14
- for (let key of __getOwnPropNames(from))
15
- if (!__hasOwnProp.call(to, key) && key !== except)
16
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
- }
18
- return to;
19
- };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
- mod
27
- ));
28
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
- var vite_plugin_cedar_dev_dispatcher_exports = {};
30
- __export(vite_plugin_cedar_dev_dispatcher_exports, {
31
- cedarDevDispatcherPlugin: () => cedarDevDispatcherPlugin
32
- });
33
- module.exports = __toCommonJS(vite_plugin_cedar_dev_dispatcher_exports);
34
- var import_node_net = __toESM(require("node:net"), 1);
35
- var import_node_path = __toESM(require("node:path"), 1);
36
- var import_project_config = require("@cedarjs/project-config");
37
- let cachedDispatcher = null;
38
- let dispatcherGeneration = 0;
39
- let buildPromise = null;
40
- async function getDispatcher() {
41
- if (cachedDispatcher !== null) {
42
- return cachedDispatcher;
43
- }
44
- if (buildPromise !== null) {
45
- await buildPromise;
46
- return cachedDispatcher ?? getDispatcher();
47
- }
48
- const generationAtStart = dispatcherGeneration;
49
- buildPromise = (async () => {
50
- try {
51
- const { rebuildApi, buildApi } = await import("@cedarjs/internal/dist/build/api");
52
- try {
53
- await rebuildApi();
54
- } catch {
55
- await buildApi();
56
- }
57
- } catch (err) {
58
- console.warn(
59
- "[cedar-dev-dispatcher] API compilation failed; serving with last-known-good dist:",
60
- err
61
- );
62
- }
63
- const { buildCedarDispatcher } = await import("@cedarjs/api-server/udDispatcher");
64
- const { fetchable } = await buildCedarDispatcher({ cacheBust: Date.now() });
65
- if (generationAtStart === dispatcherGeneration) {
66
- cachedDispatcher = fetchable;
67
- }
68
- return fetchable;
69
- })();
70
- try {
71
- await buildPromise;
72
- } finally {
73
- if (generationAtStart === dispatcherGeneration) {
74
- buildPromise = null;
75
- }
76
- }
77
- if (cachedDispatcher !== null) {
78
- return cachedDispatcher;
79
- }
80
- return getDispatcher();
81
- }
82
- function invalidateDispatcher() {
83
- cachedDispatcher = null;
84
- buildPromise = null;
85
- dispatcherGeneration++;
86
- }
87
- function isViteInternalRequest(url) {
88
- return url.startsWith("/@") || url.startsWith("/__vite") || url.startsWith("/__hmr") || url.includes("?import") || url.includes("?t=") || url.includes("?v=");
89
- }
90
- async function nodeRequestToFetch(req) {
91
- const host = req.headers.host ?? "localhost";
92
- const url = `http://${host}${req.url ?? "/"}`;
93
- const headers = new Headers();
94
- for (const [key, value] of Object.entries(req.headers)) {
95
- if (value === void 0) {
96
- continue;
97
- }
98
- if (Array.isArray(value)) {
99
- for (const v of value) {
100
- headers.append(key, v);
101
- }
102
- } else {
103
- headers.set(key, value);
104
- }
105
- }
106
- const method = (req.method ?? "GET").toUpperCase();
107
- const hasBody = ["POST", "PUT", "PATCH", "DELETE"].includes(method);
108
- let body;
109
- if (hasBody) {
110
- body = await new Promise((resolve, reject) => {
111
- const chunks = [];
112
- req.on("data", (chunk) => chunks.push(chunk));
113
- req.on("end", () => resolve(Buffer.concat(chunks)));
114
- req.on("error", reject);
115
- });
116
- }
117
- return new Request(url, {
118
- method,
119
- headers,
120
- body: hasBody && body && body.length > 0 ? new Uint8Array(body) : void 0
121
- });
122
- }
123
- async function fetchResponseToNode(fetchRes, res) {
124
- res.statusCode = fetchRes.status;
125
- fetchRes.headers.forEach((value, key) => {
126
- res.setHeader(key, value);
127
- });
128
- const bodyBuffer = await fetchRes.arrayBuffer();
129
- if (bodyBuffer.byteLength > 0) {
130
- res.end(Buffer.from(bodyBuffer));
131
- } else {
132
- res.end();
133
- }
134
- }
135
- let apiServerIsUp;
136
- async function checkApiPort(host, port) {
137
- return new Promise((resolve) => {
138
- const socket = new import_node_net.default.Socket();
139
- socket.setTimeout(100);
140
- socket.on("connect", () => {
141
- socket.destroy();
142
- resolve(true);
143
- });
144
- socket.on("timeout", () => {
145
- socket.destroy();
146
- resolve(false);
147
- });
148
- socket.on("error", () => {
149
- socket.destroy();
150
- resolve(false);
151
- });
152
- socket.connect(port, host);
153
- });
154
- }
155
- function cedarDevDispatcherPlugin() {
156
- return {
157
- name: "cedar-dev-dispatcher",
158
- apply: "serve",
159
- configureServer(server) {
160
- const cedarConfig = (0, import_project_config.getConfig)();
161
- const apiUrl = cedarConfig.web.apiUrl.replace(/\/$/, "");
162
- const apiGqlUrl = cedarConfig.web.apiGraphQLUrl ?? apiUrl + "/graphql";
163
- const apiPort = cedarConfig.api.port;
164
- const apiHost = cedarConfig.api.host || "127.0.0.1";
165
- function isApiRequest(url) {
166
- return url === apiUrl || url.startsWith(apiUrl + "/") || url.startsWith(apiUrl + "?") || url === apiGqlUrl || url.startsWith(apiGqlUrl + "/") || url.startsWith(apiGqlUrl + "?");
167
- }
168
- server.watcher.on("change", (filePath) => {
169
- if (filePath.startsWith((0, import_project_config.getPaths)().api.src + import_node_path.default.sep)) {
170
- invalidateDispatcher();
171
- }
172
- });
173
- server.middlewares.use(
174
- async (req, res, next) => {
175
- const url = req.url ?? "/";
176
- if (isViteInternalRequest(url)) {
177
- return next();
178
- }
179
- if (!isApiRequest(url)) {
180
- return next();
181
- }
182
- if (apiServerIsUp === void 0) {
183
- apiServerIsUp = await checkApiPort(apiHost, apiPort);
184
- }
185
- if (apiServerIsUp) {
186
- return next();
187
- }
188
- try {
189
- const dispatcher = await getDispatcher();
190
- const fetchRequest = await nodeRequestToFetch(req);
191
- const fetchResponse = await dispatcher.fetch(fetchRequest);
192
- await fetchResponseToNode(fetchResponse, res);
193
- } catch (err) {
194
- console.error(
195
- "[cedar-dev-dispatcher] Error handling API request:",
196
- err
197
- );
198
- if (!res.headersSent) {
199
- res.writeHead(500, { "Content-Type": "application/json" });
200
- }
201
- res.end(
202
- JSON.stringify(
203
- {
204
- errors: [
205
- {
206
- message: err instanceof Error ? err.message : "Internal Server Error"
207
- }
208
- ]
209
- },
210
- null,
211
- 2
212
- )
213
- );
214
- }
215
- }
216
- );
217
- }
218
- };
219
- }
220
- // Annotate the CommonJS export names for ESM import in node:
221
- 0 && (module.exports = {
222
- cedarDevDispatcherPlugin
223
- });
@@ -1,3 +0,0 @@
1
- import type { Plugin } from 'vite';
2
- export declare function cedarDevDispatcherPlugin(): Plugin;
3
- //# sourceMappingURL=vite-plugin-cedar-dev-dispatcher.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"vite-plugin-cedar-dev-dispatcher.d.ts","sourceRoot":"","sources":["../../src/plugins/vite-plugin-cedar-dev-dispatcher.ts"],"names":[],"mappings":"AAYA,OAAO,KAAK,EAAE,MAAM,EAAiB,MAAM,MAAM,CAAA;AA+LjD,wBAAgB,wBAAwB,IAAI,MAAM,CA4FjD"}
@@ -1,189 +0,0 @@
1
- import net from "node:net";
2
- import path from "node:path";
3
- import { getConfig, getPaths } from "@cedarjs/project-config";
4
- let cachedDispatcher = null;
5
- let dispatcherGeneration = 0;
6
- let buildPromise = null;
7
- async function getDispatcher() {
8
- if (cachedDispatcher !== null) {
9
- return cachedDispatcher;
10
- }
11
- if (buildPromise !== null) {
12
- await buildPromise;
13
- return cachedDispatcher ?? getDispatcher();
14
- }
15
- const generationAtStart = dispatcherGeneration;
16
- buildPromise = (async () => {
17
- try {
18
- const { rebuildApi, buildApi } = await import("@cedarjs/internal/dist/build/api");
19
- try {
20
- await rebuildApi();
21
- } catch {
22
- await buildApi();
23
- }
24
- } catch (err) {
25
- console.warn(
26
- "[cedar-dev-dispatcher] API compilation failed; serving with last-known-good dist:",
27
- err
28
- );
29
- }
30
- const { buildCedarDispatcher } = await import("@cedarjs/api-server/udDispatcher");
31
- const { fetchable } = await buildCedarDispatcher({ cacheBust: Date.now() });
32
- if (generationAtStart === dispatcherGeneration) {
33
- cachedDispatcher = fetchable;
34
- }
35
- return fetchable;
36
- })();
37
- try {
38
- await buildPromise;
39
- } finally {
40
- if (generationAtStart === dispatcherGeneration) {
41
- buildPromise = null;
42
- }
43
- }
44
- if (cachedDispatcher !== null) {
45
- return cachedDispatcher;
46
- }
47
- return getDispatcher();
48
- }
49
- function invalidateDispatcher() {
50
- cachedDispatcher = null;
51
- buildPromise = null;
52
- dispatcherGeneration++;
53
- }
54
- function isViteInternalRequest(url) {
55
- return url.startsWith("/@") || url.startsWith("/__vite") || url.startsWith("/__hmr") || url.includes("?import") || url.includes("?t=") || url.includes("?v=");
56
- }
57
- async function nodeRequestToFetch(req) {
58
- const host = req.headers.host ?? "localhost";
59
- const url = `http://${host}${req.url ?? "/"}`;
60
- const headers = new Headers();
61
- for (const [key, value] of Object.entries(req.headers)) {
62
- if (value === void 0) {
63
- continue;
64
- }
65
- if (Array.isArray(value)) {
66
- for (const v of value) {
67
- headers.append(key, v);
68
- }
69
- } else {
70
- headers.set(key, value);
71
- }
72
- }
73
- const method = (req.method ?? "GET").toUpperCase();
74
- const hasBody = ["POST", "PUT", "PATCH", "DELETE"].includes(method);
75
- let body;
76
- if (hasBody) {
77
- body = await new Promise((resolve, reject) => {
78
- const chunks = [];
79
- req.on("data", (chunk) => chunks.push(chunk));
80
- req.on("end", () => resolve(Buffer.concat(chunks)));
81
- req.on("error", reject);
82
- });
83
- }
84
- return new Request(url, {
85
- method,
86
- headers,
87
- body: hasBody && body && body.length > 0 ? new Uint8Array(body) : void 0
88
- });
89
- }
90
- async function fetchResponseToNode(fetchRes, res) {
91
- res.statusCode = fetchRes.status;
92
- fetchRes.headers.forEach((value, key) => {
93
- res.setHeader(key, value);
94
- });
95
- const bodyBuffer = await fetchRes.arrayBuffer();
96
- if (bodyBuffer.byteLength > 0) {
97
- res.end(Buffer.from(bodyBuffer));
98
- } else {
99
- res.end();
100
- }
101
- }
102
- let apiServerIsUp;
103
- async function checkApiPort(host, port) {
104
- return new Promise((resolve) => {
105
- const socket = new net.Socket();
106
- socket.setTimeout(100);
107
- socket.on("connect", () => {
108
- socket.destroy();
109
- resolve(true);
110
- });
111
- socket.on("timeout", () => {
112
- socket.destroy();
113
- resolve(false);
114
- });
115
- socket.on("error", () => {
116
- socket.destroy();
117
- resolve(false);
118
- });
119
- socket.connect(port, host);
120
- });
121
- }
122
- function cedarDevDispatcherPlugin() {
123
- return {
124
- name: "cedar-dev-dispatcher",
125
- apply: "serve",
126
- configureServer(server) {
127
- const cedarConfig = getConfig();
128
- const apiUrl = cedarConfig.web.apiUrl.replace(/\/$/, "");
129
- const apiGqlUrl = cedarConfig.web.apiGraphQLUrl ?? apiUrl + "/graphql";
130
- const apiPort = cedarConfig.api.port;
131
- const apiHost = cedarConfig.api.host || "127.0.0.1";
132
- function isApiRequest(url) {
133
- return url === apiUrl || url.startsWith(apiUrl + "/") || url.startsWith(apiUrl + "?") || url === apiGqlUrl || url.startsWith(apiGqlUrl + "/") || url.startsWith(apiGqlUrl + "?");
134
- }
135
- server.watcher.on("change", (filePath) => {
136
- if (filePath.startsWith(getPaths().api.src + path.sep)) {
137
- invalidateDispatcher();
138
- }
139
- });
140
- server.middlewares.use(
141
- async (req, res, next) => {
142
- const url = req.url ?? "/";
143
- if (isViteInternalRequest(url)) {
144
- return next();
145
- }
146
- if (!isApiRequest(url)) {
147
- return next();
148
- }
149
- if (apiServerIsUp === void 0) {
150
- apiServerIsUp = await checkApiPort(apiHost, apiPort);
151
- }
152
- if (apiServerIsUp) {
153
- return next();
154
- }
155
- try {
156
- const dispatcher = await getDispatcher();
157
- const fetchRequest = await nodeRequestToFetch(req);
158
- const fetchResponse = await dispatcher.fetch(fetchRequest);
159
- await fetchResponseToNode(fetchResponse, res);
160
- } catch (err) {
161
- console.error(
162
- "[cedar-dev-dispatcher] Error handling API request:",
163
- err
164
- );
165
- if (!res.headersSent) {
166
- res.writeHead(500, { "Content-Type": "application/json" });
167
- }
168
- res.end(
169
- JSON.stringify(
170
- {
171
- errors: [
172
- {
173
- message: err instanceof Error ? err.message : "Internal Server Error"
174
- }
175
- ]
176
- },
177
- null,
178
- 2
179
- )
180
- );
181
- }
182
- }
183
- );
184
- }
185
- };
186
- }
187
- export {
188
- cedarDevDispatcherPlugin
189
- };