package.build.npm.cjs.transports.offline.js.map Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of browser Show documentation
Show all versions of browser Show documentation
Official Sentry SDK for browsers
{"version":3,"file":"offline.js","sources":["../../../../src/transports/offline.ts"],"sourcesContent":["import type { OfflineStore, OfflineTransportOptions } from '@sentry/core';\nimport { makeOfflineTransport } from '@sentry/core';\nimport type { BaseTransportOptions, Envelope, Transport } from '@sentry/types';\nimport { parseEnvelope, serializeEnvelope } from '@sentry/utils';\nimport { makeFetchTransport } from './fetch';\n\n// 'Store', 'promisifyRequest' and 'createStore' were originally copied from the 'idb-keyval' package before being\n// modified and simplified: https://github.com/jakearchibald/idb-keyval\n//\n// At commit: 0420a704fd6cbb4225429c536b1f61112d012fca\n// Original licence:\n\n// Copyright 2016, Jake Archibald\n//\n// Licensed under the Apache License, Version 2.0 (the \"License\");\n// you may not use this file except in compliance with the License.\n// You may obtain a copy of the License at\n//\n// http://www.apache.org/licenses/LICENSE-2.0\n//\n// Unless required by applicable law or agreed to in writing, software\n// distributed under the License is distributed on an \"AS IS\" BASIS,\n// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n// See the License for the specific language governing permissions and\n// limitations under the License.\n\ntype Store = (callback: (store: IDBObjectStore) => T | PromiseLike) => Promise;\n\nfunction promisifyRequest(request: IDBRequest | IDBTransaction): Promise {\n return new Promise((resolve, reject) => {\n // @ts-expect-error - file size hacks\n request.oncomplete = request.onsuccess = () => resolve(request.result);\n // @ts-expect-error - file size hacks\n request.onabort = request.onerror = () => reject(request.error);\n });\n}\n\n/** Create or open an IndexedDb store */\nexport function createStore(dbName: string, storeName: string): Store {\n const request = indexedDB.open(dbName);\n request.onupgradeneeded = () => request.result.createObjectStore(storeName);\n const dbp = promisifyRequest(request);\n\n return callback => dbp.then(db => callback(db.transaction(storeName, 'readwrite').objectStore(storeName)));\n}\n\nfunction keys(store: IDBObjectStore): Promise {\n return promisifyRequest(store.getAllKeys() as IDBRequest);\n}\n\n/** Insert into the end of the store */\nexport function push(store: Store, value: Uint8Array | string, maxQueueSize: number): Promise {\n return store(store => {\n return keys(store).then(keys => {\n if (keys.length >= maxQueueSize) {\n return;\n }\n\n // We insert with an incremented key so that the entries are popped in order\n store.put(value, Math.max(...keys, 0) + 1);\n return promisifyRequest(store.transaction);\n });\n });\n}\n\n/** Insert into the front of the store */\nexport function unshift(store: Store, value: Uint8Array | string, maxQueueSize: number): Promise {\n return store(store => {\n return keys(store).then(keys => {\n if (keys.length >= maxQueueSize) {\n return;\n }\n\n // We insert with an decremented key so that the entries are popped in order\n store.put(value, Math.min(...keys, 0) - 1);\n return promisifyRequest(store.transaction);\n });\n });\n}\n\n/** Pop the oldest value from the store */\nexport function shift(store: Store): Promise {\n return store(store => {\n return keys(store).then(keys => {\n const firstKey = keys[0];\n if (firstKey == null) {\n return undefined;\n }\n\n return promisifyRequest(store.get(firstKey)).then(value => {\n store.delete(firstKey);\n return promisifyRequest(store.transaction).then(() => value);\n });\n });\n });\n}\n\nexport interface BrowserOfflineTransportOptions extends Omit {\n /**\n * Name of indexedDb database to store envelopes in\n * Default: 'sentry-offline'\n */\n dbName?: string;\n /**\n * Name of indexedDb object store to store envelopes in\n * Default: 'queue'\n */\n storeName?: string;\n /**\n * Maximum number of envelopes to store\n * Default: 30\n */\n maxQueueSize?: number;\n}\n\nfunction createIndexedDbStore(options: BrowserOfflineTransportOptions): OfflineStore {\n let store: Store | undefined;\n\n // Lazily create the store only when it's needed\n function getStore(): Store {\n if (store == undefined) {\n store = createStore(options.dbName || 'sentry-offline', options.storeName || 'queue');\n }\n\n return store;\n }\n\n return {\n push: async (env: Envelope) => {\n try {\n const serialized = await serializeEnvelope(env);\n await push(getStore(), serialized, options.maxQueueSize || 30);\n } catch (_) {\n //\n }\n },\n unshift: async (env: Envelope) => {\n try {\n const serialized = await serializeEnvelope(env);\n await unshift(getStore(), serialized, options.maxQueueSize || 30);\n } catch (_) {\n //\n }\n },\n shift: async () => {\n try {\n const deserialized = await shift(getStore());\n if (deserialized) {\n return parseEnvelope(deserialized);\n }\n } catch (_) {\n //\n }\n\n return undefined;\n },\n };\n}\n\nfunction makeIndexedDbOfflineTransport(\n createTransport: (options: T) => Transport,\n): (options: T & BrowserOfflineTransportOptions) => Transport {\n return options => createTransport({ ...options, createStore: createIndexedDbStore });\n}\n\n/**\n * Creates a transport that uses IndexedDb to store events when offline.\n */\nexport function makeBrowserOfflineTransport(\n createTransport: (options: T) => Transport = makeFetchTransport,\n): (options: T & BrowserOfflineTransportOptions) => Transport {\n return makeIndexedDbOfflineTransport(makeOfflineTransport(createTransport));\n}\n"],"names":["serializeEnvelope","parseEnvelope","makeFetchTransport","makeOfflineTransport"],"mappings":";;;;;;AAMA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAIA,SAAS,gBAAgB,CAAgB,OAAO,EAA8C;AAC9F,EAAE,OAAO,IAAI,OAAO,CAAI,CAAC,OAAO,EAAE,MAAM,KAAK;AAC7C;AACA,IAAI,OAAO,CAAC,UAAA,GAAa,OAAO,CAAC,SAAU,GAAE,MAAM,OAAO,CAAC,OAAO,CAAC,MAAM,CAAC,CAAA;AAC1E;AACA,IAAI,OAAO,CAAC,OAAA,GAAU,OAAO,CAAC,OAAQ,GAAE,MAAM,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,CAAA;AACnE,GAAG,CAAC,CAAA;AACJ,CAAA;AACA;AACA;AACO,SAAS,WAAW,CAAC,MAAM,EAAU,SAAS,EAAiB;AACtE,EAAE,MAAM,UAAU,SAAS,CAAC,IAAI,CAAC,MAAM,CAAC,CAAA;AACxC,EAAE,OAAO,CAAC,eAAgB,GAAE,MAAM,OAAO,CAAC,MAAM,CAAC,iBAAiB,CAAC,SAAS,CAAC,CAAA;AAC7E,EAAE,MAAM,GAAI,GAAE,gBAAgB,CAAC,OAAO,CAAC,CAAA;AACvC;AACA,EAAE,OAAO,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