@openpeepshq/common 0.6.1 → 0.6.3

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.
@@ -292,7 +292,7 @@ const formatRecurrencePattern = (recurrence, t, start) => {
292
292
  const interval = recurrence.interval ?? 1;
293
293
  if (recurrence.freq === 'DAILY') {
294
294
  return interval === 1
295
- ? t('events.repeat.daily', { defaultValue: 'Daily' })
295
+ ? t('events.form.repeat.daily', { defaultValue: 'Daily' })
296
296
  : t('events.repeat.everyNDays', {
297
297
  defaultValue: 'Every {{count}} days',
298
298
  count: interval,
@@ -300,7 +300,7 @@ const formatRecurrencePattern = (recurrence, t, start) => {
300
300
  }
301
301
  if (recurrence.freq === 'MONTHLY') {
302
302
  return interval === 1
303
- ? t('events.repeat.monthly', { defaultValue: 'Monthly' })
303
+ ? t('events.form.repeat.monthly', { defaultValue: 'Monthly' })
304
304
  : t('events.repeat.everyNMonths', {
305
305
  defaultValue: 'Every {{count}} months',
306
306
  count: interval,
@@ -325,7 +325,7 @@ const formatRecurrencePattern = (recurrence, t, start) => {
325
325
  });
326
326
  }
327
327
  return interval === 1
328
- ? t('events.repeat.weekly', { defaultValue: 'Weekly' })
328
+ ? t('events.form.repeat.weekly', { defaultValue: 'Weekly' })
329
329
  : t('events.repeat.everyNWeeks', {
330
330
  defaultValue: 'Every {{count}} weeks',
331
331
  count: interval,
@@ -1,3 +1,5 @@
1
+ /** True when `end` is set and not after `start` (equal times are invalid). */
2
+ export declare const isEndBeforeStart: (start?: string, end?: string) => boolean;
1
3
  /**
2
4
  * Interpret a datetime-local wall clock (`YYYY-MM-DDTHH:mm`) in `timeZone`
3
5
  * and return a UTC ISO string.
@@ -18,6 +18,8 @@ const tzOffsetMs = (utcMs, timeZone) => {
18
18
  return asUtc - utcMs;
19
19
  };
20
20
  const pad2 = (value) => String(value).padStart(2, '0');
21
+ /** True when `end` is set and not after `start` (equal times are invalid). */
22
+ export const isEndBeforeStart = (start, end) => Boolean(start && end && new Date(end) <= new Date(start));
21
23
  /**
22
24
  * Interpret a datetime-local wall clock (`YYYY-MM-DDTHH:mm`) in `timeZone`
23
25
  * and return a UTC ISO string.
@@ -41,4 +41,13 @@ export type CategoryNode = {
41
41
  };
42
42
  export declare const categoryTreeFromPaths: (paths: readonly (readonly string[])[]) => CategoryNode[];
43
43
  export declare const pathStartsWith: (path: readonly string[], prefix: readonly string[]) => boolean;
44
+ /** Returns an injective key for a category path. Uses `JSON.stringify` so that
45
+ * a folder named `A/B` (single segment `["A/B"]`) never collides with
46
+ * folder `A` containing subfolder `B` (`["A","B"]`).
47
+ */
48
+ export declare const categoryPathKey: (path: readonly string[]) => string;
49
+ /** Validates that no segment of a category path is blank or whitespace-only.
50
+ * Returns `true` for `undefined` (absent — not a validation error).
51
+ */
52
+ export declare const validResourceCategoryPath: (path: readonly string[] | undefined) => boolean;
44
53
  export declare const normalizeResourceTags: (tags: readonly string[] | undefined) => string[];
@@ -122,6 +122,19 @@ export const categoryTreeFromPaths = (paths) => {
122
122
  };
123
123
  export const pathStartsWith = (path, prefix) => prefix.length <= path.length &&
124
124
  prefix.every((segment, index) => path[index] === segment);
125
+ /** Returns an injective key for a category path. Uses `JSON.stringify` so that
126
+ * a folder named `A/B` (single segment `["A/B"]`) never collides with
127
+ * folder `A` containing subfolder `B` (`["A","B"]`).
128
+ */
129
+ export const categoryPathKey = (path) => JSON.stringify(path);
130
+ /** Validates that no segment of a category path is blank or whitespace-only.
131
+ * Returns `true` for `undefined` (absent — not a validation error).
132
+ */
133
+ export const validResourceCategoryPath = (path) => {
134
+ if (!path)
135
+ return true;
136
+ return path.every((segment) => segment.trim().length > 0 && segment === segment.trim());
137
+ };
125
138
  export const normalizeResourceTags = (tags) => {
126
139
  const seen = new Set();
127
140
  const out = [];
@@ -19,6 +19,7 @@ export declare const interpolate: (text: string | undefined, params?: Record<str
19
19
  export declare const sleep: (seconds: number) => Promise<void>;
20
20
  export declare const deepSet: <T>(obj: T, path: string | (string | number)[], val: unknown) => T;
21
21
  export declare const deepGet: (obj: unknown, path: (string | number)[]) => unknown;
22
+ export declare const deepEqual: (a: unknown, b: unknown) => boolean;
22
23
  /**
23
24
  * Checks if a value is valid => not (null, undefined, empty, etc.)
24
25
  * Special case: treats 0 as a valid value unlike standard JS truthiness
package/dist/lib/utils.js CHANGED
@@ -34,12 +34,23 @@ export const getUniqueBy = (arr, predicate) => {
34
34
  const set = new Set();
35
35
  return arr.filter((o) => !set.has(predicate(o)) && set.add(predicate(o)));
36
36
  };
37
+ const UNSAFE_KEYS = new Set(['__proto__', 'prototype', 'constructor']);
37
38
  export const interpolate = (text, params = {}) => {
38
39
  try {
39
40
  if (!text)
40
41
  return text;
41
42
  const names = Object.keys(params);
42
43
  const vals = Object.values(params);
44
+ // Sanitize: reject param values that would produce unsafe jsonpath keys.
45
+ // jsonpath 1.3.0 throws "Unsafe key" for __proto__/prototype/constructor,
46
+ // which would cause an unhandled 500 in the SSO handler.
47
+ for (const val of vals) {
48
+ if (UNSAFE_KEYS.has(val)) {
49
+ throw new Error(`Interpolated value "${val}" matches an unsafe key (` +
50
+ [...UNSAFE_KEYS].join(', ') +
51
+ `) — this would trigger a jsonpath parse error`);
52
+ }
53
+ }
43
54
  return new Function(...names, `return \`${text}\`;`)(...vals);
44
55
  }
45
56
  catch {
@@ -84,6 +95,22 @@ export const deepSet = (obj, path, val) => {
84
95
  export const deepGet = (obj, path) =>
85
96
  // @ts-expect-error deepget is not typeable
86
97
  path.reduce((o, i) => o[i], obj);
98
+ export const deepEqual = (a, b) => {
99
+ if (a === b)
100
+ return true;
101
+ if (typeof a !== 'object' || typeof b !== 'object')
102
+ return false;
103
+ if (a === null || b === null)
104
+ return false;
105
+ if (Array.isArray(a) !== Array.isArray(b))
106
+ return false;
107
+ const keysA = Object.keys(a);
108
+ const keysB = Object.keys(b);
109
+ if (keysA.length !== keysB.length)
110
+ return false;
111
+ return keysA.every((key) => Object.prototype.hasOwnProperty.call(b, key) &&
112
+ deepEqual(a[key], b[key]));
113
+ };
87
114
  /**
88
115
  * Checks if a value is valid => not (null, undefined, empty, etc.)
89
116
  * Special case: treats 0 as a valid value unlike standard JS truthiness
@@ -39006,6 +39006,7 @@ export declare const mediaStreamRequestSchema: z.ZodObject<{
39006
39006
  }, z.core.$strip>;
39007
39007
  export type MediaStreamRequest = z.infer<typeof mediaStreamRequestSchema>;
39008
39008
  export declare const updateProfileRequestSchema: z.ZodObject<{
39009
+ timeZone: z.ZodOptional<z.ZodString>;
39009
39010
  type: z.ZodEnum<{
39010
39011
  local: "local";
39011
39012
  guest: "guest";
@@ -39025,7 +39026,6 @@ export declare const updateProfileRequestSchema: z.ZodObject<{
39025
39026
  alt: z.ZodOptional<z.ZodNumber>;
39026
39027
  }, z.core.$strip>>;
39027
39028
  }, z.core.$strip>>;
39028
- timeZone: z.ZodOptional<z.ZodString>;
39029
39029
  deletedHandle: z.ZodOptional<z.ZodString>;
39030
39030
  header: z.ZodOptional<z.ZodNullable<z.ZodURL>>;
39031
39031
  bio: z.ZodOptional<z.ZodString>;
@@ -2,6 +2,7 @@ import { z } from 'zod';
2
2
  import validator from 'validator';
3
3
  import ISO6391 from 'iso-639-1';
4
4
  import { extendZodWithOpenApi } from '@asteasolutions/zod-to-openapi';
5
+ import { isEndBeforeStart } from '../lib/eventTime.js';
5
6
  import { groupRelationships } from './capabilities.js';
6
7
  import { capabilitiesSchema } from './config.js';
7
8
  import { HANDLE_RESERVED_MESSAGE, additionalForbiddenHandlesSchema, classifyAdditionalForbiddenHandle, collectAdditionalForbiddenHandles, forbiddenHandles, handleRegex, handleRegexBase, isDefaultForbiddenHandle, isForbiddenHandle, normalizeForbiddenHandle, } from './forbiddenHandles.js';
@@ -421,6 +422,10 @@ export const eventOccurrenceExceptionSchema = z
421
422
  start: eventIsoDatetimeSchema.optional(),
422
423
  end: eventIsoDatetimeSchema.optional(),
423
424
  physicalLocation: locationSchema.optional(),
425
+ })
426
+ .refine((exception) => !isEndBeforeStart(exception.start, exception.end), {
427
+ message: 'events.form.endBeforeStart',
428
+ path: ['end'],
424
429
  })
425
430
  .openapi('EventOccurrenceException');
426
431
  export const eventSchema = noteSchema
@@ -443,7 +448,7 @@ export const eventSchema = noteSchema
443
448
  recurrence: eventRecurrenceSchema.optional(),
444
449
  exceptions: eventOccurrenceExceptionSchema.array().optional(),
445
450
  })
446
- .refine((data) => !data.end || new Date(data.end) > new Date(data.start), {
451
+ .refine((data) => !isEndBeforeStart(data.start, data.end), {
447
452
  message: 'events.form.endBeforeStart',
448
453
  path: ['end'],
449
454
  })
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openpeepshq/common",
3
- "version": "0.6.1",
3
+ "version": "0.6.3",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "sideEffects": false,
@@ -8,7 +8,7 @@
8
8
  "@asteasolutions/zod-to-openapi": "^8.5.0",
9
9
  "@date-fns/tz": "^1.5.0",
10
10
  "date-fns": "^4.1.0",
11
- "ical-generator": "^10.2.0",
11
+ "ical-generator": "^11.1.3",
12
12
  "iso-639-1": "^3.1.5",
13
13
  "jose": "^6.2.2",
14
14
  "timezones-ical-library": "^2.4.0",
@@ -59,10 +59,9 @@
59
59
  "@types/validator": "^13.15.10",
60
60
  "@typescript-eslint/eslint-plugin": "^8.72.0",
61
61
  "@typescript-eslint/parser": "^8.72.0",
62
- "@vitest/coverage-v8": "^2.1.9",
63
- "@vitest/ui": "^2.1.9",
64
- "concurrently": "^9.2.1",
65
- "eslint": "^10.1.0",
62
+ "@vitest/coverage-v8": "^4.1.12",
63
+ "@vitest/ui": "^4.1.12",
64
+ "eslint": "^10.14.0",
66
65
  "eslint-config-prettier": "^9.1.0",
67
66
  "i18next": "^26.0.3",
68
67
  "livekit-client": "^2.19.2",
@@ -70,7 +69,7 @@
70
69
  "tsc-alias": "^1.8.16",
71
70
  "typed-emitter": "^2.1.0",
72
71
  "typescript": "^6.0.2",
73
- "vitest": "^2.1.9"
72
+ "vitest": "^4.1.12"
74
73
  },
75
74
  "scripts": {
76
75
  "lint": "eslint .",
@@ -437,7 +437,7 @@ const formatRecurrencePattern = (
437
437
 
438
438
  if (recurrence.freq === 'DAILY') {
439
439
  return interval === 1
440
- ? t('events.repeat.daily', { defaultValue: 'Daily' })
440
+ ? t('events.form.repeat.daily', { defaultValue: 'Daily' })
441
441
  : t('events.repeat.everyNDays', {
442
442
  defaultValue: 'Every {{count}} days',
443
443
  count: interval,
@@ -446,7 +446,7 @@ const formatRecurrencePattern = (
446
446
 
447
447
  if (recurrence.freq === 'MONTHLY') {
448
448
  return interval === 1
449
- ? t('events.repeat.monthly', { defaultValue: 'Monthly' })
449
+ ? t('events.form.repeat.monthly', { defaultValue: 'Monthly' })
450
450
  : t('events.repeat.everyNMonths', {
451
451
  defaultValue: 'Every {{count}} months',
452
452
  count: interval,
@@ -475,7 +475,7 @@ const formatRecurrencePattern = (
475
475
  }
476
476
 
477
477
  return interval === 1
478
- ? t('events.repeat.weekly', { defaultValue: 'Weekly' })
478
+ ? t('events.form.repeat.weekly', { defaultValue: 'Weekly' })
479
479
  : t('events.repeat.everyNWeeks', {
480
480
  defaultValue: 'Every {{count}} weeks',
481
481
  count: interval,
@@ -34,6 +34,10 @@ const tzOffsetMs = (utcMs: number, timeZone: string): number => {
34
34
 
35
35
  const pad2 = (value: number): string => String(value).padStart(2, '0');
36
36
 
37
+ /** True when `end` is set and not after `start` (equal times are invalid). */
38
+ export const isEndBeforeStart = (start?: string, end?: string) =>
39
+ Boolean(start && end && new Date(end) <= new Date(start));
40
+
37
41
  /**
38
42
  * Interpret a datetime-local wall clock (`YYYY-MM-DDTHH:mm`) in `timeZone`
39
43
  * and return a UTC ISO string.
@@ -176,6 +176,25 @@ export const pathStartsWith = (
176
176
  prefix.length <= path.length &&
177
177
  prefix.every((segment, index) => path[index] === segment);
178
178
 
179
+ /** Returns an injective key for a category path. Uses `JSON.stringify` so that
180
+ * a folder named `A/B` (single segment `["A/B"]`) never collides with
181
+ * folder `A` containing subfolder `B` (`["A","B"]`).
182
+ */
183
+ export const categoryPathKey = (path: readonly string[]): string =>
184
+ JSON.stringify(path);
185
+
186
+ /** Validates that no segment of a category path is blank or whitespace-only.
187
+ * Returns `true` for `undefined` (absent — not a validation error).
188
+ */
189
+ export const validResourceCategoryPath = (
190
+ path: readonly string[] | undefined,
191
+ ): boolean => {
192
+ if (!path) return true;
193
+ return path.every(
194
+ (segment) => segment.trim().length > 0 && segment === segment.trim(),
195
+ );
196
+ };
197
+
179
198
  export const normalizeResourceTags = (
180
199
  tags: readonly string[] | undefined,
181
200
  ): string[] => {
package/src/lib/utils.ts CHANGED
@@ -104,6 +104,8 @@ export const getUniqueBy = <T, V = string>(
104
104
  return arr.filter((o) => !set.has(predicate(o)) && set.add(predicate(o)));
105
105
  };
106
106
 
107
+ const UNSAFE_KEYS = new Set(['__proto__', 'prototype', 'constructor']);
108
+
107
109
  export const interpolate = (
108
110
  text: string | undefined,
109
111
  params: Record<string, string> = {},
@@ -113,6 +115,20 @@ export const interpolate = (
113
115
 
114
116
  const names = Object.keys(params);
115
117
  const vals = Object.values(params);
118
+
119
+ // Sanitize: reject param values that would produce unsafe jsonpath keys.
120
+ // jsonpath 1.3.0 throws "Unsafe key" for __proto__/prototype/constructor,
121
+ // which would cause an unhandled 500 in the SSO handler.
122
+ for (const val of vals) {
123
+ if (UNSAFE_KEYS.has(val)) {
124
+ throw new Error(
125
+ `Interpolated value "${val}" matches an unsafe key (` +
126
+ [...UNSAFE_KEYS].join(', ') +
127
+ `) — this would trigger a jsonpath parse error`,
128
+ );
129
+ }
130
+ }
131
+
116
132
  return new Function(...names, `return \`${text}\`;`)(...vals);
117
133
  } catch {
118
134
  throw new Error(
@@ -170,6 +186,24 @@ export const deepGet = (obj: unknown, path: (string | number)[]) =>
170
186
  // @ts-expect-error deepget is not typeable
171
187
  path.reduce((o, i) => o[i], obj);
172
188
 
189
+ export const deepEqual = (a: unknown, b: unknown): boolean => {
190
+ if (a === b) return true;
191
+ if (typeof a !== 'object' || typeof b !== 'object') return false;
192
+ if (a === null || b === null) return false;
193
+ if (Array.isArray(a) !== Array.isArray(b)) return false;
194
+ const keysA = Object.keys(a);
195
+ const keysB = Object.keys(b);
196
+ if (keysA.length !== keysB.length) return false;
197
+ return keysA.every(
198
+ (key) =>
199
+ Object.prototype.hasOwnProperty.call(b, key) &&
200
+ deepEqual(
201
+ (a as Record<string, unknown>)[key],
202
+ (b as Record<string, unknown>)[key],
203
+ ),
204
+ );
205
+ };
206
+
173
207
  /**
174
208
  * Checks if a value is valid => not (null, undefined, empty, etc.)
175
209
  * Special case: treats 0 as a valid value unlike standard JS truthiness
@@ -2,6 +2,7 @@ import { z, util, ZodObject, ZodRawShape } from 'zod';
2
2
  import validator from 'validator';
3
3
  import ISO6391 from 'iso-639-1';
4
4
  import { extendZodWithOpenApi } from '@asteasolutions/zod-to-openapi';
5
+ import { isEndBeforeStart } from '../lib/eventTime';
5
6
  import { groupRelationships } from './capabilities';
6
7
  import { capabilitiesSchema } from './config';
7
8
  import {
@@ -597,6 +598,10 @@ export const eventOccurrenceExceptionSchema = z
597
598
  end: eventIsoDatetimeSchema.optional(),
598
599
  physicalLocation: locationSchema.optional(),
599
600
  })
601
+ .refine((exception) => !isEndBeforeStart(exception.start, exception.end), {
602
+ message: 'events.form.endBeforeStart',
603
+ path: ['end'],
604
+ })
600
605
  .openapi('EventOccurrenceException');
601
606
  export type EventOccurrenceException = z.infer<
602
607
  typeof eventOccurrenceExceptionSchema
@@ -622,7 +627,7 @@ export const eventSchema = noteSchema
622
627
  recurrence: eventRecurrenceSchema.optional(),
623
628
  exceptions: eventOccurrenceExceptionSchema.array().optional(),
624
629
  })
625
- .refine((data) => !data.end || new Date(data.end) > new Date(data.start), {
630
+ .refine((data) => !isEndBeforeStart(data.start, data.end), {
626
631
  message: 'events.form.endBeforeStart',
627
632
  path: ['end'],
628
633
  })