Workflow of my module - node.js

I'm writing my first "serious" node.js module and I need some advices/best practices on a couple of things...
Please read comments in the code for the questions!
server.js
var Client = require('./index');
var client = Client.createClient();
client.on('data', function(data) {
console.log(data);
});
client.on('ERROR', function(data) {
console.log(data);
});
node-module.js
exports.Client = require('./Client');
exports.createClient = function(options) {
return new exports.Client(options);
};
/**
* Question 1:
* I want to have an optional WebUI for this, how should it recieve the data?
* I figured via event-emitters is best(?)
* But where should it be placed (and how) so it can access the events from Client.js?
*/
exports.createServer = function(options) {
//...
}
Client.js
Client.Handlers = require('./Handlers');
module.exports = Client;
function Client(options) {
// ...
this._callback_handlers['ERROR'] = Client.Handlers.error
/**
* Question 2:
* Initialize connection to server
*
* Should it be something like this instead:
* this._socket = this.Connect(options); ?
* How do I achieve this?
* And at the same time keeping the buffer-variable local to the Connect function?
*/
this.Connect();
});
Client.prototype.Connect = function() {
var self = this;
var buffer = '';
this._socket = net.connect({
host: self.options.host,
port: self.options.port
}, function() {
})
.on('data', function(chunk) {
var offset, part;
buffer += chunk;
/**
* Question 3:
* Buffer up and parse only whole rows
*
* This is a OK way to do it, right?
* Should I listen for the event "data"
* instead of calling self.parse_response() like a normal function?
*
*/
while((offset = buffer.indexOf("\n")) > -1) {
part = buffer.substr(0, offset);
buffer = buffer.substr(offset + 1);
if(part) {
self.emit('data', part);
self.parse_response(part);
}
}
})
};
Client.prototype.parse_response = function(data) {
// ...
/**
* Question 4:
* Send callbacks
*
* _callback_handlers is an array containing callbacks functions
* (see top of Client.js: this._callback_handlers['ERROR'] = Client.Handlers.error)
*
* I would perfer to use something like this instead:
* this.emit(data[0], data[1]);
*
* But how do I make Handlers.js listen to these events?
* since "Client" dosent exist there
*
* The reason I have handlers.js in the first place
* is to give the code a better structure
*
*/
if(this._callback_handlers[data[0]]) {
this._callback_handlers[data[0]].apply(this, data[1]);
}
}
Handlers.js
//...
exports.error = function(a) {
this.emit('ERROR', a);
}
//...

Related

Node ExcelJS writing file and saving to S3 heap out of memory issue

I have a download center module where I am generating multiple excel files S3 links at the same against the passed filters from frontend using mongodb aggregations. It all works fine however it is taking in a lot of memory and sometimes report generation crashes due to javascript heap out of memory error. I need to know how I can resolve this heap out of memory error. I am using latest version of exceljs(4.3.0) and using LoopbackJS 3(deprecated) as Node framework. Here are some code snippets which might help you guys with figuring out the issue.
const excelReport = new ExcelReport(report.name);
excelReport.setHeaders(headers);
const aggregation = executeEventBaseAggregation(
"RiderDeliveryBatch",
pipeline
);
aggregation.event.on("data", function (x) {
let rows = {};
rows["Date Created At"] = utils.localDateTime(x.parcel.createdAt);
rows["Pickup Date"] = utils.localDateTime(x.parcelStatuses.find(
(y) =>
y.statusRepositoryId.toString() ===
statuses.find((val) => val.key === "parcel-picked-up").id.toString()
).createdAt);
rows["CN"] = x.parcel._id;
rows["Vendor Order ID"] = x.parcel.vendorParcelId;
rows["Vendor Name"] = x.vendor.name;
rows["Consignee First Name"] = x.customerData.firstName;
rows["Consignee Last Name"] = x.customerData.lastName;
rows["Address"] = x.customerData.address;
rows["Origin City"] = getCities(x.parcel.originCityId);
rows["Destination City"] = cities.find(
(y) => y.id.toString() === x.customerData.cityId.toString()
).name;
rows["Destination subCity"] = x.customerData["subCity"] ? x.customerData["subCity"] : "";
rows["COD"] = x.parcel.amount;
rows["Attempts"] = confirmDispatched.length;
excelReport.addRow(rows);
});
aggregation.event.on("error", function (e) {
console.log("aggregation error");
updateDownloadCenterReport(downloadCenterReport, {
status: "error",
e,
});
});
aggregation.event.on("done", function () {
console.log("aggregation done");
excelReport
.upload()
.then(async (link) => {
await updateDownloadCenterReport(downloadCenterReport, {
excelLink: link,
status: "success",
pdfLink: "",
});
longRunningTaskEnd();
})
.catch(async (error) => {
await updateDownloadCenterReport(downloadCenterReport, {
status: "error",
error,
});
longRunningTaskEnd();
});
});
ExcelJS util module
class ExcelReport {
/**
* Initializes excel sheet with provided worksheet name
* #param {string} worksheetName
*/
constructor(worksheetName) {
this.workbook = new Excel.Workbook();
this.worksheet = this.workbook.addWorksheet(worksheetName);
}
/**
*
* #param {{header:string;key:string;width:number}[]} headers
*/
setHeaders(headers) {
this.worksheet.columns = headers;
return this;
}
/**
*
* #param {{}} row
*/
addRow(row) {
this.worksheet.addRow(row);
return this;
}
/**
*
* #param {{}[]} rows
*/
addRows(rows) {
this.worksheet.addRows(rows);
return this;
}
/**
* uploads the file to AWS
* #returns {Promise<string>}
*/
async upload() {
const aws = new FileUploadService(
awsConfig.accessKeyId,
awsConfig.secretAccessKey,
awsConfig.bucketName
);
const bufferFile = await this.workbook.xlsx.writeBuffer();
const link = await aws.uploadBuffer(
"download-center",
bufferFile,
"xlsx",
this.worksheet.name
);
return link;
}
}

AWS X-Ray w/ HAPI - No logs in Console

Trying out AWS new service X-RAY for my microservice deployment on Kubernetes on EC2. Here are the logs I am seeing:
root#internal-reporting-test-1680758663-3gweh:/usr/src/app# cat AWSXRay.log
2016-12-06 04:36:23.5840 +00:00 [INFO] UDP message sent: {"trace_id":"1-58464048-03fbcdf5c861f95fc5204ec6","id":"105107e6c9df4bfe","start_time":1480998983.553,"name":"localhost","http":{"request":{"method":"GET","user_agent":"curl/7.38.0","client_ip":"127.0.0.1","url":"http://localhost"},"response":{"status":404,"content_length":0}},"service":{"version":"1.0.0"},"error":true,"end_time":1480998983.574}
2016-12-06 04:36:33.2560 +00:00 [INFO] UDP message sent: {"trace_id":"1-58464051-3e3799a566c0933093f51b7a","id":"94d4267609d2ba40","start_time":1480998993.205,"name":"localhost","http":{"request":{"method":"GET","user_agent":"curl/7.38.0","client_ip":"127.0.0.1","url":"http://localhost"},"response":{"status":200,"content_length":0}},"service":{"version":"1.0.0"},"end_time":1480998993.252}
2016-12-06 04:36:35.5810 +00:00 [INFO] UDP message sent: {"trace_id":"1-58464054-5df1e9ec46ef2b26b2d29965","id":"113fd36cddb5de95","start_time":1480998995.552,"name":"localhost","http":{"request":{"method":"GET","user_agent":"curl/7.38.0","client_ip":"127.0.0.1","url":"http://localhost"},"response":{"status":200,"content_length":0}},"service":{"version":"1.0.0"},"end_time":1480998995.579}
2016-12-06 04:36:41.3350 +00:00 [INFO] UDP message sent: {"trace_id":"1-58464059-0dfd2e274ca20499c6bce17f","id":"1dfac818142b2d28","start_time":1480999001.304,"name":"localhost","http":{"request":{"method":"GET","user_agent":"curl/7.38.0","client_ip":"127.0.0.1","url":"http://localhost"},"response":{"status":200,"content_length":0}},"service":{"version":"1.0.0"},"end_time":1480999001.332}
Curl I am using:
# curl localhost/v1/internal/report/users
Supervisor Conf:
[supervisord]
nodaemon=true
[program:xray]
command=/usr/src/app/xray
[program:npm]
command=npm start
Mods to express middleware to use with hapi:
/**
* Express_mw module.
*
* Exposes middleware functions to enable automated data capturing on a web service. To enable on a Node.js/Express application,
* use 'app.use(AWSXRay.express.openSegment())' before defining your routes. After your routes, before any extra error
* handling middleware, use 'app.use(AWSXRay.express.closeSegment())'.
* Use AWSXRay.getSegment() to access the current segment/subsegment.
* Otherwise, for manual mode, this appends the Segment object to the request object as req.segment.
* #module express_mw
*/
var _ = require('underscore');
var CLSUtils = require('../utils').CLSUtils;
var MWUtils = require('./mw_utils');
var Local = require('../segments/attributes/local');
var Segment = require('../segments/segment');
var getCauseType = require('../utils').getCauseTypeFromHttpStatus;
var XRAY_HEADER = 'x-amzn-trace-id';
var NAME = process.env.XRAY_TRACING_NAME;
var DEFAULT_NAME = process.env.XRAY_TRACING_DEFAULT_NAME;
/**
* Use 'app.use(AWSXRay.express.openSegment())' before defining your routes.
* Use AWSXRay.getSegment() to access the current segment/subsegment.
* Otherwise, for manual mode, this appends the Segment object to the request object as req.segment.
* #alias module:express_mw.openSegment
* #returns {function}
*/
module.exports.openSegment = function openSegment() {
if (_.isUndefined(DEFAULT_NAME) && _.isUndefined(MWUtils.defaultName)) {
throw new Error('Default segment name must be set using "process.env.XRAY_TRACING_DEFAULT_NAME"' +
' or "setDefaultName()".');
}
return function open(request, next) {
/*
* Get the true HTTP objects
*/
req = request.raw.req;
res = request.raw.res;
var amznTraceHeader = processHeaders(req);
var segment = new Segment(resolveName(req.headers.host), amznTraceHeader.Root, amznTraceHeader.Parent);
resolveSampling(amznTraceHeader, segment, res);
segment.addAttribute('http', new Local(req));
MWUtils.populate(segment);
res.on('finish', function () {
if (this.statusCode === 429)
segment.addThrottle();
else if (getCauseType(this.statusCode))
segment[getCauseType(this.statusCode)] = true;
segment.http.close(this);
segment.close();
});
if (CLSUtils.isCLSMode()) {
var ns = CLSUtils.getNamespace();
ns.bindEmitter(req);
ns.bindEmitter(res);
ns.run(function () {
CLSUtils.setSegment(segment);
if (next) { next(); }
});
} else {
req.segment = segment;
if (next) { next(); }
}
};
};
/**
* After your routes, before any extra error handling middleware, use 'app.use(AWSXRay.express.closeSegment())'.
* #alias module:express_mw.closeSegment
* #returns {function}
*/
module.exports.closeSegment = function closeSegment() {
return function close(request, next) {
req = request.raw.req;
res = request.raw.res;
var segment = CLSUtils.isCLSMode() ? CLSUtils.getSegment() : segment = req.segment;
if (segment && err) {
segment.counter = 0;
segment.close(err);
} else if (segment) {
segment.close();
}
if (next)
next(err);
};
};
function processHeaders(req) {
var amznTraceHeader = {};
if (req && req.headers && req.headers[XRAY_HEADER]) {
_.each(req.headers[XRAY_HEADER].replace(/ /g,'').split(';'), function (header) {
var pair = header.split('=');
this[pair[0]] = pair[1];
}, amznTraceHeader);
}
return amznTraceHeader;
}
function resolveName(hostName) {
if (NAME)
return NAME;
var regex = new RegExp('(?:[0-9]{1,3}\.){3}[0-9]{1,3}', 'g');
var hostIp = hostName.match(regex) || _.isEmpty(hostName);
return !hostIp ? hostName : (DEFAULT_NAME ? DEFAULT_NAME : MWUtils.defaultName);
}
function resolveSampling(amznTraceHeader, segment, res) {
var isSampled;
if (amznTraceHeader.Sampled === '1')
isSampled = true;
else if (amznTraceHeader.Sampled === '0')
isSampled = false;
isSampled = !_.isUndefined(isSampled) ? isSampled : MWUtils.shouldSample();
if (amznTraceHeader.Sampled === '?')
res.header[XRAY_HEADER] = 'Root=' + amznTraceHeader.Root + '; Sampled=' + (isSampled ? '1' : '0');
if (!isSampled)
segment.notTraced = true;
}
The changes are relatively simple while playing with this service. Just pretty much forcing the raw node HTTP objects to the middleware.
Logs are showing the UDP requests to the xray service are working as expected. Yet in the console I see no results.
Anyone have any ideas?

nodejs process.exit() prevents function from executing properly

I am writing my first NodeJs script. Below is some code I have set up to test a database connection.
When I include process.exit() at the very end of the script, nothing is logged to the console - however, when I simply remove that line of code, the function logs the query results appropriately (output included also).
I am wondering why the process.exit() at the very end of the code prevents the function from functioning and if I am using the exit method wrong.
Code:
/*
* Runs every minute on an uptime agent
* Checks list of sites to see if they're up
*/
// strict mode (see http://stackoverflow.com/questions/8651415/what-is-strict-mode-and-how-is-it-used for information)
'use strict';
// cuz im lazy
String.prototype.lc = function() { return this.toLowerCase(); }
String.prototype.uc = function() { return this.toUpperCase(); }
/** EXCEPTIONS **/
var DbConnectError = function(m) {
this.name = 'DbConnectError';
this.message = m;
}
var DbConfigError = function(m) {
this.name = 'DbConfigError';
this.message = m;
}
/** DATABSE **/
/*
* change log
* 08/07/2015 Tyler J Barnes
* -- init dev
*/
var db = function() {
// error messages
this._errors = [];
// connection state
this._isConnected = false;
// db configuration
this._config = {
host: '',
user: '',
password: '',
database: ''
};
// reference
this._db = null;
// connection obj ref
this._con = null;
// sql statement
this._sqlStmt = '';
// is prepared statement -- needs data binded
this._isPrepared = false;
// data to bind to prepared stmts
this._sqlData = null;
// query result set
this._result = null;
/*
* initialize
* #param (object) : cofig prop and values
* #return void
*/
this.ini = function(config) {
// make sure config was passed
if(!config || (typeof config).lc() != 'object') {
throw new DbConnectError('Invalid DB Configuration');
}
// check for appropriate properties
// if exist, store value
for(var p in this._config) {
if(!(p in config)) {
this._errors.push('Missing database config: '+p+'\n');
} else {
this._config[p] = config[p];
}
}
// throw any errors before continue
if(this._errors.length > 0) {
var tmp = '';
for(var i = 0; i < this._errors.length; i++) {
tmp+=this._errors[i];
}
throw new DbConfigError(tmp);
}
this._db = require('mysql');
};
// create connection -- returns thread id
this.con = function() {
this._con = this._db.createConnection(this._config);
this._con.connect();
this._isConnected = true;
};
// sets sql statement
this.setSqlStmt = function(str, prepared, bindData) {
this._sqlStmt = str;
if(prepared) {
this._isPrepared = true;
this._sqlData = bindData;
} else {
this._isPrepared = false;
this._sqlData = null;
}
};
// kills connection
this.die = function() {
if(this._isConnected) {
this._con.end();
}
};
}
var c = {
host: 'asdfasdf',
user: 'asdfasdf',
password: 'asdfasdf',
database: 'asdfasdf'
};
var d = new db();
d.ini(c);
d.con();
d._con.query('SELECT * FROM Agents', function(err,rows,fields) {
if(err) console.log(err);
console.log(rows);
});
d._con.end();
// this is what upsets me
process.exit();
Output when process.exit() is removed:
[{agentid:1, host: 'asdfasdfadf'....etc}]
The database query operation is asynchronous. This means that it will only be concluded after the program executes all of the main module, including the exit system call.
Instead, you should only terminate the process when the results are obtained from the database:
var d = new db();
d.ini(c);
d.con();
d._con.query('SELECT * FROM Agents', function(err,rows,fields) {
if(err) console.log(err);
console.log(rows);
process.exit();
});
d._con.end();
This is one of the typical misunderstandings of how JavaScript's asynchronous function calls work. I would advise you to read on the subject. These two questions may have some useful content:
How does Asynchronous Javascript Execution happen? and when not to use return statement?
How does JavaScript handle AJAX responses in the background?

SmoothStreaming issues on Chromecast

I'm trying to load a smoothstreaming media into the chromecast. For that I've used the samples provided by google:
<body>
<video id='vid' />
<script type="text/javascript"
src="//www.gstatic.com/cast/sdk/libs/receiver/2.0.0/cast_receiver.js">
</script>
<script type="text/javascript"
src="//www.gstatic.com/cast/sdk/libs/mediaplayer/0.3.0/media_player.js">
</script>
<script type="text/javascript">
window.onload = function() {
// If you set ?Debug=true in the URL, such as a different App ID in the
// developer console, include debugging information.
cast.receiver.logger.setLevelValue(cast.receiver.LoggerLevel.DEBUG);
var mediaElement = document.getElementById('vid');
// Create the media manager. This will handle all media messages by default.
window.mediaManager = new cast.receiver.MediaManager(mediaElement);
// Remember the default value for the Receiver onLoad, so this sample can Play
// non-adaptive media as well.
window.defaultOnLoad = mediaManager.onLoad;
mediaManager.onLoad = function (event) {
/proto
// MPEG-DASH
protocol = cast.player.api.CreateDashStreamingProtocol(host);
} else if (url.indexOf('.ism/') >= 0) {
// Smooth Streaming
protocol = cast.player.api.CreateSmoothStreamingProtocol(host);
}
// How to override a method in Host. I know that it's safe to just provide this
// method.
host.onError = function(errorCode) {
console.log("Fatal Error - "+errorCode);
window.player.unload();
};
// If you need cookies, then set withCredentials = true also set any header
// information you need. If you don't need them, there can be some unexpected
// effects by setting this value.
// host.updateSegmentRequestInfo = function(requestInfo) {
// requestInfo.withCredentials = true;
// };
console.log("we have protocol "+ext);
if (protocol !== null) {
console.log("Starting Media Player Library");
window.player = new cast.player.api.Player(host);
window.player.load(protocol, initStart);
}
mediaElement.autoplay = autoplay; // Make sure autoplay get's set
if (url.lastIndexOf('.m3u8') >= 0) {
// HTTP Live Streaming
protocol = cast.player.api.CreateHlsStreamingProtocol(host);
} else if (url.lastIndexOf('.mpd') >= 0) {
// MPEG-DASH
protocol = cast.player.api.CreateDashStreamingProtocol(host);
} else if (url.indexOf('.ism/') >= 0) {
// Smooth Streaming
protocol = cast.player.api.CreateSmoothStreamingProtocol(host);
}
// How to override a method in Host. I know that it's safe to just provide this
// method.
host.onError = function(errorCode) {
console.log("Fatal Error - "+errorCode);
window.player.unload();
};
// If you need cookies, then set withCredentials = true also set any header
// information you need. If you don't need them, there can be some unexpected
// effects by setting this value.
// host.updateSegmentRequestInfo = function(requestInfo) {
// requestInfo.withCredentials = true;
// };
console.log("we have protocol "+ext);
And on the sender:
/**
* global variables
*/
var currentMediaSession = null;
var currentVolume = 0.5;
var progressFlag = 1;
var mediaCurrentTime = 0;
var session = null;
var mediaURLs = [
'http://playready.directtaps.net/smoothstreaming/TTLSS720VC1/To_The_Limit_720.ism/',
'http://commondatastorage.googleapis.com/gtv-videos-bucket/ED_1280.mp4',
'http://commondatastorage.googleapis.com/gtv-videos-bucket/tears_of_steel_1080p.mov',
'http://commondatastorage.googleapis.com/gtv-videos-bucket/reel_2012_1280x720.mp4',
'http://commondatastorage.googleapis.com/gtv-videos-bucket/Google%20IO%202011%2045%20Min%20Walk%20Out.mp3'];
var mediaTitles = [
'Big Buck Bunny',
'Elephant Dream',
'Tears of Steel',
'Reel 2012',
'Google I/O 2011 Audio'];
var mediaThumbs = [
'images/bunny.jpg',
'images/ed.jpg',
'images/Tears.jpg',
'images/reel.jpg',
'images/google-io-2011.jpg'];
var currentMediaURL = mediaURLs[0];
/**
* Call initialization
*/
if (!chrome.cast || !chrome.cast.isAvailable) {
setTimeout(initializeCastApi, 1000);
}
/**
* initialization
*/
function initializeCastApi() {
// default app ID to the default media receiver app
// optional: you may change it to your own app ID/receiver
var applicationID = '21176C05';
var sessionRequest = new chrome.cast.SessionRequest(applicationID);
var apiConfig = new chrome.cast.ApiConfig(sessionRequest,
sessionListener,
receiverListener);
chrome.cast.initialize(apiConfig, onInitSuccess, onError);
};
/**
* initialization success callback
*/
function onInitSuccess() {
appendMessage("init success");
}
/**
* initialization error callback
*/
function onError() {
console.log("error");
appendMessage("error");
}
/**
* generic success callback
*/
function onSuccess(message) {
console.log(message);
}
/**
* callback on success for stopping app
*/
function onStopAppSuccess() {
console.log('Session stopped');
appendMessage('Session stopped');
document.getElementById("casticon").src = 'images/cast_icon_idle.png';
}
/**
* session listener during initialization
*/
function sessionListener(e) {
console.log('New session ID: ' + e.sessionId);
appendMessage('New session ID:' + e.sessionId);
session = e;
if (session.media.length != 0) {
appendMessage(
'Found ' + session.media.length + ' existing media sessions.');
onMediaDiscovered('onRequestSessionSuccess_', session.media[0]);
}
session.addMediaListener(
onMediaDiscovered.bind(this, 'addMediaListener'));
session.addUpdateListener(sessionUpdateListener.bind(this));
}
/**
* session update listener
*/
function sessionUpdateListener(isAlive) {
var message = isAlive ? 'Session Updated' : 'Session Removed';
message += ': ' + session.sessionId;
appendMessage(message);
if (!isAlive) {
session = null;
}
};
/**
* receiver listener during initialization
*/
function receiverListener(e) {
if( e === 'available' ) {
console.log("receiver found");
appendMessage("receiver found");
}
else {
console.log("receiver list empty");
appendMessage("receiver list empty");
}
}
/**
* select a media URL
* #param {string} m An index for media URL
*/
function selectMedia(m) {
console.log("media selected" + m);
appendMessage("media selected" + m);
currentMediaURL = mediaURLs[m];
var playpauseresume = document.getElementById("playpauseresume");
document.getElementById('thumb').src = mediaThumbs[m];
}
/**
* launch app and request session
*/
function launchApp() {
console.log("launching app...");
appendMessage("launching app...");
chrome.cast.requestSession(onRequestSessionSuccess, onLaunchError);
}
/**
* callback on success for requestSession call
* #param {Object} e A non-null new session.
*/
function onRequestSessionSuccess(e) {
console.log("session success: " + e.sessionId);
appendMessage("session success: " + e.sessionId);
session = e;
document.getElementById("casticon").src = 'images/cast_icon_active.png';
}
/**
* callback on launch error
*/
function onLaunchError() {
console.log("launch error");
appendMessage("launch error");
}
/**
* stop app/session
*/
function stopApp() {
session.stop(onStopAppSuccess, onError);
}
/**
* load media
* #param {string} i An index for media
*/
function loadMedia(i) {
if (!session) {
console.log("no session");
appendMessage("no session");
return;
}
console.log("loading..." + currentMediaURL);
appendMessage("loading..." + currentMediaURL);
var mediaInfo = new chrome.cast.media.MediaInfo(currentMediaURL);
mediaInfo.contentType = 'application/vnd.ms-sstr+xml';
var request = new chrome.cast.media.LoadRequest(mediaInfo);
request.autoplay = false;
request.currentTime = 0;
var payload = {
"title:" : mediaTitles[i],
"thumb" : mediaThumbs[i]
};
var json = {
"payload" : payload
};
request.customData = json;
session.loadMedia(request,
onMediaDiscovered.bind(this, 'loadMedia'),
onMediaError);
}
/**
* callback on success for loading media
* #param {Object} e A non-null media object
*/
function onMediaDiscovered(how, mediaSession) {
console.log("new media session ID:" + mediaSession.mediaSessionId);
appendMessage("new media session ID:" + mediaSession.mediaSessionId + ' (' + how + ')');
currentMediaSession = mediaSession;
mediaSession.addUpdateListener(onMediaStatusUpdate);
mediaCurrentTime = currentMediaSession.currentTime;
playpauseresume.innerHTML = 'Play';
document.getElementById("casticon").src = 'images/cast_icon_active.png';
}
/**
* callback on media loading error
* #param {Object} e A non-null media object
*/
function onMediaError(e) {
console.log("media error");
appendMessage("media error");
document.getElementById("casticon").src = 'images/cast_icon_warning.png';
}
/**
* callback for media status event
* #param {Object} e A non-null media object
*/
function onMediaStatusUpdate(isAlive) {
if( progressFlag ) {
document.getElementById("progress").value = parseInt(100 * currentMediaSession.currentTime / currentMediaSession.media.duration);
}
document.getElementById("playerstate").innerHTML = currentMediaSession.playerState;
}
/**
* play media
*/
function playMedia() {
if( !currentMediaSession )
return;
var playpauseresume = document.getElementById("playpauseresume");
if( playpauseresume.innerHTML == 'Play' ) {
currentMediaSession.play(null,
mediaCommandSuccessCallback.bind(this,"playing started for " + currentMediaSession.sessionId),
onError);
playpauseresume.innerHTML = 'Pause';
//currentMediaSession.addListener(onMediaStatusUpdate);
appendMessage("play started");
}
else {
if( playpauseresume.innerHTML == 'Pause' ) {
currentMediaSession.pause(null,
mediaCommandSuccessCallback.bind(this,"paused " + currentMediaSession.sessionId),
onError);
playpauseresume.innerHTML = 'Resume';
appendMessage("paused");
}
else {
if( playpauseresume.innerHTML == 'Resume' ) {
currentMediaSession.play(null,
mediaCommandSuccessCallback.bind(this,"resumed " + currentMediaSession.sessionId),
onError);
playpauseresume.innerHTML = 'Pause';
appendMessage("resumed");
}
}
}
}
/**
* stop media
*/
function stopMedia() {
if( !currentMediaSession )
return;
currentMediaSession.stop(null,
mediaCommandSuccessCallback.bind(this,"stopped " + currentMediaSession.sessionId),
onError);
var playpauseresume = document.getElementById("playpauseresume");
playpauseresume.innerHTML = 'Play';
appendMessage("media stopped");
}
/**
* set media volume
* #param {Number} level A number for volume level
* #param {Boolean} mute A true/false for mute/unmute
*/
function setMediaVolume(level, mute) {
if( !currentMediaSession )
return;
var volume = new chrome.cast.Volume();
volume.level = level;
currentVolume = volume.level;
volume.muted = mute;
var request = new chrome.cast.media.VolumeRequest();
request.volume = volume;
currentMediaSession.setVolume(request,
mediaCommandSuccessCallback.bind(this, 'media set-volume done'),
onError);
}
/**
* mute media
* #param {DOM Object} cb A checkbox element
*/
function muteMedia(cb) {
if( cb.checked == true ) {
document.getElementById('muteText').innerHTML = 'Unmute media';
setMediaVolume(currentVolume, true);
appendMessage("media muted");
}
else {
document.getElementById('muteText').innerHTML = 'Mute media';
setMediaVolume(currentVolume, false);
appendMessage("media unmuted");
}
}
/**
* seek media position
* #param {Number} pos A number to indicate percent
*/
function seekMedia(pos) {
console.log('Seeking ' + currentMediaSession.sessionId + ':' +
currentMediaSession.mediaSessionId + ' to ' + pos + "%");
progressFlag = 0;
var request = new chrome.cast.media.SeekRequest();
request.currentTime = pos * currentMediaSession.media.duration / 100;
currentMediaSession.seek(request,
onSeekSuccess.bind(this, 'media seek done'),
onError);
}
/**
* callback on success for media commands
* #param {string} info A message string
* #param {Object} e A non-null media object
*/
function onSeekSuccess(info) {
console.log(info);
appendMessage(info);
setTimeout(function(){progressFlag = 1},1500);
}
/**
* callback on success for media commands
* #param {string} info A message string
* #param {Object} e A non-null media object
*/
function mediaCommandSuccessCallback(info) {
console.log(info);
appendMessage(info);
}
/**
* append message to debug message window
* #param {string} message A message string
*/
function appendMessage(message) {
var dw = document.getElementById("debugmessage");
dw.innerHTML += '\n' + JSON.stringify(message);
};
I am trying media #1 which is .ism - I also tried with the /Manifest - but no luck.
In the debugging console I am getting:
[214.054s] [cast.player.api.Player] load -3
media_player.js:23
[214.060s] [goog.net.XhrIo] Opening Xhr [GET http://playready.directtaps.net/smoothstreaming/TTLSS720VC1/To_The_Limit_720.ism/Manifest -1]
media_player.js:23
[214.065s] [goog.net.XhrIo] Will abort after 10000ms if incomplete, xhr2 false [GET http://playready.directtaps.net/smoothstreaming/TTLSS720VC1/To_The_Limit_720.ism/Manifest -1]
media_player.js:23
[214.070s] [goog.net.XhrIo] Sending request [GET http://playready.directtaps.net/smoothstreaming/TTLSS720VC1/To_The_Limit_720.ism/Manifest -1]
media_player.js:23
[214.088s] [goog.net.XhrIo] Request complete [GET http://playready.directtaps.net/smoothstreaming/TTLSS720VC1/To_The_Limit_720.ism/Manifest 200]
media_player.js:23
[214.120s] [cast.player.api.Player] sourceopen
media_player.js:23
[214.142s] [cast.player.api.Player] play
media_player.js:23
[214.291s] [cast.receiver.MediaManager] Load metadata error
cast_receiver.js:19
[214.296s] [cast.receiver.MediaManager] Resetting media element
cast_receiver.js:19
[214.303s] [cast.receiver.MediaManager] Sending error message to 4:client-34217
cast_receiver.js:19
[214.307s] [cast.receiver.IpcChannel] IPC message sent: {"namespace":"urn:x-cast:com.google.cast.media","senderId":"4:client-34217","data":"{\"requestId\":9217017,\"type\":\"LOAD_FAILED\"}"}
cast_receiver.js:19
[214.171s] [cast.player.api.Player] error
media_player.js:23
Fatal Error - 1 index.html:61
[214.176s] [cast.player.api.Player] unload -3
Any idea???
Thanks!
Where did you find that URL? It's incomplete, the right one is http://playready.directtaps.net/smoothstreaming/TTLSS720VC1/To_The_Limit_720.ism/manifest, but it is unlikely to work. Other files have been posted at: Microsoft's SmoothStreaming samples files, that said, I don't believe that Microsoft is providing a CORS header that works for Chromecast.
We do provide some very simple media that you can host yourself. And we provide a very simple server, if you need that to host and provide a CORS header as well.

how to define error message in sails.js

It's my first time using sails and it looks like it's good but I run into a problem, is it possible to define a custom error message in sails model validation because it looks like the error message being returned is to technical and not user friendly.
Thanks
Update: https://gist.github.com/mikermcneil/8366092
Here's another alternative:
/**
* Takes a Sails Model object (e.g. User) and a ValidationError object and translates it into a friendly
* object for sending via JSON to client-side frameworks.
*
* To use add a new object on your model describing what validation errors should be translated:
*
* module.exports = {
* attributes: {
* name: {
* type: 'string',
* required: true
* }
* },
*
* validation_messages: {
* name: {
* required: 'you have to specify a name or else'
* }
* }
* };
*
* Then in your controller you could write something like this:
*
* var validator = require('sails-validator-tool');
*
* Mymodel.create(options).done(function(error, mymodel) {
* if(error) {
* if(error.ValidationError) {
* error_object = validator(Mymodel, error.Validation);
* res.send({result: false, errors: error_object});
* }
* }
* });
*
* #param model {Object} An instance of a Sails.JS model object.
* #param validationErrors {Object} A standard Sails.JS validation object.
*
* #returns {Object} An object with friendly validation error conversions.
*/
module.exports = function(model, validationError) {
var validation_response = {};
var messages = model.validation_messages;
validation_fields = Object.keys(messages);
validation_fields.forEach(function(validation_field) {
if(validationError[validation_field]) {
var processField = validationError[validation_field];
//console.log(processField);
processField.forEach(function(rule) {
if(messages[validation_field][rule.rule]) {
if(!(validation_response[validation_field] instanceof Array)) {
validation_response[validation_field] = new Array();
}
var newMessage={};
newMessage[rule.rule] = messages[validation_field][rule.rule];
validation_response[validation_field].push(newMessage);
}
});
}
});
return validation_response;
};
Credits to: sfb_
Here is an ugly fix:
make a folder named my-validation-utils. create a index.js file there. and put the following content there:
var user = {
email:{
required:'Email Required',
email:'Should be an email'
},
name:{
required:'name required'
}
};
var product={
name:{
required:'Product name is required'
}
}
var validationMessages = {
user:user,
product:product
};
/**
* This function expects the name of the model and error.validationError
* and puts the user defined messages in error.validationError
*/
module.exports = function(model,validationError){
var messages = validationMessages[model];
for(key in messages){
var element = messages[key];
if(validationError[key]){
for(i in validationError[key]){
var err = validationError[key][i];
err.message = element[err.rule];
}
}
}
return validationError;
};
Now in your controller do something like this:
User.create(user).done(function (error, user) {
if (error) {
if (error.ValidationError) {
var validator = require('path/to/my-validation-utils');
var errors = validator('user',error.ValidationError);// puts the messages for model user
//now errors contains the validationErrors with user defined messages
}
} else {
//user is saved
}
});

Resources