rajt 0.0.102 → 0.0.104

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.102",
4
+ "version": "0.0.104",
5
5
  "type": "module",
6
6
  "main": "./src/index.ts",
7
7
  "files": ["bin", "src"],
@@ -1,21 +1,41 @@
1
- import { Routes } from '../types'
1
+ import type { IRequest, Routes } from '../types'
2
2
  import { Roles, Abilities } from './types'
3
+ import { verbAlias } from '../http'
3
4
 
4
5
  export class Ability {
5
6
  static #roles: Roles = {}
6
7
  static #abilities: Abilities = []
8
+ static #actions: Record<string, string> = {}
7
9
 
8
10
  static empty() {
9
11
  this.#roles = {}
10
12
  this.#abilities = []
11
13
  }
12
14
 
13
- static fromRoutes(actions: Routes) {
14
- if (!actions?.length) return
15
+ static fromRoutes(routes: Routes) {
16
+ if (!routes?.length) return
15
17
 
16
- this.#abilities = Array.from(new Set(actions?.map(a => Array.isArray(a) ? a[3] : a.name) || []))
17
- .map(a => this.format(a))
18
- .filter(Boolean)
18
+ const actions: Record<string, string> = {}
19
+ const abilities = new Set()
20
+ for (const route of routes) {
21
+ const isArr = Array.isArray(route)
22
+ const name = isArr ? route[3] : route.name
23
+ if (!name || abilities.has(name)) continue
24
+
25
+ abilities.add(name)
26
+ actions[(isArr ? route[1] : route.path) +'$'+ verbAlias[isArr ? route[0] : route.method]] = name
27
+ const formatted = this.format(name)
28
+
29
+ if (formatted)
30
+ this.#abilities.push(formatted)
31
+ }
32
+
33
+ this.#actions = actions
34
+ }
35
+
36
+ static fromRequest(req: IRequest) {
37
+ const ability = this.#actions[`${req.routePath}$` + verbAlias[req.method.toLowerCase()]]
38
+ return ability ? this.format(ability) : ''
19
39
  }
20
40
 
21
41
  static fromAction(target: any): string {
@@ -1,16 +1,11 @@
1
1
  import { Ability } from './ability'
2
2
  import response from '../response'
3
3
  import { GET_REQUEST } from '../request'
4
- import Config from '../config'
5
- import { verbAlias } from '../http'
6
- import type {
7
- Context, Next,
8
- IRequest,
9
- } from '../types'
4
+ import type { Context, Next, IRequest } from '../types'
10
5
 
11
6
  export async function Autorized(c: Context, next: Next) {
12
7
  const req = c.get(GET_REQUEST as unknown as string) as IRequest
13
- const ability = Ability.fromAction(Config.get(`routes.${req.routePath}$`+ verbAlias[req.method.toLowerCase()]))
8
+ const ability = Ability.fromRequest(req)
14
9
 
15
10
  if (!req?.user || !ability || req.cant(ability))
16
11
  return response.unauthorized()
package/src/cli/utils.ts CHANGED
@@ -142,7 +142,7 @@ export const build = async (platform: Platform, env: string = 'prd') => {
142
142
  const opts = {
143
143
  entryPoints: [join(_rajt, `prod${platform}.ts`)],
144
144
  bundle: true,
145
- minify: true,
145
+ minify: false,
146
146
  outfile: join(_root, dist +'/index.js'),
147
147
  format: 'esm',
148
148
  target: isCF ? 'es2022' : 'node20',
package/src/config.ts CHANGED
@@ -1,77 +1,4 @@
1
1
  import { DataBag } from 't0n'
2
2
 
3
- export default class Config {
4
- static #c: DataBag
5
-
6
- static {
7
- this.#c = new DataBag()
8
- }
9
-
10
- static all<T = any>(): Record<string, T> {
11
- return this.#c.all()
12
- }
13
-
14
- static has(key: string): boolean {
15
- return this.#c.has(key)
16
- }
17
-
18
- static add<T = any>(data: Record<string, T> = {}) {
19
- this.#c.add(data)
20
- }
21
-
22
- static replace<T = any>(data: Record<string, T> = {}) {
23
- this.#c.replace(data)
24
- }
25
-
26
- static get<T = any>(key: string, defaultValue?: T): T {
27
- return this.#c.get(key, defaultValue)
28
- }
29
-
30
- static set<T = any>(key: string, value: T) {
31
- this.#c.set(key, value)
32
- }
33
-
34
- static remove(key: string) {
35
- this.#c.remove(key)
36
- }
37
-
38
- static clear() {
39
- this.#c.clear()
40
- }
41
-
42
- // @ts-ignore
43
- static get length(): number {
44
- return this.#c.length
45
- }
46
- static get size(): number {
47
- return this.#c.length
48
- }
49
-
50
- static keys(): string[] {
51
- return this.#c.keys()
52
- }
53
-
54
- static values<T = any>(): T[] {
55
- return this.#c.values()
56
- }
57
-
58
- static entries<T = any>(): [string, T][] {
59
- return this.#c.entries()
60
- }
61
-
62
- static toArray<T = any>(): [string, T][] {
63
- return this.#c.entries()
64
- }
65
-
66
- static jsonSerialize<T = any>(): Record<string, T> {
67
- return this.#c.all()
68
- }
69
-
70
- static toJson(options: number = 0): string {
71
- return this.#c.toJson(options)
72
- }
73
-
74
- static [Symbol.iterator](): IterableIterator<[string, any]> {
75
- return this.#c[Symbol.iterator]()
76
- }
77
- }
3
+ const Config = new DataBag()
4
+ export default Config
package/src/register.ts CHANGED
@@ -1,3 +1,6 @@
1
+ import { isProd } from './utils/environment'
2
+ const isPrd = isProd()
3
+
1
4
  export const handlers: Record<string, Function> = {}
2
5
 
3
6
  export function registerHandler(id: string, handler: any) {
@@ -6,7 +9,7 @@ export function registerHandler(id: string, handler: any) {
6
9
 
7
10
  export function getHandler(id: string): Function {
8
11
  const handler = handlers[id] || null
9
- if (!handler) throw new Error(`Handler ${id} not registered`)
12
+ if (isPrd && !handler) throw new Error(`Handler ${id} not registered`)
10
13
  return handler
11
14
  }
12
15
 
package/src/routes.ts CHANGED
@@ -328,7 +328,7 @@ async function dependencyPath(lib: string) {
328
328
  export async function cacheRoutes() {
329
329
  const env = Object.entries(
330
330
  config({ path: join(_root, '.env.prod') })?.parsed || {}
331
- ).filter(([key, val]) => key?.toLowerCase().startsWith('aws')) // prevent AWS credentials
331
+ ).filter(([key, val]) => !key?.toLowerCase().startsWith('aws')) // prevent AWS credentials
332
332
 
333
333
  const version = versionSHA('../../.git') // @ts-ignore
334
334
  env.push(['VERSION_SHA', process.env['VERSION_SHA'] = version]) // @ts-ignore
@@ -387,7 +387,6 @@ import { registerHandler, registerMiddleware } from '${_rajtDir}/src/register'
387
387
  ${handlers.map(([file, name, _export]) => `\nimport ${_export ? `{ ${name} }` : name} from '${_rajtDir}/src/${file}'\nregisterHandler('${name}', ${name})`).join('\n')}
388
388
 
389
389
  ${Object.entries(openApi)?.length ? `registerHandler('RAJT_OPENAPI', ${stringifyToJS(openApi)})` : ''}
390
- Config.set('routes', ${stringifyToJS(Object.fromEntries(routes.map(r => ([r.path+'$'+verbAlias[r.method], r.name]))))})
391
390
 
392
391
  ${routes.map(r => `import ${r.name} from '../${normalizeImportPath(r.file)}'`).join('\n')}
393
392
  ${middlewares.map(r => `import ${r.name} from '../${normalizeImportPath(r.file)}'`).join('\n')}
@@ -11,7 +11,7 @@ export function detectEnvironment(): symbol {
11
11
  try {
12
12
  if (
13
13
  process.env?.npm_lifecycle_event === 'dev'
14
- || process.env?.npm_lifecycle_script === 'rajt'
14
+ || process.env?.npm_lifecycle_script?.startsWith('rajt')
15
15
  || process.env?.AWS_SAM_LOCAL
16
16
  // || process?.argv?.includes('--dev')
17
17
  || process?.argv?.some(arg => ['-port', '-platform', '--dev', '--development', '--watch'].includes(arg))
@@ -6,6 +6,8 @@ export function resolve(...objs: any[]) {
6
6
  if (typeof obj === 'string')
7
7
  obj = getHandler(obj)
8
8
 
9
+ if (obj == null) continue
10
+
9
11
  if (typeof obj === 'function' && obj?.length === 2) {
10
12
 
11
13
  } else if (obj?.run) {