@sixb/client 0.1.20 → 0.1.21

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 (45) hide show
  1. package/dist/activity-attribution.d.ts +29 -0
  2. package/dist/activity-attribution.d.ts.map +1 -0
  3. package/dist/activity-tracker.d.ts +105 -0
  4. package/dist/activity-tracker.d.ts.map +1 -0
  5. package/dist/browser.d.ts +2 -0
  6. package/dist/browser.d.ts.map +1 -1
  7. package/dist/browser.js +513 -11
  8. package/dist/browser.js.map +6 -4
  9. package/dist/{chunk-g05ar78c.js → chunk-3b154p8n.js} +3 -3
  10. package/dist/{chunk-tkjkrcdt.js → chunk-3f2pjdzk.js} +155 -2
  11. package/dist/chunk-3f2pjdzk.js.map +19 -0
  12. package/dist/{chunk-awbb496e.js → chunk-9wd8x2em.js} +1 -1
  13. package/dist/{chunk-3aa3m9z6.js → chunk-ana8rtgb.js} +2 -2
  14. package/dist/{chunk-95bnb04n.js → chunk-qdq5gfqx.js} +1 -1
  15. package/dist/{chunk-12s8gbwj.js → chunk-s5zamywm.js} +2 -2
  16. package/dist/generated/@tanstack/react-query.gen.d.ts +2281 -591
  17. package/dist/generated/@tanstack/react-query.gen.d.ts.map +1 -1
  18. package/dist/generated/index.d.ts +2 -2
  19. package/dist/generated/index.d.ts.map +1 -1
  20. package/dist/generated/sdk.gen.d.ts +89 -1
  21. package/dist/generated/sdk.gen.d.ts.map +1 -1
  22. package/dist/generated/types.gen.d.ts +1669 -448
  23. package/dist/generated/types.gen.d.ts.map +1 -1
  24. package/dist/hooks.js +299 -6
  25. package/dist/hooks.js.map +3 -3
  26. package/dist/index.js +40 -6
  27. package/dist/index.js.map +1 -1
  28. package/dist/logs.js +3 -3
  29. package/dist/models.js +36 -2
  30. package/dist/models.js.map +1 -1
  31. package/dist/query.js +3 -3
  32. package/package.json +2 -2
  33. package/src/activity-attribution.ts +211 -0
  34. package/src/activity-tracker.ts +527 -0
  35. package/src/browser.ts +14 -1
  36. package/src/generated/@tanstack/react-query.gen.ts +580 -0
  37. package/src/generated/index.ts +101 -0
  38. package/src/generated/sdk.gen.ts +389 -0
  39. package/src/generated/types.gen.ts +2405 -1090
  40. package/dist/chunk-tkjkrcdt.js.map +0 -19
  41. /package/dist/{chunk-g05ar78c.js.map → chunk-3b154p8n.js.map} +0 -0
  42. /package/dist/{chunk-awbb496e.js.map → chunk-9wd8x2em.js.map} +0 -0
  43. /package/dist/{chunk-3aa3m9z6.js.map → chunk-ana8rtgb.js.map} +0 -0
  44. /package/dist/{chunk-95bnb04n.js.map → chunk-qdq5gfqx.js.map} +0 -0
  45. /package/dist/{chunk-12s8gbwj.js.map → chunk-s5zamywm.js.map} +0 -0
package/dist/index.js CHANGED
@@ -134,6 +134,15 @@ import {
134
134
  retryAgentRun2,
135
135
  listAgentThreadRuns2,
136
136
  getAgentRun2,
137
+ listArtifacts2,
138
+ createArtifact2,
139
+ deleteArtifact2,
140
+ getArtifact2,
141
+ publishArtifactVersion2,
142
+ createArtifactFrame2,
143
+ getArtifactVersion2,
144
+ getArtifactFileContent2,
145
+ headArtifactFileContent2,
137
146
  listModels2,
138
147
  removeObjectLink2,
139
148
  upsertObjectLink2,
@@ -143,12 +152,20 @@ import {
143
152
  getLatestTelemetry2,
144
153
  listEvents2,
145
154
  listLogs2,
155
+ getActivityCaptureConfig2,
156
+ captureActivity2,
157
+ listActivityVisits2,
158
+ listActivityUsers2,
159
+ getActivitySummary2,
160
+ getActivityCalendar2,
161
+ getActivityVisitTimeline2,
162
+ listActivityConnections2,
146
163
  listProjections2,
147
164
  getProjection2,
148
165
  listProjectionRuns2,
149
166
  getProjectionRun2,
150
167
  listWebhookRuns2
151
- } from "./chunk-tkjkrcdt.js";
168
+ } from "./chunk-3f2pjdzk.js";
152
169
  import {
153
170
  createSixbAgentsWebSocketUrl2,
154
171
  createAgentRunSocket2,
@@ -169,7 +186,7 @@ import {
169
186
  objectFileContentUrl2,
170
187
  SixbFileUploadError2,
171
188
  uploadFile2
172
- } from "./chunk-g05ar78c.js";
189
+ } from "./chunk-3b154p8n.js";
173
190
  import {
174
191
  SIXB_CSRF_HEADER_NAME2,
175
192
  SIXB_CSRF_TOKEN_RESPONSE_HEADER_NAME2,
@@ -179,7 +196,7 @@ import {
179
196
  createSixbClient2,
180
197
  configureSixbClient2,
181
198
  createSixbClientConfig2
182
- } from "./chunk-awbb496e.js";
199
+ } from "./chunk-9wd8x2em.js";
183
200
  import {
184
201
  encodeObjectId2,
185
202
  decodeObjectId2,
@@ -195,8 +212,8 @@ import {
195
212
  logs2,
196
213
  isStoredLogLine2,
197
214
  isStoredLogLine3
198
- } from "./chunk-12s8gbwj.js";
199
- import"./chunk-95bnb04n.js";
215
+ } from "./chunk-s5zamywm.js";
216
+ import"./chunk-qdq5gfqx.js";
200
217
  import"./chunk-0ka9ykez.js";
201
218
  export {
202
219
  ActionRunFailedError2 as ActionRunFailedError,
@@ -213,6 +230,7 @@ export {
213
230
  cancelAgentRun2 as cancelAgentRun,
214
231
  cancelWorkflowIntervention2 as cancelWorkflowIntervention,
215
232
  cancelWorkflowRun2 as cancelWorkflowRun,
233
+ captureActivity2 as captureActivity,
216
234
  client2 as client,
217
235
  completeFileUpload2 as completeFileUpload,
218
236
  configureSixbClient2 as configureSixbClient,
@@ -221,6 +239,8 @@ export {
221
239
  createAgentRunSocket2 as createAgentRunSocket,
222
240
  createAgentThread2 as createAgentThread,
223
241
  createAiLimitPolicy2 as createAiLimitPolicy,
242
+ createArtifact2 as createArtifact,
243
+ createArtifactFrame2 as createArtifactFrame,
224
244
  createAuthInvitation2 as createAuthInvitation,
225
245
  createAuthPersonalAccessToken2 as createAuthPersonalAccessToken,
226
246
  createAuthServiceAccount2 as createAuthServiceAccount,
@@ -237,6 +257,7 @@ export {
237
257
  createWsSubscribeExecutor2 as createWsSubscribeExecutor,
238
258
  decodeObjectId2 as decodeObjectId,
239
259
  deleteAiLimitPolicy2 as deleteAiLimitPolicy,
260
+ deleteArtifact2 as deleteArtifact,
240
261
  disableAuthServiceAccount2 as disableAuthServiceAccount,
241
262
  disconnectConnectorConnection2 as disconnectConnectorConnection,
242
263
  encodeObjectId2 as encodeObjectId,
@@ -248,6 +269,10 @@ export {
248
269
  getAction2 as getAction,
249
270
  getActionRun2 as getActionRun,
250
271
  getActionRunFileContent2 as getActionRunFileContent,
272
+ getActivityCalendar2 as getActivityCalendar,
273
+ getActivityCaptureConfig2 as getActivityCaptureConfig,
274
+ getActivitySummary2 as getActivitySummary,
275
+ getActivityVisitTimeline2 as getActivityVisitTimeline,
251
276
  getAgent2 as getAgent,
252
277
  getAgentMessageFileContent2 as getAgentMessageFileContent,
253
278
  getAgentRun2 as getAgentRun,
@@ -255,6 +280,9 @@ export {
255
280
  getAiAccountingOverview2 as getAiAccountingOverview,
256
281
  getAiLimitStatus2 as getAiLimitStatus,
257
282
  getAiLimitSubjectOptions2 as getAiLimitSubjectOptions,
283
+ getArtifact2 as getArtifact,
284
+ getArtifactFileContent2 as getArtifactFileContent,
285
+ getArtifactVersion2 as getArtifactVersion,
258
286
  getAuthAccessManagementOptions2 as getAuthAccessManagementOptions,
259
287
  getAuthInvitationOptions2 as getAuthInvitationOptions,
260
288
  getAuthMembershipOptions2 as getAuthMembershipOptions,
@@ -287,6 +315,7 @@ export {
287
315
  getWorkflowRunFileContent2 as getWorkflowRunFileContent,
288
316
  headActionRunFileContent2 as headActionRunFileContent,
289
317
  headAgentMessageFileContent2 as headAgentMessageFileContent,
318
+ headArtifactFileContent2 as headArtifactFileContent,
290
319
  headObjectFileContent2 as headObjectFileContent,
291
320
  headWorkflowNodeRunFileContent2 as headWorkflowNodeRunFileContent,
292
321
  headWorkflowRunFileContent2 as headWorkflowRunFileContent,
@@ -297,12 +326,16 @@ export {
297
326
  issueSharedAccessGrant2 as issueSharedAccessGrant,
298
327
  listActionRuns2 as listActionRuns,
299
328
  listActions2 as listActions,
329
+ listActivityConnections2 as listActivityConnections,
330
+ listActivityUsers2 as listActivityUsers,
331
+ listActivityVisits2 as listActivityVisits,
300
332
  listAgentThreadMessages2 as listAgentThreadMessages,
301
333
  listAgentThreadRuns2 as listAgentThreadRuns,
302
334
  listAgentThreads2 as listAgentThreads,
303
335
  listAiLimitPolicies2 as listAiLimitPolicies,
304
336
  listAiModelCallGroups2 as listAiModelCallGroups,
305
337
  listAiModelCalls2 as listAiModelCalls,
338
+ listArtifacts2 as listArtifacts,
306
339
  listAuthAccessTokens2 as listAuthAccessTokens,
307
340
  listAuthInvitations2 as listAuthInvitations,
308
341
  listAuthMembers2 as listAuthMembers,
@@ -342,6 +375,7 @@ export {
342
375
  parseEventStreamMessage2 as parseEventStreamMessage,
343
376
  parseLogStreamMessage2 as parseLogStreamMessage,
344
377
  postAgentThreadMessage2 as postAgentThreadMessage,
378
+ publishArtifactVersion2 as publishArtifactVersion,
345
379
  queryObjectLinks2 as queryObjectLinks,
346
380
  queryObjects2 as queryObjects,
347
381
  reactivateAuthMember2 as reactivateAuthMember,
@@ -386,4 +420,4 @@ export {
386
420
  upsertObjectLink2 as upsertObjectLink
387
421
  };
388
422
 
389
- //# debugId=24C4E4B498A72DF764756E2164756E21
423
+ //# debugId=036D074A6058D64064756E2164756E21
package/dist/index.js.map CHANGED
@@ -4,6 +4,6 @@
4
4
  "sourcesContent": [
5
5
  ],
6
6
  "mappings": "",
7
- "debugId": "24C4E4B498A72DF764756E2164756E21",
7
+ "debugId": "036D074A6058D64064756E2164756E21",
8
8
  "names": []
9
9
  }
package/dist/logs.js CHANGED
@@ -6,11 +6,11 @@ import {
6
6
  logs2,
7
7
  isStoredLogLine2,
8
8
  isStoredLogLine3
9
- } from "./chunk-12s8gbwj.js";
10
- import"./chunk-95bnb04n.js";
9
+ } from "./chunk-s5zamywm.js";
10
+ import"./chunk-qdq5gfqx.js";
11
11
  import"./chunk-0ka9ykez.js";
12
12
  import"./chunk-j9t5h44d.js";
13
- import"./chunk-tkjkrcdt.js";
13
+ import"./chunk-3f2pjdzk.js";
14
14
  export {
15
15
  createLogSocket2 as createLogSocket,
16
16
  createSixbLogsWebSocketUrl2 as createSixbLogsWebSocketUrl,
package/dist/models.js CHANGED
@@ -136,6 +136,15 @@ import {
136
136
  retryAgentRun2,
137
137
  listAgentThreadRuns2,
138
138
  getAgentRun2,
139
+ listArtifacts2,
140
+ createArtifact2,
141
+ deleteArtifact2,
142
+ getArtifact2,
143
+ publishArtifactVersion2,
144
+ createArtifactFrame2,
145
+ getArtifactVersion2,
146
+ getArtifactFileContent2,
147
+ headArtifactFileContent2,
139
148
  listModels2,
140
149
  removeObjectLink2,
141
150
  upsertObjectLink2,
@@ -145,12 +154,20 @@ import {
145
154
  getLatestTelemetry2,
146
155
  listEvents2,
147
156
  listLogs2,
157
+ getActivityCaptureConfig2,
158
+ captureActivity2,
159
+ listActivityVisits2,
160
+ listActivityUsers2,
161
+ getActivitySummary2,
162
+ getActivityCalendar2,
163
+ getActivityVisitTimeline2,
164
+ listActivityConnections2,
148
165
  listProjections2,
149
166
  getProjection2,
150
167
  listProjectionRuns2,
151
168
  getProjectionRun2,
152
169
  listWebhookRuns2
153
- } from "./chunk-tkjkrcdt.js";
170
+ } from "./chunk-3f2pjdzk.js";
154
171
  export {
155
172
  abortFileUpload2 as abortFileUpload,
156
173
  addConnectorConnection2 as addConnectorConnection,
@@ -158,11 +175,14 @@ export {
158
175
  cancelAgentRun2 as cancelAgentRun,
159
176
  cancelWorkflowIntervention2 as cancelWorkflowIntervention,
160
177
  cancelWorkflowRun2 as cancelWorkflowRun,
178
+ captureActivity2 as captureActivity,
161
179
  client2 as client,
162
180
  completeFileUpload2 as completeFileUpload,
163
181
  countObjects2 as countObjects,
164
182
  createAgentThread2 as createAgentThread,
165
183
  createAiLimitPolicy2 as createAiLimitPolicy,
184
+ createArtifact2 as createArtifact,
185
+ createArtifactFrame2 as createArtifactFrame,
166
186
  createAuthInvitation2 as createAuthInvitation,
167
187
  createAuthPersonalAccessToken2 as createAuthPersonalAccessToken,
168
188
  createAuthServiceAccount2 as createAuthServiceAccount,
@@ -171,6 +191,7 @@ export {
171
191
  createFileUpload2 as createFileUpload,
172
192
  decodeObjectId2 as decodeObjectId,
173
193
  deleteAiLimitPolicy2 as deleteAiLimitPolicy,
194
+ deleteArtifact2 as deleteArtifact,
174
195
  disableAuthServiceAccount2 as disableAuthServiceAccount,
175
196
  disconnectConnectorConnection2 as disconnectConnectorConnection,
176
197
  encodeObjectId2 as encodeObjectId,
@@ -181,6 +202,10 @@ export {
181
202
  getAction2 as getAction,
182
203
  getActionRun2 as getActionRun,
183
204
  getActionRunFileContent2 as getActionRunFileContent,
205
+ getActivityCalendar2 as getActivityCalendar,
206
+ getActivityCaptureConfig2 as getActivityCaptureConfig,
207
+ getActivitySummary2 as getActivitySummary,
208
+ getActivityVisitTimeline2 as getActivityVisitTimeline,
184
209
  getAgent2 as getAgent,
185
210
  getAgentMessageFileContent2 as getAgentMessageFileContent,
186
211
  getAgentRun2 as getAgentRun,
@@ -188,6 +213,9 @@ export {
188
213
  getAiAccountingOverview2 as getAiAccountingOverview,
189
214
  getAiLimitStatus2 as getAiLimitStatus,
190
215
  getAiLimitSubjectOptions2 as getAiLimitSubjectOptions,
216
+ getArtifact2 as getArtifact,
217
+ getArtifactFileContent2 as getArtifactFileContent,
218
+ getArtifactVersion2 as getArtifactVersion,
191
219
  getAuthAccessManagementOptions2 as getAuthAccessManagementOptions,
192
220
  getAuthInvitationOptions2 as getAuthInvitationOptions,
193
221
  getAuthMembershipOptions2 as getAuthMembershipOptions,
@@ -219,18 +247,23 @@ export {
219
247
  getWorkflowRunFileContent2 as getWorkflowRunFileContent,
220
248
  headActionRunFileContent2 as headActionRunFileContent,
221
249
  headAgentMessageFileContent2 as headAgentMessageFileContent,
250
+ headArtifactFileContent2 as headArtifactFileContent,
222
251
  headObjectFileContent2 as headObjectFileContent,
223
252
  headWorkflowNodeRunFileContent2 as headWorkflowNodeRunFileContent,
224
253
  headWorkflowRunFileContent2 as headWorkflowRunFileContent,
225
254
  issueSharedAccessGrant2 as issueSharedAccessGrant,
226
255
  listActionRuns2 as listActionRuns,
227
256
  listActions2 as listActions,
257
+ listActivityConnections2 as listActivityConnections,
258
+ listActivityUsers2 as listActivityUsers,
259
+ listActivityVisits2 as listActivityVisits,
228
260
  listAgentThreadMessages2 as listAgentThreadMessages,
229
261
  listAgentThreadRuns2 as listAgentThreadRuns,
230
262
  listAgentThreads2 as listAgentThreads,
231
263
  listAiLimitPolicies2 as listAiLimitPolicies,
232
264
  listAiModelCallGroups2 as listAiModelCallGroups,
233
265
  listAiModelCalls2 as listAiModelCalls,
266
+ listArtifacts2 as listArtifacts,
234
267
  listAuthAccessTokens2 as listAuthAccessTokens,
235
268
  listAuthInvitations2 as listAuthInvitations,
236
269
  listAuthMembers2 as listAuthMembers,
@@ -262,6 +295,7 @@ export {
262
295
  listWorkflowRuns2 as listWorkflowRuns,
263
296
  listWorkflows2 as listWorkflows,
264
297
  postAgentThreadMessage2 as postAgentThreadMessage,
298
+ publishArtifactVersion2 as publishArtifactVersion,
265
299
  queryObjectLinks2 as queryObjectLinks,
266
300
  queryObjects2 as queryObjects,
267
301
  reactivateAuthMember2 as reactivateAuthMember,
@@ -301,4 +335,4 @@ export {
301
335
  upsertObjectLink2 as upsertObjectLink
302
336
  };
303
337
 
304
- //# debugId=2E29309C911E2C3664756E2164756E21
338
+ //# debugId=28486CD7CAC778A764756E2164756E21
@@ -4,6 +4,6 @@
4
4
  "sourcesContent": [
5
5
  ],
6
6
  "mappings": "",
7
- "debugId": "2E29309C911E2C3664756E2164756E21",
7
+ "debugId": "28486CD7CAC778A764756E2164756E21",
8
8
  "names": []
9
9
  }
package/dist/query.js CHANGED
@@ -3,10 +3,10 @@ import {
3
3
  SixbQueryError2,
4
4
  objects2,
5
5
  createHttpQueryExecutor2
6
- } from "./chunk-3aa3m9z6.js";
7
- import"./chunk-awbb496e.js";
6
+ } from "./chunk-ana8rtgb.js";
7
+ import"./chunk-9wd8x2em.js";
8
8
  import"./chunk-j9t5h44d.js";
9
- import"./chunk-tkjkrcdt.js";
9
+ import"./chunk-3f2pjdzk.js";
10
10
  export {
11
11
  SixbQueryError2 as SixbQueryError,
12
12
  createHttpQueryExecutor2 as createHttpQueryExecutor,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sixb/client",
3
- "version": "0.1.20",
3
+ "version": "0.1.21",
4
4
  "description": "Type-safe SDK for Sixb API with TanStack Query hooks",
5
5
  "keywords": [
6
6
  "sixb",
@@ -67,7 +67,7 @@
67
67
  "prepack": "bun run build"
68
68
  },
69
69
  "dependencies": {
70
- "@sixb/core": "^0.1.20"
70
+ "@sixb/core": "^0.1.21"
71
71
  },
72
72
  "peerDependencies": {
73
73
  "@tanstack/react-query": "^5.0.0",
@@ -0,0 +1,211 @@
1
+ import type { ActivityAttribution } from "./activity-tracker"
2
+
3
+ const MAX_AGE_MS = 30 * 60_000
4
+ const MAX_STORED_LENGTH = 2_048
5
+ const HOSTNAME = /^(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)*[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?$/
6
+ const CONTROL_CHARACTERS = /[\x00-\x1f\x7f]/
7
+ const UTM_KEYS = ["source", "medium", "campaign"] as const
8
+ const ATTRIBUTION_KEYS = [...UTM_KEYS, "referrer"] as const
9
+
10
+ type ArrivalStorage = Pick<Storage, "getItem" | "setItem" | "removeItem">
11
+ type MutableAttribution = { -readonly [Key in keyof ActivityAttribution]: ActivityAttribution[Key] }
12
+
13
+ /**
14
+ * How a document was reached: its UTM labels, else the external host that referred it, else `{}`
15
+ * for direct. `undefined` when the campaign labels are malformed: not collected, rather than a
16
+ * direct arrival it was not. Never a browsing URL.
17
+ */
18
+ export function readActivityAttribution(
19
+ href: string,
20
+ referrer: string,
21
+ apiBaseUrl: string
22
+ ): ActivityAttribution | undefined {
23
+ const url = new URL(href)
24
+
25
+ const utm: MutableAttribution = {}
26
+ let malformed = false
27
+ for (const key of UTM_KEYS) {
28
+ const raw = url.searchParams.get(`utm_${key}`)
29
+ const value = label(raw)
30
+ if (value !== undefined) {
31
+ utm[key] = value
32
+ } else if (raw?.trim()) {
33
+ malformed = true
34
+ }
35
+ }
36
+
37
+ if (Object.keys(utm).length > 0) {
38
+ return utm
39
+ }
40
+ if (malformed) {
41
+ return undefined
42
+ }
43
+
44
+ return externalReferrer(referrer, [url.hostname, new URL(apiBaseUrl).hostname])
45
+ }
46
+
47
+ /**
48
+ * A short-lived, per-tab handoff across Sixb's own redirects, such as sign-in. It holds the
49
+ * arrival only: no event, route or identity.
50
+ */
51
+ export class ActivityArrival {
52
+ attribution: ActivityAttribution | undefined
53
+
54
+ constructor(
55
+ private readonly storage: ArrivalStorage | null,
56
+ private readonly key: string,
57
+ current: ActivityAttribution | undefined,
58
+ private readonly now: () => number = Date.now
59
+ ) {
60
+ const pending = this.takePending()
61
+ const hasCampaign = UTM_KEYS.some((key) => current?.[key] !== undefined)
62
+ // A fresh campaign wins; otherwise the arrival from before the redirect does.
63
+ this.attribution = hasCampaign ? current : (pending ?? current)
64
+ }
65
+
66
+ preserve(): void {
67
+ if (this.attribution === undefined) {
68
+ return
69
+ }
70
+
71
+ try {
72
+ this.storage?.setItem(
73
+ this.key,
74
+ JSON.stringify({ at: this.now(), attribution: this.attribution })
75
+ )
76
+ } catch {
77
+ // Without sessionStorage, URL UTMs still survive the normal returnTo flow.
78
+ }
79
+ }
80
+
81
+ private takePending(): ActivityAttribution | undefined {
82
+ try {
83
+ const raw = this.storage?.getItem(this.key)
84
+ this.storage?.removeItem(this.key)
85
+ if (!raw || raw.length > MAX_STORED_LENGTH) {
86
+ return undefined
87
+ }
88
+
89
+ const value: unknown = JSON.parse(raw)
90
+ if (
91
+ !value ||
92
+ typeof value !== "object" ||
93
+ !("at" in value) ||
94
+ !("attribution" in value) ||
95
+ typeof value.at !== "number"
96
+ ) {
97
+ return undefined
98
+ }
99
+
100
+ const age = this.now() - value.at
101
+ return age >= 0 && age < MAX_AGE_MS ? storedAttribution(value.attribution) : undefined
102
+ } catch {
103
+ // Storage restrictions must never interfere with sign-in or the app itself.
104
+ return undefined
105
+ }
106
+ }
107
+ }
108
+
109
+ let arrival: ActivityArrival | undefined
110
+
111
+ /** Reads this document's arrival, for the authority `scope` the client is configured for. */
112
+ export function prepareActivityArrival(apiBaseUrl: string, scope: string): void {
113
+ arrival = undefined
114
+ if (typeof window === "undefined") {
115
+ return
116
+ }
117
+
118
+ let storage: ArrivalStorage | null = null
119
+ try {
120
+ storage = window.sessionStorage
121
+ } catch {
122
+ // Storage may be blocked.
123
+ }
124
+
125
+ try {
126
+ arrival = new ActivityArrival(
127
+ storage,
128
+ `sixb:activity-arrival:${JSON.stringify([apiBaseUrl, scope])}`,
129
+ readActivityAttribution(window.location.href, document.referrer, apiBaseUrl)
130
+ )
131
+ } catch {
132
+ // Outside a browser there may be no usable location.
133
+ }
134
+ }
135
+
136
+ /** Keeps the arrival across a Sixb redirect that replaces this document. */
137
+ export function preserveSixbActivityArrival(): void {
138
+ arrival?.preserve()
139
+ }
140
+
141
+ export function takeActivityArrival(): ActivityAttribution | undefined {
142
+ const value = arrival?.attribution
143
+ // A later redirect from this document continues the visit; it is not another arrival.
144
+ if (arrival) {
145
+ arrival.attribution = {}
146
+ }
147
+ return value
148
+ }
149
+
150
+ export function clearActivityArrival(): void {
151
+ arrival = undefined
152
+ }
153
+
154
+ function label(value: unknown): string | undefined {
155
+ if (typeof value !== "string") {
156
+ return undefined
157
+ }
158
+
159
+ const text = value.trim()
160
+ return text && text.length <= 100 && !CONTROL_CHARACTERS.test(text) ? text : undefined
161
+ }
162
+
163
+ function externalReferrer(referrer: string, ownHosts: readonly string[]): ActivityAttribution {
164
+ try {
165
+ const previous = new URL(referrer)
166
+ const hostname = previous.hostname
167
+ if (
168
+ (previous.protocol === "http:" || previous.protocol === "https:") &&
169
+ !ownHosts.includes(hostname) &&
170
+ hostname.length <= 253 &&
171
+ HOSTNAME.test(hostname)
172
+ ) {
173
+ return { referrer: hostname }
174
+ }
175
+ } catch {
176
+ // Browsers may send no referrer. Direct means no usable source is available.
177
+ }
178
+ return {}
179
+ }
180
+
181
+ /** Stored data is untrusted: anything but the exact attribution shape is dropped. */
182
+ function storedAttribution(value: unknown): ActivityAttribution | undefined {
183
+ if (!value || typeof value !== "object" || Array.isArray(value)) {
184
+ return undefined
185
+ }
186
+ if (Object.keys(value).some((key) => !ATTRIBUTION_KEYS.some((field) => field === key))) {
187
+ return undefined
188
+ }
189
+
190
+ const result: MutableAttribution = {}
191
+ for (const key of ATTRIBUTION_KEYS) {
192
+ const raw: unknown = Reflect.get(value, key)
193
+ if (raw === undefined) {
194
+ continue
195
+ }
196
+ if (typeof raw !== "string") {
197
+ return undefined
198
+ }
199
+
200
+ const valid =
201
+ key === "referrer"
202
+ ? raw.length > 0 && raw.length <= 253 && HOSTNAME.test(raw)
203
+ : label(raw) === raw
204
+ if (!valid) {
205
+ return undefined
206
+ }
207
+ result[key] = raw
208
+ }
209
+
210
+ return result
211
+ }