rajt 0.0.68 → 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/create-app.ts +1 -5
- 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",
|
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/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
|
-
}
|