Skip to content

Commit 00dd4c5

Browse files
committed
fix(utils): 调整sha256, 移除 getWindow, isWeb
1 parent cda8521 commit 00dd4c5

File tree

8 files changed

+14
-29
lines changed

8 files changed

+14
-29
lines changed

examples/nuxt/playground/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,11 @@
2626
"@opentiny/vue-vite-import": "workspace:~",
2727
"@tiptap/vue-3": "^2.1.0",
2828
"@vitejs/plugin-vue-jsx": "^3.0.0",
29-
"js-sha256": "^0.9.0",
3029
"onigasm": "^2.2.5",
3130
"postcss": "^8.4.16",
3231
"typescript": "^5.0.0",
3332
"vite-plugin-checker": "^0.5.2",
3433
"vite-plugin-dynamic-import": "^1.2.4",
3534
"vite-svg-loader": "^3.6.0"
3635
}
37-
}
36+
}

examples/vue2/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
"@vue/composition-api": "1.7.2",
3838
"@vue/runtime-dom": "^3.2.31",
3939
"@vue/test-utils": "^1.3.3",
40-
"js-sha256": "^0.9.0",
4140
"jsdom": "^21.0.0",
4241
"onigasm": "^2.2.5",
4342
"postcss": "^8.4.16",
@@ -60,4 +59,4 @@
6059
"vue-template-compiler": "2.6.14",
6160
"vue-tsc": "^1.0.16"
6261
}
63-
}
62+
}

examples/vue3/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
"@vitest/ui": "^0.31.0",
4141
"@vue/runtime-core": "3.2.31",
4242
"@vue/test-utils": "^2.2.7",
43-
"js-sha256": "^0.9.0",
4443
"jsdom": "^21.0.0",
4544
"onigasm": "^2.2.5",
4645
"postcss": "^8.4.16",
@@ -59,4 +58,4 @@
5958
"vue": "^3.3.9",
6059
"vue-i18n": "^9.1.7"
6160
}
62-
}
61+
}

packages/renderless/src/file-upload/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import type {
3030
} from '@/types'
3131

3232
import { extend } from '../common/object'
33-
import { xss, logger, crypt } from '@opentiny/utils'
33+
import { xss, logger, sha256 } from '@opentiny/utils'
3434
import uploadAjax from '../common/deps/upload-ajax'
3535
import { isObject } from '../common/type'
3636
import { isEmptyObject } from '../common/type'
@@ -513,7 +513,7 @@ export const getFileHash =
513513
reader.readAsArrayBuffer(file.raw)
514514
reader.onload = async (e) => {
515515
if (file.status === constants.FILE_STATUS.FAIL) return
516-
const hash = await crypt.sha256(e.target && e.target.result)
516+
const hash = sha256(e.target && e.target.result)
517517
file.hash = file.raw.hash = hash
518518
resolve(hash)
519519
emit('hash-progress', 100)
@@ -2046,7 +2046,7 @@ export const segmentUpload =
20462046
reader.readAsArrayBuffer(file)
20472047
reader.onload = async (e) => {
20482048
if (props.edm.isCheckCode === true) {
2049-
const hash = await crypt.sha256(e.target && e.target.result)
2049+
const hash = sha256(e.target && e.target.result)
20502050
file.hash = hash
20512051
}
20522052
resolve(file)

packages/utils/src/crypt/index.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
1-
import { getWindow } from '../globalConfig'
2-
31
/** 生成字节流或字符串的sha256编码 */
4-
export async function sha256(message: ArrayBuffer | string) {
2+
export function sha256(message: ArrayBuffer | string) {
53
const isArrayBuffer = Object.prototype.toString.call(message) === '[object ArrayBuffer]'
64
const msgUint8 = isArrayBuffer ? message : new TextEncoder().encode(message as string) // 编码为(utf-8)Uint8Array
7-
const hashBuffer = await getWindow().crypto.subtle.digest('SHA-256', msgUint8) // 计算消息的哈希值
5+
const hashBuffer = globalThis.crypto.subtle.digest('SHA-256', msgUint8) // 计算消息的哈希值
86
const hashArray = Array.from(new Uint8Array(hashBuffer)) // 将缓冲区转换为字节数组
97
const hashHex = hashArray.map((b) => b.toString(16).padStart(2, '0')).join('') // 将字节数组转换为十六进制字符串
108

119
return hashHex
1210
}
13-
14-
export default { sha256 }

packages/utils/src/globalConfig/index.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
export const isWeb = () =>
2-
typeof window !== 'undefined' && typeof document !== 'undefined' && window.document === document
3-
4-
/** 获取globalThis. 在web上, window===globalThis 在node.js中, global=== globalThis。
5-
* 所以该函数没必要存在,待移除
6-
*/
7-
export const getWindow = () => (isWeb() ? window : global)
8-
91
// 需要微前端的用户传入该变量
102
export const globalConfig = {
113
viewportWindow: null // 获取真实视口的window,解决在微前端中某些bug

packages/utils/src/index.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import xss from './xss'
22
import logger from './logger'
3-
import crypt from './crypt'
43
import ResizeObserver from './resize-observer'
54

6-
export { xss, logger, crypt, ResizeObserver }
5+
export { xss, logger, ResizeObserver }
6+
7+
export { sha256 } from './crypt'
8+
export { globalConfig, getViewportWindow } from './globalConfig'
79

8-
export { getWindow, isWeb, globalConfig, getViewportWindow } from './globalConfig'
910
export { getDays, getWeek, lastMonth, nextMonth, getCalendar, transformArray, parseDate } from './calendar'
11+
1012
export {
1113
isLeapYear,
1214
toDate,

packages/utils/src/logger/index.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import { getWindow } from '../globalConfig'
2-
3-
const _win: any = getWindow()
1+
const _win: any = globalThis
42
/** 使用 logger.xxx 代替 window.console.xxx, 避免语法警告 */
53
export const logger = _win.console as Console
64

0 commit comments

Comments
 (0)