webspresso 0.0.105 → 0.0.106

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 (134) hide show
  1. package/.agents/AGENTS.md +15 -2
  2. package/.agents/CLI.md +219 -7
  3. package/.agents/FLAGS.md +118 -0
  4. package/.agents/LOGGING.md +93 -0
  5. package/.agents/METRICS.md +127 -0
  6. package/.agents/MIGRATION_JOBS.md +95 -0
  7. package/.agents/PLUGINS.md +10 -3
  8. package/.agents/QUEUE.md +42 -0
  9. package/.agents/RATE_LIMIT.md +147 -0
  10. package/.agents/SCALE_OUT.md +109 -0
  11. package/.agents/SECRETS.md +117 -0
  12. package/AGENTS.md +10 -0
  13. package/bin/commands/add-aws.js +396 -0
  14. package/bin/commands/add-ci.js +523 -0
  15. package/bin/commands/add-cloudflare.js +240 -0
  16. package/bin/commands/add-cloudrun.js +381 -0
  17. package/bin/commands/add-migration-job.js +396 -0
  18. package/bin/commands/assets-sync.js +209 -0
  19. package/bin/commands/client-generate.js +52 -0
  20. package/bin/commands/console.js +53 -0
  21. package/bin/commands/db-migrate.js +47 -2
  22. package/bin/commands/db-studio.js +84 -0
  23. package/bin/commands/deploy-check.js +242 -13
  24. package/bin/commands/export.js +87 -0
  25. package/bin/commands/secrets-pull.js +208 -0
  26. package/bin/utils/db.js +7 -1
  27. package/bin/webspresso.js +22 -0
  28. package/core/app.js +421 -0
  29. package/core/auth/authenticate.js +93 -0
  30. package/core/auth/index.js +34 -4
  31. package/core/auth/manager.js +50 -5
  32. package/core/auth/middleware.js +16 -68
  33. package/core/auth/redis-session.js +1008 -0
  34. package/core/auth/sessions.js +156 -0
  35. package/core/cache/cdn.js +296 -0
  36. package/core/cache/index.js +12 -0
  37. package/core/cache/tagged-lru.js +9 -1
  38. package/core/console/commands.js +148 -0
  39. package/core/console/context.js +260 -0
  40. package/core/console/eval.js +70 -0
  41. package/core/console/index.js +22 -0
  42. package/core/console/repl.js +102 -0
  43. package/core/errors/domain.js +1 -0
  44. package/core/events/dispatcher.js +7 -0
  45. package/core/events/signal.js +7 -0
  46. package/core/export/discovery.js +191 -0
  47. package/core/export/exporter.js +277 -0
  48. package/core/export/index.js +24 -0
  49. package/core/flags/evaluator.js +252 -0
  50. package/core/flags/hasher.js +37 -0
  51. package/core/flags/index.js +21 -0
  52. package/core/flags/manager.js +263 -0
  53. package/core/flags/store.js +126 -0
  54. package/core/health/middleware.js +3 -3
  55. package/core/http/adapter.js +74 -0
  56. package/core/http/express-adapter.js +46 -0
  57. package/core/http/hono-adapter.js +531 -0
  58. package/core/http/index.js +41 -0
  59. package/core/http/pipeline.js +485 -0
  60. package/core/index.js +336 -0
  61. package/core/limits/middleware.js +9 -1
  62. package/core/logger/formatters.js +264 -0
  63. package/core/logger/index.js +260 -0
  64. package/core/logger/trace.js +137 -0
  65. package/core/metrics/collectors.js +125 -0
  66. package/core/metrics/index.js +45 -0
  67. package/core/metrics/middleware.js +230 -0
  68. package/core/metrics/registry.js +366 -0
  69. package/core/orm/d1.js +265 -0
  70. package/core/orm/index.js +105 -5
  71. package/core/orm/migrate-cloud.js +194 -0
  72. package/core/orm/pool.js +97 -0
  73. package/core/orm/query-builder.js +9 -0
  74. package/core/queue/adapters/cloudflare.js +131 -0
  75. package/core/queue/adapters/pubsub.js +400 -0
  76. package/core/queue/adapters/sqs.js +419 -0
  77. package/core/queue/adapters/wait-until.js +77 -0
  78. package/core/queue/index.js +8 -0
  79. package/core/queue/manager.js +11 -0
  80. package/core/rate-limit/index.js +22 -0
  81. package/core/rate-limit/limiter.js +219 -0
  82. package/core/rate-limit/stores/memory.js +129 -0
  83. package/core/rate-limit/stores/redis.js +151 -0
  84. package/core/rate-limit/utils.js +112 -0
  85. package/core/resilience/circuit-breaker.js +31 -0
  86. package/core/secrets/index.js +113 -0
  87. package/core/secrets/manager.js +308 -0
  88. package/core/secrets/providers/aws.js +288 -0
  89. package/core/secrets/providers/env.js +44 -0
  90. package/core/secrets/providers/gcp.js +307 -0
  91. package/core/secrets/providers/memory.js +76 -0
  92. package/core/shutdown/shutdown-manager.js +9 -0
  93. package/core/stream/index.js +19 -0
  94. package/core/stream/text-stream.js +326 -0
  95. package/core/studio/index.js +43 -0
  96. package/core/studio/inspector.js +258 -0
  97. package/core/studio/server.js +325 -0
  98. package/core/studio/ui.js +681 -0
  99. package/index.d.ts +934 -2
  100. package/index.js +125 -3
  101. package/package.json +3 -1
  102. package/plugins/admin-panel/index.js +12 -5
  103. package/plugins/api-key/index.js +3 -2
  104. package/plugins/audit-log/index.js +3 -1
  105. package/plugins/basic-auth/index.js +3 -2
  106. package/plugins/cors.js +3 -2
  107. package/plugins/csrf/index.js +4 -3
  108. package/plugins/csv/index.js +8 -5
  109. package/plugins/email/index.js +11 -9
  110. package/plugins/index.js +3 -0
  111. package/plugins/queue/index.js +18 -2
  112. package/plugins/redirect/index.js +7 -2
  113. package/plugins/seo-checker/index.js +2 -2
  114. package/plugins/site-analytics/index.js +3 -1
  115. package/plugins/swagger.js +8 -2
  116. package/plugins/upload/gcs-provider.js +148 -0
  117. package/plugins/upload/index.js +132 -74
  118. package/plugins/upload/s3-provider.js +257 -0
  119. package/plugins/webhooks/index.js +27 -23
  120. package/plugins/xlsx/index.js +7 -5
  121. package/src/api/api-loader.js +12 -95
  122. package/src/api/define-api.js +1 -0
  123. package/src/client-runtime/constants.js +10 -0
  124. package/src/client-runtime/mount.js +2 -3
  125. package/src/dev/client-gen.js +458 -0
  126. package/src/discovery/scan-routes.js +5 -2
  127. package/src/file-router.js +2 -0
  128. package/src/helpers.js +53 -0
  129. package/src/pages/page-loader.js +12 -205
  130. package/src/plugin-manager.js +9 -1
  131. package/src/server.js +231 -140
  132. package/src/services/executor.js +9 -0
  133. package/src/services/registry.js +9 -1
  134. package/src/services/validator.js +38 -20
@@ -113,34 +113,39 @@ function webhooksPlugin(options = {}) {
113
113
  // Attach dispatcher to app and request objects
114
114
  if (ctx.app) {
115
115
  ctx.app.webhooks = dispatcher;
116
- ctx.app.use((req, res, next) => {
116
+ }
117
+ const target = ctx.http || ctx.app;
118
+ if (target && typeof target.use === 'function') {
119
+ target.use((req, res, next) => {
117
120
  req.webhooks = dispatcher;
118
121
  next();
119
122
  });
123
+ }
120
124
 
121
- // Register built-in services in app.serviceRegistry if available
122
- if (ctx.app.serviceRegistry && typeof ctx.app.serviceRegistry.register === 'function') {
123
- if (!ctx.app.serviceRegistry.has('webhook.dispatch')) {
124
- ctx.app.serviceRegistry.register('webhook.dispatch', {
125
- handler: async (input) => dispatcher.dispatch(input.event, input.payload, input),
126
- });
127
- }
128
- if (!ctx.app.serviceRegistry.has('webhook.ping')) {
129
- ctx.app.serviceRegistry.register('webhook.ping', {
130
- handler: async (input) => dispatcher.pingEndpoint(input.endpointId),
131
- });
132
- }
133
- if (!ctx.app.serviceRegistry.has('webhook.retry')) {
134
- ctx.app.serviceRegistry.register('webhook.retry', {
135
- handler: async (input) => dispatcher.retryDelivery(input.deliveryId),
136
- });
137
- }
125
+ // Register built-in services in app.serviceRegistry if available
126
+ const registry = ctx.serviceRegistry || ctx.services || ctx.app?.serviceRegistry;
127
+ if (registry && typeof registry.register === 'function') {
128
+ if (!registry.has('webhook.dispatch')) {
129
+ registry.register('webhook.dispatch', {
130
+ handler: async (input) => dispatcher.dispatch(input.event, input.payload, input),
131
+ });
132
+ }
133
+ if (!registry.has('webhook.ping')) {
134
+ registry.register('webhook.ping', {
135
+ handler: async (input) => dispatcher.pingEndpoint(input.endpointId),
136
+ });
138
137
  }
138
+ if (!registry.has('webhook.retry')) {
139
+ registry.register('webhook.retry', {
140
+ handler: async (input) => dispatcher.retryDelivery(input.deliveryId),
141
+ });
142
+ }
143
+ }
139
144
 
140
- // Integrate with core/queue if queue is present
141
- const queueManager = options.queue || ctx.app.queue;
142
- if (queueManager && typeof queueManager.define === 'function') {
143
- dispatcher.setQueue(queueManager);
145
+ // Integrate with core/queue if queue is present
146
+ const queueManager = options.queue || ctx.queue || ctx.app?.queue;
147
+ if (queueManager && typeof queueManager.define === 'function') {
148
+ dispatcher.setQueue(queueManager);
144
149
  if (!queueManager.handlers?.has?.('webhooks.deliver')) {
145
150
  queueManager.define('webhooks.deliver', async (job) => {
146
151
  const { endpointId, deliveryId, event, payload, attempt } = job.data;
@@ -167,7 +172,6 @@ function webhooksPlugin(options = {}) {
167
172
  });
168
173
  }
169
174
  }
170
- }
171
175
 
172
176
  return () => {
173
177
  dispatcher.dispose();
@@ -53,12 +53,13 @@ function xlsxPlugin(options = {}) {
53
53
  }
54
54
 
55
55
  // 2. Register built-in services (xlsx.generate, xlsx.parse, xlsx.exportModel)
56
- if (ctx.app && ctx.app.serviceRegistry) {
56
+ const registry = ctx.serviceRegistry || ctx.services || ctx.app?.serviceRegistry;
57
+ if (registry && typeof registry.register === 'function') {
57
58
  const db = dbOption ?? ctx.db ?? ctx.app?.get?.('db') ?? null;
58
59
  const services = createXlsxServices({ db, ...options });
59
60
  for (const [name, def] of Object.entries(services)) {
60
- if (!ctx.app.serviceRegistry.has(name)) {
61
- ctx.app.serviceRegistry.register(name, def);
61
+ if (!registry.has(name)) {
62
+ registry.register(name, def);
62
63
  }
63
64
  }
64
65
  }
@@ -74,8 +75,9 @@ function xlsxPlugin(options = {}) {
74
75
  ctx.middlewares.xlsx = middleware;
75
76
  }
76
77
 
77
- if (ctx.app && typeof ctx.app.use === 'function') {
78
- ctx.app.use(middleware);
78
+ const target = ctx.http || ctx.app;
79
+ if (target && typeof target.use === 'function') {
80
+ target.use(middleware);
79
81
  }
80
82
  },
81
83
  };
@@ -11,6 +11,7 @@ const { ZodError } = require('zod');
11
11
  const { z: zForApi } = require('../../core/validation');
12
12
  const { resolveMiddlewares } = require('../file-router');
13
13
  const { translateValidationIssues, getTranslatorForRequest } = require('./validation-i18n');
14
+ const { executeApiPipeline, enrichRouteError } = require('../../core/http');
14
15
 
15
16
  /**
16
17
  * Compiles a schema definition from an API module
@@ -136,110 +137,26 @@ function createApiHandler(descriptor, context) {
136
137
  }
137
138
  }
138
139
 
139
- // 2. Injections
140
- if (db) req.db = db;
141
- if (serviceRegistry) {
142
- req.service = (name, input, opts) =>
143
- serviceRegistry.call(name, input, { req, res, db }, opts);
144
- }
145
-
146
- // 3. Schema validation
147
- if (compiledSchema) {
148
- try {
149
- validateRequestInput(req, compiledSchema);
150
- } catch (err) {
151
- if (err instanceof ZodError || err.name === 'ZodError' || Array.isArray(err?.issues)) {
152
- const pagesDir = context.pagesDir || appOptions.pagesDir || 'pages';
153
- const routeDir = descriptor.file ? path.dirname(descriptor.file) : '';
154
- const translatedIssues = translateValidationIssues(err.issues, {
155
- req,
156
- pagesDir,
157
- routeDir,
158
- isDev,
159
- });
160
- return res.status(400).json({
161
- error: 'Validation Error',
162
- issues: translatedIssues,
163
- });
164
- }
165
- throw err;
166
- }
167
- } else {
168
- req.input = {
169
- body: req.body,
170
- params: req.params,
171
- query: req.query,
172
- };
173
- }
174
-
175
- // 4. Resolve and execute middlewares (module-local first, cached across requests)
140
+ // 2. Resolve middlewares (module-local first, cached across requests)
176
141
  if (!cachedMiddlewares) {
177
142
  const moduleMiddlewares = descriptor.moduleConfig?.middlewares || {};
178
143
  const combinedRegistry = Object.assign({}, middlewares, moduleMiddlewares);
179
144
  cachedMiddlewares = resolveMiddlewares(apiDef.middleware, combinedRegistry);
180
145
  }
181
146
 
182
- for (const mw of cachedMiddlewares) {
183
- if (typeof mw !== 'function') continue;
184
- await new Promise((resolve, reject) => {
185
- try {
186
- mw(req, res, (err) => {
187
- if (err) reject(err);
188
- else resolve();
189
- });
190
- } catch (syncErr) {
191
- reject(syncErr);
192
- }
193
- });
194
- if (res.headersSent) return;
195
- }
196
-
197
- // 5. Execute handler
198
- const pagesDir = context.pagesDir || appOptions.pagesDir || 'pages';
199
- const routeDir = descriptor.file ? path.dirname(descriptor.file) : '';
200
- const tr = getTranslatorForRequest(req, { pagesDir, routeDir, isDev });
201
-
202
- const apiCtx = {
147
+ // 3. Execute framework-agnostic API pipeline
148
+ await executeApiPipeline({
149
+ descriptor,
150
+ context,
151
+ apiDef,
152
+ handlerFn,
153
+ compiledSchema,
154
+ middlewares: cachedMiddlewares,
203
155
  req,
204
156
  res,
205
- params: req.params || {},
206
- query: req.query || {},
207
- body: req.body || {},
208
- input: req.input,
209
- locale: tr.locale,
210
- t: tr.t,
211
- service: (name, input, opts) =>
212
- serviceRegistry ? serviceRegistry.call(name, input, { req, res, db }, opts) : null,
213
- module: descriptor.module || null,
214
- config: appOptions,
215
- logger: console,
216
- db,
217
- sse: (sourceOrOptions, maybeOptions) => res.sse(sourceOrOptions, maybeOptions),
218
- };
219
-
220
- req.ctx = apiCtx;
221
- res.service = apiCtx.service;
222
- res.input = apiCtx.input;
223
- res.module = apiCtx.module;
224
-
225
- const result = await handlerFn(req, res, apiCtx);
226
-
227
- // 6. Auto-respond if handler returned a value and response not yet sent
228
- if (!res.headersSent) {
229
- if (result !== undefined) {
230
- return res.json(result);
231
- }
232
- }
157
+ });
233
158
  } catch (err) {
234
- if (err && typeof err === 'object') {
235
- if (!err.route) err.route = descriptor.path;
236
- if (!err.method) err.method = descriptor.method ? descriptor.method.toUpperCase() : (req.method || 'GET');
237
- if (!err.source) err.source = descriptor.source || descriptor.file;
238
- if (!err.file) err.file = descriptor.file;
239
- if (!err.module) err.module = descriptor.module || null;
240
- if (!err.phase) err.phase = 'handler';
241
- if (!err.requestId) err.requestId = req.id || (req.headers && req.headers['x-request-id']) || null;
242
- }
159
+ enrichRouteError(err, descriptor, req);
243
160
  return next(err);
244
161
  }
245
162
  };
@@ -11,6 +11,7 @@
11
11
  * @param {string} [config.description] - Endpoint description for OpenAPI
12
12
  * @param {Array<string>} [config.tags] - Endpoint tags for OpenAPI
13
13
  * @param {Array<string|Function>} [config.middleware] - Named or functional middlewares
14
+ * @param {string|{ key: string, statusCode?: number, message?: string }} [config.flag] - Feature flag required to access this endpoint
14
15
  * @param {Object|Function} [config.schema] - Request schemas (params, query, body, headers)
15
16
  * @param {Object} [config.response] - Expected response schemas by status code
16
17
  * @param {Function} config.handler - Request handler: async (req, ctx) => data
@@ -0,0 +1,10 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * Client runtime base path constant
5
+ */
6
+ const CLIENT_RUNTIME_BASE = '/__webspresso/client-runtime';
7
+
8
+ module.exports = {
9
+ CLIENT_RUNTIME_BASE,
10
+ };
@@ -4,9 +4,7 @@
4
4
  */
5
5
 
6
6
  const path = require('path');
7
- const express = require('express');
8
-
9
- const CLIENT_RUNTIME_BASE = '/__webspresso/client-runtime';
7
+ const { CLIENT_RUNTIME_BASE } = require('./constants');
10
8
 
11
9
  /** Resolve a file next to the package's resolved main entry (works with package "exports"). */
12
10
  function pkgFileFromMain(pkg, ...segments) {
@@ -21,6 +19,7 @@ function pkgFileFromMain(pkg, ...segments) {
21
19
  function mountClientRuntime(app, flags) {
22
20
  if (!flags || (!flags.alpine && !flags.swup)) return;
23
21
 
22
+ const express = require('express');
24
23
  const router = express.Router();
25
24
 
26
25
  function send(res, filePath) {