11import { createHash } from 'crypto' ;
22import { promises as fsPromises , readFileSync } from 'fs' ;
33import path from 'path' ;
4+ // eslint-disable-next-line no-shadow -- TODO: Remove when we require Node v20+.
5+ import { File } from 'node:buffer' ;
6+ // ^ note: this was introduced in v18.13.0. Because of its unavailability in
7+ // earlier versions, the actual implementation in submit-addon.js retrieves
8+ // the File constructor in a different way, which also works in Node 18.0.0.
9+ // Our CI tests with Node 18.19.0 as the lowest version, so this passes tests.
410
511// eslint-disable-next-line import/no-extraneous-dependencies
612import CRC32 from 'crc-32' ;
@@ -16,9 +22,6 @@ import Client, {
1622 saveIdToFile ,
1723 saveUploadUuidToFile ,
1824 signAddon ,
19-
20- // eslint-disable-next-line no-shadow -- Not actually available under Node 20. Use global once possible.
21- FileBlob as File ,
2225} from '../../../src/util/submit-addon.js' ;
2326import { withTempDir } from '../../../src/util/temp-dir.js' ;
2427
@@ -372,6 +375,25 @@ describe('util.submit-addon', () => {
372375 assert . equal ( await fileRes . text ( ) , FILE_CONTENT ) ;
373376 assert . equal ( String ( fileRes ) , '[object File]' ) ;
374377 } ) ) ;
378+
379+ it ( 'should return a File whose name is preserved in FormData' , ( ) =>
380+ withTempDir ( async ( tmpDir ) => {
381+ const client = new Client ( clientDefaults ) ;
382+ const FILE_BASENAME = 'testfile.txt' ;
383+ const FILE_CONTENT = 'somecontent' ;
384+ const filePath = path . join ( tmpDir . path ( ) , FILE_BASENAME ) ;
385+ await fsPromises . writeFile ( filePath , FILE_CONTENT ) ;
386+ const fileRes = client . fileFromSync ( filePath ) ;
387+
388+ // Regression test for https://github.com/mozilla/web-ext/issues/3418
389+ const fd = new FormData ( ) ;
390+ fd . set ( 'upload' , fileRes ) ;
391+ const fileOut = fd . get ( 'upload' ) ;
392+
393+ assert . equal ( fileOut . name , FILE_BASENAME ) ;
394+ assert . equal ( fileOut . size , FILE_CONTENT . length ) ;
395+ assert . equal ( await fileOut . text ( ) , FILE_CONTENT ) ;
396+ } ) ) ;
375397 } ) ;
376398
377399 describe ( 'getPreviousUuidOrUploadXpi' , ( ) => {
0 commit comments