All files defaultWorkflow.js

89.28% Statements 50/56
86.36% Branches 19/22
100% Functions 2/2
89.28% Lines 50/56

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 571x 1x 1x 1x 1x 1x 1x 11x 11x 11x 11x 11x 2x 2x 9x 11x 11x 11x 11x 5x     4x 11x 2x 1x 1x 2x     2x 2x 11x 1x 11x     1x 11x 1x 1x 6x 6x 6x 6x 11x 11x 2x 2x 2x 2x 2x 2x 11x 4x 6x  
import { extractWorkflow } from './pngMetadata.js';
 
const DEFAULT_WORKFLOW_CANDIDATES = [
  { path: '/default-workflow.json', type: 'json' },
  { path: '/default-workflow.png', type: 'png' },
];
 
async function loadCandidate(candidate, fetchImpl, extractWorkflowFn) {
  let response;
  try {
    response = await fetchImpl(candidate.path, { cache: 'no-store' });
  } catch {
    return null;
  }
 
  const contentType = response.headers?.get?.('content-type') || '';
  const isHtmlFallback = typeof contentType === 'string' && contentType.toLowerCase().includes('text/html');
 
  if (!response.ok) {
    if (response.status === 404 || response.status === 0) return null;
    throw new Error(`Failed to load ${candidate.path} (${response.status})`);
  }
 
  if (candidate.type === 'json') {
    if (isHtmlFallback) return null;
    try {
      return await response.json();
    } catch {
      throw new Error(`${candidate.path} is not valid JSON`);
    }
  }
 
  if (isHtmlFallback) return null;
  const workflow = await extractWorkflowFn(await response.blob());
  if (!workflow) {
    throw new Error(`${candidate.path} does not contain embedded workflow metadata`);
  }
  return workflow;
}
 
export async function loadDefaultWorkflowAsset({
  fetchImpl = fetch,
  extractWorkflowFn = extractWorkflow,
} = {}) {
  for (const candidate of DEFAULT_WORKFLOW_CANDIDATES) {
    const workflow = await loadCandidate(candidate, fetchImpl, extractWorkflowFn);
    if (workflow) {
      return {
        source: candidate.path,
        format: candidate.type,
        workflow,
      };
    }
  }
  return null;
}