#!/usr/bin/env node import { createHash } from 'node:crypto'; import { copyFileSync, existsSync, readFileSync, unlinkSync, writeFileSync } from 'node:fs'; import { fileURLToPath } from 'node:url'; import { LANGUAGES, mapSdkVersion, readCanonicalVersion } from './sdk-version.mjs'; import { normalizeSdkBranding } from './sdk-branding.mjs'; const language = process.argv[2]; if (!LANGUAGES.includes(language)) { console.error(`Usage: node fern/scripts/record-sdk-metadata.mjs ${LANGUAGES.join('|')}`); process.exit(2); } const repositoryDirectory = fileURLToPath(new URL('../../', import.meta.url)); const outputDirectory = language === 'typescript' ? `${repositoryDirectory}cloudpdf/sdk` : `${repositoryDirectory}sdks/${language}`; const fernMetadata = JSON.parse(readFileSync(`${outputDirectory}/.fern/metadata.json`, 'utf8')); if (language === 'typescript') { // A committed generated tree cannot contain provenance for the commit or // environment that generated it: CI and local regeneration would always // produce different metadata. Git already supplies provenance for this // workspace-resident artifact; the OpenAPI hash below is the deterministic // freshness identity. fernMetadata.originGitCommit = null; fernMetadata.originGitCommitIsDirty = null; delete fernMetadata.invokedBy; delete fernMetadata.ciProvider; writeFileSync( `${outputDirectory}/.fern/metadata.json`, `${JSON.stringify(fernMetadata, null, 4)}\n`, ); } const canonicalVersion = readCanonicalVersion(); const sdkVersion = mapSdkVersion(canonicalVersion, language); const openApiSha256 = createHash('sha256') .update(readFileSync(`${repositoryDirectory}cloudpdf/contract/openapi.json`)) .digest('hex'); const metadata = { language, canonicalVersion, sdkVersion, source: { repository: 'embedpdf/embed-pdf-viewer', openapi: 'cloudpdf/contract/openapi.json', openapiSha256: openApiSha256, gitCommit: fernMetadata.originGitCommit ?? null, gitCommitIsDirty: fernMetadata.originGitCommitIsDirty ?? null, }, fern: { cliVersion: fernMetadata.cliVersion, generatorName: fernMetadata.generatorName, generatorVersion: fernMetadata.generatorVersion, }, }; writeFileSync( `${outputDirectory}/cloudpdf-generation.json`, `${JSON.stringify(metadata, null, 2)}\n`, ); copyFileSync(`${repositoryDirectory}cloudpdf/contract/LICENSE`, `${outputDirectory}/LICENSE`); normalizeSdkBranding(outputDirectory, language); // Registry-facing metadata that Fern does not currently expose through every // generator configuration. Keep these deterministic and validate them below so // a generator upgrade cannot silently publish incomplete package metadata. if (language === 'typescript') { // The TypeScript SDK is a package in this repository's pnpm workspace, not // a standalone generated repository. Remove Fern's nested workspace marker // so installs consistently resolve through the root lockfile. const nestedWorkspacePath = `${outputDirectory}/pnpm-workspace.yaml`; if (existsSync(nestedWorkspacePath)) { const nestedWorkspace = readFileSync(nestedWorkspacePath, 'utf8'); if (nestedWorkspace !== "packages: ['.']") { throw new Error('TypeScript nested pnpm workspace shape changed; revisit its removal'); } unlinkSync(nestedWorkspacePath); } // fern-typescript-sdk 3.87.2 emits a useless JavaScript string escape in its // ESM rename helper. It is behaviorally harmless, but CodeQL correctly flags // it and a direct edit would be overwritten by the next generation. Patch // only the dynamic-import regex block and fail closed if Fern changes shape. const renameScriptPath = `${outputDirectory}/scripts/rename-to-esm-files.js`; let renameScript = readFileSync(renameScriptPath, 'utf8'); const dynamicRegexStart = renameScript.indexOf(' const dynamicRegex = new RegExp('); const dynamicRegexEnd = renameScript.indexOf( ' newContent = newContent.replace(dynamicRegex,', dynamicRegexStart, ); if (dynamicRegexStart === -1 || dynamicRegexEnd === -1) { throw new Error('TypeScript ESM rename helper shape changed; revisit the regex patch'); } const generatedRelativeImportPattern = String.raw`(\\.\\.\?\\/[^'"]+)`; const patchedRelativeImportPattern = String.raw`(\\.\\.?\\/[^'"]+)`; const dynamicRegexBlock = renameScript.slice(dynamicRegexStart, dynamicRegexEnd); const hasGeneratedPattern = dynamicRegexBlock.includes(generatedRelativeImportPattern); const hasPatchedPattern = dynamicRegexBlock.includes(patchedRelativeImportPattern); if (hasGeneratedPattern === hasPatchedPattern) { throw new Error('TypeScript ESM rename helper regex changed; revisit the regex patch'); } if (hasGeneratedPattern) { renameScript = renameScript.slice(0, dynamicRegexStart) + dynamicRegexBlock.replace(generatedRelativeImportPattern, patchedRelativeImportPattern) + renameScript.slice(dynamicRegexEnd); writeFileSync(renameScriptPath, renameScript); } const manifestPath = `${outputDirectory}/package.json`; const manifest = JSON.parse(readFileSync(manifestPath, 'utf8')); delete manifest.packageManager; manifest.publishConfig = { access: 'public' }; // Fern's defaults are watch-mode commands. Generation runs in CI and the // workspace release gate, so these must be finite checks. manifest.scripts.test = 'vitest run'; manifest.scripts['test:unit'] = 'vitest run --project unit'; manifest.scripts['test:wire'] = 'vitest run --project wire'; // The browser upload entry (`src/upload.ts`, handwritten, in .fernignore) // is an entry Fern doesn't know of. Pages import only it, so it gets its // own export, in Fern's shape, before `./package.json`. if (!manifest.exports?.['./package.json']) { throw new Error('TypeScript package exports changed shape; revisit the ./upload export'); } const { './package.json': packageJsonExport, ...entries } = manifest.exports; manifest.exports = { ...entries, './upload': { import: { types: './dist/esm/upload.d.mts', default: './dist/esm/upload.mjs' }, require: { types: './dist/cjs/upload.d.ts', default: './dist/cjs/upload.js' }, default: './dist/cjs/upload.js', }, './package.json': packageJsonExport, }; writeFileSync(manifestPath, `${JSON.stringify(manifest, null, 4)}\n`); } if (language === 'php') { const manifestPath = `${outputDirectory}/composer.json`; const manifest = JSON.parse(readFileSync(manifestPath, 'utf8')); // Packagist derives versions from immutable VCS tags and Composer warns when // a published library hard-codes its version in composer.json. delete manifest.version; manifest.authors = [ { name: 'CloudPDF', email: 'hello@cloudpdf.com', homepage: 'https://www.cloudpdf.com', }, ]; manifest.support = { issues: 'https://github.com/embedpdf/cloudpdf-sdk-php/issues', source: 'https://github.com/embedpdf/cloudpdf-sdk-php', }; writeFileSync(manifestPath, `${JSON.stringify(manifest, null, 2)}\n`); } if (language === 'python') { const projectPath = `${outputDirectory}/pyproject.toml`; const project = readFileSync(projectPath, 'utf8') .replace('description = ""', 'description = "The official Python SDK for the CloudPDF API."') .replace('authors = []', 'authors = ["CloudPDF "]'); writeFileSync(projectPath, project); // fern-python-sdk 5.26.0 names the environment class from // `environment_class_name` (CloudPDFEnvironment) in the code, but its // README and reference examples import a `CloudPDFClientEnvironment` that // doesn't exist. Use the real name, and fail if the code ever disagrees. const environmentSource = readFileSync(`${outputDirectory}/src/cloudpdf/environment.py`, 'utf8'); if (!environmentSource.includes('class CloudPDFEnvironment(enum.Enum):')) { throw new Error('Python environment class changed; revisit the README environment patch'); } for (const document of ['README.md', 'reference.md']) { const documentPath = `${outputDirectory}/${document}`; writeFileSync( documentPath, readFileSync(documentPath, 'utf8').replaceAll( 'CloudPDFClientEnvironment', 'CloudPDFEnvironment', ), ); } // Clients read an object of a kind they don't know, one a newer server // sends, as its union's catch-all (docs/conventions/api-evolution.md, // "Reading tolerantly"). fern-python-sdk 5.26.0 has no setting for it: each // union dispatches with pydantic's `discriminator`, which refuses an unknown // tag and with it the whole response. Put a validator in front of each open // union's dispatch that turns an unknown kind into the catch-all; a known // one goes on to its model as before. Fail if Fern changes a union's shape. const openUnions = JSON.parse( readFileSync(`${repositoryDirectory}fern/tolerant-reading/open-unions.json`, 'utf8'), ); for (const union of openUnions) readUnknownKindAsCatchAll(union); } // The Ruby generator intentionally leaves registry metadata in its generated // custom.gemspec.rb hook. Fill that hook deterministically until each SDK has // its own repository-owned customization layer. if (language === 'ruby') { // fern-ruby-sdk 1.28.1 sends a binary multipart field through the runtime's // add_file, but only when it is given and without a content type, and it // leaves the required `file` out of the request model and the examples. // Require the file, send it as a PDF, and make the parameter visible. Keep // this strict so a generator upgrade forces us to re-evaluate the patch. const uploadClientPath = `${outputDirectory}/lib/CloudPDF/documents/client.rb`; const generatedMultipartBlock = ` if params[:file] body.add_file( name: "file", file: params[:file] ) end`; const patchedMultipartBlock = ` raise ArgumentError, "file is required" unless params[:file] body.add_file(name: "file", file: params[:file], content_type: "application/pdf")`; let uploadClient = readFileSync(uploadClientPath, 'utf8'); if ( !uploadClient.includes(generatedMultipartBlock) && !uploadClient.includes(patchedMultipartBlock) ) { throw new Error('Ruby upload proxy generator shape changed; revisit the multipart patch'); } uploadClient = uploadClient.replace(generatedMultipartBlock, patchedMultipartBlock); uploadClient = uploadClient.replace( '# @option params [String] :doc_id\n #\n # @example\n # client.documents.upload_proxy(\n # tenant_id: "tenantId",\n # doc_id: "docId"\n # )', '# @option params [String] :doc_id\n # @option params [#read] :file\n #\n # @example\n # client.documents.upload_proxy(\n # tenant_id: "tenantId",\n # doc_id: "docId",\n # file: File.open("document.pdf", "rb")\n # )', ); writeFileSync(uploadClientPath, uploadClient); const uploadTypePath = `${outputDirectory}/lib/CloudPDF/documents/types/upload_proxy_documents_request.rb`; const generatedIdField = ' field :doc_id, -> { String }, optional: false, nullable: false, api_name: "docId"\n'; const patchedFileField = ' field :file, -> { Object }, optional: false, nullable: false\n'; const uploadTypeSource = readFileSync(uploadTypePath, 'utf8'); if ( !uploadTypeSource.includes(generatedIdField) && !uploadTypeSource.includes(patchedFileField) ) { throw new Error('Ruby upload proxy request shape changed; revisit the file field patch'); } const uploadType = uploadTypeSource.includes(patchedFileField) ? uploadTypeSource : uploadTypeSource.replace(generatedIdField, `${generatedIdField}\n${patchedFileField}`); writeFileSync(uploadTypePath, uploadType); const referencePath = `${outputDirectory}/reference.md`; const generatedReferenceExample = `client.documents.upload_proxy( tenant_id: "tenantId", doc_id: "docId" )`; const patchedReferenceExample = 'File.open("document.pdf", "rb") do |file|'; const referenceSource = readFileSync(referencePath, 'utf8'); if ( !referenceSource.includes(generatedReferenceExample) && !referenceSource.includes(patchedReferenceExample) ) { throw new Error('Ruby upload proxy reference shape changed; revisit the file example patch'); } if (referenceSource.includes(generatedReferenceExample)) { writeFileSync( referencePath, referenceSource.replace( generatedReferenceExample, `File.open("document.pdf", "rb") do |file| client.documents.upload_proxy( tenant_id: "tenantId", doc_id: "docId", file: file ) end`, ), ); } writeFileSync( `${outputDirectory}/custom.gemspec.rb`, `# frozen_string_literal: true def add_custom_gemspec_data(spec) spec.authors = ["CloudPDF"] spec.email = ["hello@cloudpdf.com"] spec.homepage = "https://www.cloudpdf.com" spec.license = "Apache-2.0" spec.summary = "The official Ruby SDK for the CloudPDF API." spec.description = "A typed Ruby client for deployment, tenant, and document operations in the CloudPDF API." spec.metadata["homepage_uri"] = spec.homepage spec.metadata["source_code_uri"] = "https://github.com/embedpdf/cloudpdf-sdk-ruby" spec.files = spec.files.reject do |file| file.start_with?(".github/") || file == "cloudpdf-generation.json" end end `, ); } // NuGet supports SemVer prereleases, but AssemblyVersion and FileVersion are // numeric CLR versions. Fern currently aliases both to $(Version), which makes // prerelease SDKs fail to compile. Preserve the NuGet package version and use a // stable numeric binary version for the current major/minor/patch line. if (language === 'csharp') { const projectPath = `${outputDirectory}/src/CloudPDF/CloudPDF.csproj`; const numericBinaryVersion = `${canonicalVersion.split('-')[0]}.0`; const project = readFileSync(projectPath, 'utf8') .replace( 'CloudPDF', `CloudPDF CloudPDF The official .NET SDK for the CloudPDF API. https://www.cloudpdf.com https://github.com/embedpdf/cloudpdf-sdk-dotnet git cloudpdf;pdf;api;sdk`, ) .replace( '$(Version)', `${numericBinaryVersion}`, ) .replace( '$(Version)', `${numericBinaryVersion}`, ); writeFileSync(projectPath, project); } // Maven Central requires source and Javadoc artifacts, complete POM metadata, // and a PGP signature for every published artifact. Fern generates the first // two but currently leaves placeholder SCM URLs and an OSSRH-era remote // repository. Stage a signed local Maven repository instead; the repository // release workflow bundles and uploads it through the Central Portal API. if (language === 'java') { // The generated API surface is large enough to make Gradle's default 512 MiB // build heap thrash during compileJava. Keep the build VM configuration in // the generated project so it applies both here and in the standalone SDK. writeFileSync( `${outputDirectory}/gradle.properties`, 'org.gradle.jvmargs=-Xmx2g -XX:MaxMetaspaceSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8\n', ); const projectPath = `${outputDirectory}/build.gradle`; const project = readFileSync(projectPath, 'utf8') .replace(" id 'maven-publish'\n", " id 'maven-publish'\n id 'signing'\n") .replace( ` licenses { license { name = 'APACHE-2.0' } }`, ` licenses { license { name = 'Apache License, Version 2.0' url = 'https://www.apache.org/licenses/LICENSE-2.0.txt' distribution = 'repo' } }`, ) .replace( ` developers { developer { name = 'CloudPDF' email = 'hello@cloudpdf.com' } }`, ` developers { developer { id = 'cloudpdf' name = 'CloudPDF' email = 'hello@cloudpdf.com' organization = 'CloudPDF' organizationUrl = 'https://www.cloudpdf.com' } }`, ) .replace( ` scm { connection = 'scm:git:git://github.com/YOUR-ORG/YOUR-REPO.git' developerConnection = 'scm:git:git://github.com/YOUR-ORG/YOUR-REPO.git' url = 'https://github.com/YOUR-ORG/YOUR-REPO' }`, ` scm { connection = 'scm:git:https://github.com/embedpdf/cloudpdf-sdk-java.git' developerConnection = 'scm:git:ssh://git@github.com/embedpdf/cloudpdf-sdk-java.git' url = 'https://github.com/embedpdf/cloudpdf-sdk-java' }`, ) .replace( ` repositories { maven { url "$System.env.MAVEN_PUBLISH_REGISTRY_URL" credentials { username "$System.env.MAVEN_USERNAME" password "$System.env.MAVEN_PASSWORD" } } }`, ` repositories { maven { name = 'centralStaging' url = layout.buildDirectory.dir('central-staging') } }`, ).concat(` tasks.withType(GenerateModuleMetadata) { enabled = false } tasks.withType(Jar).configureEach { zip64 = true } signing { useInMemoryPgpKeys( System.getenv('MAVEN_GPG_PRIVATE_KEY'), System.getenv('MAVEN_GPG_PASSPHRASE') ) sign publishing.publications.maven } `); writeFileSync(projectPath, project); } /** * Appends to a Python open union's module a validator that reads a kind the * SDK doesn't know as the union's catch-all (one row of * fern/tolerant-reading/open-unions.json): the body's fields, with each * `fill` field kept when the body carries it as its type and `otherwise` * when it doesn't. Running it again leaves the module as it is. */ function readUnknownKindAsCatchAll({ name, tag, catchAll, fill }) { const snake = (text) => text.replace(/([a-z0-9])([A-Z])/g, '$1_$2').toLowerCase(); const pythonValue = (value) => (value === null ? 'None' : JSON.stringify(value)); const isType = { integer: (variable) => `isinstance(${variable}, int) and not isinstance(${variable}, bool)`, string: (variable) => `isinstance(${variable}, str)`, }; const modulePath = `${outputDirectory}/src/cloudpdf/types/${snake(name)}.py`; const source = readFileSync(modulePath, 'utf8'); // Each union is its own module, so its names can't meet another union's. const known = '_KNOWN_KINDS'; const reader = `_read_unknown_${tag}_as_${snake(catchAll)}`; const comment = (text) => text .split(' ') .reduce( (lines, word) => { const last = lines[lines.length - 1]; if (`${last} ${word}`.length > 79) lines.push(`# ${word}`); else lines[lines.length - 1] = `${last} ${word}`; return lines; }, ['#'], ) .join('\n'); const reads = fill.map(({ field }) => ` ${snake(field)} = value.get("${field}")\n`); const fills = fill.map( ({ field, is, otherwise }) => ` "${field}": ${snake(field)} if ${isType[is](snake(field))} else ${pythonValue(otherwise)},\n`, ); const kept = fill.length === 0 ? '' : `, keeping ${fill.map(({ field }) => `\`${field}\``).join(' and ')} when the body carries ${fill.length === 1 ? 'it' : 'them'}`; const patch = ` ${comment( `A ${name} of a ${tag} this SDK doesn't know, one a newer server sends, reads as ` + `\`${catchAll}\`${kept}. A ${tag} it knows is read as that ${tag}. Added after ` + 'generation by fern/scripts/record-sdk-metadata.mjs from ' + 'fern/tolerant-reading/open-unions.json.', )} if IS_PYDANTIC_V2: ${known} = frozenset( member.model_fields["${tag}"].default for member in typing.get_args(typing.get_args(${name})[0]) ) def ${reader}(value: typing.Any) -> typing.Any: if not isinstance(value, dict): return value ${tag}_ = value.get("${tag}") if not isinstance(${tag}_, str) or ${tag}_ in ${known}: return value ${reads.join('')} return { **value, "${tag}": "${catchAll}", ${fills.join('')} } ${name} = typing_extensions.Annotated[ # type: ignore[misc] ${name}, pydantic.BeforeValidator(${reader}) ] `; if (source.endsWith(patch)) return; const pascal = catchAll.replace(/(^|-)([a-z])/g, (_, __, letter) => letter.toUpperCase()); const dispatch = new RegExp( `\\n${name} = typing_extensions\\.Annotated\\[\\n typing\\.Union\\[[^\\]]*\\b${name}_${pascal}\\b[^\\]]*\\],\\n pydantic\\.Field\\(discriminator="${tag}"\\),\\n\\]\\n$`, ); if (!dispatch.test(source) || !source.includes('IS_PYDANTIC_V2')) { throw new Error(`Python ${name} union changed; revisit its unknown-${tag} patch`); } writeFileSync(modulePath, source + patch); }