Skip to content

Commit

Permalink
chore: clean blob (#3528)
Browse files Browse the repository at this point in the history
  • Loading branch information
septs authored Jun 30, 2021
1 parent c832b4d commit 07dbc5e
Show file tree
Hide file tree
Showing 16 changed files with 33 additions and 69 deletions.
2 changes: 1 addition & 1 deletion packages/dashboard/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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:*",
Expand Down
4 changes: 2 additions & 2 deletions packages/dashboard/src/components/FileUpload/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion packages/maskbook/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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), {
Expand Down
6 changes: 3 additions & 3 deletions packages/maskbook/src/database/helpers/avatar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -13,7 +13,7 @@ export const queryAvatarDataURL = memoizePromise(
async function (identifier: ProfileIdentifier | GroupIdentifier): Promise<string | undefined> {
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(),
)
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -62,7 +62,7 @@ const images: Record<Mask, string> = {
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
Expand Down Expand Up @@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -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])

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -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('')
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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)) {
Expand Down

This file was deleted.

This file was deleted.

2 changes: 0 additions & 2 deletions packages/maskbook/src/polyfill/web-apis/worker.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
import './implementation/Blob.prototype.arrayBuffer'
import './implementation/File.prototype.arrayBuffer'
import './implementation/EventTarget.constructor'
3 changes: 2 additions & 1 deletion packages/maskbook/src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export function dispatchCustomEvents<T extends keyof CustomEvents>(
* @param image
*/
export async function pasteImageToActiveElements(image: File | Blob): Promise<void> {
const bytes = new Uint8Array(await image.arrayBuffer())
const bytes = new Uint8Array(await blobToArrayBuffer(image))
dispatchCustomEvents(document.activeElement, 'paste', { type: 'image', value: Array.from(bytes) })
}

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion packages/shared/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion packages/web3-shared/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
24 changes: 10 additions & 14 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 07dbc5e

Please sign in to comment.