@@ -33,13 +33,15 @@ const {
3333const binding = internalBinding ( 'fs' ) ;
3434const { Buffer } = require ( 'buffer' ) ;
3535
36- const { codes, hideStackFrames } = require ( 'internal/errors' ) ;
3736const {
38- ERR_FS_FILE_TOO_LARGE ,
39- ERR_INVALID_ARG_TYPE ,
40- ERR_INVALID_ARG_VALUE ,
41- ERR_METHOD_NOT_IMPLEMENTED ,
42- } = codes ;
37+ codes : {
38+ ERR_FS_FILE_TOO_LARGE ,
39+ ERR_INVALID_ARG_TYPE ,
40+ ERR_INVALID_ARG_VALUE ,
41+ ERR_METHOD_NOT_IMPLEMENTED ,
42+ } ,
43+ AbortError,
44+ } = require ( 'internal/errors' ) ;
4345const { isArrayBufferView } = require ( 'internal/util/types' ) ;
4446const { rimrafPromises } = require ( 'internal/fs/rimraf' ) ;
4547const {
@@ -93,13 +95,6 @@ const {
9395const getDirectoryEntriesPromise = promisify ( getDirents ) ;
9496const validateRmOptionsPromise = promisify ( validateRmOptions ) ;
9597
96- let DOMException ;
97- const lazyDOMException = hideStackFrames ( ( message , name ) => {
98- if ( DOMException === undefined )
99- DOMException = internalBinding ( 'messaging' ) . DOMException ;
100- return new DOMException ( message , name ) ;
101- } ) ;
102-
10398class FileHandle extends EventEmitterMixin ( JSTransferable ) {
10499 constructor ( filehandle ) {
105100 super ( ) ;
@@ -272,15 +267,18 @@ async function fsCall(fn, handle, ...args) {
272267 }
273268}
274269
270+ function checkAborted ( signal ) {
271+ if ( signal ?. aborted )
272+ throw new AbortError ( ) ;
273+ }
274+
275275async function writeFileHandle ( filehandle , data , signal ) {
276276 // `data` could be any kind of typed array.
277277 data = new Uint8Array ( data . buffer , data . byteOffset , data . byteLength ) ;
278278 let remaining = data . length ;
279279 if ( remaining === 0 ) return ;
280280 do {
281- if ( signal ?. aborted ) {
282- throw lazyDOMException ( 'The operation was aborted' , 'AbortError' ) ;
283- }
281+ checkAborted ( signal ) ;
284282 const { bytesWritten } =
285283 await write ( filehandle , data , 0 ,
286284 MathMin ( kWriteFileMaxChunkSize , data . length ) ) ;
@@ -296,14 +294,11 @@ async function writeFileHandle(filehandle, data, signal) {
296294async function readFileHandle ( filehandle , options ) {
297295 const signal = options ?. signal ;
298296
299- if ( signal ?. aborted ) {
300- throw lazyDOMException ( 'The operation was aborted' , 'AbortError' ) ;
301- }
297+ checkAborted ( signal ) ;
298+
302299 const statFields = await binding . fstat ( filehandle . fd , false , kUsePromises ) ;
303300
304- if ( signal ?. aborted ) {
305- throw lazyDOMException ( 'The operation was aborted' , 'AbortError' ) ;
306- }
301+ checkAborted ( signal ) ;
307302
308303 let size ;
309304 if ( ( statFields [ 1 /* mode */ ] & S_IFMT ) === S_IFREG ) {
@@ -321,9 +316,7 @@ async function readFileHandle(filehandle, options) {
321316 const buffers = [ ] ;
322317 const fullBuffer = noSize ? undefined : Buffer . allocUnsafeSlow ( size ) ;
323318 do {
324- if ( signal ?. aborted ) {
325- throw lazyDOMException ( 'The operation was aborted' , 'AbortError' ) ;
326- }
319+ checkAborted ( signal ) ;
327320 let buffer ;
328321 let offset ;
329322 let length ;
@@ -693,9 +686,7 @@ async function writeFile(path, data, options) {
693686 if ( path instanceof FileHandle )
694687 return writeFileHandle ( path , data , options . signal ) ;
695688
696- if ( options . signal ?. aborted ) {
697- throw lazyDOMException ( 'The operation was aborted' , 'AbortError' ) ;
698- }
689+ checkAborted ( options . signal ) ;
699690
700691 const fd = await open ( path , flag , options . mode ) ;
701692 const { signal } = options ;
@@ -716,9 +707,7 @@ async function readFile(path, options) {
716707 if ( path instanceof FileHandle )
717708 return readFileHandle ( path , options ) ;
718709
719- if ( options . signal ?. aborted ) {
720- throw lazyDOMException ( 'The operation was aborted' , 'AbortError' ) ;
721- }
710+ checkAborted ( options . signal ) ;
722711
723712 const fd = await open ( path , flag , 0o666 ) ;
724713 return PromisePrototypeFinally ( readFileHandle ( fd , options ) , fd . close ) ;
0 commit comments