-
Notifications
You must be signed in to change notification settings - Fork 29
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
feat: new way of creating temporary files backed up by fs #137
feat: new way of creating temporary files backed up by fs #137
Conversation
Codecov Report
@@ Coverage Diff @@
## main #137 +/- ##
========================================
Coverage ? 100.00%
========================================
Files ? 4
Lines ? 511
Branches ? 83
========================================
Hits ? 511
Misses ? 0
Partials ? 0 Continue to review full report at Codecov.
|
import { createTemporaryBlob } from 'fetch-blob/from.js'
const res = await fetch(url)
// thinking `response.blob()` should basically do this
const iterable = res.body
let blob = await createTemporaryBlob(iterable, 'text/html') // will be written to the OS temporary directory
blob = undefined // will GC the blob and eventually remove the file from the temporary location on the disk import { createTemporaryFile } from 'fetch-blob/from.js'
const fd = new FormData()
for await (const part of parseMultipart(payload)) {
const file = await createTemporaryFile(part.iterable, part.fileName, { type: part.type })
fd.append(part.fieldName, file)
}
return fd |
Oh btw, it's also possible to stop the writing if you pass a AbortSignal as an option either via: |
cc @KhafraDev @ronag @tunnckoCore who may all be interested in this. |
thanks for the ping. I've spent last few hours digging around File, FormData, and the different parsers. Making the parser just an (async) iterator function seems awesome. |
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.
elegant 👍🏼
was wondering if Everything else in
|
|
By the way, this Thought it a bit deeper for my use-case, I think that allowing |
If you want to have a filename, then u should use createTemporaryFile instead that returns a File class rather than a Blob so i don't think you should be able to pass in a filename as an option Also you should be able to create multiple files with the same name |
Well, yea, my thinking was wrong. They are temporary files and it doesn't matter what the filename is. I actually implemented it without any changes to the branch. The thing that I'll make formidable to do by default is to always create temp files, and later give that standard File to the user. If they want to save on disk there will be option that will just write permanent file to non temp user-chosen dir. Simple. Haha. And i think we can actually introduce it in the v3 and v2, with a bit backward compat file props. Turns out we switch out off of file.type and file.name in v3.. and v2 been more File compat, haha. All these recent things really lead to a more clear vision of what v4 can be. |
One thing I have been thinking of in node-fetch when we are parsing large multipart payloads with
response.formData()
is for the possibility to write dose temporary large file to the filesystem in order to not blow up the memory.But writing them to a place on the disk would also require cleaning it up.
That's why I have now also introduced the new
createTemporaryBlob
andcreateTemporaryFile
that writes whateverfsPromise.writeFile(data)
supportsThe purpose of this PR is:
Creating new temporary files to offload some data to the filesystem and cleaning when it's garbage collected
This is what had to change:
I exported two new features
createTemporaryBlob
andcreateTemporaryFile
in./from.js
This is what like reviewers to know:
GC
and you no longer have any references to that blob anymore, then the file will automatically be removed.fsPromise.writeFile(stream)
is more superior tostream.pipe(fs.createWritableStream(path))
asfsPromises. writeFile
supports more different type of data. It works withstring, ArrayBufferView
and(async)Iterator
so it supports both whatwg streams and node streams (from v15.14.0, v14.18.0)fs.createWritableStream
in all my projects... + it can tell when it's finish with a promise.