Skip to content

Commit

Permalink
fix: prefer globalThis instead of window to support webworkers
Browse files Browse the repository at this point in the history
There is no `window` in either WebWorkers and ServiceWorkers.
  • Loading branch information
legendecas committed Sep 9, 2021
1 parent feea516 commit b9958b4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ import {
parseEnvironment,
} from '../../utils/environment';

const _global = typeof globalThis === 'object' ? globalThis : window;

/**
* Gets the environment variables
*/
export function getEnv(): Required<ENVIRONMENT> {
const _window = window as typeof window & RAW_ENVIRONMENT;
const globalEnv = parseEnvironment(_window);
const globalEnv = parseEnvironment(_global as typeof globalThis & RAW_ENVIRONMENT);
return Object.assign({}, DEFAULT_ENVIRONMENT, globalEnv);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright The OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import * as assert from 'assert';
import { getEnv } from '../../../src/platform/browser/environment';

describe('getEnv', () => {
it('get environs in window', () => {
const env = getEnv();
assert.strictEqual(typeof env, 'object');
});
});

0 comments on commit b9958b4

Please sign in to comment.