Skip to content

Commit

Permalink
Fixed saving the anonymized bundle id (#6266)
Browse files Browse the repository at this point in the history
* Fixed storing analytics in package.json

* Update CHANGELOG.md
  • Loading branch information
kraenhansen authored Nov 23, 2023
1 parent de3062e commit 5c41fc2
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 27 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
* When place an embedded object would create a new object and keep the original object too. ([#6239](https://github.com/realm/realm-js/issues/6239), since v12.0.0)
* When setting an embedded object in a `Realm.List` by index, the new object would be inserted at the end rather than replacing the existing object at the given index. ([#6239](https://github.com/realm/realm-js/issues/6239), since v12.0.0)
* When `SyncConfiguration.clientReset` was `undefined`, no client reset mode was set which could lead to an app crash with the message `m_mode != ClientResyncMode::Manual`. The default mode is now `RecoverUnsyncedChanges` and no callbacks are defined. ([#6260](https://github.com/realm/realm-js/issues/6260), since v12.0.0)
* Fixed writing the `realm-constants.json` file used for analytics / telemetry, which used to cause errors such as `Unable to resolve module ../realm-constants.json` for users installing the package into a mono-repo. We're now storing this information in the `realm/package.json` file instead. ([#6144](https://github.com/realm/realm-js/issues/6144), since v12.0.0-rc.2)


### Compatibility
* React Native >= v0.71.4
Expand Down
8 changes: 5 additions & 3 deletions packages/realm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
"email": "[email protected]",
"url": "https://www.mongodb.com/docs/realm/"
},
"config": {
"anonymizedBundleId": "XTm00/yIvWo3KdyQ9XpE8JyUGe8sEekoBYSSPyvwdUg="
},
"types": "./types.d.cts",
"main": "./index.node.js",
"react-native": "./index.react-native.js",
Expand All @@ -42,8 +45,7 @@
},
"./scripts/submit-analytics": "./scripts/submit-analytics.mjs",
"./react-native.config.js": "./react-native.config.js",
"./package.json": "./package.json",
"./realm-constants.json": "./realm-constants.json"
"./package.json": "./package.json"
},
"files": [
"dependencies.list",
Expand Down Expand Up @@ -324,4 +326,4 @@
6
]
}
}
}
1 change: 0 additions & 1 deletion packages/realm/realm-constants.json

This file was deleted.

6 changes: 3 additions & 3 deletions packages/realm/rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export default [
outputToFilesystem: true,
}),
],
external: ["bson", "debug", "node-fetch", "node:module", "node:fs", "node:path", "realm/realm-constants.json"],
external: ["bson", "debug", "node-fetch", "node:module", "node:fs", "node:path"],
},
{
input: "src/platform/react-native/index.ts",
Expand All @@ -94,7 +94,7 @@ export default [
outputToFilesystem: true,
}),
],
external: ["bson", "debug", "react-native", "realm/realm-constants.json"],
external: ["bson", "debug", "react-native"],
},
{
input: "src/index.ts",
Expand All @@ -111,6 +111,6 @@ export default [
},
}),
],
external: ["bson", "realm/realm-constants.json"],
external: ["bson"],
},
];
40 changes: 26 additions & 14 deletions packages/realm/scripts/submit-analytics.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export { collectPlatformData };
// emulate old __dirname: https://flaviocopes.com/fix-dirname-not-defined-es-module-scope/
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const realmPackagePath = path.resolve(__dirname, "..");

/**
* Path and credentials required to submit analytics through the webhook.
Expand Down Expand Up @@ -102,10 +103,17 @@ function getProjectRoot() {
* Finds and read package.json
* @returns package.json as a JavaScript object
*/
function getPackageJson(packagePath) {
function readPackageJson(packagePath) {
const packageJsonPath = path.resolve(packagePath, "package.json");
const packageJson = fs.readFileSync(packageJsonPath, "utf-8");
return JSON.parse(packageJson);
return JSON.parse(fs.readFileSync(packageJsonPath, "utf-8"));
}

/**
* Finds and write package.json
*/
function writePackageJson(packagePath, content) {
const packageJsonPath = path.resolve(packagePath, "package.json");
fs.writeFileSync(packageJsonPath, JSON.stringify(content, null, 2), "utf-8");
}

/**
Expand All @@ -127,9 +135,7 @@ function isAnalyticsDisabled() {
}

function getRealmVersion() {
const packageJsonPath = path.resolve(__dirname, "..", "package.json");
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf-8"));
return packageJson["version"];
return readPackageJson(realmPackagePath)["version"];
}

/**
Expand All @@ -138,7 +144,7 @@ function getRealmVersion() {
* @returns the Realm Core version as a string
*/
function getRealmCoreVersion() {
const dependenciesListPath = path.resolve(__dirname, "../dependencies.list");
const dependenciesListPath = path.resolve(realmPackagePath, "dependencies.list");
const dependenciesList = fs
.readFileSync(dependenciesListPath)
.toString()
Expand All @@ -151,11 +157,13 @@ function getRealmCoreVersion() {
* Save the anonymized bundle ID for later usage at runtime.
*/
function saveBundleId(anonymizedBundleId) {
const localPath = path.resolve(path.join(getProjectRoot(), "node_modules", "realm"));
fs.mkdirSync(localPath, { recursive: true });
const realmConstantsFile = path.resolve(path.join(localPath, "realm-constants.json"));
const realmConstants = { REALM_ANONYMIZED_BUNDLE_ID: anonymizedBundleId };
fs.writeFileSync(realmConstantsFile, JSON.stringify(realmConstants));
const packageJson = readPackageJson(realmPackagePath);
// Initialize an object if it's missing
if (typeof packageJson.config !== "object") {
packageJson.config = {};
}
packageJson.config.anonymizedBundleId = anonymizedBundleId;
writePackageJson(realmPackagePath, packageJson);
}

/**
Expand All @@ -164,7 +172,11 @@ function saveBundleId(anonymizedBundleId) {
*/
function getInstallationMethod() {
const userAgent = process.env["npm_config_user_agent"];
return userAgent.split(" ")[0].split("/");
if (userAgent) {
return userAgent.split(" ")[0].split("/");
} else {
return "unknown";
}
}

/**
Expand All @@ -189,7 +201,7 @@ async function collectPlatformData(packagePath = getProjectRoot()) {
let jsEngine = "v8";
let bundleId = "unknown";

const packageJson = getPackageJson(packagePath);
const packageJson = readPackageJson(packagePath);

if (packageJson.name) {
bundleId = packageJson["name"];
Expand Down
5 changes: 2 additions & 3 deletions packages/realm/src/platform/node/device-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
import os from "node:os";
import process from "node:process";

import { version } from "realm/package.json";
import { REALM_ANONYMIZED_BUNDLE_ID } from "realm/realm-constants.json";
import { config, version } from "realm/package.json";

import { inject } from "../device-info";

Expand All @@ -41,7 +40,7 @@ inject({
frameworkName: typeof process.versions.electron === "string" ? "Electron" : "Node.js",
frameworkVersion: process.versions.electron || process.version,

bundleId: REALM_ANONYMIZED_BUNDLE_ID,
bundleId: config?.anonymizedBundleId || "unknown",
};
},
});
5 changes: 2 additions & 3 deletions packages/realm/src/platform/react-native/device-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@

import { Platform } from "react-native";

import { version } from "realm/package.json";
import { REALM_ANONYMIZED_BUNDLE_ID } from "realm/realm-constants.json";
import { config, version } from "realm/package.json";

import { inject } from "../device-info";
import { JsPlatformHelpers } from "../../binding";
Expand Down Expand Up @@ -77,7 +76,7 @@ inject({
frameworkName: "react-native",
frameworkVersion: getReactNativeVersion(),

bundleId: REALM_ANONYMIZED_BUNDLE_ID,
bundleId: config?.anonymizedBundleId || "unknown",
};
},
});

0 comments on commit 5c41fc2

Please sign in to comment.