template-replacement 3.3.2 → 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 +1373 -1401
  15. package/dist/main/sign.js +1559 -1573
  16. package/dist/replace/general.js +281 -306
  17. package/dist/replace/sign.js +304 -315
  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 +142 -124
  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 +144 -116
  27. package/index.ts +9 -17
  28. package/office/zip.ts +106 -97
  29. package/package.json +17 -11
  30. package/replace/base.ts +203 -214
  31. package/replace/general.ts +44 -19
  32. package/replace/image.ts +88 -86
  33. package/replace/interface.ts +29 -24
  34. package/replace/paramsData.ts +108 -96
  35. package/replace/sign.ts +79 -43
  36. package/task/urlDownloadTask.ts +54 -63
  37. package/temp/index.ts +139 -131
  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 -51
  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-BuKBOMgk.js +0 -269
  54. package/dist/general.d.ts +0 -1
  55. package/dist/index-tFDVIkZX.js +0 -46
  56. package/dist/sign.d.ts +0 -1
@@ -1,77 +1,68 @@
1
1
  import axios, { AxiosProgressEvent } from "axios"
2
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
+ import { hashString } from "../helper"
14
4
 
15
5
  export default class urlDownloadTask {
16
- urls: string[]
17
- downloadProgressListener:((progressEvent: AxiosProgressEvent) => void)[] = []
6
+ urls: string[]
7
+ #downloadProgressListener: ((progressEvent: AxiosProgressEvent) => void)[] = []
18
8
 
19
- constructor(urls: string[]) {
20
- if (urls.constructor !== Array) {
21
- throw new Error("不是可用的链接数组数据")
22
- }
23
- this.urls = urls
9
+ constructor(urls: string[]) {
10
+ if (urls.constructor !== Array) {
11
+ throw new Error('不是可用的链接数组数据')
24
12
  }
13
+ this.urls = urls
14
+ }
25
15
 
26
- async start(): Promise<(Blob|undefined)[]> {
27
- const tasks = []
28
- for (const url of this.urls) {
29
- tasks.push(this.getUrlData(url))
30
- }
31
- return Promise.all(tasks)
16
+ start(): Promise<(Blob | undefined)[]> {
17
+ const tasks = []
18
+ for (const url of this.urls) {
19
+ tasks.push(this.getUrlData(url))
32
20
  }
21
+ return Promise.all(tasks)
22
+ }
33
23
 
34
- async getUrlData(url: string): Promise<Blob|undefined> {
35
- const hash = await hashString(url)
36
- const fileObj = file(hash)
37
- const data = await fileObj.read()
38
- if (data.size) {
39
- return data
40
- }
41
- const getData = await this.download(url)
42
- if (!getData) {
43
- return undefined
44
- }
45
- fileObj.write(getData)
46
- return getData
24
+ async getUrlData(url: string): Promise<Blob | undefined> {
25
+ const hash = await hashString(url)
26
+ const fileObj = file(hash)
27
+ const data = await fileObj.read()
28
+ if (data.size) {
29
+ return data
47
30
  }
31
+ const getData = await this.download(url)
32
+ if (!getData) {
33
+ return undefined
34
+ }
35
+ fileObj.write(getData)
36
+ return getData
37
+ }
48
38
 
49
- async download(url: string): Promise<Blob> {
50
- const response = await axios({
51
- url: url,
52
- method: 'get',
53
- responseType: 'blob',
54
- onDownloadProgress: (progressEvent: AxiosProgressEvent) => {
55
- for (const fun of this.downloadProgressListener) {
56
- fun(progressEvent)
57
- }
58
- }
59
- })
60
-
61
- const contentDisposition = response.headers['content-disposition']
62
- // 解析文件名
63
- if (contentDisposition) {
64
- const matches = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/.exec(contentDisposition)
65
- if (matches != null && matches[1]) {
66
- const filename = matches[1].replace(/['"]/g, '')
67
- const contentType = response.headers['content-type'] ?? 'application/octet-stream'
68
- return new File([response.data], filename, { type: contentType })
69
- }
39
+ async download(url: string): Promise<Blob> {
40
+ const response = await axios.get(url, {
41
+ responseType: 'blob',
42
+ onDownloadProgress: (progressEvent: AxiosProgressEvent) => {
43
+ for (const fun of this.#downloadProgressListener) {
44
+ fun(progressEvent)
70
45
  }
71
- return response.data
72
- }
46
+ },
47
+ })
73
48
 
74
- onDownloadProgress(listen: (progressEvent: AxiosProgressEvent) => void) {
75
- this.downloadProgressListener.push(listen)
49
+ const contentDisposition = response.headers['content-disposition']
50
+ // 解析文件名
51
+ if (contentDisposition) {
52
+ const matches = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/.exec(
53
+ contentDisposition,
54
+ )
55
+ if (matches != null && matches[1]) {
56
+ const filename = matches[1].replace(/['"]/g, '')
57
+ return new File([response.data], filename, {
58
+ type: response.headers['content-type'] ?? 'application/octet-stream',
59
+ })
60
+ }
76
61
  }
77
- }
62
+ return response.data
63
+ }
64
+
65
+ onDownloadProgress(listen: (progressEvent: AxiosProgressEvent) => void) {
66
+ this.#downloadProgressListener.push(listen)
67
+ }
68
+ }
package/temp/index.ts CHANGED
@@ -1,163 +1,171 @@
1
- import { fileTypeByBuffer, fileTypes, getFileNameFromUrl, urlsToFileBlobs } from '../helper'
1
+ import {
2
+ fileTypeByBuffer,
3
+ fileTypes,
4
+ getFileNameFromUrl,
5
+ urlsToFileBlobs,
6
+ } from '../helper'
2
7
  import { TempInterface } from './interface'
3
8
 
4
9
  export type TempImageInfo = {
5
- hash: string,
6
- blob: Blob,
7
- path: string
10
+ hash: string
11
+ blob: Blob
12
+ path: string
8
13
  }
9
14
 
10
15
  export enum status {
11
- waitLoad, //文件待加载
12
- loaded, //文件已加载
13
- replaceFinish, //完成替换
14
- replaceFail, //替换失败
15
- loadFail, //文件加载失败
16
+ waitLoad, //文件待加载
17
+ loaded, //文件已加载
18
+ replaceFinish, //完成替换
19
+ replaceFail, //替换失败
20
+ loadFail, //文件加载失败
16
21
  }
17
22
 
18
23
  //传递的文件信息
19
24
  export type transmitFileInfo = {
20
- name: string,
21
- uint8Array: Uint8Array,
22
- isDecode: boolean,
25
+ name: string
26
+ uint8Array: Uint8Array
27
+ isDecode: boolean
23
28
  }
24
29
 
25
30
  //将传递的文件信息转为模板对象
26
- export function transmitFileInfoToTemp(data: transmitFileInfo) {
27
- if (!data.uint8Array || !data.name) {
28
- throw new Error("模板文件信息错误")
29
- }
30
- const temp = new Temp(undefined, undefined, data.uint8Array, data.name)
31
- temp.isDecode = data.isDecode
32
- return temp
31
+ export function transmitFileInfoToTemp(data: transmitFileInfo): Temp {
32
+ if (!data.uint8Array || !data.name) {
33
+ throw new Error('模板文件信息错误')
34
+ }
35
+ const temp = new Temp(undefined, undefined, data.uint8Array, data.name)
36
+ temp.isDecode = data.isDecode
37
+ return temp
33
38
  }
34
39
 
35
- export default class Temp implements TempInterface{
36
- name: string = ''
37
- blob?: File|Blob
38
- uint8Array?: Uint8Array
39
- url?: string
40
- status = status.waitLoad // 0文件待加载,1文件已加载,2完成替换,3替换失败
41
- isDecode: boolean = false //文件是否被加密
42
-
43
- _output?: File|Blob
44
- _type?: fileTypes
45
-
46
- tempImages: Record<string, TempImageInfo> = {}
47
-
48
- constructor(file?: File|Blob, url?: string, uint8Array?: Uint8Array, name?: string) {
49
- if (uint8Array) {
50
- this.uint8Array = uint8Array
51
- this.setStatus(status.loaded)
52
- }else if (file) {
53
- this.blob = file
54
- this.setStatus(status.loaded)
55
- }else if (url) {
56
- this.url = url
57
- }
58
- if (name) {
59
- this.name = name
60
- }else{
61
- this.name = (file as File)?.name ?? ''
62
- }
63
-
64
- if (!url && !file && !uint8Array) {
65
- throw new Error("缺少文件数据或文件链接")
66
- }
40
+ export default class Temp implements TempInterface {
41
+ name: string = ''
42
+ blob?: File | Blob
43
+ uint8Array?: Uint8Array
44
+ url?: string
45
+ status = status.waitLoad // 0文件待加载,1文件已加载,2完成替换,3替换失败
46
+ isDecode: boolean = false //文件是否需要解密
47
+
48
+ tempImages: Record<string, TempImageInfo> = {}
49
+
50
+ private _output?: File | Blob = undefined
51
+ private _type?: fileTypes = undefined
52
+
53
+ constructor(
54
+ file?: File | Blob,
55
+ url?: string,
56
+ uint8Array?: Uint8Array,
57
+ name?: string,
58
+ ) {
59
+ if (uint8Array) {
60
+ this.uint8Array = uint8Array
61
+ this.setStatus(status.loaded)
62
+ } else if (file) {
63
+ this.blob = file
64
+ this.setStatus(status.loaded)
65
+ } else if (url) {
66
+ this.url = url
67
67
  }
68
-
69
- getName() {
70
- if (this.name) {
71
- return this.name
72
- }
73
- if ((this.blob as File)?.name) {
74
- this.name = (this.blob as File).name
75
- return this.name
76
- }
77
- return this.url ? getFileNameFromUrl(this.url) : ''
68
+ if (name) {
69
+ this.name = name
70
+ } else {
71
+ this.name = (file as File)?.name ?? ''
78
72
  }
79
73
 
80
- async type(): Promise<fileTypes> {
81
- if (this._type) {
82
- return this._type
83
- }
84
- if (this.uint8Array) {
85
- this._type = await fileTypeByBuffer(this.uint8Array)
86
- }else{
87
- const file = await this.getBlob()
88
- if (file) {
89
- this._type = await fileTypeByBuffer(file)
90
- }else{
91
- this._type = fileTypes.unknown
92
- }
93
- }
94
- return this._type
74
+ if (!url && !file && !uint8Array) {
75
+ throw new Error('缺少文件数据或文件链接')
95
76
  }
77
+ }
96
78
 
97
- async getBuffer(): Promise<Uint8Array|undefined>{
98
- if (this.uint8Array) {
99
- return this.uint8Array
100
- }
101
- const blob = await this.getBlob()
102
- if (blob) {
103
- this.uint8Array = new Uint8Array(await blob.arrayBuffer())
104
- }
105
-
106
- return this.uint8Array
79
+ getName() {
80
+ if (this.name) {
81
+ return this.name
107
82
  }
108
-
109
- async getBlob(): Promise<Blob|undefined> {
110
- if (this.blob) {
111
- return this.blob
112
- }
113
- if (!this.blob) {
114
- if (this.uint8Array) {
115
- this.blob = new Blob([ this.uint8Array as BlobPart ])
116
- }else if (this.url) {
117
- const [ blob ] = await urlsToFileBlobs([ this.url ])
118
- if (blob) {
119
- this.blob = blob
120
- }
121
- }
122
- }
123
- if ((this.status === status.waitLoad || this.status === status.loadFail) && this.blob) {
124
- this.setStatus(status.loaded)
125
- }
126
- if (!this.blob) {
127
- this.setStatus(status.loadFail)
128
- }
129
- return this.blob
83
+ if ((this.blob as File)?.name) {
84
+ this.name = (this.blob as File).name
85
+ return this.name
130
86
  }
87
+ return this.url ? getFileNameFromUrl(this.url) : ''
88
+ }
131
89
 
132
- setStatus(status: status): void {
133
- this.status = status
90
+ async type(): Promise<fileTypes> {
91
+ if (this._type) {
92
+ return this._type
134
93
  }
135
-
136
- setOutputFile(file: File|Blob): void {
137
- this._output = file
94
+ const buffer = await this.getBuffer()
95
+ if (buffer) {
96
+ this._type = await fileTypeByBuffer(buffer)
97
+ } else {
98
+ this._type = fileTypes.unknown
138
99
  }
100
+ return this._type
101
+ }
139
102
 
140
- setTempImages(images: Record<string, TempImageInfo>): void {
141
- this.tempImages = images
103
+ async getBuffer(): Promise<Uint8Array | undefined> {
104
+ if (this.uint8Array) {
105
+ return this.uint8Array
142
106
  }
143
-
144
- outputFile(): File|Blob|undefined {
145
- return this._output ?? this.blob
107
+ const blob = await this.getBlob()
108
+ if (blob) {
109
+ this.uint8Array = new Uint8Array(await blob.arrayBuffer())
146
110
  }
147
111
 
148
- async getTransmitFileInfo(): Promise<transmitFileInfo|undefined> {
149
- let uint8Array = this.uint8Array
150
- this.uint8Array = undefined
151
- if (!uint8Array) {
152
- uint8Array = await this.getBuffer()
153
- }
154
- if (!uint8Array) {
155
- return undefined
156
- }
157
- return {
158
- name: this.getName(),
159
- uint8Array: new Uint8Array(uint8Array.buffer.slice(0)),
160
- isDecode: this.isDecode,
112
+ return this.uint8Array
113
+ }
114
+
115
+ async getBlob(): Promise<Blob | undefined> {
116
+ if (this.blob) {
117
+ return this.blob
118
+ }
119
+ if (!this.blob) {
120
+ if (this.uint8Array) {
121
+ this.blob = new Blob([this.uint8Array as BlobPart])
122
+ } else if (this.url) {
123
+ const [blob] = await urlsToFileBlobs([this.url])
124
+ if (blob) {
125
+ this.blob = blob
161
126
  }
127
+ }
128
+ }
129
+ if (
130
+ (this.status === status.waitLoad || this.status === status.loadFail) &&
131
+ this.blob
132
+ ) {
133
+ this.setStatus(status.loaded)
134
+ }
135
+ if (!this.blob) {
136
+ this.setStatus(status.loadFail)
137
+ }
138
+ return this.blob
139
+ }
140
+
141
+ setStatus(status: status): void {
142
+ this.status = status
143
+ }
144
+
145
+ setOutputFile(file: File | Blob): void {
146
+ this._output = file
147
+ }
148
+
149
+ setTempImages(images: Record<string, TempImageInfo>): void {
150
+ this.tempImages = images
151
+ }
152
+
153
+ outputFile(): File | Blob | undefined {
154
+ return this._output ?? this.blob
155
+ }
156
+
157
+ async getTransmitFileInfo(): Promise<transmitFileInfo | undefined> {
158
+ const uint8Array = this.uint8Array
159
+ ? this.uint8Array
160
+ : await this.getBuffer()
161
+ if (!uint8Array) {
162
+ return undefined
163
+ }
164
+ this.uint8Array = undefined
165
+ return {
166
+ name: this.getName(),
167
+ uint8Array,
168
+ isDecode: this.isDecode,
162
169
  }
170
+ }
163
171
  }
package/temp/interface.ts CHANGED
@@ -2,17 +2,17 @@ import { status, TempImageInfo } from '.'
2
2
  import { fileTypes } from '../helper'
3
3
 
4
4
  export interface TempInterface {
5
- type(): Promise<fileTypes>
5
+ type(): Promise<fileTypes>
6
6
 
7
- getBuffer(): Promise<Uint8Array|undefined>
7
+ getBuffer(): Promise<Uint8Array | undefined>
8
8
 
9
- getBlob(): Promise<Blob|undefined>
9
+ getBlob(): Promise<Blob | undefined>
10
10
 
11
- setStatus(status: status): void
11
+ setStatus(status: status): void
12
12
 
13
- setOutputFile(file: File|Blob): void
13
+ setOutputFile(file: File | Blob): void
14
14
 
15
- setTempImages(images: Record<string, TempImageInfo>): void
15
+ setTempImages(images: Record<string, TempImageInfo>): void
16
16
 
17
- outputFile(): File|Blob|undefined
18
- }
17
+ outputFile(): File | Blob | undefined
18
+ }
package/tsconfig.json CHANGED
@@ -27,7 +27,7 @@
27
27
  /* Modules */
28
28
  "module": "es2022", /* Specify what module code is generated. */
29
29
  // "rootDir": "./", /* Specify the root folder within your source files. */
30
- "moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */
30
+ // "moduleResolution": "node16", /* Specify how TypeScript looks up a file from a given module specifier. */
31
31
  // "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
32
32
  // "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
33
33
  // "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
package/vite.config.ts CHANGED
@@ -1,26 +1,23 @@
1
- import { defineConfig } from 'vite';
2
- import wasmPack from 'vite-plugin-wasm-pack';
3
- import dts from 'vite-plugin-dts';
1
+ import { defineConfig } from 'vite'
4
2
 
5
3
  export default defineConfig({
6
- plugins: [
7
- wasmPack([], ['template-replacement-sign-core-wasm','template-replacement-core-wasm']),
8
- dts({
9
- entryRoot: './dist',
10
- insertTypesEntry: true,
11
- }),
12
- ],
13
4
  build: {
5
+ target: 'esnext',
14
6
  outDir: './dist',
15
7
  lib: {
16
- entry: ['./worker/main/sign.ts', './worker/main/general.ts', './replace/general.ts', './replace/sign.ts'],
8
+ entry: [
9
+ './worker/main/sign.ts',
10
+ './worker/main/general.ts',
11
+ './replace/general.ts',
12
+ './replace/sign.ts',
13
+ ],
17
14
  name: 'template-replacement',
18
15
  fileName: (format, entryName) => `${entryName}.js`,
19
16
  formats: ['es'],
20
17
  },
21
18
  rollupOptions: {
22
19
  output: {
23
- entryFileNames: (info: { facadeModuleId: string; }) => {
20
+ entryFileNames: (info) => {
24
21
  const modules = info.facadeModuleId?.split('/')
25
22
  if (!modules?.length || modules?.length < 2) {
26
23
  return '[name].js'
@@ -32,5 +29,5 @@ export default defineConfig({
32
29
  },
33
30
  worker: {
34
31
  format: 'es',
35
- }
36
- })
32
+ },
33
+ })