@@ -13,8 +13,11 @@ const {
1313} = primordials ;
1414
1515const {
16+ ERR_FEATURE_UNAVAILABLE_ON_PLATFORM ,
17+ ERR_INCOMPATIBLE_OPTION_PAIR ,
1618 ERR_INVALID_ARG_TYPE ,
1719 ERR_METHOD_NOT_IMPLEMENTED ,
20+ ERR_MISSING_OPTION ,
1821 ERR_OUT_OF_RANGE ,
1922 ERR_STREAM_DESTROYED ,
2023 ERR_SYSTEM_ERROR ,
@@ -40,6 +43,9 @@ const {
4043} = require ( 'internal/fs/utils' ) ;
4144const { Readable, Writable, finished } = require ( 'stream' ) ;
4245const { toPathIfFileURL } = require ( 'internal/url' ) ;
46+ const { isWindows } = require ( 'internal/util' ) ;
47+ const binding = internalBinding ( 'fs' ) ;
48+ const { O_RDONLY , O_WRONLY } = internalBinding ( 'constants' ) . fs ;
4349const kIoDone = Symbol ( 'kIoDone' ) ;
4450const kIsPerformingIO = Symbol ( 'kIsPerformingIO' ) ;
4551
@@ -160,6 +166,23 @@ function importFd(stream, options) {
160166 [ 'number' , 'FileHandle' ] , options . fd ) ;
161167}
162168
169+ function importWindowsHandle ( stream , options , flags ) {
170+ if ( options . windowsHandle == null ) {
171+ throw new ERR_MISSING_OPTION ( 'options.windowsHandle' ) ;
172+ }
173+ if ( ! isWindows ) {
174+ throw new ERR_FEATURE_UNAVAILABLE_ON_PLATFORM ( 'windowsHandle' ) ;
175+ }
176+ if ( options . fs ) {
177+ // The HANDLE is wrapped using the real filesystem, so a custom fs
178+ // implementation cannot be combined with it.
179+ throw new ERR_METHOD_NOT_IMPLEMENTED ( 'windowsHandle with fs' ) ;
180+ }
181+ validateInteger ( options . windowsHandle , 'options.windowsHandle' ) ;
182+ stream [ kFs ] = fs ;
183+ return binding . handleToFd ( options . windowsHandle , flags ) ;
184+ }
185+
163186function ReadStream ( path , options ) {
164187 if ( ! ( this instanceof ReadStream ) )
165188 return new ReadStream ( path , options ) ;
@@ -173,7 +196,11 @@ function ReadStream(path, options) {
173196 options . autoDestroy = false ;
174197 }
175198
176- if ( options . fd == null ) {
199+ if ( options . fd != null && options . windowsHandle != null ) {
200+ throw new ERR_INCOMPATIBLE_OPTION_PAIR ( 'windowsHandle' , 'fd' ) ;
201+ } else if ( options . windowsHandle != null ) {
202+ this . fd = getValidatedFd ( importWindowsHandle ( this , options , O_RDONLY ) ) ;
203+ } else if ( options . fd == null ) {
177204 this . fd = null ;
178205 this [ kFs ] = options . fs || fs ;
179206 validateFunction ( this [ kFs ] . open , 'options.fs.open' ) ;
@@ -325,7 +352,11 @@ function WriteStream(path, options) {
325352 // Only buffers are supported.
326353 options . decodeStrings = true ;
327354
328- if ( options . fd == null ) {
355+ if ( options . fd != null && options . windowsHandle != null ) {
356+ throw new ERR_INCOMPATIBLE_OPTION_PAIR ( 'windowsHandle' , 'fd' ) ;
357+ } else if ( options . windowsHandle != null ) {
358+ this . fd = getValidatedFd ( importWindowsHandle ( this , options , O_WRONLY ) ) ;
359+ } else if ( options . fd == null ) {
329360 this . fd = null ;
330361 this [ kFs ] = options . fs || fs ;
331362 validateFunction ( this [ kFs ] . open , 'options.fs.open' ) ;
0 commit comments