Skip to content

Instantly share code, notes, and snippets.

@viraj124
Last active July 15, 2020 17:43
Show Gist options
  • Save viraj124/b4ed054bee4ef10c6925dbdeee7ffe12 to your computer and use it in GitHub Desktop.
Save viraj124/b4ed054bee4ef10c6925dbdeee7ffe12 to your computer and use it in GitHub Desktop.
global.XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
const {SpaceClient} = require("@fleekhq/space-client");
const client = new SpaceClient({url: `http://0.0.0.0:9998`});
(async () => {
const bucket = "buckettoshare" + Date.now();
try {
console.log("creating bucket...");
const res = await client.createBucket({slug: bucket});
const bucketObj = res.getBucket();
console.log(bucketObj);
} catch (error) {
console.error(error);
}
try {
console.log("creating folder...");
await client.createFolder({bucket, path: "testFolderA"});
console.log("folder created!");
} catch (error) {
console.error(error);
}
// list directory doesn't work and wed on't need it for now
// try {
// console.log('fetchig directory...');
// const res = await client.listDirectory({ bucket, path:"" });
// const entryList = res.getEntriesList();
// console.log(entryList);
// } catch (error) {
// console.error(error);
// }
try {
console.log("sharing bucket...");
const res = await client.shareBucket({bucket});
const threadInfo = res.getThreadinfo();
console.log({bucket, key: threadInfo.getKey(), addressList: threadInfo.getAddressesList(), addresses: threadInfo.getAddressesList().join(", ")});
} catch (error) {
console.error(error);
}
try {
console.log("sharing & joining bucket...");
const res = await client.shareBucket({bucket});
const threadInfo = res.getThreadinfo();
console.log({bucket, key: threadInfo.getKey(), addressList: threadInfo.getAddressesList(), addresses: threadInfo.getAddressesList().join(", ")});
// joining bucket needs to be run on the other machine
const res = await client.joinBucket({
bucket: "buckettoshare1594789044480",
threadInfo: {
key: "bjpkpt3wqkhf7os6zlwi2itplyxjs6tmgcbiqxdn4rgeqgcrvhmgidz2an3pcxmcr2zhs6v324n45xuvym543lz5qkjoa7tfs2o3fyii",
addresses: ["/ip4/127.0.0.1/tcp/4006/p2p/12D3KooWGWzytUr7X9GfoBbGwPdDmeT42MFGaoP8wNxmWMMiyb6H/thread/bafksufublxb7pd63roskj574xcyyxzet3m3juxwysjrjnla4ddh3wei", "/ip4/192.168.43.151/tcp/4006/p2p/12D3KooWGWzytUr7X9GfoBbGwPdDmeT42MFGaoP8wNxmWMMiyb6H/thread/bafksufublxb7pd63roskj574xcyyxzet3m3juxwysjrjnla4ddh3wei", "/ip4/157.33.234.252/tcp/4006/p2p/12D3KooWGWzytUr7X9GfoBbGwPdDmeT42MFGaoP8wNxmWMMiyb6H/thread/bafksufublxb7pd63roskj574xcyyxzet3m3juxwysjrjnla4ddh3wei", "/ip4/157.33.198.184/tcp/4006/p2p/12D3KooWGWzytUr7X9GfoBbGwPdDmeT42MFGaoP8wNxmWMMiyb6H/thread/bafksufublxb7pd63roskj574xcyyxzet3m3juxwysjrjnla4ddh3wei",]
}
});
console.log("result", res);
// uploading files
const stream = await client.addItems({
bucket: "buckettoshare1594789044480", targetPath: "/", // path in the bucket to be saved
sourcePaths: ["G:/Node-Down-Time-App/Nodes.csv"]
});
// events to capture the file uploading journey
await stream.on("data", (data) => {
console.log("data: ", data);
});
await stream.on("error", (error) => {
console.error("error: ", error);
});
await stream.on("end", () => {
console.log("end");
});
} catch (error) {
console.error(error);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment