webspresso 0.0.107 → 0.0.108

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 (91) hide show
  1. package/.agents/AGENTS.md +4 -1
  2. package/.agents/CACHE.md +1 -0
  3. package/.agents/CONTEXT.md +152 -0
  4. package/.agents/ORM.md +74 -0
  5. package/.agents/SHUTDOWN.md +8 -1
  6. package/.agents/skills/webspresso-framework/SKILL.md +2 -1
  7. package/.agents/skills/webspresso-orm-and-database/SKILL.md +9 -0
  8. package/.agents/skills/webspresso-routing-and-api/SKILL.md +18 -2
  9. package/.agents/skills/webspresso-services/SKILL.md +36 -3
  10. package/AGENTS.md +6 -3
  11. package/core/app.js +7 -5
  12. package/core/applySchema.js +3 -72
  13. package/core/cache/lru.js +10 -0
  14. package/core/cache/tagged-lru.js +4 -0
  15. package/core/compileSchema.js +3 -68
  16. package/core/compression/compressible.js +53 -6
  17. package/core/compression/negotiate.js +31 -12
  18. package/core/context/app-context.js +201 -0
  19. package/core/context/index.js +55 -0
  20. package/core/context/request.js +241 -0
  21. package/core/events/dispatcher.js +144 -15
  22. package/core/events/signal.js +9 -0
  23. package/core/health/probes.js +12 -2
  24. package/core/http/index.js +10 -1
  25. package/core/http/pipeline.js +519 -368
  26. package/core/index.js +24 -1
  27. package/core/orm/eager-loader.js +90 -55
  28. package/core/orm/index.js +3 -39
  29. package/core/orm/model.js +6 -1
  30. package/core/orm/schema-helpers.js +59 -0
  31. package/core/orm/transaction.js +109 -14
  32. package/core/plugins/index.js +8 -0
  33. package/core/plugins/plugin-manager.js +1098 -0
  34. package/core/queue/manager.js +55 -27
  35. package/core/rate-limit/stores/memory.js +23 -10
  36. package/core/resilience/circuit-breaker.js +34 -14
  37. package/core/schema-cache.js +2 -64
  38. package/core/services/builtins/auth.js +273 -0
  39. package/core/services/builtins/exchange.js +229 -0
  40. package/core/services/builtins/file-manager.js +178 -0
  41. package/core/services/builtins/mail.js +134 -0
  42. package/core/services/builtins/media.js +196 -0
  43. package/core/services/builtins/system.js +219 -0
  44. package/core/services/discovery.js +133 -0
  45. package/core/services/executor.js +629 -0
  46. package/core/services/index.js +110 -5
  47. package/core/services/loader.js +161 -0
  48. package/core/services/memoize.js +408 -0
  49. package/core/services/registry.js +731 -0
  50. package/core/services/validator.js +424 -0
  51. package/core/shutdown/shutdown-manager.js +146 -85
  52. package/core/ssr/stream.js +109 -16
  53. package/core/stream/text-stream.js +45 -4
  54. package/core/utils/index.js +110 -0
  55. package/core/utils/timeout.js +4 -0
  56. package/core/validation/apply.js +74 -0
  57. package/core/validation/cache.js +66 -0
  58. package/core/validation/compiler.js +77 -0
  59. package/core/validation/index.js +22 -1
  60. package/index.d.ts +77 -4
  61. package/index.js +64 -20
  62. package/package.json +1 -1
  63. package/src/app-context.js +4 -196
  64. package/src/dev/typegen.js +1 -1
  65. package/src/plugin-manager.js +5 -1086
  66. package/src/server.js +4 -1
  67. package/src/services/builtins/auth.js +2 -273
  68. package/src/services/builtins/exchange.js +2 -229
  69. package/src/services/builtins/file-manager.js +2 -178
  70. package/src/services/builtins/mail.js +2 -134
  71. package/src/services/builtins/media.js +2 -196
  72. package/src/services/builtins/system.js +2 -219
  73. package/src/services/discovery.js +2 -133
  74. package/src/services/executor.js +2 -575
  75. package/src/services/index.js +4 -109
  76. package/src/services/loader.js +1 -119
  77. package/src/services/memoize.js +2 -408
  78. package/src/services/registry.js +1 -711
  79. package/src/services/validator.js +2 -424
  80. package/core/utils/array.js +0 -12
  81. package/core/utils/base64url.js +0 -8
  82. package/core/utils/ensure-leading-slash.js +0 -6
  83. package/core/utils/escape-html.js +0 -7
  84. package/core/utils/escape-regexp.js +0 -7
  85. package/core/utils/get-client-ip.js +0 -6
  86. package/core/utils/hmac.js +0 -6
  87. package/core/utils/random.js +0 -9
  88. package/core/utils/safe-compare.js +0 -3
  89. package/core/utils/sha256.js +0 -6
  90. package/core/utils/strip-trailing-slash.js +0 -6
  91. package/core/utils/walk.js +0 -7
@@ -0,0 +1,229 @@
1
+ /**
2
+ * Built-in Data Exchange & Spreadsheet Services
3
+ * @module src/services/builtins/exchange
4
+ */
5
+
6
+ const { z } = require('zod');
7
+ const { ValidationError, NotFoundError } = require('../../errors');
8
+ const { getModel } = require('../../orm/model');
9
+ const { buildXlsxBuffer } = require('../../../plugins/data-exchange/export-xlsx');
10
+ const {
11
+ parseCsvToRows,
12
+ parseXlsxToRows,
13
+ buildPayloadForRow,
14
+ allowedImportColumns,
15
+ } = require('../../../plugins/data-exchange/import');
16
+ const { buildHeaderMapping, dataRowsToObjects } = require('../../../plugins/data-exchange/parse-table');
17
+
18
+ /**
19
+ * Convert base64 data to Buffer
20
+ * @param {string} base64Str
21
+ * @returns {Buffer}
22
+ */
23
+ function parseBase64Buffer(base64Str) {
24
+ const match = base64Str.match(/^data:([^;]+);base64,(.+)$/);
25
+ if (match) {
26
+ return Buffer.from(match[2], 'base64');
27
+ }
28
+ return Buffer.from(base64Str, 'base64');
29
+ }
30
+
31
+ /**
32
+ * Create built-in exchange services map
33
+ * @param {Object} [options]
34
+ * @param {Object} [options.db]
35
+ * @returns {Record<string, Object>}
36
+ */
37
+ function createExchangeServices(options = {}) {
38
+ const db = options.db || null;
39
+
40
+ return {
41
+ 'exchange.export': {
42
+ schema: z.object({
43
+ model: z.string().min(1, 'Model name is required'),
44
+ ids: z.array(z.union([z.string(), z.number()])).optional(),
45
+ where: z.record(z.any()).optional(),
46
+ limit: z.number().int().positive().default(10000),
47
+ }),
48
+ auth: 'admin',
49
+ async handler(input, ctx) {
50
+ const { model: modelName, ids, where, limit } = input;
51
+ const activeDb = ctx.db || db;
52
+ if (!activeDb) {
53
+ throw new Error('Database instance is required in context for exchange.export');
54
+ }
55
+
56
+ const model = activeDb.getModel ? activeDb.getModel(modelName) : getModel(modelName);
57
+ if (!model) {
58
+ throw new NotFoundError(`Model "${modelName}" not found`);
59
+ }
60
+
61
+ const repo = activeDb.getRepository(modelName);
62
+ let records = [];
63
+
64
+ if (ids && ids.length > 0) {
65
+ records = await repo.query().whereIn(model.primaryKey, ids).limit(limit).list();
66
+ } else if (where && Object.keys(where).length > 0) {
67
+ records = await repo.findAll(where, { limit });
68
+ } else {
69
+ records = await repo.query().limit(limit).list();
70
+ }
71
+
72
+ const buffer = await buildXlsxBuffer(model, records);
73
+ const safeName = String(model.name).replace(/[^\w.-]+/g, '_');
74
+
75
+ return {
76
+ buffer,
77
+ filename: `${safeName}_export.xlsx`,
78
+ rowCount: records.length,
79
+ model: modelName,
80
+ };
81
+ },
82
+ },
83
+
84
+ 'exchange.import': {
85
+ schema: z.object({
86
+ model: z.string().min(1, 'Model name is required'),
87
+ buffer: z.any().optional(),
88
+ base64: z.string().optional(),
89
+ csvText: z.string().optional(),
90
+ mode: z.enum(['insert', 'upsert']).default('insert'),
91
+ upsertKey: z.string().optional(),
92
+ }),
93
+ auth: 'admin',
94
+ transaction: true,
95
+ async handler(input, ctx) {
96
+ const { model: modelName, buffer: rawBuffer, base64, csvText, mode, upsertKey: userUpsertKey } = input;
97
+ const activeDb = ctx.db || db;
98
+ const trx = ctx.trx;
99
+ if (!activeDb) {
100
+ throw new Error('Database instance is required in context for exchange.import');
101
+ }
102
+
103
+ const model = activeDb.getModel ? activeDb.getModel(modelName) : getModel(modelName);
104
+ if (!model) {
105
+ throw new NotFoundError(`Model "${modelName}" not found`);
106
+ }
107
+
108
+ let buffer;
109
+ let isCsv = false;
110
+
111
+ if (csvText) {
112
+ buffer = Buffer.from(csvText, 'utf8');
113
+ isCsv = true;
114
+ } else if (rawBuffer) {
115
+ buffer = Buffer.isBuffer(rawBuffer) ? rawBuffer : Buffer.from(rawBuffer);
116
+ } else if (base64) {
117
+ buffer = parseBase64Buffer(base64);
118
+ }
119
+
120
+ if (!buffer || buffer.length === 0) {
121
+ throw new ValidationError('Missing spreadsheet file content (buffer, base64, or csvText)', [
122
+ { field: 'buffer', message: 'Spreadsheet content is required' },
123
+ ]);
124
+ }
125
+
126
+ let rows = [];
127
+ if (isCsv) {
128
+ rows = parseCsvToRows(buffer);
129
+ } else {
130
+ try {
131
+ rows = await parseXlsxToRows(buffer);
132
+ } catch (err) {
133
+ // Fallback to CSV parsing
134
+ try {
135
+ rows = parseCsvToRows(buffer);
136
+ } catch (csvErr) {
137
+ throw new ValidationError(`Invalid spreadsheet format: ${err.message}`, [
138
+ { field: 'buffer', message: 'Could not parse Excel or CSV file' },
139
+ ]);
140
+ }
141
+ }
142
+ }
143
+
144
+ if (rows.length < 2) {
145
+ throw new ValidationError('File must include a header row and at least one data row', [
146
+ { field: 'buffer', message: 'File is empty or missing data rows' },
147
+ ]);
148
+ }
149
+
150
+ const isColumnAllowed = (col) => {
151
+ if (model.columns && typeof model.columns.has === 'function' && model.columns.has(col)) return true;
152
+ if (model.schema && model.schema.shape && col in model.schema.shape) return true;
153
+ return false;
154
+ };
155
+
156
+ let upsertKey = String(userUpsertKey || model.primaryKey);
157
+ if (!isColumnAllowed(upsertKey)) {
158
+ throw new ValidationError(`upsertKey "${upsertKey}" is not a column on model ${modelName}`);
159
+ }
160
+
161
+ const allowedKeys = model.columns && model.columns.size
162
+ ? allowedImportColumns(model)
163
+ : Object.keys(model.schema?.shape || {}).filter(c => !model.hidden?.includes(c));
164
+ const headerMapping = buildHeaderMapping(rows[0], allowedKeys);
165
+ const objects = dataRowsToObjects(rows, headerMapping);
166
+
167
+ const repo = activeDb.getRepository(modelName);
168
+ const summary = {
169
+ success: true,
170
+ created: 0,
171
+ updated: 0,
172
+ failed: 0,
173
+ totalRows: objects.length,
174
+ errors: [],
175
+ };
176
+
177
+ for (const item of objects) {
178
+ const { rowNumber, data } = item;
179
+ let payload;
180
+ try {
181
+ payload = buildPayloadForRow(model, data, mode);
182
+ } catch (e) {
183
+ summary.failed++;
184
+ summary.errors.push({ row: rowNumber, message: e.message || String(e) });
185
+ continue;
186
+ }
187
+
188
+ try {
189
+ if (mode === 'insert') {
190
+ await repo.create(payload, { trx });
191
+ summary.created++;
192
+ continue;
193
+ }
194
+
195
+ // Upsert mode
196
+ const keyVal = payload[upsertKey];
197
+ if (keyVal === undefined || keyVal === null || keyVal === '') {
198
+ const insertPayload = { ...payload };
199
+ delete insertPayload[upsertKey];
200
+ await repo.create(insertPayload, { trx });
201
+ summary.created++;
202
+ continue;
203
+ }
204
+
205
+ const existing = await repo.findOne({ [upsertKey]: keyVal }, { trx });
206
+ if (existing) {
207
+ const id = existing[model.primaryKey];
208
+ await repo.update(id, payload, { trx });
209
+ summary.updated++;
210
+ } else {
211
+ await repo.create({ ...payload, [upsertKey]: keyVal }, { trx });
212
+ summary.created++;
213
+ }
214
+ } catch (e) {
215
+ summary.failed++;
216
+ summary.errors.push({ row: rowNumber, message: e.message || String(e) });
217
+ }
218
+ }
219
+
220
+ summary.success = summary.failed === 0;
221
+ return summary;
222
+ },
223
+ },
224
+ };
225
+ }
226
+
227
+ module.exports = {
228
+ createExchangeServices,
229
+ };
@@ -0,0 +1,178 @@
1
+ /**
2
+ * Built-in File Manager Services
3
+ * @module src/services/builtins/file-manager
4
+ */
5
+
6
+ const path = require('path');
7
+ const fs = require('fs/promises');
8
+ const { z } = require('zod');
9
+ const { ValidationError, NotFoundError } = require('../../errors');
10
+ const {
11
+ listFiles,
12
+ createDirectory,
13
+ renameItem,
14
+ deleteItem,
15
+ moveItem,
16
+ saveUploadedFile,
17
+ } = require('../../../plugins/file-manager/core');
18
+ const { parseBase64 } = require('./media');
19
+
20
+ /**
21
+ * Create built-in file manager services map
22
+ * @param {Object} [options]
23
+ * @param {string} [options.baseDir]
24
+ * @param {string} [options.publicBasePath]
25
+ * @returns {Record<string, Object>}
26
+ */
27
+ function createFileManagerServices(options = {}) {
28
+ const defaultBaseDir = path.resolve(
29
+ options.baseDir || path.join(process.cwd(), 'public', 'uploads')
30
+ );
31
+ const defaultPublicBase = options.publicBasePath || '/uploads';
32
+
33
+ return {
34
+ 'fileManager.list': {
35
+ schema: z.object({
36
+ path: z.string().optional().default(''),
37
+ search: z.string().optional(),
38
+ type: z.string().optional(),
39
+ sort: z.enum(['name', 'size', 'mtime', 'ext']).optional().default('name'),
40
+ order: z.enum(['asc', 'desc']).optional().default('asc'),
41
+ page: z.number().int().positive().optional(),
42
+ perPage: z.number().int().positive().optional(),
43
+ baseDir: z.string().optional(),
44
+ publicBasePath: z.string().optional(),
45
+ }),
46
+ async handler(input) {
47
+ const baseDir = input.baseDir ? path.resolve(input.baseDir) : defaultBaseDir;
48
+ const publicBasePath = input.publicBasePath || defaultPublicBase;
49
+
50
+ return listFiles(baseDir, input.path, {
51
+ search: input.search,
52
+ type: input.type,
53
+ sort: input.sort,
54
+ order: input.order,
55
+ page: input.page,
56
+ perPage: input.perPage,
57
+ publicBasePath,
58
+ });
59
+ },
60
+ },
61
+
62
+ 'fileManager.mkdir': {
63
+ schema: z.object({
64
+ path: z.string().optional().default(''),
65
+ name: z.string().min(1, 'Directory name is required'),
66
+ baseDir: z.string().optional(),
67
+ }),
68
+ async handler(input) {
69
+ const baseDir = input.baseDir ? path.resolve(input.baseDir) : defaultBaseDir;
70
+ return createDirectory(baseDir, input.path, input.name);
71
+ },
72
+ },
73
+
74
+ 'fileManager.rename': {
75
+ schema: z.object({
76
+ path: z.string().min(1, 'Target path is required'),
77
+ newName: z.string().min(1, 'New name is required'),
78
+ baseDir: z.string().optional(),
79
+ }),
80
+ async handler(input) {
81
+ const baseDir = input.baseDir ? path.resolve(input.baseDir) : defaultBaseDir;
82
+ return renameItem(baseDir, input.path, input.newName);
83
+ },
84
+ },
85
+
86
+ 'fileManager.delete': {
87
+ schema: z.object({
88
+ path: z.string().min(1, 'Path is required'),
89
+ baseDir: z.string().optional(),
90
+ }),
91
+ async handler(input) {
92
+ const baseDir = input.baseDir ? path.resolve(input.baseDir) : defaultBaseDir;
93
+ return deleteItem(baseDir, input.path);
94
+ },
95
+ },
96
+
97
+ 'fileManager.move': {
98
+ schema: z.object({
99
+ source: z.string().min(1, 'Source path is required'),
100
+ destination: z.string().optional().default(''),
101
+ baseDir: z.string().optional(),
102
+ }),
103
+ async handler(input) {
104
+ const baseDir = input.baseDir ? path.resolve(input.baseDir) : defaultBaseDir;
105
+ return moveItem(baseDir, input.source, input.destination);
106
+ },
107
+ },
108
+
109
+ 'fileManager.upload': {
110
+ schema: z.object({
111
+ path: z.string().optional().default(''),
112
+ buffer: z.any().optional(),
113
+ base64: z.string().optional(),
114
+ filePath: z.string().optional(),
115
+ originalName: z.string().min(1).default('upload.bin'),
116
+ mimeType: z.string().optional(),
117
+ baseDir: z.string().optional(),
118
+ publicBasePath: z.string().optional(),
119
+ }),
120
+ async handler(input) {
121
+ let {
122
+ path: targetRelPath,
123
+ buffer,
124
+ base64,
125
+ filePath,
126
+ originalName,
127
+ mimeType,
128
+ baseDir: customBaseDir,
129
+ publicBasePath: customPublicBase,
130
+ } = input;
131
+ const baseDir = customBaseDir ? path.resolve(customBaseDir) : defaultBaseDir;
132
+ const publicBasePath = customPublicBase || defaultPublicBase;
133
+
134
+ if (buffer && !(buffer instanceof Buffer) && !(buffer instanceof Uint8Array)) {
135
+ if (typeof buffer === 'string') {
136
+ buffer = Buffer.from(buffer);
137
+ } else {
138
+ throw new ValidationError('Invalid buffer provided');
139
+ }
140
+ }
141
+
142
+ if (!buffer && base64) {
143
+ const parsed = parseBase64(base64);
144
+ buffer = parsed.buffer;
145
+ if (parsed.mimeType && (!mimeType || mimeType === 'application/octet-stream')) {
146
+ mimeType = parsed.mimeType;
147
+ }
148
+ } else if (!buffer && filePath) {
149
+ try {
150
+ buffer = await fs.readFile(path.resolve(filePath));
151
+ if (!originalName || originalName === 'upload.bin') {
152
+ originalName = path.basename(filePath);
153
+ }
154
+ } catch (err) {
155
+ throw new NotFoundError(`Source file not found: ${filePath}`, { cause: err });
156
+ }
157
+ }
158
+
159
+ if (!buffer || buffer.length === 0) {
160
+ throw new ValidationError('No file content provided');
161
+ }
162
+
163
+ return saveUploadedFile(
164
+ baseDir,
165
+ targetRelPath,
166
+ buffer,
167
+ originalName,
168
+ mimeType,
169
+ publicBasePath
170
+ );
171
+ },
172
+ },
173
+ };
174
+ }
175
+
176
+ module.exports = {
177
+ createFileManagerServices,
178
+ };
@@ -0,0 +1,134 @@
1
+ /**
2
+ * Built-in Mail & Notification Services
3
+ * @module src/services/builtins/mail
4
+ */
5
+
6
+ const { z } = require('zod');
7
+ const { ValidationError, NotFoundError } = require('../../errors');
8
+
9
+ /**
10
+ * Create built-in mail services map
11
+ * @param {Object} options
12
+ * @param {Object} options.emailService - Instance created by createEmailService
13
+ * @param {Object} [options.registry] - TemplateRegistry instance
14
+ * @param {Object} [options.db] - Database or knex instance
15
+ * @param {string} [options.tableName='email_logs']
16
+ * @returns {Record<string, Object>}
17
+ */
18
+ function createMailServices(options = {}) {
19
+ const { emailService, registry, db, tableName = 'email_logs' } = options;
20
+
21
+ if (!emailService) {
22
+ throw new Error('emailService instance is required for createMailServices');
23
+ }
24
+
25
+ const emailSchema = z.string().email();
26
+ const recipientSchema = z.union([emailSchema, z.array(emailSchema)]);
27
+
28
+ return {
29
+ 'mail.send': {
30
+ schema: z.object({
31
+ to: recipientSchema,
32
+ subject: z.string().min(1, 'Subject is required'),
33
+ from: z.string().optional(),
34
+ replyTo: z.string().optional(),
35
+ cc: z.union([z.string(), z.array(z.string())]).optional(),
36
+ bcc: z.union([z.string(), z.array(z.string())]).optional(),
37
+ template: z.string().optional(),
38
+ mjml: z.string().optional(),
39
+ html: z.string().optional(),
40
+ text: z.string().optional(),
41
+ data: z.record(z.any()).optional().default({}),
42
+ attachments: z.array(z.any()).optional(),
43
+ }),
44
+ async handler(input, ctx) {
45
+ try {
46
+ const result = await emailService.send(input);
47
+ return {
48
+ success: true,
49
+ messageId: result.messageId,
50
+ to: input.to,
51
+ subject: input.subject,
52
+ };
53
+ } catch (err) {
54
+ throw new Error(`Failed to send email: ${err.message}`);
55
+ }
56
+ },
57
+ },
58
+
59
+ 'mail.send-templated': {
60
+ schema: z.object({
61
+ template: z.string().min(1, 'Template ID is required'),
62
+ to: recipientSchema,
63
+ subject: z.string().min(1, 'Subject is required'),
64
+ data: z.record(z.any()).optional().default({}),
65
+ from: z.string().optional(),
66
+ replyTo: z.string().optional(),
67
+ attachments: z.array(z.any()).optional(),
68
+ }),
69
+ async handler(input, ctx) {
70
+ const { template, ...rest } = input;
71
+ try {
72
+ const result = await emailService.send({
73
+ template,
74
+ ...rest,
75
+ });
76
+ return {
77
+ success: true,
78
+ messageId: result.messageId,
79
+ template,
80
+ to: input.to,
81
+ };
82
+ } catch (err) {
83
+ throw new Error(`Failed to send templated email: ${err.message}`);
84
+ }
85
+ },
86
+ },
87
+
88
+ 'mail.preview': {
89
+ schema: z.object({
90
+ template: z.string().optional(),
91
+ mjml: z.string().optional(),
92
+ data: z.record(z.any()).optional().default({}),
93
+ }),
94
+ async handler(input, ctx) {
95
+ const { template, mjml, data } = input;
96
+ if (!template && !mjml) {
97
+ throw new ValidationError('Either template or mjml must be provided for preview', [
98
+ { field: 'template', message: 'Either template or mjml must be provided' },
99
+ ]);
100
+ }
101
+
102
+ const previewOpts = template ? template : { mjml };
103
+ const rendered = await emailService.preview(previewOpts, data);
104
+ return {
105
+ html: rendered.html,
106
+ text: rendered.text,
107
+ errors: rendered.errors || [],
108
+ };
109
+ },
110
+ },
111
+
112
+ 'mail.query-logs': {
113
+ schema: z.object({
114
+ limit: z.number().int().positive().default(50),
115
+ offset: z.number().int().nonnegative().default(0),
116
+ status: z.enum(['sent', 'error']).optional(),
117
+ to: z.string().optional(),
118
+ template: z.string().optional(),
119
+ }),
120
+ auth: 'admin',
121
+ async handler(input, ctx) {
122
+ if (!emailService.queryLogs) {
123
+ return { logs: [], total: 0 };
124
+ }
125
+ const logs = await emailService.queryLogs(input);
126
+ return { logs, total: logs.length };
127
+ },
128
+ },
129
+ };
130
+ }
131
+
132
+ module.exports = {
133
+ createMailServices,
134
+ };