Skip to content

Conversation

@m1212e
Copy link

@m1212e m1212e commented Oct 25, 2025

Following up to this discussion here is my suggestion of a max lifetime implementation for SOFA webhooks.

  • switched to use function style instead of class so it aligns more with the rest of the codebase
  • add option for termination messages which will get sent to the webhook before it ends
  • add option for maximum lifetime of webhooks

Let me know what you think, I'm looking forward to your feedback!

import { parseVariable } from './parse.js';
import { logger } from './logger.js';
import { ObjMap } from 'graphql/jsutils/ObjMap.js';
import { pipeline, Writable } from 'readable-stream';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In order to keep the implementation environment agnostic, is it possible to use WritableStream from @whatwg-node/fetch that we use across the library instead.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thats what I kinda was going for with readable-stream. They use the native stream implementation if available and fall back to userland implementation if they aren't. But I'll try your suggestion with the fetch package, but I think they do not provide a pipeline operator which automatically handles backpressure etc.?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I kinda ran into some issues so I think it would be the most straight forward thing to rely on the readable-stream package if this is not an issue from your side!

Copy link
Member

@ardatan ardatan Oct 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I see on npm, it is a mirror of the builtin streams pacakge of Node. And if we have this package in SOFA, we will no longer support non-Node environments. I'd prefer to have an agnostic solution with the standard ReadableStream, WriteableStream etc.
I think for maximum lifetime, we can rely on setTimeout and call of stop in case of timeout instead of refactoring whole logic with Node-specific API.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the stop part, this is what currently is happening, I use setTimeout to schedule a run for termination later on.
Stream wise, the support for the api is pretty good: https://developer.mozilla.org/en-US/docs/Web/API/WritableStream#browser_compatibility
Node, Bun and Deno all support the api natively. For browser and backwards consistency, the readable-stream package does the heavy lifting.
Would you prefer the implementation to use the native streams API directly?

Copy link
Member

@ardatan ardatan Oct 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But unfortunately this has the same problem. stream package is not available on non-Node environment. If we merge this as-is. We will no longer be able to support non-Node environments like we do today.
There are other non-Node evnironments that don't support Node-specific builtin packages yet such as CF Workers(it has some kind of support with custom flags which add some overhead).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I'm not mistaken, at least CF workers follows the WinterTC API standard, which does support WritableStreams: https://min-common-api.proposal.wintertc.org/#api-index / https://github.com/cloudflare/workerd/blob/46fd550189f0016933ae6babb4397a8576424f26/src/workerd/api/node/tests/streams-test.js#L29

But nontheless I see your point. Would a runtime check for the api availability + a dynamic import of the api in case it is available erase your concerns?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WriteableStreams are not Writable of Node.js. They are different APIs.
What if api is not available for the environment then we will disable the webhooks feature for that environment, right?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. I fully removed the use of streams.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, did you have the chance to take a look yet? Would you like me to change or improve something?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants