prostgles-server 4.2.269 → 4.2.271

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