@@ -7,8 +7,8 @@ import Replicate, {
7
7
parseProgressFromLogs ,
8
8
} from "replicate" ;
9
9
import nock from "nock" ;
10
+ import { Readable } from "node:stream" ;
10
11
import { createReadableStream } from "./lib/stream" ;
11
- import { PassThrough } from "node:stream" ;
12
12
13
13
let client : Replicate ;
14
14
const BASE_URL = "https://api.replicate.com/v1" ;
@@ -1187,16 +1187,17 @@ describe("Replicate client", () => {
1187
1187
// Continue with tests for other methods
1188
1188
1189
1189
describe ( "createReadableStream" , ( ) => {
1190
- function createStream ( body : string | NodeJS . ReadableStream , status = 200 ) {
1191
- const streamEndpoint = "https://stream.replicate.com" ;
1192
- nock ( streamEndpoint )
1193
- . get ( "/fake_stream" )
1194
- . matchHeader ( "Accept" , "text/event-stream" )
1195
- . reply ( status , body ) ;
1196
-
1190
+ function createStream ( body : string | ReadableStream , status = 200 ) {
1191
+ const streamEndpoint = "https://stream.replicate.com/fake_stream" ;
1192
+ const fetch = jest . fn ( ( url ) => {
1193
+ if ( url !== streamEndpoint ) {
1194
+ throw new Error ( `Unmocked call to fetch() with url: ${ url } ` ) ;
1195
+ }
1196
+ return new Response ( body , { status } ) ;
1197
+ } ) ;
1197
1198
return createReadableStream ( {
1198
- url : ` ${ streamEndpoint } /fake_stream` ,
1199
- fetch : fetch ,
1199
+ url : streamEndpoint ,
1200
+ fetch : fetch as any ,
1200
1201
} ) ;
1201
1202
}
1202
1203
@@ -1330,9 +1331,6 @@ describe("Replicate client", () => {
1330
1331
} ) ;
1331
1332
1332
1333
test ( "supports the server writing data lines in multiple chunks" , async ( ) => {
1333
- const body = new PassThrough ( ) ;
1334
- const stream = createStream ( body ) ;
1335
-
1336
1334
// Create a stream of data chunks split on the pipe character for readability.
1337
1335
const data = `
1338
1336
event: output
@@ -1348,45 +1346,47 @@ describe("Replicate client", () => {
1348
1346
` . replace ( / ^ [ ] + / gm, "" ) ;
1349
1347
1350
1348
const chunks = data . split ( "|" ) ;
1349
+ const body = new ReadableStream ( {
1350
+ async pull ( controller ) {
1351
+ if ( chunks . length ) {
1352
+ await new Promise ( ( resolve ) => setTimeout ( resolve , 1 ) ) ;
1353
+ const chunk = chunks . shift ( ) ;
1354
+ controller . enqueue ( new TextEncoder ( ) . encode ( chunk ) ) ;
1355
+ }
1356
+ } ,
1357
+ } ) ;
1358
+
1359
+ const stream = createStream ( body ) ;
1351
1360
1352
1361
// Consume the iterator in parallel to writing it.
1353
- const reading = new Promise ( ( resolve , reject ) => {
1354
- ( async ( ) => {
1355
- const iterator = stream [ Symbol . asyncIterator ] ( ) ;
1356
- expect ( await iterator . next ( ) ) . toEqual ( {
1357
- done : false ,
1358
- value : {
1359
- event : "output" ,
1360
- id : "EVENT_1" ,
1361
- data : "hello,\nthis is a new line,\nand this is a new line too" ,
1362
- } ,
1363
- } ) ;
1364
- expect ( await iterator . next ( ) ) . toEqual ( {
1365
- done : false ,
1366
- value : { event : "done" , id : "EVENT_2" , data : "{}" } ,
1367
- } ) ;
1368
- expect ( await iterator . next ( ) ) . toEqual ( { done : true } ) ;
1369
- } ) ( ) . then ( resolve , reject ) ;
1362
+ const iterator = stream [ Symbol . asyncIterator ] ( ) ;
1363
+ expect ( await iterator . next ( ) ) . toEqual ( {
1364
+ done : false ,
1365
+ value : {
1366
+ event : "output" ,
1367
+ id : "EVENT_1" ,
1368
+ data : "hello,\nthis is a new line,\nand this is a new line too" ,
1369
+ } ,
1370
1370
} ) ;
1371
-
1372
- // Write the chunks to the stream at an interval.
1373
- const writing = new Promise ( ( resolve , reject ) => {
1374
- ( async ( ) => {
1375
- for await ( const chunk of chunks ) {
1376
- body . write ( chunk ) ;
1377
- await new Promise ( ( resolve ) => setTimeout ( resolve , 1 ) ) ;
1378
- }
1379
- body . end ( ) ;
1380
- resolve ( null ) ;
1381
- } ) ( ) . then ( resolve , reject ) ;
1371
+ expect ( await iterator . next ( ) ) . toEqual ( {
1372
+ done : false ,
1373
+ value : { event : "done" , id : "EVENT_2" , data : "{}" } ,
1382
1374
} ) ;
1375
+ expect ( await iterator . next ( ) ) . toEqual ( { done : true } ) ;
1383
1376
1384
1377
// Wait for both promises to resolve.
1385
- await Promise . all ( [ reading , writing ] ) ;
1386
1378
} ) ;
1387
1379
1388
1380
test ( "supports the server writing data in a complete mess" , async ( ) => {
1389
- const body = new PassThrough ( ) ;
1381
+ const body = new ReadableStream ( {
1382
+ async pull ( controller ) {
1383
+ if ( chunks . length ) {
1384
+ await new Promise ( ( resolve ) => setTimeout ( resolve , 1 ) ) ;
1385
+ const chunk = chunks . shift ( ) ;
1386
+ controller . enqueue ( new TextEncoder ( ) . encode ( chunk ) ) ;
1387
+ }
1388
+ } ,
1389
+ } ) ;
1390
1390
const stream = createStream ( body ) ;
1391
1391
1392
1392
// Create a stream of data chunks split on the pipe character for readability.
@@ -1407,40 +1407,20 @@ describe("Replicate client", () => {
1407
1407
1408
1408
const chunks = data . split ( "|" ) ;
1409
1409
1410
- // Consume the iterator in parallel to writing it.
1411
- const reading = new Promise ( ( resolve , reject ) => {
1412
- ( async ( ) => {
1413
- const iterator = stream [ Symbol . asyncIterator ] ( ) ;
1414
- expect ( await iterator . next ( ) ) . toEqual ( {
1415
- done : false ,
1416
- value : {
1417
- event : "output" ,
1418
- id : "EVENT_1" ,
1419
- data : "hello,\nthis is a new line,\nand this is a new line too" ,
1420
- } ,
1421
- } ) ;
1422
- expect ( await iterator . next ( ) ) . toEqual ( {
1423
- done : false ,
1424
- value : { event : "done" , id : "EVENT_2" , data : "{}" } ,
1425
- } ) ;
1426
- expect ( await iterator . next ( ) ) . toEqual ( { done : true } ) ;
1427
- } ) ( ) . then ( resolve , reject ) ;
1410
+ const iterator = stream [ Symbol . asyncIterator ] ( ) ;
1411
+ expect ( await iterator . next ( ) ) . toEqual ( {
1412
+ done : false ,
1413
+ value : {
1414
+ event : "output" ,
1415
+ id : "EVENT_1" ,
1416
+ data : "hello,\nthis is a new line,\nand this is a new line too" ,
1417
+ } ,
1428
1418
} ) ;
1429
-
1430
- // Write the chunks to the stream at an interval.
1431
- const writing = new Promise ( ( resolve , reject ) => {
1432
- ( async ( ) => {
1433
- for await ( const chunk of chunks ) {
1434
- body . write ( chunk ) ;
1435
- await new Promise ( ( resolve ) => setTimeout ( resolve , 1 ) ) ;
1436
- }
1437
- body . end ( ) ;
1438
- resolve ( null ) ;
1439
- } ) ( ) . then ( resolve , reject ) ;
1419
+ expect ( await iterator . next ( ) ) . toEqual ( {
1420
+ done : false ,
1421
+ value : { event : "done" , id : "EVENT_2" , data : "{}" } ,
1440
1422
} ) ;
1441
-
1442
- // Wait for both promises to resolve.
1443
- await Promise . all ( [ reading , writing ] ) ;
1423
+ expect ( await iterator . next ( ) ) . toEqual ( { done : true } ) ;
1444
1424
} ) ;
1445
1425
1446
1426
test ( "supports ending without a done" , async ( ) => {
0 commit comments