@ohos-ports/atg-shared-utilities 0.0.0-stage → 0.10.12-beta.1
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.
- package/LICENSE +202 -0
- package/README.md +176 -2
- package/index.js +33 -0
- package/package.json +59 -5
- package/src/arrayUtils.js +175 -0
- package/src/ipUtils.js +435 -0
- package/src/promiseUtils.js +108 -0
- package/src/request.js +187 -0
- package/src/secureVault.js +155 -0
- package/src/tmshUtils.js +66 -0
- package/src/tracer.js +530 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,177 @@
|
|
|
1
|
-
#
|
|
1
|
+
# atg-shared-utilities
|
|
2
2
|
|
|
3
|
-
This
|
|
3
|
+
This project holds a number of generic utility functions that can be shared across the F5 Automation Toolchain projects.
|
|
4
|
+
|
|
5
|
+
## CONTRIBUTIONS
|
|
6
|
+
|
|
7
|
+
Read through the contributing/README.md for information on how to contribute to this project.
|
|
8
|
+
|
|
9
|
+
## ipUtils
|
|
10
|
+
|
|
11
|
+
These utilities will assist in IP address calculations
|
|
12
|
+
|
|
13
|
+
### minimizeIP(ip)
|
|
14
|
+
|
|
15
|
+
Minimize zeroes in the string representation of an F5 IPv4/6 address
|
|
16
|
+
|
|
17
|
+
### isIPv4(ip)
|
|
18
|
+
|
|
19
|
+
Checks if an address is IPv4
|
|
20
|
+
|
|
21
|
+
### isIPv6(ip)
|
|
22
|
+
|
|
23
|
+
Checks if an address is IPv6
|
|
24
|
+
|
|
25
|
+
### splitAddress(address)
|
|
26
|
+
|
|
27
|
+
Splits an IPv4 or IPv6 address into an address and port pair
|
|
28
|
+
|
|
29
|
+
### getCidrFromNetmask(netmask, noSlash)
|
|
30
|
+
|
|
31
|
+
Returns the CIDR for the given netmask. Optionally returns CIDR without a leading '/'
|
|
32
|
+
|
|
33
|
+
### parseIpAddress(address)
|
|
34
|
+
|
|
35
|
+
Parses an IP address into its components: IP, route domain, CIRD, netmask, IP with route
|
|
36
|
+
|
|
37
|
+
## promiseUtils
|
|
38
|
+
|
|
39
|
+
These utilities will assist in managing promises within your program.
|
|
40
|
+
|
|
41
|
+
### delay(t, v)
|
|
42
|
+
|
|
43
|
+
This function delays t (time in milliseconds). Afterwhich it resolves v (optional).
|
|
44
|
+
|
|
45
|
+
### series(function[])
|
|
46
|
+
|
|
47
|
+
This function will resolve an array of functions sequentially. Note each function must return a Promise.
|
|
48
|
+
|
|
49
|
+
### parallel(function[])
|
|
50
|
+
|
|
51
|
+
This function will resolve an array of functions in parallel. Note each function must return a Promise.
|
|
52
|
+
|
|
53
|
+
### raceSuccess(Promise[])
|
|
54
|
+
|
|
55
|
+
This function will run an array of promises. Then resolve with the result of the first Promise that resolves.
|
|
56
|
+
|
|
57
|
+
### retryPromise(fn, options, args[])
|
|
58
|
+
|
|
59
|
+
fn - The function to be run.
|
|
60
|
+
options - An object with .retries and .delay. Both are required.
|
|
61
|
+
args - An array of arguments passed into fn.
|
|
62
|
+
|
|
63
|
+
This will run the function fn with the arguments args, up to a number of times equal to options.retries, or until a resolve.
|
|
64
|
+
|
|
65
|
+
## arrayUtils
|
|
66
|
+
|
|
67
|
+
This collection of utils focuses on generic functions useful to arrays.
|
|
68
|
+
|
|
69
|
+
### ensureArray(variable)
|
|
70
|
+
|
|
71
|
+
Takes a variable, converts it to an array (if it is not an array), and returns it. If undefined, it will return `[]`.
|
|
72
|
+
|
|
73
|
+
### doesArrayContainAnyOf(array1, array2)
|
|
74
|
+
|
|
75
|
+
Checks if any member of tArray is present in array.
|
|
76
|
+
|
|
77
|
+
### doesArrayContain(array, target)
|
|
78
|
+
|
|
79
|
+
Checks if any member of target is present in array.
|
|
80
|
+
|
|
81
|
+
### insertAfterOrAtBeginning(array, target, item, comparison)
|
|
82
|
+
|
|
83
|
+
Inserts an item after another item in an array or at the beginning of the array.
|
|
84
|
+
|
|
85
|
+
### insertBeforeOrAtBeginning(array, target, item, comparison)
|
|
86
|
+
|
|
87
|
+
Inserts an item before another item in an array or at the beginning of the array.
|
|
88
|
+
|
|
89
|
+
### insertAfterOrAtEnd(array, target, item, comparison)
|
|
90
|
+
|
|
91
|
+
Inserts an item after another item in an array or at the end of the array.
|
|
92
|
+
|
|
93
|
+
### insertBeforeOrAtEnd(array, target, item, comparison)
|
|
94
|
+
|
|
95
|
+
Inserts an item before another item in an array or at the end of the array.
|
|
96
|
+
|
|
97
|
+
## requestUtils
|
|
98
|
+
|
|
99
|
+
This collection of utils is for sending http requests.
|
|
100
|
+
|
|
101
|
+
### send(requestOptions, body, options)
|
|
102
|
+
|
|
103
|
+
Send an http/https request using the options and body. This is a wrapper around [node's http/https](https://nodejs.org/api/https.html#https) request functionality.
|
|
104
|
+
|
|
105
|
+
## SecureVault
|
|
106
|
+
|
|
107
|
+
These BIG-IP only functions will use a BIG-IP's radius server to encrypt and decrypt JSON strings for you.
|
|
108
|
+
|
|
109
|
+
### encrypt(string)
|
|
110
|
+
|
|
111
|
+
This function will take in a string and return an encrypted array from that string.
|
|
112
|
+
|
|
113
|
+
### decrypt(string)
|
|
114
|
+
|
|
115
|
+
This function will take in an encrypted string and return an unencrypted string.
|
|
116
|
+
|
|
117
|
+
## Tracer
|
|
118
|
+
|
|
119
|
+
This lib contains classes and functions that facilitate performance tracing, with current implementation using `Jaeger`, which follows OpenTelemetry (see <https://opentelemetry.io/docs/concepts/data-sources/> for basics)
|
|
120
|
+
|
|
121
|
+
**Performance tracing is not intended for production use**. But since tracing is integrated with the rest of the production code, a `Tracer` has to exist, and will then act as no-op if it's not enabled upon instantiation (default).
|
|
122
|
+
|
|
123
|
+
### Builds
|
|
124
|
+
|
|
125
|
+
#### jaeger-client
|
|
126
|
+
|
|
127
|
+
The `jaeger-client` is listed as an optional dependency and will only be loaded/required at tracer initialization and only if tracing is enabled. This is so that we don't include the modules when creating a production build.
|
|
128
|
+
|
|
129
|
+
It is recommended that projects use a separate script for creating a trace-enabled build. For example, a production build might be created using `npm run build` which uses `npm ci --only=production --no-optional`, while a perf build might be created using `npm run buildperf` which uses `npm ci --only=production`, which then allows the optional depenedencies to be included.
|
|
130
|
+
|
|
131
|
+
#### error-package
|
|
132
|
+
|
|
133
|
+
Jaeger throws an error for older node versions (specifically with dependency `thriftrw -> error`) wherein a read-only name property is being modified. Consequently we had to modify package-lock.json and pin it to 7.2.1., otherwise, `thriftw` uses its own subdirectory of node_modules with the older error version of 7.0.2.
|
|
134
|
+
|
|
135
|
+
### Settings
|
|
136
|
+
|
|
137
|
+
There are two ways to enable a tracer upon instantiation:
|
|
138
|
+
|
|
139
|
+
- by passing through constructor options, or
|
|
140
|
+
- by setting env variables
|
|
141
|
+
- This is an option for quick dev debugging or for projects that might not have persisted settings yet.
|
|
142
|
+
- App settings are generally loaded after app start is completed; env variables serve as backup to enable trace and get information early on while app is starting.
|
|
143
|
+
- For projects using iControl REST Framework: restnoded is specific in how it's started and run file needs to be updated to include the env variables)
|
|
144
|
+
|
|
145
|
+
Example variables:
|
|
146
|
+
|
|
147
|
+
```
|
|
148
|
+
export F5_PERF_TRACING_ENDPOINT=http://{jaegerHost}:{jaegerPort}/api/traces
|
|
149
|
+
export F5_PERF_TRACING_DEBUG=true
|
|
150
|
+
export F5_PERF_TRACING_ENABLED=true
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
### Usage
|
|
154
|
+
|
|
155
|
+
Spans
|
|
156
|
+
|
|
157
|
+
- A span is a basic unit of trace. A span can have other related spans, either at the same level, or as a child span. A child span denotes a subset of some operation(s) done within a parent span.
|
|
158
|
+
- A span can have log (events) associated with it, as well as tags (key-value pair of attributes pertaining to the span).
|
|
159
|
+
- Sample use given a typical REST endpoint with url `/shared/myProject/myResource/{myResourceId}` which allows POST to create a new resource
|
|
160
|
+
- Using `tracer.startHttpSpan()` at the beginning of the endpoint handler when request is received will create:
|
|
161
|
+
|
|
162
|
+
- A new span with the api path `/shared/myProject/myResource/{myResourceId}`.The `{myResourceId}` here is literal - we don't want spans for each resourceId.
|
|
163
|
+
- The relevant http tags (e.g. `http.url:shared/myProject/myResource/resourceId123`, `http.method:POST`)
|
|
164
|
+
|
|
165
|
+
- Using `tracer.startChildSpan()` in subsequent operations, (e.g. inside `validateResource()` then again in `createNewResource()`) and passing the previous http span as the parent arg will create those child spans with their own start and end time, tags and events. This allows for further drill down and inspection.
|
|
166
|
+
|
|
167
|
+
Tracer.close()
|
|
168
|
+
|
|
169
|
+
- It is important to explicitly call `tracer.close()` when tracer is no longer needed to flush out any pending operations and avoid memory leaks.
|
|
170
|
+
|
|
171
|
+
## License
|
|
172
|
+
|
|
173
|
+
[Apache License 2.0](https://choosealicense.com/licenses/apache-2.0/)
|
|
174
|
+
|
|
175
|
+
## Copyright
|
|
176
|
+
|
|
177
|
+
Copyright 2014-2024 F5 Networks Inc.
|
package/index.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2021 F5 Networks, Inc.
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
'use strict';
|
|
18
|
+
|
|
19
|
+
const arrayUtils = require('./src/arrayUtils');
|
|
20
|
+
const ipUtils = require('./src/ipUtils');
|
|
21
|
+
const promiseUtils = require('./src/promiseUtils');
|
|
22
|
+
const requestUtils = require('./src/request');
|
|
23
|
+
const secureVault = require('./src/secureVault');
|
|
24
|
+
const tracer = require('./src/tracer');
|
|
25
|
+
|
|
26
|
+
module.exports = {
|
|
27
|
+
arrayUtils,
|
|
28
|
+
ipUtils,
|
|
29
|
+
promiseUtils,
|
|
30
|
+
requestUtils,
|
|
31
|
+
secureVault,
|
|
32
|
+
tracer
|
|
33
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,60 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
"name": "@ohos-ports/atg-shared-utilities",
|
|
3
|
+
"version": "0.10.12-beta.1",
|
|
4
|
+
"scripts": {
|
|
5
|
+
"lint": "eslint .",
|
|
6
|
+
"test": "mocha test/unit/ --recursive",
|
|
7
|
+
"coverage": "nyc npm test"
|
|
8
|
+
},
|
|
9
|
+
"dependencies": {
|
|
10
|
+
"big-integer": "^1.6.52"
|
|
11
|
+
},
|
|
12
|
+
"devDependencies": {
|
|
13
|
+
"@f5devcentral/eslint-config-f5-atg": "^0.1.8",
|
|
14
|
+
"chai": "^4.5.0",
|
|
15
|
+
"chai-as-promised": "^7.1.2",
|
|
16
|
+
"eslint": "^8.57.1",
|
|
17
|
+
"mocha": "^10.8.2",
|
|
18
|
+
"nock": "10.0.0",
|
|
19
|
+
"nyc": "^15.1.0",
|
|
20
|
+
"sinon": "7.5.0"
|
|
21
|
+
},
|
|
22
|
+
"eslintConfig": {
|
|
23
|
+
"extends": "@f5devcentral/eslint-config-f5-atg",
|
|
24
|
+
"rules": {
|
|
25
|
+
"func-names": "off"
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
"nyc": {
|
|
29
|
+
"reporter": [
|
|
30
|
+
"html",
|
|
31
|
+
"text",
|
|
32
|
+
"json-summary"
|
|
33
|
+
],
|
|
34
|
+
"exclude": [
|
|
35
|
+
"gulpfile.js",
|
|
36
|
+
"coverage/**",
|
|
37
|
+
"test/**",
|
|
38
|
+
"**/node_modules/**",
|
|
39
|
+
"**/dist/**"
|
|
40
|
+
]
|
|
41
|
+
},
|
|
42
|
+
"description": "This project holds a number of generic utility functions that can be shared across the F5 Automation Toolchain projects.",
|
|
43
|
+
"main": "index.js",
|
|
44
|
+
"directories": {
|
|
45
|
+
"test": "test"
|
|
46
|
+
},
|
|
47
|
+
"repository": {
|
|
48
|
+
"type": "git",
|
|
49
|
+
"url": "git@github.com:f5devcentral/f5-atg-shared-utilities.git"
|
|
50
|
+
},
|
|
51
|
+
"author": "F5 Networks",
|
|
52
|
+
"license": "Apache-2.0",
|
|
53
|
+
"optionalDependencies": {
|
|
54
|
+
"error": "7.2.1",
|
|
55
|
+
"jaeger-client": "^3.19.0"
|
|
56
|
+
},
|
|
57
|
+
"files": [
|
|
58
|
+
"src"
|
|
59
|
+
]
|
|
60
|
+
}
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2026 F5, Inc.
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
'use strict';
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Takes a variable, converts it to an array (if it is not an array), and returns it.
|
|
21
|
+
*
|
|
22
|
+
* @param {any} input - The item to be converted to an array
|
|
23
|
+
*
|
|
24
|
+
* @returns {any[]} - The input inside an array (or just the input if it was already an array).
|
|
25
|
+
*/
|
|
26
|
+
const ensureArray = function (input) {
|
|
27
|
+
if (typeof input === 'undefined') {
|
|
28
|
+
return [];
|
|
29
|
+
}
|
|
30
|
+
return (Array.isArray(input)) ? input : [input];
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Checks if any member of tArray is present in array
|
|
35
|
+
*
|
|
36
|
+
* @public
|
|
37
|
+
* @param {array} arr1 - check base array
|
|
38
|
+
* @param {array} arr2 - the target array to compare with
|
|
39
|
+
* @returns {boolean} - true any of arr2 is in arr1, else false
|
|
40
|
+
*/
|
|
41
|
+
const doesArrayContainAnyOf = function (arr1, arr2) {
|
|
42
|
+
if (typeof arr1 === 'undefined' || typeof arr2 === 'undefined') {
|
|
43
|
+
return false;
|
|
44
|
+
}
|
|
45
|
+
return arr1.some((r) => arr2.indexOf(r) >= 0);
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Checks if any member of tArray is present in array
|
|
50
|
+
*
|
|
51
|
+
* @public
|
|
52
|
+
* @param {array} array - check base array
|
|
53
|
+
* @param {value} target - the target value of any type
|
|
54
|
+
* @returns {boolean} - true if target is in the array, else false
|
|
55
|
+
*/
|
|
56
|
+
const doesArrayContain = function (array, target) {
|
|
57
|
+
if (typeof array === 'undefined') {
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
return array.indexOf(target) !== -1;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
const getIndex = function (array, target, comparison) {
|
|
64
|
+
let targetIndex;
|
|
65
|
+
if (comparison === 'eq') {
|
|
66
|
+
targetIndex = array.findIndex((element) => element === target);
|
|
67
|
+
} else {
|
|
68
|
+
targetIndex = array.findIndex((element) => element.indexOf(target) > -1);
|
|
69
|
+
}
|
|
70
|
+
return targetIndex;
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
const getLastIndex = function (array, target, comparison) {
|
|
74
|
+
let targetIndex;
|
|
75
|
+
for (targetIndex = array.length - 1; targetIndex >= 0; targetIndex -= 1) {
|
|
76
|
+
if (comparison === 'eq') {
|
|
77
|
+
if (array[targetIndex] === target) {
|
|
78
|
+
break;
|
|
79
|
+
}
|
|
80
|
+
} else if (array[targetIndex].indexOf(target) > -1) {
|
|
81
|
+
break;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
return targetIndex;
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Inserts an item after another item in an array or at the beginning of the array.
|
|
89
|
+
*
|
|
90
|
+
* @param {array} array - The array into which to insert
|
|
91
|
+
* @param {string} target - The string to search for
|
|
92
|
+
* @param {string} item - The item to insert
|
|
93
|
+
* @param {string} comparison - The type of comparison to perform.
|
|
94
|
+
* 'eq' to do an '===' comparison
|
|
95
|
+
* 'inc' to see if the item is contained in an element of the array
|
|
96
|
+
*/
|
|
97
|
+
const insertAfterOrAtBeginning = function (array, target, item, comparison) {
|
|
98
|
+
const targetIndex = getLastIndex(array, target, comparison);
|
|
99
|
+
|
|
100
|
+
if (targetIndex > -1) {
|
|
101
|
+
array.splice(targetIndex + 1, 0, item);
|
|
102
|
+
} else {
|
|
103
|
+
array.unshift(item);
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Inserts an item before another item in an array or at the beginning of the array.
|
|
109
|
+
*
|
|
110
|
+
* @param {array} array - The array into which to insert
|
|
111
|
+
* @param {string} target - The string to search for
|
|
112
|
+
* @param {string} item - The item to insert
|
|
113
|
+
* @param {string} comparison - The type of comparison to perform.
|
|
114
|
+
* 'eq' to do an '===' comparison
|
|
115
|
+
* 'inc' to see if the item is contained in an element of the array
|
|
116
|
+
*/
|
|
117
|
+
const insertBeforeOrAtBeginning = function (array, target, item, comparison) {
|
|
118
|
+
const targetIndex = getIndex(array, target, comparison);
|
|
119
|
+
|
|
120
|
+
if (targetIndex > -1) {
|
|
121
|
+
array.splice(targetIndex, 0, item);
|
|
122
|
+
} else {
|
|
123
|
+
array.unshift(item);
|
|
124
|
+
}
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Inserts an item after another item in an array or at the end of the array.
|
|
129
|
+
*
|
|
130
|
+
* @param {array} array - The array into which to insert
|
|
131
|
+
* @param {string} target - The string to search for
|
|
132
|
+
* @param {string} item - The item to insert
|
|
133
|
+
* @param {string} comparison - The type of comparison to perform.
|
|
134
|
+
* 'eq' to do an '===' comparison
|
|
135
|
+
* 'inc' to see if the item is contained in an element of the array
|
|
136
|
+
*/
|
|
137
|
+
const insertAfterOrAtEnd = function (array, target, item, comparison) {
|
|
138
|
+
const targetIndex = getLastIndex(array, target, comparison);
|
|
139
|
+
|
|
140
|
+
if (targetIndex > -1) {
|
|
141
|
+
array.splice(targetIndex + 1, 0, item);
|
|
142
|
+
} else {
|
|
143
|
+
array.push(item);
|
|
144
|
+
}
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Inserts an item before another item in an array or at the end of the array.
|
|
149
|
+
*
|
|
150
|
+
* @param {array} array - The array into which to insert
|
|
151
|
+
* @param {string} target - The string to search for
|
|
152
|
+
* @param {string} item - The item to insert
|
|
153
|
+
* @param {string} comparison - The type of comparison to perform.
|
|
154
|
+
* 'eq' to do an '===' comparison
|
|
155
|
+
* 'inc' to see if the item is contained in an element of the array
|
|
156
|
+
*/
|
|
157
|
+
const insertBeforeOrAtEnd = function (array, target, item, comparison) {
|
|
158
|
+
const targetIndex = getIndex(array, target, comparison);
|
|
159
|
+
|
|
160
|
+
if (targetIndex > -1) {
|
|
161
|
+
array.splice(targetIndex, 0, item);
|
|
162
|
+
} else {
|
|
163
|
+
array.push(item);
|
|
164
|
+
}
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
module.exports = {
|
|
168
|
+
ensureArray,
|
|
169
|
+
doesArrayContainAnyOf,
|
|
170
|
+
doesArrayContain,
|
|
171
|
+
insertAfterOrAtBeginning,
|
|
172
|
+
insertAfterOrAtEnd,
|
|
173
|
+
insertBeforeOrAtBeginning,
|
|
174
|
+
insertBeforeOrAtEnd
|
|
175
|
+
};
|