Skip to content
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

File support #102

Open
azangru opened this issue Jul 16, 2024 · 3 comments
Open

File support #102

azangru opened this issue Jul 16, 2024 · 3 comments

Comments

@azangru
Copy link

azangru commented Jul 16, 2024

fake-indexeddb seems to support the storage of blobs (#56); however, if one attempts to store a File, then the file, when retrieved, will be converted into a Blob, and part of its information, such as file name, will be lost.

Example:

require('fake-indexeddb/auto');

const STORE_NAME = 'TEST';
const testId = 'test-id';
const file = new File([
  'hello world!'
], 'file.txt');


console.log('File prototype:', Object.getPrototypeOf(file)); //  Blob [File] { name: [Getter], lastModified: [Getter] }

const request = indexedDB.open("test", 3);
request.onupgradeneeded = function () {
  const db = request.result;
  const store = db.createObjectStore(STORE_NAME);

  store.put({
    title: "My test record",
    file
  }, testId);
}
request.onsuccess = function (event) {
    const db = event.target.result;

    const tx = db.transaction(STORE_NAME);

    tx.objectStore(STORE_NAME).get(testId).addEventListener("success", function (event) {
      const result = event.target.result;
      console.log("RETRIEVED:", result); // prints { title: 'My test record', file: Blob { size: 12, type: '' } }
      console.log('File prototype', Object.getPrototypeOf(result.file)); // the prototype of the file is now Object [Blob]
    });
    tx.oncomplete = function () {
      console.log("All done!");
    };
};
@dumbmatter
Copy link
Owner

dumbmatter commented Jul 16, 2024

This seems to be because of Node's structuredClone function works. More minimal example:

const file = new File([
  'hello world!'
], 'file.txt');

console.log(Object.getPrototypeOf(file));
console.log(Object.getPrototypeOf(structuredClone(file)));

If you run that in Node, you see the 2nd log is just a Blob rather than a File, but if you run it in a web browser then both are Files.

fake-indexeddb internally uses structuredClone from Node, which does work very similar to the web browser version which is used in the IndexedDB spec, but IIRC there are some minor differences and I guess this is one.

Whether this is a bug in Node or it's intended behavior, I'm not sure, I haven't had a chance to investigate further.

@azangru
Copy link
Author

azangru commented Jul 16, 2024

This issue in the Node repo suggests that it may be a Node bug.

@azangru
Copy link
Author

azangru commented Oct 4, 2024

For reference: the related PR was merged into Node last week, and the related Node issue was marked as completed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants