prostgles-server 4.2.191 → 4.2.193

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 (143) hide show
  1. package/dist/Auth/AuthHandler.d.ts +9 -14
  2. package/dist/Auth/AuthHandler.d.ts.map +1 -1
  3. package/dist/Auth/AuthHandler.js +35 -88
  4. package/dist/Auth/AuthHandler.js.map +1 -1
  5. package/dist/Auth/AuthTypes.d.ts +16 -6
  6. package/dist/Auth/AuthTypes.d.ts.map +1 -1
  7. package/dist/Auth/authProviders/setOAuthProviders.js +1 -1
  8. package/dist/Auth/authProviders/setOAuthProviders.js.map +1 -1
  9. package/dist/Auth/endpoints/getConfirmEmailRequestHandler.js +1 -1
  10. package/dist/Auth/endpoints/getConfirmEmailRequestHandler.js.map +1 -1
  11. package/dist/Auth/endpoints/getRegisterRequestHandler.js +1 -1
  12. package/dist/Auth/endpoints/getRegisterRequestHandler.js.map +1 -1
  13. package/dist/Auth/setupAuthRoutes.d.ts.map +1 -1
  14. package/dist/Auth/setupAuthRoutes.js +16 -10
  15. package/dist/Auth/setupAuthRoutes.js.map +1 -1
  16. package/dist/Auth/utils/getUserFromRequest.d.ts +7 -0
  17. package/dist/Auth/utils/getUserFromRequest.d.ts.map +1 -0
  18. package/dist/Auth/utils/getUserFromRequest.js +66 -0
  19. package/dist/Auth/utils/getUserFromRequest.js.map +1 -0
  20. package/dist/DboBuilder/DboBuilder.d.ts +1 -1
  21. package/dist/DboBuilder/DboBuilder.d.ts.map +1 -1
  22. package/dist/DboBuilder/DboBuilder.js +6 -1
  23. package/dist/DboBuilder/DboBuilder.js.map +1 -1
  24. package/dist/DboBuilder/DboBuilderTypes.d.ts +15 -7
  25. package/dist/DboBuilder/DboBuilderTypes.d.ts.map +1 -1
  26. package/dist/DboBuilder/DboBuilderTypes.js.map +1 -1
  27. package/dist/DboBuilder/QueryBuilder/getNewQuery.js +2 -2
  28. package/dist/DboBuilder/QueryBuilder/getNewQuery.js.map +1 -1
  29. package/dist/DboBuilder/QueryStreamer.js +1 -1
  30. package/dist/DboBuilder/QueryStreamer.js.map +1 -1
  31. package/dist/DboBuilder/TableHandler/TableHandler.d.ts.map +1 -1
  32. package/dist/DboBuilder/TableHandler/TableHandler.js +2 -3
  33. package/dist/DboBuilder/TableHandler/TableHandler.js.map +1 -1
  34. package/dist/DboBuilder/TableHandler/insert.js +2 -2
  35. package/dist/DboBuilder/TableHandler/update.js +1 -1
  36. package/dist/DboBuilder/TableHandler/update.js.map +1 -1
  37. package/dist/DboBuilder/ViewHandler/ViewHandler.d.ts +0 -4
  38. package/dist/DboBuilder/ViewHandler/ViewHandler.d.ts.map +1 -1
  39. package/dist/DboBuilder/ViewHandler/ViewHandler.js +37 -24
  40. package/dist/DboBuilder/ViewHandler/ViewHandler.js.map +1 -1
  41. package/dist/DboBuilder/ViewHandler/find.js +1 -1
  42. package/dist/DboBuilder/ViewHandler/find.js.map +1 -1
  43. package/dist/DboBuilder/ViewHandler/getExistsCondition.js +4 -4
  44. package/dist/DboBuilder/ViewHandler/getExistsCondition.js.map +1 -1
  45. package/dist/DboBuilder/ViewHandler/subscribe.d.ts.map +1 -1
  46. package/dist/DboBuilder/ViewHandler/subscribe.js +9 -15
  47. package/dist/DboBuilder/ViewHandler/subscribe.js.map +1 -1
  48. package/dist/DboBuilder/dboBuilderUtils.d.ts.map +1 -1
  49. package/dist/DboBuilder/dboBuilderUtils.js +3 -1
  50. package/dist/DboBuilder/dboBuilderUtils.js.map +1 -1
  51. package/dist/DboBuilder/insertNestedRecords.d.ts +4 -3
  52. package/dist/DboBuilder/insertNestedRecords.d.ts.map +1 -1
  53. package/dist/DboBuilder/insertNestedRecords.js +12 -12
  54. package/dist/DboBuilder/insertNestedRecords.js.map +1 -1
  55. package/dist/DboBuilder/runSQL.d.ts +3 -2
  56. package/dist/DboBuilder/runSQL.d.ts.map +1 -1
  57. package/dist/DboBuilder/runSQL.js +12 -15
  58. package/dist/DboBuilder/runSQL.js.map +1 -1
  59. package/dist/FileManager/initFileManager.d.ts.map +1 -1
  60. package/dist/FileManager/initFileManager.js +5 -4
  61. package/dist/FileManager/initFileManager.js.map +1 -1
  62. package/dist/Prostgles.d.ts +3 -2
  63. package/dist/Prostgles.d.ts.map +1 -1
  64. package/dist/Prostgles.js +11 -20
  65. package/dist/Prostgles.js.map +1 -1
  66. package/dist/ProstglesTypes.d.ts +2 -2
  67. package/dist/ProstglesTypes.d.ts.map +1 -1
  68. package/dist/ProstglesTypes.js +1 -6
  69. package/dist/ProstglesTypes.js.map +1 -1
  70. package/dist/PubSubManager/PubSubManager.js +2 -2
  71. package/dist/PubSubManager/PubSubManager.js.map +1 -1
  72. package/dist/PubSubManager/addSync.d.ts.map +1 -1
  73. package/dist/PubSubManager/addSync.js +1 -3
  74. package/dist/PubSubManager/addSync.js.map +1 -1
  75. package/dist/PublishParser/PublishParser.d.ts +11 -18
  76. package/dist/PublishParser/PublishParser.d.ts.map +1 -1
  77. package/dist/PublishParser/PublishParser.js +28 -27
  78. package/dist/PublishParser/PublishParser.js.map +1 -1
  79. package/dist/PublishParser/getFileTableRules.d.ts +2 -3
  80. package/dist/PublishParser/getFileTableRules.d.ts.map +1 -1
  81. package/dist/PublishParser/getFileTableRules.js +18 -20
  82. package/dist/PublishParser/getFileTableRules.js.map +1 -1
  83. package/dist/PublishParser/getSchemaFromPublish.d.ts +2 -9
  84. package/dist/PublishParser/getSchemaFromPublish.d.ts.map +1 -1
  85. package/dist/PublishParser/getSchemaFromPublish.js +5 -5
  86. package/dist/PublishParser/getSchemaFromPublish.js.map +1 -1
  87. package/dist/PublishParser/getTableRulesWithoutFileTable.d.ts +1 -1
  88. package/dist/PublishParser/getTableRulesWithoutFileTable.d.ts.map +1 -1
  89. package/dist/PublishParser/getTableRulesWithoutFileTable.js +30 -33
  90. package/dist/PublishParser/getTableRulesWithoutFileTable.js.map +1 -1
  91. package/dist/PublishParser/publishTypesAndUtils.d.ts +6 -11
  92. package/dist/PublishParser/publishTypesAndUtils.d.ts.map +1 -1
  93. package/dist/PublishParser/publishTypesAndUtils.js.map +1 -1
  94. package/dist/RestApi.d.ts +1 -1
  95. package/dist/RestApi.d.ts.map +1 -1
  96. package/dist/RestApi.js +19 -16
  97. package/dist/RestApi.js.map +1 -1
  98. package/dist/initProstgles.d.ts.map +1 -1
  99. package/dist/initProstgles.js +11 -15
  100. package/dist/initProstgles.js.map +1 -1
  101. package/dist/onSocketConnected.d.ts.map +1 -1
  102. package/dist/onSocketConnected.js +5 -5
  103. package/dist/onSocketConnected.js.map +1 -1
  104. package/dist/runClientRequest.d.ts +14 -29
  105. package/dist/runClientRequest.d.ts.map +1 -1
  106. package/dist/runClientRequest.js +20 -34
  107. package/dist/runClientRequest.js.map +1 -1
  108. package/lib/Auth/AuthHandler.ts +49 -103
  109. package/lib/Auth/AuthTypes.ts +19 -7
  110. package/lib/Auth/authProviders/setOAuthProviders.ts +1 -1
  111. package/lib/Auth/endpoints/getConfirmEmailRequestHandler.ts +1 -1
  112. package/lib/Auth/endpoints/getRegisterRequestHandler.ts +1 -1
  113. package/lib/Auth/setupAuthRoutes.ts +17 -13
  114. package/lib/Auth/utils/getUserFromRequest.ts +71 -0
  115. package/lib/DboBuilder/DboBuilder.ts +7 -3
  116. package/lib/DboBuilder/DboBuilderTypes.ts +19 -17
  117. package/lib/DboBuilder/QueryBuilder/getNewQuery.ts +2 -2
  118. package/lib/DboBuilder/QueryStreamer.ts +1 -1
  119. package/lib/DboBuilder/TableHandler/TableHandler.ts +2 -3
  120. package/lib/DboBuilder/TableHandler/insert.ts +2 -2
  121. package/lib/DboBuilder/TableHandler/update.ts +1 -1
  122. package/lib/DboBuilder/ViewHandler/ViewHandler.ts +38 -37
  123. package/lib/DboBuilder/ViewHandler/find.ts +1 -1
  124. package/lib/DboBuilder/ViewHandler/getExistsCondition.ts +4 -4
  125. package/lib/DboBuilder/ViewHandler/subscribe.ts +22 -41
  126. package/lib/DboBuilder/dboBuilderUtils.ts +3 -1
  127. package/lib/DboBuilder/insertNestedRecords.ts +18 -16
  128. package/lib/DboBuilder/runSQL.ts +14 -16
  129. package/lib/FileManager/initFileManager.ts +16 -12
  130. package/lib/Prostgles.ts +23 -29
  131. package/lib/ProstglesTypes.ts +9 -31
  132. package/lib/PubSubManager/PubSubManager.ts +3 -3
  133. package/lib/PubSubManager/addSync.ts +1 -3
  134. package/lib/PublishParser/PublishParser.ts +35 -45
  135. package/lib/PublishParser/getFileTableRules.ts +24 -48
  136. package/lib/PublishParser/getSchemaFromPublish.ts +12 -23
  137. package/lib/PublishParser/getTableRulesWithoutFileTable.ts +30 -41
  138. package/lib/PublishParser/publishTypesAndUtils.ts +8 -21
  139. package/lib/RestApi.ts +43 -31
  140. package/lib/initProstgles.ts +51 -64
  141. package/lib/onSocketConnected.ts +12 -9
  142. package/lib/runClientRequest.ts +50 -66
  143. package/package.json +3 -3
@@ -1,31 +1,20 @@
1
- import { AnyObject, TableHandler, UserLike, getKeys, pickKeys } from "prostgles-types";
2
- import { ExpressReq } from "./Auth/AuthTypes";
3
- import { LocalParams, PRGLIOSocket } from "./DboBuilder/DboBuilder";
1
+ import {
2
+ AnyObject,
3
+ SQLOptions,
4
+ SQLRequest,
5
+ TableHandler,
6
+ UserLike,
7
+ getKeys,
8
+ pickKeys,
9
+ } from "prostgles-types";
10
+ import { AuthClientRequest } from "./Auth/AuthTypes";
11
+ import { LocalParams } from "./DboBuilder/DboBuilder";
12
+ import { TableHandler as TableHandlerServer } from "./DboBuilder/TableHandler/TableHandler";
4
13
  import { parseFieldFilter } from "./DboBuilder/ViewHandler/parseFieldFilter";
5
14
  import { canRunSQL } from "./DboBuilder/runSQL";
6
15
  import { Prostgles } from "./Prostgles";
7
- import { TableHandler as TableHandlerServer } from "./DboBuilder/TableHandler/TableHandler";
8
16
  import { TableRule } from "./PublishParser/publishTypesAndUtils";
9
17
 
10
- type ReqInfo =
11
- | {
12
- type: "socket";
13
- socket: PRGLIOSocket;
14
- httpReq?: undefined;
15
- }
16
- | {
17
- type: "http";
18
- httpReq: ExpressReq;
19
- socket?: undefined;
20
- };
21
- type ReqInfoClient =
22
- | {
23
- socket: PRGLIOSocket;
24
- }
25
- | {
26
- httpReq: ExpressReq;
27
- };
28
-
29
18
  const TABLE_METHODS = {
30
19
  find: 1,
31
20
  findOne: 1,
@@ -50,7 +39,7 @@ const SOCKET_ONLY_COMMANDS = [
50
39
  "sync",
51
40
  ] as const satisfies typeof TABLE_METHODS_KEYS;
52
41
 
53
- type Args = ReqInfo & {
42
+ type Args = {
54
43
  tableName: string;
55
44
  command: string;
56
45
  param1: any;
@@ -58,13 +47,6 @@ type Args = ReqInfo & {
58
47
  param3: any;
59
48
  };
60
49
 
61
- const getReqInfoClient = (reqInfo: ReqInfo): ReqInfoClient => {
62
- if (reqInfo.type === "socket") {
63
- return { socket: reqInfo.socket };
64
- }
65
- return { httpReq: reqInfo.httpReq };
66
- };
67
-
68
50
  type TableMethodFunctionWithRulesAndLocalParams = (
69
51
  arg1: any,
70
52
  arg2: any,
@@ -73,17 +55,13 @@ type TableMethodFunctionWithRulesAndLocalParams = (
73
55
  localParams: LocalParams
74
56
  ) => any;
75
57
 
76
- export const runClientRequest = async function (this: Prostgles, args: Args) {
58
+ export const runClientRequest = async function (
59
+ this: Prostgles,
60
+ args: Args,
61
+ clientReq: AuthClientRequest
62
+ ) {
77
63
  /* Channel name will only include client-sent params so we ignore table_rules enforced params */
78
- if (
79
- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
80
- (args.type === "socket" && !args.socket) ||
81
- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
82
- (args.type === "http" && !args.httpReq) ||
83
- // !this.authHandler ||
84
- !this.publishParser ||
85
- !this.dbo
86
- ) {
64
+ if (!this.publishParser || !this.dbo) {
87
65
  throw "socket/httpReq or authhandler missing";
88
66
  }
89
67
 
@@ -92,17 +70,16 @@ export const runClientRequest = async function (this: Prostgles, args: Args) {
92
70
  throw `Invalid command: ${nonValidatedCommand}. Expecting one of: ${TABLE_METHODS_KEYS};`;
93
71
  }
94
72
  const command = nonValidatedCommand as keyof TableHandler;
95
- if (args.type !== "socket" && SOCKET_ONLY_COMMANDS.some((v) => v === command)) {
73
+ if (!clientReq.socket && SOCKET_ONLY_COMMANDS.some((v) => v === command)) {
96
74
  throw (
97
75
  "The following commands cannot be completed over a non-websocket connection: " +
98
76
  SOCKET_ONLY_COMMANDS
99
77
  );
100
78
  }
101
79
 
102
- const reqInfo = getReqInfoClient(args);
103
- const clientInfo = await this.authHandler?.getClientInfo(args);
80
+ const clientInfo = await this.authHandler?.getUserFromRequest(clientReq);
104
81
  const validRules = await this.publishParser.getValidatedRequestRule(
105
- { tableName, command, localParams: reqInfo },
82
+ { tableName, command, clientReq },
106
83
  clientInfo
107
84
  );
108
85
 
@@ -123,11 +100,11 @@ export const runClientRequest = async function (this: Prostgles, args: Args) {
123
100
  ...(pickKeys(clientInfo.user, ["id", "type"]) as UserLike),
124
101
  };
125
102
  const localParams: LocalParams = {
126
- ...reqInfo,
103
+ clientReq,
127
104
  isRemoteRequest: { user: sessionUser },
128
105
  };
129
106
  if (param3 && (param3 as LocalParams).returnQuery) {
130
- const isAllowed = await canRunSQL(this, localParams);
107
+ const isAllowed = await canRunSQL(this, clientReq);
131
108
  if (isAllowed) {
132
109
  localParams.returnQuery = (param3 as LocalParams).returnQuery;
133
110
  } else {
@@ -156,47 +133,54 @@ export const runClientRequest = async function (this: Prostgles, args: Args) {
156
133
  // return result;
157
134
  };
158
135
 
159
- export const clientCanRunSqlRequest = async function (this: Prostgles, args: ReqInfo) {
160
- const reqInfo = getReqInfoClient(args);
136
+ // const getReqInfoClient = <A extends AuthClientRequest>(args: A): AuthClientRequest =>
137
+ // args.httpReq ? { res: args.res, httpReq: args.httpReq } : { socket: args.socket };
138
+
139
+ export const clientCanRunSqlRequest = async function (
140
+ this: Prostgles,
141
+ clientReq: AuthClientRequest
142
+ ) {
161
143
  if (!this.opts.publishRawSQL || typeof this.opts.publishRawSQL !== "function") {
162
- return { allowed: false, reqInfo };
144
+ return { allowed: false, clientReq };
163
145
  }
164
146
  const canRunSQL = async () => {
165
147
  if (!this.authHandler) {
166
148
  throw "authHandler missing";
167
149
  }
168
- const publishParams = await this.publishParser?.getPublishParams(reqInfo);
169
- const res = await this.opts.publishRawSQL?.(publishParams as any);
150
+ const publishParams = await this.publishParser?.getPublishParams(clientReq);
151
+ const res = publishParams && (await this.opts.publishRawSQL?.(publishParams));
170
152
  return Boolean((res && typeof res === "boolean") || res === "*");
171
153
  };
172
154
 
173
155
  const allowed = await canRunSQL();
174
- return { allowed, reqInfo };
156
+ return { allowed, reqInfo: clientReq };
175
157
  };
176
158
 
177
- type ArgsSql = ReqInfo & {
178
- query: string;
179
- args?: AnyObject | any[];
180
- options?: any;
181
- };
182
- export const runClientSqlRequest = async function (this: Prostgles, params: ArgsSql) {
183
- const { allowed, reqInfo } = await clientCanRunSqlRequest.bind(this)(params);
159
+ export const runClientSqlRequest = async function (
160
+ this: Prostgles,
161
+ reqData: SQLRequest,
162
+ clientReq: AuthClientRequest
163
+ ) {
164
+ const { allowed } = await clientCanRunSqlRequest.bind(this)(clientReq);
184
165
  if (!allowed) {
185
166
  throw "Not allowed to execute sql";
186
167
  }
187
168
  if (!this.dbo?.sql) throw "Internal error: sql handler missing";
188
- const { query, args, options } = params;
189
- return this.dbo.sql(query, args, options, reqInfo);
169
+ const { query, params, options } = reqData;
170
+ return this.dbo.sql(query, params, options, { clientReq });
190
171
  };
191
172
 
192
- type ArgsMethod = ReqInfo & {
173
+ type ArgsMethod = {
193
174
  method: string;
194
175
  params?: any[];
195
176
  };
196
- export const runClientMethod = async function (this: Prostgles, reqArgs: ArgsMethod) {
197
- const reqInfo = getReqInfoClient(reqArgs);
177
+ export const runClientMethod = async function (
178
+ this: Prostgles,
179
+ reqArgs: ArgsMethod,
180
+ clientReq: AuthClientRequest
181
+ ) {
198
182
  const { method, params = [] } = reqArgs;
199
- const methods = await this.publishParser?.getAllowedMethods(reqInfo);
183
+ const methods = await this.publishParser?.getAllowedMethods(clientReq, undefined);
200
184
 
201
185
  if (!methods || !methods[method]) {
202
186
  throw "Disallowed/missing method " + JSON.stringify(method);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "prostgles-server",
3
- "version": "4.2.191",
3
+ "version": "4.2.193",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -55,12 +55,12 @@
55
55
  "pg": "^8.11.5",
56
56
  "pg-cursor": "^2.11.0",
57
57
  "pg-promise": "^11.9.1",
58
- "prostgles-types": "^4.0.130"
58
+ "prostgles-types": "^4.0.133"
59
59
  },
60
60
  "devDependencies": {
61
61
  "@types/express": "^4.17.21",
62
62
  "@types/json-schema": "^7.0.15",
63
- "@types/node": "^22.8.1",
63
+ "@types/node": "^22.10.2",
64
64
  "@types/nodemailer": "^6.4.17",
65
65
  "@types/pg": "^8.11.5",
66
66
  "@types/pg-cursor": "^2.7.2",