-
Notifications
You must be signed in to change notification settings - Fork 62
feat: add streamedListObjects for unlimited object retrieval #654
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
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
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
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
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 |
|---|---|---|
| @@ -0,0 +1,176 @@ | ||
| {{>partial_header}} | ||
|
|
||
| import type { Readable } from "node:stream"; | ||
|
|
||
| // Helper: create async iterable from classic EventEmitter-style Readable streams | ||
| const createAsyncIterableFromReadable = (readable: any): AsyncIterable<any> => { | ||
| return { | ||
| [Symbol.asyncIterator](): AsyncIterator<any> { | ||
| const chunkQueue: any[] = []; | ||
| const pendings: Array<{ resolve: (v: IteratorResult<any>) => void; reject: (e?: any) => void }> = []; | ||
| let ended = false; | ||
| let error: any = null; | ||
|
|
||
| const onData = (chunk: any) => { | ||
| if (pendings.length > 0) { | ||
| const { resolve } = pendings.shift()!; | ||
| resolve({ value: chunk, done: false }); | ||
| } else { | ||
| chunkQueue.push(chunk); | ||
| } | ||
| }; | ||
|
|
||
| const onEnd = () => { | ||
| if (error) return; // Don't process end if error already occurred | ||
| ended = true; | ||
| while (pendings.length > 0) { | ||
| const { resolve } = pendings.shift()!; | ||
| resolve({ value: undefined, done: true }); | ||
| } | ||
| }; | ||
|
|
||
| const onError = (err: any) => { | ||
| error = err; | ||
| while (pendings.length > 0) { | ||
| const { reject } = pendings.shift()!; | ||
| reject(err); | ||
| } | ||
| cleanup(); | ||
| }; | ||
|
|
||
| readable.on("data", onData); | ||
| readable.once("end", onEnd); | ||
| readable.once("error", onError); | ||
|
|
||
| const cleanup = () => { | ||
| readable.off("data", onData); | ||
| readable.off("end", onEnd); | ||
| readable.off("error", onError); | ||
| }; | ||
|
|
||
| return { | ||
| next(): Promise<IteratorResult<any>> { | ||
| if (error) { | ||
| return Promise.reject(error); | ||
| } | ||
| if (chunkQueue.length > 0) { | ||
| const value = chunkQueue.shift(); | ||
| return Promise.resolve({ value, done: false }); | ||
| } | ||
| if (ended) { | ||
| cleanup(); | ||
| return Promise.resolve({ value: undefined, done: true }); | ||
| } | ||
| return new Promise<IteratorResult<any>>((resolve, reject) => { | ||
| pendings.push({ resolve, reject }); | ||
| }); | ||
| }, | ||
| return(): Promise<IteratorResult<any>> { | ||
| try { | ||
| cleanup(); | ||
| } finally { | ||
| if (readable && typeof readable.destroy === "function") { | ||
| readable.destroy(); | ||
| } | ||
| } | ||
| return Promise.resolve({ value: undefined, done: true }); | ||
| }, | ||
| throw(e?: any): Promise<IteratorResult<any>> { | ||
| try { | ||
| cleanup(); | ||
| } finally { | ||
| if (readable && typeof readable.destroy === "function") { | ||
| readable.destroy(e); | ||
| } | ||
| } | ||
| return Promise.reject(e); | ||
| } | ||
| }; | ||
| } | ||
| }; | ||
| }; | ||
|
|
||
| /** | ||
| * Parse newline-delimited JSON (NDJSON) from a Node.js readable stream | ||
| * @param stream - Node.js readable stream, AsyncIterable, string, or Buffer | ||
| * @returns AsyncGenerator that yields parsed JSON objects | ||
| */ | ||
| export async function* parseNDJSONStream( | ||
| stream: Readable | AsyncIterable<Uint8Array | string | Buffer> | string | Uint8Array | Buffer | ||
| ): AsyncGenerator<any> { | ||
| const decoder = new TextDecoder("utf-8"); | ||
| let buffer = ""; | ||
|
|
||
| // If stream is actually a string or Buffer-like, handle as whole payload | ||
| const isString = typeof stream === "string"; | ||
| const isBuffer = typeof Buffer !== "undefined" && Buffer.isBuffer && Buffer.isBuffer(stream); | ||
| const isUint8Array = typeof Uint8Array !== "undefined" && stream instanceof Uint8Array; | ||
|
|
||
| if (isString || isBuffer || isUint8Array) { | ||
| const text = isString | ||
| ? (stream as string) | ||
| : decoder.decode(isBuffer ? new Uint8Array(stream as Buffer) : (stream as Uint8Array)); | ||
| const lines = text.split("\n"); | ||
|
|
||
| for (const line of lines) { | ||
| const trimmed = line.trim(); | ||
| if (!trimmed) { | ||
| continue; | ||
| } | ||
|
|
||
| try { | ||
| yield JSON.parse(trimmed); | ||
| } catch (err) { | ||
| console.warn("Failed to parse JSON line:", err); | ||
| } | ||
| } | ||
| return; | ||
| } | ||
|
|
||
| const isAsyncIterable = stream && typeof (stream as any)[Symbol.asyncIterator] === "function"; | ||
| const source: AsyncIterable<any> = isAsyncIterable ? (stream as any) : createAsyncIterableFromReadable(stream as any); | ||
|
|
||
| for await (const chunk of source) { | ||
| // Node.js streams can return Buffer or string chunks | ||
| // Convert to Uint8Array if needed for TextDecoder | ||
| const uint8Chunk = typeof chunk === "string" | ||
| ? new TextEncoder().encode(chunk) | ||
| : chunk instanceof Buffer | ||
| ? new Uint8Array(chunk) | ||
| : chunk; | ||
|
|
||
| // Append decoded chunk to buffer | ||
| buffer += decoder.decode(uint8Chunk, { stream: true }); | ||
|
|
||
| // Split on newlines | ||
| const lines = buffer.split("\n"); | ||
|
|
||
| // Keep the last (potentially incomplete) line in the buffer | ||
| buffer = lines.pop() || ""; | ||
|
|
||
| // Parse and yield complete lines | ||
| for (const line of lines) { | ||
| const trimmed = line.trim(); | ||
| if (trimmed) { | ||
| try { | ||
| yield JSON.parse(trimmed); | ||
| } catch (err) { | ||
| console.warn("Failed to parse JSON line:", err); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Flush any remaining decoder state | ||
| buffer += decoder.decode(); | ||
|
|
||
| // Handle any remaining data in buffer | ||
| if (buffer.trim()) { | ||
| try { | ||
| yield JSON.parse(buffer); | ||
| } catch (err) { | ||
| console.warn("Failed to parse final JSON buffer:", err); | ||
| } | ||
| } | ||
| } | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we don't need this file anymore in the generator, since this would require reverse synching it back from the SDK, we can just do the changes in the SDK directly
Unless you're using something from the generator here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://github.com/openfga/js-sdk/blob/0c2a0ab16995585d98582f32efec8b82090a1249/streaming.ts
If this is using some config/constants that should come from the generator, maybe then we can continue having it here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch. I included it, so that all files would come from the generator. That said, looks like the need for the generator has been simplified and this can be removed.