Skip to content

Commit

Permalink
Sync connection parameter bundleId (#5978)
Browse files Browse the repository at this point in the history
* Write bundle id to a write during installation and use it at runtime

---------
Co-authored-by: Andrew Meyer <[email protected]>
Co-authored-by: LJ <[email protected]>
  • Loading branch information
kneth authored Jul 14, 2023
1 parent 03c1887 commit aaf22f4
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 7 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,10 @@ Currently the following information is reported:
* If a JavaScript framework (currently React Native and Electron) is used and its version.
* Which JavaScript engine is being used.
* Node.js version number.
* An anonymous machine identifier and hashed application path to aggregate the other information on.
* TypeScript version if used.
* An anonymous machine identifier and hashed application name to aggregate the other information on.

Moreover, we unconditionally write various constants to a file which we might use at runtime.

## Code of Conduct

Expand Down
1 change: 1 addition & 0 deletions packages/realm/bindgen/js_opt_in_spec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ records:
- device_version
- framework_name
- framework_version
- bundle_id

AppConfig:
fields:
Expand Down
2 changes: 1 addition & 1 deletion packages/realm/bindgen/vendor/realm-core
Submodule realm-core updated 1 files
+1 −0 bindgen/spec.yml
4 changes: 3 additions & 1 deletion packages/realm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@
},
"./scripts/submit-analytics": "./scripts/submit-analytics.mjs",
"./react-native.config.js": "./react-native.config.js",
"./package.json": "./package.json"
"./package.json": "./package.json",
"./realm-constants.json": "./realm-constants.json"

This comment has been minimized.

Copy link
@kraenhansen

kraenhansen Aug 5, 2023

Member

I feel the "realm" prefix here is redundant, as it's already namespaced under node_modules/realm.

},
"files": [
"dependencies.list",
"dist",
"index.node.js",
"index.react-native.js",
Expand Down
3 changes: 3 additions & 0 deletions packages/realm/realm-constants.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"REALM_ANONYMIZED_BUNDLE_ID": "<undefined>"

This comment has been minimized.

Copy link
@kraenhansen

kraenhansen Aug 5, 2023

Member

I feel the "REALM_" prefix here is redundant - what other anonymized bundle ids would we expect to store here? Also - why not use undefined instead of a <undefined> string?

}
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"],
external: ["bson", "debug", "node-fetch", "node:module", "node:fs", "node:path", "realm/realm-constants.json"],
},
{
input: "src/platform/react-native/index.ts",
Expand All @@ -94,7 +94,7 @@ export default [
outputToFilesystem: true,
}),
],
external: ["bson", "debug", "react-native"],
external: ["bson", "debug", "react-native", "realm/realm-constants.json"],
},
{
input: "src/index.ts",
Expand All @@ -111,6 +111,6 @@ export default [
},
}),
],
external: ["bson"],
external: ["bson", "realm/realm-constants.json"],
},
];
15 changes: 14 additions & 1 deletion packages/realm/scripts/submit-analytics.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,17 @@ function getRealmCoreVersion() {
return dependenciesList.find((e) => e[0] === "REALM_CORE_VERSION")[1];
}

/**
* 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));
}

/**
* Determines if `npm` or `yarn` is used.
* @returns An array with two elements: method and version
Expand Down Expand Up @@ -183,6 +194,8 @@ async function collectPlatformData(packagePath = getProjectRoot()) {
if (packageJson.name) {
bundleId = packageJson["name"];
}
const anonymizedBundleId = sha256(bundleId);
saveBundleId(anonymizedBundleId);

if (packageJson.dependencies && packageJson.dependencies["react-native"]) {
framework = "react-native";
Expand Down Expand Up @@ -262,7 +275,7 @@ async function collectPlatformData(packagePath = getProjectRoot()) {
"JS Analytics Version": 3,
distinct_id: identifier,
"Anonymized Builder Id": sha256(identifier),
"Anonymized Bundle Id": sha256(bundleId),
"Anonymized Bundle Id": anonymizedBundleId,
"Realm Version": realmVersion,
Binding: "Javascript",
Version: packageJson.version,
Expand Down
4 changes: 4 additions & 0 deletions packages/realm/src/platform/node/device-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
////////////////////////////////////////////////////////////////////////////

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 { inject } from "../device-info";

Expand All @@ -38,6 +40,8 @@ inject({

frameworkName: typeof process.versions.electron === "string" ? "Electron" : "Node.js",
frameworkVersion: process.versions.electron || process.version,

bundleId: REALM_ANONYMIZED_BUNDLE_ID,
};
},
});
3 changes: 3 additions & 0 deletions packages/realm/src/platform/react-native/device-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import { Platform } from "react-native";

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

import { inject } from "../device-info";
import { JsPlatformHelpers } from "../../binding";
Expand Down Expand Up @@ -75,6 +76,8 @@ inject({

frameworkName: "react-native",
frameworkVersion: getReactNativeVersion(),

bundleId: REALM_ANONYMIZED_BUNDLE_ID,
};
},
});

0 comments on commit aaf22f4

Please sign in to comment.