@enhance-eng/sdk 0.1.0-dev.sha-d9190c779d58 → 0.1.158

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@enhance-eng/sdk",
3
- "version": "0.1.0-dev.sha-d9190c779d58",
3
+ "version": "0.1.158",
4
4
  "description": "Capture editable customer journeys in Enhance from your tests.",
5
5
  "homepage": "https://enhancelabs.ai",
6
6
  "repository": {
@@ -9,6 +9,7 @@
9
9
  "directory": "packages/sdk"
10
10
  },
11
11
  "license": "UNLICENSED",
12
+ "author": "Enhance",
12
13
  "sideEffects": false,
13
14
  "type": "module",
14
15
  "imports": {
@@ -34,20 +35,24 @@
34
35
  }
35
36
  },
36
37
  "files": [
37
- "dist",
38
- "README.md"
38
+ "dist"
39
39
  ],
40
40
  "dependencies": {
41
+ "@img/sharp-wasm32": "0.35.3",
42
+ "sharp": "0.35.3",
43
+ "undici": "7.29.0",
41
44
  "zod": "^4.4.3"
42
45
  },
43
46
  "devDependencies": {
47
+ "@fontsource-variable/jetbrains-mono": "^5.3.0",
44
48
  "@playwright/test": "^1.62.0",
45
49
  "tsdown": "^0.22.14",
46
50
  "typescript": "^5.9.3",
47
51
  "vitest": "^4.1.10",
48
- "@enhance3/capture-browser": "0.0.0",
49
52
  "@enhance3/contract": "0.0.0",
50
- "@enhance3/tsconfig": "0.0.0"
53
+ "@enhance3/capture-browser": "0.0.0",
54
+ "@enhance3/tsconfig": "0.0.0",
55
+ "@enhance3/eslint-config": "0.0.0"
51
56
  },
52
57
  "peerDependencies": {
53
58
  "@playwright/test": ">=1.62.0 <2"
@@ -66,7 +71,9 @@
66
71
  },
67
72
  "scripts": {
68
73
  "build": "tsdown",
69
- "test": "vitest run",
74
+ "lint": "pnpm --workspace-root exec eslint packages/sdk --max-warnings 0",
75
+ "test": "pnpm run build && vitest run",
76
+ "test:changed": "vitest run --passWithNoTests --changed origin/main",
70
77
  "typecheck": "tsc --noEmit"
71
78
  }
72
79
  }
@@ -1,226 +0,0 @@
1
- import { _ as reserveCaptureBlobsOutputSchema, d as beginCaptureRunInputSchema, f as beginCaptureRunOutputSchema, g as getCaptureRunOutputSchema, h as captureRunDescriptorSchema, i as writeCapturePayload, l as captureBlobs, n as readCaptureManifest, o as captureManifestDigest, r as writeCaptureJson, s as captureError, t as readCaptureBlob, u as selectedCaptures } from "./capture-files.lib-BA_KBz9W.mjs";
2
- import { mkdir } from "node:fs/promises";
3
- import nodePath from "node:path";
4
- //#region src/node/capture-publication.client.ts
5
- const UPLOAD_BATCH_SIZE = 400;
6
- const POLL_INTERVAL_MS = 250;
7
- const uploadBlob = async (input) => {
8
- if (input.held === void 0) throw captureError("transport", "Enhance omitted a requested blob reservation.", input.runId);
9
- const upload = input.held.upload;
10
- if (upload === null) return;
11
- const bytes = await readCaptureBlob(input.directory, input.blob);
12
- try {
13
- if (!(await fetch(upload.url, {
14
- method: "PUT",
15
- headers: upload.headers,
16
- body: new Blob([Uint8Array.from(bytes)]),
17
- signal: AbortSignal.timeout(input.transport.timeoutMs),
18
- redirect: "error"
19
- })).ok) throw captureError("transport", "Upload refused.", input.runId);
20
- } catch {
21
- throw captureError("transport", "Capture upload was not confirmed. Resume the same manifest.", input.runId);
22
- }
23
- };
24
- const uploadBlobs = async (transport, manifest, directory, blobs) => {
25
- for (let start = 0; start < blobs.length; start += UPLOAD_BATCH_SIZE) {
26
- const batch = blobs.slice(start, start + UPLOAD_BATCH_SIZE);
27
- for (const blob of batch) await readCaptureBlob(directory, blob);
28
- const reserved = await transport.request("captureRun.reserveAssets", {
29
- runId: manifest.run.id,
30
- blobs: batch
31
- }, reserveCaptureBlobsOutputSchema);
32
- for (const blob of batch) await uploadBlob({
33
- transport,
34
- runId: manifest.run.id,
35
- directory,
36
- blob,
37
- held: reserved.uploads.find((item) => item.sha256 === blob.sha256)
38
- });
39
- }
40
- };
41
- const committedRun = async (transport, runId) => {
42
- let status = await transport.request("captureRun.publish", { runId }, getCaptureRunOutputSchema);
43
- const deadline = Date.now() + transport.publicationTimeoutMs;
44
- while (status.receipt === null && status.failure === null && !["cancelled", "failed"].includes(status.state)) {
45
- if (Date.now() >= deadline) throw captureError("transport", `Capture run ${runId} is still processing. Resume the same manifest.`, runId);
46
- await new Promise((resolve) => {
47
- setTimeout(resolve, POLL_INTERVAL_MS);
48
- });
49
- status = await transport.request("captureRun.publish", { runId }, getCaptureRunOutputSchema);
50
- }
51
- if (status.receipt === null) throw captureError(status.failure?.code === "conflict" ? "conflict" : "conversion", status.failure?.message ?? "The capture run was cancelled.", runId);
52
- return status.receipt;
53
- };
54
- const checkedReceipt = (receipt, manifest) => {
55
- if (receipt.runId !== manifest.run.id || receipt.canvasId !== manifest.run.canvasId || receipt.manifestDigest !== captureManifestDigest(manifest)) throw captureError("conflict", "This run committed a different manifest.", manifest.run.id);
56
- return receipt;
57
- };
58
- const publishManifest = async (transport, manifest, directory) => {
59
- const artifacts = selectedCaptures(manifest);
60
- if (typeof artifacts === "string") throw captureError("artifact", artifacts);
61
- const blobs = captureBlobs(artifacts);
62
- if (typeof blobs === "string") throw captureError("artifact", blobs);
63
- const current = await transport.request("captureRun.get", { runId: manifest.run.id }, getCaptureRunOutputSchema, true);
64
- if (current.receipt !== null) {
65
- const receipt = checkedReceipt(current.receipt, manifest);
66
- await writeCaptureJson(nodePath.join(directory, "receipt.json"), receipt);
67
- return receipt;
68
- }
69
- if (current.state === "collecting") await uploadBlobs(transport, manifest, directory, blobs);
70
- await transport.request("captureRun.seal", { manifest }, getCaptureRunOutputSchema);
71
- const receipt = checkedReceipt(await committedRun(transport, manifest.run.id), manifest);
72
- await writeCaptureJson(nodePath.join(directory, "receipt.json"), receipt);
73
- return receipt;
74
- };
75
- const publishCaptureManifest = async (transport, manifest, directory) => {
76
- try {
77
- return await publishManifest(transport, manifest, directory);
78
- } catch (error) {
79
- if (error instanceof Error) throw Object.assign(error, { runId: manifest.run.id });
80
- throw captureError("transport", "Capture publication was not confirmed. Resume the same manifest.", manifest.run.id);
81
- }
82
- };
83
- //#endregion
84
- //#region src/node/capture-transport.client.ts
85
- const MAX_ERROR_LENGTH = 1e3;
86
- const DEFAULT_TIMEOUT_MS = 3e4;
87
- const DEFAULT_PUBLICATION_TIMEOUT_MS = 6e5;
88
- const MAX_PUBLICATION_TIMEOUT_MS = 36e5;
89
- const MAX_TIMEOUT_MS = 3e5;
90
- const HTTP_ERRORS = /* @__PURE__ */ new Map([
91
- [401, "authorization"],
92
- [403, "authorization"],
93
- [409, "conflict"],
94
- [400, "artifact"]
95
- ]);
96
- const requestResponse = async (url, options) => {
97
- try {
98
- return await fetch(url, options);
99
- } catch {
100
- throw captureError("transport", "Enhance did not confirm the request. Resume the same run to recover its result.");
101
- }
102
- };
103
- const responseBody = async (response) => {
104
- try {
105
- return await response.json();
106
- } catch {
107
- return null;
108
- }
109
- };
110
- const call = async (url, options, schema, procedure) => {
111
- const response = await requestResponse(url, options);
112
- const body = await responseBody(response);
113
- if (!response.ok) {
114
- const error = typeof body === "object" && body !== null ? Reflect.get(body, "error") : null;
115
- const message = typeof error === "object" && error !== null ? Reflect.get(error, "message") : null;
116
- throw captureError(HTTP_ERRORS.get(response.status) ?? "transport", typeof message === "string" ? message.slice(0, MAX_ERROR_LENGTH) : `Enhance rejected ${procedure} (HTTP ${String(response.status)}).`);
117
- }
118
- const result = typeof body === "object" && body !== null ? Reflect.get(body, "result") : null;
119
- const data = typeof result === "object" && result !== null ? Reflect.get(result, "data") : void 0;
120
- const parsed = schema.safeParse(data);
121
- if (!parsed.success) throw captureError("transport", `Enhance returned an incompatible ${procedure} response.`);
122
- return parsed.data;
123
- };
124
- const captureTransport = (options) => {
125
- const base = URL.parse(options.apiUrl);
126
- if (base === null || !["http:", "https:"].includes(base.protocol) || base.username !== "" || base.password !== "" || options.token.trim() === "") throw captureError("configuration", "Enhance needs an HTTP API URL and an explicit token.");
127
- const timeoutMs = options.timeoutMs ?? DEFAULT_TIMEOUT_MS;
128
- const publicationTimeoutMs = options.publicationTimeoutMs ?? DEFAULT_PUBLICATION_TIMEOUT_MS;
129
- if (!Number.isSafeInteger(publicationTimeoutMs) || publicationTimeoutMs < 1 || publicationTimeoutMs > MAX_PUBLICATION_TIMEOUT_MS) throw captureError("configuration", "Enhance publication timeout must be between 1 and 3600000 milliseconds.");
130
- if (!Number.isSafeInteger(timeoutMs) || timeoutMs < 1 || timeoutMs > MAX_TIMEOUT_MS) throw captureError("configuration", "Enhance timeout must be between 1 and 300000 milliseconds.");
131
- return {
132
- timeoutMs,
133
- publicationTimeoutMs,
134
- request: async (procedure, input, schema, isQuery = false) => {
135
- const url = new URL(`${base.pathname.replace(/\/$/u, "")}/trpc/${procedure}`, base.origin);
136
- if (isQuery) url.searchParams.set("input", JSON.stringify(input));
137
- return call(url, {
138
- method: isQuery ? "GET" : "POST",
139
- headers: {
140
- authorization: `Bearer ${options.token}`,
141
- "content-type": "application/json"
142
- },
143
- ...!isQuery && { body: JSON.stringify(input) },
144
- signal: AbortSignal.timeout(timeoutMs),
145
- redirect: "error"
146
- }, schema, procedure);
147
- }
148
- };
149
- };
150
- //#endregion
151
- //#region src/node/capture-client.client.ts
152
- const nodeManifest = (descriptor, captured) => {
153
- const participant = descriptor.participants[0];
154
- if (participant === void 0) throw captureError("configuration", "No capture participant was reserved.");
155
- return {
156
- format: "enhance-capture-run",
157
- version: 1,
158
- run: descriptor,
159
- ordering: "partial",
160
- participants: [{
161
- id: participant,
162
- status: "passed"
163
- }],
164
- attempts: [{
165
- participant,
166
- testId: "node",
167
- project: "node",
168
- retry: 0,
169
- status: "passed",
170
- captures: captured
171
- }]
172
- };
173
- };
174
- const createRun = (transport, input, directory) => {
175
- const descriptor = captureRunDescriptorSchema.parse(input);
176
- const artifacts = [];
177
- let isClosed = false;
178
- return {
179
- descriptor,
180
- directory,
181
- add: async (payload) => {
182
- if (isClosed) throw captureError("configuration", "This run no longer accepts captures.");
183
- const written = writeCapturePayload(directory, payload);
184
- artifacts.push(written);
185
- await written;
186
- },
187
- publish: async () => {
188
- isClosed = true;
189
- const captured = await Promise.all(artifacts);
190
- if (descriptor.participants.length !== 1) throw captureError("configuration", "Multi-participant runs must publish a complete merged manifest.");
191
- const manifest = nodeManifest(descriptor, captured);
192
- const selected = selectedCaptures(manifest);
193
- if (typeof selected === "string") throw captureError("artifact", selected);
194
- await mkdir(directory, { recursive: true });
195
- await writeCaptureJson(nodePath.join(directory, "manifest.json"), manifest);
196
- return publishCaptureManifest(transport, manifest, directory);
197
- },
198
- cancel: async () => {
199
- isClosed = true;
200
- return (await transport.request("captureRun.cancel", { runId: descriptor.id }, getCaptureRunOutputSchema)).receipt;
201
- }
202
- };
203
- };
204
- const createEnhance = (options) => {
205
- const transport = captureTransport(options);
206
- const get = async (runId) => transport.request("captureRun.get", { runId }, getCaptureRunOutputSchema, true);
207
- return { captures: {
208
- begin: async (input) => {
209
- const request = beginCaptureRunInputSchema.parse({
210
- canvasId: input.canvas,
211
- source: input.source,
212
- runKey: input.runKey,
213
- participants: input.participants
214
- });
215
- const { run } = await transport.request("captureRun.begin", request, beginCaptureRunOutputSchema);
216
- const directory = input.directory ?? nodePath.join(".enhance", "captures", run.id);
217
- await mkdir(directory, { recursive: true });
218
- await writeCaptureJson(nodePath.join(directory, "run.json"), run);
219
- return createRun(transport, run, directory);
220
- },
221
- publishManifest: async (path) => publishCaptureManifest(transport, await readCaptureManifest(path), nodePath.dirname(path)),
222
- get
223
- } };
224
- };
225
- //#endregion
226
- export { createEnhance as t };
@@ -1,34 +0,0 @@
1
- import { a as CaptureReceipt, l as getCaptureRunOutputSchema, o as CaptureRunDescriptor, t as CapturePayload } from "./capture-files.lib-BpwdV8DQ.mjs";
2
- //#region src/node/capture-transport.client.d.ts
3
- interface CaptureClientOptions {
4
- readonly apiUrl: string;
5
- readonly token: string;
6
- readonly timeoutMs?: number;
7
- readonly publicationTimeoutMs?: number;
8
- }
9
- //#endregion
10
- //#region src/node/capture-client.client.d.ts
11
- interface BeginCaptureRun {
12
- readonly canvas: string;
13
- readonly source?: string;
14
- readonly runKey: string;
15
- readonly participants?: readonly string[];
16
- readonly directory?: string;
17
- }
18
- interface CaptureRun {
19
- readonly descriptor: CaptureRunDescriptor;
20
- readonly directory: string;
21
- readonly add: (payload: CapturePayload) => Promise<void>;
22
- readonly publish: () => Promise<CaptureReceipt>;
23
- readonly cancel: () => Promise<CaptureReceipt | null>;
24
- }
25
- interface EnhanceClient {
26
- readonly captures: {
27
- readonly begin: (input: BeginCaptureRun) => Promise<CaptureRun>;
28
- readonly publishManifest: (path: string) => Promise<CaptureReceipt>;
29
- readonly get: (runId: string) => Promise<ReturnType<typeof getCaptureRunOutputSchema.parse>>;
30
- };
31
- }
32
- declare const createEnhance: (options: CaptureClientOptions) => EnhanceClient;
33
- //#endregion
34
- export { CaptureClientOptions as a, createEnhance as i, CaptureRun as n, EnhanceClient as r, BeginCaptureRun as t };