Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'is-stream' in functional components in JavaScript. Our advanced machine learning engine meticulously scans each line of code, cross-referencing millions of open source libraries to ensure your implementation is not just functional, but also robust and secure. Elevate your React applications to new heights by mastering the art of handling side effects, API calls, and asynchronous operations with confidence and precision.
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);
};
const isBufferOrStream = obj => Buffer.isBuffer(obj) || isStream.readable(obj) || isSource(obj)
// An object like { content?, path? }, where content isBufferOrStream and path isString
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)
}
}
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
}
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;
// 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
}
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)) }
}
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)
})
}
//
// 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;
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