-
Notifications
You must be signed in to change notification settings - Fork 7
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
Move streaming to GA #262
Merged
Merged
Move streaming to GA #262
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
a3f74b5
Initial Streaming Implementation (#232)
ptpaterson a22ce91
bump version for beta release (#237)
ptpaterson 4b13d36
Merge branch 'main' into beta
pnwpedro bb816df
Merge branch 'main' into beta
pnwpedro 26d00f5
Update beta install instructions (#248)
pnwpedro 5905436
Update dev dependencies (#253)
ptpaterson fb1ad8b
[DOCS-2646] Document `<stream>.close()` method (#257)
jrodewig f003f5b
Remove streaming feature flags from beta (#251)
ptpaterson cf6929c
Sort service errors into the proper subclass (#252)
ptpaterson 90499c5
Add ttl as public field for Document (#254)
ptpaterson d7581de
[DOCS-2744] Replace FQLX beta doc links in `beta` branch (#256)
ptpaterson d77f95d
[DOCS-2750] Move streaming to GA
jrodewig 5c503ee
Add encoding/decoding for Fauna Bytes (#260)
ptpaterson 1b38d08
Bump major version for next beta release (#263)
ptpaterson e5a4084
merge beta changes
ptpaterson d2340ff
bump version for GA
ptpaterson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,6 @@ See the [Fauna Documentation](https://docs.fauna.com/fauna/current/) for additio | |
- [Query options](#query-options) | ||
- [Query statistics](#query-statistics) | ||
- [Pagination](#pagination) | ||
- [Event Streaming (beta)](#event-streaming-beta) | ||
- [Client configuration](#client-configuration) | ||
- [Environment variables](#environment-variables) | ||
- [Retry](#retry) | ||
|
@@ -29,6 +28,11 @@ See the [Fauna Documentation](https://docs.fauna.com/fauna/current/) for additio | |
- [Query timeout](#query-timeout) | ||
- [Client timeout](#client-timeout) | ||
- [HTTP/2 session idle timeout](#http2-session-idle-timeout) | ||
- [Event Streaming](#event-streaming) | ||
- [Start a stream](#start-a-stream) | ||
- [Iterate on a stream](#iterate-on-a-stream) | ||
- [Close a stream](#close-a-stream) | ||
- [Stream options](#stream-options) | ||
- [Contributing](#contributing) | ||
- [Set up the repo](#set-up-the-repo) | ||
- [Run tests](#run-tests) | ||
|
@@ -312,16 +316,6 @@ for await (const products of pages) { | |
client.close(); | ||
``` | ||
|
||
|
||
## Event Streaming (beta) | ||
|
||
[Event Streaming](https://docs.fauna.com/fauna/current/learn/streaming) is | ||
currently available in the beta version of the driver: | ||
|
||
- [Beta JavaScript driver](https://www.npmjs.com/package/fauna/v/1.4.0-beta.0) | ||
- [Beta JavaScript driver docs](https://github.com/fauna/fauna-js/tree/beta) | ||
|
||
|
||
## Client configuration | ||
|
||
The driver's `Client` instance comes with reasonable defaults that should be | ||
|
@@ -443,6 +437,136 @@ const client = new Client({ http2_session_idle_ms: 6000 }); | |
> **Warning** | ||
> Setting `http2_session_idle_ms` to small values can lead to a race condition where requests cannot be transmitted before the session is closed, yielding `ERR_HTTP2_GOAWAY_SESSION` errors. | ||
|
||
## Event Streaming | ||
|
||
The driver supports [Event Streaming](https://docs.fauna.com/fauna/current/learn/streaming). | ||
|
||
### Start a stream | ||
|
||
To get a stream token, append | ||
[`toStream()`](https://docs.fauna.com/fauna/current/reference/reference/schema_entities/set/tostream) | ||
or | ||
[`changesOn()`](https://docs.fauna.com/fauna/current/reference/reference/schema_entities/set/changeson) | ||
to a set from a [supported | ||
source](https://docs.fauna.com/fauna/current/reference/streaming_reference/#supported-sources). | ||
|
||
To start and subscribe to the stream, pass the stream token to `Client.stream()`: | ||
|
||
```javascript | ||
const response = await client.query(fql` | ||
let set = Product.all() | ||
|
||
{ | ||
initialPage: set.pageSize(10), | ||
streamToken: set.toStream() | ||
} | ||
`); | ||
const { initialPage, streamToken } = response.data; | ||
|
||
client.stream(streamToken) | ||
``` | ||
|
||
You can also pass a query that produces a stream token directly to `Client.stream()`: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here, should it be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes — It's an instance here as well. |
||
|
||
```javascript | ||
const query = fql`Product.all().changesOn(.price, .quantity)` | ||
|
||
client.stream(query) | ||
``` | ||
|
||
### Iterate on a stream | ||
|
||
You can iterate on the stream using an async loop: | ||
|
||
```javascript | ||
try { | ||
for await (const event of stream) { | ||
switch (event.type) { | ||
case "update": | ||
case "add": | ||
case "remove": | ||
console.log("Stream event:", event); | ||
// ... | ||
break; | ||
} | ||
} | ||
} catch (error) { | ||
// An error will be handled here if Fauna returns a terminal, "error" event, or | ||
// if Fauna returns a non-200 response when trying to connect, or | ||
// if the max number of retries on network errors is reached. | ||
|
||
// ... handle fatal error | ||
} | ||
``` | ||
|
||
Or you can use a callback function: | ||
|
||
```javascript | ||
stream.start( | ||
function onEvent(event) { | ||
switch (event.type) { | ||
case "update": | ||
case "add": | ||
case "remove": | ||
console.log("Stream event:", event); | ||
// ... | ||
break; | ||
} | ||
}, | ||
function onFatalError(error) { | ||
// An error will be handled here if Fauna returns a terminal, "error" event, or | ||
// if Fauna returns a non-200 response when trying to connect, or | ||
// if the max number of retries on network errors is reached. | ||
|
||
// ... handle fatal error | ||
} | ||
); | ||
``` | ||
|
||
### Close a stream | ||
|
||
Use `<stream>.close()` to close a stream: | ||
|
||
```javascript | ||
const stream = await client.stream(fql`Product.all().toStream()`) | ||
|
||
let count = 0; | ||
for await (const event of stream) { | ||
console.log("Stream event:", event); | ||
// ... | ||
count++; | ||
|
||
// Close the stream after 2 events | ||
if (count === 2) { | ||
stream.close() | ||
break; | ||
} | ||
} | ||
``` | ||
|
||
### Stream options | ||
|
||
The [client configuration](#client-configuration) sets default options for the | ||
`Client.stream()` method. | ||
|
||
You can pass an `options` object to override these defaults: | ||
|
||
```javascript | ||
const options = { | ||
long_type: "number", | ||
max_attempts: 5, | ||
max_backoff: 1000, | ||
secret: "YOUR_FAUNA_SECRET", | ||
status_events: true, | ||
}; | ||
|
||
client.stream(fql`Product.all().toStream()`, options) | ||
``` | ||
|
||
For supported properties, see [Stream | ||
options](https://docs.fauna.com/fauna/current/drivers/js-client#stream-options) | ||
in the Fauna docs. | ||
|
||
|
||
## Contributing | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { | ||
StreamClient, | ||
StreamToken, | ||
getDefaultHTTPClient, | ||
StreamClientConfiguration, | ||
} from "../../src"; | ||
import { getDefaultHTTPClientOptions } from "../client"; | ||
|
||
const defaultHttpClient = getDefaultHTTPClient(getDefaultHTTPClientOptions()); | ||
const defaultConfig: StreamClientConfiguration = { | ||
secret: "secret", | ||
long_type: "number", | ||
max_attempts: 3, | ||
max_backoff: 20, | ||
httpStreamClient: defaultHttpClient, | ||
}; | ||
const dummyStreamToken = new StreamToken("dummy"); | ||
|
||
describe("StreamClientConfiguration", () => { | ||
it("can be instantiated directly with a token", () => { | ||
new StreamClient(dummyStreamToken, defaultConfig); | ||
}); | ||
|
||
it("can be instantiated directly with a lambda", async () => { | ||
new StreamClient(() => Promise.resolve(dummyStreamToken), defaultConfig); | ||
}); | ||
|
||
it.each` | ||
fieldName | ||
${"long_type"} | ||
${"httpStreamClient"} | ||
${"max_backoff"} | ||
${"max_attempts"} | ||
${"secret"} | ||
`( | ||
"throws a TypeError if $fieldName provided is undefined", | ||
async ({ fieldName }: { fieldName: keyof StreamClientConfiguration }) => { | ||
expect.assertions(1); | ||
|
||
const config = { ...defaultConfig }; | ||
delete config[fieldName]; | ||
try { | ||
new StreamClient(dummyStreamToken, config); | ||
} catch (e: any) { | ||
expect(e).toBeInstanceOf(TypeError); | ||
} | ||
}, | ||
); | ||
|
||
it("throws a RangeError if 'max_backoff' is less than or equal to zero", async () => { | ||
expect.assertions(1); | ||
|
||
const config = { ...defaultConfig, max_backoff: 0 }; | ||
try { | ||
new StreamClient(dummyStreamToken, config); | ||
} catch (e: any) { | ||
expect(e).toBeInstanceOf(RangeError); | ||
} | ||
}); | ||
|
||
it("throws a RangeError if 'max_attempts' is less than or equal to zero", async () => { | ||
expect.assertions(1); | ||
|
||
const config = { ...defaultConfig, max_attempts: 0 }; | ||
try { | ||
new StreamClient(dummyStreamToken, config); | ||
} catch (e: any) { | ||
expect(e).toBeInstanceOf(RangeError); | ||
} | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is it a
Client
class or its instanceclient
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's an instance.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll work on getting this fixed. Thanks for raising @w01fgang!