@celljs/core 3.0.0 → 3.1.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 (140) hide show
  1. package/lib/browser/application/frontend-application-state.js +2 -7
  2. package/lib/browser/application/frontend-application-state.js.map +1 -1
  3. package/lib/browser/application/frontend-application.js +8 -16
  4. package/lib/browser/application/frontend-application.js.map +1 -1
  5. package/lib/browser/application/index.js +3 -16
  6. package/lib/browser/application/index.js.map +1 -1
  7. package/lib/browser/index.js +4 -17
  8. package/lib/browser/index.js.map +1 -1
  9. package/lib/browser/shell/application-shell.js +2 -7
  10. package/lib/browser/shell/application-shell.js.map +1 -1
  11. package/lib/browser/shell/index.js +3 -16
  12. package/lib/browser/shell/index.js.map +1 -1
  13. package/lib/common/annotation/aspect.js +1 -1
  14. package/lib/common/annotation/aspect.js.map +1 -1
  15. package/lib/common/annotation/autowired.js +1 -1
  16. package/lib/common/annotation/autowired.js.map +1 -1
  17. package/lib/common/annotation/component.js +7 -2
  18. package/lib/common/annotation/component.js.map +1 -1
  19. package/lib/common/annotation/detached/index.js +4 -17
  20. package/lib/common/annotation/detached/index.js.map +1 -1
  21. package/lib/common/annotation/index.js +18 -31
  22. package/lib/common/annotation/index.js.map +1 -1
  23. package/lib/common/annotation/value.js +1 -1
  24. package/lib/common/annotation/value.js.map +1 -1
  25. package/lib/common/aop/aop-proxy-factory.js +2 -7
  26. package/lib/common/aop/aop-proxy-factory.js.map +1 -1
  27. package/lib/common/aop/class-filter.js +2 -7
  28. package/lib/common/aop/class-filter.js.map +1 -1
  29. package/lib/common/aop/index.js +4 -17
  30. package/lib/common/aop/index.js.map +1 -1
  31. package/lib/common/application/application-protocol.js +5 -13
  32. package/lib/common/application/application-protocol.js.map +1 -1
  33. package/lib/common/application/application-state.js +2 -7
  34. package/lib/common/application/application-state.js.map +1 -1
  35. package/lib/common/application/application.js +4 -12
  36. package/lib/common/application/application.js.map +1 -1
  37. package/lib/common/application/index.js +3 -16
  38. package/lib/common/application/index.js.map +1 -1
  39. package/lib/common/config/config-provider.js +4 -12
  40. package/lib/common/config/config-provider.js.map +1 -1
  41. package/lib/common/config/index.js +4 -17
  42. package/lib/common/config/index.js.map +1 -1
  43. package/lib/common/container/index.js +6 -19
  44. package/lib/common/container/index.js.map +1 -1
  45. package/lib/common/el/core-context-initializer.js +5 -13
  46. package/lib/common/el/core-context-initializer.js.map +1 -1
  47. package/lib/common/el/expression-compiler.js +4 -12
  48. package/lib/common/el/expression-compiler.js.map +1 -1
  49. package/lib/common/el/expression-context-provider.js +5 -16
  50. package/lib/common/el/expression-context-provider.js.map +1 -1
  51. package/lib/common/el/expression-handler.js +8 -16
  52. package/lib/common/el/expression-handler.js.map +1 -1
  53. package/lib/common/el/index.js +7 -20
  54. package/lib/common/el/index.js.map +1 -1
  55. package/lib/common/el/jexl-engine-provider.js +2 -7
  56. package/lib/common/el/jexl-engine-provider.js.map +1 -1
  57. package/lib/common/error/{cutom-error.d.ts → custom-error.d.ts} +1 -1
  58. package/lib/common/error/custom-error.d.ts.map +1 -0
  59. package/lib/common/error/{cutom-error.js → custom-error.js} +1 -1
  60. package/lib/common/error/custom-error.js.map +1 -0
  61. package/lib/common/error/error-protocol.d.ts +1 -1
  62. package/lib/common/error/error-protocol.d.ts.map +1 -1
  63. package/lib/common/error/illegal-argument-error.d.ts +1 -1
  64. package/lib/common/error/illegal-argument-error.d.ts.map +1 -1
  65. package/lib/common/error/illegal-argument-error.js +2 -2
  66. package/lib/common/error/illegal-argument-error.js.map +1 -1
  67. package/lib/common/error/illegal-state-error.d.ts +4 -0
  68. package/lib/common/error/illegal-state-error.d.ts.map +1 -0
  69. package/lib/common/error/illegal-state-error.js +8 -0
  70. package/lib/common/error/illegal-state-error.js.map +1 -0
  71. package/lib/common/error/index.d.ts +3 -1
  72. package/lib/common/error/index.d.ts.map +1 -1
  73. package/lib/common/error/index.js +6 -17
  74. package/lib/common/error/index.js.map +1 -1
  75. package/lib/common/error/invalid-mime-type-error.d.ts +6 -0
  76. package/lib/common/error/invalid-mime-type-error.d.ts.map +1 -0
  77. package/lib/common/error/invalid-mime-type-error.js +12 -0
  78. package/lib/common/error/invalid-mime-type-error.js.map +1 -0
  79. package/lib/common/index.js +12 -25
  80. package/lib/common/index.js.map +1 -1
  81. package/lib/common/logger/abstract-logger.js +5 -13
  82. package/lib/common/logger/abstract-logger.js.map +1 -1
  83. package/lib/common/logger/index.js +4 -17
  84. package/lib/common/logger/index.js.map +1 -1
  85. package/lib/common/logger/logger.js +2 -7
  86. package/lib/common/logger/logger.js.map +1 -1
  87. package/lib/common/provider/component-filter-registry.js +5 -16
  88. package/lib/common/provider/component-filter-registry.js.map +1 -1
  89. package/lib/common/provider/index.js +6 -19
  90. package/lib/common/provider/index.js.map +1 -1
  91. package/lib/common/provider/provider-creator.js +2 -7
  92. package/lib/common/provider/provider-creator.js.map +1 -1
  93. package/lib/common/test/index.js +2 -15
  94. package/lib/common/test/index.js.map +1 -1
  95. package/lib/common/test/mock-logger.js +2 -7
  96. package/lib/common/test/mock-logger.js.map +1 -1
  97. package/lib/common/utils/assert.d.ts +174 -0
  98. package/lib/common/utils/assert.d.ts.map +1 -0
  99. package/lib/common/utils/assert.js +287 -0
  100. package/lib/common/utils/assert.js.map +1 -0
  101. package/lib/common/utils/index.d.ts +3 -0
  102. package/lib/common/utils/index.d.ts.map +1 -1
  103. package/lib/common/utils/index.js +20 -30
  104. package/lib/common/utils/index.js.map +1 -1
  105. package/lib/common/utils/mime-type-util.d.ts +37 -0
  106. package/lib/common/utils/mime-type-util.d.ts.map +1 -0
  107. package/lib/common/utils/mime-type-util.js +172 -0
  108. package/lib/common/utils/mime-type-util.js.map +1 -0
  109. package/lib/common/utils/mime-type.d.ts +33 -0
  110. package/lib/common/utils/mime-type.d.ts.map +1 -0
  111. package/lib/common/utils/mime-type.js +262 -0
  112. package/lib/common/utils/mime-type.js.map +1 -0
  113. package/lib/common/utils/os.d.ts +2 -2
  114. package/lib/common/utils/os.js +2 -2
  115. package/lib/node/application/backend-application-state.js +2 -7
  116. package/lib/node/application/backend-application-state.js.map +1 -1
  117. package/lib/node/application/backend-application.js +4 -12
  118. package/lib/node/application/backend-application.js.map +1 -1
  119. package/lib/node/application/index.js +3 -16
  120. package/lib/node/application/index.js.map +1 -1
  121. package/lib/node/context/index.js +2 -15
  122. package/lib/node/context/index.js.map +1 -1
  123. package/lib/node/index.js +4 -17
  124. package/lib/node/index.js.map +1 -1
  125. package/lib/node/utils/index.js +2 -15
  126. package/lib/node/utils/index.js.map +1 -1
  127. package/package.json +3 -3
  128. package/src/common/error/error-protocol.ts +1 -1
  129. package/src/common/error/illegal-argument-error.ts +1 -1
  130. package/src/common/error/illegal-state-error.ts +5 -0
  131. package/src/common/error/index.ts +3 -1
  132. package/src/common/error/invalid-mime-type-error.ts +7 -0
  133. package/src/common/utils/assert.ts +300 -0
  134. package/src/common/utils/index.ts +3 -0
  135. package/src/common/utils/mime-type-util.ts +180 -0
  136. package/src/common/utils/mime-type.ts +276 -0
  137. package/src/common/utils/os.ts +2 -2
  138. package/lib/common/error/cutom-error.d.ts.map +0 -1
  139. package/lib/common/error/cutom-error.js.map +0 -1
  140. /package/src/common/error/{cutom-error.ts → custom-error.ts} +0 -0
@@ -0,0 +1,300 @@
1
+ import { IllegalStateError } from '../error/illegal-state-error';
2
+ import { getSuperClasses } from './class-util';
3
+
4
+ /* eslint-disable @typescript-eslint/no-explicit-any */
5
+ /* eslint-disable no-null/no-null */
6
+
7
+ export abstract class Assert {
8
+ /**
9
+ * Assert a boolean expression, throwing an `IllegalStateError`
10
+ * if the expression evaluates to `false`.
11
+ * @param expression a boolean expression
12
+ * @param message the exception message to use if the assertion fails
13
+ * @throws IllegalStateError if `expression` is `false`
14
+ * @example
15
+ * Assert.state(id === undefined, "The id property must not already be initialized");
16
+ * Assert.state(entity.getId() === undefined,
17
+ * () => "ID for entity " + entity.getName() + " must not already be initialized");
18
+ */
19
+ static state(expression: boolean, message: string | (() => string)): void {
20
+ if (!expression) {
21
+ throw new IllegalStateError(this.resolveMessage(message));
22
+ }
23
+ }
24
+
25
+ /**
26
+ * Assert a boolean expression, throwing an `IllegalStateError`
27
+ * if the expression evaluates to `false`.
28
+ * @param expression a boolean expression
29
+ * @param message the exception message to use if the assertion fails
30
+ * @throws IllegalStateError if `expression` is `false`
31
+ * @example
32
+ * Assert.isTrue(i > 0, "The value must be greater than zero");
33
+ * Assert.isTrue(i > 0, () => "The value '" + i + "' must be greater than zero");
34
+ */
35
+ static isTrue(expression: boolean, message: string | (() => string)): void {
36
+ if (!expression) {
37
+ throw new IllegalStateError(this.resolveMessage(message));
38
+ }
39
+ }
40
+
41
+ /**
42
+ * Assert that an object is `undefined`.
43
+ * @param object the object to check
44
+ * @param message the exception message to use if the assertion fails
45
+ * @throws IllegalStateError if the object is not `undefined`
46
+ * @example
47
+ * Assert.isNull(value, "The value must be null");
48
+ * Assert.isNull(value, () => "The value '" + value + "' must be null");
49
+ */
50
+ static isNull(object: any, message: string | (() => string)): void {
51
+ if (object !== undefined || object !== null) {
52
+ throw new IllegalStateError(this.resolveMessage(message));
53
+ }
54
+ }
55
+
56
+ /**
57
+ * Assert that an object is not `undefined`.
58
+ * @param object the object to check
59
+ * @param message the exception message to use if the assertion fails
60
+ * @throws IllegalStateError if the object is `undefined`
61
+ * @example
62
+ * Assert.notNull(clazz, "The class must not be null");
63
+ * Assert.notNull(entity.getId(),
64
+ * () => "ID for entity " + entity.getName() + " must not be null");
65
+ */
66
+ static notNull(object: unknown, message: string | (() => string)): void {
67
+ if (object === undefined || object === null) {
68
+ throw new IllegalStateError(this.resolveMessage(message));
69
+ }
70
+ }
71
+
72
+ /**
73
+ * Assert that the given String is not empty; that is,
74
+ * it must not be `undefined` and not the empty String.
75
+ * @param text the String to check
76
+ * @param message the exception message to use if the assertion fails
77
+ * @throws IllegalStateError if the text is empty
78
+ * @example
79
+ * Assert.hasLength(name, "Name must not be empty");
80
+ * Assert.hasLength(name, () => "Name for account '" + account.getId() + "' must not be empty");
81
+ */
82
+ static hasLength(text: string | undefined, message: string | (() => string)): void {
83
+ if (!text || text.length === 0) {
84
+ throw new IllegalStateError(this.resolveMessage(message));
85
+ }
86
+ }
87
+
88
+ /**
89
+ * Assert that the given String contains valid text content; that is, it must not
90
+ * be `undefined` and must contain at least one non-whitespace character.
91
+ * @param text the String to check
92
+ * @param message the exception message to use if the assertion fails
93
+ * @throws IllegalStateError if the text does not contain valid text content
94
+ * @example
95
+ * Assert.hasText(account.getName(), "Name must not be empty");
96
+ * Assert.hasText(account.getName(),
97
+ * () => "Name for account '" + account.getId() + "' must not be empty");
98
+ */
99
+ static hasText(text: string | undefined, message: string | (() => string)): void {
100
+ if (!text || text.trim().length === 0) {
101
+ throw new IllegalStateError(this.resolveMessage(message));
102
+ }
103
+ }
104
+
105
+ /**
106
+ * Assert that the given text does not contain the given substring.
107
+ * @param textToSearch the text to search
108
+ * @param substring the substring to find within the text
109
+ * @param message the exception message to use if the assertion fails
110
+ * @throws IllegalStateError if the text contains the substring
111
+ * @example
112
+ * Assert.doesNotContain(name, forbidden, "Name must not contain '" + forbidden + "'");
113
+ * Assert.doesNotContain(name, forbidden,
114
+ * () => "Name must not contain '" + forbidden + "'");
115
+ */
116
+ static doesNotContain(textToSearch: string | undefined, substring: string, message: string | (() => string)): void {
117
+ if (textToSearch && substring && textToSearch.includes(substring)) {
118
+ throw new IllegalStateError(this.resolveMessage(message));
119
+ }
120
+ }
121
+
122
+ /**
123
+ * Assert that an array contains elements; that is, it must not be
124
+ * `undefined` and must contain at least one element.
125
+ * @param array the array to check
126
+ * @param message the exception message to use if the assertion fails
127
+ * @throws IllegalStateError if the object array is `undefined` or contains no elements
128
+ * @example
129
+ * Assert.notEmpty(array, "The array must contain elements");
130
+ * Assert.notEmpty(array, () => "The " + arrayType + " array must contain elements");
131
+ */
132
+ static notEmpty(array: unknown[] | undefined, message: string | (() => string)): void {
133
+ if (!array || array.length === 0) {
134
+ throw new IllegalStateError(this.resolveMessage(message));
135
+ }
136
+ }
137
+
138
+ /**
139
+ * Assert that an array contains no `undefined` elements.
140
+ * <p>Note: Does not complain if the array is empty!
141
+ * @param array the array to check
142
+ * @param message the exception message to use if the assertion fails
143
+ * @throws IllegalStateError if the object array contains a `undefined` element
144
+ * @example
145
+ * Assert.noNullElements(array, "The array must contain non-null elements");
146
+ * Assert.noNullElements(array, () => "The " + arrayType + " array must contain non-null elements");
147
+ */
148
+ static noNullElements(array: unknown[] | undefined, message: string | (() => string)): void {
149
+ if (array) {
150
+ for (const element of array) {
151
+ if (element === undefined || element === null) {
152
+ throw new IllegalStateError(this.resolveMessage(message));
153
+ }
154
+ }
155
+ }
156
+ }
157
+
158
+ /**
159
+ * Assert that a collection contains elements; that is, it must not be
160
+ * `undefined` and must contain at least one element.
161
+ * @param collection the collection to check
162
+ * @param message the exception message to use if the assertion fails
163
+ * @throws IllegalStateError if the collection is `undefined` or
164
+ * contains no elements
165
+ * @example
166
+ * Assert.notEmpty(collection, "Collection must contain elements");
167
+ * Assert.notEmpty(collection, () => "The " + collectionType + " collection must contain elements");
168
+ */
169
+ static notEmptyCollection(collection: unknown[] | undefined, message: string | (() => string)): void {
170
+ if (!collection || collection.length === 0) {
171
+ throw new IllegalStateError(this.resolveMessage(message));
172
+ }
173
+ }
174
+
175
+ /**
176
+ * Assert that a collection contains no `undefined` elements.
177
+ * <p>Note: Does not complain if the collection is empty!
178
+ * @param collection the collection to check
179
+ * @param message the exception message to use if the assertion fails
180
+ * @throws IllegalStateError if the collection contains a `undefined`
181
+ * @example
182
+ * Assert.noNullElements(collection, "Collection must contain non-null elements");
183
+ * Assert.noNullElements(collection, () => "The " + collectionName + " must contain non-null elements");
184
+ */
185
+ static noNullElementsCollection(collection: unknown[] | undefined, message: string | (() => string)): void {
186
+ if (collection) {
187
+ for (const element of collection) {
188
+ if (element === undefined || element === null) {
189
+ throw new IllegalStateError(this.resolveMessage(message));
190
+ }
191
+ }
192
+ }
193
+ }
194
+
195
+ /**
196
+ * Assert that a Map contains entries; that is, it must not be `undefined`
197
+ * and must contain at least one entry.
198
+ * @param map the map to check
199
+ * @param message the exception message to use if the assertion fails
200
+ * @throws IllegalStateError if the map is `undefined` or contains no entries
201
+ * @example
202
+ * Assert.notEmpty(map, "The map must contain entries");
203
+ * Assert.notEmpty(map, () => "The " + mapType + " map must contain entries");
204
+ */
205
+ static notEmptyMap(map: Map<unknown, unknown> | undefined, message: string | (() => string)): void {
206
+ if (!map || map.size === 0) {
207
+ throw new IllegalStateError(this.resolveMessage(message));
208
+ }
209
+ }
210
+
211
+ /**
212
+ * Assert that the provided object is an instance of the provided class.
213
+ * @param type the type to check against
214
+ * @param obj the object to check
215
+ * @param message a message which will be prepended to provide further context.
216
+ * If it is empty or ends in ":" or ";" or "," or ".", a full exception message
217
+ * will be appended. If it ends in a space, the name of the offending object's
218
+ * type will be appended. In any other case, a ":" with a space and the name
219
+ * of the offending object's type will be appended.
220
+ * @throws IllegalStateError if the object is not an instance of type
221
+ * @example
222
+ * Assert.instanceOf(Foo, foo, "Foo expected");
223
+ * Assert.instanceOf(Foo, foo, () => "Processing " + Foo.name + ":");
224
+ * Assert.instanceOf(Foo, foo);
225
+ */
226
+ static isInstanceOf(type: any, obj: unknown, message?: string | (() => string)): void {
227
+ this.notNull(type, 'Type to check against must not be null');
228
+ if (!(obj instanceof type)) {
229
+ this.instanceCheckFailed(type, obj, this.resolveMessage(message));
230
+ }
231
+ }
232
+
233
+ /**
234
+ * Assert that `superType.isAssignableFrom(subType)` is `true`.
235
+ * @param superType the supertype to check against
236
+ * @param subType the subtype to check
237
+ * @param message a message which will be prepended to provide further context.
238
+ * If it is empty or ends in ":" or ";" or "," or ".", a full exception message
239
+ * will be appended. If it ends in a space, the name of the offending subtype
240
+ * will be appended. In any other case, a ":" with a space and the name of the
241
+ * offending subtype will be appended.
242
+ * @throws IllegalStateError if the classes are not assignable
243
+ * @example
244
+ * Assert.isAssignable(Number, myClass, "Number expected");
245
+ * Assert.isAssignable(Number, myClass, () => "Processing " + myAttributeName + ":");
246
+ */
247
+ static isAssignable(superType: unknown, subType: any | undefined, message?: string | (() => string)): void {
248
+ this.notNull(superType, 'Supertype to check against must not be null');
249
+ if (subType === undefined || !getSuperClasses(subType).includes(superType)) {
250
+ this.assignableCheckFailed(superType, subType, this.resolveMessage(message));
251
+ }
252
+ }
253
+
254
+ private static instanceCheckFailed(type: unknown, obj: any | undefined, msg: string | undefined): void {
255
+ const className = (obj !== undefined ? obj.constructor.name : 'undefined');
256
+ let result = '';
257
+ let defaultMessage = true;
258
+ if (msg && msg.length > 0) {
259
+ if (this.endsWithSeparator(msg)) {
260
+ result = msg + ' ';
261
+ } else {
262
+ result = this.messageWithTypeName(msg, className);
263
+ defaultMessage = false;
264
+ }
265
+ }
266
+ if (defaultMessage) {
267
+ result = result + (`Object of class [${className}] must be an instance of ${type}`);
268
+ }
269
+ throw new IllegalStateError(result);
270
+ }
271
+
272
+ private static assignableCheckFailed(superType: unknown, subType: unknown | undefined, msg: string | undefined): void {
273
+ let result = '';
274
+ let defaultMessage = true;
275
+ if (msg && msg.length > 0) {
276
+ if (this.endsWithSeparator(msg)) {
277
+ result = msg + ' ';
278
+ } else {
279
+ result = this.messageWithTypeName(msg, subType);
280
+ defaultMessage = false;
281
+ }
282
+ }
283
+ if (defaultMessage) {
284
+ result = result + (`${subType} is not assignable to ${superType}`);
285
+ }
286
+ throw new IllegalStateError(result);
287
+ }
288
+
289
+ private static endsWithSeparator(msg: string): boolean {
290
+ return (msg.endsWith(':') || msg.endsWith(';') || msg.endsWith(',') || msg.endsWith('.'));
291
+ }
292
+
293
+ private static messageWithTypeName(msg: string, typeName: any): string {
294
+ return msg + (msg.endsWith(' ') ? '' : ': ') + ('name' in typeName) ? typeName.name : typeName;
295
+ }
296
+
297
+ private static resolveMessage(message?: (() => string) | string): string | undefined {
298
+ return typeof message === 'function' ? message() : message;
299
+ }
300
+ }
@@ -14,3 +14,6 @@ export * from './global-util';
14
14
  export * from './async';
15
15
  export * from './url-util';
16
16
  export * from './uuid';
17
+ export * from './assert';
18
+ export * from './mime-type-util';
19
+ export * from './mime-type';
@@ -0,0 +1,180 @@
1
+ import { MimeType } from './mime-type';
2
+ import { InvalidMimeTypeError } from '../error';
3
+
4
+ export abstract class MimeTypeUtils {
5
+ private static readonly BOUNDARY_CHARS: string[] =
6
+ ['-', '_', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
7
+ 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A',
8
+ 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U',
9
+ 'V', 'W', 'X', 'Y', 'Z'];
10
+
11
+ static readonly ALL: MimeType = new MimeType('*', '*');
12
+ static readonly ALL_VALUE: string = '*/*';
13
+ static readonly APPLICATION_GRAPHQL: MimeType = new MimeType('application', 'graphql+json');
14
+ static readonly APPLICATION_GRAPHQL_VALUE: string = 'application/graphql+json';
15
+ static readonly APPLICATION_JSON: MimeType = new MimeType('application', 'json');
16
+ static readonly APPLICATION_JSON_VALUE: string = 'application/json';
17
+ static readonly APPLICATION_OCTET_STREAM: MimeType = new MimeType('application', 'octet-stream');
18
+ static readonly APPLICATION_OCTET_STREAM_VALUE: string = 'application/octet-stream';
19
+ static readonly APPLICATION_XML: MimeType = new MimeType('application', 'xml');
20
+ static readonly APPLICATION_XML_VALUE: string = 'application/xml';
21
+ static readonly IMAGE_GIF: MimeType = new MimeType('image', 'gif');
22
+ static readonly IMAGE_GIF_VALUE: string = 'image/gif';
23
+ static readonly IMAGE_JPEG: MimeType = new MimeType('image', 'jpeg');
24
+ static readonly IMAGE_JPEG_VALUE: string = 'image/jpeg';
25
+ static readonly IMAGE_PNG: MimeType = new MimeType('image', 'png');
26
+ static readonly IMAGE_PNG_VALUE: string = 'image/png';
27
+ static readonly TEXT_HTML: MimeType = new MimeType('text', 'html');
28
+ static readonly TEXT_HTML_VALUE: string = 'text/html';
29
+ static readonly TEXT_PLAIN: MimeType = new MimeType('text', 'plain');
30
+ static readonly TEXT_PLAIN_VALUE: string = 'text/plain';
31
+ static readonly TEXT_XML: MimeType = new MimeType('text', 'xml');
32
+ static readonly TEXT_XML_VALUE: string = 'text/xml';
33
+
34
+ private static cachedMimeTypes: Map<string, MimeType> = new Map();
35
+
36
+ static parseMimeType(mimeType: string): MimeType {
37
+ if (!mimeType) {
38
+ throw new InvalidMimeTypeError(mimeType, '"mimeType" must not be empty');
39
+ }
40
+ if (mimeType.startsWith('multipart')) {
41
+ return this.parseMimeTypeInternal(mimeType);
42
+ }
43
+ if (!this.cachedMimeTypes.has(mimeType)) {
44
+ this.cachedMimeTypes.set(mimeType, this.parseMimeTypeInternal(mimeType));
45
+ }
46
+ return this.cachedMimeTypes.get(mimeType)!;
47
+ }
48
+
49
+ private static parseMimeTypeInternal(mimeType: string): MimeType {
50
+ let index = mimeType.indexOf(';');
51
+ let fullType = (index >= 0 ? mimeType.substring(0, index) : mimeType).trim();
52
+ if (fullType.length === 0) {
53
+ throw new InvalidMimeTypeError(mimeType, '"mimeType" must not be empty');
54
+ }
55
+
56
+ if (fullType === '*') {
57
+ fullType = '*/*';
58
+ }
59
+ const subIndex = fullType.indexOf('/');
60
+ if (subIndex === -1) {
61
+ throw new InvalidMimeTypeError(mimeType, 'does not contain "/"');
62
+ }
63
+ if (subIndex === fullType.length - 1) {
64
+ throw new InvalidMimeTypeError(mimeType, 'does not contain subtype after "/"');
65
+ }
66
+ const type = fullType.substring(0, subIndex);
67
+ const subtype = fullType.substring(subIndex + 1);
68
+ if (type === '*' && subtype !== '*') {
69
+ throw new InvalidMimeTypeError(mimeType, 'wildcard type is legal only in "*/*" (all mime types)');
70
+ }
71
+
72
+ let parameters: Map<string, string> | undefined = undefined;
73
+ do {
74
+ let nextIndex = index + 1;
75
+ let quoted = false;
76
+ while (nextIndex < mimeType.length) {
77
+ const ch = mimeType.charAt(nextIndex);
78
+ if (ch === ';') {
79
+ if (!quoted) {
80
+ break;
81
+ }
82
+ } else if (ch === '"') {
83
+ quoted = !quoted;
84
+ }
85
+ nextIndex++;
86
+ }
87
+ const parameter = mimeType.substring(index + 1, nextIndex).trim();
88
+ if (parameter.length > 0) {
89
+ if (parameters === undefined) {
90
+ parameters = new Map();
91
+ }
92
+ const eqIndex = parameter.indexOf('=');
93
+ if (eqIndex >= 0) {
94
+ const attribute = parameter.substring(0, eqIndex).trim();
95
+ const value = parameter.substring(eqIndex + 1).trim();
96
+ parameters.set(attribute, value);
97
+ }
98
+ }
99
+ index = nextIndex;
100
+ } while (index < mimeType.length);
101
+
102
+ try {
103
+ return new MimeType(type, subtype, parameters);
104
+ } catch (ex) {
105
+ throw new InvalidMimeTypeError(mimeType, ex.message);
106
+ }
107
+ }
108
+
109
+ static parseMimeTypes(mimeTypes: string): MimeType[] {
110
+ if (!mimeTypes) {
111
+ return [];
112
+ }
113
+ return this.tokenize(mimeTypes).filter(mimeType => !!mimeType).map(this.parseMimeType);
114
+ }
115
+
116
+ static tokenize(mimeTypes: string): string[] {
117
+ if (!mimeTypes) {
118
+ return [];
119
+ }
120
+ const tokens: string[] = [];
121
+ let inQuotes = false;
122
+ let startIndex = 0;
123
+ let i = 0;
124
+ while (i < mimeTypes.length) {
125
+ switch (mimeTypes.charAt(i)) {
126
+ case '"':
127
+ inQuotes = !inQuotes;
128
+ break;
129
+ case ',':
130
+ if (!inQuotes) {
131
+ tokens.push(mimeTypes.substring(startIndex, i));
132
+ startIndex = i + 1;
133
+ }
134
+ break;
135
+ case '\\':
136
+ i++;
137
+ break;
138
+ }
139
+ i++;
140
+ }
141
+ tokens.push(mimeTypes.substring(startIndex));
142
+ return tokens;
143
+ }
144
+
145
+ static toString(mimeTypes: MimeType[]): string {
146
+ return mimeTypes.map(mimeType => mimeType.toString()).join(', ');
147
+ }
148
+
149
+ static sortBySpecificity<T extends MimeType>(mimeTypes: T[]): void {
150
+ if (mimeTypes.length > 50) {
151
+ throw new InvalidMimeTypeError(mimeTypes.toString(), 'Too many elements');
152
+ }
153
+ this.bubbleSort(mimeTypes, (a, b) => a.isLessSpecific(b));
154
+ }
155
+
156
+ private static bubbleSort<T>(list: T[], swap: (a: T, b: T) => boolean): void {
157
+ const len = list.length;
158
+ for (let i = 0; i < len; i++) {
159
+ for (let j = 1; j < len - i; j++) {
160
+ const prev = list[j - 1];
161
+ const cur = list[j];
162
+ if (swap(prev, cur)) {
163
+ list[j] = prev;
164
+ list[j - 1] = cur;
165
+ }
166
+ }
167
+ }
168
+ }
169
+
170
+ static generateMultipartBoundary(): Uint8Array {
171
+ const boundary = new Uint8Array(Math.floor(Math.random() * 11) + 30);
172
+ for (let i = 0; i < boundary.length; i++) {
173
+ boundary[i] = this.BOUNDARY_CHARS[Math.floor(Math.random() * this.BOUNDARY_CHARS.length)].charCodeAt(0);
174
+ }
175
+ return boundary;
176
+ }
177
+ static generateMultipartBoundaryString(): string {
178
+ return new TextDecoder('ascii').decode(this.generateMultipartBoundary());
179
+ }
180
+ }