import { readdirSync, readFileSync } from 'node:fs'; import { join, relative } from 'node:path'; import { fileURLToPath } from 'node:url'; const marker = /\/\/ compat\(client-0275\): ([a-z0-9]+(?:-[a-z0-9]+)*)(?:\s*--\s*\S.*)?$/; const forbiddenWire = /"space:|\b\w*(?:SocketIo|EngineIo)\w*\b|"\/graphql|"affine_session"|"x-affine-version"|"\/api\/(?:auth|oauth)\/|"affine_csrf_token"|"x-affine-(?:client-kind|refresh-attempt)"/; const guardedRoots = [ 'packages/common/store/src', 'packages/frontend/client/src', 'packages/frontend/desktop/src', 'packages/frontend/mobile/src', 'blocksuite/crates', ]; function* rustFiles(root) { for (const entry of readdirSync(root, { withFileTypes: true })) { const path = join(root, entry.name); if ( entry.isDirectory() && !['target', 'node_modules', '.git'].includes(entry.name) ) { yield* rustFiles(path); } else if (entry.isFile() && entry.name.endsWith('.rs')) { yield path; } } } export function checkWireSources(sources, backlog) { const errors = []; const markers = new Map(); const headings = new Set(); for (const match of backlog.matchAll(/^### (.+)\s*$/gm)) { headings.add(match[1].trim()); } for (const [path, source] of sources) { source.split('\n').forEach((line, index) => { const location = `${path}:${index + 1}`; const slug = marker.exec(line)?.[1]; if (slug && markers.has(slug)) { errors.push( `${location}: marker ${slug} already occurs at ${markers.get(slug)}` ); } else if (slug) { markers.set(slug, location); } if (forbiddenWire.test(line) && !slug) { errors.push( `${location}: legacy wire syntax requires a compatibility marker` ); } }); } for (const [slug, location] of markers) { if (!headings.has(slug)) { errors.push(`${location}: no ablation backlog entry for ${slug}`); } } return { errors, markers }; } export function checkClientWireBoundary(root, backlog) { const sources = guardedRoots.flatMap(dir => [...rustFiles(join(root, dir))].map(path => [ relative(root, path), readFileSync(path, 'utf8'), ]) ); return checkWireSources(sources, backlog); } const corePackages = [ 'affine-store', 'affine-client', 'affine-desktop', 'affine-mobile', 'blocksuite-playground-doc', 'blocksuite-editor', 'playground', 'blocksuite-preview', ]; const forbiddenWasm = new Set([ 'reqwest', 'hyper', 'rustls', 'tokio', 'graphql_client', 'tokio-tungstenite', 'openssl', 'native-tls', ]); export function checkNormalDependencies(metadata, wasm) { const packages = new Map(metadata.packages.map(pkg => [pkg.id, pkg.name])); const nodes = new Map( metadata.resolve.nodes.map(node => [node.id, node.deps]) ); const members = new Map( metadata.workspace_members.map(id => [packages.get(id), id]) ); const errors = []; function visit(from, forbidden) { const visited = new Set(); const pending = [from]; while (pending.length) { const id = pending.pop(); if (visited.has(id)) continue; visited.add(id); const name = packages.get(id); if (forbidden.has(name)) { errors.push( `${packages.get(from)} can reach ${name} through normal dependencies` ); } for (const dep of nodes.get(id) ?? []) { if (dep.dep_kinds.some(edge => edge.kind === null)) pending.push(dep.pkg); } } } for (const name of corePackages) { if (!members.has(name)) { errors.push(`missing core workspace package ${name}`); continue; } visit(members.get(name), new Set(['affine-compat-v027'])); } if (wasm) { for (const id of members.values()) visit(id, forbiddenWasm); } return errors; } if (process.argv[1] === fileURLToPath(import.meta.url)) { const [path, target] = process.argv.slice(2); const errors = checkNormalDependencies( JSON.parse(readFileSync(path, 'utf8')), target === 'wasm32-unknown-unknown' ); for (const error of errors) console.error(error); if (errors.length) process.exitCode = 1; }