UNPKG

webgme-engine

Version:

WebGME server and Client API without a GUI

233 lines (196 loc) 12.2 kB
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>JSDoc: Source: common/util/serialization.js</title> <script src="scripts/prettify/prettify.js"> </script> <script src="scripts/prettify/lang-css.js"> </script> <!--[if lt IE 9]> <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script> <![endif]--> <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css"> <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css"> </head> <body> <div id="main"> <h1 class="page-title">Source: common/util/serialization.js</h1> <section> <article> <pre class="prettyprint source linenums"><code>/*globals define*/ /** * @author pmeijer / https://github.com/pmeijer */ define([ 'q', 'blob/util', 'common/util/util', 'common/storage/util', 'common/core/coreQ' ], function (Q, blobUtil, commonUtils, storageUtils, Core) { /** * Functions for serializing a webgme project or models. These will work both on the client and server. &lt;br> * To include in your module (e.g. Plugin) require via 'common/util/serialization'. * &lt;br> * &lt;bold>Important!&lt;/bold> In order to export, the state must have been persisted in the database before exporting. * If you make changes with the core make sure to persist and make a commit (it could be a headless commit too) * before attempting to export. * @module serialization */ var exports = {}; /** * Exports a snap-shot of the entire project-tree and uploads a webgmex-file on the blob storage. * @param {ProjectInterface} project * @param {BlobClient} blobClient * @param {object} parameters - One of rootHash, commitHash and branchName and tagName must be given. * If more than one is given, the order of precedence is: branchName, commitHash, tagName and rootHash. * @param {string} [parameters.rootHash] - The hash of the tree root. * @param {string} [parameters.commitHash] - The tree associated with the commitHash. * @param {string} [parameters.branchName] - The tree at the given branch. * @param {string} [parameters.tagName] - The tree at the given tag. * @param {boolean} [parameters.withAssets=false] - Bundle the encountered assets linked from attributes. * @param {string} [parameters.kind] - If not given will use the one defined in project (if any). * @param {string} [parameters.outName] - Name of the output blob (.webgmex will be appended). * @param {function} [callback] * @param {Error|null} callback.err - If there was an error * @param {object} callback.result - Data about the exported project * @param {string} callback.result.hash - Metadata hash to the exported webgmex-file * @param {string} callback.result.downloadUrl - Download url of the exported webgmex-file * @param {string} callback.result.fileName - The name of the exported webgmex-file * @returns {Promise} */ exports.exportProjectToFile = function exportProjectToFile(project, blobClient, parameters, callback) { var fileName; return storageUtils.getProjectJson(project, { branchName: parameters.branchName, commitHash: parameters.commitHash, rootHash: parameters.rootHash, tagName: parameters.tagName, kind: parameters.kind }) .then(function (rawJson) { fileName = typeof parameters.outName === 'string' ? parameters.outName : rawJson.projectId + '_' + (rawJson.commitHash || '').substr(1, 6); fileName += '.webgmex'; return blobUtil.buildProjectPackage(project.logger.fork('blobUtil'), blobClient, rawJson, parameters.withAssets, fileName); }) .then(function (blobHash) { return { hash: blobHash, downloadUrl: blobClient.getRelativeDownloadURL(blobHash), fileName: fileName }; }) .nodeify(callback); }; /** * Exports a selection of models and packages them in webgmexm file which is uploaded to the blob storage. * @param {ProjectInterface} project * @param {BlobClient} blobClient * @param {object} parameters - One of rootHash, commitHash and branchName and tagName must be given. * If more than one is given, the order of precedence is: branchName, commitHash, tagName and rootHash. * @param {string[]} parameters.paths - Paths to node to export. * @param {string} [parameters.rootHash] - The hash of the tree root. * @param {string} [parameters.commitHash] - The tree associated with the commitHash. * @param {string} [parameters.branchName] - The tree at the given branch. * @param {string} [parameters.tagName] - The tree at the given tag. * @param {boolean} [parameters.withAssets=false] * @param {string} [parameters.outName] - Name of the output blob (.webgmex will be appended). * @param {function} [callback] * @param {Error|null} callback.err - If there was an error * @param {object} callback.result - Data about the exported project * @param {string} callback.result.hash - Metadata hash to the exported webgmexm-file * @param {string} callback.result.downloadUrl - Download url of the exported webgmexm-file * @param {string} callback.result.fileName - The name of the exported webgmexm-file * @returns {Promise} */ exports.exportModelsToFile = function exportModelsToFile(project, blobClient, parameters, callback) { var core, closureInfo, fileName; return storageUtils.getRootHash(project, parameters) .then(function (rootHash) { core = new Core(project, { globConf: project.gmeConfig, logger: project.logger }); if (!parameters.paths || parameters.paths instanceof Array === false || parameters.paths.length === 0) { throw new Error('No paths given to export! parameters: ' + JSON.stringify(parameters)); } return core.loadRoot(rootHash); }) .then(function (rootNode) { return Q.all(parameters.paths.map(function (path) { return core.loadByPath(rootNode, path); })); }) .then(function (nodes) { nodes.forEach(function (node, idx) { if (!node) { throw new Error('Given path does not exist [' + parameters.paths[idx] + '].'); } }); // All nodes exist - get the closure info from the core. closureInfo = core.getClosureInformation(nodes); return Q.all(nodes.map(function (node) { return storageUtils.getProjectJson(project, {rootHash: core.getHash(node)}); })); }) .then(function (rawJsons) { var output = { projectId: project.projectId, commitHash: parameters.commitHash, selectionInfo: closureInfo, kind: rawJsons[0].kind, objects: [], hashes: {objects: [], assets: []} }, i; fileName = typeof parameters.outName === 'string' ? parameters.outName : project.projectId + '_' + (output.commitHash || '').substr(1, 6); fileName += '.webgmexm'; for (i = 0; i &lt; rawJsons.length; i += 1) { commonUtils.extendArrayUnique(output.hashes.objects, rawJsons[i].hashes.objects); commonUtils.extendArrayUnique(output.hashes.assets, rawJsons[i].hashes.assets); commonUtils.extendObjectArrayUnique( output.objects, rawJsons[i].objects, project.ID_NAME ); } return blobUtil.buildProjectPackage(project.logger.fork('blobUtil'), blobClient, output, parameters.withAssets, fileName); }) .then(function (blobHash) { return { hash: blobHash, downloadUrl: blobClient.getRelativeDownloadURL(blobHash), fileName: fileName }; }) .nodeify(callback); }; return exports; });</code></pre> </article> </section> </div> <nav> <h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="Server_GMEAuth.html">Server:GMEAuth</a></li><li><a href="Server_SafeStorage.html">Server:SafeStorage</a></li><li><a href="Server_UserProject.html">Server:UserProject</a></li><li><a href="module-Core.html">Core</a></li><li><a href="module-Storage.html">Storage</a></li><li><a href="module-crosscuts.html">crosscuts</a></li><li><a href="module-serialization.html">serialization</a></li></ul><h3>Externals</h3><ul><li><a href="external-Promise.html">Promise</a></li></ul><h3>Classes</h3><ul><li><a href="AddOnBase.html">AddOnBase</a></li><li><a href="AddOnUpdateResult.html">AddOnUpdateResult</a></li><li><a href="Artifact.html">Artifact</a></li><li><a href="BlobClient.html">BlobClient</a></li><li><a href="BlobMetadata.html">BlobMetadata</a></li><li><a href="BlobRunPluginClient.html">BlobRunPluginClient</a></li><li><a href="Client.html">Client</a></li><li><a href="Core.html">Core</a></li><li><a href="ExecutorClient.html">ExecutorClient</a></li><li><a href="GMENode.html">GMENode</a></li><li><a href="GmeLogger.html">GmeLogger</a></li><li><a href="InterPluginResult.html">InterPluginResult</a></li><li><a href="JobInfo.html">JobInfo</a></li><li><a href="OutputInfo.html">OutputInfo</a></li><li><a href="PluginBase.html">PluginBase</a></li><li><a href="PluginConfig.html">PluginConfig</a></li><li><a href="PluginMessage.html">PluginMessage</a></li><li><a href="PluginNodeDescription.html">PluginNodeDescription</a></li><li><a href="PluginResult.html">PluginResult</a></li><li><a href="Project.html">Project</a></li><li><a href="ProjectInterface.html">ProjectInterface</a></li><li><a href="Server_GMEAuth-GMEAuth.html">GMEAuth</a></li><li><a href="Server_SafeStorage-SafeStorage.html">SafeStorage</a></li><li><a href="Server_UserProject-UserProject.html">UserProject</a></li><li><a href="WebsocketRouter.html">WebsocketRouter</a></li><li><a href="WebsocketRouterUser.html">WebsocketRouterUser</a></li></ul><h3>Events</h3><ul><li><a href="Client.html#event:BRANCH_CHANGED">BRANCH_CHANGED</a></li><li><a href="Client.html#event:BRANCH_CLOSED">BRANCH_CLOSED</a></li><li><a href="Client.html#event:BRANCH_OPENED">BRANCH_OPENED</a></li><li><a href="Client.html#event:BRANCH_STATUS_CHANGED">BRANCH_STATUS_CHANGED</a></li><li><a href="Client.html#event:CONNECTED_USERS_CHANGED">CONNECTED_USERS_CHANGED</a></li><li><a href="Client.html#event:NETWORK_STATUS_CHANGED">NETWORK_STATUS_CHANGED</a></li><li><a href="Client.html#event:NOTIFICATION">NOTIFICATION</a></li><li><a href="Client.html#event:PLUGIN_FINISHED">PLUGIN_FINISHED</a></li><li><a href="Client.html#event:PLUGIN_INITIATED">PLUGIN_INITIATED</a></li><li><a href="Client.html#event:PLUGIN_NOTIFICATION">PLUGIN_NOTIFICATION</a></li><li><a href="Client.html#event:PROJECT_CLOSED">PROJECT_CLOSED</a></li><li><a href="Client.html#event:PROJECT_OPENED">PROJECT_OPENED</a></li></ul><h3><a href="global.html">Global</a></h3> </nav> <br class="clear"> <footer> Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.2</a> on Fri Jun 21 2024 09:43:40 GMT-0400 (Eastern Daylight Time) </footer> <script> prettyPrint(); </script> <script src="scripts/linenumber.js"> </script> </body> </html>