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

Add deno.Buffer #1121

Merged
merged 12 commits into from
Nov 4, 2018
Merged

Add deno.Buffer #1121

merged 12 commits into from
Nov 4, 2018

Conversation

ry
Copy link
Member

@ry ry commented Oct 30, 2018

Do not confuse this with Node's Buffer. This is a direct port of Go's
bytes.ByteBuffer - it allows buffering of Reader objects.

@ry ry requested a review from piscisaureus October 30, 2018 18:12
Copy link
Contributor

@kitsonk kitsonk left a comment

Choose a reason for hiding this comment

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

Async generators are a thing and while not part of the GoLang API, implementing a [Symbol.asnycIterator]() would allow:

async function f() {
  for await (const x of denoBuffer) {
     console.log(x);
  }
}

Which relates to my other comments about having a more JavaScripty API.

js/buffer.ts Outdated Show resolved Hide resolved
@ry
Copy link
Member Author

ry commented Oct 30, 2018

@kitsonk Regarding async iterators, there was a lengthy discussion a few months ago about whether Reader interfaces should be just async iterators: #387 (comment)

My conclusion was that it was overloading concerns and it would be better to provide a utility function for transforming any Reader into an async iterator.

function readerIterator(r: deno.Reader): AsyncIterator<ArrayBufferView>;

In particular, it's imperative that users are able to provide memory for the read() rather than having it allocated for them.

@GrosSacASac
Copy link

Hi, I am not a go expert, what is the difference with ArrayBuffer ?
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer

@kitsonk
Copy link
Contributor

kitsonk commented Oct 30, 2018

My only feedback would be toAsyncIterator(r: deno.Reader)... and I believe it would need to return a modified AsyncIterator so it can be cleanly used in for await in. Something like:

interface IterableAsyncIterator<T> extends AsyncIterator<T> {
  [Symbol.asyncIterator]: AsyncIterator<T>;
}

Or just an object with the Symbol.asyncIterator symbol on it and nothing else.

@kitsonk
Copy link
Contributor

kitsonk commented Oct 30, 2018

Hi, I am not a go expert, what is the difference with ArrayBuffer ?
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer

Functionally it conforms to the Deno's Reader/Writer APIs, so it can be easier to utilise with things like files and other readable/writable objects. The ArrayBuffer API does not conform, it is a more lower level construct. It does utilise a typed array within its implementation. The Web APIs don't really have good usable APIs around things like file reading and writing. So personally it being in the deno namespace makes it clear that it is something that Deno is adding to the mix. All the ArrayBuffer and associated APIs are still supported within Deno.

@bartlomieju
Copy link
Member

@ry @kitsonk Regarding that users should be able to provide memory, how can we handle that with for-await-in?

Is there a way to pass ArrayBufferWay to async iterator in this example?

for await (const buf of toAsyncIterator(someReader)) {
   console.log(x);
}

@ry
Copy link
Member Author

ry commented Oct 30, 2018

@GrosSacASac This is a Buffer that can grow. You can read() and write() from it. One situation where you might want to do this, is if you wanted to, say, buffer up an http response so it can be parsed:

import { dial, Buffer, copy, stdout } from "deno";
dial("tcp", "google.com:80").then((conn) => {
  let buf = new Buffer();
  buf.writeString("GET / HTTP/1.1\r\n\r\n");
  copy(conn, buf);
  copy(stdout, conn);
});

@ry
Copy link
Member Author

ry commented Oct 30, 2018

@bartlomieju Ideally in the async iterator case we would allocate in Rust and pass it over to V8... This isn't implemented tho. So just allocate let buf = new Uint8Array(1024) and use that to pass to read().

js/buffer.ts Outdated Show resolved Hide resolved
js/buffer.ts Show resolved Hide resolved
js/buffer.ts Show resolved Hide resolved
js/buffer.ts Show resolved Hide resolved
js/buffer.ts Show resolved Hide resolved
js/buffer.ts Show resolved Hide resolved
js/buffer.ts Show resolved Hide resolved
js/buffer.ts Outdated Show resolved Hide resolved
js/buffer.ts Show resolved Hide resolved
while (true) {
try {
const i = this._grow(MIN_READ);
this._reslice(i);
Copy link
Member

Choose a reason for hiding this comment

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

I really have trouble following what's going on here...

js/buffer.ts Outdated Show resolved Hide resolved
js/buffer.ts Outdated Show resolved Hide resolved
js/buffer.ts Outdated Show resolved Hide resolved
js/buffer.ts Outdated Show resolved Hide resolved
js/buffer_test.ts Outdated Show resolved Hide resolved
Do not confuse this with Node's Buffer. This is a direct port of Go's
bytes.ByteBuffer - it allows buffering of Reader objects.
Copy link
Member

@piscisaureus piscisaureus left a comment

Choose a reason for hiding this comment

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

I am not so sure how this is going to be used and whether it belongs in Deno.
But ok let's see where it goes.

@ry ry merged commit bd88e56 into denoland:master Nov 4, 2018
ry added a commit to ry/deno that referenced this pull request Nov 5, 2018
- Performance and stability improvements on all platforms.
- Add repl (denoland#998)
- Add deno.Buffer (denoland#1121)
- Support cargo check (denoland#1128)
- Upgrade Rust crates and Flatbuffers. (denoland#1145, denoland#1127)
- Add helper to turn deno.Reader into async iterator (denoland#1130)
- Add ability to load JSON as modules (denoland#1065)
- Add deno.resources() (denoland#1119)
- Add application/x-typescript mime type support (denoland#1111)
@ry ry mentioned this pull request Nov 5, 2018
ry added a commit that referenced this pull request Nov 5, 2018
- Performance and stability improvements on all platforms.
- Add repl (#998)
- Add deno.Buffer (#1121)
- Support cargo check (#1128)
- Upgrade Rust crates and Flatbuffers. (#1145, #1127)
- Add helper to turn deno.Reader into async iterator (#1130)
- Add ability to load JSON as modules (#1065)
- Add deno.resources() (#1119)
- Add application/x-typescript mime type support (#1111)
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.

5 participants