prostgles-server 4.2.270 → 4.2.272
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/Auth/AuthHandler.d.ts +15 -8
- package/dist/Auth/AuthHandler.d.ts.map +1 -1
- package/dist/Auth/AuthHandler.js +34 -58
- package/dist/Auth/AuthHandler.js.map +1 -1
- package/dist/Auth/AuthTypes.d.ts +12 -2
- package/dist/Auth/AuthTypes.d.ts.map +1 -1
- package/dist/Auth/AuthTypes.js.map +1 -1
- package/dist/Auth/endpoints/setCatchAllRequestHandler.d.ts.map +1 -1
- package/dist/Auth/endpoints/setCatchAllRequestHandler.js +26 -19
- package/dist/Auth/endpoints/setCatchAllRequestHandler.js.map +1 -1
- package/dist/Auth/getClientAuth.d.ts +1 -1
- package/dist/Auth/getClientAuth.d.ts.map +1 -1
- package/dist/Auth/getClientAuth.js +12 -4
- package/dist/Auth/getClientAuth.js.map +1 -1
- package/dist/Auth/login.d.ts +2 -1
- package/dist/Auth/login.d.ts.map +1 -1
- package/dist/Auth/login.js +20 -13
- package/dist/Auth/login.js.map +1 -1
- package/dist/Auth/setupAuthRoutes.d.ts.map +1 -1
- package/dist/Auth/setupAuthRoutes.js +11 -3
- package/dist/Auth/setupAuthRoutes.js.map +1 -1
- package/dist/Auth/utils/getSidAndUserFromRequest.d.ts +4 -2
- package/dist/Auth/utils/getSidAndUserFromRequest.d.ts.map +1 -1
- package/dist/Auth/utils/getSidAndUserFromRequest.js +40 -25
- package/dist/Auth/utils/getSidAndUserFromRequest.js.map +1 -1
- package/dist/Auth/utils/getUserOrError.d.ts +10 -0
- package/dist/Auth/utils/getUserOrError.d.ts.map +1 -0
- package/dist/Auth/utils/getUserOrError.js +48 -0
- package/dist/Auth/utils/getUserOrError.js.map +1 -0
- package/dist/Auth/utils/handleGetUser.d.ts +7 -0
- package/dist/Auth/utils/handleGetUser.d.ts.map +1 -0
- package/dist/Auth/utils/handleGetUser.js +47 -0
- package/dist/Auth/utils/handleGetUser.js.map +1 -0
- package/dist/DboBuilder/DboBuilderTypes.d.ts +2 -1
- package/dist/DboBuilder/DboBuilderTypes.d.ts.map +1 -1
- package/dist/Prostgles.d.ts.map +1 -1
- package/dist/Prostgles.js +6 -0
- package/dist/Prostgles.js.map +1 -1
- package/dist/PublishParser/PublishParser.d.ts +2 -2
- package/dist/PublishParser/PublishParser.d.ts.map +1 -1
- package/dist/PublishParser/PublishParser.js +11 -5
- package/dist/PublishParser/PublishParser.js.map +1 -1
- package/dist/PublishParser/getSchemaFromPublish.d.ts.map +1 -1
- package/dist/PublishParser/getSchemaFromPublish.js +70 -69
- package/dist/PublishParser/getSchemaFromPublish.js.map +1 -1
- package/dist/onSocketConnected.d.ts.map +1 -1
- package/dist/onSocketConnected.js +15 -14
- package/dist/onSocketConnected.js.map +1 -1
- package/dist/runClientRequest.d.ts.map +1 -1
- package/dist/runClientRequest.js +3 -0
- package/dist/runClientRequest.js.map +1 -1
- package/lib/Auth/AuthHandler.ts +42 -83
- package/lib/Auth/AuthTypes.ts +13 -3
- package/lib/Auth/endpoints/setCatchAllRequestHandler.ts +29 -24
- package/lib/Auth/getClientAuth.ts +28 -14
- package/lib/Auth/login.ts +24 -17
- package/lib/Auth/setupAuthRoutes.ts +12 -3
- package/lib/Auth/utils/getSidAndUserFromRequest.ts +42 -33
- package/lib/Auth/utils/getUserOrError.ts +56 -0
- package/lib/Auth/utils/handleGetUser.ts +63 -0
- package/lib/DboBuilder/DboBuilderTypes.ts +2 -1
- package/lib/Prostgles.ts +7 -0
- package/lib/PublishParser/PublishParser.ts +14 -6
- package/lib/PublishParser/getSchemaFromPublish.ts +97 -94
- package/lib/onSocketConnected.ts +15 -12
- package/lib/runClientRequest.ts +3 -0
- package/package.json +2 -2
|
@@ -1,20 +1,26 @@
|
|
|
1
1
|
import {
|
|
2
2
|
DBSchemaTable,
|
|
3
3
|
getKeys,
|
|
4
|
+
includes,
|
|
5
|
+
isEmpty,
|
|
6
|
+
isObject,
|
|
4
7
|
MethodKey,
|
|
5
8
|
pickKeys,
|
|
6
9
|
TableInfo,
|
|
7
10
|
TableSchemaErrors,
|
|
8
11
|
TableSchemaForClient,
|
|
12
|
+
type AnyObject,
|
|
9
13
|
} from "prostgles-types";
|
|
10
14
|
import { AuthClientRequest, AuthResultWithSID } from "../Auth/AuthTypes";
|
|
11
15
|
import { getErrorAsObject } from "../DboBuilder/DboBuilder";
|
|
16
|
+
import type { TableHandler } from "../DboBuilder/TableHandler/TableHandler";
|
|
12
17
|
import { TABLE_METHODS } from "../Prostgles";
|
|
13
18
|
import { type PublishObject, PublishParser } from "./PublishParser";
|
|
14
19
|
|
|
15
20
|
type Args = AuthClientRequest & {
|
|
16
21
|
userData: AuthResultWithSID | undefined;
|
|
17
22
|
};
|
|
23
|
+
const SUBSCRIBE_METHODS = ["subscribe", "subscribeOne", "sync", "unsubscribe", "unsync"] as const;
|
|
18
24
|
|
|
19
25
|
export async function getSchemaFromPublish(
|
|
20
26
|
this: PublishParser,
|
|
@@ -32,7 +38,9 @@ export async function getSchemaFromPublish(
|
|
|
32
38
|
/* Publish tables and views based on socket */
|
|
33
39
|
const clientInfo =
|
|
34
40
|
userData ?? (await this.prostgles.authHandler?.getSidAndUserFromRequest(clientReq));
|
|
35
|
-
|
|
41
|
+
if (clientInfo === "new-session-redirect") {
|
|
42
|
+
throw "new-session-redirect";
|
|
43
|
+
}
|
|
36
44
|
let _publish: PublishObject | undefined;
|
|
37
45
|
try {
|
|
38
46
|
_publish = await this.getPublishAsObject(clientReq, clientInfo);
|
|
@@ -64,115 +72,110 @@ export async function getSchemaFromPublish(
|
|
|
64
72
|
}
|
|
65
73
|
await Promise.all(
|
|
66
74
|
tableNames.map(async (tableName) => {
|
|
75
|
+
const { canSubscribe, tablesOrViews } = this.prostgles.dboBuilder;
|
|
67
76
|
if (!this.dbo[tableName]) {
|
|
68
77
|
const errMsg = [
|
|
69
78
|
`Table ${tableName} does not exist`,
|
|
70
|
-
`Expecting one of: ${JSON.stringify(
|
|
71
|
-
`DBO tables: ${JSON.stringify(Object.keys(this.dbo).filter((k) => (this.dbo[k] as any).find))}`,
|
|
79
|
+
`Expecting one of: ${JSON.stringify(tablesOrViews?.map((tov) => tov.name))}`,
|
|
72
80
|
].join("\n");
|
|
73
81
|
throw errMsg;
|
|
74
82
|
}
|
|
75
83
|
|
|
76
|
-
const
|
|
77
|
-
|
|
78
|
-
if (table_rules && Object.keys(table_rules).length) {
|
|
79
|
-
schema[tableName] = {};
|
|
80
|
-
const tableSchema = schema[tableName]!;
|
|
81
|
-
let methods: MethodKey[] = [];
|
|
82
|
-
let tableInfo: TableInfo | undefined;
|
|
83
|
-
let tableColumns: DBSchemaTable["columns"] | undefined;
|
|
84
|
-
|
|
85
|
-
if (typeof table_rules === "object") {
|
|
86
|
-
methods = getKeys(table_rules) as any;
|
|
87
|
-
}
|
|
84
|
+
const tableRules = await this.getTableRules({ clientReq, tableName }, clientInfo);
|
|
88
85
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
await Promise.all(
|
|
96
|
-
methods
|
|
97
|
-
.filter((m) => m !== ("select" as any))
|
|
98
|
-
.map(async (method) => {
|
|
99
|
-
if (method === "sync" && table_rules[method]) {
|
|
100
|
-
/* Pass sync info */
|
|
101
|
-
tableSchema[method] = table_rules[method];
|
|
102
|
-
} else if ((table_rules as any)[method]) {
|
|
103
|
-
tableSchema[method] =
|
|
104
|
-
method === "insert" ?
|
|
105
|
-
pickKeys(table_rules.insert!, ["allowedNestedInserts"])
|
|
106
|
-
: {};
|
|
86
|
+
if (!tableRules || isEmpty(tableRules)) return;
|
|
87
|
+
if (!isObject(tableRules)) {
|
|
88
|
+
throw `Invalid tableRules for table ${tableName}. Expecting an object`;
|
|
89
|
+
}
|
|
107
90
|
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
91
|
+
schema[tableName] = {};
|
|
92
|
+
const tableSchema = schema[tableName]!;
|
|
93
|
+
const methods = getKeys(tableRules).filter(
|
|
94
|
+
(m) => canSubscribe || !includes(SUBSCRIBE_METHODS, m)
|
|
95
|
+
);
|
|
96
|
+
let tableInfo: TableInfo | undefined;
|
|
97
|
+
let tableColumns: DBSchemaTable["columns"] | undefined;
|
|
98
|
+
|
|
99
|
+
await Promise.all(
|
|
100
|
+
methods
|
|
101
|
+
.filter((m) => m !== "select")
|
|
102
|
+
.map(async (method) => {
|
|
103
|
+
if (method === "sync") {
|
|
104
|
+
/* Pass sync info */
|
|
105
|
+
tableSchema[method] = tableRules[method];
|
|
106
|
+
} else if (includes(getKeys(tableRules), method) && tableRules[method]) {
|
|
107
|
+
//@ts-ignore
|
|
108
|
+
tableSchema[method] =
|
|
109
|
+
method === "insert" ?
|
|
110
|
+
pickKeys(tableRules[method]!, ["allowedNestedInserts"])
|
|
111
|
+
: ({} as AnyObject);
|
|
112
|
+
|
|
113
|
+
/* Test for issues with the common table CRUD methods () */
|
|
114
|
+
if (includes(TABLE_METHODS, method)) {
|
|
115
|
+
try {
|
|
116
|
+
const parsedTableRule = await this.getValidatedRequestRule(
|
|
117
|
+
{
|
|
118
|
+
tableName,
|
|
119
|
+
command: method,
|
|
120
|
+
clientReq,
|
|
121
|
+
},
|
|
122
|
+
clientInfo
|
|
123
|
+
);
|
|
124
|
+
if (this.prostgles.opts.testRulesOnConnect) {
|
|
125
|
+
await (this.dbo[tableName] as TableHandler)[method](
|
|
126
|
+
{},
|
|
127
|
+
{},
|
|
128
|
+
undefined,
|
|
129
|
+
parsedTableRule,
|
|
112
130
|
{
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
}
|
|
117
|
-
clientInfo
|
|
131
|
+
...clientReq,
|
|
132
|
+
isRemoteRequest: {},
|
|
133
|
+
testRule: true,
|
|
134
|
+
}
|
|
118
135
|
);
|
|
119
|
-
if (this.prostgles.opts.testRulesOnConnect) {
|
|
120
|
-
await (this.dbo[tableName] as any)[method](
|
|
121
|
-
{},
|
|
122
|
-
{},
|
|
123
|
-
{},
|
|
124
|
-
valid_table_command_rules,
|
|
125
|
-
{
|
|
126
|
-
...clientReq,
|
|
127
|
-
isRemoteRequest: true,
|
|
128
|
-
testRule: true,
|
|
129
|
-
}
|
|
130
|
-
);
|
|
131
|
-
}
|
|
132
|
-
} catch (e) {
|
|
133
|
-
console.error(`${tableName}.${method}`, e);
|
|
134
|
-
tableSchemaErrors[tableName] ??= {};
|
|
135
|
-
tableSchemaErrors[tableName]![method] = {
|
|
136
|
-
error: "Internal publish error. Check server logs",
|
|
137
|
-
};
|
|
138
|
-
|
|
139
|
-
throw {
|
|
140
|
-
...getErrorAsObject(e),
|
|
141
|
-
publish_path: `publish.${tableName}.${method}: \n -> ${e}`,
|
|
142
|
-
};
|
|
143
136
|
}
|
|
137
|
+
} catch (e) {
|
|
138
|
+
console.error(`${tableName}.${method}`, e);
|
|
139
|
+
tableSchemaErrors[tableName] ??= {};
|
|
140
|
+
tableSchemaErrors[tableName]![method] = {
|
|
141
|
+
error: "Internal publish error. Check server logs",
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
throw {
|
|
145
|
+
...getErrorAsObject(e),
|
|
146
|
+
publish_path: `publish.${tableName}.${method}: \n -> ${e}`,
|
|
147
|
+
};
|
|
144
148
|
}
|
|
149
|
+
}
|
|
145
150
|
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
tableColumns = res;
|
|
163
|
-
}
|
|
151
|
+
if (method === "getInfo" || method === "getColumns") {
|
|
152
|
+
const tableRules = await this.getValidatedRequestRule(
|
|
153
|
+
{ tableName, command: method, clientReq },
|
|
154
|
+
clientInfo
|
|
155
|
+
);
|
|
156
|
+
const res = await (this.dbo[tableName] as TableHandler)[method](
|
|
157
|
+
undefined,
|
|
158
|
+
undefined,
|
|
159
|
+
undefined,
|
|
160
|
+
tableRules,
|
|
161
|
+
{ ...clientReq, isRemoteRequest: {} }
|
|
162
|
+
);
|
|
163
|
+
if (method === "getInfo") {
|
|
164
|
+
tableInfo = res as TableInfo;
|
|
165
|
+
} else {
|
|
166
|
+
tableColumns = res as DBSchemaTable["columns"];
|
|
164
167
|
}
|
|
165
168
|
}
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
}
|
|
169
|
+
}
|
|
170
|
+
})
|
|
171
|
+
);
|
|
172
|
+
|
|
173
|
+
if (tableInfo && tableColumns) {
|
|
174
|
+
tables.push({
|
|
175
|
+
name: tableName,
|
|
176
|
+
info: tableInfo,
|
|
177
|
+
columns: tableColumns,
|
|
178
|
+
});
|
|
176
179
|
}
|
|
177
180
|
})
|
|
178
181
|
);
|
package/lib/onSocketConnected.ts
CHANGED
|
@@ -15,9 +15,18 @@ export async function onSocketConnected(this: Prostgles, socket: PRGLIOSocket) {
|
|
|
15
15
|
this.connectedSockets.push(socket);
|
|
16
16
|
|
|
17
17
|
try {
|
|
18
|
+
const getUser = async () => {
|
|
19
|
+
if (!this.authHandler) throw "authHandler missing";
|
|
20
|
+
const res = await this.authHandler.getSidAndUserFromRequest({ socket });
|
|
21
|
+
if (res === "new-session-redirect") {
|
|
22
|
+
throw "new-session-redirect";
|
|
23
|
+
}
|
|
24
|
+
return res;
|
|
25
|
+
};
|
|
26
|
+
|
|
18
27
|
await this.opts.onLog?.({
|
|
19
28
|
type: "connect",
|
|
20
|
-
sid: this.authHandler?.
|
|
29
|
+
sid: this.authHandler?.getValidatedSid({ socket }),
|
|
21
30
|
socketId: socket.id,
|
|
22
31
|
connectedSocketIds: this.connectedSockets.map((s) => s.id),
|
|
23
32
|
});
|
|
@@ -30,22 +39,20 @@ export async function onSocketConnected(this: Prostgles, socket: PRGLIOSocket) {
|
|
|
30
39
|
if (onUseOrSocketConnected) {
|
|
31
40
|
if (!authHandler) throw "authHandler missing";
|
|
32
41
|
const errorInfo = await onUseOrSocketConnected(
|
|
33
|
-
authHandler.
|
|
42
|
+
authHandler.getValidatedSid({ socket }),
|
|
34
43
|
getClientRequestIPsInfo({ socket }),
|
|
35
44
|
{ socket }
|
|
36
45
|
);
|
|
37
46
|
if (errorInfo) {
|
|
38
|
-
socket.emit(CHANNELS.CONNECTION, {
|
|
47
|
+
socket.emit(CHANNELS.CONNECTION, {
|
|
48
|
+
error: "error" in errorInfo ? errorInfo.error : "New session setup",
|
|
49
|
+
});
|
|
39
50
|
socket.disconnect();
|
|
40
51
|
return;
|
|
41
52
|
}
|
|
42
53
|
}
|
|
43
54
|
if (this.opts.onSocketConnect) {
|
|
44
55
|
try {
|
|
45
|
-
const getUser = async () => {
|
|
46
|
-
if (!this.authHandler) throw "authHandler missing";
|
|
47
|
-
return await this.authHandler.getSidAndUserFromRequest({ socket });
|
|
48
|
-
};
|
|
49
56
|
await this.opts.onSocketConnect({
|
|
50
57
|
socket,
|
|
51
58
|
dbo: dbo as DBOFullyTyped,
|
|
@@ -91,16 +98,12 @@ export async function onSocketConnected(this: Prostgles, socket: PRGLIOSocket) {
|
|
|
91
98
|
this.dboBuilder.queryStreamer.onDisconnect(socket.id);
|
|
92
99
|
void this.opts.onLog?.({
|
|
93
100
|
type: "disconnect",
|
|
94
|
-
sid: this.authHandler?.
|
|
101
|
+
sid: this.authHandler?.getValidatedSid({ socket }),
|
|
95
102
|
socketId: socket.id,
|
|
96
103
|
connectedSocketIds: this.connectedSockets.map((s) => s.id),
|
|
97
104
|
});
|
|
98
105
|
|
|
99
106
|
if (this.opts.onSocketDisconnect) {
|
|
100
|
-
const getUser = async () => {
|
|
101
|
-
if (!this.authHandler) throw "authHandler missing";
|
|
102
|
-
return await this.authHandler.getSidAndUserFromRequest({ socket });
|
|
103
|
-
};
|
|
104
107
|
void this.opts.onSocketDisconnect({ socket, dbo: dbo as DBOFullyTyped, db, getUser });
|
|
105
108
|
}
|
|
106
109
|
});
|
package/lib/runClientRequest.ts
CHANGED
|
@@ -77,6 +77,9 @@ export const runClientRequest = async function (
|
|
|
77
77
|
}
|
|
78
78
|
|
|
79
79
|
const clientInfo = await this.authHandler?.getSidAndUserFromRequest(clientReq);
|
|
80
|
+
if (clientInfo === "new-session-redirect") {
|
|
81
|
+
throw clientInfo;
|
|
82
|
+
}
|
|
80
83
|
const validRules = await this.publishParser.getValidatedRequestRule(
|
|
81
84
|
{ tableName, command, clientReq },
|
|
82
85
|
clientInfo
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "prostgles-server",
|
|
3
|
-
"version": "4.2.
|
|
3
|
+
"version": "4.2.272",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -57,7 +57,7 @@
|
|
|
57
57
|
"pg": "^8.11.5",
|
|
58
58
|
"pg-cursor": "^2.11.0",
|
|
59
59
|
"pg-promise": "^11.9.1",
|
|
60
|
-
"prostgles-types": "^4.0.
|
|
60
|
+
"prostgles-types": "^4.0.166"
|
|
61
61
|
},
|
|
62
62
|
"devDependencies": {
|
|
63
63
|
"@eslint/js": "^9.22.0",
|