Skip to content

Commit

Permalink
Minor clode cleanup in makeTarFile
Browse files Browse the repository at this point in the history
  • Loading branch information
lpsinger committed Sep 6, 2023
1 parent 02a970e commit 823a6c1
Showing 1 changed file with 14 additions and 20 deletions.
34 changes: 14 additions & 20 deletions app/routes/circulars/circulars.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,30 +270,36 @@ export async function circularRedirect(query: string) {
}
}

async function* getAllRecords(): AsyncGenerator<Circular[], void, unknown> {
async function* getAllRecords() {
const db = await tables()
const client = db._doc as unknown as DynamoDBDocument
const TableName = db.name('circulars')
const pages = paginateScan({ client }, { TableName })

for await (const page of pages) {
const items: Circular[] = page.Items as Circular[]
yield items
yield page.Items as Circular[]
}
}

const formatters: Record<string, (circular: Circular) => string> = {
json({ sub, ...circular }) {
return JSON.stringify(circular, null, 2)
},
txt: formatCircular,
}

export async function makeTarFile(fileType: 'json' | 'txt'): Promise<Blob> {
return new Promise(async (resolve, reject) => {
const formatter = formatters[fileType]
const pack = tarPack()
const chunks: Uint8Array[] = []
const chunks: Buffer[] = []

pack.on('data', (chunk) => {
chunks.push(chunk)
})

pack.on('end', () => {
const tarData = new Uint8Array(Buffer.concat(chunks))
resolve(new Blob([tarData], { type: 'application/x-tar' }))
resolve(new Blob(chunks, { type: 'application/x-tar' }))
})

pack.on('error', (err) => {
Expand All @@ -302,20 +308,8 @@ export async function makeTarFile(fileType: 'json' | 'txt'): Promise<Blob> {

for await (const circularArray of getAllRecords()) {
for (const circular of circularArray) {
if (fileType === 'txt') {
const txt_entry = pack.entry(
{ name: `archive.txt/${circular.circularId}.txt` },
formatCircular(circular)
)
txt_entry.end()
} else if (fileType === 'json') {
delete circular.sub
const json_entry = pack.entry(
{ name: `archive.json/${circular.circularId}.json` },
JSON.stringify(circular, null, 2)
)
json_entry.end()
}
const name = `archive.${fileType}/${circular.circularId}.${fileType}`
pack.entry({ name }, formatter(circular)).end()
}
}

Expand Down

0 comments on commit 823a6c1

Please sign in to comment.