@rpcbase/client 0.153.0 → 0.155.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,56 @@
1
+ /* @flow */
2
+ import {useEffect} from "react"
3
+ import {usePostHog} from "posthog-js/react"
4
+ import FingerprintJS from "@fingerprintjs/fingerprintjs"
5
+
6
+ import getUid from "../auth/getUid"
7
+
8
+ // TMP: RB_TENANT_ID must come from the server, not be set in env here
9
+ import {RB_TENANT_ID, POSTHOG_KEY} from "env"
10
+
11
+
12
+ const AnalyticsContainer = ({children}) => {
13
+ const {hostname} = window.location
14
+ if (__DEV__ || hostname === "localhost" || hostname.endsWith(".local")) return null
15
+ if (!POSTHOG_KEY) return null
16
+
17
+ const posthog = usePostHog()
18
+
19
+ const uid = getUid()
20
+
21
+ useEffect(() => {
22
+ if (!posthog) return
23
+
24
+ if (uid) {
25
+ posthog.identify(uid, {
26
+ uid,
27
+ env: __DEV__ ? "dev" : "prod",
28
+ })
29
+ posthog.group("customer", RB_TENANT_ID, {
30
+ name: "default",
31
+ })
32
+ posthog.group("env", __DEV__ ? "dev" : "prod")
33
+ }
34
+ // user isn't identified, use fingerprint instead
35
+ else {
36
+ const run = async() => {
37
+ const fp = await FingerprintJS.load()
38
+ const fpResult = await fp.get()
39
+
40
+ posthog.identify(fpResult.visitorId, {
41
+ type: "fingerprint",
42
+ env: __DEV__ ? "dev" : "prod",
43
+ })
44
+ }
45
+
46
+ run()
47
+ }
48
+ }, [posthog, uid])
49
+
50
+
51
+ return (
52
+ <>{children}</>
53
+ )
54
+ }
55
+
56
+ export default AnalyticsContainer
@@ -5,13 +5,15 @@ import {PostHogProvider} from "posthog-js/react"
5
5
 
6
6
  import {HashStateProvider} from "../hashState"
7
7
 
8
+ import AnalyticsContainer from "./AnalyticsContainer"
9
+
8
10
  // TODO: flags here should be optional
9
11
  import {flagValues} from "config/flags"
10
12
 
11
13
  import {POSTHOG_KEY} from "env"
12
14
 
13
15
 
14
- const AnalyticsWrapper = ({children, ...props}) => {
16
+ const PostHogWrapper = ({children, ...props}) => {
15
17
 
16
18
  const {hostname} = window.location
17
19
 
@@ -33,7 +35,9 @@ const AnalyticsWrapper = ({children, ...props}) => {
33
35
  },
34
36
  }}
35
37
  >
36
- {children}
38
+ <AnalyticsContainer>
39
+ {children}
40
+ </AnalyticsContainer>
37
41
  </PostHogProvider>
38
42
  )
39
43
  }
@@ -43,11 +47,11 @@ const AnalyticsWrapper = ({children, ...props}) => {
43
47
  const AppProvider = ({children, ...props}) => {
44
48
 
45
49
  return (
46
- <AnalyticsWrapper>
50
+ <PostHogWrapper>
47
51
  <HashStateProvider>
48
52
  {children}
49
53
  </HashStateProvider>
50
- </AnalyticsWrapper>
54
+ </PostHogWrapper>
51
55
  )
52
56
 
53
57
  }
package/get_object_id.js CHANGED
@@ -3,7 +3,7 @@ import assert from "assert"
3
3
 
4
4
  import isHexadecimal from "validator/lib/isHexadecimal"
5
5
 
6
- import {TENANT_PREFIX} from "env"
6
+ import {RB_TENANT_ID} from "env"
7
7
 
8
8
 
9
9
  const get_random_bytes = (n) => {
@@ -13,11 +13,11 @@ const get_random_bytes = (n) => {
13
13
  }
14
14
 
15
15
  // Validation: is defined
16
- assert(TENANT_PREFIX, "expected TENANT_PREFIX to be defined")
16
+ assert(RB_TENANT_ID, "expected RB_TENANT_ID to be defined")
17
17
  // is hexadecimal
18
- assert(isHexadecimal(TENANT_PREFIX), "expected TENANT_PREFIX to be a hexadecimal")
18
+ assert(isHexadecimal(RB_TENANT_ID), "expected RB_TENANT_ID to be a hexadecimal")
19
19
  // is 4 bytes
20
- assert(TENANT_PREFIX.length === 8, "TENANT_PREFIX must be exactly bytes long ie: 8 hex chars")
20
+ assert(RB_TENANT_ID.length === 8, "RB_TENANT_ID must be exactly bytes long ie: 8 hex chars")
21
21
 
22
22
  // generates a 12 bytes mongodb object id using the org id prefix or custom customer id
23
23
  const get_object_id = () => {
@@ -27,7 +27,7 @@ const get_object_id = () => {
27
27
  const random_bytes = get_random_bytes(4)
28
28
  const random_hex = Buffer.from(random_bytes).toString("hex")
29
29
 
30
- const obj_id = `${ts_bytes}${TENANT_PREFIX}${random_hex}`
30
+ const obj_id = `${ts_bytes}${RB_TENANT_ID}${random_hex}`
31
31
  return obj_id
32
32
  }
33
33
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rpcbase/client",
3
- "version": "0.153.0",
3
+ "version": "0.155.0",
4
4
  "scripts": {
5
5
  "test": "../../node_modules/.bin/wireit"
6
6
  },
@@ -4,11 +4,11 @@ import PouchDB from "pouchdb-core"
4
4
  import IndexedDBAdapter from "pouchdb-adapter-indexeddb"
5
5
  import FindPlugin from "pouchdb-find"
6
6
 
7
- import {TENANT_PREFIX, RB_APP_NAME} from "env"
7
+ import {RB_TENANT_ID, RB_APP_NAME} from "env"
8
8
 
9
9
  const log = debug("rb:rts:store")
10
10
 
11
- let prefix = `rb/${TENANT_PREFIX}/`
11
+ let prefix = `rb/${RB_TENANT_ID}/`
12
12
 
13
13
  if (RB_APP_NAME) prefix += `${RB_APP_NAME}/`
14
14
 
@@ -43,7 +43,7 @@ const run_query = async({model_name, query, query_key, options}, callback) => {
43
43
  // console.log("run_query", {model_name, query, query_key, options})
44
44
  // console.time("store run_query")
45
45
 
46
- // TODO: we should prefix model_name with tenant_prefix + env_id
46
+ // TODO: we should prefix model_name with RB_TENANT_ID + env_id
47
47
  const collection = get_collection(model_name)
48
48
 
49
49
  const replaced_query = replace_query_keys(query, (k) => (k.startsWith("_") && k !== "_id") ? `${UNDERSCORE_PREFIX}${k}` : k)