partey_workadventure/back/src/Server/server/utils.ts

38 lines
974 B
TypeScript
Raw Normal View History

2020-09-28 18:52:54 +02:00
import { ReadStream } from 'fs';
2020-09-29 10:57:14 +02:00
function extend(who: any, from: any, overwrite = true) {
2020-09-28 18:52:54 +02:00
const ownProps = Object.getOwnPropertyNames(Object.getPrototypeOf(from)).concat(
Object.keys(from)
);
ownProps.forEach(prop => {
if (prop === 'constructor' || from[prop] === undefined) return;
if (who[prop] && overwrite) {
who[`_${prop}`] = who[prop];
}
if (typeof from[prop] === 'function') who[prop] = from[prop].bind(who);
else who[prop] = from[prop];
});
}
function stob(stream: ReadStream): Promise<Buffer> {
return new Promise(resolve => {
2020-09-29 10:57:14 +02:00
const buffers: Buffer[] = [];
2020-09-28 18:52:54 +02:00
stream.on('data', buffers.push.bind(buffers));
stream.on('end', () => {
switch (buffers.length) {
case 0:
resolve(Buffer.allocUnsafe(0));
break;
case 1:
resolve(buffers[0]);
break;
default:
resolve(Buffer.concat(buffers));
}
});
});
}
2020-09-29 10:57:14 +02:00
export { extend, stob };