revojs 0.1.20 → 0.1.22
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/{app-C4yy27ko.js → app-CZxSS8Ok.js} +36 -25
- package/dist/{index-D-rKmbh3.d.ts → index-CUwjaRno.d.ts} +11 -14
- package/dist/index.d.ts +2 -2
- package/dist/index.js +3 -3
- package/dist/kit/index.d.ts +1 -1
- package/dist/vite/index.d.ts +1 -1
- package/dist/vite/index.js +10 -14
- package/package.json +1 -1
|
@@ -50,7 +50,7 @@ var Router = class extends Radix {
|
|
|
50
50
|
const { pathname } = useUrl(scope);
|
|
51
51
|
const context = {
|
|
52
52
|
route: this.rootNode,
|
|
53
|
-
segments:
|
|
53
|
+
segments: (request.method.toLowerCase() + pathname).split("/"),
|
|
54
54
|
parameters: {}
|
|
55
55
|
};
|
|
56
56
|
const invoke$1 = (node, index) => {
|
|
@@ -71,7 +71,7 @@ var Router = class extends Radix {
|
|
|
71
71
|
if (node.children[WILDCARD]) {
|
|
72
72
|
const wildcardNode = node.children[WILDCARD];
|
|
73
73
|
context.parameters[wildcardNode.parameter] = segment;
|
|
74
|
-
return wildcardNode.value ?? invoke$1(wildcardNode,
|
|
74
|
+
return wildcardNode.value ?? invoke$1(wildcardNode, context.segments.length);
|
|
75
75
|
}
|
|
76
76
|
};
|
|
77
77
|
const route = invoke$1(this.rootNode, 0);
|
|
@@ -117,7 +117,7 @@ function withQuery(input, query) {
|
|
|
117
117
|
}
|
|
118
118
|
function useCookies(scope, schema) {
|
|
119
119
|
const { request } = useServer(scope);
|
|
120
|
-
const entries = (
|
|
120
|
+
const entries = (isClient ? document.cookie : request.headers.get("Cookie") ?? "").split("; ").reduce((result, cookie) => {
|
|
121
121
|
const [name, value] = cookie.split("=");
|
|
122
122
|
if (name && value) result[name] = decodeURIComponent(value);
|
|
123
123
|
return result;
|
|
@@ -144,11 +144,11 @@ function setCookie(scope, name, value, options) {
|
|
|
144
144
|
if (options?.priority) cookie += `; Priority=${options.priority}`;
|
|
145
145
|
if (options?.sameSite) cookie += `; SameSite=${options.sameSite}`;
|
|
146
146
|
if (options?.secure) cookie += `; Secure`;
|
|
147
|
-
if (
|
|
147
|
+
if (isServer) response.headers.append("Set-Cookie", cookie);
|
|
148
148
|
else document.cookie = cookie;
|
|
149
149
|
}
|
|
150
150
|
function getState(scope, name) {
|
|
151
|
-
if (
|
|
151
|
+
if (isServer) {
|
|
152
152
|
const { states } = useServer(scope);
|
|
153
153
|
return states[name];
|
|
154
154
|
} else {
|
|
@@ -160,7 +160,7 @@ function getState(scope, name) {
|
|
|
160
160
|
}
|
|
161
161
|
}
|
|
162
162
|
function setState(scope, name, value) {
|
|
163
|
-
if (
|
|
163
|
+
if (isServer) {
|
|
164
164
|
const { states } = useServer(scope);
|
|
165
165
|
states[name] = value;
|
|
166
166
|
} else {
|
|
@@ -207,9 +207,8 @@ function mimeType(file) {
|
|
|
207
207
|
return mimeTypes[extension ?? ""] ?? "text/plain";
|
|
208
208
|
}
|
|
209
209
|
function toRoutePath(path) {
|
|
210
|
-
const
|
|
211
|
-
|
|
212
|
-
return result.concat(properties).join("/");
|
|
210
|
+
const segments = path.toLowerCase().replaceAll(/\[\.\.\.(.*?)\]/g, (_, value) => WILDCARD + value).replaceAll(/\[(.*?)\]/g, (_, value) => PARAMETER + value).split(".");
|
|
211
|
+
return [(segments.shift() ?? "").split("/").filter((value) => value !== "index").join("/"), ...segments];
|
|
213
212
|
}
|
|
214
213
|
async function invoke(scope, pipeline, index = 0) {
|
|
215
214
|
return await pipeline.at(index)?.fetch(scope, async () => await invoke(scope, pipeline, index + 1));
|
|
@@ -218,7 +217,10 @@ async function createServer() {
|
|
|
218
217
|
const router = new Router();
|
|
219
218
|
const middlewares = new Array();
|
|
220
219
|
const routes = await import("#virtual/routes").then((module) => Object.entries(module.default));
|
|
221
|
-
for (const [path, route] of routes)
|
|
220
|
+
for (const [path, route] of routes) {
|
|
221
|
+
const [result, method] = toRoutePath(path);
|
|
222
|
+
router.use(method + "/" + result, route);
|
|
223
|
+
}
|
|
222
224
|
middlewares.push(router);
|
|
223
225
|
return {
|
|
224
226
|
router,
|
|
@@ -282,22 +284,34 @@ const mimeTypes = {
|
|
|
282
284
|
exe: "application/vnd.microsoft.portable-executable",
|
|
283
285
|
apk: "application/vnd.android.package-archive"
|
|
284
286
|
};
|
|
287
|
+
const isServer = import.meta.server ?? globalThis?.import?.meta?.server;
|
|
288
|
+
const isClient = import.meta.client ?? globalThis?.import?.meta?.client;
|
|
285
289
|
|
|
286
290
|
//#endregion
|
|
287
291
|
//#region src/shared/index.ts
|
|
288
|
-
var
|
|
289
|
-
constructor() {
|
|
290
|
-
super("stop");
|
|
291
|
-
}
|
|
292
|
-
};
|
|
293
|
-
var Scope = class extends EventTarget {
|
|
292
|
+
var Scope = class {
|
|
294
293
|
parentScope;
|
|
295
294
|
context;
|
|
295
|
+
hooks;
|
|
296
296
|
constructor(parentScope) {
|
|
297
|
-
super();
|
|
298
297
|
this.parentScope = parentScope;
|
|
299
|
-
this.parentScope?.onStop(() => this.stop());
|
|
300
298
|
this.context = {};
|
|
299
|
+
this.hooks = {};
|
|
300
|
+
}
|
|
301
|
+
registerHook(name, invoke$1) {
|
|
302
|
+
const targets = this.hooks[name] ?? [];
|
|
303
|
+
targets.push(invoke$1);
|
|
304
|
+
this.hooks[name] = targets;
|
|
305
|
+
return () => {
|
|
306
|
+
const index = this.hooks[name]?.indexOf(invoke$1);
|
|
307
|
+
if (index && index >= 0) this.hooks[name]?.splice(index, 1);
|
|
308
|
+
if (this.hooks[name]?.length === 0) delete this.hooks[name];
|
|
309
|
+
};
|
|
310
|
+
}
|
|
311
|
+
async dispatchHook(name, ...inputs) {
|
|
312
|
+
const results = new Array();
|
|
313
|
+
for (const invoke$1 of this.hooks[name] ?? []) results.push(await invoke$1(...inputs));
|
|
314
|
+
return results;
|
|
301
315
|
}
|
|
302
316
|
getContext(input) {
|
|
303
317
|
let scope = this;
|
|
@@ -310,13 +324,10 @@ var Scope = class extends EventTarget {
|
|
|
310
324
|
setContext(input, value) {
|
|
311
325
|
this.context[input] = value;
|
|
312
326
|
}
|
|
313
|
-
onStop(input) {
|
|
314
|
-
this.addEventListener("stop", input, { once: true });
|
|
315
|
-
}
|
|
316
|
-
stop() {
|
|
317
|
-
return this.dispatchEvent(new StopEvent());
|
|
318
|
-
}
|
|
319
327
|
};
|
|
328
|
+
function defineHook(name) {
|
|
329
|
+
return name;
|
|
330
|
+
}
|
|
320
331
|
function defineContext(name) {
|
|
321
332
|
return name;
|
|
322
333
|
}
|
|
@@ -365,4 +376,4 @@ const SERVER = "ssr";
|
|
|
365
376
|
const CLIENT = "client";
|
|
366
377
|
|
|
367
378
|
//#endregion
|
|
368
|
-
export { CLIENT, PARAMETER, ROUTER_CONTEXT, Radix, Router, SERVER, SERVER_CONTEXT, STATES, Scope,
|
|
379
|
+
export { CLIENT, PARAMETER, ROUTER_CONTEXT, Radix, Router, SERVER, SERVER_CONTEXT, STATES, Scope, WILDCARD, createApp, createServer, defineContext, defineHook, defineMiddleware, defineRoute, getState, invoke, isClient, isFailure, isServer, mergeObjects, mimeType, mimeTypes, parseSchema, sendBadRequest, sendHtml, sendJson, sendRedirect, sendText, sendUnauthorized, setCookie, setState, toRoutePath, useCookies, useHeaders, useQuery, useRouter, useServer, useSetCookies, useUrl, withQuery };
|
|
@@ -2,7 +2,8 @@
|
|
|
2
2
|
type Descriptor<T> = string & {
|
|
3
3
|
[descriptor]: T;
|
|
4
4
|
};
|
|
5
|
-
type
|
|
5
|
+
type Invoke = (...inputs: Array<any>) => any;
|
|
6
|
+
type Context = Record<string, any>;
|
|
6
7
|
type Output<T> = Success<T> | Failure;
|
|
7
8
|
type InferInput<T extends Schema> = NonNullable<T["~standard"]["types"]>["input"];
|
|
8
9
|
type InferOutput<T extends Schema> = NonNullable<T["~standard"]["types"]>["output"];
|
|
@@ -25,28 +26,22 @@ interface Schema<T = unknown, TOutput = T> {
|
|
|
25
26
|
};
|
|
26
27
|
};
|
|
27
28
|
}
|
|
28
|
-
declare class
|
|
29
|
-
constructor();
|
|
30
|
-
}
|
|
31
|
-
declare class Scope extends EventTarget {
|
|
29
|
+
declare class Scope {
|
|
32
30
|
parentScope?: Scope;
|
|
33
31
|
readonly context: Context;
|
|
32
|
+
readonly hooks: Record<string, Array<Invoke>>;
|
|
34
33
|
constructor(parentScope?: Scope);
|
|
34
|
+
registerHook<T extends Invoke>(name: Descriptor<T>, invoke: T): () => void;
|
|
35
|
+
dispatchHook<T extends Invoke>(name: Descriptor<T>, ...inputs: Parameters<T>): Promise<Awaited<ReturnType<T>>[]>;
|
|
35
36
|
getContext<T>(input: Descriptor<T>): T;
|
|
36
37
|
setContext<T>(input: Descriptor<T>, value: T): void;
|
|
37
|
-
onStop(input: (event: StopEvent) => void): void;
|
|
38
|
-
stop(): boolean;
|
|
39
38
|
}
|
|
39
|
+
declare function defineHook<T extends Invoke>(name: string): Descriptor<T>;
|
|
40
40
|
declare function defineContext<T>(name: string): Descriptor<T>;
|
|
41
41
|
declare function isFailure<T>(result: Output<T>): result is Failure;
|
|
42
42
|
declare function parseSchema<T extends Schema>(scope: Scope, schema: T, value: unknown): InferOutput<T>;
|
|
43
43
|
declare function mergeObjects<TBase, TInput>(base: TBase, input: TInput): TBase & TInput;
|
|
44
44
|
declare const descriptor: unique symbol;
|
|
45
|
-
declare global {
|
|
46
|
-
interface ElementEventMap {
|
|
47
|
-
stop: StopEvent;
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
45
|
//#endregion
|
|
51
46
|
//#region src/server/index.d.ts
|
|
52
47
|
type CookiePriority = "Low" | "Medium" | "High";
|
|
@@ -143,7 +138,7 @@ declare function sendRedirect(scope: Scope, path: string, config?: Mergeable<Res
|
|
|
143
138
|
declare function sendBadRequest(scope: Scope, text: string, config?: Mergeable<ResponseConfig>): Response;
|
|
144
139
|
declare function sendUnauthorized(scope: Scope, config?: Mergeable<ResponseConfig>): Response;
|
|
145
140
|
declare function mimeType(file: string): MimeType;
|
|
146
|
-
declare function toRoutePath(path: string): string;
|
|
141
|
+
declare function toRoutePath(path: string): [string, ...Array<string>];
|
|
147
142
|
declare function invoke(scope: Scope, pipeline: Array<Middleware>, index?: number): Promise<Result>;
|
|
148
143
|
declare function createServer(): Promise<Server>;
|
|
149
144
|
declare const ROUTER_CONTEXT: Descriptor<RouterContext>;
|
|
@@ -152,6 +147,8 @@ declare const WILDCARD = "$";
|
|
|
152
147
|
declare const PARAMETER = ":";
|
|
153
148
|
declare let STATES: States;
|
|
154
149
|
declare const mimeTypes: Record<string, MimeType>;
|
|
150
|
+
declare const isServer: boolean;
|
|
151
|
+
declare const isClient: boolean;
|
|
155
152
|
//#endregion
|
|
156
153
|
//#region src/app/index.d.ts
|
|
157
154
|
type Environment = typeof CLIENT | typeof SERVER;
|
|
@@ -191,4 +188,4 @@ declare const CLIENT = "client";
|
|
|
191
188
|
//#region src/client/index.d.ts
|
|
192
189
|
declare function $fetch<T>(scope: Scope, input: string | URL, options?: RequestInit): Promise<T>;
|
|
193
190
|
//#endregion
|
|
194
|
-
export { $fetch, App, BuildConfig, CLIENT, Config, Context, CookieOptions, CookiePriority, CookieSameSite, Descriptor, DevelopmentConfig, Encoding, Environment, Failure, HttpMethod, InferInput, InferOutput, Issue, Mergeable, Middleware, MimeType, Module, Node, Output, PARAMETER, ParameterNode, PathNode, ROUTER_CONTEXT, Radix, ResponseConfig, Result, Route, Router, RouterContext, SERVER, SERVER_CONTEXT, STATES, Schema, Scope, Server, ServerContext, Source, States, StatusCode,
|
|
191
|
+
export { $fetch, App, BuildConfig, CLIENT, Config, Context, CookieOptions, CookiePriority, CookieSameSite, Descriptor, DevelopmentConfig, Encoding, Environment, Failure, HttpMethod, InferInput, InferOutput, Invoke, Issue, Mergeable, Middleware, MimeType, Module, Node, Output, PARAMETER, ParameterNode, PathNode, ROUTER_CONTEXT, Radix, ResponseConfig, Result, Route, Router, RouterContext, SERVER, SERVER_CONTEXT, STATES, Schema, Scope, Server, ServerContext, Source, States, StatusCode, Success, Virtual, WILDCARD, WildcardNode, createApp, createServer, defineContext, defineHook, defineMiddleware, defineRoute, getState, invoke, isClient, isFailure, isServer, mergeObjects, mimeType, mimeTypes, parseSchema, sendBadRequest, sendHtml, sendJson, sendRedirect, sendText, sendUnauthorized, setCookie, setState, toRoutePath, useCookies, useHeaders, useQuery, useRouter, useServer, useSetCookies, useUrl, withQuery };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { $fetch, App, BuildConfig, CLIENT, Config, Context, CookieOptions, CookiePriority, CookieSameSite, Descriptor, DevelopmentConfig, Encoding, Environment, Failure, HttpMethod, InferInput, InferOutput, Issue, Mergeable, Middleware, MimeType, Module, Node, Output, PARAMETER, ParameterNode, PathNode, ROUTER_CONTEXT, Radix, ResponseConfig, Result, Route, Router, RouterContext, SERVER, SERVER_CONTEXT, STATES, Schema, Scope, Server, ServerContext, Source, States, StatusCode,
|
|
2
|
-
export { $fetch, App, BuildConfig, CLIENT, Config, Context, CookieOptions, CookiePriority, CookieSameSite, Descriptor, DevelopmentConfig, Encoding, Environment, Failure, HttpMethod, InferInput, InferOutput, Issue, Mergeable, Middleware, MimeType, Module, Node, Output, PARAMETER, ParameterNode, PathNode, ROUTER_CONTEXT, Radix, ResponseConfig, Result, Route, Router, RouterContext, SERVER, SERVER_CONTEXT, STATES, Schema, Scope, Server, ServerContext, Source, States, StatusCode,
|
|
1
|
+
import { $fetch, App, BuildConfig, CLIENT, Config, Context, CookieOptions, CookiePriority, CookieSameSite, Descriptor, DevelopmentConfig, Encoding, Environment, Failure, HttpMethod, InferInput, InferOutput, Invoke, Issue, Mergeable, Middleware, MimeType, Module, Node, Output, PARAMETER, ParameterNode, PathNode, ROUTER_CONTEXT, Radix, ResponseConfig, Result, Route, Router, RouterContext, SERVER, SERVER_CONTEXT, STATES, Schema, Scope, Server, ServerContext, Source, States, StatusCode, Success, Virtual, WILDCARD, WildcardNode, createApp, createServer, defineContext, defineHook, defineMiddleware, defineRoute, getState, invoke, isClient, isFailure, isServer, mergeObjects, mimeType, mimeTypes, parseSchema, sendBadRequest, sendHtml, sendJson, sendRedirect, sendText, sendUnauthorized, setCookie, setState, toRoutePath, useCookies, useHeaders, useQuery, useRouter, useServer, useSetCookies, useUrl, withQuery } from "./index-CUwjaRno.js";
|
|
2
|
+
export { $fetch, App, BuildConfig, CLIENT, Config, Context, CookieOptions, CookiePriority, CookieSameSite, Descriptor, DevelopmentConfig, Encoding, Environment, Failure, HttpMethod, InferInput, InferOutput, Invoke, Issue, Mergeable, Middleware, MimeType, Module, Node, Output, PARAMETER, ParameterNode, PathNode, ROUTER_CONTEXT, Radix, ResponseConfig, Result, Route, Router, RouterContext, SERVER, SERVER_CONTEXT, STATES, Schema, Scope, Server, ServerContext, Source, States, StatusCode, Success, Virtual, WILDCARD, WildcardNode, createApp, createServer, defineContext, defineHook, defineMiddleware, defineRoute, getState, invoke, isClient, isFailure, isServer, mergeObjects, mimeType, mimeTypes, parseSchema, sendBadRequest, sendHtml, sendJson, sendRedirect, sendText, sendUnauthorized, setCookie, setState, toRoutePath, useCookies, useHeaders, useQuery, useRouter, useServer, useSetCookies, useUrl, withQuery };
|
package/dist/index.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { CLIENT, PARAMETER, ROUTER_CONTEXT, Radix, Router, SERVER, SERVER_CONTEXT, STATES, Scope,
|
|
1
|
+
import { CLIENT, PARAMETER, ROUTER_CONTEXT, Radix, Router, SERVER, SERVER_CONTEXT, STATES, Scope, WILDCARD, createApp, createServer, defineContext, defineHook, defineMiddleware, defineRoute, getState, invoke, isClient, isFailure, isServer, mergeObjects, mimeType, mimeTypes, parseSchema, sendBadRequest, sendHtml, sendJson, sendRedirect, sendText, sendUnauthorized, setCookie, setState, toRoutePath, useCookies, useHeaders, useQuery, useRouter, useServer, useSetCookies, useUrl, withQuery } from "./app-CZxSS8Ok.js";
|
|
2
2
|
|
|
3
3
|
//#region src/client/index.ts
|
|
4
4
|
async function $fetch(scope, input, options) {
|
|
5
5
|
let response;
|
|
6
|
-
if (
|
|
6
|
+
if (isServer) {
|
|
7
7
|
const { states, request, variables } = useServer(scope);
|
|
8
8
|
const next = new Scope();
|
|
9
9
|
const url = new URL(input.toString(), request.url);
|
|
@@ -25,4 +25,4 @@ async function $fetch(scope, input, options) {
|
|
|
25
25
|
}
|
|
26
26
|
|
|
27
27
|
//#endregion
|
|
28
|
-
export { $fetch, CLIENT, PARAMETER, ROUTER_CONTEXT, Radix, Router, SERVER, SERVER_CONTEXT, STATES, Scope,
|
|
28
|
+
export { $fetch, CLIENT, PARAMETER, ROUTER_CONTEXT, Radix, Router, SERVER, SERVER_CONTEXT, STATES, Scope, WILDCARD, createApp, createServer, defineContext, defineHook, defineMiddleware, defineRoute, getState, invoke, isClient, isFailure, isServer, mergeObjects, mimeType, mimeTypes, parseSchema, sendBadRequest, sendHtml, sendJson, sendRedirect, sendText, sendUnauthorized, setCookie, setState, toRoutePath, useCookies, useHeaders, useQuery, useRouter, useServer, useSetCookies, useUrl, withQuery };
|
package/dist/kit/index.d.ts
CHANGED
package/dist/vite/index.d.ts
CHANGED
package/dist/vite/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { CLIENT, SERVER, SERVER_CONTEXT, Scope, createApp, invoke } from "../app-
|
|
1
|
+
import { CLIENT, SERVER, SERVER_CONTEXT, Scope, createApp, invoke } from "../app-CZxSS8Ok.js";
|
|
2
2
|
import { useKit } from "../kit-Bzr1NqHb.js";
|
|
3
3
|
import { basename, dirname, isAbsolute, join, posix, relative, win32 } from "path";
|
|
4
4
|
import { isRunnableDevEnvironment } from "vite";
|
|
@@ -307,7 +307,7 @@ function virtuals(virtuals$1) {
|
|
|
307
307
|
//#endregion
|
|
308
308
|
//#region package.json
|
|
309
309
|
var name = "revojs";
|
|
310
|
-
var version = "0.1.
|
|
310
|
+
var version = "0.1.22";
|
|
311
311
|
|
|
312
312
|
//#endregion
|
|
313
313
|
//#region src/vite/index.ts
|
|
@@ -406,18 +406,14 @@ function revojs(config) {
|
|
|
406
406
|
if (server) {
|
|
407
407
|
request.url = request.originalUrl;
|
|
408
408
|
const scope = new Scope();
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
if (result) await toNodeRequest(result, response);
|
|
418
|
-
} finally {
|
|
419
|
-
scope.stop();
|
|
420
|
-
}
|
|
409
|
+
scope.setContext(SERVER_CONTEXT, {
|
|
410
|
+
states: {},
|
|
411
|
+
request: fromNodeRequest(request, response),
|
|
412
|
+
response: { headers: new Headers() },
|
|
413
|
+
variables: process.env
|
|
414
|
+
});
|
|
415
|
+
var result = await invoke(scope, app.config.development.middlewares.concat({ fetch: server.fetch }));
|
|
416
|
+
if (result) await toNodeRequest(result, response);
|
|
421
417
|
}
|
|
422
418
|
next();
|
|
423
419
|
});
|