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

Switch @bufbuild/connect-web to use @bufbuild/connect #471

Merged
merged 2 commits into from
Feb 17, 2023

Conversation

timostamm
Copy link
Member

@timostamm timostamm commented Feb 17, 2023

This updates @bufbuild/connect-web to share logic with @bufbuild/connect-node. Shared types like ConnectError, createPromiseClient, and others live in @bufbuild/connect, and are re-exported from the other packages.

This switch comes with the following improvements and bug fixes:

This also changes the interfaces Interceptor and Transport.

To understand this change, it is best to compare the old Transport with the new Transport. Transport now has a method stream() instead of just serverStream(), and this method takes an AsyncIterable<I> as input. This allows us to implement full duplex streaming via the Node.js http2 package.

This means that the types related to interceptors also had to change (old vs new), but note that a simple interceptor that adds a request header still looks the same:

function addHeaderInterceptor(value: string): Interceptor {
  return (next) => async (req) => {
    req.header.set("foo", value);
    return await next(req);
  };
}

And intercepting streams actually becomes more straight-forward, thanks to asynchronous iterables:

function streamLoggingInterceptor(): Interceptor {
  async function* logStream(stream: AsyncIterable<Message>) {
    for await (const m of stream) {
      console.log("saw streaming response message", m);
      yield m;
    }
  }
  return (next) => async (req) => {
    const res = await next(req);
    if (res.stream) {
      return {...res, message: logStream(res.message)};
    }
    console.log("saw unary response message", res.message);
    return res;
  };
}

@timostamm timostamm merged commit 9cec37f into main Feb 17, 2023
@timostamm timostamm deleted the tstamm/switch-connect-web-to-use-core-v2 branch February 17, 2023 16:57
@timostamm timostamm changed the title Switch @bufbuild/connect-web to use @bufbuild/connect-core Switch @bufbuild/connect-web to use @bufbuild/connect Feb 24, 2023
@timostamm timostamm mentioned this pull request Feb 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants