PaddleHQ / paddle-node-sdk

Node.js SDK for working with the Paddle API in server-side apps.
https://developer.paddle.com/
Apache License 2.0
42 stars 6 forks source link

[Bug]: Not supported to run in Cloudflare Worker Edge environment #37

Open wytxer opened 3 months ago

wytxer commented 3 months ago

What happened?

Using the Next.js 14 + TypeScript tech stack, with the API set to the edge environment, an error occurs when calling paddle.transactions.create after importing the @paddle/paddle-node-sdk package: Module build failed: UnhandledSchemeError: Reading from "node:crypto".

Steps to reproduce

Reproduction code snippet:

// route.ts

import { Environment, Paddle } from '@paddle/paddle-node-sdk'

export const runtime = 'edge'

const paddle = new Paddle(process.env.PADDLE_SECRET_KEY!, {
  environment: Environment.production,
})

const transaction = await paddle.transactions.create({
  items: [
    {
      quantity: 1,
      priceId: 'priceId',
    },
  ],
})

What did you expect to happen?

I hope to use @paddle/paddle-node-sdk in an edge environment.

Logs

⨯ node:crypto
Module build failed: UnhandledSchemeError: Reading from "node:crypto" is not handled by plugins (Unhandled scheme).
Webpack supports "data:" and "file:" URIs by default.
You may need an additional plugin to handle "node:" URIs.
Import trace for requested module:
node:crypto
node_modules/@paddle/paddle-node-sdk/dist/esm/internal/api/client.js
node_modules/@paddle/paddle-node-sdk/dist/esm/paddle.js
node_modules/@paddle/paddle-node-sdk/dist/esm/index.js
vijayasingam-paddle commented 3 months ago

Hi @wytxer, Thank you for opening this bug report with all the details. I am sorry that you are running into this issue.

Our node SDK was built for Node.js Runtime, as Edge runtime uses web API some of the dependencies we use like the crypto module will not work.

Even though removing the node: prefix might solve the problem for you, I fear it will break for others. So that is not an option.

I will find a solution to this problem and get back to you once it is released.

Thank you.

wytxer commented 3 months ago

Thank you for your reply. I am building a product based on Cloudflare Worker. Is there a plan for when it will be released? @vijayasingam-paddle

vijayasingam-paddle commented 2 months ago

Hi @wytxer, I am afraid that it will take a while for us to change this as we had not planned to run our SDK using web APIs and supporting both runtimes will be tricky.

We do support CloudFlare workers using NodeJS runtime. However, looking at your snippet, you are deploying a NextJS app to CloudFlare workers which as I understand can only run on an edge runtime.

If you don't mind me asking, is there any particular reason not to deploy this in Vercel or Netlify where the infra plays well with NextJS applications compared to CF workers?

Thank you.

westlinkin commented 2 months ago

@vijayasingam-paddle I'm trying to implement paddle in Cloudflare workers, As you mentioned,

We do support CloudFlare workers using NodeJS runtime.

I'm planning to use Cloudflare workers as the backend, so could you let me know if this works?

vijayasingam-paddle commented 2 months ago

Hi @westlinkin, Yes, our SDK works in CloudFlare workers using NodeJS runtime. This issue is relevant only to the Edge runtime

Please feel free to reach out to us if you face any difficulties while integrating with your application.

Thank you.

richhost commented 2 months ago

Oh, I use CloudFlare Worker as the backend, node:crypto prevents me continuing.

vijayasingam-paddle commented 2 months ago

Hi @richhost, We haven't made any progress on this yet. As we had not planned to support this, it is taking longer to prepare for this change.

Meanwhile, can you please try version 1.2.2? We were using crypto as a dependency at this point so it might work for you. There are a few functional differences between 1.2.2 and the latest version. You can read through our Changelog to find the differences

I will get back to you once a proper fix is released.

Thank you.

wytxer commented 2 months ago

Hi @wytxer, I am afraid that it will take a while for us to change this as we had not planned to run our SDK using web APIs and supporting both runtimes will be tricky.

We do support CloudFlare workers using NodeJS runtime. However, looking at your snippet, you are deploying a NextJS app to CloudFlare workers which as I understand can only run on an edge runtime.

If you don't mind me asking, is there any particular reason not to deploy this in Vercel or Netlify where the infra plays well with NextJS applications compared to CF workers?

Thank you.

Because the website that is already online is based on Cloudflare Worker, migrating the entire site to a platform like Vercel to use the Paddle API would be very costly.

wytxer commented 2 months ago

@vijayasingam-paddle I'm trying to implement paddle in Cloudflare workers, As you mentioned,

We do support CloudFlare workers using NodeJS runtime.

I'm planning to use Cloudflare workers as the backend, so could you let me know if this works?

Bro, you can consider trying the version I forked: https://github.com/wytxer/paddle-node-sdk. I changed crypto to crypto-js, and one of my projects is using it. The API is completely consistent with the official version. You can click to check my modification history.

npm i @wytxer/paddle-node-sdk

Abel1011 commented 2 months ago

Hello, you could try this in your next.config.js:

webpack: (config) => {
    config.externals.push({
      'node:crypto': 'commonjs crypto',
    });
    return config;
  },