@oxy-hq/sdk 0.1.4 → 0.1.6
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/README.md +52 -31
- package/dist/index.cjs +81 -49
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +17 -10
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +17 -10
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +81 -50
- package/dist/index.mjs.map +1 -1
- package/dist/{postMessage-CufWf9ji.cjs → postMessage-B1J0jDRN.cjs} +15 -11
- package/dist/postMessage-B1J0jDRN.cjs.map +1 -0
- package/dist/{postMessage-CVS3MsL5.cjs → postMessage-BSNS3ccd.cjs} +1 -1
- package/dist/{postMessage-DLGITn0e.mjs → postMessage-BxdgtX8j.mjs} +15 -11
- package/dist/postMessage-BxdgtX8j.mjs.map +1 -0
- package/dist/{postMessage-DwfY0HM5.mjs → postMessage-D5wWgwcO.mjs} +1 -1
- package/package.json +2 -2
- package/dist/postMessage-CufWf9ji.cjs.map +0 -1
- package/dist/postMessage-DLGITn0e.mjs.map +0 -1
package/dist/index.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.cts","names":[],"sources":["../src/config.ts","../src/types.ts","../src/client.ts","../src/parquet.ts","../src/auth/postMessage.ts"],"sourcesContent":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.cts","names":[],"sources":["../src/config.ts","../src/types.ts","../src/client.ts","../src/parquet.ts","../src/auth/postMessage.ts"],"sourcesContent":[],"mappings":";;;;;;;AAGiB,UAAA,SAAA,CAAS;EAsDV;;;EAA8C,OAAA,EAAA,MAAA;EAAS;AA6DvE;;EACc,MAAA,CAAA,EAAA,MAAA;EACH;;;;;;ACrHX;EAQiB,MAAA,CAAA,EAAA,MAAA;EAQA;AAMjB;AAKA;EAQiB,OAAA,CAAA,EAAA,MAAA;EAQA;AAQjB;AAgBA;AAaA;AAUA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@@ -1,4 +1,6 @@
|
|
|
1
1
|
|
|
2
|
+
import * as duckdb from "@duckdb/duckdb-wasm";
|
|
3
|
+
|
|
2
4
|
//#region src/config.d.ts
|
|
3
5
|
/**
|
|
4
6
|
* Configuration for the Oxy SDK
|
|
@@ -106,7 +108,7 @@ interface FileReference {
|
|
|
106
108
|
*/
|
|
107
109
|
interface TableData {
|
|
108
110
|
columns: string[];
|
|
109
|
-
rows:
|
|
111
|
+
rows: unknown[][];
|
|
110
112
|
total_rows?: number;
|
|
111
113
|
}
|
|
112
114
|
type DataContainer = Record<string, FileReference>;
|
|
@@ -129,7 +131,7 @@ interface DisplayWithError {
|
|
|
129
131
|
*/
|
|
130
132
|
interface DisplayData {
|
|
131
133
|
type: string;
|
|
132
|
-
content:
|
|
134
|
+
content: unknown;
|
|
133
135
|
}
|
|
134
136
|
/**
|
|
135
137
|
* Response from get displays endpoint
|
|
@@ -142,7 +144,7 @@ interface GetDisplaysResponse {
|
|
|
142
144
|
*/
|
|
143
145
|
interface QueryResult$1 {
|
|
144
146
|
columns: string[];
|
|
145
|
-
rows:
|
|
147
|
+
rows: unknown[][];
|
|
146
148
|
rowCount: number;
|
|
147
149
|
}
|
|
148
150
|
/**
|
|
@@ -152,8 +154,8 @@ interface QueryResult$1 {
|
|
|
152
154
|
* Request message sent from iframe to parent window
|
|
153
155
|
*/
|
|
154
156
|
interface OxyAuthRequestMessage {
|
|
155
|
-
type:
|
|
156
|
-
version:
|
|
157
|
+
type: "OXY_AUTH_REQUEST";
|
|
158
|
+
version: "1.0";
|
|
157
159
|
timestamp: number;
|
|
158
160
|
requestId: string;
|
|
159
161
|
}
|
|
@@ -161,8 +163,8 @@ interface OxyAuthRequestMessage {
|
|
|
161
163
|
* Response message sent from parent window to iframe
|
|
162
164
|
*/
|
|
163
165
|
interface OxyAuthResponseMessage {
|
|
164
|
-
type:
|
|
165
|
-
version:
|
|
166
|
+
type: "OXY_AUTH_RESPONSE";
|
|
167
|
+
version: "1.0";
|
|
166
168
|
requestId: string;
|
|
167
169
|
apiKey?: string;
|
|
168
170
|
projectId?: string;
|
|
@@ -186,7 +188,7 @@ interface PostMessageAuthResult {
|
|
|
186
188
|
apiKey?: string;
|
|
187
189
|
projectId?: string;
|
|
188
190
|
baseUrl?: string;
|
|
189
|
-
source:
|
|
191
|
+
source: "postmessage";
|
|
190
192
|
}
|
|
191
193
|
/**
|
|
192
194
|
* Custom error classes for postMessage authentication
|
|
@@ -388,12 +390,16 @@ declare class OxyClient {
|
|
|
388
390
|
}
|
|
389
391
|
//#endregion
|
|
390
392
|
//#region src/parquet.d.ts
|
|
393
|
+
/**
|
|
394
|
+
* Initialize DuckDB-WASM instance
|
|
395
|
+
*/
|
|
396
|
+
declare function initializeDuckDB(): Promise<duckdb.AsyncDuckDB>;
|
|
391
397
|
/**
|
|
392
398
|
* Query result interface
|
|
393
399
|
*/
|
|
394
400
|
interface QueryResult {
|
|
395
401
|
columns: string[];
|
|
396
|
-
rows:
|
|
402
|
+
rows: unknown[][];
|
|
397
403
|
rowCount: number;
|
|
398
404
|
}
|
|
399
405
|
/**
|
|
@@ -401,6 +407,7 @@ interface QueryResult {
|
|
|
401
407
|
*/
|
|
402
408
|
declare class ParquetReader {
|
|
403
409
|
private tableName;
|
|
410
|
+
private internalTableName;
|
|
404
411
|
private registered;
|
|
405
412
|
constructor(tableName?: string);
|
|
406
413
|
/**
|
|
@@ -535,5 +542,5 @@ declare function isInIframe(): boolean;
|
|
|
535
542
|
*/
|
|
536
543
|
declare function requestAuthFromParent(options?: PostMessageAuthOptions): Promise<PostMessageAuthResult>;
|
|
537
544
|
//#endregion
|
|
538
|
-
export { type AppDataResponse, type AppItem, type DataContainer, type DisplayData, type DisplayWithError, type FileReference, type OxyAuthRequestMessage, type OxyAuthResponseMessage, OxyClient, type OxyConfig, type QueryResult as ParquetQueryResult, ParquetReader, PostMessageAuthInvalidOriginError, PostMessageAuthInvalidResponseError, PostMessageAuthNotInIframeError, type PostMessageAuthOptions, type PostMessageAuthResult, PostMessageAuthTimeoutError, type QueryResult$1 as QueryResult, type TableData, createConfig, createConfigAsync, isInIframe, queryParquet, readParquet, requestAuthFromParent };
|
|
545
|
+
export { type AppDataResponse, type AppItem, type DataContainer, type DisplayData, type DisplayWithError, type FileReference, type OxyAuthRequestMessage, type OxyAuthResponseMessage, OxyClient, type OxyConfig, type QueryResult as ParquetQueryResult, ParquetReader, PostMessageAuthInvalidOriginError, PostMessageAuthInvalidResponseError, PostMessageAuthNotInIframeError, type PostMessageAuthOptions, type PostMessageAuthResult, PostMessageAuthTimeoutError, type QueryResult$1 as QueryResult, type TableData, createConfig, createConfigAsync, initializeDuckDB, isInIframe, queryParquet, readParquet, requestAuthFromParent };
|
|
539
546
|
//# sourceMappingURL=index.d.mts.map
|
package/dist/index.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/config.ts","../src/types.ts","../src/client.ts","../src/parquet.ts","../src/auth/postMessage.ts"],"sourcesContent":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/config.ts","../src/types.ts","../src/client.ts","../src/parquet.ts","../src/auth/postMessage.ts"],"sourcesContent":[],"mappings":";;;;;;;AAGiB,UAAA,SAAA,CAAS;EAsDV;;;EAA8C,OAAA,EAAA,MAAA;EAAS;AA6DvE;;EACc,MAAA,CAAA,EAAA,MAAA;EACH;;;;;;ACrHX;EAQiB,MAAA,CAAA,EAAA,MAAA;EAQA;AAMjB;AAKA;EAQiB,OAAA,CAAA,EAAA,MAAA;EAQA;AAQjB;AAgBA;AAaA;AAUA;AAYA;EAYiB,YAAA,CAAA,EAAA,MAAA;EAcJ;AAoBb;AAeA;AA0BA;;;;ACnLA;;;;;;;;;;;;AAoN8C,iBFxK9B,YAAA,CEwK8B,SAAA,CAAA,EFxKL,OEwKK,CFxKG,SEwKH,CAAA,CAAA,EFxKgB,SEwKhB;;;;;;;;;;ACtM9C;AA0CA;AASA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QAAA,iBAAO;EAQP,QAAA,UAAa;EAQb,WAAA,CAAS,SAAA,CAAA,EAAA,MAAA;EAMd;AAKZ;AAQA;AAQA;AAQA;AAgBA;AAaA;AAUA;AAYA;AAYA;AAcA;AAoBA;EAea,eAAA,CAAA,IAAA,EE7DiB,IF6DjB,CAAA,EE7DwB,OF6DQ,CAAA,IAAA,CAAA;EA0BhC;;;;ACnLb;;;;;;;;;EAqKqC,KAAA,CAAA,GAAA,EAAA,MAAA,CAAA,EC7BT,OD6BS,CC7BD,WD6BC,CAAA;EAoBI;;;;;;;;;;;;EC3KnB,MAAA,CAAA,KAAgB,CAAhB,EAAA,MAAgB,CAAA,EA6KN,OA7KkB,CA6KV,WA7KiB,CAAA;EA0CxC;AASjB;;;;;;;;;;;EAoKwB,SAAA,CAAA,CAAA,EAzBH,OAyBG,CAzBK,WAyBL,CAAA;EAwCF;;;;;AAkCtB;;;;;;WApFiB;;AC/MjB;AA+JA;EACW,KAAA,CAAA,CAAA,EDyDM,OCzDN,CAAA,IAAA,CAAA;;;;;;;;;;;;;;;;iBDiGW,YAAA,OACd,qBAEL,QAAQ;;;;;;;;;;;;;;;iBA+BW,WAAA,OACd,uBAEL,QAAQ;;;;;AHzMX;;;AAEW,iBI/FK,UAAA,CAAA,CJ+FL,EAAA,OAAA;ACrCX;AAUA;AAYA;AAYA;AAcA;AAoBA;AAeA;AA0BA;;;;ACnLA;;;;;;;;;;;AAyLiC,iBEdX,qBAAA,CFcW,OAAA,CAAA,EEbtB,sBFasB,CAAA,EEZ9B,OFY8B,CEZtB,qBFYsB,CAAA"}
|
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// @oxy/sdk - TypeScript SDK for Oxy data platform
|
|
2
|
-
import { a as PostMessageAuthInvalidOriginError, c as PostMessageAuthTimeoutError, n as isInIframe, o as PostMessageAuthInvalidResponseError, r as requestAuthFromParent, s as PostMessageAuthNotInIframeError } from "./postMessage-
|
|
2
|
+
import { a as PostMessageAuthInvalidOriginError, c as PostMessageAuthTimeoutError, n as isInIframe, o as PostMessageAuthInvalidResponseError, r as requestAuthFromParent, s as PostMessageAuthNotInIframeError } from "./postMessage-BxdgtX8j.mjs";
|
|
3
3
|
import * as duckdb from "@duckdb/duckdb-wasm";
|
|
4
4
|
|
|
5
5
|
//#region src/config.ts
|
|
@@ -66,25 +66,40 @@ function createConfig(overrides) {
|
|
|
66
66
|
* ```
|
|
67
67
|
*/
|
|
68
68
|
async function createConfigAsync(overrides) {
|
|
69
|
-
const { isInIframe: isInIframe$1
|
|
69
|
+
const { isInIframe: isInIframe$1 } = await import("./postMessage-D5wWgwcO.mjs");
|
|
70
70
|
let baseUrl = overrides?.baseUrl || process.env.OXY_URL;
|
|
71
71
|
let apiKey = overrides?.apiKey || process.env.OXY_API_KEY;
|
|
72
72
|
let projectId = overrides?.projectId || process.env.OXY_PROJECT_ID;
|
|
73
73
|
const disableAutoAuth = overrides?.disableAutoAuth ?? false;
|
|
74
74
|
const parentOrigin = overrides?.parentOrigin;
|
|
75
|
-
if (!disableAutoAuth && isInIframe$1() && !apiKey) if (!parentOrigin)
|
|
76
|
-
else
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
apiKey = authResult.apiKey;
|
|
82
|
-
if (authResult.projectId) projectId = authResult.projectId;
|
|
83
|
-
if (authResult.baseUrl) baseUrl = authResult.baseUrl;
|
|
84
|
-
console.log("[Oxy SDK] Successfully authenticated via postMessage");
|
|
85
|
-
} catch (error) {
|
|
75
|
+
if (!disableAutoAuth && isInIframe$1() && !apiKey) if (!parentOrigin) logWarningAboutMissingParentOrigin();
|
|
76
|
+
else apiKey = await attemptPostMessageAuth(parentOrigin, overrides?.timeout || 5e3, apiKey, projectId, baseUrl).then((result) => {
|
|
77
|
+
if (result.projectId) projectId = result.projectId;
|
|
78
|
+
if (result.baseUrl) baseUrl = result.baseUrl;
|
|
79
|
+
return result.apiKey;
|
|
80
|
+
}).catch((error) => {
|
|
86
81
|
console.error("[Oxy SDK] Failed to authenticate via postMessage:", error.message);
|
|
87
|
-
|
|
82
|
+
return apiKey;
|
|
83
|
+
});
|
|
84
|
+
return createFinalConfig(baseUrl, apiKey, projectId, overrides);
|
|
85
|
+
}
|
|
86
|
+
function logWarningAboutMissingParentOrigin() {
|
|
87
|
+
console.warn("[Oxy SDK] Running in iframe without API key and no parentOrigin specified. PostMessage authentication will be skipped. Provide parentOrigin config to enable automatic authentication.");
|
|
88
|
+
}
|
|
89
|
+
async function attemptPostMessageAuth(parentOrigin, timeout, currentApiKey, currentProjectId, currentBaseUrl) {
|
|
90
|
+
const { requestAuthFromParent: requestAuthFromParent$1 } = await import("./postMessage-D5wWgwcO.mjs");
|
|
91
|
+
const authResult = await requestAuthFromParent$1({
|
|
92
|
+
parentOrigin,
|
|
93
|
+
timeout
|
|
94
|
+
});
|
|
95
|
+
console.log("[Oxy SDK] Successfully authenticated via postMessage");
|
|
96
|
+
return {
|
|
97
|
+
apiKey: authResult.apiKey || currentApiKey,
|
|
98
|
+
projectId: authResult.projectId || currentProjectId,
|
|
99
|
+
baseUrl: authResult.baseUrl || currentBaseUrl
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
function createFinalConfig(baseUrl, apiKey, projectId, overrides) {
|
|
88
103
|
if (!baseUrl) throw new Error("OXY_URL environment variable or baseUrl config is required");
|
|
89
104
|
if (!projectId) throw new Error("OXY_PROJECT_ID environment variable or projectId config is required");
|
|
90
105
|
return {
|
|
@@ -93,8 +108,8 @@ async function createConfigAsync(overrides) {
|
|
|
93
108
|
projectId,
|
|
94
109
|
branch: overrides?.branch || process.env.OXY_BRANCH,
|
|
95
110
|
timeout: overrides?.timeout || 3e4,
|
|
96
|
-
parentOrigin,
|
|
97
|
-
disableAutoAuth
|
|
111
|
+
parentOrigin: overrides?.parentOrigin,
|
|
112
|
+
disableAutoAuth: overrides?.disableAutoAuth
|
|
98
113
|
};
|
|
99
114
|
}
|
|
100
115
|
|
|
@@ -102,6 +117,15 @@ async function createConfigAsync(overrides) {
|
|
|
102
117
|
//#region src/parquet.ts
|
|
103
118
|
let dbInstance = null;
|
|
104
119
|
let connection = null;
|
|
120
|
+
let operationQueue = Promise.resolve();
|
|
121
|
+
/**
|
|
122
|
+
* Enqueue an operation to prevent race conditions on shared DuckDB instance
|
|
123
|
+
*/
|
|
124
|
+
function enqueueOperation(operation) {
|
|
125
|
+
const currentOperation = operationQueue.then(operation, operation);
|
|
126
|
+
operationQueue = currentOperation.then(() => {}, () => {});
|
|
127
|
+
return currentOperation;
|
|
128
|
+
}
|
|
105
129
|
/**
|
|
106
130
|
* Initialize DuckDB-WASM instance
|
|
107
131
|
*/
|
|
@@ -132,6 +156,7 @@ var ParquetReader = class {
|
|
|
132
156
|
constructor(tableName = "data") {
|
|
133
157
|
this.registered = false;
|
|
134
158
|
this.tableName = tableName;
|
|
159
|
+
this.internalTableName = `${tableName}_${`${Date.now()}_${Math.random().toString(36).substring(2, 9)}`}`;
|
|
135
160
|
}
|
|
136
161
|
/**
|
|
137
162
|
* Register a Parquet file from a Blob
|
|
@@ -146,16 +171,18 @@ var ParquetReader = class {
|
|
|
146
171
|
* ```
|
|
147
172
|
*/
|
|
148
173
|
async registerParquet(blob) {
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
174
|
+
await enqueueOperation(async () => {
|
|
175
|
+
const conn = await getConnection();
|
|
176
|
+
const db = await initializeDuckDB();
|
|
177
|
+
const arrayBuffer = await blob.arrayBuffer();
|
|
178
|
+
const uint8Array = new Uint8Array(arrayBuffer);
|
|
179
|
+
await db.registerFileBuffer(`${this.internalTableName}.parquet`, uint8Array);
|
|
180
|
+
try {
|
|
181
|
+
await conn.query(`DROP TABLE IF EXISTS ${this.internalTableName}`);
|
|
182
|
+
} catch {}
|
|
183
|
+
await conn.query(`CREATE TABLE ${this.internalTableName} AS SELECT * FROM '${this.internalTableName}.parquet'`);
|
|
184
|
+
this.registered = true;
|
|
185
|
+
});
|
|
159
186
|
}
|
|
160
187
|
/**
|
|
161
188
|
* Execute a SQL query against the registered Parquet data
|
|
@@ -172,22 +199,26 @@ var ParquetReader = class {
|
|
|
172
199
|
*/
|
|
173
200
|
async query(sql) {
|
|
174
201
|
if (!this.registered) throw new Error("Parquet file not registered. Call registerParquet() first.");
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
const
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
row
|
|
202
|
+
return enqueueOperation(async () => {
|
|
203
|
+
const conn = await getConnection();
|
|
204
|
+
const rewrittenSql = sql.replace(new RegExp(`\\b${this.tableName}\\b`, "g"), this.internalTableName);
|
|
205
|
+
const result = await conn.query(rewrittenSql);
|
|
206
|
+
const columns = result.schema.fields.map((field) => field.name);
|
|
207
|
+
const rows = [];
|
|
208
|
+
for (let i = 0; i < result.numRows; i++) {
|
|
209
|
+
const row = [];
|
|
210
|
+
for (let j = 0; j < result.numCols; j++) {
|
|
211
|
+
const col = result.getChildAt(j);
|
|
212
|
+
row.push(col?.get(i));
|
|
213
|
+
}
|
|
214
|
+
rows.push(row);
|
|
183
215
|
}
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
};
|
|
216
|
+
return {
|
|
217
|
+
columns,
|
|
218
|
+
rows,
|
|
219
|
+
rowCount: result.numRows
|
|
220
|
+
};
|
|
221
|
+
});
|
|
191
222
|
}
|
|
192
223
|
/**
|
|
193
224
|
* Get all data from the registered table
|
|
@@ -238,17 +269,17 @@ var ParquetReader = class {
|
|
|
238
269
|
* Close and cleanup resources
|
|
239
270
|
*/
|
|
240
271
|
async close() {
|
|
241
|
-
if (this.registered) {
|
|
272
|
+
if (this.registered) await enqueueOperation(async () => {
|
|
242
273
|
const conn = await getConnection();
|
|
243
274
|
const db = await initializeDuckDB();
|
|
244
275
|
try {
|
|
245
|
-
await conn.query(`DROP TABLE IF EXISTS ${this.
|
|
246
|
-
} catch
|
|
276
|
+
await conn.query(`DROP TABLE IF EXISTS ${this.internalTableName}`);
|
|
277
|
+
} catch {}
|
|
247
278
|
try {
|
|
248
|
-
await db.dropFile(`${this.
|
|
249
|
-
} catch
|
|
279
|
+
await db.dropFile(`${this.internalTableName}.parquet`);
|
|
280
|
+
} catch {}
|
|
250
281
|
this.registered = false;
|
|
251
|
-
}
|
|
282
|
+
});
|
|
252
283
|
}
|
|
253
284
|
};
|
|
254
285
|
/**
|
|
@@ -370,7 +401,7 @@ var OxyClient = class OxyClient {
|
|
|
370
401
|
return response.json();
|
|
371
402
|
} catch (error) {
|
|
372
403
|
clearTimeout(timeoutId);
|
|
373
|
-
if (error.name === "AbortError") throw new Error(`Request timeout after ${this.config.timeout || 3e4}ms`);
|
|
404
|
+
if (error instanceof Error && error.name === "AbortError") throw new Error(`Request timeout after ${this.config.timeout || 3e4}ms`);
|
|
374
405
|
throw error;
|
|
375
406
|
}
|
|
376
407
|
}
|
|
@@ -491,7 +522,7 @@ var OxyClient = class OxyClient {
|
|
|
491
522
|
async getFile(filePath) {
|
|
492
523
|
const pathb64 = this.encodePathBase64(filePath);
|
|
493
524
|
const query = this.buildQueryParams();
|
|
494
|
-
return this.request(`/${this.config.projectId}/app/file/${pathb64}${query}`, { headers: {
|
|
525
|
+
return this.request(`/${this.config.projectId}/app/file/${pathb64}${query}`, { headers: { Accept: "application/octet-stream" } });
|
|
495
526
|
}
|
|
496
527
|
/**
|
|
497
528
|
* Gets a file URL for direct browser access
|
|
@@ -541,5 +572,5 @@ var OxyClient = class OxyClient {
|
|
|
541
572
|
};
|
|
542
573
|
|
|
543
574
|
//#endregion
|
|
544
|
-
export { OxyClient, ParquetReader, PostMessageAuthInvalidOriginError, PostMessageAuthInvalidResponseError, PostMessageAuthNotInIframeError, PostMessageAuthTimeoutError, createConfig, createConfigAsync, isInIframe, queryParquet, readParquet, requestAuthFromParent };
|
|
575
|
+
export { OxyClient, ParquetReader, PostMessageAuthInvalidOriginError, PostMessageAuthInvalidResponseError, PostMessageAuthNotInIframeError, PostMessageAuthTimeoutError, createConfig, createConfigAsync, initializeDuckDB, isInIframe, queryParquet, readParquet, requestAuthFromParent };
|
|
545
576
|
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","names":["isInIframe","requestAuthFromParent"],"sources":["../src/config.ts","../src/parquet.ts","../src/client.ts"],"sourcesContent":["/**\n * Configuration for the Oxy SDK\n */\nexport interface OxyConfig {\n /**\n * Base URL of the Oxy API (e.g., 'https://api.oxy.tech' or 'http://localhost:3000')\n */\n baseUrl: string;\n\n /**\n * API key for authentication (optional for local development)\n */\n apiKey?: string;\n\n /**\n * Project ID (UUID)\n */\n projectId: string;\n\n /**\n * Optional branch name (defaults to current branch if not specified)\n */\n branch?: string;\n\n /**\n * Request timeout in milliseconds (default: 30000)\n */\n timeout?: number;\n\n /**\n * Parent window origin for postMessage authentication (iframe scenarios)\n * Required when using postMessage auth for security.\n * Example: 'https://app.example.com'\n * Use '*' only in development!\n */\n parentOrigin?: string;\n\n /**\n * Disable automatic postMessage authentication even if in iframe\n * Set to true if you want to provide API key manually in iframe context\n */\n disableAutoAuth?: boolean;\n}\n\n/**\n * Creates an Oxy configuration from environment variables\n *\n * Environment variables:\n * - OXY_URL: Base URL of the Oxy API\n * - OXY_API_KEY: API key for authentication\n * - OXY_PROJECT_ID: Project ID (UUID)\n * - OXY_BRANCH: (Optional) Branch name\n *\n * @param overrides - Optional configuration overrides\n * @returns OxyConfig object\n * @throws Error if required environment variables are missing\n */\nexport function createConfig(overrides?: Partial<OxyConfig>): OxyConfig {\n const baseUrl = overrides?.baseUrl || process.env.OXY_URL;\n const apiKey = overrides?.apiKey || process.env.OXY_API_KEY;\n const projectId = overrides?.projectId || process.env.OXY_PROJECT_ID;\n\n if (!baseUrl) {\n throw new Error('OXY_URL environment variable or baseUrl config is required');\n }\n\n if (!projectId) {\n throw new Error('OXY_PROJECT_ID environment variable or projectId config is required');\n }\n\n return {\n baseUrl: baseUrl.replace(/\\/$/, ''), // Remove trailing slash\n apiKey,\n projectId,\n branch: overrides?.branch || process.env.OXY_BRANCH,\n timeout: overrides?.timeout || 30000,\n parentOrigin: overrides?.parentOrigin,\n disableAutoAuth: overrides?.disableAutoAuth,\n };\n}\n\n/**\n * Creates an Oxy configuration asynchronously with support for postMessage authentication\n *\n * This is the recommended method for iframe scenarios where authentication\n * needs to be obtained from the parent window via postMessage.\n *\n * When running in an iframe without an API key, this function will:\n * 1. Detect the iframe context\n * 2. Send an authentication request to the parent window\n * 3. Wait for the parent to respond with credentials\n * 4. Return the configured client\n *\n * Environment variables (fallback):\n * - OXY_URL: Base URL of the Oxy API\n * - OXY_API_KEY: API key for authentication\n * - OXY_PROJECT_ID: Project ID (UUID)\n * - OXY_BRANCH: (Optional) Branch name\n *\n * @param overrides - Optional configuration overrides\n * @returns Promise resolving to OxyConfig object\n * @throws Error if required configuration is missing\n * @throws PostMessageAuthTimeoutError if parent doesn't respond\n *\n * @example\n * ```typescript\n * // Automatic iframe detection and authentication\n * const config = await createConfigAsync({\n * parentOrigin: 'https://app.example.com',\n * projectId: 'my-project-id',\n * baseUrl: 'https://api.oxy.tech'\n * });\n * ```\n */\nexport async function createConfigAsync(\n overrides?: Partial<OxyConfig>\n): Promise<OxyConfig> {\n // Import postMessage utilities (dynamic to avoid circular deps)\n const { isInIframe, requestAuthFromParent } = await import('./auth/postMessage');\n\n // Start with environment variables and overrides\n let baseUrl = overrides?.baseUrl || process.env.OXY_URL;\n let apiKey = overrides?.apiKey || process.env.OXY_API_KEY;\n let projectId = overrides?.projectId || process.env.OXY_PROJECT_ID;\n\n const disableAutoAuth = overrides?.disableAutoAuth ?? false;\n const parentOrigin = overrides?.parentOrigin;\n\n // Automatic iframe detection and authentication\n if (!disableAutoAuth && isInIframe() && !apiKey) {\n if (!parentOrigin) {\n console.warn(\n '[Oxy SDK] Running in iframe without API key and no parentOrigin specified. ' +\n 'PostMessage authentication will be skipped. ' +\n 'Provide parentOrigin config to enable automatic authentication.'\n );\n } else {\n try {\n const authResult = await requestAuthFromParent({\n parentOrigin,\n timeout: overrides?.timeout || 5000,\n });\n\n apiKey = authResult.apiKey;\n if (authResult.projectId) {\n projectId = authResult.projectId;\n }\n if (authResult.baseUrl) {\n baseUrl = authResult.baseUrl;\n }\n\n console.log('[Oxy SDK] Successfully authenticated via postMessage');\n } catch (error) {\n console.error(\n '[Oxy SDK] Failed to authenticate via postMessage:',\n (error as Error).message\n );\n // Fall through to use environment variables if available\n }\n }\n }\n\n // Validation\n if (!baseUrl) {\n throw new Error('OXY_URL environment variable or baseUrl config is required');\n }\n\n if (!projectId) {\n throw new Error('OXY_PROJECT_ID environment variable or projectId config is required');\n }\n\n return {\n baseUrl: baseUrl.replace(/\\/$/, ''), // Remove trailing slash\n apiKey,\n projectId,\n branch: overrides?.branch || process.env.OXY_BRANCH,\n timeout: overrides?.timeout || 30000,\n parentOrigin,\n disableAutoAuth,\n };\n}\n","import * as duckdb from '@duckdb/duckdb-wasm';\n\nlet dbInstance: duckdb.AsyncDuckDB | null = null;\nlet connection: duckdb.AsyncDuckDBConnection | null = null;\n\n/**\n * Initialize DuckDB-WASM instance\n */\nasync function initializeDuckDB(): Promise<duckdb.AsyncDuckDB> {\n if (dbInstance) {\n return dbInstance;\n }\n\n const JSDELIVR_BUNDLES = duckdb.getJsDelivrBundles();\n\n // Select a bundle based on browser features\n const bundle = await duckdb.selectBundle(JSDELIVR_BUNDLES);\n\n const worker_url = URL.createObjectURL(\n new Blob([`importScripts(\"${bundle.mainWorker}\");`], {\n type: 'text/javascript',\n })\n );\n\n const worker = new Worker(worker_url);\n const logger = new duckdb.ConsoleLogger();\n\n dbInstance = new duckdb.AsyncDuckDB(logger, worker);\n await dbInstance.instantiate(bundle.mainModule, bundle.pthreadWorker);\n URL.revokeObjectURL(worker_url);\n\n return dbInstance;\n}\n\n/**\n * Get or create a DuckDB connection\n */\nasync function getConnection(): Promise<duckdb.AsyncDuckDBConnection> {\n if (connection) {\n return connection;\n }\n\n const db = await initializeDuckDB();\n connection = await db.connect();\n return connection;\n}\n\n/**\n * Query result interface\n */\nexport interface QueryResult {\n columns: string[];\n rows: any[][];\n rowCount: number;\n}\n\n/**\n * ParquetReader provides methods to read and query Parquet files\n */\nexport class ParquetReader {\n private tableName: string;\n private registered: boolean = false;\n\n constructor(tableName: string = 'data') {\n this.tableName = tableName;\n }\n\n /**\n * Register a Parquet file from a Blob\n *\n * @param blob - Parquet file as Blob\n *\n * @example\n * ```typescript\n * const blob = await client.getFile('data/sales.parquet');\n * const reader = new ParquetReader('sales');\n * await reader.registerParquet(blob);\n * ```\n */\n async registerParquet(blob: Blob): Promise<void> {\n const conn = await getConnection();\n const db = await initializeDuckDB();\n\n // Convert blob to Uint8Array\n const arrayBuffer = await blob.arrayBuffer();\n const uint8Array = new Uint8Array(arrayBuffer);\n\n // Register the file with DuckDB\n await db.registerFileBuffer(\n `${this.tableName}.parquet`,\n uint8Array\n );\n\n // Drop table if it exists\n try {\n await conn.query(`DROP TABLE IF EXISTS ${this.tableName}`);\n } catch (e) {\n // Ignore error if table doesn't exist\n }\n\n // Create table from parquet\n await conn.query(\n `CREATE TABLE ${this.tableName} AS SELECT * FROM '${this.tableName}.parquet'`\n );\n\n this.registered = true;\n }\n\n /**\n * Execute a SQL query against the registered Parquet data\n *\n * @param sql - SQL query string\n * @returns Query result with columns and rows\n *\n * @example\n * ```typescript\n * const result = await reader.query('SELECT * FROM sales LIMIT 10');\n * console.log(result.columns);\n * console.log(result.rows);\n * ```\n */\n async query(sql: string): Promise<QueryResult> {\n if (!this.registered) {\n throw new Error('Parquet file not registered. Call registerParquet() first.');\n }\n\n const conn = await getConnection();\n const result = await conn.query(sql);\n\n const columns = result.schema.fields.map((field) => field.name);\n const rows: any[][] = [];\n\n // Convert Arrow table to rows\n for (let i = 0; i < result.numRows; i++) {\n const row: any[] = [];\n for (let j = 0; j < result.numCols; j++) {\n const col = result.getChildAt(j);\n row.push(col?.get(i));\n }\n rows.push(row);\n }\n\n return {\n columns,\n rows,\n rowCount: result.numRows,\n };\n }\n\n /**\n * Get all data from the registered table\n *\n * @param limit - Maximum number of rows to return (default: all)\n * @returns Query result\n *\n * @example\n * ```typescript\n * const allData = await reader.getAll();\n * const first100 = await reader.getAll(100);\n * ```\n */\n async getAll(limit?: number): Promise<QueryResult> {\n const limitClause = limit ? ` LIMIT ${limit}` : '';\n return this.query(`SELECT * FROM ${this.tableName}${limitClause}`);\n }\n\n /**\n * Get table schema information\n *\n * @returns Schema information\n *\n * @example\n * ```typescript\n * const schema = await reader.getSchema();\n * console.log(schema.columns); // ['id', 'name', 'sales']\n * console.log(schema.rows); // [['id', 'INTEGER'], ['name', 'VARCHAR'], ...]\n * ```\n */\n async getSchema(): Promise<QueryResult> {\n return this.query(`DESCRIBE ${this.tableName}`);\n }\n\n /**\n * Get row count\n *\n * @returns Number of rows in the table\n *\n * @example\n * ```typescript\n * const count = await reader.count();\n * console.log(`Total rows: ${count}`);\n * ```\n */\n async count(): Promise<number> {\n const result = await this.query(`SELECT COUNT(*) as count FROM ${this.tableName}`);\n return result.rows[0][0];\n }\n\n /**\n * Close and cleanup resources\n */\n async close(): Promise<void> {\n if (this.registered) {\n const conn = await getConnection();\n const db = await initializeDuckDB();\n\n // Drop the table\n try {\n await conn.query(`DROP TABLE IF EXISTS ${this.tableName}`);\n } catch (e) {\n // Ignore error\n }\n\n // Drop the registered file buffer\n try {\n await db.dropFile(`${this.tableName}.parquet`);\n } catch (e) {\n // Ignore error if file doesn't exist\n }\n\n this.registered = false;\n }\n }\n}\n\n/**\n * Helper function to quickly read a Parquet blob and execute a query\n *\n * @param blob - Parquet file as Blob\n * @param sql - SQL query to execute (optional, defaults to SELECT *)\n * @returns Query result\n *\n * @example\n * ```typescript\n * const blob = await client.getFile('data/sales.parquet');\n * const result = await queryParquet(blob, 'SELECT product, SUM(amount) as total FROM data GROUP BY product');\n * console.log(result);\n * ```\n */\nexport async function queryParquet(\n blob: Blob,\n sql?: string\n): Promise<QueryResult> {\n // Generate unique table name to avoid conflicts\n const uniqueId = `temp_${Date.now()}_${Math.random().toString(36).substring(2, 9)}`;\n const reader = new ParquetReader(uniqueId);\n\n await reader.registerParquet(blob);\n\n const query = sql || `SELECT * FROM ${uniqueId}`;\n const result = await reader.query(query);\n\n await reader.close();\n return result;\n}\n\n/**\n * Helper function to read Parquet file and get all data\n *\n * @param blob - Parquet file as Blob\n * @param limit - Maximum number of rows (optional)\n * @returns Query result\n *\n * @example\n * ```typescript\n * const blob = await client.getFile('data/sales.parquet');\n * const data = await readParquet(blob, 1000);\n * console.log(`Loaded ${data.rowCount} rows`);\n * ```\n */\nexport async function readParquet(\n blob: Blob,\n limit?: number\n): Promise<QueryResult> {\n // Generate unique table name to avoid conflicts\n const uniqueId = `temp_${Date.now()}_${Math.random().toString(36).substring(2, 9)}`;\n const reader = new ParquetReader(uniqueId);\n\n await reader.registerParquet(blob);\n\n const result = await reader.getAll(limit);\n\n await reader.close();\n return result;\n}\n","import { OxyConfig, createConfigAsync } from './config';\nimport {\n AppItem,\n AppDataResponse,\n GetDisplaysResponse,\n ApiError,\n TableData,\n} from './types';\nimport { readParquet } from './parquet';\n\n/**\n * Oxy API Client for interacting with Oxy data\n */\nexport class OxyClient {\n private config: OxyConfig;\n\n constructor(config: OxyConfig) {\n this.config = config;\n }\n\n /**\n * Creates an OxyClient instance asynchronously with support for postMessage authentication\n *\n * This is the recommended method when using the SDK in an iframe that needs to\n * obtain authentication from the parent window via postMessage.\n *\n * @param config - Optional configuration overrides\n * @returns Promise resolving to OxyClient instance\n * @throws Error if required configuration is missing\n * @throws PostMessageAuthTimeoutError if parent doesn't respond\n *\n * @example\n * ```typescript\n * // In an iframe - automatic postMessage auth\n * const client = await OxyClient.create({\n * parentOrigin: 'https://app.example.com',\n * projectId: 'my-project-id',\n * baseUrl: 'https://api.oxy.tech'\n * });\n *\n * // Use the client normally\n * const apps = await client.listApps();\n * ```\n */\n static async create(config?: Partial<OxyConfig>): Promise<OxyClient> {\n const resolvedConfig = await createConfigAsync(config);\n return new OxyClient(resolvedConfig);\n }\n\n /**\n * Encodes a file path to base64 for use in API URLs\n */\n private encodePathBase64(path: string): string {\n if (typeof Buffer !== 'undefined') {\n // Node.js environment\n return Buffer.from(path).toString('base64');\n } else {\n // Browser environment\n return btoa(path);\n }\n }\n\n /**\n * Makes an authenticated HTTP request to the Oxy API\n */\n private async request<T>(\n endpoint: string,\n options: RequestInit = {}\n ): Promise<T> {\n const url = `${this.config.baseUrl}${endpoint}`;\n\n const headers: Record<string, string> = {\n 'Content-Type': 'application/json',\n ...((options.headers as Record<string, string>) || {}),\n };\n\n // Only add Authorization header if API key is provided (optional for local dev)\n if (this.config.apiKey) {\n headers['Authorization'] = `Bearer ${this.config.apiKey}`;\n }\n\n const controller = new AbortController();\n const timeoutId = setTimeout(() => controller.abort(), this.config.timeout || 30000);\n\n try {\n const response = await fetch(url, {\n ...options,\n headers,\n signal: controller.signal,\n });\n\n clearTimeout(timeoutId);\n\n if (!response.ok) {\n const errorText = await response.text().catch(() => 'Unknown error');\n const error: ApiError = {\n message: `API request failed: ${response.statusText}`,\n status: response.status,\n details: errorText,\n };\n throw error;\n }\n\n // Handle binary responses\n const acceptHeader =\n typeof options.headers === 'object' && options.headers !== null\n ? (options.headers as Record<string, string>)['Accept']\n : undefined;\n if (acceptHeader === 'application/octet-stream') {\n return response.blob() as Promise<T>;\n }\n\n return response.json();\n } catch (error: any) {\n clearTimeout(timeoutId);\n\n if (error.name === 'AbortError') {\n throw new Error(`Request timeout after ${this.config.timeout || 30000}ms`);\n }\n\n throw error;\n }\n }\n\n /**\n * Builds query parameters including optional branch\n */\n private buildQueryParams(additionalParams: Record<string, string> = {}): string {\n const params: Record<string, string> = { ...additionalParams };\n\n if (this.config.branch) {\n params.branch = this.config.branch;\n }\n\n const searchParams = new URLSearchParams(params);\n const queryString = searchParams.toString();\n return queryString ? `?${queryString}` : '';\n }\n\n /**\n * Lists all apps in the project\n *\n * @returns Array of app items\n *\n * @example\n * ```typescript\n * const apps = await client.listApps();\n * console.log('Available apps:', apps);\n * ```\n */\n async listApps(): Promise<AppItem[]> {\n const query = this.buildQueryParams();\n return this.request<AppItem[]>(`/${this.config.projectId}/app${query}`);\n }\n\n /**\n * Gets data for a specific app\n *\n * @param appPath - Relative path to the app file (e.g., 'my-app.app.yml')\n * @returns App data response\n *\n * @example\n * ```typescript\n * const data = await client.getAppData('dashboard.app.yml');\n * if (data.error) {\n * console.error('Error:', data.error);\n * } else {\n * console.log('App data:', data.data);\n * }\n * ```\n */\n async getAppData(appPath: string): Promise<AppDataResponse> {\n const pathb64 = this.encodePathBase64(appPath);\n const query = this.buildQueryParams();\n return this.request<AppDataResponse>(\n `/${this.config.projectId}/app/${pathb64}${query}`\n );\n }\n\n /**\n * Runs an app and returns fresh data (bypasses cache)\n *\n * @param appPath - Relative path to the app file\n * @returns App data response\n *\n * @example\n * ```typescript\n * const data = await client.runApp('dashboard.app.yml');\n * console.log('Fresh app data:', data.data);\n * ```\n */\n async runApp(appPath: string): Promise<AppDataResponse> {\n const pathb64 = this.encodePathBase64(appPath);\n const query = this.buildQueryParams();\n return this.request<AppDataResponse>(\n `/${this.config.projectId}/app/${pathb64}/run${query}`,\n { method: 'POST' }\n );\n }\n\n /**\n * Gets display configurations for an app\n *\n * @param appPath - Relative path to the app file\n * @returns Display configurations with potential errors\n *\n * @example\n * ```typescript\n * const displays = await client.getDisplays('dashboard.app.yml');\n * displays.displays.forEach(d => {\n * if (d.error) {\n * console.error('Display error:', d.error);\n * } else {\n * console.log('Display:', d.display);\n * }\n * });\n * ```\n */\n async getDisplays(appPath: string): Promise<GetDisplaysResponse> {\n const pathb64 = this.encodePathBase64(appPath);\n const query = this.buildQueryParams();\n return this.request<GetDisplaysResponse>(\n `/${this.config.projectId}/app/${pathb64}/displays${query}`\n );\n }\n\n /**\n * Gets a file from the app state directory (e.g., generated charts, images)\n *\n * This is useful for retrieving generated assets like charts, images, or other\n * files produced by app workflows and stored in the state directory.\n *\n * @param filePath - Relative path to the file in state directory\n * @returns Blob containing the file data\n *\n * @example\n * ```typescript\n * // Get a generated chart image\n * const blob = await client.getFile('charts/sales-chart.png');\n * const imageUrl = URL.createObjectURL(blob);\n *\n * // Use in an img tag\n * document.querySelector('img').src = imageUrl;\n * ```\n *\n * @example\n * ```typescript\n * // Download a file\n * const blob = await client.getFile('exports/data.csv');\n * const a = document.createElement('a');\n * a.href = URL.createObjectURL(blob);\n * a.download = 'data.csv';\n * a.click();\n * ```\n */\n async getFile(filePath: string): Promise<Blob> {\n const pathb64 = this.encodePathBase64(filePath);\n const query = this.buildQueryParams();\n return this.request<Blob>(\n `/${this.config.projectId}/app/file/${pathb64}${query}`,\n {\n headers: {\n 'Accept': 'application/octet-stream',\n },\n }\n );\n }\n\n /**\n * Gets a file URL for direct browser access\n *\n * This returns a URL that can be used directly in img tags, fetch calls, etc.\n * The URL includes authentication via query parameters.\n *\n * @param filePath - Relative path to the file in state directory\n * @returns Full URL to the file\n *\n * @example\n * ```typescript\n * const imageUrl = client.getFileUrl('charts/sales-chart.png');\n *\n * // Use directly in img tag (in environments where query-based auth is supported)\n * document.querySelector('img').src = imageUrl;\n * ```\n */\n getFileUrl(filePath: string): string {\n const pathb64 = this.encodePathBase64(filePath);\n const query = this.buildQueryParams();\n return `${this.config.baseUrl}/${this.config.projectId}/app/file/${pathb64}${query}`;\n }\n\n /**\n * Fetches a parquet file and parses it into table data\n *\n * @param filePath - Relative path to the parquet file\n * @param limit - Maximum number of rows to return (default: 100)\n * @returns TableData with columns and rows\n *\n * @example\n * ```typescript\n * const tableData = await client.getTableData('data/sales.parquet', 50);\n * console.log(tableData.columns);\n * console.log(tableData.rows);\n * console.log(`Total rows: ${tableData.total_rows}`);\n * ```\n */\n async getTableData(filePath: string, limit: number = 100): Promise<TableData> {\n const blob = await this.getFile(filePath);\n const result = await readParquet(blob, limit);\n\n return {\n columns: result.columns,\n rows: result.rows,\n total_rows: result.rowCount,\n };\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAyDA,SAAgB,aAAa,WAA2C;CACtE,MAAM,UAAU,WAAW,WAAW,QAAQ,IAAI;CAClD,MAAM,SAAS,WAAW,UAAU,QAAQ,IAAI;CAChD,MAAM,YAAY,WAAW,aAAa,QAAQ,IAAI;AAEtD,KAAI,CAAC,QACH,OAAM,IAAI,MAAM,6DAA6D;AAG/E,KAAI,CAAC,UACH,OAAM,IAAI,MAAM,sEAAsE;AAGxF,QAAO;EACL,SAAS,QAAQ,QAAQ,OAAO,GAAG;EACnC;EACA;EACA,QAAQ,WAAW,UAAU,QAAQ,IAAI;EACzC,SAAS,WAAW,WAAW;EAC/B,cAAc,WAAW;EACzB,iBAAiB,WAAW;EAC7B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAoCH,eAAsB,kBACpB,WACoB;CAEpB,MAAM,EAAE,0BAAY,mDAA0B,MAAM,OAAO;CAG3D,IAAI,UAAU,WAAW,WAAW,QAAQ,IAAI;CAChD,IAAI,SAAS,WAAW,UAAU,QAAQ,IAAI;CAC9C,IAAI,YAAY,WAAW,aAAa,QAAQ,IAAI;CAEpD,MAAM,kBAAkB,WAAW,mBAAmB;CACtD,MAAM,eAAe,WAAW;AAGhC,KAAI,CAAC,mBAAmBA,cAAY,IAAI,CAAC,OACvC,KAAI,CAAC,aACH,SAAQ,KACN,yLAGD;KAED,KAAI;EACF,MAAM,aAAa,MAAMC,wBAAsB;GAC7C;GACA,SAAS,WAAW,WAAW;GAChC,CAAC;AAEF,WAAS,WAAW;AACpB,MAAI,WAAW,UACb,aAAY,WAAW;AAEzB,MAAI,WAAW,QACb,WAAU,WAAW;AAGvB,UAAQ,IAAI,uDAAuD;UAC5D,OAAO;AACd,UAAQ,MACN,qDACC,MAAg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ACV,IAAI,KAAK,OAAO,UAAU,OAAO,QAAQ,MAAM,SAC/C,EAAE,QAAQ,QAAQ,CACnB;;;;;;;;;;;;;;;;;;;;CAqBH,MAAM,YAAY,SAA+C;EAC/D,MAAM,UAAU,KAAK,iBAAiB,QAAQ;EAC9C,MAAM,QAAQ,KAAK,kBAAkB;AACrC,SAAO,KAAK,QACV,IAAI,KAAK,OAAO,UAAU,OAAO,QAAQ,WAAW,QACrD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAgCH,MAAM,QAAQ,UAAiC;EAC7C,MAAM,UAAU,KAAK,iBAAiB,SAAS;EAC/C,MAAM,QAAQ,KAAK,kBAAkB;AACrC,SAAO,KAAK,QACV,IAAI,KAAK,OAAO,UAAU,YAAY,UAAU,SAChD,EACE,SAAS,EACP,UAAU,4BACX,EACF,CACF;;;;;;;;;;;;;;;;;;;CAoBH,WAAW,UAA0B;EACnC,MAAM,UAAU,KAAK,iBAAiB,SAAS;EAC/C,MAAM,QAAQ,KAAK,kBAAkB;AACrC,SAAO,GAAG,KAAK,OAAO,QAAQ,GAAG,KAAK,OAAO,UAAU,YAAY,UAAU;;;;;;;;;;;;;;;;;CAkB/E,MAAM,aAAa,UAAkB,QAAgB,KAAyB;EAE5E,MAAM,SAAS,MAAM,YADR,MAAM,KAAK,QAAQ,SAAS,EACF,MAAM;AAE7C,SAAO;GACL,SAAS,OAAO;GAChB,MAAM,OAAO;GACb,YAAY,OAAO;GACpB"}
|
|
1
|
+
{"version":3,"file":"index.mjs","names":["isInIframe","requestAuthFromParent"],"sources":["../src/config.ts","../src/parquet.ts","../src/client.ts"],"sourcesContent":["/**\n * Configuration for the Oxy SDK\n */\nexport interface OxyConfig {\n /**\n * Base URL of the Oxy API (e.g., 'https://api.oxy.tech' or 'http://localhost:3000')\n */\n baseUrl: string;\n\n /**\n * API key for authentication (optional for local development)\n */\n apiKey?: string;\n\n /**\n * Project ID (UUID)\n */\n projectId: string;\n\n /**\n * Optional branch name (defaults to current branch if not specified)\n */\n branch?: string;\n\n /**\n * Request timeout in milliseconds (default: 30000)\n */\n timeout?: number;\n\n /**\n * Parent window origin for postMessage authentication (iframe scenarios)\n * Required when using postMessage auth for security.\n * Example: 'https://app.example.com'\n * Use '*' only in development!\n */\n parentOrigin?: string;\n\n /**\n * Disable automatic postMessage authentication even if in iframe\n * Set to true if you want to provide API key manually in iframe context\n */\n disableAutoAuth?: boolean;\n}\n\n/**\n * Creates an Oxy configuration from environment variables\n *\n * Environment variables:\n * - OXY_URL: Base URL of the Oxy API\n * - OXY_API_KEY: API key for authentication\n * - OXY_PROJECT_ID: Project ID (UUID)\n * - OXY_BRANCH: (Optional) Branch name\n *\n * @param overrides - Optional configuration overrides\n * @returns OxyConfig object\n * @throws Error if required environment variables are missing\n */\nexport function createConfig(overrides?: Partial<OxyConfig>): OxyConfig {\n const baseUrl = overrides?.baseUrl || process.env.OXY_URL;\n const apiKey = overrides?.apiKey || process.env.OXY_API_KEY;\n const projectId = overrides?.projectId || process.env.OXY_PROJECT_ID;\n\n if (!baseUrl) {\n throw new Error(\n \"OXY_URL environment variable or baseUrl config is required\",\n );\n }\n\n if (!projectId) {\n throw new Error(\n \"OXY_PROJECT_ID environment variable or projectId config is required\",\n );\n }\n\n return {\n baseUrl: baseUrl.replace(/\\/$/, \"\"), // Remove trailing slash\n apiKey,\n projectId,\n branch: overrides?.branch || process.env.OXY_BRANCH,\n timeout: overrides?.timeout || 30000,\n parentOrigin: overrides?.parentOrigin,\n disableAutoAuth: overrides?.disableAutoAuth,\n };\n}\n\n/**\n * Creates an Oxy configuration asynchronously with support for postMessage authentication\n *\n * This is the recommended method for iframe scenarios where authentication\n * needs to be obtained from the parent window via postMessage.\n *\n * When running in an iframe without an API key, this function will:\n * 1. Detect the iframe context\n * 2. Send an authentication request to the parent window\n * 3. Wait for the parent to respond with credentials\n * 4. Return the configured client\n *\n * Environment variables (fallback):\n * - OXY_URL: Base URL of the Oxy API\n * - OXY_API_KEY: API key for authentication\n * - OXY_PROJECT_ID: Project ID (UUID)\n * - OXY_BRANCH: (Optional) Branch name\n *\n * @param overrides - Optional configuration overrides\n * @returns Promise resolving to OxyConfig object\n * @throws Error if required configuration is missing\n * @throws PostMessageAuthTimeoutError if parent doesn't respond\n *\n * @example\n * ```typescript\n * // Automatic iframe detection and authentication\n * const config = await createConfigAsync({\n * parentOrigin: 'https://app.example.com',\n * projectId: 'my-project-id',\n * baseUrl: 'https://api.oxy.tech'\n * });\n * ```\n */\nexport async function createConfigAsync(\n overrides?: Partial<OxyConfig>,\n): Promise<OxyConfig> {\n // Import postMessage utilities (dynamic to avoid circular deps)\n const { isInIframe } = await import(\"./auth/postMessage\");\n\n // Start with environment variables and overrides\n let baseUrl = overrides?.baseUrl || process.env.OXY_URL;\n let apiKey = overrides?.apiKey || process.env.OXY_API_KEY;\n let projectId = overrides?.projectId || process.env.OXY_PROJECT_ID;\n\n const disableAutoAuth = overrides?.disableAutoAuth ?? false;\n const parentOrigin = overrides?.parentOrigin;\n\n // Automatic iframe detection and authentication\n if (!disableAutoAuth && isInIframe() && !apiKey) {\n if (!parentOrigin) {\n logWarningAboutMissingParentOrigin();\n } else {\n apiKey = await attemptPostMessageAuth(\n parentOrigin,\n overrides?.timeout || 5000,\n apiKey,\n projectId,\n baseUrl,\n )\n .then((result) => {\n if (result.projectId) projectId = result.projectId;\n if (result.baseUrl) baseUrl = result.baseUrl;\n return result.apiKey;\n })\n .catch((error) => {\n console.error(\n \"[Oxy SDK] Failed to authenticate via postMessage:\",\n (error as Error).message,\n );\n return apiKey;\n });\n }\n }\n\n return createFinalConfig(baseUrl, apiKey, projectId, overrides);\n}\n\nfunction logWarningAboutMissingParentOrigin(): void {\n console.warn(\n \"[Oxy SDK] Running in iframe without API key and no parentOrigin specified. \" +\n \"PostMessage authentication will be skipped. \" +\n \"Provide parentOrigin config to enable automatic authentication.\",\n );\n}\n\nasync function attemptPostMessageAuth(\n parentOrigin: string,\n timeout: number,\n currentApiKey: string | undefined,\n currentProjectId: string | undefined,\n currentBaseUrl: string | undefined,\n): Promise<{ apiKey?: string; projectId?: string; baseUrl?: string }> {\n const { requestAuthFromParent } = await import(\"./auth/postMessage\");\n const authResult = await requestAuthFromParent({ parentOrigin, timeout });\n\n console.log(\"[Oxy SDK] Successfully authenticated via postMessage\");\n\n return {\n apiKey: authResult.apiKey || currentApiKey,\n projectId: authResult.projectId || currentProjectId,\n baseUrl: authResult.baseUrl || currentBaseUrl,\n };\n}\n\nfunction createFinalConfig(\n baseUrl: string | undefined,\n apiKey: string | undefined,\n projectId: string | undefined,\n overrides?: Partial<OxyConfig>,\n): OxyConfig {\n // Validation\n if (!baseUrl) {\n throw new Error(\n \"OXY_URL environment variable or baseUrl config is required\",\n );\n }\n\n if (!projectId) {\n throw new Error(\n \"OXY_PROJECT_ID environment variable or projectId config is required\",\n );\n }\n\n return {\n baseUrl: baseUrl.replace(/\\/$/, \"\"), // Remove trailing slash\n apiKey,\n projectId,\n branch: overrides?.branch || process.env.OXY_BRANCH,\n timeout: overrides?.timeout || 30000,\n parentOrigin: overrides?.parentOrigin,\n disableAutoAuth: overrides?.disableAutoAuth,\n };\n}\n","import * as duckdb from \"@duckdb/duckdb-wasm\";\n\nlet dbInstance: duckdb.AsyncDuckDB | null = null;\nlet connection: duckdb.AsyncDuckDBConnection | null = null;\n\n// Queue to serialize operations and prevent race conditions\nlet operationQueue = Promise.resolve();\n\n/**\n * Enqueue an operation to prevent race conditions on shared DuckDB instance\n */\nfunction enqueueOperation<T>(operation: () => Promise<T>): Promise<T> {\n const currentOperation = operationQueue.then(operation, operation);\n operationQueue = currentOperation.then(\n () => {\n return;\n },\n () => {\n return;\n },\n );\n return currentOperation;\n}\n\n/**\n * Initialize DuckDB-WASM instance\n */\nexport async function initializeDuckDB(): Promise<duckdb.AsyncDuckDB> {\n if (dbInstance) {\n return dbInstance;\n }\n\n const JSDELIVR_BUNDLES = duckdb.getJsDelivrBundles();\n\n // Select a bundle based on browser features\n const bundle = await duckdb.selectBundle(JSDELIVR_BUNDLES);\n\n const worker_url = URL.createObjectURL(\n new Blob([`importScripts(\"${bundle.mainWorker}\");`], {\n type: \"text/javascript\",\n }),\n );\n\n const worker = new Worker(worker_url);\n const logger = new duckdb.ConsoleLogger();\n\n dbInstance = new duckdb.AsyncDuckDB(logger, worker);\n await dbInstance.instantiate(bundle.mainModule, bundle.pthreadWorker);\n URL.revokeObjectURL(worker_url);\n\n return dbInstance;\n}\n\n/**\n * Get or create a DuckDB connection\n */\nasync function getConnection(): Promise<duckdb.AsyncDuckDBConnection> {\n if (connection) {\n return connection;\n }\n\n const db = await initializeDuckDB();\n connection = await db.connect();\n return connection;\n}\n\n/**\n * Query result interface\n */\nexport interface QueryResult {\n columns: string[];\n rows: unknown[][];\n rowCount: number;\n}\n\n/**\n * ParquetReader provides methods to read and query Parquet files\n */\nexport class ParquetReader {\n private tableName: string;\n private internalTableName: string;\n private registered: boolean = false;\n\n constructor(tableName: string = \"data\") {\n this.tableName = tableName;\n // Generate unique internal table name to prevent conflicts between concurrent instances\n // Note: Math.random() is acceptable here as uniqueness is only needed for table naming (not security-critical)\n /* eslint-disable sonarjs/pseudo-random */\n const uniqueId = `${Date.now()}_${Math.random().toString(36).substring(2, 9)}`;\n /* eslint-enable sonarjs/pseudo-random */\n this.internalTableName = `${tableName}_${uniqueId}`;\n }\n\n /**\n * Register a Parquet file from a Blob\n *\n * @param blob - Parquet file as Blob\n *\n * @example\n * ```typescript\n * const blob = await client.getFile('data/sales.parquet');\n * const reader = new ParquetReader('sales');\n * await reader.registerParquet(blob);\n * ```\n */\n async registerParquet(blob: Blob): Promise<void> {\n await enqueueOperation(async () => {\n const conn = await getConnection();\n const db = await initializeDuckDB();\n\n // Convert blob to Uint8Array\n const arrayBuffer = await blob.arrayBuffer();\n const uint8Array = new Uint8Array(arrayBuffer);\n\n // Register the file with DuckDB using unique name\n await db.registerFileBuffer(\n `${this.internalTableName}.parquet`,\n uint8Array,\n );\n\n // Drop table if it exists\n try {\n await conn.query(`DROP TABLE IF EXISTS ${this.internalTableName}`);\n } catch {\n // Ignore error if table doesn't exist\n }\n\n // Create table from parquet\n await conn.query(\n `CREATE TABLE ${this.internalTableName} AS SELECT * FROM '${this.internalTableName}.parquet'`,\n );\n\n this.registered = true;\n });\n }\n\n /**\n * Execute a SQL query against the registered Parquet data\n *\n * @param sql - SQL query string\n * @returns Query result with columns and rows\n *\n * @example\n * ```typescript\n * const result = await reader.query('SELECT * FROM sales LIMIT 10');\n * console.log(result.columns);\n * console.log(result.rows);\n * ```\n */\n async query(sql: string): Promise<QueryResult> {\n if (!this.registered) {\n throw new Error(\n \"Parquet file not registered. Call registerParquet() first.\",\n );\n }\n\n return enqueueOperation(async () => {\n const conn = await getConnection();\n\n // Replace user's table name with internal unique table name\n const rewrittenSql = sql.replace(\n new RegExp(`\\\\b${this.tableName}\\\\b`, \"g\"),\n this.internalTableName,\n );\n\n const result = await conn.query(rewrittenSql);\n\n const columns = result.schema.fields.map((field) => field.name);\n const rows: unknown[][] = [];\n\n // Convert Arrow table to rows\n for (let i = 0; i < result.numRows; i++) {\n const row: unknown[] = [];\n for (let j = 0; j < result.numCols; j++) {\n const col = result.getChildAt(j);\n row.push(col?.get(i));\n }\n rows.push(row);\n }\n\n return {\n columns,\n rows,\n rowCount: result.numRows,\n };\n });\n }\n\n /**\n * Get all data from the registered table\n *\n * @param limit - Maximum number of rows to return (default: all)\n * @returns Query result\n *\n * @example\n * ```typescript\n * const allData = await reader.getAll();\n * const first100 = await reader.getAll(100);\n * ```\n */\n async getAll(limit?: number): Promise<QueryResult> {\n const limitClause = limit ? ` LIMIT ${limit}` : \"\";\n return this.query(`SELECT * FROM ${this.tableName}${limitClause}`);\n }\n\n /**\n * Get table schema information\n *\n * @returns Schema information\n *\n * @example\n * ```typescript\n * const schema = await reader.getSchema();\n * console.log(schema.columns); // ['id', 'name', 'sales']\n * console.log(schema.rows); // [['id', 'INTEGER'], ['name', 'VARCHAR'], ...]\n * ```\n */\n async getSchema(): Promise<QueryResult> {\n return this.query(`DESCRIBE ${this.tableName}`);\n }\n\n /**\n * Get row count\n *\n * @returns Number of rows in the table\n *\n * @example\n * ```typescript\n * const count = await reader.count();\n * console.log(`Total rows: ${count}`);\n * ```\n */\n async count(): Promise<number> {\n const result = await this.query(\n `SELECT COUNT(*) as count FROM ${this.tableName}`,\n );\n return result.rows[0][0] as number;\n }\n\n /**\n * Close and cleanup resources\n */\n async close(): Promise<void> {\n if (this.registered) {\n await enqueueOperation(async () => {\n const conn = await getConnection();\n const db = await initializeDuckDB();\n\n // Drop the table using internal unique name\n try {\n await conn.query(`DROP TABLE IF EXISTS ${this.internalTableName}`);\n } catch {\n // Ignore error\n }\n\n // Drop the registered file buffer using internal unique name\n try {\n await db.dropFile(`${this.internalTableName}.parquet`);\n } catch {\n // Ignore error if file doesn't exist\n }\n\n this.registered = false;\n });\n }\n }\n}\n\n/**\n * Helper function to quickly read a Parquet blob and execute a query\n *\n * @param blob - Parquet file as Blob\n * @param sql - SQL query to execute (optional, defaults to SELECT *)\n * @returns Query result\n *\n * @example\n * ```typescript\n * const blob = await client.getFile('data/sales.parquet');\n * const result = await queryParquet(blob, 'SELECT product, SUM(amount) as total FROM data GROUP BY product');\n * console.log(result);\n * ```\n */\nexport async function queryParquet(\n blob: Blob,\n sql?: string,\n): Promise<QueryResult> {\n // Generate unique table name to avoid conflicts\n // Note: Math.random() is acceptable here as uniqueness is only needed for table naming (not security-critical)\n /* eslint-disable sonarjs/pseudo-random */\n const uniqueId = `temp_${Date.now()}_${Math.random().toString(36).substring(2, 9)}`;\n /* eslint-enable sonarjs/pseudo-random */\n const reader = new ParquetReader(uniqueId);\n\n await reader.registerParquet(blob);\n\n const query = sql || `SELECT * FROM ${uniqueId}`;\n const result = await reader.query(query);\n\n await reader.close();\n return result;\n}\n\n/**\n * Helper function to read Parquet file and get all data\n *\n * @param blob - Parquet file as Blob\n * @param limit - Maximum number of rows (optional)\n * @returns Query result\n *\n * @example\n * ```typescript\n * const blob = await client.getFile('data/sales.parquet');\n * const data = await readParquet(blob, 1000);\n * console.log(`Loaded ${data.rowCount} rows`);\n * ```\n */\nexport async function readParquet(\n blob: Blob,\n limit?: number,\n): Promise<QueryResult> {\n // Generate unique table name to avoid conflicts\n // Note: Math.random() is acceptable here as uniqueness is only needed for table naming (not security-critical)\n /* eslint-disable sonarjs/pseudo-random */\n const uniqueId = `temp_${Date.now()}_${Math.random().toString(36).substring(2, 9)}`;\n /* eslint-enable sonarjs/pseudo-random */\n const reader = new ParquetReader(uniqueId);\n\n await reader.registerParquet(blob);\n\n const result = await reader.getAll(limit);\n\n await reader.close();\n return result;\n}\n","import { OxyConfig, createConfigAsync } from \"./config\";\nimport {\n AppItem,\n AppDataResponse,\n GetDisplaysResponse,\n ApiError,\n TableData,\n} from \"./types\";\nimport { readParquet } from \"./parquet\";\n\n/**\n * Oxy API Client for interacting with Oxy data\n */\nexport class OxyClient {\n private config: OxyConfig;\n\n constructor(config: OxyConfig) {\n this.config = config;\n }\n\n /**\n * Creates an OxyClient instance asynchronously with support for postMessage authentication\n *\n * This is the recommended method when using the SDK in an iframe that needs to\n * obtain authentication from the parent window via postMessage.\n *\n * @param config - Optional configuration overrides\n * @returns Promise resolving to OxyClient instance\n * @throws Error if required configuration is missing\n * @throws PostMessageAuthTimeoutError if parent doesn't respond\n *\n * @example\n * ```typescript\n * // In an iframe - automatic postMessage auth\n * const client = await OxyClient.create({\n * parentOrigin: 'https://app.example.com',\n * projectId: 'my-project-id',\n * baseUrl: 'https://api.oxy.tech'\n * });\n *\n * // Use the client normally\n * const apps = await client.listApps();\n * ```\n */\n static async create(config?: Partial<OxyConfig>): Promise<OxyClient> {\n const resolvedConfig = await createConfigAsync(config);\n return new OxyClient(resolvedConfig);\n }\n\n /**\n * Encodes a file path to base64 for use in API URLs\n */\n private encodePathBase64(path: string): string {\n if (typeof Buffer !== \"undefined\") {\n // Node.js environment\n return Buffer.from(path).toString(\"base64\");\n } else {\n // Browser environment\n return btoa(path);\n }\n }\n\n /**\n * Makes an authenticated HTTP request to the Oxy API\n */\n private async request<T>(\n endpoint: string,\n options: RequestInit = {},\n ): Promise<T> {\n const url = `${this.config.baseUrl}${endpoint}`;\n\n const headers: Record<string, string> = {\n \"Content-Type\": \"application/json\",\n ...((options.headers as Record<string, string>) || {}),\n };\n\n // Only add Authorization header if API key is provided (optional for local dev)\n if (this.config.apiKey) {\n headers[\"Authorization\"] = `Bearer ${this.config.apiKey}`;\n }\n\n const controller = new AbortController();\n const timeoutId = setTimeout(\n () => controller.abort(),\n this.config.timeout || 30000,\n );\n\n try {\n const response = await fetch(url, {\n ...options,\n headers,\n signal: controller.signal,\n });\n\n clearTimeout(timeoutId);\n\n if (!response.ok) {\n const errorText = await response.text().catch(() => \"Unknown error\");\n const error: ApiError = {\n message: `API request failed: ${response.statusText}`,\n status: response.status,\n details: errorText,\n };\n throw error;\n }\n\n // Handle binary responses\n const acceptHeader =\n typeof options.headers === \"object\" && options.headers !== null\n ? (options.headers as Record<string, string>)[\"Accept\"]\n : undefined;\n if (acceptHeader === \"application/octet-stream\") {\n return response.blob() as Promise<T>;\n }\n\n return response.json();\n } catch (error: unknown) {\n clearTimeout(timeoutId);\n\n if (error instanceof Error && error.name === \"AbortError\") {\n throw new Error(\n `Request timeout after ${this.config.timeout || 30000}ms`,\n );\n }\n\n throw error;\n }\n }\n\n /**\n * Builds query parameters including optional branch\n */\n private buildQueryParams(\n additionalParams: Record<string, string> = {},\n ): string {\n const params: Record<string, string> = { ...additionalParams };\n\n if (this.config.branch) {\n params.branch = this.config.branch;\n }\n\n const searchParams = new URLSearchParams(params);\n const queryString = searchParams.toString();\n return queryString ? `?${queryString}` : \"\";\n }\n\n /**\n * Lists all apps in the project\n *\n * @returns Array of app items\n *\n * @example\n * ```typescript\n * const apps = await client.listApps();\n * console.log('Available apps:', apps);\n * ```\n */\n async listApps(): Promise<AppItem[]> {\n const query = this.buildQueryParams();\n return this.request<AppItem[]>(`/${this.config.projectId}/app${query}`);\n }\n\n /**\n * Gets data for a specific app\n *\n * @param appPath - Relative path to the app file (e.g., 'my-app.app.yml')\n * @returns App data response\n *\n * @example\n * ```typescript\n * const data = await client.getAppData('dashboard.app.yml');\n * if (data.error) {\n * console.error('Error:', data.error);\n * } else {\n * console.log('App data:', data.data);\n * }\n * ```\n */\n async getAppData(appPath: string): Promise<AppDataResponse> {\n const pathb64 = this.encodePathBase64(appPath);\n const query = this.buildQueryParams();\n return this.request<AppDataResponse>(\n `/${this.config.projectId}/app/${pathb64}${query}`,\n );\n }\n\n /**\n * Runs an app and returns fresh data (bypasses cache)\n *\n * @param appPath - Relative path to the app file\n * @returns App data response\n *\n * @example\n * ```typescript\n * const data = await client.runApp('dashboard.app.yml');\n * console.log('Fresh app data:', data.data);\n * ```\n */\n async runApp(appPath: string): Promise<AppDataResponse> {\n const pathb64 = this.encodePathBase64(appPath);\n const query = this.buildQueryParams();\n return this.request<AppDataResponse>(\n `/${this.config.projectId}/app/${pathb64}/run${query}`,\n { method: \"POST\" },\n );\n }\n\n /**\n * Gets display configurations for an app\n *\n * @param appPath - Relative path to the app file\n * @returns Display configurations with potential errors\n *\n * @example\n * ```typescript\n * const displays = await client.getDisplays('dashboard.app.yml');\n * displays.displays.forEach(d => {\n * if (d.error) {\n * console.error('Display error:', d.error);\n * } else {\n * console.log('Display:', d.display);\n * }\n * });\n * ```\n */\n async getDisplays(appPath: string): Promise<GetDisplaysResponse> {\n const pathb64 = this.encodePathBase64(appPath);\n const query = this.buildQueryParams();\n return this.request<GetDisplaysResponse>(\n `/${this.config.projectId}/app/${pathb64}/displays${query}`,\n );\n }\n\n /**\n * Gets a file from the app state directory (e.g., generated charts, images)\n *\n * This is useful for retrieving generated assets like charts, images, or other\n * files produced by app workflows and stored in the state directory.\n *\n * @param filePath - Relative path to the file in state directory\n * @returns Blob containing the file data\n *\n * @example\n * ```typescript\n * // Get a generated chart image\n * const blob = await client.getFile('charts/sales-chart.png');\n * const imageUrl = URL.createObjectURL(blob);\n *\n * // Use in an img tag\n * document.querySelector('img').src = imageUrl;\n * ```\n *\n * @example\n * ```typescript\n * // Download a file\n * const blob = await client.getFile('exports/data.csv');\n * const a = document.createElement('a');\n * a.href = URL.createObjectURL(blob);\n * a.download = 'data.csv';\n * a.click();\n * ```\n */\n async getFile(filePath: string): Promise<Blob> {\n const pathb64 = this.encodePathBase64(filePath);\n const query = this.buildQueryParams();\n return this.request<Blob>(\n `/${this.config.projectId}/app/file/${pathb64}${query}`,\n {\n headers: {\n Accept: \"application/octet-stream\",\n },\n },\n );\n }\n\n /**\n * Gets a file URL for direct browser access\n *\n * This returns a URL that can be used directly in img tags, fetch calls, etc.\n * The URL includes authentication via query parameters.\n *\n * @param filePath - Relative path to the file in state directory\n * @returns Full URL to the file\n *\n * @example\n * ```typescript\n * const imageUrl = client.getFileUrl('charts/sales-chart.png');\n *\n * // Use directly in img tag (in environments where query-based auth is supported)\n * document.querySelector('img').src = imageUrl;\n * ```\n */\n getFileUrl(filePath: string): string {\n const pathb64 = this.encodePathBase64(filePath);\n const query = this.buildQueryParams();\n return `${this.config.baseUrl}/${this.config.projectId}/app/file/${pathb64}${query}`;\n }\n\n /**\n * Fetches a parquet file and parses it into table data\n *\n * @param filePath - Relative path to the parquet file\n * @param limit - Maximum number of rows to return (default: 100)\n * @returns TableData with columns and rows\n *\n * @example\n * ```typescript\n * const tableData = await client.getTableData('data/sales.parquet', 50);\n * console.log(tableData.columns);\n * console.log(tableData.rows);\n * console.log(`Total rows: ${tableData.total_rows}`);\n * ```\n */\n async getTableData(\n filePath: string,\n limit: number = 100,\n ): Promise<TableData> {\n const blob = await this.getFile(filePath);\n const result = await readParquet(blob, limit);\n\n return {\n columns: result.columns,\n rows: result.rows,\n total_rows: result.rowCount,\n };\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAyDA,SAAgB,aAAa,WAA2C;CACtE,MAAM,UAAU,WAAW,WAAW,QAAQ,IAAI;CAClD,MAAM,SAAS,WAAW,UAAU,QAAQ,IAAI;CACh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C,SAAO,GAAG,KAAK,OAAO,QAAQ,GAAG,KAAK,OAAO,UAAU,YAAY,UAAU;;;;;;;;;;;;;;;;;CAkB/E,MAAM,aACJ,UACA,QAAgB,KACI;EAEpB,MAAM,SAAS,MAAM,YADR,MAAM,KAAK,QAAQ,SAAS,EACF,MAAM;AAE7C,SAAO;GACL,SAAS,OAAO;GAChB,MAAM,OAAO;GACb,YAAY,OAAO;GACpB"}
|
|
@@ -27,10 +27,16 @@ var PostMessageAuthInvalidOriginError = class extends Error {
|
|
|
27
27
|
*/
|
|
28
28
|
var PostMessageAuthNotInIframeError = class extends Error {
|
|
29
29
|
constructor() {
|
|
30
|
-
|
|
30
|
+
const currentContext = getCurrentContext();
|
|
31
|
+
super(`PostMessage authentication is only available when running in an iframe context.\n\nCurrent context: ${currentContext}\n\nIf you're running in a regular browser window, use direct configuration instead:\nconst client = new OxyClient({ apiKey: 'your-key', ... })`);
|
|
31
32
|
this.name = "PostMessageAuthNotInIframeError";
|
|
32
33
|
}
|
|
33
34
|
};
|
|
35
|
+
function getCurrentContext() {
|
|
36
|
+
if (typeof window === "undefined") return "non-browser (Node.js)";
|
|
37
|
+
if (window === window.parent) return "top-level window";
|
|
38
|
+
return "iframe";
|
|
39
|
+
}
|
|
34
40
|
/**
|
|
35
41
|
* Error thrown when authentication response is malformed or invalid
|
|
36
42
|
*/
|
|
@@ -52,7 +58,7 @@ function isInIframe() {
|
|
|
52
58
|
if (typeof window === "undefined") return false;
|
|
53
59
|
try {
|
|
54
60
|
return window !== window.parent && window.parent !== null;
|
|
55
|
-
} catch
|
|
61
|
+
} catch {
|
|
56
62
|
return true;
|
|
57
63
|
}
|
|
58
64
|
}
|
|
@@ -80,7 +86,7 @@ function validateOrigin(messageOrigin, allowedOrigin) {
|
|
|
80
86
|
const messageUrl = new URL(messageOrigin);
|
|
81
87
|
const allowedUrl = new URL(allowedOrigin);
|
|
82
88
|
return messageUrl.origin === allowedUrl.origin;
|
|
83
|
-
} catch
|
|
89
|
+
} catch {
|
|
84
90
|
return messageOrigin === allowedOrigin;
|
|
85
91
|
}
|
|
86
92
|
}
|
|
@@ -94,13 +100,7 @@ function validateOrigin(messageOrigin, allowedOrigin) {
|
|
|
94
100
|
*/
|
|
95
101
|
function createAuthListener(requestId, origin, timeout) {
|
|
96
102
|
return new Promise((resolve, reject) => {
|
|
97
|
-
|
|
98
|
-
let listener;
|
|
99
|
-
timeoutId = setTimeout(() => {
|
|
100
|
-
window.removeEventListener("message", listener);
|
|
101
|
-
reject(new PostMessageAuthTimeoutError(timeout));
|
|
102
|
-
}, timeout);
|
|
103
|
-
listener = (event) => {
|
|
103
|
+
const listener = (event) => {
|
|
104
104
|
if (!validateOrigin(event.origin, origin)) return;
|
|
105
105
|
if (!event.data || event.data.type !== "OXY_AUTH_RESPONSE") return;
|
|
106
106
|
const response = event.data;
|
|
@@ -115,6 +115,10 @@ function createAuthListener(requestId, origin, timeout) {
|
|
|
115
115
|
window.removeEventListener("message", listener);
|
|
116
116
|
resolve(response);
|
|
117
117
|
};
|
|
118
|
+
const timeoutId = setTimeout(() => {
|
|
119
|
+
window.removeEventListener("message", listener);
|
|
120
|
+
reject(new PostMessageAuthTimeoutError(timeout));
|
|
121
|
+
}, timeout);
|
|
118
122
|
window.addEventListener("message", listener);
|
|
119
123
|
});
|
|
120
124
|
}
|
|
@@ -225,4 +229,4 @@ Object.defineProperty(exports, 'validateOrigin', {
|
|
|
225
229
|
return validateOrigin;
|
|
226
230
|
}
|
|
227
231
|
});
|
|
228
|
-
//# sourceMappingURL=postMessage-
|
|
232
|
+
//# sourceMappingURL=postMessage-B1J0jDRN.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"postMessage-B1J0jDRN.cjs","names":[],"sources":["../src/types.ts","../src/auth/postMessage.ts"],"sourcesContent":["/**\n * Represents an app item in the project\n */\nexport interface AppItem {\n name: string;\n path: string;\n}\n\n/**\n * Reference to a data file (usually parquet)\n */\nexport interface FileReference {\n file_path: string;\n}\n\n/**\n * Table data structure for in-memory tables\n * (used when data is fetched and parsed)\n */\nexport interface TableData {\n columns: string[];\n rows: unknown[][];\n total_rows?: number;\n}\n\nexport type DataContainer = Record<string, FileReference>;\n\n/**\n * Response from app data endpoints\n */\nexport interface AppDataResponse {\n data: DataContainer | null;\n error: string | null;\n}\n\n/**\n * Display with potential error\n */\nexport interface DisplayWithError {\n display?: DisplayData;\n error?: string;\n}\n\n/**\n * Display data structure\n */\nexport interface DisplayData {\n type: string;\n content: unknown;\n}\n\n/**\n * Response from get displays endpoint\n */\nexport interface GetDisplaysResponse {\n displays: DisplayWithError[];\n}\n\n/**\n * Error response from the API\n */\nexport interface ApiError {\n message: string;\n status: number;\n details?: unknown;\n}\n\n/**\n * Query result from ParquetReader\n */\nexport interface QueryResult {\n columns: string[];\n rows: unknown[][];\n rowCount: number;\n}\n\n/**\n * PostMessage authentication protocol types\n */\n\n/**\n * Request message sent from iframe to parent window\n */\nexport interface OxyAuthRequestMessage {\n type: \"OXY_AUTH_REQUEST\";\n version: \"1.0\";\n timestamp: number;\n requestId: string;\n}\n\n/**\n * Response message sent from parent window to iframe\n */\nexport interface OxyAuthResponseMessage {\n type: \"OXY_AUTH_RESPONSE\";\n version: \"1.0\";\n requestId: string;\n apiKey?: string;\n projectId?: string;\n baseUrl?: string;\n}\n\n/**\n * Options for postMessage authentication\n */\nexport interface PostMessageAuthOptions {\n /** Required parent window origin for security (e.g., 'https://app.example.com'). Use '*' only in development! */\n parentOrigin?: string;\n /** Timeout in milliseconds (default: 5000) */\n timeout?: number;\n /** Number of retry attempts (default: 0) */\n retries?: number;\n}\n\n/**\n * Result from successful postMessage authentication\n */\nexport interface PostMessageAuthResult {\n apiKey?: string;\n projectId?: string;\n baseUrl?: string;\n source: \"postmessage\";\n}\n\n/**\n * Custom error classes for postMessage authentication\n */\n\n/**\n * Error thrown when postMessage authentication times out\n */\nexport class PostMessageAuthTimeoutError extends Error {\n constructor(timeout: number) {\n super(\n `Parent window did not respond to authentication request within ${timeout}ms.\\n\\n` +\n `Possible causes:\\n` +\n `- Parent window is not listening for 'OXY_AUTH_REQUEST' messages\\n` +\n `- Parent origin mismatch\\n` +\n `- Network/browser issues\\n\\n` +\n `Troubleshooting:\\n` +\n `1. Verify parent window has message listener set up\\n` +\n `2. Check parentOrigin configuration matches parent window origin\\n` +\n `3. Open browser console in parent window for errors`,\n );\n this.name = \"PostMessageAuthTimeoutError\";\n }\n}\n\n/**\n * Error thrown when authentication response comes from unauthorized origin\n */\nexport class PostMessageAuthInvalidOriginError extends Error {\n constructor(expected: string, actual: string) {\n super(\n `Received authentication response from unauthorized origin.\\n\\n` +\n `Expected: ${expected}\\n` +\n `Actual: ${actual}\\n\\n` +\n `This is a security error. Verify your parentOrigin configuration.`,\n );\n this.name = \"PostMessageAuthInvalidOriginError\";\n }\n}\n\n/**\n * Error thrown when postMessage authentication is attempted outside iframe context\n */\nexport class PostMessageAuthNotInIframeError extends Error {\n constructor() {\n const currentContext = getCurrentContext();\n super(\n `PostMessage authentication is only available when running in an iframe context.\\n\\n` +\n `Current context: ${currentContext}\\n\\n` +\n `If you're running in a regular browser window, use direct configuration instead:\\n` +\n `const client = new OxyClient({ apiKey: 'your-key', ... })`,\n );\n this.name = \"PostMessageAuthNotInIframeError\";\n }\n}\n\nfunction getCurrentContext(): string {\n if (typeof window === \"undefined\") {\n return \"non-browser (Node.js)\";\n }\n if (window === window.parent) {\n return \"top-level window\";\n }\n return \"iframe\";\n}\n\n/**\n * Error thrown when authentication response is malformed or invalid\n */\nexport class PostMessageAuthInvalidResponseError extends Error {\n constructor(reason: string) {\n super(`Invalid authentication response from parent: ${reason}`);\n this.name = \"PostMessageAuthInvalidResponseError\";\n }\n}\n","/**\n * PostMessage-based authentication for iframe scenarios\n *\n * This module enables secure cross-origin authentication between an iframe\n * (running the Oxy SDK) and its parent window (holding the API key).\n */\n\nimport type {\n OxyAuthRequestMessage,\n OxyAuthResponseMessage,\n PostMessageAuthOptions,\n PostMessageAuthResult,\n} from \"../types\";\n\nimport {\n PostMessageAuthTimeoutError,\n PostMessageAuthNotInIframeError,\n PostMessageAuthInvalidResponseError,\n} from \"../types\";\n\n/**\n * Check if the current context is running inside an iframe\n *\n * @returns true if running in an iframe, false otherwise (including Node.js)\n */\nexport function isInIframe(): boolean {\n // Check if we're in a browser environment\n if (typeof window === \"undefined\") {\n return false;\n }\n\n // Check if window has a parent and it's different from itself\n try {\n return window !== window.parent && window.parent !== null;\n } catch {\n // Cross-origin access might throw an error, but we're still in an iframe\n return true;\n }\n}\n\n/**\n * Generate a unique request ID for tracking auth requests\n *\n * @returns A unique identifier string\n */\nexport function generateRequestId(): string {\n // Use crypto.randomUUID if available (modern browsers)\n if (typeof crypto !== \"undefined\" && crypto.randomUUID) {\n return crypto.randomUUID();\n }\n\n // Fallback for older environments: timestamp + random\n // Note: Math.random() is used here as a fallback for environments without crypto.randomUUID\n // This is acceptable for non-security-critical request IDs (used only for message correlation)\n /* eslint-disable sonarjs/pseudo-random */\n return `${Date.now()}-${Math.random().toString(36).substring(2, 11)}`;\n /* eslint-enable sonarjs/pseudo-random */\n}\n\n/**\n * Validate that a message origin matches the expected origin\n *\n * @param messageOrigin - The origin from the message event\n * @param allowedOrigin - The expected/allowed origin\n * @returns true if origin is valid, false otherwise\n */\nexport function validateOrigin(\n messageOrigin: string,\n allowedOrigin?: string,\n): boolean {\n // If no allowed origin is specified, reject (security)\n if (!allowedOrigin) {\n return false;\n }\n\n // Wildcard - allow any origin (development only!)\n if (allowedOrigin === \"*\") {\n return true;\n }\n\n // Exact match\n if (messageOrigin === allowedOrigin) {\n return true;\n }\n\n // Try URL parsing for more flexible matching\n try {\n const messageUrl = new URL(messageOrigin);\n const allowedUrl = new URL(allowedOrigin);\n return messageUrl.origin === allowedUrl.origin;\n } catch {\n // If URL parsing fails, do simple string match\n return messageOrigin === allowedOrigin;\n }\n}\n\n/**\n * Create a promise that listens for an authentication response\n *\n * @param requestId - The request ID to match against\n * @param origin - The expected parent origin\n * @param timeout - Timeout in milliseconds\n * @returns Promise that resolves with the auth response\n */\nfunction createAuthListener(\n requestId: string,\n origin: string | undefined,\n timeout: number,\n): Promise<OxyAuthResponseMessage> {\n return new Promise((resolve, reject) => {\n // Set up message listener\n const listener = (event: MessageEvent) => {\n // Validate origin\n if (!validateOrigin(event.origin, origin)) {\n // Don't reject here - might be other postMessage traffic\n // Just ignore messages from wrong origins\n return;\n }\n\n // Check message type\n if (!event.data || event.data.type !== \"OXY_AUTH_RESPONSE\") {\n // Not our message type, ignore\n return;\n }\n\n const response = event.data as OxyAuthResponseMessage;\n\n // Validate request ID matches\n if (response.requestId !== requestId) {\n // Wrong request ID, ignore (might be another auth in progress)\n return;\n }\n\n // Validate version\n if (response.version !== \"1.0\") {\n clearTimeout(timeoutId);\n window.removeEventListener(\"message\", listener);\n reject(\n new PostMessageAuthInvalidResponseError(\n `Unsupported protocol version: ${response.version}`,\n ),\n );\n return;\n }\n\n // Success!\n clearTimeout(timeoutId);\n window.removeEventListener(\"message\", listener);\n resolve(response);\n };\n\n // Set up timeout\n const timeoutId = setTimeout(() => {\n window.removeEventListener(\"message\", listener);\n reject(new PostMessageAuthTimeoutError(timeout));\n }, timeout);\n\n // Start listening\n window.addEventListener(\"message\", listener);\n });\n}\n\n/**\n * Request authentication from parent window via postMessage\n *\n * This is the main entry point for iframe-based authentication.\n * It sends a request to the parent window and waits for a response.\n *\n * @param options - Configuration options for the auth request\n * @returns Promise that resolves with authentication credentials\n * @throws {PostMessageAuthNotInIframeError} If not in an iframe\n * @throws {PostMessageAuthTimeoutError} If parent doesn't respond in time\n * @throws {PostMessageAuthInvalidOriginError} If response from wrong origin\n * @throws {PostMessageAuthInvalidResponseError} If response is malformed\n *\n * @example\n * ```typescript\n * const auth = await requestAuthFromParent({\n * parentOrigin: 'https://app.example.com',\n * timeout: 5000\n * });\n * console.log('Received API key:', auth.apiKey);\n * ```\n */\nexport async function requestAuthFromParent(\n options: PostMessageAuthOptions = {},\n): Promise<PostMessageAuthResult> {\n const { parentOrigin, timeout = 5000, retries = 0 } = options;\n\n // Validate we're in an iframe\n if (!isInIframe()) {\n throw new PostMessageAuthNotInIframeError();\n }\n\n // Validate we're in a browser environment\n if (typeof window === \"undefined\") {\n throw new PostMessageAuthNotInIframeError();\n }\n\n // Generate request ID\n const requestId = generateRequestId();\n\n // Create request message\n const request: OxyAuthRequestMessage = {\n type: \"OXY_AUTH_REQUEST\",\n version: \"1.0\",\n timestamp: Date.now(),\n requestId,\n };\n\n // Attempt authentication with retries\n let lastError: Error | null = null;\n const maxAttempts = retries + 1;\n\n for (let attempt = 0; attempt < maxAttempts; attempt++) {\n try {\n // Set up listener before sending request to avoid race condition\n const responsePromise = createAuthListener(\n requestId,\n parentOrigin,\n timeout,\n );\n\n // Send request to parent\n const targetOrigin = parentOrigin || \"*\";\n window.parent.postMessage(request, targetOrigin);\n\n // Wait for response\n const response = await responsePromise;\n\n // Return successful result\n return {\n apiKey: response.apiKey,\n projectId: response.projectId,\n baseUrl: response.baseUrl,\n source: \"postmessage\",\n };\n } catch (error) {\n lastError = error as Error;\n\n // If this isn't a timeout, don't retry\n if (!(error instanceof PostMessageAuthTimeoutError)) {\n throw error;\n }\n\n // If we have more attempts, continue\n if (attempt < maxAttempts - 1) {\n // Optional: Add a small delay before retry\n await new Promise((resolve) => setTimeout(resolve, 100));\n continue;\n }\n\n // All retries exhausted\n throw error;\n }\n }\n\n // Should never reach here, but TypeScript needs this\n throw lastError || new Error(\"Authentication failed\");\n}\n"],"mappings":";;;;;;;;;AAmIA,IAAa,8BAAb,cAAiD,MAAM;CACrD,YAAY,SAAiB;AAC3B,QACE,kEAAkE,QAAQ,+UAS3E;AACD,OAAK,OAAO;;;;;;AAOhB,IAAa,oCAAb,cAAuD,MAAM;CAC3D,YAAY,UAAkB,QAAgB;AAC5C,QACE,2EACe,SAAS,YACX,OAAO,uEAErB;AACD,OAAK,OAAO;;;;;;AAOhB,IAAa,kCAAb,cAAqD,MAAM;CACzD,cAAc;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