@starko-one/sdk 0.0.0-stage → 0.1.0-beta.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 (108) hide show
  1. package/CHANGELOG.md +124 -0
  2. package/LICENSE +17 -0
  3. package/NOTICE +58 -0
  4. package/README.md +81 -2
  5. package/dist/api-B5IHeINe.d.ts +558 -0
  6. package/dist/api-DR4HazCu.d.cts +558 -0
  7. package/dist/browser/index.cjs +168 -0
  8. package/dist/browser/index.d.cts +131 -0
  9. package/dist/browser/index.d.ts +131 -0
  10. package/dist/browser/index.js +7 -0
  11. package/dist/build/rollup.cjs +22 -0
  12. package/dist/build/rollup.d.cts +34 -0
  13. package/dist/build/rollup.d.ts +34 -0
  14. package/dist/build/rollup.js +3 -0
  15. package/dist/build/vite.cjs +51 -0
  16. package/dist/build/vite.d.cts +23 -0
  17. package/dist/build/vite.d.ts +23 -0
  18. package/dist/build/vite.js +46 -0
  19. package/dist/build/webpack.cjs +18 -0
  20. package/dist/build/webpack.d.cts +39 -0
  21. package/dist/build/webpack.d.ts +39 -0
  22. package/dist/build/webpack.js +3 -0
  23. package/dist/chunk-7HYGODU6.cjs +583 -0
  24. package/dist/chunk-A5M4NOVT.js +413 -0
  25. package/dist/chunk-A7MYYWMP.cjs +15 -0
  26. package/dist/chunk-BAQA47FM.js +1113 -0
  27. package/dist/chunk-CM7HM6CC.js +563 -0
  28. package/dist/chunk-DFYNIWUN.js +608 -0
  29. package/dist/chunk-DMVVCGKO.cjs +96 -0
  30. package/dist/chunk-FARGXYJ4.cjs +56 -0
  31. package/dist/chunk-G3MYIAX2.js +1226 -0
  32. package/dist/chunk-G4NAVZQ4.js +249 -0
  33. package/dist/chunk-GH7RYPKS.js +96 -0
  34. package/dist/chunk-HADYYQNL.cjs +134 -0
  35. package/dist/chunk-HOQPAB4X.js +178 -0
  36. package/dist/chunk-J5RU73SW.js +214 -0
  37. package/dist/chunk-JNAJ3YZB.cjs +223 -0
  38. package/dist/chunk-KN7H22EQ.cjs +659 -0
  39. package/dist/chunk-LDQ4I7FZ.cjs +1151 -0
  40. package/dist/chunk-NPGESRVQ.js +1212 -0
  41. package/dist/chunk-OPWM5V7A.cjs +258 -0
  42. package/dist/chunk-PYYWPIFL.cjs +1230 -0
  43. package/dist/chunk-R7JLZ4WO.cjs +115 -0
  44. package/dist/chunk-SC6VQSES.js +213 -0
  45. package/dist/chunk-TPSJGLR3.cjs +220 -0
  46. package/dist/chunk-UKBY6HPC.js +131 -0
  47. package/dist/chunk-VRSEHRBX.cjs +185 -0
  48. package/dist/chunk-X74AONZA.js +90 -0
  49. package/dist/chunk-XW3I5CJN.js +12 -0
  50. package/dist/chunk-XW56FKKJ.js +53 -0
  51. package/dist/chunk-Z3G5SW7R.cjs +1251 -0
  52. package/dist/chunk-ZVMAN7OO.cjs +431 -0
  53. package/dist/cli/index.cjs +1487 -0
  54. package/dist/common-ARWXbeS3.d.ts +36 -0
  55. package/dist/common-CwIFh2hK.d.cts +36 -0
  56. package/dist/dedupe-eHBIfUUD.d.cts +13 -0
  57. package/dist/dedupe-fw8aNtt4.d.ts +13 -0
  58. package/dist/handler-DTZ4Rzcf.d.ts +82 -0
  59. package/dist/handler-_00lctTI.d.cts +82 -0
  60. package/dist/index.cjs +277 -0
  61. package/dist/index.d.cts +187 -0
  62. package/dist/index.d.ts +187 -0
  63. package/dist/index.js +4 -0
  64. package/dist/next/client.cjs +113 -0
  65. package/dist/next/client.d.cts +26 -0
  66. package/dist/next/client.d.ts +26 -0
  67. package/dist/next/client.js +41 -0
  68. package/dist/next/config.cjs +275 -0
  69. package/dist/next/config.d.cts +193 -0
  70. package/dist/next/config.d.ts +193 -0
  71. package/dist/next/config.js +261 -0
  72. package/dist/next/index.cjs +148 -0
  73. package/dist/next/index.d.cts +32 -0
  74. package/dist/next/index.d.ts +32 -0
  75. package/dist/next/index.js +59 -0
  76. package/dist/node/index.cjs +257 -0
  77. package/dist/node/index.d.cts +145 -0
  78. package/dist/node/index.d.ts +145 -0
  79. package/dist/node/index.js +83 -0
  80. package/dist/options-0BhybO54.d.cts +47 -0
  81. package/dist/options-0BhybO54.d.ts +47 -0
  82. package/dist/react/index.cjs +107 -0
  83. package/dist/react/index.d.cts +84 -0
  84. package/dist/react/index.d.ts +84 -0
  85. package/dist/react/index.js +103 -0
  86. package/dist/scope-Bvh45bOf.d.cts +404 -0
  87. package/dist/scope-Bvh45bOf.d.ts +404 -0
  88. package/dist/server-edge-BPOGA3GH.cjs +51 -0
  89. package/dist/server-edge-GYKAJREU.js +48 -0
  90. package/dist/server-node-HYK2OB2J.js +24 -0
  91. package/dist/server-node-J2DBAHDV.cjs +27 -0
  92. package/dist/session-C_WzbEN2.d.cts +4 -0
  93. package/dist/session-C_WzbEN2.d.ts +4 -0
  94. package/dist/session-propagation-B7REA7uy.d.cts +30 -0
  95. package/dist/session-propagation-BnJjKJbR.d.ts +30 -0
  96. package/dist/starko.min.js +6 -0
  97. package/docs/browser.md +381 -0
  98. package/docs/configuration.md +269 -0
  99. package/docs/nextjs.md +312 -0
  100. package/docs/node.md +376 -0
  101. package/docs/other-backends.md +232 -0
  102. package/docs/privacy.md +112 -0
  103. package/docs/react.md +187 -0
  104. package/docs/source-maps.md +332 -0
  105. package/docs/troubleshooting.md +171 -0
  106. package/docs/widget.md +84 -0
  107. package/docs/wire-format.md +367 -0
  108. package/package.json +98 -4
@@ -0,0 +1,413 @@
1
+ import { parseSessionHeader, SESSION_HEADER } from './chunk-XW3I5CJN.js';
2
+ import { compatLine, SDK_VERSION, setScopeManager, getClient, clientMethod, isThenable, captureException, getGlobalScope, warnAlways } from './chunk-DFYNIWUN.js';
3
+
4
+ // src/node/request.ts
5
+ var get = (o, k) => {
6
+ try {
7
+ return o && typeof o === "object" ? o[k] : void 0;
8
+ } catch {
9
+ return void 0;
10
+ }
11
+ };
12
+ var str = (v) => typeof v === "string" && v ? v : void 0;
13
+ function readHeader(headers, name) {
14
+ if (!headers || typeof headers !== "object") return void 0;
15
+ try {
16
+ const h = headers;
17
+ if (typeof h.get === "function") return h.get.call(headers, name) ?? void 0;
18
+ if (name in h) return h[name];
19
+ const lower = name.toLowerCase();
20
+ for (const k of Object.keys(h)) if (k.toLowerCase() === lower) return h[k];
21
+ } catch {
22
+ }
23
+ return void 0;
24
+ }
25
+ function isRequestLike(v) {
26
+ if (!v || typeof v !== "object") return false;
27
+ const headers = get(v, "headers");
28
+ return !!headers && typeof headers === "object" && (typeof get(v, "method") === "string" || typeof get(v, "url") === "string");
29
+ }
30
+ var ROUTE_KEY = /* @__PURE__ */ Symbol.for("starko.sdk.route");
31
+ var joinRoute = (base, path) => (str(base) || "") + (Array.isArray(path) ? path.map(String).join("|") : String(path));
32
+ function trackRoute(req) {
33
+ try {
34
+ if (!req || typeof req !== "object" || Object.prototype.hasOwnProperty.call(req, ROUTE_KEY)) return;
35
+ const r = req;
36
+ let value = r.route;
37
+ r[ROUTE_KEY] = void 0;
38
+ Object.defineProperty(req, "route", {
39
+ configurable: true,
40
+ enumerable: true,
41
+ get: () => value,
42
+ set(v) {
43
+ value = v;
44
+ try {
45
+ const path = get(v, "path");
46
+ if (path !== void 0 && path !== null) r[ROUTE_KEY] = joinRoute(r.baseUrl, path);
47
+ } catch {
48
+ }
49
+ }
50
+ });
51
+ } catch {
52
+ }
53
+ }
54
+ function routeOf(req) {
55
+ const tracked = get(req, ROUTE_KEY);
56
+ if (typeof tracked === "string" && tracked) return tracked;
57
+ const route = get(req, "route");
58
+ const path = get(route, "path");
59
+ if (path === void 0 || path === null) return void 0;
60
+ return joinRoute(get(req, "baseUrl"), path);
61
+ }
62
+ function urlOf(req) {
63
+ const raw = str(get(req, "originalUrl")) || str(get(req, "url")) || str(get(req, "path"));
64
+ if (!raw) return void 0;
65
+ if (/^[a-z][a-z0-9+.-]*:\/\//i.test(raw)) return raw;
66
+ const headers = get(req, "headers");
67
+ const host = str(readHeader(headers, "host")) || str(readHeader(headers, ":authority"));
68
+ if (!host) return raw;
69
+ let protocol = str(get(req, "protocol"));
70
+ if (!protocol) protocol = str(readHeader(headers, ":scheme")) || (get(get(req, "socket"), "encrypted") ? "https" : "http");
71
+ return `${protocol.replace(/:$/, "")}://${host}${raw.startsWith("/") ? raw : `/${raw}`}`;
72
+ }
73
+ function requestDataFrom(req) {
74
+ const out = {};
75
+ try {
76
+ const method = str(get(req, "method"));
77
+ if (method) out.method = method.toUpperCase();
78
+ const url = urlOf(req);
79
+ if (url) out.url = url;
80
+ const route = routeOf(req);
81
+ if (route) out.route = route;
82
+ const sid = parseSessionHeader(readHeader(get(req, "headers"), SESSION_HEADER));
83
+ if (sid) out.sessionId = sid;
84
+ } catch {
85
+ }
86
+ return out;
87
+ }
88
+
89
+ // src/node/scope.ts
90
+ var KEY = /* @__PURE__ */ Symbol.for(`starko.sdk.node.scope.v3/${compatLine(SDK_VERSION)}`);
91
+ var alsClass;
92
+ var resourceFn;
93
+ function state() {
94
+ const g = globalThis;
95
+ let s = g[KEY];
96
+ if (!s) g[KEY] = s = { serving: false, byRequest: /* @__PURE__ */ new WeakMap() };
97
+ return s;
98
+ }
99
+ function isNode() {
100
+ try {
101
+ const v = typeof process !== "undefined" && process.versions;
102
+ return !!(v && v.node && !v.bun && !v.deno);
103
+ } catch {
104
+ return false;
105
+ }
106
+ }
107
+ function storage() {
108
+ const s = state();
109
+ if (s.als) return s.als;
110
+ try {
111
+ const C = alsClass || globalThis.AsyncLocalStorage;
112
+ if (typeof C === "function") {
113
+ const als = new C();
114
+ const hooksBased = "kResourceStore" in als;
115
+ if (typeof als.enterWith !== "function") s.mode = "process";
116
+ else if (hooksBased) s.mode = typeof resourceFn === "function" ? "resources" : "process";
117
+ else s.mode = isNode() ? "frames" : "process";
118
+ s.resource = s.mode === "resources" ? resourceFn : void 0;
119
+ s.als = als;
120
+ }
121
+ } catch {
122
+ }
123
+ return s.als;
124
+ }
125
+ var syncStore;
126
+ var deref = (r) => r && typeof r.deref === "function" ? r.deref() : r;
127
+ function currentResource(s) {
128
+ try {
129
+ return s.resource ? s.resource() : void 0;
130
+ } catch {
131
+ return void 0;
132
+ }
133
+ }
134
+ function stale(s, c) {
135
+ return s.mode === "resources" && !c.live && c.res !== void 0 && deref(c.res) === currentResource(s);
136
+ }
137
+ function owned(s, c) {
138
+ return c.live && (s.mode !== "resources" || deref(c.res) === currentResource(s));
139
+ }
140
+ var currentStore = () => {
141
+ try {
142
+ const st = storage();
143
+ if (!st) return syncStore;
144
+ const store = st.getStore();
145
+ return store && store.confined && stale(state(), store.confined) ? void 0 : store;
146
+ } catch {
147
+ return void 0;
148
+ }
149
+ };
150
+ function beginServing() {
151
+ const s = state();
152
+ if (s.serving && s.base) return s.base;
153
+ const global = getGlobalScope();
154
+ const base = global.clone();
155
+ base.clearBreadcrumbs();
156
+ const init = s.initUser;
157
+ base.setUser(init && global.user === init ? init : null);
158
+ s.base = base;
159
+ s.serving = true;
160
+ return base;
161
+ }
162
+ var baseScope = () => beginServing();
163
+ function confine(from) {
164
+ try {
165
+ const s = state();
166
+ const st = storage();
167
+ if (!st || !st.enterWith || s.mode !== "frames" && s.mode !== "resources") return void 0;
168
+ const c = { live: true };
169
+ if (s.mode === "resources") {
170
+ const r = currentResource(s);
171
+ if (!r) return void 0;
172
+ c.res = typeof WeakRef === "function" ? new WeakRef(r) : r;
173
+ }
174
+ const scope = (from || baseScope()).clone();
175
+ st.enterWith({ scope, confined: c });
176
+ try {
177
+ queueMicrotask(() => {
178
+ c.live = false;
179
+ });
180
+ } catch {
181
+ c.live = false;
182
+ }
183
+ return scope;
184
+ } catch {
185
+ return void 0;
186
+ }
187
+ }
188
+ function unscopedRead(s) {
189
+ return s.mode === "process" || !s.als || !s.base ? getGlobalScope() : s.base;
190
+ }
191
+ function writableScope() {
192
+ const s = state();
193
+ const store = currentStore();
194
+ if (!s.serving) return store ? store.scope : getGlobalScope();
195
+ if (store && !store.confined) return store.scope;
196
+ if (store && store.confined && owned(s, store.confined)) return store.scope;
197
+ return confine(store ? store.scope : void 0) || (store ? store.scope : getGlobalScope());
198
+ }
199
+ var sameUser = (a, b) => {
200
+ if (!a || !b) return a === b;
201
+ try {
202
+ return JSON.stringify(a) === JSON.stringify(b);
203
+ } catch {
204
+ return false;
205
+ }
206
+ };
207
+ var manager = {
208
+ getScope: writableScope,
209
+ peekScope() {
210
+ const s = state();
211
+ const store = currentStore();
212
+ if (store) return store.scope;
213
+ return s.serving ? unscopedRead(s) : getGlobalScope();
214
+ },
215
+ withScope(callback) {
216
+ const s = state();
217
+ const st = storage();
218
+ const current = st ? currentStore() : syncStore;
219
+ const from = current ? current.scope : s.serving ? unscopedRead(s) : getGlobalScope();
220
+ const store = { scope: from.clone(), request: current && current.request, source: current && current.source, key: current && current.key };
221
+ return st ? st.run(store, () => callback(store.scope)) : runSync(store, () => callback(store.scope));
222
+ },
223
+ scopeForUser(user) {
224
+ const s = state();
225
+ if (!s.serving) return writableScope();
226
+ const store = currentStore();
227
+ if (store && !store.confined) return store.scope;
228
+ if (s.mode === "process" || !s.als) {
229
+ if (!user) return getGlobalScope();
230
+ if (!s.warned) {
231
+ s.warned = true;
232
+ warnAlways(
233
+ "setUser()/identify() was called outside any request scope on a server that handles requests, in a runtime whose AsyncLocalStorage cannot confine it to the calling code: the user was NOT applied (it would reach other requests' errors). Call it inside the request (Starko.withScope, Starko.runWithRequestScope, Starko.wrapHandler), or pass `user` to init() for a process-wide user. This warning is printed once."
234
+ );
235
+ }
236
+ return void 0;
237
+ }
238
+ if (user && store && store.confined && store.scope.user && !sameUser(store.scope.user, user)) {
239
+ return confine() || writableScope();
240
+ }
241
+ return writableScope();
242
+ }
243
+ };
244
+ function installAsyncScopeManager(cls, executionAsyncResource) {
245
+ if (typeof cls === "function") alsClass = cls;
246
+ if (typeof executionAsyncResource === "function") resourceFn = executionAsyncResource;
247
+ const s = state();
248
+ if (s.als && s.mode === "process" && typeof cls === "function" && typeof executionAsyncResource === "function") {
249
+ s.als = void 0;
250
+ s.mode = void 0;
251
+ }
252
+ setScopeManager(manager);
253
+ }
254
+ function markServing() {
255
+ try {
256
+ beginServing();
257
+ } catch {
258
+ }
259
+ }
260
+ function noteInit(user) {
261
+ try {
262
+ const s = state();
263
+ s.initUser = user;
264
+ if (s.serving && s.base) s.base.setUser(user);
265
+ } catch {
266
+ }
267
+ }
268
+ function getCurrentStore() {
269
+ return currentStore();
270
+ }
271
+ function readScope() {
272
+ return manager.peekScope();
273
+ }
274
+ function storeOf(req) {
275
+ try {
276
+ return req && typeof req === "object" ? state().byRequest.get(req) : void 0;
277
+ } catch {
278
+ return void 0;
279
+ }
280
+ }
281
+ function linkRequest(req, store) {
282
+ try {
283
+ state().byRequest.set(req, store);
284
+ } catch {
285
+ }
286
+ }
287
+ function runInStore(store, fn) {
288
+ const st = storage();
289
+ return st ? st.run(store, fn) : runSync(store, fn);
290
+ }
291
+ function runSync(store, fn) {
292
+ const prev = syncStore;
293
+ syncStore = store;
294
+ try {
295
+ return fn();
296
+ } finally {
297
+ syncStore = prev;
298
+ }
299
+ }
300
+ var asGetter = (request) => typeof request === "function" ? request : request ? () => request : void 0;
301
+ function openRequestScope(fn, request, source, from, key) {
302
+ let st;
303
+ let store;
304
+ try {
305
+ installAsyncScopeManager();
306
+ st = storage();
307
+ const base = beginServing();
308
+ store = { scope: (from || base).clone(), request: asGetter(request), source, key: key || source || {} };
309
+ if (source) state().byRequest.set(source, store);
310
+ } catch {
311
+ return fn(readScope());
312
+ }
313
+ if (!st) return runSync(store, () => fn(store.scope));
314
+ return st.run(store, () => fn(store.scope));
315
+ }
316
+ function runWithRequestScope(fn, request) {
317
+ return openRequestScope(fn, request);
318
+ }
319
+ function getRequestData() {
320
+ try {
321
+ const r = currentStore()?.request;
322
+ return r ? r() : void 0;
323
+ } catch {
324
+ return void 0;
325
+ }
326
+ }
327
+ function currentRequestKey() {
328
+ try {
329
+ return currentStore()?.key;
330
+ } catch {
331
+ return void 0;
332
+ }
333
+ }
334
+
335
+ // src/node/handler.ts
336
+ function platformWaitUntil() {
337
+ try {
338
+ const g = globalThis;
339
+ for (const key of ["@next/request-context", "@vercel/request-context"]) {
340
+ const holder = g[Symbol.for(key)];
341
+ const ctx = holder && typeof holder.get === "function" ? holder.get() : void 0;
342
+ if (ctx && typeof ctx.waitUntil === "function") return ctx.waitUntil.bind(ctx);
343
+ }
344
+ const cf = g[/* @__PURE__ */ Symbol.for("__cloudflare-context__")];
345
+ const cfCtx = cf && cf.ctx;
346
+ if (cfCtx && typeof cfCtx.waitUntil === "function") return cfCtx.waitUntil.bind(cfCtx);
347
+ } catch {
348
+ }
349
+ return void 0;
350
+ }
351
+ function flushIfPending(timeoutMs, skipBusy = false) {
352
+ try {
353
+ const c = getClient();
354
+ const flush = clientMethod(c, "flush");
355
+ if (!flush) return Promise.resolve(true);
356
+ const pending = clientMethod(c, "hasPending");
357
+ if (pending && !pending()) return Promise.resolve(true);
358
+ const busy = clientMethod(c, "isSendBusy");
359
+ if (skipBusy && busy && busy()) return Promise.resolve(false);
360
+ return Promise.resolve(flush(timeoutMs)).catch(() => false);
361
+ } catch {
362
+ return Promise.resolve(false);
363
+ }
364
+ }
365
+ function wrapHandler(fn, options = {}) {
366
+ const timeout = typeof options.flushTimeout === "number" && options.flushTimeout >= 0 ? options.flushTimeout : 2e3;
367
+ const capture = (err) => {
368
+ try {
369
+ captureException(err, { mechanism: { type: "instrument", handled: false } });
370
+ } catch {
371
+ }
372
+ };
373
+ return function starkoWrappedHandler(...args) {
374
+ let data;
375
+ try {
376
+ data = options.getRequestData ? options.getRequestData(...args) : isRequestLike(args[0]) ? requestDataFrom(args[0]) : void 0;
377
+ } catch {
378
+ data = void 0;
379
+ }
380
+ return runWithRequestScope(() => {
381
+ let out;
382
+ try {
383
+ out = fn.apply(this, args);
384
+ } catch (err) {
385
+ capture(err);
386
+ void flushIfPending(timeout);
387
+ throw err;
388
+ }
389
+ if (!isThenable(out)) {
390
+ void flushIfPending(timeout);
391
+ return out;
392
+ }
393
+ const settle = async () => {
394
+ const waitUntil = platformWaitUntil();
395
+ if (waitUntil) waitUntil(flushIfPending(timeout));
396
+ else await flushIfPending(timeout);
397
+ };
398
+ return Promise.resolve(out).then(
399
+ async (value) => {
400
+ await settle();
401
+ return value;
402
+ },
403
+ async (err) => {
404
+ capture(err);
405
+ await settle();
406
+ throw err;
407
+ }
408
+ );
409
+ }, data);
410
+ };
411
+ }
412
+
413
+ export { currentRequestKey, flushIfPending, getCurrentStore, getRequestData, installAsyncScopeManager, linkRequest, markServing, noteInit, openRequestScope, platformWaitUntil, readScope, requestDataFrom, runInStore, runWithRequestScope, storeOf, trackRoute, wrapHandler };
@@ -0,0 +1,15 @@
1
+ 'use strict';
2
+
3
+ var chunkR7JLZ4WO_cjs = require('./chunk-R7JLZ4WO.cjs');
4
+
5
+ // src/core/session.ts
6
+ var SESSION_HEADER = "x-starko-session-id";
7
+ function parseSessionHeader(value) {
8
+ const v = Array.isArray(value) ? value[0] : value;
9
+ if (typeof v !== "string") return void 0;
10
+ const id = v.trim();
11
+ return chunkR7JLZ4WO_cjs.SESSION_ID_PATTERN.test(id) ? id : void 0;
12
+ }
13
+
14
+ exports.SESSION_HEADER = SESSION_HEADER;
15
+ exports.parseSessionHeader = parseSessionHeader;