How to use is-stream - 10 common examples

To help you get started, we’ve selected a few is-stream examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github stolksdorf / vitreum / node_modules / gulp-nodemon / node_modules / nodemon / node_modules / update-notifier / node_modules / latest-version / node_modules / package-json / node_modules / got / index.js View on Github external
delete opts.body;
	delete opts.json;
	delete opts.timeout;

	if (json) {
		opts.headers.accept = opts.headers.accept || 'application/json';
	}

	if (body) {
		if (typeof body !== 'string' && !Buffer.isBuffer(body) && !isStream.readable(body)) {
			throw new GotError('options.body must be a ReadableStream, string or Buffer');
		}

		opts.method = opts.method || 'POST';

		if (!opts.headers['content-length'] && !opts.headers['transfer-encoding'] && !isStream.readable(body)) {
			var length = typeof body === 'string' ? Buffer.byteLength(body) : body.length;
			opts.headers['content-length'] = length;
		}
	}

	opts.method = opts.method || 'GET';

	// returns a proxy stream to the response
	// if no callback has been provided
	if (!cb) {
		proxy = duplexify();

		// forward errors on the stream
		cb = function (err, data, response) {
			proxy.emit('error', err, data, response);
		};
github ipfs / js-ipfs / src / core / components / files-regular.js View on Github external
        const isBufferOrStream = obj => Buffer.isBuffer(obj) || isStream.readable(obj) || isSource(obj)
        // An object like { content?, path? }, where content isBufferOrStream and path isString
github ipfs / js-ipfs / src / core / components / files-regular.js View on Github external
return content.map((data) => {
    // Buffer input
    if (Buffer.isBuffer(data)) {
      data = { path: '', content: pull.values([data]) }
    }

    // Readable stream input
    if (isStream.readable(data)) {
      data = { path: '', content: toPull.source(data) }
    }

    if (isSource(data)) {
      data = { path: '', content: data }
    }

    if (data && data.content && typeof data.content !== 'function') {
      if (Buffer.isBuffer(data.content)) {
        data.content = pull.values([data.content])
      }

      if (isStream.readable(data.content)) {
        data.content = toPull.source(data.content)
      }
    }
github ipfs-shipyard / ipfs-postmsg-proxy / src / client / files / add.js View on Github external
function fileToJson (file, opts) {
  opts = opts || {}

  if (isBuffer(file)) { // Buffer
    if (opts.onProgressIncrement) opts.onProgressIncrement(file.length)
    return bufferToJson(file)
  } else if (isStream.readable(file)) { // Node stream
    return pullStreamToJson(toPull.source(file), opts)
  } else if (isSource(file)) { // Pull stream
    return pullStreamToJson(file, opts)
  } else if (file && file.content) { // Object { path?, content }
    return Object.assign({}, file, { content: fileToJson(file.content, opts) })
  }

  return file // Object { path } maybe, but could be anything
}
github open-wc / open-wc / packages / es-dev-server / src / utils / utils.js View on Github external
export async function getBodyAsString(ctx) {
  let requestCanceled;
  ctx.req.on('close', () => {
    requestCanceled = true;
  });

  if (Buffer.isBuffer(ctx.body)) {
    return ctx.body.toString();
  }

  if (typeof ctx.body === 'string') {
    return ctx.body;
  }

  if (isStream(ctx.body)) {
    // cache request path, see above
    // @ts-ignore
    if (ctx.body.path) {
      // @ts-ignore
      filePathsForRequests.set(ctx.request, ctx.body.path);
    }

    // a stream can only be read once, so after reading it assign
    // the string response to the body so that it can be accessed
    // again later
    try {
      const bodyBuffer = await getStream.buffer(ctx.body);
      const contentLength = Number(ctx.response.get('content-length'));

      if (await isBinaryFile(bodyBuffer, contentLength)) {
        ctx.body = bodyBuffer;
github nfour / serverless-build-plugin / lib / ServerlessBuild.js View on Github external
// Ensure file exists first
                if ( stats.isFile() )
                    artifactZip.addFile(filePath, fileName, this.config.zip)
            })
        } else
        if ( typeOf.String(result) || result instanceof Buffer ) {
            //
            // STRINGS, BUFFERS
            //

            if ( typeOf.String(result) ) result = new Buffer(result)

            artifactZip.addBuffer(result, 'handler.js', this.config.zip)

        } else
        if ( isStream(result) ) {
            //
            // STREAMS
            //

            artifactZip.addReadStream(result, 'handler.js', this.config.zip)

        } else {
            throw new Error("Unrecognized build output")
        }

        // TODO: read from serverless.yml -> package.includes for extenerals as well as excludes

        return artifactZip
    }
github binded / persistent-memoize / src / stream-utils.js View on Github external
export const toStream = (val) => {
  if (isStream(val)) {
    return { type: 'stream', bodyStream: val }
  }
  if (Buffer.isBuffer(val)) {
    return { type: 'buffer', bodyStream: bufferToStream(val) }
  }
  // Assume value can be encoded to json
  return { type: 'json', bodyStream: stringToStream(JSON.stringify(val)) }
}
github roccomuso / transfer-sh / index.js View on Github external
return new Promise(function (resolve, reject) {
    var wStream = isWritable(destination) ? destination : fs.createWriteStream(destination)
    eos(wStream, function (err) {
      if (err) return reject(new Error('stream had an error or closed early'))
      resolve(this)
    })
    /* start decrypt */
    self.inputStream.pipe(base64.decode())
      .pipe(self.sDecrypt)
      .pipe(wStream)
  })
}
github catdad / grandma / lib / is-stream.js View on Github external
//
    // So we are just going to manually make this pass...
    //
    // https://github.com/nodejs/node/issues/8828
    //
    // womp womp
    if (type === 'writable' && stream === process.stdout) {
        return true;
    }

    if (!isStream[type](stream)) {
        throw new TypeError(errStr);
    }
};

isStream.assertObjectStream = function assertObjectStream(stream, type, errStr) {
    isStream.assertStream(stream, type, errStr);

    if (type === 'readable') {
        assertObjectMode(stream, '_readableState', errStr);
    } else if (type === 'writable') {
        assertObjectMode(stream, '_writableState', errStr);
    }
};

module.exports = isStream;
github catdad / grandma / lib / is-stream.js View on Github external
var isStream = require('is-stream');

function assertType(type) {
    if (type !== 'readable' && type !== 'writable') {
        throw new Error(type + ' is not a known stream type');
    }
}

function assertObjectMode(stream, state, errStr) {
    if (!stream[state] || !stream[state].objectMode) {
        throw new TypeError(errStr);
    }
}

isStream.stdio = function isStdio(stream) {
    return stream === process.stdin ||
           stream === process.stdout ||
           stream === process.stderr;
};

isStream.assertStream = function assertStream(stream, type, errStr) {
    assertType(type);

    // When redirecting to a file, for some reason, this is not
    // detected as a writable stream, even though it is.
    //
    //   `grandma run testname -d 2ms -c 1 > file.log`
    //
    // So we are just going to manually make this pass...
    //
    // https://github.com/nodejs/node/issues/8828

is-stream

Check if something is a Node.js stream

MIT
Latest version published 10 months ago

Package Health Score

79 / 100
Full package analysis