Skip to content

Commit

Permalink
Update ipfs endpoints, improve error handling (#1337)
Browse files Browse the repository at this point in the history
  • Loading branch information
stwiname committed Oct 5, 2022
1 parent e2e6b97 commit 63c0b23
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
20 changes: 15 additions & 5 deletions packages/cli/src/controller/publish-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export async function uploadFile(
}
let ipfsClusterCid: string;
try {
ipfsClusterCid = await UploadFileByCluster(
ipfsClusterCid = await uploadFileByCluster(
determineStringOrFsStream(content) ? await fs.promises.readFile(content.path, 'utf8') : content,
authToken
);
Expand All @@ -134,7 +134,7 @@ function determineStringOrFsStream(toBeDetermined: unknown): toBeDetermined is f
return !!(toBeDetermined as fs.ReadStream).path;
}

async function UploadFileByCluster(content: string, authToken: string): Promise<string> {
async function uploadFileByCluster(content: string, authToken: string): Promise<string> {
const bodyFormData = new FormData();
bodyFormData.append('data', content);
const result = (
Expand All @@ -151,7 +151,17 @@ async function UploadFileByCluster(content: string, authToken: string): Promise<
maxContentLength: 50 * 1024 * 1024,
})
).data as ClusterResponseData;
return result.cid?.['/'];

if (typeof result.cid === 'string') {
return result.cid;
}
const cid = result.cid?.['/'];

if (!cid) {
throw new Error('Failed to get CID from response');
}

return cid;
}

function mapToObject(map: Map<string | number, unknown>): Record<string | number, unknown> {
Expand All @@ -170,10 +180,10 @@ function isFileReference(value: any): value is FileReference {

interface ClusterResponseData {
name: string;
cid: cidSpec;
cid: CidSpec | string;
size: number;
}
// cluster response cid stored as {'/': 'QmVq2bqunmkmEmMCY3x9U9kDcgoRBGRbuBm5j7XKZDvSYt'}
interface cidSpec {
interface CidSpec {
'/': string;
}
8 changes: 4 additions & 4 deletions packages/common/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
// SPDX-License-Identifier: Apache-2.0

//IPFS
export const IPFS_DEV = 'https://interipfs.thechaindata.com';
export const IPFS_PROD = 'https://ipfs.subquery.network';
export const IPFS_NODE_ENDPOINT = `${IPFS_PROD}/ipfs/api/v0`;
export const IPFS_CLUSTER_ENDPOINT = `${IPFS_PROD}/cluster/add`;
export const IPFS_READ = 'https://unauthipfs.subquery.network';
export const IPFS_WRITE = 'https://authipfs.subquery.network';
export const IPFS_NODE_ENDPOINT = `${IPFS_READ}/ipfs/api/v0`;
export const IPFS_CLUSTER_ENDPOINT = `${IPFS_WRITE}/cluster/add`;
export const IPFS_REGEX = /^ipfs:\/\//i;

// MANIFEST
Expand Down

0 comments on commit 63c0b23

Please sign in to comment.