rajt 0.0.67 ā 0.0.69
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/package.json +1 -12
- package/src/cli/commands/build.ts +6 -7
- package/src/cli/commands/deploy.ts +26 -14
- package/src/cli/commands/dev.ts +15 -19
- package/src/cli/commands/utils.ts +39 -30
- package/src/cli/index.ts +4 -4
- package/src/create-app.ts +1 -5
- package/src/utils/log.ts +11 -7
- package/src/context.ts +0 -43
- package/src/esbuild.mjs +0 -124
- package/src/exceptions.ts +0 -15
- package/src/scripts/cache-routes.ts +0 -10
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.69",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
7
7
|
"bin": {
|
|
@@ -26,13 +26,6 @@
|
|
|
26
26
|
],
|
|
27
27
|
"scripts": {
|
|
28
28
|
"rajt": "./src/bin/rajt.js",
|
|
29
|
-
"dev": "tsx watch src/dev.ts",
|
|
30
|
-
"aws:build": "bun run --silent cache:routes && bun run --silent aws:export && bun run --silent clean:temp",
|
|
31
|
-
"cf:build": "bun run --silent cache:routes && bun run --silent cf:export && bun run --silent clean:temp",
|
|
32
|
-
"aws:build:watch": "chokidar \"../../{actions,configs,enums,locales,middlewares,models,utils}/**/*.ts\" -c \"bun run --silent aws:build\" --initial",
|
|
33
|
-
"cf:build:watch": "chokidar \"../../{actions,configs,enums,locales,middlewares,models,utils}/**/*.ts\" -c \"bun run --silent cf:build\" --initial",
|
|
34
|
-
"aws:export": "node src/esbuild.mjs aws",
|
|
35
|
-
"cf:export": "node src/esbuild.mjs cf",
|
|
36
29
|
"aws:local": "bun run --silent aws:build && bun run --silent sam:local",
|
|
37
30
|
"aws:package": "bun run --silent aws:build && bun run --silent sam:package",
|
|
38
31
|
"aws:deploy": "bun run --silent aws:build && bun run --silent sam:package && bun run --silent sam:deploy",
|
|
@@ -41,10 +34,6 @@
|
|
|
41
34
|
"sam:package": "sam package --template-file ../../template-prod.yaml --output-template-file ../../packaged.yaml",
|
|
42
35
|
"sam:deploy": "sam deploy --template-file ../../packaged.yaml --stack-name rajt-llrt --capabilities CAPABILITY_IAM",
|
|
43
36
|
"sam:update": "source ../../.env.prod && aws lambda update-function-code --function-name $AWS_NAME --zip-file fileb://../../lambda.zip --region $AWS_REGION --no-cli-pager 2>&1 >/dev/null",
|
|
44
|
-
"cf:local-": "source ../../.env.dev && bun run --silent cf:build:watch -- cd ../../dist && bunx wrangler dev --port=$PORT --persist-to='../.wrangler/state'",
|
|
45
|
-
"cf:deploy": "bun run --silent cf:build && cd ../../dist && bunx wrangler deploy",
|
|
46
|
-
"cache:routes": "tsx src/scripts/cache-routes.ts",
|
|
47
|
-
"ensure-dirs": "rm -rf ../../dist ../../tmp && mkdir -p ../../tmp && chmod 755 ../../tmp && mkdir -p ../../dist && chmod 755 ../../dist",
|
|
48
37
|
"clean": "rm -rf ../../dist ../../tmp",
|
|
49
38
|
"clean:build": "rm -rf ../../dist",
|
|
50
39
|
"clean:temp": "rm -rf ../../tmp",
|
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
import { defineCommand } from 'citty'
|
|
2
2
|
import { gray } from 'picocolors'
|
|
3
3
|
|
|
4
|
-
import { build, normalizePlatform } from './utils'
|
|
5
|
-
import {
|
|
4
|
+
import { build, normalizePlatform, platformError } from './utils'
|
|
5
|
+
import { wait, error, } from '../../utils/log'
|
|
6
6
|
|
|
7
7
|
import { platforms } from './constants'
|
|
8
8
|
|
|
9
9
|
export default defineCommand({
|
|
10
10
|
meta: {
|
|
11
11
|
name: 'build',
|
|
12
|
-
description: 'šļø
|
|
12
|
+
description: 'šļø Perform the build\n',
|
|
13
13
|
},
|
|
14
14
|
args: {
|
|
15
15
|
platform: {
|
|
@@ -20,12 +20,11 @@ export default defineCommand({
|
|
|
20
20
|
},
|
|
21
21
|
},
|
|
22
22
|
async run({ args }) { // @ts-ignore
|
|
23
|
-
const platform = normalizePlatform(args.p || args.platform || 'node')
|
|
24
|
-
|
|
23
|
+
const platform = normalizePlatform(args.p || args.platform || args._[0] || 'node')
|
|
25
24
|
if (!platform)
|
|
26
|
-
return
|
|
25
|
+
return platformError()
|
|
27
26
|
|
|
28
|
-
|
|
27
|
+
wait('Building for platform: '+ gray(platform))
|
|
29
28
|
|
|
30
29
|
try {
|
|
31
30
|
await build(platform)
|
|
@@ -1,11 +1,8 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { spawn, type ChildProcess } from 'node:child_process'
|
|
3
|
-
|
|
1
|
+
import { spawn } from 'node:child_process'
|
|
4
2
|
import { defineCommand } from 'citty'
|
|
5
|
-
import { gray } from 'picocolors'
|
|
6
3
|
|
|
7
|
-
import { normalizePlatform } from './utils'
|
|
8
|
-
import {
|
|
4
|
+
import { _root, normalizePlatform, platformError, getRuntime } from './utils'
|
|
5
|
+
import { error } from '../../utils/log'
|
|
9
6
|
import { platforms } from './constants'
|
|
10
7
|
|
|
11
8
|
import build from './build'
|
|
@@ -13,7 +10,7 @@ import build from './build'
|
|
|
13
10
|
export default defineCommand({
|
|
14
11
|
meta: {
|
|
15
12
|
name: 'deploy',
|
|
16
|
-
description: 'āļø
|
|
13
|
+
description: 'āļø Perform the build and execute deploy\n',
|
|
17
14
|
},
|
|
18
15
|
args: {
|
|
19
16
|
platform: {
|
|
@@ -21,23 +18,38 @@ export default defineCommand({
|
|
|
21
18
|
description: 'Environment platform',
|
|
22
19
|
type: 'enum',
|
|
23
20
|
options: platforms,
|
|
24
|
-
required: true,
|
|
25
21
|
},
|
|
26
22
|
},
|
|
27
23
|
async run({ args }) { // @ts-ignore
|
|
28
|
-
const platform = normalizePlatform(args.p || args.platform)
|
|
29
|
-
|
|
24
|
+
const platform = normalizePlatform(args.p || args.platform || args._[0])
|
|
30
25
|
if (!platform)
|
|
31
|
-
return
|
|
26
|
+
return platformError()
|
|
32
27
|
|
|
33
28
|
// @ts-ignore
|
|
34
|
-
await build.run({args: { platform }})
|
|
29
|
+
await build.run({ args: { platform } })
|
|
30
|
+
const isBun = getRuntime() == 'bun'
|
|
31
|
+
|
|
35
32
|
switch (platform) {
|
|
36
33
|
case 'aws':
|
|
37
34
|
// TODO: perform aws deploy
|
|
38
|
-
return
|
|
35
|
+
return error('Platform not yet implemented, contact the webmaster')
|
|
39
36
|
case 'cf':
|
|
40
|
-
|
|
37
|
+
const child = spawn(
|
|
38
|
+
isBun ? 'bunx' : 'npx',
|
|
39
|
+
['wrangler', 'deploy'],
|
|
40
|
+
{
|
|
41
|
+
stdio: 'inherit',
|
|
42
|
+
cwd: _root,
|
|
43
|
+
}
|
|
44
|
+
)
|
|
45
|
+
|
|
46
|
+
child.on('exit', code => process.exit(code ?? 0))
|
|
47
|
+
.on('message', msg => {
|
|
48
|
+
process.send && process.send(msg)
|
|
49
|
+
}).on('disconnect', () => {
|
|
50
|
+
process.disconnect && process.disconnect()
|
|
51
|
+
})
|
|
52
|
+
|
|
41
53
|
return
|
|
42
54
|
}
|
|
43
55
|
},
|
package/src/cli/commands/dev.ts
CHANGED
|
@@ -1,17 +1,14 @@
|
|
|
1
1
|
|
|
2
|
-
import {
|
|
2
|
+
import { join } from 'node:path'
|
|
3
3
|
import { spawn, type ChildProcess } from 'node:child_process'
|
|
4
4
|
|
|
5
5
|
import { defineCommand } from 'citty'
|
|
6
|
-
|
|
7
6
|
import type { Miniflare } from 'miniflare'
|
|
8
|
-
import { build, wait, watch, createMiniflare, getDockerHost } from './utils'
|
|
9
|
-
import { step, error,
|
|
7
|
+
import { _root, build, wait, watch, normalizePlatform, platformError, getRuntime, createMiniflare, getDockerHost } from './utils'
|
|
8
|
+
import { step, error, event, warn } from '../../utils/log'
|
|
10
9
|
import { withPort } from '../../utils/port'
|
|
11
10
|
import shutdown from '../../utils/shutdown'
|
|
12
11
|
|
|
13
|
-
const __dirname = join(dirname(new URL(import.meta.url).pathname), '../../../../../')
|
|
14
|
-
|
|
15
12
|
export default defineCommand({
|
|
16
13
|
meta: {
|
|
17
14
|
name: 'dev',
|
|
@@ -36,8 +33,11 @@ export default defineCommand({
|
|
|
36
33
|
// required: true,
|
|
37
34
|
},
|
|
38
35
|
},
|
|
39
|
-
async run({ args }) {
|
|
40
|
-
const platform = args.p || args.platform
|
|
36
|
+
async run({ args }) { // @ts-ignore
|
|
37
|
+
const platform = normalizePlatform(args.p || args.platform || args._[0] || 'node')
|
|
38
|
+
if (!platform)
|
|
39
|
+
return platformError()
|
|
40
|
+
|
|
41
41
|
const desiredPort = args.port ? Number(args.port) : 3000
|
|
42
42
|
const host = args.host ? String(args.host) : 'localhost'
|
|
43
43
|
switch (platform) {
|
|
@@ -89,7 +89,7 @@ export default defineCommand({
|
|
|
89
89
|
[
|
|
90
90
|
'local', 'start-api',
|
|
91
91
|
'--warm-containers', 'LAZY',
|
|
92
|
-
'--debug', '--template-file', join(
|
|
92
|
+
'--debug', '--template-file', join(_root, 'template-dev.yaml'),
|
|
93
93
|
'--port', String(port),
|
|
94
94
|
],
|
|
95
95
|
{
|
|
@@ -120,7 +120,7 @@ export default defineCommand({
|
|
|
120
120
|
}
|
|
121
121
|
|
|
122
122
|
await buildLambda()
|
|
123
|
-
|
|
123
|
+
event(`API running on http://${host}:${port}`)
|
|
124
124
|
|
|
125
125
|
watch(async () => {
|
|
126
126
|
await buildLambda()
|
|
@@ -157,7 +157,7 @@ export default defineCommand({
|
|
|
157
157
|
}
|
|
158
158
|
|
|
159
159
|
await buildWorker()
|
|
160
|
-
|
|
160
|
+
event(`API running on http://${host}:${port}`)
|
|
161
161
|
|
|
162
162
|
watch(async () => {
|
|
163
163
|
step('Restarting server')
|
|
@@ -168,10 +168,10 @@ export default defineCommand({
|
|
|
168
168
|
default:
|
|
169
169
|
case 'node':
|
|
170
170
|
return withPort(desiredPort, async (port) => {
|
|
171
|
-
const isBun =
|
|
171
|
+
const isBun = getRuntime() == 'bun'
|
|
172
172
|
const params = isBun
|
|
173
|
-
? ['run', '--port='+ port, '--hot', '--silent', '--no-clear-screen', '--no-summary', join(
|
|
174
|
-
: [join(
|
|
173
|
+
? ['run', '--port='+ port, '--hot', '--silent', '--no-clear-screen', '--no-summary', join(_root, 'node_modules/rajt/src/dev.ts')]
|
|
174
|
+
: [join(_root, 'node_modules/.bin/tsx'), 'watch', join(_root, 'node_modules/rajt/src/dev-node.ts')]
|
|
175
175
|
|
|
176
176
|
const child = spawn(
|
|
177
177
|
process.execPath,
|
|
@@ -182,7 +182,7 @@ export default defineCommand({
|
|
|
182
182
|
}
|
|
183
183
|
)
|
|
184
184
|
|
|
185
|
-
|
|
185
|
+
event(`API running on http://${host}:${port}`)
|
|
186
186
|
|
|
187
187
|
if (isBun && child?.stdout) {
|
|
188
188
|
child.stdout?.on('data', data => {
|
|
@@ -199,10 +199,6 @@ export default defineCommand({
|
|
|
199
199
|
if (process.disconnect) process.disconnect()
|
|
200
200
|
})
|
|
201
201
|
})
|
|
202
|
-
// default:
|
|
203
|
-
// return warn(
|
|
204
|
-
// `š Provide a valid platform: ${['aws', 'cf', 'node'].map(p => colors.blue(p)).join(', ')}.\n`
|
|
205
|
-
// )
|
|
206
202
|
}
|
|
207
203
|
},
|
|
208
204
|
})
|
|
@@ -6,18 +6,19 @@ import { readFile, stat, writeFile } from 'node:fs/promises'
|
|
|
6
6
|
import { basename, dirname, join, relative } from 'node:path'
|
|
7
7
|
|
|
8
8
|
import chokidar from 'chokidar'
|
|
9
|
+
import { gray } from 'picocolors'
|
|
9
10
|
import type { ChokidarEventName, Platform } from './types'
|
|
10
11
|
|
|
11
12
|
import { cacheRoutes } from '../../routes'
|
|
12
|
-
import { step, warn } from '../../utils/log'
|
|
13
|
+
import { step, substep, event, error, warn } from '../../utils/log'
|
|
13
14
|
import { platforms } from './constants'
|
|
14
15
|
|
|
15
|
-
const
|
|
16
|
-
const __rajt = join(
|
|
16
|
+
export const _root = join(dirname(new URL(import.meta.url).pathname), '../../../../../')
|
|
17
|
+
const __rajt = join(_root, 'node_modules/rajt/src')
|
|
17
18
|
|
|
18
19
|
export function normalizePlatform(platform: Platform) {
|
|
19
|
-
platform = platform
|
|
20
|
-
if (!platforms
|
|
20
|
+
platform = platform?.toLowerCase() as Platform
|
|
21
|
+
if (!platforms?.includes(platform)) return null
|
|
21
22
|
|
|
22
23
|
switch (platform) {
|
|
23
24
|
case 'lambda':
|
|
@@ -36,6 +37,16 @@ export function normalizePlatform(platform: Platform) {
|
|
|
36
37
|
}
|
|
37
38
|
}
|
|
38
39
|
|
|
40
|
+
export const platformError = () => error(`Provide a valid platform: ${platforms.map(p => gray(p)).join(', ')}.\n`)
|
|
41
|
+
|
|
42
|
+
export function getRuntime() {
|
|
43
|
+
try {
|
|
44
|
+
return process?.isBun || typeof Bun != 'undefined' ? 'bun' : 'node'
|
|
45
|
+
} catch {
|
|
46
|
+
return 'node'
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
39
50
|
export const formatSize = (bytes: number) => {
|
|
40
51
|
if (bytes < 1024) return `${bytes}b`
|
|
41
52
|
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(2)}kb`
|
|
@@ -51,7 +62,7 @@ export const build = async (platform: Platform) => {
|
|
|
51
62
|
const startTime = Date.now()
|
|
52
63
|
|
|
53
64
|
const isCF = platform == 'cf'
|
|
54
|
-
const distDir = join(
|
|
65
|
+
const distDir = join(_root, 'dist')
|
|
55
66
|
|
|
56
67
|
existsSync(distDir)
|
|
57
68
|
? readdirSync(distDir).forEach(file => rmSync(join(distDir, file), { recursive: true, force: true }))
|
|
@@ -61,9 +72,9 @@ export const build = async (platform: Platform) => {
|
|
|
61
72
|
for (let file of [
|
|
62
73
|
'wrangler.toml',
|
|
63
74
|
]) {
|
|
64
|
-
file = join(
|
|
75
|
+
file = join(_root, file)
|
|
65
76
|
if (existsSync(file))
|
|
66
|
-
copyFileSync(file, join(
|
|
77
|
+
copyFileSync(file, join(_root, 'dist', basename(file)))
|
|
67
78
|
}
|
|
68
79
|
}
|
|
69
80
|
|
|
@@ -73,7 +84,7 @@ export const build = async (platform: Platform) => {
|
|
|
73
84
|
entryPoints: [join(__rajt, `prod${platform}.ts`)],
|
|
74
85
|
bundle: true,
|
|
75
86
|
minify: true,
|
|
76
|
-
outfile: join(
|
|
87
|
+
outfile: join(_root, 'dist/index.js'),
|
|
77
88
|
format: 'esm',
|
|
78
89
|
target: isCF ? 'es2022' : 'node20',
|
|
79
90
|
// platform: 'neutral',
|
|
@@ -98,7 +109,7 @@ export const build = async (platform: Platform) => {
|
|
|
98
109
|
// '.ts': 'ts',
|
|
99
110
|
// '.js': 'js'
|
|
100
111
|
// },
|
|
101
|
-
// tsconfig: join(
|
|
112
|
+
// tsconfig: join(_root, 'tsconfig.json'),
|
|
102
113
|
// sourcemap: true,
|
|
103
114
|
// logLevel: 'info',
|
|
104
115
|
plugins: [
|
|
@@ -144,19 +155,17 @@ export const build = async (platform: Platform) => {
|
|
|
144
155
|
|
|
145
156
|
await cacheRoutes()
|
|
146
157
|
|
|
147
|
-
|
|
158
|
+
event('Routes cached')
|
|
148
159
|
|
|
149
160
|
const result = await esbuild.build(opts)
|
|
150
161
|
if (!result?.metafile) throw Error('build fail')
|
|
151
162
|
|
|
152
|
-
step('Build completed successfully')
|
|
153
|
-
|
|
154
163
|
const stats = await stat(opts.outfile)
|
|
155
164
|
const size = formatSize(stats.size)
|
|
156
165
|
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
`${relative(join(
|
|
166
|
+
event('Build done in '+ formatTime(Date.now() - startTime))
|
|
167
|
+
substep(
|
|
168
|
+
// `${relative(join(_root, 'node_modules/rajt/src'), opts.entryPoints[0])} ā ${relative(_root, opts.outfile)}`,
|
|
160
169
|
`Size: ${size}`,
|
|
161
170
|
`Files: ${Object.keys(result.metafile.outputs).length}`
|
|
162
171
|
)
|
|
@@ -164,7 +173,7 @@ export const build = async (platform: Platform) => {
|
|
|
164
173
|
|
|
165
174
|
async function parseWranglerConfig(file: string) {
|
|
166
175
|
try {
|
|
167
|
-
return TOML.parse(await readFile(join(
|
|
176
|
+
return TOML.parse(await readFile(join(_root, file), 'utf-8'))
|
|
168
177
|
} catch (e) {
|
|
169
178
|
warn(`Could not parse ${file}, using defaults`)
|
|
170
179
|
return {}
|
|
@@ -183,7 +192,7 @@ export async function createMiniflare(options = {}, configPath = 'wrangler.toml'
|
|
|
183
192
|
liveReload: options.liveReload !== false,
|
|
184
193
|
updateCheck: false,
|
|
185
194
|
|
|
186
|
-
scriptPath: join(
|
|
195
|
+
scriptPath: join(_root, 'dist/index.js'),
|
|
187
196
|
compatibilityDate: config.compatibility_date || '2024-11-01',
|
|
188
197
|
compatibilityFlags: config.compatibility_flags || [
|
|
189
198
|
'nodejs_compat',
|
|
@@ -205,11 +214,11 @@ export async function createMiniflare(options = {}, configPath = 'wrangler.toml'
|
|
|
205
214
|
// { type: 'ESModule', include: ['**/*.js', '**/*.ts'] },
|
|
206
215
|
// ],
|
|
207
216
|
|
|
208
|
-
kvPersist: join(
|
|
209
|
-
cachePersist: join(
|
|
210
|
-
d1Persist: join(
|
|
211
|
-
r2Persist: join(
|
|
212
|
-
durablesPersist: join(
|
|
217
|
+
kvPersist: join(_root, '.wrangler/state/v3/kv'),
|
|
218
|
+
cachePersist: join(_root, '.wrangler/state/v3/cache'),
|
|
219
|
+
d1Persist: join(_root, '.wrangler/state/v3/d1'),
|
|
220
|
+
r2Persist: join(_root, '.wrangler/state/v3/r2'),
|
|
221
|
+
durablesPersist: join(_root, '.wrangler/state/v3/durable_objects'),
|
|
213
222
|
|
|
214
223
|
verbose: false,
|
|
215
224
|
|
|
@@ -224,7 +233,7 @@ export async function createMiniflare(options = {}, configPath = 'wrangler.toml'
|
|
|
224
233
|
upstream: config.upstream || 'https://example.com',
|
|
225
234
|
|
|
226
235
|
sitePath: config.site?.bucket ?
|
|
227
|
-
join(
|
|
236
|
+
join(_root, config.site.bucket) : undefined,
|
|
228
237
|
siteInclude: config.site?.include || ['**/*'],
|
|
229
238
|
siteExclude: config.site?.exclude || [],
|
|
230
239
|
|
|
@@ -247,7 +256,7 @@ function getAssetChangeMessage(
|
|
|
247
256
|
e: ChokidarEventName,
|
|
248
257
|
path: string
|
|
249
258
|
): string {
|
|
250
|
-
path = relative(
|
|
259
|
+
path = relative(_root, path)
|
|
251
260
|
switch (e) {
|
|
252
261
|
case 'add':
|
|
253
262
|
return `File ${path} was added`
|
|
@@ -265,11 +274,11 @@ function getAssetChangeMessage(
|
|
|
265
274
|
|
|
266
275
|
export async function watch(cb: (e: ChokidarEventName | string, file: string) => Promise<void>) {
|
|
267
276
|
const codeWatcher = chokidar.watch([
|
|
268
|
-
join(
|
|
269
|
-
join(
|
|
270
|
-
join(
|
|
271
|
-
join(
|
|
272
|
-
join(
|
|
277
|
+
join(_root, '{actions,features,routes,configs,enums,locales,middlewares,models,utils}/**/*.ts'),
|
|
278
|
+
join(_root, '.env.dev'),
|
|
279
|
+
join(_root, '.env.prod'),
|
|
280
|
+
join(_root, 'package.json'),
|
|
281
|
+
join(_root, 'wrangler.toml'),
|
|
273
282
|
], {
|
|
274
283
|
ignored: /(^|[/\\])\../, // ignore hidden files
|
|
275
284
|
persistent: true,
|
package/src/cli/index.ts
CHANGED
|
@@ -30,8 +30,8 @@ const version = [name, colors.isColorSupported ? colors.gray('v'+rajtVersion) :
|
|
|
30
30
|
|
|
31
31
|
if (directly()) {
|
|
32
32
|
const _args = process.argv.slice(2)
|
|
33
|
-
const
|
|
34
|
-
if (!
|
|
33
|
+
const length = _args.length
|
|
34
|
+
if (!length || (length == 1 && ['-v', '--version', '--v', '-version'].includes(_args[0]))) {
|
|
35
35
|
console.log(version)
|
|
36
36
|
process.exit(0)
|
|
37
37
|
}
|
|
@@ -50,7 +50,7 @@ if (directly()) {
|
|
|
50
50
|
const main = defineCommand({
|
|
51
51
|
meta: {
|
|
52
52
|
name: 'rajt',
|
|
53
|
-
version:
|
|
53
|
+
version: '',
|
|
54
54
|
description: name,
|
|
55
55
|
},
|
|
56
56
|
subCommands: {
|
|
@@ -60,5 +60,5 @@ if (directly()) {
|
|
|
60
60
|
},
|
|
61
61
|
})
|
|
62
62
|
|
|
63
|
-
runMain(main, { rawArgs:
|
|
63
|
+
runMain(main, { rawArgs: length ? undefined : ['-h'], showUsage })
|
|
64
64
|
}
|
package/src/create-app.ts
CHANGED
|
@@ -10,7 +10,6 @@ import { createColors } from 'picocolors'
|
|
|
10
10
|
import { getColorEnabledAsync } from 'hono/utils/color'
|
|
11
11
|
import { Envir } from 't0n'
|
|
12
12
|
import type { Routes } from './types'
|
|
13
|
-
import { BadRequest, Unauthorized } from './exceptions'
|
|
14
13
|
import { resolve, resolveMiddleware } from './utils/resolve'
|
|
15
14
|
import { getMiddlewares, getHandler } from './register'
|
|
16
15
|
import { isDev } from './utils/environment'
|
|
@@ -36,13 +35,10 @@ const EHandler = async (e: Error | HTTPResponseError) => {
|
|
|
36
35
|
console.error(e)
|
|
37
36
|
|
|
38
37
|
switch (true) {
|
|
39
|
-
case e instanceof Unauthorized:
|
|
40
38
|
case 'status' in e && e.status == 401:
|
|
41
39
|
return response.unauthorized()
|
|
42
40
|
|
|
43
|
-
case e
|
|
44
|
-
case 'status' in e && e.status == 400:
|
|
45
|
-
// @ts-ignore
|
|
41
|
+
case 'status' in e && e.status == 400: // @ts-ignore
|
|
46
42
|
return response.badRequest(null, e?.message)
|
|
47
43
|
|
|
48
44
|
default:
|
package/src/utils/log.ts
CHANGED
|
@@ -1,20 +1,23 @@
|
|
|
1
1
|
import { blue, bold, gray, green, magenta, red, yellow, white } from 'picocolors'
|
|
2
2
|
|
|
3
3
|
const _step = (color: Function, ...msg: any[]) => {
|
|
4
|
-
if (msg?.length && msg.length < 2)
|
|
5
|
-
return console.log(color('ā') + ` ${msg[0]}\n`)
|
|
6
|
-
|
|
7
4
|
const length = msg.length
|
|
8
|
-
|
|
5
|
+
if (!length) return
|
|
6
|
+
if (length < 2)
|
|
7
|
+
return console.log(color('ā') +' '+ msg[0])
|
|
9
8
|
|
|
9
|
+
const total = length - 1
|
|
10
10
|
for (let i: number = 0; i < length; i++) {
|
|
11
11
|
switch (i) {
|
|
12
12
|
case 0:
|
|
13
|
-
|
|
13
|
+
console.log(color('ā') + ' ' + msg[i])
|
|
14
|
+
continue
|
|
14
15
|
case total:
|
|
15
|
-
|
|
16
|
+
console.log(` ${gray('ā')} ${msg[i]}\t`)
|
|
17
|
+
continue
|
|
16
18
|
default:
|
|
17
|
-
|
|
19
|
+
console.log(` ${gray('ā')} ` + msg[i])
|
|
20
|
+
continue
|
|
18
21
|
}
|
|
19
22
|
}
|
|
20
23
|
}
|
|
@@ -29,6 +32,7 @@ export const substep = (...msg: any[]) => {
|
|
|
29
32
|
}
|
|
30
33
|
|
|
31
34
|
export const ln = () => console.log('\n')
|
|
35
|
+
export const rn = () => console.log('\t')
|
|
32
36
|
|
|
33
37
|
export const logo = gray(bold('Ī»'))
|
|
34
38
|
export const prefixes = {
|
package/src/context.ts
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import { Context } from 'hono'
|
|
2
|
-
import { getCookie, getSignedCookie, setCookie, setSignedCookie, deleteCookie } from 'hono/cookie'
|
|
3
|
-
import type { CookieOptions, CookiePrefixOptions } from 'hono/utils/cookie'
|
|
4
|
-
|
|
5
|
-
const cookieWrapper = (c: Context) => ({
|
|
6
|
-
all: () => getCookie(c),
|
|
7
|
-
allSigned: (secret: string) => getSignedCookie(c, secret),
|
|
8
|
-
get: (name: string, prefixOptions?: CookiePrefixOptions) => prefixOptions ? getCookie(c, name, prefixOptions) : getCookie(c, name),
|
|
9
|
-
getSigned: (secret: string, name: string, prefixOptions?: CookiePrefixOptions) => prefixOptions ? getSignedCookie(c, secret, name, prefixOptions) : getSignedCookie(c, secret, name),
|
|
10
|
-
set: (name: string, value: string, opt?: CookieOptions) => setCookie(c, name, value, opt),
|
|
11
|
-
setSigned: (name: string, value: string, secret: string, opt?: CookieOptions) => setSignedCookie(c, name, value, secret, opt),
|
|
12
|
-
delete: (name: string, opt?: CookieOptions) => deleteCookie(c, name, opt)
|
|
13
|
-
})
|
|
14
|
-
|
|
15
|
-
export default class CX {
|
|
16
|
-
static #c: Context
|
|
17
|
-
static #cookie: ReturnType<typeof cookieWrapper>
|
|
18
|
-
|
|
19
|
-
static setContext(c: Context) {
|
|
20
|
-
this.#c = c
|
|
21
|
-
this.#cookie = cookieWrapper(c)
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
static get cx(): Context {
|
|
25
|
-
return this.#c
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
static get cookie() {
|
|
29
|
-
return this.#cookie
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
static get ip(): string | undefined {
|
|
33
|
-
return this.#c.req.header('cf-connecting-ip')
|
|
34
|
-
|| this.#c.req.header('x-forwarded-for')?.split(',')[0]?.trim()
|
|
35
|
-
|| this.#c.env?.aws?.lambda?.event?.requestContext?.identity?.sourceIp
|
|
36
|
-
|| this.#c.req.header('x-real-ip')
|
|
37
|
-
|| this.#c.env?.remoteAddr?.hostname
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
static get userAgent(): string | undefined {
|
|
41
|
-
return this.#c.req.header('user-agent')
|
|
42
|
-
}
|
|
43
|
-
}
|
package/src/esbuild.mjs
DELETED
|
@@ -1,124 +0,0 @@
|
|
|
1
|
-
import esbuild from 'esbuild'
|
|
2
|
-
import { basename, dirname, join, relative } from 'node:path'
|
|
3
|
-
import { mkdirSync, existsSync, readdirSync, rmSync, copyFileSync } from 'node:fs'
|
|
4
|
-
import { readFile, stat, writeFile } from 'node:fs/promises'
|
|
5
|
-
|
|
6
|
-
const fail = (e) => {
|
|
7
|
-
console.error('ā Build failed' + (e ? ':' : ''), e || '')
|
|
8
|
-
process.exit(1)
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
const args = process.argv.slice(2)
|
|
12
|
-
const platform = args[0] || ''
|
|
13
|
-
|
|
14
|
-
const platforms = ['aws', 'cf']
|
|
15
|
-
if (!platform || !platforms.includes(platform))
|
|
16
|
-
fail()
|
|
17
|
-
|
|
18
|
-
const __dirname = dirname(new URL(import.meta.url).pathname)
|
|
19
|
-
|
|
20
|
-
const formatSize = (bytes) => {
|
|
21
|
-
if (bytes < 1024) return `${bytes}b`
|
|
22
|
-
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(2)}kb`
|
|
23
|
-
return `${(bytes / (1024 * 1024)).toFixed(2)}mb`
|
|
24
|
-
}
|
|
25
|
-
const formatTime = (ms) => {
|
|
26
|
-
if (ms < 1000) return `${ms}ms`
|
|
27
|
-
return `${(ms / 1000).toFixed(2)}s`
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
const isCF = platform == 'cf'
|
|
31
|
-
const buildOptions = {
|
|
32
|
-
entryPoints: [join(__dirname, `prod-${platform}.ts`)],
|
|
33
|
-
bundle: true,
|
|
34
|
-
minify: true,
|
|
35
|
-
outfile: join(__dirname, '../../../dist/index.js'),
|
|
36
|
-
platform: isCF ? 'browser' : 'node',
|
|
37
|
-
target: isCF ? 'es2022' : 'node20',
|
|
38
|
-
conditions: isCF ? ['worker', 'browser'] : [],
|
|
39
|
-
format: 'esm',
|
|
40
|
-
treeShaking: true,
|
|
41
|
-
legalComments: 'none',
|
|
42
|
-
external: [
|
|
43
|
-
'@aws-sdk', '@smithy',
|
|
44
|
-
...(isCF ? [
|
|
45
|
-
'cloudflare:workers',
|
|
46
|
-
'node:crypto', 'crypto',
|
|
47
|
-
'node:buffer', 'buffer',
|
|
48
|
-
] : []),
|
|
49
|
-
],
|
|
50
|
-
metafile: true,
|
|
51
|
-
write: false,
|
|
52
|
-
plugins: [
|
|
53
|
-
{
|
|
54
|
-
name: 'preserve-class-names',
|
|
55
|
-
setup(build) {
|
|
56
|
-
build.onLoad(
|
|
57
|
-
{ filter: /(actions|features|routes)\/.*\.ts$/ },
|
|
58
|
-
async (args) => {
|
|
59
|
-
const contents = await readFile(args.path, 'utf8')
|
|
60
|
-
const result = await esbuild.transform(contents, {
|
|
61
|
-
loader: 'ts',
|
|
62
|
-
minify: true,
|
|
63
|
-
keepNames: true
|
|
64
|
-
})
|
|
65
|
-
return { contents: result.code, loader: 'ts' }
|
|
66
|
-
}
|
|
67
|
-
)
|
|
68
|
-
},
|
|
69
|
-
},
|
|
70
|
-
{
|
|
71
|
-
name: 'remove-use-strict',
|
|
72
|
-
setup(build) {
|
|
73
|
-
build.onEnd(async (result) => {
|
|
74
|
-
if (!result.outputFiles) return
|
|
75
|
-
|
|
76
|
-
const files = result.outputFiles.filter(file => file.path.endsWith('.js'))
|
|
77
|
-
await Promise.all(files.map(async file => {
|
|
78
|
-
if (!file.path.endsWith('.js')) return
|
|
79
|
-
|
|
80
|
-
await writeFile(
|
|
81
|
-
file.path,
|
|
82
|
-
new TextDecoder()
|
|
83
|
-
.decode(file.contents)
|
|
84
|
-
.replace(/(["'`])\s*use strict\s*\1;?|`use strict`;?/g, '')
|
|
85
|
-
)
|
|
86
|
-
}))
|
|
87
|
-
})
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
]
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
const startTime = Date.now()
|
|
94
|
-
const cwd = join(__dirname, '../../..')
|
|
95
|
-
|
|
96
|
-
const distDir = join(cwd, 'dist')
|
|
97
|
-
existsSync(distDir)
|
|
98
|
-
? readdirSync(distDir).forEach(file => rmSync(join(distDir, file), { recursive: true, force: true }))
|
|
99
|
-
: mkdirSync(distDir, { recursive: true })
|
|
100
|
-
|
|
101
|
-
for (const file of await readdirSync(distDir))
|
|
102
|
-
await rmSync(join(distDir, file))
|
|
103
|
-
|
|
104
|
-
if (isCF) {
|
|
105
|
-
for (let file of [
|
|
106
|
-
'wrangler.toml',
|
|
107
|
-
]) {
|
|
108
|
-
file = join(cwd, file)
|
|
109
|
-
if (existsSync(file))
|
|
110
|
-
copyFileSync(file, join(cwd, 'dist', basename(file)))
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
esbuild.build(buildOptions)
|
|
115
|
-
.then(async result => {
|
|
116
|
-
const outputFile = buildOptions.outfile
|
|
117
|
-
const stats = await stat(outputFile)
|
|
118
|
-
const size = formatSize(stats.size)
|
|
119
|
-
|
|
120
|
-
console.log(`\nā”ļø Done in ${formatTime(Date.now() - startTime)}`)
|
|
121
|
-
console.log(` ${relative(join(cwd, 'node_modules/rajt/src'), buildOptions.entryPoints[0])} ā ${relative(cwd, outputFile)}`)
|
|
122
|
-
console.log(` Size: ${size}`)
|
|
123
|
-
console.log(` Files: ${Object.keys(result.metafile.outputs).length}`)
|
|
124
|
-
}).catch(fail)
|
package/src/exceptions.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
export class Unauthorized extends Error {
|
|
2
|
-
status = 401
|
|
3
|
-
constructor(message = 'Unauthorized') {
|
|
4
|
-
super(message)
|
|
5
|
-
this.name = 'UnauthorizedError'
|
|
6
|
-
}
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
export class BadRequest extends Error {
|
|
10
|
-
status = 400
|
|
11
|
-
constructor(message = 'Bad Request') {
|
|
12
|
-
super(message)
|
|
13
|
-
this.name = 'BadRequestError'
|
|
14
|
-
}
|
|
15
|
-
}
|