n20-common-lib 2.22.55 → 2.22.56
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/package.json +1 -2
- package/src/components/FileUploadTable/index.vue +163 -6
- package/src/components/ShowColumn/index.vue +54 -23
- package/src/utils/asyncGetRelaNos.js +5 -14
- package/theme/fonts/OpenSans-Regular.629a55a7.ttf +0 -0
- package/theme/fonts/OpenSans-Semibold.33f225b8.ttf +0 -0
- package/theme/fonts/iconfont.1871f864.ttf +0 -0
- package/theme/fonts/iconfont.38d4b428.woff +0 -0
- package/theme/fonts/iconfont.7ddd3bd8.woff +0 -0
- package/theme/fonts/iconfont.b56977e8.woff2 +0 -0
- package/theme/fonts/iconfont.ca8ecaf4.woff2 +0 -0
- package/theme/fonts/iconfont.f0839d3b.ttf +0 -0
- package/src/utils/permission-storage-worker.v1.js +0 -23
- package/src/utils/permissionStorage.js +0 -138
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "n20-common-lib",
|
|
3
|
-
"version": "2.22.
|
|
3
|
+
"version": "2.22.56",
|
|
4
4
|
"private": false,
|
|
5
5
|
"scripts": {
|
|
6
6
|
"serve": "vue-cli-service serve",
|
|
@@ -68,7 +68,6 @@
|
|
|
68
68
|
"v-viewer": "1.6.4",
|
|
69
69
|
"vue-jsonp": "2.0.0",
|
|
70
70
|
"vuedraggable": "*",
|
|
71
|
-
"worker-loader": "^3.0.8",
|
|
72
71
|
"webpack-duplicate-relano-plugin": "^0.1.0",
|
|
73
72
|
"xe-utils": "^3.5.11",
|
|
74
73
|
"ngraph.events": "1.2.2"
|
|
@@ -57,10 +57,10 @@
|
|
|
57
57
|
<span v-if="requiredTypes.includes(row[keys.type])" style="color: red" class="m-r-s">*</span>
|
|
58
58
|
<el-select
|
|
59
59
|
v-model="row[keys.type]"
|
|
60
|
-
:disabled="row._typeDisabled"
|
|
60
|
+
:disabled="row._typeDisabled || row._typeUpdating"
|
|
61
61
|
:placeholder="'请选择' | $lc"
|
|
62
62
|
style="width: calc(100% - 16px)"
|
|
63
|
-
@change="
|
|
63
|
+
@change="handleTypeChange(row)"
|
|
64
64
|
>
|
|
65
65
|
<el-option
|
|
66
66
|
v-for="item in typeOptions"
|
|
@@ -268,7 +268,13 @@
|
|
|
268
268
|
:close-on-click-modal="false"
|
|
269
269
|
:destroy-on-open="true"
|
|
270
270
|
>
|
|
271
|
-
<el-select
|
|
271
|
+
<el-select
|
|
272
|
+
v-if="!autoMatchBatchFileType"
|
|
273
|
+
v-model="bathType"
|
|
274
|
+
class="m-b-s w-100p"
|
|
275
|
+
clearable
|
|
276
|
+
@change="handleBathChange"
|
|
277
|
+
>
|
|
272
278
|
<el-option
|
|
273
279
|
v-for="item in typeOptions"
|
|
274
280
|
:key="item.type"
|
|
@@ -278,7 +284,7 @@
|
|
|
278
284
|
/>
|
|
279
285
|
</el-select>
|
|
280
286
|
<Upload
|
|
281
|
-
v-if="bathType"
|
|
287
|
+
v-if="autoMatchBatchFileType || bathType"
|
|
282
288
|
ref="upload-batch"
|
|
283
289
|
class="n20-upload-drag"
|
|
284
290
|
:msg-type="null"
|
|
@@ -305,7 +311,7 @@
|
|
|
305
311
|
</template>
|
|
306
312
|
</Upload>
|
|
307
313
|
<div class="dialog-footer">
|
|
308
|
-
<el-button type="primary" @click="batchUploadFn">{{ '确认' | $lc }}</el-button>
|
|
314
|
+
<el-button type="primary" :loading="batchUploading" @click="batchUploadFn">{{ '确认' | $lc }}</el-button>
|
|
309
315
|
<el-button plain @click="visibleBatch = false">{{ '取消' | $lc }}</el-button>
|
|
310
316
|
</div>
|
|
311
317
|
</Dialog>
|
|
@@ -435,6 +441,10 @@ export default {
|
|
|
435
441
|
type: Boolean,
|
|
436
442
|
default: false
|
|
437
443
|
},
|
|
444
|
+
autoMatchBatchFileType: {
|
|
445
|
+
type: Boolean,
|
|
446
|
+
default: false
|
|
447
|
+
},
|
|
438
448
|
showBatchPrint: {
|
|
439
449
|
type: Boolean,
|
|
440
450
|
default: false
|
|
@@ -503,6 +513,7 @@ export default {
|
|
|
503
513
|
previewSameOrg: false,
|
|
504
514
|
seeRow: {},
|
|
505
515
|
officeStatus: false,
|
|
516
|
+
batchUploading: false,
|
|
506
517
|
// 批量上传使用的文件数据(避免直接修改计算属性)
|
|
507
518
|
_batchFileData: null
|
|
508
519
|
}
|
|
@@ -757,11 +768,157 @@ export default {
|
|
|
757
768
|
data: JSON.stringify(dto)
|
|
758
769
|
}
|
|
759
770
|
},
|
|
760
|
-
batchUploadFn() {
|
|
771
|
+
async batchUploadFn() {
|
|
772
|
+
if (this.autoMatchBatchFileType) {
|
|
773
|
+
await this.autoMatchBatchUpload()
|
|
774
|
+
return
|
|
775
|
+
}
|
|
761
776
|
let $uploadwrap = this.$refs['upload-batch']
|
|
762
777
|
// 直接提交上传,关闭对话框的逻辑在 batchSuccess 回调中处理
|
|
763
778
|
$uploadwrap.submit()
|
|
764
779
|
},
|
|
780
|
+
async autoMatchBatchUpload() {
|
|
781
|
+
let $uploadwrap = this.$refs['upload-batch']
|
|
782
|
+
let uploadFiles = $uploadwrap?.$refs?.upload?.uploadFiles || []
|
|
783
|
+
let files = uploadFiles.filter((item) => item.status === 'ready' && item.raw)
|
|
784
|
+
if (!files.length) {
|
|
785
|
+
this.$message.warning($lc('请先选择上传文件'))
|
|
786
|
+
return
|
|
787
|
+
}
|
|
788
|
+
|
|
789
|
+
try {
|
|
790
|
+
for (let item of files) {
|
|
791
|
+
let result = await $uploadwrap.beforeUploadFn(item.raw)
|
|
792
|
+
if (result === false) {
|
|
793
|
+
return
|
|
794
|
+
}
|
|
795
|
+
}
|
|
796
|
+
|
|
797
|
+
let FD = new FormData()
|
|
798
|
+
files.forEach((item) => {
|
|
799
|
+
FD.append('file', item.raw)
|
|
800
|
+
})
|
|
801
|
+
|
|
802
|
+
let remoteFileUpload = JSON.parse(this.fileData?.data || '{}')
|
|
803
|
+
remoteFileUpload.bussValues = this.typeOptions.map((item) => item.type)
|
|
804
|
+
FD.append('remoteFileUpload', JSON.stringify(remoteFileUpload))
|
|
805
|
+
|
|
806
|
+
this.batchUploading = true
|
|
807
|
+
let { data } = await axios.post(
|
|
808
|
+
this.apiPrefix
|
|
809
|
+
? `${this.apiPrefix}/neams/eamsbaserecord/batchSaveFile`
|
|
810
|
+
: '/neams/eamsbaserecord/batchSaveFile',
|
|
811
|
+
FD,
|
|
812
|
+
{
|
|
813
|
+
headers: Object.assign(auth.setHeaders(this.headers), { 'Content-Type': 'multipart/form-data' }),
|
|
814
|
+
loading: false,
|
|
815
|
+
noMsg: true,
|
|
816
|
+
timeout: 900000
|
|
817
|
+
}
|
|
818
|
+
)
|
|
819
|
+
|
|
820
|
+
let responseList = Array.isArray(data) ? data : []
|
|
821
|
+
let rows = responseList.map((item) => this.buildAutoMatchBatchRow(item))
|
|
822
|
+
this.tableData.splice(0, 0, ...rows)
|
|
823
|
+
let pendingFiles = [...files]
|
|
824
|
+
rows.forEach((row) => {
|
|
825
|
+
let fileIndex = pendingFiles.findIndex((item) => item.raw.name === row[this.keys.name])
|
|
826
|
+
let sourceFile = pendingFiles.splice(fileIndex > -1 ? fileIndex : 0, 1)[0]
|
|
827
|
+
this.$emit('on-success', sourceFile?.raw, row)
|
|
828
|
+
})
|
|
829
|
+
|
|
830
|
+
$uploadwrap.clearFiles()
|
|
831
|
+
$uploadwrap.fileList = []
|
|
832
|
+
this.visibleBatch = false
|
|
833
|
+
} catch (err) {
|
|
834
|
+
this.$message.error(err?.msg || $lc('上传失败'))
|
|
835
|
+
} finally {
|
|
836
|
+
this.batchUploading = false
|
|
837
|
+
}
|
|
838
|
+
},
|
|
839
|
+
buildAutoMatchBatchRow(item) {
|
|
840
|
+
let userInfo = JSON.parse(sessionStorage.getItem('userInfo') || '{}')
|
|
841
|
+
let row = {
|
|
842
|
+
...item,
|
|
843
|
+
_name: item.fileName,
|
|
844
|
+
_percent: 100,
|
|
845
|
+
_status: 'success',
|
|
846
|
+
_typeDisabled: false,
|
|
847
|
+
_typeUpdating: false,
|
|
848
|
+
_shouldUpdateFileType: !item.attno,
|
|
849
|
+
_lastFileType: item.attno,
|
|
850
|
+
_lastFileTypeName: item.attname,
|
|
851
|
+
_lastBussValue: item.bussValue
|
|
852
|
+
}
|
|
853
|
+
row[this.keys.rowKey] = item.beid
|
|
854
|
+
row[this.keys.type] = item.attno
|
|
855
|
+
row[this.keys.name] = item.fileName
|
|
856
|
+
row[this.keys.url] = this.keys.url === this.keys.rowKey ? item.beid : item.filePath || item.beid
|
|
857
|
+
row[this.keys.time] = row[this.keys.time] || dayjs().format('YYYY-MM-DD HH:mm:ss')
|
|
858
|
+
row[this.keys.user] = row[this.keys.user] || userInfo.uname
|
|
859
|
+
return row
|
|
860
|
+
},
|
|
861
|
+
handleTypeChange(row) {
|
|
862
|
+
let type = row[this.keys.type]
|
|
863
|
+
this.$emit('typeChange', type)
|
|
864
|
+
if (!row._shouldUpdateFileType || !type) {
|
|
865
|
+
return
|
|
866
|
+
}
|
|
867
|
+
this.updateFileBuss(row, type)
|
|
868
|
+
},
|
|
869
|
+
async updateFileBuss(row, type) {
|
|
870
|
+
let previousType = row._lastFileType
|
|
871
|
+
let previousTypeName = row._lastFileTypeName
|
|
872
|
+
let previousBussValue = row._lastBussValue
|
|
873
|
+
let typeOption = this.typeOptions.find((item) => item.type === type)
|
|
874
|
+
let typeName = typeOption?.attname || typeOption?.label || ''
|
|
875
|
+
let bussValue = typeOption?.bussValue
|
|
876
|
+
|
|
877
|
+
this.$set(row, 'attno', type)
|
|
878
|
+
this.$set(row, 'attname', typeName)
|
|
879
|
+
if (bussValue !== undefined) {
|
|
880
|
+
this.$set(row, 'bussValue', bussValue)
|
|
881
|
+
}
|
|
882
|
+
this.$set(row, '_typeUpdating', true)
|
|
883
|
+
|
|
884
|
+
try {
|
|
885
|
+
await axios.post(
|
|
886
|
+
this.apiPrefix
|
|
887
|
+
? `${this.apiPrefix}/neams/eamsbaserecord/updateFlieBuss`
|
|
888
|
+
: '/neams/eamsbaserecord/updateFlieBuss',
|
|
889
|
+
[
|
|
890
|
+
{
|
|
891
|
+
beid: row.beid,
|
|
892
|
+
reid: row.reid,
|
|
893
|
+
appno: row.appno,
|
|
894
|
+
syscode: row.syscode,
|
|
895
|
+
bussValue: row.bussValue,
|
|
896
|
+
bussId: row.bussId,
|
|
897
|
+
bussName: row.bussName,
|
|
898
|
+
attno: row.attno,
|
|
899
|
+
attname: row.attname,
|
|
900
|
+
filePath: row.filePath,
|
|
901
|
+
fileName: row.fileName,
|
|
902
|
+
fmid: row.fmid
|
|
903
|
+
}
|
|
904
|
+
],
|
|
905
|
+
{
|
|
906
|
+
noMsg: true
|
|
907
|
+
}
|
|
908
|
+
)
|
|
909
|
+
this.$set(row, '_lastFileType', type)
|
|
910
|
+
this.$set(row, '_lastFileTypeName', typeName)
|
|
911
|
+
this.$set(row, '_lastBussValue', row.bussValue)
|
|
912
|
+
} catch (err) {
|
|
913
|
+
this.$set(row, this.keys.type, previousType)
|
|
914
|
+
this.$set(row, 'attno', previousType)
|
|
915
|
+
this.$set(row, 'attname', previousTypeName)
|
|
916
|
+
this.$set(row, 'bussValue', previousBussValue)
|
|
917
|
+
this.$message.error(err?.msg || $lc('附件类型更新失败'))
|
|
918
|
+
} finally {
|
|
919
|
+
this.$set(row, '_typeUpdating', false)
|
|
920
|
+
}
|
|
921
|
+
},
|
|
765
922
|
batchSuccess(response, file, fileList) {
|
|
766
923
|
// 创建完整的行对象,初始化状态字段
|
|
767
924
|
let row = {
|
|
@@ -462,20 +462,14 @@ function saveTransform(list, labelKey, isFilter) {
|
|
|
462
462
|
let listN = []
|
|
463
463
|
if (!isFilter) {
|
|
464
464
|
list.forEach((c) => {
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
listN.push(
|
|
469
|
-
}
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
}
|
|
474
|
-
if (sFn && c[labelKey]) {
|
|
475
|
-
listN.push({ _colKey: labelKey, _colVal: c[labelKey] })
|
|
476
|
-
} else {
|
|
477
|
-
listN.push(c)
|
|
478
|
-
}
|
|
465
|
+
const colKey = getColumnKey(c, labelKey)
|
|
466
|
+
// 已有列只保存稳定标识和用户设置,label 始终从当前 columns 中获取
|
|
467
|
+
if (colKey && !c.isNew && !c.remote) {
|
|
468
|
+
listN.push(createColumnReference(c, colKey))
|
|
469
|
+
}
|
|
470
|
+
// 用户新增列、远程列等没有本地定义可供回填,需要保留完整配置
|
|
471
|
+
else {
|
|
472
|
+
listN.push(c)
|
|
479
473
|
}
|
|
480
474
|
})
|
|
481
475
|
} else {
|
|
@@ -494,6 +488,44 @@ function saveTransform(list, labelKey, isFilter) {
|
|
|
494
488
|
return listN
|
|
495
489
|
}
|
|
496
490
|
|
|
491
|
+
const COLUMN_KEY_PRIORITY = ['columnKey', 'prop', 'slotName', 'field', 'type']
|
|
492
|
+
const COLUMN_SETTING_KEYS = ['fixed', 'width', 'wrap', 'bold', 'align']
|
|
493
|
+
|
|
494
|
+
function getColumnKey(column, labelKey = 'label') {
|
|
495
|
+
return COLUMN_KEY_PRIORITY.find(
|
|
496
|
+
(key) => key !== labelKey && column[key] !== undefined && column[key] !== null && column[key] !== ''
|
|
497
|
+
)
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
function createColumnReference(column, colKey) {
|
|
501
|
+
const entry = { _colKey: colKey, _colVal: column[colKey] }
|
|
502
|
+
COLUMN_SETTING_KEYS.forEach((key) => {
|
|
503
|
+
if (column[key] !== undefined) entry[key] = column[key]
|
|
504
|
+
})
|
|
505
|
+
return entry
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
function findCurrentColumn(savedColumn, columnsT, labelKey) {
|
|
509
|
+
if (savedColumn._colKey) {
|
|
510
|
+
const column = columnsT.find((c) => c[savedColumn._colKey] === savedColumn._colVal)
|
|
511
|
+
if (column) return column
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
// 兼容历史完整对象,以及曾使用翻译后 label 作为 _colKey 的数据
|
|
515
|
+
const colKey = getColumnKey(savedColumn, labelKey)
|
|
516
|
+
if (colKey) {
|
|
517
|
+
return columnsT.find((c) => c[colKey] === savedColumn[colKey])
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
function mergeColumnSettings(column, savedColumn) {
|
|
522
|
+
const settings = {}
|
|
523
|
+
COLUMN_SETTING_KEYS.forEach((key) => {
|
|
524
|
+
if (savedColumn[key] !== undefined) settings[key] = savedColumn[key]
|
|
525
|
+
})
|
|
526
|
+
return Object.keys(settings).length ? { ...column, ...settings } : column
|
|
527
|
+
}
|
|
528
|
+
|
|
497
529
|
function getTransform(list, columnsT, labelKey = 'label') {
|
|
498
530
|
let columns = []
|
|
499
531
|
list.forEach((d) => {
|
|
@@ -505,16 +537,15 @@ function getTransform(list, columnsT, labelKey = 'label') {
|
|
|
505
537
|
column && columns.push(column)
|
|
506
538
|
// 如果list中的每一项是对象
|
|
507
539
|
} else if (typeof d === 'object') {
|
|
508
|
-
|
|
509
|
-
if (
|
|
510
|
-
//
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
// 如果d中没有_colKey属性
|
|
515
|
-
} else {
|
|
516
|
-
// 将d添加到columns中
|
|
540
|
+
const column = findCurrentColumn(d, columnsT, labelKey)
|
|
541
|
+
if (column) {
|
|
542
|
+
// 使用当前 columns 的完整定义(包括当前语言 label),仅合并用户设置
|
|
543
|
+
columns.push(mergeColumnSettings(column, d))
|
|
544
|
+
} else if (!d._colKey) {
|
|
545
|
+
// 用户新增列、远程列等没有本地定义,保持原有数据形态
|
|
517
546
|
columns.push(d)
|
|
547
|
+
} else {
|
|
548
|
+
// _colKey 数据未匹配到当前列时沿用原行为:忽略已不存在的列
|
|
518
549
|
}
|
|
519
550
|
}
|
|
520
551
|
})
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import axios from './axios'
|
|
2
2
|
import { getItem, setItem } from './storageEdit'
|
|
3
|
-
import { persistPermissionItems } from './permissionStorage'
|
|
4
3
|
|
|
5
4
|
/**
|
|
6
5
|
* @description: 按需/按模块 加载权限
|
|
@@ -77,21 +76,13 @@ async function asyncGetRelaNos(appNo, rns) {
|
|
|
77
76
|
// 将 ssCltrelaNos 重新赋值给 ssCltrelaNos ,并将 ssCltrelaNos 重新写回到 sessionStorage 中
|
|
78
77
|
// 将 oRelaNos 重新赋值给 ssRelaNosG ,并将 ssRelaNosG 重新写回到 sessionStorage 中
|
|
79
78
|
// 如果需要同步,则将 isSync 写回到 sessionStorage 中
|
|
79
|
+
setItem('relaNos', ssRelaNos, isSync)
|
|
80
|
+
setItem('cltrelaNos', ssCltrelaNos, isSync)
|
|
81
|
+
|
|
80
82
|
let ssRelaNosG = getItem('relaNosGlobal')
|
|
81
83
|
ssRelaNosG.push(...oRelaNos)
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
await persistPermissionItems([
|
|
85
|
-
{ key: 'relaNos', value: ssRelaNos },
|
|
86
|
-
{ key: 'cltrelaNos', value: ssCltrelaNos },
|
|
87
|
-
{ key: 'relaNosGlobal', value: ssRelaNosG }
|
|
88
|
-
])
|
|
89
|
-
sessionStorage.setItem('isSync', isSync)
|
|
90
|
-
} else {
|
|
91
|
-
setItem('relaNos', ssRelaNos, false)
|
|
92
|
-
setItem('cltrelaNos', ssCltrelaNos, false)
|
|
93
|
-
setItem('relaNosGlobal', ssRelaNosG, false)
|
|
94
|
-
}
|
|
84
|
+
setItem('relaNosGlobal', ssRelaNosG, isSync)
|
|
85
|
+
isSync && sessionStorage.setItem('isSync', isSync)
|
|
95
86
|
} catch (error) {
|
|
96
87
|
// 如果请求失败,从已加载列表中移除该模块
|
|
97
88
|
const index = RLFM.indexOf(appNo)
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import LZString from 'lz-string'
|
|
2
|
-
|
|
3
|
-
self.onmessage = function ({ data }) {
|
|
4
|
-
if (
|
|
5
|
-
!data ||
|
|
6
|
-
(typeof data.id !== 'number' && typeof data.id !== 'string') ||
|
|
7
|
-
typeof data.value !== 'string'
|
|
8
|
-
) {
|
|
9
|
-
return
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
try {
|
|
13
|
-
self.postMessage({
|
|
14
|
-
id: data.id,
|
|
15
|
-
compressed: LZString.compressToBase64(data.value)
|
|
16
|
-
})
|
|
17
|
-
} catch (error) {
|
|
18
|
-
self.postMessage({
|
|
19
|
-
id: data.id,
|
|
20
|
-
error: error && error.message ? error.message : 'Compression failed'
|
|
21
|
-
})
|
|
22
|
-
}
|
|
23
|
-
}
|
|
@@ -1,138 +0,0 @@
|
|
|
1
|
-
import PermissionStorageWorker from 'worker-loader?filename=js/permission-storage-worker.[contenthash:8].js!./permission-storage-worker.v1.js'
|
|
2
|
-
import { setItem } from './storageEdit'
|
|
3
|
-
|
|
4
|
-
const LARGE_STORAGE_THRESHOLD = 524288
|
|
5
|
-
const WORKER_TIMEOUT = 30000
|
|
6
|
-
let compressionWorker = null
|
|
7
|
-
let requestSequence = 0
|
|
8
|
-
const pendingRequests = new Map()
|
|
9
|
-
|
|
10
|
-
function rejectPendingRequests(error) {
|
|
11
|
-
pendingRequests.forEach(({ reject, timeoutId }) => {
|
|
12
|
-
clearTimeout(timeoutId)
|
|
13
|
-
reject(error)
|
|
14
|
-
})
|
|
15
|
-
pendingRequests.clear()
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
function disposeWorker(error) {
|
|
19
|
-
if (compressionWorker) {
|
|
20
|
-
compressionWorker.terminate()
|
|
21
|
-
compressionWorker = null
|
|
22
|
-
}
|
|
23
|
-
rejectPendingRequests(error)
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
function getCompressionWorker() {
|
|
27
|
-
if (compressionWorker) return compressionWorker
|
|
28
|
-
if (typeof Worker === 'undefined') return null
|
|
29
|
-
|
|
30
|
-
try {
|
|
31
|
-
compressionWorker = new PermissionStorageWorker()
|
|
32
|
-
} catch (error) {
|
|
33
|
-
compressionWorker = null
|
|
34
|
-
return null
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
compressionWorker.onmessage = ({ data }) => {
|
|
38
|
-
if (!data) return
|
|
39
|
-
|
|
40
|
-
const pending = pendingRequests.get(data.id)
|
|
41
|
-
if (!pending) return
|
|
42
|
-
|
|
43
|
-
clearTimeout(pending.timeoutId)
|
|
44
|
-
pendingRequests.delete(data.id)
|
|
45
|
-
|
|
46
|
-
if (data.error || typeof data.compressed !== 'string') {
|
|
47
|
-
pending.reject(new Error(data.error || 'Invalid compression result'))
|
|
48
|
-
return
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
pending.resolve(data.compressed)
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
compressionWorker.onerror = () => {
|
|
55
|
-
disposeWorker(new Error('Permission compression worker failed'))
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
return compressionWorker
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
function compressInWorker(value) {
|
|
62
|
-
const worker = getCompressionWorker()
|
|
63
|
-
if (!worker) {
|
|
64
|
-
return Promise.reject(
|
|
65
|
-
new Error('Permission compression worker is unavailable')
|
|
66
|
-
)
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
return new Promise((resolve, reject) => {
|
|
70
|
-
const id = ++requestSequence
|
|
71
|
-
const timeoutId = setTimeout(() => {
|
|
72
|
-
if (!pendingRequests.has(id)) return
|
|
73
|
-
disposeWorker(new Error('Permission compression worker timed out'))
|
|
74
|
-
}, WORKER_TIMEOUT)
|
|
75
|
-
|
|
76
|
-
pendingRequests.set(id, { resolve, reject, timeoutId })
|
|
77
|
-
worker.postMessage({ id, value })
|
|
78
|
-
})
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
function writeCompressedValue(key, compressed) {
|
|
82
|
-
const storage = window.sessionStorage
|
|
83
|
-
const rawSetItem = storage._setItem
|
|
84
|
-
const rawRemoveItem = storage._removeItem
|
|
85
|
-
|
|
86
|
-
if (
|
|
87
|
-
typeof rawSetItem !== 'function' ||
|
|
88
|
-
typeof rawRemoveItem !== 'function'
|
|
89
|
-
) {
|
|
90
|
-
throw new Error('Extended Storage API is unavailable')
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
try {
|
|
94
|
-
rawSetItem.call(storage, `${key}:[LZ]`, compressed)
|
|
95
|
-
rawRemoveItem.call(storage, key)
|
|
96
|
-
} catch (error) {
|
|
97
|
-
// A previous uncompressed value can consume the remaining quota.
|
|
98
|
-
rawRemoveItem.call(storage, key)
|
|
99
|
-
rawSetItem.call(storage, `${key}:[LZ]`, compressed)
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
async function persistItem({ key, value, serialized }) {
|
|
104
|
-
if (
|
|
105
|
-
typeof serialized !== 'string' ||
|
|
106
|
-
serialized.length <= LARGE_STORAGE_THRESHOLD
|
|
107
|
-
) {
|
|
108
|
-
window.sessionStorage.setItem(key, serialized)
|
|
109
|
-
return
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
try {
|
|
113
|
-
const compressed = await compressInWorker(serialized)
|
|
114
|
-
writeCompressedValue(key, compressed)
|
|
115
|
-
} catch (error) {
|
|
116
|
-
// Preserve the existing synchronous behavior when Worker is unavailable.
|
|
117
|
-
setItem(key, value, true)
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
/**
|
|
122
|
-
* Persist permission objects with the same cache keys and LZ storage format
|
|
123
|
-
* used by n20-common-lib, while moving large compression work off main thread.
|
|
124
|
-
*/
|
|
125
|
-
export async function persistPermissionItems(items) {
|
|
126
|
-
const serializedItems = items.map(({ key, value }) => {
|
|
127
|
-
window[`_keep_${key}`] = value
|
|
128
|
-
return {
|
|
129
|
-
key,
|
|
130
|
-
value,
|
|
131
|
-
serialized: JSON.stringify(value)
|
|
132
|
-
}
|
|
133
|
-
})
|
|
134
|
-
|
|
135
|
-
for (const item of serializedItems) {
|
|
136
|
-
await persistItem(item)
|
|
137
|
-
}
|
|
138
|
-
}
|