revojs 0.1.18 → 0.1.19

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.
@@ -0,0 +1,447 @@
1
+ import { CLIENT, SERVER, SERVER_CONTEXT, Scope, createApp, invoke } from "../app-D1k2p6G_.js";
2
+ import { isRunnableDevEnvironment } from "vite";
3
+ import { once } from "events";
4
+ import { Readable, Stream } from "stream";
5
+ import { existsSync, readFileSync } from "fs";
6
+ import { basename, dirname, isAbsolute, join, posix, relative, win32 } from "path";
7
+ import { fileURLToPath } from "url";
8
+ import { globSync } from "tinyglobby";
9
+
10
+ //#region src/vite/node/index.ts
11
+ function splitSetCookieString(cookiesString) {
12
+ if (Array.isArray(cookiesString)) return cookiesString.flatMap((c) => splitSetCookieString(c));
13
+ if (typeof cookiesString !== "string") return [];
14
+ const cookiesStrings = [];
15
+ let pos = 0;
16
+ let start;
17
+ let ch;
18
+ let lastComma;
19
+ let nextStart;
20
+ let cookiesSeparatorFound;
21
+ const skipWhitespace = () => {
22
+ while (pos < cookiesString.length && /\s/.test(cookiesString.charAt(pos))) pos += 1;
23
+ return pos < cookiesString.length;
24
+ };
25
+ const notSpecialChar = () => {
26
+ ch = cookiesString.charAt(pos);
27
+ return ch !== "=" && ch !== ";" && ch !== ",";
28
+ };
29
+ while (pos < cookiesString.length) {
30
+ start = pos;
31
+ cookiesSeparatorFound = false;
32
+ while (skipWhitespace()) {
33
+ ch = cookiesString.charAt(pos);
34
+ if (ch === ",") {
35
+ lastComma = pos;
36
+ pos += 1;
37
+ skipWhitespace();
38
+ nextStart = pos;
39
+ while (pos < cookiesString.length && notSpecialChar()) pos += 1;
40
+ if (pos < cookiesString.length && cookiesString.charAt(pos) === "=") {
41
+ cookiesSeparatorFound = true;
42
+ pos = nextStart;
43
+ cookiesStrings.push(cookiesString.slice(start, lastComma));
44
+ start = pos;
45
+ } else pos = lastComma + 1;
46
+ } else pos += 1;
47
+ }
48
+ if (!cookiesSeparatorFound || pos >= cookiesString.length) cookiesStrings.push(cookiesString.slice(start));
49
+ }
50
+ return cookiesStrings;
51
+ }
52
+ function createReadableStreamFromReadable(source) {
53
+ let pump = new StreamPump(source);
54
+ return new ReadableStream(pump, pump);
55
+ }
56
+ var StreamPump = class {
57
+ highWaterMark;
58
+ accumalatedSize;
59
+ stream;
60
+ controller;
61
+ constructor(stream) {
62
+ this.highWaterMark = stream.readableHighWaterMark || new Stream.Readable().readableHighWaterMark;
63
+ this.accumalatedSize = 0;
64
+ this.stream = stream;
65
+ this.enqueue = this.enqueue.bind(this);
66
+ this.error = this.error.bind(this);
67
+ this.close = this.close.bind(this);
68
+ }
69
+ size(chunk) {
70
+ return chunk?.byteLength || 0;
71
+ }
72
+ start(controller) {
73
+ this.controller = controller;
74
+ this.stream.on("data", this.enqueue);
75
+ this.stream.once("error", this.error);
76
+ this.stream.once("end", this.close);
77
+ this.stream.once("close", this.close);
78
+ }
79
+ pull() {
80
+ this.resume();
81
+ }
82
+ cancel(reason) {
83
+ if (this.stream.destroy) this.stream.destroy(reason);
84
+ this.stream.off("data", this.enqueue);
85
+ this.stream.off("error", this.error);
86
+ this.stream.off("end", this.close);
87
+ this.stream.off("close", this.close);
88
+ }
89
+ enqueue(chunk) {
90
+ if (this.controller) try {
91
+ let bytes = chunk instanceof Uint8Array ? chunk : Buffer.from(chunk);
92
+ let available = (this.controller.desiredSize || 0) - bytes.byteLength;
93
+ this.controller.enqueue(bytes);
94
+ if (available <= 0) this.pause();
95
+ } catch (error) {
96
+ this.controller.error(/* @__PURE__ */ new Error("Could not create Buffer, chunk must be of type string or an instance of Buffer, ArrayBuffer, or Array or an Array-like Object"));
97
+ this.cancel();
98
+ }
99
+ }
100
+ pause() {
101
+ if (this.stream.pause) this.stream.pause();
102
+ }
103
+ resume() {
104
+ if (this.stream.readable && this.stream.resume) this.stream.resume();
105
+ }
106
+ close() {
107
+ if (this.controller) {
108
+ this.controller.close();
109
+ delete this.controller;
110
+ }
111
+ }
112
+ error(error) {
113
+ if (this.controller) {
114
+ this.controller.error(error);
115
+ delete this.controller;
116
+ }
117
+ }
118
+ };
119
+ function fromNodeHeaders(nodeHeaders) {
120
+ let headers = new Headers();
121
+ for (let [key, values] of Object.entries(nodeHeaders)) if (values) if (Array.isArray(values)) for (let value of values) headers.append(key, value);
122
+ else headers.set(key, values);
123
+ return headers;
124
+ }
125
+ function fromNodeRequest(nodeReq, nodeRes) {
126
+ let origin = nodeReq.headers.origin && "null" !== nodeReq.headers.origin ? nodeReq.headers.origin : `http://${nodeReq.headers.host}`;
127
+ let url = new URL(nodeReq.url ?? "", origin);
128
+ let controller = new AbortController();
129
+ let init = {
130
+ method: nodeReq.method,
131
+ headers: fromNodeHeaders(nodeReq.headers),
132
+ signal: controller.signal
133
+ };
134
+ if (nodeReq.method !== "GET" && nodeReq.method !== "HEAD") {
135
+ init.body = createReadableStreamFromReadable(nodeReq);
136
+ init.duplex = "half";
137
+ }
138
+ nodeRes.on("finish", () => controller = null);
139
+ nodeRes.on("close", () => controller?.abort());
140
+ return new Request(url.href, init);
141
+ }
142
+ async function toNodeRequest(res, nodeRes) {
143
+ nodeRes.statusCode = res.status;
144
+ nodeRes.statusMessage = res.statusText;
145
+ let cookiesStrings = [];
146
+ for (let [name$1, value] of res.headers) if (name$1 === "set-cookie") cookiesStrings.push(...splitSetCookieString(value));
147
+ else nodeRes.setHeader(name$1, value);
148
+ if (cookiesStrings.length) nodeRes.setHeader("set-cookie", cookiesStrings);
149
+ if (res.body) {
150
+ let responseBody = res.body;
151
+ let readable = Readable.from(responseBody);
152
+ readable.pipe(nodeRes);
153
+ await once(readable, "end");
154
+ } else nodeRes.end();
155
+ }
156
+
157
+ //#endregion
158
+ //#region src/vite/plugins/client.ts
159
+ const SUFFIX = "?client";
160
+ function client() {
161
+ let server;
162
+ let bundle;
163
+ return {
164
+ name: "client",
165
+ sharedDuringBuild: true,
166
+ configureServer(devServer) {
167
+ server = devServer;
168
+ },
169
+ writeBundle(_, clientBundle) {
170
+ if (this.environment.name === CLIENT) bundle = clientBundle;
171
+ },
172
+ load(key) {
173
+ if (key.endsWith(SUFFIX)) {
174
+ const path = key.substring(0, key.length - 7);
175
+ if (bundle) for (const name$1 in bundle) {
176
+ const file = bundle[name$1];
177
+ if (file && file.type === "asset" && file.fileName === basename(path)) return file.source.toString();
178
+ }
179
+ return readFileSync(path, "utf-8");
180
+ }
181
+ return null;
182
+ },
183
+ async transform(code, key) {
184
+ if (key.endsWith(SUFFIX)) {
185
+ code = server ? await server.transformIndexHtml(key, code) : code;
186
+ return { code: `export default \`${code}\`` };
187
+ }
188
+ return null;
189
+ }
190
+ };
191
+ }
192
+
193
+ //#endregion
194
+ //#region src/vite/plugins/css.ts
195
+ function css() {
196
+ let devServer;
197
+ const styles = new Array();
198
+ return {
199
+ name: "css",
200
+ apply: "serve",
201
+ configureServer(server) {
202
+ devServer = server;
203
+ },
204
+ transform(_, source) {
205
+ if (source.match(/\.(css|less|sass|scss|styl|stylus|pcss|postcss|sss)(?:$|\?)/)) {
206
+ if (!styles.includes(source) && !source.includes("?") && !source.includes(".node_modules") && !source.includes("@vite")) styles.push(relative(devServer.config.root, source));
207
+ }
208
+ },
209
+ transformIndexHtml() {
210
+ return [...styles.map((path) => ({
211
+ tag: "link",
212
+ injectTo: "head",
213
+ attrs: {
214
+ rel: "stylesheet",
215
+ href: "/" + path,
216
+ "data-preload": true
217
+ }
218
+ })), {
219
+ tag: "script",
220
+ injectTo: "head",
221
+ attrs: { type: "module" },
222
+ children: `
223
+ const observer = new MutationObserver(() => {
224
+ if (document.querySelector('style[data-vite-dev-id]')) {
225
+ document.querySelectorAll('[data-preload]').forEach((node) => node.remove());
226
+
227
+ observer.disconnect();
228
+ }
229
+ });
230
+
231
+ observer.observe(document.head, { childList: true });
232
+ `
233
+ }];
234
+ }
235
+ };
236
+ }
237
+
238
+ //#endregion
239
+ //#region src/vite/plugins/entry.ts
240
+ function entry() {
241
+ let entryName;
242
+ let entryPath;
243
+ return {
244
+ name: "entry",
245
+ enforce: "pre",
246
+ sharedDuringBuild: true,
247
+ resolveId: {
248
+ filter: { id: /\.html$/ },
249
+ handler(source, importer, options) {
250
+ if (this.environment.name === CLIENT) {
251
+ if (importer && entryPath) {
252
+ const path = join(dirname(importer), source);
253
+ if (existsSync(path)) return path;
254
+ }
255
+ if (options.isEntry) {
256
+ entryName = basename(source);
257
+ entryPath = source;
258
+ return entryName;
259
+ }
260
+ }
261
+ }
262
+ },
263
+ load: {
264
+ filter: { id: /\.html$/ },
265
+ handler(source) {
266
+ if (entryName && entryPath && source === entryName) return readFileSync(entryPath, {
267
+ encoding: "utf-8",
268
+ flag: "r"
269
+ });
270
+ return null;
271
+ }
272
+ }
273
+ };
274
+ }
275
+
276
+ //#endregion
277
+ //#region src/vite/plugins/virtuals.ts
278
+ function virtuals(virtuals$1) {
279
+ const cache = /* @__PURE__ */ new Set();
280
+ return {
281
+ name: "virtuals",
282
+ enforce: "pre",
283
+ sharedDuringBuild: true,
284
+ resolveId(key, importer) {
285
+ if (cache.has(key)) return key;
286
+ if (key.startsWith("#")) {
287
+ const path = "/" + key.slice(1);
288
+ cache.add(path);
289
+ return path;
290
+ }
291
+ return null;
292
+ },
293
+ load(key) {
294
+ const virtual = virtuals$1["#" + key.slice(1)];
295
+ if (typeof virtual === "string") return readFileSync(virtual, {
296
+ encoding: "utf-8",
297
+ flag: "r"
298
+ });
299
+ var code = virtual?.(this.environment.name);
300
+ if (code) return code;
301
+ return null;
302
+ }
303
+ };
304
+ }
305
+
306
+ //#endregion
307
+ //#region package.json
308
+ var name = "revojs";
309
+ var version = "0.1.19";
310
+
311
+ //#endregion
312
+ //#region src/vite/index.ts
313
+ function useKit(app, source) {
314
+ source = source.toString();
315
+ if (source.startsWith("file://")) source = dirname(fileURLToPath(source));
316
+ return {
317
+ source,
318
+ toPath: (...paths) => {
319
+ return join(source, ...paths).split(win32.sep).join(posix.sep);
320
+ },
321
+ addVirtual: (name$1, virtual) => {
322
+ app.virtuals["#virtual/" + name$1] = virtual;
323
+ },
324
+ addAlias: (name$1, path) => {
325
+ app.alias["#alias/" + name$1] = join(source, path).split(win32.sep).join(posix.sep);
326
+ }
327
+ };
328
+ }
329
+ function addRoutes(app, path) {
330
+ app.config.sources.routes?.entries.push(path);
331
+ }
332
+ function addAssets(app, path) {
333
+ app.config.sources.assets?.entries.push(path);
334
+ }
335
+ function revojs(config) {
336
+ const app = createApp(config);
337
+ return [
338
+ {
339
+ name,
340
+ version,
341
+ sharedDuringBuild: true,
342
+ async config() {
343
+ const { toPath, addVirtual } = useKit(app, process.cwd());
344
+ for (const module of app.config.modules) await module.setup?.(app);
345
+ if (app.config.client) addVirtual("client", () => `import client from "${app.config.client}?client"; export default client`);
346
+ if (app.config.server) addVirtual("server", () => `import { createServer } from "revojs"; export default await createServer()`);
347
+ for (const name$1 in app.config.sources) {
348
+ const source = app.config.sources[name$1];
349
+ if (source) addVirtual(name$1, () => {
350
+ const entries = {};
351
+ for (let path of source.entries) {
352
+ path = isAbsolute(path) ? path : toPath(path);
353
+ for (const asset of globSync(source.match, { cwd: path })) entries[asset] = join(path, asset).split(win32.sep).join(posix.sep);
354
+ }
355
+ const content = Object.values(entries).reduce((content$1, path, index) => content$1 + `import $${index} from "${source.resolve?.(path) ?? path}" \n`, "");
356
+ const result = Object.keys(entries).map((name$2, index) => {
357
+ if (entries[name$2]) return `"${name$2}": $${index}`;
358
+ });
359
+ return `${content} export default {${result}}`;
360
+ });
361
+ }
362
+ return {
363
+ appType: "custom",
364
+ optimizeDeps: { exclude: ["revojs"] },
365
+ resolve: { alias: app.alias },
366
+ build: { rollupOptions: { external: app.config.build.externals } },
367
+ builder: { async buildApp(builder) {
368
+ for (const key in builder.environments) {
369
+ const environment = builder.environments[key];
370
+ if (environment) await builder.build(environment);
371
+ }
372
+ } },
373
+ environments: {
374
+ ...app.config.client && { [CLIENT]: {
375
+ consumer: "client",
376
+ resolve: { noExternal: true },
377
+ build: {
378
+ rollupOptions: { input: { index: app.config.client } },
379
+ outDir: "./dist/client",
380
+ copyPublicDir: true,
381
+ emptyOutDir: true
382
+ },
383
+ define: {
384
+ "import.meta.server": false,
385
+ "import.meta.client": true
386
+ }
387
+ } },
388
+ ...app.config.server && { [SERVER]: {
389
+ consumer: "server",
390
+ resolve: {
391
+ noExternal: true,
392
+ conditions: ["import"],
393
+ externalConditions: ["import"]
394
+ },
395
+ build: {
396
+ rollupOptions: { input: { index: app.config.server } },
397
+ outDir: "./dist/server",
398
+ copyPublicDir: false,
399
+ emptyOutDir: true
400
+ },
401
+ define: {
402
+ "import.meta.server": true,
403
+ "import.meta.client": false
404
+ }
405
+ } }
406
+ }
407
+ };
408
+ },
409
+ configResolved(config$1) {
410
+ if (app.config.client === void 0) delete config$1.environments[CLIENT];
411
+ if (app.config.server === void 0) delete config$1.environments[SERVER];
412
+ },
413
+ async configureServer(devServer) {
414
+ const target = devServer.environments[SERVER];
415
+ if (isRunnableDevEnvironment(target)) return () => {
416
+ devServer.middlewares.use(async (request, response, next) => {
417
+ const server = await target.runner.import("#virtual/server").then((module) => module.default);
418
+ if (server) {
419
+ request.url = request.originalUrl;
420
+ const scope = new Scope();
421
+ try {
422
+ scope.setContext(SERVER_CONTEXT, {
423
+ states: {},
424
+ request: fromNodeRequest(request, response),
425
+ response: { headers: new Headers() },
426
+ variables: process.env
427
+ });
428
+ var result = await invoke(scope, app.config.development.middlewares.concat({ fetch: server.fetch }));
429
+ if (result) await toNodeRequest(result, response);
430
+ } finally {
431
+ scope.stop();
432
+ }
433
+ }
434
+ next();
435
+ });
436
+ };
437
+ }
438
+ },
439
+ virtuals(app.virtuals),
440
+ client(),
441
+ entry(),
442
+ css()
443
+ ];
444
+ }
445
+
446
+ //#endregion
447
+ export { addAssets, addRoutes, revojs, useKit };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "revojs",
3
- "version": "0.1.18",
3
+ "version": "0.1.19",
4
4
  "type": "module",
5
5
  "repository": "coverbase/revojs",
6
6
  "license": "MIT",
@@ -9,6 +9,10 @@
9
9
  "types": "./dist/index.d.ts",
10
10
  "import": "./dist/index.js"
11
11
  },
12
+ "./vite": {
13
+ "types": "./dist/vite/index.d.ts",
14
+ "import": "./dist/vite/index.js"
15
+ },
12
16
  "./types": {
13
17
  "types": "./src/types/index.d.ts"
14
18
  }
@@ -24,8 +28,13 @@
24
28
  "build": "tsdown",
25
29
  "watch": "tsdown -w"
26
30
  },
31
+ "dependencies": {
32
+ "tinyglobby": "^0.2.15",
33
+ "vite": "^7.1.10"
34
+ },
27
35
  "devDependencies": {
28
36
  "@revojs/tsconfig": "*",
37
+ "@types/node": "^24.7.2",
29
38
  "tsdown": "^0.15.1"
30
39
  }
31
40
  }