guan404ming commented on code in PR #69295: URL: https://github.com/apache/airflow/pull/69295#discussion_r3557291116
########## ts-sdk/src/cli/pack.ts: ########## @@ -0,0 +1,167 @@ +/*! + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +// airflow-ts-pack: bundle a TypeScript entrypoint into the single-file +// artifact NodeCoordinator consumes — `bundle.mjs` with the airflow +// metadata embedded as a leading `//# airflowMetadata=<base64>` comment. +// +// Mirrors airflow-go-pack: build first, then run the built bundle with +// --airflow-metadata so the manifest comes from the bundle's own task +// registry and schema version, never from a hand-written sidecar. + +import { execFileSync } from "node:child_process"; +import { readFileSync, writeFileSync } from "node:fs"; +import path from "node:path"; + +import { build } from "esbuild"; + +import { AIRFLOW_METADATA_FLAG, type BundleManifest } from "../coordinator/runtime.js"; + +const AIRFLOW_BUNDLE_METADATA_VERSION = "1.0"; +const BUNDLE_FILENAME = "bundle.mjs"; +export const EMBEDDED_METADATA_PREFIX = "//# airflowMetadata="; + +const USAGE = `Usage: airflow-ts-pack <entry> [--outdir <dir>] [--source <name>] + +Bundles <entry> into <outdir>/${BUNDLE_FILENAME} with esbuild and embeds the +airflow metadata generated from the bundle's registered tasks. + +Options: + --outdir <dir> Output directory (default: dist) + --source <name> Display name of the primary source file (default: <entry> basename) +`; + +export interface PackArgs { + entry: string; + outdir: string; + source: string; +} + +export function parsePackArgs(argv: readonly string[]): PackArgs { + let entry: string | null = null; + let outdir = "dist"; + let source: string | null = null; + for (let i = 0; i < argv.length; i += 1) { + const arg = argv[i]!; + if (arg === "--outdir" || arg === "--source") { + const value = argv[i + 1]; + if (!value) throw new Error(`${arg} requires a value\n\n${USAGE}`); + if (arg === "--outdir") outdir = value; + else source = value; + i += 1; + } else if (arg.startsWith("-")) { + throw new Error(`Unknown option ${arg}\n\n${USAGE}`); + } else if (entry) { + throw new Error(`Unexpected argument ${arg}\n\n${USAGE}`); + } else { + entry = arg; + } + } + if (!entry) throw new Error(`Missing entry file\n\n${USAGE}`); + return { entry, outdir, source: source ?? path.basename(entry) }; +} + +export interface PackMetadata { + airflow_bundle_metadata_version: string; + sdk: { language: string; version: string; supervisor_schema_version: string }; + source: string; + dags: BundleManifest["dags"]; +} + +// JSON string literals are valid YAML double-quoted scalars, so every +// scalar below is emitted through JSON.stringify for correct escaping. +export function renderMetadataYaml(metadata: PackMetadata): string { + const lines = [ + `airflow_bundle_metadata_version: ${JSON.stringify(metadata.airflow_bundle_metadata_version)}`, + "sdk:", + ` language: ${JSON.stringify(metadata.sdk.language)}`, + ` version: ${JSON.stringify(metadata.sdk.version)}`, + ` supervisor_schema_version: ${JSON.stringify(metadata.sdk.supervisor_schema_version)}`, + `source: ${JSON.stringify(metadata.source)}`, + "dags:", + ]; + for (const [dagId, dag] of Object.entries(metadata.dags)) { + lines.push(` ${JSON.stringify(dagId)}:`); + lines.push(` tasks: [${dag.tasks.map((task) => JSON.stringify(task)).join(", ")}]`); + } + return `${lines.join("\n")}\n`; +} + +function readSdkVersion(): string { + const packageJsonUrl = new URL("../../package.json", import.meta.url); + const { version } = JSON.parse(readFileSync(packageJsonUrl, "utf-8")) as { version: string }; + return version; +} + +function readBundleManifest(bundlePath: string): BundleManifest { + const stdout = execFileSync(process.execPath, [bundlePath, AIRFLOW_METADATA_FLAG], { + encoding: "utf-8", + }); + let manifest: BundleManifest; + try { + manifest = JSON.parse(stdout) as BundleManifest; + } catch (error) { + throw new Error(`Bundle produced invalid --airflow-metadata output: ${String(error)}`, { + cause: error, + }); + } + if (!manifest.supervisor_schema_version || typeof manifest.dags !== "object") { + throw new Error("Bundle produced incomplete --airflow-metadata output"); + } + return manifest; +} + +export async function runPack(argv: readonly string[]): Promise<void> { + const args = parsePackArgs(argv); + const bundlePath = path.join(args.outdir, BUNDLE_FILENAME); + + await build({ + entryPoints: [args.entry], + bundle: true, + platform: "node", + format: "esm", + target: "node22", + outfile: bundlePath, + }); + + const manifest = readBundleManifest(bundlePath); + if (Object.keys(manifest.dags).length === 0) { + throw new Error( + `${args.entry} registered no tasks; call registerTask(...) before startCoordinator()`, + ); + } + + const metadataYaml = renderMetadataYaml({ + airflow_bundle_metadata_version: AIRFLOW_BUNDLE_METADATA_VERSION, + sdk: { + language: "typescript", + version: readSdkVersion(), + supervisor_schema_version: manifest.supervisor_schema_version, + }, + source: args.source, + dags: manifest.dags, + }); + const metadataLine = `${EMBEDDED_METADATA_PREFIX}${Buffer.from(metadataYaml, "utf-8").toString("base64")}\n`; + writeFileSync( + bundlePath, + Buffer.concat([Buffer.from(metadataLine, "utf-8"), readFileSync(bundlePath)]), + ); Review Comment: Fixed. The packer now strips a leading hashbang before prepending the metadata line — the coordinator always runs the bundle through `node`, so it's dead weight. Covered by a shebang-entry fixture test. ########## ts-sdk/src/cli/pack.ts: ########## @@ -0,0 +1,167 @@ +/*! + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +// airflow-ts-pack: bundle a TypeScript entrypoint into the single-file +// artifact NodeCoordinator consumes — `bundle.mjs` with the airflow +// metadata embedded as a leading `//# airflowMetadata=<base64>` comment. +// +// Mirrors airflow-go-pack: build first, then run the built bundle with +// --airflow-metadata so the manifest comes from the bundle's own task +// registry and schema version, never from a hand-written sidecar. + +import { execFileSync } from "node:child_process"; +import { readFileSync, writeFileSync } from "node:fs"; +import path from "node:path"; + +import { build } from "esbuild"; + +import { AIRFLOW_METADATA_FLAG, type BundleManifest } from "../coordinator/runtime.js"; + +const AIRFLOW_BUNDLE_METADATA_VERSION = "1.0"; +const BUNDLE_FILENAME = "bundle.mjs"; +export const EMBEDDED_METADATA_PREFIX = "//# airflowMetadata="; + +const USAGE = `Usage: airflow-ts-pack <entry> [--outdir <dir>] [--source <name>] + +Bundles <entry> into <outdir>/${BUNDLE_FILENAME} with esbuild and embeds the +airflow metadata generated from the bundle's registered tasks. + +Options: + --outdir <dir> Output directory (default: dist) + --source <name> Display name of the primary source file (default: <entry> basename) +`; + +export interface PackArgs { + entry: string; + outdir: string; + source: string; +} + +export function parsePackArgs(argv: readonly string[]): PackArgs { + let entry: string | null = null; + let outdir = "dist"; + let source: string | null = null; + for (let i = 0; i < argv.length; i += 1) { + const arg = argv[i]!; + if (arg === "--outdir" || arg === "--source") { + const value = argv[i + 1]; + if (!value) throw new Error(`${arg} requires a value\n\n${USAGE}`); + if (arg === "--outdir") outdir = value; + else source = value; + i += 1; + } else if (arg.startsWith("-")) { + throw new Error(`Unknown option ${arg}\n\n${USAGE}`); + } else if (entry) { + throw new Error(`Unexpected argument ${arg}\n\n${USAGE}`); + } else { + entry = arg; + } + } + if (!entry) throw new Error(`Missing entry file\n\n${USAGE}`); + return { entry, outdir, source: source ?? path.basename(entry) }; +} + +export interface PackMetadata { + airflow_bundle_metadata_version: string; + sdk: { language: string; version: string; supervisor_schema_version: string }; + source: string; + dags: BundleManifest["dags"]; +} + +// JSON string literals are valid YAML double-quoted scalars, so every +// scalar below is emitted through JSON.stringify for correct escaping. +export function renderMetadataYaml(metadata: PackMetadata): string { + const lines = [ + `airflow_bundle_metadata_version: ${JSON.stringify(metadata.airflow_bundle_metadata_version)}`, + "sdk:", + ` language: ${JSON.stringify(metadata.sdk.language)}`, + ` version: ${JSON.stringify(metadata.sdk.version)}`, + ` supervisor_schema_version: ${JSON.stringify(metadata.sdk.supervisor_schema_version)}`, + `source: ${JSON.stringify(metadata.source)}`, + "dags:", + ]; + for (const [dagId, dag] of Object.entries(metadata.dags)) { + lines.push(` ${JSON.stringify(dagId)}:`); + lines.push(` tasks: [${dag.tasks.map((task) => JSON.stringify(task)).join(", ")}]`); + } + return `${lines.join("\n")}\n`; +} + +function readSdkVersion(): string { + const packageJsonUrl = new URL("../../package.json", import.meta.url); + const { version } = JSON.parse(readFileSync(packageJsonUrl, "utf-8")) as { version: string }; + return version; +} + +function readBundleManifest(bundlePath: string): BundleManifest { + const stdout = execFileSync(process.execPath, [bundlePath, AIRFLOW_METADATA_FLAG], { + encoding: "utf-8", + }); Review Comment: Fixed. The manifest line is now prefixed with a `__AIRFLOW_METADATA__ ` sentinel and the packer picks that line from stdout, so import-time logging can't break parsing. Also added `timeout`/`maxBuffer` and wrapped `execFileSync` with a clear error prefix. ########## ts-sdk/package.json: ########## @@ -57,7 +60,8 @@ "node": ">=22" }, "dependencies": { - "@msgpack/msgpack": "^3.1.2" + "@msgpack/msgpack": "^3.1.2", + "esbuild": "^0.28.1" Review Comment: Fixed. Moved `esbuild` to an optional peer dependency; the packer imports it dynamically and gives an install hint when missing. Runtime-only installs no longer pull it. ########## ts-sdk/src/cli/pack.ts: ########## @@ -0,0 +1,167 @@ +/*! + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +// airflow-ts-pack: bundle a TypeScript entrypoint into the single-file +// artifact NodeCoordinator consumes — `bundle.mjs` with the airflow +// metadata embedded as a leading `//# airflowMetadata=<base64>` comment. +// +// Mirrors airflow-go-pack: build first, then run the built bundle with +// --airflow-metadata so the manifest comes from the bundle's own task +// registry and schema version, never from a hand-written sidecar. + +import { execFileSync } from "node:child_process"; +import { readFileSync, writeFileSync } from "node:fs"; +import path from "node:path"; + +import { build } from "esbuild"; + +import { AIRFLOW_METADATA_FLAG, type BundleManifest } from "../coordinator/runtime.js"; + +const AIRFLOW_BUNDLE_METADATA_VERSION = "1.0"; +const BUNDLE_FILENAME = "bundle.mjs"; +export const EMBEDDED_METADATA_PREFIX = "//# airflowMetadata="; + +const USAGE = `Usage: airflow-ts-pack <entry> [--outdir <dir>] [--source <name>] + +Bundles <entry> into <outdir>/${BUNDLE_FILENAME} with esbuild and embeds the +airflow metadata generated from the bundle's registered tasks. + +Options: + --outdir <dir> Output directory (default: dist) + --source <name> Display name of the primary source file (default: <entry> basename) +`; + +export interface PackArgs { + entry: string; + outdir: string; + source: string; +} + +export function parsePackArgs(argv: readonly string[]): PackArgs { + let entry: string | null = null; + let outdir = "dist"; + let source: string | null = null; + for (let i = 0; i < argv.length; i += 1) { + const arg = argv[i]!; + if (arg === "--outdir" || arg === "--source") { + const value = argv[i + 1]; + if (!value) throw new Error(`${arg} requires a value\n\n${USAGE}`); + if (arg === "--outdir") outdir = value; + else source = value; + i += 1; + } else if (arg.startsWith("-")) { + throw new Error(`Unknown option ${arg}\n\n${USAGE}`); + } else if (entry) { + throw new Error(`Unexpected argument ${arg}\n\n${USAGE}`); + } else { + entry = arg; + } + } + if (!entry) throw new Error(`Missing entry file\n\n${USAGE}`); + return { entry, outdir, source: source ?? path.basename(entry) }; +} + +export interface PackMetadata { + airflow_bundle_metadata_version: string; + sdk: { language: string; version: string; supervisor_schema_version: string }; + source: string; + dags: BundleManifest["dags"]; +} + +// JSON string literals are valid YAML double-quoted scalars, so every +// scalar below is emitted through JSON.stringify for correct escaping. +export function renderMetadataYaml(metadata: PackMetadata): string { + const lines = [ + `airflow_bundle_metadata_version: ${JSON.stringify(metadata.airflow_bundle_metadata_version)}`, + "sdk:", + ` language: ${JSON.stringify(metadata.sdk.language)}`, + ` version: ${JSON.stringify(metadata.sdk.version)}`, + ` supervisor_schema_version: ${JSON.stringify(metadata.sdk.supervisor_schema_version)}`, + `source: ${JSON.stringify(metadata.source)}`, + "dags:", + ]; + for (const [dagId, dag] of Object.entries(metadata.dags)) { + lines.push(` ${JSON.stringify(dagId)}:`); + lines.push(` tasks: [${dag.tasks.map((task) => JSON.stringify(task)).join(", ")}]`); + } + return `${lines.join("\n")}\n`; +} + +function readSdkVersion(): string { + const packageJsonUrl = new URL("../../package.json", import.meta.url); + const { version } = JSON.parse(readFileSync(packageJsonUrl, "utf-8")) as { version: string }; + return version; +} + +function readBundleManifest(bundlePath: string): BundleManifest { + const stdout = execFileSync(process.execPath, [bundlePath, AIRFLOW_METADATA_FLAG], { + encoding: "utf-8", + }); + let manifest: BundleManifest; + try { + manifest = JSON.parse(stdout) as BundleManifest; + } catch (error) { + throw new Error(`Bundle produced invalid --airflow-metadata output: ${String(error)}`, { + cause: error, + }); + } + if (!manifest.supervisor_schema_version || typeof manifest.dags !== "object") { Review Comment: Fixed, added the `manifest.dags &&` check. ########## ts-sdk/src/cli/pack.ts: ########## @@ -0,0 +1,167 @@ +/*! + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +// airflow-ts-pack: bundle a TypeScript entrypoint into the single-file +// artifact NodeCoordinator consumes — `bundle.mjs` with the airflow +// metadata embedded as a leading `//# airflowMetadata=<base64>` comment. +// +// Mirrors airflow-go-pack: build first, then run the built bundle with +// --airflow-metadata so the manifest comes from the bundle's own task +// registry and schema version, never from a hand-written sidecar. + +import { execFileSync } from "node:child_process"; +import { readFileSync, writeFileSync } from "node:fs"; +import path from "node:path"; + +import { build } from "esbuild"; + +import { AIRFLOW_METADATA_FLAG, type BundleManifest } from "../coordinator/runtime.js"; + +const AIRFLOW_BUNDLE_METADATA_VERSION = "1.0"; +const BUNDLE_FILENAME = "bundle.mjs"; +export const EMBEDDED_METADATA_PREFIX = "//# airflowMetadata="; + +const USAGE = `Usage: airflow-ts-pack <entry> [--outdir <dir>] [--source <name>] + +Bundles <entry> into <outdir>/${BUNDLE_FILENAME} with esbuild and embeds the +airflow metadata generated from the bundle's registered tasks. + +Options: + --outdir <dir> Output directory (default: dist) + --source <name> Display name of the primary source file (default: <entry> basename) +`; + +export interface PackArgs { + entry: string; + outdir: string; + source: string; +} + +export function parsePackArgs(argv: readonly string[]): PackArgs { + let entry: string | null = null; + let outdir = "dist"; + let source: string | null = null; + for (let i = 0; i < argv.length; i += 1) { + const arg = argv[i]!; + if (arg === "--outdir" || arg === "--source") { + const value = argv[i + 1]; + if (!value) throw new Error(`${arg} requires a value\n\n${USAGE}`); + if (arg === "--outdir") outdir = value; + else source = value; + i += 1; + } else if (arg.startsWith("-")) { + throw new Error(`Unknown option ${arg}\n\n${USAGE}`); + } else if (entry) { + throw new Error(`Unexpected argument ${arg}\n\n${USAGE}`); + } else { + entry = arg; + } + } + if (!entry) throw new Error(`Missing entry file\n\n${USAGE}`); + return { entry, outdir, source: source ?? path.basename(entry) }; +} + +export interface PackMetadata { + airflow_bundle_metadata_version: string; + sdk: { language: string; version: string; supervisor_schema_version: string }; + source: string; + dags: BundleManifest["dags"]; +} + +// JSON string literals are valid YAML double-quoted scalars, so every +// scalar below is emitted through JSON.stringify for correct escaping. +export function renderMetadataYaml(metadata: PackMetadata): string { + const lines = [ + `airflow_bundle_metadata_version: ${JSON.stringify(metadata.airflow_bundle_metadata_version)}`, + "sdk:", + ` language: ${JSON.stringify(metadata.sdk.language)}`, + ` version: ${JSON.stringify(metadata.sdk.version)}`, + ` supervisor_schema_version: ${JSON.stringify(metadata.sdk.supervisor_schema_version)}`, + `source: ${JSON.stringify(metadata.source)}`, + "dags:", + ]; + for (const [dagId, dag] of Object.entries(metadata.dags)) { + lines.push(` ${JSON.stringify(dagId)}:`); + lines.push(` tasks: [${dag.tasks.map((task) => JSON.stringify(task)).join(", ")}]`); + } + return `${lines.join("\n")}\n`; +} + +function readSdkVersion(): string { + const packageJsonUrl = new URL("../../package.json", import.meta.url); + const { version } = JSON.parse(readFileSync(packageJsonUrl, "utf-8")) as { version: string }; + return version; +} + +function readBundleManifest(bundlePath: string): BundleManifest { + const stdout = execFileSync(process.execPath, [bundlePath, AIRFLOW_METADATA_FLAG], { + encoding: "utf-8", + }); + let manifest: BundleManifest; + try { + manifest = JSON.parse(stdout) as BundleManifest; + } catch (error) { + throw new Error(`Bundle produced invalid --airflow-metadata output: ${String(error)}`, { + cause: error, + }); + } + if (!manifest.supervisor_schema_version || typeof manifest.dags !== "object") { + throw new Error("Bundle produced incomplete --airflow-metadata output"); + } + return manifest; +} + +export async function runPack(argv: readonly string[]): Promise<void> { + const args = parsePackArgs(argv); + const bundlePath = path.join(args.outdir, BUNDLE_FILENAME); + + await build({ + entryPoints: [args.entry], + bundle: true, + platform: "node", + format: "esm", + target: "node22", + outfile: bundlePath, + }); + + const manifest = readBundleManifest(bundlePath); + if (Object.keys(manifest.dags).length === 0) { Review Comment: Fixed. The bundle is now built to a staging file and `bundle.mjs` is written only after validation passes, so the empty-registry error leaves no marker-less bundle behind. ########## ts-sdk/src/cli/pack.ts: ########## @@ -0,0 +1,167 @@ +/*! + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +// airflow-ts-pack: bundle a TypeScript entrypoint into the single-file +// artifact NodeCoordinator consumes — `bundle.mjs` with the airflow +// metadata embedded as a leading `//# airflowMetadata=<base64>` comment. +// +// Mirrors airflow-go-pack: build first, then run the built bundle with +// --airflow-metadata so the manifest comes from the bundle's own task +// registry and schema version, never from a hand-written sidecar. + +import { execFileSync } from "node:child_process"; +import { readFileSync, writeFileSync } from "node:fs"; +import path from "node:path"; + +import { build } from "esbuild"; + +import { AIRFLOW_METADATA_FLAG, type BundleManifest } from "../coordinator/runtime.js"; + +const AIRFLOW_BUNDLE_METADATA_VERSION = "1.0"; +const BUNDLE_FILENAME = "bundle.mjs"; +export const EMBEDDED_METADATA_PREFIX = "//# airflowMetadata="; + +const USAGE = `Usage: airflow-ts-pack <entry> [--outdir <dir>] [--source <name>] + +Bundles <entry> into <outdir>/${BUNDLE_FILENAME} with esbuild and embeds the +airflow metadata generated from the bundle's registered tasks. + +Options: + --outdir <dir> Output directory (default: dist) + --source <name> Display name of the primary source file (default: <entry> basename) +`; + +export interface PackArgs { + entry: string; + outdir: string; + source: string; +} + +export function parsePackArgs(argv: readonly string[]): PackArgs { Review Comment: Done. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
