• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

adobe / reactor-turbine / 19547546514

20 Nov 2025 06:35PM UTC coverage: 95.912%. First build
19547546514

Pull #191

github

web-flow
Merge a2b53a905 into 810a8cf76
Pull Request #191: PDCL 13739 (chatty logs) & PDCL 4521 (shared logging)

340 of 377 branches covered (90.19%)

Branch coverage included in aggregate %.

422 of 433 new or added lines in 14 files covered. (97.46%)

833 of 846 relevant lines covered (98.46%)

56.14 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

89.58
/src/createDynamicHostResolver.js
1
/***************************************************************************************
2
 * (c) 2021 Adobe. All rights reserved.
3
 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
 * you may not use this file except in compliance with the License. You may obtain a copy
5
 * of the License at http://www.apache.org/licenses/LICENSE-2.0
6
 *
7
 * Unless required by applicable law or agreed to in writing, software distributed under
8
 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
 * OF ANY KIND, either express or implied. See the License for the specific language
10
 * governing permissions and limitations under the License.
11
 ****************************************************************************************/
12

13
var validateInjectedParams = require('./helpers/validate-injected-params');
3✔
14

15
function injectCreateDynamicHostResolver({ window }) {
16
  return function createDynamicHostResolver(
204✔
17
    turbineEmbedCode,
18
    dynamicCdnEnabled,
19
    cdnAllowList,
20
    debugController
21
  ) {
22
    // A missing list means that we are not trying to dynamic replace (archives,
23
    // sftp, no premium CDN option enabled on the company).
24
    // even an empty list is flagging to us that we're trying to enforce dynamic
25
    var isDynamicEnforced = Boolean(
219✔
26
      dynamicCdnEnabled && Array.isArray(cdnAllowList)
435✔
27
    );
28
    var shouldAugment = Boolean(isDynamicEnforced && turbineEmbedCode);
219✔
29

30
    // using document.createElement('a') because IE10/11 doesn't support new URL()
31
    var turbineUrl = document.createElement('a');
219✔
32
    if (isDynamicEnforced) {
219✔
33
      var throwUnavailableEmbedCode = function () {
138✔
34
        var missingEmbedCodeError = new Error(
12✔
35
          'Unable to find the Library Embed Code for Dynamic Host Resolution.'
36
        );
37
        missingEmbedCodeError.code = 'dynamic_host_resolver_constructor_error';
12✔
38
        throw missingEmbedCodeError;
12✔
39
      };
40
      if (turbineEmbedCode) {
138✔
41
        if (!/^((https?:)?\/\/).+/.test(turbineEmbedCode)) {
129✔
42
          throwUnavailableEmbedCode();
3✔
43
        }
44
        if (/^\/\/.+/.test(turbineEmbedCode)) {
126✔
45
          // for IE 10, you must throw on the protocol
46
          turbineUrl.href = window.location.protocol + turbineEmbedCode;
9✔
47
        } else {
48
          turbineUrl.href = turbineEmbedCode;
117✔
49
        }
50
      }
51

52
      // check URL construction
53
      if (!turbineUrl.hostname) {
135✔
54
        throwUnavailableEmbedCode();
9✔
55
      }
56
      // is this within the allowed list of hosts?
57
      if (cdnAllowList.indexOf(turbineUrl.hostname) === -1) {
126✔
58
        var dynamicDeniedError = new Error(
12✔
59
          'This library is not authorized for this domain. ' +
60
            'Please contact your CSM for more information.'
61
        );
62
        dynamicDeniedError.code = 'dynamic_host_not_allowed';
12✔
63
        throw dynamicDeniedError;
12✔
64
      }
65
    }
66

67
    /**
68
     * Returns the host of the Turbine embed code, or an empty string if Dynamic Host
69
     * is not enabled.
70
     * @returns {string}
71
     */
72
    var memoizedHostResult;
73
    var getTurbineHost = function () {
195✔
74
      if (memoizedHostResult != null) {
78✔
75
        return memoizedHostResult;
42✔
76
      }
77

78
      if (shouldAugment) {
36!
79
        // be sure we always force https to Adobe managed domains.
80
        // IE 10/11 returns the :443 protocol when modern browsers don't, so this replacement
81
        // is bringing every browser in line with the same return value
82
        var sanitizedHost = turbineUrl.host;
36✔
83
        if (/:80$/.test(sanitizedHost)) {
36✔
84
          sanitizedHost = sanitizedHost.replace(':80', '');
3✔
85
        } else if (/:80\/$/.test(sanitizedHost)) {
33!
NEW
86
          sanitizedHost = sanitizedHost.replace(':80/', '');
×
87
        } else if (/:443$/.test(sanitizedHost)) {
33!
NEW
88
          sanitizedHost = sanitizedHost.replace(':443', '');
×
89
        } else if (/:443\/$/.test(sanitizedHost)) {
33!
NEW
90
          sanitizedHost = sanitizedHost.replace(':443/', '');
×
91
        }
92

93
        memoizedHostResult = turbineUrl.protocol + '//' + sanitizedHost;
36✔
94
      } else {
NEW
95
        memoizedHostResult = '';
×
96
      }
97

98
      return memoizedHostResult;
36✔
99
    };
100

101
    /**
102
     * Returns a url decorated with the host of the Turbine embed code. If Dynamic host
103
     * is disabled, the original sourceUrl is returned unmodified.
104
     * @param sourceUrl
105
     * @returns {string|*}
106
     */
107
    var decorateWithDynamicHost = function (sourceUrl) {
195✔
108
      if (shouldAugment && typeof sourceUrl === 'string') {
111✔
109
        var urlParts = [
72✔
110
          getTurbineHost(),
111
          sourceUrl.charAt(0) === '/' ? sourceUrl.slice(1) : sourceUrl
72✔
112
        ];
113

114
        return urlParts.join('/');
72✔
115
      }
116

117
      return sourceUrl;
39✔
118
    };
119

120
    var dynamicHostResolver = {
195✔
121
      getTurbineHost: getTurbineHost,
122
      decorateWithDynamicHost: decorateWithDynamicHost,
123
      get isDynamicEnforced() {
124
        return isDynamicEnforced;
57✔
125
      }
126
    };
127

128
    // when we're in debug mode, this will expose the dynamicHostResolver to
129
    // the window for debugging.
130
    if (window) {
195!
131
      function decideToExposeDynamicHostResolver(isDebugEnabled) {
132
        if (isDebugEnabled) {
204✔
133
          window.dynamicHostResolver = dynamicHostResolver;
66✔
134
        } else {
135
          delete window.dynamicHostResolver;
138✔
136
        }
137
      }
138
      debugController.onDebugChanged(decideToExposeDynamicHostResolver);
195✔
139
      // if debug is already enabled when the page is loaded, we won't get a
140
      // debugChanged callback, so check it right on page-load.
141
      decideToExposeDynamicHostResolver(debugController.getDebugEnabled());
195✔
142
    }
143

144
    return dynamicHostResolver;
195✔
145
  };
146
}
147

148
const validateInjection = validateInjectedParams(
3✔
149
  injectCreateDynamicHostResolver
150
);
151

152
module.exports = validateInjection({
3✔
153
  window: require('@adobe/reactor-window')
154
});
155

156
if (REACTOR_KARMA_CI_UNIT_TEST_MODE) {
3!
157
  /* START.TESTS_ONLY */
158
  module.exports.injectCreateDynamicHostResolver = validateInjection;
3✔
159
  /* END.TESTS_ONLY */
160
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc