Skip to content

fix: defend against bad mdapi responses #1236

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

Merged
merged 1 commit into from
Feb 9, 2024
Merged
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
36 changes: 21 additions & 15 deletions src/resolve/treeContainers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ import { Readable } from 'node:stream';
import { statSync, existsSync, readdirSync, createReadStream, readFileSync } from 'graceful-fs';
import * as JSZip from 'jszip';
import { Messages, SfError } from '@salesforce/core';
import { baseName, parseMetadataXml } from '../utils';
import { SourcePath } from '../common';
import { VirtualDirectory } from './types';
import { isString } from '@salesforce/ts-types';
import { baseName, parseMetadataXml } from '../utils/path';
import type { SourcePath } from '../common/types';
import type { VirtualDirectory } from './types';

Messages.importMessagesDirectory(__dirname);
const messages = Messages.loadMessages('@salesforce/source-deploy-retrieve', 'sdr');
Expand Down Expand Up @@ -241,18 +242,23 @@ export class VirtualTreeContainer extends TreeContainer {
public static fromFilePaths(paths: string[]): VirtualTreeContainer {
// a map to reduce array iterations
const virtualDirectoryByFullPath = new Map<string, VirtualDirectory>();
paths.map((filename) => {
const splits = filename.split(sep);
for (let i = 0; i < splits.length - 1; i++) {
const fullPathSoFar = splits.slice(0, i + 1).join(sep);
const existing = virtualDirectoryByFullPath.get(fullPathSoFar);
virtualDirectoryByFullPath.set(fullPathSoFar, {
dirPath: fullPathSoFar,
// only add to children if we don't already have it
children: Array.from(new Set(existing?.children ?? []).add(splits[i + 1])),
});
}
});
paths
.filter(
// defending against undefined being passed in. The metadata API sometimes responds missing fileName
isString
)
.map((filename) => {
const splits = filename.split(sep);
for (let i = 0; i < splits.length - 1; i++) {
const fullPathSoFar = splits.slice(0, i + 1).join(sep);
const existing = virtualDirectoryByFullPath.get(fullPathSoFar);
virtualDirectoryByFullPath.set(fullPathSoFar, {
dirPath: fullPathSoFar,
// only add to children if we don't already have it
children: Array.from(new Set(existing?.children ?? []).add(splits[i + 1])),
});
}
});
return new VirtualTreeContainer(Array.from(virtualDirectoryByFullPath.values()));
}

Expand Down