prostgles-server 4.2.271 → 4.2.273

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 (69) hide show
  1. package/dist/Auth/AuthHandler.d.ts +12 -6
  2. package/dist/Auth/AuthHandler.d.ts.map +1 -1
  3. package/dist/Auth/AuthHandler.js +12 -6
  4. package/dist/Auth/AuthHandler.js.map +1 -1
  5. package/dist/Auth/AuthTypes.d.ts +5 -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.d.ts.map +1 -1
  9. package/dist/Auth/endpoints/setCatchAllRequestHandler.js +24 -17
  10. package/dist/Auth/endpoints/setCatchAllRequestHandler.js.map +1 -1
  11. package/dist/Auth/getClientAuth.d.ts +1 -1
  12. package/dist/Auth/getClientAuth.d.ts.map +1 -1
  13. package/dist/Auth/getClientAuth.js +10 -1
  14. package/dist/Auth/getClientAuth.js.map +1 -1
  15. package/dist/Auth/login.d.ts +2 -1
  16. package/dist/Auth/login.d.ts.map +1 -1
  17. package/dist/Auth/login.js +20 -13
  18. package/dist/Auth/login.js.map +1 -1
  19. package/dist/Auth/setupAuthRoutes.d.ts.map +1 -1
  20. package/dist/Auth/setupAuthRoutes.js +11 -3
  21. package/dist/Auth/setupAuthRoutes.js.map +1 -1
  22. package/dist/Auth/utils/getSidAndUserFromRequest.d.ts +3 -2
  23. package/dist/Auth/utils/getSidAndUserFromRequest.d.ts.map +1 -1
  24. package/dist/Auth/utils/getSidAndUserFromRequest.js +3 -2
  25. package/dist/Auth/utils/getSidAndUserFromRequest.js.map +1 -1
  26. package/dist/Auth/utils/getUserOrError.d.ts +3 -2
  27. package/dist/Auth/utils/getUserOrError.d.ts.map +1 -1
  28. package/dist/Auth/utils/handleGetUser.d.ts +3 -2
  29. package/dist/Auth/utils/handleGetUser.d.ts.map +1 -1
  30. package/dist/Auth/utils/handleGetUser.js +3 -9
  31. package/dist/Auth/utils/handleGetUser.js.map +1 -1
  32. package/dist/DboBuilder/ViewHandler/ViewHandler.d.ts +1 -1
  33. package/dist/DboBuilder/ViewHandler/ViewHandler.d.ts.map +1 -1
  34. package/dist/Prostgles.d.ts.map +1 -1
  35. package/dist/Prostgles.js +6 -0
  36. package/dist/Prostgles.js.map +1 -1
  37. package/dist/ProstglesTypes.d.ts +1 -1
  38. package/dist/ProstglesTypes.d.ts.map +1 -1
  39. package/dist/PubSubManager/PubSubManager.d.ts +1 -1
  40. package/dist/PubSubManager/PubSubManager.d.ts.map +1 -1
  41. package/dist/PublishParser/PublishParser.d.ts +2 -2
  42. package/dist/PublishParser/PublishParser.d.ts.map +1 -1
  43. package/dist/PublishParser/PublishParser.js +8 -1
  44. package/dist/PublishParser/PublishParser.js.map +1 -1
  45. package/dist/PublishParser/getSchemaFromPublish.d.ts.map +1 -1
  46. package/dist/PublishParser/getSchemaFromPublish.js +3 -0
  47. package/dist/PublishParser/getSchemaFromPublish.js.map +1 -1
  48. package/dist/onSocketConnected.d.ts.map +1 -1
  49. package/dist/onSocketConnected.js +12 -11
  50. package/dist/onSocketConnected.js.map +1 -1
  51. package/dist/runClientRequest.d.ts.map +1 -1
  52. package/dist/runClientRequest.js +3 -0
  53. package/dist/runClientRequest.js.map +1 -1
  54. package/lib/Auth/AuthHandler.ts +17 -7
  55. package/lib/Auth/AuthTypes.ts +4 -2
  56. package/lib/Auth/endpoints/setCatchAllRequestHandler.ts +27 -22
  57. package/lib/Auth/getClientAuth.ts +18 -4
  58. package/lib/Auth/login.ts +24 -17
  59. package/lib/Auth/setupAuthRoutes.ts +12 -3
  60. package/lib/Auth/utils/getSidAndUserFromRequest.ts +5 -3
  61. package/lib/Auth/utils/getUserOrError.ts +2 -2
  62. package/lib/Auth/utils/handleGetUser.ts +6 -10
  63. package/lib/Prostgles.ts +6 -0
  64. package/lib/ProstglesTypes.ts +1 -1
  65. package/lib/PublishParser/PublishParser.ts +10 -3
  66. package/lib/PublishParser/getSchemaFromPublish.ts +3 -1
  67. package/lib/onSocketConnected.ts +12 -9
  68. package/lib/runClientRequest.ts +3 -0
  69. package/package.json +1 -3
@@ -5,14 +5,20 @@ import {
5
5
  CHANNELS,
6
6
  getObjectEntries,
7
7
  isEmpty,
8
+ isObject,
8
9
  } from "prostgles-types";
9
- import { AuthClientRequest, LoginWithOAuthConfig, AuthResultWithSID } from "./AuthTypes";
10
+ import {
11
+ AuthClientRequest,
12
+ LoginWithOAuthConfig,
13
+ AuthResultWithSID,
14
+ type AuthResult,
15
+ } from "./AuthTypes";
10
16
  import { AUTH_ROUTES_AND_PARAMS, AuthHandler } from "./AuthHandler";
11
17
 
12
18
  export async function getClientAuth(
13
19
  this: AuthHandler,
14
20
  clientReq: AuthClientRequest
15
- ): Promise<{ auth: AuthSocketSchema; userData: AuthResultWithSID }> {
21
+ ): Promise<{ auth: AuthSocketSchema; userData: AuthResultWithSID } | "new-session-redirect"> {
16
22
  let pathGuard = false;
17
23
  const {
18
24
  loginWithOAuth,
@@ -30,6 +36,12 @@ export async function getClientAuth(
30
36
  * Due to SPA nature of some clients, we need to check if the connected client ends up on a protected route
31
37
  */
32
38
  if (clientReq.socket) {
39
+ const getUserFromRequest = async (clientReq: AuthClientRequest): Promise<AuthResult> => {
40
+ const sidAndUser = await this.getSidAndUserFromRequest(clientReq);
41
+ if (isObject(sidAndUser) && sidAndUser.sid && sidAndUser.user) {
42
+ return sidAndUser;
43
+ }
44
+ };
33
45
  const { socket } = clientReq;
34
46
  socket.removeAllListeners(CHANNELS.AUTHGUARD);
35
47
  socket.on(
@@ -54,7 +66,7 @@ export async function getClientAuth(
54
66
  pathname &&
55
67
  typeof pathname === "string" &&
56
68
  this.isUserRoute(pathname) &&
57
- !(await this.getUserFromRequest({ socket }))
69
+ !(await getUserFromRequest({ socket }))
58
70
  ) {
59
71
  cb(null, { shouldReload: true });
60
72
  } else {
@@ -70,7 +82,9 @@ export async function getClientAuth(
70
82
  }
71
83
 
72
84
  const userData = await this.getSidAndUserFromRequest(clientReq);
73
-
85
+ if (userData === "new-session-redirect") {
86
+ return userData;
87
+ }
74
88
  const auth: AuthSocketSchema = {
75
89
  providers: getOAuthProviders(loginWithOAuth),
76
90
  signupWithEmailAndPassword: signupWithEmailAndPassword && {
package/lib/Auth/login.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { DBOFullyTyped } from "../DBSchemaBuilder";
2
2
  import { AuthHandler, getClientRequestIPsInfo, HTTP_FAIL_CODES } from "./AuthHandler";
3
- import { ExpressReq, LoginParams } from "./AuthTypes";
3
+ import { ExpressReq, LoginParams, type BasicSession } from "./AuthTypes";
4
4
  import { LoginResponseHandler } from "./endpoints/setLoginRequestHandler";
5
5
  import { throttledAuthCall } from "./utils/throttledReject";
6
6
 
@@ -30,22 +30,9 @@ export async function login(
30
30
  return result;
31
31
  }
32
32
 
33
- const { sid, expires } = result.session;
34
- if (!sid) {
35
- console.error("Invalid sid");
36
- return "server-error";
37
- }
38
- if (sid && (typeof sid !== "string" || typeof expires !== "number")) {
39
- console.error(
40
- "Bad login result type. \nExpecting: undefined | null | { sid: string; expires: number }"
41
- );
42
- return "server-error";
43
- }
44
- if (expires < Date.now()) {
45
- console.error(
46
- "auth.login() is returning an expired session. Can only login with a session.expires greater than Date.now()"
47
- );
48
- return "server-error";
33
+ const sessionErrorCode = getBasicSessionErrorCode(result.session);
34
+ if (sessionErrorCode) {
35
+ return sessionErrorCode;
49
36
  }
50
37
 
51
38
  return result;
@@ -76,3 +63,23 @@ export async function login(
76
63
  }
77
64
  this.setCookieAndGoToReturnURLIFSet(loginResponse.session, { req, res });
78
65
  }
66
+
67
+ export const getBasicSessionErrorCode = (session: Pick<BasicSession, "expires" | "sid">) => {
68
+ const { sid, expires } = session;
69
+ if (!sid) {
70
+ console.error("Invalid sid");
71
+ return "server-error";
72
+ }
73
+ if (sid && (typeof sid !== "string" || typeof expires !== "number")) {
74
+ console.error(
75
+ "Bad login result type. \nExpecting: undefined | null | { sid: string; expires: number }"
76
+ );
77
+ return "server-error";
78
+ }
79
+ if (expires < Date.now()) {
80
+ console.error(
81
+ "auth.login() is returning an expired session. Can only login with a session.expires greater than Date.now()"
82
+ );
83
+ return "server-error";
84
+ }
85
+ };
@@ -42,17 +42,23 @@ export function setupAuthRoutes(this: AuthHandler) {
42
42
  if (onUseOrSocketConnected) {
43
43
  const prostglesUseMiddleware: RequestHandler = async (req, res, next) => {
44
44
  const reqInfo = { httpReq: req, res };
45
- const errorInfo = await onUseOrSocketConnected(
45
+ const errorInfoOrSession = await onUseOrSocketConnected(
46
46
  this.getSIDNoError(reqInfo),
47
47
  getClientRequestIPsInfo(reqInfo),
48
48
  reqInfo
49
49
  );
50
50
 
51
- if (errorInfo) {
52
- const { error, httpCode } = errorInfo;
51
+ if (errorInfoOrSession && "error" in errorInfoOrSession) {
52
+ const { error, httpCode } = errorInfoOrSession;
53
53
  res.status(httpCode).json({ error });
54
54
  return;
55
55
  }
56
+
57
+ if (errorInfoOrSession && "session" in errorInfoOrSession) {
58
+ const { session } = errorInfoOrSession;
59
+ this.validateSessionAndSetCookie(session, { req, res });
60
+ return;
61
+ }
56
62
  next();
57
63
  };
58
64
  upsertNamedExpressMiddleware(app, prostglesUseMiddleware, "prostglesonUseOrSocketConnected");
@@ -66,6 +72,9 @@ export function setupAuthRoutes(this: AuthHandler) {
66
72
  next,
67
73
  getUser: async () => {
68
74
  const userOrErr = await this.getUserOrError({ httpReq: req, res });
75
+ if (userOrErr === "new-session-redirect") {
76
+ throw "new-session-redirect";
77
+ }
69
78
  if (userOrErr.error) {
70
79
  res.status(HTTP_FAIL_CODES.BAD_REQUEST).json(userOrErr.error);
71
80
  throw userOrErr.error;
@@ -1,7 +1,9 @@
1
+ import { isObject } from "prostgles-types";
1
2
  import { DBOFullyTyped } from "../../DBSchemaBuilder";
2
3
  import { AuthHandler, getClientRequestIPsInfo } from "../AuthHandler";
3
4
  import { AuthClientRequest, AuthResultWithSID } from "../AuthTypes";
4
5
  import { throttledAuthCall } from "./throttledReject";
6
+ import type { GetUserOrRedirected } from "./handleGetUser";
5
7
 
6
8
  /**
7
9
  * For a given sid return the user data if available using the auth handler's getUser method.
@@ -11,7 +13,7 @@ import { throttledAuthCall } from "./throttledReject";
11
13
  export async function getSidAndUserFromRequest(
12
14
  this: AuthHandler,
13
15
  clientReq: AuthClientRequest
14
- ): Promise<AuthResultWithSID> {
16
+ ): Promise<GetUserOrRedirected> {
15
17
  /**
16
18
  * Get cached session if available
17
19
  */
@@ -71,14 +73,14 @@ export async function getSidAndUserFromRequest(
71
73
  // return { sid, preferredLogin: !clientInfo?.user ? clientInfo?.preferredLogin : undefined };
72
74
  // }, 100);
73
75
  const result = await this.handleGetUser(clientReq);
74
- if (result.error) {
76
+ if (isObject(result) && result.error) {
75
77
  throw result.error;
76
78
  }
77
79
  await this.prostgles.opts.onLog?.({
78
80
  type: "auth",
79
81
  command: "getClientInfo",
80
82
  duration: Date.now() - authStart,
81
- sid: result.sid,
83
+ sid: isObject(result) ? result.sid : undefined,
82
84
  socketId: clientReq.socket?.id,
83
85
  });
84
86
  return result;
@@ -3,7 +3,7 @@ import { DBOFullyTyped } from "../../DBSchemaBuilder";
3
3
  import type { AuthHandler } from "../AuthHandler";
4
4
  import { AuthClientRequest, AuthResultWithSID } from "../AuthTypes";
5
5
  import { getClientRequestIPsInfo } from "../utils/getClientRequestIPsInfo";
6
- import { isAuthError } from "./handleGetUser";
6
+ import { isAuthError, type GetUserOrRedirected } from "./handleGetUser";
7
7
  import { throttledAuthCall } from "./throttledReject";
8
8
 
9
9
  /**
@@ -14,7 +14,7 @@ import { throttledAuthCall } from "./throttledReject";
14
14
  export async function getUserOrError(
15
15
  this: AuthHandler,
16
16
  clientReq: AuthClientRequest
17
- ): Promise<AuthResultWithSID> {
17
+ ): Promise<GetUserOrRedirected> {
18
18
  // const sid = this.getValidatedSid(clientReq);
19
19
  // if (!sid) return { sid };
20
20
 
@@ -1,14 +1,15 @@
1
+ import { AuthResponse, isObject } from "prostgles-types";
1
2
  import type { DBOFullyTyped } from "../../DBSchemaBuilder";
2
- import { tout } from "../../PubSubManager/initPubSubManager";
3
3
  import { getClientRequestIPsInfo, type AuthHandler } from "../AuthHandler";
4
4
  import type { AuthClientRequest, AuthResultOrError, AuthResultWithSID } from "../AuthTypes";
5
5
  import { throttledAuthCall } from "./throttledReject";
6
- import { AuthResponse, isObject } from "prostgles-types";
6
+
7
+ export type GetUserOrRedirected = AuthResultWithSID | "new-session-redirect";
7
8
 
8
9
  export async function handleGetUserThrottled(
9
10
  this: AuthHandler,
10
11
  clientReq: AuthClientRequest
11
- ): Promise<AuthResultWithSID> {
12
+ ): Promise<GetUserOrRedirected> {
12
13
  const getSessionForCaching = this.opts.cacheSession?.getSession;
13
14
  const result = await throttledAuthCall(async () => {
14
15
  const clientInfoOrErr = await this.opts.getUser(
@@ -31,13 +32,8 @@ export async function handleGetUserThrottled(
31
32
  if (!("httpReq" in clientReq) || !clientReq.httpReq)
32
33
  throw "httpReq missing. new-session not implemented for sockets.";
33
34
  const { httpReq, res } = clientReq;
34
- this.setCookieAndGoToReturnURLIFSet(clientInfo.session, { req: httpReq, res });
35
- /** Wait for refresh */
36
- await tout(200);
37
- return {
38
- error: { success: false, code: "something-went-wrong" },
39
- sid: this.getValidatedSid(clientReq),
40
- } satisfies AuthResultWithSID;
35
+ this.validateSessionAndSetCookie(clientInfo.session, { req: httpReq, res });
36
+ return "new-session-redirect" as const;
41
37
  }
42
38
 
43
39
  const sid = this.getValidatedSid(clientReq);
package/lib/Prostgles.ts CHANGED
@@ -367,6 +367,9 @@ export class Prostgles {
367
367
  : { type: "http" as const, ...clientReq };
368
368
 
369
369
  const userData = await this.authHandler?.getSidAndUserFromRequest(clientInfo);
370
+ if (userData === "new-session-redirect") {
371
+ throw "new-session-redirect";
372
+ }
370
373
  const { publishParser } = this;
371
374
  let fullSchema: Awaited<ReturnType<PublishParser["getSchemaFromPublish"]>> | undefined;
372
375
  let publishValidationError;
@@ -427,6 +430,9 @@ export class Prostgles {
427
430
  });
428
431
 
429
432
  const authInfo = await this.authHandler?.getClientAuth(clientReq);
433
+ if (authInfo === "new-session-redirect") {
434
+ throw "new-session-redirect";
435
+ }
430
436
 
431
437
  const clientSchema: ClientSchema = {
432
438
  schema,
@@ -299,7 +299,7 @@ export type ProstglesInitOptions<S = void, SUser extends SessionUser = SessionUs
299
299
  /**
300
300
  * Usefull for logging or debugging
301
301
  */
302
- onLog?: (evt: EventInfo) => Promise<void>;
302
+ onLog?: (evt: EventInfo) => Promise<void> | void;
303
303
  };
304
304
 
305
305
  export type TableConfigMigrations = {
@@ -10,12 +10,11 @@ import { getTableRulesWithoutFileTable } from "./getTableRulesWithoutFileTable";
10
10
  import {
11
11
  DboTable,
12
12
  DboTableCommand,
13
- ParsedPublishTable,
13
+ ParsedTableRule,
14
14
  PublishMethods,
15
15
  type PublishObject,
16
16
  PublishParams,
17
17
  RULE_TO_METHODS,
18
- ParsedTableRule,
19
18
  parsePublishTableRule,
20
19
  } from "./publishTypesAndUtils";
21
20
 
@@ -43,9 +42,14 @@ export class PublishParser {
43
42
  clientReq: AuthClientRequest,
44
43
  clientInfo: AuthResultWithSID | undefined
45
44
  ): Promise<PublishParams> {
45
+ const _clientInfo =
46
+ clientInfo ?? (await this.prostgles.authHandler?.getSidAndUserFromRequest(clientReq));
47
+ if (_clientInfo === "new-session-redirect") {
48
+ throw "new-session-redirect";
49
+ }
46
50
  return {
47
51
  sid: undefined,
48
- ...(clientInfo ?? (await this.prostgles.authHandler?.getSidAndUserFromRequest(clientReq))),
52
+ ..._clientInfo,
49
53
  dbo: this.dbo as DBOFullyTyped,
50
54
  db: this.db,
51
55
  clientReq,
@@ -109,6 +113,9 @@ export class PublishParser {
109
113
  }: DboTableCommand): Promise<ParsedTableRule> {
110
114
  const clientInfo =
111
115
  clientReq && (await this.prostgles.authHandler?.getSidAndUserFromRequest(clientReq));
116
+ if (clientInfo === "new-session-redirect") {
117
+ throw "new-session-redirect";
118
+ }
112
119
  const rules = await this.getValidatedRequestRule({ tableName, command, clientReq }, clientInfo);
113
120
  return rules;
114
121
  }
@@ -38,7 +38,9 @@ export async function getSchemaFromPublish(
38
38
  /* Publish tables and views based on socket */
39
39
  const clientInfo =
40
40
  userData ?? (await this.prostgles.authHandler?.getSidAndUserFromRequest(clientReq));
41
-
41
+ if (clientInfo === "new-session-redirect") {
42
+ throw "new-session-redirect";
43
+ }
42
44
  let _publish: PublishObject | undefined;
43
45
  try {
44
46
  _publish = await this.getPublishAsObject(clientReq, clientInfo);
@@ -15,6 +15,15 @@ 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
29
  sid: this.authHandler?.getValidatedSid({ socket }),
@@ -35,17 +44,15 @@ export async function onSocketConnected(this: Prostgles, socket: PRGLIOSocket) {
35
44
  { socket }
36
45
  );
37
46
  if (errorInfo) {
38
- socket.emit(CHANNELS.CONNECTION, { error: errorInfo.error });
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,
@@ -97,10 +104,6 @@ export async function onSocketConnected(this: Prostgles, socket: PRGLIOSocket) {
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
  });
@@ -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.271",
3
+ "version": "4.2.273",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -68,8 +68,6 @@
68
68
  "@types/pg": "^8.11.5",
69
69
  "@types/pg-cursor": "^2.7.2",
70
70
  "@types/sharp": "^0.30.4",
71
- "@typescript-eslint/eslint-plugin": "^8.26.1",
72
- "@typescript-eslint/parser": "^8.26.1",
73
71
  "eslint": "^9.22.0",
74
72
  "eslint-plugin-security": "^3.0.1",
75
73
  "prettier": "^3.4.2",