sentry-miniapp 1.1.0 → 1.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.
@@ -245,7 +245,7 @@ function ensurePolyfills() {
245
245
  ensurePolyfills();
246
246
  const DEBUG_BUILD = typeof __SENTRY_DEBUG__ === "undefined" || __SENTRY_DEBUG__;
247
247
  const GLOBAL_OBJ = globalThis;
248
- const SDK_VERSION$1 = "10.39.0";
248
+ const SDK_VERSION$1 = "10.45.0";
249
249
  function getMainCarrier() {
250
250
  getSentryCarrier(GLOBAL_OBJ);
251
251
  return GLOBAL_OBJ;
@@ -462,8 +462,7 @@ function _htmlElementAsString(el, keyAttrs) {
462
462
  }
463
463
  }
464
464
  }
465
- const allowedAttrs = ["aria-label", "type", "name", "title", "alt"];
466
- for (const k of allowedAttrs) {
465
+ for (const k of ["aria-label", "type", "name", "title", "alt"]) {
467
466
  const attr = elem.getAttribute(k);
468
467
  if (attr) {
469
468
  out.push(`[${k}="${attr}"]`);
@@ -513,16 +512,9 @@ function serializeEventTarget(target) {
513
512
  }
514
513
  function getOwnProperties(obj) {
515
514
  if (typeof obj === "object" && obj !== null) {
516
- const extractedProps = {};
517
- for (const property in obj) {
518
- if (Object.prototype.hasOwnProperty.call(obj, property)) {
519
- extractedProps[property] = obj[property];
520
- }
521
- }
522
- return extractedProps;
523
- } else {
524
- return {};
515
+ return Object.fromEntries(Object.entries(obj));
525
516
  }
517
+ return {};
526
518
  }
527
519
  let RESOLVED_RUNNER;
528
520
  function withRandomSafeContext(cb) {
@@ -1553,14 +1545,13 @@ function getCapturedScopesOnSpan(span) {
1553
1545
  };
1554
1546
  }
1555
1547
  const SENTRY_BAGGAGE_KEY_PREFIX = "sentry-";
1556
- const SENTRY_BAGGAGE_KEY_PREFIX_REGEX = /^sentry-/;
1557
1548
  function baggageHeaderToDynamicSamplingContext(baggageHeader) {
1558
1549
  const baggageObject = parseBaggageHeader(baggageHeader);
1559
1550
  if (!baggageObject) {
1560
1551
  return void 0;
1561
1552
  }
1562
1553
  const dynamicSamplingContext = Object.entries(baggageObject).reduce((acc, [key, value]) => {
1563
- if (key.match(SENTRY_BAGGAGE_KEY_PREFIX_REGEX)) {
1554
+ if (key.startsWith(SENTRY_BAGGAGE_KEY_PREFIX)) {
1564
1555
  const nonPrefixedKey = key.slice(SENTRY_BAGGAGE_KEY_PREFIX.length);
1565
1556
  acc[nonPrefixedKey] = value;
1566
1557
  }
@@ -2240,28 +2231,24 @@ function createAttachmentEnvelopeItem(attachment) {
2240
2231
  buffer
2241
2232
  ];
2242
2233
  }
2243
- const ITEM_TYPE_TO_DATA_CATEGORY_MAP = {
2244
- session: "session",
2234
+ const DATA_CATEGORY_OVERRIDES = {
2245
2235
  sessions: "session",
2246
- attachment: "attachment",
2247
- transaction: "transaction",
2248
2236
  event: "error",
2249
2237
  client_report: "internal",
2250
2238
  user_report: "default",
2251
- profile: "profile",
2252
2239
  profile_chunk: "profile",
2253
2240
  replay_event: "replay",
2254
2241
  replay_recording: "replay",
2255
2242
  check_in: "monitor",
2256
- feedback: "feedback",
2257
- span: "span",
2258
2243
  raw_security: "security",
2259
2244
  log: "log_item",
2260
- metric: "metric",
2261
2245
  trace_metric: "metric"
2262
2246
  };
2247
+ function _isOverriddenType(type) {
2248
+ return type in DATA_CATEGORY_OVERRIDES;
2249
+ }
2263
2250
  function envelopeItemTypeToDataCategory(type) {
2264
- return ITEM_TYPE_TO_DATA_CATEGORY_MAP[type];
2251
+ return _isOverriddenType(type) ? DATA_CATEGORY_OVERRIDES[type] : type;
2265
2252
  }
2266
2253
  function getSdkMetadataForEnvelopeHeader(metadataOrEvent) {
2267
2254
  if (!(metadataOrEvent == null ? void 0 : metadataOrEvent.sdk)) {
@@ -3553,10 +3540,10 @@ function addEventProcessor(callback) {
3553
3540
  }
3554
3541
  function startSession(context) {
3555
3542
  const isolationScope = getIsolationScope();
3556
- const currentScope = getCurrentScope();
3543
+ const { user } = getCombinedScopeData(isolationScope, getCurrentScope());
3557
3544
  const { userAgent } = GLOBAL_OBJ.navigator || {};
3558
3545
  const session = makeSession(__spreadValues(__spreadValues({
3559
- user: currentScope.getUser() || isolationScope.getUser()
3546
+ user
3560
3547
  }, userAgent && { userAgent }), context));
3561
3548
  const currentSession = isolationScope.getSession();
3562
3549
  if ((currentSession == null ? void 0 : currentSession.status) === "ok") {
@@ -4223,6 +4210,7 @@ class Client {
4223
4210
  // @ts-expect-error - PromiseLike is a subset of Promise
4224
4211
  close(timeout) {
4225
4212
  return __async(this, null, function* () {
4213
+ _INTERNAL_flushLogsBuffer(this);
4226
4214
  const result = yield this.flush(timeout);
4227
4215
  this.getOptions().enabled = false;
4228
4216
  this.emit("close");
@@ -4370,6 +4358,14 @@ class Client {
4370
4358
  return {};
4371
4359
  });
4372
4360
  }
4361
+ /**
4362
+ * Disposes of the client and releases all resources.
4363
+ *
4364
+ * Subclasses should override this method to clean up their own resources.
4365
+ * After calling dispose(), the client should not be used anymore.
4366
+ */
4367
+ dispose() {
4368
+ }
4373
4369
  /* eslint-enable @typescript-eslint/unified-signatures */
4374
4370
  /** Setup integrations for this client. */
4375
4371
  _setupIntegrations() {
@@ -4454,12 +4450,13 @@ class Client {
4454
4450
  isolationScope.setLastEventId(event.event_id || hint.event_id);
4455
4451
  }
4456
4452
  return prepareEvent(options, event, hint, currentScope, this, isolationScope).then((evt) => {
4453
+ var _a;
4457
4454
  if (evt === null) {
4458
4455
  return evt;
4459
4456
  }
4460
4457
  this.emit("postprocessEvent", evt, hint);
4461
4458
  evt.contexts = __spreadValues({
4462
- trace: getTraceContextFromScope(currentScope)
4459
+ trace: __spreadValues(__spreadValues({}, (_a = evt.contexts) == null ? void 0 : _a.trace), getTraceContextFromScope(currentScope))
4463
4460
  }, evt.contexts);
4464
4461
  const dynamicSamplingContext = getDynamicSamplingContextFromScope(this, currentScope);
4465
4462
  evt.sdkProcessingMetadata = __spreadValues({
@@ -4527,11 +4524,12 @@ class Client {
4527
4524
  }
4528
4525
  const dataCategory = getDataCategoryByType(event.type);
4529
4526
  return this._prepareEvent(event, hint, currentScope, isolationScope).then((prepared) => {
4527
+ var _a;
4530
4528
  if (prepared === null) {
4531
4529
  this.recordDroppedEvent("event_processor", dataCategory);
4532
4530
  throw _makeDoNotSendEventError("An event processor returned `null`, will not send event.");
4533
4531
  }
4534
- const isInternalException = hint.data && hint.data.__sentry__ === true;
4532
+ const isInternalException = ((_a = hint.data) == null ? void 0 : _a.__sentry__) === true;
4535
4533
  if (isInternalException) {
4536
4534
  return prepared;
4537
4535
  }
@@ -4816,6 +4814,112 @@ function initAndBind(clientClass, options) {
4816
4814
  function setCurrentClient(client) {
4817
4815
  getCurrentScope().setClient(client);
4818
4816
  }
4817
+ const MIN_DELAY = 100;
4818
+ const START_DELAY = 5e3;
4819
+ const MAX_DELAY = 36e5;
4820
+ function makeOfflineTransport(createTransport2) {
4821
+ function log2(...args) {
4822
+ DEBUG_BUILD && debug.log("[Offline]:", ...args);
4823
+ }
4824
+ return (options) => {
4825
+ const transport = createTransport2(options);
4826
+ if (!options.createStore) {
4827
+ throw new Error("No `createStore` function was provided");
4828
+ }
4829
+ const store = options.createStore(options);
4830
+ let retryDelay = START_DELAY;
4831
+ let flushTimer;
4832
+ function shouldQueue(env, error2, retryDelay2) {
4833
+ if (envelopeContainsItemType(env, ["client_report"])) {
4834
+ return false;
4835
+ }
4836
+ if (options.shouldStore) {
4837
+ return options.shouldStore(env, error2, retryDelay2);
4838
+ }
4839
+ return true;
4840
+ }
4841
+ function flushIn(delay) {
4842
+ if (flushTimer) {
4843
+ clearTimeout(flushTimer);
4844
+ }
4845
+ flushTimer = safeUnref(
4846
+ setTimeout(() => __async(null, null, function* () {
4847
+ flushTimer = void 0;
4848
+ const found = yield store.shift();
4849
+ if (found) {
4850
+ log2("Attempting to send previously queued event");
4851
+ found[0].sent_at = (/* @__PURE__ */ new Date()).toISOString();
4852
+ void send(found, true).catch((e) => {
4853
+ log2("Failed to retry sending", e);
4854
+ });
4855
+ }
4856
+ }), delay)
4857
+ );
4858
+ }
4859
+ function flushWithBackOff() {
4860
+ if (flushTimer) {
4861
+ return;
4862
+ }
4863
+ flushIn(retryDelay);
4864
+ retryDelay = Math.min(retryDelay * 2, MAX_DELAY);
4865
+ }
4866
+ function send(envelope, isRetry = false) {
4867
+ return __async(this, null, function* () {
4868
+ var _a, _b;
4869
+ if (!isRetry && envelopeContainsItemType(envelope, ["replay_event", "replay_recording"])) {
4870
+ yield store.push(envelope);
4871
+ flushIn(MIN_DELAY);
4872
+ return {};
4873
+ }
4874
+ try {
4875
+ if (options.shouldSend && (yield options.shouldSend(envelope)) === false) {
4876
+ throw new Error("Envelope not sent because `shouldSend` callback returned false");
4877
+ }
4878
+ const result = yield transport.send(envelope);
4879
+ let delay = MIN_DELAY;
4880
+ if (result) {
4881
+ if ((_a = result.headers) == null ? void 0 : _a["retry-after"]) {
4882
+ delay = parseRetryAfterHeader(result.headers["retry-after"]);
4883
+ } else if ((_b = result.headers) == null ? void 0 : _b["x-sentry-rate-limits"]) {
4884
+ delay = 6e4;
4885
+ } else if ((result.statusCode || 0) >= 400) {
4886
+ return result;
4887
+ }
4888
+ }
4889
+ flushIn(delay);
4890
+ retryDelay = START_DELAY;
4891
+ return result;
4892
+ } catch (e) {
4893
+ if (yield shouldQueue(envelope, e, retryDelay)) {
4894
+ if (isRetry) {
4895
+ yield store.unshift(envelope);
4896
+ } else {
4897
+ yield store.push(envelope);
4898
+ }
4899
+ flushWithBackOff();
4900
+ log2("Error sending. Event queued.", e);
4901
+ return {};
4902
+ } else {
4903
+ throw e;
4904
+ }
4905
+ }
4906
+ });
4907
+ }
4908
+ if (options.flushAtStartup) {
4909
+ flushWithBackOff();
4910
+ }
4911
+ return {
4912
+ send,
4913
+ flush: (timeout) => {
4914
+ if (timeout === void 0) {
4915
+ retryDelay = START_DELAY;
4916
+ flushIn(MIN_DELAY);
4917
+ }
4918
+ return transport.flush(timeout);
4919
+ }
4920
+ };
4921
+ };
4922
+ }
4819
4923
  const DEFAULT_BREADCRUMBS = 100;
4820
4924
  function addBreadcrumb(breadcrumb, hint) {
4821
4925
  const client = getClient();
@@ -4832,7 +4936,7 @@ function addBreadcrumb(breadcrumb, hint) {
4832
4936
  }
4833
4937
  isolationScope.addBreadcrumb(finalBreadcrumb, maxBreadcrumbs);
4834
4938
  }
4835
- const SDK_VERSION = "1.0.0-beta.1";
4939
+ const SDK_VERSION = "1.2.0";
4836
4940
  const SDK_NAME = "sentry.javascript.miniapp";
4837
4941
  const getSDK = () => {
4838
4942
  let currentSdk = {
@@ -4860,9 +4964,35 @@ const getSDK = () => {
4860
4964
  currentSdk = qq;
4861
4965
  } else if (typeof swan === "object" && swan !== null) {
4862
4966
  currentSdk = swan;
4967
+ } else if (typeof ks === "object" && ks !== null) {
4968
+ currentSdk = ks;
4863
4969
  } else {
4864
4970
  throw new Error("sentry-miniapp 暂不支持此平台");
4865
4971
  }
4972
+ if (typeof my === "object" && my !== null && currentSdk === my && !currentSdk.request && currentSdk.httpRequest) {
4973
+ currentSdk.request = currentSdk.httpRequest;
4974
+ }
4975
+ if (typeof my === "object" && my !== null && currentSdk === my || typeof dd === "object" && dd !== null && currentSdk === dd) {
4976
+ if (currentSdk.getStorageSync) {
4977
+ const originalGet = currentSdk.getStorageSync;
4978
+ currentSdk.getStorageSync = (key) => {
4979
+ const res = originalGet.call(currentSdk, { key });
4980
+ return res ? res.data : null;
4981
+ };
4982
+ }
4983
+ if (currentSdk.setStorageSync) {
4984
+ const originalSet = currentSdk.setStorageSync;
4985
+ currentSdk.setStorageSync = (key, data) => {
4986
+ originalSet.call(currentSdk, { key, data });
4987
+ };
4988
+ }
4989
+ if (currentSdk.removeStorageSync) {
4990
+ const originalRemove = currentSdk.removeStorageSync;
4991
+ currentSdk.removeStorageSync = (key) => {
4992
+ originalRemove.call(currentSdk, { key });
4993
+ };
4994
+ }
4995
+ }
4866
4996
  return currentSdk;
4867
4997
  };
4868
4998
  const getAppName = () => {
@@ -4879,6 +5009,8 @@ const getAppName = () => {
4879
5009
  currentAppName = "qq";
4880
5010
  } else if (typeof swan === "object" && swan !== null) {
4881
5011
  currentAppName = "swan";
5012
+ } else if (typeof ks === "object" && ks !== null) {
5013
+ currentAppName = "kuaishou";
4882
5014
  }
4883
5015
  return currentAppName;
4884
5016
  };
@@ -4918,7 +5050,11 @@ const getSystemInfo = () => {
4918
5050
  return result;
4919
5051
  }
4920
5052
  if (currentSdk.getSystemInfoSync) {
4921
- return currentSdk.getSystemInfoSync();
5053
+ const syncInfo = currentSdk.getSystemInfoSync();
5054
+ if (!syncInfo.SDKVersion && syncInfo.version) {
5055
+ syncInfo.SDKVersion = syncInfo.version;
5056
+ }
5057
+ return syncInfo;
4922
5058
  }
4923
5059
  return null;
4924
5060
  } catch (error2) {
@@ -4971,20 +5107,24 @@ function createMiniappTransport(options) {
4971
5107
  header: __spreadValues({
4972
5108
  "Content-Type": "application/json"
4973
5109
  }, request.headers),
5110
+ // Alipay uses `headers` instead of `header`
5111
+ headers: __spreadValues({
5112
+ "Content-Type": "application/json"
5113
+ }, request.headers),
4974
5114
  timeout: 1e4,
4975
5115
  success: (res) => {
4976
- var _a2, _b2;
4977
- const status = res.statusCode;
5116
+ const status = res.statusCode || res.status;
5117
+ const resHeaders = res.header || res.headers || {};
4978
5118
  resolve({
4979
5119
  statusCode: status,
4980
5120
  headers: {
4981
- "x-sentry-rate-limits": (_a2 = res.header) == null ? void 0 : _a2["x-sentry-rate-limits"],
4982
- "retry-after": (_b2 = res.header) == null ? void 0 : _b2["retry-after"]
5121
+ "x-sentry-rate-limits": resHeaders["x-sentry-rate-limits"],
5122
+ "retry-after": resHeaders["retry-after"]
4983
5123
  }
4984
5124
  });
4985
5125
  },
4986
5126
  fail: (error2) => {
4987
- reject(new Error(`Network request failed: ${error2.errMsg || error2.message || "Unknown error"}`));
5127
+ reject(new Error(`Network request failed: ${error2.errMsg || error2.errorMessage || error2.message || "Unknown error"}`));
4988
5128
  }
4989
5129
  };
4990
5130
  if (sdk().request) {
@@ -4998,8 +5138,75 @@ function createMiniappTransport(options) {
4998
5138
  }
4999
5139
  return createTransport(options, makeRequest);
5000
5140
  }
5141
+ const MAX_OFFLINE_CACHE_SIZE = 30;
5142
+ const OFFLINE_STORE_KEY = "sentry_offline_store";
5143
+ function createMiniappOfflineStore(_options) {
5144
+ return {
5145
+ push: (env) => __async(null, null, function* () {
5146
+ try {
5147
+ const store = getStore();
5148
+ store.push(env);
5149
+ if (store.length > MAX_OFFLINE_CACHE_SIZE) {
5150
+ store.shift();
5151
+ }
5152
+ setStore(store);
5153
+ } catch (e) {
5154
+ console.warn("[Sentry] Failed to push to offline store", e);
5155
+ }
5156
+ }),
5157
+ unshift: (env) => __async(null, null, function* () {
5158
+ try {
5159
+ const store = getStore();
5160
+ store.unshift(env);
5161
+ if (store.length > MAX_OFFLINE_CACHE_SIZE) {
5162
+ store.pop();
5163
+ }
5164
+ setStore(store);
5165
+ } catch (e) {
5166
+ console.warn("[Sentry] Failed to unshift to offline store", e);
5167
+ }
5168
+ }),
5169
+ shift: () => __async(null, null, function* () {
5170
+ try {
5171
+ const store = getStore();
5172
+ if (store.length === 0) {
5173
+ return void 0;
5174
+ }
5175
+ const env = store.shift();
5176
+ setStore(store);
5177
+ return env;
5178
+ } catch (e) {
5179
+ console.warn("[Sentry] Failed to shift from offline store", e);
5180
+ return void 0;
5181
+ }
5182
+ })
5183
+ };
5184
+ }
5185
+ function getStore() {
5186
+ try {
5187
+ const storageApi = sdk().getStorageSync;
5188
+ if (storageApi) {
5189
+ const storedStr = storageApi(OFFLINE_STORE_KEY);
5190
+ if (storedStr) {
5191
+ return typeof storedStr === "string" ? JSON.parse(storedStr) : storedStr;
5192
+ }
5193
+ }
5194
+ } catch (e) {
5195
+ }
5196
+ return [];
5197
+ }
5198
+ function setStore(store) {
5199
+ try {
5200
+ const storageApi = sdk().setStorageSync;
5201
+ if (storageApi) {
5202
+ storageApi(OFFLINE_STORE_KEY, JSON.stringify(store));
5203
+ }
5204
+ } catch (e) {
5205
+ }
5206
+ }
5001
5207
  const index$1 = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
5002
5208
  __proto__: null,
5209
+ createMiniappOfflineStore,
5003
5210
  createMiniappTransport
5004
5211
  }, Symbol.toStringTag, { value: "Module" }));
5005
5212
  class MiniappClient extends Client {
@@ -5011,9 +5218,17 @@ class MiniappClient extends Client {
5011
5218
  constructor(options = {}) {
5012
5219
  super(__spreadProps(__spreadValues({}, options), {
5013
5220
  transport: options.transport || ((transportOptions) => {
5014
- return createMiniappTransport(__spreadProps(__spreadValues({}, transportOptions), {
5221
+ const baseTransport = createMiniappTransport(__spreadProps(__spreadValues({}, transportOptions), {
5015
5222
  headers: {}
5016
5223
  }));
5224
+ if (options.enableOfflineCache !== false) {
5225
+ return makeOfflineTransport(() => baseTransport)(__spreadProps(__spreadValues({}, transportOptions), {
5226
+ createStore: createMiniappOfflineStore,
5227
+ flushAtStartup: true
5228
+ // 启动时自动重试发送
5229
+ }));
5230
+ }
5231
+ return baseTransport;
5017
5232
  })
5018
5233
  }));
5019
5234
  }
@@ -5299,7 +5514,6 @@ let GlobalHandlers = _GlobalHandlers;
5299
5514
  const _TryCatch = class _TryCatch {
5300
5515
  constructor() {
5301
5516
  this.name = _TryCatch.id;
5302
- this._ignoreOnError = 0;
5303
5517
  }
5304
5518
  /** JSDoc */
5305
5519
  _wrapTimeFunction(original) {
@@ -5337,7 +5551,6 @@ const _TryCatch = class _TryCatch {
5337
5551
  * and provide better metadata.
5338
5552
  */
5339
5553
  setupOnce() {
5340
- this._ignoreOnError = this._ignoreOnError;
5341
5554
  const global2 = globalThis;
5342
5555
  if (global2.setTimeout) {
5343
5556
  fill(global2, "setTimeout", this._wrapTimeFunction.bind(this));