propro-utils 1.5.97 → 1.6.0

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.
@@ -1,11 +1,11 @@
1
- require('dotenv').config();
2
- const axios = require('axios');
3
- const { getOrSetCache } = require('../utils/redis');
4
- const { v4: uuidv4 } = require('uuid');
5
- const ServiceManager = require('../utils/serviceManager');
1
+ require("dotenv").config();
2
+ const axios = require("axios");
3
+ const { getOrSetCache } = require("../utils/redis");
4
+ const { v4: uuidv4 } = require("uuid");
5
+ const ServiceManager = require("../utils/serviceManager");
6
6
  const defaultUserGlobalStyleShortcuts =
7
- require('./defaultUserGlobalStyleShortcuts.json').defaultGlobalStyleShortcuts;
8
- const defaultFolders = require('./defaultFolders.json').defaultFolders;
7
+ require("./defaultUserGlobalStyleShortcuts.json").defaultGlobalStyleShortcuts;
8
+ const defaultFolders = require("./defaultFolders.json").defaultFolders;
9
9
 
10
10
  /**
11
11
  * Retrieves the account profile data from the authentication server and caches it using Redis.
@@ -20,10 +20,10 @@ const defaultFolders = require('./defaultFolders.json').defaultFolders;
20
20
  const getAccountProfile = async (redisClient, userSchema, accountId) => {
21
21
  try {
22
22
  const accessToken =
23
- req.cookies['x-access-token'] || req.headers.authorization?.split(' ')[1];
23
+ req.cookies["x-access-token"] || req.headers.authorization?.split(" ")[1];
24
24
 
25
25
  if (!accessToken) {
26
- throw new Error('Access token is required');
26
+ throw new Error("Access token is required");
27
27
  }
28
28
 
29
29
  const fetchPermission = async () => {
@@ -49,7 +49,7 @@ const getAccountProfile = async (redisClient, userSchema, accountId) => {
49
49
  );
50
50
 
51
51
  if (!profileData) {
52
- throw new Error('Invalid permissions');
52
+ throw new Error("Invalid permissions");
53
53
  }
54
54
 
55
55
  return profileData;
@@ -57,7 +57,7 @@ const getAccountProfile = async (redisClient, userSchema, accountId) => {
57
57
  if (error.response && error.response.status) {
58
58
  throw new Error(error.response.data.message);
59
59
  }
60
- throw new Error('Error validating token');
60
+ throw new Error("Error validating token");
61
61
  }
62
62
  };
63
63
 
@@ -67,12 +67,13 @@ const getAccountProfile = async (redisClient, userSchema, accountId) => {
67
67
  * @returns {Promise<Object>} The consolidated UserGlobalStyles document
68
68
  */
69
69
  async function consolidateGlobalStyles(accountId) {
70
- const userStyleSchema = await ServiceManager.getService('UserStyleSchema');
70
+ const userStyleSchema = await ServiceManager.getService("UserStyleSchema");
71
71
 
72
- // Find all global styles for this account
72
+ // Find all global styles for this account with allowDiskUse option
73
73
  const allStyles = await userStyleSchema
74
74
  .find({ accountId })
75
- .sort({ createdAt: 1 });
75
+ .sort({ createdAt: 1 })
76
+ .option({ allowDiskUse: true });
76
77
 
77
78
  if (allStyles.length <= 1) {
78
79
  return allStyles[0];
@@ -83,8 +84,8 @@ async function consolidateGlobalStyles(accountId) {
83
84
  const uniqueShortcuts = new Map();
84
85
 
85
86
  // Merge all shortcuts, keeping the latest version of each shortcut by ID
86
- allStyles.forEach(style => {
87
- style.styleShortcuts.forEach(shortcut => {
87
+ allStyles.forEach((style) => {
88
+ style.styleShortcuts.forEach((shortcut) => {
88
89
  uniqueShortcuts.set(shortcut.id.toString(), shortcut);
89
90
  });
90
91
  });
@@ -100,7 +101,7 @@ async function consolidateGlobalStyles(accountId) {
100
101
  });
101
102
 
102
103
  // Update user reference if needed
103
- const userSchema = await ServiceManager.getService('UserSchema');
104
+ const userSchema = await ServiceManager.getService("UserSchema");
104
105
  await userSchema.updateMany(
105
106
  { accountId },
106
107
  { userGlobalStyles: mainStyle._id }
@@ -108,7 +109,6 @@ async function consolidateGlobalStyles(accountId) {
108
109
 
109
110
  return mainStyle;
110
111
  }
111
-
112
112
  /**
113
113
  * Creates a new user global styles document with default style shortcuts.
114
114
  *
@@ -140,8 +140,8 @@ async function createUserGlobalStyles(userStyleSchema, accountId) {
140
140
  */
141
141
  async function createDefaultFolders(folderSchema, accountId) {
142
142
  try {
143
- console.log('Creating default folders for user:', accountId);
144
- const folderPromises = defaultFolders.map(folder =>
143
+ console.log("Creating default folders for user:", accountId);
144
+ const folderPromises = defaultFolders.map((folder) =>
145
145
  folderSchema.create({
146
146
  ...folder,
147
147
  user_id: accountId,
@@ -149,17 +149,17 @@ async function createDefaultFolders(folderSchema, accountId) {
149
149
  );
150
150
  return Promise.all(folderPromises);
151
151
  } catch (error) {
152
- console.error('Error in createDefaultFolders:', error);
153
- throw new Error('Failed to create default folders');
152
+ console.error("Error in createDefaultFolders:", error);
153
+ throw new Error("Failed to create default folders");
154
154
  }
155
155
  }
156
156
 
157
157
  const DEFAULT_THEME = {
158
- canvasBackground: '#1E1D1D',
158
+ canvasBackground: "#1E1D1D",
159
159
  defaultItemWidth: 200,
160
- defaultColor: '#ffffff',
161
- fontSize: '16px',
162
- name: 'Default Theme',
160
+ defaultColor: "#ffffff",
161
+ fontSize: "16px",
162
+ name: "Default Theme",
163
163
  isDefault: true,
164
164
  };
165
165
 
@@ -171,49 +171,49 @@ const DEFAULT_THEME = {
171
171
  * @returns {Promise<Object>} A promise that resolves to the complete user object
172
172
  * @throws {Error} If there's an issue with database operations or invalid input
173
173
  */
174
- const checkIfUserExists = async accountId => {
174
+ const checkIfUserExists = async (accountId) => {
175
175
  // Input validation
176
- if (!accountId || typeof accountId !== 'string') {
177
- console.warn('Invalid accountId provided:', accountId);
178
- throw new Error('Invalid accountId provided');
176
+ if (!accountId || typeof accountId !== "string") {
177
+ console.warn("Invalid accountId provided:", accountId);
178
+ throw new Error("Invalid accountId provided");
179
179
  }
180
180
 
181
181
  try {
182
182
  const schemaResults = await Promise.all([
183
- ServiceManager.getService('UserSchema'),
184
- ServiceManager.getService('UserStyleSchema'),
185
- ServiceManager.getService('FolderSchema'),
186
- ServiceManager.getService('ThemeSchema'),
183
+ ServiceManager.getService("UserSchema"),
184
+ ServiceManager.getService("UserStyleSchema"),
185
+ ServiceManager.getService("FolderSchema"),
186
+ ServiceManager.getService("ThemeSchema"),
187
187
  ]);
188
188
 
189
189
  const [userSchema, userStyleSchema, folderSchema, themeSchema] =
190
190
  schemaResults;
191
191
 
192
192
  if (!userSchema) {
193
- throw new Error('UserSchema service not available');
193
+ throw new Error("UserSchema service not available");
194
194
  }
195
195
 
196
196
  // Optional schemas warning
197
197
  if (!userStyleSchema) {
198
198
  console.warn(
199
- 'UserStyleSchema service not available - style features will be skipped'
199
+ "UserStyleSchema service not available - style features will be skipped"
200
200
  );
201
201
  }
202
202
  if (!folderSchema) {
203
203
  console.warn(
204
- 'FolderSchema service not available - folder features will be skipped'
204
+ "FolderSchema service not available - folder features will be skipped"
205
205
  );
206
206
  }
207
207
  if (!themeSchema) {
208
208
  console.warn(
209
- 'ThemeSchema service not available - theme features will be skipped'
209
+ "ThemeSchema service not available - theme features will be skipped"
210
210
  );
211
211
  }
212
212
 
213
213
  // Find existing user with populated fields
214
214
  const user = await userSchema
215
215
  .findOne({ accountId })
216
- .select('-theme -folderList -userGlobalStyles -mapList')
216
+ .select("-theme -folderList -userGlobalStyles -mapList")
217
217
  .lean();
218
218
 
219
219
  if (user) {
@@ -231,19 +231,19 @@ const checkIfUserExists = async accountId => {
231
231
  (async () => {
232
232
  const existingFolders = await folderSchema
233
233
  .find({ user_id: user.id })
234
- .select('name')
234
+ .select("name")
235
235
  .lean();
236
236
 
237
237
  const existingFolderNames = new Set(
238
- existingFolders.map(f => f.name)
238
+ existingFolders.map((f) => f.name)
239
239
  );
240
240
  const foldersToCreate = defaultFolders.filter(
241
- folder => !existingFolderNames.has(folder.name)
241
+ (folder) => !existingFolderNames.has(folder.name)
242
242
  );
243
243
 
244
244
  if (foldersToCreate.length > 0) {
245
245
  return folderSchema.insertMany(
246
- foldersToCreate.map(folder => ({
246
+ foldersToCreate.map((folder) => ({
247
247
  ...folder,
248
248
  user_id: user.id,
249
249
  }))
@@ -258,7 +258,7 @@ const checkIfUserExists = async accountId => {
258
258
  updates.push(
259
259
  (async () => {
260
260
  const defaultThemeExists = await themeSchema.exists({
261
- name: 'Default Theme',
261
+ name: "Default Theme",
262
262
  accountId,
263
263
  });
264
264
 
@@ -286,7 +286,7 @@ const checkIfUserExists = async accountId => {
286
286
  // Return fresh user data after updates, excluding specified fields
287
287
  return userSchema
288
288
  .findOne({ accountId })
289
- .select('-theme -folderList -userGlobalStyles -mapList')
289
+ .select("-theme -folderList -userGlobalStyles -mapList")
290
290
  .lean();
291
291
  }
292
292
 
@@ -298,7 +298,7 @@ const checkIfUserExists = async accountId => {
298
298
  let userGlobalStyles;
299
299
  if (userStyleSchema) {
300
300
  creationTasks.push(
301
- createUserGlobalStyles(userStyleSchema, accountId).then(styles => {
301
+ createUserGlobalStyles(userStyleSchema, accountId).then((styles) => {
302
302
  userGlobalStyles = styles;
303
303
  return styles;
304
304
  })
@@ -316,8 +316,8 @@ const checkIfUserExists = async accountId => {
316
316
  userId,
317
317
  id: uuidv4(),
318
318
  })
319
- .then(theme => {
320
- console.log('Created theme:', theme);
319
+ .then((theme) => {
320
+ console.log("Created theme:", theme);
321
321
  defaultTheme = theme;
322
322
  return theme;
323
323
  })
@@ -343,10 +343,10 @@ const checkIfUserExists = async accountId => {
343
343
 
344
344
  return userSchema
345
345
  .findById(newUser._id)
346
- .select('-theme -folderList -userGlobalStyles -mapList')
346
+ .select("-theme -folderList -userGlobalStyles -mapList")
347
347
  .lean();
348
348
  } catch (error) {
349
- console.error('Detailed error in checkIfUserExists:', {
349
+ console.error("Detailed error in checkIfUserExists:", {
350
350
  accountId,
351
351
  errorName: error.name,
352
352
  errorMessage: error.message,
@@ -361,9 +361,9 @@ const updateUserGlobalStyleShortcuts = async (userId, shortcutId, shortcut) => {
361
361
  try {
362
362
  const user = await userSchema
363
363
  .findOne({ id: userId })
364
- .populate('userGlobalStyles');
364
+ .populate("userGlobalStyles");
365
365
  if (!user) {
366
- throw new Error('User not found');
366
+ throw new Error("User not found");
367
367
  }
368
368
 
369
369
  // Ensure we have consolidated styles
@@ -371,7 +371,7 @@ const updateUserGlobalStyleShortcuts = async (userId, shortcutId, shortcut) => {
371
371
 
372
372
  // Find the index of the shortcut to update
373
373
  const shortcutIndex = consolidatedStyles.styleShortcuts.findIndex(
374
- s => s.id.toString() === shortcutId
374
+ (s) => s.id.toString() === shortcutId
375
375
  );
376
376
 
377
377
  if (shortcutIndex === -1) {
@@ -388,7 +388,7 @@ const updateUserGlobalStyleShortcuts = async (userId, shortcutId, shortcut) => {
388
388
  await consolidatedStyles.save();
389
389
  return consolidatedStyles;
390
390
  } catch (error) {
391
- console.error('Error updating user global style shortcuts:', error);
391
+ console.error("Error updating user global style shortcuts:", error);
392
392
  throw error;
393
393
  }
394
394
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "propro-utils",
3
- "version": "1.5.97",
3
+ "version": "1.6.0",
4
4
  "description": "Auth middleware for propro-auth",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -3,19 +3,19 @@
3
3
  * This module provides functions for setting and clearing authentication cookies.
4
4
  */
5
5
 
6
- const { URL } = require('url');
6
+ const { URL } = require("url");
7
7
 
8
8
  /**
9
9
  * Safely stringify an object, handling circular references
10
10
  * @param {Object} obj - The object to stringify
11
11
  * @return {string} A JSON string representation of the object
12
12
  */
13
- const safeStringify = obj => {
13
+ const safeStringify = (obj) => {
14
14
  const seen = new WeakSet();
15
15
  return JSON.stringify(obj, (key, value) => {
16
- if (typeof value === 'object' && value !== null) {
16
+ if (typeof value === "object" && value !== null) {
17
17
  if (seen.has(value)) {
18
- return '[Circular]';
18
+ return "[Circular]";
19
19
  }
20
20
  seen.add(value);
21
21
  }
@@ -28,7 +28,7 @@ const safeStringify = obj => {
28
28
  * @param {Object} user - The user object to sanitize
29
29
  * @return {Object} A sanitized version of the user object
30
30
  */
31
- const sanitizeUser = user => {
31
+ const sanitizeUser = (user) => {
32
32
  const sanitized = { ...user };
33
33
 
34
34
  delete sanitized.password;
@@ -61,10 +61,10 @@ const sanitizeUser = user => {
61
61
  * @param {Object} details - Cookie details
62
62
  * @returns {Promise} Promise that resolves when cookie is set
63
63
  */
64
- const setChromeExtensionCookie = details => {
64
+ const setChromeExtensionCookie = (details) => {
65
65
  return new Promise((resolve, reject) => {
66
66
  try {
67
- chrome.cookies.set(details, cookie => {
67
+ chrome.cookies.set(details, (cookie) => {
68
68
  if (chrome.runtime.lastError) {
69
69
  reject(chrome.runtime.lastError);
70
70
  } else {
@@ -83,13 +83,13 @@ const setChromeExtensionCookie = details => {
83
83
  */
84
84
  const setAuthCookies = async (res, tokens, account, user, appUrl) => {
85
85
  if (!tokens?.refresh?.token || !tokens?.access?.token) {
86
- throw new Error('Invalid tokens object');
86
+ throw new Error("Invalid tokens object");
87
87
  }
88
88
  if (!account) {
89
- throw new Error('Invalid account object');
89
+ throw new Error("Invalid account object");
90
90
  }
91
91
  if (!user) {
92
- throw new Error('Invalid user object');
92
+ throw new Error("Invalid user object");
93
93
  }
94
94
 
95
95
  const currentDateTime = new Date();
@@ -102,34 +102,34 @@ const setAuthCookies = async (res, tokens, account, user, appUrl) => {
102
102
  let domain;
103
103
  try {
104
104
  domain = appUrl ? new URL(appUrl).hostname : undefined;
105
- if (domain?.includes('mapmap.app')) {
106
- domain = '.mapmap.app';
105
+ if (domain?.includes("mapmap.app")) {
106
+ domain = ".mapmap.app";
107
107
  }
108
- if (domain?.includes('localhost')) {
108
+ if (domain?.includes("localhost")) {
109
109
  domain = undefined;
110
110
  }
111
- if (domain?.includes('propro.so')) {
112
- domain = 'propro.so';
111
+ if (domain?.includes("propro.so")) {
112
+ domain = ".propro.so";
113
113
  }
114
114
  } catch (error) {
115
- console.error('Invalid appUrl:', { error, appUrl });
115
+ console.error("Invalid appUrl:", { error, appUrl });
116
116
  domain = undefined;
117
117
  }
118
118
 
119
119
  const commonAttributes = {
120
120
  secure: true,
121
- sameSite: 'None',
121
+ sameSite: "None",
122
122
  domain,
123
- path: '/',
123
+ path: "/",
124
124
  };
125
125
 
126
126
  const httpOnlyCookies = {
127
- 'x-refresh-token': {
127
+ "x-refresh-token": {
128
128
  value: tokens.refresh.token,
129
129
  maxAge: refreshMaxAge,
130
130
  httpOnly: true,
131
131
  },
132
- 'x-access-token': {
132
+ "x-access-token": {
133
133
  value: tokens.access.token,
134
134
  maxAge: accessMaxAge,
135
135
  httpOnly: true,
@@ -150,7 +150,7 @@ const setAuthCookies = async (res, tokens, account, user, appUrl) => {
150
150
  maxAge: refreshMaxAge,
151
151
  },
152
152
  has_account_token: {
153
- value: JSON.stringify({ value: 'true', expires: accessMaxAge }),
153
+ value: JSON.stringify({ value: "true", expires: accessMaxAge }),
154
154
  maxAge: accessMaxAge,
155
155
  },
156
156
  };
@@ -170,21 +170,21 @@ const setAuthCookies = async (res, tokens, account, user, appUrl) => {
170
170
  ...regularCookies,
171
171
  }).map(([name, config]) => {
172
172
  return setChromeExtensionCookie({
173
- url: `https://${domain || 'propro.so'}`,
173
+ url: `https://${domain || "propro.so"}`,
174
174
  name,
175
175
  value: config.value,
176
176
  secure: true,
177
177
  httpOnly: !!config.httpOnly,
178
- sameSite: 'no_restriction',
179
- path: '/',
178
+ sameSite: "no_restriction",
179
+ path: "/",
180
180
  expirationDate: Math.floor((Date.now() + config.maxAge) / 1000),
181
- domain: domain?.startsWith('.') ? domain : `.${domain || 'propro.so'}`,
181
+ domain: domain?.startsWith(".") ? domain : `.${domain || "propro.so"}`,
182
182
  });
183
183
  });
184
184
 
185
185
  await Promise.allSettled(extensionCookiePromises);
186
186
 
187
- console.log('Auth cookies set successfully', {
187
+ console.log("Auth cookies set successfully", {
188
188
  domain,
189
189
  sameSite: commonAttributes.sameSite,
190
190
  cookieNames: [
@@ -193,11 +193,11 @@ const setAuthCookies = async (res, tokens, account, user, appUrl) => {
193
193
  ],
194
194
  });
195
195
  } catch (error) {
196
- console.error('Error setting cookies:', {
196
+ console.error("Error setting cookies:", {
197
197
  error: error.message,
198
198
  stack: error.stack,
199
199
  });
200
- throw new Error('Failed to set authentication cookies');
200
+ throw new Error("Failed to set authentication cookies");
201
201
  }
202
202
  };
203
203
 
@@ -208,44 +208,44 @@ const clearAuthCookies = async (res, appUrl) => {
208
208
  let domain;
209
209
  try {
210
210
  domain = appUrl ? new URL(appUrl).hostname : undefined;
211
- if (domain?.includes('mapmap.app')) {
212
- domain = '.mapmap.app';
211
+ if (domain?.includes("mapmap.app")) {
212
+ domain = ".mapmap.app";
213
213
  }
214
- if (domain?.includes('localhost')) {
214
+ if (domain?.includes("localhost")) {
215
215
  domain = undefined;
216
216
  }
217
217
  } catch (error) {
218
- console.error('Invalid appUrl:', error);
218
+ console.error("Invalid appUrl:", error);
219
219
  domain = undefined;
220
220
  }
221
221
 
222
222
  const commonAttributes = {
223
223
  secure: true,
224
- sameSite: 'None',
224
+ sameSite: "None",
225
225
  domain,
226
- path: '/',
226
+ path: "/",
227
227
  };
228
228
 
229
229
  const cookieNames = [
230
- 'x-refresh-token',
231
- 'x-access-token',
232
- 'user',
233
- 'account',
234
- 'has_account_token',
230
+ "x-refresh-token",
231
+ "x-access-token",
232
+ "user",
233
+ "account",
234
+ "has_account_token",
235
235
  ];
236
236
 
237
237
  // Clear web cookies
238
- cookieNames.forEach(cookieName => {
238
+ cookieNames.forEach((cookieName) => {
239
239
  res.clearCookie(cookieName, commonAttributes);
240
240
  });
241
241
 
242
242
  try {
243
243
  const extensionClearPromises = cookieNames.map(
244
- name =>
245
- new Promise(resolve => {
244
+ (name) =>
245
+ new Promise((resolve) => {
246
246
  chrome.cookies.remove(
247
247
  {
248
- url: `https://${domain || 'mapmap.app'}`,
248
+ url: `https://${domain || "mapmap.app"}`,
249
249
  name,
250
250
  },
251
251
  resolve
@@ -258,7 +258,7 @@ const clearAuthCookies = async (res, appUrl) => {
258
258
  // Not in extension context, ignore
259
259
  }
260
260
 
261
- console.log('Auth cookies cleared successfully', {
261
+ console.log("Auth cookies cleared successfully", {
262
262
  domain,
263
263
  cookieNames,
264
264
  sameSite: commonAttributes.sameSite,