-
-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow customization of output format #63
Comments
Hello @ccremer , thank you for the issue. Unfortunately the protocol details cannot be modified in such a way. The sveltekit-sse/src/lib/produce.js Lines 38 to 42 in e5ade90
The spec itself doesn't say anything about uri encoding, but without uri encoding messages would split into chunks on new lines. It would be a pretty large overhead to then reconstruct the original message on the client side. If you want to consume these streams from a CLI you will need to implement an SSE client that follows the spec. Once you've done that, you just need to decode each message as uri and you'll get the original string. sveltekit-sse/src/lib/source.js Lines 34 to 36 in e5ade90
NoteIf your CLI is indeed also made with NodeJs, Bun, etc, I think they all support the Note I just tried using Let me know if this answers your question. |
Thanks for answer and explanations. async function createSseClient(url: string, apiKey: string) {
try {
const response = await fetch(url, {
headers: {
authorization: `Bearer ${apiKey}`,
},
method: 'POST',
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const reader = response?.body?.getReader();
while (reader && true) {
const { value, done } = await reader.read();
if (done) {
break;
}
const decoder = new TextDecoder();
const text = decoder.decode(value);
const lines = text.split('\n');
for (const line of lines) {
if (line.trim() === '') {
continue; // Skip empty lines
}
const [key, ...valueParts] = line.split(':');
if (key === 'data') {
const data = valueParts.join(':').trim();
console.log(data);
}
}
}
} catch (error) {
console.error('Error connecting to SSE stream:', error);
}
}
const API_ACCESS_KEY = process.env.API_ACCESS_KEY;
if (!API_ACCESS_KEY) {
throw new Error('API_ACCESS_KEY environment variable is required.');
}
const url = process.argv.slice(2)[0];
if (!url) {
throw new Error('URL of migration endpoint is required as first argument.');
}
createSseClient(url, API_ACCESS_KEY); This code just prints the lines to console. Anyway, thanks for the help. |
Hi,
Thanks for this library, greatly appreciate the effort!
Is there a way to customize the output format of the events transmitted over the wire?
When activating the endpoint over curl, curl sees this:
It would be super helpful to have the server already formatted with different formats:
plaintext:
JSON (1 per line):
tsv:
csv:
(at least plaintext and JSON would be helpful to start with)
This would be easier to integrate with CLI tools to process events.
I envision a scenario where I could set the format as a query parameter, e.g.
http://localhost:5173/api/v1/sse?format=json
, where the server-side-code would then read the query parameter and set the format in the producer accordingly. The current format could remain as default to not break existing implementations.The text was updated successfully, but these errors were encountered: