masto.js icon indicating copy to clipboard operation
masto.js copied to clipboard

createRestAPIClient in serverless function throws 'source.on is not a function' error

Open blackspike opened this issue 1 year ago • 2 comments

Hi! Tricky bug that only happens sometimes, I have a nuxt server route (nitro, see simplified code below) – I've isolated it to the createRestAPIClient.

If I hard code the values before the function it works fine, but creating it dynamically often throws an error. Might be related to https://github.com/neet/masto.js/issues/1029 but I'm not sure.

Weirdly it seems to work all the time in a netlify function, but not in nuxt. Thanks for the great lib!

import { createRestAPIClient } from "masto"

export default defineEventHandler(async (event) => {

  const { post, fediverseAccessToken, fediverseUrl } = getQuery(event)

  const masto = createRestAPIClient({
    accessToken: fediverseAccessToken,
    url: fediverseUrl,
  })

  const imageUrl = `https://www.w3.org/MarkUp/Test/xhtml-print/20050519/tests/jpeg420exif.jpg`
  const remoteFile = await fetch(imageUrl)

  // Create media from an URL
  const imageAttachment = await masto.v2.media.create({
    file: await remoteFile.blob(),
    description: 'todo',
  })

  // Publish!
  postResponse = await masto.v1.statuses.create({
    status: post,
    visibility: 'public',
    mediaIds: [imageAttachment.id],
  })

  return postResponse

})

error

[nuxt] [request error] [unhandled] [500] source.on is not a function
  at DelayedStream.create (./node_modules/delayed-stream/lib/delayed_stream.js:33:10)  
  at CombinedStream.append (./node_modules/combined-stream/lib/combined_stream.js:45:37)  
  at FormData.append (./node_modules/form-data/lib/form_data.js:74:3)  
  at SerializerNativeImpl.serialize (./node_modules/masto/dist/index.js:1112:30)  
  at HttpNativeImpl.createRequest (./node_modules/masto/dist/index.js:925:38)  
  at HttpNativeImpl.<anonymous> (./node_modules/masto/dist/index.js:892:34)  
  at Generator.next (<anonymous>)  
  at ./node_modules/masto/dist/index.js:76:71  
  at new Promise (<anonymous>)  
  at __awaiter (./node_modules/masto/dist/index.js:72:12)

blackspike avatar Feb 02 '24 10:02 blackspike

@blackspike Hello, thanks for reporting an issue. I'm still investigating it, but I suspect you are running it on a environment that doesn't have a native support for FormData, instead of a problem of #1029.

Node.js has introduced its native support for web APIs like FormData, fetch, and Blob in 18.0.0, so we have dropped supports for <18.x in a recent version to make it easier to integrate with browser bundle.

Your error message seems like using a non-native npm form-data module because the trace says ./node_modules/form-data/lib/form_data.js. It partially implements FormData so sometimes used as a polyfill for old environments but has a subtle difference, including the lack in stream support.

Can you make sure that you are using proper version for Node.js? Sorry for the inconvinience.

neet avatar Feb 05 '24 00:02 neet

Hi neet! Thanks for looking. Yeah it's a head scratcher as it sometimes works but sometimes throws that error. It's using native node fetch on node v18.18.2 so I can't see why it's not working. Like I say it also works fine when run in a netlify serverless function, but not in a nuxt server route, which makes me think nuxt is causing the error rather than your library, so I'll just use that.

Cheers!

blackspike avatar Feb 05 '24 14:02 blackspike

Closing as it doesn't seem to be a problem arising from Masto.js. Feel free to reopen if you think you still have a problem.

neet avatar Apr 28 '24 03:04 neet