mirror of
https://github.com/jackwener/OpenCLI.git
synced 2026-09-14 18:25:42 +08:00
4fe9a73ebc
* refactor: migrate adapter imports to package exports Replace all relative imports (../../src/registry.js, ../../browser/cdp.js, etc.) with package exports (@jackwener/opencli/registry, @jackwener/opencli/errors, etc.) across all 484 adapter files. This decouples adapter import resolution from directory structure: - User CLIs in ~/.opencli/clis/ resolve via node_modules symlink - Internal adapters resolve via Node.js self-referencing - No more shim files needed for import resolution Changes: - package.json: add sub-path exports for all public modules - clis/**: replace relative imports with @jackwener/opencli/... - discovery.ts: simplify ensureUserCliCompatShims to symlink-only - registry-api.ts: export CommandArgs type - Remove root-level shim directories (browser/, download/, pipeline/) - Remove shim entries from tsconfig.json include and package.json files * test: add regression tests for package exports Prevents regressions like #788/#791 by: 1. Scanning all adapter files for forbidden relative imports (../../src/, ../../browser/, etc.) — fails if any remain 2. Verifying every package.json export maps to an existing source file 18 new test cases. * fix: use junction on Windows + broaden test patterns - discovery.ts: use 'junction' symlink type on Windows (no admin required) - package-exports.test.ts: generalize forbidden patterns to catch any depth of ../ traversal (not just ../../ and ../../../) * fix: update stale vi.mock/importActual paths in adapter tests Test files still used old relative paths for vi.mock() and vi.importActual() calls. Updated 5 test files to use package exports. Also broadened regression test patterns to catch mock/importActual paths. * fix: use rm instead of unlink for symlink cleanup, add warn on failure Addresses review feedback from Astro-Han: - rm() handles both symlinks and stale directories (unlink fails on dirs) - Log a warning when symlink creation fails instead of silent catch * docs: update import examples to use package exports Update all documentation, contributing guides, and skills to use @jackwener/opencli/registry instead of ../../src/registry.js. Without this, users following the docs would write adapters with broken imports since the old shim files are no longer created.
79 lines
2.4 KiB
TypeScript
79 lines
2.4 KiB
TypeScript
/**
|
|
* Transcode poller for Douyin video processing.
|
|
*
|
|
* After a video is uploaded via TOS and the "confirm upload" API is called,
|
|
* Douyin transcodes the video asynchronously. This module polls the transcode
|
|
* status endpoint until encode=2 (complete) or a timeout is reached.
|
|
*/
|
|
|
|
import type { IPage } from '@jackwener/opencli/types';
|
|
import { TimeoutError } from '@jackwener/opencli/errors';
|
|
import { browserFetch } from './browser-fetch.js';
|
|
import type { TranscodeResult } from './types.js';
|
|
|
|
const POLL_INTERVAL_MS = 3_000;
|
|
const DEFAULT_TIMEOUT_MS = 300_000;
|
|
|
|
const TRANSCODE_URL_BASE =
|
|
'https://creator.douyin.com/web/api/media/video/transend/';
|
|
|
|
type BrowserFetchFn = (
|
|
page: IPage,
|
|
method: 'GET' | 'POST',
|
|
url: string,
|
|
) => Promise<unknown>;
|
|
|
|
/**
|
|
* Lower-level poll function that accepts an injected fetch function.
|
|
* Exported for testability.
|
|
*/
|
|
export async function pollTranscodeWithFetch(
|
|
fetchFn: BrowserFetchFn,
|
|
page: IPage,
|
|
videoId: string,
|
|
timeoutMs = DEFAULT_TIMEOUT_MS,
|
|
): Promise<TranscodeResult> {
|
|
const url = `${TRANSCODE_URL_BASE}?video_id=${encodeURIComponent(videoId)}&aid=1128`;
|
|
const deadline = Date.now() + timeoutMs;
|
|
|
|
while (Date.now() < deadline) {
|
|
const result = (await fetchFn(page, 'GET', url)) as {
|
|
encode: number;
|
|
} & TranscodeResult;
|
|
|
|
if (result.encode === 2) {
|
|
return result;
|
|
}
|
|
|
|
// Wait before next poll, but don't exceed the deadline
|
|
const remaining = deadline - Date.now();
|
|
if (remaining <= 0) break;
|
|
await new Promise<void>(resolve =>
|
|
setTimeout(resolve, Math.min(POLL_INTERVAL_MS, remaining)),
|
|
);
|
|
}
|
|
|
|
throw new TimeoutError(
|
|
`Douyin transcode for video ${videoId}`,
|
|
Math.round(timeoutMs / 1000),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Poll Douyin's transcode status endpoint until the video is fully transcoded
|
|
* (encode=2) or the timeout expires.
|
|
*
|
|
* @param page - Browser page for making credentialed API calls
|
|
* @param videoId - The video_id returned from the confirm upload step
|
|
* @param timeoutMs - Maximum wait time in ms (default: 300 000 = 5 minutes)
|
|
* @returns TranscodeResult including duration, fps, dimensions, and poster info
|
|
* @throws TimeoutError if transcode does not complete within timeoutMs
|
|
*/
|
|
export async function pollTranscode(
|
|
page: IPage,
|
|
videoId: string,
|
|
timeoutMs?: number,
|
|
): Promise<TranscodeResult> {
|
|
return pollTranscodeWithFetch(browserFetch, page, videoId, timeoutMs);
|
|
}
|