index.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. export function isStream(stream, {checkOpen = true} = {}) {
  2. return stream !== null
  3. && typeof stream === 'object'
  4. && (stream.writable || stream.readable || !checkOpen || (stream.writable === undefined && stream.readable === undefined))
  5. && typeof stream.pipe === 'function';
  6. }
  7. export function isWritableStream(stream, {checkOpen = true} = {}) {
  8. return isStream(stream, {checkOpen})
  9. && (stream.writable || !checkOpen)
  10. && typeof stream.write === 'function'
  11. && typeof stream.end === 'function'
  12. && typeof stream.writable === 'boolean'
  13. && typeof stream.writableObjectMode === 'boolean'
  14. && typeof stream.destroy === 'function'
  15. && typeof stream.destroyed === 'boolean';
  16. }
  17. export function isReadableStream(stream, {checkOpen = true} = {}) {
  18. return isStream(stream, {checkOpen})
  19. && (stream.readable || !checkOpen)
  20. && typeof stream.read === 'function'
  21. && typeof stream.readable === 'boolean'
  22. && typeof stream.readableObjectMode === 'boolean'
  23. && typeof stream.destroy === 'function'
  24. && typeof stream.destroyed === 'boolean';
  25. }
  26. export function isDuplexStream(stream, options) {
  27. return isWritableStream(stream, options)
  28. && isReadableStream(stream, options);
  29. }
  30. export function isTransformStream(stream, options) {
  31. return isDuplexStream(stream, options)
  32. && typeof stream._transform === 'function';
  33. }