 help                      = CND.get_logger 'help',      badge
 urge                      = CND.get_logger 'urge',      badge
 echo                      = CND.echo.bind CND
-# #...........................................................................................................
-# PATH                      = require 'path'
-# OS                        = require 'os'
+#...........................................................................................................
+PATH                      = require 'path'
 FS                        = require 'fs'
 CP                        = require 'child_process'
 glob                      = require 'globby'
-### files, conversion from/to NodeJS push streams: ###
-### later
-new_file_source           = require 'pull-file'
-new_file_sink             = require 'pull-write-file'
-###
-STPS                      = require 'stream-to-pull-stream'
 #...........................................................................................................
-### stream creation: ###
 new_pushable              = require 'pull-pushable'
-#...........................................................................................................
-### transforms: ###
 $pull_split               = require 'pull-split'
 # $pull_stringify           = require 'pull-stringify'
 $pull_utf8_decoder        = require 'pull-utf8-decoder'

@_isa_writeonly_njs_stream  = ( x ) -> ( @_isa_njs_stream x ) and x.writable and not x.readable
@_isa_duplex_njs_stream     = ( x ) -> ( @_isa_njs_stream x ) and x.readable and     x.writable


#-----------------------------------------------------------------------------------------------------------
# @_nodejs_input_to_pull_source = ( P... ) -> STPS.source P...


#===========================================================================================================
# READ FROM, WRITE TO FILES, NODEJS STREAMS
#-----------------------------------------------------------------------------------------------------------
@read_from_file = ( path, options ) ->
  ### TAINT consider using https://pull-stream.github.io/#pull-file-reader instead ###
  switch ( arity = arguments.length )
    when 1 then null
    when 2
      if CND.isa_function options
        [ path, options, on_stop, ] = [ path, null, options, ]
    else throw new Error "µ9983 expected 1 or 2 arguments, got #{arity}"
  #.........................................................................................................
  return @read_from_nodejs_stream ( FS.createReadStream path, options )

#-----------------------------------------------------------------------------------------------------------
@write_to_file = ( path, options, on_stop ) ->
  ### TAINT consider using https://pull-stream.github.io/#pull-write-file instead ###
  ### TAINT code duplication ###
  switch ( arity = arguments.length )
    when 1 then null
    when 2
      if CND.isa_function options
        [ path, options, on_stop, ] = [ path, null, options, ]
    when 3
    else throw new Error "µ9983 expected 1 to 3 arguments, got #{arity}"
  #.........................................................................................................
  return @write_to_nodejs_stream ( FS.createWriteStream path, options ), on_stop

#-----------------------------------------------------------------------------------------------------------
@read_from_nodejs_stream = ( stream ) ->
  switch ( arity = arguments.length )
    when 1 then null
    else throw new Error "µ9983 expected 1 argument, got #{arity}"
  #.........................................................................................................
  return STPS.source stream, ( error ) -> finish error

#-----------------------------------------------------------------------------------------------------------
@write_to_nodejs_stream = ( stream, on_stop ) ->
  ### TAINT code duplication ###
  switch ( arity = arguments.length )
    when 1, 2 then null
    else throw new Error "µ9983 expected 1 or 2 arguments, got #{arity}"
  #.........................................................................................................
  if on_stop? and ( ( type = CND.type_of on_stop ) isnt 'function' )
    throw new Error "µ9383 expected a function, got a #{type}"
  #.........................................................................................................
  has_finished = false
  #.........................................................................................................
  finish = ( error ) ->
    ### In case there was an error, throw that error if we already called on_stop, or there is no
    callback given; this is to prevent silent failures: ###
    if error? and ( has_finished or ( not on_stop? ) )
      has_finished = true
      throw error
    #.......................................................................................................
    ### Otherwise, call back (with optional error) only in case we have not yet finished; this is to
    prevent inadvertently calling back more than once: ###
    if not has_finished
      has_finished = true
      if on_stop?
        debug '39983'
        return on_stop error if error?
        return on_stop()
    #.......................................................................................................
    return null
  #.........................................................................................................
  stream.on 'close', -> finish()
  return STPS.sink stream, ( error ) -> finish error


#===========================================================================================================

  delete x[ key ]
  return R


#===========================================================================================================
# ISA METHODS
