julianhille / MuhammaraJS

Muhammara a node module with c/cpp bindings to modify PDF with js for node or electron (based/replacement on/of galkhana/hummusjs)
Other
205 stars 41 forks source link

New Recipe from buffer throws "path" error #349

Closed luicfrr closed 7 months ago

luicfrr commented 8 months ago

Here's my code:

const pdfBuffer = Buffer.from( 'base64string...', 'base64' )
const newBuffer = await new Promise<Buffer>( async ( res ) => {
    const pdfDoc = new Recipe( pdfBuffer, {
        author: 'test',
        title: 'pdf test',
        subject: 'test on muhammara'
    } )
    pdfDoc.endPDF( buffer => res( buffer ) )
} )

Runnint this code I get the following error:

TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string or an instance of Buffer or URL. Received an instance of Object
      at Object.openSync (node:fs:593:10)
      at Object.writeFileSync (node:fs:2249:35)
      at Recipe.endPDF (path/to/backend/node_modules/muhammara/lib/Recipe.js:262:10)
      at newBuffer (path/to/backend/build/apps/api/index.js:66:16)
      at new Promise (<anonymous>)
      at path/to/backend/build/apps/api/index.js:60:29 {
    code: 'ERR_INVALID_ARG_TYPE'
 }
 Node.js v18.16.0

Reading docs i think there's an error on it because I can't add null as seccond param while creating new Recipe.

luicfrr commented 8 months ago

After some tests I suppose the error is in this type definition.

I have no function error (only typescript error) if I add an undefined as second param of my function:

const pdfBuffer = Buffer.from( 'base64string...', 'base64' )
const newBuffer = await new Promise<Buffer>( async ( res ) => {
    const pdfDoc = new Recipe( pdfBuffer, undefined, {
        author: 'test',
        title: 'pdf test',
        subject: 'test on muhammara'
    } )
    pdfDoc.endPDF( buffer => res( buffer ) )
} )

I think Recipe type declaration should be something like this:

export class Recipe {
    constructor(src: string, output?: string | null, options?: Recipe.RecipeOptions);
    constructor(buffer: Buffer, output?: string | null, options?: Recipe.RecipeOptions);
....
luicfrr commented 7 months ago

fixed in #354