@miradorlabs/parallax-web 1.0.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.
package/.nvmrc ADDED
@@ -0,0 +1 @@
1
+ 22.13.0
package/.prettierrc ADDED
@@ -0,0 +1,8 @@
1
+ {
2
+ "semi": true,
3
+ "trailingComma": "es5",
4
+ "singleQuote": true,
5
+ "printWidth": 100,
6
+ "tabWidth": 2,
7
+ "useTabs": false
8
+ }
package/README.md ADDED
@@ -0,0 +1,217 @@
1
+ # Parallax Web Client SDK
2
+
3
+ Web browser SDK for the Parallax tracing platform. This package provides a browser-compatible client using gRPC-Web to interact with the Parallax Gateway API.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @miradorlabs/parallax-web
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ### Basic Setup
14
+
15
+ ```typescript
16
+ import { ParallaxClient } from '@miradorlabs/parallax-web';
17
+
18
+ // Initialize the client with your API key
19
+ const client = new ParallaxClient('your-api-key');
20
+
21
+ // Or with a custom gateway URL
22
+ const client = new ParallaxClient('your-api-key', 'https://your-gateway.example.com:50053');
23
+ ```
24
+
25
+ ### Creating a Trace
26
+
27
+ ```typescript
28
+ const traceResponse = await client.createTrace({
29
+ name: 'My Application Trace',
30
+ attributes: {
31
+ 'project.id': 'my-project',
32
+ 'environment': 'production',
33
+ },
34
+ tags: ['web', 'user-action'],
35
+ });
36
+
37
+ const traceId = traceResponse.traceId;
38
+ ```
39
+
40
+ ### Starting a Span
41
+
42
+ ```typescript
43
+ const spanResponse = await client.startSpan({
44
+ name: 'User Login',
45
+ traceId: traceId,
46
+ attributes: {
47
+ 'user.id': 'user-123',
48
+ 'action': 'login',
49
+ },
50
+ });
51
+
52
+ const spanId = spanResponse.spanId;
53
+ ```
54
+
55
+ ### Adding Span Attributes
56
+
57
+ ```typescript
58
+ await client.addSpanAttributes({
59
+ traceId: traceId,
60
+ spanId: spanId,
61
+ attributes: {
62
+ 'response.status': '200',
63
+ 'response.time': '145ms',
64
+ },
65
+ });
66
+ ```
67
+
68
+ ### Adding Span Events
69
+
70
+ ```typescript
71
+ await client.addSpanEvent({
72
+ traceId: traceId,
73
+ spanId: spanId,
74
+ eventName: 'User Authenticated',
75
+ attributes: {
76
+ 'auth.method': 'oauth',
77
+ 'auth.provider': 'google',
78
+ },
79
+ });
80
+ ```
81
+
82
+ ### Adding Span Errors
83
+
84
+ ```typescript
85
+ await client.addSpanError({
86
+ traceId: traceId,
87
+ spanId: spanId,
88
+ errorType: 'ValidationError',
89
+ message: 'Invalid email format',
90
+ stackTrace: error.stack,
91
+ attributes: {
92
+ 'error.field': 'email',
93
+ },
94
+ });
95
+ ```
96
+
97
+ ### Adding Span Hints (Blockchain Transactions)
98
+
99
+ ```typescript
100
+ await client.addSpanHint({
101
+ traceId: traceId,
102
+ parentSpanId: spanId,
103
+ chainTransaction: {
104
+ txHash: '0x1234567890abcdef',
105
+ chainId: 1, // Ethereum mainnet
106
+ },
107
+ });
108
+ ```
109
+
110
+ ### Finishing a Span
111
+
112
+ ```typescript
113
+ await client.finishSpan({
114
+ traceId: traceId,
115
+ spanId: spanId,
116
+ });
117
+ ```
118
+
119
+ ## Complete Example
120
+
121
+ ```typescript
122
+ import { ParallaxClient } from '@miradorlabs/parallax-web';
123
+
124
+ async function trackUserAction() {
125
+ const client = new ParallaxClient('your-api-key');
126
+
127
+ try {
128
+ // Create trace
129
+ const { traceId } = await client.createTrace({
130
+ name: 'User Purchase Flow',
131
+ attributes: { 'user.id': 'user-123' },
132
+ tags: ['purchase', 'web'],
133
+ });
134
+
135
+ // Start span
136
+ const { spanId } = await client.startSpan({
137
+ name: 'Checkout Process',
138
+ traceId: traceId,
139
+ attributes: { 'cart.items': '3' },
140
+ });
141
+
142
+ // Add event
143
+ await client.addSpanEvent({
144
+ traceId: traceId,
145
+ spanId: spanId,
146
+ eventName: 'Payment Initiated',
147
+ attributes: { 'payment.method': 'card' },
148
+ });
149
+
150
+ // Finish span
151
+ await client.finishSpan({
152
+ traceId: traceId,
153
+ spanId: spanId,
154
+ });
155
+ } catch (error) {
156
+ console.error('Tracing error:', error);
157
+ }
158
+ }
159
+ ```
160
+
161
+ ## Browser Compatibility
162
+
163
+ This SDK uses the Fetch API and is compatible with modern browsers that support:
164
+ - ES2020
165
+ - Fetch API
166
+ - Promises
167
+ - Uint8Array
168
+
169
+ For older browsers, you may need polyfills.
170
+
171
+ ## Module Formats
172
+
173
+ The package provides multiple module formats:
174
+
175
+ - **ESM** (`dist/index.esm.js`): For modern bundlers (Webpack, Vite, etc.)
176
+ - **UMD** (`dist/index.umd.js`): For browser globals and older module systems
177
+ - **TypeScript** (`dist/index.d.ts`): Type definitions
178
+
179
+ ## Development
180
+
181
+ ### Building
182
+
183
+ ```bash
184
+ npm run build
185
+ ```
186
+
187
+ ### Testing
188
+
189
+ ```bash
190
+ npm test
191
+ npm run test:watch
192
+ npm run test:coverage
193
+ ```
194
+
195
+ ### Linting
196
+
197
+ ```bash
198
+ npm run lint
199
+ npm run format
200
+ ```
201
+
202
+ ## Differences from Node.js Client
203
+
204
+ The web client differs from `@miradorlabs/parallax` (Node.js) in the following ways:
205
+
206
+ - Uses **gRPC-Web** instead of gRPC (`@grpc/grpc-js`)
207
+ - Uses browser **Fetch API** instead of Node.js HTTP
208
+ - Uses **https** URLs instead of insecure connections
209
+ - Designed for browser environments (no Node.js dependencies)
210
+
211
+ ## License
212
+
213
+ ISC
214
+
215
+ ## Author
216
+
217
+ @mirador
package/SETUP.md ADDED
@@ -0,0 +1,220 @@
1
+ # Web Parallax Client Setup Summary
2
+
3
+ This document summarizes the complete configuration and structure of the web-parallax-client SDK.
4
+
5
+ ## Directory Structure
6
+
7
+ ```
8
+ web-parallax-client/
9
+ ├── dist/ # Build output (generated)
10
+ │ ├── index.esm.js # ES Module bundle
11
+ │ ├── index.esm.js.map # ESM source map
12
+ │ ├── index.umd.js # UMD bundle (browser global)
13
+ │ ├── index.umd.js.map # UMD source map
14
+ │ └── index.d.ts # TypeScript declarations
15
+ ├── src/
16
+ │ ├── grpc/
17
+ │ │ └── index.ts # GrpcWebRpc adapter (uses Fetch API)
18
+ │ ├── parallax/
19
+ │ │ └── index.ts # ParallaxClient main class
20
+ │ └── helpers/
21
+ │ └── index.ts # Serialization helpers
22
+ ├── tests/
23
+ │ └── parallax.test.ts # Jest unit tests
24
+ ├── index.ts # Main entry point
25
+ ├── package.json # NPM package configuration
26
+ ├── tsconfig.json # TypeScript config (dev)
27
+ ├── tsconfig.build.json # TypeScript config (build)
28
+ ├── rollup.config.mjs # Rollup bundler configuration
29
+ ├── jest.config.ts # Jest test configuration
30
+ ├── eslint.config.js # ESLint configuration
31
+ ├── .prettierrc # Prettier configuration
32
+ ├── .nvmrc # Node version (22.13.0)
33
+ ├── .gitignore # Git ignore rules
34
+ └── README.md # User documentation
35
+ ```
36
+
37
+ ## Key Configuration Files
38
+
39
+ ### package.json
40
+
41
+ - **Name**: `@miradorlabs/parallax-web`
42
+ - **Main Entry Points**:
43
+ - `main`: `dist/index.umd.js` (UMD for Node.js/CommonJS)
44
+ - `module`: `dist/index.esm.js` (ES Module for bundlers)
45
+ - `browser`: `dist/index.umd.js` (Browser global)
46
+ - `types`: `dist/index.d.ts` (TypeScript definitions)
47
+ - **Dependencies**:
48
+ - `google-protobuf`: ^4.0.1
49
+ - `mirador-gateway-parallax-web`: grpc-web package from GCS
50
+ - `rxjs`: ^7.8.2
51
+ - **Scripts**:
52
+ - `build`: Build all bundles with Rollup
53
+ - `test`: Run Jest tests
54
+ - `test:watch`: Run Jest in watch mode
55
+ - `test:coverage`: Generate coverage report
56
+
57
+ ### rollup.config.mjs
58
+
59
+ Generates three outputs:
60
+
61
+ 1. **ESM Bundle** (`dist/index.esm.js`)
62
+ - Format: ES Module
63
+ - For modern bundlers (Webpack, Vite, etc.)
64
+
65
+ 2. **UMD Bundle** (`dist/index.umd.js`)
66
+ - Format: UMD
67
+ - Global name: `ParallaxWeb`
68
+ - For browser `<script>` tags
69
+
70
+ 3. **Type Definitions** (`dist/index.d.ts`)
71
+ - TypeScript declarations
72
+
73
+ All bundles treat external dependencies as external:
74
+ - `google-protobuf`
75
+ - `mirador-gateway-parallax-web/*`
76
+ - `rxjs/*`
77
+
78
+ ### tsconfig.json
79
+
80
+ - Target: ES2020
81
+ - Module: ESNext (for browser)
82
+ - Lib: ES2020, DOM, DOM.Iterable
83
+ - Strict mode enabled
84
+ - Module resolution: bundler
85
+
86
+ ### jest.config.ts
87
+
88
+ - Preset: ts-jest
89
+ - Test environment: **jsdom** (browser simulation)
90
+ - Test match: `**/*.test.ts`
91
+ - Coverage from `src/**/*.ts`
92
+
93
+ ## Key Differences from nodejs-parallax-client
94
+
95
+ | Feature | nodejs-parallax-client | web-parallax-client |
96
+ |---------|------------------------|---------------------|
97
+ | gRPC Library | `@grpc/grpc-js` | Fetch API (gRPC-Web compatible) |
98
+ | Transport | Native gRPC | HTTP/HTTPS with gRPC-Web protocol |
99
+ | Environment | Node.js | Browser |
100
+ | Module System | CommonJS | ESM + UMD |
101
+ | Test Environment | Node | jsdom (browser) |
102
+ | Connection | Can use insecure | Always uses HTTPS |
103
+ | Default URL | localhost:50053 | https://gateway-parallax-dev... |
104
+
105
+ ## Source Files
106
+
107
+ ### src/grpc/index.ts - GrpcWebRpc
108
+
109
+ Implements the gRPC-Web RPC adapter using browser Fetch API:
110
+
111
+ - **Unary requests**: Standard HTTP POST with protobuf binary body
112
+ - **Server streaming**: Uses ReadableStream from Response.body
113
+ - **Headers**: Includes `Content-Type: application/grpc-web+proto`
114
+ - **API Key**: Sent as `x-api-key` header
115
+ - **Type casting**: `data.buffer as ArrayBuffer` for Fetch compatibility
116
+
117
+ ### src/parallax/index.ts - ParallaxClient
118
+
119
+ Main SDK client with methods:
120
+ - `createTrace()`: Create a new trace
121
+ - `startSpan()`: Start a span within a trace
122
+ - `finishSpan()`: Finish a span
123
+ - `addSpanAttributes()`: Add attributes to a span
124
+ - `addSpanEvent()`: Add an event to a span
125
+ - `addSpanError()`: Add an error to a span
126
+ - `addSpanHint()`: Add a blockchain transaction hint
127
+
128
+ ### tests/parallax.test.ts
129
+
130
+ Comprehensive test suite covering:
131
+ - Client instantiation
132
+ - All SDK methods (success and error cases)
133
+ - Integration scenarios
134
+ - API key handling
135
+ - Mock implementation of GrpcWebRpc
136
+
137
+ ## Build Process
138
+
139
+ ```bash
140
+ npm run build
141
+ ```
142
+
143
+ 1. Clears `dist/` directory
144
+ 2. Runs Rollup with three configurations in parallel:
145
+ - ESM bundle with source maps
146
+ - UMD bundle with source maps
147
+ - TypeScript declarations
148
+
149
+ Build warnings about `mirador-gateway-parallax-web` imports are **expected** - these are external dependencies resolved at runtime.
150
+
151
+ ## Testing
152
+
153
+ ```bash
154
+ npm test # Run all tests
155
+ npm run test:watch # Watch mode
156
+ npm run test:coverage # Generate coverage report
157
+ ```
158
+
159
+ Tests use:
160
+ - **jsdom**: Simulates browser environment
161
+ - **ts-jest**: TypeScript transformation
162
+ - **Mocking**: GrpcWebRpc is mocked for unit tests
163
+
164
+ ## Publishing
165
+
166
+ ```bash
167
+ npm run release:patch # Bump patch version (1.0.0 -> 1.0.1)
168
+ npm run release:minor # Bump minor version (1.0.0 -> 1.1.0)
169
+ npm run release:major # Bump major version (1.0.0 -> 2.0.0)
170
+ ```
171
+
172
+ Versioning automatically:
173
+ 1. Updates `package.json` version
174
+ 2. Creates git tag
175
+ 3. Pushes with `--follow-tags`
176
+
177
+ ## Browser Compatibility
178
+
179
+ Requires modern browsers supporting:
180
+ - ES2020 features
181
+ - Fetch API
182
+ - Promises
183
+ - ReadableStream
184
+ - Uint8Array
185
+
186
+ For older browsers, polyfills may be needed.
187
+
188
+ ## Usage in Browser
189
+
190
+ ### Via NPM/Bundler (Recommended)
191
+
192
+ ```typescript
193
+ import { ParallaxClient } from '@miradorlabs/parallax-web';
194
+
195
+ const client = new ParallaxClient('your-api-key');
196
+ ```
197
+
198
+ ### Via UMD Script Tag
199
+
200
+ ```html
201
+ <script src="node_modules/@miradorlabs/parallax-web/dist/index.umd.js"></script>
202
+ <script>
203
+ const client = new ParallaxWeb.ParallaxClient('your-api-key');
204
+ </script>
205
+ ```
206
+
207
+ ## Next Steps
208
+
209
+ 1. **Install dependencies**: Already done (`npm install`)
210
+ 2. **Build**: Already done (`npm run build`)
211
+ 3. **Test**: Run `npm test` to verify all tests pass
212
+ 4. **Customize**: Update `GRPC_GATEWAY_API_URL` in `src/parallax/index.ts` if needed
213
+ 5. **Publish**: When ready, use `npm run release:patch` to version and publish
214
+
215
+ ## Notes
216
+
217
+ - The package uses the grpc-web package from GCS: `mirador-gateway-parallax-grpc-web-1.0.9.tgz`
218
+ - Type warnings during build about missing type declarations are expected for external packages
219
+ - All gRPC-Web requests use the browser's Fetch API, making this SDK compatible with modern web applications
220
+ - The SDK maintains the same API surface as the Node.js client for consistency
@@ -0,0 +1,67 @@
1
+ import * as apiGateway from 'mirador-gateway-parallax-web/proto/gateway/parallax/v1/parallax_gateway';
2
+ import { CreateTraceRequest, StartSpanRequest, FinishSpanRequest, AddSpanAttributesRequest, AddSpanEventRequest, AddSpanErrorRequest, AddSpanHintRequest } from 'mirador-gateway-parallax-web/proto/gateway/parallax/v1/parallax_gateway';
3
+ import { Observable } from 'rxjs';
4
+
5
+ declare class ParallaxClient {
6
+ apiKey?: string | undefined;
7
+ apiUrl: string;
8
+ private apiGatewayRpc;
9
+ constructor(apiKey?: string | undefined, apiUrl?: string);
10
+ /**
11
+ * Create a new trace
12
+ * @param params Parameters to create a new trace
13
+ * @returns Response from the create trace operation
14
+ */
15
+ createTrace(params: CreateTraceRequest): Promise<apiGateway.CreateTraceResponse>;
16
+ /**
17
+ * Start a new span within a trace
18
+ * @param params Parameters to start a new span
19
+ */
20
+ startSpan(params: StartSpanRequest): Promise<any>;
21
+ /**
22
+ * Finish a span within a trace
23
+ * @param params Parameters to finish a span
24
+ */
25
+ finishSpan(params: FinishSpanRequest): Promise<any>;
26
+ /**
27
+ * Add attributes to a span
28
+ * @param params Parameters to add attributes to a span
29
+ */
30
+ addSpanAttributes(params: AddSpanAttributesRequest): Promise<any>;
31
+ /**
32
+ * Add an event to a span
33
+ * @param params Parameters to add an event to a span
34
+ */
35
+ addSpanEvent(params: AddSpanEventRequest): Promise<any>;
36
+ /**
37
+ * Add an error to a span
38
+ * @param params Parameters to add an error to a span
39
+ */
40
+ addSpanError(params: AddSpanErrorRequest): Promise<any>;
41
+ /**
42
+ * Add a hint to a span
43
+ * @param params Parameters to add a hint to a span
44
+ */
45
+ addSpanHint(params: AddSpanHintRequest): Promise<any>;
46
+ }
47
+
48
+ interface Metadata {
49
+ [key: string]: string;
50
+ }
51
+ interface Rpc {
52
+ request(service: string, method: string, data: Uint8Array, metadata?: Metadata): Promise<Uint8Array>;
53
+ clientStreamingRequest(service: string, method: string, data: Observable<Uint8Array>): Promise<Uint8Array>;
54
+ serverStreamingRequest(service: string, method: string, data: Uint8Array): Observable<Uint8Array>;
55
+ bidirectionalStreamingRequest(service: string, method: string, data: Observable<Uint8Array>): Observable<Uint8Array>;
56
+ }
57
+ declare class GrpcWebRpc implements Rpc {
58
+ private url;
59
+ private apiKey?;
60
+ constructor(url: string, apiKey?: string);
61
+ request(service: string, method: string, data: Uint8Array, metadata?: Metadata): Promise<Uint8Array>;
62
+ clientStreamingRequest(): Promise<Uint8Array>;
63
+ serverStreamingRequest(service: string, method: string, data: Uint8Array): Observable<Uint8Array>;
64
+ bidirectionalStreamingRequest(): Observable<Uint8Array>;
65
+ }
66
+
67
+ export { GrpcWebRpc, ParallaxClient };