feat: manually mirror opencoze's code from bytedance
Change-Id: I09a73aadda978ad9511264a756b2ce51f5761adf
This commit is contained in:
69
frontend/packages/common/uploader-adapter/README.md
Normal file
69
frontend/packages/common/uploader-adapter/README.md
Normal file
@@ -0,0 +1,69 @@
|
||||
# @coze-studio/uploader-adapter
|
||||
|
||||
uploader adapter
|
||||
|
||||
## Overview
|
||||
|
||||
This package is part of the Coze Studio monorepo and provides utilities functionality. It serves as a core component in the Coze ecosystem.
|
||||
|
||||
## Getting Started
|
||||
|
||||
### Installation
|
||||
|
||||
Add this package to your `package.json`:
|
||||
|
||||
```json
|
||||
{
|
||||
"dependencies": {
|
||||
"@coze-studio/uploader-adapter": "workspace:*"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Then run:
|
||||
|
||||
```bash
|
||||
rush update
|
||||
```
|
||||
|
||||
### Usage
|
||||
|
||||
```typescript
|
||||
import { /* exported functions/components */ } from '@coze-studio/uploader-adapter';
|
||||
|
||||
// Example usage
|
||||
// TODO: Add specific usage examples
|
||||
```
|
||||
|
||||
## Features
|
||||
|
||||
- Core functionality for Coze Studio
|
||||
- TypeScript support
|
||||
- Modern ES modules
|
||||
|
||||
## API Reference
|
||||
|
||||
### Exports
|
||||
|
||||
- `type Config,
|
||||
type EventPayloadMaps,`
|
||||
|
||||
|
||||
For detailed API documentation, please refer to the TypeScript definitions.
|
||||
|
||||
## Development
|
||||
|
||||
This package is built with:
|
||||
|
||||
- TypeScript
|
||||
- Modern JavaScript
|
||||
- Vitest for testing
|
||||
- ESLint for code quality
|
||||
|
||||
## Contributing
|
||||
|
||||
This package is part of the Coze Studio monorepo. Please follow the monorepo contribution guidelines.
|
||||
|
||||
## License
|
||||
|
||||
Apache-2.0
|
||||
@@ -0,0 +1,119 @@
|
||||
/*
|
||||
* 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 { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import type { Config, STSToken } from '@coze-arch/uploader-interface';
|
||||
|
||||
import { getUploader, type FileOption, type CozeUploader } from '../src/index';
|
||||
|
||||
// 在 vi.mock 工厂函数中定义所有 mock
|
||||
vi.mock('tt-uploader', () => {
|
||||
const mockAddImageFile = vi.fn().mockReturnValue('mock-key');
|
||||
const mockUploader = vi.fn().mockImplementation(() => ({
|
||||
addImageFile: mockAddImageFile,
|
||||
}));
|
||||
|
||||
// 将 mock 函数挂载到 global 对象上,以便测试用例访问
|
||||
(global as any).__mockAddImageFile = mockAddImageFile;
|
||||
(global as any).__mockUploader = mockUploader;
|
||||
|
||||
return {
|
||||
__esModule: true,
|
||||
default: mockUploader,
|
||||
};
|
||||
});
|
||||
|
||||
// 从 global 对象获取 mock 函数
|
||||
const mockAddImageFile = (global as any).__mockAddImageFile;
|
||||
const mockUploader = (global as any).__mockUploader;
|
||||
|
||||
describe('getUploader', () => {
|
||||
let config: Config;
|
||||
let stsToken: STSToken;
|
||||
let file: Blob;
|
||||
|
||||
beforeEach(() => {
|
||||
config = {
|
||||
userId: 'user1',
|
||||
appId: 123,
|
||||
imageHost: 'https://img.example.com',
|
||||
};
|
||||
stsToken = {
|
||||
AccessKeyId: 'ak',
|
||||
SecretAccessKey: 'sk',
|
||||
SessionToken: 'token',
|
||||
ExpiredTime: '2024-01-01T00:00:00Z',
|
||||
CurrentTime: '2023-01-01T00:00:00Z',
|
||||
};
|
||||
file = new Blob(['test'], { type: 'text/plain' });
|
||||
mockAddImageFile.mockClear();
|
||||
mockUploader.mockClear();
|
||||
});
|
||||
|
||||
it('should create uploader with correct config (domestic)', () => {
|
||||
getUploader(config);
|
||||
expect(mockUploader).toHaveBeenCalledWith({
|
||||
region: 'cn-north-1',
|
||||
imageHost: 'img.example.com',
|
||||
appId: 123,
|
||||
userId: 'user1',
|
||||
useFileExtension: undefined,
|
||||
uploadTimeout: undefined,
|
||||
imageConfig: undefined,
|
||||
});
|
||||
});
|
||||
|
||||
it('should create uploader with correct config (oversea)', () => {
|
||||
getUploader(config, true);
|
||||
expect(mockUploader).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ region: 'ap-singapore-1' }),
|
||||
);
|
||||
});
|
||||
|
||||
it('addFile should call addImageFile with correct params', () => {
|
||||
const uploader = getUploader(config) as CozeUploader;
|
||||
const fileOption: FileOption = { file, stsToken };
|
||||
const key = uploader.addFile(fileOption);
|
||||
expect(mockAddImageFile).toHaveBeenCalledWith({ file, stsToken });
|
||||
expect(key).toBe('mock-key');
|
||||
});
|
||||
|
||||
it('should strip https:// from imageHost', () => {
|
||||
config.imageHost = 'https://img2.example.com';
|
||||
getUploader(config);
|
||||
expect(mockUploader).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ imageHost: 'img2.example.com' }),
|
||||
);
|
||||
});
|
||||
|
||||
it('should fallback to imageFallbackHost if imageHost is missing', () => {
|
||||
config.imageHost = undefined;
|
||||
config.imageFallbackHost = 'https://fallback.example.com';
|
||||
getUploader(config);
|
||||
expect(mockUploader).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ imageHost: 'fallback.example.com' }),
|
||||
);
|
||||
});
|
||||
|
||||
it('should use empty string if no imageHost or fallback', () => {
|
||||
config.imageHost = undefined;
|
||||
config.imageFallbackHost = undefined;
|
||||
getUploader(config);
|
||||
expect(mockUploader).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ imageHost: '' }),
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"operationSettings": [
|
||||
{
|
||||
"operationName": "test:cov",
|
||||
"outputFolderNames": ["coverage"]
|
||||
},
|
||||
{
|
||||
"operationName": "ts-check",
|
||||
"outputFolderNames": ["dist"]
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
const { defineConfig } = require('@coze-arch/eslint-config');
|
||||
|
||||
module.exports = defineConfig({
|
||||
packageRoot: __dirname,
|
||||
preset: 'node',
|
||||
rules: {
|
||||
'@typescript-eslint/no-explicit-any': 'off',
|
||||
},
|
||||
});
|
||||
29
frontend/packages/common/uploader-adapter/package.json
Normal file
29
frontend/packages/common/uploader-adapter/package.json
Normal file
@@ -0,0 +1,29 @@
|
||||
{
|
||||
"name": "@coze-studio/uploader-adapter",
|
||||
"version": "0.0.1",
|
||||
"description": "uploader adapter",
|
||||
"license": "Apache-2.0",
|
||||
"author": "chenjiawei.inizio@bytedance.com",
|
||||
"maintainers": [],
|
||||
"main": "src/index.ts",
|
||||
"scripts": {
|
||||
"build": "exit 0",
|
||||
"lint": "eslint ./ --cache",
|
||||
"test": "vitest --run --passWithNoTests",
|
||||
"test:cov": "npm run test -- --coverage"
|
||||
},
|
||||
"dependencies": {
|
||||
"@coze-arch/uploader-interface": "workspace:*",
|
||||
"tt-uploader": "1.5.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@coze-arch/eslint-config": "workspace:*",
|
||||
"@coze-arch/ts-config": "workspace:*",
|
||||
"@coze-arch/vitest-config": "workspace:*",
|
||||
"@types/node": "^18",
|
||||
"@vitest/coverage-v8": "~3.0.5",
|
||||
"sucrase": "^3.32.0",
|
||||
"vitest": "~3.0.5"
|
||||
}
|
||||
}
|
||||
|
||||
77
frontend/packages/common/uploader-adapter/src/index.ts
Normal file
77
frontend/packages/common/uploader-adapter/src/index.ts
Normal file
@@ -0,0 +1,77 @@
|
||||
/*
|
||||
* 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 Uploader, { type ImageXFileOption } from 'tt-uploader';
|
||||
import {
|
||||
type Config,
|
||||
type STSToken,
|
||||
type ObjectSync,
|
||||
} from '@coze-arch/uploader-interface';
|
||||
|
||||
export interface FileOption {
|
||||
file: Blob;
|
||||
stsToken: STSToken;
|
||||
type?: any;
|
||||
callbackArgs?: string;
|
||||
testHost?: string;
|
||||
objectSync?: ObjectSync;
|
||||
}
|
||||
|
||||
export const getUploader = (config: Config, isOversea?: boolean) => {
|
||||
const imageHost = (
|
||||
config.imageHost ||
|
||||
config.imageFallbackHost ||
|
||||
''
|
||||
).replace(/^https:\/\//, config.schema ? `${config.schema}://` : '');
|
||||
const uploader = new Uploader({
|
||||
/**
|
||||
* 需要根据当前用户的部署环境动态获取schema
|
||||
* schema 兼容特殊 http 场景字段
|
||||
*/
|
||||
schema: config.schema,
|
||||
region: isOversea ? 'ap-singapore-1' : 'cn-north-1',
|
||||
imageHost,
|
||||
appId: config.appId,
|
||||
userId: config.userId,
|
||||
useFileExtension: config.useFileExtension,
|
||||
uploadTimeout: config.uploadTimeout,
|
||||
imageConfig: config.imageConfig,
|
||||
} as any);
|
||||
|
||||
const originalAddImageFile: (option: ImageXFileOption) => string =
|
||||
uploader.addImageFile.bind(uploader);
|
||||
|
||||
uploader.addFile = function (options: FileOption) {
|
||||
const imageOptions: ImageXFileOption = {
|
||||
file: options.file,
|
||||
stsToken: options.stsToken,
|
||||
};
|
||||
return originalAddImageFile(imageOptions);
|
||||
};
|
||||
return uploader as CozeUploader;
|
||||
};
|
||||
|
||||
type UploadEventName = 'complete' | 'error' | 'progress' | 'stream-progress';
|
||||
|
||||
export type CozeUploader = Uploader & {
|
||||
addFile: (options: FileOption) => string;
|
||||
removeAllListeners: (eventName: UploadEventName) => void;
|
||||
};
|
||||
|
||||
export {
|
||||
type Config,
|
||||
type EventPayloadMaps,
|
||||
} from '@coze-arch/uploader-interface';
|
||||
22
frontend/packages/common/uploader-adapter/src/utils.ts
Normal file
22
frontend/packages/common/uploader-adapter/src/utils.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
* 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 const REGION_MAP = {
|
||||
'cn-north-1': 'cn-north-1',
|
||||
'ap-singapore-1': 'ap-singapore-1',
|
||||
// volcengine 没有 va 环境
|
||||
'us-east-1': 'ap-singapore-1',
|
||||
};
|
||||
@@ -0,0 +1,28 @@
|
||||
{
|
||||
"extends": "@coze-arch/ts-config/tsconfig.node.json",
|
||||
"$schema": "https://json.schemastore.org/tsconfig",
|
||||
"compilerOptions": {
|
||||
"outDir": "dist",
|
||||
"rootDir": "src",
|
||||
"module": "CommonJS",
|
||||
"target": "ES2020",
|
||||
"moduleResolution": "node",
|
||||
"tsBuildInfoFile": "dist/tsconfig.build.tsbuildinfo"
|
||||
},
|
||||
"include": ["src"],
|
||||
"exclude": ["node_modules", "dist"],
|
||||
"references": [
|
||||
{
|
||||
"path": "../../../config/eslint-config/tsconfig.build.json"
|
||||
},
|
||||
{
|
||||
"path": "../../../config/ts-config/tsconfig.build.json"
|
||||
},
|
||||
{
|
||||
"path": "../../../config/vitest-config/tsconfig.build.json"
|
||||
},
|
||||
{
|
||||
"path": "../uploader-interface/tsconfig.build.json"
|
||||
}
|
||||
]
|
||||
}
|
||||
15
frontend/packages/common/uploader-adapter/tsconfig.json
Normal file
15
frontend/packages/common/uploader-adapter/tsconfig.json
Normal file
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"$schema": "https://json.schemastore.org/tsconfig",
|
||||
"exclude": ["**/*"],
|
||||
"compilerOptions": {
|
||||
"composite": true
|
||||
},
|
||||
"references": [
|
||||
{
|
||||
"path": "./tsconfig.build.json"
|
||||
},
|
||||
{
|
||||
"path": "./tsconfig.misc.json"
|
||||
}
|
||||
]
|
||||
}
|
||||
18
frontend/packages/common/uploader-adapter/tsconfig.misc.json
Normal file
18
frontend/packages/common/uploader-adapter/tsconfig.misc.json
Normal file
@@ -0,0 +1,18 @@
|
||||
{
|
||||
"extends": "@coze-arch/ts-config/tsconfig.node.json",
|
||||
"$schema": "https://json.schemastore.org/tsconfig",
|
||||
"compilerOptions": {
|
||||
"rootDir": "./",
|
||||
"outDir": "./dist",
|
||||
"module": "CommonJS",
|
||||
"target": "ES2020",
|
||||
"moduleResolution": "node"
|
||||
},
|
||||
"include": ["__tests__", "vitest.config.ts"],
|
||||
"exclude": ["./dist"],
|
||||
"references": [
|
||||
{
|
||||
"path": "./tsconfig.build.json"
|
||||
}
|
||||
]
|
||||
}
|
||||
22
frontend/packages/common/uploader-adapter/vitest.config.ts
Normal file
22
frontend/packages/common/uploader-adapter/vitest.config.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
* 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 { defineConfig } from '@coze-arch/vitest-config';
|
||||
|
||||
export default defineConfig({
|
||||
dirname: __dirname,
|
||||
preset: 'node',
|
||||
});
|
||||
Reference in New Issue
Block a user