@tellescope/utilities 1.72.2 → 1.74.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.
Files changed (85) hide show
  1. package/lib/cjs/ObjectId/bson_value.d.ts +10 -0
  2. package/lib/cjs/ObjectId/bson_value.d.ts.map +1 -0
  3. package/lib/cjs/ObjectId/bson_value.js +20 -0
  4. package/lib/cjs/ObjectId/bson_value.js.map +1 -0
  5. package/lib/cjs/ObjectId/constants.d.ts +107 -0
  6. package/lib/cjs/ObjectId/constants.d.ts.map +1 -0
  7. package/lib/cjs/ObjectId/constants.js +108 -0
  8. package/lib/cjs/ObjectId/constants.js.map +1 -0
  9. package/lib/cjs/ObjectId/error.d.ts +50 -0
  10. package/lib/cjs/ObjectId/error.d.ts.map +1 -0
  11. package/lib/cjs/ObjectId/error.js +117 -0
  12. package/lib/cjs/ObjectId/error.js.map +1 -0
  13. package/lib/cjs/ObjectId/objectid.d.ts +96 -0
  14. package/lib/cjs/ObjectId/objectid.d.ts.map +1 -0
  15. package/lib/cjs/ObjectId/objectid.js +300 -0
  16. package/lib/cjs/ObjectId/objectid.js.map +1 -0
  17. package/lib/cjs/ObjectId/parser/utils.d.ts +8 -0
  18. package/lib/cjs/ObjectId/parser/utils.d.ts.map +1 -0
  19. package/lib/cjs/ObjectId/parser/utils.js +32 -0
  20. package/lib/cjs/ObjectId/parser/utils.js.map +1 -0
  21. package/lib/cjs/ObjectId/utils/byte_utils.d.ts +46 -0
  22. package/lib/cjs/ObjectId/utils/byte_utils.d.ts.map +1 -0
  23. package/lib/cjs/ObjectId/utils/byte_utils.js +48 -0
  24. package/lib/cjs/ObjectId/utils/byte_utils.js.map +1 -0
  25. package/lib/cjs/ObjectId/utils/node_byte_utils.d.ts +31 -0
  26. package/lib/cjs/ObjectId/utils/node_byte_utils.d.ts.map +1 -0
  27. package/lib/cjs/ObjectId/utils/node_byte_utils.js +95 -0
  28. package/lib/cjs/ObjectId/utils/node_byte_utils.js.map +1 -0
  29. package/lib/cjs/ObjectId/utils/web_byte_utils.d.ts +27 -0
  30. package/lib/cjs/ObjectId/utils/web_byte_utils.d.ts.map +1 -0
  31. package/lib/cjs/ObjectId/utils/web_byte_utils.js +126 -0
  32. package/lib/cjs/ObjectId/utils/web_byte_utils.js.map +1 -0
  33. package/lib/cjs/utils.d.ts +2 -2
  34. package/lib/cjs/utils.d.ts.map +1 -1
  35. package/lib/cjs/utils.js +5 -4
  36. package/lib/cjs/utils.js.map +1 -1
  37. package/lib/esm/ObjectId/bson_value.d.ts +10 -0
  38. package/lib/esm/ObjectId/bson_value.d.ts.map +1 -0
  39. package/lib/esm/ObjectId/bson_value.js +17 -0
  40. package/lib/esm/ObjectId/bson_value.js.map +1 -0
  41. package/lib/esm/ObjectId/constants.d.ts +107 -0
  42. package/lib/esm/ObjectId/constants.d.ts.map +1 -0
  43. package/lib/esm/ObjectId/constants.js +105 -0
  44. package/lib/esm/ObjectId/constants.js.map +1 -0
  45. package/lib/esm/ObjectId/error.d.ts +50 -0
  46. package/lib/esm/ObjectId/error.d.ts.map +1 -0
  47. package/lib/esm/ObjectId/error.js +114 -0
  48. package/lib/esm/ObjectId/error.js.map +1 -0
  49. package/lib/esm/ObjectId/objectid.d.ts +96 -0
  50. package/lib/esm/ObjectId/objectid.d.ts.map +1 -0
  51. package/lib/esm/ObjectId/objectid.js +297 -0
  52. package/lib/esm/ObjectId/objectid.js.map +1 -0
  53. package/lib/esm/ObjectId/parser/utils.d.ts +8 -0
  54. package/lib/esm/ObjectId/parser/utils.d.ts.map +1 -0
  55. package/lib/esm/ObjectId/parser/utils.js +22 -0
  56. package/lib/esm/ObjectId/parser/utils.js.map +1 -0
  57. package/lib/esm/ObjectId/utils/byte_utils.d.ts +46 -0
  58. package/lib/esm/ObjectId/utils/byte_utils.d.ts.map +1 -0
  59. package/lib/esm/ObjectId/utils/byte_utils.js +45 -0
  60. package/lib/esm/ObjectId/utils/byte_utils.js.map +1 -0
  61. package/lib/esm/ObjectId/utils/node_byte_utils.d.ts +31 -0
  62. package/lib/esm/ObjectId/utils/node_byte_utils.d.ts.map +1 -0
  63. package/lib/esm/ObjectId/utils/node_byte_utils.js +91 -0
  64. package/lib/esm/ObjectId/utils/node_byte_utils.js.map +1 -0
  65. package/lib/esm/ObjectId/utils/web_byte_utils.d.ts +27 -0
  66. package/lib/esm/ObjectId/utils/web_byte_utils.d.ts.map +1 -0
  67. package/lib/esm/ObjectId/utils/web_byte_utils.js +122 -0
  68. package/lib/esm/ObjectId/utils/web_byte_utils.js.map +1 -0
  69. package/lib/esm/utils.d.ts +2 -2
  70. package/lib/esm/utils.d.ts.map +1 -1
  71. package/lib/esm/utils.js +2 -1
  72. package/lib/esm/utils.js.map +1 -1
  73. package/lib/tsconfig.tsbuildinfo +1 -1
  74. package/package.json +4 -6
  75. package/src/ObjectId/LICENSE.md +201 -0
  76. package/src/ObjectId/README.txt +1 -0
  77. package/src/ObjectId/bson_value.ts +18 -0
  78. package/src/ObjectId/constants.ts +141 -0
  79. package/src/ObjectId/error.ts +83 -0
  80. package/src/ObjectId/objectid.ts +325 -0
  81. package/src/ObjectId/parser/utils.ts +29 -0
  82. package/src/ObjectId/utils/byte_utils.ts +61 -0
  83. package/src/ObjectId/utils/node_byte_utils.ts +141 -0
  84. package/src/ObjectId/utils/web_byte_utils.ts +190 -0
  85. package/src/utils.ts +3 -1
@@ -0,0 +1,190 @@
1
+ import { BSONError } from '../error';
2
+
3
+ type TextDecoder = {
4
+ readonly encoding: string;
5
+ readonly fatal: boolean;
6
+ readonly ignoreBOM: boolean;
7
+ decode(input?: Uint8Array): string;
8
+ };
9
+ type TextDecoderConstructor = {
10
+ new (label: 'utf8', options: { fatal: boolean; ignoreBOM?: boolean }): TextDecoder;
11
+ };
12
+
13
+ type TextEncoder = {
14
+ readonly encoding: string;
15
+ encode(input?: string): Uint8Array;
16
+ };
17
+ type TextEncoderConstructor = {
18
+ new (): TextEncoder;
19
+ };
20
+
21
+ // Web global
22
+ declare const TextDecoder: TextDecoderConstructor;
23
+ declare const TextEncoder: TextEncoderConstructor;
24
+ declare const atob: (base64: string) => string;
25
+ declare const btoa: (binary: string) => string;
26
+
27
+ type ArrayBufferViewWithTag = ArrayBufferView & {
28
+ [Symbol.toStringTag]?: string;
29
+ };
30
+
31
+ function isReactNative() {
32
+ const { navigator } = globalThis as { navigator?: { product?: string } };
33
+ return typeof navigator === 'object' && navigator.product === 'ReactNative';
34
+ }
35
+
36
+ /** @internal */
37
+ export function webMathRandomBytes(byteLength: number) {
38
+ if (byteLength < 0) {
39
+ throw new RangeError(`The argument 'byteLength' is invalid. Received ${byteLength}`);
40
+ }
41
+ return webByteUtils.fromNumberArray(
42
+ Array.from({ length: byteLength }, () => Math.floor(Math.random() * 256))
43
+ );
44
+ }
45
+
46
+ /** @internal */
47
+ const webRandomBytes: (byteLength: number) => Uint8Array = (() => {
48
+ const { crypto } = globalThis as {
49
+ crypto?: { getRandomValues?: (space: Uint8Array) => Uint8Array };
50
+ };
51
+ if (crypto != null && typeof crypto.getRandomValues === 'function') {
52
+ return (byteLength: number) => {
53
+ // @ts-expect-error: crypto.getRandomValues cannot actually be null here
54
+ // You cannot separate getRandomValues from crypto (need to have this === crypto)
55
+ return crypto.getRandomValues(webByteUtils.allocate(byteLength));
56
+ };
57
+ } else {
58
+ if (isReactNative()) {
59
+ const { console } = globalThis as { console?: { warn?: (message: string) => void } };
60
+ console?.warn?.(
61
+ 'BSON: For React Native please polyfill crypto.getRandomValues, e.g. using: https://www.npmjs.com/package/react-native-get-random-values.'
62
+ );
63
+ }
64
+ return webMathRandomBytes;
65
+ }
66
+ })();
67
+
68
+ const HEX_DIGIT = /(\d|[a-f])/i;
69
+
70
+ /** @internal */
71
+ export const webByteUtils = {
72
+ toLocalBufferType(
73
+ potentialUint8array: Uint8Array | ArrayBufferViewWithTag | ArrayBuffer
74
+ ): Uint8Array {
75
+ const stringTag =
76
+ potentialUint8array?.[Symbol.toStringTag] ??
77
+ Object.prototype.toString.call(potentialUint8array);
78
+
79
+ if (stringTag === 'Uint8Array') {
80
+ return potentialUint8array as Uint8Array;
81
+ }
82
+
83
+ if (ArrayBuffer.isView(potentialUint8array)) {
84
+ return new Uint8Array(
85
+ potentialUint8array.buffer.slice(
86
+ potentialUint8array.byteOffset,
87
+ potentialUint8array.byteOffset + potentialUint8array.byteLength
88
+ )
89
+ );
90
+ }
91
+
92
+ if (
93
+ stringTag === 'ArrayBuffer' ||
94
+ stringTag === 'SharedArrayBuffer' ||
95
+ stringTag === '[object ArrayBuffer]' ||
96
+ stringTag === '[object SharedArrayBuffer]'
97
+ ) {
98
+ return new Uint8Array(potentialUint8array);
99
+ }
100
+
101
+ throw new BSONError(`Cannot make a Uint8Array from ${String(potentialUint8array)}`);
102
+ },
103
+
104
+ allocate(size: number): Uint8Array {
105
+ if (typeof size !== 'number') {
106
+ throw new TypeError(`The "size" argument must be of type number. Received ${String(size)}`);
107
+ }
108
+ return new Uint8Array(size);
109
+ },
110
+
111
+ equals(a: Uint8Array, b: Uint8Array): boolean {
112
+ if (a.byteLength !== b.byteLength) {
113
+ return false;
114
+ }
115
+ for (let i = 0; i < a.byteLength; i++) {
116
+ if (a[i] !== b[i]) {
117
+ return false;
118
+ }
119
+ }
120
+ return true;
121
+ },
122
+
123
+ fromNumberArray(array: number[]): Uint8Array {
124
+ return Uint8Array.from(array);
125
+ },
126
+
127
+ fromBase64(base64: string): Uint8Array {
128
+ return Uint8Array.from(atob(base64), c => c.charCodeAt(0));
129
+ },
130
+
131
+ toBase64(uint8array: Uint8Array): string {
132
+ return btoa(webByteUtils.toISO88591(uint8array));
133
+ },
134
+
135
+ /** **Legacy** binary strings are an outdated method of data transfer. Do not add public API support for interpreting this format */
136
+ fromISO88591(codePoints: string): Uint8Array {
137
+ return Uint8Array.from(codePoints, c => c.charCodeAt(0) & 0xff);
138
+ },
139
+
140
+ /** **Legacy** binary strings are an outdated method of data transfer. Do not add public API support for interpreting this format */
141
+ toISO88591(uint8array: Uint8Array): string {
142
+ return Array.from(Uint16Array.from(uint8array), b => String.fromCharCode(b)).join('');
143
+ },
144
+
145
+ fromHex(hex: string): Uint8Array {
146
+ const evenLengthHex = hex.length % 2 === 0 ? hex : hex.slice(0, hex.length - 1);
147
+ const buffer = [];
148
+
149
+ for (let i = 0; i < evenLengthHex.length; i += 2) {
150
+ const firstDigit = evenLengthHex[i];
151
+ const secondDigit = evenLengthHex[i + 1];
152
+
153
+ if (!HEX_DIGIT.test(firstDigit)) {
154
+ break;
155
+ }
156
+ if (!HEX_DIGIT.test(secondDigit)) {
157
+ break;
158
+ }
159
+
160
+ const hexDigit = Number.parseInt(`${firstDigit}${secondDigit}`, 16);
161
+ buffer.push(hexDigit);
162
+ }
163
+
164
+ return Uint8Array.from(buffer);
165
+ },
166
+
167
+ toHex(uint8array: Uint8Array): string {
168
+ return Array.from(uint8array, byte => byte.toString(16).padStart(2, '0')).join('');
169
+ },
170
+
171
+ fromUTF8(text: string): Uint8Array {
172
+ return new TextEncoder().encode(text);
173
+ },
174
+
175
+ toUTF8(uint8array: Uint8Array, start: number, end: number): string {
176
+ return new TextDecoder('utf8', { fatal: false }).decode(uint8array.slice(start, end));
177
+ },
178
+
179
+ utf8ByteLength(input: string): number {
180
+ return webByteUtils.fromUTF8(input).byteLength;
181
+ },
182
+
183
+ encodeUTF8Into(buffer: Uint8Array, source: string, byteOffset: number): number {
184
+ const bytes = webByteUtils.fromUTF8(source);
185
+ buffer.set(bytes, byteOffset);
186
+ return bytes.byteLength;
187
+ },
188
+
189
+ randomBytes: webRandomBytes
190
+ };
package/src/utils.ts CHANGED
@@ -1,8 +1,10 @@
1
- import { ObjectId } from "bson"
2
1
  import { CalendarEvent, CompoundFilter, Enduser, EnduserRelationship, FormResponseAnswerNumber, FormResponseValue, FormResponseValueAnswer, ManagedContentRecord, MedicationResponse, Organization, Purchase, RoundRobinAssignmentInfo, TableInputCell, Ticket, Timezone, USA_STATE_TO_TIMEZONE, User, UserActivityInfo, UserActivityStatus } from "@tellescope/types-models"
3
2
  import { ADMIN_ROLE, get_inverse_relationship_type } from "@tellescope/constants"
4
3
  import sanitizeHtml from 'sanitize-html';
5
4
 
5
+ import { ObjectId } from "./ObjectId/objectid";
6
+ export { ObjectId }
7
+
6
8
  export type Indexable<T=any> = { [index: string]: T }
7
9
 
8
10
  export const user_is_admin = (u: { id: string, roles?: string[] } & ({ type: 'user' } | { type: 'enduser' })) =>