{"version":3,"file":"console.cjs","sourceRoot":"","sources":["../../../src/common/endowments/console.ts"],"names":[],"mappings":";;;AAAA,2CAAyC;AAGzC,sDAAkD;AAErC,QAAA,wBAAwB,GAAG,IAAI,GAAG,CAAC;IAC9C,KAAK;IACL,QAAQ;IACR,OAAO;IACP,OAAO;IACP,MAAM;IACN,MAAM;CACP,CAAC,CAAC;AAEH;;;;GAIG;AACU,QAAA,cAAc,GAAG,IAAI,GAAG,CAAC;IACpC,OAAO;IACP,OAAO;IACP,MAAM;IACN,KAAK;IACL,MAAM;IACN,KAAK;IACL,QAAQ;IACR,OAAO;IACP,OAAO;IACP,OAAO;IACP,gBAAgB;IAChB,UAAU;IACV,OAAO;IACP,OAAO;IACP,YAAY;IACZ,QAAQ;IACR,SAAS;IACT,YAAY;IACZ,MAAM;IACN,SAAS;IACT,SAAS;IACT,WAAW;IACX,SAAS;CACV,CAAC,CAAC;AAEH,MAAM,gBAAgB,GAAG,CAAC,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,CAAU,CAAC;AAM5E;;;;;;;;GAQG;AACH,SAAS,UAAU,CAAC,WAAmB,EAAE,OAAgB,EAAE,GAAG,IAAe;IAC3E,MAAM,MAAM,GAAG,IAAI,WAAW,GAAG,CAAC;IAElC,qFAAqF;IACrF,+BAA+B;IAC/B,IAAI,OAAO,OAAO,KAAK,QAAQ,EAAE,CAAC;QAChC,OAAO,CAAC,GAAG,MAAM,IAAI,OAAO,EAAE,EAAE,GAAG,IAAI,CAAC,CAAC;IAC3C,CAAC;IAED,sFAAsF;IACtF,4BAA4B;IAC5B,OAAO,CAAC,MAAM,EAAE,OAAO,EAAE,GAAG,IAAI,CAAC,CAAC;AACpC,CAAC;AAED;;;;;;;GAOG;AACH,SAAS,aAAa,CAAC,EAAE,WAAW,KAA8B,EAAE;IAClE,IAAA,cAAM,EACJ,WAAW,KAAK,SAAS,EACzB,wEAAwE,CACzE,CAAC;IACF,MAAM,IAAI,GAAG,MA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esContent":["import { assert } from '@metamask/utils';\n\nimport type { EndowmentFactoryOptions } from './commonEndowmentFactory';\nimport { rootRealmGlobal } from '../globalObject';\n\nexport const consoleAttenuatedMethods = new Set([\n  'log',\n  'assert',\n  'error',\n  'debug',\n  'info',\n  'warn',\n]);\n\n/**\n * A set of all the `console` method names that will be included in the\n * attenuated console object. Covers values available in both browser and\n * Node.js.\n */\nexport const consoleMethods = new Set([\n  'debug',\n  'error',\n  'info',\n  'log',\n  'warn',\n  'dir',\n  'dirxml',\n  'table',\n  'trace',\n  'group',\n  'groupCollapsed',\n  'groupEnd',\n  'clear',\n  'count',\n  'countReset',\n  'assert',\n  'profile',\n  'profileEnd',\n  'time',\n  'timeLog',\n  'timeEnd',\n  'timeStamp',\n  'context',\n]);\n\nconst consoleFunctions = ['log', 'error', 'debug', 'info', 'warn'] as const;\n\ntype ConsoleFunctions = {\n  [Key in (typeof consoleFunctions)[number]]: (typeof rootRealmGlobal.console)[Key];\n};\n\n/**\n * Gets the appropriate (prepended) message to pass to one of the attenuated\n * method calls.\n *\n * @param sourceLabel - Label identifying the source of the console call.\n * @param message - The first argument passed to the console method.\n * @param args - The array of additional arguments.\n * @returns An array of arguments to be passed into an attenuated console method call.\n */\nfunction getMessage(sourceLabel: string, message: unknown, ...args: unknown[]) {\n  const prefix = `[${sourceLabel}]`;\n\n  // If the first argument is a string, prepend the prefix to the message, and keep the\n  // rest of the arguments as-is.\n  if (typeof message === 'string') {\n    return [`${prefix} ${message}`, ...args];\n  }\n\n  // Otherwise, the `message` is an object, array, etc., so add the prefix as a separate\n  // message to the arguments.\n  return [prefix, message, ...args];\n}\n\n/**\n * Create a {@link console} object, with the same properties as the global\n * {@link console} object, but with some methods replaced.\n *\n * @param options - Factory options used in construction of the endowment.\n * @param options.sourceLabel - Label identifying the source of the console call.\n * @returns The {@link console} object with the replaced methods.\n */\nfunction createConsole({ sourceLabel }: EndowmentFactoryOptions = {}) {\n  assert(\n    sourceLabel !== undefined,\n    'The \"sourceLabel\" option is required by the console endowment factory.',\n  );\n  const keys = Object.getOwnPropertyNames(\n    rootRealmGlobal.console,\n  ) as (keyof typeof console)[];\n\n  const attenuatedConsole = keys.reduce((target, key) => {\n    if (consoleMethods.has(key) && !consoleAttenuatedMethods.has(key)) {\n      return { ...target, [key]: rootRealmGlobal.console[key] };\n    }\n\n    return target;\n  }, {});\n\n  return harden({\n    console: {\n      ...attenuatedConsole,\n      assert: (\n        value: any,\n        message?: string | undefined,\n        ...optionalParams: any[]\n      ) => {\n        rootRealmGlobal.console.assert(\n          value,\n          ...getMessage(sourceLabel, message, ...optionalParams),\n        );\n      },\n      ...consoleFunctions.reduce<ConsoleFunctions>((target, key) => {\n        return {\n          ...target,\n          [key]: (message?: unknown, ...optionalParams: any[]) => {\n            rootRealmGlobal.console[key](\n              ...getMessage(sourceLabel, message, ...optionalParams),\n            );\n          },\n        };\n      }, {} as ConsoleFunctions),\n    },\n  });\n}\n\nconst endowmentModule = {\n  names: ['console'] as const,\n  factory: createConsole,\n};\n\nexport default endowmentModule;\n"]}