feat: manually mirror opencoze's code from bytedance

Change-Id: I09a73aadda978ad9511264a756b2ce51f5761adf
This commit is contained in:
fanlv
2025-07-20 17:36:12 +08:00
commit 890153324f
14811 changed files with 1923430 additions and 0 deletions

View File

@@ -0,0 +1,55 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed 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.
*/
import pkg from '../package.json';
type AssetsType = 'cmaps' | 'pdf.worker';
// 这里需要写 bnpm 已经发布的版本
//
const DEFAULT_VERSION = '0.1.0-alpha.x6e892414ec';
/**
* 该方法用于生产 unpkg 环境的 worker & cmaps 链接,注意并非 pdfjs 原生方法
*/
export const generatePdfAssetsUrl = (assets: AssetsType) => {
const { name } = pkg;
let assetsUrl;
switch (assets) {
case 'cmaps': {
assetsUrl = 'lib/cmaps/';
break;
}
case 'pdf.worker': {
assetsUrl = 'lib/worker.js';
break;
}
default: {
throw new Error(
'目前只支持引用 cmaps 与 pdf.worker 文件,如需引用其他文件请联系 @fanwenjie.fe',
);
}
}
const onlinePkgName = name.replace(/^@/, '');
const domain =
REGION === 'cn'
? 'lf-cdn.coze.cn/obj/unpkg'
: 'sf-cdn.coze.com/obj/unpkg-va';
// cp-disable-next-line
return `//${domain}/${onlinePkgName}/${DEFAULT_VERSION}/${assetsUrl}`;
};

View File

@@ -0,0 +1,20 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed 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.
*/
/// <reference types='pdfjs-dist' />
declare module 'pdfjs-dist/build/pdf.worker.mjs';
declare module 'pdfjs-dist/build/pdf.worker.entry.js';
declare const REGION: 'cn' | 'sg' | 'va';

View File

@@ -0,0 +1,27 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed 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.
*/
export {
getDocument,
type PDFDocumentProxy,
type PDFPageProxy,
type PageViewport,
} from 'pdfjs-dist';
export { type TextContent } from 'pdfjs-dist/types/src/display/text_layer';
export { type TextItem } from 'pdfjs-dist/types/src/display/api';
export { generatePdfAssetsUrl } from './generate-assets';
export { initPdfJsWorker } from './init-pdfjs-dist';

View File

@@ -0,0 +1,28 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed 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.
*/
import { GlobalWorkerOptions } from 'pdfjs-dist';
import { generatePdfAssetsUrl } from './generate-assets';
/**
* 该方法用于初始化 pdfjs-dist 的 workerSrc 参数,可重复调用
*/
export const initPdfJsWorker = () => {
if (!GlobalWorkerOptions.workerSrc) {
GlobalWorkerOptions.workerSrc = generatePdfAssetsUrl('pdf.worker');
}
};