Storage.getFile() not returning files from GAIA
bilalanees98 opened this issue · 6 comments
What version of Stacks.js are you using?
"@stacks/connect": "^7.0.0",
"@stacks/storage": "^5.0.3",
"@stacks/transactions": "^5.0.2",
Describe the bug
Unable to read a file stored on Gaia from after it has been put there successfully.
how to reproduce
Use the following piece of code:
const privateKey =
"896adae13a1bf88db0b2ec94339b62382ec6f34cd7e2ff8abae7ec271e05f9d8";
const appConfig = new AppConfig();
const userSession = new UserSession({ appConfig });
userSession.store.getSessionData().userData = {
appPrivateKey: privateKey,
};
const storage = new Storage({ userSession });
const myData = JSON.stringify({
hello: "world",
num: 1,
});
//replace file path with a file that does not yet exist on gaia
try {
console.log("file on gaia: ", await storage.getFile("a_random_file8.json"));
} catch (error) {}
console.log("fileUrl: ", await storage.putFile("a_random_file8.json", myData));
console.log("file on gaia: ", await storage.getFile("a_random_file8.json")); //file not found error thrown
//getting the file will keep returning an error for a few minutes. After which the getFile call will complete successfully.
However by simply omitting the first getFile call this gets resolved:
const privateKey =
"896adae13a1bf88db0b2ec94339b62382ec6f34cd7e2ff8abae7ec271e05f9d8";
const appConfig = new AppConfig();
const userSession = new UserSession({ appConfig });
userSession.store.getSessionData().userData = {
appPrivateKey: privateKey,
};
const storage = new Storage({ userSession });
const myData = JSON.stringify({
hello: "world",
num: 1,
});
//try {
// console.log("file on gaia: ", await storage.getFile("a_random_file8.json"));
//} catch (error) {}
//replace file path with a file that does not yet exist on gaia
console.log("fileUrl: ", await storage.putFile("a_random_file8.json", myData));
console.log("file on gaia: ", await storage.getFile("a_random_file8.json")); //no error thrown, file is fetched
Expected behavior
Both pieces of code should return the file after it has been written successfully.
Additional context
This is just an example to illustrate a piece of code in an application that checks whether a file exists (and returns its contents) on GAIA before creating it.
Oh, I see. This might be a cacheing issue or similar. Ideally, we could pass a flag to bust-cache -- would that work? Will check with devops
That might be helpful, would need to test it out though.
@janniks any update on this?
There might be an escape hatch to easily do this on the current setup.
Could you test if the following makes a difference for your case? We'll use a global fetch-options object to configure the network call that's being made by the storage methods.
import { getFetchOptions } from '@stacks/network';
const fetchOpts = getFetchOptions();
fetchOpts.headers['Cache-Control'] = 'no-cache';
Run this somewhere before your storage call and it should make the request not-cache the result. Should work with any of the cache-control headers https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control
lmk if this works 🙏 if not please re-open
I tried updating fetch-options using the following code:
import { getFetchOptions } from '@stacks/network';
const fetchOpts = getFetchOptions();
fetchOpts.headers['Cache-Control'] = 'no-cache';
const file = await storage.getFile("test-file.json");
On running this code, I got the following error:
Uncaught (in promise) TypeError: Cannot set properties of undefined (setting 'Cache-Control')
I also tried updating fetch-options using setFetchOptions functions:
import { setFetchOptions } from '@stacks/network';
setFetchOptions({ headers: {
"Cache-Control": "no-cache",
}});
const file = await storage.getFile("test-file.json");
Running this code results in a CORS error.
Let me know if I'm missing something. Thanks.
@bilal-waheed is working on this from our end. And the above mentioned did not work.
Any other possible options in mind @janniks?
Oh and apparently I can't re-open an issue.