Skip to content
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

fix: SSE data reporting #1298

Merged
merged 2 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions packages/binding-http/src/subscription-protocols.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,7 @@ export class SSESubscription implements InternalSubscription {
};
this.eventSource.onmessage = (event) => {
debug(`HttpClient received ${JSON.stringify(event)} from ${this.form.href}`);
const output = new Content(
this.form.contentType ?? ContentSerdes.DEFAULT,
Readable.from(JSON.stringify(event))
);
const output = new Content(this.form.contentType ?? ContentSerdes.DEFAULT, Readable.from(event.data));
danielpeintner marked this conversation as resolved.
Show resolved Hide resolved
next(output);
};
this.eventSource.onerror = function (event) {
Expand Down
12 changes: 11 additions & 1 deletion packages/binding-http/test/http-client-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import * as http from "http";
import { Content, DefaultContent, ContentSerdes, createLoggers, ProtocolServer } from "@node-wot/core";

import { Readable } from "stream";
import { text } from "node:stream/consumers";

import HttpClient from "../src/http-client";
import { HttpForm } from "../src/http";
Expand All @@ -37,6 +38,7 @@ import FakeTimers from "@sinonjs/fake-timers";

// Add spies
import spies from "chai-spies";
import { fail } from "assert";

const { debug } = createLoggers("binding-http", "http-client-test");

Expand Down Expand Up @@ -425,6 +427,8 @@ class HttpClientTest1 {
@suite("HTTP client subscriptions")
class HttpClientTest2 {
@test "should register to sse server and get server sent event"(done: Mocha.Done) {
let dataCheckError: string | undefined;

// create sse server
const clock = FakeTimers.install();
const app = express();
Expand All @@ -445,6 +449,9 @@ class HttpClientTest2 {
clearInterval(pusher);
sseStream.unpipe(res);
done();
if (dataCheckError !== undefined) {
fail(dataCheckError);
}
});
});

Expand All @@ -463,7 +470,10 @@ class HttpClientTest2 {
};

client
.subscribeResource(form, (data) => {
.subscribeResource(form, async (data) => {
if ((await text(data.body)) !== "Test event") {
dataCheckError = "Data should report 'Test event'";
}
client.unlinkResource(form);
server.close();
clock.uninstall();
Expand Down
Loading