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.
144 lines
5.0 KiB
TypeScript
144 lines
5.0 KiB
TypeScript
/**
|
|
* Bilibili shared helpers: WBI signing, authenticated fetch, nav data, UID resolution.
|
|
*/
|
|
|
|
import https from 'node:https';
|
|
import type { IPage } from '@jackwener/opencli/types';
|
|
import { AuthRequiredError, EmptyResultError } from '@jackwener/opencli/errors';
|
|
|
|
/**
|
|
* Resolve Bilibili short URL / short code to BV ID.
|
|
* Supports: BV1MV9NBtENN, XYzsqGa, b23.tv/XYzsqGa, https://b23.tv/XYzsqGa
|
|
*/
|
|
export function resolveBvid(input: unknown): Promise<string> {
|
|
const trimmed = String(input).trim();
|
|
if (/^BV[A-Za-z0-9]+$/i.test(trimmed)) {
|
|
return Promise.resolve(trimmed);
|
|
}
|
|
const shortCode = trimmed.replace(/^https?:\/\//, '').replace(/^(www\.)?b23\.tv\//, '');
|
|
const url = 'https://b23.tv/' + shortCode;
|
|
return new Promise((resolve, reject) => {
|
|
const req = https.get(url, (res) => {
|
|
const location = res.headers.location;
|
|
if (location) {
|
|
const match = location.match(/\/video\/(BV[A-Za-z0-9]+)/);
|
|
if (match) { res.resume(); resolve(match[1]); return; }
|
|
}
|
|
res.resume();
|
|
reject(new Error(`Cannot resolve BV ID from short URL: ${trimmed}`));
|
|
});
|
|
req.on('error', reject);
|
|
req.setTimeout(5000, () => { req.destroy(); reject(new Error(`Timeout resolving short URL: ${trimmed}`)); });
|
|
});
|
|
}
|
|
|
|
const MIXIN_KEY_ENC_TAB = [
|
|
46,47,18,2,53,8,23,32,15,50,10,31,58,3,45,35,27,43,5,49,
|
|
33,9,42,19,29,28,14,39,12,38,41,13,37,48,7,16,24,55,40,
|
|
61,26,17,0,1,60,51,30,4,22,25,54,21,56,59,6,63,57,62,11,
|
|
36,20,34,44,52,
|
|
];
|
|
|
|
export function stripHtml(s: string): string {
|
|
return s.replace(/<[^>]+>/g, '').replace(/&[a-z]+;/gi, ' ').trim();
|
|
}
|
|
|
|
export function payloadData(payload: any): any {
|
|
return payload?.data ?? payload;
|
|
}
|
|
|
|
async function getNavData(page: IPage): Promise<any> {
|
|
return page.evaluate(`
|
|
async () => {
|
|
const res = await fetch('https://api.bilibili.com/x/web-interface/nav', { credentials: 'include' });
|
|
return await res.json();
|
|
}
|
|
`);
|
|
}
|
|
|
|
async function getWbiKeys(page: IPage): Promise<{ imgKey: string; subKey: string }> {
|
|
const nav = await getNavData(page);
|
|
const wbiImg = nav?.data?.wbi_img ?? {};
|
|
const imgUrl = wbiImg.img_url ?? '';
|
|
const subUrl = wbiImg.sub_url ?? '';
|
|
const imgKey = imgUrl.split('/').pop()?.split('.')[0] ?? '';
|
|
const subKey = subUrl.split('/').pop()?.split('.')[0] ?? '';
|
|
return { imgKey, subKey };
|
|
}
|
|
|
|
function getMixinKey(imgKey: string, subKey: string): string {
|
|
const raw = imgKey + subKey;
|
|
return MIXIN_KEY_ENC_TAB.map(i => raw[i] || '').join('').slice(0, 32);
|
|
}
|
|
|
|
async function md5(text: string): Promise<string> {
|
|
const { createHash } = await import('node:crypto');
|
|
return createHash('md5').update(text).digest('hex');
|
|
}
|
|
|
|
export async function wbiSign(
|
|
page: IPage,
|
|
params: Record<string, any>,
|
|
): Promise<Record<string, string>> {
|
|
const { imgKey, subKey } = await getWbiKeys(page);
|
|
const mixinKey = getMixinKey(imgKey, subKey);
|
|
const wts = Math.floor(Date.now() / 1000);
|
|
const sorted: Record<string, string> = {};
|
|
const allParams: Record<string, any> = { ...params, wts: String(wts) };
|
|
for (const key of Object.keys(allParams).sort()) {
|
|
sorted[key] = String(allParams[key]).replace(/[!'()*]/g, '');
|
|
}
|
|
// Bilibili WBI verification expects %20 for spaces, not + (URLSearchParams default).
|
|
// Using + causes signature mismatch → CORS-blocked error response → TypeError: Failed to fetch.
|
|
const query = new URLSearchParams(sorted).toString().replace(/\+/g, '%20');
|
|
const wRid = await md5(query + mixinKey);
|
|
sorted.w_rid = wRid;
|
|
return sorted;
|
|
}
|
|
|
|
export async function apiGet(
|
|
page: IPage,
|
|
path: string,
|
|
opts: { params?: Record<string, any>; signed?: boolean } = {},
|
|
): Promise<any> {
|
|
const baseUrl = 'https://api.bilibili.com';
|
|
let params = opts.params ?? {};
|
|
if (opts.signed) {
|
|
params = await wbiSign(page, params);
|
|
}
|
|
const qs = new URLSearchParams(
|
|
Object.fromEntries(Object.entries(params).map(([k, v]) => [k, String(v)])),
|
|
).toString().replace(/\+/g, '%20');
|
|
const url = `${baseUrl}${path}?${qs}`;
|
|
return fetchJson(page, url);
|
|
}
|
|
|
|
export async function fetchJson(page: IPage, url: string): Promise<any> {
|
|
const urlJs = JSON.stringify(url);
|
|
return page.evaluate(`
|
|
async () => {
|
|
const res = await fetch(${urlJs}, { credentials: "include" });
|
|
return await res.json();
|
|
}
|
|
`);
|
|
}
|
|
|
|
export async function getSelfUid(page: IPage): Promise<string> {
|
|
const nav = await getNavData(page);
|
|
const mid = nav?.data?.mid;
|
|
if (!mid) throw new AuthRequiredError('bilibili.com');
|
|
return String(mid);
|
|
}
|
|
|
|
export async function resolveUid(page: IPage, input: string): Promise<string> {
|
|
if (/^\d+$/.test(input)) return input;
|
|
// Search for user by name
|
|
const payload = await apiGet(page, '/x/web-interface/wbi/search/type', {
|
|
params: { search_type: 'bili_user', keyword: input },
|
|
signed: true,
|
|
});
|
|
const results = payload?.data?.result ?? [];
|
|
if (results.length > 0) return String(results[0].mid);
|
|
throw new EmptyResultError(`bilibili user search: ${input}`, 'User may not exist or username may have changed.');
|
|
}
|