Crawlee—A web scraping and browser automation library for Node.js to build reliable crawlers. In JavaScript and TypeScript. Extract data for AI, LLMs, RAG, or GPTs. Download HTML, PDF, JPG, PNG, and other files from websites. Works with Puppeteer, Playwright, Cheerio, JSDOM, and raw HTTP. Both headful and headless mode. With proxy rotation.
TypeScript apifyautomationcrawlercrawlingheadlessheadless-chromejavascriptnodejsnpmplaywrightpuppeteerscraperscrapingtypescriptweb-crawlerweb-crawlingweb-scraping
chore: move the api surface extractor to its own repo (#4045)
Jindřich Bärcceca2800139
diff --git a/package.json b/package.jsonindex a82cccc07..9f26609e1 100644--- a/package.json+++ b/package.json@@ -49,8 +49,8 @@"publish:prod": "lerna publish from-package --contents dist --force-publish --yes","release:prod": "pnpm build && pnpm publish:prod","release:pin-versions": "turbo run copy -- -- --pin-versions",- "api:extract": "pnpm build && tsx scripts/api-extractor/run.ts",- "api:check": "tsx scripts/api-extractor/run.ts --verify",+ "api:extract": "pnpm build && pnpm dlx github:apify/api-extractor-report --exclude=@crawlee/cli,@crawlee/templates --extract-command='pnpm api:extract'",+ "api:check": "pnpm dlx github:apify/api-extractor-report --exclude=@crawlee/cli,@crawlee/templates --extract-command='pnpm api:extract' --verify","lint": "oxlint packages test docs --tsconfig=tsconfig.json --type-aware","lint:fix": "oxlint packages test docs --tsconfig=tsconfig.json --type-aware --fix","format": "oxfmt packages test docs --write",@@ -74,7 +74,6 @@"@crawlee/stagehand": "workspace:*","@crawlee/types": "workspace:*","@crawlee/utils": "workspace:*",- "@microsoft/api-extractor": "^7.58.9","@oxlint/plugins": "^1.62.0","@playwright/browser-chromium": "1.61.1","@playwright/browser-firefox": "1.61.1",diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex bceb7a212..3f10a87df 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -281,9 +281,6 @@ importers:'@crawlee/utils':specifier: workspace:*version: link:packages/utils- '@microsoft/api-extractor':- specifier: ^7.58.9- version: 7.58.9(@types/[email protected])'@oxlint/plugins':specifier: ^1.62.0version: 1.62.0@@ -3453,19 +3450,6 @@ packages:'@mermaid-js/[email protected]':resolution: {integrity: sha512-oYPyv8A4As1yH5Bx+04iQEQxXuIQDe0GKCNSRgao6z8AM9jixXIfP0vsppRLvGf+nKIOb9/LdpWA4YuJiVvESA==}- '@microsoft/[email protected]':- resolution: {integrity: sha512-aIcoQggPyer3B6Ze3usz0YWC/oBwUHfRH5ETUsr+oT2BRA6SfTJl7IKPcPZkX4UR+PohowzW4uMxsvjrn8vm+w==}-- '@microsoft/[email protected]':- resolution: {integrity: sha512-S2UF4yza5GoxCmf7hJQNxJNZN9ltOVuOQv8Dy+Z21aol5ERoBNMdWcQHm4MJMPPItW4H/4rZD906iaf4mUojJA==}- hasBin: true-- '@microsoft/[email protected]':- resolution: {integrity: sha512-9brPoVdfN9k9g0dcWkFeA7IH9bbcttzDJlXvkf8b2OBzd5MueR1V2wkKBL0abn0otvmkHJC6aapBOTJDDeMCZg==}-- '@microsoft/[email protected]':- resolution: {integrity: sha512-xgAyonlVVS+q7Vc7qLW0UrJU7rSFcETRWsqdXZtjzRU8dF+6CkozTK4V4y1LwOX7j8r/vHphjDeMeGI4tNGeGA==}-'@modelcontextprotocol/[email protected]':resolution: {integrity: sha512-zo37mZA9hJWpULgkRpowewez1y6ML5GsXJPY8FI0tBBCd77HEvza4jDqRKOXgHNn867PVGCyTdzqpz0izu5ZjQ==}engines: {node: '>=18'}@@ -4299,36 +4283,6 @@ packages:'@rtsao/[email protected]':resolution: {integrity: sha512-zt6OdqaDoOnJ1ZYsCYGt9YmWzDXl4vQdKTyJev62gFhRGKdx7mcT54V9KIjg+d2wi9EXsPvAPKe7i7WjfVWB8g==}- '@rushstack/[email protected]':- resolution: {integrity: sha512-wlKmIKIYCKuCASbITvOxLZXepPbwXvrv7S6ig6XNWFchSyhL/E2txmVXspHY49Wu2dzf7nI27a2k/yV5BA3EiA==}- peerDependencies:- '@types/node': '*'- peerDependenciesMeta:- '@types/node':- optional: true-- '@rushstack/[email protected]':- resolution: {integrity: sha512-gulfhBs6n+I5b7DvjKRfhMGyUejtSgOHTclF/eONr8hcgF1APEDjhxIsfdUYYMzC3rvLwGluqLjbwCFZ8nxrog==}- peerDependencies:- '@types/node': '*'- peerDependenciesMeta:- '@types/node':- optional: true-- '@rushstack/[email protected]':- resolution: {integrity: sha512-aAA518n6wxxjCfnTAOjQnm7ngNE0FVHxHAw2pxKlIhxrMn0XQjGcXKF0oKWpjBgJOmsaJpVob/v+zr3zxgPWuA==}-- '@rushstack/[email protected]':- resolution: {integrity: sha512-8ZQS4MMaGsv27EXCBiH7WMPkRZrffeDoIevs6z9TM5dzqiY6+Hn4evfK/G+gvgBTjfvfkHIZPQQmalmI2sM4TQ==}- peerDependencies:- '@types/node': '*'- peerDependenciesMeta:- '@types/node':- optional: true-- '@rushstack/[email protected]':- resolution: {integrity: sha512-fwI076HYknC0IrMXdY6UmjDv+PH7NHhNJX3/pY2UblSE5XrXgndXZPiOe/6ZtuFpn6DvVDVNhtkIzQ+Qu/MhVQ==}-'@sapphire/[email protected]':resolution: {integrity: sha512-cvGzxbba6sav2zZkH8GPf2oGk9yYoD5qrNWdu9fRehifgnFZJMV+nuy2nON2roRO4yQQ+v7MK/Pktl/HgfsUXg==}engines: {node: '>=v14.0.0', npm: '>=7.0.0'}@@ -4756,9 +4710,6 @@ packages:'@tybys/[email protected]':resolution: {integrity: sha512-6+7nlbMVX/PVDCwaIQ8nTOPveOcFLSt8GcXdx8hD0bt39uWxYT88uXzqTd4fTvqta7oeUJqudepapKNt2DYJFw==}- '@types/[email protected]':- resolution: {integrity: sha512-ebDJ9b0e702Yr7pWgB0jzm+CX4Srzz8RcXtLJDJB+BSccqMa36uyH/zUsSYao5+BD1ytv3k3rPYCq4mAE1hsXA==}-'@types/[email protected]':resolution: {integrity: sha512-HLFeCYgz89uk22N5Qg3dvGvsv46B8GLvKKo1zKG4NybA8U2DiEO3w9lqGg29t/tfLRJpJ6iQxnVw4OnB7MoM9g==}@@ -5575,14 +5526,6 @@ packages:peerDependencies:zod: ^3.25.76 || ^4.1.8- resolution: {integrity: sha512-mv00Te6nmYbRp5DCwclxtt7yV/joXJPGS7nM+97GdxvuttCOfgI3K4U25zboyeX0O+myI8ERluxQe5wljMmVIw==}- peerDependencies:- ajv: ^8.5.0- peerDependenciesMeta:- ajv:- optional: true-resolution: {integrity: sha512-Wx0Kx52hxE7C18hkMEggYlEifqWZtYaRgouJor+WMdPnQyEK13vgEWyVNup7SoeeoLMsr4kf5h6dOW11I15MUA==}peerDependencies:@@ -5684,9 +5627,6 @@ packages:resolution: {integrity: sha512-PYjyFOLKQ9y57JvQ6QLo8dAgNqswh8M1RMJYdQduT6xbWSgK36P/Z/v+p888pM69jMMfS8Xd8F6I1kQ/I9HUGg==}- resolution: {integrity: sha512-o5Roy6tNG4SL/FOkCAN6RzjiakZS25RLYFrcMttJqbdd8BWrnA+fGz57iN5Pb06pvBGvl5gQ0B48dJlslXvoTg==}-resolution: {integrity: sha512-8+9WqebbFzpX9OR+Wa6O29asIogeRMzcGtAINdpMHHyAg10f05aSFVBbcEqGf/PXw1EjAZ+q2/bEBg3DvurK3Q==}@@ -7078,10 +7018,6 @@ packages:resolution: {integrity: sha512-gCcO42XCHKEs7Ag0S7aGYsnJ7hlgrO3qderYqeiY0Eqk+0GFfuvT13IA0hHreJTa2KCdDVyGMeOhdMNmrrTjVg==}- resolution: {integrity: sha512-DPi0FmjiSU5EvQV0++GFDOJ9ASQUVFh5kD+OzOnYdi7n3Wpm9hWWGfB/O2blfHcMVTL5WkQXSnRiK9makhrcnw==}- engines: {node: '>=0.3.1'}-resolution: {integrity: sha512-kqag/Nl+f3GwyK25fhUMYj81BUOrZ9IuJsjIcDE5icNM9FJHAVm3VcUDxdLPoQtTuUylWm6ZIknYJwwaPxsUzg==}@@ -8879,9 +8815,6 @@ packages:resolution: {integrity: sha512-ekilCSN1jwRvIbgeg/57YFh8qQDNbwDb9xT/qu2DAHbFFZUicIl4ygVaAvzveMhMVr3LnpSKTNnwt8PoOfmKhQ==}hasBin: true- resolution: {integrity: sha512-8wb9Yw966OSxApiCt0K3yNJL8pnNeIv+OEq2YMidz4FKP6nonSRoOXc80iXY4JaN2FC11B9qsNmDsm+ZOfMROA==}-resolution: {integrity: sha512-1RuuER6kmt8K8I3nIWvPZKi5RQCb568ZPyY4Pwjlua+yo+63ZTmIwxLZH0heBmiKN4uxjvCiarDrjaeH84xicQ==}@@ -11909,9 +11842,6 @@ packages:resolution: {integrity: sha512-mTyOoPbrivtXnwnIxZRFYRrPNtEFKlpB2fvjSnCQUiAA6qAZzqwna5envK4uk6OIeP17CsdF3rSBGYVBsU0Tkg==}- resolution: {integrity: sha512-D9cPgkvLlV3t3IzL0D0YLvGA9Ahk4PcvVwUbN0dSGr1aP0Nrt4AEnTUbuGvquEC0mA64Gqt1fzirlRs5ibXx8g==}-resolution: {integrity: sha512-wvLeHgcVHKO8Sc/H/5lkGreJQVeYMm9rlmt8PuR1xE31rIuXhuzznUUqAt8MqLhB3MqJdFzlNAfpcWnxiFUcPw==}engines: {node: '>=12'}@@ -16286,41 +16216,6 @@ snapshots:dependencies:'@chevrotain/types': 11.1.2- '@microsoft/[email protected](@types/[email protected])':- dependencies:- '@microsoft/tsdoc': 0.16.0- '@microsoft/tsdoc-config': 0.18.1- '@rushstack/node-core-library': 5.23.1(@types/[email protected])- transitivePeerDependencies:- - '@types/node'-- '@microsoft/[email protected](@types/[email protected])':- dependencies:- '@microsoft/api-extractor-model': 7.33.8(@types/[email protected])- '@microsoft/tsdoc': 0.16.0- '@microsoft/tsdoc-config': 0.18.1- '@rushstack/node-core-library': 5.23.1(@types/[email protected])- '@rushstack/rig-package': 0.7.3- '@rushstack/terminal': 0.24.0(@types/[email protected])- '@rushstack/ts-command-line': 5.3.10(@types/[email protected])- diff: 8.0.4- minimatch: 9.0.9- resolve: 1.22.12- semver: 7.7.4- source-map: 0.6.1- typescript: 5.9.3- transitivePeerDependencies:- - '@types/node'-- '@microsoft/[email protected]':- dependencies:- '@microsoft/tsdoc': 0.16.0- ajv: 8.18.0- jju: 1.4.0- resolve: 1.22.12-- '@microsoft/[email protected]': {}-'@modelcontextprotocol/[email protected](@cfworker/[email protected])([email protected])([email protected])':dependencies:'@hono/node-server': 1.19.17([email protected])@@ -17074,45 +16969,6 @@ snapshots:'@rtsao/[email protected]': {}- '@rushstack/[email protected](@types/[email protected])':- dependencies:- ajv: 8.18.0- ajv-draft-04: 1.0.0([email protected])- ajv-formats: 3.0.1([email protected])- fs-extra: 11.3.4- import-lazy: 4.0.0- jju: 1.4.0- resolve: 1.22.12- semver: 7.7.4- optionalDependencies:- '@types/node': 24.12.2-- '@rushstack/[email protected](@types/[email protected])':- optionalDependencies:- '@types/node': 24.12.2-- '@rushstack/[email protected]':- dependencies:- jju: 1.4.0- resolve: 1.22.12-- '@rushstack/[email protected](@types/[email protected])':- dependencies:- '@rushstack/node-core-library': 5.23.1(@types/[email protected])- '@rushstack/problem-matcher': 0.2.1(@types/[email protected])- supports-color: 8.1.1- optionalDependencies:- '@types/node': 24.12.2-- '@rushstack/[email protected](@types/[email protected])':- dependencies:- '@rushstack/terminal': 0.24.0(@types/[email protected])- '@types/argparse': 1.0.38- argparse: 1.0.10- string-argv: 0.3.2- transitivePeerDependencies:- - '@types/node'-'@sapphire/[email protected]': {}'@sec-ant/[email protected]': {}@@ -17508,8 +17364,6 @@ snapshots:dependencies:tslib: 2.8.1- '@types/[email protected]': {}-'@types/[email protected]':dependencies:'@types/connect': 3.4.38@@ -18346,10 +18200,6 @@ snapshots:'@opentelemetry/api': 1.9.0zod: 4.4.3- optionalDependencies:- ajv: 8.18.0-optionalDependencies:ajv: 8.18.0@@ -18520,10 +18370,6 @@ snapshots:- dependencies:- sprintf-js: 1.0.3-@@ -20170,8 +20016,6 @@ snapshots:- [email protected]: {}-dependencies:bn.js: 4.12.3@@ -22514,8 +22358,6 @@ snapshots:- [email protected]: {}-dependencies:'@hapi/hoek': 9.3.0@@ -26596,8 +26438,6 @@ snapshots:dependencies:through: 2.3.8- [email protected]: {}-diff --git a/scripts/api-extractor/api-extractor.base.json b/scripts/api-extractor/api-extractor.base.jsondeleted file mode 100644index de57e9e7e..000000000--- a/scripts/api-extractor/api-extractor.base.json+++ /dev/null@@ -1,60 +0,0 @@-{- "$schema": "https://developer.microsoft.com/json-schemas/api-extractor/v7/api-extractor.schema.json",-- "newlineKind": "lf",-- "compiler": {- "overrideTsconfig": {- "compilerOptions": {- "module": "NodeNext",- "moduleResolution": "NodeNext",- "target": "ESNext",- "lib": ["DOM", "ES2023", "ES2024", "DOM.AsyncIterable"],- "skipLibCheck": true,- "strict": true- }- }- },-- "apiReport": {- "enabled": true,- "reportFolder": "<projectFolder>/../../docs/public-api",- "reportTempFolder": "<projectFolder>/../../docs/public-api/temp"- },-- "docModel": {- "enabled": false- },-- "dtsRollup": {- "enabled": false- },-- "tsdocMetadata": {- "enabled": false- },-- "messages": {- "compilerMessageReporting": {- "default": {- "logLevel": "none"- }- },- "extractorMessageReporting": {- "default": {- "logLevel": "none"- },- "ae-incompatible-release-tags": {- "logLevel": "warning"- },- "ae-forgotten-export": {- "logLevel": "warning"- }- },- "tsdocMessageReporting": {- "default": {- "logLevel": "none"- }- }- }-}diff --git a/scripts/api-extractor/run.ts b/scripts/api-extractor/run.tsdeleted file mode 100644index ad0171f5d..000000000--- a/scripts/api-extractor/run.ts+++ /dev/null@@ -1,669 +0,0 @@-/* eslint-disable no-console */-import { spawnSync } from 'node:child_process';-import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs';-import { basename, dirname, relative, resolve } from 'node:path';--import { Extractor, ExtractorConfig, type IConfigFile } from '@microsoft/api-extractor';-import { globbySync } from 'globby';-// The workspace `typescript` is the TS 7 native compiler, which no longer ships the JS API-// this script parses reports with; `typescript-v6` is an npm alias to classic TypeScript.-import ts from 'typescript-v6';--/**- * Generates (`--verify` to check) a per-package map of the public type-level interface of- * each publishable `@crawlee/*` package, committed to `docs/public-api/<package>.api.md`.- * These reports define where we promise backwards compatibility; changes must be reviewed.- *- * Before extraction we sanitize the `.d.ts` files (for the duration of the run, restoring them- * afterwards): (1) the build (`scripts/typescript_fixes.mjs`) injects `// @ts-ignore` comment- * lines that crash API Extractor's AST walker, so we strip them; (2) we rewrite the legacy- * JSDoc `@ignore` tag to `@internal`, since API Extractor only trims by release tag and would- * otherwise leak `@ignore`-d members into the `public` report. Rewriting `@ignore` also trims- * the members that were crashing API Extractor's AST walker (e.g. `BrowserLauncher`'s inline- * `import("ow")` `optionsShape`), so the affected packages now extract cleanly on the primary- * pass. A few packages may still re-export a comment-injected member across a package boundary- * and crash anyway; those are retried against a sanitized mirror of the dist tree with- * `@crawlee/*` deps remapped via tsconfig `paths`.- *- * A report should describe exactly the surface it maps: every name it mentions declared, and- * nothing declared that it does not mention. API Extractor gets us close but not there, because- * it decides what to *include* before it trims the non-`@public` declarations and never- * revisits that decision. Three things follow, and we handle each after extraction:- *- * 1. Imports of symbols only an `@internal` member used survive the trim and read as public- * surface (`Cookie` from `tough-cookie` in `@crawlee/core`). We drop them —- * see `pruneUnusedImports`.- * 2. Types the public API references but the entry point never exports would otherwise be- * named by the report and defined nowhere. `includeForgottenExports` emits them instead- * (without `export`, since they are observable but not importable) — the alternative was- * exporting ~38 types from their packages, committing us to names we never meant to- * publish. API Extractor labels them exactly like a real export, so we add a banner- * saying otherwise — see `annotateForgottenDeclarations`. The same pre-trim blind spot- * means it also emits declarations reachable only from trimmed members, which we drop —- * see `pruneDeadForgottenDeclarations`.- * 3. What (2) cannot supply, because the release-tag trim removes it first: a `@public`- * signature referencing an `@internal` type. That is a genuine tagging bug in the source,- * so the run fails — see `danglingReferences` and `INCOMPATIBLE_RELEASE_TAGS`.- *- * The pruning in (1) and (2) feed each other — dropping a declaration orphans its imports — so- * `pruneReport` runs them to a fixed point.- *- * When running under GitHub Actions (or with `--github`), failures are additionally emitted- * as workflow commands (`::error::`) so they show up as inline annotations in the CI run.- */--const root = resolve(import.meta.dirname, '..', '..');-const baseConfigPath = resolve(import.meta.dirname, 'api-extractor.base.json');-const baseConfig = JSON.parse(readFileSync(baseConfigPath, 'utf8')) as IConfigFile;-const reportFolder = resolve(root, 'docs', 'public-api');-// API Extractor writes the "public" variant to a `.public.api.md` staging file here; we then-// promote it onto the committed `<name>.api.md` ourselves (see `extract`), so the committed-// filenames stay stable while the report content is @public-only (no @internal symbols).-const stagingFolder = resolve(reportFolder, 'temp');-const mirrorRoot = resolve(root, 'node_modules', '.cache', 'api-extractor-dts');-const verify = process.argv.includes('--verify');--// Emit GitHub Actions workflow commands (annotations) when running in CI, so out-of-date-// reports and crashes surface as inline warnings/errors. Opt in with `--github` or force-// off with `--no-github` (auto-detected via the runner-set GITHUB_ACTIONS env var otherwise).-const github = process.argv.includes('--github')- || (process.env.GITHUB_ACTIONS === 'true' && !process.argv.includes('--no-github'));--// GitHub workflow commands must escape `%`, `\r` and `\n` in the message. See-// https://docs.github.com/en/actions/reference/workflows-and-actions/workflow-commands-const ghEscape = (message: string) => message.replace(/%/g, '%25').replace(/\r/g, '%0D').replace(/\n/g, '%0A');-const ghCommand = (kind: 'error' | 'warning', message: string) => {- if (github) console.log(`::${kind}::${ghEscape(message)}`);-};--const TS_IGNORE_LINE = /^\s*\/\/ @ts-ignore optional peer dependency or compatibility with es2022\s*$/;-// `@ignore` is a legacy JSDoc/TypeDoc tag that API Extractor does not act on — unlike the-// release tags (`@internal`/`@alpha`/`@beta`), it does NOT trim the member from the report,-// so `@ignore`-d symbols wrongly leak into the `public` variant. There is no config knob for-// this, so we rewrite the tag to `@internal` in the (transient) `.d.ts`, letting API-// Extractor's real release-tag trimming drop them from the public surface map. We only rewrite-// `@ignore` when it sits directly behind a JSDoc gutter (`/**` or a leading `*`), which covers-// both the single-line `/** @ignore */` and multi-line ` * @ignore` forms while leaving prose-// or string literals that merely mention "@ignore" untouched.-const IGNORE_TAG = /(\/\*\*|\*)(\s*)@ignore\b/g;-// CLI binary and project scaffolding are tooling, not an importable API where we promise BC.-const EXCLUDED = new Set(['@crawlee/cli', '@crawlee/templates']);--// The two API Extractor analyzer messages we act on (everything else is silenced to `none`-// in `api-extractor.base.json`); both describe a symbol the report references but never-// declares, which makes the committed surface map internally inconsistent.-//-// ae-incompatible-release-tags — a @public symbol's signature references an @internal one.-// The referenced type is trimmed from the @public report, so the surface map is left-// referring to a name that appears nowhere in it. Fatal: this is a genuine tagging bug,-// and the fix belongs in the source (drop the tag — untagged is implicitly public — or-// keep the type out of the public signature).-// ae-forgotten-export — a referenced symbol isn't exported from the entry point at all, so-// the report names a type consumers cannot import. Also fatal, but only once confirmed-// against the finished report: these messages are raised before the `@public` trim, so-// most of them concern symbols that never reach it (see `danglingReferences`).-const INCOMPATIBLE_RELEASE_TAGS = 'ae-incompatible-release-tags';-const FORGOTTEN_EXPORT = 'ae-forgotten-export';-type AnalyzerMessageId = typeof INCOMPATIBLE_RELEASE_TAGS | typeof FORGOTTEN_EXPORT;--/** Pulls `Foo` out of `The symbol "Foo" needs to be exported by the entry point index.d.ts`. */-const quotedSymbol = (text: string) => text.match(/"([^"]+)"/)?.[1] ?? text;--interface PackageManifest {- name: string;- private?: boolean;- types?: string;- exports?: Record<string, string | { types?: string }>;-}--const packageJsonPaths = globbySync('packages/*/package.json', { cwd: root, absolute: true }).sort();--function manifest(pkgJsonPath: string): PackageManifest {- return JSON.parse(readFileSync(pkgJsonPath, 'utf8')) as PackageManifest;-}--function dtsEntry(pkgDir: string, pkg: PackageManifest): string | undefined {- const dot = pkg.exports?.['.'];- const candidate = (typeof dot === 'object' ? dot.types : undefined) ?? pkg.types ?? './dist/index.d.ts';- const full = resolve(pkgDir, candidate);- return existsSync(full) ? full : undefined;-}--const sanitizeDts = (content: string) =>- content- .split('\n')- .filter((line) => !TS_IGNORE_LINE.test(line))- .join('\n')- .replace(IGNORE_TAG, '$1$2@internal');--type ImportStatement = ts.ImportDeclaration | ts.ImportEqualsDeclaration;-const isImport = (node: ts.Node): node is ImportStatement =>- ts.isImportDeclaration(node) || ts.isImportEqualsDeclaration(node);--/** Local binding names introduced by an import statement (`[]` for a side-effect import). */-function importBindings(node: ImportStatement): string[] {- if (ts.isImportEqualsDeclaration(node)) return [node.name.text];- const clause = node.importClause;- if (!clause) return [];- const names = clause.name ? [clause.name.text] : [];- const bound = clause.namedBindings;- if (bound && ts.isNamespaceImport(bound)) names.push(bound.name.text);- if (bound && ts.isNamedImports(bound)) names.push(...bound.elements.map((element) => element.name.text));- return names;-}--/**- * Drops import statements whose binding is never referenced in the rest of the report.- *- * API Extractor collects the import list from the entry point *before* it trims the- * non-`@public` declarations, and never revisits it — deliberately, since a rollup may- * legitimately need an import that its release-tag filter would have dropped. In the API- * report, though, a type reachable only from an `@internal` member survives as a bare- * import and reads as public surface (e.g. `Cookie` from `tough-cookie` in `@crawlee/core`),- * producing review noise on changes that never touched the public API. There is no config- * knob for this, so we post-process the report.- *- * The report body is ordinary TypeScript, so we parse it rather than pattern-match lines:- * bindings come from real import nodes (covering every shape the emitter produces, including- * `import X = require(...)`), and usages from real identifier tokens — so a name occurring- * only in a string literal or a `// Warning:` comment correctly does not count as a use.- */-function parseReport(report: string): { lines: string[]; open: number; close: number; source: ts.SourceFile } | undefined {- const lines = report.split('\n');- // The report is a fixed markdown skeleton wrapping a single ```ts fence.- const open = lines.indexOf('```ts');- const close = lines.lastIndexOf('```');- if (open === -1 || close <= open) return undefined;-- const source = ts.createSourceFile(- 'report.ts',- lines.slice(open + 1, close).join('\n'),- ts.ScriptTarget.Latest,- true,- ts.ScriptKind.TS,- );- // `parseDiagnostics` is internal, but there is no public per-file equivalent that doesn't- // require a whole Program. A report that doesn't parse means our assumptions are broken,- // so skip the analysis rather than act on a half-understood tree.- const diagnostics = (source as unknown as { parseDiagnostics?: readonly ts.Diagnostic[] }).parseDiagnostics;- if (diagnostics?.length) {- const message = `report did not parse (${diagnostics.length} syntax errors) — skipping import pruning and the dangling-reference check`;- console.warn(`! ${message}`);- ghCommand('warning', message);- return undefined;- }- return { lines, open, close, source };-}--function pruneUnusedImports(report: string): string {- const parsed = parseReport(report);- if (!parsed) return report;- const { lines, open, close, source } = parsed;-- const imports = source.statements.filter(isImport);- if (imports.length === 0) return report;-- const used = new Set<string>();- const collect = (node: ts.Node) => {- // Skip the import statements themselves so a binding never counts as its own usage.- if (isImport(node)) return;- if (ts.isIdentifier(node)) used.add(node.text);- node.forEachChild(collect);- };- source.forEachChild(collect);-- // Offset back into the surrounding markdown; `getStart` skips leading trivia so we never- // swallow a comment sitting above an import.- const lineOf = (position: number) => open + 1 + source.getLineAndCharacterOfPosition(position).line;- const unused = imports.filter((node) => {- const bindings = importBindings(node);- return bindings.length > 0 && bindings.every((binding) => !used.has(binding));- });- if (unused.length === 0) return report;-- const dropped = new Set<number>();- for (const node of unused) {- for (let line = lineOf(node.getStart(source)); line <= lineOf(node.getEnd()); line++) dropped.add(line);- }- // Emptying the block entirely would leave the blank line that separated it from the- // declarations stacked on the one after the ```ts fence; drop it so the output matches- // what API Extractor emits for an import-less report.- const after = lineOf(unused[unused.length - 1].getEnd()) + 1;- if (unused.length === imports.length && lines[after] === '') dropped.add(after);-- return lines.filter((_, index) => !dropped.has(index)).join('\n');-}--/**- * Drops forgotten-export declarations that nothing in the report refers to.- *- * `includeForgottenExports` emits a declaration for every symbol the entry point failed to- * export, but API Extractor decides that before the `@public` trim — so it also emits the ones- * reachable only from members that never make it into the report. Those exist to back a- * reference that isn't there, and they drag their own imports back in with them.- *- * Eligibility comes from API Extractor's own `ae-forgotten-export` list rather than from the- * absence of an `export` keyword. Missing `export` is not sufficient: `export * as ns` is- * rendered as `declare namespace ns { export { ... } }`, which also carries no export modifier- * yet is a real part of the surface — and removing it would cascade into everything it names- * (`@crawlee/utils`' `social` namespace and all its members). Only symbols API Extractor could- * not export are candidates, so nothing that is genuinely reachable can be dropped.- */-function pruneDeadForgottenDeclarations(report: string, forgotten: ReadonlySet<string>): string {- if (forgotten.size === 0) return report;- const parsed = parseReport(report);- if (!parsed) return report;- const { lines, open, close, source } = parsed;-- // Positions, not counts: a declaration must not keep itself alive. Backend classes here- // name themselves (`static create(): Promise<DatasetBackend>`), so a plain occurrence count- // would never let one go.- const occurrences = new Map<string, number[]>();- const record = (node: ts.Node) => {- // Only genuine references count. `storage.DatasetBackend` on an unrelated method must not- // keep the local `DatasetBackend` class alive, and nor must a member of the same name.- if (ts.isIdentifier(node) && !isDeclarationName(node)) {- const positions = occurrences.get(node.text);- if (positions) positions.push(node.getStart(source));- else occurrences.set(node.text, [node.getStart(source)]);- }- node.forEachChild(record);- };- source.forEachChild(record);-- const dead = source.statements.filter((statement) => {- if (isImport(statement) || ts.isExportDeclaration(statement)) return false;- const modifiers = ts.canHaveModifiers(statement) ? ts.getModifiers(statement) : undefined;- if (modifiers?.some((modifier) => modifier.kind === ts.SyntaxKind.ExportKeyword)) return false;- const names = ts.isVariableStatement(statement)- ? statement.declarationList.declarations.map((declaration) => declaration.name)- : [(statement as { name?: ts.Node }).name];- if (names.length === 0 || !names.every((name) => name && ts.isIdentifier(name))) return false;- const from = statement.getStart(source);- const to = statement.getEnd();- return (names as ts.Identifier[]).every(- (name) =>- forgotten.has(name.text)- && !(occurrences.get(name.text) ?? []).some((position) => position < from || position > to),- );- });- if (dead.length === 0) return report;-- const lineOf = (position: number) => open + 1 + source.getLineAndCharacterOfPosition(position).line;- const dropped = new Set<number>();- for (const statement of dead) {- // Take the `// @public (undocumented)` banner API Extractor writes above the declaration- // with it. Ask for the actual comment ranges rather than working back from- // `getFullStart()`, which sits at the end of the *previous* statement.- const comments = ts.getLeadingCommentRanges(source.text, statement.getFullStart()) ?? [];- const start = lineOf(comments[0]?.pos ?? statement.getStart(source));- for (let line = start; line <= lineOf(statement.getEnd()); line++) dropped.add(line);- // Also take the blank line that separated it from the previous entry, so the neighbours- // don't end up glued together.- if (start - 1 > open && lines[start - 1].trim() === '') dropped.add(start - 1);- }- return lines.filter((_, index) => !dropped.has(index)).join('\n');-}--/**- * Removes everything the report carries but does not need: forgotten-export declarations- * nothing refers to, and imports nothing refers to. Each pass can expose more of the other- * (dropping a declaration orphans the imports it used), so this runs to a fixed point. It- * always terminates — every pass that changes anything strictly removes lines.- */-function pruneReport(report: string, forgotten: ReadonlySet<string>): string {- for (let current = report; ; ) {- const next = pruneUnusedImports(pruneDeadForgottenDeclarations(current, forgotten));- if (next === current) return current;- current = next;- }-}--/** Banner marking a declaration that is observable but not importable. */-const NOT_EXPORTED_BANNER = '// Not exported by the entry point; reachable only as a referenced type.';--/**- * Labels the declarations `includeForgottenExports` contributed.- *- * Without this they render as `// @public (undocumented)`, identical to a genuine export apart- * from a missing `export` keyword — far too subtle to survive review. The distinction matters:- * their *shape* is part of the surface we promise not to break, but their *name* is not- * something a consumer can import.- */-function annotateForgottenDeclarations(report: string, forgotten: ReadonlySet<string>): string {- if (forgotten.size === 0) return report;- const parsed = parseReport(report);- if (!parsed) return report;- const { lines, open, source } = parsed;-- const lineOf = (position: number) => open + 1 + source.getLineAndCharacterOfPosition(position).line;- const marked = new Set<number>();- for (const statement of source.statements) {- if (isImport(statement) || ts.isExportDeclaration(statement)) continue;- const modifiers = ts.canHaveModifiers(statement) ? ts.getModifiers(statement) : undefined;- if (modifiers?.some((modifier) => modifier.kind === ts.SyntaxKind.ExportKeyword)) continue;- const names = ts.isVariableStatement(statement)- ? statement.declarationList.declarations.map((declaration) => declaration.name)- : [(statement as { name?: ts.Node }).name];- if (!names.length || !names.every((name) => name && ts.isIdentifier(name) && forgotten.has(name.text))) continue;- // Sit above API Extractor's own banner rather than replacing it, so its markers survive.- const comments = ts.getLeadingCommentRanges(source.text, statement.getFullStart()) ?? [];- const at = lineOf(comments[0]?.pos ?? statement.getStart(source));- // Our banner becomes a leading comment itself, so skip anything already labelled. The- // staged report is regenerated from scratch each run and never arrives annotated, but- // that is a property of the caller, not of this function.- if (lines[at] !== NOT_EXPORTED_BANNER) marked.add(at);- }- if (marked.size === 0) return report;-- return lines.flatMap((line, index) => (marked.has(index) ? [NOT_EXPORTED_BANNER, line] : [line])).join('\n');-}--/** True when the identifier names something (a declaration, member or property) rather than referring to it. */-function isDeclarationName(id: ts.Identifier): boolean {- const parent = id.parent as ts.Node | undefined;- if (!parent) return false;- // `a.b` / `A.B` — only the leftmost part resolves against the report's own scope.- if (ts.isQualifiedName(parent) && parent.right === id) return true;- if (ts.isPropertyAccessExpression(parent) && parent.name === id) return true;- // `export { x }` (as a `declare namespace` uses to re-expose its members) names the local- // binding, so it is a reference to it rather than a declaration of it.- if (ts.isExportSpecifier(parent)) return false;- return 'name' in parent && (parent as { name?: ts.Node }).name === id;-}--/** Names the report introduces itself: top-level declarations plus whatever the imports bind. */-function declaredNames(source: ts.SourceFile): Set<string> {- const names = new Set<string>();- for (const statement of source.statements) {- if (isImport(statement)) {- for (const binding of importBindings(statement)) names.add(binding);- } else if (ts.isVariableStatement(statement)) {- for (const declaration of statement.declarationList.declarations) {- if (ts.isIdentifier(declaration.name)) names.add(declaration.name.text);- }- } else {- const name = (statement as { name?: ts.Node }).name;- if (name && ts.isIdentifier(name)) names.add(name.text);- }- }- return names;-}--/**- * Names the report refers to but never declares or imports, leaving the committed surface map- * describing types a consumer cannot actually name.- *- * `candidates` comes from API Extractor's `ae-forgotten-export` messages. Restricting the search- * to those is what makes this check cheap and exact: deciding "is this identifier unresolved?"- * from scratch would mean re-implementing name resolution over the TypeScript globals, whereas- * API Extractor has already told us precisely which symbols it could not export.- *- * The filtering matters in the other direction too. Those messages are raised during analysis,- * *before* the `@public` trim, so most of them concern symbols reachable only from members that- * never make it into the report — harmless, and not something the source should be contorted to- * fix. Checking the finished artifact instead of the raw message list keeps only the real ones.- */-function danglingReferences(report: string, candidates: Iterable<string>): string[] {- const wanted = new Set(candidates);- if (wanted.size === 0) return [];- const parsed = parseReport(report);- if (!parsed) return [];-- const declared = declaredNames(parsed.source);- const dangling = new Set<string>();- const visit = (node: ts.Node) => {- if (ts.isIdentifier(node) && wanted.has(node.text) && !declared.has(node.text) && !isDeclarationName(node)) {- dangling.add(node.text);- }- node.forEachChild(visit);- };- parsed.source.forEachChild(visit);- return [...dangling].sort();-}--const reportBaseName = (name: string) => name.replace('@', '').replace('/', '-');-const reportFileName = (name: string) => `${reportBaseName(name)}.api.md`;-// With `reportVariants: ['public']`, API Extractor appends the variant kind to the file name,-// producing `<base>.public.api.md`. We stage that, then promote it to `<base>.api.md`.-const stagedFileName = (name: string) => `${reportBaseName(name)}.public.api.md`;--/** Lazily built sanitized mirror of the dist tree, with a `@crawlee/*` -> mirror paths map. */-let mirror: { packages: string; paths: Record<string, string[]> } | undefined;-function getMirror() {- if (mirror) return mirror;- rmSync(mirrorRoot, { recursive: true, force: true });- for (const file of globbySync('packages/*/dist/**/*.d.ts', { cwd: root, absolute: true })) {- const target = resolve(mirrorRoot, relative(root, file));- mkdirSync(dirname(target), { recursive: true });- writeFileSync(target, sanitizeDts(readFileSync(file, 'utf8')));- }- const packages = resolve(mirrorRoot, 'packages');- const paths: Record<string, string[]> = {};- for (const pkgJsonPath of packageJsonPaths) {- const dir = resolve(packages, relative(resolve(root, 'packages'), dirname(pkgJsonPath)));- if (existsSync(resolve(dir, 'dist/index.d.ts'))) paths[manifest(pkgJsonPath).name] = [resolve(dir, 'dist/index.d.ts')];- }- mirror = { packages, paths };- return mirror;-}--function extract(pkgDir: string, pkgJsonPath: string, entry: string, paths?: Record<string, string[]>) {- const name = manifest(pkgJsonPath).name;- const config = ExtractorConfig.prepare({- configObjectFullPath: baseConfigPath,- packageJsonFullPath: pkgJsonPath,- configObject: {- ...baseConfig,- projectFolder: pkgDir,- mainEntryPointFilePath: entry,- compiler: paths- ? { overrideTsconfig: { compilerOptions: { baseUrl: root, paths } } }- : baseConfig.compiler,- apiReport: {- enabled: true,- // @public-only: drops @internal/@alpha/@beta symbols from the surface map.- reportVariants: ['public'],- // Emit declarations for symbols the public API references but the entry point- // never exports, instead of leaving the report referring to names that appear- // nowhere in it. They are emitted *without* `export`, which is the honest- // rendering: the shape is part of the observable surface and is tracked for BC,- // but the name is not importable. The alternative — exporting every such type- // from its package — would have added ~38 new public exports here, committing us- // to names we never meant to publish, so we track the shape instead.- includeForgottenExports: true,- reportFileName: reportFileName(name),- // Stage into temp; we promote the `.public.api.md` output onto the committed- // `<base>.api.md` ourselves so the tracked filenames don't change.- reportFolder: stagingFolder,- reportTempFolder: stagingFolder,- },- },- });- // Collected rather than printed, so `main` decides severity and the output stays grouped- // per package. Duplicates are common (one message per overload/declaration), hence the Set.- const diagnostics: Record<AnalyzerMessageId, Set<string>> = {- [INCOMPATIBLE_RELEASE_TAGS]: new Set(),- [FORGOTTEN_EXPORT]: new Set(),- };- // Let API Extractor always write the staged report (localBuild), then diff it against the- // committed report ourselves so `--verify` keys off the stable `<base>.api.md` name.- Extractor.invoke(config, {- localBuild: true,- showVerboseMessages: false,- messageCallback: (message) => {- diagnostics[message.messageId as AnalyzerMessageId]?.add(message.text);- // Suppress API Extractor's own console output; everything else is already `none`.- message.handled = true;- },- });-- const forgotten = new Set([...diagnostics[FORGOTTEN_EXPORT]].map(quotedSymbol));- const stagedPath = resolve(stagingFolder, stagedFileName(name));- // Annotate after pruning, so declarations that are about to be dropped are never labelled.- const staged = annotateForgottenDeclarations(pruneReport(readFileSync(stagedPath, 'utf8'), forgotten), forgotten);- // Persist the pruned report so a `--verify` failure diffs against what we actually compare.- writeFileSync(stagedPath, staged);- const committedPath = resolve(reportFolder, reportFileName(name));- const committed = existsSync(committedPath) ? readFileSync(committedPath, 'utf8') : undefined;- const apiReportChanged = staged !== committed;- if (apiReportChanged && !verify) writeFileSync(committedPath, staged);-- // Anything still referenced but neither declared nor imported after the pruning above —- // i.e. a forgotten export that `includeForgottenExports` could not supply because the- // release-tag trim dropped it first (see `danglingReferences`).- const unexported = danglingReferences(staged, forgotten);- return { apiReportChanged, committedPath, stagedPath, diagnostics, unexported };-}--type ExtractResult = ReturnType<typeof extract>;--// Render the surface diff between the committed report and the freshly staged one, so a-// failing `--verify` shows *what* changed rather than only telling you to re-run `api:extract`.-// Uses `git diff --no-index` (git is always present in CI) to avoid a diffing dependency. Runs-// from `root` with repo-relative paths and `committed`/`extracted` prefixes so the diff header-// reads cleanly instead of dumping absolute, machine-specific paths.-function reportDiff(committedPath: string, stagedPath: string): string {- const result = spawnSync(- 'git',- [- '--no-pager',- 'diff',- '--no-index',- '--no-color',- '--src-prefix=committed/',- '--dst-prefix=extracted/',- '--',- relative(root, committedPath),- relative(root, stagedPath),- ],- { cwd: root, encoding: 'utf8', maxBuffer: 32 * 1024 * 1024 },- );- // `git diff --no-index` exits 1 when files differ (expected here); only bail on a real error.- return (result.stdout ?? '').trim() || (result.stderr ?? '').trim();-}--function main() {- let failed = 0;-- // Report filenames owned by an in-scope package. Any committed `*.api.md` not in this set- // is orphaned (e.g. its package was removed or renamed) and gets pruned in extract mode.- // Keyed off package existence, not per-run success, so a transient build/extract failure- // never deletes an otherwise-valid report.- const expectedReports = new Set(- packageJsonPaths- .map(manifest)- .filter((pkg) => !pkg.private && !EXCLUDED.has(pkg.name))- .map((pkg) => reportFileName(pkg.name)),- );-- // We rewrite the `.d.ts` files in place for the duration of the run (restored after) to:- // 1. strip the `// @ts-ignore` lines the build injects, which crash Extractor's AST walker;- // 2. rewrite `@ignore` -> `@internal` so those members get trimmed from the public report.- const originals = new Map<string, string>();- for (const file of globbySync('packages/*/dist/**/*.d.ts', { cwd: root, absolute: true })) {- const content = readFileSync(file, 'utf8');- const sanitized = sanitizeDts(content);- if (sanitized !== content) {- originals.set(file, content);- writeFileSync(file, sanitized);- }- }-- try {- for (const pkgJsonPath of packageJsonPaths) {- const pkg = manifest(pkgJsonPath);- if (pkg.private || EXCLUDED.has(pkg.name)) continue;-- const pkgDir = dirname(pkgJsonPath);- const entry = dtsEntry(pkgDir, pkg);- if (!entry) {- const message = `${pkg.name}: no built dist/index.d.ts — run "pnpm build" first`;- console.error(`✗ ${message}`);- ghCommand('error', message);- failed++;- continue;- }-- // Up to date iff the committed report didn't change, and the report is internally- // consistent (no @public symbol referencing a trimmed @internal one). Both modes- // enforce consistency: unlike an out-of-date report, regenerating can't fix it.- const ok = (result: ExtractResult, via = '') => {- if (result.unexported.length > 0) {- const message = `${pkg.name}: referenced by the public API but not exported from the entry point: ${result.unexported.join(', ')} — export them, or keep them out of the public signature`;- console.error(`✗ ${message}`);- ghCommand('error', message);- failed++;- }-- const inconsistent = result.diagnostics[INCOMPATIBLE_RELEASE_TAGS];- for (const text of inconsistent) {- const message = `${pkg.name}: ${text} — the referenced symbol is trimmed from the public report, leaving a dangling reference; drop the tag, or keep the type out of the public signature`;- console.error(`✗ ${message}`);- ghCommand('error', message);- failed++;- }-- if (verify && result.apiReportChanged) {- const message = `${pkg.name}: report out of date${via} — run "pnpm api:extract" and commit the changes in docs/public-api/`;- console.error(`✗ ${pkg.name}: report out of date${via}`);- // Print the actual surface diff so the failure is self-explanatory in the CI- // log; keep the concise message for the inline GitHub annotation.- const diff = reportDiff(result.committedPath, result.stagedPath);- if (diff) console.error(`${diff}\n`);- ghCommand('error', message);- failed++;- } else if (inconsistent.size === 0 && result.unexported.length === 0) {- console.log(`✓ ${pkg.name}${via}`);- }- };-- // Fallback: retry against the sanitized mirror (dodges an API Extractor crash on- // cross-package re-exports of comment-injected members).- const viaMirror = () => {- const { packages, paths } = getMirror();- const mirrorEntry = resolve(packages, relative(resolve(root, 'packages'), pkgDir), relative(pkgDir, entry));- const { [pkg.name]: _self, ...deps } = paths;- return extract(pkgDir, pkgJsonPath, mirrorEntry, deps);- };-- try {- ok(extract(pkgDir, pkgJsonPath, entry));- } catch {- try {- ok(viaMirror(), ' (via mirror)');- } catch (err) {- const message = `${pkg.name}: api-extractor crashed: ${(err as Error).message}`;- console.error(`✗ ${message}`);- ghCommand('error', message);- failed++;- }- }- }- } finally {- for (const [file, content] of originals) writeFileSync(file, content);- rmSync(mirrorRoot, { recursive: true, force: true });- }-- // Prune orphaned reports: committed `*.api.md` files with no owning in-scope package- // (e.g. a removed/renamed package). Delete them in extract mode; flag them in verify mode.- for (const file of globbySync('*.api.md', { cwd: reportFolder, absolute: true })) {- if (expectedReports.has(basename(file))) continue;- if (verify) {- const message = `${basename(file)}: orphaned report (no matching package) — run "pnpm api:extract" to remove it`;- console.error(`✗ ${message}`);- ghCommand('error', message);- failed++;- } else {- rmSync(file);- console.log(`✓ removed orphaned report ${basename(file)}`);- }- }-- if (failed > 0) {- if (verify) console.error('\nRun "pnpm api:extract" and commit the changes in docs/public-api/.');- process.exit(1);- }-}--main();