|
1 | 1 | import path from 'node:path'; |
2 | 2 | import { WorkflowRunNotFoundError } from '@workflow/errors'; |
3 | 3 | import { |
| 4 | + type CreateHookRequest, |
4 | 5 | type Event, |
5 | 6 | EventSchema, |
| 7 | + type GetHookParams, |
6 | 8 | type Hook, |
7 | 9 | HookSchema, |
| 10 | + type ListHooksParams, |
| 11 | + type PaginatedResponse, |
8 | 12 | type Step, |
9 | 13 | StepSchema, |
10 | 14 | type Storage, |
@@ -94,6 +98,123 @@ const getObjectCreatedAt = |
94 | 98 | return ulidToDate(ulid); |
95 | 99 | }; |
96 | 100 |
|
| 101 | +/** |
| 102 | + * Creates a hooks storage implementation using the filesystem. |
| 103 | + * Implements the Storage['hooks'] interface with hook CRUD operations. |
| 104 | + */ |
| 105 | +function createHooksStorage(basedir: string): Storage['hooks'] { |
| 106 | + // Helper function to find a hook by token (shared between create and getByToken) |
| 107 | + async function findHookByToken(token: string): Promise<Hook | null> { |
| 108 | + const hooksDir = path.join(basedir, 'hooks'); |
| 109 | + const files = await listJSONFiles(hooksDir); |
| 110 | + |
| 111 | + for (const file of files) { |
| 112 | + const hookPath = path.join(hooksDir, `${file}.json`); |
| 113 | + const hook = await readJSON(hookPath, HookSchema); |
| 114 | + if (hook && hook.token === token) { |
| 115 | + return hook; |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + return null; |
| 120 | + } |
| 121 | + |
| 122 | + async function create(runId: string, data: CreateHookRequest): Promise<Hook> { |
| 123 | + // Check if a hook with the same token already exists |
| 124 | + // Token uniqueness is enforced globally per embedded environment |
| 125 | + const existingHook = await findHookByToken(data.token); |
| 126 | + if (existingHook) { |
| 127 | + throw new Error( |
| 128 | + `Hook with token ${data.token} already exists for this project` |
| 129 | + ); |
| 130 | + } |
| 131 | + |
| 132 | + const now = new Date(); |
| 133 | + |
| 134 | + const result = { |
| 135 | + runId, |
| 136 | + hookId: data.hookId, |
| 137 | + token: data.token, |
| 138 | + metadata: data.metadata, |
| 139 | + ownerId: 'embedded-owner', |
| 140 | + projectId: 'embedded-project', |
| 141 | + environment: 'embedded', |
| 142 | + createdAt: now, |
| 143 | + } as Hook; |
| 144 | + |
| 145 | + const hookPath = path.join(basedir, 'hooks', `${data.hookId}.json`); |
| 146 | + await writeJSON(hookPath, result); |
| 147 | + return result; |
| 148 | + } |
| 149 | + |
| 150 | + async function get(hookId: string, params?: GetHookParams): Promise<Hook> { |
| 151 | + const hookPath = path.join(basedir, 'hooks', `${hookId}.json`); |
| 152 | + const hook = await readJSON(hookPath, HookSchema); |
| 153 | + if (!hook) { |
| 154 | + throw new Error(`Hook ${hookId} not found`); |
| 155 | + } |
| 156 | + const resolveData = params?.resolveData || DEFAULT_RESOLVE_DATA_OPTION; |
| 157 | + return filterHookData(hook, resolveData); |
| 158 | + } |
| 159 | + |
| 160 | + async function getByToken(token: string): Promise<Hook> { |
| 161 | + const hook = await findHookByToken(token); |
| 162 | + if (!hook) { |
| 163 | + throw new Error(`Hook with token ${token} not found`); |
| 164 | + } |
| 165 | + return hook; |
| 166 | + } |
| 167 | + |
| 168 | + async function list( |
| 169 | + params: ListHooksParams |
| 170 | + ): Promise<PaginatedResponse<Hook>> { |
| 171 | + const hooksDir = path.join(basedir, 'hooks'); |
| 172 | + const resolveData = params.resolveData || DEFAULT_RESOLVE_DATA_OPTION; |
| 173 | + |
| 174 | + const result = await paginatedFileSystemQuery({ |
| 175 | + directory: hooksDir, |
| 176 | + schema: HookSchema, |
| 177 | + sortOrder: params.pagination?.sortOrder, |
| 178 | + limit: params.pagination?.limit, |
| 179 | + cursor: params.pagination?.cursor, |
| 180 | + filePrefix: undefined, // Hooks don't have ULIDs, so we can't optimize by filename |
| 181 | + filter: (hook) => { |
| 182 | + // Filter by runId if provided |
| 183 | + if (params.runId && hook.runId !== params.runId) { |
| 184 | + return false; |
| 185 | + } |
| 186 | + return true; |
| 187 | + }, |
| 188 | + getCreatedAt: () => { |
| 189 | + // Hook files don't have ULID timestamps in filename |
| 190 | + // We need to read the file to get createdAt, but that's inefficient |
| 191 | + // So we return the hook's createdAt directly (item.createdAt will be used for sorting) |
| 192 | + // Return a dummy date to pass the null check, actual sorting uses item.createdAt |
| 193 | + return new Date(0); |
| 194 | + }, |
| 195 | + getId: (hook) => hook.hookId, |
| 196 | + }); |
| 197 | + |
| 198 | + // Transform the data after pagination |
| 199 | + return { |
| 200 | + ...result, |
| 201 | + data: result.data.map((hook) => filterHookData(hook, resolveData)), |
| 202 | + }; |
| 203 | + } |
| 204 | + |
| 205 | + async function dispose(hookId: string): Promise<Hook> { |
| 206 | + const hookPath = path.join(basedir, 'hooks', `${hookId}.json`); |
| 207 | + const hook = await readJSON(hookPath, HookSchema); |
| 208 | + if (!hook) { |
| 209 | + throw new Error(`Hook ${hookId} not found`); |
| 210 | + } |
| 211 | + await deleteJSON(hookPath); |
| 212 | + return hook; |
| 213 | + } |
| 214 | + |
| 215 | + return { create, get, getByToken, list, dispose }; |
| 216 | +} |
| 217 | + |
97 | 218 | export function createStorage(basedir: string): Storage { |
98 | 219 | return { |
99 | 220 | runs: { |
@@ -415,97 +536,6 @@ export function createStorage(basedir: string): Storage { |
415 | 536 | }, |
416 | 537 |
|
417 | 538 | // Hooks |
418 | | - hooks: { |
419 | | - async create(runId, data) { |
420 | | - const now = new Date(); |
421 | | - |
422 | | - const result = { |
423 | | - runId, |
424 | | - hookId: data.hookId, |
425 | | - token: data.token, |
426 | | - metadata: data.metadata, |
427 | | - ownerId: 'embedded-owner', |
428 | | - projectId: 'embedded-project', |
429 | | - environment: 'embedded', |
430 | | - createdAt: now, |
431 | | - } as Hook; |
432 | | - |
433 | | - const hookPath = path.join(basedir, 'hooks', `${data.hookId}.json`); |
434 | | - await writeJSON(hookPath, result); |
435 | | - return result; |
436 | | - }, |
437 | | - |
438 | | - async get(hookId, params) { |
439 | | - const hookPath = path.join(basedir, 'hooks', `${hookId}.json`); |
440 | | - // Try webhook schema first (which includes response), fall back to HookSchema |
441 | | - const hook = await readJSON(hookPath, HookSchema); |
442 | | - if (!hook) { |
443 | | - throw new Error(`Hook ${hookId} not found`); |
444 | | - } |
445 | | - const resolveData = params?.resolveData || DEFAULT_RESOLVE_DATA_OPTION; |
446 | | - return filterHookData(hook, resolveData); |
447 | | - }, |
448 | | - |
449 | | - async getByToken(token) { |
450 | | - // Need to search through all hooks to find one with matching token |
451 | | - const hooksDir = path.join(basedir, 'hooks'); |
452 | | - const files = await listJSONFiles(hooksDir); |
453 | | - |
454 | | - for (const file of files) { |
455 | | - const hookPath = path.join(hooksDir, `${file}.json`); |
456 | | - const hook = await readJSON(hookPath, HookSchema); |
457 | | - if (hook && hook.token === token) { |
458 | | - return hook; |
459 | | - } |
460 | | - } |
461 | | - |
462 | | - throw new Error(`Hook with token ${token} not found`); |
463 | | - }, |
464 | | - |
465 | | - async list(params) { |
466 | | - const hooksDir = path.join(basedir, 'hooks'); |
467 | | - const resolveData = params.resolveData || DEFAULT_RESOLVE_DATA_OPTION; |
468 | | - |
469 | | - const result = await paginatedFileSystemQuery({ |
470 | | - directory: hooksDir, |
471 | | - schema: HookSchema, |
472 | | - sortOrder: params.pagination?.sortOrder, |
473 | | - limit: params.pagination?.limit, |
474 | | - cursor: params.pagination?.cursor, |
475 | | - filePrefix: undefined, // Hooks don't have ULIDs, so we can't optimize by filename |
476 | | - filter: (hook) => { |
477 | | - // Filter by runId if provided |
478 | | - if (params.runId && hook.runId !== params.runId) { |
479 | | - return false; |
480 | | - } |
481 | | - return true; |
482 | | - }, |
483 | | - getCreatedAt: () => { |
484 | | - // Hook files don't have ULID timestamps in filename |
485 | | - // We need to read the file to get createdAt, but that's inefficient |
486 | | - // So we return the hook's createdAt directly (item.createdAt will be used for sorting) |
487 | | - // Return a dummy date to pass the null check, actual sorting uses item.createdAt |
488 | | - return new Date(0); |
489 | | - }, |
490 | | - getId: (hook) => hook.hookId, |
491 | | - }); |
492 | | - |
493 | | - // Transform the data after pagination |
494 | | - return { |
495 | | - ...result, |
496 | | - data: result.data.map((hook) => filterHookData(hook, resolveData)), |
497 | | - }; |
498 | | - }, |
499 | | - |
500 | | - async dispose(hookId) { |
501 | | - const hookPath = path.join(basedir, 'hooks', `${hookId}.json`); |
502 | | - const hook = await readJSON(hookPath, HookSchema); |
503 | | - if (!hook) { |
504 | | - throw new Error(`Hook ${hookId} not found`); |
505 | | - } |
506 | | - await deleteJSON(hookPath); |
507 | | - return hook; |
508 | | - }, |
509 | | - }, |
| 539 | + hooks: createHooksStorage(basedir), |
510 | 540 | }; |
511 | 541 | } |
0 commit comments