rajt 0.0.55 → 0.0.57

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 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.55",
4
+ "version": "0.0.57",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
7
7
  "exports": {
@@ -19,17 +19,22 @@
19
19
  ],
20
20
  "scripts": {
21
21
  "dev": "tsx watch src/dev.ts",
22
- "local": "bun run --silent build && bun run --silent sam:local",
23
- "build": "bun run --silent cache:routes && bun run --silent export && bun run --silent clean:temp",
24
- "build:watch": "chokidar \"../../{actions,configs,models,utils}/**/*.ts\" -c \"bun run --silent build\" --initial",
25
- "export": "node src/esbuild.mjs",
26
- "package": "bun run --silent build && bun run --silent sam:package",
27
- "deploy": "bun run --silent build && bun run --silent sam:package && bun run --silent sam:deploy",
28
- "update": "bun run --silent build && bun run --silent zip && bun run --silent sam:update",
22
+ "aws:build": "bun run --silent cache:routes && bun run --silent aws:export && bun run --silent clean:temp",
23
+ "cf:build": "bun run --silent cache:routes && bun run --silent cf:export && bun run --silent clean:temp",
24
+ "aws:build:watch": "chokidar \"../../{actions,configs,enums,locales,middlewares,models,utils}/**/*.ts\" -c \"bun run --silent aws:build\" --initial",
25
+ "cf:build:watch": "chokidar \"../../{actions,configs,enums,locales,middlewares,models,utils}/**/*.ts\" -c \"bun run --silent cf:build\" --initial",
26
+ "aws:export": "node src/esbuild.mjs aws",
27
+ "cf:export": "node src/esbuild.mjs cf",
28
+ "aws:local": "bun run --silent aws:build && bun run --silent sam:local",
29
+ "aws:package": "bun run --silent aws:build && bun run --silent sam:package",
30
+ "aws:deploy": "bun run --silent aws:build && bun run --silent sam:package && bun run --silent sam:deploy",
31
+ "aws:update": "bun run --silent aws:build && bun run --silent zip && bun run --silent sam:update",
29
32
  "sam:local": "sam local start-api --warm-containers LAZY --debug --template-file ../../template-dev.yaml",
30
33
  "sam:package": "sam package --template-file ../../template-prod.yaml --output-template-file ../../packaged.yaml",
31
34
  "sam:deploy": "sam deploy --template-file ../../packaged.yaml --stack-name rajt-llrt --capabilities CAPABILITY_IAM",
32
35
  "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",
36
+ "cf:local": "bun run --silent cf:build && cd ../../dist && bunx wrangler dev",
37
+ "cf:deploy": "bun run --silent cf:build && cd ../../dist && bunx wrangler deploy",
33
38
  "cache:routes": "tsx src/scripts/cache-routes.ts",
34
39
  "ensure-dirs": "rm -rf ../../dist ../../tmp && mkdir -p ../../tmp && chmod 755 ../../tmp && mkdir -p ../../dist && chmod 755 ../../dist",
35
40
  "clean": "rm -rf ../../dist ../../tmp",
@@ -45,7 +50,7 @@
45
50
  "@hono/zod-validator": "^0.4.3",
46
51
  "@types/node": "^20.11.0",
47
52
  "chokidar-cli": "^3.0.0",
48
- "cripta": "0.1.8",
53
+ "cripta": "^0.1",
49
54
  "dotenv": "^16.5.0",
50
55
  "esbuild": "^0.25.2",
51
56
  "hono": "^4.7.6",
@@ -88,4 +93,4 @@
88
93
  "bun",
89
94
  "nodejs"
90
95
  ]
91
- }
96
+ }
package/src/esbuild.mjs CHANGED
@@ -1,7 +1,20 @@
1
1
  import esbuild from 'esbuild'
2
- import { dirname, join, relative } from 'path'
3
- import { fileURLToPath } from 'url'
4
- import { readFile, stat, writeFile } from 'fs/promises'
2
+ import { basename, dirname, join, relative } from 'node:path'
3
+ import { fileURLToPath } from 'node:url'
4
+ import { mkdirSync, existsSync, readdirSync, rmSync, copyFileSync } from 'node:fs'
5
+ import { readFile, stat, writeFile } from 'node:fs/promises'
6
+
7
+ const fail = (e) => {
8
+ console.error('❌ Build failed' + (e ? ':' : ''), e || '')
9
+ process.exit(1)
10
+ }
11
+
12
+ const args = process.argv.slice(2)
13
+ const platform = args[0] || ''
14
+
15
+ const platforms = ['aws', 'cf']
16
+ if (!platform || !platforms.includes(platform))
17
+ fail()
5
18
 
6
19
  const __dirname = dirname(fileURLToPath(import.meta.url))
7
20
 
@@ -15,17 +28,26 @@ const formatTime = (ms) => {
15
28
  return `${(ms / 1000).toFixed(2)}s`
16
29
  }
17
30
 
31
+ const isCF = platform == 'cf'
18
32
  const buildOptions = {
19
- entryPoints: [join(__dirname, 'prod.ts')],
33
+ entryPoints: [join(__dirname, `prod-${platform}.ts`)],
20
34
  bundle: true,
21
35
  minify: true,
22
36
  outfile: join(__dirname, '../../../dist/index.js'),
23
- platform: 'node',
24
- target: 'node20',
37
+ platform: isCF ? 'browser' : 'node20',
38
+ target: isCF ? 'es2022' : 'node20',
39
+ conditions: isCF ? ['worker', 'browser'] : [],
25
40
  format: 'esm',
26
41
  treeShaking: true,
27
42
  legalComments: 'none',
28
- external: ['@aws-sdk', '@smithy'],
43
+ external: [
44
+ '@aws-sdk', '@smithy',
45
+ ...(isCF ? [
46
+ 'cloudflare:workers',
47
+ 'node:crypto',
48
+ 'crypto',
49
+ ] : []),
50
+ ],
29
51
  metafile: true,
30
52
  write: false,
31
53
  plugins: [
@@ -69,21 +91,33 @@ const buildOptions = {
69
91
  ]
70
92
  }
71
93
 
72
- try {
73
- const startTime = Date.now()
74
- const result = await esbuild.build(buildOptions)
94
+ const startTime = Date.now()
95
+ const cwd = join(__dirname, '../../..')
75
96
 
76
- const cwd = join(__dirname, '../../..')
97
+ const distDir = join(cwd, 'dist')
98
+ existsSync(distDir)
99
+ ? readdirSync(distDir).forEach(file => rmSync(join(distDir, file), { recursive: true, force: true }))
100
+ : mkdirSync(distDir, { recursive: true })
77
101
 
78
- const outputFile = buildOptions.outfile
79
- const stats = await stat(outputFile)
80
- const size = formatSize(stats.size)
102
+ for (const file of await readdirSync(distDir))
103
+ await rmSync(join(distDir, file))
81
104
 
82
- console.log(`\n⚡️ Done in ${formatTime(Date.now() - startTime)}`)
83
- console.log(` ${relative(join(cwd, 'node_modules/rajt/src'), buildOptions.entryPoints[0])} → ${relative(cwd, outputFile)}`)
84
- console.log(` Size: ${size}`)
85
- console.log(` Files: ${Object.keys(result.metafile.outputs).length}`)
86
- } catch (error) {
87
- console.error('❌ Build failed:', error)
88
- process.exit(1)
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)))
89
111
  }
112
+
113
+ esbuild.build(buildOptions)
114
+ .then(async result => {
115
+ const outputFile = buildOptions.outfile
116
+ const stats = await stat(outputFile)
117
+ const size = formatSize(stats.size)
118
+
119
+ console.log(`\n⚡️ Done in ${formatTime(Date.now() - startTime)}`)
120
+ console.log(` ${relative(join(cwd, 'node_modules/rajt/src'), buildOptions.entryPoints[0])} → ${relative(cwd, outputFile)}`)
121
+ console.log(` Size: ${size}`)
122
+ console.log(` Files: ${Object.keys(result.metafile.outputs).length}`)
123
+ }).catch(fail)
@@ -16,5 +16,4 @@ Ability.fromRoutes(routes)
16
16
  // @ts-ignore
17
17
  const app = createApp({ routes })
18
18
 
19
- // export default app // AWS Lambda & Cloudflare Workers
20
19
  export const handler = handle(app) // AWS Lambda (LLRT)
package/src/prod-cf.ts ADDED
@@ -0,0 +1,18 @@
1
+ import createApp from './create-app'
2
+ import { Ability } from './auth'
3
+
4
+ // @ts-ignore
5
+ await import('../../../tmp/import-routes.mjs')
6
+
7
+ // @ts-ignore
8
+ const routes = (await import('../../../tmp/routes.json')).default
9
+
10
+ // @ts-ignore
11
+ Ability.roles = (await import('../../../roles.json')).default
12
+ // @ts-ignore
13
+ Ability.fromRoutes(routes)
14
+
15
+ // @ts-ignore
16
+ const app = createApp({ routes })
17
+
18
+ export default app
@@ -23,7 +23,7 @@ async function cacheRoutes() {
23
23
  const iPath = '../../tmp/import-routes.mjs'
24
24
  ensureDir(iPath)
25
25
  writeFileSync(iPath, `// AUTO-GENERATED FILE - DO NOT EDIT
26
- ${env.map(([key, val]) => `process.env.${key} = ${JSON.stringify(val)}`).join('\n')}
26
+ ${env?.length ? `import { Envir } from '../node_modules/t0n/dist/index'\nEnvir.add({${env.map(([key, val]) => key + ':' + JSON.stringify(val)).join(',')}})` : ''}
27
27
 
28
28
  import { registerHandler, registerMiddleware } from '../node_modules/rajt/src/register'
29
29
 
@@ -13,7 +13,7 @@ export default function getLastCommitHash(path: string = '.git') {
13
13
 
14
14
  return headContent
15
15
  } catch (e) {
16
- console.error('Error reading HEAD file: ', e?.message)
16
+ // console.error('Error reading HEAD file: ', e?.message)
17
17
  return null
18
18
  }
19
19
  }