@dxos/context 0.8.4-main.fbb7a13 → 0.8.4-main.fcc0d83b33
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/dist/lib/browser/index.mjs +42 -28
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +42 -28
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/types/src/context-disposed-error.d.ts.map +1 -1
- package/dist/types/src/context.d.ts.map +1 -1
- package/dist/types/src/index.d.ts +1 -0
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/promise-utils.d.ts.map +1 -1
- package/dist/types/src/resource.d.ts +1 -1
- package/dist/types/src/resource.d.ts.map +1 -1
- package/dist/types/src/trace-context.d.ts +54 -0
- package/dist/types/src/trace-context.d.ts.map +1 -0
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +5 -8
- package/src/context.ts +1 -0
- package/src/index.ts +1 -0
- package/src/resource.ts +5 -3
- package/src/trace-context.ts +72 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dxos/context",
|
|
3
|
-
"version": "0.8.4-main.
|
|
3
|
+
"version": "0.8.4-main.fcc0d83b33",
|
|
4
4
|
"description": "Async utils.",
|
|
5
5
|
"homepage": "https://dxos.org",
|
|
6
6
|
"bugs": "https://github.com/dxos/dxos/issues",
|
|
@@ -23,19 +23,16 @@
|
|
|
23
23
|
}
|
|
24
24
|
},
|
|
25
25
|
"types": "dist/types/src/index.d.ts",
|
|
26
|
-
"typesVersions": {
|
|
27
|
-
"*": {}
|
|
28
|
-
},
|
|
29
26
|
"files": [
|
|
30
27
|
"dist",
|
|
31
28
|
"src"
|
|
32
29
|
],
|
|
33
30
|
"dependencies": {
|
|
34
31
|
"@hazae41/symbol-dispose-polyfill": "^1.0.2",
|
|
35
|
-
"@dxos/debug": "0.8.4-main.
|
|
36
|
-
"@dxos/
|
|
37
|
-
"@dxos/
|
|
38
|
-
"@dxos/
|
|
32
|
+
"@dxos/debug": "0.8.4-main.fcc0d83b33",
|
|
33
|
+
"@dxos/log": "0.8.4-main.fcc0d83b33",
|
|
34
|
+
"@dxos/util": "0.8.4-main.fcc0d83b33",
|
|
35
|
+
"@dxos/node-std": "0.8.4-main.fcc0d83b33"
|
|
39
36
|
},
|
|
40
37
|
"publishConfig": {
|
|
41
38
|
"access": "public"
|
package/src/context.ts
CHANGED
|
@@ -236,6 +236,7 @@ export class Context {
|
|
|
236
236
|
|
|
237
237
|
derive({ onError, attributes }: CreateContextProps = {}): Context {
|
|
238
238
|
const newCtx = new Context({
|
|
239
|
+
parent: this,
|
|
239
240
|
// TODO(dmaretskyi): Optimize to not require allocating a new closure for every context.
|
|
240
241
|
onError: async (error) => {
|
|
241
242
|
if (!onError) {
|
package/src/index.ts
CHANGED
package/src/resource.ts
CHANGED
|
@@ -2,12 +2,12 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
+
import '@hazae41/symbol-dispose-polyfill';
|
|
6
|
+
|
|
5
7
|
import { throwUnhandledError } from '@dxos/util';
|
|
6
8
|
|
|
7
9
|
import { Context } from './context';
|
|
8
10
|
|
|
9
|
-
import '@hazae41/symbol-dispose-polyfill';
|
|
10
|
-
|
|
11
11
|
export enum LifecycleState {
|
|
12
12
|
CLOSED = 'CLOSED',
|
|
13
13
|
OPEN = 'OPEN',
|
|
@@ -164,6 +164,7 @@ export abstract class Resource implements Lifecycle {
|
|
|
164
164
|
async #open(ctx?: Context): Promise<void> {
|
|
165
165
|
this.#closePromise = null;
|
|
166
166
|
this.#parentCtx = ctx?.derive({ name: this.#name }) ?? this.#createParentContext();
|
|
167
|
+
this.#internalCtx = this.#createContext(this.#parentCtx);
|
|
167
168
|
await this._open(this.#parentCtx);
|
|
168
169
|
this.#lifecycleState = LifecycleState.OPEN;
|
|
169
170
|
}
|
|
@@ -176,9 +177,10 @@ export abstract class Resource implements Lifecycle {
|
|
|
176
177
|
this.#lifecycleState = LifecycleState.CLOSED;
|
|
177
178
|
}
|
|
178
179
|
|
|
179
|
-
#createContext(): Context {
|
|
180
|
+
#createContext(attributeParent?: Context): Context {
|
|
180
181
|
return new Context({
|
|
181
182
|
name: this.#name,
|
|
183
|
+
parent: attributeParent,
|
|
182
184
|
onError: (error) =>
|
|
183
185
|
queueMicrotask(async () => {
|
|
184
186
|
try {
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2026 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { Context } from './context';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Context attribute key for trace context data.
|
|
9
|
+
* Stores {@link TraceContextData} (W3C traceparent/tracestate strings).
|
|
10
|
+
*/
|
|
11
|
+
export const TRACE_SPAN_ATTRIBUTE = 'dxos.trace-span';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* W3C Trace Context wire format for propagating trace identity.
|
|
15
|
+
* Stored on DXOS {@link Context} attributes and carried across RPC boundaries.
|
|
16
|
+
*
|
|
17
|
+
* Because these are plain strings (not live runtime objects), they remain valid
|
|
18
|
+
* after the originating span ends — enabling long-lived contexts (`this._ctx`)
|
|
19
|
+
* to serve as parents for later child spans without a retention cache.
|
|
20
|
+
*
|
|
21
|
+
* @see https://www.w3.org/TR/trace-context/
|
|
22
|
+
*/
|
|
23
|
+
export type TraceContextData = {
|
|
24
|
+
/**
|
|
25
|
+
* W3C `traceparent` header value.
|
|
26
|
+
* Format: `{version}-{traceId}-{spanId}-{traceFlags}` (e.g., `00-abc...def-012...789-01`).
|
|
27
|
+
*/
|
|
28
|
+
traceparent: string;
|
|
29
|
+
/** Optional W3C `tracestate` header value carrying vendor-specific trace data. */
|
|
30
|
+
tracestate?: string;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Codec for propagating trace identity across RPC boundaries.
|
|
35
|
+
*
|
|
36
|
+
* Hardcoded in `RpcPeer` — every outgoing request calls {@link encode} to
|
|
37
|
+
* extract W3C trace context from the DXOS `Context`, and every incoming
|
|
38
|
+
* request calls {@link decode} to reconstruct a DXOS `Context` carrying the
|
|
39
|
+
* caller's trace context.
|
|
40
|
+
*
|
|
41
|
+
* This works because `TRACE_SPAN_ATTRIBUTE` stores serializable
|
|
42
|
+
* {@link TraceContextData} strings, not opaque runtime objects.
|
|
43
|
+
*/
|
|
44
|
+
export class ContextRpcCodec {
|
|
45
|
+
/**
|
|
46
|
+
* Read the W3C trace context from a DXOS `Context` for an outgoing RPC.
|
|
47
|
+
*
|
|
48
|
+
* @returns `TraceContextData` to attach to the wire message, or `undefined`
|
|
49
|
+
* if the context has no active trace.
|
|
50
|
+
*/
|
|
51
|
+
static encode(ctx: Context): TraceContextData | undefined {
|
|
52
|
+
const traceCtx = ctx.getAttribute(TRACE_SPAN_ATTRIBUTE);
|
|
53
|
+
if (traceCtx == null || typeof traceCtx.traceparent !== 'string') {
|
|
54
|
+
return undefined;
|
|
55
|
+
}
|
|
56
|
+
return traceCtx as TraceContextData;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Reconstruct a DXOS `Context` from W3C trace context received in an
|
|
61
|
+
* incoming RPC request.
|
|
62
|
+
*
|
|
63
|
+
* @returns A `Context` carrying the trace context, or `Context.default()`
|
|
64
|
+
* if the data is missing/invalid.
|
|
65
|
+
*/
|
|
66
|
+
static decode(traceContext: TraceContextData): Context {
|
|
67
|
+
if (typeof traceContext.traceparent !== 'string' || traceContext.traceparent.length === 0) {
|
|
68
|
+
return Context.default();
|
|
69
|
+
}
|
|
70
|
+
return new Context({ attributes: { [TRACE_SPAN_ATTRIBUTE]: traceContext } });
|
|
71
|
+
}
|
|
72
|
+
}
|