alepha 0.13.0 → 0.13.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.
Files changed (195) hide show
  1. package/dist/api-jobs/index.d.ts +26 -26
  2. package/dist/api-users/index.d.ts +1 -1
  3. package/dist/cli/{dist-Sz2EXvQX.cjs → dist-Dl9Vl7Ur.js} +17 -13
  4. package/dist/cli/{dist-BBPjuQ56.js.map → dist-Dl9Vl7Ur.js.map} +1 -1
  5. package/dist/cli/index.d.ts +3 -11
  6. package/dist/cli/index.js +106 -74
  7. package/dist/cli/index.js.map +1 -1
  8. package/dist/email/index.js +71 -73
  9. package/dist/email/index.js.map +1 -1
  10. package/dist/orm/index.d.ts +1 -1
  11. package/dist/orm/index.js.map +1 -1
  12. package/dist/queue/index.d.ts +4 -4
  13. package/dist/retry/index.d.ts +1 -1
  14. package/dist/retry/index.js +2 -2
  15. package/dist/retry/index.js.map +1 -1
  16. package/dist/scheduler/index.d.ts +6 -6
  17. package/dist/security/index.d.ts +28 -28
  18. package/dist/server/index.js +1 -1
  19. package/dist/server/index.js.map +1 -1
  20. package/dist/server-health/index.d.ts +17 -17
  21. package/dist/server-metrics/index.js +170 -174
  22. package/dist/server-metrics/index.js.map +1 -1
  23. package/dist/server-security/index.d.ts +9 -9
  24. package/dist/vite/index.js +4 -5
  25. package/dist/vite/index.js.map +1 -1
  26. package/dist/websocket/index.d.ts +7 -7
  27. package/package.json +52 -103
  28. package/src/cli/apps/AlephaPackageBuilderCli.ts +7 -2
  29. package/src/cli/assets/appRouterTs.ts +9 -0
  30. package/src/cli/assets/indexHtml.ts +2 -1
  31. package/src/cli/assets/mainBrowserTs.ts +10 -0
  32. package/src/cli/commands/CoreCommands.ts +6 -5
  33. package/src/cli/commands/DrizzleCommands.ts +65 -57
  34. package/src/cli/commands/VerifyCommands.ts +1 -1
  35. package/src/cli/services/ProjectUtils.ts +44 -38
  36. package/src/orm/providers/DrizzleKitProvider.ts +1 -1
  37. package/src/retry/descriptors/$retry.ts +5 -3
  38. package/src/server/providers/NodeHttpServerProvider.ts +1 -1
  39. package/src/vite/helpers/boot.ts +3 -3
  40. package/dist/api-files/index.cjs +0 -1293
  41. package/dist/api-files/index.cjs.map +0 -1
  42. package/dist/api-files/index.d.cts +0 -829
  43. package/dist/api-jobs/index.cjs +0 -274
  44. package/dist/api-jobs/index.cjs.map +0 -1
  45. package/dist/api-jobs/index.d.cts +0 -654
  46. package/dist/api-notifications/index.cjs +0 -380
  47. package/dist/api-notifications/index.cjs.map +0 -1
  48. package/dist/api-notifications/index.d.cts +0 -289
  49. package/dist/api-parameters/index.cjs +0 -66
  50. package/dist/api-parameters/index.cjs.map +0 -1
  51. package/dist/api-parameters/index.d.cts +0 -84
  52. package/dist/api-users/index.cjs +0 -6009
  53. package/dist/api-users/index.cjs.map +0 -1
  54. package/dist/api-users/index.d.cts +0 -4740
  55. package/dist/api-verifications/index.cjs +0 -407
  56. package/dist/api-verifications/index.cjs.map +0 -1
  57. package/dist/api-verifications/index.d.cts +0 -207
  58. package/dist/batch/index.cjs +0 -408
  59. package/dist/batch/index.cjs.map +0 -1
  60. package/dist/batch/index.d.cts +0 -330
  61. package/dist/bin/index.cjs +0 -17
  62. package/dist/bin/index.cjs.map +0 -1
  63. package/dist/bin/index.d.cts +0 -1
  64. package/dist/bucket/index.cjs +0 -303
  65. package/dist/bucket/index.cjs.map +0 -1
  66. package/dist/bucket/index.d.cts +0 -355
  67. package/dist/cache/index.cjs +0 -241
  68. package/dist/cache/index.cjs.map +0 -1
  69. package/dist/cache/index.d.cts +0 -202
  70. package/dist/cache-redis/index.cjs +0 -84
  71. package/dist/cache-redis/index.cjs.map +0 -1
  72. package/dist/cache-redis/index.d.cts +0 -40
  73. package/dist/cli/chunk-DSlc6foC.cjs +0 -43
  74. package/dist/cli/dist-BBPjuQ56.js +0 -2778
  75. package/dist/cli/dist-Sz2EXvQX.cjs.map +0 -1
  76. package/dist/cli/index.cjs +0 -1241
  77. package/dist/cli/index.cjs.map +0 -1
  78. package/dist/cli/index.d.cts +0 -422
  79. package/dist/command/index.cjs +0 -693
  80. package/dist/command/index.cjs.map +0 -1
  81. package/dist/command/index.d.cts +0 -340
  82. package/dist/core/index.cjs +0 -2264
  83. package/dist/core/index.cjs.map +0 -1
  84. package/dist/core/index.d.cts +0 -1927
  85. package/dist/datetime/index.cjs +0 -318
  86. package/dist/datetime/index.cjs.map +0 -1
  87. package/dist/datetime/index.d.cts +0 -145
  88. package/dist/email/index.cjs +0 -10874
  89. package/dist/email/index.cjs.map +0 -1
  90. package/dist/email/index.d.cts +0 -186
  91. package/dist/fake/index.cjs +0 -34641
  92. package/dist/fake/index.cjs.map +0 -1
  93. package/dist/fake/index.d.cts +0 -74
  94. package/dist/file/index.cjs +0 -1212
  95. package/dist/file/index.cjs.map +0 -1
  96. package/dist/file/index.d.cts +0 -698
  97. package/dist/lock/index.cjs +0 -226
  98. package/dist/lock/index.cjs.map +0 -1
  99. package/dist/lock/index.d.cts +0 -361
  100. package/dist/lock-redis/index.cjs +0 -113
  101. package/dist/lock-redis/index.cjs.map +0 -1
  102. package/dist/lock-redis/index.d.cts +0 -24
  103. package/dist/logger/index.cjs +0 -521
  104. package/dist/logger/index.cjs.map +0 -1
  105. package/dist/logger/index.d.cts +0 -281
  106. package/dist/orm/index.cjs +0 -2986
  107. package/dist/orm/index.cjs.map +0 -1
  108. package/dist/orm/index.d.cts +0 -2213
  109. package/dist/queue/index.cjs +0 -1044
  110. package/dist/queue/index.cjs.map +0 -1
  111. package/dist/queue/index.d.cts +0 -1265
  112. package/dist/queue-redis/index.cjs +0 -873
  113. package/dist/queue-redis/index.cjs.map +0 -1
  114. package/dist/queue-redis/index.d.cts +0 -82
  115. package/dist/redis/index.cjs +0 -153
  116. package/dist/redis/index.cjs.map +0 -1
  117. package/dist/redis/index.d.cts +0 -82
  118. package/dist/retry/index.cjs +0 -146
  119. package/dist/retry/index.cjs.map +0 -1
  120. package/dist/retry/index.d.cts +0 -172
  121. package/dist/router/index.cjs +0 -111
  122. package/dist/router/index.cjs.map +0 -1
  123. package/dist/router/index.d.cts +0 -46
  124. package/dist/scheduler/index.cjs +0 -576
  125. package/dist/scheduler/index.cjs.map +0 -1
  126. package/dist/scheduler/index.d.cts +0 -145
  127. package/dist/security/index.cjs +0 -2402
  128. package/dist/security/index.cjs.map +0 -1
  129. package/dist/security/index.d.cts +0 -598
  130. package/dist/server/index.cjs +0 -1680
  131. package/dist/server/index.cjs.map +0 -1
  132. package/dist/server/index.d.cts +0 -810
  133. package/dist/server-auth/index.cjs +0 -3146
  134. package/dist/server-auth/index.cjs.map +0 -1
  135. package/dist/server-auth/index.d.cts +0 -1164
  136. package/dist/server-cache/index.cjs +0 -252
  137. package/dist/server-cache/index.cjs.map +0 -1
  138. package/dist/server-cache/index.d.cts +0 -164
  139. package/dist/server-compress/index.cjs +0 -141
  140. package/dist/server-compress/index.cjs.map +0 -1
  141. package/dist/server-compress/index.d.cts +0 -38
  142. package/dist/server-cookies/index.cjs +0 -234
  143. package/dist/server-cookies/index.cjs.map +0 -1
  144. package/dist/server-cookies/index.d.cts +0 -144
  145. package/dist/server-cors/index.cjs +0 -201
  146. package/dist/server-cors/index.cjs.map +0 -1
  147. package/dist/server-cors/index.d.cts +0 -140
  148. package/dist/server-health/index.cjs +0 -62
  149. package/dist/server-health/index.cjs.map +0 -1
  150. package/dist/server-health/index.d.cts +0 -58
  151. package/dist/server-helmet/index.cjs +0 -131
  152. package/dist/server-helmet/index.cjs.map +0 -1
  153. package/dist/server-helmet/index.d.cts +0 -97
  154. package/dist/server-links/index.cjs +0 -992
  155. package/dist/server-links/index.cjs.map +0 -1
  156. package/dist/server-links/index.d.cts +0 -513
  157. package/dist/server-metrics/index.cjs +0 -4535
  158. package/dist/server-metrics/index.cjs.map +0 -1
  159. package/dist/server-metrics/index.d.cts +0 -35
  160. package/dist/server-multipart/index.cjs +0 -237
  161. package/dist/server-multipart/index.cjs.map +0 -1
  162. package/dist/server-multipart/index.d.cts +0 -50
  163. package/dist/server-proxy/index.cjs +0 -186
  164. package/dist/server-proxy/index.cjs.map +0 -1
  165. package/dist/server-proxy/index.d.cts +0 -234
  166. package/dist/server-rate-limit/index.cjs +0 -241
  167. package/dist/server-rate-limit/index.cjs.map +0 -1
  168. package/dist/server-rate-limit/index.d.cts +0 -183
  169. package/dist/server-security/index.cjs +0 -316
  170. package/dist/server-security/index.cjs.map +0 -1
  171. package/dist/server-security/index.d.cts +0 -173
  172. package/dist/server-static/index.cjs +0 -170
  173. package/dist/server-static/index.cjs.map +0 -1
  174. package/dist/server-static/index.d.cts +0 -121
  175. package/dist/server-swagger/index.cjs +0 -1021
  176. package/dist/server-swagger/index.cjs.map +0 -1
  177. package/dist/server-swagger/index.d.cts +0 -382
  178. package/dist/sms/index.cjs +0 -221
  179. package/dist/sms/index.cjs.map +0 -1
  180. package/dist/sms/index.d.cts +0 -130
  181. package/dist/thread/index.cjs +0 -350
  182. package/dist/thread/index.cjs.map +0 -1
  183. package/dist/thread/index.d.cts +0 -260
  184. package/dist/topic/index.cjs +0 -282
  185. package/dist/topic/index.cjs.map +0 -1
  186. package/dist/topic/index.d.cts +0 -523
  187. package/dist/topic-redis/index.cjs +0 -71
  188. package/dist/topic-redis/index.cjs.map +0 -1
  189. package/dist/topic-redis/index.d.cts +0 -42
  190. package/dist/vite/index.cjs +0 -1077
  191. package/dist/vite/index.cjs.map +0 -1
  192. package/dist/vite/index.d.cts +0 -542
  193. package/dist/websocket/index.cjs +0 -1117
  194. package/dist/websocket/index.cjs.map +0 -1
  195. package/dist/websocket/index.d.cts +0 -861
@@ -1,173 +0,0 @@
1
- import * as alepha1 from "alepha";
2
- import { Alepha, Descriptor, KIND } from "alepha";
3
- import { JwtProvider, Permission, SecurityProvider, UserAccount, UserAccountToken } from "alepha/security";
4
- import { FetchOptions, ServerRequest, ServerRouterProvider } from "alepha/server";
5
- import * as alepha_logger0 from "alepha/logger";
6
-
7
- //#region src/server-security/providers/ServerBasicAuthProvider.d.ts
8
- interface BasicAuthOptions {
9
- username: string;
10
- password: string;
11
- }
12
- interface BasicAuthDescriptorConfig extends BasicAuthOptions {
13
- /** Name identifier for this basic auth (default: property key) */
14
- name?: string;
15
- /** Path patterns to match (supports wildcards like /devtools/*) */
16
- paths?: string[];
17
- }
18
- declare class ServerBasicAuthProvider {
19
- protected readonly alepha: Alepha;
20
- protected readonly log: alepha_logger0.Logger;
21
- protected readonly routerProvider: ServerRouterProvider;
22
- protected readonly realm = "Secure Area";
23
- /**
24
- * Registered basic auth descriptors with their configurations
25
- */
26
- readonly registeredAuths: BasicAuthDescriptorConfig[];
27
- /**
28
- * Register a basic auth configuration (called by descriptors)
29
- */
30
- registerAuth(config: BasicAuthDescriptorConfig): void;
31
- readonly onStart: alepha1.HookDescriptor<"start">;
32
- /**
33
- * Hook into server:onRequest to check basic auth
34
- */
35
- readonly onRequest: alepha1.HookDescriptor<"server:onRequest">;
36
- /**
37
- * Hook into action:onRequest to check basic auth for actions
38
- */
39
- readonly onActionRequest: alepha1.HookDescriptor<"action:onRequest">;
40
- /**
41
- * Check basic authentication
42
- */
43
- checkAuth(request: ServerRequest, options: BasicAuthOptions): void;
44
- /**
45
- * Performs a timing-safe comparison of credentials to prevent timing attacks.
46
- * Always compares both username and password to avoid leaking which one is wrong.
47
- */
48
- protected timingSafeCredentialCheck(inputUsername: string, inputPassword: string, expectedUsername: string, expectedPassword: string): boolean;
49
- /**
50
- * Compares two buffers in constant time, handling different lengths safely.
51
- * Returns 1 if equal, 0 if not equal.
52
- */
53
- protected safeCompare(input: Buffer, expected: Buffer): number;
54
- /**
55
- * Send WWW-Authenticate header
56
- */
57
- protected sendAuthRequired(request: ServerRequest): void;
58
- }
59
- declare const isBasicAuth: (value: unknown) => value is {
60
- basic: BasicAuthOptions;
61
- };
62
- //#endregion
63
- //#region src/server-security/providers/ServerSecurityProvider.d.ts
64
- declare class ServerSecurityProvider {
65
- protected readonly log: alepha_logger0.Logger;
66
- protected readonly securityProvider: SecurityProvider;
67
- protected readonly jwtProvider: JwtProvider;
68
- protected readonly alepha: Alepha;
69
- protected readonly onConfigure: alepha1.HookDescriptor<"configure">;
70
- protected readonly onActionRequest: alepha1.HookDescriptor<"action:onRequest">;
71
- protected readonly onRequest: alepha1.HookDescriptor<"server:onRequest">;
72
- protected check(user: UserAccountToken, secure: ServerRouteSecure): void;
73
- /**
74
- * Get the user account token for a local action call.
75
- * There are three possible sources for the user:
76
- * - `options.user`: the user passed in the options
77
- * - `"system"`: the system user from the state (you MUST set state `server.security.system.user`)
78
- * - `"context"`: the user from the request context (you MUST be in an HTTP request context)
79
- *
80
- * Priority order: `options.user` > `"system"` > `"context"`.
81
- *
82
- * In testing environment, if no user is provided, a test user is created based on the SecurityProvider's roles.
83
- */
84
- protected createUserFromLocalFunctionContext(options: {
85
- user?: UserAccountToken | "system" | "context";
86
- }, permission?: Permission): UserAccountToken;
87
- protected createTestUser(): UserAccountToken;
88
- protected readonly onClientRequest: alepha1.HookDescriptor<"client:onRequest">;
89
- }
90
- type ServerRouteSecure = {
91
- realm?: string;
92
- basic?: BasicAuthOptions;
93
- };
94
- //#endregion
95
- //#region src/server-security/descriptors/$basicAuth.d.ts
96
- /**
97
- * Declares HTTP Basic Authentication for server routes.
98
- * This descriptor provides methods to protect routes with username/password authentication.
99
- */
100
- declare const $basicAuth: {
101
- (options: BasicAuthDescriptorConfig): AbstractBasicAuthDescriptor;
102
- [KIND]: typeof BasicAuthDescriptor;
103
- };
104
- interface AbstractBasicAuthDescriptor {
105
- readonly name: string;
106
- readonly options: BasicAuthDescriptorConfig;
107
- check(request: ServerRequest, options?: BasicAuthOptions): void;
108
- }
109
- declare class BasicAuthDescriptor extends Descriptor<BasicAuthDescriptorConfig> implements AbstractBasicAuthDescriptor {
110
- protected readonly serverBasicAuthProvider: ServerBasicAuthProvider;
111
- get name(): string;
112
- protected onInit(): void;
113
- /**
114
- * Checks basic auth for the given request using this descriptor's configuration.
115
- */
116
- check(request: ServerRequest, options?: BasicAuthOptions): void;
117
- }
118
- //#endregion
119
- //#region src/server-security/index.d.ts
120
- declare module "alepha" {
121
- interface State {
122
- /**
123
- * Real (or fake) user account, used for internal actions.
124
- *
125
- * If you define this, you assume that all actions are executed by this user by default.
126
- * > To force a different user, you need to pass it explicitly in the options.
127
- */
128
- "alepha.server.security.system.user"?: UserAccountToken;
129
- /**
130
- * The authenticated user account attached to the server request state.
131
- *
132
- * @internal
133
- */
134
- "alepha.server.request.user"?: UserAccount;
135
- }
136
- }
137
- declare module "alepha/server" {
138
- interface ServerRequest<TConfig> {
139
- user?: UserAccountToken;
140
- }
141
- interface ServerActionRequest<TConfig> {
142
- user: UserAccountToken;
143
- }
144
- interface ServerRoute {
145
- /**
146
- * If true, the route will be protected by the security provider.
147
- * All actions are secure by default, but you can disable it for specific actions.
148
- */
149
- secure?: boolean | ServerRouteSecure;
150
- }
151
- interface ClientRequestOptions extends FetchOptions {
152
- /**
153
- * Forward user from the previous request.
154
- * If "system", use system user. @see {ServerSecurityProvider.localSystemUser}
155
- * If "context", use the user from the current context (e.g. request).
156
- *
157
- * @default "system" if provided, else "context" if available.
158
- */
159
- user?: UserAccountToken | "system" | "context";
160
- }
161
- }
162
- /**
163
- * Plugin for Alepha Server that provides security features. Based on the Alepha Security module.
164
- *
165
- * By default, all $action will be guarded by a permission check.
166
- *
167
- * @see {@link ServerSecurityProvider}
168
- * @module alepha.server.security
169
- */
170
- declare const AlephaServerSecurity: alepha1.Service<alepha1.Module>;
171
- //#endregion
172
- export { $basicAuth, AbstractBasicAuthDescriptor, AlephaServerSecurity, BasicAuthDescriptor, BasicAuthDescriptorConfig, BasicAuthOptions, ServerBasicAuthProvider, ServerRouteSecure, ServerSecurityProvider, isBasicAuth };
173
- //# sourceMappingURL=index.d.cts.map
@@ -1,170 +0,0 @@
1
- let alepha = require("alepha");
2
- let alepha_server = require("alepha/server");
3
- let node_fs = require("node:fs");
4
- let node_fs_promises = require("node:fs/promises");
5
- let node_path = require("node:path");
6
- let alepha_datetime = require("alepha/datetime");
7
- let alepha_file = require("alepha/file");
8
- let alepha_logger = require("alepha/logger");
9
-
10
- //#region src/server-static/descriptors/$serve.ts
11
- /**
12
- * Create a new static file handler.
13
- */
14
- const $serve = (options = {}) => {
15
- return (0, alepha.createDescriptor)(ServeDescriptor, options);
16
- };
17
- var ServeDescriptor = class extends alepha.Descriptor {};
18
- $serve[alepha.KIND] = ServeDescriptor;
19
-
20
- //#endregion
21
- //#region src/server-static/providers/ServerStaticProvider.ts
22
- var ServerStaticProvider = class {
23
- alepha = (0, alepha.$inject)(alepha.Alepha);
24
- routerProvider = (0, alepha.$inject)(alepha_server.ServerRouterProvider);
25
- dateTimeProvider = (0, alepha.$inject)(alepha_datetime.DateTimeProvider);
26
- fileDetector = (0, alepha.$inject)(alepha_file.FileDetector);
27
- log = (0, alepha_logger.$logger)();
28
- directories = [];
29
- configure = (0, alepha.$hook)({
30
- on: "configure",
31
- handler: async () => {
32
- await Promise.all(this.alepha.descriptors($serve).map((it) => this.createStaticServer(it.options)));
33
- }
34
- });
35
- async createStaticServer(options) {
36
- const prefix = options.path ?? "/";
37
- let root = options.root ?? process.cwd();
38
- if (!(0, node_path.isAbsolute)(root)) root = (0, node_path.join)(process.cwd(), root);
39
- this.log.debug("Serve static files", {
40
- prefix,
41
- root
42
- });
43
- await (0, node_fs_promises.stat)(root);
44
- const files = await this.getAllFiles(root, options.ignoreDotEnvFiles);
45
- const routes = await Promise.all(files.map(async (file) => {
46
- const path = file.replace(root, "").replace(/\\/g, "/");
47
- this.log.trace(`Mount ${(0, node_path.join)(prefix, path)} -> ${(0, node_path.join)(root, path)}`);
48
- return {
49
- path: (0, node_path.join)(prefix, encodeURI(path)),
50
- handler: await this.createFileHandler((0, node_path.join)(root, path), options)
51
- };
52
- }));
53
- for (const route of routes) {
54
- this.routerProvider.createRoute(route);
55
- if (options.indexFallback !== false && route.path.endsWith("index.html")) this.routerProvider.createRoute({
56
- path: route.path.replace(/index\.html$/, ""),
57
- handler: route.handler
58
- });
59
- }
60
- this.directories.push({
61
- options,
62
- files: files.map((file) => file.replace(root, "").replace(/\\/g, "/"))
63
- });
64
- if (options.historyApiFallback) this.routerProvider.createRoute({
65
- path: (0, node_path.join)(prefix, "*").replace(/\\/g, "/"),
66
- handler: async (request) => {
67
- const { reply } = request;
68
- if (request.url.pathname.includes(".")) {
69
- reply.headers["content-type"] = "text/plain";
70
- reply.body = "Not Found";
71
- reply.status = 404;
72
- return;
73
- }
74
- reply.headers["content-type"] = "text/html";
75
- reply.status = 200;
76
- return (0, node_fs.createReadStream)((0, node_path.join)(root, "index.html"));
77
- }
78
- });
79
- }
80
- async createFileHandler(filepath, options) {
81
- const filename = (0, node_path.basename)(filepath);
82
- const hasGzip = await (0, node_fs_promises.access)(`${filepath}.gz`).then(() => true).catch(() => false);
83
- const hasBr = await (0, node_fs_promises.access)(`${filepath}.br`).then(() => true).catch(() => false);
84
- const fileStat = await (0, node_fs_promises.stat)(filepath);
85
- const lastModified = fileStat.mtime.toUTCString();
86
- const etag = `"${fileStat.size}-${fileStat.mtime.getTime()}"`;
87
- const contentType = this.fileDetector.getContentType(filename);
88
- const cacheControl = this.getCacheControl(filename, options);
89
- return async (request) => {
90
- const { headers, reply } = request;
91
- let path = filepath;
92
- const encoding = headers["accept-encoding"];
93
- if (encoding) {
94
- if (hasBr && encoding.includes("br")) {
95
- reply.headers["content-encoding"] = "br";
96
- path += ".br";
97
- } else if (hasGzip && encoding.includes("gzip")) {
98
- reply.headers["content-encoding"] = "gzip";
99
- path += ".gz";
100
- }
101
- }
102
- reply.headers["content-type"] = contentType;
103
- reply.headers["accept-ranges"] = "bytes";
104
- reply.headers["last-modified"] = lastModified;
105
- if (cacheControl) {
106
- reply.headers["cache-control"] = `public, max-age=${cacheControl.maxAge}`;
107
- if (cacheControl.immutable) reply.headers["cache-control"] += ", immutable";
108
- }
109
- reply.headers.etag = etag;
110
- if (headers["if-none-match"] === etag || headers["if-modified-since"] === lastModified) {
111
- reply.status = 304;
112
- return;
113
- }
114
- return (0, node_fs.createReadStream)(path);
115
- };
116
- }
117
- getCacheFileTypes() {
118
- return [
119
- ".js",
120
- ".css",
121
- ".woff",
122
- ".woff2",
123
- ".ttf",
124
- ".eot",
125
- ".otf",
126
- ".jpg",
127
- ".jpeg",
128
- ".png",
129
- ".svg",
130
- ".gif"
131
- ];
132
- }
133
- getCacheControl(filename, options) {
134
- if (!options.cacheControl) return;
135
- const fileTypes = options.cacheControl.fileTypes ?? this.getCacheFileTypes();
136
- for (const type of fileTypes) if (filename.endsWith(type)) return {
137
- immutable: options.cacheControl.immutable ?? true,
138
- maxAge: this.dateTimeProvider.duration(options.cacheControl.maxAge ?? [30, "days"]).as("seconds")
139
- };
140
- }
141
- async getAllFiles(dir, ignoreDotEnvFiles = true) {
142
- const entries = await (0, node_fs_promises.readdir)(dir, { withFileTypes: true });
143
- return (await Promise.all(entries.map((dirent) => {
144
- if (ignoreDotEnvFiles && dirent.name.startsWith(".")) return [];
145
- const fullPath = (0, node_path.join)(dir, dirent.name);
146
- return dirent.isDirectory() ? this.getAllFiles(fullPath) : fullPath;
147
- }))).flat();
148
- }
149
- };
150
-
151
- //#endregion
152
- //#region src/server-static/index.ts
153
- /**
154
- * Create static file server with `$static()`.
155
- *
156
- * @see {@link ServerStaticProvider}
157
- * @module alepha.server.static
158
- */
159
- const AlephaServerStatic = (0, alepha.$module)({
160
- name: "alepha.server.static",
161
- descriptors: [$serve],
162
- services: [alepha_server.AlephaServer, ServerStaticProvider]
163
- });
164
-
165
- //#endregion
166
- exports.$serve = $serve;
167
- exports.AlephaServerStatic = AlephaServerStatic;
168
- exports.ServeDescriptor = ServeDescriptor;
169
- exports.ServerStaticProvider = ServerStaticProvider;
170
- //# sourceMappingURL=index.cjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.cjs","names":["Descriptor","KIND","Alepha","ServerRouterProvider","DateTimeProvider","FileDetector","AlephaServer"],"sources":["../../src/server-static/descriptors/$serve.ts","../../src/server-static/providers/ServerStaticProvider.ts","../../src/server-static/index.ts"],"sourcesContent":["import { createDescriptor, Descriptor, KIND } from \"alepha\";\nimport type { DurationLike } from \"alepha/datetime\";\n\n/**\n * Create a new static file handler.\n */\nexport const $serve = (\n options: ServeDescriptorOptions = {},\n): ServeDescriptor => {\n return createDescriptor(ServeDescriptor, options);\n};\n\nexport interface ServeDescriptorOptions {\n /**\n * Prefix for the served path.\n *\n * @default \"/\"\n */\n path?: string;\n\n /**\n * Path to the directory to serve.\n *\n * @default process.cwd()\n */\n root?: string;\n\n /**\n * If true, descriptor will be ignored.\n *\n * @default false\n */\n disabled?: boolean;\n\n /**\n * Whether to keep dot files (e.g. `.gitignore`, `.env`) in the served directory.\n *\n * @default true\n */\n ignoreDotEnvFiles?: boolean;\n\n /**\n * Whether to use the index.html file when the path is a directory.\n *\n * @default true\n */\n indexFallback?: boolean;\n\n /**\n * Force all requests \"not found\" to be served with the index.html file.\n * This is useful for single-page applications (SPAs) that use client-side only routing.\n */\n historyApiFallback?: boolean;\n\n /**\n * Optional name of the descriptor.\n * This is used for logging and debugging purposes.\n *\n * @default Key name.\n */\n name?: string;\n\n /**\n * Whether to use cache control headers.\n *\n * @default {}\n */\n cacheControl?: Partial<CacheControlOptions> | false;\n}\n\nexport interface CacheControlOptions {\n /**\n * Whether to use cache control headers.\n *\n * @default [.js, .css]\n */\n fileTypes: string[];\n\n /**\n * The maximum age of the cache in seconds.\n *\n * @default 60 * 60 * 24 * 2 // 2 days\n */\n maxAge: DurationLike;\n\n /**\n * Whether to use immutable cache control headers.\n *\n * @default true\n */\n immutable: boolean;\n}\n\nexport class ServeDescriptor extends Descriptor<ServeDescriptorOptions> {}\n\n$serve[KIND] = ServeDescriptor;\n","import { createReadStream } from \"node:fs\";\nimport { access, readdir, stat } from \"node:fs/promises\";\nimport { basename, isAbsolute, join } from \"node:path\";\nimport type { Readable as NodeStream } from \"node:stream\";\nimport { $hook, $inject, Alepha } from \"alepha\";\nimport { DateTimeProvider } from \"alepha/datetime\";\nimport { FileDetector } from \"alepha/file\";\nimport { $logger } from \"alepha/logger\";\nimport { type ServerHandler, ServerRouterProvider } from \"alepha/server\";\nimport { $serve, type ServeDescriptorOptions } from \"../descriptors/$serve.ts\";\n\nexport class ServerStaticProvider {\n protected readonly alepha = $inject(Alepha);\n protected readonly routerProvider = $inject(ServerRouterProvider);\n protected readonly dateTimeProvider = $inject(DateTimeProvider);\n protected readonly fileDetector = $inject(FileDetector);\n protected readonly log = $logger();\n protected readonly directories: ServeDirectory[] = [];\n\n protected readonly configure = $hook({\n on: \"configure\",\n handler: async () => {\n await Promise.all(\n this.alepha\n .descriptors($serve)\n .map((it) => this.createStaticServer(it.options)),\n );\n },\n });\n\n public async createStaticServer(\n options: ServeDescriptorOptions,\n ): Promise<void> {\n const prefix = options.path ?? \"/\";\n\n let root = options.root ?? process.cwd();\n if (!isAbsolute(root)) {\n root = join(process.cwd(), root);\n }\n\n this.log.debug(\"Serve static files\", { prefix, root });\n\n await stat(root);\n\n // 1. get all files in the root directory (recursively)\n const files = await this.getAllFiles(root, options.ignoreDotEnvFiles);\n\n // 2. create a $route for each file (yes, this could be a lot of routes)\n const routes = await Promise.all(\n files.map(async (file) => {\n const path = file.replace(root, \"\").replace(/\\\\/g, \"/\");\n this.log.trace(`Mount ${join(prefix, path)} -> ${join(root, path)}`);\n return {\n path: join(prefix, encodeURI(path)),\n handler: await this.createFileHandler(join(root, path), options),\n };\n }),\n );\n\n for (const route of routes) {\n this.routerProvider.createRoute(route);\n\n // if route is for index.html, also create a route without it\n // e.g. /my/path/index.html -> /my/path/\n if (\n options.indexFallback !== false &&\n route.path.endsWith(\"index.html\")\n ) {\n this.routerProvider.createRoute({\n path: route.path.replace(/index\\.html$/, \"\"),\n handler: route.handler,\n });\n }\n }\n\n // 3. store the directory info for reference\n this.directories.push({\n options,\n files: files.map((file) => file.replace(root, \"\").replace(/\\\\/g, \"/\")),\n });\n\n // bonus! for SPAs, handle history API fallback\n if (options.historyApiFallback) {\n // meaning all unmatched routes should serve index.html\n this.routerProvider.createRoute({\n path: join(prefix, \"*\").replace(/\\\\/g, \"/\"),\n handler: async (request) => {\n const { reply } = request;\n\n if (request.url.pathname.includes(\".\")) {\n // If the request is for a file (e.g., /style.css), do not fall back\n reply.headers[\"content-type\"] = \"text/plain\";\n reply.body = \"Not Found\";\n reply.status = 404;\n return;\n }\n\n reply.headers[\"content-type\"] = \"text/html\";\n reply.status = 200;\n\n // Serve index.html for all unmatched routes\n return createReadStream(join(root, \"index.html\"));\n },\n });\n }\n }\n\n public async createFileHandler(\n filepath: string,\n options: ServeDescriptorOptions,\n ): Promise<ServerHandler> {\n const filename = basename(filepath);\n\n const hasGzip = await access(`${filepath}.gz`)\n .then(() => true)\n .catch(() => false);\n\n const hasBr = await access(`${filepath}.br`)\n .then(() => true)\n .catch(() => false);\n\n const fileStat = await stat(filepath);\n const lastModified = fileStat.mtime.toUTCString();\n const etag = `\"${fileStat.size}-${fileStat.mtime.getTime()}\"`;\n const contentType = this.fileDetector.getContentType(filename);\n const cacheControl = this.getCacheControl(filename, options);\n\n return async (request): Promise<NodeStream | undefined> => {\n const { headers, reply } = request;\n let path = filepath;\n\n const encoding = headers[\"accept-encoding\"];\n if (encoding) {\n if (hasBr && encoding.includes(\"br\")) {\n reply.headers[\"content-encoding\"] = \"br\";\n path += \".br\";\n } else if (hasGzip && encoding.includes(\"gzip\")) {\n reply.headers[\"content-encoding\"] = \"gzip\";\n path += \".gz\";\n }\n }\n\n reply.headers[\"content-type\"] = contentType;\n reply.headers[\"accept-ranges\"] = \"bytes\";\n reply.headers[\"last-modified\"] = lastModified;\n\n if (cacheControl) {\n reply.headers[\"cache-control\"] =\n `public, max-age=${cacheControl.maxAge}`;\n if (cacheControl.immutable) {\n reply.headers[\"cache-control\"] += \", immutable\";\n }\n }\n\n reply.headers.etag = etag;\n if (\n headers[\"if-none-match\"] === etag ||\n headers[\"if-modified-since\"] === lastModified\n ) {\n reply.status = 304;\n return;\n }\n\n return createReadStream(path);\n };\n }\n\n protected getCacheFileTypes(): string[] {\n return [\n \".js\",\n \".css\",\n \".woff\",\n \".woff2\",\n \".ttf\",\n \".eot\",\n \".otf\",\n \".jpg\",\n \".jpeg\",\n \".png\",\n \".svg\",\n \".gif\",\n ];\n }\n\n protected getCacheControl(\n filename: string,\n options: ServeDescriptorOptions,\n ): { maxAge: number; immutable: boolean } | undefined {\n if (!options.cacheControl) {\n return;\n }\n\n const fileTypes =\n options.cacheControl.fileTypes ?? this.getCacheFileTypes();\n\n for (const type of fileTypes) {\n if (filename.endsWith(type)) {\n return {\n immutable: options.cacheControl.immutable ?? true,\n maxAge: this.dateTimeProvider\n .duration(options.cacheControl.maxAge ?? [30, \"days\"])\n .as(\"seconds\"),\n };\n }\n }\n }\n\n public async getAllFiles(\n dir: string,\n ignoreDotEnvFiles = true,\n ): Promise<string[]> {\n const entries = await readdir(dir, { withFileTypes: true });\n\n const files = await Promise.all(\n entries.map((dirent) => {\n // skip .env & other dot files\n if (ignoreDotEnvFiles && dirent.name.startsWith(\".\")) {\n return [];\n }\n\n const fullPath = join(dir, dirent.name);\n return dirent.isDirectory() ? this.getAllFiles(fullPath) : fullPath;\n }),\n );\n\n return files.flat();\n }\n}\n\nexport interface ServeDirectory {\n options: ServeDescriptorOptions;\n files: string[];\n}\n","import { $module } from \"alepha\";\nimport { AlephaServer } from \"alepha/server\";\nimport { $serve } from \"./descriptors/$serve.ts\";\nimport { ServerStaticProvider } from \"./providers/ServerStaticProvider.ts\";\n\n// ---------------------------------------------------------------------------------------------------------------------\n\nexport * from \"./descriptors/$serve.ts\";\nexport * from \"./providers/ServerStaticProvider.ts\";\n\n// ---------------------------------------------------------------------------------------------------------------------\n\n/**\n * Create static file server with `$static()`.\n *\n * @see {@link ServerStaticProvider}\n * @module alepha.server.static\n */\nexport const AlephaServerStatic = $module({\n name: \"alepha.server.static\",\n descriptors: [$serve],\n services: [AlephaServer, ServerStaticProvider],\n});\n"],"mappings":";;;;;;;;;;;;;AAMA,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BAAc,qBAAqB;CAC/C,CAAC"}
@@ -1,121 +0,0 @@
1
- import * as alepha1 from "alepha";
2
- import { Alepha, Descriptor, KIND } from "alepha";
3
- import { DateTimeProvider, DurationLike } from "alepha/datetime";
4
- import * as alepha_logger0 from "alepha/logger";
5
- import { FileDetector } from "alepha/file";
6
- import { ServerHandler, ServerRouterProvider } from "alepha/server";
7
-
8
- //#region src/server-static/descriptors/$serve.d.ts
9
- /**
10
- * Create a new static file handler.
11
- */
12
- declare const $serve: {
13
- (options?: ServeDescriptorOptions): ServeDescriptor;
14
- [KIND]: typeof ServeDescriptor;
15
- };
16
- interface ServeDescriptorOptions {
17
- /**
18
- * Prefix for the served path.
19
- *
20
- * @default "/"
21
- */
22
- path?: string;
23
- /**
24
- * Path to the directory to serve.
25
- *
26
- * @default process.cwd()
27
- */
28
- root?: string;
29
- /**
30
- * If true, descriptor will be ignored.
31
- *
32
- * @default false
33
- */
34
- disabled?: boolean;
35
- /**
36
- * Whether to keep dot files (e.g. `.gitignore`, `.env`) in the served directory.
37
- *
38
- * @default true
39
- */
40
- ignoreDotEnvFiles?: boolean;
41
- /**
42
- * Whether to use the index.html file when the path is a directory.
43
- *
44
- * @default true
45
- */
46
- indexFallback?: boolean;
47
- /**
48
- * Force all requests "not found" to be served with the index.html file.
49
- * This is useful for single-page applications (SPAs) that use client-side only routing.
50
- */
51
- historyApiFallback?: boolean;
52
- /**
53
- * Optional name of the descriptor.
54
- * This is used for logging and debugging purposes.
55
- *
56
- * @default Key name.
57
- */
58
- name?: string;
59
- /**
60
- * Whether to use cache control headers.
61
- *
62
- * @default {}
63
- */
64
- cacheControl?: Partial<CacheControlOptions> | false;
65
- }
66
- interface CacheControlOptions {
67
- /**
68
- * Whether to use cache control headers.
69
- *
70
- * @default [.js, .css]
71
- */
72
- fileTypes: string[];
73
- /**
74
- * The maximum age of the cache in seconds.
75
- *
76
- * @default 60 * 60 * 24 * 2 // 2 days
77
- */
78
- maxAge: DurationLike;
79
- /**
80
- * Whether to use immutable cache control headers.
81
- *
82
- * @default true
83
- */
84
- immutable: boolean;
85
- }
86
- declare class ServeDescriptor extends Descriptor<ServeDescriptorOptions> {}
87
- //#endregion
88
- //#region src/server-static/providers/ServerStaticProvider.d.ts
89
- declare class ServerStaticProvider {
90
- protected readonly alepha: Alepha;
91
- protected readonly routerProvider: ServerRouterProvider;
92
- protected readonly dateTimeProvider: DateTimeProvider;
93
- protected readonly fileDetector: FileDetector;
94
- protected readonly log: alepha_logger0.Logger;
95
- protected readonly directories: ServeDirectory[];
96
- protected readonly configure: alepha1.HookDescriptor<"configure">;
97
- createStaticServer(options: ServeDescriptorOptions): Promise<void>;
98
- createFileHandler(filepath: string, options: ServeDescriptorOptions): Promise<ServerHandler>;
99
- protected getCacheFileTypes(): string[];
100
- protected getCacheControl(filename: string, options: ServeDescriptorOptions): {
101
- maxAge: number;
102
- immutable: boolean;
103
- } | undefined;
104
- getAllFiles(dir: string, ignoreDotEnvFiles?: boolean): Promise<string[]>;
105
- }
106
- interface ServeDirectory {
107
- options: ServeDescriptorOptions;
108
- files: string[];
109
- }
110
- //#endregion
111
- //#region src/server-static/index.d.ts
112
- /**
113
- * Create static file server with `$static()`.
114
- *
115
- * @see {@link ServerStaticProvider}
116
- * @module alepha.server.static
117
- */
118
- declare const AlephaServerStatic: alepha1.Service<alepha1.Module>;
119
- //#endregion
120
- export { $serve, AlephaServerStatic, CacheControlOptions, ServeDescriptor, ServeDescriptorOptions, ServeDirectory, ServerStaticProvider };
121
- //# sourceMappingURL=index.d.cts.map