@ditojs/server 2.0.4 → 2.0.5
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,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ditojs/server",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.5",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Dito.js Server – Dito.js is a declarative and modern web framework, based on Objection.js, Koa.js and Vue.js",
|
|
6
6
|
"repository": "https://github.com/ditojs/dito/tree/master/packages/server",
|
|
@@ -22,9 +22,9 @@
|
|
|
22
22
|
"node >= 18"
|
|
23
23
|
],
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@ditojs/admin": "^2.0.
|
|
25
|
+
"@ditojs/admin": "^2.0.5",
|
|
26
26
|
"@ditojs/build": "^2.0.0",
|
|
27
|
-
"@ditojs/router": "^2.0.
|
|
27
|
+
"@ditojs/router": "^2.0.5",
|
|
28
28
|
"@ditojs/utils": "^2.0.1",
|
|
29
29
|
"@koa/cors": "^4.0.0",
|
|
30
30
|
"@koa/multer": "^3.0.2",
|
|
@@ -90,7 +90,7 @@
|
|
|
90
90
|
"typescript": "^5.0.3"
|
|
91
91
|
},
|
|
92
92
|
"types": "types",
|
|
93
|
-
"gitHead": "
|
|
93
|
+
"gitHead": "ee7c025d76cf6a59825a407455d3b990e0f016ed",
|
|
94
94
|
"scripts": {
|
|
95
95
|
"types": "tsc --noEmit ./src/index.d.ts"
|
|
96
96
|
},
|
package/src/app/Application.js
CHANGED
|
@@ -13,7 +13,6 @@ import compress from 'koa-compress'
|
|
|
13
13
|
import conditional from 'koa-conditional-get'
|
|
14
14
|
import mount from 'koa-mount'
|
|
15
15
|
import passport from 'koa-passport'
|
|
16
|
-
import session from 'koa-session'
|
|
17
16
|
import etag from 'koa-etag'
|
|
18
17
|
import helmet from 'koa-helmet'
|
|
19
18
|
import responseTime from 'koa-response-time'
|
|
@@ -24,7 +23,6 @@ import {
|
|
|
24
23
|
hyphenate, clone, merge, parseDataPath, normalizeDataPath,
|
|
25
24
|
toPromiseCallback, mapConcurrently
|
|
26
25
|
} from '@ditojs/utils'
|
|
27
|
-
import SessionStore from './SessionStore.js'
|
|
28
26
|
import { Validator } from './Validator.js'
|
|
29
27
|
import { EventEmitter } from '../lib/index.js'
|
|
30
28
|
import { Controller, AdminController } from '../controllers/index.js'
|
|
@@ -45,6 +43,7 @@ import {
|
|
|
45
43
|
findRoute,
|
|
46
44
|
handleError,
|
|
47
45
|
handleRoute,
|
|
46
|
+
handleSession,
|
|
48
47
|
handleUser,
|
|
49
48
|
logRequests
|
|
50
49
|
} from '../middleware/index.js'
|
|
@@ -564,37 +563,7 @@ export class Application extends Koa {
|
|
|
564
563
|
this.use(createTransaction())
|
|
565
564
|
// 5. session
|
|
566
565
|
if (app.session) {
|
|
567
|
-
|
|
568
|
-
modelClass,
|
|
569
|
-
autoCommit = true,
|
|
570
|
-
...options
|
|
571
|
-
} = getOptions(app.session)
|
|
572
|
-
if (modelClass) {
|
|
573
|
-
// Create a ContextStore that resolved the specified model class,
|
|
574
|
-
// uses it to persist and retrieve the session, and automatically
|
|
575
|
-
// binds all db operations to `ctx.transaction`, if it is set.
|
|
576
|
-
// eslint-disable-next-line new-cap
|
|
577
|
-
options.ContextStore = SessionStore(modelClass)
|
|
578
|
-
}
|
|
579
|
-
options.autoCommit = false
|
|
580
|
-
this.use(session(options, this))
|
|
581
|
-
this.use(async (ctx, next) => {
|
|
582
|
-
const { transacted } = ctx.route
|
|
583
|
-
try {
|
|
584
|
-
await next()
|
|
585
|
-
if (autoCommit && transacted) {
|
|
586
|
-
// When transacted, only commit when there are no errors. Otherwise,
|
|
587
|
-
// the commit will fail and the original error will be lost.
|
|
588
|
-
await ctx.session.commit()
|
|
589
|
-
}
|
|
590
|
-
} finally {
|
|
591
|
-
// When not transacted, keep the original behavior of always
|
|
592
|
-
// committing.
|
|
593
|
-
if (autoCommit && !transacted) {
|
|
594
|
-
await ctx.session.commit()
|
|
595
|
-
}
|
|
596
|
-
}
|
|
597
|
-
})
|
|
566
|
+
this.use(handleSession(this, getOptions(app.session)))
|
|
598
567
|
}
|
|
599
568
|
// 6. passport
|
|
600
569
|
if (app.passport) {
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import session from 'koa-session'
|
|
2
|
+
import compose from 'koa-compose'
|
|
3
|
+
import { isString } from '@ditojs/utils'
|
|
4
|
+
|
|
5
|
+
export function handleSession(app, {
|
|
6
|
+
modelClass,
|
|
7
|
+
autoCommit = true,
|
|
8
|
+
...options
|
|
9
|
+
} = {}) {
|
|
10
|
+
if (modelClass) {
|
|
11
|
+
// Create a ContextStore that resolved the specified model class,
|
|
12
|
+
// uses it to persist and retrieve the session, and automatically
|
|
13
|
+
// binds all db operations to `ctx.transaction`, if it is set.
|
|
14
|
+
// eslint-disable-next-line new-cap
|
|
15
|
+
options.ContextStore = SessionStore(modelClass)
|
|
16
|
+
}
|
|
17
|
+
options.autoCommit = false
|
|
18
|
+
return compose([
|
|
19
|
+
session(options, app),
|
|
20
|
+
async (ctx, next) => {
|
|
21
|
+
// Get hold of `session` now, since it may not be available from the
|
|
22
|
+
// `ctx` if the session is destroyed.
|
|
23
|
+
const { session, route: { transacted } } = ctx
|
|
24
|
+
try {
|
|
25
|
+
await next()
|
|
26
|
+
if (autoCommit && transacted) {
|
|
27
|
+
// When transacted, only commit when there are no errors. Otherwise,
|
|
28
|
+
// the commit will fail and the original error will be lost.
|
|
29
|
+
await session.commit()
|
|
30
|
+
}
|
|
31
|
+
} finally {
|
|
32
|
+
// When not transacted, keep the original behavior of always
|
|
33
|
+
// committing.
|
|
34
|
+
if (autoCommit && !transacted) {
|
|
35
|
+
await session.commit()
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
])
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const SessionStore = modelClass => class SessionStore {
|
|
43
|
+
constructor(ctx) {
|
|
44
|
+
this.ctx = ctx
|
|
45
|
+
this.modelClass = isString(modelClass)
|
|
46
|
+
? ctx.app.models[modelClass]
|
|
47
|
+
: modelClass
|
|
48
|
+
if (!this.modelClass) {
|
|
49
|
+
throw new Error(`Unable to find model class: '${modelClass}'`)
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
query() {
|
|
54
|
+
return this.modelClass.query(this.ctx.transaction)
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
async get(id) {
|
|
58
|
+
const session = await this.query().findById(id)
|
|
59
|
+
return session?.value || {}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
async set(id, value) {
|
|
63
|
+
await this.query()
|
|
64
|
+
.findById(id)
|
|
65
|
+
.upsert({
|
|
66
|
+
...this.modelClass.getReference(id),
|
|
67
|
+
value
|
|
68
|
+
})
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
async destroy(key) {
|
|
72
|
+
return this.query().deleteById(key)
|
|
73
|
+
}
|
|
74
|
+
}
|
package/src/middleware/index.js
CHANGED
package/src/app/SessionStore.js
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
export default modelName => class SessionStore {
|
|
2
|
-
constructor(ctx) {
|
|
3
|
-
this.ctx = ctx
|
|
4
|
-
this.modelClass = ctx.app.models[modelName]
|
|
5
|
-
if (!this.modelClass) {
|
|
6
|
-
throw new Error(`Unable to find model class: '${modelName}'`)
|
|
7
|
-
}
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
query() {
|
|
11
|
-
return this.modelClass.query(this.ctx.transaction)
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
async get(id) {
|
|
15
|
-
const session = await this.query().findById(id)
|
|
16
|
-
return session?.value || {}
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
async set(id, value) {
|
|
20
|
-
await this.query()
|
|
21
|
-
.findById(id)
|
|
22
|
-
.upsert({
|
|
23
|
-
...this.modelClass.getReference(id),
|
|
24
|
-
value
|
|
25
|
-
})
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
async destroy(key) {
|
|
29
|
-
return this.query().deleteById(key)
|
|
30
|
-
}
|
|
31
|
-
}
|