Skip to content

Commit

Permalink
🎉 feat: extension checksum
Browse files Browse the repository at this point in the history
  • Loading branch information
SaltyAom committed Dec 10, 2023
1 parent 3d925c7 commit 5c3d385
Show file tree
Hide file tree
Showing 5 changed files with 337 additions and 29 deletions.
4 changes: 1 addition & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
<<<<<<< HEAD
# 0.8.0
Feature:
- `header` initialization function
Expand Down Expand Up @@ -27,7 +26,7 @@ Change:
Bug fix:
- remove `headers`, `path` from `PreContext`
- remove `derive` from `PreContext`
=======

# 0.7.31 - 9 Dec 2023
Improvement:
- [#345](https://github.com/elysiajs/elysia/pull/345) add font to `SchemaOptions`
Expand All @@ -38,7 +37,6 @@ Bug fix:
- [#330](https://github.com/elysiajs/elysia/pull/330) preserve query params for mounted handler
- [#332](https://github.com/elysiajs/elysia/pull/332) reexport TSchema from typebox
- [#319](https://github.com/elysiajs/elysia/pull/319) TypeBox Ref error when using Elysia.group()
>>>>>>> main

# 0.7.30 - 5 Dec 2023
Bug fix:
Expand Down
38 changes: 30 additions & 8 deletions example/a.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,43 @@ import { Elysia, t } from '../src'

const a = new Elysia({ name: 'a', seed: 'awdawd' }).extends(
({ onBeforeHandle }) => ({
a(fn: () => void) {
onBeforeHandle(fn)
a(fn: string) {
onBeforeHandle(() => {
console.log(fn)
})
}
})
)
const b = new Elysia({ name: 'b', seed: 'add' }).use(a).decorate('b', 'b')

// const app = new Elysia()
// .use(a)
// .use(b)
// .get('/', () => 'Hello World', {
// a: 'a'
// })
// .listen(3000)

const orders = []

const app = new Elysia()
.use(a)
.use(b)
.extends(({ onBeforeHandle }) => ({
hi(fn: () => any) {
onBeforeHandle(fn)
}
}))
.onBeforeHandle(() => {
orders.push(1)
})
.get('/', () => 'Hello World', {
a() {
console.log('a')
beforeHandle() {
orders.push(2)
},
hi: () => {
orders.push(3)
}
})
.listen(3000)

console.dir({ main: app.dependencies }, { depth: 10 })
console.log(app.routes[0])
app.handle(new Request('http://localhost/'))
// console.dir({ main: app.dependencies }, { depth: 10 })
40 changes: 31 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ import {
filterGlobalHook,
asGlobal,
traceBackExtension,
replaceUrlPath
replaceUrlPath,
primitiveHooks
} from './utils'

import {
Expand Down Expand Up @@ -340,6 +341,8 @@ export default class Elysia<
) => {
if (typeof type === 'function' || Array.isArray(type)) {
if (!localHook[stackName]) localHook[stackName] = []
if (typeof localHook[stackName] === 'function')
localHook[stackName] = [localHook[stackName]]

if (Array.isArray(type))
localHook[stackName] = (
Expand All @@ -350,7 +353,7 @@ export default class Elysia<
return
}

const { insert = 'before', stack = 'local' } = type
const { insert = 'after', stack = 'local' } = type

if (stack === 'global') {
if (!Array.isArray(fn)) {
Expand All @@ -376,6 +379,8 @@ export default class Elysia<
return
} else {
if (!localHook[stackName]) localHook[stackName] = []
if (typeof localHook[stackName] === 'function')
localHook[stackName] = [localHook[stackName]]

if (!Array.isArray(fn)) {
if (insert === 'before') {
Expand Down Expand Up @@ -411,14 +416,31 @@ export default class Elysia<
onError: createManager('error')
}

for (const extension of this.extensions)
traceBackExtension(
extension(manager),
localHook as any,
checksum(
this.config.name + JSON.stringify(this.config.seed)
)
for (const extension of this.extensions) {
const customHookValues: Record<string, unknown> = {}
for (const [key, value] of Object.entries(localHook)) {
if (primitiveHooks.includes(key as any)) continue

customHookValues[key] = value
}

// @ts-ignore
if (!extension.$elysiaChecksum)
// @ts-ignore
extension.$elysiaChecksum = []

const hash = checksum(JSON.stringify(customHookValues))

// @ts-ignore
if (extension.$elysiaChecksum.includes(hash)) continue

// @ts-ignore
extension.$elysiaChecksum.push(
checksum(JSON.stringify(customHookValues))
)

traceBackExtension(extension(manager), localHook as any)
}
}

const hooks = mergeHook(globalHook, localHook)
Expand Down
11 changes: 2 additions & 9 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -562,21 +562,14 @@ export const unsignCookie = async (input: string, secret: string | null) => {
export const traceBackExtension = (
extension: BaseExtension,
property: Record<string, unknown>,
checksum: number | undefined,
hooks = property
) => {
for (const [key, value] of Object.entries(property)) {
if (primitiveHooks.includes(key as any) || !(key in extension)) continue

if (typeof extension[key] === 'function') {
// Property is reference to the original object
// @ts-ignore
if (!property[key]?.$elysiaChecksum) {
// @ts-ignore
property[key].$elysiaChecksum = checksum
extension[key](value)
}
extension[key](value)
} else if (typeof extension[key] === 'object')
traceBackExtension(extension[key], value as any, checksum, hooks)
traceBackExtension(extension[key], value as any, hooks)
}
}
Loading

0 comments on commit 5c3d385

Please sign in to comment.