Skip to content

Commit

Permalink
refactor: clean up protobuf usage
Browse files Browse the repository at this point in the history
  • Loading branch information
LouisBrunner committed Apr 5, 2021
1 parent 6f12846 commit e4733ae
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 23 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ Ce dépôt contient le code source de [https://mes-pronoms.vercel.app/](https://

- Tous les pronoms/articles
- Meilleure interface (https://material-ui.com/ vs https://blueprintjs.com/ vs https://react.semantic-ui.com/ vs https://evergreen.segment.com/ vs https://ant.design/)
- Finish shortForm

## Remerciements et références

Expand Down
25 changes: 3 additions & 22 deletions logic/storage/format/pb.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,15 @@
/* eslint-disable @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call */
import {util} from 'protobufjs/light'
import {emptyStorage, PronounsStorage} from 'logic/storage/types'
import {WireFormat} from 'logic/storage/format/types'
import {PronounList} from 'logic/types'
import {ensureChoice, parseChoice} from 'logic/business'
import protobuf from './format.proto'
import {ConfigMessage} from './format.proto'
import Pbf from 'pbf'
import pako from 'pako'

// FIXME: mix-matching the proto libraries because neither are good

const proto = {
loaded: false,
Pronoun: undefined,
Config: undefined,
}
const loadDefinitions = (): void => {
if (proto.loaded) {
return
}
proto.Config = protobuf.ConfigMessage
proto.Pronoun = protobuf.PronounMessage
proto.loaded = true
}

const compress = (store: PronounsStorage): string => {
loadDefinitions()

const pronouns = []
for (const pronoun of PronounList) {
const current = store.pronouns[pronoun]
Expand All @@ -44,7 +27,7 @@ const compress = (store: PronounsStorage): string => {

const obj = {pronouns}
const pbf = new Pbf()
proto.Config.write(obj, pbf)
ConfigMessage.write(obj, pbf)
const buffer = pako.deflate(pbf.finish())
return encodeURIComponent(util.base64.encode(buffer, 0, buffer.length))
}
Expand All @@ -60,13 +43,11 @@ type PronounObject = {
}

const decompress = (plain: string): PronounsStorage => {
loadDefinitions()

const bigBuffer = new Uint8Array(plain.length)
const len = util.base64.decode(decodeURIComponent(plain), bigBuffer, 0)
const buffer = pako.inflate(bigBuffer.subarray(0, len))
const pbf = new Pbf(buffer)
const obj = proto.Config.read(pbf) as ConfigObject
const obj = ConfigMessage.read(pbf) as ConfigObject
if (obj.pronouns.length > PronounList.length) {
throw new Error(`invalid number of pronouns '${obj.pronouns.length} vs ${PronounList.length}'`)
}
Expand Down
26 changes: 26 additions & 0 deletions package-lock.json

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

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
"@testing-library/react-hooks": "^5.1.1",
"@types/lz-string": "^1.3.34",
"@types/node": "^14.14.37",
"@types/pako": "^1.0.1",
"@types/pbf": "^3.0.2",
"@types/react": "^17.0.3",
"@types/styled-components": "^5.1.9",
"@typescript-eslint/eslint-plugin": "^4.20.0",
Expand Down

0 comments on commit e4733ae

Please sign in to comment.