diff --git a/packages/dashboard/package.json b/packages/dashboard/package.json index 1fc46ee10a80..8955d0d2e12d 100644 --- a/packages/dashboard/package.json +++ b/packages/dashboard/package.json @@ -12,7 +12,7 @@ }, "dependencies": { "@dimensiondev/holoflows-kit": "0.8.0-20210317064617-6c4792c", - "@dimensiondev/kit": "=0.0.0-20210630024153-886532b", + "@dimensiondev/kit": "=0.0.0-20210630045432-e05621c", "@masknet/icons": "workspace:*", "@masknet/plugin-example": "workspace:*", "@masknet/plugin-infra": "workspace:*", diff --git a/packages/dashboard/src/components/FileUpload/index.tsx b/packages/dashboard/src/components/FileUpload/index.tsx index 937faa3e9506..18e5f9759d3a 100644 --- a/packages/dashboard/src/components/FileUpload/index.tsx +++ b/packages/dashboard/src/components/FileUpload/index.tsx @@ -2,7 +2,7 @@ import { MaskColorVar } from '@masknet/theme' import { makeStyles, Typography } from '@material-ui/core' import { useEffect, useState } from 'react' import { File as FileIcon } from '@masknet/icons' -import { toText } from '@dimensiondev/kit' +import { blobToText } from '@dimensiondev/kit' const useStyles = makeStyles(() => ({ root: { @@ -52,7 +52,7 @@ export default function FileUpload({ width, height, readAsText, onChange }: File useEffect(() => { if (file) { if (readAsText) { - toText(file).then((result) => onChange(file, result)) + blobToText(file).then((result) => onChange(file, result)) } else { onChange(file) } diff --git a/packages/maskbook/package.json b/packages/maskbook/package.json index bb9d94b0e56d..8cf995606b38 100644 --- a/packages/maskbook/package.json +++ b/packages/maskbook/package.json @@ -9,7 +9,7 @@ "@balancer-labs/sor": "^1.0.0", "@dimensiondev/common-protocols": "1.6.0-20201027083702-d0ae6e2", "@dimensiondev/holoflows-kit": "0.8.0-20210317064617-6c4792c", - "@dimensiondev/kit": "=0.0.0-20210630024153-886532b", + "@dimensiondev/kit": "=0.0.0-20210630045432-e05621c", "@dimensiondev/metamask-extension-provider": "3.0.6-20210519045409-1835d4d", "@dimensiondev/stego-js": "=0.11.1-20201027083223-8ab41be", "@dimensiondev/uniswap-sdk": "=3.0.4-20210619092641-a270dc0", diff --git a/packages/maskbook/src/components/InjectedComponents/SteganographyTextPayload.ts b/packages/maskbook/src/components/InjectedComponents/SteganographyTextPayload.ts index 285ca47f4872..df1b18b3eb22 100644 --- a/packages/maskbook/src/components/InjectedComponents/SteganographyTextPayload.ts +++ b/packages/maskbook/src/components/InjectedComponents/SteganographyTextPayload.ts @@ -1,4 +1,4 @@ -import { decodeArrayBuffer } from '@dimensiondev/kit' +import { blobToArrayBuffer, decodeArrayBuffer } from '@dimensiondev/kit' import { GrayscaleAlgorithm } from '@dimensiondev/stego-js/esm/grayscale' import Services from '../../extension/service' import { ImageTemplateTypes, ImagePayloadURLs } from '../../resources/image-payload' @@ -7,7 +7,7 @@ import { downloadUrl } from '../../utils/utils' export async function SteganographyTextPayload(template: ImageTemplateTypes, text: string) { const pass = activatedSocialNetworkUI.configuration.steganography?.password?.() || 'mask' - const blankImage = await downloadUrl(ImagePayloadURLs[template]).then((x) => x.arrayBuffer()) + const blankImage = await downloadUrl(ImagePayloadURLs[template]).then(blobToArrayBuffer) const secretImage = new Uint8Array( decodeArrayBuffer( await Services.Steganography.encodeImage(new Uint8Array(blankImage), { diff --git a/packages/maskbook/src/database/helpers/avatar.ts b/packages/maskbook/src/database/helpers/avatar.ts index 5b1b9809891a..b56cb3945539 100644 --- a/packages/maskbook/src/database/helpers/avatar.ts +++ b/packages/maskbook/src/database/helpers/avatar.ts @@ -3,7 +3,7 @@ import { queryAvatarDB, isAvatarOutdatedDB, storeAvatarDB } from '../avatar' import { memoizePromise } from '../../utils/memoize' import { MaskMessage } from '../../utils/messages' import { downloadUrl } from '../../utils/utils' -import { toDataURL } from '@dimensiondev/kit' +import { blobToArrayBuffer, blobToDataURL } from '@dimensiondev/kit' /** * Get a (cached) blob url for an identifier. @@ -13,7 +13,7 @@ export const queryAvatarDataURL = memoizePromise( async function (identifier: ProfileIdentifier | GroupIdentifier): Promise { const buffer = await queryAvatarDB(identifier) if (!buffer) throw new Error('Avatar not found') - return toDataURL(new Blob([buffer], { type: 'image/png' })) + return blobToDataURL(new Blob([buffer], { type: 'image/png' })) }, (id) => id.toText(), ) @@ -35,7 +35,7 @@ export async function storeAvatar( if (typeof avatar === 'string') { if (avatar.startsWith('http') === false) return if (force || (await isAvatarOutdatedDB(identifier, 'lastUpdateTime'))) { - await storeAvatarDB(identifier, await (await downloadUrl(avatar)).arrayBuffer()) + await storeAvatarDB(identifier, await blobToArrayBuffer(await downloadUrl(avatar))) } // else do nothing } else { diff --git a/packages/maskbook/src/extension/background-script/SteganographyService.ts b/packages/maskbook/src/extension/background-script/SteganographyService.ts index 9b64e41ad0ae..1048e54fe092 100644 --- a/packages/maskbook/src/extension/background-script/SteganographyService.ts +++ b/packages/maskbook/src/extension/background-script/SteganographyService.ts @@ -5,10 +5,10 @@ import type { EncodeOptions, DecodeOptions } from '@dimensiondev/stego-js/cjs/st import { downloadUrl } from '../../utils/utils' import { memoizePromise } from '../../utils/memoize' import { getDimension } from '../../utils/image' -import { decodeArrayBuffer, encodeArrayBuffer } from '../../utils/type-transform/String-ArrayBuffer' - +import { decodeArrayBuffer, encodeArrayBuffer, blobToArrayBuffer } from '@dimensiondev/kit' import { assertEnvironment, Environment } from '@dimensiondev/holoflows-kit' import type { ImageTemplateTypes } from '../../resources/image-payload' + assertEnvironment(Environment.ManifestBackground) type Mask = 'v1' | 'v2' | 'v4' | 'transparent' @@ -62,7 +62,7 @@ const images: Record = { v4: new URL('./SteganographyResources/mask-v4.png', import.meta.url).toString(), transparent: new URL('./SteganographyResources/mask-transparent.png', import.meta.url).toString(), } -const getMaskBuf = memoizePromise(async (type: Mask) => (await downloadUrl(images[type])).arrayBuffer(), void 0) +const getMaskBuf = memoizePromise(async (type: Mask) => blobToArrayBuffer(await downloadUrl(images[type])), void 0) type EncodeImageOptions = { template?: ImageTemplateTypes @@ -99,5 +99,5 @@ export async function decodeImage(buf: string | ArrayBuffer, options: DecodeImag } export async function decodeImageUrl(url: string, options: DecodeImageOptions) { - return decodeImage(await (await downloadUrl(url)).arrayBuffer(), options) + return decodeImage(await blobToArrayBuffer(await downloadUrl(url)), options) } diff --git a/packages/maskbook/src/extension/options-page/DashboardComponents/RestoreFromBackupBox.tsx b/packages/maskbook/src/extension/options-page/DashboardComponents/RestoreFromBackupBox.tsx index 47b3bff739eb..7261513a65a6 100644 --- a/packages/maskbook/src/extension/options-page/DashboardComponents/RestoreFromBackupBox.tsx +++ b/packages/maskbook/src/extension/options-page/DashboardComponents/RestoreFromBackupBox.tsx @@ -4,7 +4,7 @@ import { makeStyles } from '@material-ui/core' import { useI18N } from '../../../utils' import { useStylesExtends } from '../../../components/custom-ui-helper' import { RestoreBox } from './RestoreBox' -import { toText } from '@dimensiondev/kit' +import { blobToText } from '@dimensiondev/kit' const useStyles = makeStyles((theme) => ({ root: { @@ -37,7 +37,7 @@ export function RestoreFromBackupBox(props: RestoreFromBackupBoxProps) { // invoke callback useEffect(() => { if (file) { - toText(file).then((result) => props.onChange?.(file, result)) + blobToText(file).then((result) => props.onChange?.(file, result)) } }, [file, props.onChange]) diff --git a/packages/maskbook/src/extension/options-page/DashboardComponents/RestoreFromQRCodeImageBox.tsx b/packages/maskbook/src/extension/options-page/DashboardComponents/RestoreFromQRCodeImageBox.tsx index c6e962b9ba2b..dbf105bdff3d 100644 --- a/packages/maskbook/src/extension/options-page/DashboardComponents/RestoreFromQRCodeImageBox.tsx +++ b/packages/maskbook/src/extension/options-page/DashboardComponents/RestoreFromQRCodeImageBox.tsx @@ -5,7 +5,7 @@ import { useI18N } from '../../../utils' import { RestoreBox } from './RestoreBox' import { useStylesExtends } from '../../../components/custom-ui-helper' import { QRCodeImageScanner } from './QRCodeImageScanner' -import { toDataURL } from '@dimensiondev/kit' +import { blobToDataURL } from '@dimensiondev/kit' const useStyles = makeStyles((theme) => ({ root: { @@ -59,7 +59,7 @@ export function RestoreFromQRCodeImageBox(props: RestoreFromQRCodeImageBoxProps) // read file as data URL useEffect(() => { if (file) { - toDataURL(file).then(setDataURL, () => setDataURL('')) + blobToDataURL(file).then(setDataURL, () => setDataURL('')) } else { setDataURL('') } diff --git a/packages/maskbook/src/plugins/FileService/SNSAdaptor/components/Upload.tsx b/packages/maskbook/src/plugins/FileService/SNSAdaptor/components/Upload.tsx index 3fb8f7706c3c..125c2f57eb2a 100644 --- a/packages/maskbook/src/plugins/FileService/SNSAdaptor/components/Upload.tsx +++ b/packages/maskbook/src/plugins/FileService/SNSAdaptor/components/Upload.tsx @@ -1,5 +1,5 @@ import { Attachment } from '@dimensiondev/common-protocols' -import { encodeArrayBuffer } from '@dimensiondev/kit' +import { blobToArrayBuffer, encodeArrayBuffer } from '@dimensiondev/kit' import { Checkbox, FormControlLabel, Link, makeStyles, Typography } from '@material-ui/core' import { isNil } from 'lodash-es' import { useState } from 'react' @@ -64,7 +64,7 @@ export const Upload: React.FC = () => { if (encrypted) { key = makeFileKey() } - const block = new Uint8Array(await file.arrayBuffer()) + const block = new Uint8Array(await blobToArrayBuffer(file)) const checksum = encodeArrayBuffer(await Attachment.checksum(block)) const item = await PluginFileServiceRPC.getFileInfo(checksum) if (isNil(item)) { diff --git a/packages/maskbook/src/polyfill/web-apis/implementation/Blob.prototype.arrayBuffer.ts b/packages/maskbook/src/polyfill/web-apis/implementation/Blob.prototype.arrayBuffer.ts deleted file mode 100644 index 6f184a777afb..000000000000 --- a/packages/maskbook/src/polyfill/web-apis/implementation/Blob.prototype.arrayBuffer.ts +++ /dev/null @@ -1,9 +0,0 @@ -import { toArrayBuffer } from './File.prototype.arrayBuffer' - -if (typeof Blob !== 'undefined' && !Blob.prototype.arrayBuffer) { - Object.defineProperty(Blob.prototype, 'arrayBuffer', { - configurable: true, - writable: true, - value: toArrayBuffer, - }) -} diff --git a/packages/maskbook/src/polyfill/web-apis/implementation/File.prototype.arrayBuffer.ts b/packages/maskbook/src/polyfill/web-apis/implementation/File.prototype.arrayBuffer.ts deleted file mode 100644 index 7af253edac73..000000000000 --- a/packages/maskbook/src/polyfill/web-apis/implementation/File.prototype.arrayBuffer.ts +++ /dev/null @@ -1,22 +0,0 @@ -export {} -if (typeof File !== 'undefined' && !File.prototype.arrayBuffer) { - Object.defineProperty(File.prototype, 'arrayBuffer', { - configurable: true, - writable: true, - value: toArrayBuffer, - }) -} - -export function toArrayBuffer(this: File | Blob) { - return new Promise((resolve, reject) => { - const reader = new FileReader() - reader.addEventListener('abort', reject) - reader.addEventListener('error', reject) - reader.addEventListener('load', () => { - const result = reader.result as ArrayBuffer - if (typeof result === 'string') reject(new TypeError()) - resolve(result) - }) - reader.readAsArrayBuffer(this) - }) -} diff --git a/packages/maskbook/src/polyfill/web-apis/worker.ts b/packages/maskbook/src/polyfill/web-apis/worker.ts index d654144ec0d2..7a3842d89239 100644 --- a/packages/maskbook/src/polyfill/web-apis/worker.ts +++ b/packages/maskbook/src/polyfill/web-apis/worker.ts @@ -1,3 +1 @@ -import './implementation/Blob.prototype.arrayBuffer' -import './implementation/File.prototype.arrayBuffer' import './implementation/EventTarget.constructor' diff --git a/packages/maskbook/src/utils/utils.ts b/packages/maskbook/src/utils/utils.ts index e1dc648fbcae..c7dd3d066e8c 100644 --- a/packages/maskbook/src/utils/utils.ts +++ b/packages/maskbook/src/utils/utils.ts @@ -41,7 +41,7 @@ export function dispatchCustomEvents( * @param image */ export async function pasteImageToActiveElements(image: File | Blob): Promise { - const bytes = new Uint8Array(await image.arrayBuffer()) + const bytes = new Uint8Array(await blobToArrayBuffer(image)) dispatchCustomEvents(document.activeElement, 'paste', { type: 'image', value: Array.from(bytes) }) } @@ -167,6 +167,7 @@ export function addUint8Array(a: ArrayBuffer, b: ArrayBuffer) { } import Services from '../extension/service' +import { blobToArrayBuffer } from '@dimensiondev/kit' export { parseURL } from '@masknet/shared' /** * !!!! Please use the Promise constructor if possible diff --git a/packages/shared/package.json b/packages/shared/package.json index e366f37886b6..4d012b1ce8cf 100644 --- a/packages/shared/package.json +++ b/packages/shared/package.json @@ -4,7 +4,7 @@ "types": "./dist/index.d.ts", "dependencies": { "@dimensiondev/holoflows-kit": "0.8.0-20210317064617-6c4792c", - "@dimensiondev/kit": "=0.0.0-20210624142900-972fd05", + "@dimensiondev/kit": "=0.0.0-20210630045432-e05621c", "@masknet/web3-shared": "workspace:*", "@servie/events": "^3.0.0", "anchorme": "^2.1.2", diff --git a/packages/web3-shared/package.json b/packages/web3-shared/package.json index cba67676a275..18552d321fd1 100644 --- a/packages/web3-shared/package.json +++ b/packages/web3-shared/package.json @@ -3,7 +3,7 @@ "main": "./dist/index.js", "types": "./dist/index.d.ts", "dependencies": { - "@dimensiondev/kit": "=0.0.0-20210624142900-972fd05", + "@dimensiondev/kit": "=0.0.0-20210630045432-e05621c", "@masknet/constants": "workspace:*", "@masknet/contracts": "workspace:*", "bignumber.js": "9.0.1", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 02075180e8c2..bc8eeef1bb3c 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -138,7 +138,7 @@ importers: specifiers: '@babel/core': ^7.14.3 '@dimensiondev/holoflows-kit': 0.8.0-20210317064617-6c4792c - '@dimensiondev/kit': '=0.0.0-20210630024153-886532b' + '@dimensiondev/kit': '=0.0.0-20210630045432-e05621c' '@masknet/icons': workspace:* '@masknet/plugin-example': workspace:* '@masknet/plugin-infra': workspace:* @@ -174,7 +174,7 @@ importers: wallet.ts: ^1.0.1 dependencies: '@dimensiondev/holoflows-kit': 0.8.0-20210317064617-6c4792c - '@dimensiondev/kit': 0.0.0-20210630024153-886532b + '@dimensiondev/kit': 0.0.0-20210630045432-e05621c '@masknet/icons': link:../icons '@masknet/plugin-example': link:../plugins/example '@masknet/plugin-infra': link:../plugin-infra @@ -236,7 +236,7 @@ importers: '@balancer-labs/sor': ^1.0.0 '@dimensiondev/common-protocols': 1.6.0-20201027083702-d0ae6e2 '@dimensiondev/holoflows-kit': 0.8.0-20210317064617-6c4792c - '@dimensiondev/kit': '=0.0.0-20210630024153-886532b' + '@dimensiondev/kit': '=0.0.0-20210630045432-e05621c' '@dimensiondev/metamask-extension-provider': 3.0.6-20210519045409-1835d4d '@dimensiondev/stego-js': '=0.11.1-20201027083223-8ab41be' '@dimensiondev/uniswap-sdk': '=3.0.4-20210619092641-a270dc0' @@ -398,7 +398,7 @@ importers: '@balancer-labs/sor': 1.0.0 '@dimensiondev/common-protocols': 1.6.0-20201027083702-d0ae6e2 '@dimensiondev/holoflows-kit': 0.8.0-20210317064617-6c4792c_webextension-polyfill@0.8.0 - '@dimensiondev/kit': 0.0.0-20210630024153-886532b + '@dimensiondev/kit': 0.0.0-20210630045432-e05621c '@dimensiondev/metamask-extension-provider': 3.0.6-20210519045409-1835d4d '@dimensiondev/stego-js': 0.11.1-20201027083223-8ab41be '@dimensiondev/uniswap-sdk': 3.0.4-20210619092641-a270dc0_418b2ad04426b02e1076b55b7e1862e2 @@ -587,7 +587,7 @@ importers: packages/shared: specifiers: '@dimensiondev/holoflows-kit': 0.8.0-20210317064617-6c4792c - '@dimensiondev/kit': '=0.0.0-20210624142900-972fd05' + '@dimensiondev/kit': '=0.0.0-20210630045432-e05621c' '@masknet/web3-shared': workspace:* '@servie/events': ^3.0.0 anchorme: ^2.1.2 @@ -602,7 +602,7 @@ importers: wallet.ts: ^1.0.1 dependencies: '@dimensiondev/holoflows-kit': 0.8.0-20210317064617-6c4792c - '@dimensiondev/kit': 0.0.0-20210624142900-972fd05 + '@dimensiondev/kit': 0.0.0-20210630045432-e05621c '@masknet/web3-shared': link:../web3-shared '@servie/events': 3.0.0 anchorme: 2.1.2 @@ -667,7 +667,7 @@ importers: packages/web3-shared: specifiers: - '@dimensiondev/kit': '=0.0.0-20210624142900-972fd05' + '@dimensiondev/kit': '=0.0.0-20210630045432-e05621c' '@masknet/constants': workspace:* '@masknet/contracts': workspace:* '@types/use-subscription': ^1.0.0 @@ -682,7 +682,7 @@ importers: web3-core: ^1.3.6 web3-utils: ^1.3.6 dependencies: - '@dimensiondev/kit': 0.0.0-20210624142900-972fd05 + '@dimensiondev/kit': 0.0.0-20210630045432-e05621c '@masknet/constants': link:../constants '@masknet/contracts': link:../contracts bignumber.js: 9.0.1 @@ -3574,12 +3574,8 @@ packages: webextension-polyfill: 0.8.0 dev: false - /@dimensiondev/kit/0.0.0-20210624142900-972fd05: - resolution: {integrity: sha512-afmIrZndLSgqg9hWUfwC10uAivJjImSlWMXbmYKhC/OUeHfyz6tbUO2PEKT1xz9L4ujFsAjgkvAKHnaeqNo9Qg==, tarball: download/@dimensiondev/kit/0.0.0-20210624142900-972fd05/2216af7740aa6220c4041d6d1b92e9124bdac3ca3594c0456ef603dfbcbf2387} - dev: false - - /@dimensiondev/kit/0.0.0-20210630024153-886532b: - resolution: {integrity: sha512-SG5IT1NqJjuz0F2U3cDMArmzQWB896ag/8Bs+TmBcOqzFJ+/SU+G8x3pr2aAy54Lik8vfw0MmasRwKUzYVjT/A==, tarball: download/@dimensiondev/kit/0.0.0-20210630024153-886532b/756556cabebd9404cbda13e494a3433646af4cb4cbf143520a176f783cc18483} + /@dimensiondev/kit/0.0.0-20210630045432-e05621c: + resolution: {integrity: sha512-RRghCa0HR3NRgGDb3fw6VpvAgCEYs8nFk8M10xXubbXQUgcwp7UVQStI3cm7mV7PTGC1lsaAv2YLTqIQB8w9YA==, tarball: download/@dimensiondev/kit/0.0.0-20210630045432-e05621c/6d03398c21a05c6d7d2e7ffd4b8d5cc435cc9217fd2ddd485c0d08538af1485e} dev: false /@dimensiondev/metamask-extension-provider/3.0.6-20210519045409-1835d4d: