Skip to content

Commit f280b0b

Browse files
authored
Switch to replicate.stream in Bun integration test (#227)
* Switch to replicate.stream in Bun integration test * Vendor @stardazed/streams-text-encoding * Modify vendored source of @stardazed/streams-text-encoding * Use polyfill when TextDecoderStream isn't available * Use globalThis instead of global * Set 30s test timeout * Add information about vendoring and patching TextDecoderStream polyfill * Use replicate/canary for streaming test * Fix README
1 parent 9245a09 commit f280b0b

File tree

6 files changed

+143
-21
lines changed

6 files changed

+143
-21
lines changed

.github/workflows/ci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -166,4 +166,4 @@ jobs:
166166
cd integration/${{ matrix.suite }}
167167
bun uninstall replicate
168168
bun install "file:../../${{ needs.build.outputs.tarball-name }}"
169-
bun test
169+
bun test --timeout 30000

README.md

+27-12
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ app.get('/webhooks/replicate', async (c) => {
110110
const prediction = await c.req.json();
111111
console.log(prediction);
112112
//=> {"id": "xyz", "status": "successful", ... }
113-
113+
114114
// Acknowledge the webhook.
115115
c.status(200);
116116
c.json({ok: true});
@@ -217,15 +217,15 @@ Run a model and await the result. Unlike [`replicate.prediction.create`](#replic
217217
const output = await replicate.run(identifier, options, progress);
218218
```
219219

220-
| name | type | description |
221-
| ------------------------------- | -------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
222-
| `identifier` | string | **Required**. The model version identifier in the format `{owner}/{name}:{version}`, for example `stability-ai/sdxl:8beff3369e81422112d93b89ca01426147de542cd4684c244b673b105188fe5f` |
223-
| `options.input` | object | **Required**. An object with the model inputs. |
224-
| `options.wait` | object | Options for waiting for the prediction to finish |
225-
| `options.wait.interval` | number | Polling interval in milliseconds. Defaults to 500 |
226-
| `options.webhook` | string | An HTTPS URL for receiving a webhook when the prediction has new output |
227-
| `options.webhook_events_filter` | string[] | An array of events which should trigger [webhooks](https://replicate.com/docs/webhooks). Allowable values are `start`, `output`, `logs`, and `completed` |
228-
| `options.signal` | object | An [AbortSignal](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) to cancel the prediction |
220+
| name | type | description |
221+
| ------------------------------- | -------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
222+
| `identifier` | string | **Required**. The model version identifier in the format `{owner}/{name}:{version}`, for example `stability-ai/sdxl:8beff3369e81422112d93b89ca01426147de542cd4684c244b673b105188fe5f` |
223+
| `options.input` | object | **Required**. An object with the model inputs. |
224+
| `options.wait` | object | Options for waiting for the prediction to finish |
225+
| `options.wait.interval` | number | Polling interval in milliseconds. Defaults to 500 |
226+
| `options.webhook` | string | An HTTPS URL for receiving a webhook when the prediction has new output |
227+
| `options.webhook_events_filter` | string[] | An array of events which should trigger [webhooks](https://replicate.com/docs/webhooks). Allowable values are `start`, `output`, `logs`, and `completed` |
228+
| `options.signal` | object | An [AbortSignal](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) to cancel the prediction |
229229
| `progress` | function | Callback function that receives the prediction object as it's updated. The function is called when the prediction is created, each time it's updated while polling for completion, and when it's completed. |
230230

231231
Throws `Error` if the prediction failed.
@@ -246,7 +246,7 @@ Example that logs progress as the model is running:
246246
const model = "stability-ai/sdxl:8beff3369e81422112d93b89ca01426147de542cd4684c244b673b105188fe5f";
247247
const input = { prompt: "a 19th century portrait of a raccoon gentleman wearing a suit" };
248248
const onProgress = (prediction) => {
249-
const last_log_line = prediction.logs.split("\n").pop()
249+
const last_log_line = prediction.logs.split("\n").pop()
250250
console.log({id: prediction.id, log: last_log_line})
251251
}
252252
const output = await replicate.run(model, { input }, onProgress)
@@ -875,6 +875,21 @@ The `Replicate` constructor and all `replicate.*` methods are fully typed.
875875

876876
We have a few dependencies that have been bundled into the vendor directory rather than adding external npm dependencies.
877877

878-
These have been generated using bundlejs.com and copied into the appropriate directory along with the license and repository information.
878+
These have been generated using bundlejs.com and copied into the appropriate directory along with the license and repository information.
879879

880880
* [eventsource-parser/stream](https://bundlejs.com/?bundle&q=eventsource-parser%40latest%2Fstream&config=%7B%22esbuild%22%3A%7B%22format%22%3A%22cjs%22%2C%22minify%22%3Afalse%2C%22platform%22%3A%22neutral%22%7D%7D)
881+
* [streams-text-encoding/text-decoder-stream](https://bundlejs.com/?q=%40stardazed%2Fstreams-text-encoding&treeshake=%5B%7B+TextDecoderStream+%7D%5D&config=%7B%22esbuild%22%3A%7B%22format%22%3A%22cjs%22%2C%22minify%22%3Afalse%7D%7D)
882+
883+
> [!NOTE]
884+
> The vendored implementation of `TextDecoderStream` requires
885+
> the following patch to be applied to the output of bundlejs.com:
886+
>
887+
> ```diff
888+
> constructor(label, options) {
889+
> - this[decDecoder] = new TextDecoder(label, options);
890+
> - this[decTransform] = new TransformStream(new TextDecodeTransformer(this[decDecoder]));
891+
> + const decoder = new TextDecoder(label || "utf-8", options || {});
892+
> + this[decDecoder] = decoder;
893+
> + this[decTransform] = new TransformStream(new TextDecodeTransformer(decoder));
894+
> }
895+
> ```

integration/bun/index.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@ import type {
1919

2020
test("main", async () => {
2121
const output = await main();
22-
expect(output as any).toEqual("hello Brünnhilde Bun");
22+
expect(output).toContain("Brünnhilde Bun");
2323
});

integration/bun/index.ts

+15-7
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,20 @@ const replicate = new Replicate({
55
});
66

77
export default async function main() {
8-
return await replicate.run(
9-
"replicate/hello-world:5c7d5dc6dd8bf75c1acaa8565735e7986bc5b66206b55cca93cb72c9bf15ccaa",
10-
{
11-
input: {
12-
text: "Brünnhilde Bun",
13-
},
8+
const model =
9+
"replicate/canary:30e22229542eb3f79d4f945dacb58d32001b02cc313ae6f54eef27904edf3272";
10+
const options = {
11+
input: {
12+
text: "Brünnhilde Bun",
13+
},
14+
};
15+
const output = [];
16+
17+
for await (const { event, data } of replicate.stream(model, options)) {
18+
if (event === "output") {
19+
output.push(data);
1420
}
15-
);
21+
}
22+
23+
return output.join("").trim();
1624
}

lib/stream.js

+4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ const { streamAsyncIterator } = require("./util");
55
const {
66
EventSourceParserStream,
77
} = require("../vendor/eventsource-parser/stream");
8+
const { TextDecoderStream } =
9+
typeof globalThis.TextDecoderStream === "undefined"
10+
? require("../vendor/streams-text-encoding/text-decoder-stream")
11+
: globalThis;
812

913
/**
1014
* A server-sent event.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// Adapted from https://github.com/stardazed/sd-streams
2+
//
3+
// MIT License
4+
//
5+
// Copyright (c) 2018-Present @zenmumbler
6+
//
7+
// Permission is hereby granted, free of charge, to any person obtaining a copy
8+
// of this software and associated documentation files (the "Software"), to deal
9+
// in the Software without restriction, including without limitation the rights
10+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
// copies of the Software, and to permit persons to whom the Software is
12+
// furnished to do so, subject to the following conditions:
13+
//
14+
// The above copyright notice and this permission notice shall be included in all
15+
// copies or substantial portions of the Software.
16+
//
17+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
// SOFTWARE.
24+
var __defProp = Object.defineProperty;
25+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
26+
var __getOwnPropNames = Object.getOwnPropertyNames;
27+
var __hasOwnProp = Object.prototype.hasOwnProperty;
28+
var __export = (target, all) => {
29+
for (var name in all)
30+
__defProp(target, name, { get: all[name], enumerable: true });
31+
};
32+
var __copyProps = (to, from, except, desc) => {
33+
if (from && typeof from === "object" || typeof from === "function") {
34+
for (let key of __getOwnPropNames(from))
35+
if (!__hasOwnProp.call(to, key) && key !== except)
36+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
37+
}
38+
return to;
39+
};
40+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
41+
42+
// /input.ts
43+
var input_exports = {};
44+
__export(input_exports, {
45+
TextDecoderStream: () => TextDecoderStream
46+
});
47+
module.exports = __toCommonJS(input_exports);
48+
49+
// http-url:https://unpkg.com/@stardazed/[email protected]/dist/sd-streams-text-encoding.esm.js
50+
var decDecoder = Symbol("decDecoder");
51+
var decTransform = Symbol("decTransform");
52+
var TextDecodeTransformer = class {
53+
constructor(decoder) {
54+
this.decoder_ = decoder;
55+
}
56+
transform(chunk, controller) {
57+
if (!(chunk instanceof ArrayBuffer || ArrayBuffer.isView(chunk))) {
58+
throw new TypeError("Input data must be a BufferSource");
59+
}
60+
const text = this.decoder_.decode(chunk, { stream: true });
61+
if (text.length !== 0) {
62+
controller.enqueue(text);
63+
}
64+
}
65+
flush(controller) {
66+
const text = this.decoder_.decode();
67+
if (text.length !== 0) {
68+
controller.enqueue(text);
69+
}
70+
}
71+
};
72+
var TextDecoderStream = class {
73+
constructor(label, options) {
74+
const decoder = new TextDecoder(label || "utf-8", options || {});
75+
this[decDecoder] = decoder;
76+
this[decTransform] = new TransformStream(new TextDecodeTransformer(decoder));
77+
}
78+
get encoding() {
79+
return this[decDecoder].encoding;
80+
}
81+
get fatal() {
82+
return this[decDecoder].fatal;
83+
}
84+
get ignoreBOM() {
85+
return this[decDecoder].ignoreBOM;
86+
}
87+
get readable() {
88+
return this[decTransform].readable;
89+
}
90+
get writable() {
91+
return this[decTransform].writable;
92+
}
93+
};
94+
var encEncoder = Symbol("encEncoder");
95+
var encTransform = Symbol("encTransform");

0 commit comments

Comments
 (0)