@@ -145,6 +145,58 @@ export class PGlite implements PGliteInterface {
145
145
? { print : console . info , printErr : console . error }
146
146
: { print : ( ) => { } , printErr : ( ) => { } } ) ,
147
147
locateFile : await makeLocateFile ( ) ,
148
+ preRun : [
149
+ ( mod : any ) => {
150
+ // Register /dev/blob device
151
+ // This is used to read and write blobs when used in COPY TO/FROM
152
+ // e.g. COPY mytable TO '/dev/blob' WITH (FORMAT binary)
153
+ // The data is returned by the query as a `blob` property in the results
154
+ const devId = mod . FS . makedev ( 64 , 0 ) ;
155
+ let callCounter = 0 ;
156
+ const devOpt = {
157
+ open : ( stream : any ) => { } ,
158
+ close : ( stream : any ) => { } ,
159
+ read : (
160
+ stream : any ,
161
+ buffer : Uint8Array ,
162
+ offset : number ,
163
+ length : number ,
164
+ position : number ,
165
+ ) => {
166
+ const buf = this . #queryReadBuffer;
167
+ if ( ! buf ) {
168
+ throw new Error ( "No File or Blob provided to read from" ) ;
169
+ }
170
+ const contents = new Uint8Array ( buf ) ;
171
+ if ( position >= contents . length ) return 0 ;
172
+ const size = Math . min ( contents . length - position , length ) ;
173
+ for ( let i = 0 ; i < size ; i ++ ) {
174
+ buffer [ offset + i ] = contents [ position + i ] ;
175
+ }
176
+ return size ;
177
+ } ,
178
+ write : (
179
+ stream : any ,
180
+ buffer : Uint8Array ,
181
+ offset : number ,
182
+ length : number ,
183
+ position : number ,
184
+ ) => {
185
+ callCounter ++ ;
186
+ this . #queryWriteChunks ??= [ ] ;
187
+ this . #queryWriteChunks. push (
188
+ buffer . slice ( offset , offset + length ) ,
189
+ ) ;
190
+ return length ;
191
+ } ,
192
+ llseek : ( stream : any , offset : number , whence : number ) => {
193
+ throw new Error ( "Cannot seek /dev/blob" ) ;
194
+ } ,
195
+ } ;
196
+ mod . FS . registerDevice ( devId , devOpt ) ;
197
+ mod . FS . mkdev ( "/dev/blob" , devId ) ;
198
+ } ,
199
+ ] ,
148
200
} ;
149
201
150
202
emscriptenOpts = await this . fs ! . emscriptenOpts ( emscriptenOpts ) ;
0 commit comments