
Research
Malicious npm Packages Impersonate Flashbots SDKs, Targeting Ethereum Wallet Credentials
Four npm packages disguised as cryptographic tools steal developer credentials and send them to attacker-controlled Telegram infrastructure.
cloudflare-workers-sse
Advanced tools
Elegant Server-Sent Events (SSE) Streaming for Cloudflare Workers.
Elegant Server-Sent Events (SSE) Streaming for Cloudflare Workers.
With npm
npm install cloudflare-workers-sse
With Yarn
yarn add cloudflare-workers-sse
With pnpm
pnpm add cloudflare-workers-sse
With Bun
bun add cloudflare-workers-sse
The implementation of SSE involves two components: a client that receives messages and a server (in this case, a worker) that publishes them.
Let's start with the worker.
import { sse } from "cloudflare-workers-sse";
export default {
fetch: sse(handler),
};
async function* handler(request: Request, env: Env, ctx: ExecutionContext) {
yield {
event: "greeting",
data: { text: "Hi there!" },
};
}
And that's basically it. All messages yielded are streamed to a client listening for them. Once there are no more messages, the stream is closed.
Although the simplest client-side implementation can be achieved using EventSource
, for more advanced scenarios — such as using POST
requests or handling authentication — it is recommended to use libraries such as @microsoft/fetch-event-source
.
const eventSource = new EventSource(
"https://<YOUR_WORKER_SUBDOMAIN>.workers.dev"
);
eventSource.addEventListener("greeting", (event) => {
// handle the greeting message
});
All messages yielded by a handler should conform to the SSEMessage
interface.
interface SSEMessage {
id?: string;
event?: string;
data?: null | boolean | number | bigint | string | Jsonifiable;
retry?: number;
}
data
can be of any primitive type (except Symbol
) or an object, in which case it will be converted to JSON. More information about Jsonifiable
can be found here. If data is omitted or set to undefined
or null
, the empty data
field will be added.
retry
sets the reconnection time in milliseconds. If specified, must be a positive integer, otherwise a RangeError
is thrown.
Errors thrown by a handler or during serialization can be caught via the onError
callback. It may also return a message to stream to the client before the connection closes.
import { sse } from "cloudflare-workers-sse";
export default {
fetch: sse(handler, {
onError: (error, request, env, ctx) => ({ event: "error_occurred" }),
}),
};
By default, only essential headers such as Content-Type
, Cache-Control
, and Connection
are included in the response. To send additional headers, use the customHeaders
option.
import { sse } from "cloudflare-workers-sse";
export default {
fetch: sse(handler, {
customHeaders: { "access-control-allow-origin": "https://example.com" },
}),
};
If your worker requires additional logic — such as request validation, response modification, or other pre/post-processing — you can implement a middleware.
import { type FetchHandler, sse } from "cloudflare-workers-sse";
export default {
fetch: middleware(sse(handler)),
};
function middleware<Env>(nextHandler: FetchHandler<Env>): FetchHandler<Env> {
return async function middlewareHandler(
request: Request,
env: Env,
ctx: ExecutionContext
) {
// a before logic
const response = await nextHandler(request, env, ctx);
// an after logic
return response;
};
}
FAQs
Elegant Server-Sent Events (SSE) Streaming for Cloudflare Workers.
The npm package cloudflare-workers-sse receives a total of 8 weekly downloads. As such, cloudflare-workers-sse popularity was classified as not popular.
We found that cloudflare-workers-sse demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Four npm packages disguised as cryptographic tools steal developer credentials and send them to attacker-controlled Telegram infrastructure.
Security News
Ruby maintainers from Bundler and rbenv teams are building rv to bring Python uv's speed and unified tooling approach to Ruby development.
Security News
Following last week’s supply chain attack, Nx published findings on the GitHub Actions exploit and moved npm publishing to Trusted Publishers.