diff --git a/packages/common/CHANGELOG.md b/packages/common/CHANGELOG.md index 464bcce10d..eb13a8f0e9 100644 --- a/packages/common/CHANGELOG.md +++ b/packages/common/CHANGELOG.md @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] +### Changed +- Expose CID on IPFS Reader (#2551) ## [5.1.1] - 2024-08-14 ### Added diff --git a/packages/common/src/project/readers/ipfs-reader.ts b/packages/common/src/project/readers/ipfs-reader.ts index 877117f1df..a51204d9b5 100644 --- a/packages/common/src/project/readers/ipfs-reader.ts +++ b/packages/common/src/project/readers/ipfs-reader.ts @@ -17,7 +17,7 @@ export class IPFSReader implements Reader { private ipfs: IPFSHTTPClientLite; private cache: Record> = {}; - constructor(private readonly cid: string, gateway?: string) { + constructor(readonly cid: string, gateway?: string) { if (!CIDv0.test(cid) && !CIDv1.test(cid)) { throw new Error('IPFS project path CID is not valid'); } diff --git a/packages/node-core/CHANGELOG.md b/packages/node-core/CHANGELOG.md index 5f98cdc8e6..3567cee1fa 100644 --- a/packages/node-core/CHANGELOG.md +++ b/packages/node-core/CHANGELOG.md @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] +### Changed +- Reuse the same temp dir when project is from IPFS (#2551) ## [14.1.3] - 2024-08-30 ### Fixed diff --git a/packages/node-core/src/configure/configure.module.ts b/packages/node-core/src/configure/configure.module.ts index 4ca178e850..0766a3a7cf 100644 --- a/packages/node-core/src/configure/configure.module.ts +++ b/packages/node-core/src/configure/configure.module.ts @@ -1,7 +1,10 @@ // Copyright 2020-2024 SubQuery Pte Ltd authors & contributors // SPDX-License-Identifier: GPL-3.0 -import {handleCreateSubqueryProjectError, LocalReader, makeTempDir, ReaderFactory} from '@subql/common'; +import fs from 'fs'; +import os from 'os'; +import path from 'path'; +import {handleCreateSubqueryProjectError, IPFSReader, LocalReader, makeTempDir, ReaderFactory} from '@subql/common'; import {IEndpointConfig, Reader} from '@subql/types-core'; import {camelCase, isNil, omitBy} from 'lodash'; import {ISubqueryProject} from '../indexer'; @@ -64,13 +67,10 @@ export function yargsToIConfig(yargs: Args, nameMapping: Record value = [value]; } if (Array.isArray(value)) { - value = value.reduce( - (acc, endpoint, index) => { - acc[endpoint] = endpointConfig[index] ?? {}; - return acc; - }, - {} as Record - ); + value = value.reduce((acc, endpoint, index) => { + acc[endpoint] = endpointConfig[index] ?? {}; + return acc; + }, {} as Record); } } if (key === 'primary-network-endpoint') { @@ -95,8 +95,18 @@ let rootDir: string; async function getCachedRoot(reader: Reader, configRoot?: string): Promise { if (reader instanceof LocalReader) return reader.root; + // Case for in workers when the parent has decided the directory if (configRoot) return configRoot; + // Allows reusing the same directory on restarts when project is run from ipfs, this can stop duplicating files in the tmp dir + if (reader instanceof IPFSReader) { + rootDir = path.resolve(os.tmpdir(), reader.cid); + if (!fs.existsSync(rootDir)) { + await fs.promises.mkdir(rootDir); + } + return rootDir; + } + if (!rootDir) { rootDir = await makeTempDir(); }