template-replacement 3.3.3 → 3.4.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.
Files changed (56) hide show
  1. package/.editorconfig +8 -0
  2. package/.oxfmtrc.jsonc +7 -0
  3. package/.oxlintrc.json +3 -0
  4. package/README.md +39 -9
  5. package/core/base.ts +54 -55
  6. package/core/general.ts +37 -9
  7. package/core/sign.ts +43 -8
  8. package/dispatcher/general.ts +2 -1
  9. package/dispatcher/sign.ts +2 -1
  10. package/dispatcher/workerGeneral.ts +1 -1
  11. package/dispatcher/workerSign.ts +1 -1
  12. package/dist/base-DQz39fXI.js +249 -0
  13. package/dist/index-oILo_kXG.js +46 -0
  14. package/dist/main/general.js +1334 -1384
  15. package/dist/main/sign.js +1476 -1514
  16. package/dist/replace/general.js +283 -313
  17. package/dist/replace/sign.js +309 -327
  18. package/download/index.ts +13 -13
  19. package/download/stream.ts +29 -28
  20. package/eslint.config.ts +28 -0
  21. package/fileSystem/db/index.ts +25 -25
  22. package/fileSystem/db/indexedDBCache.ts +145 -143
  23. package/fileSystem/index.ts +5 -8
  24. package/fileSystem/interface.ts +4 -5
  25. package/fileSystem/opfs/index.ts +40 -36
  26. package/helper/index.ts +136 -125
  27. package/index.ts +6 -6
  28. package/office/zip.ts +106 -97
  29. package/package.json +14 -8
  30. package/replace/base.ts +203 -222
  31. package/replace/general.ts +44 -24
  32. package/replace/image.ts +88 -90
  33. package/replace/interface.ts +29 -24
  34. package/replace/paramsData.ts +107 -95
  35. package/replace/sign.ts +79 -52
  36. package/task/urlDownloadTask.ts +53 -55
  37. package/temp/index.ts +139 -124
  38. package/temp/interface.ts +8 -8
  39. package/tsconfig.json +1 -1
  40. package/vite.config.ts +11 -14
  41. package/worker/child/agency.ts +49 -41
  42. package/worker/child/base.ts +125 -89
  43. package/worker/child/general.ts +2 -3
  44. package/worker/child/sign.ts +4 -4
  45. package/worker/index.ts +52 -53
  46. package/worker/interface.ts +9 -6
  47. package/worker/main/general.ts +5 -5
  48. package/worker/main/index.ts +191 -66
  49. package/worker/main/sign.ts +5 -5
  50. package/worker/type.ts +16 -15
  51. package/dist/assets/template_replacement_core_wasm_bg.wasm +0 -0
  52. package/dist/assets/template_replacement_sign_core_wasm_bg.wasm +0 -0
  53. package/dist/base-CJv023nf.js +0 -284
  54. package/dist/general.d.ts +0 -1
  55. package/dist/index-tFDVIkZX.js +0 -46
  56. package/dist/sign.d.ts +0 -1
package/replace/base.ts CHANGED
@@ -1,236 +1,217 @@
1
- import Interface, { media } from './interface';
2
- import { Interface as CoreInterface } from '../core/base';
3
- import paramsData from './paramsData';
4
- import Temp from '../temp';
5
- import { fileTypes } from '../helper';
6
-
7
- export default class Base implements Interface{
8
- #files: Temp[] = []
9
- #core: CoreInterface
10
-
11
- constructor(core: CoreInterface) {
12
- this.#core = core
1
+ import Interface, { media } from './interface'
2
+ import { AsyncCoreInterface } from '../core/base'
3
+ import paramsData from './paramsData'
4
+ import Temp from '../temp'
5
+ import { fileTypes } from '../helper'
6
+
7
+ export type filesTidyResultItem = {
8
+ names: string[]
9
+ uint8Arrays: Uint8Array[]
10
+ }
11
+
12
+ export type filesTidyResult = {
13
+ //需要解密的文件
14
+ decode: filesTidyResultItem,
15
+ //不需要解密的文件
16
+ noDecode: filesTidyResultItem
17
+ }
18
+
19
+ //整理模板文件
20
+ async function tempFilesTidy(files: Temp[] = []): Promise<filesTidyResult> {
21
+ //等待文件加载完成
22
+ const tasks = []
23
+ for (const file of files) {
24
+ tasks.push(file.getBuffer())
25
+ }
26
+ await Promise.all(tasks)
27
+
28
+ const result: filesTidyResult = {
29
+ decode: {
30
+ names: [],
31
+ uint8Arrays: [],
32
+ },
33
+ noDecode: {
34
+ names: [],
35
+ uint8Arrays: [],
36
+ },
37
+ }
38
+
39
+ //整理出需要解密和不需要解密的文件
40
+ for (const file of files) {
41
+ if (!file.uint8Array) {
42
+ continue
13
43
  }
14
-
15
- addTempFile(tempFile: Temp){
16
- this.#files.push(tempFile)
44
+ if (file.isDecode) {
45
+ result.decode.names.push(file.name)
46
+ result.decode.uint8Arrays.push(file.uint8Array)
47
+ } else {
48
+ result.noDecode.names.push(file.name)
49
+ result.noDecode.uint8Arrays.push(file.uint8Array)
17
50
  }
18
-
19
- clear(): void {
20
- this.#files.length = 0
51
+ }
52
+ return result
53
+ }
54
+
55
+ export default class Base implements Interface {
56
+ #files: Temp[] = []
57
+ #core: AsyncCoreInterface
58
+
59
+ constructor(core: AsyncCoreInterface) {
60
+ this.#core = core
61
+ }
62
+
63
+ addTempFile(tempFile: Temp) {
64
+ this.#files.push(tempFile)
65
+ }
66
+
67
+ clear(): void {
68
+ this.#files.length = 0
69
+ }
70
+
71
+ //提取单个文件内的所有变量
72
+ async extractOneFileVariables(variables: Record<string, string[]>, file: Temp): Promise<void> {
73
+ const buffer = await file.getBuffer()
74
+ if (!buffer) {
75
+ return
21
76
  }
22
-
23
- async extractVariables(files: Temp[] | undefined): Promise<Record<string, string[]>> {
24
- if (!files) {
25
- files = this.#files
26
- }
27
- const data: Record<string, string[]> = {}
28
- const tasks = []
29
- for (const file of files) {
30
- tasks.push(new Promise<void>(async resolve => {
31
- try {
32
- const buffer = await file.getBuffer()
33
- if (buffer && (file.isDecode || (await file.type()) !== fileTypes.unknown)) {
34
- data[file.name] = await this.#core.extract_one_file_variable_names(buffer, file.isDecode)
35
- }
36
- resolve()
37
- } catch (error) {
38
- console.error(error)
39
- }
40
- }))
41
- }
42
- await Promise.all(tasks)
43
- return data
77
+ if ((await file.type()) === fileTypes.unknown && !file.isDecode) {
78
+ return
44
79
  }
45
-
46
- async extractMedias(files: Temp[] | undefined): Promise<Record<string, media[]>> {
47
- if (!files) {
48
- files = this.#files
49
- }
50
- const data: Record<string, media[]> = {}
51
- const tasks = []
52
- for (const file of files) {
53
- tasks.push(new Promise<void>(async resolve => {
54
- try {
55
- const buffer = await file.getBuffer()
56
- if (buffer && (file.isDecode || (await file.type()) !== fileTypes.unknown)) {
57
- let medias = await this.#core.extract_one_file_medias(buffer, file.isDecode)
58
- data[file.name] = []
59
- if (medias && Array.isArray(medias)) {
60
- for (const m of medias) {
61
- if (m.id && m.data) {
62
- data[file.name].push({
63
- id: m.id,
64
- data: new Uint8Array(m.data)
65
- })
66
- }
67
- }
68
- }
69
- }
70
- resolve()
71
- } catch (error) {
72
- console.error(error)
73
- }
74
- }))
75
- }
76
- await Promise.all(tasks)
77
- return data
80
+ variables[file.name] = await this.#core.extract_one_file_variable_names(
81
+ buffer,
82
+ file.isDecode,
83
+ )
84
+ }
85
+
86
+ //提取多个文件内的所有变量
87
+ async extractVariables(
88
+ files: Temp[] | undefined,
89
+ ): Promise<Record<string, string[]>> {
90
+ if (!files) {
91
+ files = this.#files
78
92
  }
79
-
80
- async handle(paramsData: paramsData, files: Uint8Array[], isDecode: boolean = false): Promise<Uint8Array[]> {
81
- return []
93
+ const variables: Record<string, string[]> = {}
94
+ const tasks = []
95
+ for (const file of files) {
96
+ tasks.push(this.extractOneFileVariables(variables, file))
82
97
  }
83
- async handleMultipleParams(paramsData: paramsData[], files: Uint8Array[], isDecode: boolean = false): Promise<Uint8Array[]> {
84
- return []
98
+ await Promise.allSettled(tasks)
99
+ return variables
100
+ }
101
+
102
+ //提取单个文件内的所有媒体文件
103
+ async extractOneFileMedias(medias: Record<string, media[]>, file: Temp): Promise<void> {
104
+ const buffer = await file.getBuffer()
105
+ if (!buffer) {
106
+ return
85
107
  }
86
-
87
- async sign(data: any): Promise<string> {
88
- return ""
108
+ if ((await file.type()) === fileTypes.unknown && !file.isDecode) {
109
+ return
89
110
  }
90
-
91
- async execute(params: paramsData, files: Temp[] | undefined): Promise<Record<string, Uint8Array>> {
92
- if (!files) {
93
- files = this.#files
94
- }
95
-
96
- //等待文件加载完成
97
- const tasks = []
98
- for (const file of files) {
99
- tasks.push(file.getBuffer())
100
- }
101
- await Promise.all(tasks)
102
-
103
- const fileMap: { decode: { names: string[], uint8Arrays: Uint8Array[] }, noDecode: { names: string[], uint8Arrays: Uint8Array[] }} = {
104
- //需要解密的文件
105
- decode: {
106
- names: [],
107
- uint8Arrays: [],
108
- },
109
- //不需要解密的文件
110
- noDecode: {
111
- names: [],
112
- uint8Arrays: [],
113
- }
114
- };
115
-
116
- //整理出需要解密和不需要解密的文件
117
- for (const file of files) {
118
- if (!file.uint8Array) {
119
- continue;
120
- }
121
- if (file.isDecode) {
122
- fileMap.decode.names.push(file.name)
123
- fileMap.decode.uint8Arrays.push(file.uint8Array)
124
- } else {
125
- fileMap.noDecode.names.push(file.name)
126
- fileMap.noDecode.uint8Arrays.push(file.uint8Array)
127
- }
128
- }
129
- //分别处理需要解密和不需要解密的文件
130
- const res = await Promise.all([
131
- this._execute(params, fileMap.noDecode.names, fileMap.noDecode.uint8Arrays, false),
132
- this._execute(params, fileMap.decode.names, fileMap.decode.uint8Arrays, true),
133
- ])
134
-
135
- return {
136
- ...res[0],
137
- ...res[1],
138
- }
111
+ const res = await this.#core.extract_one_file_medias(
112
+ buffer,
113
+ file.isDecode,
114
+ )
115
+ medias[file.name] = []
116
+ if (!Array.isArray(res)) {
117
+ return
139
118
  }
140
-
141
- async _execute(params: paramsData, names: string[], uint8Arrays: Uint8Array[], isDecode: boolean = false): Promise<Record<string, Uint8Array>> {
142
- const resData: Record<string, Uint8Array> = {}
143
- if (!uint8Arrays.length) {
144
- return resData
145
- }
146
- const res = await this.handle(params, uint8Arrays, isDecode)
147
- for (let index = 0; index < res.length; index++) {
148
- const file = res[index]
149
- if (file.length) {
150
- resData[names[index]] = file
151
- }
152
- }
153
- return resData
119
+ for (const { id, data } of res) {
120
+ if (id && data) {
121
+ medias[file.name].push({
122
+ id: id,
123
+ data: new Uint8Array(data),
124
+ })
125
+ }
154
126
  }
155
-
156
- async executeMultipleParams(paramsList: paramsData[], files: Temp[] | undefined): Promise<Record<string, Uint8Array>[]> {
157
- if (!files) {
158
- files = this.#files
159
- }
160
-
161
- //等待文件加载完成
162
- const tasks = []
163
- for (const file of files) {
164
- tasks.push(file.getBuffer())
165
- }
166
- await Promise.all(tasks)
167
-
168
- const fileMap: { decode: { names: string[], uint8Arrays: Uint8Array[] }, noDecode: { names: string[], uint8Arrays: Uint8Array[] }} = {
169
- //需要解密的文件
170
- decode: {
171
- names: [],
172
- uint8Arrays: [],
173
- },
174
- //不需要解密的文件
175
- noDecode: {
176
- names: [],
177
- uint8Arrays: [],
178
- }
179
- };
180
-
181
- //整理出需要解密和不需要解密的文件
182
- for (const file of files) {
183
- if (!file.uint8Array) {
184
- continue;
185
- }
186
- if (file.isDecode) {
187
- fileMap.decode.names.push(file.name)
188
- fileMap.decode.uint8Arrays.push(file.uint8Array)
189
- } else {
190
- fileMap.noDecode.names.push(file.name)
191
- fileMap.noDecode.uint8Arrays.push(file.uint8Array)
192
- }
193
- }
194
- //分别处理需要解密和不需要解密的文件
195
- const res = await Promise.all([
196
- this._executeMultipleParams(paramsList, fileMap.noDecode.names, fileMap.noDecode.uint8Arrays, false),
197
- this._executeMultipleParams(paramsList, fileMap.decode.names, fileMap.decode.uint8Arrays, true),
198
- ])
199
-
200
- const result = []
201
- for (let i = 0; i < res[0].length; i++) {
202
- result.push({...res[0][i], ...res[1][i]})
203
- }
204
- return result
127
+ }
128
+
129
+ //提取多个文件内的所有媒体文件
130
+ async extractMedias(
131
+ files: Temp[] | undefined,
132
+ ): Promise<Record<string, media[]>> {
133
+ if (!files) {
134
+ files = this.#files
205
135
  }
206
-
207
-
208
- async _executeMultipleParams(paramsList: paramsData[], names: string[], uint8Arrays: Uint8Array[], isDecode: boolean = false): Promise<Record<string, Uint8Array>[]> {
209
- const result: Record<string, Uint8Array>[] = Array(paramsList.length) //整理出每一套参数的替换结果文件
210
- if (!uint8Arrays.length) {
211
- return result
212
- }
213
- const resFileList = await this.handleMultipleParams(paramsList, uint8Arrays, isDecode)
214
-
215
- let resFileIndex = 0
216
- for (let index = 0; index < paramsList.length; index++) {
217
- const resultItem: Record<string, Uint8Array> = {}
218
- for (const name of names) {
219
- const file = resFileList[resFileIndex++]
220
- if (file.length) {
221
- resultItem[name] = file
222
- }
223
- }
224
- result[index] = resultItem
225
- }
226
- return result
136
+ const medias: Record<string, media[]> = {}
137
+ const tasks = []
138
+ for (const file of files) {
139
+ tasks.push(this.extractOneFileMedias(medias, file))
227
140
  }
228
-
229
- fileEncrypt(file: Uint8Array): Promise<Uint8Array> {
230
- return this.#core.file_encrypt(file)
141
+ await Promise.all(tasks)
142
+ return medias
143
+ }
144
+
145
+ async handle(
146
+ paramsData: paramsData,
147
+ files: Uint8Array[],
148
+ encode_files: Uint8Array[],
149
+ ): Promise<Uint8Array[]> {
150
+ return []
151
+ }
152
+
153
+ async handleMultipleParams(
154
+ paramsData: paramsData[],
155
+ files: Uint8Array[],
156
+ encode_files: Uint8Array[],
157
+ ): Promise<Uint8Array[]> {
158
+ return []
159
+ }
160
+
161
+ async sign(data: unknown): Promise<string> {
162
+ return ''
163
+ }
164
+
165
+ async execute(
166
+ params: paramsData,
167
+ files: Temp[] | undefined,
168
+ ): Promise<Record<string, Uint8Array>> {
169
+ const { noDecode, decode } = await tempFilesTidy(files ?? this.#files)
170
+ const res = await this.handle(params, noDecode.uint8Arrays, decode.uint8Arrays);
171
+ const result: Record<string, Uint8Array> = {}
172
+ let i = 0
173
+ for (const name of noDecode.names) {
174
+ result[name] = res[i++] ?? new Uint8Array()
231
175
  }
232
-
233
- filesEncrypt(files: (Uint8Array)[]): Promise<(Uint8Array)[]> {
234
- return this.#core.files_encrypt(files)
176
+ for (const name of decode.names) {
177
+ result[name] = res[i++] ?? new Uint8Array()
178
+ }
179
+ return result
180
+ }
181
+
182
+ async executeMultipleParams(
183
+ paramsList: paramsData[],
184
+ files: Temp[] | undefined,
185
+ ): Promise<Record<string, Uint8Array>[]> {
186
+ const { noDecode, decode } = await tempFilesTidy(files ?? this.#files)
187
+ const resFileList = await this.handleMultipleParams(paramsList, noDecode.uint8Arrays, decode.uint8Arrays);
188
+ const result: Record<string, Uint8Array>[] = Array(paramsList.length)
189
+
190
+ let resFileIndex = 0
191
+ for (let index = 0; index < paramsList.length; index++) {
192
+ const resultItem: Record<string, Uint8Array> = {}
193
+ for (const name of noDecode.names) {
194
+ const file = resFileList[resFileIndex++]
195
+ if (file.length) {
196
+ resultItem[name] = file
197
+ }
198
+ }
199
+ for (const name of decode.names) {
200
+ const file = resFileList[resFileIndex++]
201
+ if (file.length) {
202
+ resultItem[name] = file
203
+ }
204
+ }
205
+ result[index] = resultItem
235
206
  }
236
- }
207
+ return result
208
+ }
209
+
210
+ async fileEncrypt(file: Uint8Array): Promise<Uint8Array> {
211
+ return this.#core.file_encrypt(file)
212
+ }
213
+
214
+ async filesEncrypt(files: Uint8Array[]): Promise<Uint8Array[]> {
215
+ return this.#core.files_encrypt(files)
216
+ }
217
+ }
@@ -1,28 +1,48 @@
1
- import Base from './base';
2
- import core, { replace_batch, replace_batch_multiple_params } from '../core/general'
3
- import paramsData, { replaceParams } from './paramsData';
4
-
1
+ import Base from './base'
2
+ import core, {
3
+ replace_batch,
4
+ replace_batch_multiple_params
5
+ } from '../core/general'
6
+ import paramsData, { replaceParams } from './paramsData'
5
7
  export default class General extends Base {
6
- constructor() {
7
- super(core())
8
- }
8
+ constructor() {
9
+ super(core())
10
+ }
9
11
 
10
- async handle(paramsData: paramsData, files: Uint8Array[], isDecode: boolean = false): Promise<Uint8Array[]> {
11
- const [params, mediaBuffers] = await paramsData.toReplaceParams()
12
- return replace_batch(params, mediaBuffers, files, isDecode)
13
- }
12
+ async handle(
13
+ paramsData: paramsData,
14
+ files: Uint8Array[],
15
+ encode_files: Uint8Array[],
16
+ ): Promise<Uint8Array[]> {
17
+ const [params, mediaBuffers] = await paramsData.toReplaceParams()
18
+ return replace_batch(params, mediaBuffers, files, encode_files)
19
+ }
14
20
 
15
- async handleMultipleParams(paramsList: paramsData[], files: Uint8Array[], isDecode: boolean = false): Promise<Uint8Array[]> {
16
- const mediaBuffers: Uint8Array[] = [],
17
- newParamsListTasks: Promise<replaceParams>[] = []
18
- for (const paramsData of paramsList) {
19
- newParamsListTasks.push(new Promise<replaceParams>((resolve, reject) => {
20
- paramsData.toReplaceParams(mediaBuffers).then(([params]) => {
21
- resolve(params)
22
- }).catch(reject)
23
- }))
24
- }
25
- const newParamsList = await Promise.all(newParamsListTasks)
26
- return replace_batch_multiple_params(newParamsList, mediaBuffers, files, isDecode)
21
+ async handleMultipleParams(
22
+ paramsList: paramsData[],
23
+ files: Uint8Array[],
24
+ encode_files: Uint8Array[],
25
+ ): Promise<Uint8Array[]> {
26
+ const mediaBuffers: Uint8Array[] = [],
27
+ newParamsListTasks: Promise<replaceParams>[] = []
28
+ for (const paramsData of paramsList) {
29
+ newParamsListTasks.push(
30
+ new Promise<replaceParams>((resolve, reject) => {
31
+ paramsData
32
+ .toReplaceParams(mediaBuffers)
33
+ .then(([params]) => {
34
+ resolve(params)
35
+ })
36
+ .catch(reject)
37
+ }),
38
+ )
27
39
  }
28
- }
40
+ const newParamsList = await Promise.all(newParamsListTasks)
41
+ return replace_batch_multiple_params(
42
+ newParamsList,
43
+ mediaBuffers,
44
+ files,
45
+ encode_files,
46
+ )
47
+ }
48
+ }