rajt 0.0.37 → 0.0.39

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.37",
4
+ "version": "0.0.39",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
7
7
  "exports": {
@@ -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/auth/index.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  export { Ability } from './ability'
2
- export { Auth } from './auth'
2
+ export { Auth, Auth as Gate } from './auth'
3
3
  export { Authnz } from './authnz'
4
4
  export { Token } from './token'
5
5
 
package/src/auth/token.ts CHANGED
@@ -77,7 +77,7 @@ export class Token {
77
77
  try {
78
78
  const parsedUrl = new URL(formattedUrl)
79
79
  return parsedUrl.host
80
- } catch (e) {
80
+ } catch {
81
81
  return ''
82
82
  }
83
83
  }
package/src/context.ts CHANGED
@@ -28,4 +28,16 @@ export default class CX {
28
28
  static get cookie() {
29
29
  return this.#cookie
30
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
+ }
31
43
  }
package/src/dev.ts CHANGED
@@ -25,7 +25,7 @@ const desiredPort = process.env?.PORT ? Number(process.env.PORT) : 3000
25
25
  getAvailablePort(desiredPort)
26
26
  .then(port => {
27
27
  if (port != desiredPort)
28
- console.warn(`🟨 Port ${desiredPort} was in use, using ${port} as a fallback`)
28
+ console.warn(`🟠 Port ${desiredPort} was in use, using ${port} as a fallback`)
29
29
 
30
30
  console.log(`šŸš€ API running on http://localhost:${port}`)
31
31
  serve({ fetch, port })
package/src/esbuild.mjs CHANGED
@@ -1,9 +1,9 @@
1
1
  import esbuild from 'esbuild'
2
- import path from 'path'
2
+ import { dirname, join, relative } from 'path'
3
3
  import { fileURLToPath } from 'url'
4
- import fs from 'fs/promises'
4
+ import { readFile, stat, writeFile } from 'fs/promises'
5
5
 
6
- const __dirname = path.dirname(fileURLToPath(import.meta.url))
6
+ const __dirname = dirname(fileURLToPath(import.meta.url))
7
7
 
8
8
  const formatSize = (bytes) => {
9
9
  if (bytes < 1024) return `${bytes}b`
@@ -16,10 +16,10 @@ const formatTime = (ms) => {
16
16
  }
17
17
 
18
18
  const buildOptions = {
19
- entryPoints: [path.join(__dirname, 'prod.ts')],
19
+ entryPoints: [join(__dirname, 'prod.ts')],
20
20
  bundle: true,
21
21
  minify: true,
22
- outfile: path.join(__dirname, '../../../dist/index.js'),
22
+ outfile: join(__dirname, '../../../dist/index.js'),
23
23
  platform: 'node',
24
24
  target: 'node20',
25
25
  format: 'esm',
@@ -27,37 +27,60 @@ const buildOptions = {
27
27
  legalComments: 'none',
28
28
  external: ['@aws-sdk', '@smithy'],
29
29
  metafile: true,
30
- plugins: [{
31
- name: 'preserve-class-names',
32
- setup(build) {
33
- build.onLoad(
34
- { filter: /(actions|features)\/.*\.ts$/ },
35
- async (args) => {
36
- const contents = await fs.readFile(args.path, 'utf8')
37
- const result = await esbuild.transform(contents, {
38
- loader: 'ts',
39
- minify: true,
40
- keepNames: true
41
- })
42
- return { contents: result.code, loader: 'ts' }
43
- }
44
- )
30
+ write: false,
31
+ plugins: [
32
+ {
33
+ name: 'preserve-class-names',
34
+ setup(build) {
35
+ build.onLoad(
36
+ { filter: /(actions|features)\/.*\.ts$/ },
37
+ async (args) => {
38
+ const contents = await readFile(args.path, 'utf8')
39
+ const result = await esbuild.transform(contents, {
40
+ loader: 'ts',
41
+ minify: true,
42
+ keepNames: true
43
+ })
44
+ return { contents: result.code, loader: 'ts' }
45
+ }
46
+ )
47
+ },
45
48
  },
46
- }]
49
+ {
50
+ name: 'remove-use-strict',
51
+ setup(build) {
52
+ build.onEnd(async (result) => {
53
+ if (!result.outputFiles) return
54
+
55
+ const files = result.outputFiles.filter(file => file.path.endsWith('.js'))
56
+ await Promise.all(files.map(async file => {
57
+ if (!file.path.endsWith('.js')) return
58
+
59
+ await writeFile(
60
+ file.path,
61
+ new TextDecoder()
62
+ .decode(file.contents)
63
+ .replace(/(["'`])\s*use strict\s*\1;?|`use strict`;?/g, '')
64
+ )
65
+ }))
66
+ })
67
+ }
68
+ }
69
+ ]
47
70
  }
48
71
 
49
72
  try {
50
73
  const startTime = Date.now()
51
74
  const result = await esbuild.build(buildOptions)
52
75
 
53
- const cwd = path.join(__dirname, '../../..')
76
+ const cwd = join(__dirname, '../../..')
54
77
 
55
78
  const outputFile = buildOptions.outfile
56
- const stats = await fs.stat(outputFile)
79
+ const stats = await stat(outputFile)
57
80
  const size = formatSize(stats.size)
58
81
 
59
82
  console.log(`\nāš”ļø Done in ${formatTime(Date.now() - startTime)}`)
60
- console.log(` ${path.relative(path.join(cwd, 'node_modules/rajt/src'), buildOptions.entryPoints[0])} → ${path.relative(cwd, outputFile)}`)
83
+ console.log(` ${relative(join(cwd, 'node_modules/rajt/src'), buildOptions.entryPoints[0])} → ${relative(cwd, outputFile)}`)
61
84
  console.log(` Size: ${size}`)
62
85
  console.log(` Files: ${Object.keys(result.metafile.outputs).length}`)
63
86
  } catch (error) {
package/src/register.ts CHANGED
@@ -2,7 +2,7 @@ export const handlers: Record<string, Function> = {}
2
2
 
3
3
  export function registerHandler(id: string, handler: any) {
4
4
  if (id in handlers)
5
- console.warn(`Handler "${id}" has already been registered`)
5
+ console.warn(`🟠 Handler "${id}" has already been registered`)
6
6
 
7
7
  handlers[id] = handler
8
8
  }
package/src/request.ts CHANGED
@@ -87,4 +87,12 @@ export default class Request {
87
87
  static get cookie() {
88
88
  return c.cookie
89
89
  }
90
+
91
+ static get ip() {
92
+ return c.ip
93
+ }
94
+
95
+ static get userAgent() {
96
+ return c.userAgent
97
+ }
90
98
  }