rajt 0.0.36 → 0.0.38

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.36",
4
+ "version": "0.0.38",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
7
7
  "exports": {
@@ -10,7 +10,9 @@
10
10
  "./dynamodb": "./src/dynamodb/index.ts",
11
11
  "./dynamodb/types": "./src/dynamodb/types.ts",
12
12
  "./http": "./src/http.ts",
13
- "./types": "./src/types.ts"
13
+ "./types": "./src/types.ts",
14
+ "./env": "./src/utils/environment.ts",
15
+ "./length": "./src/utils/length.ts"
14
16
  },
15
17
  "files": [
16
18
  "src"
@@ -32,16 +32,16 @@ export class Ability {
32
32
  return path == '/'
33
33
  ? 'index'
34
34
  : path.normalize('NFD')
35
- .replace(/[\u0300-\u036f]/g, '')
36
- .replace(/^\/*/, '')
37
- .replace(/([a-z])([A-Z])/g, '$1-$2')
38
- .replace(/[^a-zA-Z0-9/]|[\s_.]/g, '-')
39
- .replace(/-+/g, '-')
40
- .replace(/\//g, '.')
41
- .replace(/\.-/g, '.')
42
- .replace(/^[._-]+/, '')
43
- .replace(/[._-]+$/, '')
44
- .toLowerCase()
35
+ .replace(/[\u0300-\u036f]/g, '')
36
+ .replace(/^\/*/, '')
37
+ .replace(/([a-z])([A-Z])/g, '$1-$2')
38
+ .replace(/[^a-zA-Z0-9/]|[\s\-.]/g, '_')
39
+ .replace(/_+/g, '_')
40
+ .replace(/\//g, '.')
41
+ .replace(/\._/g, '.')
42
+ .replace(/^[._-]+/, '')
43
+ .replace(/[._-]+$/, '')
44
+ .toLowerCase()
45
45
  }
46
46
 
47
47
  static get abilities() {
@@ -54,13 +54,20 @@ export class Authnz<T extends object> {
54
54
 
55
55
  #match(rule: string, ability: string): boolean {
56
56
  if (rule === ability) return true
57
- if (rule.endsWith('.*')) {
58
- const prefix = rule.slice(0, -2)
59
- return ability.startsWith(`${prefix}.`) || ability === prefix
60
- }
57
+ if (
58
+ this.#wildcardMatch(rule, ability, '_*')
59
+ || this.#wildcardMatch(rule, ability, '-*')
60
+ || this.#wildcardMatch(rule, ability, '.*')
61
+ ) return true
62
+
61
63
  return false
62
64
  }
63
65
 
66
+ #wildcardMatch(rule: string, ability: string, suffix: string) {
67
+ return rule.endsWith(suffix)
68
+ && (ability.startsWith(rule.slice(0, -2) + suffix[0]) || ability === rule.slice(0, -2))
69
+ }
70
+
64
71
  get abilities() {
65
72
  return this.#abilities
66
73
  }
package/src/create-app.ts CHANGED
@@ -9,7 +9,7 @@ import type { Routes } from './types'
9
9
  import { BadRequest, Unauthorized } from './exceptions'
10
10
  import { resolve, resolveMiddleware } from './utils/resolve'
11
11
  import { getMiddlewares, getHandler } from './register'
12
- import env from './utils/environment'
12
+ import { isDev } from './utils/environment'
13
13
  import { Auth } from './auth'
14
14
  import response from './response'
15
15
  import cx from './context'
@@ -25,7 +25,6 @@ export type ServerOptions<E extends Env = Env> = Partial<{
25
25
  init?: InitFunction<E>,
26
26
  }>
27
27
 
28
- const isDev = env() === 'dev'
29
28
  const NFHandler = () => response.notFound()
30
29
  const EHandler = async (e: Error | HTTPResponseError) => {
31
30
  console.error(e)
@@ -43,7 +42,7 @@ const EHandler = async (e: Error | HTTPResponseError) => {
43
42
  default:
44
43
  return response.internalError(
45
44
  // @ts-ignore
46
- isDev
45
+ isDev()
47
46
  ? e.stack?.split('\n').map(line =>
48
47
  line.replace(
49
48
  /at (.+ )?\(?([^)]+)\)?/g,
@@ -67,12 +66,12 @@ const EHandler = async (e: Error | HTTPResponseError) => {
67
66
 
68
67
  // return json.internalError(
69
68
  // // @ts-ignore
70
- // isDev ? e.stack?.split('\n at ').map() : undefined,
69
+ // isDev() ? e.stack?.split('\n at ').map() : undefined,
71
70
  // e.message || 'Internal Error'
72
71
  // )
73
72
  // error: e.message,
74
73
  // cause: e.cause || '???',
75
- // stack: isDev ? e.stack : undefined
74
+ // stack: isDev (? e.stack : undefined
76
75
  }
77
76
 
78
77
  export const createApp = <E extends Env>(options?: ServerOptions<E>) => {
@@ -13,3 +13,7 @@ export default function getEnvironment() {
13
13
 
14
14
  return 'prod'
15
15
  }
16
+
17
+ export const isDev = () => getEnvironment() === 'dev'
18
+ export const isProd = () => getEnvironment() === 'prod'
19
+ export const isPrd = isProd