template-replacement 3.2.3 → 3.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/assets/template_replacement_core_wasm_bg.wasm +0 -0
- package/dist/assets/template_replacement_sign_core_wasm_bg.wasm +0 -0
- package/dist/{base-DIR-qpD-.js → base-ByF_YUTt.js} +94 -42
- package/dist/index-5nrkfdR0.js +61 -0
- package/dist/main/general.js +1460 -1226
- package/dist/main/sign.js +1988 -1748
- package/dist/replace/general.js +179 -153
- package/dist/replace/sign.js +287 -255
- package/package.json +8 -7
- package/replace/base.ts +82 -5
- package/replace/general.ts +10 -1
- package/replace/interface.ts +3 -0
- package/replace/paramsData.ts +1 -2
- package/replace/sign.ts +27 -6
- package/worker/child/agency.ts +37 -2
- package/worker/child/base.ts +27 -14
- package/worker/index.ts +4 -3
- package/worker/main/index.ts +38 -21
- package/dist/index-BkwrGCka.js +0 -61
package/package.json
CHANGED
|
@@ -1,27 +1,28 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "template-replacement",
|
|
3
3
|
"description": "模板文件替换",
|
|
4
|
-
"version": "3.
|
|
4
|
+
"version": "3.3.0",
|
|
5
5
|
"author": "fushiliang <994301536@qq.com>",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"main": "index.ts",
|
|
8
|
+
"license": "Apache 2.0",
|
|
8
9
|
"scripts": {
|
|
9
10
|
"build": "npx vite build --config vite.config.ts"
|
|
10
11
|
},
|
|
11
12
|
"dependencies": {
|
|
12
13
|
"@types/streamsaver": "^2.0.5",
|
|
13
|
-
"axios": "^1.
|
|
14
|
+
"axios": "^1.12.2",
|
|
14
15
|
"fflate": "^0.8.2",
|
|
15
16
|
"file-type": "^19.6.0",
|
|
16
17
|
"streamsaver": "^2.0.6",
|
|
17
|
-
"template-replacement-core-wasm": "^1.
|
|
18
|
-
"template-replacement-sign-core-wasm": "^1.
|
|
18
|
+
"template-replacement-core-wasm": "^1.4.0",
|
|
19
|
+
"template-replacement-sign-core-wasm": "^1.4.0",
|
|
19
20
|
"vite-plugin-wasm-pack": "^0.1.12"
|
|
20
21
|
},
|
|
21
22
|
"devDependencies": {
|
|
22
|
-
"terser": "^5.
|
|
23
|
-
"typescript": "^5.
|
|
24
|
-
"vite": "^6.3.
|
|
23
|
+
"terser": "^5.44.0",
|
|
24
|
+
"typescript": "^5.9.2",
|
|
25
|
+
"vite": "^6.3.6",
|
|
25
26
|
"vite-plugin-dts": "^4.5.4"
|
|
26
27
|
},
|
|
27
28
|
"keywords": [
|
package/replace/base.ts
CHANGED
|
@@ -52,14 +52,14 @@ export default class Base implements Interface{
|
|
|
52
52
|
let medias = await this.#core.extract_one_file_medias(buffer, file.isDecode)
|
|
53
53
|
data[file.name] = []
|
|
54
54
|
if (medias && Array.isArray(medias)) {
|
|
55
|
-
|
|
55
|
+
for (const m of medias) {
|
|
56
56
|
if (m.id && m.data) {
|
|
57
57
|
data[file.name].push({
|
|
58
58
|
id: m.id,
|
|
59
59
|
data: new Uint8Array(m.data)
|
|
60
60
|
})
|
|
61
61
|
}
|
|
62
|
-
}
|
|
62
|
+
}
|
|
63
63
|
}
|
|
64
64
|
}
|
|
65
65
|
resolve()
|
|
@@ -72,6 +72,9 @@ export default class Base implements Interface{
|
|
|
72
72
|
async handle(paramsData: paramsData, files: Uint8Array[], isDecode: boolean = false): Promise<Uint8Array[]> {
|
|
73
73
|
return []
|
|
74
74
|
}
|
|
75
|
+
async handleMultipleParams(paramsData: paramsData[], files: Uint8Array[], isDecode: boolean = false): Promise<Uint8Array[]> {
|
|
76
|
+
return []
|
|
77
|
+
}
|
|
75
78
|
|
|
76
79
|
async sign(data: any): Promise<string> {
|
|
77
80
|
return ""
|
|
@@ -133,14 +136,88 @@ export default class Base implements Interface{
|
|
|
133
136
|
return resData
|
|
134
137
|
}
|
|
135
138
|
const res = await this.handle(params, uint8Arrays, isDecode)
|
|
136
|
-
res.
|
|
139
|
+
for (let index = 0; index < res.length; index++) {
|
|
140
|
+
const file = res[index]
|
|
137
141
|
if (file.length) {
|
|
138
|
-
resData[names[
|
|
142
|
+
resData[names[index]] = file
|
|
139
143
|
}
|
|
140
|
-
}
|
|
144
|
+
}
|
|
141
145
|
return resData
|
|
142
146
|
}
|
|
143
147
|
|
|
148
|
+
async executeMultipleParams(paramsList: paramsData[], files: Temp[] | undefined): Promise<Record<string, Uint8Array>[]> {
|
|
149
|
+
if (!files) {
|
|
150
|
+
files = this.#files
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
//等待文件加载完成
|
|
154
|
+
const tasks = []
|
|
155
|
+
for (const file of files) {
|
|
156
|
+
tasks.push(file.getBuffer())
|
|
157
|
+
}
|
|
158
|
+
await Promise.all(tasks)
|
|
159
|
+
|
|
160
|
+
const fileMap: { decode: { names: string[], uint8Arrays: Uint8Array[] }, noDecode: { names: string[], uint8Arrays: Uint8Array[] }} = {
|
|
161
|
+
//需要解密的文件
|
|
162
|
+
decode: {
|
|
163
|
+
names: [],
|
|
164
|
+
uint8Arrays: [],
|
|
165
|
+
},
|
|
166
|
+
//不需要解密的文件
|
|
167
|
+
noDecode: {
|
|
168
|
+
names: [],
|
|
169
|
+
uint8Arrays: [],
|
|
170
|
+
}
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
//整理出需要解密和不需要解密的文件
|
|
174
|
+
for (const file of files) {
|
|
175
|
+
if (!file.uint8Array) {
|
|
176
|
+
continue;
|
|
177
|
+
}
|
|
178
|
+
if (file.isDecode) {
|
|
179
|
+
fileMap.decode.names.push(file.name)
|
|
180
|
+
fileMap.decode.uint8Arrays.push(file.uint8Array)
|
|
181
|
+
} else {
|
|
182
|
+
fileMap.noDecode.names.push(file.name)
|
|
183
|
+
fileMap.noDecode.uint8Arrays.push(file.uint8Array)
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
//分别处理需要解密和不需要解密的文件
|
|
187
|
+
const res = await Promise.all([
|
|
188
|
+
this._executeMultipleParams(paramsList, fileMap.noDecode.names, fileMap.noDecode.uint8Arrays, false),
|
|
189
|
+
this._executeMultipleParams(paramsList, fileMap.decode.names, fileMap.decode.uint8Arrays, true),
|
|
190
|
+
])
|
|
191
|
+
|
|
192
|
+
const result = []
|
|
193
|
+
for (let i = 0; i < res[0].length; i++) {
|
|
194
|
+
result.push({...res[0][i], ...res[1][i]})
|
|
195
|
+
}
|
|
196
|
+
return result
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
async _executeMultipleParams(paramsList: paramsData[], names: string[], uint8Arrays: Uint8Array[], isDecode: boolean = false): Promise<Record<string, Uint8Array>[]> {
|
|
201
|
+
const result: Record<string, Uint8Array>[] = Array(paramsList.length) //整理出每一套参数的替换结果文件
|
|
202
|
+
if (!uint8Arrays.length) {
|
|
203
|
+
return result
|
|
204
|
+
}
|
|
205
|
+
const resFileList = await this.handleMultipleParams(paramsList, uint8Arrays, isDecode)
|
|
206
|
+
|
|
207
|
+
let resFileIndex = 0
|
|
208
|
+
for (let index = 0; index < paramsList.length; index++) {
|
|
209
|
+
const resultItem: Record<string, Uint8Array> = {}
|
|
210
|
+
for (const name of names) {
|
|
211
|
+
const file = resFileList[resFileIndex++]
|
|
212
|
+
if (file.length) {
|
|
213
|
+
resultItem[name] = file
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
result[index] = resultItem
|
|
217
|
+
}
|
|
218
|
+
return result
|
|
219
|
+
}
|
|
220
|
+
|
|
144
221
|
async fileEncrypt(file: Uint8Array): Promise<Uint8Array> {
|
|
145
222
|
return await this.#core.file_encrypt(file)
|
|
146
223
|
}
|
package/replace/general.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import Base from './base';
|
|
2
|
-
import core, { replace_batch } from '../core/general'
|
|
2
|
+
import core, { replace_batch, replace_batch_multiple_params } from '../core/general'
|
|
3
3
|
import paramsData from './paramsData';
|
|
4
4
|
|
|
5
5
|
export default class General extends Base {
|
|
@@ -11,4 +11,13 @@ export default class General extends Base {
|
|
|
11
11
|
const [params, mediaBuffers] = await paramsData.toReplaceParams()
|
|
12
12
|
return replace_batch(params, mediaBuffers, files, isDecode)
|
|
13
13
|
}
|
|
14
|
+
|
|
15
|
+
async handleMultipleParams(paramsList: paramsData[], files: Uint8Array[], isDecode: boolean = false): Promise<Uint8Array[]> {
|
|
16
|
+
let params, mediaBuffers: Uint8Array[] = [], newParamsList: any[] = []
|
|
17
|
+
for (const paramsData of paramsList) {
|
|
18
|
+
[params, mediaBuffers] = await paramsData.toReplaceParams(mediaBuffers)
|
|
19
|
+
newParamsList.push(params)
|
|
20
|
+
}
|
|
21
|
+
return replace_batch_multiple_params(newParamsList, mediaBuffers, files, isDecode)
|
|
22
|
+
}
|
|
14
23
|
}
|
package/replace/interface.ts
CHANGED
|
@@ -26,6 +26,9 @@ export default interface ReplaceInterface {
|
|
|
26
26
|
//执行替换任务
|
|
27
27
|
execute(params: paramsData, files: Temp[] | undefined): Promise<Record<string, Uint8Array>>
|
|
28
28
|
|
|
29
|
+
//执行替换任务(多套参数)
|
|
30
|
+
executeMultipleParams(params: paramsData[], files: Temp[] | undefined): Promise<Record<string, Uint8Array>[]>
|
|
31
|
+
|
|
29
32
|
//文件加密
|
|
30
33
|
fileEncrypt(file: Uint8Array): Promise<Uint8Array>
|
|
31
34
|
|
package/replace/paramsData.ts
CHANGED
|
@@ -36,11 +36,10 @@ export default class paramsData {
|
|
|
36
36
|
}
|
|
37
37
|
|
|
38
38
|
//转为替换参数
|
|
39
|
-
async toReplaceParams(): Promise<[replaceParams, Uint8Array[]]> {
|
|
39
|
+
async toReplaceParams(mediaBuffers: Uint8Array[] = []): Promise<[replaceParams, Uint8Array[]]> {
|
|
40
40
|
const text: Record<string, value> = {}
|
|
41
41
|
const media: Record<string, value> = {}
|
|
42
42
|
const tasks = []
|
|
43
|
-
const mediaBuffers: Uint8Array[] = []
|
|
44
43
|
for (const key in this.textData) {
|
|
45
44
|
tasks.push(new Promise<void>(async resolve => {
|
|
46
45
|
const value = this.textData[key]
|
package/replace/sign.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import Base from './base';
|
|
2
|
-
import core, { add_template, add_media, replace_batch_verify, replace_params_encode } from '../core/sign'
|
|
2
|
+
import core, { add_template, add_media, replace_batch_verify, replace_params_encode, replace_batch_verify_multiple_params, replace_params_encode_multiple_params } from '../core/sign'
|
|
3
3
|
import paramsData from './paramsData';
|
|
4
4
|
|
|
5
5
|
export default class Sign extends Base {
|
|
@@ -12,11 +12,9 @@ export default class Sign extends Base {
|
|
|
12
12
|
const [params] = await paramsData.toReplaceParams()
|
|
13
13
|
|
|
14
14
|
const addFileTasks: Promise<number>[] = []
|
|
15
|
-
|
|
16
|
-
addFileTasks.push(
|
|
17
|
-
|
|
18
|
-
}))
|
|
19
|
-
})
|
|
15
|
+
for (const file of files) {
|
|
16
|
+
addFileTasks.push(add_template(file, isDecode))
|
|
17
|
+
}
|
|
20
18
|
|
|
21
19
|
const encodeData = {
|
|
22
20
|
files: await Promise.all(addFileTasks),
|
|
@@ -28,4 +26,27 @@ export default class Sign extends Base {
|
|
|
28
26
|
const verifyCode = await this.sign(paramsEncode)
|
|
29
27
|
return replace_batch_verify(String(verifyCode), paramsEncode.data)
|
|
30
28
|
}
|
|
29
|
+
|
|
30
|
+
async handleMultipleParams(paramsList: paramsData[], files: Uint8Array[], isDecode: boolean = false): Promise<Uint8Array[]> {
|
|
31
|
+
let variables = []
|
|
32
|
+
for (const paramsData of paramsList) {
|
|
33
|
+
paramsData.add_media = add_media
|
|
34
|
+
const [params] = await paramsData.toReplaceParams()
|
|
35
|
+
variables.push(params)
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const addFileTasks: Promise<number>[] = []
|
|
39
|
+
for (const file of files) {
|
|
40
|
+
addFileTasks.push(add_template(file, isDecode))
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const encodeData = {
|
|
44
|
+
files: await Promise.all(addFileTasks),
|
|
45
|
+
variables
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const paramsEncode = await replace_params_encode_multiple_params(encodeData)
|
|
49
|
+
const verifyCode = await this.sign(paramsEncode)
|
|
50
|
+
return replace_batch_verify_multiple_params(String(verifyCode), paramsEncode.data)
|
|
51
|
+
}
|
|
31
52
|
}
|
package/worker/child/agency.ts
CHANGED
|
@@ -54,7 +54,7 @@ export default class implements ReplaceInterface {
|
|
|
54
54
|
const text: textData = {}
|
|
55
55
|
for (const key in params.textData) {
|
|
56
56
|
const value = params.textData[key] as Record<keyof image, any>
|
|
57
|
-
if (value
|
|
57
|
+
if (value && value.file instanceof Blob) {
|
|
58
58
|
text[key] = new image(value.file)
|
|
59
59
|
delete value.file
|
|
60
60
|
text[key].setPropertys(value)
|
|
@@ -66,7 +66,7 @@ export default class implements ReplaceInterface {
|
|
|
66
66
|
const media: mediaData = {}
|
|
67
67
|
for (const key in params.mediaData) {
|
|
68
68
|
const value = params.mediaData[key] as Record<keyof image, any>
|
|
69
|
-
if (value
|
|
69
|
+
if (value && value.file instanceof Blob) {
|
|
70
70
|
media[key] = new image(value.file)
|
|
71
71
|
delete value.file
|
|
72
72
|
media[key].setPropertys(value)
|
|
@@ -75,6 +75,41 @@ export default class implements ReplaceInterface {
|
|
|
75
75
|
return this.replace.execute(new paramsData(text, media), files)
|
|
76
76
|
}
|
|
77
77
|
|
|
78
|
+
//执行替换任务(多套参数)
|
|
79
|
+
executeMultipleParams(paramsMultiple: paramsData[], files: Temp[] | undefined): Promise<Record<string, Uint8Array>[]> {
|
|
80
|
+
if (files) {
|
|
81
|
+
for (const i in files) {
|
|
82
|
+
files[i] = transmitFileInfoToTemp(files[i] as transmitFileInfo)
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
const paramsList= []
|
|
86
|
+
for (const params of paramsMultiple) {
|
|
87
|
+
const text: textData = {}
|
|
88
|
+
for (const key in params.textData) {
|
|
89
|
+
const value = params.textData[key] as Record<keyof image, any>
|
|
90
|
+
if (value && value.file instanceof Blob) {
|
|
91
|
+
text[key] = new image(value.file)
|
|
92
|
+
delete value.file
|
|
93
|
+
text[key].setPropertys(value)
|
|
94
|
+
}else {
|
|
95
|
+
text[key] = value
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const media: mediaData = {}
|
|
100
|
+
for (const key in params.mediaData) {
|
|
101
|
+
const value = params.mediaData[key] as Record<keyof image, any>
|
|
102
|
+
if (value && value.file instanceof Blob) {
|
|
103
|
+
media[key] = new image(value.file)
|
|
104
|
+
delete value.file
|
|
105
|
+
media[key].setPropertys(value)
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
paramsList.push(new paramsData(text, media))
|
|
109
|
+
}
|
|
110
|
+
return this.replace.executeMultipleParams(paramsList, files)
|
|
111
|
+
}
|
|
112
|
+
|
|
78
113
|
//文件加密
|
|
79
114
|
fileEncrypt(file: Uint8Array): Promise<Uint8Array> {
|
|
80
115
|
return this.replace.fileEncrypt(file)
|
package/worker/child/base.ts
CHANGED
|
@@ -14,9 +14,10 @@ const allowCallMethodNames: Partial<Record<methodKeys<ReplaceInterface>, boolean
|
|
|
14
14
|
execute: true,
|
|
15
15
|
filesEncrypt: true,
|
|
16
16
|
fileEncrypt: true,
|
|
17
|
+
executeMultipleParams: true,
|
|
17
18
|
}
|
|
18
19
|
|
|
19
|
-
const tasks
|
|
20
|
+
const tasks = new Map<string, Function>()
|
|
20
21
|
|
|
21
22
|
let dispatch: ReplaceInterface
|
|
22
23
|
|
|
@@ -36,7 +37,7 @@ export async function call<T>(method: string, ...params: any[]): Promise<T> {
|
|
|
36
37
|
})
|
|
37
38
|
|
|
38
39
|
return new Promise<T>((resolve, reject) => {
|
|
39
|
-
tasks
|
|
40
|
+
tasks.set(replyId, resolve)
|
|
40
41
|
})
|
|
41
42
|
}
|
|
42
43
|
|
|
@@ -66,22 +67,30 @@ addEventListener('message', async event => {
|
|
|
66
67
|
value.buffer && transfer.push(value.buffer)
|
|
67
68
|
}
|
|
68
69
|
break;
|
|
70
|
+
case 'executeMultipleParams':
|
|
71
|
+
for (const map of (res as Record<string, Uint8Array>[])) {
|
|
72
|
+
for (const key in map) {
|
|
73
|
+
const value: Uint8Array = map[key]
|
|
74
|
+
value.buffer && transfer.push(value.buffer)
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
break;
|
|
69
78
|
case 'extractMedias':
|
|
70
79
|
for (const key in (res as Record<string, Uint8Array>)) {
|
|
71
80
|
const medias: media[] = res[key]
|
|
72
|
-
|
|
81
|
+
for (const media of medias) {
|
|
73
82
|
transfer.push(media.data.buffer)
|
|
74
|
-
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
break;
|
|
86
|
+
case 'fileEncrypt':
|
|
87
|
+
(res as Uint8Array).length && transfer.push((res as Uint8Array).buffer)
|
|
88
|
+
break;
|
|
89
|
+
case 'filesEncrypt':
|
|
90
|
+
for (const item of (res as Uint8Array[])) {
|
|
91
|
+
transfer.push(item.buffer)
|
|
75
92
|
}
|
|
76
93
|
break;
|
|
77
|
-
case 'fileEncrypt':
|
|
78
|
-
(res as Uint8Array).length && transfer.push((res as Uint8Array).buffer)
|
|
79
|
-
break;
|
|
80
|
-
case 'filesEncrypt':
|
|
81
|
-
for (const item of (res as Uint8Array[])) {
|
|
82
|
-
transfer.push(item.buffer)
|
|
83
|
-
}
|
|
84
|
-
break;
|
|
85
94
|
}
|
|
86
95
|
}
|
|
87
96
|
postMessage({
|
|
@@ -95,7 +104,11 @@ addEventListener('message', async event => {
|
|
|
95
104
|
break;
|
|
96
105
|
case messageTypes.methodCallReply:
|
|
97
106
|
// 方法调用的返回数据
|
|
98
|
-
tasks
|
|
99
|
-
|
|
107
|
+
const fn = tasks.get(data?.data?.replyId)
|
|
108
|
+
if (!fn) {
|
|
109
|
+
return
|
|
110
|
+
}
|
|
111
|
+
fn(data?.data?.result)
|
|
112
|
+
tasks.delete(data?.data?.replyId)
|
|
100
113
|
}
|
|
101
114
|
})
|
package/worker/index.ts
CHANGED
|
@@ -32,9 +32,10 @@ export default class worker implements DispatcherInterface {
|
|
|
32
32
|
for (const fun of this.#listenerList) {
|
|
33
33
|
tasks.push(fun(event))
|
|
34
34
|
}
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
35
|
+
const res = await Promise.all(tasks)
|
|
36
|
+
for (const reply of res) {
|
|
37
|
+
reply as any && worker.postMessage(reply)
|
|
38
|
+
}
|
|
38
39
|
}
|
|
39
40
|
this.#workers.push(worker)
|
|
40
41
|
}
|
package/worker/main/index.ts
CHANGED
|
@@ -19,7 +19,7 @@ const chunkMinNum = 20
|
|
|
19
19
|
export default class WorkerReplace implements ReplaceInterface {
|
|
20
20
|
#files: Temp[] = []
|
|
21
21
|
#dispatcher: DispatcherInterface
|
|
22
|
-
#tasks
|
|
22
|
+
#tasks = new Map<string, Function>()
|
|
23
23
|
#concurrency: number = 1
|
|
24
24
|
|
|
25
25
|
constructor(dispatcher: DispatcherInterface) {
|
|
@@ -36,9 +36,10 @@ export default class WorkerReplace implements ReplaceInterface {
|
|
|
36
36
|
if (!replyData) {
|
|
37
37
|
return
|
|
38
38
|
}
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
39
|
+
const fn = this.#tasks.get(replyData.replyId)
|
|
40
|
+
if (fn) {
|
|
41
|
+
fn(replyData.result)
|
|
42
|
+
this.#tasks.delete(replyData.replyId)
|
|
42
43
|
}
|
|
43
44
|
break;
|
|
44
45
|
case messageTypes.methodCall:
|
|
@@ -74,15 +75,15 @@ export default class WorkerReplace implements ReplaceInterface {
|
|
|
74
75
|
#call(method: string, params: any[]): any {
|
|
75
76
|
const transfer: any = []
|
|
76
77
|
|
|
77
|
-
|
|
78
|
+
for (const param of params) {
|
|
78
79
|
if (param instanceof Array) {
|
|
79
|
-
|
|
80
|
+
for (const item of param) {
|
|
80
81
|
if (item?.uint8Array?.buffer?.length) {
|
|
81
82
|
transfer.push(item.uint8Array.buffer)
|
|
82
83
|
}
|
|
83
|
-
}
|
|
84
|
+
}
|
|
84
85
|
}
|
|
85
|
-
}
|
|
86
|
+
}
|
|
86
87
|
|
|
87
88
|
const replyId = generateId()
|
|
88
89
|
this.#dispatcher.postMessage({
|
|
@@ -94,18 +95,18 @@ export default class WorkerReplace implements ReplaceInterface {
|
|
|
94
95
|
}
|
|
95
96
|
}, transfer.length ? { transfer } : undefined)
|
|
96
97
|
return new Promise((resolve, reject) => {
|
|
97
|
-
this.#tasks
|
|
98
|
+
this.#tasks.set(replyId, resolve)
|
|
98
99
|
})
|
|
99
100
|
}
|
|
100
101
|
|
|
101
102
|
//分片
|
|
102
|
-
#chunk<T>(
|
|
103
|
-
let chunks
|
|
104
|
-
if (this.#concurrency > 1 &&
|
|
105
|
-
const chunkSize = Math.ceil(
|
|
106
|
-
chunks = splitArrayIntoChunks<T>(
|
|
103
|
+
#chunk<T>(data: T[], chunkPackage?: (chunkData: T[]) => any): (T[] | ((chunkData: T[]) => any))[] {
|
|
104
|
+
let chunks
|
|
105
|
+
if (this.#concurrency > 1 && data.length > chunkMinNum) {
|
|
106
|
+
const chunkSize = Math.ceil(data.length / (Math.round(data.length / chunkMinNum)))
|
|
107
|
+
chunks = splitArrayIntoChunks<T>(data, chunkSize)
|
|
107
108
|
} else {
|
|
108
|
-
chunks = [
|
|
109
|
+
chunks = [data]
|
|
109
110
|
}
|
|
110
111
|
if (chunkPackage) {
|
|
111
112
|
for (const i in chunks) {
|
|
@@ -120,9 +121,9 @@ export default class WorkerReplace implements ReplaceInterface {
|
|
|
120
121
|
files = this.#files
|
|
121
122
|
}
|
|
122
123
|
const tasks: Promise<transmitFileInfo | undefined>[] = []
|
|
123
|
-
|
|
124
|
+
for (const file of files) {
|
|
124
125
|
tasks.push(file.getTransmitFileInfo())
|
|
125
|
-
}
|
|
126
|
+
}
|
|
126
127
|
const res = await Promise.all(tasks)
|
|
127
128
|
return res.filter(item => !!item)
|
|
128
129
|
}
|
|
@@ -134,9 +135,9 @@ export default class WorkerReplace implements ReplaceInterface {
|
|
|
134
135
|
|
|
135
136
|
async #chunkCall(method: string, paramChunks: any[]): Promise<Record<string, any>> {
|
|
136
137
|
const tasks: Promise<Record<string, any>>[] = []
|
|
137
|
-
|
|
138
|
+
for (const chunk of paramChunks) {
|
|
138
139
|
tasks.push(this.#call(method, chunk))
|
|
139
|
-
}
|
|
140
|
+
}
|
|
140
141
|
const tasksRes = await Promise.all(tasks)
|
|
141
142
|
return tasksRes.reduce((accumulator, current) => {
|
|
142
143
|
return { ...accumulator, ...current }
|
|
@@ -175,6 +176,22 @@ export default class WorkerReplace implements ReplaceInterface {
|
|
|
175
176
|
return this.#chunkCall('execute', chunks)
|
|
176
177
|
}
|
|
177
178
|
|
|
179
|
+
async executeMultipleParams(params: paramsData[], files: Temp[] | undefined): Promise<Record<string, Uint8Array>[]> {
|
|
180
|
+
const fileData = await this.#getTempFileData(files)
|
|
181
|
+
const tasks: Promise<Record<string, any>[]>[] = []
|
|
182
|
+
this.#chunk(fileData, chunkData => {
|
|
183
|
+
tasks.push(this.#call('executeMultipleParams', [params, chunkData] as any))
|
|
184
|
+
})
|
|
185
|
+
const tasksRes = (await Promise.all(tasks)) as Record<string, Uint8Array>[][]
|
|
186
|
+
const res = new Array(params.length)
|
|
187
|
+
for (const item of tasksRes) {
|
|
188
|
+
for (let index = 0; index < item.length; index++) {
|
|
189
|
+
res[index] = {...item[index], ...res[index]}
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
return res
|
|
193
|
+
}
|
|
194
|
+
|
|
178
195
|
async fileEncrypt(file: Uint8Array): Promise<Uint8Array> {
|
|
179
196
|
return await this.#call('fileEncrypt', [file])
|
|
180
197
|
}
|
|
@@ -183,9 +200,9 @@ export default class WorkerReplace implements ReplaceInterface {
|
|
|
183
200
|
const chunks = this.#chunk<Uint8Array>(files) as (Uint8Array[])[]
|
|
184
201
|
|
|
185
202
|
const tasks: Promise<Uint8Array[]>[] = []
|
|
186
|
-
|
|
203
|
+
for (const chunk of chunks) {
|
|
187
204
|
tasks.push(this.#call('filesEncrypt', [chunk]))
|
|
188
|
-
}
|
|
205
|
+
}
|
|
189
206
|
const tasksRes = await Promise.all(tasks)
|
|
190
207
|
return tasksRes.flat()
|
|
191
208
|
}
|
package/dist/index-BkwrGCka.js
DELETED
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
var g = (r) => {
|
|
2
|
-
throw TypeError(r);
|
|
3
|
-
};
|
|
4
|
-
var p = (r, s, t) => s.has(r) || g("Cannot " + t);
|
|
5
|
-
var e = (r, s, t) => (p(r, s, "read from private field"), t ? t.call(r) : s.get(r)), n = (r, s, t) => s.has(r) ? g("Cannot add the same private member more than once") : s instanceof WeakSet ? s.add(r) : s.set(r, t), a = (r, s, t, o) => (p(r, s, "write to private field"), o ? o.call(r, t) : s.set(r, t), t), k = (r, s, t) => (p(r, s, "access private method"), t);
|
|
6
|
-
var l = (r, s, t, o) => ({
|
|
7
|
-
set _(f) {
|
|
8
|
-
a(r, s, f, t);
|
|
9
|
-
},
|
|
10
|
-
get _() {
|
|
11
|
-
return e(r, s, o);
|
|
12
|
-
}
|
|
13
|
-
});
|
|
14
|
-
var i, c, u, h, d, y;
|
|
15
|
-
class v {
|
|
16
|
-
constructor(s, t) {
|
|
17
|
-
n(this, d);
|
|
18
|
-
n(this, i);
|
|
19
|
-
n(this, c, 0);
|
|
20
|
-
n(this, u, []);
|
|
21
|
-
n(this, h, []);
|
|
22
|
-
if (a(this, i, Number(t)), !e(this, i) || e(this, i) < 1)
|
|
23
|
-
try {
|
|
24
|
-
a(this, i, navigator.hardwareConcurrency < 8 ? navigator.hardwareConcurrency : 8);
|
|
25
|
-
} catch {
|
|
26
|
-
}
|
|
27
|
-
(!e(this, i) || e(this, i) < 1) && a(this, i, 1);
|
|
28
|
-
for (let o = 0; o < e(this, i); o++)
|
|
29
|
-
k(this, d, y).call(this, s);
|
|
30
|
-
}
|
|
31
|
-
concurrency() {
|
|
32
|
-
return e(this, i);
|
|
33
|
-
}
|
|
34
|
-
postMessage(s, t) {
|
|
35
|
-
e(this, u)[++l(this, c)._] || a(this, c, 0), e(this, u)[e(this, c)].postMessage(s, t);
|
|
36
|
-
}
|
|
37
|
-
addListener(s) {
|
|
38
|
-
e(this, h).push(s);
|
|
39
|
-
}
|
|
40
|
-
removeListener(s) {
|
|
41
|
-
for (const t in e(this, h))
|
|
42
|
-
if (e(this, h)[t] == s) {
|
|
43
|
-
e(this, h).splice(t, 1);
|
|
44
|
-
return;
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
i = new WeakMap(), c = new WeakMap(), u = new WeakMap(), h = new WeakMap(), d = new WeakSet(), y = function(s) {
|
|
49
|
-
const t = new s();
|
|
50
|
-
t.onmessage = async (o) => {
|
|
51
|
-
const f = [];
|
|
52
|
-
for (const w of e(this, h))
|
|
53
|
-
f.push(w(o));
|
|
54
|
-
(await Promise.all(f)).forEach((w) => {
|
|
55
|
-
w && t.postMessage(w);
|
|
56
|
-
});
|
|
57
|
-
}, e(this, u).push(t);
|
|
58
|
-
};
|
|
59
|
-
export {
|
|
60
|
-
v as w
|
|
61
|
-
};
|