Skip to content

Commit

Permalink
fix(ipfs): ipfs cluster pinning bugfix (#919)
Browse files Browse the repository at this point in the history
  • Loading branch information
dasein108 authored Jul 27, 2023
1 parent 4972747 commit 3bab51e
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 91 deletions.
81 changes: 17 additions & 64 deletions src/utils/ipfs/cluster-utils.ts
Original file line number Diff line number Diff line change
@@ -1,75 +1,21 @@
import {
AddResponse,
PinResponse,
StatusResponse,
} from '@nftstorage/ipfs-cluster/dist/src/interface';
import { $TsFixMe } from 'src/types/tsfix';

import { chunksToBlob } from 'src/utils/ipfs/content-utils';

import { Cluster } from '@nftstorage/ipfs-cluster';
import { addIpfsContentToDb } from './db-utils';
import { getMimeFromUint8Array } from './stream-utils';

const CYBERNODE_URL = 'https://io.cybernode.ai';

const cluster = new Cluster(CYBERNODE_URL);

// eslint-disable-next-line import/no-unused-modules, import/prefer-default-export
export const addDataChunksToIpfsCluster = async (
cid: string,
chunks: Uint8Array,
saveToDb?: boolean
): Promise<AddResponse | undefined> => {
try {
if (chunks.length > 0) {
const mime = await getMimeFromUint8Array(chunks);
const blob = chunksToBlob([chunks], mime);

// result.cid is cidV1 - can we use that?
if (saveToDb) {
addIpfsContentToDb(cid, chunks);
}
// console.log('----cluster:', await cluster.info());
// TODO: ERROR on DEV ENV
// Access to fetch at 'https://io.cybernode.ai/add?stream-channels=false&raw-leaves=true&cid-version=1'
// from origin 'https://localhost:3001'
// has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present .....
return await pinToIpfsCluster(cid.toString(), blob);
}
} catch (error) {
console.log('error addDataChunksToIpfsCluster', cid, error);
}

return undefined;
};

// TODO: ****** Legacy code
const addFileToIpfsCluster = async (
// cid: string,
file: File | Blob | string
export const addToIpfsCluster = async (
file: File | string
): Promise<AddResponse | PinResponse | undefined> => {
let dataFile: $TsFixMe = null;

// TODO: unclear logic
// if (cid === file) {
// return cyberNode.pin(cid);
// }

if (file instanceof Blob) {
cluster.add(file, { cidVersion: 0 });
} else if (typeof file === 'string') {
// Why need this, can we use blob?
dataFile = new File([file], 'file.txt');
cluster.add(dataFile, { cidVersion: 0 });
}
// TODO: unclear type
// } else if (file.name && file.size < 8 * 10 ** 6) {
// dataFile = new File([file], file.name);
// }

return undefined;
// dublicated
// return cluster.pin(cid);
const dataFile =
typeof file === 'string' ? new File([file], 'file.txt') : file;
return cluster.add(dataFile, { cidVersion: 0, rawLeaves: false });
};

// check if the file is pinned to cybernode,
Expand All @@ -81,11 +27,11 @@ const addFileToIpfsCluster = async (
// eslint-disable-next-line import/no-unused-modules
export const pinToIpfsCluster = async (
cid: string,
file?: $TsFixMe
file?: File | string
): Promise<PinResponse | AddResponse | undefined> => {
try {
const cidStatus = await cluster.status(cid);

console.log('----cluster pin:', cid, cidStatus);
const pinInfoValues = Object.values(cidStatus.peerMap);
if (pinInfoValues.length > 0) {
// already pinned
Expand All @@ -95,10 +41,11 @@ export const pinToIpfsCluster = async (

// add to cluster and pin
if (file) {
return await addFileToIpfsCluster(file);
return await addToIpfsCluster(file);
}

return await cluster.pin(cid);
const result = await cluster.pin(cid);
console.log('----cluster pin result:', cid, result);
}

return undefined;
Expand All @@ -107,3 +54,9 @@ export const pinToIpfsCluster = async (
return undefined;
}
};

export const clusterPinStatus = async (
cid: string
): Promise<StatusResponse> => {
return cluster.status(cid);
};
10 changes: 10 additions & 0 deletions src/utils/ipfs/content-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,13 @@ export const parseRawIpfsData = async (
return undefined;
}
};

export const contentToUint8Array = async (
content: File | string
): Promise<Uint8Array> => {
return new Uint8Array(
typeof content === 'string'
? Buffer.from(content)
: await content.arrayBuffer()
);
};
40 changes: 13 additions & 27 deletions src/utils/ipfs/utils-ipfs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ import {

import { CYBER } from '../config';

// import { addDataChunksToIpfsCluster, pinToIpfsCluster } from './cluster-utils';
import { getIpfsContentFromDb, addIpfsContentToDb } from './db-utils';
import { addDataChunksToIpfsCluster } from './cluster-utils';
import { convertTimeToMilliseconds } from '../helpers';
import { addToIpfsCluster } from './cluster-utils';
import { contentToUint8Array, detectCybContentType } from './content-utils';

const FILE_SIZE_DOWNLOAD = 20 * 10 ** 6;

Expand Down Expand Up @@ -158,6 +158,7 @@ const fetchIPFSContentFromNode = async (
// if already pinned skip pin
if (!meta.local && allowedSize) {
node.pin.add(cid);

meta.pinTime = Date.now() - meta.catTime;
} else {
meta.pinTime = -1;
Expand Down Expand Up @@ -347,35 +348,20 @@ const catIPFSContentFromNode = (

const addContenToIpfs = async (
node: AppIPFS,
content: ImportCandidate
content: File | string
): Promise<Option<string>> => {
let cid: AddResult;
let arrayBuffer: Buffer | ArrayBuffer | undefined;

// let arrayBuffer: Buffer | ArrayBuffer | undefined;
let addResult;
if (node) {
if (typeof content === 'string') {
cid = await node.add(Buffer.from(content), { pin: true });
arrayBuffer = Buffer.from(content);
} else {
cid = await node.add(content, { pin: true });
if (content instanceof File) {
arrayBuffer = await content.arrayBuffer();
}
}
addResult = await node.add(content, { pin: true });
}
const pinResponse = await addToIpfsCluster(content);

if (arrayBuffer) {
const raw = new Uint8Array(arrayBuffer);
const result = await addDataChunksToIpfsCluster(
cid.cid.toString(),
raw,
node.nodeType === 'embedded'
);
const cid = addResult?.path || pinResponse?.cid;

console.log('result', result);
}
return cid.path;
}
return undefined;
await addIpfsContentToDb(cid, await contentToUint8Array(content));

return cid;
};

const CYBER_NODE_SWARM_PEER_ID =
Expand Down

0 comments on commit 3bab51e

Please sign in to comment.