@kevingodell/node-red-pipe2jpeg
Advanced tools
Comparing version 0.1.1-beta.0 to 0.1.1-beta.1
@@ -14,7 +14,5 @@ { | ||
"base_path_duplicate": "basePath \"__basePath__\" is already in use", | ||
"pipe2jpeg_not_found": "pipe2jpeg not found for \"__basePath__\"", | ||
"mjpeg_video_not_found": "mjpeg video not found for \"__basePath__\"", | ||
"jpeg_image_not_found": "jpeg image not found for \"__basePath__\"" | ||
"jpeg_data_not_found": "jpeg data not found for \"__basePath__\"" | ||
} | ||
} | ||
} |
{ | ||
"name": "@kevingodell/node-red-pipe2jpeg", | ||
"version": "0.1.1-beta.0", | ||
"version": "0.1.1-beta.1", | ||
"description": "Parse a jpeg from a buffer and send it to a downstream node or serve it via http.", | ||
@@ -19,2 +19,3 @@ "main": "pipe2jpeg.js", | ||
"keywords": [ | ||
"node-red", | ||
"buffer", | ||
@@ -49,3 +50,3 @@ "jpeg", | ||
"dependencies": { | ||
"pipe2jpeg": "^0.4.0" | ||
"pipe2jpeg": "^0.4.1" | ||
}, | ||
@@ -52,0 +53,0 @@ "files": [ |
204
pipe2jpeg.js
@@ -74,7 +74,3 @@ 'use strict'; | ||
status: `pipe2jpeg/${this.basePath}/status`, | ||
buffer: { | ||
// jpeg: `pipe2jpeg/${this.basePath}/buffer/jpeg`, | ||
array: `pipe2jpeg/${this.basePath}/buffer/array`, | ||
concat: `pipe2jpeg/${this.basePath}/buffer/concat`, | ||
}, | ||
buffer: `pipe2jpeg/${this.basePath}/buffer/${this.bufferType}`, | ||
}; | ||
@@ -106,3 +102,3 @@ } | ||
this.send({ _msgid: this._msgid, topic: this.topic.status, status: 'playlist', payload: this.playlist }); | ||
this.send({ _msgid: this._msgid, topic: this.topic.status, retain: true, status: 'playlist', payload: this.playlist }); | ||
} | ||
@@ -117,52 +113,16 @@ | ||
// const topic = this.topic.buffer.jpeg; | ||
const retain = true; | ||
const retain = false; | ||
const topic = this.topic.buffer; | ||
const onData = (() => { | ||
if (this.bufferType === 'concat') { | ||
const topic = this.topic.buffer.concat; | ||
const getPayload = this.bufferType === 'concat' ? Pipe2jpegNode.getPayloadAsJpeg : Pipe2jpegNode.getPayloadAsList; | ||
return ({ jpeg }) => { | ||
if (this.resWaitingForMjpeg && this.resWaitingForMjpeg.size > 0) { | ||
this.resWaitingForMjpeg.forEach(res => { | ||
if (res.writableEnded === false || res.finished === false) { | ||
res.write(`Content-Type: image/jpeg\r\nContent-Length: ${jpeg.length}\r\n\r\n`); | ||
this.pipe2jpeg.on('data', data => { | ||
const { payload, totalLength } = getPayload(data); | ||
res.write(jpeg); | ||
this.send([null, { _msgid: this._msgid, topic, retain, payload, totalLength }]); | ||
}); | ||
res.write('\r\n--pipe2jpeg\r\n'); | ||
} | ||
}); | ||
} | ||
this.send([null, { _msgid: this._msgid, topic, retain, payload: jpeg }]); | ||
}; | ||
} else { | ||
const topic = this.topic.buffer.array; | ||
return ({ list, totalLength }) => { | ||
if (this.resWaitingForMjpeg && this.resWaitingForMjpeg.size > 0) { | ||
this.resWaitingForMjpeg.forEach(res => { | ||
if (res.writableEnded === false || res.finished === false) { | ||
res.write(`Content-Type: image/jpeg\r\nContent-Length: ${totalLength}\r\n\r\n`); | ||
list.forEach(buffer => { | ||
res.write(buffer); | ||
}); | ||
res.write('\r\n--pipe2jpeg\r\n'); | ||
} | ||
}); | ||
} | ||
this.send([null, { _msgid: this._msgid, topic, retain, payload: list, totalLength }]); | ||
}; | ||
} | ||
})(); | ||
this.pipe2jpeg.on('data', onData); | ||
this.pipe2jpeg.on('error', error => { | ||
this.pipe2jpeg.resetCacheEmit(); | ||
this.pipe2jpeg.resetCache(); | ||
@@ -190,3 +150,3 @@ this.error(error); | ||
this.send({ _msgid: this._msgid, topic: this.topic.status, status: 'reset', payload: this.playlist }); | ||
this.send({ _msgid: this._msgid, topic: this.topic.status, retain: true, status: 'reset', payload: this.playlist }); | ||
} | ||
@@ -209,3 +169,3 @@ | ||
this.pipe2jpeg.resetCacheEmit(); | ||
this.pipe2jpeg.resetCache(); | ||
@@ -262,6 +222,2 @@ this.pipe2jpeg = undefined; | ||
/* if (key === 'm3u8' && typeof value === 'string') { | ||
return value.split('\n').slice(0, -1); | ||
}*/ | ||
return value; | ||
@@ -286,96 +242,57 @@ }, | ||
httpRouter.use((req, res, next) => { | ||
if (this.pipe2jpeg instanceof Pipe2jpeg) { | ||
if (this.pipe2jpeg instanceof Pipe2jpeg && this.pipe2jpeg.totalLength) { | ||
return next(); | ||
} | ||
return res.status(404).send(_('pipe2jpeg.error.pipe2jpeg_not_found', { basePath: this.basePath })); | ||
return res.status(404).send(_('pipe2jpeg.error.jpeg_data_not_found', { basePath: this.basePath })); | ||
}); | ||
const [getImage, getVideo] = (() => { | ||
if (this.bufferType === 'concat') { | ||
return [ | ||
(req, res) => { | ||
const { jpeg } = this.pipe2jpeg; | ||
const [getPayload, writePayload] = | ||
this.bufferType === 'concat' ? [Pipe2jpegNode.getPayloadAsJpeg, Pipe2jpegNode.writePayloadAsJpeg] : [Pipe2jpegNode.getPayloadAsList, Pipe2jpegNode.writePayloadAsList]; | ||
if (jpeg) { | ||
res.type('jpeg'); | ||
const getImage = (req, res) => { | ||
res.type('jpeg'); | ||
return res.send(jpeg); | ||
} | ||
const { payload } = getPayload(this.pipe2jpeg); | ||
res.status(404).send(_('pipe2jpeg.error.jpeg_image_not_found', { basePath: this.basePath })); | ||
}, | ||
(req, res) => { | ||
const { jpeg } = this.pipe2jpeg; | ||
writePayload(res, payload); | ||
if (jpeg) { | ||
res.set('Content-Type', 'multipart/x-mixed-replace;boundary=pipe2jpeg'); | ||
res.end(); | ||
}; | ||
// res.write('--pipe2jpeg\r\n'); | ||
const getVideo = (req, res) => { | ||
const { payload, totalLength } = getPayload(this.pipe2jpeg); | ||
res.write(`Content-Type: image/jpeg\r\nContent-Length: ${jpeg.length}\r\n\r\n`); | ||
res.set('Content-Type', 'multipart/x-mixed-replace;boundary=pipe2jpeg'); | ||
res.write(jpeg); | ||
res.write(`Content-Type: image/jpeg\r\nContent-Length: ${totalLength}\r\n\r\n`); | ||
res.write('\r\n--pipe2jpeg\r\n'); | ||
writePayload(res, payload); | ||
this.resWaitingForMjpeg.add(res); | ||
res.write('\r\n--pipe2jpeg\r\n'); | ||
return res.once('close', () => { | ||
this.resWaitingForMjpeg instanceof Set && this.resWaitingForMjpeg.delete(res); | ||
this.resWaitingForMjpeg.add(res); | ||
res.end(); | ||
}); | ||
} | ||
return res.once('close', () => { | ||
this.resWaitingForMjpeg instanceof Set && this.resWaitingForMjpeg.delete(res); | ||
return res.status(404).send(_('pipe2jpeg.error.mjpeg_video_not_found', { basePath: this.basePath })); | ||
}, | ||
]; | ||
} else { | ||
return [ | ||
(req, res) => { | ||
const { list } = this.pipe2jpeg; | ||
res.end(); | ||
}); | ||
}; | ||
if (list) { | ||
res.type('jpeg'); | ||
this.pipe2jpeg.on('data', data => { | ||
if (this.resWaitingForMjpeg && this.resWaitingForMjpeg.size > 0) { | ||
const { payload, totalLength } = getPayload(data); | ||
list.forEach(buffer => { | ||
res.write(buffer); | ||
}); | ||
this.resWaitingForMjpeg.forEach(res => { | ||
if (res.writableEnded === false || res.finished === false) { | ||
res.write(`Content-Type: image/jpeg\r\nContent-Length: ${totalLength}\r\n\r\n`); | ||
return res.end(); | ||
} | ||
writePayload(res, payload); | ||
res.status(404).send(_('pipe2jpeg.error.jpeg_image_not_found', { basePath: this.basePath })); | ||
}, | ||
(req, res) => { | ||
const { list, totalLength } = this.pipe2jpeg; | ||
if (list) { | ||
res.set('Content-Type', 'multipart/x-mixed-replace;boundary=pipe2jpeg'); | ||
// res.write('--pipe2jpeg\r\n'); | ||
res.write(`Content-Type: image/jpeg\r\nContent-Length: ${totalLength}\r\n\r\n`); | ||
list.forEach(buffer => { | ||
res.write(buffer); | ||
}); | ||
res.write('\r\n--pipe2jpeg\r\n'); | ||
this.resWaitingForMjpeg.add(res); | ||
return res.once('close', () => { | ||
this.resWaitingForMjpeg instanceof Set && this.resWaitingForMjpeg.delete(res); | ||
res.end(); | ||
}); | ||
} | ||
return res.status(404).send(_('pipe2jpeg.error.mjpeg_video_not_found', { basePath: this.basePath })); | ||
}, | ||
]; | ||
res.write('\r\n--pipe2jpeg\r\n'); | ||
} | ||
}); | ||
} | ||
})(); | ||
}); | ||
@@ -433,3 +350,3 @@ httpRouter.get('/image.jpeg', getImage); | ||
reset() { | ||
this.pipe2jpeg.resetCacheEmit(); | ||
this.pipe2jpeg.resetCache(); | ||
} | ||
@@ -462,3 +379,3 @@ | ||
handleMsg(msg) { | ||
const { payload, action } = msg; | ||
const { payload } = msg; | ||
@@ -482,4 +399,2 @@ if (Buffer.isBuffer(payload)) { | ||
this.reset(); | ||
return; | ||
} | ||
@@ -499,2 +414,20 @@ } | ||
} | ||
static getPayloadAsJpeg(data) { | ||
return { payload: data.jpeg, totalLength: data.totalLength }; | ||
} | ||
static getPayloadAsList(data) { | ||
return { payload: data.list, totalLength: data.totalLength }; | ||
} | ||
static writePayloadAsJpeg(res, payload) { | ||
res.write(payload); | ||
} | ||
static writePayloadAsList(res, payload) { | ||
payload.forEach(buffer => { | ||
res.write(buffer); | ||
}); | ||
} | ||
} | ||
@@ -517,8 +450,2 @@ | ||
Pipe2jpeg.prototype.resetCacheEmit = function () { | ||
this.emit('reset'); | ||
this.resetCache(); | ||
}; | ||
Pipe2jpeg.prototype.toJSON = function () { | ||
@@ -530,4 +457,3 @@ return { | ||
timestamp: this.timestamp, | ||
byteOffset: this.byteOffset, | ||
}; | ||
}; |
31319
667
Updatedpipe2jpeg@^0.4.1