FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Fix JS Image deserialization without data by xiaolu-ai26 · Pull Request #17 · onenodehq/onenode · GitHub

Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .ts  (1) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
22 changes: 14 additions & 8 deletions onenode-js/src/ejson/image.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export interface ImageIndexOptions {
}

export class Image {
private data: string | File | Blob | ArrayBuffer | Uint8Array;
private data: string | File | Blob | ArrayBuffer | Uint8Array | null;
private mimeType: string;
private _chunks: string[];
private embModel: string | null;
Expand All @@ -36,9 +36,12 @@ export class Image {
return typeof process !== 'undefined' && process.versions && !!process.versions.node;
}

constructor(data: string | File | Blob | ArrayBuffer | Uint8Array) {
// Handle different input types
if (typeof data === "string") {
constructor(data: string | File | Blob | ArrayBuffer | Uint8Array | null = null) {
// Handle internal deserialization placeholders for pending/projected images.
if (data === null) {
this.data = null;
this.mimeType = "";
} else if (typeof data === "string") {
// String input - could be base64, data URL, HTTP URL, or file path
if (data.startsWith("data:")) {
// Data URL format
Expand Down Expand Up @@ -97,7 +100,7 @@ export class Image {
this.data = data;
this.mimeType = this.extractMimeTypeFromUint8Array(data);
} else {
throw new Error("Invalid data type: must be string (base64/data URL/HTTP URL/file path), File, Blob, ArrayBuffer, or Uint8Array");
throw new Error("Invalid data type: must be string (base64/data URL/HTTP URL/file path), File, Blob, ArrayBuffer, Uint8Array, or null");
}

// MIME type validation only matters when indexing
Expand Down Expand Up @@ -197,12 +200,12 @@ export class Image {
}
}

public getData(): string | File | Blob | ArrayBuffer | Uint8Array {
public getData(): string | File | Blob | ArrayBuffer | Uint8Array | null {
return this.data;
}

public getBinaryData(): File | Blob | ArrayBuffer | Uint8Array | null {
return typeof this.data === "string" ? null : this.data;
return typeof this.data === "string" || this.data === null ? null : this.data;
}

public getBase64Data(): string | null {
Expand All @@ -214,6 +217,9 @@ export class Image {
}

public hasBinaryData(): boolean {
if (this.data === null) {
return false;
}
// Consider URL strings as processed data, not binary
if (typeof this.data === "string") {
return this.data.startsWith('http'); // URL is considered as processed data
Expand Down Expand Up @@ -367,7 +373,7 @@ export class Image {
// Get data from database (can be URL or binary data)
// After processing, the data field contains the public URL
const dataValue = data["data"];
const instance = new Image(dataValue || "");
const instance = new Image(dataValue ?? null);

// Override the auto-detected mime_type with the one from database
instance.mimeType = data["mime_type"];
Expand Down

Back | FazBrowse Home | New Git URL