-
Notifications
You must be signed in to change notification settings - Fork 274
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
Add LazyBlob helper class to lazy load file from filesystem #104
Conversation
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.
This is going to be really cool when ready!
My main request is that from the outside, it should behave exactly as a Blob
. No need for .init()
or dispose()
, and instanceof Blob
should work.
Co-authored-by: Eliott C. <[email protected]>
Co-authored-by: Eliott C. <[email protected]>
@coyotte508 I can get ride of Here it's DX vs performances so I will let you choose 👍 |
The most calls will be three I think, on large files:
On smaller files, it will be two calls: one Given that, I'd rather prioritize DX on this one ! The overhead should be low. Maybe an optimization would be to directly load small files in memory, if we really care about perf. |
I agree with that 👍 Also, better have more syscall to open/close than unused file descriptor 🙈 |
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.
Getting close!
For the TS Error: maybe |
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 it's ready after the changes.
I just can't believe this module doesn't already exist on npm (only versions I found loaded everything in memory)
packages/hub/src/lib/LazyBlob.ts
Outdated
private async init(): Promise<void> { | ||
const { size } = await this.execute((file) => file.stat()); | ||
|
||
this.totalSize = size; | ||
} |
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.
This can be removed
packages/hub/src/lib/LazyBlob.ts
Outdated
} | ||
|
||
stream(): ReadableStream { | ||
return Readable.toWeb(createReadStream(this.path)); |
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.
return Readable.toWeb(createReadStream(this.path)); | |
return Readable.toWeb(createReadStream(this.path, {start: this.start, end: this.end})); |
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 thought about this one on my way back from the beach today 😁
packages/hub/src/lib/LazyBlob.ts
Outdated
private path: string; | ||
private start: number; | ||
private end: number; | ||
private totalSize: number; |
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.
This can be removed as well
packages/hub/src/lib/LazyBlob.ts
Outdated
if (this.start !== null) { | ||
if (this.end !== null) { | ||
return Math.abs(this.end - this.start); | ||
} | ||
|
||
return this.totalSize - this.start; | ||
} | ||
|
||
return this.totalSize; |
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.
if (this.start !== null) { | |
if (this.end !== null) { | |
return Math.abs(this.end - this.start); | |
} | |
return this.totalSize - this.start; | |
} | |
return this.totalSize; | |
return this.end - this.start; |
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.
Alirght, thanks a lot!
@coyotte508 What about the documentation? If I understood well it's in the same repo, I can give it a try |
I intend to keep it internal. A file with So I think maybe a smart way to expose it is to allow passing a It also opens the door for supporting web urls later on. (using Range header, or HEAD calls appropriately to get a slice / the size) |
Yes I saw, I'm familiar with this kind of dual behavior when it's needed to provide a SDK for both node and the browser (See Kuzzle Javascript SDK) If I understand well, you might want to allows a string representing a path to a local file for
|
When I was mentioning Basically, change: type ContentSource = Blob; To type ContentSource = Blob | URL; Btw |
Ok understood 👍 So if the URL is a local file (starting with |
Yea, if |
Do you want me to do this feature, supporting URL as content, in a separate PR? |
Sure! Then maybe you can remove the export in this one and keep the |
No problem ;-) |
Description
This PR add a new helper class
LazyBlob
which aim to prevent loading huge file into RAM.This new class can be used instead of a
Blob
with thecommit
function.Fix #68