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

fix(cli): decode instability #1415

Merged
merged 6 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@
"commander": "^12.1.0",
"debug": "^4.3.6",
"eth-provider": "^0.13.6",
"fastq": "^1.17.1",
"fs-extra": "^11.2.0",
"lodash": "^4.17.21",
"prompts": "^2.4.2",
Expand Down
51 changes: 33 additions & 18 deletions packages/cli/src/package.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import Debug from 'debug';
import { CannonStorage, DeploymentInfo } from '@usecannon/builder';
import { promise as createQueue, queueAsPromised } from 'fastq';
import { createDefaultReadRegistry } from './registry';
import { resolveCliSettings } from './settings';
import { getMainLoader } from './loader';
Expand All @@ -18,34 +17,50 @@ export async function readDeploy(packageRef: string, chainId: number) {
* Get a list of all the deployments recursively that are imported by the given deployment. Keep in mind
* that it will only return unique builds, not necessarily one per import/provision.
*/
export async function readDeployRecursive(packageRef: string, chainId: number) {
export async function readDeployRecursive(packageRef: string, chainId: number): Promise<DeploymentInfo[]> {
debug('readDeployTree', packageRef, chainId);

const store = await _getStore();
const deployInfo = await _readDeploy(store, packageRef, chainId);

const result = new Map<string, DeploymentInfo | null>();

const __readImports = async (info: DeploymentInfo) => {
async function processDeployment(info: DeploymentInfo): Promise<void> {
const importUrls = _deployImports(info).map(({ url }) => url);
await Promise.all(importUrls.map((url) => queue.push(url)));
};

const queue: queueAsPromised<string> = createQueue(async (url) => {
if (result.has(url)) return;
debug('readDeployTree child', url);

result.set(url, null); // Avoid double fetching/recursion
const info = (await store.readBlob(url)) as DeploymentInfo;
if (!info) throw new Error(`deployment not found: ${url}`);
result.set(url, info); // Set fetched value

await __readImports(info);
}, 5);
await Promise.all(
importUrls.map(async (url) => {
if (result.has(url)) return;

debug('readDeployTree child', url);
try {
// Avoid double fetching/recursion, by setting an empty value
// result.has(url) is going to return true before finishing the readBlob call.
result.set(url, null);

const childInfo = (await store.readBlob(url)) as DeploymentInfo;
if (!childInfo) throw new Error(`deployment not found: ${url}`);

result.set(url, childInfo);
await processDeployment(childInfo);
} catch (error) {
if (error instanceof Error) {
debug(`Error processing ${url}: ${error.message}`);
} else {
debug(`Error processing ${url}: ${error}`);
}
}
})
);
}

await __readImports(deployInfo);
try {
await processDeployment(deployInfo);
} catch (error) {
debug('Error processing deployments:', error);
}

return [deployInfo, ...result.values()] as DeploymentInfo[];
return [deployInfo, ...Array.from(result.values()).filter((val) => !!val)];
}

function _deployImports(deployInfo: DeploymentInfo) {
Expand Down
Loading
Loading