@brainfish-ai/web-tracker 0.0.4-alpha.8 → 0.0.4-aplha.19

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,54 @@
1
+ import { NameRedactor } from './built-ins/NameRedactor';
2
+ import * as simpleRegexpBuiltIns from './built-ins/simple-regexp-patterns';
3
+ import { SimpleRegexpRedactor } from './built-ins/SimpleRegexpRedactor';
4
+ import {
5
+ AsyncCustomRedactorConfig,
6
+ IAsyncRedactor,
7
+ CompositeRedactorOptions,
8
+ SyncCustomRedactorConfig,
9
+ ISyncRedactor,
10
+ } from './types';
11
+ import { isSimpleRegexpCustomRedactorConfig, snakeCase } from './utils';
12
+
13
+ function normalizeCustomRedactorConfig(redactorConfig: any) {
14
+ return isSimpleRegexpCustomRedactorConfig(redactorConfig)
15
+ ? new SimpleRegexpRedactor({
16
+ regexpPattern: redactorConfig.regexpPattern,
17
+ replaceWith: redactorConfig.replaceWith,
18
+ })
19
+ : redactorConfig;
20
+ }
21
+
22
+ export function composeChildRedactors<T extends AsyncCustomRedactorConfig>(opts: CompositeRedactorOptions<T> = {}) {
23
+ const childRedactors: T extends SyncCustomRedactorConfig
24
+ ? Array<ISyncRedactor>
25
+ : Array<IAsyncRedactor | ISyncRedactor> = [] as any;
26
+
27
+ if (opts.customRedactors && opts.customRedactors.before) {
28
+ opts.customRedactors.before.map(normalizeCustomRedactorConfig).forEach((redactor) => childRedactors.push(redactor));
29
+ }
30
+
31
+ for (const regexpName of Object.keys(simpleRegexpBuiltIns)) {
32
+ if (
33
+ !opts.builtInRedactors ||
34
+ !(opts.builtInRedactors as any)[regexpName] ||
35
+ (opts.builtInRedactors as any)[regexpName].enabled !== false
36
+ ) {
37
+ childRedactors.push(
38
+ new SimpleRegexpRedactor({
39
+ regexpPattern: (simpleRegexpBuiltIns as any)[regexpName],
40
+ replaceWith: opts.globalReplaceWith || snakeCase(regexpName).toUpperCase(),
41
+ })
42
+ );
43
+ }
44
+ }
45
+
46
+ if (!opts.builtInRedactors || !opts.builtInRedactors.names || opts.builtInRedactors.names.enabled !== false) {
47
+ childRedactors.push(new NameRedactor(opts.globalReplaceWith));
48
+ }
49
+
50
+ if (opts.customRedactors && opts.customRedactors.after) {
51
+ opts.customRedactors.after.map(normalizeCustomRedactorConfig).forEach((redactor) => childRedactors.push(redactor));
52
+ }
53
+ return childRedactors;
54
+ }
@@ -0,0 +1,21 @@
1
+ import { composeChildRedactors } from './composition';
2
+ import { CompositeRedactorOptions, ISyncRedactor, SyncCustomRedactorConfig } from './types';
3
+
4
+ /** @public */
5
+ export interface SyncCompositeRedactorOptions extends CompositeRedactorOptions<SyncCustomRedactorConfig> {}
6
+
7
+ /** @public */
8
+ export class SyncRedactor implements ISyncRedactor {
9
+ private childRedactors: ISyncRedactor[] = [];
10
+
11
+ constructor(opts?: SyncCompositeRedactorOptions) {
12
+ this.childRedactors = composeChildRedactors(opts);
13
+ }
14
+
15
+ redact = (textToRedact: string) => {
16
+ for (const redactor of this.childRedactors) {
17
+ textToRedact = redactor.redact(textToRedact);
18
+ }
19
+ return textToRedact;
20
+ };
21
+ }
@@ -0,0 +1,34 @@
1
+ import * as simpleRegexpBuiltIns from './built-ins/simple-regexp-patterns';
2
+
3
+ export interface ISyncRedactor {
4
+ redact(textToRedact: string): string;
5
+ }
6
+
7
+ export interface IAsyncRedactor {
8
+ redactAsync(textToRedact: string): Promise<string>;
9
+ }
10
+
11
+ export type IRedactor = ISyncRedactor | IAsyncRedactor;
12
+
13
+ export interface SimpleRegexpCustomRedactorConfig {
14
+ regexpPattern: RegExp;
15
+ replaceWith: string;
16
+ }
17
+
18
+ export type SyncCustomRedactorConfig = SimpleRegexpCustomRedactorConfig | ISyncRedactor;
19
+
20
+ export type AsyncCustomRedactorConfig = SyncCustomRedactorConfig | IAsyncRedactor;
21
+
22
+ export interface CompositeRedactorOptions<T extends AsyncCustomRedactorConfig> {
23
+ globalReplaceWith?: string;
24
+ builtInRedactors?: {
25
+ [RedactorName in keyof typeof simpleRegexpBuiltIns | 'names']?: {
26
+ enabled?: boolean;
27
+ replaceWith?: string;
28
+ };
29
+ };
30
+ customRedactors?: {
31
+ before?: Array<T>;
32
+ after?: Array<T>;
33
+ };
34
+ }
@@ -0,0 +1,15 @@
1
+ import { SimpleRegexpCustomRedactorConfig, AsyncCustomRedactorConfig, ISyncRedactor, IRedactor } from './types';
2
+
3
+ export function isSimpleRegexpCustomRedactorConfig(
4
+ redactor: AsyncCustomRedactorConfig
5
+ ): redactor is SimpleRegexpCustomRedactorConfig {
6
+ return typeof (redactor as SimpleRegexpCustomRedactorConfig).regexpPattern !== 'undefined';
7
+ }
8
+
9
+ export function isSyncRedactor(redactor: IRedactor): redactor is ISyncRedactor {
10
+ return typeof (redactor as ISyncRedactor).redact === 'function';
11
+ }
12
+
13
+ export function snakeCase(str: string) {
14
+ return str.replace(/[A-Z]/g, (letter, index) => (index !== 0 ? `_${letter.toLowerCase()}` : letter.toLowerCase()));
15
+ }
@@ -1,4 +1,4 @@
1
- import { SyncRedactor } from "redact-pii";
1
+ import { SyncRedactor } from "./../redact/index";
2
2
 
3
3
  const customRedactors = {
4
4
  before: [
package/src/tracker.ts CHANGED
@@ -2,7 +2,7 @@ import { Tracker } from './index';
2
2
 
3
3
  declare global {
4
4
  interface Window {
5
- tracker: {
5
+ BrainfishAnalytics: {
6
6
  q?: [string, ...any[]];
7
7
  (method: string, ...args: any[]): void;
8
8
  };
@@ -10,8 +10,8 @@ declare global {
10
10
  }
11
11
 
12
12
  ((window) => {
13
- if (window.tracker && 'q' in window.tracker) {
14
- const queue = window.tracker.q || [];
13
+ if (window.BrainfishAnalytics && 'q' in window.BrainfishAnalytics) {
14
+ const queue = window.BrainfishAnalytics.q || [];
15
15
  const tracker = new Tracker(queue.shift()[1]) as any;
16
16
  queue.forEach((item) => {
17
17
  if (item[0] in tracker) {
@@ -19,12 +19,12 @@ declare global {
19
19
  }
20
20
  });
21
21
 
22
- window.tracker = (t, ...args) => {
22
+ window.BrainfishAnalytics = (t, ...args) => {
23
23
  const fn = tracker[t] ? tracker[t].bind(tracker) : undefined;
24
24
  if (typeof fn === 'function') {
25
25
  fn(...args);
26
26
  } else {
27
- console.warn(`tracker.js: ${t} is not a function`);
27
+ console.warn(`Method ${t} does not exist on BrainfishAnalytics`);
28
28
  }
29
29
  };
30
30
  }
package/src/vite-env.d.ts CHANGED
@@ -1 +1,7 @@
1
- declare const __PACKAGE_VERSION__: string;
1
+ interface ImportMetaEnv {
2
+ readonly PACKAGE_VERSION: string;
3
+ }
4
+
5
+ interface ImportMeta {
6
+ readonly env: ImportMetaEnv;
7
+ }
package/tsconfig.json CHANGED
@@ -8,8 +8,9 @@
8
8
  "esModuleInterop": true,
9
9
  "types": ["vite/client", "jest"],
10
10
  "baseUrl": ".",
11
- "declaration": true
11
+ "declaration": true,
12
+ "resolveJsonModule": true
12
13
  },
13
- "include": ["src"],
14
+ "include": ["src", "./package.json"],
14
15
  "exclude": ["src/**/*.test.ts", "src/**/*.spec.ts", "src/**/tests/**"]
15
16
  }
package/vite.config.ts CHANGED
@@ -4,7 +4,6 @@ import dts from 'vite-plugin-dts';
4
4
  import compression from 'vite-plugin-compression';
5
5
  import { visualizer } from 'rollup-plugin-visualizer';
6
6
  import tsconfigPaths from 'vite-tsconfig-paths';
7
- import { replaceCodePlugin } from 'vite-plugin-replace';
8
7
  import { version } from './package.json';
9
8
 
10
9
  export default defineConfig({
@@ -17,24 +16,17 @@ export default defineConfig({
17
16
  }),
18
17
  compression(),
19
18
  visualizer(),
20
- replaceCodePlugin({
21
- replacements: [
22
- {
23
- from: "__PACKAGE_VERSION__",
24
- to: `"${version}"`,
25
- },
26
- ],
27
- }),
28
19
  ],
29
20
  build: {
30
- target: 'esnext',
31
- sourcemap: false,
21
+ target: 'es2015',
22
+ sourcemap: true,
32
23
  lib: {
33
- entry: ['index.ts', 'src/tracker.ts'],
34
- formats: ['es'],
35
- fileName: (format, entryName) => `${entryName}.js`,
24
+ entry: 'index.ts',
25
+ name: 'BrainfishAnalytics',
26
+ formats: ['es', 'cjs'],
27
+ fileName: (format, entry) => `${entry}${format === 'es' ? '' : '.cjs'}.js`,
36
28
  },
37
- minify: false,
29
+ minify: true,
38
30
  terserOptions: {
39
31
  compress: {
40
32
  drop_console: true,
@@ -45,15 +37,19 @@ export default defineConfig({
45
37
  treeshake: true,
46
38
  plugins: [terser()],
47
39
  external: ['@google-cloud/dlp'],
40
+ output: {
41
+ inlineDynamicImports: true, // Ensures all imports are inlined into the main bundle
42
+ manualChunks: undefined, // Disables code splitting
43
+ },
48
44
  },
49
45
  },
50
46
  test: {
51
47
  environment: 'jsdom',
52
48
  },
53
49
  define: {
54
- __PACKAGE_VERSION__: version,
50
+ 'import.meta.env.PACKAGE_VERSION': JSON.stringify(version),
55
51
  },
56
52
  optimizeDeps: {
57
53
  include: ['@brainfish-ai/tracker-sdk'],
58
- }
54
+ },
59
55
  });
File without changes
File without changes
File without changes
File without changes