@@ -8,7 +8,7 @@ import Replicate, {
8
8
} from "replicate" ;
9
9
import nock from "nock" ;
10
10
import fetch from "cross-fetch" ;
11
- import { Stream } from "./lib/stream" ;
11
+ import { createReadableStream } from "./lib/stream" ;
12
12
import { PassThrough } from "node:stream" ;
13
13
14
14
let client : Replicate ;
@@ -1188,19 +1188,24 @@ describe("Replicate client", () => {
1188
1188
1189
1189
// Continue with tests for other methods
1190
1190
1191
- describe ( "Stream" , ( ) => {
1192
- function createStream ( body : string | NodeJS . ReadableStream , status = 200 ) {
1191
+ describe ( "createReadableStream" , ( ) => {
1192
+ async function createStream (
1193
+ body : string | NodeJS . ReadableStream ,
1194
+ status = 200
1195
+ ) {
1193
1196
const streamEndpoint = "https://stream.replicate.com" ;
1194
1197
nock ( streamEndpoint )
1195
1198
. get ( "/fake_stream" )
1196
1199
. matchHeader ( "Accept" , "text/event-stream" )
1197
1200
. reply ( status , body ) ;
1198
1201
1199
- return new Stream ( { url : `${ streamEndpoint } /fake_stream` , fetch } ) ;
1202
+ return await createReadableStream ( {
1203
+ url : `${ streamEndpoint } /fake_stream` ,
1204
+ } ) ;
1200
1205
}
1201
1206
1202
1207
test ( "consumes a server sent event stream" , async ( ) => {
1203
- const stream = createStream (
1208
+ const stream = await createStream (
1204
1209
`
1205
1210
event: output
1206
1211
id: EVENT_1
@@ -1209,13 +1214,11 @@ describe("Replicate client", () => {
1209
1214
event: done
1210
1215
id: EVENT_2
1211
1216
data: {}
1212
- `
1213
- . trim ( )
1214
- . replace ( / ^ [ ] + / gm, "" )
1217
+
1218
+ ` . replace ( / ^ [ ] + / gm, "" )
1215
1219
) ;
1216
1220
1217
1221
const iterator = stream [ Symbol . asyncIterator ] ( ) ;
1218
-
1219
1222
expect ( await iterator . next ( ) ) . toEqual ( {
1220
1223
done : false ,
1221
1224
value : { event : "output" , id : "EVENT_1" , data : "hello world" } ,
@@ -1229,7 +1232,7 @@ describe("Replicate client", () => {
1229
1232
} ) ;
1230
1233
1231
1234
test ( "consumes multiple events" , async ( ) => {
1232
- const stream = createStream (
1235
+ const stream = await createStream (
1233
1236
`
1234
1237
event: output
1235
1238
id: EVENT_1
@@ -1242,9 +1245,8 @@ describe("Replicate client", () => {
1242
1245
event: done
1243
1246
id: EVENT_3
1244
1247
data: {}
1245
- `
1246
- . trim ( )
1247
- . replace ( / ^ [ ] + / gm, "" )
1248
+
1249
+ ` . replace ( / ^ [ ] + / gm, "" )
1248
1250
) ;
1249
1251
1250
1252
const iterator = stream [ Symbol . asyncIterator ] ( ) ;
@@ -1266,7 +1268,7 @@ describe("Replicate client", () => {
1266
1268
} ) ;
1267
1269
1268
1270
test ( "ignores unexpected characters" , async ( ) => {
1269
- const stream = createStream (
1271
+ const stream = await createStream (
1270
1272
`
1271
1273
: hi
1272
1274
@@ -1277,9 +1279,8 @@ describe("Replicate client", () => {
1277
1279
event: done
1278
1280
id: EVENT_2
1279
1281
data: {}
1280
- `
1281
- . trim ( )
1282
- . replace ( / ^ [ ] + / gm, "" )
1282
+
1283
+ ` . replace ( / ^ [ ] + / gm, "" )
1283
1284
) ;
1284
1285
1285
1286
const iterator = stream [ Symbol . asyncIterator ] ( ) ;
@@ -1297,7 +1298,7 @@ describe("Replicate client", () => {
1297
1298
} ) ;
1298
1299
1299
1300
test ( "supports multiple lines of output in a single event" , async ( ) => {
1300
- const stream = createStream (
1301
+ const stream = await createStream (
1301
1302
`
1302
1303
: hi
1303
1304
@@ -1310,9 +1311,8 @@ describe("Replicate client", () => {
1310
1311
event: done
1311
1312
id: EVENT_2
1312
1313
data: {}
1313
- `
1314
- . trim ( )
1315
- . replace ( / ^ [ ] + / gm, "" )
1314
+
1315
+ ` . replace ( / ^ [ ] + / gm, "" )
1316
1316
) ;
1317
1317
1318
1318
const iterator = stream [ Symbol . asyncIterator ] ( ) ;
@@ -1335,7 +1335,7 @@ describe("Replicate client", () => {
1335
1335
1336
1336
test ( "supports the server writing data lines in multiple chunks" , async ( ) => {
1337
1337
const body = new PassThrough ( ) ;
1338
- const stream = createStream ( body ) ;
1338
+ const stream = await createStream ( body ) ;
1339
1339
1340
1340
// Create a stream of data chunks split on the pipe character for readability.
1341
1341
const data = `
@@ -1348,9 +1348,8 @@ describe("Replicate client", () => {
1348
1348
event: done
1349
1349
id: EVENT_2
1350
1350
data: {}
1351
- `
1352
- . trim ( )
1353
- . replace ( / ^ [ ] + / gm, "" ) ;
1351
+
1352
+ ` . replace ( / ^ [ ] + / gm, "" ) ;
1354
1353
1355
1354
const chunks = data . split ( "|" ) ;
1356
1355
@@ -1392,7 +1391,7 @@ describe("Replicate client", () => {
1392
1391
1393
1392
test ( "supports the server writing data in a complete mess" , async ( ) => {
1394
1393
const body = new PassThrough ( ) ;
1395
- const stream = createStream ( body ) ;
1394
+ const stream = await createStream ( body ) ;
1396
1395
1397
1396
// Create a stream of data chunks split on the pipe character for readability.
1398
1397
const data = `
@@ -1407,9 +1406,8 @@ describe("Replicate client", () => {
1407
1406
event: d|one
1408
1407
id: EVENT|_2
1409
1408
data: {}
1410
- `
1411
- . trim ( )
1412
- . replace ( / ^ [ ] + / gm, "" ) ;
1409
+
1410
+ ` . replace ( / ^ [ ] + / gm, "" ) ;
1413
1411
1414
1412
const chunks = data . split ( "|" ) ;
1415
1413
@@ -1450,15 +1448,13 @@ describe("Replicate client", () => {
1450
1448
} ) ;
1451
1449
1452
1450
test ( "supports ending without a done" , async ( ) => {
1453
- const stream = createStream (
1451
+ const stream = await createStream (
1454
1452
`
1455
1453
event: output
1456
1454
id: EVENT_1
1457
1455
data: hello world
1458
1456
1459
- `
1460
- . trim ( )
1461
- . replace ( / ^ [ ] + / gm, "" )
1457
+ ` . replace ( / ^ [ ] + / gm, "" )
1462
1458
) ;
1463
1459
1464
1460
const iterator = stream [ Symbol . asyncIterator ] ( ) ;
@@ -1470,7 +1466,7 @@ describe("Replicate client", () => {
1470
1466
} ) ;
1471
1467
1472
1468
test ( "an error event in the stream raises an exception" , async ( ) => {
1473
- const stream = createStream (
1469
+ const stream = await createStream (
1474
1470
`
1475
1471
event: output
1476
1472
id: EVENT_1
@@ -1480,27 +1476,23 @@ describe("Replicate client", () => {
1480
1476
id: EVENT_2
1481
1477
data: An unexpected error occurred
1482
1478
1483
- `
1484
- . trim ( )
1485
- . replace ( / ^ [ ] + / gm, "" )
1479
+ ` . replace ( / ^ [ ] + / gm, "" )
1486
1480
) ;
1487
1481
1488
1482
const iterator = stream [ Symbol . asyncIterator ] ( ) ;
1489
1483
expect ( await iterator . next ( ) ) . toEqual ( {
1490
1484
done : false ,
1491
1485
value : { event : "output" , id : "EVENT_1" , data : "hello world" } ,
1492
1486
} ) ;
1493
- await expect ( iterator . next ( ) ) . rejects . toThrowError (
1494
- "An unexpected error occurred"
1495
- ) ;
1487
+ await expect ( iterator . next ( ) ) . rejects . toThrowError ( "Unexpected Error" ) ;
1496
1488
expect ( await iterator . next ( ) ) . toEqual ( { done : true } ) ;
1497
1489
} ) ;
1498
1490
1499
1491
test ( "an error when fetching the stream raises an exception" , async ( ) => {
1500
- const stream = createStream ( "{}" , 500 ) ;
1492
+ const stream = await createStream ( "{}" , 500 ) ;
1501
1493
const iterator = stream [ Symbol . asyncIterator ] ( ) ;
1502
1494
await expect ( iterator . next ( ) ) . rejects . toThrowError (
1503
- "Request to https://stream.replicate.com/fake_stream failed with status 500 Internal Server Error: {}. "
1495
+ "Request to https://stream.replicate.com/fake_stream failed with status 500"
1504
1496
) ;
1505
1497
expect ( await iterator . next ( ) ) . toEqual ( { done : true } ) ;
1506
1498
} ) ;
0 commit comments