feat: manually mirror opencoze's code from bytedance
Change-Id: I09a73aadda978ad9511264a756b2ce51f5761adf
This commit is contained in:
61
frontend/packages/arch/load-remote-worker/README.md
Normal file
61
frontend/packages/arch/load-remote-worker/README.md
Normal file
@@ -0,0 +1,61 @@
|
||||
# @coze-arch/load-remote-worker
|
||||
|
||||
load remote worker inspire by https://github.com/jantimon/remote-web-worker/
|
||||
|
||||
## Overview
|
||||
|
||||
This package is part of the Coze Studio monorepo and provides architecture functionality. It includes api.
|
||||
|
||||
## Getting Started
|
||||
|
||||
### Installation
|
||||
|
||||
Add this package to your `package.json`:
|
||||
|
||||
```json
|
||||
{
|
||||
"dependencies": {
|
||||
"@coze-arch/load-remote-worker": "workspace:*"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Then run:
|
||||
|
||||
```bash
|
||||
rush update
|
||||
```
|
||||
|
||||
### Usage
|
||||
|
||||
```typescript
|
||||
import { /* exported functions/components */ } from '@coze-arch/load-remote-worker';
|
||||
|
||||
// Example usage
|
||||
// TODO: Add specific usage examples
|
||||
```
|
||||
|
||||
## Features
|
||||
|
||||
- Api
|
||||
|
||||
## API Reference
|
||||
|
||||
Please refer to the TypeScript definitions for detailed API documentation.
|
||||
|
||||
## 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,114 @@
|
||||
/*
|
||||
* 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 { RemoteWebWorker, register } from '../src/index';
|
||||
|
||||
// 获取模拟函数
|
||||
const mockCreateObjectURL = vi.mocked(URL.createObjectURL);
|
||||
|
||||
describe('RemoteWebWorker', () => {
|
||||
beforeEach(() => {
|
||||
// 清除模拟调用记录
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
describe('constructor', () => {
|
||||
it('应该直接使用本地 URL', () => {
|
||||
const localUrl = 'worker.js';
|
||||
const options = { type: 'module' };
|
||||
|
||||
new RemoteWebWorker(localUrl, options);
|
||||
|
||||
expect(mockCreateObjectURL).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('应该直接使用同源的远程 URL', () => {
|
||||
const sameOriginUrl = 'https://example.com/worker.js';
|
||||
const options = { type: 'module' };
|
||||
|
||||
new RemoteWebWorker(sameOriginUrl, options);
|
||||
|
||||
expect(mockCreateObjectURL).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('应该直接使用 blob URL', () => {
|
||||
const blobUrl = 'blob:https://example.com/worker.js';
|
||||
const options = { type: 'module' };
|
||||
|
||||
new RemoteWebWorker(blobUrl, options);
|
||||
|
||||
expect(mockCreateObjectURL).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('应该为跨域 URL 创建 Blob 并使用 URL.createObjectURL', () => {
|
||||
const crossOriginUrl = 'https://other-domain.com/worker.js';
|
||||
const options = { type: 'module' };
|
||||
|
||||
new RemoteWebWorker(crossOriginUrl, options);
|
||||
|
||||
expect(mockCreateObjectURL).toHaveBeenCalledTimes(1);
|
||||
|
||||
// 验证创建的 Blob 内容
|
||||
const blobArg = mockCreateObjectURL.mock.calls[0][0];
|
||||
expect(blobArg).toBeInstanceOf(Blob);
|
||||
// 由于 Blob 的内容无法直接访问,我们只能验证它被创建了
|
||||
});
|
||||
|
||||
it('应该处理非字符串 URL', () => {
|
||||
const nonStringUrl = {
|
||||
toString: () => 'https://other-domain.com/worker.js',
|
||||
};
|
||||
const options = { type: 'module' };
|
||||
|
||||
new RemoteWebWorker(nonStringUrl as any, options);
|
||||
|
||||
expect(mockCreateObjectURL).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('register', () => {
|
||||
it('应该将全局 Worker 替换为 RemoteWebWorker', () => {
|
||||
// 创建一个模拟的全局对象
|
||||
const mockGlobal = {
|
||||
worker() {
|
||||
/* 空函数 */
|
||||
},
|
||||
};
|
||||
|
||||
// 将 worker 属性重命名为 Worker,以便测试 register 函数
|
||||
Object.defineProperty(mockGlobal, 'Worker', {
|
||||
get() {
|
||||
return this.worker;
|
||||
},
|
||||
set(value) {
|
||||
this.worker = value;
|
||||
},
|
||||
enumerable: true,
|
||||
configurable: true,
|
||||
});
|
||||
|
||||
register(mockGlobal as any);
|
||||
|
||||
expect(mockGlobal.worker).toBe(RemoteWebWorker);
|
||||
});
|
||||
|
||||
it('当全局对象未定义时不应该抛出错误', () => {
|
||||
expect(() => register(undefined as any)).not.toThrow();
|
||||
});
|
||||
});
|
||||
54
frontend/packages/arch/load-remote-worker/__tests__/setup.ts
Normal file
54
frontend/packages/arch/load-remote-worker/__tests__/setup.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* 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 { vi } from 'vitest';
|
||||
|
||||
// 定义一个模拟的 Worker 类
|
||||
class MockWorker {
|
||||
constructor(
|
||||
public scriptURL: string,
|
||||
public options: any,
|
||||
) {}
|
||||
|
||||
// 添加 Worker 接口所需的方法
|
||||
terminate(): void {
|
||||
// 空实现
|
||||
}
|
||||
|
||||
postMessage(): void {
|
||||
// 空实现
|
||||
}
|
||||
|
||||
onmessage = null;
|
||||
onmessageerror = null;
|
||||
}
|
||||
|
||||
// 全局模拟
|
||||
global.Worker = MockWorker as any;
|
||||
global.URL = {
|
||||
createObjectURL: vi.fn().mockReturnValue('blob:mocked-object-url'),
|
||||
} as any;
|
||||
|
||||
global.Blob = class MockBlob {
|
||||
constructor(
|
||||
public array: any[],
|
||||
public options: any,
|
||||
) {}
|
||||
} as any;
|
||||
|
||||
global.location = {
|
||||
origin: 'https://example.com',
|
||||
} as any;
|
||||
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"operationSettings": [
|
||||
{
|
||||
"operationName": "test:cov",
|
||||
"outputFolderNames": ["coverage"]
|
||||
},
|
||||
{
|
||||
"operationName": "ts-check",
|
||||
"outputFolderNames": ["./dist"]
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"codecov": {
|
||||
"incrementCoverage": 90
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
const { defineConfig } = require('@coze-arch/eslint-config');
|
||||
|
||||
module.exports = defineConfig({
|
||||
packageRoot: __dirname,
|
||||
preset: 'web',
|
||||
rules: {},
|
||||
});
|
||||
25
frontend/packages/arch/load-remote-worker/package.json
Normal file
25
frontend/packages/arch/load-remote-worker/package.json
Normal file
@@ -0,0 +1,25 @@
|
||||
{
|
||||
"name": "@coze-arch/load-remote-worker",
|
||||
"version": "0.0.1",
|
||||
"description": "load remote worker inspire by https://github.com/jantimon/remote-web-worker/",
|
||||
"license": "Apache-2.0",
|
||||
"author": "fanwenjie.fe@bytedance.com",
|
||||
"maintainers": [],
|
||||
"main": "src/index.ts",
|
||||
"scripts": {
|
||||
"build": "exit 0",
|
||||
"lint": "eslint ./ --cache",
|
||||
"test": "vitest --run --passWithNoTests",
|
||||
"test:cov": "npm run test -- --coverage"
|
||||
},
|
||||
"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"
|
||||
}
|
||||
}
|
||||
|
||||
65
frontend/packages/arch/load-remote-worker/src/index.ts
Normal file
65
frontend/packages/arch/load-remote-worker/src/index.ts
Normal file
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
// Inspire by https://github.com/jantimon/remote-web-worker
|
||||
// Patch Worker to allow loading scripts from remote URLs
|
||||
//
|
||||
// It's a workaround for the fact that the Worker constructor
|
||||
// accepts only local URLs, not remote URLs:
|
||||
// https://developer.mozilla.org/en-US/docs/Web/API/Worker/Worker
|
||||
//
|
||||
// As a workaround this patched Worker constructor will
|
||||
// use `importScripts` to load the remote script.
|
||||
// https://developer.mozilla.org/en-US/docs/Web/API/WorkerGlobalScope/importScripts
|
||||
//
|
||||
// Compatibility: Chrome 4+, Firefox 4+, Safari 4+
|
||||
|
||||
export class RemoteWebWorker extends Worker {
|
||||
constructor(scriptURL, options) {
|
||||
const url = String(scriptURL);
|
||||
const remoteWorkerUrl =
|
||||
url.includes('://') &&
|
||||
!url.startsWith(location.origin) &&
|
||||
// 适配 @byted/uploader 等底层库的worker 调用
|
||||
!url.startsWith('blob:')
|
||||
? URL.createObjectURL(
|
||||
new Blob(
|
||||
[
|
||||
`importScripts=((i)=>(...a)=>i(...a.map((u)=>''+new URL(u,"${url}"))))(importScripts);importScripts("${url}")`,
|
||||
],
|
||||
{
|
||||
type: 'text/javascript',
|
||||
},
|
||||
),
|
||||
)
|
||||
: scriptURL;
|
||||
|
||||
super(remoteWorkerUrl, options);
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: 这种实现很脏,会篡改全局实例容易引发意外,但短期内为了向后兼容,暂时保留,后续需要:
|
||||
// 1. 将业务代码中的 worker 调用切换为 RemoteWebWorker 调用
|
||||
// 2. 这个 package 本身需要增加 ut
|
||||
// 3. 增加 lint 规则,不允许直接调用 Worker,统一使用 RemoteWebWorker 版本
|
||||
/**
|
||||
* @deprecated Do not use this function!!!
|
||||
*/
|
||||
export const register = (global: typeof globalThis) => {
|
||||
if (typeof global !== 'undefined') {
|
||||
global.Worker = RemoteWebWorker;
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,22 @@
|
||||
{
|
||||
"$schema": "https://json.schemastore.org/tsconfig",
|
||||
"extends": "@coze-arch/ts-config/tsconfig.web.json",
|
||||
"compilerOptions": {
|
||||
"types": [],
|
||||
"rootDir": "./src",
|
||||
"outDir": "./dist",
|
||||
"tsBuildInfoFile": "dist/tsconfig.build.tsbuildinfo"
|
||||
},
|
||||
"include": ["src"],
|
||||
"references": [
|
||||
{
|
||||
"path": "../../../config/eslint-config/tsconfig.build.json"
|
||||
},
|
||||
{
|
||||
"path": "../../../config/ts-config/tsconfig.build.json"
|
||||
},
|
||||
{
|
||||
"path": "../../../config/vitest-config/tsconfig.build.json"
|
||||
}
|
||||
]
|
||||
}
|
||||
15
frontend/packages/arch/load-remote-worker/tsconfig.json
Normal file
15
frontend/packages/arch/load-remote-worker/tsconfig.json
Normal file
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"$schema": "https://json.schemastore.org/tsconfig",
|
||||
"compilerOptions": {
|
||||
"composite": true
|
||||
},
|
||||
"references": [
|
||||
{
|
||||
"path": "./tsconfig.build.json"
|
||||
},
|
||||
{
|
||||
"path": "./tsconfig.misc.json"
|
||||
}
|
||||
],
|
||||
"exclude": ["**/*"]
|
||||
}
|
||||
16
frontend/packages/arch/load-remote-worker/tsconfig.misc.json
Normal file
16
frontend/packages/arch/load-remote-worker/tsconfig.misc.json
Normal file
@@ -0,0 +1,16 @@
|
||||
{
|
||||
"extends": "@coze-arch/ts-config/tsconfig.web.json",
|
||||
"$schema": "https://json.schemastore.org/tsconfig",
|
||||
"include": ["__tests__", "vitest.config.ts"],
|
||||
"exclude": ["./dist"],
|
||||
"references": [
|
||||
{
|
||||
"path": "./tsconfig.build.json"
|
||||
}
|
||||
],
|
||||
"compilerOptions": {
|
||||
"rootDir": "./",
|
||||
"outDir": "./dist",
|
||||
"types": ["vitest/globals"]
|
||||
}
|
||||
}
|
||||
30
frontend/packages/arch/load-remote-worker/vitest.config.ts
Normal file
30
frontend/packages/arch/load-remote-worker/vitest.config.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
* 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 { mergeConfig } from 'vitest/config';
|
||||
import { defineConfig } from '@coze-arch/vitest-config';
|
||||
|
||||
export default mergeConfig(
|
||||
defineConfig({
|
||||
dirname: __dirname,
|
||||
preset: 'web',
|
||||
}),
|
||||
{
|
||||
test: {
|
||||
setupFiles: ['./__tests__/setup.ts'],
|
||||
},
|
||||
},
|
||||
);
|
||||
Reference in New Issue
Block a user