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

feat: Make WebSocket Reader/Writer (#5001) #5002

Merged
merged 3 commits into from
Apr 30, 2020

Conversation

trebler
Copy link
Contributor

@trebler trebler commented Apr 30, 2020

Resolves #5001

@CLAassistant
Copy link

CLAassistant commented Apr 30, 2020

CLA assistant check
All committers have signed the CLA.

Copy link
Member

@ry ry left a comment

Choose a reason for hiding this comment

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

LGTM - nice tests!

@ry ry merged commit 8ec3668 into denoland:master Apr 30, 2020
@keroxp
Copy link
Contributor

keroxp commented May 6, 2020

@ry Why this merged? WebSocket and TCP are totally different protocol layer. And also this R/W implementation ignores ping/pong/close frame. This abstraction is not good. If use case described in #5001 is needed, one should do that or use readFrame/ writeFrame in ws modules.

async read(p: Uint8Array): Promise<number | null> {
for await (const ev of this.receive()) {
if (ev instanceof Uint8Array) {
return copyBytes(p, ev);
Copy link
Contributor

Choose a reason for hiding this comment

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

copyBytes ignores overflowed data of src. It seems to discard data in frames, doesn't it?.

Copy link
Contributor Author

@trebler trebler May 6, 2020

Choose a reason for hiding this comment

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

I think you are right. Would something like this make sense instead?

#readBacklog = new Uint8Array();

  async read(p: Uint8Array): Promise<number | null> {
    for await (const ev of this.receive()) {
      if (ev instanceof Uint8Array) {
        const copiedFromBacklog = Math.min(
          this.#readBacklog.byteLength,
          p.byteLength,
        );

        p.set(this.#readBacklog.subarray(0, copiedFromBacklog));

        this.#readBacklog = this.#readBacklog.subarray(copiedFromBacklog);

        if (copiedFromBacklog === p.byteLength) {
          this.#readBacklog = concat(this.#readBacklog, ev);
          return copiedFromBacklog;
        }

        const diff = p.byteLength - copiedFromBacklog;
        const copy = Math.min(diff, ev.byteLength);

        p.set(ev.subarray(0, copy), copiedFromBacklog);

        if (copy < ev.byteLength) {
          this.#readBacklog = concat(this.#readBacklog, ev.subarray(copy));
        }

        return copiedFromBacklog + copy;
      }
    }

    if (this.#readBacklog.byteLength) {
      const copiedFromBacklog = copyBytes(this.#readBacklog, p);
      this.#readBacklog = this.#readBacklog.subarray(copiedFromBacklog);

      return copiedFromBacklog;
    }

    return null;
  }

@trebler
Copy link
Contributor Author

trebler commented May 6, 2020

And also this R/W implementation ignores ping/pong/close frame.

@keroxp, the purpose was to only handle data frames / messages. ping/pong/close are "technical" frames. E.g., if one wanted to pipe all WS messages to/from a file, DB, TCP socket, one would want to only read/write "useful" information (being messages).

@ry
Copy link
Member

ry commented May 6, 2020

Why this merged?

@keroxp Negligence on my part. Now that you raise the point, I agree that Reader/Writer isn't the right abstraction for a frame oriented protocol. Let's revert it.

SASUKE40 pushed a commit to SASUKE40/deno that referenced this pull request May 7, 2020
ry pushed a commit that referenced this pull request May 8, 2020
caspervonb pushed a commit to caspervonb/deno_std that referenced this pull request Jan 21, 2021
caspervonb pushed a commit to caspervonb/deno_std that referenced this pull request Jan 21, 2021
caspervonb pushed a commit to caspervonb/deno_std that referenced this pull request Jan 24, 2021
caspervonb pushed a commit to caspervonb/deno_std that referenced this pull request Jan 24, 2021
caspervonb pushed a commit to caspervonb/deno_std that referenced this pull request Jan 24, 2021
caspervonb pushed a commit to caspervonb/deno_std that referenced this pull request Jan 24, 2021
caspervonb pushed a commit to caspervonb/deno_std that referenced this pull request Jan 24, 2021
caspervonb pushed a commit to caspervonb/deno_std that referenced this pull request Jan 24, 2021
caspervonb pushed a commit to caspervonb/deno_std that referenced this pull request Jan 31, 2021
caspervonb pushed a commit to caspervonb/deno_std that referenced this pull request Jan 31, 2021
caspervonb pushed a commit to caspervonb/deno_std that referenced this pull request Jan 31, 2021
caspervonb pushed a commit to caspervonb/deno_std that referenced this pull request Jan 31, 2021
caspervonb pushed a commit to caspervonb/deno_std that referenced this pull request Jan 31, 2021
caspervonb pushed a commit to caspervonb/deno_std that referenced this pull request Jan 31, 2021
caspervonb pushed a commit to caspervonb/deno_std that referenced this pull request Jan 31, 2021
caspervonb pushed a commit to caspervonb/deno_std that referenced this pull request Jan 31, 2021
caspervonb pushed a commit to caspervonb/deno_std that referenced this pull request Feb 1, 2021
caspervonb pushed a commit to caspervonb/deno_std that referenced this pull request Feb 1, 2021
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.

[WS] Make WebSocket Reader/Writer
4 participants