script_impl.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. "use strict";
  2. /**
  3. * @license
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. Object.defineProperty(exports, "__esModule", { value: true });
  7. exports.unwrapScript = exports.isScript = exports.EMPTY_SCRIPT = exports.createScript = exports.SafeScript = void 0;
  8. require("../environment/dev");
  9. /* g3_import_pure from './pure' */
  10. var secrets_1 = require("./secrets");
  11. var trusted_types_1 = require("./trusted_types");
  12. /**
  13. * Runtime implementation of `TrustedScript` in browswers that don't support it.
  14. * script element.
  15. */
  16. var ScriptImpl = /** @class */ (function () {
  17. function ScriptImpl(script, token) {
  18. if (process.env.NODE_ENV !== 'production') {
  19. (0, secrets_1.ensureTokenIsValid)(token);
  20. }
  21. this.privateDoNotAccessOrElseWrappedScript = script;
  22. }
  23. ScriptImpl.prototype.toString = function () {
  24. return this.privateDoNotAccessOrElseWrappedScript.toString();
  25. };
  26. return ScriptImpl;
  27. }());
  28. function createScriptInternal(script, trusted) {
  29. return (trusted !== null && trusted !== void 0 ? trusted : new ScriptImpl(script, secrets_1.secretToken));
  30. }
  31. var GlobalTrustedScript = (typeof window !== undefined) ? window.TrustedScript : undefined;
  32. /**
  33. * Also exports the constructor so that instanceof checks work.
  34. */
  35. exports.SafeScript = (GlobalTrustedScript !== null && GlobalTrustedScript !== void 0 ? GlobalTrustedScript : ScriptImpl);
  36. /**
  37. * Builds a new `SafeScript` from the given string, without enforcing
  38. * safety guarantees. It may cause side effects by creating a Trusted Types
  39. * policy. This shouldn't be exposed to application developers, and must only be
  40. * used as a step towards safe builders or safe constants.
  41. */
  42. function createScript(script) {
  43. var _a;
  44. /** @noinline */
  45. var noinlineScript = script;
  46. return createScriptInternal(noinlineScript, (_a = (0, trusted_types_1.getTrustedTypesPolicy)()) === null || _a === void 0 ? void 0 : _a.createScript(noinlineScript));
  47. }
  48. exports.createScript = createScript;
  49. /**
  50. * An empty `SafeScript` constant.
  51. * Unlike the functions above, using this will not create a policy.
  52. */
  53. exports.EMPTY_SCRIPT =
  54. /* #__PURE__ */ (function () { var _a; return createScriptInternal('', (_a = (0, trusted_types_1.getTrustedTypes)()) === null || _a === void 0 ? void 0 : _a.emptyScript); })();
  55. /**
  56. * Checks if the given value is a `SafeScript` instance.
  57. */
  58. function isScript(value) {
  59. return value instanceof exports.SafeScript;
  60. }
  61. exports.isScript = isScript;
  62. /**
  63. * Returns the value of the passed `SafeScript` object while ensuring it
  64. * has the correct type.
  65. *
  66. * Returns a native `TrustedScript` or a string if Trusted Types are disabled.
  67. */
  68. function unwrapScript(value) {
  69. var _a;
  70. if ((_a = (0, trusted_types_1.getTrustedTypes)()) === null || _a === void 0 ? void 0 : _a.isScript(value)) {
  71. return value;
  72. }
  73. else if (value instanceof ScriptImpl) {
  74. return value.privateDoNotAccessOrElseWrappedScript;
  75. }
  76. else {
  77. var message = '';
  78. if (process.env.NODE_ENV !== 'production') {
  79. message = 'Unexpected type when unwrapping SafeScript';
  80. }
  81. throw new Error(message);
  82. }
  83. }
  84. exports.unwrapScript = unwrapScript;