template-replacement 3.0.8
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/.workflow/publish_to_npmjs.yml +26 -0
- package/core/base.ts +54 -0
- package/core/general.ts +13 -0
- package/core/sign.ts +13 -0
- package/db/index.ts +5 -0
- package/db/indexedDBCache.ts +129 -0
- package/dispatcher/general.ts +6 -0
- package/dispatcher/sign.ts +6 -0
- package/dispatcher/workerGeneral.ts +7 -0
- package/dispatcher/workerSign.ts +7 -0
- 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-CC-6cmrq.js +189 -0
- package/dist/general.d.ts +1 -0
- package/dist/index-BkwrGCka.js +61 -0
- package/dist/main/general.js +4543 -0
- package/dist/main/sign.js +4566 -0
- package/dist/replace/general.js +424 -0
- package/dist/replace/sign.js +428 -0
- package/dist/sign.d.ts +1 -0
- package/download/index.ts +22 -0
- package/download/stream.ts +38 -0
- package/helper/index.ts +162 -0
- package/index.ts +21 -0
- package/office/zip.ts +116 -0
- package/package.json +33 -0
- package/replace/base.ts +124 -0
- package/replace/general.ts +14 -0
- package/replace/image.ts +116 -0
- package/replace/interface.ts +29 -0
- package/replace/paramsData.ts +117 -0
- package/replace/sign.ts +31 -0
- package/task/urlDownloadTask.ts +67 -0
- package/temp/index.ts +157 -0
- package/temp/interface.ts +18 -0
- package/tsconfig.json +108 -0
- package/vite.config.ts +36 -0
- package/worker/child/agency.ts +78 -0
- package/worker/child/base.ts +89 -0
- package/worker/child/general.ts +5 -0
- package/worker/child/sign.ts +9 -0
- package/worker/index.ts +65 -0
- package/worker/interface.ts +11 -0
- package/worker/main/general.ts +8 -0
- package/worker/main/index.ts +176 -0
- package/worker/main/sign.ts +8 -0
- package/worker/type.ts +24 -0
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import image, { extent, textWrapTypes } from './image'
|
|
2
|
+
|
|
3
|
+
export type textData = Record<string, string|image>
|
|
4
|
+
export type mediaData = Record<string, image>
|
|
5
|
+
|
|
6
|
+
type imageValue = {
|
|
7
|
+
id: String,
|
|
8
|
+
index: Number,
|
|
9
|
+
suffix: String,
|
|
10
|
+
wp_extent: extent,
|
|
11
|
+
text_wrap: textWrapTypes,
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
type value = {
|
|
15
|
+
Image?: imageValue,
|
|
16
|
+
Text?: string
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
type replaceParams = {
|
|
20
|
+
text: Record<string, value>
|
|
21
|
+
media: Record<string, value>
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export default class paramsData {
|
|
25
|
+
textData: textData = {}
|
|
26
|
+
mediaData: mediaData = {}
|
|
27
|
+
add_media?: (file: Uint8Array) => Promise<string>
|
|
28
|
+
|
|
29
|
+
constructor(text?: textData, media?: mediaData) {
|
|
30
|
+
if (text && text.constructor === Object) {
|
|
31
|
+
this.textData = text
|
|
32
|
+
}
|
|
33
|
+
if (media && media.constructor === Object) {
|
|
34
|
+
this.mediaData = media
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
//转为替换参数
|
|
39
|
+
async toReplaceParams(): Promise<[replaceParams, Uint8Array[]]> {
|
|
40
|
+
const text: Record<string, value> = {}
|
|
41
|
+
const media: Record<string, value> = {}
|
|
42
|
+
const tasks = []
|
|
43
|
+
const mediaBuffers: Uint8Array[] = []
|
|
44
|
+
for (const key in this.textData) {
|
|
45
|
+
tasks.push(new Promise<void>(async resolve => {
|
|
46
|
+
const value = this.textData[key]
|
|
47
|
+
if (value instanceof image) {
|
|
48
|
+
let id = value.id ?? ''
|
|
49
|
+
let index = 0
|
|
50
|
+
if (!id) {
|
|
51
|
+
const buffer = await value.file.arrayBuffer()
|
|
52
|
+
const uint8Array = new Uint8Array(buffer)
|
|
53
|
+
if(this.add_media) {
|
|
54
|
+
id = await this.add_media(uint8Array)
|
|
55
|
+
}else{
|
|
56
|
+
index = mediaBuffers.push(uint8Array) - 1
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
text[key] = {
|
|
60
|
+
Image: {
|
|
61
|
+
index,
|
|
62
|
+
id,
|
|
63
|
+
suffix: '',
|
|
64
|
+
wp_extent: value.wpExtent ?? {cx: 0, cy: 0},
|
|
65
|
+
text_wrap: value.textWrap
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
} else {
|
|
69
|
+
text[key] = {
|
|
70
|
+
Text: String(value)
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
resolve()
|
|
74
|
+
}))
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
for (const key in this.mediaData) {
|
|
78
|
+
tasks.push(new Promise<void>(async resolve => {
|
|
79
|
+
const value = this.mediaData[key]
|
|
80
|
+
if (value instanceof image) {
|
|
81
|
+
let id = value.id ?? ''
|
|
82
|
+
let index = 0
|
|
83
|
+
if (!id) {
|
|
84
|
+
const buffer = await value.file.arrayBuffer()
|
|
85
|
+
const uint8Array = new Uint8Array(buffer)
|
|
86
|
+
|
|
87
|
+
if(this.add_media) {
|
|
88
|
+
id = await this.add_media(uint8Array)
|
|
89
|
+
}else{
|
|
90
|
+
index = mediaBuffers.push(uint8Array) - 1
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
media[key] = {
|
|
94
|
+
Image: {
|
|
95
|
+
index,
|
|
96
|
+
id,
|
|
97
|
+
suffix: '',
|
|
98
|
+
wp_extent: value.wpExtent ?? {cx: 0, cy: 0},
|
|
99
|
+
text_wrap: value.textWrap
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
resolve()
|
|
104
|
+
}))
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
await Promise.all(tasks)
|
|
108
|
+
return [{
|
|
109
|
+
text,
|
|
110
|
+
media
|
|
111
|
+
}, mediaBuffers]
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
isEmpty() {
|
|
115
|
+
return Object.keys(this.textData).length === 0 && Object.keys(this.mediaData).length === 0
|
|
116
|
+
}
|
|
117
|
+
}
|
package/replace/sign.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import Base from './base';
|
|
2
|
+
import core, { add_template, add_media, replace_batch_verify, replace_params_encode } from '../core/sign'
|
|
3
|
+
import paramsData from './paramsData';
|
|
4
|
+
|
|
5
|
+
export default class Sign extends Base {
|
|
6
|
+
constructor() {
|
|
7
|
+
super(core())
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
async handle(paramsData: paramsData, files: Uint8Array[]): Promise<Uint8Array[]> {
|
|
11
|
+
paramsData.add_media = add_media
|
|
12
|
+
const [params] = await paramsData.toReplaceParams()
|
|
13
|
+
|
|
14
|
+
const addFileTasks: Promise<number>[] = []
|
|
15
|
+
files.forEach((file, i) => {
|
|
16
|
+
addFileTasks.push(new Promise<number>(async resolve => {
|
|
17
|
+
resolve(await add_template(file))
|
|
18
|
+
}))
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
const encodeData = {
|
|
22
|
+
files: await Promise.all(addFileTasks),
|
|
23
|
+
variables: params
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const paramsEncode = await replace_params_encode(encodeData)
|
|
27
|
+
|
|
28
|
+
const verifyCode = await this.sign(paramsEncode)
|
|
29
|
+
return replace_batch_verify(String(verifyCode), paramsEncode.data)
|
|
30
|
+
}
|
|
31
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import axios, { AxiosProgressEvent } from "axios"
|
|
2
|
+
import db from "../db/index"
|
|
3
|
+
|
|
4
|
+
export default class urlDownloadTask {
|
|
5
|
+
urls: string[]
|
|
6
|
+
downloadProgressListener:((progressEvent: AxiosProgressEvent) => void)[] = []
|
|
7
|
+
|
|
8
|
+
constructor(urls: string[]) {
|
|
9
|
+
if (urls.constructor !== Array) {
|
|
10
|
+
throw new Error("不是可用的链接数组数据")
|
|
11
|
+
}
|
|
12
|
+
this.urls = urls
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
async start(): Promise<(Blob|undefined)[]> {
|
|
16
|
+
const tasks = []
|
|
17
|
+
for (const url of this.urls) {
|
|
18
|
+
tasks.push(this.getUrlData(url))
|
|
19
|
+
}
|
|
20
|
+
return Promise.all(tasks)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
async getUrlData(url: string): Promise<Blob|undefined> {
|
|
24
|
+
const data = await db.getDataByKey<Blob>(url)
|
|
25
|
+
if (data && data.data) {
|
|
26
|
+
return data.data
|
|
27
|
+
}
|
|
28
|
+
const getData = await this.download(url)
|
|
29
|
+
if (!getData) {
|
|
30
|
+
return undefined
|
|
31
|
+
}
|
|
32
|
+
db.putData<Blob>({
|
|
33
|
+
url: url,
|
|
34
|
+
data: getData,
|
|
35
|
+
})
|
|
36
|
+
return getData
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
async download(url: string): Promise<Blob> {
|
|
40
|
+
const response = await axios({
|
|
41
|
+
url: url,
|
|
42
|
+
method: 'get',
|
|
43
|
+
responseType: 'blob',
|
|
44
|
+
onDownloadProgress: (progressEvent: AxiosProgressEvent) => {
|
|
45
|
+
for (const fun of this.downloadProgressListener) {
|
|
46
|
+
fun(progressEvent)
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
const contentDisposition = response.headers['content-disposition']
|
|
52
|
+
// 解析文件名
|
|
53
|
+
if (contentDisposition) {
|
|
54
|
+
const matches = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/.exec(contentDisposition)
|
|
55
|
+
if (matches != null && matches[1]) {
|
|
56
|
+
const filename = matches[1].replace(/['"]/g, '')
|
|
57
|
+
const contentType = response.headers['content-type'] ?? 'application/octet-stream'
|
|
58
|
+
return new File([response.data], filename, { type: contentType })
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return response.data
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
onDownloadProgress(listen: (progressEvent: AxiosProgressEvent) => void) {
|
|
65
|
+
this.downloadProgressListener.push(listen)
|
|
66
|
+
}
|
|
67
|
+
}
|
package/temp/index.ts
ADDED
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import { fileTypeByBuffer, fileTypes, getFileNameFromUrl, urlsToFileBlobs } from '../helper'
|
|
2
|
+
import { TempInterface } from './interface'
|
|
3
|
+
|
|
4
|
+
export type TempImageInfo = {
|
|
5
|
+
hash: string,
|
|
6
|
+
blob: Blob,
|
|
7
|
+
path: string
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export enum status {
|
|
11
|
+
waitLoad, //文件待加载
|
|
12
|
+
loaded, //文件已加载
|
|
13
|
+
replaceFinish, //完成替换
|
|
14
|
+
replaceFail, //替换失败
|
|
15
|
+
loadFail, //文件加载失败
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
//传递的文件信息
|
|
19
|
+
export type transmitFileInfo = {
|
|
20
|
+
name: string,
|
|
21
|
+
uint8Array: Uint8Array,
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function transmitFileInfoToTemp(data: transmitFileInfo) {
|
|
25
|
+
if (!data.uint8Array || !data.name) {
|
|
26
|
+
throw new Error("模板文件信息错误")
|
|
27
|
+
}
|
|
28
|
+
return new Temp(undefined, undefined, data.uint8Array, data.name)
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export default class Temp implements TempInterface{
|
|
32
|
+
name: string = ''
|
|
33
|
+
blob?: File|Blob
|
|
34
|
+
uint8Array?: Uint8Array
|
|
35
|
+
url?: string
|
|
36
|
+
status = status.waitLoad // 0文件待加载,1文件已加载,2完成替换,3替换失败
|
|
37
|
+
|
|
38
|
+
_output?: File|Blob
|
|
39
|
+
_type?: fileTypes
|
|
40
|
+
|
|
41
|
+
tempImages: Record<string, TempImageInfo> = {}
|
|
42
|
+
|
|
43
|
+
constructor(file?: File|Blob, url?: string, uint8Array?: Uint8Array, name?: string) {
|
|
44
|
+
if (uint8Array) {
|
|
45
|
+
this.uint8Array = uint8Array
|
|
46
|
+
this.setStatus(status.loaded)
|
|
47
|
+
}else if (file) {
|
|
48
|
+
this.blob = file
|
|
49
|
+
this.setStatus(status.loaded)
|
|
50
|
+
}else if (url) {
|
|
51
|
+
this.url = url
|
|
52
|
+
}
|
|
53
|
+
if (name) {
|
|
54
|
+
this.name = name
|
|
55
|
+
}else{
|
|
56
|
+
this.name = (file as File)?.name ?? ''
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (!url && !file && !uint8Array) {
|
|
60
|
+
throw new Error("缺少文件数据或文件链接")
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
getName() {
|
|
65
|
+
if (this.name) {
|
|
66
|
+
return this.name
|
|
67
|
+
}
|
|
68
|
+
if ((this.blob as File)?.name) {
|
|
69
|
+
this.name = (this.blob as File).name
|
|
70
|
+
return this.name
|
|
71
|
+
}
|
|
72
|
+
return this.url ? getFileNameFromUrl(this.url) : ''
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
async type(): Promise<fileTypes> {
|
|
76
|
+
if (this._type) {
|
|
77
|
+
return this._type
|
|
78
|
+
}
|
|
79
|
+
if (this.uint8Array) {
|
|
80
|
+
this._type = await fileTypeByBuffer(this.uint8Array)
|
|
81
|
+
}else{
|
|
82
|
+
const file = await this.getBlob()
|
|
83
|
+
if (file) {
|
|
84
|
+
this._type = await fileTypeByBuffer(file)
|
|
85
|
+
}else{
|
|
86
|
+
this._type = fileTypes.unknown
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
return this._type
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
async getBuffer(): Promise<Uint8Array|undefined>{
|
|
93
|
+
if (this.uint8Array) {
|
|
94
|
+
return this.uint8Array
|
|
95
|
+
}
|
|
96
|
+
const blob = await this.getBlob()
|
|
97
|
+
if (blob) {
|
|
98
|
+
this.uint8Array = new Uint8Array(await blob.arrayBuffer())
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
return this.uint8Array
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
async getBlob(): Promise<Blob|undefined> {
|
|
105
|
+
if (this.blob) {
|
|
106
|
+
return this.blob
|
|
107
|
+
}
|
|
108
|
+
if (!this.blob) {
|
|
109
|
+
if (this.uint8Array) {
|
|
110
|
+
this.blob = new Blob([ this.uint8Array ])
|
|
111
|
+
}else if (this.url) {
|
|
112
|
+
const [ blob ] = await urlsToFileBlobs([ this.url ])
|
|
113
|
+
if (blob) {
|
|
114
|
+
this.blob = blob
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
if ((this.status === status.waitLoad || this.status === status.loadFail) && this.blob) {
|
|
119
|
+
this.setStatus(status.loaded)
|
|
120
|
+
}
|
|
121
|
+
if (!this.blob) {
|
|
122
|
+
this.setStatus(status.loadFail)
|
|
123
|
+
}
|
|
124
|
+
return this.blob
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
setStatus(status: status): void {
|
|
128
|
+
this.status = status
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
setOutputFile(file: File|Blob): void {
|
|
132
|
+
this._output = file
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
setTempImages(images: Record<string, TempImageInfo>): void {
|
|
136
|
+
this.tempImages = images
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
outputFile(): File|Blob|undefined {
|
|
140
|
+
return this._output ?? this.blob
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
async getTransmitFileInfo(): Promise<transmitFileInfo|undefined> {
|
|
144
|
+
let uint8Array = this.uint8Array
|
|
145
|
+
this.uint8Array = undefined
|
|
146
|
+
if (!uint8Array) {
|
|
147
|
+
uint8Array = await this.getBuffer()
|
|
148
|
+
}
|
|
149
|
+
if (!uint8Array) {
|
|
150
|
+
return undefined
|
|
151
|
+
}
|
|
152
|
+
return {
|
|
153
|
+
name: this.getName(),
|
|
154
|
+
uint8Array: new Uint8Array(uint8Array.buffer.slice(0)),
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { status, TempImageInfo } from '.'
|
|
2
|
+
import { fileTypes } from '../helper'
|
|
3
|
+
|
|
4
|
+
export interface TempInterface {
|
|
5
|
+
type(): Promise<fileTypes>
|
|
6
|
+
|
|
7
|
+
getBuffer(): Promise<Uint8Array|undefined>
|
|
8
|
+
|
|
9
|
+
getBlob(): Promise<Blob|undefined>
|
|
10
|
+
|
|
11
|
+
setStatus(status: status): void
|
|
12
|
+
|
|
13
|
+
setOutputFile(file: File|Blob): void
|
|
14
|
+
|
|
15
|
+
setTempImages(images: Record<string, TempImageInfo>): void
|
|
16
|
+
|
|
17
|
+
outputFile(): File|Blob|undefined
|
|
18
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
/* Visit https://aka.ms/tsconfig to read more about this file */
|
|
4
|
+
|
|
5
|
+
/* Projects */
|
|
6
|
+
// "incremental": true, /* Save .tsbuildinfo files to allow for incremental compilation of projects. */
|
|
7
|
+
// "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */
|
|
8
|
+
// "tsBuildInfoFile": "./.tsbuildinfo", /* Specify the path to .tsbuildinfo incremental compilation file. */
|
|
9
|
+
// "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects. */
|
|
10
|
+
// "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */
|
|
11
|
+
// "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */
|
|
12
|
+
|
|
13
|
+
/* Language and Environment */
|
|
14
|
+
"target": "es2021", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
|
|
15
|
+
// "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
|
|
16
|
+
// "jsx": "preserve", /* Specify what JSX code is generated. */
|
|
17
|
+
// "experimentalDecorators": true, /* Enable experimental support for legacy experimental decorators. */
|
|
18
|
+
// "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */
|
|
19
|
+
// "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h'. */
|
|
20
|
+
// "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */
|
|
21
|
+
// "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using 'jsx: react-jsx*'. */
|
|
22
|
+
// "reactNamespace": "", /* Specify the object invoked for 'createElement'. This only applies when targeting 'react' JSX emit. */
|
|
23
|
+
// "noLib": true, /* Disable including any library files, including the default lib.d.ts. */
|
|
24
|
+
// "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */
|
|
25
|
+
// "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */
|
|
26
|
+
|
|
27
|
+
/* Modules */
|
|
28
|
+
"module": "es2022", /* Specify what module code is generated. */
|
|
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. */
|
|
31
|
+
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
|
|
32
|
+
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
|
|
33
|
+
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
|
|
34
|
+
// "typeRoots": [], /* Specify multiple folders that act like './node_modules/@types'. */
|
|
35
|
+
// "types": [], /* Specify type package names to be included without being referenced in a source file. */
|
|
36
|
+
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
|
|
37
|
+
// "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */
|
|
38
|
+
// "allowImportingTsExtensions": true, /* Allow imports to include TypeScript file extensions. Requires '--moduleResolution bundler' and either '--noEmit' or '--emitDeclarationOnly' to be set. */
|
|
39
|
+
// "resolvePackageJsonExports": true, /* Use the package.json 'exports' field when resolving package imports. */
|
|
40
|
+
// "resolvePackageJsonImports": true, /* Use the package.json 'imports' field when resolving imports. */
|
|
41
|
+
// "customConditions": [], /* Conditions to set in addition to the resolver-specific defaults when resolving imports. */
|
|
42
|
+
// "resolveJsonModule": true, /* Enable importing .json files. */
|
|
43
|
+
// "allowArbitraryExtensions": true, /* Enable importing files with any extension, provided a declaration file is present. */
|
|
44
|
+
// "noResolve": true, /* Disallow 'import's, 'require's or '<reference>'s from expanding the number of files TypeScript should add to a project. */
|
|
45
|
+
|
|
46
|
+
/* JavaScript Support */
|
|
47
|
+
// "allowJs": true, /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */
|
|
48
|
+
// "checkJs": true, /* Enable error reporting in type-checked JavaScript files. */
|
|
49
|
+
// "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */
|
|
50
|
+
|
|
51
|
+
/* Emit */
|
|
52
|
+
// "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
|
|
53
|
+
// "declarationMap": true, /* Create sourcemaps for d.ts files. */
|
|
54
|
+
// "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */
|
|
55
|
+
// "sourceMap": true, /* Create source map files for emitted JavaScript files. */
|
|
56
|
+
// "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */
|
|
57
|
+
// "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */
|
|
58
|
+
// "outDir": "./", /* Specify an output folder for all emitted files. */
|
|
59
|
+
// "removeComments": true, /* Disable emitting comments. */
|
|
60
|
+
// "noEmit": true, /* Disable emitting files from a compilation. */
|
|
61
|
+
// "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
|
|
62
|
+
// "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */
|
|
63
|
+
// "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */
|
|
64
|
+
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
|
|
65
|
+
// "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */
|
|
66
|
+
// "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */
|
|
67
|
+
// "newLine": "crlf", /* Set the newline character for emitting files. */
|
|
68
|
+
// "stripInternal": true, /* Disable emitting declarations that have '@internal' in their JSDoc comments. */
|
|
69
|
+
// "noEmitHelpers": true, /* Disable generating custom helper functions like '__extends' in compiled output. */
|
|
70
|
+
// "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */
|
|
71
|
+
// "preserveConstEnums": true, /* Disable erasing 'const enum' declarations in generated code. */
|
|
72
|
+
// "declarationDir": "./", /* Specify the output directory for generated declaration files. */
|
|
73
|
+
|
|
74
|
+
/* Interop Constraints */
|
|
75
|
+
// "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */
|
|
76
|
+
// "verbatimModuleSyntax": true, /* Do not transform or elide any imports or exports not marked as type-only, ensuring they are written in the output file's format based on the 'module' setting. */
|
|
77
|
+
// "isolatedDeclarations": true, /* Require sufficient annotation on exports so other tools can trivially generate declaration files. */
|
|
78
|
+
// "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
|
|
79
|
+
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
|
|
80
|
+
// "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
|
|
81
|
+
"forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */
|
|
82
|
+
|
|
83
|
+
/* Type Checking */
|
|
84
|
+
"strict": true, /* Enable all strict type-checking options. */
|
|
85
|
+
// "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied 'any' type. */
|
|
86
|
+
// "strictNullChecks": true, /* When type checking, take into account 'null' and 'undefined'. */
|
|
87
|
+
// "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */
|
|
88
|
+
// "strictBindCallApply": true, /* Check that the arguments for 'bind', 'call', and 'apply' methods match the original function. */
|
|
89
|
+
// "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */
|
|
90
|
+
// "noImplicitThis": true, /* Enable error reporting when 'this' is given the type 'any'. */
|
|
91
|
+
// "useUnknownInCatchVariables": true, /* Default catch clause variables as 'unknown' instead of 'any'. */
|
|
92
|
+
// "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */
|
|
93
|
+
// "noUnusedLocals": true, /* Enable error reporting when local variables aren't read. */
|
|
94
|
+
// "noUnusedParameters": true, /* Raise an error when a function parameter isn't read. */
|
|
95
|
+
// "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */
|
|
96
|
+
// "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */
|
|
97
|
+
// "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */
|
|
98
|
+
// "noUncheckedIndexedAccess": true, /* Add 'undefined' to a type when accessed using an index. */
|
|
99
|
+
// "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */
|
|
100
|
+
// "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type. */
|
|
101
|
+
// "allowUnusedLabels": true, /* Disable error reporting for unused labels. */
|
|
102
|
+
// "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */
|
|
103
|
+
|
|
104
|
+
/* Completeness */
|
|
105
|
+
// "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
|
|
106
|
+
"skipLibCheck": true /* Skip type checking all .d.ts files. */
|
|
107
|
+
}
|
|
108
|
+
}
|
package/vite.config.ts
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { defineConfig } from 'vite';
|
|
2
|
+
import wasmPack from 'vite-plugin-wasm-pack';
|
|
3
|
+
import dts from 'vite-plugin-dts';
|
|
4
|
+
|
|
5
|
+
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
|
+
build: {
|
|
14
|
+
outDir: './dist',
|
|
15
|
+
lib: {
|
|
16
|
+
entry: ['./worker/main/sign.ts', './worker/main/general.ts', './replace/general.ts', './replace/sign.ts'],
|
|
17
|
+
name: 'template-replacement',
|
|
18
|
+
fileName: (format, entryName) => `${entryName}.js`,
|
|
19
|
+
formats: ['es'],
|
|
20
|
+
},
|
|
21
|
+
rollupOptions: {
|
|
22
|
+
output: {
|
|
23
|
+
entryFileNames: info => {
|
|
24
|
+
const modules = info.facadeModuleId?.split('/')
|
|
25
|
+
if (!modules?.length || modules?.length < 2) {
|
|
26
|
+
return '[name].js'
|
|
27
|
+
}
|
|
28
|
+
return modules[modules?.length - 2] + '/[name].js'
|
|
29
|
+
},
|
|
30
|
+
},
|
|
31
|
+
},
|
|
32
|
+
},
|
|
33
|
+
worker: {
|
|
34
|
+
format: 'es',
|
|
35
|
+
}
|
|
36
|
+
})
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import image from "../../replace/image";
|
|
2
|
+
import ReplaceInterface, { media } from "../../replace/interface";
|
|
3
|
+
import paramsData, { mediaData, textData } from "../../replace/paramsData";
|
|
4
|
+
import Temp, { transmitFileInfo, transmitFileInfoToTemp } from "../../temp";
|
|
5
|
+
|
|
6
|
+
export default class implements ReplaceInterface {
|
|
7
|
+
replace: ReplaceInterface
|
|
8
|
+
|
|
9
|
+
constructor(replace: ReplaceInterface) {
|
|
10
|
+
this.replace = replace
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
addTempFile(tempFile: any) {
|
|
14
|
+
tempFile = transmitFileInfoToTemp(tempFile)
|
|
15
|
+
return this.replace.addTempFile(tempFile)
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
clear() {
|
|
19
|
+
return this.replace.clear()
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
//提取变量
|
|
23
|
+
extractVariables(files: Temp[] | undefined): Promise<Record<string, string[]>> {
|
|
24
|
+
if (files) {
|
|
25
|
+
for (const i in files) {
|
|
26
|
+
files[i] = transmitFileInfoToTemp(files[i] as transmitFileInfo)
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return this.replace.extractVariables(files)
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
//提取媒体
|
|
33
|
+
extractMedias(files: Temp[] | undefined): Promise<Record<string, media[]>> {
|
|
34
|
+
if (files) {
|
|
35
|
+
for (const i in files) {
|
|
36
|
+
files[i] = transmitFileInfoToTemp(files[i] as transmitFileInfo)
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return this.replace.extractMedias(files)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
//签名方法
|
|
43
|
+
sign(data: any): Promise<string> {
|
|
44
|
+
return this.replace.sign(data)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
//执行替换任务
|
|
48
|
+
execute(params: paramsData, files: Temp[] | undefined): Promise<Record<string, Uint8Array>> {
|
|
49
|
+
if (files) {
|
|
50
|
+
for (const i in files) {
|
|
51
|
+
files[i] = transmitFileInfoToTemp(files[i] as transmitFileInfo)
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
const text: textData = {}
|
|
55
|
+
for (const key in params.textData) {
|
|
56
|
+
const value = params.textData[key] as Record<keyof image, any>
|
|
57
|
+
if (value.file && value.file instanceof Blob) {
|
|
58
|
+
text[key] = new image(value.file)
|
|
59
|
+
delete value.file
|
|
60
|
+
text[key].setPropertys(value)
|
|
61
|
+
}else {
|
|
62
|
+
text[key] = value
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const media: mediaData = {}
|
|
67
|
+
for (const key in params.mediaData) {
|
|
68
|
+
const value = params.mediaData[key] as Record<keyof image, any>
|
|
69
|
+
if (value.file && value.file instanceof Blob) {
|
|
70
|
+
media[key] = new image(value.file)
|
|
71
|
+
delete value.file
|
|
72
|
+
media[key].setPropertys(value)
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
return this.replace.execute(new paramsData(text, media), files)
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
}
|