template-replacement 3.5.0 → 3.5.2

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.
@@ -1,4 +1,4 @@
1
- import { b as T, B as p } from "../base-DQz39fXI.js";
1
+ import { b as T, B as p } from "../base-BKyaWISJ.js";
2
2
  let C;
3
3
  function x(B) {
4
4
  const A = C.__externref_table_alloc();
@@ -1,4 +1,4 @@
1
- import { b as t, B as P } from "../base-DQz39fXI.js";
1
+ import { b as t, B as P } from "../base-BKyaWISJ.js";
2
2
  let C;
3
3
  function x(B) {
4
4
  const A = C.__externref_table_alloc();
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "template-replacement",
3
3
  "description": "模板文件替换",
4
- "version": "3.5.0",
4
+ "version": "3.5.2",
5
5
  "author": "fushiliang <994301536@qq.com>",
6
6
  "type": "module",
7
7
  "main": "index.ts",
package/replace/base.ts CHANGED
@@ -3,6 +3,7 @@ import { AsyncCoreInterface } from '../core/base'
3
3
  import paramsData from './paramsData'
4
4
  import Temp from '../temp'
5
5
  import { fileTypes } from '../helper'
6
+ import { Zip, ZipDeflate } from 'fflate'
6
7
 
7
8
  export type filesTidyResultItem = {
8
9
  names: string[]
@@ -158,10 +159,12 @@ export default class Base implements Interface {
158
159
  return []
159
160
  }
160
161
 
162
+ //签名方法
161
163
  async sign(data: unknown): Promise<string> {
162
164
  return ''
163
165
  }
164
166
 
167
+ //执行替换任务
165
168
  async execute(
166
169
  params: paramsData,
167
170
  files: Temp[] | undefined,
@@ -179,6 +182,47 @@ export default class Base implements Interface {
179
182
  return result
180
183
  }
181
184
 
185
+ //执行替换任务(返回zip压缩数据)
186
+ async executeToZip(
187
+ params: paramsData,
188
+ files: Temp[] | undefined,
189
+ ): Promise<Uint8Array> {
190
+ const { noDecode, decode } = await tempFilesTidy(files ?? this.#files)
191
+ const res = await this.handle(params, noDecode.uint8Arrays, decode.uint8Arrays);
192
+
193
+ return new Promise((resolve, reject) => {
194
+ const u8s: Uint8Array[] = [];
195
+ const _zip = new Zip((err, dat, final) => {
196
+ if (dat.length) {
197
+ u8s.push(dat)
198
+ }
199
+ if (final) {
200
+ const blob = new Blob(u8s as BlobPart[])
201
+ blob.arrayBuffer().then(res => {
202
+ resolve(new Uint8Array(res))
203
+ })
204
+ }
205
+ });
206
+ let i = 0
207
+ for (const name of noDecode.names) {
208
+ const helloTxt = new ZipDeflate(name, {
209
+ level: 9,
210
+ });
211
+ _zip.add(helloTxt)
212
+ helloTxt.push((res[i++] ?? new Uint8Array()) as Uint8Array, true);
213
+ }
214
+ for (const name of decode.names) {
215
+ const helloTxt = new ZipDeflate(name, {
216
+ level: 9,
217
+ });
218
+ _zip.add(helloTxt)
219
+ helloTxt.push((res[i++] ?? new Uint8Array()) as Uint8Array, true);
220
+ }
221
+ _zip.end()
222
+ })
223
+ }
224
+
225
+ //执行替换任务(多套参数)
182
226
  async executeMultipleParams(
183
227
  paramsList: paramsData[],
184
228
  files: Temp[] | undefined,
@@ -207,11 +251,61 @@ export default class Base implements Interface {
207
251
  return result
208
252
  }
209
253
 
210
- async fileEncrypt(file: Uint8Array): Promise<Uint8Array> {
254
+ //执行替换任务(多套参数,返回zip压缩数据)
255
+ async executeMultipleParamsToZip(
256
+ paramsList: paramsData[],
257
+ files: Temp[] | undefined,
258
+ ): Promise<Uint8Array> {
259
+ const { noDecode, decode } = await tempFilesTidy(files ?? this.#files)
260
+ const resFileList = await this.handleMultipleParams(paramsList, noDecode.uint8Arrays, decode.uint8Arrays);
261
+ return new Promise((resolve, reject) => {
262
+ const u8s: Uint8Array[] = [];
263
+ const _zip = new Zip((err, dat, final) => {
264
+ if (dat.length) {
265
+ u8s.push(dat)
266
+ }
267
+ if (final) {
268
+ const blob = new Blob(u8s as BlobPart[])
269
+ blob.arrayBuffer().then(res => {
270
+ resolve(new Uint8Array(res))
271
+ })
272
+ }
273
+ });
274
+
275
+ let resFileIndex = 0
276
+ for (let index = 0; index < paramsList.length; index++) {
277
+ for (const name of noDecode.names) {
278
+ const file = resFileList[resFileIndex++]
279
+ if (file.length) {
280
+ const helloTxt = new ZipDeflate(index + "/" + name, {
281
+ level: 9,
282
+ });
283
+ _zip.add(helloTxt)
284
+ helloTxt.push(file, true);
285
+ }
286
+ }
287
+ for (const name of decode.names) {
288
+ const file = resFileList[resFileIndex++]
289
+ if (file.length) {
290
+ const helloTxt = new ZipDeflate(index + "/" + name, {
291
+ level: 9,
292
+ });
293
+ _zip.add(helloTxt)
294
+ helloTxt.push(file, true);
295
+ }
296
+ }
297
+ }
298
+ _zip.end()
299
+ })
300
+ }
301
+
302
+ //文件加密
303
+ fileEncrypt(file: Uint8Array): Promise<Uint8Array> {
211
304
  return this.#core.file_encrypt(file)
212
305
  }
213
306
 
214
- async filesEncrypt(files: Uint8Array[]): Promise<Uint8Array[]> {
307
+ //文件批量加密
308
+ filesEncrypt(files: Uint8Array[]): Promise<Uint8Array[]> {
215
309
  return this.#core.files_encrypt(files)
216
310
  }
217
311
  }
@@ -28,12 +28,24 @@ export default interface ReplaceInterface {
28
28
  files: Temp[] | undefined,
29
29
  ): Promise<Record<string, Uint8Array>>
30
30
 
31
+ //执行替换任务(返回zip压缩数据)
32
+ executeToZip(
33
+ params: paramsData,
34
+ files: Temp[] | undefined,
35
+ ): Promise<Uint8Array>
36
+
31
37
  //执行替换任务(多套参数)
32
38
  executeMultipleParams(
33
39
  params: paramsData[],
34
40
  files: Temp[] | undefined,
35
41
  ): Promise<Record<string, Uint8Array>[]>
36
42
 
43
+ //执行替换任务(多套参数,返回zip压缩数据)
44
+ executeMultipleParamsToZip(
45
+ params: paramsData[],
46
+ files: Temp[] | undefined,
47
+ ): Promise<Uint8Array>
48
+
37
49
  //文件加密
38
50
  fileEncrypt(file: Uint8Array): Promise<Uint8Array>
39
51
 
@@ -80,6 +80,14 @@ export default class implements ReplaceInterface {
80
80
  return this.#replace.execute(new paramsData(text, media), files as Temp[])
81
81
  }
82
82
 
83
+ //执行替换任务(返回zip压缩数据)
84
+ async executeToZip(
85
+ params: paramsData,
86
+ files: Temp[] | transmitFileInfo[] | undefined,
87
+ ): Promise<Uint8Array> {
88
+ return new Uint8Array()
89
+ }
90
+
83
91
  //执行替换任务(多套参数)
84
92
  executeMultipleParams(
85
93
  paramsMultiple: paramsData[],
@@ -118,6 +126,14 @@ export default class implements ReplaceInterface {
118
126
  return this.#replace.executeMultipleParams(paramsList, files)
119
127
  }
120
128
 
129
+ //执行替换任务(多套参数,返回zip压缩数据)
130
+ async executeMultipleParamsToZip(
131
+ paramsMultiple: paramsData[],
132
+ files: Temp[] | undefined,
133
+ ): Promise<Uint8Array> {
134
+ return new Uint8Array()
135
+ }
136
+
121
137
  //文件加密
122
138
  fileEncrypt(file: Uint8Array): Promise<Uint8Array> {
123
139
  return this.#replace.fileEncrypt(file)
@@ -1,249 +0,0 @@
1
- class y {
2
- // 构造函数
3
- constructor() {
4
- this._initFinishCallBackFuns = [], this.#t = !1, this.#e = "template_replacement_db", this.#n = 1, this.#a = "template_files", this.#s();
5
- }
6
- #t;
7
- #e;
8
- #n;
9
- #a;
10
- #s() {
11
- return this._init ? this._init : (this._init = new Promise((t, e) => {
12
- const a = indexedDB.open(this.#e, this.#n);
13
- a.onsuccess = (s) => {
14
- if (this._db = a.result, this.#t = !0, this._initFinishCallBackFuns) {
15
- try {
16
- for (const n of this._initFinishCallBackFuns)
17
- n();
18
- } catch {
19
- }
20
- this._initFinishCallBackFuns = void 0;
21
- }
22
- t(s);
23
- }, a.onerror = (s) => {
24
- console.error(s), e(s);
25
- }, a.onupgradeneeded = (s) => {
26
- let n = a.result;
27
- n.objectStoreNames.contains(this.#a) || n.createObjectStore(this.#a, {
28
- keyPath: "key"
29
- // 设置主键
30
- }), t(s);
31
- };
32
- }), this._init);
33
- }
34
- async awaitInit() {
35
- this.#t || !this._initFinishCallBackFuns || await new Promise((t, e) => {
36
- this._initFinishCallBackFuns?.push(t);
37
- });
38
- }
39
- closeDB() {
40
- this._db?.close();
41
- }
42
- async store(t) {
43
- return await this.awaitInit(), this._db.transaction(this.#a, t).objectStore(this.#a);
44
- }
45
- /**
46
- * @description : 更新数据
47
- * @param {templateData} params 添加到数据库中的数据 { key: 文件key, data: 文件blob }
48
- * @return {*}
49
- */
50
- putData(t) {
51
- return new Promise((e, a) => {
52
- this.store("readwrite").then((s) => {
53
- const n = s.put(t);
54
- n.onsuccess = (i) => {
55
- e(i);
56
- }, n.onerror = (i) => {
57
- a(i);
58
- };
59
- }).catch(a);
60
- });
61
- }
62
- // 通过主键读取数据
63
- getDataByKey(t) {
64
- return new Promise((e, a) => {
65
- this.store().then((s) => {
66
- const n = s.get(t);
67
- n.onsuccess = () => {
68
- e(n.result);
69
- }, n.onerror = (i) => {
70
- a(i);
71
- };
72
- }).catch(a);
73
- });
74
- }
75
- // 通过主键移除数据
76
- deleteDataByKey(t) {
77
- return new Promise((e, a) => {
78
- this.store().then((s) => {
79
- const n = s.delete(t);
80
- n.onsuccess = () => {
81
- e(n.result);
82
- }, n.onerror = (i) => {
83
- a(i);
84
- };
85
- }).catch(a);
86
- });
87
- }
88
- // 清空数据库数据
89
- clearDB() {
90
- return new Promise((t, e) => {
91
- this.store("readwrite").then((a) => {
92
- const s = a.clear();
93
- s.onsuccess = (n) => {
94
- t(n);
95
- }, s.onerror = (n) => {
96
- e(n);
97
- };
98
- }).catch(e);
99
- });
100
- }
101
- }
102
- new y();
103
- var d = /* @__PURE__ */ ((r) => (r.word = "word", r.excel = "excel", r.unknown = "unknown", r))(d || {});
104
- async function f(r = []) {
105
- const t = [];
106
- for (const a of r)
107
- t.push(a.getBuffer());
108
- await Promise.all(t);
109
- const e = {
110
- decode: {
111
- names: [],
112
- uint8Arrays: []
113
- },
114
- noDecode: {
115
- names: [],
116
- uint8Arrays: []
117
- }
118
- };
119
- for (const a of r)
120
- a.uint8Array && (a.isDecode ? (e.decode.names.push(a.name), e.decode.uint8Arrays.push(a.uint8Array)) : (e.noDecode.names.push(a.name), e.noDecode.uint8Arrays.push(a.uint8Array)));
121
- return e;
122
- }
123
- class _ {
124
- #t = [];
125
- #e;
126
- constructor(t) {
127
- this.#e = t;
128
- }
129
- addTempFile(t) {
130
- this.#t.push(t);
131
- }
132
- clear() {
133
- this.#t.length = 0;
134
- }
135
- //提取单个文件内的所有变量
136
- async extractOneFileVariables(t, e) {
137
- const a = await e.getBuffer();
138
- a && (await e.type() === d.unknown && !e.isDecode || (t[e.name] = await this.#e.extract_one_file_variable_names(
139
- a,
140
- e.isDecode
141
- )));
142
- }
143
- //提取多个文件内的所有变量
144
- async extractVariables(t) {
145
- t || (t = this.#t);
146
- const e = {}, a = [];
147
- for (const s of t)
148
- a.push(this.extractOneFileVariables(e, s));
149
- return await Promise.allSettled(a), e;
150
- }
151
- //提取单个文件内的所有媒体文件
152
- async extractOneFileMedias(t, e) {
153
- const a = await e.getBuffer();
154
- if (!a || await e.type() === d.unknown && !e.isDecode)
155
- return;
156
- const s = await this.#e.extract_one_file_medias(
157
- a,
158
- e.isDecode
159
- );
160
- if (t[e.name] = [], !!Array.isArray(s))
161
- for (const { id: n, data: i } of s)
162
- n && i && t[e.name].push({
163
- id: n,
164
- data: new Uint8Array(i)
165
- });
166
- }
167
- //提取多个文件内的所有媒体文件
168
- async extractMedias(t) {
169
- t || (t = this.#t);
170
- const e = {}, a = [];
171
- for (const s of t)
172
- a.push(this.extractOneFileMedias(e, s));
173
- return await Promise.all(a), e;
174
- }
175
- async handle(t, e, a) {
176
- return [];
177
- }
178
- async handleMultipleParams(t, e, a) {
179
- return [];
180
- }
181
- async sign(t) {
182
- return "";
183
- }
184
- async execute(t, e) {
185
- const { noDecode: a, decode: s } = await f(e ?? this.#t), n = await this.handle(t, a.uint8Arrays, s.uint8Arrays), i = {};
186
- let o = 0;
187
- for (const c of a.names)
188
- i[c] = n[o++] ?? new Uint8Array();
189
- for (const c of s.names)
190
- i[c] = n[o++] ?? new Uint8Array();
191
- return i;
192
- }
193
- async executeMultipleParams(t, e) {
194
- const { noDecode: a, decode: s } = await f(e ?? this.#t), n = await this.handleMultipleParams(t, a.uint8Arrays, s.uint8Arrays), i = Array(t.length);
195
- let o = 0;
196
- for (let c = 0; c < t.length; c++) {
197
- const h = {};
198
- for (const l of a.names) {
199
- const u = n[o++];
200
- u.length && (h[l] = u);
201
- }
202
- for (const l of s.names) {
203
- const u = n[o++];
204
- u.length && (h[l] = u);
205
- }
206
- i[c] = h;
207
- }
208
- return i;
209
- }
210
- async fileEncrypt(t) {
211
- return this.#e.file_encrypt(t);
212
- }
213
- async filesEncrypt(t) {
214
- return this.#e.files_encrypt(t);
215
- }
216
- }
217
- class m {
218
- constructor(t) {
219
- this.awaitInit = t;
220
- }
221
- async add_template(t, e) {
222
- return (await this.awaitInit).add_template(t, e);
223
- }
224
- async add_media(t) {
225
- return (await this.awaitInit).add_media(t);
226
- }
227
- async extract_one_file_variable_names(t, e) {
228
- return (await this.awaitInit).extract_one_file_variable_names(t, e);
229
- }
230
- async extract_variable_names(t, e) {
231
- return (await this.awaitInit).extract_variable_names(t, e);
232
- }
233
- async extract_one_file_medias(t, e) {
234
- return (await this.awaitInit).extract_one_file_medias(t, e);
235
- }
236
- async extract_medias(t, e) {
237
- return (await this.awaitInit).extract_medias(t, e);
238
- }
239
- async file_encrypt(t) {
240
- return (await this.awaitInit).file_encrypt(t);
241
- }
242
- async files_encrypt(t) {
243
- return (await this.awaitInit).files_encrypt(t);
244
- }
245
- }
246
- export {
247
- _ as B,
248
- m as b
249
- };