@glasstrace/sdk 0.10.0 → 0.12.0

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.
Files changed (48) hide show
  1. package/dist/adapters/drizzle.js +1 -1
  2. package/dist/{chunk-ZRNG36LU.js → chunk-6GRNJ722.js} +38 -17
  3. package/dist/chunk-6GRNJ722.js.map +1 -0
  4. package/dist/chunk-IPGOKORJ.js +580 -0
  5. package/dist/chunk-IPGOKORJ.js.map +1 -0
  6. package/dist/{chunk-5MAHIPFH.js → chunk-LW7DPKBA.js} +2 -2
  7. package/dist/{chunk-KOYJ2UQE.js → chunk-MSMOH6IH.js} +108 -38
  8. package/dist/chunk-MSMOH6IH.js.map +1 -0
  9. package/dist/chunk-NSBPE2FW.js +17 -0
  10. package/dist/{chunk-O3Y45VGV.js → chunk-OKIP4SRG.js} +5 -3
  11. package/dist/{chunk-O3Y45VGV.js.map → chunk-OKIP4SRG.js.map} +1 -1
  12. package/dist/cli/init.cjs +457 -330
  13. package/dist/cli/init.cjs.map +1 -1
  14. package/dist/cli/init.d.cts +25 -1
  15. package/dist/cli/init.d.ts +25 -1
  16. package/dist/cli/init.js +114 -4
  17. package/dist/cli/init.js.map +1 -1
  18. package/dist/cli/mcp-add.cjs +63 -47
  19. package/dist/cli/mcp-add.cjs.map +1 -1
  20. package/dist/cli/mcp-add.js +3 -3
  21. package/dist/cli/uninit.js +15 -564
  22. package/dist/cli/uninit.js.map +1 -1
  23. package/dist/{esm-POMEQPKL.js → esm-KBPHCVB4.js} +2 -2
  24. package/dist/{getMachineId-bsd-TC3JSTY5.js → getMachineId-bsd-345PYXFX.js} +2 -2
  25. package/dist/{getMachineId-darwin-2SUKQCE6.js → getMachineId-darwin-5L2D25AD.js} +2 -2
  26. package/dist/{getMachineId-linux-PNAFHLXH.js → getMachineId-linux-KJR4P5HN.js} +2 -2
  27. package/dist/{getMachineId-unsupported-L2MNYW3W.js → getMachineId-unsupported-NDNXDYDY.js} +2 -2
  28. package/dist/{getMachineId-win-D6D42WOQ.js → getMachineId-win-T7PJNJXG.js} +2 -2
  29. package/dist/index.cjs +327 -159
  30. package/dist/index.cjs.map +1 -1
  31. package/dist/index.d.cts +75 -11
  32. package/dist/index.d.ts +75 -11
  33. package/dist/index.js +159 -82
  34. package/dist/index.js.map +1 -1
  35. package/dist/{source-map-uploader-OA5NCDOK.js → source-map-uploader-ZFCYOURS.js} +6 -4
  36. package/package.json +1 -1
  37. package/dist/chunk-KOYJ2UQE.js.map +0 -1
  38. package/dist/chunk-PZ5AY32C.js +0 -10
  39. package/dist/chunk-ZRNG36LU.js.map +0 -1
  40. /package/dist/{chunk-5MAHIPFH.js.map → chunk-LW7DPKBA.js.map} +0 -0
  41. /package/dist/{chunk-PZ5AY32C.js.map → chunk-NSBPE2FW.js.map} +0 -0
  42. /package/dist/{esm-POMEQPKL.js.map → esm-KBPHCVB4.js.map} +0 -0
  43. /package/dist/{getMachineId-bsd-TC3JSTY5.js.map → getMachineId-bsd-345PYXFX.js.map} +0 -0
  44. /package/dist/{getMachineId-darwin-2SUKQCE6.js.map → getMachineId-darwin-5L2D25AD.js.map} +0 -0
  45. /package/dist/{getMachineId-linux-PNAFHLXH.js.map → getMachineId-linux-KJR4P5HN.js.map} +0 -0
  46. /package/dist/{getMachineId-unsupported-L2MNYW3W.js.map → getMachineId-unsupported-NDNXDYDY.js.map} +0 -0
  47. /package/dist/{getMachineId-win-D6D42WOQ.js.map → getMachineId-win-T7PJNJXG.js.map} +0 -0
  48. /package/dist/{source-map-uploader-OA5NCDOK.js.map → source-map-uploader-ZFCYOURS.js.map} +0 -0
package/dist/index.js CHANGED
@@ -2,6 +2,7 @@ import {
2
2
  PRESIGNED_THRESHOLD_BYTES,
3
3
  collectSourceMaps,
4
4
  computeBuildHash,
5
+ discoverSourceMapFiles,
5
6
  installConsoleCapture,
6
7
  isAnonymousMode,
7
8
  isProductionDisabled,
@@ -11,23 +12,23 @@ import {
11
12
  uploadSourceMaps,
12
13
  uploadSourceMapsAuto,
13
14
  uploadSourceMapsPresigned
14
- } from "./chunk-KOYJ2UQE.js";
15
+ } from "./chunk-MSMOH6IH.js";
15
16
  import {
16
17
  buildImportGraph,
17
18
  discoverTestFiles,
18
19
  extractImports
19
- } from "./chunk-5MAHIPFH.js";
20
+ } from "./chunk-LW7DPKBA.js";
20
21
  import {
21
22
  getOrCreateAnonKey,
22
23
  readAnonKey
23
- } from "./chunk-ZRNG36LU.js";
24
+ } from "./chunk-6GRNJ722.js";
24
25
  import {
25
26
  DEFAULT_CAPTURE_CONFIG,
26
27
  GLASSTRACE_ATTRIBUTE_NAMES,
27
28
  SdkCachedConfigSchema,
28
29
  SdkInitResponseSchema,
29
30
  SessionIdSchema
30
- } from "./chunk-O3Y45VGV.js";
31
+ } from "./chunk-OKIP4SRG.js";
31
32
  import {
32
33
  INVALID_SPAN_CONTEXT,
33
34
  SamplingDecision,
@@ -43,7 +44,9 @@ import {
43
44
  isValidTraceId,
44
45
  trace
45
46
  } from "./chunk-DQ25VOKK.js";
46
- import "./chunk-PZ5AY32C.js";
47
+ import {
48
+ __require
49
+ } from "./chunk-NSBPE2FW.js";
47
50
 
48
51
  // src/errors.ts
49
52
  var SdkError = class extends Error {
@@ -56,13 +59,36 @@ var SdkError = class extends Error {
56
59
  };
57
60
 
58
61
  // src/session.ts
59
- import { createHash } from "crypto";
60
62
  var FOUR_HOURS_MS = 4 * 60 * 60 * 1e3;
63
+ var hashFn = null;
64
+ function fnv1aHash(input) {
65
+ let hash = 2166136261;
66
+ for (let i = 0; i < input.length; i++) {
67
+ hash ^= input.charCodeAt(i);
68
+ hash = Math.imul(hash, 16777619);
69
+ hash >>>= 0;
70
+ }
71
+ return hash.toString(16).padStart(8, "0");
72
+ }
73
+ function getHashFn() {
74
+ if (hashFn) return hashFn;
75
+ try {
76
+ const { createHash } = __require("crypto");
77
+ hashFn = (input) => createHash("sha256").update(input).digest("hex").slice(0, 16);
78
+ } catch {
79
+ hashFn = (input) => {
80
+ const h1 = fnv1aHash(input);
81
+ const h2 = fnv1aHash(input + "\0");
82
+ return (h1 + h2).slice(0, 16);
83
+ };
84
+ }
85
+ return hashFn;
86
+ }
61
87
  var cachedGlasstraceEnv = process.env.GLASSTRACE_ENV;
62
88
  var cachedPort = process.env.PORT ?? "3000";
63
89
  function deriveSessionId(apiKey, origin, date, windowIndex) {
64
90
  const input = JSON.stringify([apiKey, origin, date, windowIndex]);
65
- const hash = createHash("sha256").update(input).digest("hex").slice(0, 16);
91
+ const hash = getHashFn()(input);
66
92
  return SessionIdSchema.parse(hash);
67
93
  }
68
94
  function getOrigin() {
@@ -148,21 +174,44 @@ function classifyFetchTarget(url) {
148
174
  }
149
175
 
150
176
  // src/init-client.ts
151
- import { readFileSync } from "fs";
152
- import { readFile, writeFile, mkdir, chmod } from "fs/promises";
153
- import { join } from "path";
154
177
  var GLASSTRACE_DIR = ".glasstrace";
155
178
  var CONFIG_FILE = "config";
156
179
  var TWENTY_FOUR_HOURS_MS = 24 * 60 * 60 * 1e3;
157
180
  var INIT_TIMEOUT_MS = 1e4;
181
+ var fsPathAsyncCache;
182
+ async function loadFsPathAsync() {
183
+ if (fsPathAsyncCache !== void 0) return fsPathAsyncCache;
184
+ try {
185
+ const [fs2, path2] = await Promise.all([
186
+ import("fs/promises"),
187
+ import("path")
188
+ ]);
189
+ fsPathAsyncCache = { fs: fs2, path: path2 };
190
+ return fsPathAsyncCache;
191
+ } catch {
192
+ fsPathAsyncCache = null;
193
+ return null;
194
+ }
195
+ }
196
+ function loadFsSyncOrNull() {
197
+ try {
198
+ const fs2 = __require("fs");
199
+ const path2 = __require("path");
200
+ return { readFileSync: fs2.readFileSync, join: path2.join };
201
+ } catch {
202
+ return null;
203
+ }
204
+ }
158
205
  var currentConfig = null;
159
206
  var configCacheChecked = false;
160
207
  var rateLimitBackoff = false;
161
208
  function loadCachedConfig(projectRoot) {
209
+ const modules = loadFsSyncOrNull();
210
+ if (!modules) return null;
162
211
  const root = projectRoot ?? process.cwd();
163
- const configPath = join(root, GLASSTRACE_DIR, CONFIG_FILE);
212
+ const configPath = modules.join(root, GLASSTRACE_DIR, CONFIG_FILE);
164
213
  try {
165
- const content = readFileSync(configPath, "utf-8");
214
+ const content = modules.readFileSync(configPath, "utf-8");
166
215
  const parsed = JSON.parse(content);
167
216
  const cached = SdkCachedConfigSchema.parse(parsed);
168
217
  const age = Date.now() - cached.cachedAt;
@@ -182,18 +231,20 @@ function loadCachedConfig(projectRoot) {
182
231
  }
183
232
  }
184
233
  async function saveCachedConfig(response, projectRoot) {
234
+ const modules = await loadFsPathAsync();
235
+ if (!modules) return;
185
236
  const root = projectRoot ?? process.cwd();
186
- const dirPath = join(root, GLASSTRACE_DIR);
187
- const configPath = join(dirPath, CONFIG_FILE);
237
+ const dirPath = modules.path.join(root, GLASSTRACE_DIR);
238
+ const configPath = modules.path.join(dirPath, CONFIG_FILE);
188
239
  try {
189
- await mkdir(dirPath, { recursive: true, mode: 448 });
190
- await chmod(dirPath, 448);
240
+ await modules.fs.mkdir(dirPath, { recursive: true, mode: 448 });
241
+ await modules.fs.chmod(dirPath, 448);
191
242
  const cached = {
192
243
  response,
193
244
  cachedAt: Date.now()
194
245
  };
195
- await writeFile(configPath, JSON.stringify(cached), { encoding: "utf-8", mode: 384 });
196
- await chmod(configPath, 384);
246
+ await modules.fs.writeFile(configPath, JSON.stringify(cached), { encoding: "utf-8", mode: 384 });
247
+ await modules.fs.chmod(configPath, 384);
197
248
  } catch (err) {
198
249
  console.warn(
199
250
  `[glasstrace] Failed to cache config to ${configPath}: ${err instanceof Error ? err.message : String(err)}`
@@ -247,69 +298,72 @@ async function sendInitRequest(config, anonKey, sdkVersion, importGraph, healthR
247
298
  return SdkInitResponseSchema.parse(body);
248
299
  }
249
300
  async function writeClaimedKey(newApiKey, projectRoot) {
250
- const root = projectRoot ?? process.cwd();
251
- const envLocalPath = join(root, ".env.local");
252
- let envLocalWritten = false;
253
- try {
254
- let content;
301
+ const modules = await loadFsPathAsync();
302
+ if (modules) {
303
+ const root = projectRoot ?? process.cwd();
304
+ const envLocalPath = modules.path.join(root, ".env.local");
305
+ let envLocalWritten = false;
255
306
  try {
256
- content = await readFile(envLocalPath, "utf-8");
257
- if (/^GLASSTRACE_API_KEY=.*/m.test(content)) {
258
- content = content.replace(
259
- /^GLASSTRACE_API_KEY=.*$/gm,
260
- `GLASSTRACE_API_KEY=${newApiKey}`
261
- );
262
- } else {
263
- if (content.length > 0 && !content.endsWith("\n")) {
264
- content += "\n";
307
+ let content;
308
+ try {
309
+ content = await modules.fs.readFile(envLocalPath, "utf-8");
310
+ if (/^GLASSTRACE_API_KEY=.*/m.test(content)) {
311
+ content = content.replace(
312
+ /^GLASSTRACE_API_KEY=.*$/gm,
313
+ `GLASSTRACE_API_KEY=${newApiKey}`
314
+ );
315
+ } else {
316
+ if (content.length > 0 && !content.endsWith("\n")) {
317
+ content += "\n";
318
+ }
319
+ content += `GLASSTRACE_API_KEY=${newApiKey}
320
+ `;
265
321
  }
266
- content += `GLASSTRACE_API_KEY=${newApiKey}
322
+ } catch (readErr) {
323
+ const code = readErr instanceof Error ? readErr.code : void 0;
324
+ if (code !== "ENOENT") {
325
+ throw readErr;
326
+ }
327
+ content = `GLASSTRACE_API_KEY=${newApiKey}
267
328
  `;
268
329
  }
269
- } catch (readErr) {
270
- const code = readErr instanceof Error ? readErr.code : void 0;
271
- if (code !== "ENOENT") {
272
- throw readErr;
330
+ await modules.fs.writeFile(envLocalPath, content, { encoding: "utf-8", mode: 384 });
331
+ await modules.fs.chmod(envLocalPath, 384);
332
+ envLocalWritten = true;
333
+ } catch {
334
+ }
335
+ if (envLocalWritten) {
336
+ try {
337
+ process.stderr.write(
338
+ "[glasstrace] Account claimed! API key written to .env.local. Restart your dev server to use it.\n"
339
+ );
340
+ } catch {
273
341
  }
274
- content = `GLASSTRACE_API_KEY=${newApiKey}
275
- `;
342
+ return;
276
343
  }
277
- await writeFile(envLocalPath, content, { encoding: "utf-8", mode: 384 });
278
- await chmod(envLocalPath, 384);
279
- envLocalWritten = true;
280
- } catch {
281
- }
282
- if (envLocalWritten) {
344
+ let claimedKeyWritten = false;
283
345
  try {
284
- process.stderr.write(
285
- "[glasstrace] Account claimed! API key written to .env.local. Restart your dev server to use it.\n"
286
- );
346
+ const dirPath = modules.path.join(root, GLASSTRACE_DIR);
347
+ await modules.fs.mkdir(dirPath, { recursive: true, mode: 448 });
348
+ await modules.fs.chmod(dirPath, 448);
349
+ const claimedKeyPath = modules.path.join(dirPath, "claimed-key");
350
+ await modules.fs.writeFile(claimedKeyPath, newApiKey, {
351
+ encoding: "utf-8",
352
+ mode: 384
353
+ });
354
+ await modules.fs.chmod(claimedKeyPath, 384);
355
+ claimedKeyWritten = true;
287
356
  } catch {
288
357
  }
289
- return;
290
- }
291
- let claimedKeyWritten = false;
292
- try {
293
- const dirPath = join(root, GLASSTRACE_DIR);
294
- await mkdir(dirPath, { recursive: true, mode: 448 });
295
- await chmod(dirPath, 448);
296
- const claimedKeyPath = join(dirPath, "claimed-key");
297
- await writeFile(claimedKeyPath, newApiKey, {
298
- encoding: "utf-8",
299
- mode: 384
300
- });
301
- await chmod(claimedKeyPath, 384);
302
- claimedKeyWritten = true;
303
- } catch {
304
- }
305
- if (claimedKeyWritten) {
306
- try {
307
- process.stderr.write(
308
- "[glasstrace] Account claimed! API key written to .glasstrace/claimed-key. Copy it to your .env.local file.\n"
309
- );
310
- } catch {
358
+ if (claimedKeyWritten) {
359
+ try {
360
+ process.stderr.write(
361
+ "[glasstrace] Account claimed! API key written to .glasstrace/claimed-key. Copy it to your .env.local file.\n"
362
+ );
363
+ } catch {
364
+ }
365
+ return;
311
366
  }
312
- return;
313
367
  }
314
368
  try {
315
369
  process.stderr.write(
@@ -408,6 +462,12 @@ function getActiveConfig() {
408
462
  }
409
463
  return { ...DEFAULT_CAPTURE_CONFIG };
410
464
  }
465
+ function getLinkedAccountId() {
466
+ return currentConfig?.linkedAccountId;
467
+ }
468
+ function getClaimResult() {
469
+ return currentConfig?.claimResult;
470
+ }
411
471
  function _setCurrentConfig(config) {
412
472
  currentConfig = config;
413
473
  }
@@ -719,7 +779,7 @@ function buildCorsHeaders(origin) {
719
779
  }
720
780
  return headers;
721
781
  }
722
- function createDiscoveryHandler(getAnonKey, getSessionId) {
782
+ function createDiscoveryHandler(getAnonKey, getSessionId, getClaimState) {
723
783
  return async (request) => {
724
784
  let url;
725
785
  try {
@@ -763,8 +823,16 @@ function createDiscoveryHandler(getAnonKey, getSessionId) {
763
823
  );
764
824
  }
765
825
  const sessionId = getSessionId();
826
+ const responseBody = { key: anonKey, sessionId };
827
+ const claimState = getClaimState?.();
828
+ if (claimState?.claimed) {
829
+ responseBody.claimed = true;
830
+ if (claimState.accountHint) {
831
+ responseBody.accountHint = claimState.accountHint;
832
+ }
833
+ }
766
834
  return new Response(
767
- JSON.stringify({ key: anonKey, sessionId }),
835
+ JSON.stringify(responseBody),
768
836
  {
769
837
  status: 200,
770
838
  headers: corsHeaders
@@ -3522,9 +3590,15 @@ function registerGlasstrace(options) {
3522
3590
  if (isDiscoveryEnabled(config)) {
3523
3591
  let resolvedAnonKey = null;
3524
3592
  const anonKeyPromise = getOrCreateAnonKey();
3593
+ const getClaimState = () => {
3594
+ if (getLinkedAccountId()) return { claimed: true };
3595
+ if (getClaimResult()) return { claimed: true };
3596
+ return null;
3597
+ };
3525
3598
  discoveryHandler = createDiscoveryHandler(
3526
3599
  async () => resolvedAnonKey,
3527
- () => sessionManager.getSessionId(getResolvedApiKey())
3600
+ () => sessionManager.getSessionId(getResolvedApiKey()),
3601
+ getClaimState
3528
3602
  );
3529
3603
  if (config.verbose) {
3530
3604
  console.info("[glasstrace] Discovery endpoint registered (key pending).");
@@ -3540,7 +3614,8 @@ function registerGlasstrace(options) {
3540
3614
  if (currentGeneration !== registrationGeneration) return;
3541
3615
  discoveryHandler = createDiscoveryHandler(
3542
3616
  () => Promise.resolve(anonKey),
3543
- () => sessionManager.getSessionId(getResolvedApiKey())
3617
+ () => sessionManager.getSessionId(getResolvedApiKey()),
3618
+ getClaimState
3544
3619
  );
3545
3620
  await backgroundInit(config, anonKey, currentGeneration);
3546
3621
  } catch (err) {
@@ -3597,7 +3672,7 @@ async function backgroundInit(config, anonKeyForInit, generation) {
3597
3672
  if (config.verbose) {
3598
3673
  console.info("[glasstrace] Background init firing.");
3599
3674
  }
3600
- const initResult = await performInit(config, anonKeyForInit, "0.10.0");
3675
+ const initResult = await performInit(config, anonKeyForInit, "0.12.0");
3601
3676
  if (generation !== registrationGeneration) return;
3602
3677
  if (initResult?.claimResult) {
3603
3678
  setResolvedApiKey(initResult.claimResult.newApiKey);
@@ -3671,16 +3746,16 @@ async function handleSourceMapUpload(distDir) {
3671
3746
  );
3672
3747
  return;
3673
3748
  }
3674
- const { collectSourceMaps: collectSourceMaps2, computeBuildHash: computeBuildHash2, uploadSourceMaps: uploadSourceMaps2 } = await import("./source-map-uploader-OA5NCDOK.js");
3675
- const maps = await collectSourceMaps2(distDir);
3676
- if (maps.length === 0) {
3749
+ const { discoverSourceMapFiles: discoverSourceMapFiles2, computeBuildHash: computeBuildHash2, uploadSourceMaps: uploadSourceMaps2 } = await import("./source-map-uploader-ZFCYOURS.js");
3750
+ const files = await discoverSourceMapFiles2(distDir);
3751
+ if (files.length === 0) {
3677
3752
  console.info("[glasstrace] No source map files found. Skipping upload.");
3678
3753
  return;
3679
3754
  }
3680
- const buildHash = await computeBuildHash2(maps);
3681
- await uploadSourceMaps2(apiKey, endpoint, buildHash, maps);
3755
+ const buildHash = await computeBuildHash2(files);
3756
+ await uploadSourceMaps2(apiKey, endpoint, buildHash, files);
3682
3757
  console.info(
3683
- `[glasstrace] Uploaded ${String(maps.length)} source map(s) for build ${buildHash}.`
3758
+ `[glasstrace] Uploaded ${String(files.length)} source map(s) for build ${buildHash}.`
3684
3759
  );
3685
3760
  } catch (error) {
3686
3761
  const message = error instanceof Error ? error.message : "Unknown error";
@@ -3722,11 +3797,13 @@ export {
3722
3797
  computeBuildHash,
3723
3798
  createDiscoveryHandler,
3724
3799
  deriveSessionId,
3800
+ discoverSourceMapFiles,
3725
3801
  discoverTestFiles,
3726
3802
  extractImports,
3727
3803
  getActiveConfig,
3728
3804
  getDateString,
3729
3805
  getDiscoveryHandler,
3806
+ getLinkedAccountId,
3730
3807
  getOrCreateAnonKey,
3731
3808
  getOrigin,
3732
3809
  isAnonymousMode,