From 846e0c2863ad4c4c6ccc7484056ea982395d7198 Mon Sep 17 00:00:00 2001 From: Daniel Naab Date: Thu, 7 Nov 2024 14:14:16 -0600 Subject: [PATCH] Remove pdf bytes from blueprint object --- .../forms/src/context/browser/form-repo.ts | 15 +------- packages/forms/src/documents/document.ts | 3 -- .../patterns/package-download/submit.test.ts | 1 - .../src/patterns/package-download/submit.ts | 37 ++++++++++--------- packages/forms/src/repository/add-form.ts | 3 +- .../forms/src/repository/get-form.test.ts | 3 +- packages/forms/src/repository/get-form.ts | 19 +--------- .../forms/src/repository/save-form.test.ts | 3 +- packages/forms/src/repository/save-form.ts | 3 +- packages/forms/src/repository/serialize.ts | 21 ----------- packages/forms/src/types.ts | 1 - 11 files changed, 26 insertions(+), 83 deletions(-) delete mode 100644 packages/forms/src/repository/serialize.ts diff --git a/packages/forms/src/context/browser/form-repo.ts b/packages/forms/src/context/browser/form-repo.ts index a2008bd7..63b5646b 100644 --- a/packages/forms/src/context/browser/form-repo.ts +++ b/packages/forms/src/context/browser/form-repo.ts @@ -120,7 +120,7 @@ export class BrowserFormRepository implements FormRepository { async saveForm(formId: string, form: Blueprint): Promise { try { - this.storage.setItem(formKey(formId), stringifyForm(form)); + this.storage.setItem(formKey(formId), JSON.stringify(form)); } catch { return failure(`error saving '${formId}' to storage`); } @@ -179,7 +179,7 @@ export const getFormList = (storage: Storage) => { export const saveForm = (storage: Storage, formId: string, form: Blueprint) => { try { - storage.setItem(formKey(formId), stringifyForm(form)); + storage.setItem(formKey(formId), JSON.stringify(form)); } catch { return { success: false as const, @@ -191,17 +191,6 @@ export const saveForm = (storage: Storage, formId: string, form: Blueprint) => { }; }; -const stringifyForm = (form: Blueprint) => { - return JSON.stringify({ - ...form, - outputs: form.outputs.map(output => ({ - ...output, - // TODO: we probably want to do this somewhere in the documents module - data: uint8ArrayToBase64(output.data), - })), - }); -}; - const parseStringForm = (formString: string): Blueprint => { const form = JSON.parse(formString) as Blueprint; return { diff --git a/packages/forms/src/documents/document.ts b/packages/forms/src/documents/document.ts index ee5bb18c..5b964c12 100644 --- a/packages/forms/src/documents/document.ts +++ b/packages/forms/src/documents/document.ts @@ -33,7 +33,6 @@ export const addParsedPdfToForm = async ( form = addPatternMap(form, document.extract.patterns, document.extract.root); const updatedForm = addFormOutput(form, { id: document.id, - data: new Uint8Array(), // TODO: remove this no-longer-used field path: document.label, fields: document.extract.outputs, formFields: Object.fromEntries( @@ -71,7 +70,6 @@ export const addDocument = async ( form = addPatternMap(form, parsedPdf.patterns, parsedPdf.root); const updatedForm = addFormOutput(form, { id: 'document-1', // TODO: generate a unique ID - data: fileDetails.data, path: fileDetails.name, fields: parsedPdf.outputs, formFields: Object.fromEntries( @@ -89,7 +87,6 @@ export const addDocument = async ( const formWithFields = addDocumentFieldsToForm(form, fields); const updatedForm = addFormOutput(formWithFields, { id: 'document-1', // TODO: generate a unique ID - data: fileDetails.data, path: fileDetails.name, fields, // TODO: for now, reuse the field IDs from the PDF. we need to generate diff --git a/packages/forms/src/patterns/package-download/submit.test.ts b/packages/forms/src/patterns/package-download/submit.test.ts index 78659c50..c0815231 100644 --- a/packages/forms/src/patterns/package-download/submit.test.ts +++ b/packages/forms/src/patterns/package-download/submit.test.ts @@ -105,7 +105,6 @@ const createTestForm = async (): Promise => { { id: 'test-id', path: 'test.pdf', - data: new Uint8Array(pdfBytes), fields: { [input1.id]: { type: 'TextField', diff --git a/packages/forms/src/patterns/package-download/submit.ts b/packages/forms/src/patterns/package-download/submit.ts index 8baeeb60..462db543 100644 --- a/packages/forms/src/patterns/package-download/submit.ts +++ b/packages/forms/src/patterns/package-download/submit.ts @@ -14,23 +14,24 @@ export const downloadPackageHandler: SubmitHandler< return failure('Form is not complete'); } - const outputsResult: Result = await Promise.all( - opts.session.form.outputs.map(async output => { - const doc = await context.getDocument(output.id); - if (!doc.success) { - throw new Error(doc.error); - } - return { - id: output.id, - data: doc.data.data, - path: doc.data.path, - fields: output.fields, - formFields: output.formFields, - } satisfies FormOutput; - }) - ) - .then(values => success(values)) - .catch(error => failure(error)); + const outputsResult: Result<(FormOutput & { data: Uint8Array })[]> = + await Promise.all( + opts.session.form.outputs.map(async output => { + const doc = await context.getDocument(output.id); + if (!doc.success) { + throw new Error(doc.error); + } + return { + id: output.id, + path: doc.data.path, + fields: output.fields, + formFields: output.formFields, + data: doc.data.data, + }; + }) + ) + .then(values => success(values)) + .catch(error => failure(error)); if (!outputsResult.success) { return failure(outputsResult.error); } @@ -50,7 +51,7 @@ export const downloadPackageHandler: SubmitHandler< }; const generateDocumentPackage = async ( - outputs: FormOutput[], + outputs: (FormOutput & { data: Uint8Array })[], formData: Record ) => { const errors = new Array(); diff --git a/packages/forms/src/repository/add-form.ts b/packages/forms/src/repository/add-form.ts index 34370752..ec617b1c 100644 --- a/packages/forms/src/repository/add-form.ts +++ b/packages/forms/src/repository/add-form.ts @@ -2,7 +2,6 @@ import { type Result, failure, success } from '@atj/common'; import { type DatabaseContext } from '@atj/database'; import { type Blueprint } from '../index.js'; -import { stringifyForm } from './serialize.js'; export type AddForm = ( ctx: DatabaseContext, @@ -16,7 +15,7 @@ export const addForm: AddForm = async (ctx, form) => { .insertInto('forms') .values({ id: uuid, - data: stringifyForm(form), + data: JSON.stringify(form), }) .execute() .then(() => diff --git a/packages/forms/src/repository/get-form.test.ts b/packages/forms/src/repository/get-form.test.ts index 82acc0bb..33734a8b 100644 --- a/packages/forms/src/repository/get-form.test.ts +++ b/packages/forms/src/repository/get-form.test.ts @@ -12,7 +12,7 @@ describeDatabase('getForm', () => { .insertInto('forms') .values({ id: '45c66187-64e2-4d75-a45a-e80f1d035bc5', - data: '{"summary":{"title":"Title","description":"Description"},"root":"root","patterns":{"root":{"type":"sequence","id":"root","data":{"patterns":[]}}},"outputs":[{"id":"test-id","data":"AQID","path":"test.pdf","fields":{},"formFields":{}}]}', + data: '{"summary":{"title":"Title","description":"Description"},"root":"root","patterns":{"root":{"type":"sequence","id":"root","data":{"patterns":[]}}},"outputs":[{"id":"test-id","path":"test.pdf","fields":{},"formFields":{}}]}', }) .execute(); @@ -40,7 +40,6 @@ const TEST_FORM: Blueprint = { outputs: [ { id: 'test-id', - data: new Uint8Array([1, 2, 3]), path: 'test.pdf', fields: {}, formFields: {}, diff --git a/packages/forms/src/repository/get-form.ts b/packages/forms/src/repository/get-form.ts index e144162b..ce961499 100644 --- a/packages/forms/src/repository/get-form.ts +++ b/packages/forms/src/repository/get-form.ts @@ -23,22 +23,5 @@ export const getForm: GetForm = async (ctx, formId) => { }; const parseStringForm = (formString: string): Blueprint => { - const form = JSON.parse(formString) as Blueprint; - return { - ...form, - outputs: form.outputs.map((output: any) => ({ - ...output, - data: base64ToUint8Array((output as any).data), - })), - }; -}; - -const base64ToUint8Array = (base64: string): Uint8Array => { - const binaryString = atob(base64); - const len = binaryString.length; - const bytes = new Uint8Array(len); - for (let i = 0; i < len; i++) { - bytes[i] = binaryString.charCodeAt(i); - } - return bytes; + return JSON.parse(formString); }; diff --git a/packages/forms/src/repository/save-form.test.ts b/packages/forms/src/repository/save-form.test.ts index 4465bac9..098ccbd7 100644 --- a/packages/forms/src/repository/save-form.test.ts +++ b/packages/forms/src/repository/save-form.test.ts @@ -24,7 +24,6 @@ const TEST_FORM: Blueprint = { outputs: [ { id: 'test-id', - data: new Uint8Array([1, 2, 3]), path: 'test.pdf', fields: {}, formFields: {}, @@ -56,7 +55,7 @@ describeDatabase('saveForm', () => { expect(result[0].id).toEqual(addResult.data.id); expect(result[0].data).toEqual( - '{"summary":{"title":"Updated title","description":"Updated description"},"root":"root","patterns":{"root":{"type":"sequence","id":"root","data":{"patterns":[]}}},"outputs":[{"id":"test-id","data":"AQID","path":"test.pdf","fields":{},"formFields":{}}]}' + '{"summary":{"title":"Updated title","description":"Updated description"},"root":"root","patterns":{"root":{"type":"sequence","id":"root","data":{"patterns":[]}}},"outputs":[{"id":"test-id","path":"test.pdf","fields":{},"formFields":{}}]}' ); }); }); diff --git a/packages/forms/src/repository/save-form.ts b/packages/forms/src/repository/save-form.ts index 18365c80..49323773 100644 --- a/packages/forms/src/repository/save-form.ts +++ b/packages/forms/src/repository/save-form.ts @@ -2,7 +2,6 @@ import { type VoidResult, failure, success } from '@atj/common'; import { type DatabaseContext } from '@atj/database'; import { type Blueprint } from '../index.js'; -import { stringifyForm } from './serialize.js'; export type SaveForm = ( ctx: DatabaseContext, @@ -16,7 +15,7 @@ export const saveForm: SaveForm = async (ctx, id, blueprint) => { return await db .updateTable('forms') .set({ - data: stringifyForm(blueprint), + data: JSON.stringify(blueprint), }) .where('id', '=', id) .execute() diff --git a/packages/forms/src/repository/serialize.ts b/packages/forms/src/repository/serialize.ts deleted file mode 100644 index 11e32283..00000000 --- a/packages/forms/src/repository/serialize.ts +++ /dev/null @@ -1,21 +0,0 @@ -import { type Blueprint } from '..'; - -export const stringifyForm = (form: Blueprint) => { - return JSON.stringify({ - ...form, - outputs: form.outputs.map((output: any) => ({ - ...output, - // TODO: we probably want to do this somewhere in the documents module - data: uint8ArrayToBase64(output.data), - })), - }); -}; - -const uint8ArrayToBase64 = (buffer: Uint8Array): string => { - let binary = ''; - const len = buffer.byteLength; - for (let i = 0; i < len; i++) { - binary += String.fromCharCode(buffer[i]); - } - return btoa(binary); -}; diff --git a/packages/forms/src/types.ts b/packages/forms/src/types.ts index 33e7efa4..d27d6bc9 100644 --- a/packages/forms/src/types.ts +++ b/packages/forms/src/types.ts @@ -15,7 +15,6 @@ export type FormSummary = { export type FormOutput = { id: string; - data: Uint8Array; path: string; fields: DocumentFieldMap; formFields: Record;