Skip to content

Commit

Permalink
Safe encoding of draft IDs in middle end
Browse files Browse the repository at this point in the history
  • Loading branch information
ericvicenti committed Jun 28, 2024
1 parent df5c31d commit a348e60
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 9 deletions.
21 changes: 15 additions & 6 deletions frontend/apps/desktop/src/app-drafts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ async function initDrafts() {
await fs.mkdir(draftsDir, {recursive: true})
await fs.readdir(draftsDir)
const allDraftFiles = await fs.readdir(draftsDir)
const allDraftIds = allDraftFiles.map((filename) => {
return filename.replace(/\.json$/, '')
})
const allDraftIds = allDraftFiles.map(draftFileNameToId)
draftIdList = allDraftIds
}

Expand All @@ -26,12 +24,23 @@ initDrafts()
console.error('[MAIN]: error preparing drafts', e)
})

function inputIdToDraftFile(id: string) {
const encodedId = Buffer.from(id).toString('base64')
return `${encodedId}.json`
}

function draftFileNameToId(filename: string) {
const baseName = filename.replace(/\.json$/, '')
const id = Buffer.from(baseName, 'base64').toString('utf-8')
return id
}

export const draftsApi = t.router({
list: t.procedure.query(async () => {
return draftIdList
}),
get: t.procedure.input(z.string().optional()).query(async ({input}) => {
const draftPath = join(draftsDir, `${input}.json`)
get: t.procedure.input(z.string()).query(async ({input}) => {
const draftPath = join(draftsDir, inputIdToDraftFile(input))
try {
const fileContent = await fs.readFile(draftPath, 'utf-8')
const draft = JSON.parse(fileContent)
Expand All @@ -51,7 +60,7 @@ export const draftsApi = t.router({
}),
)
.mutation(async ({input}) => {
const draftPath = join(draftsDir, `${input.id}.json`)
const draftPath = join(draftsDir, inputIdToDraftFile(input.id))
if (!draftIdList?.includes(input.id)) {
draftIdList?.push(input.id)
}
Expand Down
1 change: 0 additions & 1 deletion frontend/apps/desktop/src/components/sidebar-neo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ function _SidebarNeo() {
? ({ key: 'account', accountId: myAccount } as BaseAccountRoute)
: null
}, [myAccount])
console.log({ myAccountRoute })
const navigate = useNavigate()
const replace = useNavigate('replace')
let myAccountSection: ReactNode = null
Expand Down
1 change: 0 additions & 1 deletion frontend/apps/desktop/src/models/accounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ export function useAccountIsConnected(account: HMAccount) {

export function useMyAccount_deprecated() {
const accountKeys = useAccountKeys()
console.log(accountKeys.data)
if (!accountKeys.data) return null
if (!accountKeys.data.length) return null
if (accountKeys.data.length > 1)
Expand Down
1 change: 0 additions & 1 deletion frontend/apps/desktop/src/models/entities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,6 @@ export function useEntitiesContent(
return false
})
.filter((result) => !!result)
console.log('useEntitiesContent', routes, output)

return output
}

0 comments on commit a348e60

Please sign in to comment.