offline.js.map 8.5 KB

1
  1. {"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 { Envelope, InternalBaseTransportOptions, Transport } from '@sentry/types';\nimport type { TextDecoderInternal } from '@sentry/utils';\nimport { parseEnvelope, serializeEnvelope } from '@sentry/utils';\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 = <T>(callback: (store: IDBObjectStore) => T | PromiseLike<T>) => Promise<T>;\n\nfunction promisifyRequest<T = undefined>(request: IDBRequest<T> | IDBTransaction): Promise<T> {\n return new Promise<T>((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<number[]> {\n return promisifyRequest(store.getAllKeys() as IDBRequest<number[]>);\n}\n\n/** Insert into the store */\nexport function insert(store: Store, value: Uint8Array | string, maxQueueSize: number): Promise<void> {\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/** Pop the oldest value from the store */\nexport function pop(store: Store): Promise<Uint8Array | string | undefined> {\n return store(store => {\n return keys(store).then(keys => {\n if (keys.length === 0) {\n return undefined;\n }\n\n return promisifyRequest(store.get(keys[0])).then(value => {\n store.delete(keys[0]);\n return promisifyRequest(store.transaction).then(() => value);\n });\n });\n });\n}\n\nexport interface BrowserOfflineTransportOptions extends OfflineTransportOptions {\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 * Only required for testing on node.js\n * @ignore\n */\n textDecoder?: TextDecoderInternal;\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 insert: async (env: Envelope) => {\n try {\n const serialized = await serializeEnvelope(env, options.textEncoder);\n await insert(getStore(), serialized, options.maxQueueSize || 30);\n } catch (_) {\n //\n }\n },\n pop: async () => {\n try {\n const deserialized = await pop(getStore());\n if (deserialized) {\n return parseEnvelope(\n deserialized,\n options.textEncoder || new TextEncoder(),\n options.textDecoder || new TextDecoder(),\n );\n }\n } catch (_) {\n //\n }\n\n return undefined;\n },\n };\n}\n\nfunction makeIndexedDbOfflineTransport<T>(\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<T extends InternalBaseTransportOptions>(\n createTransport: (options: T) => Transport,\n): (options: T & BrowserOfflineTransportOptions) => Transport {\n return makeIndexedDbOfflineTransport<T>(makeOfflineTransport(createTransport));\n}\n"],"names":[],"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,QAAS,IAAG,GAAG,CAAC,IAAI,CAAC,EAAA,IAAM,QAAQ,CAAC,EAAE,CAAC,WAAW,CAAC,SAAS,EAAE,WAAW,CAAC,CAAC,WAAW,CAAC,SAAS,CAAC,CAAC,CAAC,CAAA;AAC5G,CAAA;AACA;AACA,SAAS,IAAI,CAAC,KAAK,EAAqC;AACxD,EAAE,OAAO,gBAAgB,CAAC,KAAK,CAAC,U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