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SACF,CAAC;QACF,kBAAkB,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;IACrC,CAAC,CAAC,CAAC;IAEH,OAAO,kBAAkB,CAAC;AAC5B,CAAC,CAAC;AAEF,kBAAe,qBAAqB,CAAC","sourcesContent":["import type { JsonRpcNotification } from '@metamask/utils';\n\nimport consoleEndowment from './console';\nimport crypto from './crypto';\nimport date from './date';\nimport interval from './interval';\nimport math from './math';\nimport network from './network';\nimport textDecoder from './textDecoder';\nimport textEncoder from './textEncoder';\nimport timeout from './timeout';\nimport { rootRealmGlobal } from '../globalObject';\n\n/**\n * A function for sending JSON-RPC notifications from an endowment.\n * Used by endowments that perform outbound operations (e.g., network `fetch`)\n * to signal request lifecycle events.\n */\nexport type NotifyFunction = (\n  notification: Omit<JsonRpcNotification, 'jsonrpc'>,\n) => Promise<void>;\n\n/**\n * Options passed to endowment factory functions.\n */\nexport type EndowmentFactoryOptions = {\n  /**\n   * A label identifying the source of endowment interactions, used as a\n   * prefix in console output. For example, passing `\"MyApp\"` causes console\n   * messages to be prefixed with `[MyApp]`.\n   */\n  sourceLabel?: string;\n\n  /**\n   * A notification callback used by endowments that perform outbound\n   * operations (e.g., network `fetch`).\n   */\n  notify?: NotifyFunction;\n};\n\n/**\n * The object returned by an endowment factory. Contains the endowment values\n * keyed by their global name (e.g., `setTimeout`, `Date`) and an optional\n * teardown function for lifecycle management.\n */\nexport type EndowmentFactoryResult = {\n  /**\n   * An optional function that performs cleanup when active resources (e.g.,\n   * pending timers or open network connections) should be released. Must not\n   * render endowments unusable — only restore them to their initial state,\n   * since they may be reused without reconstruction.\n   */\n  teardownFunction?: () => Promise<void> | void;\n  [key: string]: unknown;\n};\n\n/**\n * Describes an endowment factory module. Each module exposes the names of\n * the endowments it provides and a factory function that produces them.\n */\nexport type EndowmentFactory = {\n  names: readonly string[];\n  factory: (options?: EndowmentFactoryOptions) => EndowmentFactoryResult;\n};\n\n/**\n * Describes a simple global value that should be hardened and exposed as an\n * endowment without additional attenuation.\n */\nexport type CommonEndowmentSpecification = {\n  endowment: unknown;\n  name: string;\n  bind?: boolean;\n};\n\n// Array of common endowments\nconst commonEndowments: CommonEndowmentSpecification[] = [\n  { endowment: AbortController, name: 'AbortController' },\n  { endowment: AbortSignal, name: 'AbortSignal' },\n  { endowment: ArrayBuffer, name: 'ArrayBuffer' },\n  { endowment: atob, name: 'atob', bind: true },\n  { endowment: BigInt, name: 'BigInt' },\n  { endowment: BigInt64Array, name: 'BigInt64Array' },\n  { endowment: BigUint64Array, name: 'BigUint64Array' },\n  { endowment: btoa, name: 'btoa', bind: true },\n  { endowment: DataView, name: 'DataView' },\n  { endowment: Float32Array, name: 'Float32Array' },\n  { endowment: Float64Array, name: 'Float64Array' },\n  { endowment: Intl, name: 'Intl' },\n  { endowment: Int8Array, name: 'Int8Array' },\n  { endowment: Int16Array, name: 'Int16Array' },\n  { endowment: Int32Array, name: 'Int32Array' },\n  { endowment: globalThis.isSecureContext, name: 'isSecureContext' },\n  { endowment: Uint8Array, name: 'Uint8Array' },\n  { endowment: Uint8ClampedArray, name: 'Uint8ClampedArray' },\n  { endowment: Uint16Array, name: 'Uint16Array' },\n  { endowment: Uint32Array, name: 'Uint32Array' },\n  { endowment: URL, name: 'URL' },\n  { endowment: URLSearchParams, name: 'URLSearchParams' },\n  { endowment: WebAssembly, name: 'WebAssembly' },\n];\n\n/**\n * Creates a consolidated collection of common endowments.\n * This function will return factories for all common endowments including\n * the additionally attenuated. All hardened with SES.\n *\n * @returns An object with common endowments.\n */\nconst buildCommonEndowments = (): EndowmentFactory[] => {\n  const endowmentFactories: EndowmentFactory[] = [\n    crypto,\n    interval,\n    math,\n    network,\n    timeout,\n    textDecoder,\n    textEncoder,\n    date,\n    consoleEndowment,\n  ];\n\n  commonEndowments.forEach((endowmentSpecification) => {\n    const endowment = {\n      names: [endowmentSpecification.name] as const,\n      factory: () => {\n        const boundEndowment =\n          typeof endowmentSpecification.endowment === 'function' &&\n          endowmentSpecification.bind\n            ? endowmentSpecification.endowment.bind(rootRealmGlobal)\n            : endowmentSpecification.endowment;\n        return {\n          [endowmentSpecification.name]: harden(boundEndowment),\n        } as const;\n      },\n    };\n    endowmentFactories.push(endowment);\n  });\n\n  return endowmentFactories;\n};\n\nexport default buildCommonEndowments;\n"]}