diff --git a/CHANGELOG.md b/CHANGELOG.md index 043511f742..ed49e74c22 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -124,7 +124,7 @@ NOTE: This version uses the Realm file format to version 20. It is not possible * None ### Enhancements -* None +* Added an export of the `bson` module on the package, making it possible to access the BSON types via `import Realm from "realm";` followed by `Realm.BSON.ObjectId`, `Realm.BSON.Decimal128`, `Realm.BSON.Binary` etc. ([#3363](https://github.com/realm/realm-js/pull/3363)) ### Fixed * Fixed RN Android error: couldn't find DSO to load: librealmreact.so caused by: dlopen failed: cannot locate symbol. ([#3347](https://github.com/realm/realm-js/issues/3347), since v10.0.0) diff --git a/docs/bson.js b/docs/bson.js new file mode 100644 index 0000000000..4423ea3a18 --- /dev/null +++ b/docs/bson.js @@ -0,0 +1,25 @@ +//////////////////////////////////////////////////////////////////////////// +// +// Copyright 2016 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. +// +//////////////////////////////////////////////////////////////////////////// + + /** + * A re-export of the "bson" package, enabling access to the BSON types without requiring an explict dependency on the "bson" package. + * + * @see {@link https://www.npmjs.com/package/bson#documentation|the BSON documentation} for more information. + * @memberof Realm + */ +const BSON = {}; \ No newline at end of file diff --git a/integration-tests/tests/package-lock.json b/integration-tests/tests/package-lock.json index b5b7ba422c..5159faec45 100644 --- a/integration-tests/tests/package-lock.json +++ b/integration-tests/tests/package-lock.json @@ -30,15 +30,6 @@ "js-tokens": "^4.0.0" } }, - "@types/bson": { - "version": "4.0.2", - "resolved": "https://registry.npmjs.org/@types/bson/-/bson-4.0.2.tgz", - "integrity": "sha512-+uWmsejEHfmSjyyM/LkrP0orfE2m5Mx9Xel4tXNeqi1ldK5XMQcDsFkBmLDtuyKUbxj2jGDo0H240fbCRJZo7Q==", - "dev": true, - "requires": { - "@types/node": "*" - } - }, "@types/chai": { "version": "4.2.11", "resolved": "https://registry.npmjs.org/@types/chai/-/chai-4.2.11.tgz", @@ -58,12 +49,6 @@ "integrity": "sha512-NYrtPht0wGzhwe9+/idPaBB+TqkY9AhTvOLMkThm0IoEfLaiVQZwBwyJ5puCkO3AUCWrmcoePjp2mbFocKy4SQ==", "dev": true }, - "@types/node": { - "version": "14.11.5", - "resolved": "https://registry.npmjs.org/@types/node/-/node-14.11.5.tgz", - "integrity": "sha512-jVFzDV6NTbrLMxm4xDSIW/gKnk8rQLF9wAzLWIOg+5nU6ACrIMndeBdXci0FGtqJbP9tQvm6V39eshc96TO2wQ==", - "dev": true - }, "ansi-styles": { "version": "3.2.1", "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-3.2.1.tgz", @@ -99,6 +84,11 @@ "integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=", "dev": true }, + "base64-js": { + "version": "1.3.1", + "resolved": "https://registry.npmjs.org/base64-js/-/base64-js-1.3.1.tgz", + "integrity": "sha512-mLQ4i2QO1ytvGWFWmcngKO//JXAQueZvwEKtjgQFM4jIK0kU+ytMfplL8j+n5mspOfjHwoAg+9yhb7BwAHm36g==" + }, "brace-expansion": { "version": "1.1.11", "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", @@ -115,6 +105,23 @@ "integrity": "sha512-qhAVI1+Av2X7qelOfAIYwXONood6XlZE/fXaBSmW/T5SzLAmCgzi+eiWE7fUvbHaeNBQH13UftjpXxsfLkMpgw==", "dev": true }, + "bson": { + "version": "4.2.0", + "resolved": "https://registry.npmjs.org/bson/-/bson-4.2.0.tgz", + "integrity": "sha512-c3MlJqdROnCRvDr/+MLfaDvQ7CvGI4p1hKX45/fvgzSwKRdOjsfRug1NJJ8ty5mXCNtUdjJEWzoZWcBQxV4TyA==", + "requires": { + "buffer": "^5.6.0" + } + }, + "buffer": { + "version": "5.7.0", + "resolved": "https://registry.npmjs.org/buffer/-/buffer-5.7.0.tgz", + "integrity": "sha512-cd+5r1VLBwUqTrmnzW+D7ABkJUM6mr7uv1dv+6jRw4Rcl7tFIFHDqHPL98LhpGFn3dbAt3gtLxtrWp4m1kFrqg==", + "requires": { + "base64-js": "^1.3.1", + "ieee754": "^1.1.13" + } + }, "buffer-from": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/buffer-from/-/buffer-from-1.1.1.tgz", @@ -278,6 +285,11 @@ "integrity": "sha1-k0EP0hsAlzUVH4howvJx80J+I/0=", "dev": true }, + "ieee754": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/ieee754/-/ieee754-1.2.1.tgz", + "integrity": "sha512-dcyqhDvX1C46lXZcVqCpK+FtMRQVdIMN6/Df5js2zouUsqG7I6sFxitIC+7KYK29KdXOLHdu9zL4sFnoVQnqaA==" + }, "inflight": { "version": "1.0.6", "resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz", @@ -423,7 +435,7 @@ "version": "file:../..", "dev": true, "requires": { - "bson": "^4.0.3", + "bson": "^4.2.0", "command-line-args": "^4.0.6", "deepmerge": "2.1.0", "deprecated-react-native-listview": "0.0.6", @@ -2473,6 +2485,12 @@ } } }, + "base64-js": { + "version": "1.3.1", + "resolved": "https://registry.npmjs.org/base64-js/-/base64-js-1.3.1.tgz", + "integrity": "sha512-mLQ4i2QO1ytvGWFWmcngKO//JXAQueZvwEKtjgQFM4jIK0kU+ytMfplL8j+n5mspOfjHwoAg+9yhb7BwAHm36g==", + "dev": true + }, "bcrypt-pbkdf": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/bcrypt-pbkdf/-/bcrypt-pbkdf-1.0.2.tgz", @@ -2528,31 +2546,12 @@ } }, "bson": { - "version": "4.0.3", - "resolved": "https://registry.npmjs.org/bson/-/bson-4.0.3.tgz", - "integrity": "sha512-7uBjjxwOSuGLmoqGI1UXWpDGc0K2WjR7dC6iaOg4iriNZo6M2EEBb8co4dEPJ5ArYCebPMie0ecgX0TWF+ZUrQ==", + "version": "4.2.0", + "resolved": "https://registry.npmjs.org/bson/-/bson-4.2.0.tgz", + "integrity": "sha512-c3MlJqdROnCRvDr/+MLfaDvQ7CvGI4p1hKX45/fvgzSwKRdOjsfRug1NJJ8ty5mXCNtUdjJEWzoZWcBQxV4TyA==", "dev": true, "requires": { - "buffer": "^5.1.0", - "long": "^4.0.0" - }, - "dependencies": { - "base64-js": { - "version": "1.3.1", - "resolved": "https://registry.npmjs.org/base64-js/-/base64-js-1.3.1.tgz", - "integrity": "sha512-mLQ4i2QO1ytvGWFWmcngKO//JXAQueZvwEKtjgQFM4jIK0kU+ytMfplL8j+n5mspOfjHwoAg+9yhb7BwAHm36g==", - "dev": true - }, - "buffer": { - "version": "5.4.3", - "resolved": "https://registry.npmjs.org/buffer/-/buffer-5.4.3.tgz", - "integrity": "sha512-zvj65TkFeIt3i6aj5bIvJDzjjQQGs4o/sNoezg1F1kYap9Nu2jcUdpwzRSJTHMMzG0H7bZkn4rNQpImhuxWX2A==", - "dev": true, - "requires": { - "base64-js": "^1.0.2", - "ieee754": "^1.1.4" - } - } + "buffer": "^5.6.0" } }, "btoa-lite": { @@ -2560,6 +2559,16 @@ "resolved": "https://registry.npmjs.org/btoa-lite/-/btoa-lite-1.0.0.tgz", "integrity": "sha1-M3dm2hWAEhD92VbCLpxokaudAzc=" }, + "buffer": { + "version": "5.7.0", + "resolved": "https://registry.npmjs.org/buffer/-/buffer-5.7.0.tgz", + "integrity": "sha512-cd+5r1VLBwUqTrmnzW+D7ABkJUM6mr7uv1dv+6jRw4Rcl7tFIFHDqHPL98LhpGFn3dbAt3gtLxtrWp4m1kFrqg==", + "dev": true, + "requires": { + "base64-js": "^1.3.1", + "ieee754": "^1.1.13" + } + }, "buffer-from": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/buffer-from/-/buffer-from-1.1.1.tgz", @@ -5853,9 +5862,9 @@ } }, "ieee754": { - "version": "1.1.12", - "resolved": "https://registry.npmjs.org/ieee754/-/ieee754-1.1.12.tgz", - "integrity": "sha512-GguP+DRY+pJ3soyIiGPTvdiVXjZ+DbXOxGpXn3eMvNW4x4irjqXm4wHKscC+TfxSJ0yw/S1F24tqdMNsMZTiLA==", + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/ieee754/-/ieee754-1.2.1.tgz", + "integrity": "sha512-dcyqhDvX1C46lXZcVqCpK+FtMRQVdIMN6/Df5js2zouUsqG7I6sFxitIC+7KYK29KdXOLHdu9zL4sFnoVQnqaA==", "dev": true }, "iferr": { @@ -6688,12 +6697,6 @@ "chalk": "^2.0.1" } }, - "long": { - "version": "4.0.0", - "resolved": "https://registry.npmjs.org/long/-/long-4.0.0.tgz", - "integrity": "sha512-XsP+KhQif4bjX1kbuSiySJFNAehNxgLb6hPRGJ9QsUr8ajHkuXGdrHmFUTUUXhDwVX2R5bY4JNZEwbUiMhV+MA==", - "dev": true - }, "loose-envify": { "version": "1.4.0", "resolved": "https://registry.npmjs.org/loose-envify/-/loose-envify-1.4.0.tgz", diff --git a/integration-tests/tests/package.json b/integration-tests/tests/package.json index 02aa306755..32b57cad12 100644 --- a/integration-tests/tests/package.json +++ b/integration-tests/tests/package.json @@ -15,7 +15,6 @@ "realm": "*" }, "devDependencies": { - "@types/bson": "^4.0.2", "@types/chai": "^4.1.7", "@types/mocha": "^5.2.6", "fs-extra": "^7.0.1", @@ -26,6 +25,7 @@ "typescript": "^3.8.2" }, "dependencies": { + "bson": "^4.2.0", "chai": "^4.2.0" }, "files": [ diff --git a/integration-tests/tests/src/bson.ts b/integration-tests/tests/src/bson.ts new file mode 100644 index 0000000000..59058e7f00 --- /dev/null +++ b/integration-tests/tests/src/bson.ts @@ -0,0 +1,11 @@ +import { expect } from "chai"; + +describe("BSON", () => { + it("gets exported", () => { + expect(typeof Realm.BSON).equals("object"); + expect(typeof Realm.BSON.ObjectId).equals("function"); + expect(typeof Realm.BSON.Decimal128).equals("function"); + expect(typeof Realm.BSON.Binary).equals("function"); + expect(typeof Realm.BSON.EJSON).equals("object"); + }); +}); diff --git a/integration-tests/tests/src/index.ts b/integration-tests/tests/src/index.ts index 42814f77ae..2517833474 100644 --- a/integration-tests/tests/src/index.ts +++ b/integration-tests/tests/src/index.ts @@ -46,6 +46,7 @@ describe(global.title, () => { require("./objects"); require("./iterators"); require("./dynamic-schema-updates"); + require("./bson"); }); beforeEach(() => { diff --git a/integration-tests/tests/src/objects.ts b/integration-tests/tests/src/objects.ts index 6bb946e1cf..3a414c6fc8 100644 --- a/integration-tests/tests/src/objects.ts +++ b/integration-tests/tests/src/objects.ts @@ -6,7 +6,6 @@ import { Person as PersonWithId, PersonSchema as PersonSchemaWithId, } from "./schemas/person-and-dog-with-object-ids"; -import { ObjectId } from "bson"; describe("Realm objects", () => { describe("Interface & object literal", () => { @@ -45,7 +44,7 @@ describe("Realm objects", () => { it("can be fetched with objectForPrimaryKey", () => { const realm = new Realm({ schema: [PersonSchemaWithId] }); - const _id = new ObjectId(); + const _id = new Realm.BSON.ObjectId(); realm.write(() => { realm.create(PersonSchemaWithId.name, { @@ -69,7 +68,7 @@ describe("Realm objects", () => { it("can be updated", () => { const realm = new Realm({ schema: [PersonSchemaWithId] }); let john: IPersonWithId; - const _id = new ObjectId(); + const _id = new Realm.BSON.ObjectId(); realm.write(() => { john = realm.create(PersonSchemaWithId.name, { @@ -164,7 +163,7 @@ describe("Realm objects", () => { it("can be fetched with objectForPrimaryKey", () => { const realm = new Realm({ schema: [PersonWithId] }); - const _id = new ObjectId(); + const _id = new Realm.BSON.ObjectId(); realm.write(() => { realm.create(PersonWithId, { @@ -185,7 +184,7 @@ describe("Realm objects", () => { it("can be updated", () => { const realm = new Realm({ schema: [PersonWithId] }); let john: PersonWithId; - const _id = new ObjectId(); + const _id = new Realm.BSON.ObjectId(); realm.write(() => { john = realm.create(PersonWithId, { diff --git a/integration-tests/tests/src/schemas/person-and-dog-with-object-ids.ts b/integration-tests/tests/src/schemas/person-and-dog-with-object-ids.ts index d7690896ed..a736727936 100755 --- a/integration-tests/tests/src/schemas/person-and-dog-with-object-ids.ts +++ b/integration-tests/tests/src/schemas/person-and-dog-with-object-ids.ts @@ -18,7 +18,7 @@ /* tslint:disable max-classes-per-file */ -import * as Realm from "realm"; +import Realm from "realm"; import { ObjectId } from "bson"; export interface IPerson { diff --git a/integration-tests/tests/src/schemas/person-and-dogs.ts b/integration-tests/tests/src/schemas/person-and-dogs.ts index 652711d312..37897b120b 100644 --- a/integration-tests/tests/src/schemas/person-and-dogs.ts +++ b/integration-tests/tests/src/schemas/person-and-dogs.ts @@ -18,7 +18,7 @@ /* tslint:disable max-classes-per-file */ -import * as Realm from "realm"; +import Realm from "realm"; export interface IPerson { name: string; diff --git a/integration-tests/tests/src/schemas/playlist-with-songs-with-ids.ts b/integration-tests/tests/src/schemas/playlist-with-songs-with-ids.ts index 1851d92863..d80a6fab4c 100644 --- a/integration-tests/tests/src/schemas/playlist-with-songs-with-ids.ts +++ b/integration-tests/tests/src/schemas/playlist-with-songs-with-ids.ts @@ -16,7 +16,7 @@ // //////////////////////////////////////////////////////////////////////////// -import * as Realm from "realm"; +import Realm from "realm"; /* tslint:disable max-classes-per-file */ diff --git a/integration-tests/tests/src/schemas/playlist-with-songs.ts b/integration-tests/tests/src/schemas/playlist-with-songs.ts index 99c0dec5da..8ad21e8f40 100644 --- a/integration-tests/tests/src/schemas/playlist-with-songs.ts +++ b/integration-tests/tests/src/schemas/playlist-with-songs.ts @@ -16,7 +16,7 @@ // //////////////////////////////////////////////////////////////////////////// -import * as Realm from "realm"; +import Realm from "realm"; /* tslint:disable max-classes-per-file */ diff --git a/integration-tests/tests/src/serialization.ts b/integration-tests/tests/src/serialization.ts index 8796ec09ca..e8674b1ff2 100755 --- a/integration-tests/tests/src/serialization.ts +++ b/integration-tests/tests/src/serialization.ts @@ -17,7 +17,6 @@ //////////////////////////////////////////////////////////////////////////// import { expect } from "chai"; -import { ObjectId } from "bson"; import { IPlaylist as IPlaylistNoId, ISong as ISongNoId, @@ -34,8 +33,8 @@ import { Playlist as PlaylistWithId, Song as SongWithId, } from "./schemas/playlist-with-songs-with-ids"; -import * as circularCollectionResult from "./structures/circular-collection-result.json"; -import * as circularCollectionResultWithIds from "./structures/circular-collection-result-with-primary-ids.json"; +import circularCollectionResult from "./structures/circular-collection-result.json"; +import circularCollectionResultWithIds from "./structures/circular-collection-result-with-primary-ids.json"; describe("JSON serialization (exposed properties)", () => { it("JsonSerializationReplacer is exposed on the Realm constructor", () => { @@ -439,7 +438,7 @@ const cacheIdTestSetups: ICacheIdTestSetup[] = [ { type: "objectId", schemaName: "ObjectIdTest", - testId: new ObjectId("5f99418846da9c45005f50bf"), + testId: new Realm.BSON.ObjectId("5f99418846da9c45005f50bf"), expectedResult: "ObjectIdTest#5f99418846da9c45005f50bf", }, ]; diff --git a/integration-tests/tests/tsconfig.json b/integration-tests/tests/tsconfig.json index 24c2b14416..cfde397c2a 100644 --- a/integration-tests/tests/tsconfig.json +++ b/integration-tests/tests/tsconfig.json @@ -3,18 +3,16 @@ "target": "es2018", "module": "commonjs", "moduleResolution": "node", + "esModuleInterop": true, "resolveJsonModule": true, "outDir": "dist", "types": [ - "node", "realm", + "bson", + "buffer", "mocha", "chai" ], - "typeRoots": [ - "types", - "node_modules/@types" - ], "noImplicitAny": true }, "include": [ diff --git a/integration-tests/tests/types/node/index.d.ts b/integration-tests/tests/types/node/index.d.ts deleted file mode 100644 index d82044dc2b..0000000000 --- a/integration-tests/tests/types/node/index.d.ts +++ /dev/null @@ -1,5 +0,0 @@ -// Our version of Node.js types are very restricted -// This file will get resolved when realm's dependency on bson resolves a dependency on @types/node. -// We need this file to prevent the NodeJS globals to be accessable from within the test source files. - -interface Buffer extends Uint8Array { } diff --git a/lib/extensions.js b/lib/extensions.js index a3450782da..7f6bc9a31b 100644 --- a/lib/extensions.js +++ b/lib/extensions.js @@ -74,9 +74,10 @@ module.exports = function(realmConstructor, context) { setConstructorOnPrototype(realmConstructor.Results); setConstructorOnPrototype(realmConstructor.Object); - realmConstructor._bson = require('bson'); - realmConstructor._Decimal128 = realmConstructor._bson.Decimal128; - realmConstructor._ObjectId = realmConstructor._bson.ObjectId; + realmConstructor.BSON = require('bson'); + realmConstructor._Decimal128 = realmConstructor.BSON.Decimal128; + realmConstructor._ObjectId = realmConstructor.BSON.ObjectId; + const { DefaultNetworkTransport } = require('realm-network-transport'); realmConstructor._networkTransport = new DefaultNetworkTransport(); diff --git a/package-lock.json b/package-lock.json index 9d1b26c6ce..29e5092eca 100644 --- a/package-lock.json +++ b/package-lock.json @@ -6,7 +6,7 @@ "dependencies": { "@babel/code-frame": { "version": "7.0.0-beta.44", - "resolved": "http://registry.npmjs.org/@babel/code-frame/-/code-frame-7.0.0-beta.44.tgz", + "resolved": "https://registry.npmjs.org/@babel/code-frame/-/code-frame-7.0.0-beta.44.tgz", "integrity": "sha512-cuAuTTIQ9RqcFRJ/Y8PvTh+paepNcaGxwQwjIDRWPXmzzyAeCO4KqS9ikMvq0MCbRk6GlYKwfzStrcP3/jSL8g==", "dev": true, "requires": { @@ -15,7 +15,7 @@ }, "@babel/generator": { "version": "7.0.0-beta.44", - "resolved": "http://registry.npmjs.org/@babel/generator/-/generator-7.0.0-beta.44.tgz", + "resolved": "https://registry.npmjs.org/@babel/generator/-/generator-7.0.0-beta.44.tgz", "integrity": "sha512-5xVb7hlhjGcdkKpMXgicAVgx8syK5VJz193k0i/0sLP6DzE6lRrU1K3B/rFefgdo9LPGMAOOOAWW4jycj07ShQ==", "dev": true, "requires": { @@ -28,7 +28,7 @@ }, "@babel/helper-function-name": { "version": "7.0.0-beta.44", - "resolved": "http://registry.npmjs.org/@babel/helper-function-name/-/helper-function-name-7.0.0-beta.44.tgz", + "resolved": "https://registry.npmjs.org/@babel/helper-function-name/-/helper-function-name-7.0.0-beta.44.tgz", "integrity": "sha512-MHRG2qZMKMFaBavX0LWpfZ2e+hLloT++N7rfM3DYOMUOGCD8cVjqZpwiL8a0bOX3IYcQev1ruciT0gdFFRTxzg==", "dev": true, "requires": { @@ -39,7 +39,7 @@ }, "@babel/helper-get-function-arity": { "version": "7.0.0-beta.44", - "resolved": "http://registry.npmjs.org/@babel/helper-get-function-arity/-/helper-get-function-arity-7.0.0-beta.44.tgz", + "resolved": "https://registry.npmjs.org/@babel/helper-get-function-arity/-/helper-get-function-arity-7.0.0-beta.44.tgz", "integrity": "sha512-w0YjWVwrM2HwP6/H3sEgrSQdkCaxppqFeJtAnB23pRiJB5E/O9Yp7JAAeWBl+gGEgmBFinnTyOv2RN7rcSmMiw==", "dev": true, "requires": { @@ -48,7 +48,7 @@ }, "@babel/helper-split-export-declaration": { "version": "7.0.0-beta.44", - "resolved": "http://registry.npmjs.org/@babel/helper-split-export-declaration/-/helper-split-export-declaration-7.0.0-beta.44.tgz", + "resolved": "https://registry.npmjs.org/@babel/helper-split-export-declaration/-/helper-split-export-declaration-7.0.0-beta.44.tgz", "integrity": "sha512-aQ7QowtkgKKzPGf0j6u77kBMdUFVBKNHw2p/3HX/POt5/oz8ec5cs0GwlgM8Hz7ui5EwJnzyfRmkNF1Nx1N7aA==", "dev": true, "requires": { @@ -63,7 +63,7 @@ }, "@babel/highlight": { "version": "7.0.0-beta.44", - "resolved": "http://registry.npmjs.org/@babel/highlight/-/highlight-7.0.0-beta.44.tgz", + "resolved": "https://registry.npmjs.org/@babel/highlight/-/highlight-7.0.0-beta.44.tgz", "integrity": "sha512-Il19yJvy7vMFm8AVAh6OZzaFoAd0hbkeMZiX3P5HGD+z7dyI7RzndHB0dg6Urh/VAFfHtpOIzDUSxmY6coyZWQ==", "dev": true, "requires": { @@ -88,7 +88,7 @@ }, "@babel/template": { "version": "7.0.0-beta.44", - "resolved": "http://registry.npmjs.org/@babel/template/-/template-7.0.0-beta.44.tgz", + "resolved": "https://registry.npmjs.org/@babel/template/-/template-7.0.0-beta.44.tgz", "integrity": "sha512-w750Sloq0UNifLx1rUqwfbnC6uSUk0mfwwgGRfdLiaUzfAOiH0tHJE6ILQIUi3KYkjiCDTskoIsnfqZvWLBDng==", "dev": true, "requires": { @@ -100,7 +100,7 @@ }, "@babel/traverse": { "version": "7.0.0-beta.44", - "resolved": "http://registry.npmjs.org/@babel/traverse/-/traverse-7.0.0-beta.44.tgz", + "resolved": "https://registry.npmjs.org/@babel/traverse/-/traverse-7.0.0-beta.44.tgz", "integrity": "sha512-UHuDz8ukQkJCDASKHf+oDt3FVUzFd+QYfuBIsiNu/4+/ix6pP/C+uQZJ6K1oEfbCMv/IKWbgDEh7fcsnIE5AtA==", "dev": true, "requires": { @@ -118,7 +118,7 @@ }, "@babel/types": { "version": "7.0.0-beta.44", - "resolved": "http://registry.npmjs.org/@babel/types/-/types-7.0.0-beta.44.tgz", + "resolved": "https://registry.npmjs.org/@babel/types/-/types-7.0.0-beta.44.tgz", "integrity": "sha512-5eTV4WRmqbaFM3v9gHAIljEQJU4Ssc6fxL61JN+Oe2ga/BwyjzjamwkCVVAQjHGuAX8i0BWo42dshL8eO5KfLQ==", "dev": true, "requires": { @@ -1689,15 +1689,6 @@ } } }, - "@types/bson": { - "version": "4.0.3", - "resolved": "https://registry.npmjs.org/@types/bson/-/bson-4.0.3.tgz", - "integrity": "sha512-mVRvYnTOZJz3ccpxhr3wgxVmSeiYinW+zlzQz3SXWaJmD1DuL05Jeq7nKw3SnbKmbleW5qrLG5vdyWe/A9sXhw==", - "dev": true, - "requires": { - "@types/node": "*" - } - }, "@types/eslint-visitor-keys": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/@types/eslint-visitor-keys/-/eslint-visitor-keys-1.0.0.tgz", @@ -1911,7 +1902,7 @@ "ansi-styles": { "version": "3.2.1", "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-3.2.1.tgz", - "integrity": "sha1-QfuyAkPlCxK+DwS43tvwdSDOhB0=", + "integrity": "sha512-VT0ZI6kZRdTh8YyJw3SMbYm/u+NqfsAxEpWO0Pf9sq8/e94WxxOpPKx9FR1FlyCtOVDNOQ+8ntlqFxiRc+r5qA==", "dev": true, "requires": { "color-convert": "^1.9.0" @@ -2102,7 +2093,7 @@ "babel-eslint": { "version": "8.2.6", "resolved": "https://registry.npmjs.org/babel-eslint/-/babel-eslint-8.2.6.tgz", - "integrity": "sha1-YnDQxzIFYoBnwPeuFpOp55es79k=", + "integrity": "sha512-aCdHjhzcILdP8c9lej7hvXKvQieyRt20SF102SIGyY4cUIiw6UaAtK4j2o3dXX74jEmy0TJ0CEhv4fTIM3SzcA==", "dev": true, "requires": { "@babel/code-frame": "7.0.0-beta.44", @@ -3057,7 +3048,7 @@ "cross-spawn": { "version": "6.0.5", "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-6.0.5.tgz", - "integrity": "sha1-Sl7Hxk364iw6FBJNus3uhG2Ay8Q=", + "integrity": "sha512-eTVLrBSt7fjbDygz805pMnstIs2VTBNkRm0qxZd+M7A5XDdxVRWO5MxGBXZhjY4cqLYLdtrGqRf8mBPmzwSpWQ==", "dev": true, "requires": { "nice-try": "^1.0.4", @@ -5160,7 +5151,7 @@ "ignore": { "version": "4.0.6", "resolved": "https://registry.npmjs.org/ignore/-/ignore-4.0.6.tgz", - "integrity": "sha1-dQ49tYYgh7RzfrrIIH/9HvJ7Jfw=", + "integrity": "sha512-cyFDKrqc/YdcWFniJhzI42+AzS+gNwmUzOSFcRCQYwySuBBBy/KjuxWLZ/FHEH6Moq1NizMOBWyTcv8O4OZIMg==", "dev": true }, "ignore-walk": { @@ -6633,7 +6624,7 @@ "mockery": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/mockery/-/mockery-2.1.0.tgz", - "integrity": "sha1-WwrvH/Vk8PgTlEXhZVNseQlxNHA=", + "integrity": "sha512-9VkOmxKlWXoDO/h1jDZaS4lH33aWfRiJiNT/tKj+8OGzrcFDLo8d0syGdbsc3Bc4GvRXPb+NMMvojotmuGJTvA==", "dev": true }, "modify-values": { @@ -8975,7 +8966,7 @@ "tmp": { "version": "0.0.33", "resolved": "https://registry.npmjs.org/tmp/-/tmp-0.0.33.tgz", - "integrity": "sha1-bTQzWIl2jSGyvNoKonfO07G/rfk=", + "integrity": "sha512-jRCJlojKnZ3addtTOjdIqoRuPEKBvNXcGYqzO6zWZX8KfKEpnGY5jfggJQ3EjKuu8D4bJRr0y+cYJFmYbImXGw==", "dev": true, "requires": { "os-tmpdir": "~1.0.2" diff --git a/package.json b/package.json index 93a4026a3b..e43726706a 100644 --- a/package.json +++ b/package.json @@ -82,7 +82,7 @@ "check-environment": "node scripts/check-environment.js" }, "dependencies": { - "bson": "^4.0.3", + "bson": "^4.2.0", "command-line-args": "^4.0.6", "deepmerge": "2.1.0", "deprecated-react-native-listview": "0.0.6", @@ -103,7 +103,6 @@ "url-parse": "^1.4.4" }, "devDependencies": { - "@types/bson": "^4.0.2", "@types/node": "^4.9.1", "@typescript-eslint/eslint-plugin": "^2.28.0", "@typescript-eslint/parser": "^2.28.0", diff --git a/packages/realm-web-integration-tests/src/bson.test.ts b/packages/realm-web-integration-tests/src/bson.test.ts new file mode 100644 index 0000000000..c6e6df249f --- /dev/null +++ b/packages/realm-web-integration-tests/src/bson.test.ts @@ -0,0 +1,41 @@ +//////////////////////////////////////////////////////////////////////////// +// +// Copyright 2020 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. +// +//////////////////////////////////////////////////////////////////////////// + +import { expect } from "chai"; +import { BSON } from "realm-web"; + +describe("BSON", () => { + it("gets exported", () => { + expect(typeof BSON).equals("object"); + }); + + it("can construct an ObjectId", () => { + const objectId = new BSON.ObjectId(); + expect(objectId).instanceOf(BSON.ObjectId); + expect(typeof objectId.toHexString()).equals("string"); + }); + + it("can parse EJSON", () => { + const result = BSON.EJSON.parse('{ "int32": { "$numberInt": "10" } }', { + relaxed: false, + }) as { int32: BSON.Int32 }; + expect(typeof result).equals("object"); + expect(typeof result.int32).equals("object"); + expect(result.int32).instanceOf(BSON.Int32); + }); +}); diff --git a/packages/realm-web-integration-tests/src/iife.test.ts b/packages/realm-web-integration-tests/src/iife.test.ts index b0d7e82448..4ab77f9259 100644 --- a/packages/realm-web-integration-tests/src/iife.test.ts +++ b/packages/realm-web-integration-tests/src/iife.test.ts @@ -82,4 +82,10 @@ describeIf(typeof window === "object", "IIFE bundle", () => { const response = await user.functions.translate("hello", "en_fr"); expect(response).to.equal("bonjour"); }); + + it("constructs a BSON.ObjectId", async () => { + const objectId = new Realm.BSON.ObjectId(); + expect(objectId).instanceOf(Realm.BSON.ObjectId); + expect(typeof objectId.toHexString()).equals("string"); + }); }); diff --git a/packages/realm-web-integration-tests/src/index.ts b/packages/realm-web-integration-tests/src/index.ts index 6d17d73f90..0225743edb 100644 --- a/packages/realm-web-integration-tests/src/index.ts +++ b/packages/realm-web-integration-tests/src/index.ts @@ -42,6 +42,7 @@ if (location.pathname.endsWith("-callback")) { require("./api-key-auth.test"); require("./email-password-auth.test"); require("./iife.test"); + require("./bson.test"); }, }); } diff --git a/packages/realm-web/CHANGELOG.md b/packages/realm-web/CHANGELOG.md index ab1659fe86..484c419b4d 100644 --- a/packages/realm-web/CHANGELOG.md +++ b/packages/realm-web/CHANGELOG.md @@ -7,6 +7,7 @@ ### Enhancements * Adding data from authentication providers, to be included in the `User#profile` object. ([#3268](https://github.com/realm/realm-js/issues/3268) via [#3481](https://github.com/realm/realm-js/pull/3481)) * Exposing the authentication provider used to authenticate a user as a `providerType` property on a `User` instance. ([#3481](https://github.com/realm/realm-js/pull/3481)) +* Added an export of the `bson` module on the package, making it possible to access the BSON types via `import Realm from "realm";` followed by `Realm.BSON.ObjectId`, `Realm.BSON.Decimal128`, `Realm.BSON.Binary` etc. ([#3363](https://github.com/realm/realm-js/pull/3363)) ### Fixed * Fixed an error in the types, where elements in the `User#identities` array would have a `userId` which was actually an `id` of the identity. ([#3481](https://github.com/realm/realm-js/pull/3481), since v0.9.0) @@ -15,17 +16,6 @@ * None 1.1.0 Release notes (2020-12-08) -============================================================= - -### Breaking Changes -* None - -### Enhancements -* Added support of OpenID Connect credential for the Google authentication provider. ([#3383](https://github.com/realm/realm-js/issues/3383)) - -### Fixed -* Fixed the type returned by `getApp` and `App.getApp`, such that an `app.currentUser` will match the `User` exported by the package. ([#3420](https://github.com/realm/realm-js/pull/3420), since v1.0.0-rc.1) - ### Internal * None diff --git a/packages/realm-web/package-lock.json b/packages/realm-web/package-lock.json index c9b88f7f46..53d6da256b 100644 --- a/packages/realm-web/package-lock.json +++ b/packages/realm-web/package-lock.json @@ -92,21 +92,6 @@ "picomatch": "^2.2.2" } }, - "@types/bson": { - "version": "4.0.2", - "resolved": "https://registry.npmjs.org/@types/bson/-/bson-4.0.2.tgz", - "integrity": "sha512-+uWmsejEHfmSjyyM/LkrP0orfE2m5Mx9Xel4tXNeqi1ldK5XMQcDsFkBmLDtuyKUbxj2jGDo0H240fbCRJZo7Q==", - "requires": { - "@types/node": "*" - }, - "dependencies": { - "@types/node": { - "version": "14.6.0", - "resolved": "https://registry.npmjs.org/@types/node/-/node-14.6.0.tgz", - "integrity": "sha512-mikldZQitV94akrc4sCcSjtJfsTKt4p+e/s0AGscVA6XArQ9kFclP+ZiYUMnq987rc6QlYxXv/EivqlfSLxpKA==" - } - } - }, "@types/chai": { "version": "4.2.12", "resolved": "https://registry.npmjs.org/@types/chai/-/chai-4.2.12.tgz", @@ -400,21 +385,20 @@ } }, "bson": { - "version": "4.0.0", - "resolved": "https://registry.npmjs.org/bson/-/bson-4.0.0.tgz", - "integrity": "sha512-303qcGsM1cosFi2xYlHViuGTHpC+gDf59aWcxB+/GZY4skzXG6ta2mw6OPlBOA4mtOyZJMvgp4IWdbmXs4tKpw==", + "version": "4.2.0", + "resolved": "https://registry.npmjs.org/bson/-/bson-4.2.0.tgz", + "integrity": "sha512-c3MlJqdROnCRvDr/+MLfaDvQ7CvGI4p1hKX45/fvgzSwKRdOjsfRug1NJJ8ty5mXCNtUdjJEWzoZWcBQxV4TyA==", "requires": { - "buffer": "^5.1.0", - "long": "^4.0.0" + "buffer": "^5.6.0" } }, "buffer": { - "version": "5.6.0", - "resolved": "https://registry.npmjs.org/buffer/-/buffer-5.6.0.tgz", - "integrity": "sha512-/gDYp/UtU0eA1ys8bOs9J6a+E/KWIY+DZ+Q2WESNUA0jFRsJOc0SNUO6xJ5SGA1xueg3NL65W6s+NY5l9cunuw==", + "version": "5.7.0", + "resolved": "https://registry.npmjs.org/buffer/-/buffer-5.7.0.tgz", + "integrity": "sha512-cd+5r1VLBwUqTrmnzW+D7ABkJUM6mr7uv1dv+6jRw4Rcl7tFIFHDqHPL98LhpGFn3dbAt3gtLxtrWp4m1kFrqg==", "requires": { - "base64-js": "^1.0.2", - "ieee754": "^1.1.4" + "base64-js": "^1.3.1", + "ieee754": "^1.1.13" } }, "buffer-es6": { @@ -1963,9 +1947,9 @@ "dev": true }, "ieee754": { - "version": "1.1.13", - "resolved": "https://registry.npmjs.org/ieee754/-/ieee754-1.1.13.tgz", - "integrity": "sha512-4vf7I2LYV/HaWerSo3XmlMkp5eZ83i+/CDluXi/IGTs/O1sejBNhTtnxzmRZfvOUqj7lZjqHkeTvpgSFDlWZTg==" + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/ieee754/-/ieee754-1.2.1.tgz", + "integrity": "sha512-dcyqhDvX1C46lXZcVqCpK+FtMRQVdIMN6/Df5js2zouUsqG7I6sFxitIC+7KYK29KdXOLHdu9zL4sFnoVQnqaA==" }, "indexof": { "version": "0.0.1", @@ -2241,11 +2225,6 @@ } } }, - "long": { - "version": "4.0.0", - "resolved": "https://registry.npmjs.org/long/-/long-4.0.0.tgz", - "integrity": "sha512-XsP+KhQif4bjX1kbuSiySJFNAehNxgLb6hPRGJ9QsUr8ajHkuXGdrHmFUTUUXhDwVX2R5bY4JNZEwbUiMhV+MA==" - }, "ltgt": { "version": "2.2.1", "resolved": "https://registry.npmjs.org/ltgt/-/ltgt-2.2.1.tgz", diff --git a/packages/realm-web/package.json b/packages/realm-web/package.json index f0d331d7c1..22642d73f6 100644 --- a/packages/realm-web/package.json +++ b/packages/realm-web/package.json @@ -45,8 +45,7 @@ }, "license": "SEE LICENSE IN LICENSE", "dependencies": { - "@types/bson": "^4.0.0", - "bson": "4.0.0", + "bson": "^4.2.0", "detect-browser": "^5.1.1", "js-base64": "^2.6.3" }, diff --git a/packages/realm-web/rollup.config.js b/packages/realm-web/rollup.config.js index 0dc3261bdb..5ef134cbe6 100644 --- a/packages/realm-web/rollup.config.js +++ b/packages/realm-web/rollup.config.js @@ -101,10 +101,7 @@ export default [ output: { file: "dist/bundle.d.ts", format: "es", - intro: [ - '/// ', - '/// ', - ].join("\n"), + intro: '/// ', }, plugins: [ dts({ @@ -113,5 +110,6 @@ export default [ }), nodeResolve(), ], + external: ["bson"], }, ]; diff --git a/types/bson.d.ts b/packages/realm-web/src/dom/detect-browser.d.ts similarity index 71% rename from types/bson.d.ts rename to packages/realm-web/src/dom/detect-browser.d.ts index 6e5087e540..47fa681e6f 100644 --- a/types/bson.d.ts +++ b/packages/realm-web/src/dom/detect-browser.d.ts @@ -16,10 +16,9 @@ // //////////////////////////////////////////////////////////////////////////// -declare namespace Realm { - // See https://stackoverflow.com/a/51114250 on why we're importing the BSON types like this - type ObjectId = import("bson").ObjectId; - type Binary = import("bson").Binary; - type Long = import("bson").Long; - type Timestamp = import("bson").Timestamp; +/** + * Used by the "detect-browser" package. + */ +declare namespace NodeJS { + type Platform = string; } diff --git a/packages/realm-web/src/dom/tsconfig.json b/packages/realm-web/src/dom/tsconfig.json index fa4e77f747..ecc8fbbea1 100644 --- a/packages/realm-web/src/dom/tsconfig.json +++ b/packages/realm-web/src/dom/tsconfig.json @@ -6,7 +6,7 @@ "realm-network-transport", "js-base64", "bson", - "simplified-node", + "buffer", "detect-browser" ], "lib": [ diff --git a/packages/realm-web/src/index.ts b/packages/realm-web/src/index.ts index 02691cbf39..609ea7f04a 100644 --- a/packages/realm-web/src/index.ts +++ b/packages/realm-web/src/index.ts @@ -16,6 +16,8 @@ // //////////////////////////////////////////////////////////////////////////// +export * as BSON from "bson"; + import { App } from "./App"; /** diff --git a/packages/realm-web/src/node/tsconfig.json b/packages/realm-web/src/node/tsconfig.json index 86d8280cd9..e41b865b51 100644 --- a/packages/realm-web/src/node/tsconfig.json +++ b/packages/realm-web/src/node/tsconfig.json @@ -6,6 +6,7 @@ "realm-network-transport", "js-base64", "bson", + "buffer", "node" ], "lib": [ diff --git a/packages/realm-web/types/simplified-node/index.d.ts b/packages/realm-web/src/tests/BSON.test.ts similarity index 59% rename from packages/realm-web/types/simplified-node/index.d.ts rename to packages/realm-web/src/tests/BSON.test.ts index 67bf769e87..144e20781f 100644 --- a/packages/realm-web/types/simplified-node/index.d.ts +++ b/packages/realm-web/src/tests/BSON.test.ts @@ -16,19 +16,12 @@ // //////////////////////////////////////////////////////////////////////////// -// Our version of Node.js types are very restricted -// This file will get resolved when realm's dependency on bson resolves a dependency on @types/node. -// We need this file to prevent the NodeJS globals to be accessable from within the test source files. +import { expect } from "chai"; +import { BSON } from ".."; -/** - * The simplest buffer we can come up with. - * NOTE: This is needed because the "bson" package's types depends on it. - */ -type Buffer = Uint8Array; - -/** - * Used by the "detect-browser" package. - */ -declare namespace NodeJS { - type Platform = string; -} +describe("BSON", () => { + it("expose an ObjectId constructor", () => { + const objectId = new BSON.ObjectId(); + expect(objectId).instanceOf(BSON.ObjectId); + }); +}); diff --git a/packages/realm-web/tsconfig.json b/packages/realm-web/tsconfig.json index 5f48a24123..b0d2c98b7c 100644 --- a/packages/realm-web/tsconfig.json +++ b/packages/realm-web/tsconfig.json @@ -14,7 +14,8 @@ "realm-network-transport", "realm", "js-base64", - "bson" + "bson", + "buffer" ], "lib": [ "es2019" @@ -22,11 +23,10 @@ }, "include": [ "types/realm/app.d.ts", - "types/realm/bson.d.ts", "types/realm/services.d.ts", "types/realm/auth-providers.d.ts", "types/environment.d.ts", - "src/**/*.ts" + "src/**/*.ts", ], "exclude": [ "src/dom/**/*.ts", diff --git a/src/js_types.hpp b/src/js_types.hpp index 3c7412b992..647288c056 100644 --- a/src/js_types.hpp +++ b/src/js_types.hpp @@ -540,7 +540,7 @@ inline typename T::Value Value::from_timestamp(typename T::Context ctx, Times template inline typename T::Object Object::create_bson_type(ContextType ctx, StringData type, std::initializer_list args) { auto realm = Value::validated_to_object(ctx, Object::get_global(ctx, "Realm")); - auto bson = Value::validated_to_object(ctx, Object::get_property(ctx, realm, "_bson")); + auto bson = Value::validated_to_object(ctx, Object::get_property(ctx, realm, "BSON")); auto ctor = Value::to_constructor(ctx, Object::get_property(ctx, bson, type)); return Function::construct(ctx, ctor, args); } @@ -695,7 +695,7 @@ inline bson::Bson Value::to_bson(typename T::Context ctx, ValueType value) { // For now going through the bson.EJSON.stringify() since it will correctly handle the special JS types. // Consider directly converting to Bson if we need more control or there are performance issues. auto realm = Value::validated_to_object(ctx, Object::get_global(ctx, "Realm")); - auto bson = Value::validated_to_object(ctx, Object::get_property(ctx, realm, "_bson")); + auto bson = Value::validated_to_object(ctx, Object::get_property(ctx, realm, "BSON")); auto ejson = Value::validated_to_object(ctx, Object::get_property(ctx, bson, "EJSON")); auto call_args_json = Object::call_method(ctx, ejson, "stringify", { value, diff --git a/tsconfig.json b/tsconfig.json index 3bf03c2d21..824f58135d 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -2,6 +2,7 @@ "compilerOptions": { "strict": true, "esModuleInterop": true, + "moduleResolution": "node", "forceConsistentCasingInFileNames": true, "target": "ES2018", }, diff --git a/types/index.d.ts b/types/index.d.ts index 073218f99f..1950431e9c 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -19,9 +19,10 @@ // TypeScript Version: 2.3.2 // With great contributions to @akim95 on github -/// /// +type ObjectId = import("bson").ObjectId; + declare namespace Realm { interface CollectionChangeSet { insertions: number[]; @@ -444,6 +445,8 @@ declare namespace Realm { */ const downloadBeforeOpenBehavior: OpenRealmBehaviorConfiguration; } + + const BSON: typeof import("bson"); } interface ProgressPromise extends Promise { @@ -581,14 +584,14 @@ declare class Realm { * @param {number|string|ObjectId} key * @returns {T | undefined} */ - objectForPrimaryKey(type: string, key: number | string | Realm.ObjectId): (T & Realm.Object) | undefined; + objectForPrimaryKey(type: string, key: number | string | ObjectId): (T & Realm.Object) | undefined; /** * @param {Class} type * @param {number|string|ObjectId} key * @returns {T | undefined} */ - objectForPrimaryKey(type: {new(...arg: any[]): T; }, key: number | string | Realm.ObjectId): T | undefined; + objectForPrimaryKey(type: {new(...arg: any[]): T; }, key: number | string | ObjectId): T | undefined; /** * @param {string} type diff --git a/types/services.d.ts b/types/services.d.ts index 68c2454850..74be637b34 100644 --- a/types/services.d.ts +++ b/types/services.d.ts @@ -16,6 +16,10 @@ // //////////////////////////////////////////////////////////////////////////// +type Binary = import("bson").Binary; +type Long = import("bson").Long; +type Timestamp = import("bson").Timestamp; + declare namespace Realm { /** * The MongoDB Realm Services bound to an app.