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