Giter VIP home page Giter VIP logo

Comments (10)

sam-potter avatar sam-potter commented on May 16, 2024 8

Hey there,

I had the same issue and after a few hours of pissing around I finally worked out why it stalls. I'm running a serverless function that had some body parser middleware running. Basically you just need to turn that off.

Under the hood, when you call Shopify.Webhooks.Registry.process(), the req.on() event callbacks would be messed up by the body parser and stall the request.

Might not be the exact problem for you, but worth giving it a try.

from shopify-api-js.

dan-turner avatar dan-turner commented on May 16, 2024 8

It would be great if we didn't need the work around to disable the body parser for this to work. Is this possible at all?

from shopify-api-js.

aditodkar avatar aditodkar commented on May 16, 2024 2

This issue is mainly because of body parser or express.json() middleware. Try this code to exclude body parser or express.json()

function excludeShopifyWebhookEndpoint(fn) {
    return function (req, res, next) {
        if (req.path === '/api/webhooks' && req.method === 'POST') {
            next();
        } else {
            fn(req, res, next);
        }
    }
}

app.use(excludeShopifyWebhookEndpoint(express.json()));

Note: Above solution is applicable for stripe and shopify webhooks :)

from shopify-api-js.

thibautvdu avatar thibautvdu commented on May 16, 2024

I'm having the same issue : timeout on await Shopify.Webhooks.Registry.register.

Relevant code :

Shopify.Webhooks.Registry.webhookRegistry = [
  {
    path: "/handleWebhooks",
    topic: "APP_UNINSTALLED",
    webhookHandler: handleUninstall.bind(this)
  },
  {
    path: "/handleWebhooks",
    topic: "THEMES_UPDATE",
    webhookHandler: handleThemeChange.bind(this)
  }
];

export const handleWebhooks = async (
  req: functions.https.Request,
  res: functions.Response<any>
) => {
  try{
    if(!Shopify.Webhooks.Registry.isWebhookPath(req.path)) {
      functions.logger.error("not a webhook path", req.path);
      return;
    }

    await Shopify.Webhooks.Registry.process(req, res);
  } catch(error) {
    functions.logger.error("failed processing webhook", error);
  }
}

from shopify-api-js.

thibautvdu avatar thibautvdu commented on May 16, 2024

@saampotter

Thanks a lot ! After digging a bit in the code, I was suspecting something of that flavor with the req.on callbacks.
The issue is that I'm using firebase functions, so if there is indeed some kind of body parsing, I can't do much about it...

I ended up re-implementing the hmac check and WebhookRegistry.process feature myself, but I'd prefer to stick with the shpify way of things.

from shopify-api-js.

atiqueahmedziad avatar atiqueahmedziad commented on May 16, 2024

@saampotter I just faced the same issue using body parser as middleware. Did you able to find any workaround ?

from shopify-api-js.

sam-potter avatar sam-potter commented on May 16, 2024

@atiqueahmedziad I simply disabled body parser middleware for the specific webhook route. Ended up implementing the HMAC verification myself because I'm not a fan of the API design of this package.

from shopify-api-js.

bishpls avatar bishpls commented on May 16, 2024

+1 for the request to have Shopify.Webhooks.Registry.process not reliant on req.on('end'), which causes it to silently break when using bodyParser or express.json(). (Or, at bare, minimum, to include a warning about this in the documentation! It's a pretty gnarly dependency to not call attention to in the developer onboarding guide)

from shopify-api-js.

pnyennhi avatar pnyennhi commented on May 16, 2024
router.post('/webhooks', async (ctx) => {
      try {
        await Shopify.Webhooks.Registry.process(ctx.req, ctx.res);
        console.log(`Webhook processed, returned status code 200`);
      } catch (error) {
        console.log(`Failed to process webhook: ${error}`);
      }
    });

    server.use(bodyParser());

I put the bodyParser below the route but it still hangs forever.
How can I disable the bodyparser without removing the code line?

from shopify-api-js.

mkevinosullivan avatar mkevinosullivan commented on May 16, 2024

Documentation regarding body parsers here

Note that webhook processing in v6.0.0 will be changing (along with just about everything else!) ... release candidate v6.0.0-rc1 is available.

from shopify-api-js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.