@webiny/api-opensearch 6.1.0-beta.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 (213) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +11 -0
  3. package/client.d.ts +8 -0
  4. package/client.js +70 -0
  5. package/client.js.map +1 -0
  6. package/compression.d.ts +6 -0
  7. package/compression.js +25 -0
  8. package/compression.js.map +1 -0
  9. package/cursors.d.ts +10 -0
  10. package/cursors.js +37 -0
  11. package/cursors.js.map +1 -0
  12. package/db/entity.d.ts +7 -0
  13. package/db/entity.js +20 -0
  14. package/db/entity.js.map +1 -0
  15. package/db/index.d.ts +3 -0
  16. package/db/index.js +5 -0
  17. package/db/index.js.map +1 -0
  18. package/db/table.d.ts +7 -0
  19. package/db/table.js +17 -0
  20. package/db/table.js.map +1 -0
  21. package/db/types.d.ts +6 -0
  22. package/db/types.js +3 -0
  23. package/db/types.js.map +1 -0
  24. package/exports/api/opensearch.d.ts +4 -0
  25. package/exports/api/opensearch.js +6 -0
  26. package/exports/api/opensearch.js.map +1 -0
  27. package/features/OpenSearchClient/OpenSearchClient.d.ts +12 -0
  28. package/features/OpenSearchClient/OpenSearchClient.js +16 -0
  29. package/features/OpenSearchClient/OpenSearchClient.js.map +1 -0
  30. package/features/OpenSearchClient/abstraction.d.ts +9 -0
  31. package/features/OpenSearchClient/abstraction.js +4 -0
  32. package/features/OpenSearchClient/abstraction.js.map +1 -0
  33. package/features/OpenSearchClient/feature.d.ts +1 -0
  34. package/features/OpenSearchClient/feature.js +10 -0
  35. package/features/OpenSearchClient/feature.js.map +1 -0
  36. package/features/OpenSearchClientFactory/OpenSearchClientFactory.d.ts +9 -0
  37. package/features/OpenSearchClientFactory/OpenSearchClientFactory.js +16 -0
  38. package/features/OpenSearchClientFactory/OpenSearchClientFactory.js.map +1 -0
  39. package/features/OpenSearchClientFactory/abstraction.d.ts +10 -0
  40. package/features/OpenSearchClientFactory/abstraction.js +4 -0
  41. package/features/OpenSearchClientFactory/abstraction.js.map +1 -0
  42. package/features/OpenSearchClientFactory/feature.d.ts +1 -0
  43. package/features/OpenSearchClientFactory/feature.js +10 -0
  44. package/features/OpenSearchClientFactory/feature.js.map +1 -0
  45. package/features/OpenSearchContext/OpenSearchContext.d.ts +8 -0
  46. package/features/OpenSearchContext/OpenSearchContext.js +13 -0
  47. package/features/OpenSearchContext/OpenSearchContext.js.map +1 -0
  48. package/features/OpenSearchContext/abstraction.d.ts +14 -0
  49. package/features/OpenSearchContext/abstraction.js +4 -0
  50. package/features/OpenSearchContext/abstraction.js.map +1 -0
  51. package/features/OpenSearchContext/feature.d.ts +2 -0
  52. package/features/OpenSearchContext/feature.js +13 -0
  53. package/features/OpenSearchContext/feature.js.map +1 -0
  54. package/index.d.ts +22 -0
  55. package/index.js +41 -0
  56. package/index.js.map +1 -0
  57. package/indexConfiguration/base.d.ts +6 -0
  58. package/indexConfiguration/base.js +35 -0
  59. package/indexConfiguration/base.js.map +1 -0
  60. package/indexConfiguration/common.d.ts +9 -0
  61. package/indexConfiguration/common.js +47 -0
  62. package/indexConfiguration/common.js.map +1 -0
  63. package/indexConfiguration/index.d.ts +2 -0
  64. package/indexConfiguration/index.js +4 -0
  65. package/indexConfiguration/index.js.map +1 -0
  66. package/indexPrefix.d.ts +1 -0
  67. package/indexPrefix.js +5 -0
  68. package/indexPrefix.js.map +1 -0
  69. package/indices.d.ts +8 -0
  70. package/indices.js +20 -0
  71. package/indices.js.map +1 -0
  72. package/limit.d.ts +1 -0
  73. package/limit.js +32 -0
  74. package/limit.js.map +1 -0
  75. package/normalize.d.ts +6 -0
  76. package/normalize.js +54 -0
  77. package/normalize.js.map +1 -0
  78. package/operations/OpenSearchCatHealth.d.ts +7 -0
  79. package/operations/OpenSearchCatHealth.js +31 -0
  80. package/operations/OpenSearchCatHealth.js.map +1 -0
  81. package/operations/OpenSearchCatNodes.d.ts +7 -0
  82. package/operations/OpenSearchCatNodes.js +29 -0
  83. package/operations/OpenSearchCatNodes.js.map +1 -0
  84. package/operations/index.d.ts +3 -0
  85. package/operations/index.js +5 -0
  86. package/operations/index.js.map +1 -0
  87. package/operations/stripConnectionFromException.d.ts +1 -0
  88. package/operations/stripConnectionFromException.js +17 -0
  89. package/operations/stripConnectionFromException.js.map +1 -0
  90. package/operations/types.d.ts +35 -0
  91. package/operations/types.js +8 -0
  92. package/operations/types.js.map +1 -0
  93. package/operators.d.ts +6 -0
  94. package/operators.js +9 -0
  95. package/operators.js.map +1 -0
  96. package/package.json +36 -0
  97. package/plugins/definition/OpenSearchBodyModifierPlugin.d.ts +13 -0
  98. package/plugins/definition/OpenSearchBodyModifierPlugin.js +18 -0
  99. package/plugins/definition/OpenSearchBodyModifierPlugin.js.map +1 -0
  100. package/plugins/definition/OpenSearchFieldPlugin.d.ts +81 -0
  101. package/plugins/definition/OpenSearchFieldPlugin.js +68 -0
  102. package/plugins/definition/OpenSearchFieldPlugin.js.map +1 -0
  103. package/plugins/definition/OpenSearchIndexPlugin.d.ts +10 -0
  104. package/plugins/definition/OpenSearchIndexPlugin.js +17 -0
  105. package/plugins/definition/OpenSearchIndexPlugin.js.map +1 -0
  106. package/plugins/definition/OpenSearchQueryBuilderOperatorPlugin.d.ts +10 -0
  107. package/plugins/definition/OpenSearchQueryBuilderOperatorPlugin.js +9 -0
  108. package/plugins/definition/OpenSearchQueryBuilderOperatorPlugin.js.map +1 -0
  109. package/plugins/definition/OpenSearchQueryModifierPlugin.d.ts +14 -0
  110. package/plugins/definition/OpenSearchQueryModifierPlugin.js +18 -0
  111. package/plugins/definition/OpenSearchQueryModifierPlugin.js.map +1 -0
  112. package/plugins/definition/OpenSearchSortModifierPlugin.d.ts +13 -0
  113. package/plugins/definition/OpenSearchSortModifierPlugin.js +18 -0
  114. package/plugins/definition/OpenSearchSortModifierPlugin.js.map +1 -0
  115. package/plugins/definition/index.d.ts +6 -0
  116. package/plugins/definition/index.js +8 -0
  117. package/plugins/definition/index.js.map +1 -0
  118. package/plugins/index.d.ts +2 -0
  119. package/plugins/index.js +4 -0
  120. package/plugins/index.js.map +1 -0
  121. package/plugins/operator/andIn.d.ts +7 -0
  122. package/plugins/operator/andIn.js +35 -0
  123. package/plugins/operator/andIn.js.map +1 -0
  124. package/plugins/operator/between.d.ts +7 -0
  125. package/plugins/operator/between.js +32 -0
  126. package/plugins/operator/between.js.map +1 -0
  127. package/plugins/operator/contains.d.ts +7 -0
  128. package/plugins/operator/contains.js +24 -0
  129. package/plugins/operator/contains.js.map +1 -0
  130. package/plugins/operator/equal.d.ts +7 -0
  131. package/plugins/operator/equal.js +50 -0
  132. package/plugins/operator/equal.js.map +1 -0
  133. package/plugins/operator/gt.d.ts +7 -0
  134. package/plugins/operator/gt.js +22 -0
  135. package/plugins/operator/gt.js.map +1 -0
  136. package/plugins/operator/gte.d.ts +7 -0
  137. package/plugins/operator/gte.js +22 -0
  138. package/plugins/operator/gte.js.map +1 -0
  139. package/plugins/operator/in.d.ts +7 -0
  140. package/plugins/operator/in.js +29 -0
  141. package/plugins/operator/in.js.map +1 -0
  142. package/plugins/operator/index.d.ts +15 -0
  143. package/plugins/operator/index.js +17 -0
  144. package/plugins/operator/index.js.map +1 -0
  145. package/plugins/operator/lt.d.ts +7 -0
  146. package/plugins/operator/lt.js +22 -0
  147. package/plugins/operator/lt.js.map +1 -0
  148. package/plugins/operator/lte.d.ts +7 -0
  149. package/plugins/operator/lte.js +22 -0
  150. package/plugins/operator/lte.js.map +1 -0
  151. package/plugins/operator/not.d.ts +7 -0
  152. package/plugins/operator/not.js +44 -0
  153. package/plugins/operator/not.js.map +1 -0
  154. package/plugins/operator/notBetween.d.ts +7 -0
  155. package/plugins/operator/notBetween.js +32 -0
  156. package/plugins/operator/notBetween.js.map +1 -0
  157. package/plugins/operator/notContains.d.ts +7 -0
  158. package/plugins/operator/notContains.js +24 -0
  159. package/plugins/operator/notContains.js.map +1 -0
  160. package/plugins/operator/notIn.d.ts +7 -0
  161. package/plugins/operator/notIn.js +27 -0
  162. package/plugins/operator/notIn.js.map +1 -0
  163. package/plugins/operator/notStartsWith.d.ts +7 -0
  164. package/plugins/operator/notStartsWith.js +23 -0
  165. package/plugins/operator/notStartsWith.js.map +1 -0
  166. package/plugins/operator/startsWith.d.ts +7 -0
  167. package/plugins/operator/startsWith.js +23 -0
  168. package/plugins/operator/startsWith.js.map +1 -0
  169. package/sharedIndex.d.ts +1 -0
  170. package/sharedIndex.js +5 -0
  171. package/sharedIndex.js.map +1 -0
  172. package/sort.d.ts +13 -0
  173. package/sort.js +64 -0
  174. package/sort.js.map +1 -0
  175. package/types.d.ts +179 -0
  176. package/types.js +3 -0
  177. package/types.js.map +1 -0
  178. package/utils/createIndex.d.ts +19 -0
  179. package/utils/createIndex.js +88 -0
  180. package/utils/createIndex.js.map +1 -0
  181. package/utils/index.d.ts +2 -0
  182. package/utils/index.js +4 -0
  183. package/utils/index.js.map +1 -0
  184. package/utils/waitUntilHealthy/UnhealthyClusterError.d.ts +4 -0
  185. package/utils/waitUntilHealthy/UnhealthyClusterError.js +11 -0
  186. package/utils/waitUntilHealthy/UnhealthyClusterError.js.map +1 -0
  187. package/utils/waitUntilHealthy/WaitUntilHealthy.d.ts +71 -0
  188. package/utils/waitUntilHealthy/WaitUntilHealthy.js +150 -0
  189. package/utils/waitUntilHealthy/WaitUntilHealthy.js.map +1 -0
  190. package/utils/waitUntilHealthy/WaitingHealthyClusterAbortedError.d.ts +4 -0
  191. package/utils/waitUntilHealthy/WaitingHealthyClusterAbortedError.js +11 -0
  192. package/utils/waitUntilHealthy/WaitingHealthyClusterAbortedError.js.map +1 -0
  193. package/utils/waitUntilHealthy/index.d.ts +4 -0
  194. package/utils/waitUntilHealthy/index.js +6 -0
  195. package/utils/waitUntilHealthy/index.js.map +1 -0
  196. package/utils/waitUntilHealthy/reason/ClusterHealthReason.d.ts +15 -0
  197. package/utils/waitUntilHealthy/reason/ClusterHealthReason.js +13 -0
  198. package/utils/waitUntilHealthy/reason/ClusterHealthReason.js.map +1 -0
  199. package/utils/waitUntilHealthy/reason/IReason.d.ts +4 -0
  200. package/utils/waitUntilHealthy/reason/IReason.js +3 -0
  201. package/utils/waitUntilHealthy/reason/IReason.js.map +1 -0
  202. package/utils/waitUntilHealthy/reason/MemoryReason.d.ts +14 -0
  203. package/utils/waitUntilHealthy/reason/MemoryReason.js +13 -0
  204. package/utils/waitUntilHealthy/reason/MemoryReason.js.map +1 -0
  205. package/utils/waitUntilHealthy/reason/ProcessorReason.d.ts +14 -0
  206. package/utils/waitUntilHealthy/reason/ProcessorReason.js +13 -0
  207. package/utils/waitUntilHealthy/reason/ProcessorReason.js.map +1 -0
  208. package/utils/waitUntilHealthy/reason/index.d.ts +3 -0
  209. package/utils/waitUntilHealthy/reason/index.js +5 -0
  210. package/utils/waitUntilHealthy/reason/index.js.map +1 -0
  211. package/where.d.ts +17 -0
  212. package/where.js +82 -0
  213. package/where.js.map +1 -0
package/types.d.ts ADDED
@@ -0,0 +1,179 @@
1
+ import type { Client } from "@opensearch-project/opensearch";
2
+ import type { QueryDslQueryContainer, QueryDslBoolQuery, SearchFieldSort, SearchSortOrder, SearchSort, SearchRequest } from "@opensearch-project/opensearch/api/types";
3
+ import type { Context, GenericRecord } from "@webiny/api/types.js";
4
+ export type { ApiResponse } from "@opensearch-project/opensearch";
5
+ export type { Client };
6
+ /** Replaces elastic-ts PrimitiveValue */
7
+ export type PrimitiveValue = null | number | string | boolean;
8
+ /** Replaces elastic-ts Query */
9
+ export type { QueryDslQueryContainer };
10
+ /** Replaces elastic-ts BoolQueryConfig */
11
+ export type { QueryDslBoolQuery };
12
+ /** Replaces elastic-ts FieldSortOptions */
13
+ export type FieldSortOptions = SearchFieldSort;
14
+ /** Replaces elastic-ts SortOrder. Narrowed to "asc" | "desc" (excludes "_doc"). */
15
+ export type SortOrder = "asc" | "desc";
16
+ /** Replaces elastic-ts Sort */
17
+ export type { SearchSort as Sort };
18
+ /** Single sort entry as an object map. Replaces elastic-ts SortType. */
19
+ export type SortType = Record<string, FieldSortOptions>;
20
+ /** Replaces elastic-ts SearchBody. Overrides search_after to allow PrimitiveValue[] (null | boolean | string | number). */
21
+ export type SearchBody = Omit<NonNullable<SearchRequest["body"]>, "search_after"> & {
22
+ search_after?: PrimitiveValue[];
23
+ };
24
+ export interface OpenSearchContext extends Context {
25
+ /**
26
+ * @deprecated Use `context.opensearch` instead.
27
+ */
28
+ elasticsearch: Client;
29
+ opensearch: Client;
30
+ }
31
+ export interface OpenSearchBoolQueryConfig extends QueryDslBoolQuery {
32
+ must: QueryDslQueryContainer[];
33
+ filter: QueryDslQueryContainer[];
34
+ should: QueryDslQueryContainer[];
35
+ must_not: QueryDslQueryContainer[];
36
+ }
37
+ export type OpenSearchQueryOperator = "eq" | "not" | "in" | "not_in" | "contains" | "not_contains" | "between" | "not_between" | "gt" | "gte" | "lt" | "lte" | string;
38
+ export interface OpenSearchQueryBuilderArgsPlugin {
39
+ name: string;
40
+ path: string;
41
+ basePath: string;
42
+ value: any;
43
+ keyword: boolean;
44
+ }
45
+ export interface OpenSearchSearchResponseHit<T> {
46
+ _index: string;
47
+ _type: string;
48
+ _id: string;
49
+ _score: number | null;
50
+ _source: T;
51
+ sort: PrimitiveValue[];
52
+ }
53
+ export interface OpenSearchSearchResponseAggregationBucket<T> {
54
+ key: T;
55
+ doc_count: number;
56
+ }
57
+ export interface OpenSearchSearchResponseBodyHits<T> {
58
+ hits: OpenSearchSearchResponseHit<T>[];
59
+ total: {
60
+ value: number;
61
+ };
62
+ }
63
+ export interface OpenSearchSearchResponseBodyAggregations<T> {
64
+ [key: string]: {
65
+ buckets: OpenSearchSearchResponseAggregationBucket<T>[];
66
+ };
67
+ }
68
+ export interface OpenSearchSearchResponseBody<T> {
69
+ hits: OpenSearchSearchResponseBodyHits<T>;
70
+ aggregations: OpenSearchSearchResponseBodyAggregations<T>;
71
+ }
72
+ export interface OpenSearchSearchResponse<T = GenericRecord> {
73
+ body: OpenSearchSearchResponseBody<T>;
74
+ }
75
+ export interface OpenSearchIndexRequestBodyMappingsDynamicTemplate {
76
+ [key: string]: {
77
+ path_match?: string;
78
+ path_unmatch?: string;
79
+ match_mapping_type?: string;
80
+ match?: string;
81
+ unmatch?: string;
82
+ mapping?: {
83
+ numeric_detection?: boolean;
84
+ date_detection?: boolean;
85
+ type?: string;
86
+ search_analyzer?: string;
87
+ analyzer?: string;
88
+ fields?: {
89
+ [key: string]: {
90
+ type: string;
91
+ search_analyzer?: string;
92
+ analyzer?: string;
93
+ ignore_above?: number;
94
+ [key: string]: any;
95
+ } | undefined;
96
+ };
97
+ [key: string]: any;
98
+ };
99
+ [key: string]: any;
100
+ };
101
+ }
102
+ export interface OpenSearchIndexRequestBody {
103
+ settings?: {
104
+ index?: {
105
+ analysis?: {
106
+ [key: string]: any;
107
+ };
108
+ number_of_shards?: number;
109
+ number_of_routing_shards?: number;
110
+ codec?: string;
111
+ soft_deletes?: {
112
+ enabled?: boolean;
113
+ retention_lease?: {
114
+ period?: string;
115
+ };
116
+ };
117
+ number_of_replicas?: number;
118
+ auto_expand_replicas?: string | "all" | false;
119
+ refresh_interval?: string;
120
+ max_result_window?: number;
121
+ max_inner_result_window?: number;
122
+ max_rescore_window?: number;
123
+ max_script_fields?: number;
124
+ max_ngram_diff?: number;
125
+ max_shingle_diff?: number;
126
+ max_terms_count?: number;
127
+ max_regex_length?: number;
128
+ routing?: {
129
+ allocation?: {
130
+ enable?: "all" | "primaries" | "new_primaries" | "none";
131
+ };
132
+ rebalance?: {
133
+ enable?: "all" | "primaries" | "new_primaries" | "none";
134
+ };
135
+ };
136
+ hidden?: boolean;
137
+ total_fields?: {
138
+ limit?: number;
139
+ };
140
+ [key: string]: any;
141
+ };
142
+ };
143
+ mappings: {
144
+ numeric_detection?: boolean;
145
+ dynamic_templates?: OpenSearchIndexRequestBodyMappingsDynamicTemplate[];
146
+ properties?: {
147
+ [key: string]: {
148
+ analyzer?: string;
149
+ type?: string;
150
+ normalizer?: string;
151
+ index?: string;
152
+ fields?: {
153
+ [key: string]: {
154
+ type: string;
155
+ ignore_above?: number;
156
+ search_analyzer?: string;
157
+ analyzer?: string;
158
+ [key: string]: any;
159
+ };
160
+ };
161
+ [key: string]: any;
162
+ };
163
+ };
164
+ [key: string]: any;
165
+ };
166
+ aliases?: {
167
+ [key: string]: {
168
+ filter?: {
169
+ [key: string]: any;
170
+ };
171
+ index_routing?: string;
172
+ is_hidden?: boolean;
173
+ is_write_index?: boolean;
174
+ routing?: string;
175
+ search_routing?: string;
176
+ };
177
+ };
178
+ }
179
+ export type { SearchSortOrder };
package/types.js ADDED
@@ -0,0 +1,3 @@
1
+ export {};
2
+
3
+ //# sourceMappingURL=types.js.map
package/types.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"names":[],"sources":["types.ts"],"sourcesContent":["import type { Client } from \"@opensearch-project/opensearch\";\nimport type {\n QueryDslQueryContainer,\n QueryDslBoolQuery,\n SearchFieldSort,\n SearchSortOrder,\n SearchSort,\n SearchRequest\n} from \"@opensearch-project/opensearch/api/types\";\nimport type { Context, GenericRecord } from \"@webiny/api/types.js\";\n\nexport type { ApiResponse } from \"@opensearch-project/opensearch\";\n\nexport type { Client };\n\n// ---------------------------------------------------------------------------\n// Local type aliases replacing elastic-ts\n// ---------------------------------------------------------------------------\n\n/** Replaces elastic-ts PrimitiveValue */\nexport type PrimitiveValue = null | number | string | boolean;\n\n/** Replaces elastic-ts Query */\nexport type { QueryDslQueryContainer };\n\n/** Replaces elastic-ts BoolQueryConfig */\nexport type { QueryDslBoolQuery };\n\n/** Replaces elastic-ts FieldSortOptions */\nexport type FieldSortOptions = SearchFieldSort;\n\n/** Replaces elastic-ts SortOrder. Narrowed to \"asc\" | \"desc\" (excludes \"_doc\"). */\nexport type SortOrder = \"asc\" | \"desc\";\n\n/** Replaces elastic-ts Sort */\nexport type { SearchSort as Sort };\n\n/** Single sort entry as an object map. Replaces elastic-ts SortType. */\nexport type SortType = Record<string, FieldSortOptions>;\n\n/** Replaces elastic-ts SearchBody. Overrides search_after to allow PrimitiveValue[] (null | boolean | string | number). */\nexport type SearchBody = Omit<NonNullable<SearchRequest[\"body\"]>, \"search_after\"> & {\n search_after?: PrimitiveValue[];\n};\n\n// ---------------------------------------------------------------------------\n// Context\n// ---------------------------------------------------------------------------\n\nexport interface OpenSearchContext extends Context {\n /**\n * @deprecated Use `context.opensearch` instead.\n */\n elasticsearch: Client;\n opensearch: Client;\n}\n\n// ---------------------------------------------------------------------------\n// Bool query with required arrays (our convention)\n// ---------------------------------------------------------------------------\n\nexport interface OpenSearchBoolQueryConfig extends QueryDslBoolQuery {\n must: QueryDslQueryContainer[];\n filter: QueryDslQueryContainer[];\n should: QueryDslQueryContainer[];\n must_not: QueryDslQueryContainer[];\n}\n\n// ---------------------------------------------------------------------------\n// Operator plugin types\n// ---------------------------------------------------------------------------\n\nexport type OpenSearchQueryOperator =\n | \"eq\"\n | \"not\"\n | \"in\"\n | \"not_in\"\n | \"contains\"\n | \"not_contains\"\n | \"between\"\n | \"not_between\"\n | \"gt\"\n | \"gte\"\n | \"lt\"\n | \"lte\"\n | string;\n\nexport interface OpenSearchQueryBuilderArgsPlugin {\n name: string;\n path: string;\n basePath: string;\n value: any;\n keyword: boolean;\n}\n\n// ---------------------------------------------------------------------------\n// Search response shapes\n// ---------------------------------------------------------------------------\n\nexport interface OpenSearchSearchResponseHit<T> {\n _index: string;\n _type: string;\n _id: string;\n _score: number | null;\n _source: T;\n sort: PrimitiveValue[];\n}\n\nexport interface OpenSearchSearchResponseAggregationBucket<T> {\n key: T;\n doc_count: number;\n}\n\nexport interface OpenSearchSearchResponseBodyHits<T> {\n hits: OpenSearchSearchResponseHit<T>[];\n total: {\n value: number;\n };\n}\n\nexport interface OpenSearchSearchResponseBodyAggregations<T> {\n [key: string]: {\n buckets: OpenSearchSearchResponseAggregationBucket<T>[];\n };\n}\n\nexport interface OpenSearchSearchResponseBody<T> {\n hits: OpenSearchSearchResponseBodyHits<T>;\n aggregations: OpenSearchSearchResponseBodyAggregations<T>;\n}\n\nexport interface OpenSearchSearchResponse<T = GenericRecord> {\n body: OpenSearchSearchResponseBody<T>;\n}\n\n// ---------------------------------------------------------------------------\n// Index request body shapes (our own definitions, not from opensearch package)\n// ---------------------------------------------------------------------------\n\nexport interface OpenSearchIndexRequestBodyMappingsDynamicTemplate {\n [key: string]: {\n path_match?: string;\n path_unmatch?: string;\n match_mapping_type?: string;\n match?: string;\n unmatch?: string;\n mapping?: {\n numeric_detection?: boolean;\n date_detection?: boolean;\n type?: string;\n search_analyzer?: string;\n analyzer?: string;\n fields?: {\n [key: string]:\n | {\n type: string;\n search_analyzer?: string;\n analyzer?: string;\n ignore_above?: number;\n [key: string]: any;\n }\n | undefined;\n };\n [key: string]: any;\n };\n [key: string]: any;\n };\n}\n\nexport interface OpenSearchIndexRequestBody {\n settings?: {\n index?: {\n analysis?: { [key: string]: any };\n number_of_shards?: number;\n number_of_routing_shards?: number;\n codec?: string;\n soft_deletes?: { enabled?: boolean; retention_lease?: { period?: string } };\n number_of_replicas?: number;\n auto_expand_replicas?: string | \"all\" | false;\n refresh_interval?: string;\n max_result_window?: number;\n max_inner_result_window?: number;\n max_rescore_window?: number;\n max_script_fields?: number;\n max_ngram_diff?: number;\n max_shingle_diff?: number;\n max_terms_count?: number;\n max_regex_length?: number;\n routing?: {\n allocation?: { enable?: \"all\" | \"primaries\" | \"new_primaries\" | \"none\" };\n rebalance?: { enable?: \"all\" | \"primaries\" | \"new_primaries\" | \"none\" };\n };\n hidden?: boolean;\n total_fields?: { limit?: number };\n [key: string]: any;\n };\n };\n mappings: {\n numeric_detection?: boolean;\n dynamic_templates?: OpenSearchIndexRequestBodyMappingsDynamicTemplate[];\n properties?: {\n [key: string]: {\n analyzer?: string;\n type?: string;\n normalizer?: string;\n index?: string;\n fields?: {\n [key: string]: {\n type: string;\n ignore_above?: number;\n search_analyzer?: string;\n analyzer?: string;\n [key: string]: any;\n };\n };\n [key: string]: any;\n };\n };\n [key: string]: any;\n };\n aliases?: {\n [key: string]: {\n filter?: { [key: string]: any };\n index_routing?: string;\n is_hidden?: boolean;\n is_write_index?: boolean;\n routing?: string;\n search_routing?: string;\n };\n };\n}\n\n// Re-export SearchSortOrder for consumers that need it (including \"_doc\")\nexport type { SearchSortOrder };\n"],"mappings":"","ignoreList":[]}
@@ -0,0 +1,19 @@
1
+ import type { Client } from "../client.js";
2
+ import type { PluginsContainer } from "@webiny/plugins";
3
+ interface OnExists {
4
+ (): void;
5
+ }
6
+ interface OnError {
7
+ (ex: Error): Error;
8
+ }
9
+ interface CreateIndexParams {
10
+ client: Client;
11
+ plugins: PluginsContainer;
12
+ type: string;
13
+ tenant: string;
14
+ index: string;
15
+ onExists?: OnExists;
16
+ onError?: OnError;
17
+ }
18
+ export declare const createIndex: (params: CreateIndexParams) => Promise<void>;
19
+ export {};
@@ -0,0 +1,88 @@
1
+ import { getLastAddedIndexPlugin } from "../indices.js";
2
+ import WebinyError from "@webiny/error";
3
+ const indexExists = async params => {
4
+ const {
5
+ client,
6
+ index,
7
+ onExists
8
+ } = params;
9
+ try {
10
+ const response = await client.indices.exists({
11
+ index,
12
+ ignore_unavailable: false,
13
+ allow_no_indices: true,
14
+ include_defaults: true,
15
+ flat_settings: false,
16
+ local: false
17
+ });
18
+ if (!response.body) {
19
+ return false;
20
+ }
21
+ if (onExists) {
22
+ onExists();
23
+ }
24
+ return true;
25
+ } catch {
26
+ console.error(`Could not determine if the index "${index}" exists.`);
27
+ }
28
+ return false;
29
+ };
30
+ const indexCreate = async params => {
31
+ const {
32
+ client,
33
+ index,
34
+ plugin,
35
+ tenant,
36
+ type,
37
+ onError
38
+ } = params;
39
+ try {
40
+ await client.indices.create({
41
+ index,
42
+ body: {
43
+ ...plugin.body
44
+ }
45
+ });
46
+ } catch (ex) {
47
+ let error = ex;
48
+ if (onError) {
49
+ error = onError(ex);
50
+ }
51
+ throw new WebinyError(error.message || `Could not create OpenSearch index for the ${type}.`, error.code || "CREATE_OPENSEARCH_INDEX_ERROR", {
52
+ error: {
53
+ ...error,
54
+ message: error.message,
55
+ code: error.code,
56
+ data: error.data
57
+ },
58
+ type,
59
+ tenant,
60
+ index,
61
+ body: plugin.body
62
+ });
63
+ }
64
+ };
65
+ export const createIndex = async params => {
66
+ const {
67
+ plugins,
68
+ type,
69
+ onExists
70
+ } = params;
71
+ const plugin = getLastAddedIndexPlugin({
72
+ container: plugins,
73
+ type
74
+ });
75
+ const exists = await indexExists(params);
76
+ if (exists) {
77
+ if (onExists) {
78
+ onExists();
79
+ }
80
+ return;
81
+ }
82
+ await indexCreate({
83
+ ...params,
84
+ plugin
85
+ });
86
+ };
87
+
88
+ //# sourceMappingURL=createIndex.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["getLastAddedIndexPlugin","WebinyError","indexExists","params","client","index","onExists","response","indices","exists","ignore_unavailable","allow_no_indices","include_defaults","flat_settings","local","body","console","error","indexCreate","plugin","tenant","type","onError","create","ex","message","code","data","createIndex","plugins","container"],"sources":["createIndex.ts"],"sourcesContent":["import type { Client } from \"~/client.js\";\nimport type { PluginsContainer } from \"@webiny/plugins\";\nimport { getLastAddedIndexPlugin } from \"~/indices.js\";\nimport type { OpenSearchIndexPlugin } from \"~/plugins/index.js\";\nimport WebinyError from \"@webiny/error\";\n\ninterface OnExists {\n (): void;\n}\n\ninterface OnError {\n (ex: Error): Error;\n}\n\ninterface ExistsIndexParams {\n client: Client;\n index: string;\n onExists?: OnExists;\n}\n\nconst indexExists = async (params: ExistsIndexParams): Promise<boolean> => {\n const { client, index, onExists } = params;\n\n try {\n const response = await client.indices.exists({\n index,\n ignore_unavailable: false,\n allow_no_indices: true,\n include_defaults: true,\n flat_settings: false,\n local: false\n });\n if (!response.body) {\n return false;\n }\n if (onExists) {\n onExists();\n }\n return true;\n } catch {\n console.error(`Could not determine if the index \"${index}\" exists.`);\n }\n return false;\n};\n\ninterface IndexCreateParams {\n client: Client;\n index: string;\n type: string;\n tenant: string;\n plugin: OpenSearchIndexPlugin;\n onError?: OnError;\n}\n\nconst indexCreate = async (params: IndexCreateParams): Promise<void> => {\n const { client, index, plugin, tenant, type, onError } = params;\n\n try {\n await client.indices.create({\n index,\n body: {\n ...plugin.body\n }\n });\n } catch (ex) {\n let error = ex;\n if (onError) {\n error = onError(ex);\n }\n throw new WebinyError(\n error.message || `Could not create OpenSearch index for the ${type}.`,\n error.code || \"CREATE_OPENSEARCH_INDEX_ERROR\",\n {\n error: {\n ...error,\n message: error.message,\n code: error.code,\n data: error.data\n },\n type,\n tenant,\n index,\n body: plugin.body\n }\n );\n }\n};\n\ninterface CreateIndexParams {\n client: Client;\n plugins: PluginsContainer;\n type: string;\n tenant: string;\n index: string;\n onExists?: OnExists;\n onError?: OnError;\n}\n\nexport const createIndex = async (params: CreateIndexParams): Promise<void> => {\n const { plugins, type, onExists } = params;\n const plugin = getLastAddedIndexPlugin<OpenSearchIndexPlugin>({\n container: plugins,\n type\n });\n\n const exists = await indexExists(params);\n if (exists) {\n if (onExists) {\n onExists();\n }\n return;\n }\n\n await indexCreate({\n ...params,\n plugin\n });\n};\n"],"mappings":"AAEA,SAASA,uBAAuB;AAEhC,OAAOC,WAAW,MAAM,eAAe;AAgBvC,MAAMC,WAAW,GAAG,MAAOC,MAAyB,IAAuB;EACvE,MAAM;IAAEC,MAAM;IAAEC,KAAK;IAAEC;EAAS,CAAC,GAAGH,MAAM;EAE1C,IAAI;IACA,MAAMI,QAAQ,GAAG,MAAMH,MAAM,CAACI,OAAO,CAACC,MAAM,CAAC;MACzCJ,KAAK;MACLK,kBAAkB,EAAE,KAAK;MACzBC,gBAAgB,EAAE,IAAI;MACtBC,gBAAgB,EAAE,IAAI;MACtBC,aAAa,EAAE,KAAK;MACpBC,KAAK,EAAE;IACX,CAAC,CAAC;IACF,IAAI,CAACP,QAAQ,CAACQ,IAAI,EAAE;MAChB,OAAO,KAAK;IAChB;IACA,IAAIT,QAAQ,EAAE;MACVA,QAAQ,CAAC,CAAC;IACd;IACA,OAAO,IAAI;EACf,CAAC,CAAC,MAAM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AE;IACR,IAAIH,QAAQ,EAAE;MACVA,QAAQ,CAAC,CAAC;IACd;IACA;EACJ;EAEA,MAAMY,WAAW,CAAC;IACd,GAAGf,MAAM;IACTgB;EACJ,CAAC,CAAC;AACN,CAAC","ignoreList":[]}
@@ -0,0 +1,2 @@
1
+ export * from "./waitUntilHealthy/index.js";
2
+ export * from "./createIndex.js";
package/utils/index.js ADDED
@@ -0,0 +1,4 @@
1
+ export * from "./waitUntilHealthy/index.js";
2
+ export * from "./createIndex.js";
3
+
4
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":[],"sources":["index.ts"],"sourcesContent":["export * from \"./waitUntilHealthy/index.js\";\nexport * from \"./createIndex.js\";\n"],"mappings":"AAAA;AACA","ignoreList":[]}
@@ -0,0 +1,4 @@
1
+ import { WebinyError } from "@webiny/error";
2
+ export declare class UnhealthyClusterError extends WebinyError {
3
+ constructor(maxWaitingTime: number);
4
+ }
@@ -0,0 +1,11 @@
1
+ import { WebinyError } from "@webiny/error";
2
+ export class UnhealthyClusterError extends WebinyError {
3
+ constructor(maxWaitingTime) {
4
+ super({
5
+ message: `Cluster did not become healthy in ${maxWaitingTime} seconds.`,
6
+ code: "UNHEALTHY_CLUSTER"
7
+ });
8
+ }
9
+ }
10
+
11
+ //# sourceMappingURL=UnhealthyClusterError.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["WebinyError","UnhealthyClusterError","constructor","maxWaitingTime","message","code"],"sources":["UnhealthyClusterError.ts"],"sourcesContent":["import { WebinyError } from \"@webiny/error\";\n\nexport class UnhealthyClusterError extends WebinyError {\n public constructor(maxWaitingTime: number) {\n super({\n message: `Cluster did not become healthy in ${maxWaitingTime} seconds.`,\n code: \"UNHEALTHY_CLUSTER\"\n });\n }\n}\n"],"mappings":"AAAA,SAASA,WAAW,QAAQ,eAAe;AAE3C,OAAO,MAAMC,qBAAqB,SAASD,WAAW,CAAC;EAC5CE,WAAWA,CAACC,cAAsB,EAAE;IACvC,KAAK,CAAC;MACFC,OAAO,EAAE,qCAAqCD,cAAc,WAAW;MACvEE,IAAI,EAAE;IACV,CAAC,CAAC;EACN;AACJ","ignoreList":[]}
@@ -0,0 +1,71 @@
1
+ import type { Client } from "../../client.js";
2
+ import { OpenSearchCatClusterHealthStatus } from "../../operations/types.js";
3
+ import type { ClusterHealthReason, MemoryReason, ProcessorReason } from "./reason/index.js";
4
+ export type WaitingReason = ProcessorReason | MemoryReason | ClusterHealthReason;
5
+ export interface IWaitUntilHealthyParams {
6
+ /**
7
+ * Minimum status allowed, otherwise the cluster is considered unhealthy.
8
+ */
9
+ minClusterHealthStatus: OpenSearchCatClusterHealthStatus.Green | OpenSearchCatClusterHealthStatus.Yellow;
10
+ /**
11
+ * Maximum processor percent allowed, otherwise the cluster is considered unhealthy.
12
+ */
13
+ maxProcessorPercent: number;
14
+ /**
15
+ * Maximum RAM percent allowed, otherwise the cluster is considered unhealthy.
16
+ */
17
+ maxRamPercent?: number;
18
+ /**
19
+ * Maximum time to wait in seconds.
20
+ * This is to prevent infinite waiting in case the cluster never becomes healthy.
21
+ */
22
+ maxWaitingTime: number;
23
+ /**
24
+ * Time in seconds to wait between each check.
25
+ * This is to prevent spamming the cluster with requests.
26
+ * Default is WAITING_TIME_STEP seconds.
27
+ */
28
+ waitingTimeStep?: number;
29
+ }
30
+ export interface IWaitOptionsOnUnhealthyParams {
31
+ startedAt: Date;
32
+ mustEndAt: Date;
33
+ waitingTimeStep: number;
34
+ runs: number;
35
+ waitingReason: WaitingReason;
36
+ }
37
+ export interface IWaitOptionsOnTimeoutParams {
38
+ startedAt: Date;
39
+ mustEndAt: Date;
40
+ waitingTimeStep: number;
41
+ runs: number;
42
+ waitingReason: WaitingReason;
43
+ }
44
+ export interface IWaitOptions {
45
+ onUnhealthy?(params: IWaitOptionsOnUnhealthyParams): Promise<void>;
46
+ onTimeout?(params: IWaitOptionsOnTimeoutParams): Promise<void>;
47
+ }
48
+ export interface IWaitUntilHealthyWaitResponse {
49
+ runningTime: number;
50
+ runs: number;
51
+ }
52
+ declare class WaitUntilHealthy {
53
+ private readonly client;
54
+ private readonly options;
55
+ private readonly catHealth;
56
+ private readonly catNodes;
57
+ private aborted;
58
+ constructor(client: Client, options: IWaitUntilHealthyParams);
59
+ abort(): void;
60
+ /**
61
+ * @throws UnhealthyClusterError
62
+ * @throws WaitingHealthyClusterAbortedError
63
+ */
64
+ wait(options?: IWaitOptions): Promise<IWaitUntilHealthyWaitResponse>;
65
+ private shouldWait;
66
+ private getProcessorPercent;
67
+ private getRamPercent;
68
+ private transformClusterHealthStatus;
69
+ }
70
+ export type { WaitUntilHealthy };
71
+ export declare const createWaitUntilHealthy: (client: Client, params: IWaitUntilHealthyParams) => WaitUntilHealthy;
@@ -0,0 +1,150 @@
1
+ import { OpenSearchCatHealth } from "../../operations/OpenSearchCatHealth.js";
2
+ import { OpenSearchCatNodes } from "../../operations/OpenSearchCatNodes.js";
3
+ import { OpenSearchCatClusterHealthStatus } from "../../operations/types.js";
4
+ import { UnhealthyClusterError } from "./UnhealthyClusterError.js";
5
+ import { createClusterHealthStatusReason, createMemoryReason, createProcessorReason } from "./reason/index.js";
6
+ import { WaitingHealthyClusterAbortedError } from "./WaitingHealthyClusterAbortedError.js";
7
+ const WAITING_TIME_STEP = 10;
8
+ class WaitUntilHealthy {
9
+ aborted = false;
10
+ constructor(client, options) {
11
+ this.client = client;
12
+ this.options = options;
13
+ this.catHealth = new OpenSearchCatHealth(this.client);
14
+ this.catNodes = new OpenSearchCatNodes(this.client);
15
+ }
16
+ abort() {
17
+ this.aborted = true;
18
+ }
19
+ /**
20
+ * @throws UnhealthyClusterError
21
+ * @throws WaitingHealthyClusterAbortedError
22
+ */
23
+ async wait(options) {
24
+ if (this.aborted) {
25
+ throw new WaitingHealthyClusterAbortedError(`Waiting for the cluster to become healthy was aborted even before it started.`);
26
+ }
27
+ const startedAt = new Date();
28
+ const mustEndAt = new Date(startedAt.getTime() + this.options.maxWaitingTime * 1000);
29
+ const waitingTimeStep = this.options.waitingTimeStep || WAITING_TIME_STEP;
30
+ let runs = 1;
31
+ let waitingReason;
32
+ while (waitingReason = await this.shouldWait()) {
33
+ if (new Date() >= mustEndAt) {
34
+ if (options?.onTimeout) {
35
+ await options.onTimeout({
36
+ startedAt,
37
+ mustEndAt,
38
+ waitingTimeStep,
39
+ waitingReason,
40
+ runs
41
+ });
42
+ }
43
+ throw new UnhealthyClusterError(this.options.maxWaitingTime);
44
+ } else if (options?.onUnhealthy) {
45
+ await options.onUnhealthy({
46
+ startedAt,
47
+ mustEndAt,
48
+ waitingTimeStep,
49
+ waitingReason,
50
+ runs
51
+ });
52
+ }
53
+ /**
54
+ * Abort check is separated from other IFs because it can be aborted in onUnhealthy callback.
55
+ */
56
+ if (this.aborted) {
57
+ throw new WaitingHealthyClusterAbortedError();
58
+ }
59
+ runs++;
60
+ await new Promise(resolve => {
61
+ setTimeout(resolve, waitingTimeStep * 1000);
62
+ });
63
+ }
64
+ const runningTime = new Date().getTime() - startedAt.getTime();
65
+ return {
66
+ runningTime,
67
+ runs
68
+ };
69
+ }
70
+ async shouldWait() {
71
+ let health;
72
+ let nodes;
73
+ try {
74
+ health = await this.catHealth.getHealth();
75
+ } catch (ex) {
76
+ return createClusterHealthStatusReason({
77
+ description: ex.message,
78
+ minimum: this.options.minClusterHealthStatus,
79
+ current: OpenSearchCatClusterHealthStatus.Red
80
+ });
81
+ }
82
+ try {
83
+ nodes = await this.catNodes.getNodes();
84
+ } catch (ex) {
85
+ return createClusterHealthStatusReason({
86
+ description: ex.message,
87
+ minimum: this.options.minClusterHealthStatus,
88
+ current: OpenSearchCatClusterHealthStatus.Red
89
+ });
90
+ }
91
+ const clusterHealthStatus = this.transformClusterHealthStatus(health.status);
92
+ const minClusterHealthStatus = this.transformClusterHealthStatus(this.options.minClusterHealthStatus);
93
+ if (clusterHealthStatus > minClusterHealthStatus) {
94
+ return createClusterHealthStatusReason({
95
+ minimum: this.options.minClusterHealthStatus,
96
+ current: health.status
97
+ });
98
+ }
99
+ const processorPercent = this.getProcessorPercent(nodes);
100
+ if (processorPercent > this.options.maxProcessorPercent) {
101
+ return createProcessorReason({
102
+ maximum: this.options.maxProcessorPercent,
103
+ current: processorPercent
104
+ });
105
+ }
106
+ /**
107
+ * Possibly no max ram definition?
108
+ */
109
+ if (this.options.maxRamPercent === undefined) {
110
+ return false;
111
+ }
112
+ const ramPercent = this.getRamPercent(nodes);
113
+ if (ramPercent > this.options.maxRamPercent) {
114
+ return createMemoryReason({
115
+ maximum: this.options.maxRamPercent,
116
+ current: ramPercent
117
+ });
118
+ }
119
+ return false;
120
+ }
121
+ getProcessorPercent(nodes) {
122
+ const total = nodes.reduce((total, node) => {
123
+ return total + parseFloat(node.cpu);
124
+ }, 0);
125
+ return total / nodes.length;
126
+ }
127
+ getRamPercent(nodes) {
128
+ const total = nodes.reduce((total, node) => {
129
+ return total + parseFloat(node["ram.percent"]);
130
+ }, 0);
131
+ return total / nodes.length;
132
+ }
133
+ transformClusterHealthStatus(status) {
134
+ switch (status) {
135
+ case OpenSearchCatClusterHealthStatus.Green:
136
+ return 1;
137
+ case OpenSearchCatClusterHealthStatus.Yellow:
138
+ return 2;
139
+ case OpenSearchCatClusterHealthStatus.Red:
140
+ return 3;
141
+ default:
142
+ return 99;
143
+ }
144
+ }
145
+ }
146
+ export const createWaitUntilHealthy = (client, params) => {
147
+ return new WaitUntilHealthy(client, params);
148
+ };
149
+
150
+ //# sourceMappingURL=WaitUntilHealthy.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["OpenSearchCatHealth","OpenSearchCatNodes","OpenSearchCatClusterHealthStatus","UnhealthyClusterError","createClusterHealthStatusReason","createMemoryReason","createProcessorReason","WaitingHealthyClusterAbortedError","WAITING_TIME_STEP","WaitUntilHealthy","aborted","constructor","client","options","catHealth","catNodes","abort","wait","startedAt","Date","mustEndAt","getTime","maxWaitingTime","waitingTimeStep","runs","waitingReason","shouldWait","onTimeout","onUnhealthy","Promise","resolve","setTimeout","runningTime","health","nodes","getHealth","ex","description","message","minimum","minClusterHealthStatus","current","Red","getNodes","clusterHealthStatus","transformClusterHealthStatus","status","processorPercent","getProcessorPercent","maxProcessorPercent","maximum","maxRamPercent","undefined","ramPercent","getRamPercent","total","reduce","node","parseFloat","cpu","length","Green","Yellow","createWaitUntilHealthy","params"],"sources":["WaitUntilHealthy.ts"],"sourcesContent":["import type { Client } from \"~/client.js\";\nimport { OpenSearchCatHealth } from \"~/operations/OpenSearchCatHealth.js\";\nimport { OpenSearchCatNodes } from \"~/operations/OpenSearchCatNodes.js\";\nimport type {\n IOpenSearchCatHealthResponse,\n IOpenSearchCatNodesResponse\n} from \"~/operations/types.js\";\nimport { OpenSearchCatClusterHealthStatus } from \"~/operations/types.js\";\nimport { UnhealthyClusterError } from \"~/utils/waitUntilHealthy/UnhealthyClusterError.js\";\nimport type { ClusterHealthReason, MemoryReason, ProcessorReason } from \"./reason/index.js\";\nimport {\n createClusterHealthStatusReason,\n createMemoryReason,\n createProcessorReason\n} from \"./reason/index.js\";\nimport { WaitingHealthyClusterAbortedError } from \"./WaitingHealthyClusterAbortedError.js\";\n\nconst WAITING_TIME_STEP = 10;\n\nexport type WaitingReason = ProcessorReason | MemoryReason | ClusterHealthReason;\n\nexport interface IWaitUntilHealthyParams {\n /**\n * Minimum status allowed, otherwise the cluster is considered unhealthy.\n */\n minClusterHealthStatus:\n | OpenSearchCatClusterHealthStatus.Green\n | OpenSearchCatClusterHealthStatus.Yellow;\n /**\n * Maximum processor percent allowed, otherwise the cluster is considered unhealthy.\n */\n maxProcessorPercent: number;\n /**\n * Maximum RAM percent allowed, otherwise the cluster is considered unhealthy.\n */\n maxRamPercent?: number;\n /**\n * Maximum time to wait in seconds.\n * This is to prevent infinite waiting in case the cluster never becomes healthy.\n */\n maxWaitingTime: number;\n /**\n * Time in seconds to wait between each check.\n * This is to prevent spamming the cluster with requests.\n * Default is WAITING_TIME_STEP seconds.\n */\n waitingTimeStep?: number;\n}\n\nexport interface IWaitOptionsOnUnhealthyParams {\n startedAt: Date;\n mustEndAt: Date;\n waitingTimeStep: number;\n runs: number;\n waitingReason: WaitingReason;\n}\n\nexport interface IWaitOptionsOnTimeoutParams {\n startedAt: Date;\n mustEndAt: Date;\n waitingTimeStep: number;\n runs: number;\n waitingReason: WaitingReason;\n}\n\nexport interface IWaitOptions {\n onUnhealthy?(params: IWaitOptionsOnUnhealthyParams): Promise<void>;\n onTimeout?(params: IWaitOptionsOnTimeoutParams): Promise<void>;\n}\n\nexport interface IWaitUntilHealthyWaitResponse {\n runningTime: number;\n runs: number;\n}\n\nclass WaitUntilHealthy {\n private readonly client: Client;\n private readonly options: IWaitUntilHealthyParams;\n\n private readonly catHealth: OpenSearchCatHealth;\n private readonly catNodes: OpenSearchCatNodes;\n\n private aborted = false;\n\n public constructor(client: Client, options: IWaitUntilHealthyParams) {\n this.client = client;\n this.options = options;\n\n this.catHealth = new OpenSearchCatHealth(this.client);\n this.catNodes = new OpenSearchCatNodes(this.client);\n }\n\n public abort(): void {\n this.aborted = true;\n }\n /**\n * @throws UnhealthyClusterError\n * @throws WaitingHealthyClusterAbortedError\n */\n public async wait(options?: IWaitOptions): Promise<IWaitUntilHealthyWaitResponse> {\n if (this.aborted) {\n throw new WaitingHealthyClusterAbortedError(\n `Waiting for the cluster to become healthy was aborted even before it started.`\n );\n }\n const startedAt = new Date();\n const mustEndAt = new Date(startedAt.getTime() + this.options.maxWaitingTime * 1000);\n const waitingTimeStep = this.options.waitingTimeStep || WAITING_TIME_STEP;\n let runs = 1;\n let waitingReason: WaitingReason | false;\n while ((waitingReason = await this.shouldWait())) {\n if (new Date() >= mustEndAt) {\n if (options?.onTimeout) {\n await options.onTimeout({\n startedAt,\n mustEndAt,\n waitingTimeStep,\n waitingReason,\n runs\n });\n }\n throw new UnhealthyClusterError(this.options.maxWaitingTime);\n } else if (options?.onUnhealthy) {\n await options.onUnhealthy({\n startedAt,\n mustEndAt,\n waitingTimeStep,\n waitingReason,\n runs\n });\n }\n /**\n * Abort check is separated from other IFs because it can be aborted in onUnhealthy callback.\n */\n if (this.aborted) {\n throw new WaitingHealthyClusterAbortedError();\n }\n runs++;\n await new Promise(resolve => {\n setTimeout(resolve, waitingTimeStep * 1000);\n });\n }\n\n const runningTime = new Date().getTime() - startedAt.getTime();\n\n return {\n runningTime,\n runs\n };\n }\n\n private async shouldWait(): Promise<WaitingReason | false> {\n let health: IOpenSearchCatHealthResponse;\n let nodes: IOpenSearchCatNodesResponse;\n try {\n health = await this.catHealth.getHealth();\n } catch (ex) {\n return createClusterHealthStatusReason({\n description: ex.message,\n minimum: this.options.minClusterHealthStatus,\n current: OpenSearchCatClusterHealthStatus.Red\n });\n }\n try {\n nodes = await this.catNodes.getNodes();\n } catch (ex) {\n return createClusterHealthStatusReason({\n description: ex.message,\n minimum: this.options.minClusterHealthStatus,\n current: OpenSearchCatClusterHealthStatus.Red\n });\n }\n\n const clusterHealthStatus = this.transformClusterHealthStatus(health.status);\n const minClusterHealthStatus = this.transformClusterHealthStatus(\n this.options.minClusterHealthStatus\n );\n if (clusterHealthStatus > minClusterHealthStatus) {\n return createClusterHealthStatusReason({\n minimum: this.options.minClusterHealthStatus,\n current: health.status\n });\n }\n\n const processorPercent = this.getProcessorPercent(nodes);\n if (processorPercent > this.options.maxProcessorPercent) {\n return createProcessorReason({\n maximum: this.options.maxProcessorPercent,\n current: processorPercent\n });\n }\n /**\n * Possibly no max ram definition?\n */\n if (this.options.maxRamPercent === undefined) {\n return false;\n }\n\n const ramPercent = this.getRamPercent(nodes);\n if (ramPercent > this.options.maxRamPercent) {\n return createMemoryReason({\n maximum: this.options.maxRamPercent,\n current: ramPercent\n });\n }\n return false;\n }\n\n private getProcessorPercent(nodes: IOpenSearchCatNodesResponse): number {\n const total = nodes.reduce<number>((total, node) => {\n return total + parseFloat(node.cpu);\n }, 0);\n return total / nodes.length;\n }\n\n private getRamPercent(nodes: IOpenSearchCatNodesResponse): number {\n const total = nodes.reduce<number>((total, node) => {\n return total + parseFloat(node[\"ram.percent\"]);\n }, 0);\n return total / nodes.length;\n }\n\n private transformClusterHealthStatus(status: OpenSearchCatClusterHealthStatus): number {\n switch (status) {\n case OpenSearchCatClusterHealthStatus.Green:\n return 1;\n case OpenSearchCatClusterHealthStatus.Yellow:\n return 2;\n case OpenSearchCatClusterHealthStatus.Red:\n return 3;\n default:\n return 99;\n }\n }\n}\n\nexport type { WaitUntilHealthy };\n\nexport const createWaitUntilHealthy = (\n client: Client,\n params: IWaitUntilHealthyParams\n): WaitUntilHealthy => {\n return new WaitUntilHealthy(client, params);\n};\n"],"mappings":"AACA,SAASA,mBAAmB;AAC5B,SAASC,kBAAkB;AAK3B,SAASC,gCAAgC;AACzC,SAASC,qBAAqB;AAE9B,SACIC,+BAA+B,EAC/BC,kBAAkB,EAClBC,qBAAqB;AAEzB,SAASC,iCAAiC;AAE1C,MAAMC,iBAAiB,GAAG,EAAE;AA0D5B,MAAMC,gBAAgB,CAAC;EAOXC,OAAO,GAAG,KAAK;EAEhBC,WAAWA,CAACC,MAAc,EAAEC,OAAgC,EAAE;IACjE,IAAI,CAACD,MAAM,GAAGA,MAAM;IACpB,IAAI,CAACC,OAAO,GAAGA,OAAO;IAEtB,IAAI,CAACC,SAAS,GAAG,IAAId,mBAAmB,CAAC,IAAI,CAACY,MAAM,CAAC;IACrD,IAAI,CAACG,QAAQ,GAAG,IAAId,kBAAkB,CAAC,IAAI,CAACW,MAAM,CAAC;EACvD;EAEOI,KAAKA,CAAA,EAAS;IACjB,IAAI,CAACN,OAAO,GAAG,IAAI;EACvB;EACA;AACJ;AACA;AACA;EACI,MAAaO,IAAIA,CAACJ,OAAsB,EAA0C;IAC9E,IAAI,IAAI,CAACH,OAAO,EAAE;MACd,MAAM,IAAIH,iCAAiC,CACvC,+EACJ,CAAC;IACL;IACA,MAAMW,SAAS,GAAG,IAAIC,IAAI,CAAC,CAAC;IAC5B,MAAMC,SAAS,GAAG,IAAID,IAAI,CAACD,SAAS,CAACG,OAAO,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G,MAAM,EAAEoD,MAAM,CAAC;AAC/C,CAAC","ignoreList":[]}
@@ -0,0 +1,4 @@
1
+ import { WebinyError } from "@webiny/error";
2
+ export declare class WaitingHealthyClusterAbortedError extends WebinyError {
3
+ constructor(message?: string);
4
+ }
@@ -0,0 +1,11 @@
1
+ import { WebinyError } from "@webiny/error";
2
+ export class WaitingHealthyClusterAbortedError extends WebinyError {
3
+ constructor(message) {
4
+ super({
5
+ message: message || `Waiting for the cluster to become healthy was aborted.`,
6
+ code: "WAITING_HEALTHY_CLUSTER_ABORTED"
7
+ });
8
+ }
9
+ }
10
+
11
+ //# sourceMappingURL=WaitingHealthyClusterAbortedError.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["WebinyError","WaitingHealthyClusterAbortedError","constructor","message","code"],"sources":["WaitingHealthyClusterAbortedError.ts"],"sourcesContent":["import { WebinyError } from \"@webiny/error\";\n\nexport class WaitingHealthyClusterAbortedError extends WebinyError {\n public constructor(message?: string) {\n super({\n message: message || `Waiting for the cluster to become healthy was aborted.`,\n code: \"WAITING_HEALTHY_CLUSTER_ABORTED\"\n });\n }\n}\n"],"mappings":"AAAA,SAASA,WAAW,QAAQ,eAAe;AAE3C,OAAO,MAAMC,iCAAiC,SAASD,WAAW,CAAC;EACxDE,WAAWA,CAACC,OAAgB,EAAE;IACjC,KAAK,CAAC;MACFA,OAAO,EAAEA,OAAO,IAAI,wDAAwD;MAC5EC,IAAI,EAAE;IACV,CAAC,CAAC;EACN;AACJ","ignoreList":[]}
@@ -0,0 +1,4 @@
1
+ export * from "./WaitingHealthyClusterAbortedError.js";
2
+ export * from "./UnhealthyClusterError.js";
3
+ export * from "./WaitUntilHealthy.js";
4
+ export * from "./reason/index.js";
@@ -0,0 +1,6 @@
1
+ export * from "./WaitingHealthyClusterAbortedError.js";
2
+ export * from "./UnhealthyClusterError.js";
3
+ export * from "./WaitUntilHealthy.js";
4
+ export * from "./reason/index.js";
5
+
6
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":[],"sources":["index.ts"],"sourcesContent":["export * from \"./WaitingHealthyClusterAbortedError.js\";\nexport * from \"./UnhealthyClusterError.js\";\nexport * from \"./WaitUntilHealthy.js\";\nexport * from \"./reason/index.js\";\n"],"mappings":"AAAA;AACA;AACA;AACA","ignoreList":[]}