11import assert from "node:assert/strict" ;
22import test from "node:test" ;
33
4- function createHarness ( { bundleOk } ) {
4+ function createHarness ( {
5+ bundleOk,
6+ streaming = true ,
7+ contentLength = 4 ,
8+ bundleSize = 4 ,
9+ } ) {
510 const messages = [ ] ;
611 const calls = [ ] ;
712 let failQuery = false ;
@@ -52,28 +57,29 @@ function createHarness({ bundleOk }) {
5257 headers : {
5358 get ( name ) {
5459 return name . toLowerCase ( ) === "content-length" && bundleOk
55- ? "4"
60+ ? String ( contentLength )
5661 : null ;
5762 } ,
5863 } ,
59- body : bundleOk
60- ? {
61- getReader ( ) {
62- return {
63- async read ( ) {
64- const chunks = [
65- new Uint8Array ( [ 1 , 2 ] ) ,
66- new Uint8Array ( [ 3 , 4 ] ) ,
67- ] ;
68- if ( chunk >= chunks . length ) return { done : true } ;
69- return { done : false , value : chunks [ chunk ++ ] } ;
70- } ,
71- } ;
72- } ,
73- }
74- : null ,
64+ body :
65+ bundleOk && streaming
66+ ? {
67+ getReader ( ) {
68+ return {
69+ async read ( ) {
70+ const chunks = [
71+ new Uint8Array ( [ 1 , 2 ] ) ,
72+ new Uint8Array ( [ 3 , 4 ] ) ,
73+ ] ;
74+ if ( chunk >= chunks . length ) return { done : true } ;
75+ return { done : false , value : chunks [ chunk ++ ] } ;
76+ } ,
77+ } ;
78+ } ,
79+ }
80+ : null ,
7581 async arrayBuffer ( ) {
76- return new Uint8Array ( [ 1 , 2 , 3 , 4 ] ) . buffer ;
82+ return new Uint8Array ( bundleSize ) . buffer ;
7783 } ,
7884 } ;
7985 } ;
@@ -88,7 +94,7 @@ function createHarness({ bundleOk }) {
8894 } ;
8995}
9096
91- const fast = createHarness ( { bundleOk : true } ) ;
97+ const fast = createHarness ( { bundleOk : true , contentLength : 2048 } ) ;
9298await import ( `../../docs/webworker.js?fast=${ Date . now ( ) } ` ) ;
9399await new Promise ( ( resolve ) => setImmediate ( resolve ) ) ;
94100const fastOnMessage = globalThis . onmessage ;
@@ -108,7 +114,7 @@ test("worker streams the website-local bundle in parallel with Pyodide", () => {
108114 ) ;
109115 assert . ok (
110116 fast . messages . some ( ( { message } ) =>
111- message ?. includes ( "Downloading browser package… 2 B / 4 B " ) ,
117+ message ?. includes ( "Downloading browser package… 2 B / 2 KiB " ) ,
112118 ) ,
113119 ) ;
114120 assert . ok (
@@ -170,6 +176,28 @@ test("worker returns query errors to the page", async () => {
170176 assert . equal ( fast . messages . at ( - 1 ) . error , "query failed" ) ;
171177} ) ;
172178
179+ test ( "worker accepts non-streaming bundle responses" , async ( ) => {
180+ const nonStreaming = createHarness ( {
181+ bundleOk : true ,
182+ streaming : false ,
183+ contentLength : 2 * 1024 * 1024 ,
184+ bundleSize : 2 * 1024 * 1024 ,
185+ } ) ;
186+ await import ( `../../docs/webworker.js?nonstream=${ Date . now ( ) } ` ) ;
187+ await new Promise ( ( resolve ) => setImmediate ( resolve ) ) ;
188+
189+ assert . ok (
190+ nonStreaming . messages . some (
191+ ( { message } ) => message === "Downloaded browser package · 2.00 MiB" ,
192+ ) ,
193+ ) ;
194+ assert . ok (
195+ nonStreaming . calls . some (
196+ ( [ name , bytes ] ) => name === "unpackArchive" && bytes === 2 * 1024 * 1024 ,
197+ ) ,
198+ ) ;
199+ } ) ;
200+
173201test ( "worker falls back to micropip when the deployed bundle is unavailable" , async ( ) => {
174202 const fallback = createHarness ( { bundleOk : false } ) ;
175203 await import ( `../../docs/webworker.js?fallback=${ Date . now ( ) } ` ) ;
0 commit comments