rajt 0.0.61 → 0.0.63
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/README.md +3 -0
- package/bin/rajt.js +45 -46
- package/package.json +14 -14
- package/src/action.ts +2 -1
- package/src/cli/commands/dev/index.ts +33 -21
- package/src/cli/commands/dev/utils.ts +1 -2
- package/src/cli/index.ts +3 -5
- package/src/create-app.ts +7 -4
- package/src/dev-node.ts +16 -0
- package/src/dev.ts +5 -18
- package/src/esbuild.mjs +1 -2
- package/src/http.ts +2 -1
- package/src/index.ts +3 -1
- package/src/request.ts +2 -0
- package/src/routes.ts +14 -14
- package/src/utils/json-import.ts +1 -3
- package/src/utils/logger.ts +10 -3
package/README.md
CHANGED
|
@@ -6,6 +6,9 @@
|
|
|
6
6
|
|
|
7
7
|
<br/>
|
|
8
8
|
|
|
9
|
+
> ⚠️ Rajt is under ALFA development — expect updates, rough edges, and occasional breack changes.
|
|
10
|
+
<br/>
|
|
11
|
+
|
|
9
12
|
This framework is fully geared towards the serverless world, specifically AWS Lambda (Node.js, Bun and LLRT runtime) / Cloudflare Workers.
|
|
10
13
|
|
|
11
14
|
- 💡 Instant Server Start
|
package/bin/rajt.js
CHANGED
|
@@ -2,19 +2,16 @@
|
|
|
2
2
|
import { spawn } from "node:child_process";
|
|
3
3
|
import { join, dirname } from "node:path";
|
|
4
4
|
import { existsSync } from "node:fs";
|
|
5
|
-
import { fileURLToPath } from "node:url";
|
|
6
5
|
|
|
7
|
-
const
|
|
8
|
-
const __dirname = dirname(__filename);
|
|
6
|
+
const __dirname = dirname(new URL(import.meta.url).pathname);
|
|
9
7
|
|
|
10
8
|
const ERR_NODE_VERSION = "18.0.0";
|
|
11
9
|
const MIN_NODE_VERSION = "18.0.0";
|
|
12
10
|
|
|
13
11
|
let rajtProcess;
|
|
14
12
|
|
|
15
|
-
// Executes ../src/cli/index.ts
|
|
16
13
|
function runRajt() {
|
|
17
|
-
if (semiver(process.versions.node, ERR_NODE_VERSION) < 0) {
|
|
14
|
+
if (process?.versions?.node && semiver(process.versions.node, ERR_NODE_VERSION) < 0) {
|
|
18
15
|
console.error(
|
|
19
16
|
`Rajt requires at least Node.js v${MIN_NODE_VERSION}. You are using v${process.versions.node}. Please update your version of Node.js.
|
|
20
17
|
|
|
@@ -24,30 +21,35 @@ Consider using a Node.js version manager such as https://volta.sh or https://git
|
|
|
24
21
|
return;
|
|
25
22
|
}
|
|
26
23
|
|
|
27
|
-
const
|
|
28
|
-
// join(__dirname, "../node_modules/.bin/tsx"),
|
|
29
|
-
// join(__dirname, "../../.bin/tsx"),
|
|
30
|
-
join(__dirname, "../node_modules/.bin/tsx"),
|
|
31
|
-
join(__dirname, "../../node_modules/.bin/tsx"),
|
|
32
|
-
join(process.cwd(), "node_modules/.bin/tsx"),
|
|
33
|
-
"tsx",
|
|
34
|
-
];
|
|
35
|
-
|
|
24
|
+
const isBun = process?.isBun || typeof Bun != 'undefined';
|
|
36
25
|
let tsxPath;
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
26
|
+
// const params = isBun ? bunParams() : nodeParams();
|
|
27
|
+
|
|
28
|
+
if (!isBun) {
|
|
29
|
+
const tsxPaths = [
|
|
30
|
+
// join(__dirname, "../node_modules/.bin/tsx"),
|
|
31
|
+
// join(__dirname, "../../.bin/tsx"),
|
|
32
|
+
join(__dirname, "../node_modules/.bin/tsx"),
|
|
33
|
+
join(__dirname, "../../node_modules/.bin/tsx"),
|
|
34
|
+
join(process.cwd(), "node_modules/.bin/tsx"),
|
|
35
|
+
"tsx",
|
|
36
|
+
];
|
|
37
|
+
|
|
38
|
+
for (const pathOption of tsxPaths) {
|
|
39
|
+
if (pathOption == "tsx" || existsSync(pathOption)) {
|
|
40
|
+
tsxPath = pathOption;
|
|
41
|
+
break;
|
|
42
|
+
}
|
|
41
43
|
}
|
|
42
|
-
}
|
|
43
44
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
45
|
+
if (!tsxPath) {
|
|
46
|
+
console.error("TypeScript file found but tsx is not available. Please install tsx:");
|
|
47
|
+
console.error(" npm i -D tsx");
|
|
48
|
+
console.error(" or");
|
|
49
|
+
console.error(" bun i -D tsx");
|
|
50
|
+
process.exit(1);
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
51
53
|
}
|
|
52
54
|
|
|
53
55
|
return spawn(
|
|
@@ -56,12 +58,9 @@ Consider using a Node.js version manager such as https://volta.sh or https://git
|
|
|
56
58
|
"--no-warnings",
|
|
57
59
|
...process.execArgv,
|
|
58
60
|
tsxPath,
|
|
59
|
-
join(__dirname, "../cli/index.ts"),
|
|
61
|
+
join(__dirname, "../src/cli/index.ts"),
|
|
60
62
|
...process.argv.slice(2),
|
|
61
|
-
].filter(arg =>
|
|
62
|
-
!arg.includes('experimental-vm-modules') &&
|
|
63
|
-
!arg.includes('loader')
|
|
64
|
-
),
|
|
63
|
+
].filter(arg => arg && !arg.includes('experimental-vm-modules') && !arg.includes('loader')),
|
|
65
64
|
{
|
|
66
65
|
stdio: ["inherit", "inherit", "inherit", "ipc"],
|
|
67
66
|
env: {
|
|
@@ -70,20 +69,17 @@ Consider using a Node.js version manager such as https://volta.sh or https://git
|
|
|
70
69
|
TSX_DISABLE_CACHE: process.env.TSX_DISABLE_CACHE || '1',
|
|
71
70
|
}
|
|
72
71
|
}
|
|
73
|
-
)
|
|
74
|
-
.
|
|
75
|
-
|
|
76
|
-
)
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
process.disconnect();
|
|
85
|
-
}
|
|
86
|
-
});
|
|
72
|
+
).on("exit", (code) =>
|
|
73
|
+
process.exit(code == null ? 0 : code)
|
|
74
|
+
).on("message", (message) => {
|
|
75
|
+
if (process.send) {
|
|
76
|
+
process.send(message);
|
|
77
|
+
}
|
|
78
|
+
}).on("disconnect", () => {
|
|
79
|
+
if (process.disconnect) {
|
|
80
|
+
process.disconnect();
|
|
81
|
+
}
|
|
82
|
+
});
|
|
87
83
|
}
|
|
88
84
|
|
|
89
85
|
var fn = new Intl.Collator(0, { numeric: 1 }).compare;
|
|
@@ -104,7 +100,10 @@ function semiver(a, b, bool) {
|
|
|
104
100
|
function directly() {
|
|
105
101
|
try {
|
|
106
102
|
return process.env?.npm_lifecycle_script == 'rajt'
|
|
107
|
-
&&
|
|
103
|
+
&& (
|
|
104
|
+
process.argv[1]?.endsWith('node_modules/.bin/rajt')
|
|
105
|
+
|| process.argv[1]?.endsWith('node_modules/rajt/bin/rajt.js')
|
|
106
|
+
)
|
|
108
107
|
} catch {
|
|
109
108
|
return false
|
|
110
109
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "rajt",
|
|
3
3
|
"description": "A serverless bundler layer, fully typed for AWS Lambda (Node.js and LLRT) and Cloudflare Workers.",
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.63",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
7
7
|
"bin": {
|
|
@@ -52,12 +52,20 @@
|
|
|
52
52
|
"start": "node ../../dist/index.js"
|
|
53
53
|
},
|
|
54
54
|
"dependencies": {
|
|
55
|
-
"@hono/zod-validator": "^0.4.3",
|
|
56
55
|
"cripta": "^0.1",
|
|
57
56
|
"forj": "^0.0.4",
|
|
58
|
-
"hono": "^4.7.6",
|
|
59
57
|
"t0n": "^0.1",
|
|
60
|
-
"
|
|
58
|
+
"@hono/zod-validator": "^0.7.6",
|
|
59
|
+
"chokidar-cli": "^3.0.0",
|
|
60
|
+
"citty": "^0.1.6",
|
|
61
|
+
"dotenv": "^16.5.0",
|
|
62
|
+
"esbuild": "^0.25.2",
|
|
63
|
+
"hono": "^4.11.7",
|
|
64
|
+
"miniflare": "^4.20251217.0",
|
|
65
|
+
"picocolors": "^1.1.1",
|
|
66
|
+
"tiny-glob": "^0.2",
|
|
67
|
+
"tsx": "^4.19.3",
|
|
68
|
+
"ua-parser-js": "^2.0.8"
|
|
61
69
|
},
|
|
62
70
|
"devDependencies": {
|
|
63
71
|
"@cloudflare/workers-types": "^4.20251230.0",
|
|
@@ -74,16 +82,8 @@
|
|
|
74
82
|
"@miniflare/web-sockets": "^2.14.4",
|
|
75
83
|
"@types/node": "^20.11.0",
|
|
76
84
|
"bun-types": "^1.2.14",
|
|
77
|
-
"
|
|
78
|
-
"
|
|
79
|
-
"dotenv": "^16.5.0",
|
|
80
|
-
"esbuild": "^0.25.2",
|
|
81
|
-
"miniflare": "^4.20251217.0",
|
|
82
|
-
"picocolors": "^1.1.1",
|
|
83
|
-
"tiny-glob": "^0.2",
|
|
84
|
-
"tsx": "^4.19.3",
|
|
85
|
-
"typescript": "^5.8.3",
|
|
86
|
-
"wrangler": "^4.56.0"
|
|
85
|
+
"typescript": "^5.9.3",
|
|
86
|
+
"wrangler": "^4.61.0"
|
|
87
87
|
},
|
|
88
88
|
"engines": {
|
|
89
89
|
"node": ">=18.0.0"
|
package/src/action.ts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import response from './response'
|
|
2
2
|
import validator from './validator'
|
|
3
|
+
import { GET_REQUEST } from './request'
|
|
3
4
|
import type { Context, IRequest, IResponse, IValidator, Rules } from './types'
|
|
4
5
|
|
|
5
6
|
export default class Action {
|
|
6
7
|
static run() {
|
|
7
8
|
const rules = this.rules(validator)
|
|
8
|
-
const h = async (c: Context) => await this.handle(c.get(
|
|
9
|
+
const h = async (c: Context) => await this.handle(c.get(GET_REQUEST as unknown as string), response)
|
|
9
10
|
if (!rules) return [h]
|
|
10
11
|
|
|
11
12
|
const pipe = validator.parse(rules)
|
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
import { existsSync } from 'node:fs'
|
|
2
|
-
import { fileURLToPath } from 'node:url'
|
|
3
2
|
import { dirname, join, relative } from 'node:path'
|
|
4
3
|
import { spawn, type ChildProcess } from 'node:child_process'
|
|
5
4
|
|
|
6
5
|
import chokidar from 'chokidar'
|
|
7
|
-
import colors from 'picocolors'
|
|
6
|
+
// import colors from 'picocolors'
|
|
8
7
|
import { defineCommand } from 'citty'
|
|
9
8
|
import type { ChokidarEventName } from '../../types'
|
|
10
9
|
|
|
@@ -13,7 +12,7 @@ import { build, createMiniflare } from './utils'
|
|
|
13
12
|
import { getAvailablePort } from '../../../utils/port'
|
|
14
13
|
import shutdown from '../../../utils/shutdown'
|
|
15
14
|
|
|
16
|
-
const __dirname = join(dirname(
|
|
15
|
+
const __dirname = join(dirname(new URL(import.meta.url).pathname), '../../../../../../')
|
|
17
16
|
|
|
18
17
|
export default defineCommand({
|
|
19
18
|
meta: {
|
|
@@ -36,7 +35,7 @@ export default defineCommand({
|
|
|
36
35
|
description: 'Environment platform',
|
|
37
36
|
type: 'enum',
|
|
38
37
|
options: ['aws', 'cf', 'node'] as const,
|
|
39
|
-
required: true,
|
|
38
|
+
// required: true,
|
|
40
39
|
},
|
|
41
40
|
},
|
|
42
41
|
async run({ args }) {
|
|
@@ -93,7 +92,7 @@ export default defineCommand({
|
|
|
93
92
|
'local', 'start-api',
|
|
94
93
|
'--warm-containers', 'LAZY',
|
|
95
94
|
'--debug', '--template-file', join(__dirname, 'template-dev.yaml'),
|
|
96
|
-
'--port',
|
|
95
|
+
'--port', String(port),
|
|
97
96
|
],
|
|
98
97
|
{
|
|
99
98
|
stdio: ['inherit', 'inherit', 'inherit', 'ipc'],
|
|
@@ -168,30 +167,43 @@ export default defineCommand({
|
|
|
168
167
|
logger.step('Server restarted')
|
|
169
168
|
})
|
|
170
169
|
})
|
|
170
|
+
default:
|
|
171
171
|
case 'node':
|
|
172
172
|
return withPort(desiredPort, async (port) => {
|
|
173
173
|
logger.step(`API running on http://${host}:${port}`)
|
|
174
|
+
const isBun = process?.isBun || typeof Bun != 'undefined'
|
|
175
|
+
const params = isBun
|
|
176
|
+
? ['run', '--port='+ port, '--hot', '--silent', '--no-clear-screen', '--no-summary', join(__dirname, 'node_modules/rajt/src/dev.ts')]
|
|
177
|
+
: [join(__dirname, 'node_modules/.bin/tsx'), 'watch', join(__dirname, 'node_modules/rajt/src/dev-node.ts')]
|
|
174
178
|
|
|
175
|
-
spawn(
|
|
179
|
+
const child = spawn(
|
|
176
180
|
process.execPath,
|
|
177
|
-
|
|
178
|
-
join(__dirname, 'node_modules/.bin/tsx'), 'watch', join(__dirname, 'node_modules/rajt/src/dev.ts'),
|
|
179
|
-
],
|
|
181
|
+
params,
|
|
180
182
|
{
|
|
181
|
-
stdio: ['inherit', 'inherit', 'inherit', 'ipc'],
|
|
182
|
-
env: {...process.env, PORT:
|
|
183
|
+
stdio: ['inherit', isBun ? 'pipe' : 'inherit', 'inherit', 'ipc'],
|
|
184
|
+
env: {...process.env, PORT: port},
|
|
183
185
|
}
|
|
184
|
-
)
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
186
|
+
)
|
|
187
|
+
|
|
188
|
+
if (isBun && child?.stdout) {
|
|
189
|
+
child.stdout?.on('data', data => {
|
|
190
|
+
const output = data.toString()
|
|
191
|
+
if (!output.includes('Started development server'))
|
|
192
|
+
process.stdout.write(output)
|
|
193
|
+
})
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
child.on('exit', code => process.exit(code ?? 0))
|
|
197
|
+
.on('message', msg => {
|
|
198
|
+
if (process.send) process.send(msg)
|
|
199
|
+
}).on('disconnect', () => {
|
|
200
|
+
if (process.disconnect) process.disconnect()
|
|
201
|
+
})
|
|
190
202
|
})
|
|
191
|
-
default:
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
203
|
+
// default:
|
|
204
|
+
// return logger.warn(
|
|
205
|
+
// `🟠 Provide a valid platform: ${['aws', 'cf', 'node'].map(p => colors.blue(p)).join(', ')}.\n`
|
|
206
|
+
// )
|
|
195
207
|
}
|
|
196
208
|
},
|
|
197
209
|
})
|
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
import esbuild from 'esbuild'
|
|
2
2
|
import TOML from '@iarna/toml'
|
|
3
3
|
import { Miniflare } from 'miniflare'
|
|
4
|
-
import { fileURLToPath } from 'node:url'
|
|
5
4
|
import { mkdirSync, existsSync, readdirSync, rmSync, copyFileSync } from 'node:fs'
|
|
6
5
|
import { readFile, stat, writeFile } from 'node:fs/promises'
|
|
7
6
|
import { basename, dirname, join, relative } from 'node:path'
|
|
8
7
|
|
|
9
8
|
import { cacheRoutes } from '../../../routes'
|
|
10
9
|
|
|
11
|
-
const __dirname = join(dirname(
|
|
10
|
+
const __dirname = join(dirname(new URL(import.meta.url).pathname), '../../../../../../')
|
|
12
11
|
const __rajt = join(__dirname, 'node_modules/rajt/src')
|
|
13
12
|
|
|
14
13
|
export const formatSize = (bytes: number) => {
|
package/src/cli/index.ts
CHANGED
|
@@ -25,11 +25,9 @@ console.error = () => {}
|
|
|
25
25
|
const directly = () => {
|
|
26
26
|
try {
|
|
27
27
|
// @ts-ignore
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|| import.meta.url == `file://${process.argv[1]}`
|
|
32
|
-
)
|
|
28
|
+
return typeof vitest == 'undefined'
|
|
29
|
+
&& process.env?.npm_lifecycle_script == 'rajt'
|
|
30
|
+
&& import.meta.url == `file://${process.argv[1]}`
|
|
33
31
|
} catch {
|
|
34
32
|
return false
|
|
35
33
|
}
|
package/src/create-app.ts
CHANGED
|
@@ -6,7 +6,8 @@ import type { Env, Context, ErrorHandler, NotFoundHandler, Next } from 'hono'
|
|
|
6
6
|
// import { createMiddleware } from 'hono/factory'
|
|
7
7
|
// import type { H, Handler, HandlerResponse } from 'hono/types'
|
|
8
8
|
import type { HTTPResponseError } from 'hono/types'
|
|
9
|
-
import
|
|
9
|
+
import { createColors } from 'picocolors'
|
|
10
|
+
import { getColorEnabledAsync } from 'hono/utils/color'
|
|
10
11
|
import { Envir } from 't0n'
|
|
11
12
|
import type { Routes } from './types'
|
|
12
13
|
import { BadRequest, Unauthorized } from './exceptions'
|
|
@@ -14,9 +15,11 @@ import { resolve, resolveMiddleware } from './utils/resolve'
|
|
|
14
15
|
import { getMiddlewares, getHandler } from './register'
|
|
15
16
|
import { isDev } from './utils/environment'
|
|
16
17
|
import localDate from './utils/local-date'
|
|
17
|
-
import request from './request'
|
|
18
|
+
import request, { GET_REQUEST } from './request'
|
|
18
19
|
import response from './response'
|
|
19
20
|
|
|
21
|
+
const colors = createColors(await getColorEnabledAsync())
|
|
22
|
+
|
|
20
23
|
type InitFunction<E extends Env = Env> = (app: Hono<E>) => void
|
|
21
24
|
|
|
22
25
|
export type ServerOptions<E extends Env = Env> = Partial<{
|
|
@@ -85,12 +88,12 @@ export const createApp = <E extends Env>(options?: ServerOptions<E>) => {
|
|
|
85
88
|
app.use('*', logger((...args: any[]) => console.log(colors.gray(localDate()), ...args)))
|
|
86
89
|
|
|
87
90
|
app.use(async (c: Context, next: Next) => {
|
|
88
|
-
c.set(
|
|
91
|
+
c.set(GET_REQUEST as unknown as string, new request(c))
|
|
89
92
|
if (c.env) Envir.add(c.env)
|
|
90
93
|
await next()
|
|
91
94
|
})
|
|
92
95
|
getMiddlewares().forEach(mw => {
|
|
93
|
-
const h = async (c: Context, next: Next) => await resolveMiddleware(mw)(c.get(
|
|
96
|
+
const h = async (c: Context, next: Next) => await resolveMiddleware(mw)(c.get(GET_REQUEST as unknown as string), next)
|
|
94
97
|
// @ts-ignore
|
|
95
98
|
mw?.path ? app.use(String(mw.path), h) : app.use(h)
|
|
96
99
|
})
|
package/src/dev-node.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { serve, type ServerType } from '@hono/node-server'
|
|
2
|
+
import app from './dev'
|
|
3
|
+
import shutdown from './utils/shutdown'
|
|
4
|
+
|
|
5
|
+
const fetch = app.fetch
|
|
6
|
+
|
|
7
|
+
const port = process.env?.PORT ? Number(process.env.PORT) : 3000
|
|
8
|
+
|
|
9
|
+
let server: ServerType | null = serve({ fetch, port })
|
|
10
|
+
|
|
11
|
+
shutdown(() => {
|
|
12
|
+
if (server) {
|
|
13
|
+
server?.close()
|
|
14
|
+
server = null
|
|
15
|
+
}
|
|
16
|
+
})
|
package/src/dev.ts
CHANGED
|
@@ -1,19 +1,15 @@
|
|
|
1
|
-
import { fileURLToPath } from 'node:url'
|
|
2
1
|
import { dirname, join } from 'node:path'
|
|
3
2
|
import { config } from 'dotenv'
|
|
4
|
-
import { serve, type ServerType } from '@hono/node-server'
|
|
5
3
|
import createApp from './create-app'
|
|
6
4
|
import { getRoutes, getMiddlewares, getConfigs } from './routes'
|
|
7
5
|
import { registerHandler, registerMiddleware } from './register'
|
|
8
6
|
import Config from './config'
|
|
9
|
-
import { Ability } from '
|
|
10
|
-
import
|
|
11
|
-
import { setEnv, detectEnvironment } from './utils/environment'
|
|
12
|
-
import shutdown from './utils/shutdown'
|
|
7
|
+
import { Ability } from 'rajt/auth'
|
|
8
|
+
import { setEnv, detectEnvironment } from 'rajt/env'
|
|
13
9
|
|
|
14
10
|
setEnv(detectEnvironment())
|
|
15
11
|
|
|
16
|
-
const __dirname = join(dirname(
|
|
12
|
+
const __dirname = join(dirname(new URL(import.meta.url).pathname), '../../../')
|
|
17
13
|
|
|
18
14
|
config({ path: join(__dirname, '.env.dev') })
|
|
19
15
|
|
|
@@ -29,15 +25,6 @@ middlewares.forEach(mw => registerMiddleware(mw.handle))
|
|
|
29
25
|
Ability.fromRoutes(routes)
|
|
30
26
|
Ability.roles = Config.get('roles', {})
|
|
31
27
|
|
|
32
|
-
const
|
|
28
|
+
const app = createApp({ routes })
|
|
33
29
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
let server: ServerType | null = serve({ fetch, port })
|
|
37
|
-
|
|
38
|
-
shutdown(() => {
|
|
39
|
-
if (server) {
|
|
40
|
-
server?.close()
|
|
41
|
-
server = null
|
|
42
|
-
}
|
|
43
|
-
})
|
|
30
|
+
export default app
|
package/src/esbuild.mjs
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import esbuild from 'esbuild'
|
|
2
2
|
import { basename, dirname, join, relative } from 'node:path'
|
|
3
|
-
import { fileURLToPath } from 'node:url'
|
|
4
3
|
import { mkdirSync, existsSync, readdirSync, rmSync, copyFileSync } from 'node:fs'
|
|
5
4
|
import { readFile, stat, writeFile } from 'node:fs/promises'
|
|
6
5
|
|
|
@@ -16,7 +15,7 @@ const platforms = ['aws', 'cf']
|
|
|
16
15
|
if (!platform || !platforms.includes(platform))
|
|
17
16
|
fail()
|
|
18
17
|
|
|
19
|
-
const __dirname = dirname(
|
|
18
|
+
const __dirname = dirname(new URL(import.meta.url).pathname)
|
|
20
19
|
|
|
21
20
|
const formatSize = (bytes) => {
|
|
22
21
|
if (bytes < 1024) return `${bytes}b`
|
package/src/http.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { Context, Next } from 'hono'
|
|
2
2
|
import { MiddlewareType } from './middleware'
|
|
3
3
|
import response from './response'
|
|
4
|
+
import { GET_REQUEST } from './request'
|
|
4
5
|
import { Ability } from './auth'
|
|
5
6
|
import mergeMiddleware from './utils/merge-middleware'
|
|
6
7
|
import { IRequest } from './types'
|
|
@@ -102,7 +103,7 @@ export function Auth(...args: any[]): void | ClassDecorator {
|
|
|
102
103
|
|
|
103
104
|
function _auth(target: Function | any) {
|
|
104
105
|
mergeMiddleware(target, async (c: Context, next: Next) => {
|
|
105
|
-
const req = c.get(
|
|
106
|
+
const req = c.get(GET_REQUEST as unknown as string) as IRequest
|
|
106
107
|
const ability = Ability.fromAction(target)
|
|
107
108
|
|
|
108
109
|
if (!req?.user || !ability || req.cant(ability))
|
package/src/index.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
export { default as Action } from './action'
|
|
1
|
+
export { default as Action, default as Route } from './action'
|
|
2
2
|
export { default as Middleware } from './middleware'
|
|
3
3
|
export { default as Response } from './response'
|
|
4
4
|
export { default as Config } from './config'
|
|
5
|
+
|
|
5
6
|
export { Enum, Envir } from 't0n'
|
|
7
|
+
export type { EnumStatic, EnumValue, EnumType } from 't0n'
|
package/src/request.ts
CHANGED
|
@@ -17,6 +17,8 @@ const cookieWrapper = (c: Context) => ({
|
|
|
17
17
|
delete: (name: string, opt?: CookieOptions) => deleteCookie(c, name, opt)
|
|
18
18
|
})
|
|
19
19
|
|
|
20
|
+
export const GET_REQUEST: unique symbol = Symbol()
|
|
21
|
+
|
|
20
22
|
export default class $Request {
|
|
21
23
|
#c!: Context
|
|
22
24
|
#cookie: ReturnType<typeof cookieWrapper>
|
package/src/routes.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { existsSync, readdirSync, statSync, writeFileSync } from 'node:fs'
|
|
2
2
|
import { dirname, join, resolve } from 'node:path'
|
|
3
|
-
import { fileURLToPath } from 'node:url'
|
|
4
3
|
|
|
5
4
|
import glob from 'tiny-glob'
|
|
6
5
|
import { config } from 'dotenv'
|
|
@@ -11,7 +10,7 @@ import ensureDir from './utils/ensuredir'
|
|
|
11
10
|
import versionSHA from './utils/version-sha'
|
|
12
11
|
import type { Route } from './types'
|
|
13
12
|
|
|
14
|
-
const __filename =
|
|
13
|
+
const __filename = new URL(import.meta.url).pathname
|
|
15
14
|
const __root = resolve(dirname(__filename), '../../..')
|
|
16
15
|
|
|
17
16
|
const importName = (name?: string) => (name || 'Fn'+ Math.random().toString(36).substring(2)).replace(/\.ts$/, '')
|
|
@@ -159,7 +158,8 @@ export async function getConfigs(
|
|
|
159
158
|
if (!dirs?.length) return {}
|
|
160
159
|
const configs: Record<string, any> = {}
|
|
161
160
|
|
|
162
|
-
const files = await glob(join(__root, dirs?.length > 1 ? `{${dirs.join(',')}}` : dirs[0], '/**/*.{ts,js,json}'))
|
|
161
|
+
const files = (await glob(join(__root, dirs?.length > 1 ? `{${dirs.join(',')}}` : dirs[0], '/**/*.{ts,js,cjs,mjs,json}')))
|
|
162
|
+
.filter(file => !file.includes('.d.'))
|
|
163
163
|
|
|
164
164
|
for (const file of files) {
|
|
165
165
|
const mod = await IMPORT(join(__root, file))
|
|
@@ -179,14 +179,6 @@ export async function getConfigs(
|
|
|
179
179
|
return configs
|
|
180
180
|
}
|
|
181
181
|
|
|
182
|
-
const env = Object.entries(
|
|
183
|
-
config({ path: '../../.env.prod' })?.parsed || {}
|
|
184
|
-
).filter(([key, val]) => key?.toLowerCase().indexOf('aws') != 0) // prevent AWS credentials
|
|
185
|
-
|
|
186
|
-
const version = versionSHA('../../.git') // @ts-ignore
|
|
187
|
-
env.push(['VERSION_SHA', process.env['VERSION_SHA'] = version]) // @ts-ignore
|
|
188
|
-
env.push(['VERSION_HASH', process.env['VERSION_HASH'] = version?.substring(0, 7)])
|
|
189
|
-
|
|
190
182
|
const IDENTIFIER_RE = /^[$_\p{ID_Start}][$_\u200C\u200D\p{ID_Continue}]*$/u
|
|
191
183
|
function stringifyToJS(value: unknown): string {
|
|
192
184
|
if (value === null) return 'null'
|
|
@@ -213,6 +205,14 @@ function stringifyToJS(value: unknown): string {
|
|
|
213
205
|
}
|
|
214
206
|
|
|
215
207
|
export async function cacheRoutes() {
|
|
208
|
+
const env = Object.entries(
|
|
209
|
+
config({ path: '../../.env.prod' })?.parsed || {}
|
|
210
|
+
).filter(([key, val]) => key?.toLowerCase().indexOf('aws') != 0) // prevent AWS credentials
|
|
211
|
+
|
|
212
|
+
const version = versionSHA('../../.git') // @ts-ignore
|
|
213
|
+
env.push(['VERSION_SHA', process.env['VERSION_SHA'] = version]) // @ts-ignore
|
|
214
|
+
env.push(['VERSION_HASH', process.env['VERSION_HASH'] = version?.substring(0, 7)])
|
|
215
|
+
|
|
216
216
|
const rolePath = join(__root, 'configs/roles.ts')
|
|
217
217
|
ensureDir(rolePath)
|
|
218
218
|
if (!existsSync(rolePath))
|
|
@@ -230,8 +230,8 @@ ${configs?.length ? `import Config from '../node_modules/rajt/src/config'\nConfi
|
|
|
230
230
|
|
|
231
231
|
import { registerHandler, registerMiddleware } from '../node_modules/rajt/src/register'
|
|
232
232
|
|
|
233
|
-
${routes.map(r => `import ${r.name} from '../${
|
|
234
|
-
${middlewares.map(r => `import ${r.name} from '../${
|
|
233
|
+
${routes.map(r => `import ${r.name} from '../${normalizeImportPath(r.file)}'`).join('\n')}
|
|
234
|
+
${middlewares.map(r => `import ${r.name} from '../${normalizeImportPath(r.file)}'`).join('\n')}
|
|
235
235
|
|
|
236
236
|
try {
|
|
237
237
|
const handlers = {${routes.map(r => r.name).join()}}
|
|
@@ -262,6 +262,6 @@ try {
|
|
|
262
262
|
])))
|
|
263
263
|
}
|
|
264
264
|
|
|
265
|
-
function
|
|
265
|
+
function normalizeImportPath(file: string) {
|
|
266
266
|
return file.replace(/\.tsx?$/i, '').replace(/(\/index)+$/i, '').replace(/\/+$/g, '')
|
|
267
267
|
}
|
package/src/utils/json-import.ts
CHANGED
|
@@ -1,10 +1,8 @@
|
|
|
1
1
|
import { readFileSync } from 'node:fs'
|
|
2
2
|
import { dirname, join } from 'node:path'
|
|
3
|
-
import { fileURLToPath } from 'node:url'
|
|
4
3
|
|
|
5
4
|
export default function jsonImport<T = any>(filePath: string, defaultValue: T = {} as T): T {
|
|
6
|
-
const
|
|
7
|
-
const __dirname = dirname(__filename)
|
|
5
|
+
const __dirname = dirname(new URL(import.meta.url).pathname)
|
|
8
6
|
|
|
9
7
|
try {
|
|
10
8
|
const fullPath = join(__dirname, filePath)
|
package/src/utils/logger.ts
CHANGED
|
@@ -13,17 +13,18 @@ export interface ILogger {
|
|
|
13
13
|
// custom
|
|
14
14
|
step(...data: any[]): void;
|
|
15
15
|
substep(...data: any[]): void;
|
|
16
|
+
stepWarn(...data: any[]): void;
|
|
16
17
|
ln(): void;
|
|
17
18
|
}
|
|
18
19
|
|
|
19
20
|
export const logger = {
|
|
20
|
-
|
|
21
|
-
|
|
21
|
+
$step(color: Function, ...args: any[]) {
|
|
22
|
+
if (args?.length && args.length < 2) return _console.log(color('⁕') +` ${args[0]}\n`)
|
|
22
23
|
const length = args.length - 1
|
|
23
24
|
args.forEach((arg, index) => {
|
|
24
25
|
switch (index) {
|
|
25
26
|
case 0:
|
|
26
|
-
return _console.log(
|
|
27
|
+
return _console.log(color('⁕') + ' ' + arg)
|
|
27
28
|
// return _console.log(colors.blue('⁕') +` ${arg} \n`)
|
|
28
29
|
case length:
|
|
29
30
|
return _console.log(` ${colors.gray('⁕')} ${arg}\n`)
|
|
@@ -31,6 +32,12 @@ export const logger = {
|
|
|
31
32
|
return _console.log(` ${colors.gray('⁕')} ` + arg)
|
|
32
33
|
}
|
|
33
34
|
})
|
|
35
|
+
},
|
|
36
|
+
step(...args: any[]) {
|
|
37
|
+
this.$step(colors.blue, ...args)
|
|
38
|
+
},
|
|
39
|
+
stepWarn(...args: any[]) {
|
|
40
|
+
this.$step(colors.yellow, ...args)
|
|
34
41
|
},
|
|
35
42
|
substep(...args: any[]) {
|
|
36
43
|
args.forEach(arg => _console.log(` ${colors.gray('⁕')} ` + arg))
|