a collection of lightweight TypeScript packages for AT Protocol, the protocol powering Bluesky
atproto bluesky typescript npm
101
fork

Configure Feed

Select the types of activity you want to include in your feed.

refactor(lex-cli): simplify blob accept constraints with wildcard deduplication

Mary f6cf57b2 43b2d762

+38 -5
+5
.changeset/simplify-blob-accept-codegen.md
··· 1 + --- 2 + '@atcute/lex-cli': patch 3 + --- 4 + 5 + simplify blob accept constraints by removing specific types covered by wildcards
+33 -5
packages/lexicons/lex-cli/src/codegen.ts
··· 59 59 60 60 const PURE = `/*#__PURE__*/`; 61 61 62 + const simplifyAccept = (accept: string[] | undefined): string[] | undefined => { 63 + if (accept === undefined || accept.length === 0 || accept.includes('*/*')) { 64 + return undefined; 65 + } 66 + 67 + const wildcards = new Set<string>(); 68 + for (const mime of accept) { 69 + if (mime.endsWith('/*')) { 70 + wildcards.add(mime.slice(0, mime.indexOf('/'))); 71 + } 72 + } 73 + 74 + if (wildcards.size === 0) { 75 + return accept; 76 + } 77 + 78 + const simplified = accept.filter((mime) => { 79 + if (mime.endsWith('/*')) { 80 + return true; 81 + } 82 + return !wildcards.has(mime.slice(0, mime.indexOf('/'))); 83 + }); 84 + 85 + return simplified.length > 0 ? simplified : undefined; 86 + }; 87 + 62 88 export function* generateLexiconApi(opts: LexiconApiOptions): Generator<SourceFile> { 63 89 const importExt = opts.modules?.importSuffix; 64 90 ··· 658 684 break; 659 685 } 660 686 case 'blob': { 661 - if (spec.accept) { 662 - const accept = spec.accept.map((mime) => mime.replace(/\*\//g, '*\\/')).join(', '); 663 - lines.push(`@accept ${accept}`); 687 + const accept = simplifyAccept(spec.accept); 688 + if (accept) { 689 + const formatted = accept.map((mime) => mime.replace(/\*\//g, '*\\/')).join(', '); 690 + lines.push(`@accept ${formatted}`); 664 691 } 665 692 if (spec.maxSize !== undefined) { 666 693 lines.push(`@maxSize ${spec.maxSize}`); ··· 896 923 pipe.push(`${PURE} v.blobSize(${lit(spec.maxSize)})`); 897 924 } 898 925 899 - if (spec.accept !== undefined && spec.accept.length > 0 && !spec.accept.includes('*/*')) { 900 - pipe.push(`${PURE} v.blobAccept(${lit(spec.accept)})`); 926 + const accept = simplifyAccept(spec.accept); 927 + if (accept !== undefined) { 928 + pipe.push(`${PURE} v.blobAccept(${lit(accept)})`); 901 929 } 902 930 903 931 let call = `${PURE} v.blob()`;