Skip to content

Commit

Permalink
Implement polyfill for globalThis (#4653)
Browse files Browse the repository at this point in the history
* Implement polyfill for globalThis

* Add eslint rule for globalThis

* Remove duplicated lint rules

* Fix lint

* Update package lock

* Redo package lock

* Add missing browser: true config to realm-network-transport rollup
  • Loading branch information
Tom Duncalf authored Jun 23, 2022
1 parent 8aaf11c commit 893b32e
Show file tree
Hide file tree
Showing 21 changed files with 658 additions and 3,416 deletions.
3 changes: 3 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
"plugin:prettier/recommended",
"./header.eslintrc"
],
"rules": {
"no-restricted-globals": ["error", "globalThis"]
},
"env": {
"es2020": true
},
Expand Down
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ const Person = {
* Logging out too quickly can cause an error if the timeout behavior is set to `openLocalRealm` ([#4453](https://github.com/realm/realm-js/issues/4453), since v10.0.0)
* Released `realm-network-transport` to adopt the changes published to fix `globalThis` undefined issue for older devices. ([#4350](https://github.com/realm/realm-js/issues/4350), since v10.0.0)
* Fixed flexible sync crash when updating subscriptions after token expiry. ([#4421](https://github.com/realm/realm-js/issues/4421), since v10.12.0)
* Fixed remaining uses of `globalThis` undefined issue, causing Realm to not load on iOS 11/12. ([#4350](https://github.com/realm/realm-js/issues/4350))

### Compatibility
* MongoDB Realm Cloud.
Expand All @@ -218,7 +219,7 @@ const Person = {

### Fixed
* Fixed issue that could cause mangling of binary data on a roundtrip to/from the database ([#4278](https://github.com/realm/realm-js/issues/4278), since v10.1.4).
* Fixed `globalThis` undefined issue for older devices. ([#4350](https://github.com/realm/realm-js/4350))
* Fixed `globalThis` undefined issue for older devices. ([#4350](https://github.com/realm/realm-js/issues/4350))
* Fixed a fatal sync error `Automatic recovery failed` during DiscardLocal client reset if the reset notifier callbacks were not set to something. ([realm/realm-core#5223](https://github.com/realm/realm-core/issues/5223), since v10.10.0)
* Changed parsed queries using the `between` operator to be inclusive of the limits, a closed interval instead of an open interval. This is to conform to the published documentation and for parity with NSPredicate's definition. ([realm/realm-core#5262](https://github.com/realm/realm-core/issues/5262), since v10.7.0)
* If a list of objects contains links to objects not included in the synchronized partition, the indices contained in the listener callback could be wrong. ([realm/realm-core#5164](https://github.com/realm/realm-core/issues/5164), since v10.0.0)
Expand Down
3 changes: 2 additions & 1 deletion lib/mongo-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

const { EJSON } = require("bson");
const { DefaultNetworkTransport } = require("realm-network-transport");
const { safeGlobalThis } = require("@realm.io/common");

const { cleanArguments, getEnvironment } = require("./utils");

Expand All @@ -29,7 +30,7 @@ function ensureWatchDependencies() {
if (environment === "reactnative") {
const EXPECTED_GLOBALS = ["fetch", "ReadableStream", "TextDecoder"];
for (const name of EXPECTED_GLOBALS) {
if (!(name in globalThis)) {
if (!(name in safeGlobalThis)) {
throw new Error(
`Realm expects the ${name} global: Please use https://www.npmjs.com/package/react-native-polyfill-globals`,
);
Expand Down
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
"postinstall": "node scripts/submit-analytics.js"
},
"dependencies": {
"@realm.io/common": "^0.1.2",
"@realm.io/common": "^0.1.3",
"bindings": "^1.5.0",
"bson": "4.4.1",
"clang-format": "^1.6.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/realm-common/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions packages/realm-common/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@realm.io/common",
"version": "0.1.2",
"version": "0.1.3",
"description": "Cross-product common code used by Realm",
"main": "./dist/bundle.cjs.js",
"module": "./dist/bundle.es.js",
Expand Down Expand Up @@ -46,4 +46,3 @@
"rollup-plugin-dts": "^1.4.0"
}
}

1 change: 1 addition & 0 deletions packages/realm-common/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@

export { deprecationWarning, handleDeprecatedPositionalArgs } from "./deprecation";
export * as symbols from "./symbols";
export { safeGlobalThis } from "./safeGlobalThis";
2 changes: 1 addition & 1 deletion packages/realm-common/src/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import { setIsDevelopmentMode } from "../environment";

// Exported for unit testing
export const isDevelopmentModeImpl = () => {
export const isDevelopmentModeImpl = (): boolean => {
try {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { app } = require("electron");
Expand Down
45 changes: 45 additions & 0 deletions packages/realm-common/src/safeGlobalThis.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
////////////////////////////////////////////////////////////////////////////
//
// Copyright 2022 Realm Inc.
//
// 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
//
// http://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.
//
////////////////////////////////////////////////////////////////////////////

// Exports a globalThis which is polyfilled for iOS 11/12
// From https://github.com/zloirock/core-js/blob/master/packages/core-js/internals/global.js

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const check = function (it: any) {
return it && it.Math == Math && it;
};

// eslint-disable-next-line no-restricted-globals
export const safeGlobalThis: typeof globalThis =
// eslint-disable-next-line no-restricted-globals
check(typeof globalThis == "object" && globalThis) ||
check(typeof window == "object" && window) ||
// eslint-disable-next-line no-restricted-globals -- safe
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore allow `self`
check(typeof self == "object" && self) ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore allow `global`
check(typeof global == "object" && global) ||
// eslint-disable-next-line no-new-func -- fallback
(function () {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore allow `this`
return this;
})() ||
Function("return this")();
1 change: 1 addition & 0 deletions packages/realm-network-transport/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
},
"license": "SEE LICENSE IN LICENSE",
"dependencies": {
"@realm.io/common": "^0.1.3",
"abort-controller": "^3.0.0",
"node-fetch": "^2.6.0"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/realm-network-transport/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export default [
typescript({
tsconfig: "src/dom/tsconfig.json",
}),
nodeResolve(),
nodeResolve({ browser: true }),
],
},
{
Expand Down
9 changes: 3 additions & 6 deletions packages/realm-network-transport/src/dom/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ export * from "../index";

import { DefaultNetworkTransport } from "../DefaultNetworkTransport";
import { AbortController, Fetch } from "../types";
import { safeGlobalThis } from "@realm.io/common";

const globalThisOrWindow = typeof globalThis === "object" ? globalThis : window;

DefaultNetworkTransport.fetch = globalThisOrWindow.fetch.bind(globalThisOrWindow) as Fetch;
DefaultNetworkTransport.AbortController = globalThisOrWindow.AbortController.bind(
globalThisOrWindow,
) as AbortController;
DefaultNetworkTransport.fetch = safeGlobalThis.fetch.bind(safeGlobalThis) as Fetch;
DefaultNetworkTransport.AbortController = safeGlobalThis.AbortController.bind(safeGlobalThis) as AbortController;
9 changes: 3 additions & 6 deletions packages/realm-network-transport/src/react-native/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,12 @@

export * from "../index";

import { safeGlobalThis } from "@realm.io/common";
import { DefaultNetworkTransport } from "../DefaultNetworkTransport";
import { AbortController, Fetch } from "../types";

const globalThisOrWindow = typeof globalThis === "object" ? globalThis : window;

DefaultNetworkTransport.fetch = globalThisOrWindow.fetch.bind(globalThisOrWindow) as Fetch;
DefaultNetworkTransport.AbortController = globalThisOrWindow.AbortController.bind(
globalThisOrWindow,
) as AbortController;
DefaultNetworkTransport.fetch = safeGlobalThis.fetch.bind(safeGlobalThis) as Fetch;
DefaultNetworkTransport.AbortController = safeGlobalThis.AbortController.bind(safeGlobalThis) as AbortController;

// Setting this non-standard option to enable text streaming
// See https://github.com/react-native-community/fetch#enable-text-streaming
Expand Down
6 changes: 3 additions & 3 deletions packages/realm-react/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 893b32e

Please sign in to comment.