@itwin/core-bentley 3.4.0-dev.9 → 3.5.0-dev.4
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/CHANGELOG.md +25 -1
- package/lib/cjs/AccessToken.d.ts +8 -8
- package/lib/cjs/AccessToken.js +9 -9
- package/lib/cjs/AccessToken.js.map +1 -1
- package/lib/cjs/Assert.d.ts +25 -25
- package/lib/cjs/Assert.js +45 -45
- package/lib/cjs/Assert.js.map +1 -1
- package/lib/cjs/AsyncMutex.d.ts +26 -26
- package/lib/cjs/AsyncMutex.js +43 -43
- package/lib/cjs/AsyncMutex.js.map +1 -1
- package/lib/cjs/BeEvent.d.ts +81 -81
- package/lib/cjs/BeEvent.js +156 -156
- package/lib/cjs/BeEvent.js.map +1 -1
- package/lib/cjs/BeSQLite.d.ts +170 -170
- package/lib/cjs/BeSQLite.js +183 -183
- package/lib/cjs/BeSQLite.js.map +1 -1
- package/lib/cjs/BentleyError.d.ts +376 -376
- package/lib/cjs/BentleyError.js +699 -699
- package/lib/cjs/BentleyError.js.map +1 -1
- package/lib/cjs/BentleyLoggerCategory.d.ts +11 -11
- package/lib/cjs/BentleyLoggerCategory.js +19 -19
- package/lib/cjs/BentleyLoggerCategory.js.map +1 -1
- package/lib/cjs/ByteStream.d.ts +92 -92
- package/lib/cjs/ByteStream.d.ts.map +1 -1
- package/lib/cjs/ByteStream.js +133 -123
- package/lib/cjs/ByteStream.js.map +1 -1
- package/lib/cjs/ClassUtils.d.ts +14 -14
- package/lib/cjs/ClassUtils.js +27 -27
- package/lib/cjs/ClassUtils.js.map +1 -1
- package/lib/cjs/Compare.d.ts +47 -47
- package/lib/cjs/Compare.d.ts.map +1 -1
- package/lib/cjs/Compare.js +75 -63
- package/lib/cjs/Compare.js.map +1 -1
- package/lib/cjs/CompressedId64Set.d.ts +134 -134
- package/lib/cjs/CompressedId64Set.js +428 -428
- package/lib/cjs/CompressedId64Set.js.map +1 -1
- package/lib/cjs/Dictionary.d.ts +125 -125
- package/lib/cjs/Dictionary.d.ts.map +1 -1
- package/lib/cjs/Dictionary.js +203 -203
- package/lib/cjs/Dictionary.js.map +1 -1
- package/lib/cjs/Disposable.d.ts +80 -80
- package/lib/cjs/Disposable.js +120 -120
- package/lib/cjs/Disposable.js.map +1 -1
- package/lib/cjs/Id.d.ts +277 -277
- package/lib/cjs/Id.d.ts.map +1 -1
- package/lib/cjs/Id.js +629 -619
- package/lib/cjs/Id.js.map +1 -1
- package/lib/cjs/IndexMap.d.ts +65 -65
- package/lib/cjs/IndexMap.js +91 -91
- package/lib/cjs/IndexMap.js.map +1 -1
- package/lib/cjs/JsonSchema.d.ts +77 -77
- package/lib/cjs/JsonSchema.js +9 -9
- package/lib/cjs/JsonSchema.js.map +1 -1
- package/lib/cjs/JsonUtils.d.ts +78 -78
- package/lib/cjs/JsonUtils.d.ts.map +1 -1
- package/lib/cjs/JsonUtils.js +151 -133
- package/lib/cjs/JsonUtils.js.map +1 -1
- package/lib/cjs/LRUMap.d.ts +129 -129
- package/lib/cjs/LRUMap.d.ts.map +1 -1
- package/lib/cjs/LRUMap.js +333 -333
- package/lib/cjs/LRUMap.js.map +1 -1
- package/lib/cjs/Logger.d.ts +141 -141
- package/lib/cjs/Logger.js +254 -254
- package/lib/cjs/Logger.js.map +1 -1
- package/lib/cjs/ObservableSet.d.ts +23 -23
- package/lib/cjs/ObservableSet.js +51 -51
- package/lib/cjs/ObservableSet.js.map +1 -1
- package/lib/cjs/OneAtATimeAction.d.ts +31 -31
- package/lib/cjs/OneAtATimeAction.d.ts.map +1 -1
- package/lib/cjs/OneAtATimeAction.js +94 -91
- package/lib/cjs/OneAtATimeAction.js.map +1 -1
- package/lib/cjs/OrderedId64Iterable.d.ts +74 -74
- package/lib/cjs/OrderedId64Iterable.js +235 -235
- package/lib/cjs/OrderedId64Iterable.js.map +1 -1
- package/lib/cjs/OrderedSet.d.ts +40 -40
- package/lib/cjs/OrderedSet.js +64 -64
- package/lib/cjs/OrderedSet.js.map +1 -1
- package/lib/cjs/PriorityQueue.d.ts +70 -70
- package/lib/cjs/PriorityQueue.js +140 -140
- package/lib/cjs/PriorityQueue.js.map +1 -1
- package/lib/cjs/ProcessDetector.d.ts +59 -59
- package/lib/cjs/ProcessDetector.js +71 -71
- package/lib/cjs/ProcessDetector.js.map +1 -1
- package/lib/cjs/SortedArray.d.ts +232 -232
- package/lib/cjs/SortedArray.d.ts.map +1 -1
- package/lib/cjs/SortedArray.js +303 -301
- package/lib/cjs/SortedArray.js.map +1 -1
- package/lib/cjs/StatusCategory.d.ts +30 -30
- package/lib/cjs/StatusCategory.js +460 -460
- package/lib/cjs/StatusCategory.js.map +1 -1
- package/lib/cjs/StringUtils.d.ts +22 -22
- package/lib/cjs/StringUtils.d.ts.map +1 -1
- package/lib/cjs/StringUtils.js +148 -146
- package/lib/cjs/StringUtils.js.map +1 -1
- package/lib/cjs/Time.d.ts +122 -122
- package/lib/cjs/Time.d.ts.map +1 -1
- package/lib/cjs/Time.js +152 -146
- package/lib/cjs/Time.js.map +1 -1
- package/lib/cjs/Tracing.d.ts +40 -40
- package/lib/cjs/Tracing.d.ts.map +1 -1
- package/lib/cjs/Tracing.js +130 -127
- package/lib/cjs/Tracing.js.map +1 -1
- package/lib/cjs/TypedArrayBuilder.d.ts +98 -0
- package/lib/cjs/TypedArrayBuilder.d.ts.map +1 -0
- package/lib/cjs/TypedArrayBuilder.js +135 -0
- package/lib/cjs/TypedArrayBuilder.js.map +1 -0
- package/lib/cjs/UnexpectedErrors.d.ts +43 -43
- package/lib/cjs/UnexpectedErrors.d.ts.map +1 -1
- package/lib/cjs/UnexpectedErrors.js +68 -65
- package/lib/cjs/UnexpectedErrors.js.map +1 -1
- package/lib/cjs/UtilityTypes.d.ts +86 -86
- package/lib/cjs/UtilityTypes.js +26 -26
- package/lib/cjs/UtilityTypes.js.map +1 -1
- package/lib/cjs/YieldManager.d.ts +18 -18
- package/lib/cjs/YieldManager.js +34 -34
- package/lib/cjs/YieldManager.js.map +1 -1
- package/lib/cjs/core-bentley.d.ts +74 -73
- package/lib/cjs/core-bentley.d.ts.map +1 -1
- package/lib/cjs/core-bentley.js +90 -89
- package/lib/cjs/core-bentley.js.map +1 -1
- package/lib/cjs/partitionArray.d.ts +21 -21
- package/lib/cjs/partitionArray.js +43 -43
- package/lib/cjs/partitionArray.js.map +1 -1
- package/lib/esm/AccessToken.d.ts +8 -8
- package/lib/esm/AccessToken.js +8 -8
- package/lib/esm/AccessToken.js.map +1 -1
- package/lib/esm/Assert.d.ts +25 -25
- package/lib/esm/Assert.js +41 -41
- package/lib/esm/Assert.js.map +1 -1
- package/lib/esm/AsyncMutex.d.ts +26 -26
- package/lib/esm/AsyncMutex.js +39 -39
- package/lib/esm/AsyncMutex.js.map +1 -1
- package/lib/esm/BeEvent.d.ts +81 -81
- package/lib/esm/BeEvent.js +150 -150
- package/lib/esm/BeEvent.js.map +1 -1
- package/lib/esm/BeSQLite.d.ts +170 -170
- package/lib/esm/BeSQLite.js +180 -180
- package/lib/esm/BeSQLite.js.map +1 -1
- package/lib/esm/BentleyError.d.ts +376 -376
- package/lib/esm/BentleyError.js +695 -695
- package/lib/esm/BentleyError.js.map +1 -1
- package/lib/esm/BentleyLoggerCategory.d.ts +11 -11
- package/lib/esm/BentleyLoggerCategory.js +16 -16
- package/lib/esm/BentleyLoggerCategory.js.map +1 -1
- package/lib/esm/ByteStream.d.ts +92 -92
- package/lib/esm/ByteStream.d.ts.map +1 -1
- package/lib/esm/ByteStream.js +129 -119
- package/lib/esm/ByteStream.js.map +1 -1
- package/lib/esm/ClassUtils.d.ts +14 -14
- package/lib/esm/ClassUtils.js +22 -22
- package/lib/esm/ClassUtils.js.map +1 -1
- package/lib/esm/Compare.d.ts +47 -47
- package/lib/esm/Compare.d.ts.map +1 -1
- package/lib/esm/Compare.js +63 -51
- package/lib/esm/Compare.js.map +1 -1
- package/lib/esm/CompressedId64Set.d.ts +134 -134
- package/lib/esm/CompressedId64Set.js +423 -423
- package/lib/esm/CompressedId64Set.js.map +1 -1
- package/lib/esm/Dictionary.d.ts +125 -125
- package/lib/esm/Dictionary.d.ts.map +1 -1
- package/lib/esm/Dictionary.js +199 -199
- package/lib/esm/Dictionary.js.map +1 -1
- package/lib/esm/Disposable.d.ts +80 -80
- package/lib/esm/Disposable.js +112 -112
- package/lib/esm/Disposable.js.map +1 -1
- package/lib/esm/Id.d.ts +277 -277
- package/lib/esm/Id.d.ts.map +1 -1
- package/lib/esm/Id.js +625 -615
- package/lib/esm/Id.js.map +1 -1
- package/lib/esm/IndexMap.d.ts +65 -65
- package/lib/esm/IndexMap.js +86 -86
- package/lib/esm/IndexMap.js.map +1 -1
- package/lib/esm/JsonSchema.d.ts +77 -77
- package/lib/esm/JsonSchema.js +8 -8
- package/lib/esm/JsonSchema.js.map +1 -1
- package/lib/esm/JsonUtils.d.ts +78 -78
- package/lib/esm/JsonUtils.d.ts.map +1 -1
- package/lib/esm/JsonUtils.js +148 -130
- package/lib/esm/JsonUtils.js.map +1 -1
- package/lib/esm/LRUMap.d.ts +129 -129
- package/lib/esm/LRUMap.d.ts.map +1 -1
- package/lib/esm/LRUMap.js +326 -326
- package/lib/esm/LRUMap.js.map +1 -1
- package/lib/esm/Logger.d.ts +141 -141
- package/lib/esm/Logger.js +249 -249
- package/lib/esm/Logger.js.map +1 -1
- package/lib/esm/ObservableSet.d.ts +23 -23
- package/lib/esm/ObservableSet.js +47 -47
- package/lib/esm/ObservableSet.js.map +1 -1
- package/lib/esm/OneAtATimeAction.d.ts +31 -31
- package/lib/esm/OneAtATimeAction.d.ts.map +1 -1
- package/lib/esm/OneAtATimeAction.js +89 -86
- package/lib/esm/OneAtATimeAction.js.map +1 -1
- package/lib/esm/OrderedId64Iterable.d.ts +74 -74
- package/lib/esm/OrderedId64Iterable.js +232 -232
- package/lib/esm/OrderedId64Iterable.js.map +1 -1
- package/lib/esm/OrderedSet.d.ts +40 -40
- package/lib/esm/OrderedSet.js +59 -59
- package/lib/esm/OrderedSet.js.map +1 -1
- package/lib/esm/PriorityQueue.d.ts +70 -70
- package/lib/esm/PriorityQueue.js +136 -136
- package/lib/esm/PriorityQueue.js.map +1 -1
- package/lib/esm/ProcessDetector.d.ts +59 -59
- package/lib/esm/ProcessDetector.js +67 -67
- package/lib/esm/ProcessDetector.js.map +1 -1
- package/lib/esm/SortedArray.d.ts +232 -232
- package/lib/esm/SortedArray.d.ts.map +1 -1
- package/lib/esm/SortedArray.js +296 -294
- package/lib/esm/SortedArray.js.map +1 -1
- package/lib/esm/StatusCategory.d.ts +30 -30
- package/lib/esm/StatusCategory.js +454 -454
- package/lib/esm/StatusCategory.js.map +1 -1
- package/lib/esm/StringUtils.d.ts +22 -22
- package/lib/esm/StringUtils.d.ts.map +1 -1
- package/lib/esm/StringUtils.js +142 -140
- package/lib/esm/StringUtils.js.map +1 -1
- package/lib/esm/Time.d.ts +122 -122
- package/lib/esm/Time.d.ts.map +1 -1
- package/lib/esm/Time.js +146 -140
- package/lib/esm/Time.js.map +1 -1
- package/lib/esm/Tracing.d.ts +40 -40
- package/lib/esm/Tracing.d.ts.map +1 -1
- package/lib/esm/Tracing.js +126 -123
- package/lib/esm/Tracing.js.map +1 -1
- package/lib/esm/TypedArrayBuilder.d.ts +98 -0
- package/lib/esm/TypedArrayBuilder.d.ts.map +1 -0
- package/lib/esm/TypedArrayBuilder.js +128 -0
- package/lib/esm/TypedArrayBuilder.js.map +1 -0
- package/lib/esm/UnexpectedErrors.d.ts +43 -43
- package/lib/esm/UnexpectedErrors.d.ts.map +1 -1
- package/lib/esm/UnexpectedErrors.js +64 -61
- package/lib/esm/UnexpectedErrors.js.map +1 -1
- package/lib/esm/UtilityTypes.d.ts +86 -86
- package/lib/esm/UtilityTypes.js +21 -21
- package/lib/esm/UtilityTypes.js.map +1 -1
- package/lib/esm/YieldManager.d.ts +18 -18
- package/lib/esm/YieldManager.js +30 -30
- package/lib/esm/YieldManager.js.map +1 -1
- package/lib/esm/core-bentley.d.ts +74 -73
- package/lib/esm/core-bentley.d.ts.map +1 -1
- package/lib/esm/core-bentley.js +78 -77
- package/lib/esm/core-bentley.js.map +1 -1
- package/lib/esm/partitionArray.d.ts +21 -21
- package/lib/esm/partitionArray.js +39 -39
- package/lib/esm/partitionArray.js.map +1 -1
- package/package.json +5 -5
package/lib/esm/Tracing.d.ts
CHANGED
|
@@ -1,41 +1,41 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
/**
|
|
3
|
-
* Mirrors the SpanKind enum from [@opentelemetry/api](https://open-telemetry.github.io/opentelemetry-js-api/enums/spankind)
|
|
4
|
-
* @alpha
|
|
5
|
-
*/
|
|
6
|
-
export declare enum SpanKind {
|
|
7
|
-
INTERNAL = 0,
|
|
8
|
-
SERVER = 1,
|
|
9
|
-
CLIENT = 2,
|
|
10
|
-
PRODUCER = 3,
|
|
11
|
-
CONSUMER = 4
|
|
12
|
-
}
|
|
13
|
-
/**
|
|
14
|
-
* Enables OpenTelemetry tracing in addition to traditional logging.
|
|
15
|
-
* @alpha
|
|
16
|
-
*/
|
|
17
|
-
export declare class Tracing {
|
|
18
|
-
private static _tracer?;
|
|
19
|
-
private static _openTelemetry?;
|
|
20
|
-
/**
|
|
21
|
-
* If OpenTelemetry tracing is enabled, creates a new span and runs the provided function in it.
|
|
22
|
-
* If OpenTelemetry tracing is _not_ enabled, runs the provided function.
|
|
23
|
-
* @param name name of the new span
|
|
24
|
-
* @param fn function to run inside the new span
|
|
25
|
-
* @param options span options
|
|
26
|
-
* @param parentContext optional context used to retrieve parent span id
|
|
27
|
-
*/
|
|
28
|
-
static withSpan<T>(name: string, fn: () => Promise<T>, options?: SpanOptions, parentContext?: SpanContext): Promise<T>;
|
|
29
|
-
/**
|
|
30
|
-
* Enable logging to OpenTelemetry. [[Tracing.withSpan]] will be enabled, all log entries will be attached to active span as span events.
|
|
31
|
-
* [[IModelHost.startup]] will call this automatically if it succeeds in requiring `@opentelemetry/api`.
|
|
32
|
-
* @note Node.js OpenTelemetry SDK should be initialized by the user.
|
|
33
|
-
*/
|
|
34
|
-
static enableOpenTelemetry(tracer: Tracer, api: typeof Tracing._openTelemetry): void;
|
|
35
|
-
private static withOpenTelemetry;
|
|
36
|
-
/** Set attributes on currently active openTelemetry span. Doesn't do anything if openTelemetry logging is not initialized.
|
|
37
|
-
* @param attributes The attributes to set
|
|
38
|
-
*/
|
|
39
|
-
static setAttributes(attributes:
|
|
40
|
-
}
|
|
1
|
+
import type { SpanAttributes, SpanContext, SpanOptions, Tracer } from "@opentelemetry/api";
|
|
2
|
+
/**
|
|
3
|
+
* Mirrors the SpanKind enum from [@opentelemetry/api](https://open-telemetry.github.io/opentelemetry-js-api/enums/spankind)
|
|
4
|
+
* @alpha
|
|
5
|
+
*/
|
|
6
|
+
export declare enum SpanKind {
|
|
7
|
+
INTERNAL = 0,
|
|
8
|
+
SERVER = 1,
|
|
9
|
+
CLIENT = 2,
|
|
10
|
+
PRODUCER = 3,
|
|
11
|
+
CONSUMER = 4
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Enables OpenTelemetry tracing in addition to traditional logging.
|
|
15
|
+
* @alpha
|
|
16
|
+
*/
|
|
17
|
+
export declare class Tracing {
|
|
18
|
+
private static _tracer?;
|
|
19
|
+
private static _openTelemetry?;
|
|
20
|
+
/**
|
|
21
|
+
* If OpenTelemetry tracing is enabled, creates a new span and runs the provided function in it.
|
|
22
|
+
* If OpenTelemetry tracing is _not_ enabled, runs the provided function.
|
|
23
|
+
* @param name name of the new span
|
|
24
|
+
* @param fn function to run inside the new span
|
|
25
|
+
* @param options span options
|
|
26
|
+
* @param parentContext optional context used to retrieve parent span id
|
|
27
|
+
*/
|
|
28
|
+
static withSpan<T>(name: string, fn: () => Promise<T>, options?: SpanOptions, parentContext?: SpanContext): Promise<T>;
|
|
29
|
+
/**
|
|
30
|
+
* Enable logging to OpenTelemetry. [[Tracing.withSpan]] will be enabled, all log entries will be attached to active span as span events.
|
|
31
|
+
* [[IModelHost.startup]] will call this automatically if it succeeds in requiring `@opentelemetry/api`.
|
|
32
|
+
* @note Node.js OpenTelemetry SDK should be initialized by the user.
|
|
33
|
+
*/
|
|
34
|
+
static enableOpenTelemetry(tracer: Tracer, api: typeof Tracing._openTelemetry): void;
|
|
35
|
+
private static withOpenTelemetry;
|
|
36
|
+
/** Set attributes on currently active openTelemetry span. Doesn't do anything if openTelemetry logging is not initialized.
|
|
37
|
+
* @param attributes The attributes to set
|
|
38
|
+
*/
|
|
39
|
+
static setAttributes(attributes: SpanAttributes): void;
|
|
40
|
+
}
|
|
41
41
|
//# sourceMappingURL=Tracing.d.ts.map
|
package/lib/esm/Tracing.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Tracing.d.ts","sourceRoot":"","sources":["../../src/Tracing.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"Tracing.d.ts","sourceRoot":"","sources":["../../src/Tracing.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAc,cAAc,EAAsB,WAAW,EAAE,WAAW,EAAY,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAIrI;;;GAGG;AACH,oBAAY,QAAQ;IAClB,QAAQ,IAAI;IACZ,MAAM,IAAI;IACV,MAAM,IAAI;IACV,QAAQ,IAAI;IACZ,QAAQ,IAAI;CACb;AA2DD;;;GAGG;AACH,qBAAa,OAAO;IAClB,OAAO,CAAC,MAAM,CAAC,OAAO,CAAC,CAAS;IAChC,OAAO,CAAC,MAAM,CAAC,cAAc,CAAC,CAAoH;IAElJ;;;;;;;OAOG;WACiB,QAAQ,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,EAAE,OAAO,CAAC,EAAE,WAAW,EAAE,aAAa,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,CAAC,CAAC;IA4BnI;;;;OAIG;WACW,mBAAmB,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,EAAE,OAAO,OAAO,CAAC,cAAc;IASpF,OAAO,CAAC,MAAM,CAAC,iBAAiB;IAShC;;OAEG;WACW,aAAa,CAAC,UAAU,EAAE,cAAc;CAGvD"}
|
package/lib/esm/Tracing.js
CHANGED
|
@@ -1,124 +1,127 @@
|
|
|
1
|
-
import { Logger } from "./Logger";
|
|
2
|
-
// re-export so that consumers can construct full SpanOptions object without external dependencies
|
|
3
|
-
/**
|
|
4
|
-
* Mirrors the SpanKind enum from [@opentelemetry/api](https://open-telemetry.github.io/opentelemetry-js-api/enums/spankind)
|
|
5
|
-
* @alpha
|
|
6
|
-
*/
|
|
7
|
-
export var SpanKind;
|
|
8
|
-
(function (SpanKind) {
|
|
9
|
-
SpanKind[SpanKind["INTERNAL"] = 0] = "INTERNAL";
|
|
10
|
-
SpanKind[SpanKind["SERVER"] = 1] = "SERVER";
|
|
11
|
-
SpanKind[SpanKind["CLIENT"] = 2] = "CLIENT";
|
|
12
|
-
SpanKind[SpanKind["PRODUCER"] = 3] = "PRODUCER";
|
|
13
|
-
SpanKind[SpanKind["CONSUMER"] = 4] = "CONSUMER";
|
|
14
|
-
})(SpanKind || (SpanKind = {}));
|
|
15
|
-
function isValidPrimitive(val) {
|
|
16
|
-
return typeof val === "string" || typeof val === "number" || typeof val === "boolean";
|
|
17
|
-
}
|
|
18
|
-
// Only _homogenous_ arrays of strings, numbers, or booleans are supported as OpenTelemetry Attribute values.
|
|
19
|
-
// Per the spec (https://opentelemetry.io/docs/reference/specification/common/common/#attribute), empty arrays and null values are supported too.
|
|
20
|
-
function isValidPrimitiveArray(val) {
|
|
21
|
-
if (!Array.isArray(val))
|
|
22
|
-
return false;
|
|
23
|
-
let itemType;
|
|
24
|
-
for (const x of val) {
|
|
25
|
-
if (x === undefined || x === null)
|
|
26
|
-
continue;
|
|
27
|
-
if (!itemType) {
|
|
28
|
-
itemType = typeof x;
|
|
29
|
-
if (!isValidPrimitive(x))
|
|
30
|
-
return false;
|
|
31
|
-
}
|
|
32
|
-
if (typeof x !== itemType)
|
|
33
|
-
return false;
|
|
34
|
-
}
|
|
35
|
-
return true;
|
|
36
|
-
}
|
|
37
|
-
function isPlainObject(obj) {
|
|
38
|
-
return typeof obj === "object" && obj !== null && Object.getPrototypeOf(obj) === Object.prototype;
|
|
39
|
-
}
|
|
40
|
-
function* getFlatEntries(obj, path = "") {
|
|
41
|
-
if (isValidPrimitiveArray(obj)) {
|
|
42
|
-
yield [path, obj];
|
|
43
|
-
return;
|
|
44
|
-
}
|
|
45
|
-
// Prefer JSON serialization over flattening for any non-POJO types.
|
|
46
|
-
// There's just too many ways trying to flatten those can go wrong (Dates, Buffers, TypedArrays, etc.)
|
|
47
|
-
if (!isPlainObject(obj) && !Array.isArray(obj)) {
|
|
48
|
-
yield [path, isValidPrimitive(obj) ? obj : JSON.stringify(obj)];
|
|
49
|
-
return;
|
|
50
|
-
}
|
|
51
|
-
// Always serialize empty objects/arrays as empty array values
|
|
52
|
-
const entries = Object.entries(obj);
|
|
53
|
-
if (entries.length === 0)
|
|
54
|
-
yield [path, []];
|
|
55
|
-
for (const [key, val] of entries)
|
|
56
|
-
yield* getFlatEntries(val, (path === "") ? key : `${path}.${key}`);
|
|
57
|
-
}
|
|
58
|
-
function flattenObject(obj) {
|
|
59
|
-
return Object.fromEntries(getFlatEntries(obj));
|
|
60
|
-
}
|
|
61
|
-
/**
|
|
62
|
-
* Enables OpenTelemetry tracing in addition to traditional logging.
|
|
63
|
-
* @alpha
|
|
64
|
-
*/
|
|
65
|
-
export class Tracing {
|
|
66
|
-
/**
|
|
67
|
-
* If OpenTelemetry tracing is enabled, creates a new span and runs the provided function in it.
|
|
68
|
-
* If OpenTelemetry tracing is _not_ enabled, runs the provided function.
|
|
69
|
-
* @param name name of the new span
|
|
70
|
-
* @param fn function to run inside the new span
|
|
71
|
-
* @param options span options
|
|
72
|
-
* @param parentContext optional context used to retrieve parent span id
|
|
73
|
-
*/
|
|
74
|
-
static async withSpan(name, fn, options, parentContext) {
|
|
75
|
-
if (Tracing._tracer === undefined || Tracing._openTelemetry === undefined)
|
|
76
|
-
return fn();
|
|
77
|
-
// this case is for context propagation - parentContext is typically constructed from HTTP headers
|
|
78
|
-
const parent = parentContext === undefined
|
|
79
|
-
? Tracing._openTelemetry.context.active()
|
|
80
|
-
: Tracing._openTelemetry.trace.setSpanContext(Tracing._openTelemetry.context.active(), parentContext);
|
|
81
|
-
return Tracing._openTelemetry.context.with(Tracing._openTelemetry.trace.setSpan(parent, Tracing._tracer.startSpan(name, options, Tracing._openTelemetry.context.active())), async () => {
|
|
82
|
-
var _a, _b, _c, _d;
|
|
83
|
-
try {
|
|
84
|
-
return await fn();
|
|
85
|
-
}
|
|
86
|
-
catch (err) {
|
|
87
|
-
if (err instanceof Error) // ignore non-Error throws, such as RpcControlResponse
|
|
88
|
-
(_b = (_a = Tracing._openTelemetry) === null || _a === void 0 ? void 0 : _a.trace.getSpan(Tracing._openTelemetry.context.active())) === null || _b === void 0 ? void 0 : _b.setAttribute("error", true);
|
|
89
|
-
throw err;
|
|
90
|
-
}
|
|
91
|
-
finally {
|
|
92
|
-
(_d = (_c = Tracing._openTelemetry) === null || _c === void 0 ? void 0 : _c.trace.getSpan(Tracing._openTelemetry.context.active())) === null || _d === void 0 ? void 0 : _d.end();
|
|
93
|
-
}
|
|
94
|
-
});
|
|
95
|
-
}
|
|
96
|
-
/**
|
|
97
|
-
* Enable logging to OpenTelemetry. [[Tracing.withSpan]] will be enabled, all log entries will be attached to active span as span events.
|
|
98
|
-
* [[IModelHost.startup]] will call this automatically if it succeeds in requiring `@opentelemetry/api`.
|
|
99
|
-
* @note Node.js OpenTelemetry SDK should be initialized by the user.
|
|
100
|
-
*/
|
|
101
|
-
static enableOpenTelemetry(tracer, api) {
|
|
102
|
-
Tracing._tracer = tracer;
|
|
103
|
-
Tracing._openTelemetry = api;
|
|
104
|
-
Logger.logTrace = Tracing.withOpenTelemetry(Logger.logTrace);
|
|
105
|
-
Logger.logInfo = Tracing.withOpenTelemetry(Logger.logInfo);
|
|
106
|
-
Logger.logWarning = Tracing.withOpenTelemetry(Logger.logWarning);
|
|
107
|
-
Logger.logError = Tracing.withOpenTelemetry(Logger.logError);
|
|
108
|
-
}
|
|
109
|
-
static withOpenTelemetry(base, isError = false) {
|
|
110
|
-
return (category, message, metaData) => {
|
|
111
|
-
var _a, _b;
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
1
|
+
import { Logger } from "./Logger";
|
|
2
|
+
// re-export so that consumers can construct full SpanOptions object without external dependencies
|
|
3
|
+
/**
|
|
4
|
+
* Mirrors the SpanKind enum from [@opentelemetry/api](https://open-telemetry.github.io/opentelemetry-js-api/enums/spankind)
|
|
5
|
+
* @alpha
|
|
6
|
+
*/
|
|
7
|
+
export var SpanKind;
|
|
8
|
+
(function (SpanKind) {
|
|
9
|
+
SpanKind[SpanKind["INTERNAL"] = 0] = "INTERNAL";
|
|
10
|
+
SpanKind[SpanKind["SERVER"] = 1] = "SERVER";
|
|
11
|
+
SpanKind[SpanKind["CLIENT"] = 2] = "CLIENT";
|
|
12
|
+
SpanKind[SpanKind["PRODUCER"] = 3] = "PRODUCER";
|
|
13
|
+
SpanKind[SpanKind["CONSUMER"] = 4] = "CONSUMER";
|
|
14
|
+
})(SpanKind || (SpanKind = {}));
|
|
15
|
+
function isValidPrimitive(val) {
|
|
16
|
+
return typeof val === "string" || typeof val === "number" || typeof val === "boolean";
|
|
17
|
+
}
|
|
18
|
+
// Only _homogenous_ arrays of strings, numbers, or booleans are supported as OpenTelemetry Attribute values.
|
|
19
|
+
// Per the spec (https://opentelemetry.io/docs/reference/specification/common/common/#attribute), empty arrays and null values are supported too.
|
|
20
|
+
function isValidPrimitiveArray(val) {
|
|
21
|
+
if (!Array.isArray(val))
|
|
22
|
+
return false;
|
|
23
|
+
let itemType;
|
|
24
|
+
for (const x of val) {
|
|
25
|
+
if (x === undefined || x === null)
|
|
26
|
+
continue;
|
|
27
|
+
if (!itemType) {
|
|
28
|
+
itemType = typeof x;
|
|
29
|
+
if (!isValidPrimitive(x))
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
if (typeof x !== itemType)
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
return true;
|
|
36
|
+
}
|
|
37
|
+
function isPlainObject(obj) {
|
|
38
|
+
return typeof obj === "object" && obj !== null && Object.getPrototypeOf(obj) === Object.prototype;
|
|
39
|
+
}
|
|
40
|
+
function* getFlatEntries(obj, path = "") {
|
|
41
|
+
if (isValidPrimitiveArray(obj)) {
|
|
42
|
+
yield [path, obj];
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
// Prefer JSON serialization over flattening for any non-POJO types.
|
|
46
|
+
// There's just too many ways trying to flatten those can go wrong (Dates, Buffers, TypedArrays, etc.)
|
|
47
|
+
if (!isPlainObject(obj) && !Array.isArray(obj)) {
|
|
48
|
+
yield [path, isValidPrimitive(obj) ? obj : JSON.stringify(obj)];
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
// Always serialize empty objects/arrays as empty array values
|
|
52
|
+
const entries = Object.entries(obj);
|
|
53
|
+
if (entries.length === 0)
|
|
54
|
+
yield [path, []];
|
|
55
|
+
for (const [key, val] of entries)
|
|
56
|
+
yield* getFlatEntries(val, (path === "") ? key : `${path}.${key}`);
|
|
57
|
+
}
|
|
58
|
+
function flattenObject(obj) {
|
|
59
|
+
return Object.fromEntries(getFlatEntries(obj));
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Enables OpenTelemetry tracing in addition to traditional logging.
|
|
63
|
+
* @alpha
|
|
64
|
+
*/
|
|
65
|
+
export class Tracing {
|
|
66
|
+
/**
|
|
67
|
+
* If OpenTelemetry tracing is enabled, creates a new span and runs the provided function in it.
|
|
68
|
+
* If OpenTelemetry tracing is _not_ enabled, runs the provided function.
|
|
69
|
+
* @param name name of the new span
|
|
70
|
+
* @param fn function to run inside the new span
|
|
71
|
+
* @param options span options
|
|
72
|
+
* @param parentContext optional context used to retrieve parent span id
|
|
73
|
+
*/
|
|
74
|
+
static async withSpan(name, fn, options, parentContext) {
|
|
75
|
+
if (Tracing._tracer === undefined || Tracing._openTelemetry === undefined)
|
|
76
|
+
return fn();
|
|
77
|
+
// this case is for context propagation - parentContext is typically constructed from HTTP headers
|
|
78
|
+
const parent = parentContext === undefined
|
|
79
|
+
? Tracing._openTelemetry.context.active()
|
|
80
|
+
: Tracing._openTelemetry.trace.setSpanContext(Tracing._openTelemetry.context.active(), parentContext);
|
|
81
|
+
return Tracing._openTelemetry.context.with(Tracing._openTelemetry.trace.setSpan(parent, Tracing._tracer.startSpan(name, options, Tracing._openTelemetry.context.active())), async () => {
|
|
82
|
+
var _a, _b, _c, _d;
|
|
83
|
+
try {
|
|
84
|
+
return await fn();
|
|
85
|
+
}
|
|
86
|
+
catch (err) {
|
|
87
|
+
if (err instanceof Error) // ignore non-Error throws, such as RpcControlResponse
|
|
88
|
+
(_b = (_a = Tracing._openTelemetry) === null || _a === void 0 ? void 0 : _a.trace.getSpan(Tracing._openTelemetry.context.active())) === null || _b === void 0 ? void 0 : _b.setAttribute("error", true);
|
|
89
|
+
throw err;
|
|
90
|
+
}
|
|
91
|
+
finally {
|
|
92
|
+
(_d = (_c = Tracing._openTelemetry) === null || _c === void 0 ? void 0 : _c.trace.getSpan(Tracing._openTelemetry.context.active())) === null || _d === void 0 ? void 0 : _d.end();
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Enable logging to OpenTelemetry. [[Tracing.withSpan]] will be enabled, all log entries will be attached to active span as span events.
|
|
98
|
+
* [[IModelHost.startup]] will call this automatically if it succeeds in requiring `@opentelemetry/api`.
|
|
99
|
+
* @note Node.js OpenTelemetry SDK should be initialized by the user.
|
|
100
|
+
*/
|
|
101
|
+
static enableOpenTelemetry(tracer, api) {
|
|
102
|
+
Tracing._tracer = tracer;
|
|
103
|
+
Tracing._openTelemetry = api;
|
|
104
|
+
Logger.logTrace = Tracing.withOpenTelemetry(Logger.logTrace);
|
|
105
|
+
Logger.logInfo = Tracing.withOpenTelemetry(Logger.logInfo);
|
|
106
|
+
Logger.logWarning = Tracing.withOpenTelemetry(Logger.logWarning);
|
|
107
|
+
Logger.logError = Tracing.withOpenTelemetry(Logger.logError);
|
|
108
|
+
}
|
|
109
|
+
static withOpenTelemetry(base, isError = false) {
|
|
110
|
+
return (category, message, metaData) => {
|
|
111
|
+
var _a, _b;
|
|
112
|
+
try {
|
|
113
|
+
(_b = (_a = Tracing._openTelemetry) === null || _a === void 0 ? void 0 : _a.trace.getSpan(Tracing._openTelemetry.context.active())) === null || _b === void 0 ? void 0 : _b.addEvent(message, { ...flattenObject(Logger.getMetaData(metaData)), error: isError });
|
|
114
|
+
}
|
|
115
|
+
catch (_e) { } // avoid throwing random errors (with stack trace mangled by async hooks) when openTelemetry collector doesn't work
|
|
116
|
+
base(category, message, metaData);
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
/** Set attributes on currently active openTelemetry span. Doesn't do anything if openTelemetry logging is not initialized.
|
|
120
|
+
* @param attributes The attributes to set
|
|
121
|
+
*/
|
|
122
|
+
static setAttributes(attributes) {
|
|
123
|
+
var _a, _b;
|
|
124
|
+
(_b = (_a = Tracing._openTelemetry) === null || _a === void 0 ? void 0 : _a.trace.getSpan(Tracing._openTelemetry.context.active())) === null || _b === void 0 ? void 0 : _b.setAttributes(attributes);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
124
127
|
//# sourceMappingURL=Tracing.js.map
|
package/lib/esm/Tracing.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Tracing.js","sourceRoot":"","sources":["../../src/Tracing.ts"],"names":[],"mappings":"AAKA,OAAO,EAAe,MAAM,EAAE,MAAM,U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C,UAAU,GAAG,OAAO,CAAC,iBAAiB,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC;QACjE,MAAM,CAAC,QAAQ,GAAG,OAAO,CAAC,iBAAiB,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;IAC/D,CAAC;IAEO,MAAM,CAAC,iBAAiB,CAAC,IAAiB,EAAE,UAAmB,KAAK;QAC1E,OAAO,CAAC,QAAQ,EAAE,OAAO,EAAE,QAAQ,EAAE,EAAE;;YACrC,MAAA,MAAA,OAAO,CAAC,cAAc,0CAAE,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,cAAc,CAAC,OAAO,CAAC,MAAM,EAAE,CAAC,0CAAE,QAAQ,CAAC,OAAO,EAAE,EAAE,GAAG,aAAa,CAAC,MAAM,CAAC,WAAW,CAAC,QAAQ,CAAC,CAAC,EAAE,KAAK,EAAE,OAAO,EAAE,CAAC,CAAC;YACtK,IAAI,CAAC,QAAQ,EAAE,OAAO,EAAE,QAAQ,CAAC,CAAC;QACpC,CAAC,CAAC;IACJ,CAAC;IAED;;OAEG;IACI,MAAM,CAAC,aAAa,CAAC,UAAsB;;QAChD,MAAA,MAAA,OAAO,CAAC,cAAc,0CAAE,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,cAAc,CAAC,OAAO,CAAC,MAAM,EAAE,CAAC,0CAAE,aAAa,CAAC,UAAU,CAAC,CAAC;IAC5G,CAAC;CACF","sourcesContent":["/*---------------------------------------------------------------------------------------------\r\n* Copyright (c) Bentley Systems, Incorporated. All rights reserved.\r\n* See LICENSE.md in the project root for license terms and full copyright notice.\r\n*--------------------------------------------------------------------------------------------*/\r\nimport type { Attributes, AttributeValue, ContextAPI, SpanContext, SpanOptions, TraceAPI, Tracer } from \"@opentelemetry/api\";\r\nimport { LogFunction, Logger } from \"./Logger\";\r\n\r\n// re-export so that consumers can construct full SpanOptions object without external dependencies\r\n/**\r\n * Mirrors the SpanKind enum from [@opentelemetry/api](https://open-telemetry.github.io/opentelemetry-js-api/enums/spankind)\r\n * @alpha\r\n */\r\nexport enum SpanKind {\r\n INTERNAL = 0,\r\n SERVER = 1,\r\n CLIENT = 2,\r\n PRODUCER = 3,\r\n CONSUMER = 4\r\n}\r\n\r\nfunction isValidPrimitive(val: unknown): val is AttributeValue {\r\n return typeof val === \"string\" || typeof val === \"number\" || typeof val === \"boolean\";\r\n}\r\n\r\n// Only _homogenous_ arrays of strings, numbers, or booleans are supported as OpenTelemetry Attribute values.\r\n// Per the spec (https://opentelemetry.io/docs/reference/specification/common/common/#attribute), empty arrays and null values are supported too.\r\nfunction isValidPrimitiveArray(val: unknown): val is AttributeValue {\r\n if (!Array.isArray(val))\r\n return false;\r\n\r\n let itemType;\r\n for (const x of val) {\r\n if (x === undefined || x === null)\r\n continue;\r\n\r\n if (!itemType) {\r\n itemType = typeof x;\r\n if (!isValidPrimitive(x))\r\n return false;\r\n }\r\n\r\n if (typeof x !== itemType)\r\n return false;\r\n }\r\n return true;\r\n}\r\n\r\nfunction isPlainObject(obj: unknown): obj is object {\r\n return typeof obj === \"object\" && obj !== null && Object.getPrototypeOf(obj) === Object.prototype;\r\n}\r\n\r\nfunction* getFlatEntries(obj: unknown, path = \"\"): Iterable<[string, AttributeValue]> {\r\n if (isValidPrimitiveArray(obj)) {\r\n yield [path, obj];\r\n return;\r\n }\r\n\r\n // Prefer JSON serialization over flattening for any non-POJO types.\r\n // There's just too many ways trying to flatten those can go wrong (Dates, Buffers, TypedArrays, etc.)\r\n if (!isPlainObject(obj) && !Array.isArray(obj)) {\r\n yield [path, isValidPrimitive(obj) ? obj : JSON.stringify(obj)];\r\n return;\r\n }\r\n\r\n // Always serialize empty objects/arrays as empty array values\r\n const entries = Object.entries(obj);\r\n if (entries.length === 0)\r\n yield [path, []];\r\n\r\n for (const [key, val] of entries)\r\n yield* getFlatEntries(val, (path === \"\") ? key : `${path}.${key}`);\r\n}\r\n\r\nfunction flattenObject(obj: object): Attributes {\r\n return Object.fromEntries(getFlatEntries(obj));\r\n}\r\n\r\n/**\r\n * Enables OpenTelemetry tracing in addition to traditional logging.\r\n * @alpha\r\n */\r\nexport class Tracing {\r\n private static _tracer?: Tracer;\r\n private static _openTelemetry?: { trace: Pick<TraceAPI, \"setSpan\" | \"setSpanContext\" | \"getSpan\">, context: Pick<ContextAPI, \"active\" | \"with\"> };\r\n\r\n /**\r\n * If OpenTelemetry tracing is enabled, creates a new span and runs the provided function in it.\r\n * If OpenTelemetry tracing is _not_ enabled, runs the provided function.\r\n * @param name name of the new span\r\n * @param fn function to run inside the new span\r\n * @param options span options\r\n * @param parentContext optional context used to retrieve parent span id\r\n */\r\n public static async withSpan<T>(name: string, fn: () => Promise<T>, options?: SpanOptions, parentContext?: SpanContext): Promise<T> {\r\n if (Tracing._tracer === undefined || Tracing._openTelemetry === undefined)\r\n return fn();\r\n\r\n // this case is for context propagation - parentContext is typically constructed from HTTP headers\r\n const parent = parentContext === undefined\r\n ? Tracing._openTelemetry.context.active()\r\n : Tracing._openTelemetry.trace.setSpanContext(Tracing._openTelemetry.context.active(), parentContext);\r\n\r\n return Tracing._openTelemetry.context.with(\r\n Tracing._openTelemetry.trace.setSpan(\r\n parent,\r\n Tracing._tracer.startSpan(name, options, Tracing._openTelemetry.context.active())\r\n ),\r\n async () => {\r\n try {\r\n return await fn();\r\n } catch (err) {\r\n if (err instanceof Error) // ignore non-Error throws, such as RpcControlResponse\r\n Tracing._openTelemetry?.trace.getSpan(Tracing._openTelemetry.context.active())?.setAttribute(\"error\", true);\r\n throw err;\r\n } finally {\r\n Tracing._openTelemetry?.trace.getSpan(Tracing._openTelemetry.context.active())?.end();\r\n }\r\n },\r\n );\r\n }\r\n\r\n /**\r\n * Enable logging to OpenTelemetry. [[Tracing.withSpan]] will be enabled, all log entries will be attached to active span as span events.\r\n * [[IModelHost.startup]] will call this automatically if it succeeds in requiring `@opentelemetry/api`.\r\n * @note Node.js OpenTelemetry SDK should be initialized by the user.\r\n */\r\n public static enableOpenTelemetry(tracer: Tracer, api: typeof Tracing._openTelemetry) {\r\n Tracing._tracer = tracer;\r\n Tracing._openTelemetry = api;\r\n Logger.logTrace = Tracing.withOpenTelemetry(Logger.logTrace);\r\n Logger.logInfo = Tracing.withOpenTelemetry(Logger.logInfo);\r\n Logger.logWarning = Tracing.withOpenTelemetry(Logger.logWarning);\r\n Logger.logError = Tracing.withOpenTelemetry(Logger.logError);\r\n }\r\n\r\n private static withOpenTelemetry(base: LogFunction, isError: boolean = false): LogFunction {\r\n return (category, message, metaData) => {\r\n Tracing._openTelemetry?.trace.getSpan(Tracing._openTelemetry.context.active())?.addEvent(message, { ...flattenObject(Logger.getMetaData(metaData)), error: isError });\r\n base(category, message, metaData);\r\n };\r\n }\r\n\r\n /** Set attributes on currently active openTelemetry span. Doesn't do anything if openTelemetry logging is not initialized.\r\n * @param attributes The attributes to set\r\n */\r\n public static setAttributes(attributes: Attributes) {\r\n Tracing._openTelemetry?.trace.getSpan(Tracing._openTelemetry.context.active())?.setAttributes(attributes);\r\n }\r\n}\r\n"]}
|
|
1
|
+
{"version":3,"file":"Tracing.js","sourceRoot":"","sources":["../../src/Tracing.ts"],"names":[],"mappings":"AAKA,OAAO,EAAe,MAAM,EAAE,MAAM,UAAU,CAAC;AAE/C,kGAAkG;AAClG;;;GAGG;AACH,MAAM,CAAN,IAAY,QAMX;AAND,WAAY,QAAQ;IAClB,+CAAY,CAAA;IACZ,2CAAU,CAAA;IACV,2CAAU,CAAA;IACV,+CAAY,CAAA;IACZ,+CAAY,CAAA;AACd,CAAC,EANW,QAAQ,KAAR,QAAQ,QAMnB;AAED,SAAS,gBAAgB,CAAC,GAAY;IACpC,OAAO,OAAO,GAAG,KAAK,QAAQ,IAAI,OAAO,GAAG,KAAK,QAAQ,IAAI,OAAO,GAAG,KAAK,SAAS,CAAC;AACxF,CAAC;AAED,6GAA6G;AAC7G,iJAAiJ;AACjJ,SAAS,qBAAqB,CAAC,GAAY;IACzC,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,GAAG,CAA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AAM,CAAC,WAAW,CAAC,QAAQ,CAAC,CAAC,EAAE,KAAK,EAAE,OAAO,EAAE,CAAC,CAAC;aACvK;YAAC,OAAO,EAAE,EAAE,GAAG,CAAC,mHAAmH;YACpI,IAAI,CAAC,QAAQ,EAAE,OAAO,EAAE,QAAQ,CAAC,CAAC;QACpC,CAAC,CAAC;IACJ,CAAC;IAED;;OAEG;IACI,MAAM,CAAC,aAAa,CAAC,UAA0B;;QACpD,MAAA,MAAA,OAAO,CAAC,cAAc,0CAAE,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,cAAc,CAAC,OAAO,CAAC,MAAM,EAAE,CAAC,0CAAE,aAAa,CAAC,UAAU,CAAC,CAAC;IAC5G,CAAC;CACF","sourcesContent":["/*---------------------------------------------------------------------------------------------\n* Copyright (c) Bentley Systems, Incorporated. All rights reserved.\n* See LICENSE.md in the project root for license terms and full copyright notice.\n*--------------------------------------------------------------------------------------------*/\nimport type { ContextAPI, SpanAttributes, SpanAttributeValue, SpanContext, SpanOptions, TraceAPI, Tracer } from \"@opentelemetry/api\";\nimport { LogFunction, Logger } from \"./Logger\";\n\n// re-export so that consumers can construct full SpanOptions object without external dependencies\n/**\n * Mirrors the SpanKind enum from [@opentelemetry/api](https://open-telemetry.github.io/opentelemetry-js-api/enums/spankind)\n * @alpha\n */\nexport enum SpanKind {\n INTERNAL = 0,\n SERVER = 1,\n CLIENT = 2,\n PRODUCER = 3,\n CONSUMER = 4\n}\n\nfunction isValidPrimitive(val: unknown): val is SpanAttributeValue {\n return typeof val === \"string\" || typeof val === \"number\" || typeof val === \"boolean\";\n}\n\n// Only _homogenous_ arrays of strings, numbers, or booleans are supported as OpenTelemetry Attribute values.\n// Per the spec (https://opentelemetry.io/docs/reference/specification/common/common/#attribute), empty arrays and null values are supported too.\nfunction isValidPrimitiveArray(val: unknown): val is SpanAttributeValue {\n if (!Array.isArray(val))\n return false;\n\n let itemType;\n for (const x of val) {\n if (x === undefined || x === null)\n continue;\n\n if (!itemType) {\n itemType = typeof x;\n if (!isValidPrimitive(x))\n return false;\n }\n\n if (typeof x !== itemType)\n return false;\n }\n return true;\n}\n\nfunction isPlainObject(obj: unknown): obj is object {\n return typeof obj === \"object\" && obj !== null && Object.getPrototypeOf(obj) === Object.prototype;\n}\n\nfunction* getFlatEntries(obj: unknown, path = \"\"): Iterable<[string, SpanAttributeValue]> {\n if (isValidPrimitiveArray(obj)) {\n yield [path, obj];\n return;\n }\n\n // Prefer JSON serialization over flattening for any non-POJO types.\n // There's just too many ways trying to flatten those can go wrong (Dates, Buffers, TypedArrays, etc.)\n if (!isPlainObject(obj) && !Array.isArray(obj)) {\n yield [path, isValidPrimitive(obj) ? obj : JSON.stringify(obj)];\n return;\n }\n\n // Always serialize empty objects/arrays as empty array values\n const entries = Object.entries(obj);\n if (entries.length === 0)\n yield [path, []];\n\n for (const [key, val] of entries)\n yield* getFlatEntries(val, (path === \"\") ? key : `${path}.${key}`);\n}\n\nfunction flattenObject(obj: object): SpanAttributes {\n return Object.fromEntries(getFlatEntries(obj));\n}\n\n/**\n * Enables OpenTelemetry tracing in addition to traditional logging.\n * @alpha\n */\nexport class Tracing {\n private static _tracer?: Tracer;\n private static _openTelemetry?: { trace: Pick<TraceAPI, \"setSpan\" | \"setSpanContext\" | \"getSpan\">, context: Pick<ContextAPI, \"active\" | \"with\"> };\n\n /**\n * If OpenTelemetry tracing is enabled, creates a new span and runs the provided function in it.\n * If OpenTelemetry tracing is _not_ enabled, runs the provided function.\n * @param name name of the new span\n * @param fn function to run inside the new span\n * @param options span options\n * @param parentContext optional context used to retrieve parent span id\n */\n public static async withSpan<T>(name: string, fn: () => Promise<T>, options?: SpanOptions, parentContext?: SpanContext): Promise<T> {\n if (Tracing._tracer === undefined || Tracing._openTelemetry === undefined)\n return fn();\n\n // this case is for context propagation - parentContext is typically constructed from HTTP headers\n const parent = parentContext === undefined\n ? Tracing._openTelemetry.context.active()\n : Tracing._openTelemetry.trace.setSpanContext(Tracing._openTelemetry.context.active(), parentContext);\n\n return Tracing._openTelemetry.context.with(\n Tracing._openTelemetry.trace.setSpan(\n parent,\n Tracing._tracer.startSpan(name, options, Tracing._openTelemetry.context.active())\n ),\n async () => {\n try {\n return await fn();\n } catch (err) {\n if (err instanceof Error) // ignore non-Error throws, such as RpcControlResponse\n Tracing._openTelemetry?.trace.getSpan(Tracing._openTelemetry.context.active())?.setAttribute(\"error\", true);\n throw err;\n } finally {\n Tracing._openTelemetry?.trace.getSpan(Tracing._openTelemetry.context.active())?.end();\n }\n },\n );\n }\n\n /**\n * Enable logging to OpenTelemetry. [[Tracing.withSpan]] will be enabled, all log entries will be attached to active span as span events.\n * [[IModelHost.startup]] will call this automatically if it succeeds in requiring `@opentelemetry/api`.\n * @note Node.js OpenTelemetry SDK should be initialized by the user.\n */\n public static enableOpenTelemetry(tracer: Tracer, api: typeof Tracing._openTelemetry) {\n Tracing._tracer = tracer;\n Tracing._openTelemetry = api;\n Logger.logTrace = Tracing.withOpenTelemetry(Logger.logTrace);\n Logger.logInfo = Tracing.withOpenTelemetry(Logger.logInfo);\n Logger.logWarning = Tracing.withOpenTelemetry(Logger.logWarning);\n Logger.logError = Tracing.withOpenTelemetry(Logger.logError);\n }\n\n private static withOpenTelemetry(base: LogFunction, isError: boolean = false): LogFunction {\n return (category, message, metaData) => {\n try {\n Tracing._openTelemetry?.trace.getSpan(Tracing._openTelemetry.context.active())?.addEvent(message, { ...flattenObject(Logger.getMetaData(metaData)), error: isError });\n } catch (_e) { } // avoid throwing random errors (with stack trace mangled by async hooks) when openTelemetry collector doesn't work\n base(category, message, metaData);\n };\n }\n\n /** Set attributes on currently active openTelemetry span. Doesn't do anything if openTelemetry logging is not initialized.\n * @param attributes The attributes to set\n */\n public static setAttributes(attributes: SpanAttributes) {\n Tracing._openTelemetry?.trace.getSpan(Tracing._openTelemetry.context.active())?.setAttributes(attributes);\n }\n}\n"]}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
/** @packageDocumentation
|
|
2
|
+
* @module Collections
|
|
3
|
+
*/
|
|
4
|
+
import { Constructor } from "./UtilityTypes";
|
|
5
|
+
/** Options used to construct a [[TypedArrayBuilder]].
|
|
6
|
+
* @public
|
|
7
|
+
*/
|
|
8
|
+
export interface TypedArrayBuilderOptions {
|
|
9
|
+
/** Controls how much additional memory is allocated when the TypedArray needs to be resized to accomodate more elements.
|
|
10
|
+
* [[TypedArrayBuilder.ensureCapacity]] multiplies the required capacity by this factor to determine the new capacity.
|
|
11
|
+
* Default: 1.5
|
|
12
|
+
* Minimum: 1.0, which causes the TypedArray to allocate exactly the space it needs each time it is resized.
|
|
13
|
+
*/
|
|
14
|
+
growthFactor?: number;
|
|
15
|
+
/** The number of elements to allocate memory for in the TypedArray when creating the builder.
|
|
16
|
+
* If you know the minimum number of elements you intend to add to the builder, you should specify that as the
|
|
17
|
+
* initial capacity to avoid reallocations when populating the array.
|
|
18
|
+
* Default: 0.
|
|
19
|
+
* Minimum: 0.
|
|
20
|
+
*/
|
|
21
|
+
initialCapacity?: number;
|
|
22
|
+
}
|
|
23
|
+
/** Incrementally builds a [TypedArray] of unsigned 8-, 16-, or 32-bit integers.
|
|
24
|
+
* Sometimes you wish to populate a `TypedArray`, but you don't know how many elements you will need.
|
|
25
|
+
* `TypedArray` requires you to specify the size upon construction, and does not permit you to change the size later.
|
|
26
|
+
*
|
|
27
|
+
* `TypedArrayBuilder` manages a `TypedArray`, permitting you to [[push]] and [[append]] elements to it. It exposes two "size" properties":
|
|
28
|
+
* - [[capacity]], the number of elements it has currently allocated space for - i.e., the length of the underlying TypedArray; and
|
|
29
|
+
* - [[length]], the number of elements that have so far been added to it, which is never bigger than [[capacity]].
|
|
30
|
+
* When [[capacity]] is exceeded, a new, bigger TypedArray is allocated and the contents of the previous array are copied over to it.
|
|
31
|
+
*
|
|
32
|
+
* Once you've finished adding elements, you can obtain the finished `TypedArray` via [[toTypedArray]].
|
|
33
|
+
* @see [[Uint8ArrayBuilder]], [[Uint16ArrayBuilder]], and [[Uint32ArrayBuilder]] to build specific types of arrays.
|
|
34
|
+
* @public
|
|
35
|
+
*/
|
|
36
|
+
export declare class TypedArrayBuilder<T extends Uint8Array | Uint16Array | Uint32Array> {
|
|
37
|
+
/** The constructor for the specific type of array being populated. */
|
|
38
|
+
protected readonly _constructor: Constructor<T>;
|
|
39
|
+
/** The underlying typed array, to be reallocated and copied when its capacity is exceeded. */
|
|
40
|
+
protected _data: T;
|
|
41
|
+
/** The number of elements added to the array so far. */
|
|
42
|
+
protected _length: number;
|
|
43
|
+
/** Multiplier applied to required capacity by [[ensureCapacity]]. */
|
|
44
|
+
readonly growthFactor: number;
|
|
45
|
+
/** Constructs a new builder from the specified options, with a [[length]] of zero. */
|
|
46
|
+
protected constructor(constructor: Constructor<T>, options?: TypedArrayBuilderOptions);
|
|
47
|
+
/** The number of elements currently in the array. */
|
|
48
|
+
get length(): number;
|
|
49
|
+
/** The number of elements that can fit into the memory currently allocated for the array. */
|
|
50
|
+
get capacity(): number;
|
|
51
|
+
/** Like [TypedArray.at](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/at),
|
|
52
|
+
* returns the element at the specified index, with negative integers counting back from the end of the array.
|
|
53
|
+
* @note It is your responsibility to ensure the index falls within the bounds of the array.
|
|
54
|
+
*/
|
|
55
|
+
at(index: number): number;
|
|
56
|
+
/** Ensure that [[capacity]] is at least equal to `newCapacity`.
|
|
57
|
+
* This is used internally by methods like [[push]] and [[append]] to ensure the array has room for the element(s) to be added.
|
|
58
|
+
* It can also be useful when you know you intend to add some number of additional elements, to minimize reallocations.
|
|
59
|
+
*
|
|
60
|
+
* If `newCapacity` is not greater than the current [[capacity]], this function does nothing.
|
|
61
|
+
* Otherwise, it allocates a new `TypedArray` with room for `newCapacity * growthFactor` elements, and copies the contents of the previous `TypedArray` into it.
|
|
62
|
+
* [[length]] remains unchanged; [[capacity]] reflects the size of the new TypeArray.
|
|
63
|
+
*/
|
|
64
|
+
ensureCapacity(newCapacity: number): number;
|
|
65
|
+
/** Append the specified value, resizing if necessary. */
|
|
66
|
+
push(value: number): void;
|
|
67
|
+
/** Append an array of values, resizing (at most once) if necessary. */
|
|
68
|
+
append(values: T): void;
|
|
69
|
+
/** Obtain the finished array.
|
|
70
|
+
* @param includeUnusedCapacity If true, the length of the returned array will be equal to [[capacity]], with extra bytes initialized to zero; otherwise, the
|
|
71
|
+
* returned array's length will be equal to [[length]].
|
|
72
|
+
*/
|
|
73
|
+
toTypedArray(includeUnusedCapacity?: boolean): T;
|
|
74
|
+
}
|
|
75
|
+
/** A [[TypedArrayBuilder]] for producing a [Uint8Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array).
|
|
76
|
+
* @public
|
|
77
|
+
*/
|
|
78
|
+
export declare class Uint8ArrayBuilder extends TypedArrayBuilder<Uint8Array> {
|
|
79
|
+
/** @see [[TypedArrayBuilder]] constructor. */
|
|
80
|
+
constructor(options?: TypedArrayBuilderOptions);
|
|
81
|
+
}
|
|
82
|
+
/** A [[TypedArrayBuilder]] for producing a [Uint16Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint16Array).
|
|
83
|
+
* @public
|
|
84
|
+
*/
|
|
85
|
+
export declare class Uint16ArrayBuilder extends TypedArrayBuilder<Uint16Array> {
|
|
86
|
+
/** @see [[TypedArrayBuilder]] constructor. */
|
|
87
|
+
constructor(options?: TypedArrayBuilderOptions);
|
|
88
|
+
}
|
|
89
|
+
/** A [[TypedArrayBuilder]] for producing a [Uint32Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint32Array).
|
|
90
|
+
* @public
|
|
91
|
+
*/
|
|
92
|
+
export declare class Uint32ArrayBuilder extends TypedArrayBuilder<Uint32Array> {
|
|
93
|
+
/** @see [[TypedArrayBuilder]] constructor. */
|
|
94
|
+
constructor(options?: TypedArrayBuilderOptions);
|
|
95
|
+
/** Obtain a view of the finished array as an array of bytes. */
|
|
96
|
+
toUint8Array(includeUnusedCapacity?: boolean): Uint8Array;
|
|
97
|
+
}
|
|
98
|
+
//# sourceMappingURL=TypedArrayBuilder.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"TypedArrayBuilder.d.ts","sourceRoot":"","sources":["../../src/TypedArrayBuilder.ts"],"names":[],"mappings":"AAIA;;GAEG;AAEH,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAG7C;;GAEG;AACH,MAAM,WAAW,wBAAwB;IACvC;;;;OAIG;IACH,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB;;;;;OAKG;IACH,eAAe,CAAC,EAAE,MAAM,CAAC;CAC1B;AAED;;;;;;;;;;;;GAYG;AACH,qBAAa,iBAAiB,CAAC,CAAC,SAAS,UAAU,GAAG,WAAW,GAAG,WAAW;IAC7E,sEAAsE;IACtE,SAAS,CAAC,QAAQ,CAAC,YAAY,EAAE,WAAW,CAAC,CAAC,CAAC,CAAC;IAChD,8FAA8F;IAC9F,SAAS,CAAC,KAAK,EAAE,CAAC,CAAC;IACnB,wDAAwD;IACxD,SAAS,CAAC,OAAO,EAAE,MAAM,CAAC;IAC1B,qEAAqE;IACrE,SAAgB,YAAY,EAAE,MAAM,CAAC;IAErC,sFAAsF;IACtF,SAAS,aAAa,WAAW,EAAE,WAAW,CAAC,CAAC,CAAC,EAAE,OAAO,CAAC,EAAE,wBAAwB;IAOrF,qDAAqD;IACrD,IAAW,MAAM,IAAI,MAAM,CAE1B;IAED,6FAA6F;IAC7F,IAAW,QAAQ,IAAI,MAAM,CAE5B;IAED;;;OAGG;IACI,EAAE,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM;IAShC;;;;;;;OAOG;IACI,cAAc,CAAC,WAAW,EAAE,MAAM,GAAG,MAAM;IAclD,yDAAyD;IAClD,IAAI,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI;IAMhC,uEAAuE;IAChE,MAAM,CAAC,MAAM,EAAE,CAAC,GAAG,IAAI;IAO9B;;;OAGG;IACI,YAAY,CAAC,qBAAqB,UAAQ,GAAG,CAAC;CAStD;AAED;;GAEG;AACH,qBAAa,iBAAkB,SAAQ,iBAAiB,CAAC,UAAU,CAAC;IAClE,8CAA8C;gBAC3B,OAAO,CAAC,EAAE,wBAAwB;CAGtD;AAED;;GAEG;AACH,qBAAa,kBAAmB,SAAQ,iBAAiB,CAAC,WAAW,CAAC;IACpE,8CAA8C;gBAC3B,OAAO,CAAC,EAAE,wBAAwB;CAGtD;AAED;;GAEG;AACH,qBAAa,kBAAmB,SAAQ,iBAAiB,CAAC,WAAW,CAAC;IACpE,8CAA8C;gBAC3B,OAAO,CAAC,EAAE,wBAAwB;IAIrD,gEAAgE;IACzD,YAAY,CAAC,qBAAqB,UAAQ,GAAG,UAAU;CAM/D"}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
/*---------------------------------------------------------------------------------------------
|
|
2
|
+
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
|
|
3
|
+
* See LICENSE.md in the project root for license terms and full copyright notice.
|
|
4
|
+
*--------------------------------------------------------------------------------------------*/
|
|
5
|
+
/** @packageDocumentation
|
|
6
|
+
* @module Collections
|
|
7
|
+
*/
|
|
8
|
+
import { assert } from "./Assert";
|
|
9
|
+
/** Incrementally builds a [TypedArray] of unsigned 8-, 16-, or 32-bit integers.
|
|
10
|
+
* Sometimes you wish to populate a `TypedArray`, but you don't know how many elements you will need.
|
|
11
|
+
* `TypedArray` requires you to specify the size upon construction, and does not permit you to change the size later.
|
|
12
|
+
*
|
|
13
|
+
* `TypedArrayBuilder` manages a `TypedArray`, permitting you to [[push]] and [[append]] elements to it. It exposes two "size" properties":
|
|
14
|
+
* - [[capacity]], the number of elements it has currently allocated space for - i.e., the length of the underlying TypedArray; and
|
|
15
|
+
* - [[length]], the number of elements that have so far been added to it, which is never bigger than [[capacity]].
|
|
16
|
+
* When [[capacity]] is exceeded, a new, bigger TypedArray is allocated and the contents of the previous array are copied over to it.
|
|
17
|
+
*
|
|
18
|
+
* Once you've finished adding elements, you can obtain the finished `TypedArray` via [[toTypedArray]].
|
|
19
|
+
* @see [[Uint8ArrayBuilder]], [[Uint16ArrayBuilder]], and [[Uint32ArrayBuilder]] to build specific types of arrays.
|
|
20
|
+
* @public
|
|
21
|
+
*/
|
|
22
|
+
export class TypedArrayBuilder {
|
|
23
|
+
/** Constructs a new builder from the specified options, with a [[length]] of zero. */
|
|
24
|
+
constructor(constructor, options) {
|
|
25
|
+
var _a, _b;
|
|
26
|
+
this._constructor = constructor;
|
|
27
|
+
this._data = new constructor((_a = options === null || options === void 0 ? void 0 : options.initialCapacity) !== null && _a !== void 0 ? _a : 0);
|
|
28
|
+
this.growthFactor = Math.max(1.0, (_b = options === null || options === void 0 ? void 0 : options.growthFactor) !== null && _b !== void 0 ? _b : 1.5);
|
|
29
|
+
this._length = 0;
|
|
30
|
+
}
|
|
31
|
+
/** The number of elements currently in the array. */
|
|
32
|
+
get length() {
|
|
33
|
+
return this._length;
|
|
34
|
+
}
|
|
35
|
+
/** The number of elements that can fit into the memory currently allocated for the array. */
|
|
36
|
+
get capacity() {
|
|
37
|
+
return this._data.length;
|
|
38
|
+
}
|
|
39
|
+
/** Like [TypedArray.at](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/at),
|
|
40
|
+
* returns the element at the specified index, with negative integers counting back from the end of the array.
|
|
41
|
+
* @note It is your responsibility to ensure the index falls within the bounds of the array.
|
|
42
|
+
*/
|
|
43
|
+
at(index) {
|
|
44
|
+
if (index < 0)
|
|
45
|
+
index = this.length - index;
|
|
46
|
+
const value = this._data[index];
|
|
47
|
+
assert(value !== undefined, "index out of bounds");
|
|
48
|
+
return value;
|
|
49
|
+
}
|
|
50
|
+
/** Ensure that [[capacity]] is at least equal to `newCapacity`.
|
|
51
|
+
* This is used internally by methods like [[push]] and [[append]] to ensure the array has room for the element(s) to be added.
|
|
52
|
+
* It can also be useful when you know you intend to add some number of additional elements, to minimize reallocations.
|
|
53
|
+
*
|
|
54
|
+
* If `newCapacity` is not greater than the current [[capacity]], this function does nothing.
|
|
55
|
+
* Otherwise, it allocates a new `TypedArray` with room for `newCapacity * growthFactor` elements, and copies the contents of the previous `TypedArray` into it.
|
|
56
|
+
* [[length]] remains unchanged; [[capacity]] reflects the size of the new TypeArray.
|
|
57
|
+
*/
|
|
58
|
+
ensureCapacity(newCapacity) {
|
|
59
|
+
if (this.capacity >= newCapacity)
|
|
60
|
+
return this.capacity;
|
|
61
|
+
assert(this.growthFactor >= 1.0);
|
|
62
|
+
newCapacity = Math.ceil(newCapacity * this.growthFactor);
|
|
63
|
+
const prevData = this._data;
|
|
64
|
+
this._data = new this._constructor(newCapacity);
|
|
65
|
+
this._data.set(prevData, 0);
|
|
66
|
+
assert(this.capacity === newCapacity);
|
|
67
|
+
return this.capacity;
|
|
68
|
+
}
|
|
69
|
+
/** Append the specified value, resizing if necessary. */
|
|
70
|
+
push(value) {
|
|
71
|
+
this.ensureCapacity(this.length + 1);
|
|
72
|
+
this._data[this.length] = value;
|
|
73
|
+
++this._length;
|
|
74
|
+
}
|
|
75
|
+
/** Append an array of values, resizing (at most once) if necessary. */
|
|
76
|
+
append(values) {
|
|
77
|
+
const newLength = this.length + values.length;
|
|
78
|
+
this.ensureCapacity(newLength);
|
|
79
|
+
this._data.set(values, this.length);
|
|
80
|
+
this._length = newLength;
|
|
81
|
+
}
|
|
82
|
+
/** Obtain the finished array.
|
|
83
|
+
* @param includeUnusedCapacity If true, the length of the returned array will be equal to [[capacity]], with extra bytes initialized to zero; otherwise, the
|
|
84
|
+
* returned array's length will be equal to [[length]].
|
|
85
|
+
*/
|
|
86
|
+
toTypedArray(includeUnusedCapacity = false) {
|
|
87
|
+
if (includeUnusedCapacity)
|
|
88
|
+
return this._data;
|
|
89
|
+
const subarray = this._data.subarray(0, this.length);
|
|
90
|
+
assert(subarray instanceof this._constructor);
|
|
91
|
+
assert(subarray.buffer === this._data.buffer);
|
|
92
|
+
return subarray;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
/** A [[TypedArrayBuilder]] for producing a [Uint8Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array).
|
|
96
|
+
* @public
|
|
97
|
+
*/
|
|
98
|
+
export class Uint8ArrayBuilder extends TypedArrayBuilder {
|
|
99
|
+
/** @see [[TypedArrayBuilder]] constructor. */
|
|
100
|
+
constructor(options) {
|
|
101
|
+
super(Uint8Array, options);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
/** A [[TypedArrayBuilder]] for producing a [Uint16Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint16Array).
|
|
105
|
+
* @public
|
|
106
|
+
*/
|
|
107
|
+
export class Uint16ArrayBuilder extends TypedArrayBuilder {
|
|
108
|
+
/** @see [[TypedArrayBuilder]] constructor. */
|
|
109
|
+
constructor(options) {
|
|
110
|
+
super(Uint16Array, options);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
/** A [[TypedArrayBuilder]] for producing a [Uint32Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint32Array).
|
|
114
|
+
* @public
|
|
115
|
+
*/
|
|
116
|
+
export class Uint32ArrayBuilder extends TypedArrayBuilder {
|
|
117
|
+
/** @see [[TypedArrayBuilder]] constructor. */
|
|
118
|
+
constructor(options) {
|
|
119
|
+
super(Uint32Array, options);
|
|
120
|
+
}
|
|
121
|
+
/** Obtain a view of the finished array as an array of bytes. */
|
|
122
|
+
toUint8Array(includeUnusedCapacity = false) {
|
|
123
|
+
if (includeUnusedCapacity)
|
|
124
|
+
return new Uint8Array(this._data.buffer);
|
|
125
|
+
return new Uint8Array(this._data.buffer, 0, this.length * 4);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
//# sourceMappingURL=TypedArrayBuilder.js.map
|