template-replacement 3.1.0 → 3.2.1

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 d, B as x } from "../base-DCmHytJQ.js";
1
+ import { b as d, B as x } from "../base-Bcdx0Hdl.js";
2
2
  let C;
3
3
  function k(I) {
4
4
  const A = C.__externref_table_alloc();
@@ -1,4 +1,4 @@
1
- import { b as Z, B as T } from "../base-DCmHytJQ.js";
1
+ import { b as Z, B as T } from "../base-Bcdx0Hdl.js";
2
2
  let C;
3
3
  function M(I) {
4
4
  const A = C.__externref_table_alloc();
@@ -0,0 +1,37 @@
1
+ import indexedDBCache from './indexedDBCache'
2
+ import FileSystem, {fileDataType} from '../interface'
3
+
4
+ const db = new indexedDBCache()
5
+
6
+ export default class DbFile implements FileSystem {
7
+ #name: string = ''
8
+
9
+ constructor(name: string){
10
+ this.#name = name
11
+ }
12
+
13
+ async write(data: fileDataType): Promise<boolean> {
14
+ try {
15
+ await db.putData<File>({
16
+ url: this.#name,
17
+ data: new File([data], this.#name),
18
+ })
19
+ return true
20
+ } catch (error) {
21
+ console.error(error)
22
+ return false
23
+ }
24
+ }
25
+
26
+ async read(): Promise<File>{
27
+ const data = await db.getDataByKey<File>(this.#name)
28
+ if (data && data.data) {
29
+ return data.data
30
+ }
31
+ return new File([], this.#name)
32
+ }
33
+
34
+ remove(): Promise<void> {
35
+ return db.deleteDataByKey(this.#name)
36
+ }
37
+ }
@@ -111,6 +111,22 @@ export default class indexedDBCache {
111
111
  })
112
112
  }
113
113
 
114
+ // 通过主键移除数据
115
+ deleteDataByKey<T>(key: string): Promise<void> {
116
+ return new Promise(async (resolve, reject) => {
117
+ // 通过主键读取数据
118
+ const request = (await this.store()).delete(key)
119
+ // 操作成功
120
+ request.onsuccess = () => {
121
+ resolve()
122
+ }
123
+ // 操作失败
124
+ request.onerror = (event) => {
125
+ reject(event)
126
+ }
127
+ })
128
+ }
129
+
114
130
  // 清空数据库数据
115
131
  clearDB(): Promise<any> {
116
132
  return new Promise(async (resolve, reject) => {
@@ -0,0 +1,13 @@
1
+ import FileSystem from './interface'
2
+ import OpfsFile from './opfs'
3
+ import DbFile from './db'
4
+
5
+ export default (name: string): FileSystem => {
6
+ if (navigator?.storage?.getDirectory) {
7
+ return new OpfsFile(name)
8
+ }
9
+ return new DbFile(name)
10
+ }
11
+
12
+
13
+
@@ -0,0 +1,8 @@
1
+
2
+ export type fileDataType = BufferSource | Blob | string
3
+
4
+ export default interface FileSystem {
5
+ write(data: fileDataType): Promise<boolean>
6
+ read(): Promise<File>
7
+ remove(): Promise<void>
8
+ }
@@ -0,0 +1,46 @@
1
+ import FileSystem, {fileDataType} from '../interface'
2
+
3
+ let opfsRoot: Promise<FileSystemDirectoryHandle> | undefined = undefined
4
+
5
+ function getOpfsRoot(): Promise<FileSystemDirectoryHandle> {
6
+ if (!opfsRoot) {
7
+ opfsRoot = navigator.storage.getDirectory()
8
+ }
9
+ return opfsRoot
10
+ }
11
+
12
+ export default class OpfsFile implements FileSystem {
13
+ #name: string = ''
14
+ #handle: FileSystemFileHandle | undefined
15
+
16
+ constructor(name: string){
17
+ this.#name = name
18
+ }
19
+
20
+ async getHandle(): Promise<FileSystemFileHandle> {
21
+ if (!this.#handle) {
22
+ this.#handle = await (await getOpfsRoot()).getFileHandle(this.#name, { create: true })
23
+ }
24
+ return this.#handle
25
+ }
26
+
27
+ async write(data: fileDataType): Promise<boolean> {
28
+ try {
29
+ const writable = await (await this.getHandle()).createWritable()
30
+ await writable.write(data)
31
+ await writable.close()
32
+ return true
33
+ } catch (error) {
34
+ console.error(error)
35
+ return false
36
+ }
37
+ }
38
+
39
+ async read(): Promise<File>{
40
+ return (await this.getHandle()).getFile()
41
+ }
42
+
43
+ async remove(): Promise<void> {
44
+ return (await getOpfsRoot()).removeEntry(this.#name)
45
+ }
46
+ }
package/helper/index.ts CHANGED
@@ -1,4 +1,3 @@
1
- import { nanoid } from 'nanoid'
2
1
  import urlDownloadTask from '../task/urlDownloadTask'
3
2
  import { fileTypeFromBuffer } from 'file-type'
4
3
 
@@ -57,7 +56,7 @@ export async function fileTypeByBuffer(buffer: Uint8Array|ArrayBuffer|Blob): Pro
57
56
  }
58
57
 
59
58
  export function generateId(): string {
60
- return nanoid()
59
+ return crypto.randomUUID()
61
60
  }
62
61
 
63
62
  export type fileArrayBufferData = {
@@ -67,7 +66,6 @@ export type fileArrayBufferData = {
67
66
 
68
67
  export async function filesReaderArrayBuffer(files: File[]): Promise<fileArrayBufferData[]> {
69
68
  const awaits = []
70
- files.forEach
71
69
  for (const file of files) {
72
70
  awaits.push(new Promise(async (resolve, reject) => {
73
71
  try {
package/index.ts CHANGED
@@ -18,4 +18,14 @@ export function WorkerGeneral(concurrency?: number): ReplaceInterface {
18
18
 
19
19
  export function WorkerSign(concurrency?: number): ReplaceInterface {
20
20
  return workerSign(concurrency)
21
+ }
22
+
23
+ type signFun = (data: any) => Promise<string>;
24
+
25
+ export default (concurrency?: number, signFn?: signFun): ReplaceInterface => {
26
+ if (concurrency) {
27
+ return signFn ? workerSign(concurrency) : workerGeneral(concurrency)
28
+ }else {
29
+ return signFn ? sign() : general()
30
+ }
21
31
  }
package/package.json CHANGED
@@ -1,27 +1,25 @@
1
1
  {
2
2
  "name": "template-replacement",
3
3
  "description": "模板文件替换",
4
- "version": "3.1.0",
4
+ "version": "3.2.1",
5
5
  "author": "fushiliang <994301536@qq.com>",
6
- "license": "ISC",
7
6
  "type": "module",
8
7
  "main": "index.ts",
9
8
  "scripts": {
10
9
  "build": "npx vite build --config vite.config.ts"
11
10
  },
12
11
  "dependencies": {
13
- "@types/crypto-js": "^4.2.2",
14
12
  "@types/streamsaver": "^2.0.5",
15
13
  "axios": "^1.10.0",
16
14
  "fflate": "^0.8.2",
17
15
  "file-type": "^19.6.0",
18
- "nanoid": "^4.0.2",
19
16
  "streamsaver": "^2.0.6",
20
17
  "template-replacement-core-wasm": "^1.1.1",
21
18
  "template-replacement-sign-core-wasm": "^1.1.1",
22
19
  "vite-plugin-wasm-pack": "^0.1.12"
23
20
  },
24
21
  "devDependencies": {
22
+ "terser": "^5.43.1",
25
23
  "typescript": "^5.8.3",
26
24
  "vite": "^6.3.5",
27
25
  "vite-plugin-dts": "^4.5.4"
@@ -0,0 +1,2 @@
1
+ onlyBuiltDependencies:
2
+ - esbuild
@@ -1,5 +1,16 @@
1
1
  import axios, { AxiosProgressEvent } from "axios"
2
- import db from "../db/index"
2
+ import file from "../fileSystem"
3
+
4
+ async function hashString(str: string): Promise<string> {
5
+ const encoder = new TextEncoder();
6
+ const data = encoder.encode(str);
7
+ const hashBuffer = await window.crypto.subtle.digest("SHA-1", data);
8
+ const hashArray = Array.from(new Uint8Array(hashBuffer));
9
+ const hashHex = hashArray
10
+ .map((b) => b.toString(16).padStart(2, "0"))
11
+ .join("");
12
+ return hashHex;
13
+ }
3
14
 
4
15
  export default class urlDownloadTask {
5
16
  urls: string[]
@@ -21,18 +32,17 @@ export default class urlDownloadTask {
21
32
  }
22
33
 
23
34
  async getUrlData(url: string): Promise<Blob|undefined> {
24
- const data = await db.getDataByKey<Blob>(url)
25
- if (data && data.data) {
26
- return data.data
35
+ const hash = await hashString(url)
36
+ const fileObj = file(hash)
37
+ const data = await fileObj.read()
38
+ if (data.size) {
39
+ return data
27
40
  }
28
41
  const getData = await this.download(url)
29
42
  if (!getData) {
30
43
  return undefined
31
44
  }
32
- db.putData<Blob>({
33
- url: url,
34
- data: getData,
35
- })
45
+ fileObj.write(getData)
36
46
  return getData
37
47
  }
38
48
 
package/vite.config.ts CHANGED
@@ -20,7 +20,7 @@ export default defineConfig({
20
20
  },
21
21
  rollupOptions: {
22
22
  output: {
23
- entryFileNames: info => {
23
+ entryFileNames: (info: { facadeModuleId: string; }) => {
24
24
  const modules = info.facadeModuleId?.split('/')
25
25
  if (!modules?.length || modules?.length < 2) {
26
26
  return '[name].js'
package/db/index.ts DELETED
@@ -1,5 +0,0 @@
1
- import indexedDBCache from './indexedDBCache'
2
-
3
- const db = new indexedDBCache()
4
-
5
- export default db
@@ -1,214 +0,0 @@
1
- var m = (i) => {
2
- throw TypeError(i);
3
- };
4
- var f = (i, e, t) => e.has(i) || m("Cannot " + t);
5
- var c = (i, e, t) => (f(i, e, "read from private field"), t ? t.call(i) : e.get(i)), _ = (i, e, t) => e.has(i) ? m("Cannot add the same private member more than once") : e instanceof WeakSet ? e.add(i) : e.set(i, t), y = (i, e, t, s) => (f(i, e, "write to private field"), s ? s.call(i, t) : e.set(i, t), t);
6
- class p {
7
- //表配置
8
- // 构造函数
9
- constructor() {
10
- this._initFinishCallBackFuns = [], this._isInitFinish = !1, this._dbName = "template_replacement", this._dbversion = 1, this._cacheTableName = "templates", this._tableMap = {}, this.initDB();
11
- }
12
- initDB() {
13
- return new Promise((e, t) => {
14
- const s = indexedDB.open(this._dbName, this._dbversion);
15
- s.onsuccess = (a) => {
16
- if (this._db = s.result, this._isInitFinish = !0, this._initFinishCallBackFuns) {
17
- try {
18
- for (const n of this._initFinishCallBackFuns)
19
- n();
20
- } catch {
21
- }
22
- this._initFinishCallBackFuns = void 0;
23
- }
24
- e(a);
25
- }, s.onerror = (a) => {
26
- console.error(a), t(a);
27
- }, s.onupgradeneeded = (a) => {
28
- let n = s.result;
29
- n.objectStoreNames.contains(this._cacheTableName) || n.createObjectStore(this._cacheTableName, {
30
- keyPath: "url"
31
- // 设置主键
32
- }), e(a);
33
- };
34
- });
35
- }
36
- async awaitInit() {
37
- this._isInitFinish || !this._initFinishCallBackFuns || await new Promise((e, t) => {
38
- var s;
39
- (s = this._initFinishCallBackFuns) == null || s.push(e);
40
- });
41
- }
42
- closeDB() {
43
- var e;
44
- (e = this._db) == null || e.close();
45
- }
46
- async store(e) {
47
- return await this.awaitInit(), this._db.transaction(this._cacheTableName, e).objectStore(this._cacheTableName);
48
- }
49
- /**
50
- * @description : 更新数据
51
- * @param {Object} params 添加到数据库中的数据 { url: 文件地址, data: 文件blob }
52
- * @return {*}
53
- */
54
- putData(e) {
55
- return new Promise(async (t, s) => {
56
- const a = (await this.store("readwrite")).put(e);
57
- a.onsuccess = (n) => {
58
- t(n);
59
- }, a.onerror = (n) => {
60
- s(n);
61
- };
62
- });
63
- }
64
- // 通过主键读取数据
65
- getDataByKey(e) {
66
- return new Promise(async (t, s) => {
67
- const a = (await this.store()).get(e);
68
- a.onsuccess = () => {
69
- t(a.result);
70
- }, a.onerror = (n) => {
71
- s(n);
72
- };
73
- });
74
- }
75
- // 清空数据库数据
76
- clearDB() {
77
- return new Promise(async (e, t) => {
78
- const s = (await this.store("readwrite")).clear();
79
- s.onsuccess = (a) => {
80
- e(a);
81
- }, s.onerror = (a) => {
82
- t(a);
83
- };
84
- });
85
- }
86
- }
87
- new p();
88
- var w = /* @__PURE__ */ ((i) => (i.word = "word", i.excel = "excel", i.unknown = "unknown", i))(w || {}), h, u;
89
- class x {
90
- constructor(e) {
91
- _(this, h, []);
92
- _(this, u);
93
- y(this, u, e);
94
- }
95
- addTempFile(e) {
96
- c(this, h).push(e);
97
- }
98
- clear() {
99
- c(this, h).length = 0;
100
- }
101
- async extractVariables(e) {
102
- e || (e = c(this, h));
103
- const t = {}, s = [];
104
- for (const a of e)
105
- s.push(new Promise(async (n, r) => {
106
- const o = await a.getBuffer();
107
- o && (a.isDecode || await a.type() !== w.unknown) && (t[a.name] = await c(this, u).extract_one_file_variable_names(o, a.isDecode)), n();
108
- }));
109
- return await Promise.all(s), t;
110
- }
111
- async extractMedias(e) {
112
- e || (e = c(this, h));
113
- const t = {}, s = [];
114
- for (const a of e)
115
- s.push(new Promise(async (n, r) => {
116
- const o = await a.getBuffer();
117
- if (o && (a.isDecode || await a.type() !== w.unknown)) {
118
- let l = await c(this, u).extract_one_file_medias(o, a.isDecode);
119
- t[a.name] = [], l && Array.isArray(l) && l.forEach((d) => {
120
- d.id && d.data && t[a.name].push({
121
- id: d.id,
122
- data: new Uint8Array(d.data)
123
- });
124
- });
125
- }
126
- n();
127
- }));
128
- return await Promise.all(s), t;
129
- }
130
- async handle(e, t, s = !1) {
131
- return [];
132
- }
133
- async sign(e) {
134
- return "";
135
- }
136
- async execute(e, t) {
137
- t || (t = c(this, h));
138
- const s = [];
139
- for (const r of t)
140
- s.push(r.getBuffer());
141
- await Promise.all(s);
142
- const a = {
143
- //需要解密的文件
144
- decode: {
145
- names: [],
146
- uint8Arrays: []
147
- },
148
- //不需要解密的文件
149
- noDecode: {
150
- names: [],
151
- uint8Arrays: []
152
- }
153
- };
154
- for (const r of t)
155
- r.uint8Array && (r.isDecode ? (a.decode.names.push(r.name), a.decode.uint8Arrays.push(r.uint8Array)) : (a.noDecode.names.push(r.name), a.noDecode.uint8Arrays.push(r.uint8Array)));
156
- const n = await Promise.all([
157
- this._execute(e, a.noDecode.names, a.noDecode.uint8Arrays, !1),
158
- this._execute(e, a.decode.names, a.decode.uint8Arrays, !0)
159
- ]);
160
- return {
161
- ...n[0],
162
- ...n[1]
163
- };
164
- }
165
- async _execute(e, t, s, a = !1) {
166
- const n = {};
167
- return s.length && (await this.handle(e, s, a)).forEach((o, l) => {
168
- o.length && (n[t[l]] = o);
169
- }), n;
170
- }
171
- async fileEncrypt(e) {
172
- return await c(this, u).file_encrypt(e);
173
- }
174
- async filesEncrypt(e) {
175
- return await c(this, u).files_encrypt(e);
176
- }
177
- }
178
- h = new WeakMap(), u = new WeakMap();
179
- class D {
180
- constructor(e, t) {
181
- this.awaitInit = e, this.module = t;
182
- }
183
- async await() {
184
- return await this.awaitInit, this;
185
- }
186
- async add_template(e, t) {
187
- return await this.awaitInit, this.module.add_template(e, t);
188
- }
189
- async add_media(e) {
190
- return await this.awaitInit, this.module.add_media(e);
191
- }
192
- async extract_one_file_variable_names(e, t) {
193
- return await this.awaitInit, this.module.extract_one_file_variable_names(e, t);
194
- }
195
- async extract_variable_names(e, t) {
196
- return await this.awaitInit, this.module.extract_variable_names(e, t);
197
- }
198
- async extract_one_file_medias(e, t) {
199
- return await this.awaitInit, this.module.extract_one_file_medias(e, t);
200
- }
201
- async extract_medias(e, t) {
202
- return await this.awaitInit, this.module.extract_medias(e, t);
203
- }
204
- async file_encrypt(e) {
205
- return await this.awaitInit, this.module.file_encrypt(e);
206
- }
207
- async files_encrypt(e) {
208
- return await this.awaitInit, this.module.files_encrypt(e);
209
- }
210
- }
211
- export {
212
- x as B,
213
- D as b
214
- };
@@ -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
- };