@cookielab.io/oopst 0.1.0 → 0.2.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.
package/dist/index.js CHANGED
@@ -4341,7 +4341,7 @@ init_esm_shims();
4341
4341
 
4342
4342
  // src/sync/aggregate.ts
4343
4343
  init_esm_shims();
4344
- import { createHash } from "crypto";
4344
+ import { createHmac } from "crypto";
4345
4345
  function toIso(hourUtc) {
4346
4346
  return hourUtc instanceof Date ? hourUtc.toISOString() : hourUtc;
4347
4347
  }
@@ -4360,8 +4360,12 @@ function finalize(acc) {
4360
4360
  sessionCount: acc.sessionIds.size
4361
4361
  };
4362
4362
  }
4363
- function createSessionKey(harness, localSessionId) {
4364
- return createHash("sha256").update(harness).update("\0").update(localSessionId).digest("hex");
4363
+ var SESSION_KEY_SALT_PATTERN = /^[a-f0-9]{64}$/u;
4364
+ function createSessionKey(harness, localSessionId, sessionKeySalt) {
4365
+ if (!SESSION_KEY_SALT_PATTERN.test(sessionKeySalt)) {
4366
+ throw new Error("invalid session key salt");
4367
+ }
4368
+ return createHmac("sha256", Buffer.from(sessionKeySalt, "hex")).update("oopst/session-key/v1").update("\0").update(harness).update("\0").update(localSessionId).digest("hex");
4365
4369
  }
4366
4370
  function sessionMapKey(harness, sessionKey) {
4367
4371
  return `${harness} ${sessionKey}`;
@@ -4374,8 +4378,8 @@ function finalizeSession(acc) {
4374
4378
  lastSeenAt: acc.lastSeenAt.toISOString()
4375
4379
  };
4376
4380
  }
4377
- function addSessionAccumulator(sessionAccumulators, bucket, localSessionId) {
4378
- const sessionKey = createSessionKey(bucket.harness, localSessionId);
4381
+ function addSessionAccumulator(sessionAccumulators, bucket, localSessionId, sessionKeySalt) {
4382
+ const sessionKey = createSessionKey(bucket.harness, localSessionId, sessionKeySalt);
4379
4383
  const sessionAccumulatorKey = sessionMapKey(bucket.harness, sessionKey);
4380
4384
  const existingSession = sessionAccumulators.get(sessionAccumulatorKey);
4381
4385
  if (existingSession) {
@@ -4419,7 +4423,7 @@ async function* aggregateUsage(source, options) {
4419
4423
  acc.cacheCreateTokens += bucket.cacheCreateTokens;
4420
4424
  for (const id of bucket.sessionIds) {
4421
4425
  acc.sessionIds.add(id);
4422
- addSessionAccumulator(sessionAccumulators, bucket, id);
4426
+ addSessionAccumulator(sessionAccumulators, bucket, id, options.sessionKeySalt);
4423
4427
  }
4424
4428
  }
4425
4429
  const finalized = [];
@@ -5827,10 +5831,14 @@ async function runSync(options) {
5827
5831
  const client = options.client ?? createClient({ token: options.token, serverUrl: options.serverUrl, fetch: options.fetch });
5828
5832
  const color = options.color === true;
5829
5833
  let watermark = null;
5834
+ let sessionKeySalt;
5830
5835
  try {
5831
5836
  options.logger.info(phase("Checking server sync point...", color));
5832
- const wm = await client.sync.getWatermark.query({ device: options.device });
5833
- watermark = wm.watermarkHourUtc ? new Date(wm.watermarkHourUtc) : null;
5837
+ const { sessionKeySalt: returnedSessionKeySalt, watermarkHourUtc } = await client.sync.getWatermark.mutate({
5838
+ device: options.device
5839
+ });
5840
+ watermark = watermarkHourUtc ? new Date(watermarkHourUtc) : null;
5841
+ sessionKeySalt = returnedSessionKeySalt;
5834
5842
  } catch (err) {
5835
5843
  return failureFromTrpcError(err, options.logger);
5836
5844
  }
@@ -5858,7 +5866,10 @@ async function runSync(options) {
5858
5866
  const batches = [];
5859
5867
  try {
5860
5868
  const progressSources = sources.map((source) => withScanProgress(source, options.logger, color));
5861
- for await (const batch of aggregateUsage(merge(progressSources), { batchSize: SYNC_BATCH_SIZE })) {
5869
+ for await (const batch of aggregateUsage(merge(progressSources), {
5870
+ batchSize: SYNC_BATCH_SIZE,
5871
+ sessionKeySalt
5872
+ })) {
5862
5873
  if (batch.buckets.length === 0 && batch.sessions.length === 0) {
5863
5874
  continue;
5864
5875
  }
@@ -5887,7 +5898,7 @@ async function runSync(options) {
5887
5898
  options.logger.info(
5888
5899
  `${dryRunPrefix(color)} ${usageRecordText(plannedBuckets)} from ${localSessionText(plannedSessions)} would be uploaded in ${uploadRequestText(batches.length)}.`
5889
5900
  );
5890
- options.logger.info(muted("No data was sent.", color));
5901
+ options.logger.info(muted("No usage data was uploaded.", color));
5891
5902
  return {
5892
5903
  exitCode: EXIT.success,
5893
5904
  batchesUploaded: batches.length,