@taujs/server 0.4.0 → 0.4.2

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,209 @@
1
+ import { FastifyRequest } from 'fastify';
2
+ import { PluginOption } from 'vite';
3
+
4
+ declare const DEBUG_CATEGORIES: readonly ["auth", "routes", "errors", "vite", "network", "ssr"];
5
+ type DebugCategory = (typeof DEBUG_CATEGORIES)[number];
6
+ type DebugConfig = boolean | DebugCategory[] | ({
7
+ all?: boolean;
8
+ } & Partial<Record<DebugCategory, boolean>>);
9
+ type LogLevel = 'debug' | 'info' | 'warn' | 'error';
10
+ interface BaseLogger {
11
+ debug?(meta?: Record<string, unknown>, message?: string): void;
12
+ info?(meta?: Record<string, unknown>, message?: string): void;
13
+ warn?(meta?: Record<string, unknown>, message?: string): void;
14
+ error?(meta?: Record<string, unknown>, message?: string): void;
15
+ child?(context: Record<string, unknown>): BaseLogger;
16
+ }
17
+ interface Logs extends BaseLogger {
18
+ debug(meta?: unknown, message?: string): void;
19
+ debug(category: DebugCategory, meta?: unknown, message?: string): void;
20
+ info(meta?: unknown, message?: string): void;
21
+ warn(meta?: unknown, message?: string): void;
22
+ error(meta?: unknown, message?: string): void;
23
+ child(context: Record<string, unknown>): Logs;
24
+ isDebugEnabled(category: DebugCategory): boolean;
25
+ }
26
+ declare class Logger implements Logs {
27
+ private config;
28
+ private debugEnabled;
29
+ private context;
30
+ constructor(config?: {
31
+ custom?: BaseLogger;
32
+ context?: Record<string, unknown>;
33
+ minLevel?: LogLevel;
34
+ includeStack?: boolean | ((level: LogLevel) => boolean);
35
+ includeContext?: boolean | ((level: LogLevel) => boolean);
36
+ singleLine?: boolean;
37
+ });
38
+ child(context: Record<string, unknown>): Logger;
39
+ configure(debug?: DebugConfig): void;
40
+ isDebugEnabled(category: DebugCategory): boolean;
41
+ private shouldEmit;
42
+ private shouldIncludeStack;
43
+ private stripStacks;
44
+ private formatTimestamp;
45
+ private emit;
46
+ info(meta?: unknown, message?: string): void;
47
+ warn(meta?: unknown, message?: string): void;
48
+ error(meta?: unknown, message?: string): void;
49
+ debug(category: DebugCategory, meta?: unknown, message?: string): void;
50
+ }
51
+ declare function createLogger(opts?: {
52
+ debug?: DebugConfig | string | boolean;
53
+ custom?: BaseLogger;
54
+ context?: Record<string, unknown>;
55
+ minLevel?: LogLevel;
56
+ includeStack?: boolean | ((level: LogLevel) => boolean);
57
+ includeContext?: boolean | ((level: LogLevel) => boolean);
58
+ singleLine?: boolean;
59
+ }): Logger;
60
+
61
+ type JsonPrimitive = string | number | boolean | null;
62
+ type JsonValue = JsonPrimitive | JsonValue[] | {
63
+ [k: string]: JsonValue;
64
+ };
65
+ type JsonObject = {
66
+ [k: string]: JsonValue;
67
+ };
68
+ type NarrowSchema<T> = {
69
+ parse: (u: unknown) => T;
70
+ } | ((u: unknown) => T);
71
+ type ServiceContext = {
72
+ signal?: AbortSignal;
73
+ deadlineMs?: number;
74
+ traceId?: string;
75
+ logger?: Logs;
76
+ user?: {
77
+ id: string;
78
+ roles: string[];
79
+ } | null;
80
+ };
81
+ type ServiceMethod<P, R extends JsonObject = JsonObject> = (params: P, ctx: ServiceContext) => Promise<R>;
82
+ type ServiceDefinition = Readonly<Record<string, ServiceMethod<any, JsonObject>>>;
83
+ type ServiceRegistry = Readonly<Record<string, ServiceDefinition>>;
84
+ type ServiceDescriptor = {
85
+ serviceName: string;
86
+ serviceMethod: string;
87
+ args?: JsonObject;
88
+ };
89
+ declare function defineService<T extends Record<string, ServiceMethod<any, JsonObject> | {
90
+ handler: ServiceMethod<any, JsonObject>;
91
+ params?: NarrowSchema<any>;
92
+ result?: NarrowSchema<any>;
93
+ }>>(spec: T): { [K in keyof T]: T[K] extends ServiceMethod<infer P, infer R> ? ServiceMethod<P, R> : T[K] extends {
94
+ handler: ServiceMethod<infer P, infer R_1>;
95
+ } ? ServiceMethod<P, R_1> : never; };
96
+ declare const defineServiceRegistry: <R extends ServiceRegistry>(registry: R) => R;
97
+
98
+ type RequestContext<L extends Logs = Logs> = {
99
+ traceId: string;
100
+ logger: L;
101
+ headers: Record<string, string>;
102
+ };
103
+
104
+ type RouteCSPConfig = {
105
+ disabled?: boolean;
106
+ mode?: 'merge' | 'replace';
107
+ directives?: CSPDirectives | ((args: {
108
+ url: string;
109
+ params: PathToRegExpParams;
110
+ headers: FastifyRequest['headers'];
111
+ req: FastifyRequest;
112
+ }) => CSPDirectives);
113
+ generateCSP?: (directives: CSPDirectives, nonce: string, req: FastifyRequest) => string;
114
+ reportOnly?: boolean;
115
+ };
116
+ type BaseMiddleware = {
117
+ auth?: {
118
+ redirect?: string;
119
+ roles?: string[];
120
+ strategy?: string;
121
+ };
122
+ csp?: RouteCSPConfig | false;
123
+ };
124
+ type DataResult = Record<string, unknown> | ServiceDescriptor;
125
+ type DataHandler<Params extends PathToRegExpParams, L extends Logs = Logs> = (params: Params, ctx: RequestContext<L> & {
126
+ [key: string]: unknown;
127
+ }) => Promise<DataResult>;
128
+ type PathToRegExpParams = Partial<Record<string, string | string[]>>;
129
+ type RouteAttributes<Params extends PathToRegExpParams = PathToRegExpParams, Middleware = BaseMiddleware, L extends Logs = Logs> = {
130
+ render: 'ssr';
131
+ hydrate?: boolean;
132
+ meta?: Record<string, unknown>;
133
+ middleware?: Middleware;
134
+ data?: DataHandler<Params, L>;
135
+ } | {
136
+ render: 'streaming';
137
+ hydrate?: boolean;
138
+ meta: Record<string, unknown>;
139
+ middleware?: Middleware;
140
+ data?: DataHandler<Params, L>;
141
+ };
142
+ type Route<Params extends PathToRegExpParams = PathToRegExpParams> = {
143
+ attr?: RouteAttributes<Params>;
144
+ path: string;
145
+ appId?: string;
146
+ };
147
+ interface InitialRouteParams extends Record<string, unknown> {
148
+ serviceName?: string;
149
+ serviceMethod?: string;
150
+ }
151
+
152
+ type CSPDirectives = Record<string, string[]>;
153
+
154
+ type CSPViolationReport = {
155
+ 'document-uri': string;
156
+ 'violated-directive': string;
157
+ 'blocked-uri'?: string;
158
+ 'source-file'?: string;
159
+ 'line-number'?: number;
160
+ 'column-number'?: number;
161
+ 'script-sample'?: string;
162
+ 'original-policy'?: string;
163
+ disposition?: 'enforce' | 'report';
164
+ };
165
+
166
+ /**
167
+ * τjs [ taujs ] Orchestration System
168
+ * (c) 2024-present Aoede Ltd
169
+ * Author: John Smith
170
+ *
171
+ * Licensed under the MIT License - attribution appreciated.
172
+ * Part of the τjs [ taujs ] system for declarative, build-time orchestration of microfrontend applications,
173
+ * including CSR, SSR, streaming, and middleware composition.
174
+ */
175
+
176
+ type SecurityConfig = {
177
+ csp?: {
178
+ defaultMode?: 'merge' | 'replace';
179
+ directives?: CSPDirectives;
180
+ generateCSP?: (directives: CSPDirectives, nonce: string, req?: FastifyRequest) => string;
181
+ reporting?: {
182
+ endpoint: string;
183
+ onViolation?: (report: CSPViolationReport, req: FastifyRequest) => void;
184
+ reportOnly?: boolean;
185
+ };
186
+ };
187
+ };
188
+ type AppRoute = Omit<Route<PathToRegExpParams>, 'appId'> & {
189
+ attr?: RouteAttributes<PathToRegExpParams>;
190
+ };
191
+ type AppConfig = {
192
+ appId: string;
193
+ entryPoint: string;
194
+ plugins?: PluginOption[];
195
+ routes?: AppRoute[];
196
+ };
197
+ type TaujsConfig = {
198
+ server?: {
199
+ host?: string;
200
+ port?: number;
201
+ hmrPort?: number;
202
+ };
203
+ security?: SecurityConfig;
204
+ apps: AppConfig[];
205
+ };
206
+
207
+ declare function defineConfig<T extends TaujsConfig>(config: T): T;
208
+
209
+ export { type AppConfig as A, type BaseLogger as B, type DebugConfig as D, type InitialRouteParams as I, type ServiceRegistry as S, type TaujsConfig as T, type SecurityConfig as a, type AppRoute as b, createLogger as c, defineConfig as d, defineServiceRegistry as e, defineService as f, type ServiceContext as g };
package/dist/Config.d.ts CHANGED
@@ -1,3 +1,3 @@
1
1
  import 'fastify';
2
2
  import 'vite';
3
- export { A as AppConfig, q as AppRoute, p as SecurityConfig, T as TaujsConfig, r as defineConfig, t as defineService, s as defineServiceRegistry } from './Config-CjwAJCfZ.js';
3
+ export { A as AppConfig, b as AppRoute, a as SecurityConfig, g as ServiceContext, T as TaujsConfig, d as defineConfig, f as defineService, e as defineServiceRegistry } from './Config-BuUuMjmO.js';
package/dist/Config.js CHANGED
@@ -1,19 +1,26 @@
1
1
  // src/utils/DataServices.ts
2
- var defineService = (spec) => {
2
+ import { performance } from "perf_hooks";
3
+ var runSchema = (schema, input) => {
4
+ if (!schema) return input;
5
+ return typeof schema.parse === "function" ? schema.parse(input) : schema(input);
6
+ };
7
+ function defineService(spec) {
3
8
  const out = {};
4
- for (const [k, v] of Object.entries(spec)) {
9
+ for (const [name, v] of Object.entries(spec)) {
5
10
  if (typeof v === "function") {
6
- out[k] = { handler: v };
11
+ out[name] = v;
7
12
  } else {
8
- out[k] = {
9
- handler: v.handler,
10
- parsers: v.parsers ?? (v.params || v.result ? { params: v.params, result: v.result } : void 0)
13
+ const { handler, params: paramsSchema, result: resultSchema } = v;
14
+ out[name] = async (params, ctx) => {
15
+ const p = runSchema(paramsSchema, params);
16
+ const r = await handler(p, ctx);
17
+ return runSchema(resultSchema, r);
11
18
  };
12
19
  }
13
20
  }
14
- return out;
15
- };
16
- var defineServiceRegistry = (registry) => registry;
21
+ return Object.freeze(out);
22
+ }
23
+ var defineServiceRegistry = (registry) => Object.freeze(Object.fromEntries(Object.entries(registry).map(([k, v]) => [k, Object.freeze(v)])));
17
24
 
18
25
  // src/Config.ts
19
26
  function defineConfig(config) {
package/dist/index.d.ts CHANGED
@@ -1,48 +1,13 @@
1
- import { S as SSRServerOptions, T as TaujsConfig, a as ServiceRegistry, D as DebugConfig, B as BaseLogger } from './Config-CjwAJCfZ.js';
2
- export { h as BaseMiddleware, C as Config, k as DataHandler, j as DataResult, G as GenericPlugin, I as InitialRouteParams, d as Manifest, M as ManifestEntry, l as PathToRegExpParams, P as ProcessedConfig, b as RenderCallbacks, g as RenderModule, e as RenderSSR, f as RenderStream, n as Route, m as RouteAttributes, R as RouteCSPConfig, o as RoutePathsAndAttributes, c as SSRManifest, i as ServiceCall } from './Config-CjwAJCfZ.js';
3
- import { FastifyPluginAsync, FastifyInstance, FastifyPluginCallback } from 'fastify';
1
+ import { FastifyPluginCallback, FastifyPluginAsync, FastifyInstance } from 'fastify';
2
+ import { T as TaujsConfig, S as ServiceRegistry, D as DebugConfig, B as BaseLogger, A as AppConfig } from './Config-BuUuMjmO.js';
3
+ export { I as InitialRouteParams, c as createLogger } from './Config-BuUuMjmO.js';
4
4
  import 'vite';
5
5
 
6
- declare module 'fastify' {
7
- interface FastifyRequest {
8
- cspNonce?: string;
9
- }
10
-
11
- interface FastifyInstance {
12
- /**
13
- * Optional authentication hook to be used by the taujs SSRServer.
14
- * This method must be decorated by the user when using auth middleware in `taujs.config.ts`.
15
- *
16
- * Example usage:
17
- * ```ts
18
- * fastify.decorate('authenticate', async function (req, reply) {
19
- * await req.jwtVerify();
20
- * });
21
- * ```
22
- */
23
- authenticate: (req: FastifyRequest, reply: FastifyReply) => Promise<void>;
24
- showBanner(): void;
25
- cspNonce?: string;
26
- }
27
- }
28
-
29
- declare const TEMPLATE: {
30
- readonly defaultEntryClient: "entry-client";
31
- readonly defaultEntryServer: "entry-server";
32
- readonly defaultHtmlTemplate: "index.html";
6
+ type StaticMountEntry = {
7
+ plugin: FastifyPluginCallback<any> | FastifyPluginAsync<any>;
8
+ options?: Record<string, unknown>;
33
9
  };
34
-
35
- /**
36
- * τjs [ taujs ] Orchestration System
37
- * (c) 2024-present Aoede Ltd
38
- * Author: John Smith
39
- *
40
- * Licensed under the MIT License - attribution appreciated.
41
- * Part of the τjs [ taujs ] system for declarative, build-time orchestration of microfrontend applications,
42
- * including CSR, SSR, streaming, and middleware composition.
43
- */
44
-
45
- declare const SSRServer: FastifyPluginAsync<SSRServerOptions>;
10
+ type StaticAssetsRegistration = false | StaticMountEntry | StaticMountEntry[];
46
11
 
47
12
  type NetResolved = {
48
13
  host: string;
@@ -50,10 +15,6 @@ type NetResolved = {
50
15
  hmrPort: number;
51
16
  };
52
17
 
53
- type StaticAssetsRegistration = {
54
- plugin: FastifyPluginCallback<any> | FastifyPluginAsync<any>;
55
- options?: Record<string, unknown>;
56
- };
57
18
  type CreateServerOptions = {
58
19
  config: TaujsConfig;
59
20
  serviceRegistry: ServiceRegistry;
@@ -62,7 +23,7 @@ type CreateServerOptions = {
62
23
  fastify?: FastifyInstance;
63
24
  debug?: DebugConfig;
64
25
  logger?: BaseLogger;
65
- registerStaticAssets?: false | StaticAssetsRegistration;
26
+ staticAssets?: false | StaticAssetsRegistration;
66
27
  port?: number;
67
28
  };
68
29
  type CreateServerResult = {
@@ -71,4 +32,32 @@ type CreateServerResult = {
71
32
  };
72
33
  declare const createServer: (opts: CreateServerOptions) => Promise<CreateServerResult>;
73
34
 
74
- export { SSRServer, SSRServerOptions, TEMPLATE, createServer };
35
+ /**
36
+ * τjs [ taujs ] Orchestration System
37
+ * (c) 2024-present Aoede Ltd
38
+ * Author: John Smith
39
+ *
40
+ * Licensed under the MIT License - attribution appreciated.
41
+ * Part of the τjs [ taujs ] system for declarative, build-time orchestration of microfrontend applications,
42
+ * including CSR, SSR, streaming, and middleware composition.
43
+ */
44
+
45
+ declare function taujsBuild({ config, projectRoot, clientBaseDir, isSSRBuild, }: {
46
+ config: {
47
+ apps: AppConfig[];
48
+ };
49
+ projectRoot: string;
50
+ clientBaseDir: string;
51
+ isSSRBuild?: boolean;
52
+ }): Promise<void>;
53
+
54
+ interface MessageMetaLogger {
55
+ debug?: (message?: string, meta?: Record<string, unknown>) => unknown;
56
+ info?: (message?: string, meta?: Record<string, unknown>) => unknown;
57
+ warn?: (message?: string, meta?: Record<string, unknown>) => unknown;
58
+ error?: (message?: string, meta?: Record<string, unknown>) => unknown;
59
+ child?: (bindings: Record<string, unknown>) => MessageMetaLogger | undefined;
60
+ }
61
+ declare function winstonAdapter(winston: MessageMetaLogger): BaseLogger;
62
+
63
+ export { createServer, taujsBuild, winstonAdapter };