@krak-stack/registry 0.1.52 → 0.1.53

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/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # @krak-stack/registry
2
2
 
3
+ ## 0.1.53
4
+
5
+ ### Patch Changes
6
+
7
+ - Use Effect Crypto for secret-salted SHA-256 visitor IDs and generated UUIDv7 event and visit IDs. Applications must provide a platform Crypto layer alongside Otlp. The versioned visitor derivation changes IDs from the previous HMAC algorithm.
8
+
9
+ Replace the Events layer factories with default `Events.layer` / `Events.baseLayer` values and partial `Events.layerWith(options)` / `Events.baseLayerWith(options)` overrides. Missing secret and origin options read server-only `APP_SECRET` and the origin of `VITE_SITE_URL`; explicit values skip their environment lookup. Geolocation remains opt-in. Migrate existing `Events.layer(options)` and `Events.baseLayer(options)` calls to their With counterparts.
10
+
3
11
  ## 0.1.52
4
12
 
5
13
  ### Patch Changes
@@ -1,4 +1,4 @@
1
- import { Context, DateTime, Effect, Layer, Redacted, Schema } from "effect";
1
+ import { Config, Context, Crypto, DateTime, Effect, Layer, Redacted, Schema } from "effect";
2
2
  import { HttpServerRequest } from "effect/http";
3
3
  import { OtlpLogsProcessor, OtlpProcessingError } from "../opentelemetry/processor.js";
4
4
  import { Otlp } from "../opentelemetry/index.js";
@@ -29,7 +29,7 @@ export declare const dailyVisitorId: (input: {
29
29
  readonly userAgent: string;
30
30
  readonly day: string;
31
31
  readonly secret: Redacted.Redacted<string>;
32
- }) => string;
32
+ }) => Effect.Effect<string, import("effect/PlatformError").PlatformError, Crypto.Crypto>;
33
33
  declare const Events_base: Context.ServiceClass<Events, "krakstack/events/Events", {
34
34
  process: (body: Uint8Array<ArrayBufferLike>) => Effect.Effect<{
35
35
  body: Uint8Array<ArrayBufferLike>;
@@ -67,7 +67,7 @@ declare const Events_base: Context.ServiceClass<Events, "krakstack/events/Events
67
67
  }) => Effect.Effect<{
68
68
  eventId: string;
69
69
  inserted: boolean;
70
- }, EventConflictError | EventsPersistenceError | Schema.SchemaError, never>;
70
+ }, EventConflictError | EventsPersistenceError | import("effect/PlatformError").PlatformError | Schema.SchemaError, never>;
71
71
  listAudit: (input: {
72
72
  readonly page?: string | number | undefined;
73
73
  readonly pageSize?: string | number | undefined;
@@ -155,7 +155,7 @@ declare const Events_base: Context.ServiceClass<Events, "krakstack/events/Events
155
155
  }) => Effect.Effect<{
156
156
  eventId: string;
157
157
  inserted: boolean;
158
- }, EventConflictError | EventsPersistenceError | Schema.SchemaError, never>;
158
+ }, EventConflictError | EventsPersistenceError | import("effect/PlatformError").PlatformError | Schema.SchemaError, never>;
159
159
  listAudit: (input: {
160
160
  readonly page?: string | number | undefined;
161
161
  readonly pageSize?: string | number | undefined;
@@ -205,11 +205,14 @@ declare const Events_base: Context.ServiceClass<Events, "krakstack/events/Events
205
205
  pageCount: number;
206
206
  };
207
207
  }, EventsPersistenceError | import("effect/Cause").NoSuchElementError | Schema.SchemaError | import("effect/sql/SqlError").SqlError, never>;
208
- }, EventsConfigurationError, Otlp>;
208
+ }, EventsConfigurationError, Crypto.Crypto | Otlp>;
209
209
  };
210
210
  export declare class Events extends Events_base {
211
- static readonly baseLayer: (options: EventsOptions) => Layer.Layer<Events, EventsConfigurationError, Otlp>;
211
+ /** Missing options resolve from APP_SECRET, VITE_SITE_URL and Otlp. */
212
+ static readonly baseLayerWith: (options?: Partial<EventsOptions>) => Layer.Layer<Events, Config.ConfigError | EventsConfigurationError, Crypto.Crypto | Otlp>;
213
+ static readonly baseLayer: Layer.Layer<Events, Config.ConfigError | EventsConfigurationError, Crypto.Crypto | Otlp>;
212
214
  /** An injected PostgreSQL/SQLite SqlClient enables persistence and the initial migration. */
213
- static readonly layer: (options: EventsOptions) => Layer.Layer<Events, EventsConfigurationError | import("effect/sql/Migrator").MigrationError | import("effect/sql/SqlError").SqlError, Otlp>;
215
+ static readonly layerWith: (options?: Partial<EventsOptions>) => Layer.Layer<Events, Config.ConfigError | EventsConfigurationError | import("effect/sql/Migrator").MigrationError | import("effect/sql/SqlError").SqlError, Crypto.Crypto | Otlp>;
216
+ static readonly layer: Layer.Layer<Events, Config.ConfigError | EventsConfigurationError | import("effect/sql/Migrator").MigrationError | import("effect/sql/SqlError").SqlError, Crypto.Crypto | Otlp>;
214
217
  static readonly ingestionLayer: Layer.Layer<OtlpLogsProcessor, never, Events>;
215
218
  }
@@ -1,7 +1,8 @@
1
1
  // ../../src/services/events/index.ts
2
- import { createHmac, randomUUID } from "node:crypto";
3
2
  import {
3
+ Config as Config2,
4
4
  Context as Context4,
5
+ Crypto,
5
6
  DateTime as DateTime2,
6
7
  Effect as Effect7,
7
8
  Layer as Layer3,
@@ -13,6 +14,7 @@ import {
13
14
  } from "effect";
14
15
  import { HttpServerRequest } from "effect/http";
15
16
  import { SqlClient as SqlClient2, SqlSchema } from "effect/sql";
17
+ import { Hex } from "effect/encoding";
16
18
 
17
19
  // ../../src/services/opentelemetry/processor.ts
18
20
  import { Context, Schema } from "effect";
@@ -616,16 +618,29 @@ var eventContent = (record, source, compareTimestamp = source === "browser") =>
616
618
  "app.event.schema.version"
617
619
  ].includes(key) || key === "user.id" || key === "organization.id").sort((a, b) => a.key.localeCompare(b.key))
618
620
  });
619
- var dailyVisitorId = (input) => createHmac("sha256", Redacted.value(input.secret)).update(JSON.stringify([
620
- "analytics.visitor.v1",
621
- input.serviceName,
622
- input.day,
623
- input.ip,
624
- input.userAgent
625
- ])).digest("hex");
621
+ var dailyVisitorId = Effect7.fnUntraced(function* (input) {
622
+ const crypto = yield* Crypto.Crypto;
623
+ const encoder = new TextEncoder;
624
+ const salt = yield* crypto.digest("SHA-256", encoder.encode(JSON.stringify([
625
+ "analytics.visitor.salt.v2",
626
+ Redacted.value(input.secret),
627
+ input.serviceName,
628
+ input.day
629
+ ])));
630
+ const digest = yield* crypto.digest("SHA-256", encoder.encode(JSON.stringify([
631
+ "analytics.visitor.v2",
632
+ input.serviceName,
633
+ input.day,
634
+ input.ip,
635
+ input.userAgent,
636
+ Hex.encode(salt)
637
+ ])));
638
+ return Hex.encode(digest);
639
+ });
626
640
 
627
641
  class Events extends Context4.Service()("krakstack/events/Events", {
628
642
  make: (options) => Effect7.gen(function* () {
643
+ const crypto = yield* Crypto.Crypto;
629
644
  const telemetry = yield* Otlp;
630
645
  const resource = options.serviceName ? yield* OtlpResource3.fromConfig({ serviceName: options.serviceName }) : yield* telemetry.resource;
631
646
  const configuration = yield* Schema8.decodeUnknownEffect(Configuration)({
@@ -717,18 +732,18 @@ class Events extends Context4.Service()("krakstack/events/Events", {
717
732
  }, Effect7.mapError(() => new EventGeolocationError({})));
718
733
  const resolveRequestContext = Effect7.fnUntraced(function* (request, now, location) {
719
734
  const userAgent = request.headers["user-agent"]?.slice(0, 2048) ?? "";
720
- const visitorId = location?.ip && userAgent ? dailyVisitorId({
735
+ const visitorId = location?.ip && userAgent ? yield* dailyVisitorId({
721
736
  serviceName: configuration.serviceName,
722
737
  secret: options.secret,
723
738
  ip: location.ip,
724
739
  userAgent,
725
740
  day: DateTime2.formatIsoDate(now)
726
- }) : undefined;
741
+ }).pipe(Effect7.provideService(Crypto.Crypto, crypto)) : undefined;
727
742
  let sessionId;
728
743
  if (visitorId && storage) {
729
744
  yield* storage.lockVisitor(visitorId);
730
745
  const latest = yield* storage.latestSession({ visitorId });
731
- sessionId = Option5.isSome(latest) && latest.value.received_at > DateTime2.toEpochMillis(now) - SESSION_TIMEOUT_MS ? latest.value.session_id : randomUUID();
746
+ sessionId = Option5.isSome(latest) && latest.value.received_at > DateTime2.toEpochMillis(now) - SESSION_TIMEOUT_MS ? latest.value.session_id : yield* crypto.randomUUIDv7;
732
747
  }
733
748
  const identity = {};
734
749
  if (visitorId)
@@ -744,7 +759,7 @@ class Events extends Context4.Service()("krakstack/events/Events", {
744
759
  });
745
760
  const track = Effect7.fn("Events.track")(function* (input) {
746
761
  const event = yield* Schema8.decodeUnknownEffect(TrackEvent)(input);
747
- const eventId = event.eventId ?? randomUUID();
762
+ const eventId = event.eventId ?? (yield* crypto.randomUUIDv7);
748
763
  const category = event.category ?? "analytics";
749
764
  if (!storage)
750
765
  return yield* new EventsPersistenceError({
@@ -1016,8 +1031,13 @@ class Events extends Context4.Service()("krakstack/events/Events", {
1016
1031
  return { process, track, listAudit };
1017
1032
  })
1018
1033
  }) {
1019
- static baseLayer = (options) => Layer3.effect(this, this.make(options));
1020
- static layer = (options) => this.baseLayer(options).pipe(Layer3.provide(Layer3.effectDiscard(Effect7.gen(function* () {
1034
+ static baseLayerWith = (options = {}) => Layer3.effect(this, Effect7.gen(function* () {
1035
+ const origin = options.origin ?? (yield* Config2.URL("VITE_SITE_URL").pipe(Config2.map((url) => url.origin)));
1036
+ const secret = options.secret ?? (yield* Config2.Redacted("APP_SECRET"));
1037
+ return yield* Events.make({ ...options, origin, secret });
1038
+ }));
1039
+ static baseLayer = this.baseLayerWith();
1040
+ static layerWith = (options = {}) => this.baseLayerWith(options).pipe(Layer3.provide(Layer3.effectDiscard(Effect7.gen(function* () {
1021
1041
  const client = yield* Effect7.serviceOption(SqlClient2.SqlClient);
1022
1042
  if (Option5.isNone(client))
1023
1043
  return;
@@ -1032,6 +1052,7 @@ class Events extends Context4.Service()("krakstack/events/Events", {
1032
1052
  }
1033
1053
  yield* migrateEvents.pipe(Effect7.provideService(SqlClient2.SqlClient, client.value));
1034
1054
  }))));
1055
+ static layer = this.layerWith();
1035
1056
  static ingestionLayer = Layer3.effect(OtlpLogsProcessor, Events);
1036
1057
  }
1037
1058
  export {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@krak-stack/registry",
3
- "version": "0.1.52",
3
+ "version": "0.1.53",
4
4
  "description": "Tree-shakable KrakStack components and Effect services.",
5
5
  "license": "MIT",
6
6
  "repository": {