te.js 1.3.1 → 2.0.0
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/.cursor/plans/ai_native_framework_features_5bb1a20a.plan.md +234 -0
- package/.cursor/plans/auto_error_fix_agent_e68979c5.plan.md +356 -0
- package/.cursor/plans/tejas_framework_test_suite_5e3c6fad.plan.md +168 -0
- package/.prettierignore +31 -0
- package/README.md +156 -14
- package/auto-docs/analysis/handler-analyzer.js +58 -0
- package/auto-docs/analysis/source-resolver.js +101 -0
- package/auto-docs/constants.js +37 -0
- package/auto-docs/index.js +146 -0
- package/auto-docs/llm/index.js +6 -0
- package/auto-docs/llm/parse.js +88 -0
- package/auto-docs/llm/prompts.js +222 -0
- package/auto-docs/llm/provider.js +187 -0
- package/auto-docs/openapi/endpoint-processor.js +277 -0
- package/auto-docs/openapi/generator.js +107 -0
- package/auto-docs/openapi/level3.js +131 -0
- package/auto-docs/openapi/spec-builders.js +244 -0
- package/auto-docs/ui/docs-ui.js +186 -0
- package/auto-docs/utils/logger.js +17 -0
- package/auto-docs/utils/strip-usage.js +10 -0
- package/cli/docs-command.js +315 -0
- package/cli/fly-command.js +71 -0
- package/cli/index.js +57 -0
- package/database/index.js +163 -5
- package/database/mongodb.js +146 -0
- package/database/redis.js +201 -0
- package/docs/README.md +36 -0
- package/docs/ammo.md +362 -0
- package/docs/api-reference.md +489 -0
- package/docs/auto-docs.md +215 -0
- package/docs/cli.md +152 -0
- package/docs/configuration.md +233 -0
- package/docs/database.md +391 -0
- package/docs/error-handling.md +417 -0
- package/docs/file-uploads.md +334 -0
- package/docs/getting-started.md +181 -0
- package/docs/middleware.md +356 -0
- package/docs/rate-limiting.md +394 -0
- package/docs/routing.md +302 -0
- package/example/API_OVERVIEW.md +77 -0
- package/example/README.md +155 -0
- package/example/index.js +27 -2
- package/example/openapi.json +390 -0
- package/example/package.json +5 -2
- package/example/services/cache.service.js +25 -0
- package/example/services/user.service.js +42 -0
- package/example/start-redis.js +2 -0
- package/example/targets/cache.target.js +35 -0
- package/example/targets/index.target.js +11 -2
- package/example/targets/users.target.js +60 -0
- package/example/tejas.config.json +13 -1
- package/package.json +20 -5
- package/rate-limit/algorithms/fixed-window.js +141 -0
- package/rate-limit/algorithms/sliding-window.js +147 -0
- package/rate-limit/algorithms/token-bucket.js +115 -0
- package/rate-limit/base.js +165 -0
- package/rate-limit/index.js +147 -0
- package/rate-limit/storage/base.js +104 -0
- package/rate-limit/storage/memory.js +102 -0
- package/rate-limit/storage/redis.js +88 -0
- package/server/ammo/body-parser.js +152 -25
- package/server/ammo/enhancer.js +6 -2
- package/server/ammo.js +356 -327
- package/server/endpoint.js +21 -0
- package/server/handler.js +113 -87
- package/server/target.js +50 -9
- package/server/targets/registry.js +160 -57
- package/te.js +363 -137
- package/tests/auto-docs/handler-analyzer.test.js +44 -0
- package/tests/auto-docs/openapi-generator.test.js +103 -0
- package/tests/auto-docs/parse.test.js +63 -0
- package/tests/auto-docs/source-resolver.test.js +58 -0
- package/tests/helpers/index.js +37 -0
- package/tests/helpers/mock-http.js +342 -0
- package/tests/helpers/test-utils.js +446 -0
- package/tests/setup.test.js +148 -0
- package/utils/configuration.js +13 -10
- package/vitest.config.js +54 -0
- package/database/mongo.js +0 -67
- package/example/targets/user/user.target.js +0 -17
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { spawn } from 'child_process';
|
|
2
|
+
import fs from 'fs';
|
|
3
|
+
import path from 'path';
|
|
4
|
+
import { fileURLToPath } from 'url';
|
|
5
|
+
import TejLogger from 'tej-logger';
|
|
6
|
+
import TejError from '../server/error.js';
|
|
7
|
+
|
|
8
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
9
|
+
const __dirname = path.dirname(__filename);
|
|
10
|
+
|
|
11
|
+
const logger = new TejLogger('MongoDBConnectionManager');
|
|
12
|
+
|
|
13
|
+
function checkMongooseInstallation() {
|
|
14
|
+
const packageJsonPath = path.join(__dirname, '..', 'package.json');
|
|
15
|
+
const nodeModulesPath = path.join(
|
|
16
|
+
__dirname,
|
|
17
|
+
'..',
|
|
18
|
+
'node_modules',
|
|
19
|
+
'mongoose',
|
|
20
|
+
);
|
|
21
|
+
|
|
22
|
+
try {
|
|
23
|
+
// Check if mongoose exists in package.json
|
|
24
|
+
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
|
|
25
|
+
const inPackageJson = !!packageJson.dependencies?.mongoose;
|
|
26
|
+
|
|
27
|
+
// Check if mongoose exists in node_modules
|
|
28
|
+
const inNodeModules = fs.existsSync(nodeModulesPath);
|
|
29
|
+
|
|
30
|
+
return {
|
|
31
|
+
needsInstall: !inPackageJson || !inNodeModules,
|
|
32
|
+
reason: !inPackageJson
|
|
33
|
+
? 'not in package.json'
|
|
34
|
+
: !inNodeModules
|
|
35
|
+
? 'not in node_modules'
|
|
36
|
+
: null,
|
|
37
|
+
};
|
|
38
|
+
} catch (error) {
|
|
39
|
+
return { needsInstall: true, reason: 'error checking installation' };
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function installMongooseSync() {
|
|
44
|
+
const spinner = ['|', '/', '-', '\\'];
|
|
45
|
+
let current = 0;
|
|
46
|
+
let intervalId;
|
|
47
|
+
|
|
48
|
+
try {
|
|
49
|
+
const { needsInstall, reason } = checkMongooseInstallation();
|
|
50
|
+
|
|
51
|
+
if (!needsInstall) {
|
|
52
|
+
return true;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// Start the spinner
|
|
56
|
+
intervalId = setInterval(() => {
|
|
57
|
+
process.stdout.write(`\r${spinner[current]} Installing mongoose...`);
|
|
58
|
+
current = (current + 1) % spinner.length;
|
|
59
|
+
}, 100);
|
|
60
|
+
|
|
61
|
+
logger.info(`Tejas will install mongoose (${reason})...`);
|
|
62
|
+
|
|
63
|
+
const command = process.platform === 'win32' ? 'npm.cmd' : 'npm';
|
|
64
|
+
const result = spawn.sync(command, ['install', 'mongoose'], {
|
|
65
|
+
stdio: 'inherit',
|
|
66
|
+
shell: true,
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
process.stdout.write('\r');
|
|
70
|
+
clearInterval(intervalId);
|
|
71
|
+
|
|
72
|
+
if (result.status === 0) {
|
|
73
|
+
logger.info('Mongoose installed successfully');
|
|
74
|
+
return true;
|
|
75
|
+
} else {
|
|
76
|
+
logger.error('Mongoose installation failed');
|
|
77
|
+
return false;
|
|
78
|
+
}
|
|
79
|
+
} catch (error) {
|
|
80
|
+
if (intervalId) {
|
|
81
|
+
process.stdout.write('\r');
|
|
82
|
+
clearInterval(intervalId);
|
|
83
|
+
}
|
|
84
|
+
logger.error('Error installing mongoose:', error);
|
|
85
|
+
return false;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Create a new MongoDB connection
|
|
91
|
+
* @param {Object} config - MongoDB configuration
|
|
92
|
+
* @param {string} config.uri - MongoDB connection URI
|
|
93
|
+
* @param {Object} [config.options={}] - Additional Mongoose options
|
|
94
|
+
* @returns {Promise<mongoose.Connection>} Mongoose connection instance
|
|
95
|
+
*/
|
|
96
|
+
async function createConnection(config) {
|
|
97
|
+
const { needsInstall } = checkMongooseInstallation();
|
|
98
|
+
|
|
99
|
+
if (needsInstall) {
|
|
100
|
+
const installed = installMongooseSync();
|
|
101
|
+
if (!installed) {
|
|
102
|
+
throw new TejError(500, 'Failed to install required mongoose package');
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const { uri, options = {} } = config;
|
|
107
|
+
|
|
108
|
+
try {
|
|
109
|
+
const mongoose = await import('mongoose').then((mod) => mod.default);
|
|
110
|
+
const connection = await mongoose.createConnection(uri, options);
|
|
111
|
+
|
|
112
|
+
connection.on('error', (err) =>
|
|
113
|
+
logger.error(`MongoDB connection error:`, err),
|
|
114
|
+
);
|
|
115
|
+
connection.on('connected', () => {
|
|
116
|
+
logger.info(`MongoDB connected to ${uri}`);
|
|
117
|
+
});
|
|
118
|
+
connection.on('disconnected', () => {
|
|
119
|
+
logger.info(`MongoDB disconnected from ${uri}`);
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
return connection;
|
|
123
|
+
} catch (error) {
|
|
124
|
+
logger.error(`Failed to create MongoDB connection:`, error);
|
|
125
|
+
throw new TejError(
|
|
126
|
+
500,
|
|
127
|
+
`Failed to create MongoDB connection: ${error.message}`,
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Close a MongoDB connection
|
|
134
|
+
* @param {mongoose.Connection} connection - Mongoose connection to close
|
|
135
|
+
* @returns {Promise<void>}
|
|
136
|
+
*/
|
|
137
|
+
async function closeConnection(connection) {
|
|
138
|
+
if (connection) {
|
|
139
|
+
await connection.close();
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
export default {
|
|
144
|
+
createConnection,
|
|
145
|
+
closeConnection,
|
|
146
|
+
};
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
import { spawnSync } from 'child_process';
|
|
2
|
+
import fs from 'fs';
|
|
3
|
+
import path from 'path';
|
|
4
|
+
import TejError from '../server/error.js';
|
|
5
|
+
import TejLogger from 'tej-logger';
|
|
6
|
+
import { pathToFileURL } from 'node:url';
|
|
7
|
+
|
|
8
|
+
const packageJsonPath = path.join(process.cwd(), 'package.json');
|
|
9
|
+
const packagePath = `${process.cwd()}/node_modules/redis/dist/index.js`;
|
|
10
|
+
|
|
11
|
+
const logger = new TejLogger('RedisConnectionManager');
|
|
12
|
+
|
|
13
|
+
function checkRedisInstallation() {
|
|
14
|
+
try {
|
|
15
|
+
// Check if redis exists in package.json
|
|
16
|
+
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
|
|
17
|
+
const inPackageJson = !!packageJson.dependencies?.redis;
|
|
18
|
+
|
|
19
|
+
// Check if redis exists in node_modules
|
|
20
|
+
const inNodeModules = fs.existsSync(packagePath);
|
|
21
|
+
|
|
22
|
+
return {
|
|
23
|
+
needsInstall: !inPackageJson || !inNodeModules,
|
|
24
|
+
reason: !inPackageJson
|
|
25
|
+
? 'not in package.json'
|
|
26
|
+
: !inNodeModules
|
|
27
|
+
? 'not in node_modules'
|
|
28
|
+
: null,
|
|
29
|
+
};
|
|
30
|
+
} catch (error) {
|
|
31
|
+
logger.error(error, true);
|
|
32
|
+
return { needsInstall: true, reason: 'error checking installation' };
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function installRedisSync() {
|
|
37
|
+
const spinner = ['|', '/', '-', '\\'];
|
|
38
|
+
let current = 0;
|
|
39
|
+
let intervalId;
|
|
40
|
+
|
|
41
|
+
try {
|
|
42
|
+
const { needsInstall, reason } = checkRedisInstallation();
|
|
43
|
+
|
|
44
|
+
if (!needsInstall) {
|
|
45
|
+
return true;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// Start the spinner
|
|
49
|
+
intervalId = setInterval(() => {
|
|
50
|
+
process.stdout.write(`\r${spinner[current]} Installing redis...`);
|
|
51
|
+
current = (current + 1) % spinner.length;
|
|
52
|
+
}, 100);
|
|
53
|
+
|
|
54
|
+
logger.info(`Tejas will install redis (${reason})...`);
|
|
55
|
+
|
|
56
|
+
const command = process.platform === 'win32' ? 'npm.cmd' : 'npm';
|
|
57
|
+
const result = spawnSync(command, ['install', 'redis'], {
|
|
58
|
+
stdio: 'inherit',
|
|
59
|
+
shell: true,
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
process.stdout.write('\r');
|
|
63
|
+
clearInterval(intervalId);
|
|
64
|
+
|
|
65
|
+
if (result.status === 0) {
|
|
66
|
+
logger.info('Redis installed successfully');
|
|
67
|
+
return true;
|
|
68
|
+
} else {
|
|
69
|
+
logger.error('Redis installation failed');
|
|
70
|
+
return false;
|
|
71
|
+
}
|
|
72
|
+
} catch (error) {
|
|
73
|
+
if (intervalId) {
|
|
74
|
+
process.stdout.write('\r');
|
|
75
|
+
clearInterval(intervalId);
|
|
76
|
+
}
|
|
77
|
+
logger.error(error, true);
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Create a new Redis client or cluster
|
|
84
|
+
* @param {Object} config - Redis configuration
|
|
85
|
+
* @param {boolean} [config.isCluster=false] - Whether to use Redis Cluster
|
|
86
|
+
* @param {Object} [config.options={}] - Additional Redis options
|
|
87
|
+
* @returns {Promise<RedisClient|RedisCluster>} Redis client or cluster instance
|
|
88
|
+
*/
|
|
89
|
+
async function createConnection(config) {
|
|
90
|
+
const { needsInstall } = checkRedisInstallation();
|
|
91
|
+
|
|
92
|
+
if (needsInstall) {
|
|
93
|
+
const installed = installRedisSync();
|
|
94
|
+
if (!installed) {
|
|
95
|
+
throw new TejError(500, 'Failed to install required redis package');
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const { isCluster = false, options = {} } = config;
|
|
100
|
+
let client;
|
|
101
|
+
|
|
102
|
+
try {
|
|
103
|
+
const { createClient, createCluster } = await import(
|
|
104
|
+
pathToFileURL(packagePath)
|
|
105
|
+
);
|
|
106
|
+
|
|
107
|
+
if (isCluster) {
|
|
108
|
+
client = createCluster({
|
|
109
|
+
...options,
|
|
110
|
+
});
|
|
111
|
+
} else {
|
|
112
|
+
client = createClient({
|
|
113
|
+
...options,
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
let connectionTimeout;
|
|
118
|
+
let hasConnected = false;
|
|
119
|
+
let connectionAttempts = 0;
|
|
120
|
+
const maxRetries = options.maxRetries || 3;
|
|
121
|
+
|
|
122
|
+
// Create a promise that will resolve when connected or reject on fatal errors
|
|
123
|
+
const connectionPromise = new Promise((resolve, reject) => {
|
|
124
|
+
connectionTimeout = setTimeout(() => {
|
|
125
|
+
if (!hasConnected) {
|
|
126
|
+
client.quit().catch(() => {});
|
|
127
|
+
reject(new TejError(500, 'Redis connection timeout'));
|
|
128
|
+
}
|
|
129
|
+
}, options.connectTimeout || 10000);
|
|
130
|
+
|
|
131
|
+
client.on('error', (err) => {
|
|
132
|
+
logger.error(`Redis connection error: ${err}`, true);
|
|
133
|
+
if (!hasConnected && connectionAttempts >= maxRetries) {
|
|
134
|
+
clearTimeout(connectionTimeout);
|
|
135
|
+
client.quit().catch(() => {});
|
|
136
|
+
reject(
|
|
137
|
+
new TejError(
|
|
138
|
+
500,
|
|
139
|
+
`Redis connection failed after ${maxRetries} attempts: ${err.message}`,
|
|
140
|
+
),
|
|
141
|
+
);
|
|
142
|
+
}
|
|
143
|
+
connectionAttempts++;
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
client.on('connect', () => {
|
|
147
|
+
hasConnected = true;
|
|
148
|
+
clearTimeout(connectionTimeout);
|
|
149
|
+
logger.info(
|
|
150
|
+
`Redis connected on ${client?.options?.url ?? client?.options?.socket?.host}`,
|
|
151
|
+
);
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
client.on('ready', () => {
|
|
155
|
+
logger.info('Redis ready');
|
|
156
|
+
resolve(client);
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
client.on('end', () => {
|
|
160
|
+
logger.info('Redis connection closed');
|
|
161
|
+
});
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
await client.connect();
|
|
165
|
+
await connectionPromise;
|
|
166
|
+
|
|
167
|
+
return client;
|
|
168
|
+
} catch (error) {
|
|
169
|
+
if (client) {
|
|
170
|
+
try {
|
|
171
|
+
await client.quit();
|
|
172
|
+
} catch (quitError) {
|
|
173
|
+
logger.error(
|
|
174
|
+
`Error while cleaning up Redis connection: ${quitError}`,
|
|
175
|
+
true,
|
|
176
|
+
);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
logger.error(`Failed to create Redis connection: ${error}`, true);
|
|
180
|
+
throw new TejError(
|
|
181
|
+
500,
|
|
182
|
+
`Failed to create Redis connection: ${error.message}`,
|
|
183
|
+
);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Close a Redis connection
|
|
189
|
+
* @param {RedisClient|RedisCluster} client - Redis client to close
|
|
190
|
+
* @returns {Promise<void>}
|
|
191
|
+
*/
|
|
192
|
+
async function closeConnection(client) {
|
|
193
|
+
if (client) {
|
|
194
|
+
await client.quit();
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
export default {
|
|
199
|
+
createConnection,
|
|
200
|
+
closeConnection,
|
|
201
|
+
};
|
package/docs/README.md
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# Tejas Documentation
|
|
2
|
+
|
|
3
|
+
Welcome to the documentation for **Tejas** — a Node.js framework for building powerful backend services.
|
|
4
|
+
|
|
5
|
+
## Table of Contents
|
|
6
|
+
|
|
7
|
+
### Getting Started
|
|
8
|
+
|
|
9
|
+
- [Introduction & Quick Start](./getting-started.md) — Install, create your first app, and core concepts
|
|
10
|
+
- [Configuration](./configuration.md) — Config sources, all available options, and environment variables
|
|
11
|
+
|
|
12
|
+
### Core Concepts
|
|
13
|
+
|
|
14
|
+
- [Routing (Targets)](./routing.md) — Define routes, parameterized paths, and endpoint metadata
|
|
15
|
+
- [Request & Response (Ammo)](./ammo.md) — Handle requests, send responses, and access request data
|
|
16
|
+
- [Middleware](./middleware.md) — Global, target, and route-level middleware with Express compatibility
|
|
17
|
+
- [Error Handling](./error-handling.md) — Zero-config error handling, TejError, and BodyParserError
|
|
18
|
+
|
|
19
|
+
### Features
|
|
20
|
+
|
|
21
|
+
- [Database Integration](./database.md) — Redis and MongoDB connections with auto-install
|
|
22
|
+
- [Rate Limiting](./rate-limiting.md) — Three algorithms, two storage backends, custom headers
|
|
23
|
+
- [File Uploads](./file-uploads.md) — Single and multiple file handling with validation
|
|
24
|
+
|
|
25
|
+
### Tooling
|
|
26
|
+
|
|
27
|
+
- [CLI Reference](./cli.md) — `tejas fly`, `tejas generate:docs`, and `tejas docs:on-push`
|
|
28
|
+
- [Auto-Documentation](./auto-docs.md) — LLM-powered OpenAPI generation and Scalar API docs UI
|
|
29
|
+
|
|
30
|
+
### Reference
|
|
31
|
+
|
|
32
|
+
- [API Reference](./api-reference.md) — Complete API documentation for all classes and functions
|
|
33
|
+
|
|
34
|
+
---
|
|
35
|
+
|
|
36
|
+
This documentation is for Tejas v2.0.0.
|