Setting the file. One moment.
Index · Vercel Optimize · vercel-labs/agent-skills · Skills Docs
ContentsBack to the top of the page lib/sanitizers/ index.mjs
JavaScript · 79 lines · 3 KB
preRelease
from
'./pre-release.mjs'
;
9 import * as middlewareConflict from './middleware-conflict.mjs' ;
10 import * as undeclaredDep from './undeclared-dep.mjs' ;
11 import * as countCorrect from './count-correct.mjs' ;
12 import * as renderingModeMislabel from './rendering-mode-mislabel.mjs' ;
13 import * as windowUnits from './window-units.mjs' ;
14 import * as functionDurationInvocations from './function-duration-invocations.mjs' ;
15 import * as botProtectionCertainty from './bot-protection-certainty.mjs' ;
16 import * as cacheTagInvalidationCertainty from './cache-tag-invalidation-certainty.mjs' ;
17 import * as missingCitation from './missing-citation.mjs' ;
18
19 export const SANITIZERS = [
20 vercelDirectiveStrip,
21 rateLimit,
22 preRelease,
23 middlewareConflict,
24 undeclaredDep,
25 countCorrect,
26 renderingModeMislabel,
27 windowUnits,
28 functionDurationInvocations,
29 botProtectionCertainty,
30 cacheTagInvalidationCertainty,
31 ];
32
33 export function recordSanitizer ( rec , tag ) {
34 rec.sanitizerTrail = rec.sanitizerTrail ?? [];
35 rec.sanitizerTrail. push (tag);
36 }
37
38 export async function applySanitizers ( rec , ctx = {}) {
39 applyDollarStrip (rec);
40
41 for ( const s of SANITIZERS ) {
42 const result = s. apply (rec, ctx) ?? {};
43 const tags = result.tags ?? (result.tag ? [result.tag] : []);
44 for ( const t of tags) recordSanitizer (rec, t);
45 if (result.needsReview) rec.needsReview = true ;
46 if (result.dropped) {
47 return { kept: false , rec, dropReason: tags[ 0 ] ?? `dropped-by:${ s . metadata ?. id ?? 'unknown'}` };
48 }
49 }
50
51 if (ctx.framework && ctx.version) {
52 const before = (rec.citations ?? []). slice ();
53 const { strippedUnknown , strippedVersion } = await sanitizeCitations (rec, ctx.framework, ctx.version);
54 for ( const u of strippedUnknown) recordSanitizer (rec, `unknown-citation:${ u }` );
55 for ( const u of strippedVersion) recordSanitizer (rec, `version-mismatch:${ u }` );
56 const lostAny = strippedUnknown. length > 0 || strippedVersion. length > 0 ;
57 const lostAll = lostAny && (rec.citations ?? []). length === 0 && before. length > 0 ;
58 if (lostAll) rec.needsReview = true ;
59 }
60
61 // missing-citation runs LAST so citation strippers above can starve a rec.
62 const missing = missingCitation. apply (rec, ctx) ?? {};
63 if (missing.dropped) {
64 return { kept: false , rec, dropReason: missing.tag ?? 'missing-citation' };
65 }
66
67 return { kept: true , rec };
68 }
69
70 export async function applySanitizersBatch ( recs , ctx = {}) {
71 const kept = [];
72 const dropped = [];
73 for ( const rec of recs) {
74 const r = await applySanitizers (rec, ctx);
75 if (r.kept) kept. push (r.rec);
76 else dropped. push ({ rec: r.rec, dropReason: r.dropReason });
77 }
78 return { kept, dropped };
79 }