Skip to content

Commit

Permalink
feat(helper): add data utils (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mister-Hope authored Jan 31, 2024
1 parent f1be46f commit ebd6f63
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 0 deletions.
1 change: 1 addition & 0 deletions tools/helper/src/client/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './composables/index.js'
export * from './utils/index.js'
export * from '../shared/index.js'
28 changes: 28 additions & 0 deletions tools/helper/src/client/utils/data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import type { DeflateOptions } from 'fflate'
import { strFromU8, strToU8, unzlibSync, zlibSync } from 'fflate/browser'

declare const __VUEPRESS_SSR__: boolean

export const encodeData = (
data: string,
level: DeflateOptions['level'] = 6,
): string => {
const buffer = strToU8(data)
// zlib headers can be found at https://stackoverflow.com/a/54915442
const zipped = zlibSync(buffer, { level })
const binary = strFromU8(zipped, true)

return __VUEPRESS_SSR__
? Buffer.from(binary, 'binary').toString('base64')
: btoa(binary)
}

export const decodeData = (base64: string): string => {
console.log(base64)

const binary = __VUEPRESS_SSR__
? Buffer.from(base64, 'base64').toString('binary')
: atob(base64)

return strFromU8(unzlibSync(strToU8(binary, true)))
}
1 change: 1 addition & 0 deletions tools/helper/src/client/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './data.js'
26 changes: 26 additions & 0 deletions tools/helper/src/node/utils/data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import type { DeflateOptions } from 'fflate'
import { strFromU8, strToU8, unzlibSync, zlibSync } from 'fflate/node'

/**
* Encode and compress data
*/
export const encodeData = (
data: string,
level: DeflateOptions['level'] = 6,
): string => {
const buffer = strToU8(data)
// zlib headers can be found at https://stackoverflow.com/a/54915442
const zipped = zlibSync(buffer, { level })
const binary = strFromU8(zipped, true)

return Buffer.from(binary, 'binary').toString('base64')
}

/**
* Decode and unzip data
*/
export const decodeData = (base64: string): string => {
const binary = Buffer.from(base64, 'base64').toString('binary')

return strFromU8(unzlibSync(strToU8(binary, true)))
}
1 change: 1 addition & 0 deletions tools/helper/src/node/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './data.js'
export * from './getInstalledStatus.js'
export * from './getRealPath.js'
export * from './logger.js'
Expand Down
49 changes: 49 additions & 0 deletions tools/helper/tests/data.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { describe, expect, it, vi } from 'vitest'
import {
encodeData as clientDecodeData,
decodeData as clientEncodeData,
} from '../src/client/utils/data.js'
import {
encodeData as nodeDecodeData,
decodeData as nodeEncodeData,
} from '../src/node/utils/data.js'

// mock define value
vi.stubGlobal('__VUEPRESS_SSR__', false)

const words = [
'Hello world.',
'Lorem ipsum dolor sit amet consectetur adipisicing elit. Dolores quasi consequatur sed accusamus repellendus mollitia dolore facilis perferendis provident, sint a tempora quos eveniet temporibus? Ipsum quasi adipisci voluptatibus fuga?',
'中国制造',
'昔人已乘黄鹤去⑵,此地空余黄鹤楼。黄鹤一去不复返,白云千载空悠悠⑶。晴川历历汉阳树⑷,芳草萋萋鹦鹉洲⑸。日暮乡关何处是⑹?烟波江上使人愁⑺',
]

const compressed = [
'eNrzSM3JyVcozy/KSdEDAB0WBGs=',
'eNoljkGOAyEMBL/SD1jtF3LJJVI+4UBPZAkwwWbeHybcSlZZXU8brNDusyJbsQHXgFQGkjVnCsYckKxdXZO2N1g0/nG/ZDo+U1y3u/BynRmS0nSp0zHYWQpbXlytrF+VvUQckrSoo3McHMu5eNipmS3+VklbKQjWbkPWkjl4sumK20d9Tb/h8avfITs0KU4rs4fEpeCYb7l9ATPWWYk=',
'eNp7smPt09l7n3Zse9mwAAA6rwiz',
'eNolj9sKgkAURX+9iwVWRgNFGoUVFvTQVUtndPBfpnOOM0/+QieE9bBh7/WwKVyCUpi/QIau9JxMcF4a8W6rGV0T3D6aiwIddRWdqm9v0GUoeryEIsAksPWS902kQQkMhlZrtmiwZ4z4sEJRhvkO52OGnr4LU9oLI3K27CS1gW8XU8bJs5M+ZS8jir+1PtHmBvKAoxT0ChOPwrsRsq3iZhhTeqRnDMUEdM0XyOsboX7tYJUm',
]

const browserUtoaResults = words.map((word) => clientDecodeData(word))
const browserAtouResults = compressed.map((data) => clientEncodeData(data))

const nodeUtoaResults = words.map((word) => nodeDecodeData(word))
const nodeAutoResults = compressed.map((data) => nodeEncodeData(data))

describe('props', () => {
it('utoa should return same result', () => {
expect(browserUtoaResults).toEqual(nodeUtoaResults)
})

it('atou should return same result', () => {
expect(browserAtouResults).toEqual(nodeAutoResults)
})

it('transform should return itself', () => {
expect(browserUtoaResults.map((word) => nodeEncodeData(word))).toEqual(
words,
)
expect(nodeUtoaResults.map((word) => nodeEncodeData(word))).toEqual(words)
})
})

0 comments on commit ebd6f63

Please sign in to comment.