Skip to content

Commit 5d4efec

Browse files
committed
Add failing cloudflare integration suite
This is currently failing but acts as a starting point for verifying that we can run the Replicate library on Cloudflare workers. The test creates a cloudflare worker that uses the streaming API (as this exercises a bunch of the API surface area) and uses the experimental unstable_dev library to run the worker in a test. We then verify that the test receives a successful response. Currently we have the following issues: * `node:crypto` needs to be replaced with web crypto (node >= 15). * replace Buffer with Blob where possible or abstract behind a conditional. * `process.env` also needs to be accessed behind a conditional.
1 parent 4d4c548 commit 5d4efec

File tree

6 files changed

+1385
-0
lines changed

6 files changed

+1385
-0
lines changed
+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.wrangler
2+
node_modules
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import Replicate from "replicate";
2+
3+
const replicate = new Replicate();
4+
5+
export default {
6+
async fetch(_request, _env, _ctx) {
7+
const stream = new ReadableStream({
8+
async start(controller) {
9+
for await (const event of replicate.stream(
10+
"replicate/hello-world:5c7d5dc6dd8bf75c1acaa8565735e7986bc5b66206b55cca93cb72c9bf15ccaa",
11+
{
12+
input: {
13+
text: "Colin CloudFlare",
14+
},
15+
}
16+
)) {
17+
controller.enqueue(`${event}`);
18+
}
19+
controller.close();
20+
},
21+
});
22+
23+
return new Response(stream);
24+
},
25+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// https://developers.cloudflare.com/workers/wrangler/api/#unstable_dev
2+
import { unstable_dev as dev } from "wrangler";
3+
import { test, after, before } from "node:test";
4+
import assert from "node:assert";
5+
6+
let worker;
7+
8+
before(async () => {
9+
worker = await dev("index.js", {
10+
experimental: { disableExperimentalWarning: true },
11+
});
12+
});
13+
14+
after(async () => {
15+
if (!worker) {
16+
// If no worker the before hook failed to run and the process will hang.
17+
process.exit(1);
18+
}
19+
await worker.stop();
20+
});
21+
22+
test("worker streams back a response", { timeout: 1000 }, async (t) => {
23+
const resp = await worker.fetch("/", { signal: t.signal });
24+
const text = await resp.text();
25+
26+
assert.equal(resp.ok, true, "status is 2xx");
27+
assert(text.length > 0, "body.length is greater than 0");
28+
});

0 commit comments

Comments
 (0)