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

Storage.getFile() not returning files from GAIA #1616

Closed
bilalanees98 opened this issue Jan 17, 2024 · 6 comments
Closed

Storage.getFile() not returning files from GAIA #1616

bilalanees98 opened this issue Jan 17, 2024 · 6 comments
Assignees
Labels
bug Unwanted or unintended logic causing harm

Comments

@bilalanees98
Copy link

bilalanees98 commented Jan 17, 2024

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.

@bilalanees98 bilalanees98 added the bug Unwanted or unintended logic causing harm label Jan 17, 2024
@github-project-automation github-project-automation bot moved this to 🆕 New in DevTools Jan 17, 2024
@janniks
Copy link
Collaborator

janniks commented Jan 22, 2024

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

@janniks janniks moved this from 🆕 New to 📋 Backlog in DevTools Jan 22, 2024
@bilalanees98
Copy link
Author

That might be helpful, would need to test it out though.

@bilalanees98
Copy link
Author

@janniks any update on this?

@janniks
Copy link
Collaborator

janniks commented Mar 25, 2024

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

@janniks janniks closed this as completed Mar 25, 2024
@github-project-automation github-project-automation bot moved this from 📋 Backlog to ✅ Done in DevTools Mar 25, 2024
@bilal-waheed
Copy link

bilal-waheed commented Apr 4, 2024

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.

@bilalanees98
Copy link
Author

bilalanees98 commented Apr 4, 2024

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Unwanted or unintended logic causing harm
Projects
Archived in project
Development

No branches or pull requests

3 participants