@entity-access/server-pages 1.0.43 → 1.1.2
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/dist/Page.d.ts +6 -13
- package/dist/Page.d.ts.map +1 -1
- package/dist/Page.js +10 -44
- package/dist/Page.js.map +1 -1
- package/dist/ServerPages.d.ts.map +1 -1
- package/dist/ServerPages.js +14 -2
- package/dist/ServerPages.js.map +1 -1
- package/dist/core/CacheProperty.d.ts +5 -0
- package/dist/core/CacheProperty.d.ts.map +1 -0
- package/dist/core/CacheProperty.js +19 -0
- package/dist/core/CacheProperty.js.map +1 -0
- package/dist/core/Executor.d.ts +5 -0
- package/dist/core/Executor.d.ts.map +1 -0
- package/dist/core/Executor.js +13 -0
- package/dist/core/Executor.js.map +1 -0
- package/dist/core/RouteTree.d.ts.map +1 -1
- package/dist/core/RouteTree.js +15 -6
- package/dist/core/RouteTree.js.map +1 -1
- package/dist/core/SessionUser.d.ts +7 -5
- package/dist/core/SessionUser.d.ts.map +1 -1
- package/dist/core/SessionUser.js +36 -0
- package/dist/core/SessionUser.js.map +1 -1
- package/dist/core/Wrapped.d.ts +13 -16
- package/dist/core/Wrapped.d.ts.map +1 -1
- package/dist/core/Wrapped.js +151 -235
- package/dist/core/Wrapped.js.map +1 -1
- package/dist/decorators/Prepare.d.ts +14 -0
- package/dist/decorators/Prepare.d.ts.map +1 -0
- package/dist/decorators/Prepare.js +111 -0
- package/dist/decorators/Prepare.js.map +1 -0
- package/dist/routes/api/entity/index.d.ts.map +1 -1
- package/dist/routes/api/entity/index.js +9 -3
- package/dist/routes/api/entity/index.js.map +1 -1
- package/dist/routes/api/entity/model/get.d.ts.map +1 -1
- package/dist/routes/api/entity/model/get.js.map +1 -1
- package/dist/routes/api/entity/query/get.d.ts.map +1 -1
- package/dist/routes/api/entity/query/get.js +9 -3
- package/dist/routes/api/entity/query/get.js.map +1 -1
- package/dist/services/CookieService.d.ts.map +1 -1
- package/dist/services/CookieService.js +8 -0
- package/dist/services/CookieService.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/src/Page.tsx +11 -53
- package/src/ServerPages.ts +14 -2
- package/src/core/CacheProperty.ts +18 -0
- package/src/core/Executor.ts +16 -0
- package/src/core/RouteTree.ts +7 -2
- package/src/core/SessionUser.ts +21 -5
- package/src/core/Wrapped.ts +206 -270
- package/src/decorators/Prepare.ts +132 -0
- package/src/routes/api/entity/index.tsx +4 -1
- package/src/routes/api/entity/model/get.tsx +1 -1
- package/src/routes/api/entity/query/get.ts +4 -1
- package/src/services/CookieService.ts +9 -1
- package/dist/decorators/Authorize.d.ts +0 -2
- package/dist/decorators/Authorize.d.ts.map +0 -1
- package/dist/decorators/Authorize.js +0 -3
- package/dist/decorators/Authorize.js.map +0 -1
- package/src/decorators/Authorize.ts +0 -3
package/src/ServerPages.ts
CHANGED
|
@@ -13,6 +13,10 @@ import { Wrapped } from "./core/Wrapped.js";
|
|
|
13
13
|
import { SecureContext } from "node:tls";
|
|
14
14
|
import AcmeCertificateService, { IAcmeOptions } from "./ssl/AcmeCertificateService.js";
|
|
15
15
|
import ChallengeServer from "./ssl/ChallengeServer.js";
|
|
16
|
+
import SessionUser from "./core/SessionUser.js";
|
|
17
|
+
import CookieService from "./services/CookieService.js";
|
|
18
|
+
import TokenService from "./services/TokenService.js";
|
|
19
|
+
import Executor from "./core/Executor.js";
|
|
16
20
|
|
|
17
21
|
RegisterSingleton
|
|
18
22
|
export default class ServerPages {
|
|
@@ -145,7 +149,15 @@ export default class ServerPages {
|
|
|
145
149
|
|
|
146
150
|
using scope = ServiceProvider.createScope(this);
|
|
147
151
|
let sent = false;
|
|
148
|
-
const
|
|
152
|
+
const user = scope.resolve(SessionUser);
|
|
153
|
+
user.resp = resp;
|
|
154
|
+
user.authorize = async () => {
|
|
155
|
+
const cookieService = scope.resolve(CookieService);
|
|
156
|
+
const tokenService = scope.resolve(TokenService);
|
|
157
|
+
await cookieService.createSessionUserFromCookie(tokenService.authCookieName, req.remoteIPAddress);
|
|
158
|
+
(user as any).isAuthorized = true;
|
|
159
|
+
};
|
|
160
|
+
const acceptJson = req.accepts("json");
|
|
149
161
|
|
|
150
162
|
|
|
151
163
|
try {
|
|
@@ -165,7 +177,7 @@ export default class ServerPages {
|
|
|
165
177
|
page.childPath = childPath;
|
|
166
178
|
page.request = req;
|
|
167
179
|
page.response = resp;
|
|
168
|
-
const content = await
|
|
180
|
+
const content = await Executor.run(page);
|
|
169
181
|
resp.setHeader("cache-control", page.cacheControl);
|
|
170
182
|
resp.removeHeader("etag");
|
|
171
183
|
sent = true;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export const CacheProperty = {
|
|
2
|
+
value: (target: any, name: string, value: any) => {
|
|
3
|
+
Object.defineProperty(target, name, {
|
|
4
|
+
value,
|
|
5
|
+
enumerable: true,
|
|
6
|
+
configurable: true
|
|
7
|
+
});
|
|
8
|
+
return value;
|
|
9
|
+
},
|
|
10
|
+
get: (target: any, name: string, get: Function) => {
|
|
11
|
+
Object.defineProperty(target, name, {
|
|
12
|
+
get: get as any,
|
|
13
|
+
enumerable: true,
|
|
14
|
+
configurable: true
|
|
15
|
+
});
|
|
16
|
+
return get.call(target);
|
|
17
|
+
}
|
|
18
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import Page from "../Page.js";
|
|
2
|
+
import { prepareSymbol } from "../decorators/Prepare.js";
|
|
3
|
+
|
|
4
|
+
export default class Executor {
|
|
5
|
+
|
|
6
|
+
public static async run(c: Page) {
|
|
7
|
+
let ps = c.constructor[prepareSymbol];
|
|
8
|
+
if (ps) {
|
|
9
|
+
for (const iterator of ps) {
|
|
10
|
+
await iterator();
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
return c.run();
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
}
|
package/src/core/RouteTree.ts
CHANGED
|
@@ -4,6 +4,7 @@ import Page from "../Page.js";
|
|
|
4
4
|
import { IRouteCheck, isPage } from "../Page.js";
|
|
5
5
|
import { pathToFileURL } from "url";
|
|
6
6
|
import Content, { IPageResult } from "../Content.js";
|
|
7
|
+
import { prepareSymbol } from "../decorators/Prepare.js";
|
|
7
8
|
|
|
8
9
|
export interface IRouteHandler {
|
|
9
10
|
get?: Promise<typeof Page>;
|
|
@@ -112,12 +113,16 @@ export default class RouteTree {
|
|
|
112
113
|
if (pageClass[isPage]) {
|
|
113
114
|
return pageClass as typeof Page;
|
|
114
115
|
}
|
|
115
|
-
|
|
116
|
-
|
|
116
|
+
const c = class extends Page {
|
|
117
|
+
|
|
118
|
+
[prepareSymbol] = pageClass[prepareSymbol];
|
|
119
|
+
|
|
120
|
+
async run() {
|
|
117
121
|
const r = await pageClass.call(this);
|
|
118
122
|
return Content.create(r);
|
|
119
123
|
}
|
|
120
124
|
}
|
|
125
|
+
return c;
|
|
121
126
|
})();
|
|
122
127
|
|
|
123
128
|
(this.handler ??= {})[name] = promise;
|
package/src/core/SessionUser.ts
CHANGED
|
@@ -14,27 +14,37 @@ export default class SessionUser {
|
|
|
14
14
|
/**
|
|
15
15
|
* SessionID saved in database for current session.
|
|
16
16
|
*/
|
|
17
|
-
sessionID: number
|
|
17
|
+
get sessionID(): number | null {
|
|
18
|
+
throw new Error("Please call Authorize first");
|
|
19
|
+
}
|
|
18
20
|
|
|
19
21
|
/**
|
|
20
22
|
* UserID
|
|
21
23
|
*/
|
|
22
|
-
userID
|
|
24
|
+
get userID(): number | null {
|
|
25
|
+
throw new Error("Please call Authorize first");
|
|
26
|
+
}
|
|
23
27
|
|
|
24
28
|
/**
|
|
25
29
|
* Logged in user name
|
|
26
30
|
*/
|
|
27
|
-
userName
|
|
31
|
+
get userName(): string {
|
|
32
|
+
throw new Error("Please call Authorize first");
|
|
33
|
+
}
|
|
28
34
|
|
|
29
35
|
/**
|
|
30
36
|
* Application Roles, user is associated with.
|
|
31
37
|
*/
|
|
32
|
-
roles
|
|
38
|
+
get roles(): string[] {
|
|
39
|
+
throw new Error("Please call Authorize first");
|
|
40
|
+
}
|
|
33
41
|
|
|
34
42
|
/**
|
|
35
43
|
* Expiry date, after which this session is invalid
|
|
36
44
|
*/
|
|
37
|
-
expiry
|
|
45
|
+
get expiry(): Date {
|
|
46
|
+
throw new Error("Please call Authorize first");
|
|
47
|
+
}
|
|
38
48
|
|
|
39
49
|
/**
|
|
40
50
|
* If set to true, session is no longer valid.
|
|
@@ -49,9 +59,15 @@ export default class SessionUser {
|
|
|
49
59
|
|
|
50
60
|
public resp: WrappedResponse;
|
|
51
61
|
|
|
62
|
+
private isAuthorized = false;
|
|
63
|
+
|
|
52
64
|
@Inject
|
|
53
65
|
protected tokenService: TokenService;
|
|
54
66
|
|
|
67
|
+
public async authorize() {
|
|
68
|
+
this.isAuthorized = true;
|
|
69
|
+
}
|
|
70
|
+
|
|
55
71
|
isInRole(role: roles) {
|
|
56
72
|
return this.roles?.includes(role) ?? false;
|
|
57
73
|
}
|