Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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)) }
}
.then((readStream) => {
t.ok(isStream(readStream, 'returns a stream'))
return streamUtils.streamToString(readStream)
})
.then((val) => {
async function writeEach(input, targets, rewriter = ident) {
const content = isStream(input) ? await streamToString(input) : input;
const jobs = targets.map(async target =>
sander.writeFile(target, rewriter(content, target))
);
return Promise.all(jobs);
}
async function writeEach(input, targets, rewriter = ident) {
const content = isStream(input) ? await streamToString(input) : input;
const jobs = targets.map(async target => sander.writeFile(target, rewriter(content, target)));
return Promise.all(jobs);
}
export function extract(input = '') {
const fileStream = isStream(input) ? input : intoStream(input);
const tikaStream = got.stream.put(URL, {
headers: {Accept: 'text/plain'}
});
return getStream(fileStream.pipe(tikaStream));
}
async setCustomAvatar(userID, stream) {
const { users } = this.uw;
if (!this.supportsCustomAvatars()) {
throw new PermissionError('Custom avatars are not enabled.');
}
const user = await users.getUser(userID);
await assertPermission(user, 'avatar.custom');
if (!isStream(stream)) {
throw new TypeError('Custom avatar must be a stream (eg. a http Request instance).');
}
const imageStream = await toImageStream(stream);
const metadata = await new Promise((resolve, reject) => {
const writeStream = this.store.createWriteStream({
key: `${user.id}.${imageStream.type}`,
}, (err, meta) => {
if (err) reject(err);
else resolve(meta);
});
pump(imageStream, writeStream);
});
const finalKey = metadata.key;
const url = new URL(finalKey, this.options.publicPath);