@glasstrace/sdk 0.7.0 → 0.7.1

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/index.js CHANGED
@@ -186,7 +186,7 @@ function classifyFetchTarget(url) {
186
186
 
187
187
  // src/init-client.ts
188
188
  import { readFileSync } from "fs";
189
- import { writeFile, mkdir } from "fs/promises";
189
+ import { readFile, writeFile, mkdir, chmod } from "fs/promises";
190
190
  import { join } from "path";
191
191
  var GLASSTRACE_DIR = ".glasstrace";
192
192
  var CONFIG_FILE = "config";
@@ -222,12 +222,14 @@ async function saveCachedConfig(response, projectRoot) {
222
222
  const dirPath = join(root, GLASSTRACE_DIR);
223
223
  const configPath = join(dirPath, CONFIG_FILE);
224
224
  try {
225
- await mkdir(dirPath, { recursive: true });
225
+ await mkdir(dirPath, { recursive: true, mode: 448 });
226
+ await chmod(dirPath, 448);
226
227
  const cached = {
227
228
  response,
228
229
  cachedAt: Date.now()
229
230
  };
230
- await writeFile(configPath, JSON.stringify(cached), "utf-8");
231
+ await writeFile(configPath, JSON.stringify(cached), { encoding: "utf-8", mode: 384 });
232
+ await chmod(configPath, 384);
231
233
  } catch (err) {
232
234
  console.warn(
233
235
  `[glasstrace] Failed to cache config to ${configPath}: ${err instanceof Error ? err.message : String(err)}`
@@ -280,6 +282,78 @@ async function sendInitRequest(config, anonKey, sdkVersion, importGraph, healthR
280
282
  const body = await response.json();
281
283
  return SdkInitResponseSchema.parse(body);
282
284
  }
285
+ async function writeClaimedKey(newApiKey, projectRoot) {
286
+ const root = projectRoot ?? process.cwd();
287
+ const envLocalPath = join(root, ".env.local");
288
+ let envLocalWritten = false;
289
+ try {
290
+ let content;
291
+ try {
292
+ content = await readFile(envLocalPath, "utf-8");
293
+ if (/^GLASSTRACE_API_KEY=.*/m.test(content)) {
294
+ content = content.replace(
295
+ /^GLASSTRACE_API_KEY=.*$/gm,
296
+ `GLASSTRACE_API_KEY=${newApiKey}`
297
+ );
298
+ } else {
299
+ if (content.length > 0 && !content.endsWith("\n")) {
300
+ content += "\n";
301
+ }
302
+ content += `GLASSTRACE_API_KEY=${newApiKey}
303
+ `;
304
+ }
305
+ } catch (readErr) {
306
+ const code = readErr instanceof Error ? readErr.code : void 0;
307
+ if (code !== "ENOENT") {
308
+ throw readErr;
309
+ }
310
+ content = `GLASSTRACE_API_KEY=${newApiKey}
311
+ `;
312
+ }
313
+ await writeFile(envLocalPath, content, { encoding: "utf-8", mode: 384 });
314
+ await chmod(envLocalPath, 384);
315
+ envLocalWritten = true;
316
+ } catch {
317
+ }
318
+ if (envLocalWritten) {
319
+ try {
320
+ process.stderr.write(
321
+ "[glasstrace] Account claimed! API key written to .env.local. Restart your dev server to use it.\n"
322
+ );
323
+ } catch {
324
+ }
325
+ return;
326
+ }
327
+ let claimedKeyWritten = false;
328
+ try {
329
+ const dirPath = join(root, GLASSTRACE_DIR);
330
+ await mkdir(dirPath, { recursive: true, mode: 448 });
331
+ await chmod(dirPath, 448);
332
+ const claimedKeyPath = join(dirPath, "claimed-key");
333
+ await writeFile(claimedKeyPath, newApiKey, {
334
+ encoding: "utf-8",
335
+ mode: 384
336
+ });
337
+ await chmod(claimedKeyPath, 384);
338
+ claimedKeyWritten = true;
339
+ } catch {
340
+ }
341
+ if (claimedKeyWritten) {
342
+ try {
343
+ process.stderr.write(
344
+ "[glasstrace] Account claimed! API key written to .glasstrace/claimed-key. Copy it to your .env.local file.\n"
345
+ );
346
+ } catch {
347
+ }
348
+ return;
349
+ }
350
+ try {
351
+ process.stderr.write(
352
+ "[glasstrace] Account claimed but could not write key to disk. Visit your dashboard settings to rotate and retrieve a new API key.\n"
353
+ );
354
+ } catch {
355
+ }
356
+ }
283
357
  async function performInit(config, anonKey, sdkVersion) {
284
358
  if (rateLimitBackoff) {
285
359
  rateLimitBackoff = false;
@@ -308,14 +382,8 @@ async function performInit(config, anonKey, sdkVersion) {
308
382
  await saveCachedConfig(result);
309
383
  if (result.claimResult) {
310
384
  try {
311
- process.stderr.write(
312
- `[glasstrace] Account claimed! Update GLASSTRACE_API_KEY=${result.claimResult.newApiKey} in your .env file.
313
- `
314
- );
315
- } catch (logErr) {
316
- console.warn(
317
- `[glasstrace] Failed to write claim migration message: ${logErr instanceof Error ? logErr.message : String(logErr)}`
318
- );
385
+ await writeClaimedKey(result.claimResult.newApiKey);
386
+ } catch {
319
387
  }
320
388
  return { claimResult: result.claimResult };
321
389
  }
@@ -3567,7 +3635,7 @@ function registerGlasstrace(options) {
3567
3635
  if (config.verbose) {
3568
3636
  console.info("[glasstrace] Background init firing.");
3569
3637
  }
3570
- const initResult = await performInit(config, anonKey, "0.7.0");
3638
+ const initResult = await performInit(config, anonKey, "0.7.1");
3571
3639
  if (initResult?.claimResult) {
3572
3640
  setResolvedApiKey(initResult.claimResult.newApiKey);
3573
3641
  notifyApiKeyResolved();
@@ -3591,7 +3659,7 @@ function registerGlasstrace(options) {
3591
3659
  if (config.verbose) {
3592
3660
  console.info("[glasstrace] Background init firing.");
3593
3661
  }
3594
- const initResult = await performInit(config, anonKey, "0.7.0");
3662
+ const initResult = await performInit(config, anonKey, "0.7.1");
3595
3663
  if (initResult?.claimResult) {
3596
3664
  setResolvedApiKey(initResult.claimResult.newApiKey);
3597
3665
  notifyApiKeyResolved();
@@ -3617,7 +3685,7 @@ function registerGlasstrace(options) {
3617
3685
  if (config.verbose) {
3618
3686
  console.info("[glasstrace] Background init firing.");
3619
3687
  }
3620
- await performInit(config, anonKeyForInit, "0.7.0");
3688
+ await performInit(config, anonKeyForInit, "0.7.1");
3621
3689
  maybeInstallConsoleCapture();
3622
3690
  } catch (err) {
3623
3691
  console.warn(