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.
- package/.editorconfig +8 -0
- package/.oxfmtrc.jsonc +7 -0
- package/.oxlintrc.json +3 -0
- package/README.md +39 -9
- package/core/base.ts +54 -55
- package/core/general.ts +37 -9
- package/core/sign.ts +43 -8
- package/dispatcher/general.ts +2 -1
- package/dispatcher/sign.ts +2 -1
- package/dispatcher/workerGeneral.ts +1 -1
- package/dispatcher/workerSign.ts +1 -1
- package/dist/base-DQz39fXI.js +249 -0
- package/dist/index-oILo_kXG.js +46 -0
- package/dist/main/general.js +1334 -1384
- package/dist/main/sign.js +1476 -1514
- package/dist/replace/general.js +283 -313
- package/dist/replace/sign.js +309 -327
- package/download/index.ts +13 -13
- package/download/stream.ts +29 -28
- package/eslint.config.ts +28 -0
- package/fileSystem/db/index.ts +25 -25
- package/fileSystem/db/indexedDBCache.ts +145 -143
- package/fileSystem/index.ts +5 -8
- package/fileSystem/interface.ts +4 -5
- package/fileSystem/opfs/index.ts +40 -36
- package/helper/index.ts +136 -125
- package/index.ts +6 -6
- package/office/zip.ts +106 -97
- package/package.json +14 -8
- package/replace/base.ts +203 -222
- package/replace/general.ts +44 -24
- package/replace/image.ts +88 -90
- package/replace/interface.ts +29 -24
- package/replace/paramsData.ts +107 -95
- package/replace/sign.ts +79 -52
- package/task/urlDownloadTask.ts +53 -55
- package/temp/index.ts +139 -124
- package/temp/interface.ts +8 -8
- package/tsconfig.json +1 -1
- package/vite.config.ts +11 -14
- package/worker/child/agency.ts +49 -41
- package/worker/child/base.ts +125 -89
- package/worker/child/general.ts +2 -3
- package/worker/child/sign.ts +4 -4
- package/worker/index.ts +52 -53
- package/worker/interface.ts +9 -6
- package/worker/main/general.ts +5 -5
- package/worker/main/index.ts +191 -66
- package/worker/main/sign.ts +5 -5
- package/worker/type.ts +16 -15
- 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-CJv023nf.js +0 -284
- package/dist/general.d.ts +0 -1
- package/dist/index-tFDVIkZX.js +0 -46
- package/dist/sign.d.ts +0 -1
package/worker/child/base.ts
CHANGED
|
@@ -1,114 +1,150 @@
|
|
|
1
1
|
import agency from './agency'
|
|
2
|
-
import { messageData, messageTypes, methodCall } from '../type'
|
|
3
|
-
import ReplaceInterface, { media } from '../../replace/interface'
|
|
4
|
-
import { generateId } from '../../helper'
|
|
2
|
+
import { messageData, messageTypes, methodCall, methodCallReply } from '../type'
|
|
3
|
+
import ReplaceInterface, { media } from '../../replace/interface'
|
|
4
|
+
import { generateId } from '../../helper'
|
|
5
5
|
|
|
6
6
|
type methodKeys<T> = {
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
|
|
8
|
+
[K in keyof T]: T[K] extends Function ? K : never
|
|
9
|
+
}[keyof T]
|
|
9
10
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
extractVariables: true,
|
|
13
|
-
extractMedias: true,
|
|
14
|
-
execute: true,
|
|
15
|
-
filesEncrypt: true,
|
|
16
|
-
fileEncrypt: true,
|
|
17
|
-
executeMultipleParams: true,
|
|
18
|
-
}
|
|
11
|
+
//允许调用的方法
|
|
12
|
+
const allowCallMethodNames: Set<methodKeys<ReplaceInterface>> = new Set(["addTempFile", "extractVariables", "extractMedias", "execute", "filesEncrypt", "fileEncrypt", "executeMultipleParams"])
|
|
19
13
|
|
|
20
|
-
|
|
14
|
+
//处理方法调用的返回值
|
|
15
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
16
|
+
const resultHandles = new Map<methodKeys<ReplaceInterface>, (result: any, transfer: Transferable[]) => Transferable[]>([
|
|
17
|
+
['execute', (result: Record<string, Uint8Array>, transfer: Transferable[] = []) => {
|
|
18
|
+
for (const key in result) {
|
|
19
|
+
const value = result[key]
|
|
20
|
+
if (value?.length) {
|
|
21
|
+
transfer.push(value.buffer)
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
return transfer
|
|
25
|
+
}],
|
|
26
|
+
['executeMultipleParams', (result: Record<string, Uint8Array>[], transfer: Transferable[] = []) => {
|
|
27
|
+
for (const map of result) {
|
|
28
|
+
for (const key in map) {
|
|
29
|
+
const value = map[key]
|
|
30
|
+
if (value?.length) {
|
|
31
|
+
transfer.push(value.buffer)
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return transfer
|
|
36
|
+
}],
|
|
37
|
+
['extractMedias', (result: Record<string, media[]>, transfer: Transferable[] = []) => {
|
|
38
|
+
for (const key in result) {
|
|
39
|
+
const medias = result[key]
|
|
40
|
+
for (const media of medias) {
|
|
41
|
+
if (media.data?.length) {
|
|
42
|
+
transfer.push(media.data.buffer)
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return transfer
|
|
47
|
+
}],
|
|
48
|
+
['fileEncrypt', (result: Uint8Array, transfer: Transferable[] = []) => {
|
|
49
|
+
if (result?.length) {
|
|
50
|
+
transfer.push(result.buffer)
|
|
51
|
+
}
|
|
52
|
+
return transfer
|
|
53
|
+
}],
|
|
54
|
+
['filesEncrypt', (result: Uint8Array[], transfer: Transferable[] = []) => {
|
|
55
|
+
for (const item of result) {
|
|
56
|
+
if (item?.length) {
|
|
57
|
+
transfer.push(item.buffer)
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return transfer
|
|
61
|
+
}],
|
|
62
|
+
])
|
|
63
|
+
|
|
64
|
+
const tasks = new Map<string, (value: unknown) => void>()
|
|
21
65
|
|
|
22
66
|
let dispatch: ReplaceInterface
|
|
23
67
|
|
|
24
68
|
export default function _init(replace: ReplaceInterface) {
|
|
25
|
-
|
|
69
|
+
dispatch = new agency(replace)
|
|
26
70
|
}
|
|
27
71
|
|
|
28
|
-
export
|
|
72
|
+
export function call<T>(method: string, ...params: unknown[]): Promise<T> {
|
|
29
73
|
const replyId = generateId()
|
|
30
74
|
postMessage({
|
|
31
75
|
type: messageTypes.methodCall,
|
|
32
76
|
data: {
|
|
33
77
|
replyId,
|
|
34
78
|
method: method,
|
|
35
|
-
params: params
|
|
36
|
-
}
|
|
79
|
+
params: params,
|
|
80
|
+
},
|
|
37
81
|
})
|
|
38
82
|
|
|
39
83
|
return new Promise<T>((resolve, reject) => {
|
|
40
|
-
tasks.set(replyId, resolve)
|
|
84
|
+
tasks.set(replyId, resolve as (value: unknown) => void)
|
|
41
85
|
})
|
|
42
86
|
}
|
|
43
87
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
88
|
+
addEventListener('message', async (event) => {
|
|
89
|
+
const data = event.data as messageData
|
|
90
|
+
if (!data?.data) {
|
|
91
|
+
return
|
|
92
|
+
}
|
|
93
|
+
switch (data.type) {
|
|
94
|
+
case messageTypes.methodCall:
|
|
95
|
+
// 调用方法
|
|
96
|
+
const callData = data.data as methodCall<methodKeys<ReplaceInterface>>
|
|
97
|
+
const method = callData.method
|
|
98
|
+
if (!allowCallMethodNames.has(method)) {
|
|
99
|
+
//跳过不允许被调用的方法
|
|
100
|
+
return
|
|
101
|
+
}
|
|
102
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
|
|
103
|
+
const fun = dispatch[method] as Function | undefined
|
|
104
|
+
if (!fun) {
|
|
105
|
+
return
|
|
106
|
+
}
|
|
107
|
+
let result: unknown
|
|
108
|
+
let error: unknown = undefined
|
|
109
|
+
try {
|
|
110
|
+
result = await Promise.resolve(fun.apply(dispatch, callData.params))
|
|
111
|
+
} catch (e) {
|
|
112
|
+
console.error(e)
|
|
113
|
+
error = (e as Error).message || ((e as Error).name || "error")
|
|
114
|
+
}
|
|
115
|
+
if (!callData.replyId) {
|
|
116
|
+
return
|
|
117
|
+
}
|
|
118
|
+
const transfer: Transferable[] = []
|
|
119
|
+
if (result) {
|
|
120
|
+
const resultHandle = resultHandles.get(method)
|
|
121
|
+
if (resultHandle) {
|
|
122
|
+
resultHandle(result, transfer)
|
|
58
123
|
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
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)
|
|
92
|
-
}
|
|
93
|
-
break;
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
postMessage({
|
|
97
|
-
type: messageTypes.methodCallReply,
|
|
98
|
-
data: {
|
|
99
|
-
replyId: callData.replyId,
|
|
100
|
-
result: res
|
|
101
|
-
}
|
|
102
|
-
}, transfer.length ? { transfer } : undefined)
|
|
103
|
-
}
|
|
104
|
-
break;
|
|
105
|
-
case messageTypes.methodCallReply:
|
|
106
|
-
// 方法调用的返回数据
|
|
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)
|
|
113
|
-
}
|
|
124
|
+
}
|
|
125
|
+
postMessage(
|
|
126
|
+
{
|
|
127
|
+
type: messageTypes.methodCallReply,
|
|
128
|
+
data: {
|
|
129
|
+
replyId: callData.replyId,
|
|
130
|
+
result,
|
|
131
|
+
error
|
|
132
|
+
},
|
|
133
|
+
},
|
|
134
|
+
transfer.length ? { transfer } : undefined,
|
|
135
|
+
)
|
|
136
|
+
break
|
|
137
|
+
case messageTypes.methodCallReply:
|
|
138
|
+
// 方法调用的返回数据
|
|
139
|
+
const replyId = (data.data as methodCallReply).replyId
|
|
140
|
+
if (!replyId) {
|
|
141
|
+
return
|
|
142
|
+
}
|
|
143
|
+
const fn = tasks.get(replyId)
|
|
144
|
+
if (!fn) {
|
|
145
|
+
return
|
|
146
|
+
}
|
|
147
|
+
fn((data.data as methodCallReply).result)
|
|
148
|
+
tasks.delete(replyId)
|
|
149
|
+
}
|
|
114
150
|
})
|
package/worker/child/general.ts
CHANGED
package/worker/child/sign.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import Replace from '../../replace/sign'
|
|
2
|
-
import init, { call } from './base'
|
|
1
|
+
import Replace from '../../replace/sign'
|
|
2
|
+
import init, { call } from './base'
|
|
3
3
|
|
|
4
4
|
const replace = new Replace()
|
|
5
5
|
init(replace)
|
|
6
6
|
|
|
7
|
-
replace.sign =
|
|
8
|
-
return
|
|
7
|
+
replace.sign = (data: unknown): Promise<string> => {
|
|
8
|
+
return call<string>('sign', data)
|
|
9
9
|
}
|
package/worker/index.ts
CHANGED
|
@@ -1,68 +1,67 @@
|
|
|
1
|
-
import { messageData } from
|
|
2
|
-
import DispatcherInterface from
|
|
1
|
+
import { messageData } from './type'
|
|
2
|
+
import DispatcherInterface from './interface'
|
|
3
3
|
|
|
4
4
|
export type webworker = new () => Worker
|
|
5
5
|
|
|
6
6
|
export default class worker implements DispatcherInterface {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
7
|
+
#concurrency: number
|
|
8
|
+
#counter: number = 0
|
|
9
|
+
#workers: Worker[] = []
|
|
10
|
+
#listenerList: ((event: MessageEvent) => void)[] = []
|
|
11
11
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
if (!this.#concurrency || this.#concurrency < 1) {
|
|
21
|
-
this.#concurrency = 1
|
|
22
|
-
}
|
|
23
|
-
for (let index = 0; index < this.#concurrency; index++) {
|
|
24
|
-
this.#addOneWorker(webworker)
|
|
25
|
-
}
|
|
12
|
+
constructor(webworker: webworker, concurrency?: number) {
|
|
13
|
+
this.#concurrency = Number(concurrency)
|
|
14
|
+
if (this.#concurrency < 1) {
|
|
15
|
+
try {
|
|
16
|
+
this.#concurrency = navigator.hardwareConcurrency < 8 ? navigator.hardwareConcurrency : 8
|
|
17
|
+
} catch (error) { }
|
|
26
18
|
}
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
const worker: Worker = new webworker()
|
|
30
|
-
worker.onmessage = async (event: MessageEvent) => {
|
|
31
|
-
const tasks: unknown[] = []
|
|
32
|
-
for (const fun of this.#listenerList) {
|
|
33
|
-
tasks.push(fun(event))
|
|
34
|
-
}
|
|
35
|
-
const res = await Promise.all(tasks)
|
|
36
|
-
for (const reply of res) {
|
|
37
|
-
if (reply) {
|
|
38
|
-
worker.postMessage(reply)
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
this.#workers.push(worker)
|
|
19
|
+
if (this.#concurrency < 1) {
|
|
20
|
+
this.#concurrency = 1
|
|
43
21
|
}
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
return this.#concurrency
|
|
22
|
+
for (let index = 0; index < this.#concurrency; index++) {
|
|
23
|
+
this.#addOneWorker(webworker)
|
|
47
24
|
}
|
|
25
|
+
}
|
|
48
26
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
27
|
+
#addOneWorker(webworker: webworker) {
|
|
28
|
+
const worker: Worker = new webworker()
|
|
29
|
+
worker.onmessage = async (event: MessageEvent) => {
|
|
30
|
+
const tasks: unknown[] = []
|
|
31
|
+
for (const fun of this.#listenerList) {
|
|
32
|
+
tasks.push(fun(event))
|
|
33
|
+
}
|
|
34
|
+
const res = await Promise.all(tasks)
|
|
35
|
+
for (const reply of res) {
|
|
36
|
+
if (reply) {
|
|
37
|
+
worker.postMessage(reply)
|
|
52
38
|
}
|
|
53
|
-
|
|
39
|
+
}
|
|
54
40
|
}
|
|
41
|
+
this.#workers.push(worker)
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
concurrency(): number {
|
|
45
|
+
return this.#concurrency
|
|
46
|
+
}
|
|
55
47
|
|
|
56
|
-
|
|
57
|
-
|
|
48
|
+
postMessage(message: messageData, options?: StructuredSerializeOptions) {
|
|
49
|
+
if (!this.#workers[++this.#counter]) {
|
|
50
|
+
this.#counter = 0
|
|
58
51
|
}
|
|
52
|
+
this.#workers[this.#counter].postMessage(message, options)
|
|
53
|
+
}
|
|
59
54
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
55
|
+
addListener(fun: (event: MessageEvent) => void) {
|
|
56
|
+
this.#listenerList.push(fun)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
removeListener(fun: (event: MessageEvent) => void) {
|
|
60
|
+
for (const i in this.#listenerList) {
|
|
61
|
+
if (this.#listenerList[i] == fun) {
|
|
62
|
+
this.#listenerList.splice(i as unknown as number, 1)
|
|
63
|
+
return
|
|
64
|
+
}
|
|
67
65
|
}
|
|
68
|
-
}
|
|
66
|
+
}
|
|
67
|
+
}
|
package/worker/interface.ts
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
|
-
import { messageData, methodCall } from
|
|
1
|
+
import { messageData, methodCall } from './type'
|
|
2
2
|
|
|
3
3
|
export default interface DispatcherInterface {
|
|
4
|
-
|
|
4
|
+
concurrency(): number
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
postMessage(
|
|
7
|
+
message: messageData | methodCall,
|
|
8
|
+
options?: StructuredSerializeOptions,
|
|
9
|
+
): void
|
|
7
10
|
|
|
8
|
-
|
|
11
|
+
addListener<T>(fun: (event: MessageEvent<T>) => void): void
|
|
9
12
|
|
|
10
|
-
|
|
11
|
-
}
|
|
13
|
+
removeListener<T>(fun: (event: MessageEvent<T>) => void): void
|
|
14
|
+
}
|
package/worker/main/general.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import webworker from
|
|
2
|
-
import base from
|
|
1
|
+
import webworker from '../child/general.ts?worker&inline'
|
|
2
|
+
import base from '../index'
|
|
3
3
|
|
|
4
4
|
export default class extends base {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
constructor(concurrency?: number) {
|
|
6
|
+
super(webworker, concurrency)
|
|
7
|
+
}
|
|
8
8
|
}
|