@workflow/next 4.0.1-beta.5 → 4.0.1-beta.51

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,744 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.getNextBuilderDeferred = getNextBuilderDeferred;
7
+ const node_crypto_1 = require("node:crypto");
8
+ const node_fs_1 = require("node:fs");
9
+ const promises_1 = require("node:fs/promises");
10
+ const node_os_1 = __importDefault(require("node:os"));
11
+ const node_path_1 = require("node:path");
12
+ const socket_server_js_1 = require("./socket-server.js");
13
+ const ROUTE_STUB_FILE_MARKER = 'WORKFLOW_ROUTE_STUB_FILE';
14
+ let CachedNextBuilderDeferred;
15
+ // Create the deferred Next builder dynamically by extending the ESM BaseBuilder.
16
+ // Exported as getNextBuilderDeferred() to allow CommonJS modules to import from
17
+ // the ESM @workflow/builders package via dynamic import at runtime.
18
+ async function getNextBuilderDeferred() {
19
+ if (CachedNextBuilderDeferred) {
20
+ return CachedNextBuilderDeferred;
21
+ }
22
+ const { BaseBuilder: BaseBuilderClass, STEP_QUEUE_TRIGGER, WORKFLOW_QUEUE_TRIGGER, detectWorkflowPatterns, isWorkflowSdkFile,
23
+ // biome-ignore lint/security/noGlobalEval: Need to use eval here to avoid TypeScript from transpiling the import statement into `require()`
24
+ } = (await eval('import("@workflow/builders")'));
25
+ class NextDeferredBuilder extends BaseBuilderClass {
26
+ socketIO;
27
+ discoveredWorkflowFiles = new Set();
28
+ discoveredStepFiles = new Set();
29
+ discoveredSerdeFiles = new Set();
30
+ trackedDependencyFiles = new Set();
31
+ deferredBuildQueue = Promise.resolve();
32
+ cacheInitialized = false;
33
+ cacheWriteTimer = null;
34
+ lastDeferredBuildSignature = null;
35
+ async build() {
36
+ const outputDir = await this.findAppDirectory();
37
+ await this.initializeDiscoveryState();
38
+ await this.writeStubFiles(outputDir);
39
+ await this.createDiscoverySocketServer();
40
+ }
41
+ async onBeforeDeferredEntries() {
42
+ await this.initializeDiscoveryState();
43
+ await this.validateDiscoveredEntryFiles();
44
+ const implicitStepFiles = await this.resolveImplicitStepFiles();
45
+ const inputFiles = Array.from(new Set([
46
+ ...this.discoveredWorkflowFiles,
47
+ ...this.discoveredStepFiles,
48
+ ...implicitStepFiles,
49
+ ])).sort();
50
+ const pendingBuild = this.deferredBuildQueue.then(() => this.buildDeferredEntriesUntilStable(inputFiles, implicitStepFiles));
51
+ // Keep the queue chain alive even when the current build fails so future
52
+ // callbacks can enqueue another attempt without triggering unhandled
53
+ // rejection warnings.
54
+ this.deferredBuildQueue = pendingBuild.catch(() => {
55
+ // Error is surfaced through `pendingBuild` below.
56
+ });
57
+ await pendingBuild;
58
+ }
59
+ async buildDeferredEntriesUntilStable(inputFiles, implicitStepFiles) {
60
+ // A successful build can discover additional transitive dependency files
61
+ // (via source maps), which changes the signature and may require one more
62
+ // build pass to include newly discovered serde files.
63
+ const maxBuildPasses = 3;
64
+ for (let buildPass = 0; buildPass < maxBuildPasses; buildPass++) {
65
+ const buildSignature = await this.createDeferredBuildSignature(inputFiles);
66
+ if (buildSignature === this.lastDeferredBuildSignature) {
67
+ return;
68
+ }
69
+ let didBuildSucceed = false;
70
+ try {
71
+ await this.buildDiscoveredFiles(inputFiles, implicitStepFiles);
72
+ didBuildSucceed = true;
73
+ }
74
+ catch (error) {
75
+ if (this.config.watch) {
76
+ console.warn('[workflow] Deferred entries build failed. Will retry only after inputs change.', error);
77
+ }
78
+ else {
79
+ throw error;
80
+ }
81
+ }
82
+ finally {
83
+ // Record attempted signature even on failure so we don't loop on the
84
+ // same broken input graph.
85
+ this.lastDeferredBuildSignature = buildSignature;
86
+ }
87
+ if (!didBuildSucceed) {
88
+ return;
89
+ }
90
+ const postBuildSignature = await this.createDeferredBuildSignature(inputFiles);
91
+ if (postBuildSignature === buildSignature) {
92
+ return;
93
+ }
94
+ }
95
+ console.warn('[workflow] Deferred entries build signature did not stabilize after 3 passes.');
96
+ }
97
+ async resolveImplicitStepFiles() {
98
+ let workflowCjsEntry;
99
+ try {
100
+ workflowCjsEntry = require.resolve('workflow', {
101
+ paths: [this.config.workingDir],
102
+ });
103
+ }
104
+ catch {
105
+ return [];
106
+ }
107
+ const workflowDistDir = (0, node_path_1.dirname)(workflowCjsEntry);
108
+ const workflowStdlibPath = this.normalizeDiscoveredFilePath((0, node_path_1.join)(workflowDistDir, 'stdlib.js'));
109
+ const candidatePaths = [workflowStdlibPath];
110
+ const existingFiles = await Promise.all(candidatePaths.map(async (filePath) => {
111
+ try {
112
+ const fileStats = await (0, promises_1.stat)(filePath);
113
+ return fileStats.isFile() ? filePath : null;
114
+ }
115
+ catch {
116
+ return null;
117
+ }
118
+ }));
119
+ return existingFiles.filter((filePath) => Boolean(filePath));
120
+ }
121
+ areFileSetsEqual(a, b) {
122
+ if (a.size !== b.size) {
123
+ return false;
124
+ }
125
+ for (const filePath of a) {
126
+ if (!b.has(filePath)) {
127
+ return false;
128
+ }
129
+ }
130
+ return true;
131
+ }
132
+ async reconcileDiscoveredEntries({ workflowCandidates, stepCandidates, serdeCandidates, validatePatterns, }) {
133
+ const candidatesByFile = new Map();
134
+ for (const filePath of workflowCandidates) {
135
+ const normalizedPath = this.normalizeDiscoveredFilePath(filePath);
136
+ const existing = candidatesByFile.get(normalizedPath);
137
+ if (existing) {
138
+ existing.hasWorkflowCandidate = true;
139
+ }
140
+ else {
141
+ candidatesByFile.set(normalizedPath, {
142
+ hasWorkflowCandidate: true,
143
+ hasStepCandidate: false,
144
+ hasSerdeCandidate: false,
145
+ });
146
+ }
147
+ }
148
+ for (const filePath of stepCandidates) {
149
+ const normalizedPath = this.normalizeDiscoveredFilePath(filePath);
150
+ const existing = candidatesByFile.get(normalizedPath);
151
+ if (existing) {
152
+ existing.hasStepCandidate = true;
153
+ }
154
+ else {
155
+ candidatesByFile.set(normalizedPath, {
156
+ hasWorkflowCandidate: false,
157
+ hasStepCandidate: true,
158
+ hasSerdeCandidate: false,
159
+ });
160
+ }
161
+ }
162
+ if (serdeCandidates) {
163
+ for (const filePath of serdeCandidates) {
164
+ const normalizedPath = this.normalizeDiscoveredFilePath(filePath);
165
+ const existing = candidatesByFile.get(normalizedPath);
166
+ if (existing) {
167
+ existing.hasSerdeCandidate = true;
168
+ }
169
+ else {
170
+ candidatesByFile.set(normalizedPath, {
171
+ hasWorkflowCandidate: false,
172
+ hasStepCandidate: false,
173
+ hasSerdeCandidate: true,
174
+ });
175
+ }
176
+ }
177
+ }
178
+ const fileEntries = Array.from(candidatesByFile.entries()).sort(([a], [b]) => a.localeCompare(b));
179
+ const validatedEntries = await Promise.all(fileEntries.map(async ([filePath, candidates]) => {
180
+ try {
181
+ const fileStats = await (0, promises_1.stat)(filePath);
182
+ if (!fileStats.isFile()) {
183
+ return null;
184
+ }
185
+ if (!validatePatterns) {
186
+ const isSdkFile = isWorkflowSdkFile(filePath);
187
+ return {
188
+ filePath,
189
+ hasUseWorkflow: candidates.hasWorkflowCandidate,
190
+ hasUseStep: candidates.hasStepCandidate,
191
+ hasSerde: candidates.hasSerdeCandidate && !isSdkFile,
192
+ };
193
+ }
194
+ const source = await (0, promises_1.readFile)(filePath, 'utf-8');
195
+ const patterns = detectWorkflowPatterns(source);
196
+ const isSdkFile = isWorkflowSdkFile(filePath);
197
+ return {
198
+ filePath,
199
+ hasUseWorkflow: patterns.hasUseWorkflow,
200
+ hasUseStep: patterns.hasUseStep,
201
+ hasSerde: patterns.hasSerde && !isSdkFile,
202
+ };
203
+ }
204
+ catch {
205
+ return null;
206
+ }
207
+ }));
208
+ const workflowFiles = new Set();
209
+ const stepFiles = new Set();
210
+ const serdeFiles = new Set();
211
+ for (const entry of validatedEntries) {
212
+ if (!entry) {
213
+ continue;
214
+ }
215
+ if (entry.hasUseWorkflow) {
216
+ workflowFiles.add(entry.filePath);
217
+ }
218
+ if (entry.hasUseStep) {
219
+ stepFiles.add(entry.filePath);
220
+ }
221
+ if (entry.hasSerde) {
222
+ serdeFiles.add(entry.filePath);
223
+ }
224
+ }
225
+ return { workflowFiles, stepFiles, serdeFiles };
226
+ }
227
+ async validateDiscoveredEntryFiles() {
228
+ const { workflowFiles, stepFiles, serdeFiles } = await this.reconcileDiscoveredEntries({
229
+ workflowCandidates: this.discoveredWorkflowFiles,
230
+ stepCandidates: this.discoveredStepFiles,
231
+ serdeCandidates: this.discoveredSerdeFiles,
232
+ validatePatterns: true,
233
+ });
234
+ const workflowsChanged = !this.areFileSetsEqual(this.discoveredWorkflowFiles, workflowFiles);
235
+ const stepsChanged = !this.areFileSetsEqual(this.discoveredStepFiles, stepFiles);
236
+ const serdeChanged = !this.areFileSetsEqual(this.discoveredSerdeFiles, serdeFiles);
237
+ if (workflowsChanged || stepsChanged || serdeChanged) {
238
+ this.discoveredWorkflowFiles.clear();
239
+ this.discoveredStepFiles.clear();
240
+ this.discoveredSerdeFiles.clear();
241
+ for (const filePath of workflowFiles) {
242
+ this.discoveredWorkflowFiles.add(filePath);
243
+ }
244
+ for (const filePath of stepFiles) {
245
+ this.discoveredStepFiles.add(filePath);
246
+ }
247
+ for (const filePath of serdeFiles) {
248
+ this.discoveredSerdeFiles.add(filePath);
249
+ }
250
+ }
251
+ if (workflowsChanged || stepsChanged) {
252
+ this.scheduleWorkflowsCacheWrite();
253
+ }
254
+ }
255
+ async buildDiscoveredFiles(inputFiles, implicitStepFiles) {
256
+ const outputDir = await this.findAppDirectory();
257
+ const workflowGeneratedDir = (0, node_path_1.join)(outputDir, '.well-known/workflow/v1');
258
+ const cacheDir = (0, node_path_1.join)(this.config.workingDir, this.getDistDir(), 'cache');
259
+ await (0, promises_1.mkdir)(cacheDir, { recursive: true });
260
+ const manifestBuildDir = (0, node_path_1.join)(cacheDir, 'workflow-generated-manifest');
261
+ const tempRouteFileName = 'route.js.temp';
262
+ const discoveredStepFiles = Array.from(new Set([...this.discoveredStepFiles, ...implicitStepFiles])).sort();
263
+ const discoveredWorkflowFiles = Array.from(this.discoveredWorkflowFiles).sort();
264
+ const trackedSerdeFiles = await this.collectTrackedSerdeFiles();
265
+ const discoveredSerdeFiles = Array.from(new Set([...this.discoveredSerdeFiles, ...trackedSerdeFiles])).sort();
266
+ const discoveredEntries = {
267
+ discoveredSteps: discoveredStepFiles,
268
+ discoveredWorkflows: discoveredWorkflowFiles,
269
+ discoveredSerdeFiles,
270
+ };
271
+ // Ensure output directories exist
272
+ await (0, promises_1.mkdir)(workflowGeneratedDir, { recursive: true });
273
+ await this.writeFileIfChanged((0, node_path_1.join)(workflowGeneratedDir, '.gitignore'), '*');
274
+ const tsconfigPath = await this.findTsConfigPath();
275
+ const options = {
276
+ inputFiles,
277
+ workflowGeneratedDir,
278
+ tsconfigPath,
279
+ routeFileName: tempRouteFileName,
280
+ discoveredEntries,
281
+ };
282
+ const { manifest: stepsManifest } = await this.buildStepsFunction(options);
283
+ const workflowsBundle = await this.buildWorkflowsFunction(options);
284
+ await this.buildWebhookRoute({
285
+ workflowGeneratedDir,
286
+ routeFileName: tempRouteFileName,
287
+ });
288
+ await this.refreshTrackedDependencyFiles(workflowGeneratedDir, tempRouteFileName);
289
+ // Merge manifests from both bundles
290
+ const manifest = {
291
+ steps: { ...stepsManifest.steps, ...workflowsBundle?.manifest?.steps },
292
+ workflows: {
293
+ ...stepsManifest.workflows,
294
+ ...workflowsBundle?.manifest?.workflows,
295
+ },
296
+ classes: {
297
+ ...stepsManifest.classes,
298
+ ...workflowsBundle?.manifest?.classes,
299
+ },
300
+ };
301
+ const manifestFilePath = (0, node_path_1.join)(workflowGeneratedDir, 'manifest.json');
302
+ const manifestBuildPath = (0, node_path_1.join)(manifestBuildDir, 'manifest.json');
303
+ const workflowBundlePath = (0, node_path_1.join)(workflowGeneratedDir, `flow/${tempRouteFileName}`);
304
+ const manifestJson = await this.createManifest({
305
+ workflowBundlePath,
306
+ manifestDir: manifestBuildDir,
307
+ manifest,
308
+ });
309
+ await this.rewriteJsonFileWithStableKeyOrder(manifestBuildPath);
310
+ await this.copyFileIfChanged(manifestBuildPath, manifestFilePath);
311
+ await this.writeFunctionsConfig(outputDir);
312
+ await this.copyFileIfChanged((0, node_path_1.join)(workflowGeneratedDir, `flow/${tempRouteFileName}`), (0, node_path_1.join)(workflowGeneratedDir, 'flow/route.js'));
313
+ await this.copyFileIfChanged((0, node_path_1.join)(workflowGeneratedDir, `step/${tempRouteFileName}`), (0, node_path_1.join)(workflowGeneratedDir, 'step/route.js'));
314
+ await this.copyFileIfChanged((0, node_path_1.join)(workflowGeneratedDir, `webhook/[token]/${tempRouteFileName}`), (0, node_path_1.join)(workflowGeneratedDir, 'webhook/[token]/route.js'));
315
+ // Expose manifest as a static file when WORKFLOW_PUBLIC_MANIFEST=1.
316
+ // Next.js serves files from public/ at the root URL.
317
+ if (this.shouldExposePublicManifest && manifestJson) {
318
+ const publicManifestDir = (0, node_path_1.join)(this.config.workingDir, 'public/.well-known/workflow/v1');
319
+ await (0, promises_1.mkdir)(publicManifestDir, { recursive: true });
320
+ await this.copyFileIfChanged(manifestFilePath, (0, node_path_1.join)(publicManifestDir, 'manifest.json'));
321
+ }
322
+ // Notify deferred entry loaders waiting on route.js stubs.
323
+ this.socketIO?.emit('build-complete');
324
+ }
325
+ async createDiscoverySocketServer() {
326
+ if (this.socketIO || process.env.WORKFLOW_SOCKET_PORT) {
327
+ return;
328
+ }
329
+ const config = {
330
+ isDevServer: Boolean(this.config.watch),
331
+ onFileDiscovered: (filePath, hasWorkflow, hasStep, hasSerde) => {
332
+ const normalizedFilePath = this.normalizeDiscoveredFilePath(filePath);
333
+ let hasCacheTrackingChange = false;
334
+ if (hasWorkflow) {
335
+ if (!this.discoveredWorkflowFiles.has(normalizedFilePath)) {
336
+ this.discoveredWorkflowFiles.add(normalizedFilePath);
337
+ hasCacheTrackingChange = true;
338
+ }
339
+ }
340
+ else {
341
+ const wasDeleted = this.discoveredWorkflowFiles.delete(normalizedFilePath);
342
+ hasCacheTrackingChange = wasDeleted || hasCacheTrackingChange;
343
+ }
344
+ if (hasStep) {
345
+ if (!this.discoveredStepFiles.has(normalizedFilePath)) {
346
+ this.discoveredStepFiles.add(normalizedFilePath);
347
+ hasCacheTrackingChange = true;
348
+ }
349
+ }
350
+ else {
351
+ const wasDeleted = this.discoveredStepFiles.delete(normalizedFilePath);
352
+ hasCacheTrackingChange = wasDeleted || hasCacheTrackingChange;
353
+ }
354
+ if (hasSerde) {
355
+ this.discoveredSerdeFiles.add(normalizedFilePath);
356
+ }
357
+ else {
358
+ this.discoveredSerdeFiles.delete(normalizedFilePath);
359
+ }
360
+ if (hasCacheTrackingChange) {
361
+ this.scheduleWorkflowsCacheWrite();
362
+ }
363
+ },
364
+ onTriggerBuild: () => {
365
+ // Deferred builder builds via onBeforeDeferredEntries callback.
366
+ },
367
+ };
368
+ this.socketIO = await (0, socket_server_js_1.createSocketServer)(config);
369
+ }
370
+ async initializeDiscoveryState() {
371
+ if (this.cacheInitialized) {
372
+ return;
373
+ }
374
+ await this.loadWorkflowsCache();
375
+ this.cacheInitialized = true;
376
+ }
377
+ getDistDir() {
378
+ return this.config.distDir || '.next';
379
+ }
380
+ getWorkflowsCacheFilePath() {
381
+ return (0, node_path_1.join)(this.config.workingDir, this.getDistDir(), 'cache', 'workflows.json');
382
+ }
383
+ normalizeDiscoveredFilePath(filePath) {
384
+ return (0, node_path_1.isAbsolute)(filePath)
385
+ ? filePath
386
+ : (0, node_path_1.resolve)(this.config.workingDir, filePath);
387
+ }
388
+ async createDeferredBuildSignature(inputFiles) {
389
+ const normalizedFiles = Array.from(new Set([
390
+ ...inputFiles.map((filePath) => this.normalizeDiscoveredFilePath(filePath)),
391
+ ...this.trackedDependencyFiles,
392
+ ])).sort();
393
+ const signatureParts = await Promise.all(normalizedFiles.map(async (filePath) => {
394
+ try {
395
+ const fileStats = await (0, promises_1.stat)(filePath);
396
+ return `${filePath}:${fileStats.size}:${Math.trunc(fileStats.mtimeMs)}`;
397
+ }
398
+ catch {
399
+ return `${filePath}:missing`;
400
+ }
401
+ }));
402
+ const signatureHash = (0, node_crypto_1.createHash)('sha256');
403
+ for (const signaturePart of signatureParts) {
404
+ signatureHash.update(signaturePart);
405
+ signatureHash.update('\n');
406
+ }
407
+ return signatureHash.digest('hex');
408
+ }
409
+ async collectTrackedSerdeFiles() {
410
+ if (this.trackedDependencyFiles.size === 0) {
411
+ return [];
412
+ }
413
+ const { serdeFiles } = await this.reconcileDiscoveredEntries({
414
+ workflowCandidates: [],
415
+ stepCandidates: [],
416
+ serdeCandidates: this.trackedDependencyFiles,
417
+ validatePatterns: true,
418
+ });
419
+ return Array.from(serdeFiles);
420
+ }
421
+ async refreshTrackedDependencyFiles(workflowGeneratedDir, routeFileName) {
422
+ const bundleFiles = [
423
+ (0, node_path_1.join)(workflowGeneratedDir, `step/${routeFileName}`),
424
+ (0, node_path_1.join)(workflowGeneratedDir, `flow/${routeFileName}`),
425
+ ];
426
+ const trackedFiles = new Set();
427
+ for (const bundleFile of bundleFiles) {
428
+ const bundleSources = await this.extractBundleSourceFiles(bundleFile);
429
+ for (const sourceFile of bundleSources) {
430
+ trackedFiles.add(sourceFile);
431
+ }
432
+ }
433
+ if (trackedFiles.size > 0) {
434
+ this.trackedDependencyFiles = trackedFiles;
435
+ }
436
+ }
437
+ async extractBundleSourceFiles(bundleFilePath) {
438
+ let bundleContents;
439
+ try {
440
+ bundleContents = await (0, promises_1.readFile)(bundleFilePath, 'utf-8');
441
+ }
442
+ catch {
443
+ return [];
444
+ }
445
+ const baseDirectory = (0, node_path_1.dirname)(bundleFilePath);
446
+ const localSourceFiles = new Set();
447
+ const sourceMapMatches = bundleContents.matchAll(/\/\/# sourceMappingURL=data:application\/json[^,]*;base64,([A-Za-z0-9+/=]+)/g);
448
+ for (const match of sourceMapMatches) {
449
+ const base64Value = match[1];
450
+ if (!base64Value) {
451
+ continue;
452
+ }
453
+ let sourceMap;
454
+ try {
455
+ sourceMap = JSON.parse(Buffer.from(base64Value, 'base64').toString('utf-8'));
456
+ }
457
+ catch {
458
+ continue;
459
+ }
460
+ const sourceRoot = typeof sourceMap.sourceRoot === 'string' ? sourceMap.sourceRoot : '';
461
+ const sources = Array.isArray(sourceMap.sources)
462
+ ? sourceMap.sources.filter((source) => typeof source === 'string')
463
+ : [];
464
+ for (const source of sources) {
465
+ if (source.startsWith('webpack://') || source.startsWith('<')) {
466
+ continue;
467
+ }
468
+ let resolvedSourcePath;
469
+ if (source.startsWith('file://')) {
470
+ try {
471
+ resolvedSourcePath = decodeURIComponent(new URL(source).pathname);
472
+ }
473
+ catch {
474
+ continue;
475
+ }
476
+ }
477
+ else if ((0, node_path_1.isAbsolute)(source)) {
478
+ resolvedSourcePath = source;
479
+ }
480
+ else {
481
+ resolvedSourcePath = (0, node_path_1.resolve)(baseDirectory, sourceRoot, source);
482
+ }
483
+ const normalizedSourcePath = this.normalizeDiscoveredFilePath(resolvedSourcePath);
484
+ const normalizedSourcePathForCheck = normalizedSourcePath.replace(/\\/g, '/');
485
+ if (normalizedSourcePathForCheck.includes('/.well-known/workflow/') ||
486
+ normalizedSourcePathForCheck.includes('/node_modules/') ||
487
+ normalizedSourcePathForCheck.includes('/.pnpm/') ||
488
+ normalizedSourcePathForCheck.includes('/.next/') ||
489
+ normalizedSourcePathForCheck.endsWith('/virtual-entry.js')) {
490
+ continue;
491
+ }
492
+ localSourceFiles.add(normalizedSourcePath);
493
+ }
494
+ }
495
+ return Array.from(localSourceFiles);
496
+ }
497
+ scheduleWorkflowsCacheWrite() {
498
+ if (this.cacheWriteTimer) {
499
+ clearTimeout(this.cacheWriteTimer);
500
+ }
501
+ this.cacheWriteTimer = setTimeout(() => {
502
+ this.cacheWriteTimer = null;
503
+ void this.writeWorkflowsCache().catch((error) => {
504
+ console.warn('Failed to write workflow discovery cache', error);
505
+ });
506
+ }, 50);
507
+ }
508
+ async readWorkflowsCache() {
509
+ const cacheFilePath = this.getWorkflowsCacheFilePath();
510
+ try {
511
+ const cacheContents = await (0, promises_1.readFile)(cacheFilePath, 'utf-8');
512
+ const parsed = JSON.parse(cacheContents);
513
+ const workflowFiles = Array.isArray(parsed.workflowFiles)
514
+ ? parsed.workflowFiles.filter((item) => typeof item === 'string')
515
+ : [];
516
+ const stepFiles = Array.isArray(parsed.stepFiles)
517
+ ? parsed.stepFiles.filter((item) => typeof item === 'string')
518
+ : [];
519
+ return { workflowFiles, stepFiles };
520
+ }
521
+ catch {
522
+ return null;
523
+ }
524
+ }
525
+ async loadWorkflowsCache() {
526
+ const cachedData = await this.readWorkflowsCache();
527
+ if (!cachedData) {
528
+ return;
529
+ }
530
+ const { workflowFiles, stepFiles, serdeFiles } = await this.reconcileDiscoveredEntries({
531
+ workflowCandidates: cachedData.workflowFiles,
532
+ stepCandidates: cachedData.stepFiles,
533
+ serdeCandidates: this.discoveredSerdeFiles,
534
+ validatePatterns: true,
535
+ });
536
+ this.discoveredWorkflowFiles.clear();
537
+ this.discoveredStepFiles.clear();
538
+ this.discoveredSerdeFiles.clear();
539
+ for (const filePath of workflowFiles) {
540
+ this.discoveredWorkflowFiles.add(filePath);
541
+ }
542
+ for (const filePath of stepFiles) {
543
+ this.discoveredStepFiles.add(filePath);
544
+ }
545
+ for (const filePath of serdeFiles) {
546
+ this.discoveredSerdeFiles.add(filePath);
547
+ }
548
+ }
549
+ async writeWorkflowsCache() {
550
+ const cacheFilePath = this.getWorkflowsCacheFilePath();
551
+ const cacheDir = (0, node_path_1.join)(this.config.workingDir, this.getDistDir(), 'cache');
552
+ await (0, promises_1.mkdir)(cacheDir, { recursive: true });
553
+ const cacheData = {
554
+ workflowFiles: Array.from(this.discoveredWorkflowFiles).sort(),
555
+ stepFiles: Array.from(this.discoveredStepFiles).sort(),
556
+ };
557
+ await (0, promises_1.writeFile)(cacheFilePath, JSON.stringify(cacheData, null, 2));
558
+ }
559
+ async writeStubFiles(outputDir) {
560
+ // Turbopack currently has a worker-concurrency limitation for pending
561
+ // virtual entries. Warn if parallelism is too low to reliably discover.
562
+ const parallelismCount = node_os_1.default.availableParallelism();
563
+ if (process.env.TURBOPACK && parallelismCount < 4) {
564
+ console.warn(`Available parallelism of ${parallelismCount} is less than needed 4. This can cause workflows/steps to fail to discover properly in turbopack`);
565
+ }
566
+ const routeStubContent = [
567
+ `// ${ROUTE_STUB_FILE_MARKER}`,
568
+ 'export const __workflowRouteStub = true;',
569
+ ].join('\n');
570
+ const workflowGeneratedDir = (0, node_path_1.join)(outputDir, '.well-known/workflow/v1');
571
+ await (0, promises_1.mkdir)((0, node_path_1.join)(workflowGeneratedDir, 'flow'), { recursive: true });
572
+ await (0, promises_1.mkdir)((0, node_path_1.join)(workflowGeneratedDir, 'step'), { recursive: true });
573
+ await (0, promises_1.mkdir)((0, node_path_1.join)(workflowGeneratedDir, 'webhook/[token]'), {
574
+ recursive: true,
575
+ });
576
+ await this.writeFileIfChanged((0, node_path_1.join)(workflowGeneratedDir, '.gitignore'), '*');
577
+ // route.js stubs are replaced by generated route.js output once discovery
578
+ // finishes and a deferred build completes.
579
+ await this.writeFileIfChanged((0, node_path_1.join)(workflowGeneratedDir, 'flow/route.js'), routeStubContent);
580
+ await this.writeFileIfChanged((0, node_path_1.join)(workflowGeneratedDir, 'step/route.js'), routeStubContent);
581
+ await this.writeFileIfChanged((0, node_path_1.join)(workflowGeneratedDir, 'webhook/[token]/route.js'), routeStubContent);
582
+ }
583
+ async getInputFiles() {
584
+ const inputFiles = await super.getInputFiles();
585
+ return inputFiles.filter((item) => {
586
+ // Match App Router entrypoints: route.ts, page.ts, layout.ts in app/ or src/app/ directories
587
+ // Matches: /app/page.ts, /app/dashboard/page.ts, /src/app/route.ts, etc.
588
+ if (item.match(/(^|.*[/\\])(app|src[/\\]app)([/\\](route|page|layout)\.|[/\\].*[/\\](route|page|layout)\.)/)) {
589
+ return true;
590
+ }
591
+ // Match Pages Router entrypoints: files in pages/ or src/pages/
592
+ if (item.match(/[/\\](pages|src[/\\]pages)[/\\]/)) {
593
+ return true;
594
+ }
595
+ return false;
596
+ });
597
+ }
598
+ async writeFunctionsConfig(outputDir) {
599
+ // we don't run this in development mode as it's not needed
600
+ if (process.env.NODE_ENV === 'development') {
601
+ return;
602
+ }
603
+ const generatedConfig = {
604
+ version: '0',
605
+ steps: {
606
+ experimentalTriggers: [STEP_QUEUE_TRIGGER],
607
+ },
608
+ workflows: {
609
+ experimentalTriggers: [WORKFLOW_QUEUE_TRIGGER],
610
+ },
611
+ };
612
+ // We write this file to the generated directory for
613
+ // the Next.js builder to consume
614
+ await this.writeFileIfChanged((0, node_path_1.join)(outputDir, '.well-known/workflow/v1/config.json'), JSON.stringify(generatedConfig, null, 2));
615
+ }
616
+ async writeFileIfChanged(filePath, contents) {
617
+ const nextBuffer = Buffer.isBuffer(contents)
618
+ ? contents
619
+ : Buffer.from(contents);
620
+ try {
621
+ const currentBuffer = await (0, promises_1.readFile)(filePath);
622
+ if (currentBuffer.equals(nextBuffer)) {
623
+ return false;
624
+ }
625
+ }
626
+ catch {
627
+ // File does not exist yet or cannot be read; write a fresh copy.
628
+ }
629
+ await (0, promises_1.mkdir)((0, node_path_1.dirname)(filePath), { recursive: true });
630
+ await (0, promises_1.writeFile)(filePath, nextBuffer);
631
+ return true;
632
+ }
633
+ async copyFileIfChanged(sourcePath, destinationPath) {
634
+ const sourceContents = await (0, promises_1.readFile)(sourcePath);
635
+ return this.writeFileIfChanged(destinationPath, sourceContents);
636
+ }
637
+ sortJsonValue(value) {
638
+ if (Array.isArray(value)) {
639
+ return value.map((item) => this.sortJsonValue(item));
640
+ }
641
+ if (value && typeof value === 'object') {
642
+ const sortedEntries = Object.entries(value)
643
+ .sort(([a], [b]) => a.localeCompare(b))
644
+ .map(([key, entryValue]) => [key, this.sortJsonValue(entryValue)]);
645
+ return Object.fromEntries(sortedEntries);
646
+ }
647
+ return value;
648
+ }
649
+ async rewriteJsonFileWithStableKeyOrder(filePath) {
650
+ try {
651
+ const contents = await (0, promises_1.readFile)(filePath, 'utf-8');
652
+ const parsed = JSON.parse(contents);
653
+ const normalized = this.sortJsonValue(parsed);
654
+ await this.writeFileIfChanged(filePath, `${JSON.stringify(normalized, null, 2)}\n`);
655
+ }
656
+ catch {
657
+ // Manifest may not exist (e.g. manifest generation failed); ignore.
658
+ }
659
+ }
660
+ async buildStepsFunction({ inputFiles, workflowGeneratedDir, tsconfigPath, routeFileName = 'route.js', discoveredEntries, }) {
661
+ // Create steps bundle
662
+ const stepsRouteDir = (0, node_path_1.join)(workflowGeneratedDir, 'step');
663
+ await (0, promises_1.mkdir)(stepsRouteDir, { recursive: true });
664
+ return await this.createStepsBundle({
665
+ // If any dynamic requires are used when bundling with ESM
666
+ // esbuild will create a too dynamic wrapper around require
667
+ // which turbopack/webpack fail to analyze. If we externalize
668
+ // correctly this shouldn't be an issue although we might want
669
+ // to use cjs as alternative to avoid
670
+ format: 'esm',
671
+ inputFiles,
672
+ outfile: (0, node_path_1.join)(stepsRouteDir, routeFileName),
673
+ externalizeNonSteps: true,
674
+ tsconfigPath,
675
+ discoveredEntries,
676
+ });
677
+ }
678
+ async buildWorkflowsFunction({ inputFiles, workflowGeneratedDir, tsconfigPath, routeFileName = 'route.js', discoveredEntries, }) {
679
+ const workflowsRouteDir = (0, node_path_1.join)(workflowGeneratedDir, 'flow');
680
+ await (0, promises_1.mkdir)(workflowsRouteDir, { recursive: true });
681
+ return await this.createWorkflowsBundle({
682
+ format: 'esm',
683
+ outfile: (0, node_path_1.join)(workflowsRouteDir, routeFileName),
684
+ bundleFinalOutput: false,
685
+ inputFiles,
686
+ tsconfigPath,
687
+ discoveredEntries,
688
+ });
689
+ }
690
+ async buildWebhookRoute({ workflowGeneratedDir, routeFileName = 'route.js', }) {
691
+ const webhookRouteFile = (0, node_path_1.join)(workflowGeneratedDir, `webhook/[token]/${routeFileName}`);
692
+ await this.createWebhookBundle({
693
+ outfile: webhookRouteFile,
694
+ bundle: false, // Next.js doesn't need bundling
695
+ });
696
+ }
697
+ async findAppDirectory() {
698
+ const appDir = (0, node_path_1.resolve)(this.config.workingDir, 'app');
699
+ const srcAppDir = (0, node_path_1.resolve)(this.config.workingDir, 'src/app');
700
+ const pagesDir = (0, node_path_1.resolve)(this.config.workingDir, 'pages');
701
+ const srcPagesDir = (0, node_path_1.resolve)(this.config.workingDir, 'src/pages');
702
+ // Helper to check if a path exists and is a directory
703
+ const isDirectory = async (path) => {
704
+ try {
705
+ await (0, promises_1.access)(path, node_fs_1.constants.F_OK);
706
+ const stats = await (0, promises_1.stat)(path);
707
+ if (!stats.isDirectory()) {
708
+ throw new Error(`Path exists but is not a directory: ${path}`);
709
+ }
710
+ return true;
711
+ }
712
+ catch (e) {
713
+ if (e instanceof Error && e.message.includes('not a directory')) {
714
+ throw e;
715
+ }
716
+ return false;
717
+ }
718
+ };
719
+ // Check if app directory exists
720
+ if (await isDirectory(appDir)) {
721
+ return appDir;
722
+ }
723
+ // Check if src/app directory exists
724
+ if (await isDirectory(srcAppDir)) {
725
+ return srcAppDir;
726
+ }
727
+ // If no app directory exists, check for pages directory and create app next to it
728
+ if (await isDirectory(pagesDir)) {
729
+ // Create app directory next to pages directory
730
+ await (0, promises_1.mkdir)(appDir, { recursive: true });
731
+ return appDir;
732
+ }
733
+ if (await isDirectory(srcPagesDir)) {
734
+ // Create src/app directory next to src/pages directory
735
+ await (0, promises_1.mkdir)(srcAppDir, { recursive: true });
736
+ return srcAppDir;
737
+ }
738
+ throw new Error('Could not find Next.js app or pages directory. Expected one of: "app", "src/app", "pages", or "src/pages" to exist.');
739
+ }
740
+ }
741
+ CachedNextBuilderDeferred = NextDeferredBuilder;
742
+ return NextDeferredBuilder;
743
+ }
744
+ //# sourceMappingURL=builder-deferred.js.map