@cloudflare/workspace 0.0.0-alpha.2 → 0.0.0-alpha.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.
@@ -0,0 +1,27 @@
1
+ import { r as WorkspaceObserver } from "../shared-DYgflRlD.js";
2
+
3
+ //#region src/observe/cloudflare.d.ts
4
+ interface CloudflareObserverOptions {
5
+ /**
6
+ * The runtime's user-tracing handle. Pass `ctx.tracing` from inside a
7
+ * Worker or Durable Object, or `tracing` imported from
8
+ * `cloudflare:workers`. `undefined` is accepted because the runtime
9
+ * omits the property in environments without the user-tracing
10
+ * feature flag; the adapter then degrades to a pass-through.
11
+ */
12
+ tracing: Tracing | undefined;
13
+ }
14
+ /**
15
+ * Build a `WorkspaceObserver` backed by the Cloudflare runtime's built-in
16
+ * user tracing surface.
17
+ *
18
+ * When `tracing` is `undefined`, returns an observer that runs the
19
+ * callback directly with a span object whose `setAttribute` is a no-op.
20
+ * This lets the same wiring code work in environments without the
21
+ * user-tracing feature flag, without forcing callers to branch on a
22
+ * possibly-undefined `ctx.tracing`.
23
+ */
24
+ declare function createCloudflareObserver(options: CloudflareObserverOptions): WorkspaceObserver;
25
+ //#endregion
26
+ export { CloudflareObserverOptions, createCloudflareObserver };
27
+ //# sourceMappingURL=cloudflare.d.ts.map
@@ -0,0 +1,38 @@
1
+ //#region src/observe/cloudflare.ts
2
+ /**
3
+ * Build a `WorkspaceObserver` backed by the Cloudflare runtime's built-in
4
+ * user tracing surface.
5
+ *
6
+ * When `tracing` is `undefined`, returns an observer that runs the
7
+ * callback directly with a span object whose `setAttribute` is a no-op.
8
+ * This lets the same wiring code work in environments without the
9
+ * user-tracing feature flag, without forcing callers to branch on a
10
+ * possibly-undefined `ctx.tracing`.
11
+ */
12
+ function createCloudflareObserver(options) {
13
+ const { tracing } = options;
14
+ if (!tracing) return passThroughObserver;
15
+ return { span(name, attributes, run) {
16
+ return tracing.enterSpan(name, (runtimeSpan) => {
17
+ applyAttributes(runtimeSpan, attributes);
18
+ return run({ setAttribute(key, value) {
19
+ if (value === void 0) return;
20
+ runtimeSpan.setAttribute(key, value);
21
+ } });
22
+ });
23
+ } };
24
+ }
25
+ function applyAttributes(runtimeSpan, attributes) {
26
+ for (const [key, value] of Object.entries(attributes)) {
27
+ if (value === void 0) continue;
28
+ runtimeSpan.setAttribute(key, value);
29
+ }
30
+ }
31
+ const passThroughSpan = { setAttribute() {} };
32
+ const passThroughObserver = { span(_name, _attributes, run) {
33
+ return run(passThroughSpan);
34
+ } };
35
+ //#endregion
36
+ export { createCloudflareObserver };
37
+
38
+ //# sourceMappingURL=cloudflare.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cloudflare.js","names":[],"sources":["../../src/observe/cloudflare.ts"],"sourcesContent":["// Cloudflare runtime adapter for the workspace observer hook.\n//\n// Wires `@cloudflare/workspace`'s span-shaped observer to the Cloudflare\n// runtime's built-in user tracing surface (`Tracing.enterSpan`). The\n// runtime owns the span lifecycle: it opens the span, propagates the\n// active context through `AsyncContextFrame` so nested spans become\n// children automatically, and ends the span when the callback's return\n// value (or promise) settles.\n//\n// Two ways to obtain a `Tracing` handle in user code:\n//\n// import { tracing } from \"cloudflare:workers\";\n//\n// const observer = createCloudflareObserver({ tracing });\n//\n// or, when an `ExecutionContext` is on hand:\n//\n// const observer = createCloudflareObserver({ tracing: ctx.tracing });\n//\n// `ctx.tracing` is optional on the `ExecutionContext` type because the\n// runtime omits it in environments without the user-tracing feature\n// flag. The adapter accepts `Tracing | undefined`; when it is undefined\n// the observer is a pure pass-through.\n//\n// Why this adapter is so small\n// ----------------------------\n// The shape of `WorkspaceObserver.span(name, attributes, run)` is a\n// faithful subset of `tracing.enterSpan(name, callback)`. The only\n// real work the adapter does is forward seed attributes onto the\n// runtime span and hand a `setAttribute`-shaped facade to the workspace\n// callback. Errors propagate through `enterSpan` naturally; the\n// workspace's own `withSpan` helper records `error.name` /\n// `error.message` before re-throwing.\n//\n// Why we do not depend on `cloudflare:workers`\n// --------------------------------------------\n// The adapter takes the `Tracing` instance as an argument rather than\n// importing it directly. That keeps this file resolvable under the\n// node-based unit tests (where `cloudflare:workers` is not present),\n// and it keeps the workspace package free of an implicit dependency on\n// the runtime module specifier in tools that do not understand it.\n\nimport type { WorkspaceObserver, WorkspaceSpan } from \"../observe.js\";\n\nexport interface CloudflareObserverOptions {\n /**\n * The runtime's user-tracing handle. Pass `ctx.tracing` from inside a\n * Worker or Durable Object, or `tracing` imported from\n * `cloudflare:workers`. `undefined` is accepted because the runtime\n * omits the property in environments without the user-tracing\n * feature flag; the adapter then degrades to a pass-through.\n */\n tracing: Tracing | undefined;\n}\n\n/**\n * Build a `WorkspaceObserver` backed by the Cloudflare runtime's built-in\n * user tracing surface.\n *\n * When `tracing` is `undefined`, returns an observer that runs the\n * callback directly with a span object whose `setAttribute` is a no-op.\n * This lets the same wiring code work in environments without the\n * user-tracing feature flag, without forcing callers to branch on a\n * possibly-undefined `ctx.tracing`.\n */\nexport function createCloudflareObserver(options: CloudflareObserverOptions): WorkspaceObserver {\n const { tracing } = options;\n if (!tracing) return passThroughObserver;\n return {\n span(name, attributes, run) {\n // The runtime requires the operation name to fit in 64 bytes.\n // Names emitted by the workspace are well under that today\n // (longest is `workspace.shell.exec.spawn` at 26 bytes), so no\n // truncation is needed here; an over-length name is a workspace\n // bug rather than a caller bug.\n return tracing.enterSpan(name, (runtimeSpan) => {\n applyAttributes(runtimeSpan, attributes);\n // Wrap the runtime span in a `WorkspaceSpan` facade. The facade\n // forwards `setAttribute` directly; `undefined` values are\n // dropped so callers can pass optional fields without\n // conditionals.\n const span: WorkspaceSpan = {\n setAttribute(key, value) {\n if (value === undefined) return;\n runtimeSpan.setAttribute(key, value);\n },\n };\n return run(span);\n });\n },\n };\n}\n\nfunction applyAttributes(\n runtimeSpan: Span,\n attributes: Readonly<Record<string, boolean | number | string | undefined>>,\n): void {\n for (const [key, value] of Object.entries(attributes)) {\n if (value === undefined) continue;\n runtimeSpan.setAttribute(key, value);\n }\n}\n\n// Used when the runtime does not expose `Tracing`. The callback runs\n// inline and the span facade is a no-op, matching the package's default\n// `noopObserver`. Duplicated here rather than re-exported to keep the\n// adapter file self-contained and avoid pulling the package entry into\n// the import graph of every adapter consumer.\nconst passThroughSpan: WorkspaceSpan = {\n setAttribute() {\n // intentionally empty\n },\n};\n\nconst passThroughObserver: WorkspaceObserver = {\n span(_name, _attributes, run) {\n return run(passThroughSpan);\n },\n};\n"],"mappings":";;;;;;;;;;;AAiEA,SAAgB,yBAAyB,SAAuD;CAC9F,MAAM,EAAE,YAAY;CACpB,IAAI,CAAC,SAAS,OAAO;CACrB,OAAO,EACL,KAAK,MAAM,YAAY,KAAK;EAM1B,OAAO,QAAQ,UAAU,OAAO,gBAAgB;GAC9C,gBAAgB,aAAa,UAAU;GAWvC,OAAO,IAAI,EALT,aAAa,KAAK,OAAO;IACvB,IAAI,UAAU,KAAA,GAAW;IACzB,YAAY,aAAa,KAAK,KAAK;GACrC,EAEY,CAAC;EACjB,CAAC;CACH,EACF;AACF;AAEA,SAAS,gBACP,aACA,YACM;CACN,KAAK,MAAM,CAAC,KAAK,UAAU,OAAO,QAAQ,UAAU,GAAG;EACrD,IAAI,UAAU,KAAA,GAAW;EACzB,YAAY,aAAa,KAAK,KAAK;CACrC;AACF;AAOA,MAAM,kBAAiC,EACrC,eAAe,CAEf,EACF;AAEA,MAAM,sBAAyC,EAC7C,KAAK,OAAO,aAAa,KAAK;CAC5B,OAAO,IAAI,eAAe;AAC5B,EACF"}
@@ -0,0 +1,63 @@
1
+ //#region src/observe.d.ts
2
+ /**
3
+ * Attribute values accepted by `WorkspaceSpan.setAttribute` and the
4
+ * initial `attributes` map passed to `Observer.span`.
5
+ *
6
+ * Restricted to scalar types so the same shape works against the
7
+ * Cloudflare runtime's built-in `Span.setAttribute` and against
8
+ * `@opentelemetry/api` without per-adapter coercion.
9
+ */
10
+ type WorkspaceAttributeValue = boolean | number | string;
11
+ /**
12
+ * Initial attributes passed when a span is started. `undefined` values
13
+ * are dropped — adapters do not forward them to the underlying span.
14
+ * This matches the Cloudflare runtime, where `setAttribute(key,
15
+ * undefined)` is a no-op.
16
+ */
17
+ type WorkspaceAttributes = Readonly<Record<string, WorkspaceAttributeValue | undefined>>;
18
+ /**
19
+ * Handle passed to the callback inside `Observer.span`. The only
20
+ * mutating method is `setAttribute`, matching the Cloudflare
21
+ * `ctx.tracing` `Span` surface. Adapters that target a richer system
22
+ * may expose more on their own internal types, but the workspace
23
+ * itself only ever calls these methods.
24
+ */
25
+ interface WorkspaceSpan {
26
+ /**
27
+ * Sets a single attribute on the span. Passing `undefined` is a
28
+ * no-op so callers can forward optional values directly.
29
+ */
30
+ setAttribute(key: string, value: WorkspaceAttributeValue | undefined): void;
31
+ }
32
+ /**
33
+ * Observer hook. A single `span` method that wraps a piece of work —
34
+ * the adapter starts a span, runs the callback, and ends the span when
35
+ * the callback's return value (or its returned promise) settles.
36
+ *
37
+ * The default observer is a no-op (see `noopObserver`); the workspace
38
+ * uses it when the caller does not pass one. Adapters live in separate
39
+ * subpaths so callers do not pay for `@opentelemetry/api` or
40
+ * `cloudflare:workers` just to import the workspace.
41
+ */
42
+ interface WorkspaceObserver {
43
+ /**
44
+ * Wraps `run` in a span named `name`, seeded with `attributes`. The
45
+ * callback receives a `WorkspaceSpan` it can use to attach further
46
+ * attributes once the work has produced values worth recording (byte
47
+ * counts, exit codes, applied / skipped counts).
48
+ *
49
+ * Implementations must run `run` synchronously and return its result
50
+ * (or its promise) unchanged so the wrapping is invisible to the
51
+ * caller. Errors thrown by `run` must propagate.
52
+ */
53
+ span<T>(name: string, attributes: WorkspaceAttributes, run: (span: WorkspaceSpan) => Promise<T>): Promise<T>;
54
+ }
55
+ /**
56
+ * Observer that does no work. Used when the caller does not pass one.
57
+ * Calling `span(...)` returns the callback's promise directly, with no
58
+ * extra `await` and no allocation beyond what the callback itself does.
59
+ */
60
+ declare const noopObserver: WorkspaceObserver;
61
+ //#endregion
62
+ export { noopObserver as a, WorkspaceSpan as i, WorkspaceAttributes as n, WorkspaceObserver as r, WorkspaceAttributeValue as t };
63
+ //# sourceMappingURL=shared-DYgflRlD.d.ts.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cloudflare/workspace",
3
- "version": "0.0.0-alpha.2",
3
+ "version": "0.0.0-alpha.4",
4
4
  "description": "Cloudflare Workspace — a SQLite-backed virtual filesystem with sync to a container-side daemon (wsd).",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -18,6 +18,10 @@
18
18
  "./git": {
19
19
  "types": "./dist/git.d.ts",
20
20
  "import": "./dist/git.js"
21
+ },
22
+ "./observe/cloudflare": {
23
+ "types": "./dist/observe/cloudflare.d.ts",
24
+ "import": "./dist/observe/cloudflare.js"
21
25
  }
22
26
  },
23
27
  "main": "./dist/index.js",