UNPKG

neft

Version:

Universal Platform

1,731 lines (1,509 loc) 731 kB
var Neft = (function(){ var process = typeof global !== 'undefined' && global.process || {env: {}}; process.env['NEFT_PLATFORM'] = "android"; process.env['NEFT_CLIENT'] = "1"; process.env['NEFT_NATIVE'] = "1"; process.env['NEFT_ANDROID'] = "1"; var __modules = {"index.coffee": function(exports){ var module = {exports: exports}; var require = __createRequire({"src/app":"src/app/index.litcoffee","src/utils":"src/utils/index.litcoffee","src/signal":"src/signal/index.litcoffee","src/dict":"src/dict/index.litcoffee","src/list":"src/list/index.litcoffee","src/log":"src/log/index.litcoffee","src/resources":"src/resources/index.litcoffee","src/renderer":"src/renderer/index.litcoffee","src/networking":"src/networking/index.litcoffee","src/schema":"src/schema/index.litcoffee","src/document":"src/document/index.litcoffee","src/assert":"src/assert/index.litcoffee","src/db":"src/db/index.litcoffee","src/native":"src/native/index.litcoffee","src/styles":"src/styles/index.coffee","src/eventLoop":"src/eventLoop/index.coffee","src/tryCatch":"src/tryCatch/index.coffee","src/binding":"src/binding/index.coffee"}); var exports = module.exports; (function(){ (function() { 'use strict'; var exports; exports = module.exports = require('src/app'); exports.utils = require('src/utils'); exports.signal = require('src/signal'); exports.Dict = require('src/dict'); exports.List = require('src/list'); exports.log = require('src/log'); exports.Resources = require('src/resources'); exports.Renderer = require('src/renderer'); exports.Networking = require('src/networking'); exports.Schema = require('src/schema'); exports.Document = require('src/document'); exports.styles = require('src/styles'); exports.assert = require('src/assert'); exports.db = require('src/db'); exports.eventLoop = require('src/eventLoop'); exports.tryCatch = require('src/tryCatch'); exports.Binding = require('src/binding'); exports["native"] = (function() { try { return require('src/native'); } catch (undefined) {} })(); exports.nmlParser = (function() { try { return require('src/nml-parser'); } catch (undefined) {} })(); }).call(this); }()); return module.exports; }, "package.json": function(exports){ var module = {exports: exports}; var require = __createRequire({}); var exports = module.exports; (function(){ module.exports = { "name": "neft", "version": "0.11.23", "preferGlobal": true, "description": "Universal Platform", "keywords": [ "framework", "native", "android", "ios", "webgl" ], "bin": { "neft": "bin/neft.js" }, "main": "index.coffee", "license": "Apache-2.0", "homepage": "http://neft.io", "author": "Krystian Kruk <krystian@neft.io>", "contributors": [], "repository": { "type": "git", "url": "https://github.com/Neft-io/neft.git" }, "bugs": "https://github.com/Neft-io/neft/issues", "engines": { "node": ">=8.0.0" }, "scripts": { "bundle": "cross-env NODE_PATH=./ coffee --nodejs --harmony scripts/bundle.coffee", "test": "node ./bin/neft.js test", "update-api-docs": "cross-env NODE_PATH=./ coffee scripts/api-docs.coffee", "prepublishOnly": "npm run bundle" }, "dependencies": { "acorn": "^5.2.1", "babel-core": "6.22.1", "babel-preset-es2015": "6.22.0", "babel-preset-es2016": "6.22.0", "babel-preset-es2017": "6.22.0", "chokidar": "1.5.1", "coffee-script": "1.10.0", "cross-env": "1.0.8", "form-data": "1.0.0-rc4", "fs-extra": "0.30.0", "glob": "7.0.3", "htmlparser2": "3.9.0", "http-server": "0.9.0", "jimp": "0.2.27", "js-yaml": "3.6.1", "mkdirp": "0.5.1", "mustache": "2.2.1", "node-notifier": "4.6.0", "node-static": "0.7.7", "open": "0.0.5", "pegjs": "0.10.0", "pngjs": "3.0.0", "qs": "6.2.0", "semver": "5.1.0", "slash": "1.0.0", "uglify-js": "2.7.5", "which": "1.2.12", "xcode": "0.8.7" }, "devDependencies": { "coffee-coverage": "1.0.1", "coveralls": "2.11.14", "escape-html": "1.0.3", "imgur": "0.2.1", "istanbul": "^0.4.5", "wd": "0.4.0" }, "android": { "package": "io.neft.tests", "versionCode": 1, "versionName": "1.0", "application": { "label": "Neft.io Tests" } }, "ios": { "package": "io.neft.tests", "application": { "label": "Neft.io Tests" } } } ; }()); return module.exports; }, "src/app/index.litcoffee": function(exports){ var module = {exports: exports}; var require = __createRequire({"src/utils":"src/utils/index.litcoffee","src/log":"src/log/index.litcoffee","src/signal":"src/signal/index.litcoffee","src/db":"src/db/index.litcoffee","src/assert":"src/assert/index.litcoffee","src/schema":"src/schema/index.litcoffee","src/networking":"src/networking/index.litcoffee","src/document":"src/document/index.litcoffee","src/renderer":"src/renderer/index.litcoffee","src/styles":"src/styles/index.coffee","src/resources":"src/resources/index.litcoffee","src/dict":"src/dict/index.litcoffee","./route":"src/app/route.litcoffee","package.json":"package.json"}); var exports = module.exports; (function(){ (function() { 'use strict'; var AppRoute, BASE_FILE_NAME_RE, DEFAULT_CONFIG, Dict, Document, Networking, Renderer, Resources, Schema, Styles, assert, bootstrapRoute, db, exports, log, pkg, signal, utils; utils = require('src/utils'); log = require('src/log'); signal = require('src/signal'); db = require('src/db'); assert = require('src/assert'); Schema = require('src/schema'); Networking = require('src/networking'); Document = require('src/document'); Renderer = require('src/renderer'); Styles = require('src/styles'); Resources = require('src/resources'); Dict = require('src/dict'); AppRoute = require('./route'); bootstrapRoute = (function() { try { return require('./bootstrap/route.node'); } catch (undefined) {} })(); //<development>; pkg = require('package.json'); //</development>; BASE_FILE_NAME_RE = /(.+)\.(?:node|server|client|browser|ios|android|native)/; DEFAULT_CONFIG = { title: 'Neft.io Application', protocol: 'http', port: 3000, host: 'localhost', language: 'en', type: 'app' }; exports = module.exports = function(opts, extraOpts) { var COOKIES_KEY, IS_CLIENT, app, component, config, ext, i, init, j, k, l, len, len1, len2, len3, method, obj, onCookiesReady, path, r, ref, ref1, ref2, ref3, ref4, ref5, route, style, stylesInitObject, windowStyle, windowStyleItem; if (opts == null) { opts = {}; } if (extraOpts == null) { extraOpts = {}; } null; //<development>; log.ok("Welcome! Neft.io v" + pkg.version + "; Feedback appreciated"); log.warn("Use this bundle only in development; type --release when it's ready"); //</development>; config = utils.clone(DEFAULT_CONFIG); config = utils.mergeAll(config, opts.config, extraOpts); app = new Dict; app.config = config; app.networking = new Networking({ type: Networking.HTTP, protocol: config.protocol, port: parseInt(config.port, 10), host: config.host, url: config.url, language: config.language }); app.routes = {}; app.styles = {}; app.components = {}; app.resources = (function() { if (opts.resources) { return Resources.fromJSON(opts.resources); } else { return new Resources; } })(); signal.create(app, 'onReady'); if (config.type == null) { config.type = 'app'; } assert.ok(utils.has(['app', 'game', 'text'], config.type), "Unexpected app.config.type value. Accepted app/game/text, but '" + config.type + "' got."); app.Route = AppRoute(app); COOKIES_KEY = '__neft_cookies'; IS_CLIENT = !process.env.NEFT_SERVER; app.cookies = Dict(); onCookiesReady = function(dict) { app.cookies = dict; if (IS_CLIENT) { return dict.set('sessionId', utils.uid(16)); } }; db.get(COOKIES_KEY, db.OBSERVABLE, function(err, dict) { var cookies; if (dict) { return onCookiesReady(dict); } else { if (IS_CLIENT) { cookies = { clientId: utils.uid(16) }; } else { cookies = {}; } return db.set(COOKIES_KEY, cookies, function(err) { return db.get(COOKIES_KEY, db.OBSERVABLE, function(err, dict) { if (dict) { return onCookiesReady(dict); } }); }); } }); app.networking.onRequest(function(req, res) { if (IS_CLIENT) { utils.merge(req.cookies, app.cookies); } else { utils.merge(res.cookies, app.cookies); } req.onLoadEnd.listeners.unshift(function() { var key, ref, val; if (IS_CLIENT) { ref = res.cookies; for (key in ref) { val = ref[key]; if (!utils.isEqual(app.cookies[key], val)) { app.cookies.set(key, val); } } } }, null); }); Renderer.setResources(app.resources); Renderer.setServerUrl(app.networking.url); Renderer.onLinkUri(function(uri) { return app.networking.createLocalRequest({ method: Networking.Request.GET, type: Networking.Request.HTML_TYPE, uri: uri }); }); app.documentScripts = utils.arrayToObject(opts.scripts, function(index, elem) { return elem.name; }, function(index, elem) { return elem.file; }); Document.Scripts.initialize(app.documentScripts); if (opts.styles != null) { ref = opts.styles; for (i = 0, len = ref.length; i < len; i++) { style = ref[i]; if ((ref1 = style.name) === 'windowItem' || ref1 === '__windowItem__') { style.file._init({ windowItem: null }); windowStyle = style.file._main({ document: null }); break; } } } app.windowItem = windowStyleItem = windowStyle != null ? windowStyle.item : void 0; assert.ok(windowStyleItem, '__windowItem__ style must be defined'); Renderer.setWindowItem(windowStyleItem); if (opts.styles != null) { stylesInitObject = { windowItem: windowStyleItem }; ref2 = opts.styles; for (j = 0, len1 = ref2.length; j < len1; j++) { style = ref2[j]; if (!(style.name != null)) { continue; } if (style.name !== 'windowItem') { style.file._init(stylesInitObject); } app.styles[style.name] = style.file; } } Styles({ windowStyle: windowStyle, styles: app.styles, queries: opts.styleQueries, resources: app.resources }); if (typeof bootstrapRoute === "function") { bootstrapRoute(app); } init = function(files, target) { var _, baseName, baseNameMatch, file, fileObj, k, len2; for (k = 0, len2 = files.length; k < len2; k++) { file = files[k]; if (!(file.name != null)) { continue; } if (typeof file.file !== 'function') { continue; } fileObj = file.file(app); target[file.name] = fileObj; if (baseNameMatch = BASE_FILE_NAME_RE.exec(file.name)) { _ = baseNameMatch[0], baseName = baseNameMatch[1]; if (target[baseName] != null) { if (utils.isPlainObject(target[baseName]) && utils.isPlainObject(fileObj)) { fileObj = utils.merge(Object.create(target[baseName]), fileObj); } } target[baseName] = fileObj; } } }; if (utils.isObject(opts.extensions)) { ref3 = opts.extensions; for (k = 0, len2 = ref3.length; k < len2; k++) { ext = ref3[k]; ext(app); } } exports.app = { Route: app.Route }; ref4 = opts.components; for (l = 0, len3 = ref4.length; l < len3; l++) { component = ref4[l]; if (component.name != null) { app.components[component.name] = Document.fromJSON(component.file); } } init(opts.routes, app.routes); ref5 = app.routes; for (path in ref5) { obj = ref5[path]; r = {}; if (utils.isObject(obj) && !(obj instanceof app.Route)) { for (method in obj) { opts = obj[method]; if (utils.isObject(opts)) { route = new app.Route(method, opts); r[route.name] = route; } else { r[method] = opts; } } } app.routes[path] = r; } if (!app.routes.index) { app.routes.index = new app.Route('get /', {}); } app.onReady.emit(); return app; }; }).call(this); }()); return module.exports; }, "src/app/route.litcoffee": function(exports){ var module = {exports: exports}; var require = __createRequire({"src/utils":"src/utils/index.litcoffee","src/signal":"src/signal/index.litcoffee","src/assert":"src/assert/index.litcoffee","src/log":"src/log/index.litcoffee","src/schema":"src/schema/index.litcoffee","src/networking":"src/networking/index.litcoffee","src/document":"src/document/index.litcoffee","src/renderer":"src/renderer/index.litcoffee","src/dict":"src/dict/index.litcoffee"}); var exports = module.exports; (function(){ (function() { 'use strict'; var Dict, Document, IS_CLIENT, IS_NODE, Networking, Renderer, Schema, assert, log, signal, utils; utils = require('src/utils'); signal = require('src/signal'); assert = require('src/assert'); log = require('src/log'); Schema = require('src/schema'); Networking = require('src/networking'); Document = require('src/document'); Renderer = require('src/renderer'); Dict = require('src/dict'); log = log.scope('App', 'Route'); IS_NODE = process.env.NEFT_NODE; IS_CLIENT = !IS_NODE; module.exports = function(app) { var Route; return Route = (function() { var callNextIfNeeded, createToHTMLFromObject, destroyRoute, factoryRoute, finishRequest, getDefaultRouteComponentName, getRouteName, handleRequest, onResponseSent, pendingRoutes, prepareRouteData, renderComponentFromConfig, resolveAsyncGetDataFuncCallback, resolveSyncGetDataFunc, routeToString, routesCache, templates, usedTemplates; if (IS_NODE) { usedTemplates = []; } else { templates = Object.create(null); } Route.lastClientRoute = null; Route.onLastClientRouteChange = signal.create(); Route.getTemplateComponent = (function() { if (IS_NODE) { return function(name) { var scope, tmpl; scope = { app: app, routes: new Dict }; tmpl = app.components[name].render(null, scope); usedTemplates.push(tmpl); return tmpl; }; } else { return function(name) { var scope; scope = { app: app, routes: new Dict }; return templates[name] != null ? templates[name] : templates[name] = app.components[name].render(null, scope); }; } })(); function Route(method, uri, opts) { var key, spaceIndex, val; if (utils.isObject(method)) { opts = method; } else if (utils.isObject(uri)) { opts = uri; } else if (!utils.isObject(opts)) { opts = {}; } if (typeof method === 'string' && typeof uri !== 'string') { opts.uri = method; } else if (typeof method === 'string' && typeof uri === 'string') { if (opts.method == null) { opts.method = method; } if (opts.uri == null) { opts.uri = uri; } } if (typeof uri === 'function') { if (opts.getData == null) { opts.getData = uri; } } assert.isObject(opts); opts = utils.clone(opts); if (typeof opts.uri === 'string') { spaceIndex = opts.uri.indexOf(' '); if (spaceIndex !== -1) { if (opts.method == null) { opts.method = opts.uri.slice(0, spaceIndex); } opts.uri = opts.uri.slice(spaceIndex + 1); } opts.uri = new Networking.Uri(opts.uri); } assert.instanceOf(opts.uri, Networking.Uri); if (opts.method == null) { opts.method = 'get'; } assert.isString(opts.method); opts.method = opts.method.toLowerCase(); assert.ok(utils.has(Networking.Request.METHODS, opts.method), "Networking doesn't provide a `" + opts.method + "` method"); if (opts.schema != null) { if (utils.isPlainObject(opts.schema)) { opts.schema = new Schema(opts.schema); } assert.instanceOf(opts.schema, Schema); } if (opts.redirect != null) { if (typeof opts.redirect === 'string') { opts.redirect = new Networking.Uri(opts.redirect); } else { assert.isFunction(opts.redirect); } } if (utils.isObject(opts.toHTML)) { opts.toHTML = createToHTMLFromObject(opts.toHTML); } for (key in opts) { val = opts[key]; this[key] = val; } this.__id__ = utils.uid(); this.app = app; this.name || (this.name = getRouteName(this)); app.networking.createHandler({ method: this.method, uri: this.uri, schema: this.schema, callback: utils.bindFunctionContext(handleRequest, this) }); } getRouteName = function(route) { var uri; assert.instanceOf(route, Route); uri = route.uri._uri; uri = uri.replace(Networking.Uri.NAMES_RE, ''); uri = uri.replace(/\*/g, ''); while (uri.indexOf('//') !== -1) { uri = uri.replace(/\/\//g, '/'); } uri = uri.replace(/^\//, ''); uri = uri.replace(/\/$/, ''); return uri; }; routesCache = Object.create(null); pendingRoutes = Object.create(null); factoryRoute = (function() { var createInstance; createInstance = function(route) { var r; r = Object.create(route); r.__hash__ = utils.uid(); if (typeof r.factory === "function") { r.factory(); } return r; }; return function(route) { var id, r; assert.instanceOf(route, Route); id = route.__id__; if (routesCache[id] == null) { routesCache[id] = []; } r = routesCache[id].pop() || createInstance(route); r = Object.create(r); r.request = r.response = null; r.route = r; r._dataPrepared = false; r._destroyComponentOnEnd = false; return r; }; })(); destroyRoute = function(route) { assert.instanceOf(route, Route); route.response.onSend.disconnect(onResponseSent, route); delete pendingRoutes[route.__hash__]; if (typeof route.destroy === "function") { route.destroy(); } if (route._dataPrepared) { switch (route.request.type) { case 'text': if (typeof route.destroyText === "function") { route.destroyText(); } break; case 'json': if (typeof route.destroyJSON === "function") { route.destroyJSON(); } break; case 'html': if (typeof route.destroyHTML === "function") { route.destroyHTML(); } } } if (route._destroyComponentOnEnd) { route.response.data.destroy(); } routesCache[route.__id__].push(Object.getPrototypeOf(route)); if (Route.lastClientRoute === route) { Route.lastClientRoute = null; Route.onLastClientRouteChange.emit(route); } }; resolveSyncGetDataFunc = function(route) { assert.instanceOf(route, Route); return route.data = route.getData(); }; resolveAsyncGetDataFuncCallback = function(route, err, data) { assert.instanceOf(route, Route); if (err != null) { if (route.response.status === 200) { route.response.status = 500; } if (route._dataPrepared && route.error === err) { return false; } route.error = err; } else { if (route._dataPrepared && route.data === data) { return false; } route.data = data; } return true; }; prepareRouteData = function(route) { var data, respData, response; assert.instanceOf(route, Route); response = route.response; if (route.error) { log.error("Error in route '" + route.uri + "':\n" + route.error); } respData = response.data; switch (route.request.type) { case 'text': data = route.toText(); break; case 'json': data = route.toJSON(); break; case 'html': data = route.toHTML(); if (respData instanceof Document && route._destroyComponentOnEnd) { respData.destroy(); response.data = null; } if (!(data instanceof Document) && response.data === respData) { data = renderComponentFromConfig.call(route, data); } } route._dataPrepared = true; if (data != null) { return response.data = data; } else if (response.data === respData) { return response.data = ''; } }; onResponseSent = function() { if (IS_NODE || this.request.type !== 'html') { destroyRoute(this); if (IS_NODE && utils.has(usedTemplates, this.response.data)) { this.response.data.destroy(); utils.remove(usedTemplates, this.response.data); } } }; finishRequest = function(route) { assert.instanceOf(route, Route); if (IS_CLIENT && route.response.data instanceof Document) { app.windowItem.node = route.response.data.node; } if (route.response.pending) { route.response.send(); } }; callNextIfNeeded = function(route, next) { if (!pendingRoutes[route.__hash__]) { if (route.response.pending) { next(); } return true; } return false; }; handleRequest = function(req, res, next) { var fakeAsync, getData, hash, lastClientRoute, redirect, route; assert.instanceOf(req, Networking.Request); assert.instanceOf(res, Networking.Response); assert.isFunction(next); route = factoryRoute(this); hash = route.__hash__; assert.notOk(pendingRoutes[hash]); if (IS_CLIENT) { if (Route.lastClientRoute) { destroyRoute(Route.lastClientRoute); } lastClientRoute = Route.lastClientRoute; Route.lastClientRoute = route; Route.onLastClientRouteChange.emit(lastClientRoute); } route.request = req; route.response = res; pendingRoutes[hash] = true; res.onSend(onResponseSent, route); if (typeof route.init === "function") { route.init(); } if (!pendingRoutes[hash]) { return next(); } redirect = route.redirect; if (typeof redirect === 'function') { redirect = route.redirect(); if (!pendingRoutes[hash]) { return next(); } } if (typeof redirect === 'string') { redirect = new Networking.Uri(redirect); } if (redirect instanceof Networking.Uri) { res.redirect(redirect.toString(req.params)); return; } getData = route.getData; fakeAsync = false; if (typeof getData === 'function') { if (getData.length === 1) { route.getData(function(err, data) { fakeAsync = true; if (callNextIfNeeded(route, next)) { return; } if (!resolveAsyncGetDataFuncCallback(route, err, data)) { return; } prepareRouteData(route); if (callNextIfNeeded(route, next)) { return; } return finishRequest(route); }); } else { resolveSyncGetDataFunc(route); if (callNextIfNeeded(route, next)) { return; } prepareRouteData(route); if (callNextIfNeeded(route, next)) { return; } finishRequest(route); } } else { prepareRouteData(route); if (callNextIfNeeded(route, next)) { return; } finishRequest(route); } if (!fakeAsync && callNextIfNeeded(route, next)) { } }; Route.prototype.next = function() { assert.ok(pendingRoutes[this.__hash__]); return destroyRoute(this); }; Route.prototype.toJSON = function() { var base, ref; if (this.response.status < 400) { return ((ref = this.data) != null ? typeof ref.toJSON === "function" ? ref.toJSON() : void 0 : void 0) || this.data; } else { return (typeof (base = this.error).toJSON === "function" ? base.toJSON() : void 0) || this.error; } }; Route.prototype.toText = function() { if (this.response.status < 400) { return this.data + ''; } else { return this.error + ''; } }; routeToString = function() { return this.method + " " + this.uri; }; getDefaultRouteComponentName = function() { var path; path = "components/" + this.name + ".xhtml"; if (app.components[path]) { return path; } }; renderComponentFromConfig = function(opts) { var component, componentName, logtime, r, tmpl, tmplComponent, tmplName, useName; componentName = (opts != null ? opts.component : void 0) || getDefaultRouteComponentName.call(this) || 'components/index.xhtml'; tmplName = (opts != null ? opts.template : void 0) || componentName; useName = (opts != null ? opts.use : void 0) || 'body'; logtime = log.time('Render'); if (componentName !== tmplName) { if (tmpl = app.components[tmplName]) { tmplComponent = Route.getTemplateComponent(tmplName); tmplComponent.use(useName, null); } else { log.warn("Template component '" + tmplName + "' can't be found for route '" + (routeToString.call(this)) + "'"); } } if (component = app.components[componentName]) { r = component.render(null, this); } else { log.warn("Component '" + tmplName + "' can't be found for route '" + (routeToString.call(this)) + "'"); } if (tmplComponent) { if (r != null) { r = tmplComponent.use(useName, r); } else { r = tmplComponent; } if (tmplComponent.context.routes.has(useName)) { tmplComponent.context.routes.pop(useName); } tmplComponent.context.routes.set(useName, this); this._destroyComponentOnEnd = false; } else { this._destroyComponentOnEnd = true; } log.end(logtime); return r; }; createToHTMLFromObject = function(opts) { return function() { return renderComponentFromConfig.call(this, opts); }; }; Route.prototype.toHTML = createToHTMLFromObject({ component: '', template: '', use: '' }); return Route; })(); }; }).call(this); }()); return module.exports; }, "src/assert/index.litcoffee": function(exports){ var module = {exports: exports}; var require = __createRequire({"src/utils":"src/utils/index.litcoffee"}); var exports = module.exports; (function(){ (function() { 'use strict'; var AssertionError, assert, createFailFunction, utils, extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }, hasProp = {}.hasOwnProperty; utils = require('src/utils'); assert = module.exports = function(expr, msg) { if (!expr) { return assert.fail(expr, true, msg, '==', assert); } }; assert.AssertionError = AssertionError = (function(superClass) { var valueToString; extend(AssertionError, superClass); valueToString = function(value) { var result; if (utils.isObject(value)) { result = (function() { try { return JSON.stringify(value); } catch (undefined) {} })(); } return result || String(value); }; AssertionError.generateMessage = function(error, msg) { return msg || ((valueToString(error.actual)) + " " + error.operator + " " + (valueToString(error.expected))); }; function AssertionError(opts) { this.name = 'AssertionError'; this.actual = opts.actual; this.expected = opts.expected; this.operator = opts.operator; this.scope = opts.scope; this.message = AssertionError.generateMessage(this, opts.message); if (typeof Error.captureStackTrace === "function") { Error.captureStackTrace(this, opts.stackStartFunction); } if (process.env.NEFT_PLATFORM === 'android') { console.error(this.stack || this.message); } } return AssertionError; })(Error); createFailFunction = function(assert) { var func; return func = function(actual, expected, msg, operator, stackStartFunction) { throw new assert.AssertionError({ actual: actual, expected: expected, message: msg, operator: operator, scope: assert._scope, stackStartFunction: stackStartFunction || func }); }; }; assert.scope = function(msg) { var func; msg = "" + this._scope + msg; func = function(expr, msg) { return assert(expr, msg); }; utils.merge(func, assert); func.fail = createFailFunction(func); func._scope = msg; return func; }; assert.fail = createFailFunction(assert); assert._scope = ''; assert.ok = assert; assert.notOk = function(expr, msg) { if (expr) { return this.fail(expr, true, msg, '!=', assert.notOk); } }; assert.is = function(actual, expected, msg) { if (!utils.is(actual, expected)) { return this.fail(actual, expected, msg, '===', assert.is); } }; assert.isNot = function(actual, expected, msg) { if (utils.is(actual, expected)) { return this.fail(actual, expected, msg, '!==', assert.isNot); } }; assert.isDefined = function(val, msg) { if (val == null) { return this.fail(val, null, msg, '!=', assert.isDefined); } }; assert.isNotDefined = function(val, msg) { if (val != null) { return this.fail(val, null, msg, '==', assert.isNotDefined); } }; assert.isPrimitive = function(val, msg) { if (!utils.isPrimitive(val)) { return this.fail(val, 'primitive', msg, 'is', assert.isPrimitive); } }; assert.isNotPrimitive = function(val, msg) { if (utils.isPrimitive(val)) { return this.fail(val, 'primitive', msg, 'isn\'t', assert.isNotPrimitive); } }; assert.isString = function(val, msg) { if (typeof val !== 'string') { return this.fail(val, 'string', msg, 'is', assert.isString); } }; assert.isNotString = function(val, msg) { if (typeof val === 'string') { return this.fail(val, 'string', msg, 'isn\'t', assert.isNotString); } }; assert.isFloat = function(val, msg) { if (!utils.isFloat(val)) { return this.fail(val, 'float', msg, 'is', assert.isFloat); } }; assert.isNotFloat = function(val, msg) { if (utils.isFloat(val)) { return this.fail(val, 'float', msg, 'isn\'t', assert.isNotFloat); } }; assert.isInteger = function(val, msg) { if (!utils.isInteger(val)) { return this.fail(val, 'integer', msg, 'is', assert.isInteger); } }; assert.isNotInteger = function(val, msg) { if (utils.isInteger(val)) { return this.fail(val, 'integer', msg, 'isn\'t', assert.isNotInteger); } }; assert.isBoolean = function(val, msg) { if (typeof val !== 'boolean') { return this.fail(val, 'boolean', msg, 'is', assert.isBoolean); } }; assert.isNotBoolean = function(val, msg) { if (typeof val === 'boolean') { return this.fail(val, 'boolean', msg, 'isn\'t', assert.isNotBoolean); } }; assert.isFunction = function(val, msg) { if (typeof val !== 'function') { return this.fail(val, 'function', msg, 'is', assert.isFunction); } }; assert.isNotFunction = function(val, msg) { if (typeof val === 'function') { return this.fail(val, 'function', msg, 'isn\'t', assert.isNotFunction); } }; assert.isObject = function(val, msg) { if (val === null || typeof val !== 'object') { return this.fail(val, 'object', msg, 'is', assert.isObject); } }; assert.isNotObject = function(val, msg) { if (val !== null && typeof val === 'object') { return this.fail(val, 'object', msg, 'isn\'t', assert.isNotObject); } }; assert.isPlainObject = function(val, msg) { if (!utils.isPlainObject(val)) { return this.fail(val, 'plain object', msg, 'is', assert.isPlainObject); } }; assert.isNotPlainObject = function(val, msg) { if (utils.isPlainObject(val)) { return this.fail(val, 'plain object', msg, 'isn\'t', assert.isNotPlainObject); } }; assert.isArray = function(val, msg) { if (!Array.isArray(val)) { return this.fail(val, 'array', msg, 'is', assert.isArray); } }; assert.isNotArray = function(val, msg) { if (Array.isArray(val)) { return this.fail(val, 'array', msg, 'isn\'t', assert.isNotArray); } }; assert.isEqual = function(val1, val2, msg, opts) { if (typeof msg === 'object') { opts = msg; msg = void 0; } if (!utils.isEqual(val1, val2, opts != null ? opts.maxDeep : void 0)) { return this.fail(val1, val2, msg, 'equal', assert.isEqual); } }; assert.isNotEqual = function(val1, val2, msg, opts) { if (typeof msg === 'object') { opts = msg; msg = void 0; } if (utils.isEqual(val1, val2, opts != null ? opts.maxDeep : void 0)) { return this.fail(val1, val2, msg, 'isn\'t equal', assert.isNotEqual); } }; assert.instanceOf = function(val, ctor, msg) { var ctorName; if (!(val instanceof ctor)) { ctorName = ctor.__path__ || ctor.__name__ || ctor.name || ctor; return this.fail(val, ctorName, msg, 'instanceof', assert.instanceOf); } }; assert.notInstanceOf = function(val, ctor, msg) { var ctorName; if (val instanceof ctor) { ctorName = ctor.__path__ || ctor.__name__ || ctor.name || ctor; return this.fail(val, ctorName, msg, 'instanceof', assert.notInstanceOf); } }; assert.lengthOf = function(val, length, msg) { if ((val != null ? val.length : void 0) !== length) { return this.fail(val, length, msg, '.length ===', assert.lengthOf); } }; assert.notLengthOf = function(val, length, msg) { if ((val != null ? val.length : void 0) === length) { return this.fail(val, length, msg, '.length !==', assert.notLengthOf); } }; assert.operator = function(val1, operator, val2, msg) { var pass; pass = (function() { switch (operator) { case '>': return val1 > val2; case '>=': return val1 >= val2; case '<': return val1 < val2; case '<=': return val1 <= val2; default: throw new TypeError("Unexpected operator `" + operator + "`"); } })(); if (!pass) { return this.fail(val1, val2, msg, operator, assert.operator); } }; assert.match = function(val, regexp, msg) { if (!regexp.test(val)) { return this.fail(val, regexp, msg, 'match', assert.match); } }; assert.notMatch = function(val, regexp, msg) { if (regexp.test(val)) { return this.fail(val, regexp, msg, 'not match', assert.match); } }; }).call(this); }()); return module.exports; }, "src/binding/index.coffee": function(exports){ var module = {exports: exports}; var require = __createRequire({"src/utils":"src/utils/index.litcoffee","src/log":"src/log/index.litcoffee","src/assert":"src/assert/index.litcoffee","src/dict":"src/dict/index.litcoffee","src/list":"src/list/index.litcoffee"}); var exports = module.exports; (function(){ (function() { 'use strict'; var Binding, Connection, Dict, List, MAX_LOOPS, assert, getPropHandlerName, isArray, log, utils; utils = require('src/utils'); log = require('src/log'); assert = require('src/assert'); Dict = require('src/dict'); List = require('src/list'); log = log.scope('Binding'); isArray = Array.isArray; MAX_LOOPS = 50; getPropHandlerName = (function() { var cache; cache = Object.create(null); return function(prop) { return cache[prop] || (cache[prop] = "on" + (utils.capitalize(prop)) + "Change"); }; })(); Connection = (function() { var pool; pool = []; Connection.factory = function(binding, item, prop, parent) { var elem; if (parent == null) { parent = null; } if (pool.length > 0 && (elem = pool.pop())) { Connection.call(elem, binding, item, prop, parent); return elem; } else { return new Connection(binding, item, prop, parent); } }; function Connection(binding1, item, prop1, parent1) { this.binding = binding1; this.prop = prop1; this.parent = parent1; this.handlerName = getPropHandlerName(this.prop); this.isConnected = false; if (isArray(item)) { this.itemId = ''; this.child = Connection.factory(this.binding, item[0], item[1], this); this.item = this.child.getValue(); } else { this.itemId = item; this.child = null; this.item = this.binding.getItemById(item); } this.connect(); Object.seal(this); } Connection.prototype.getSignalChangeListener = (function() { var noParent, noParentOnDict, withParent, withParentOnDict; withParent = function(prop, val) { if (val === void 0 || typeof prop !== 'string' || this.parent.prop === prop) { this.parent.updateItem(); } }; noParent = function() { this.binding.update(); }; withParentOnDict = function(prop) { if (this.prop === prop) { this.parent.updateItem(); } }; noParentOnDict = function(prop) { if (this.prop === prop) { this.binding.update(); } }; return function() { if (this.item instanceof Dict) { if (this.parent) { return withParentOnDict; } else { return noParentOnDict; } } else { if (this.parent) { return withParent; } else { return noParent; } } }; })(); Connection.prototype.update = function() { return this.getSignalChangeListener().call(this); }; Connection.prototype.connect = function() { var handler, item; item = this.item; if (item) { if (item instanceof Dict) { this.isConnected = true; item.onChange(this.getSignalChangeListener(), this); } else if (item instanceof List) { this.isConnected = true; handler = this.getSignalChangeListener(); item.onChange(handler, this); item.onInsert(handler, this); item.onPop(handler, this); } else if (handler = item[this.handlerName]) { this.isConnected = true; handler(this.getSignalChangeListener(), this); } } }; Connection.prototype.disconnect = function() { var handler, item; item = this.item; if (item && this.isConnected) { handler = this.getSignalChangeListener(); if (item instanceof Dict) { item.onChange.disconnect(handler, this); } else if (item instanceof List) { item.onChange.disconnect(handler, this); item.onInsert.disconnect(handler, this); item.onPop.disconnect(handler, this); } else { item[this.handlerName].disconnect(handler, this); } } this.isConnected = false; }; Connection.prototype.updateItem = function() { var oldVal, val; oldVal = this.item; if (this.child) { val = this.child.getValue(); } else { val = this.binding.getItemById(this.itemId); } if (oldVal && !this.isConnected) { this.connect(); oldVal = null; } if (oldVal !== val) { this.disconnect(); this.item = val; this.connect(); if (!this.parent) { this.binding.update(); } } if (this.parent) { this.parent.updateItem(); } }; Connection.prototype.getValue = function() { if (this.item) { return this.item[this.prop]; } else { return null; } }; Connection.prototype.destroy = function() { var ref; this.disconnect(); if ((ref = this.child) != null) { ref.destroy(); } pool.push(this); }; return Connection; })(); module.exports = Binding = (function() { Binding.New = function(binding, ctx, target) { var connections, elem, i, len, ref; if (target == null) { target = new Binding(binding, ctx); } Object.seal(target); connections = target.connections; ref = binding[1]; for (i = 0, len = ref.length; i < len; i++) { elem = ref[i]; if (isArray(elem)) { connections.push(Connection.factory(target, elem[0], elem[1])); } } return target; }; function Binding(binding, ctx1) { this.ctx = ctx1; assert.lengthOf(binding, 2); assert.isFunction(binding[0]); assert.isArray(binding[1]); this.func = binding[0]; this.args = null; this.connections || (this.connections = []); //<development>; this.updatePending = false; this.updateLoop = 0; //</development>; } Binding.prototype.getItemById = function(item) { throw new Error("Not implemented"); }; Binding.prototype.getValue = function() { throw new Error("Not implemented"); }; Binding.prototype.getDefaultValue = function() { switch (typeof this.getValue()) { case 'string': return ''; case 'number': return 0; case 'boolean': return false; default: return null; } }; Binding.prototype.setValue = function(val) { throw new Error("Not implemented"); }; Binding.prototype.onError = function(err) {}; Binding.prototype.update = function() { var result; null; //<development>; if (this.updatePending) { if (this.updateLoop > MAX_LOOPS) { return; } if (++this.updateLoop === MAX_LOOPS) { log.error(this.getLoopDetectedErrorMessage()); return; } } else { this.updateLoop = 0; } //</development>; result = utils.tryFunction(this.func, this.ctx, this.args); if (result instanceof Error) { this.onError(result); result = this.getDefaultValue(); } //<development>; this.updatePending = true; //</development>; this.setValue(result); //<development>; this.updatePending = false; //</development>; }; Binding.prototype.getLoopDetectedErrorMessage = function() { return "Potential loop detected"; }; Binding.prototype.destroy = function() { var connection; while (connection = this.connections.pop()) { connection.destroy(); } this.args = null; }; return Binding; })(); }).call(this); }()); return module.exports; }, "src/db/implementation.coffee": function(exports){ var module = {exports: exports}; var require = __createRequire({"src/utils":"src/utils/index.litcoffee","./implementations/native":"src/db/implementations/native.coffee","./implementations/memory":"src/db/implementations/memory.coffee"}); var exports = module.exports; (function(){ (function() { 'use strict'; var utils; utils = require('src/utils'); module.exports = (function() { var impl; impl = (function() { try { return require('./implementations/browser'); } catch (undefined) {} })(); impl || (impl = (function() { try { return require('./implementations/native'); } catch (undefined) {} })()); impl || (impl = require('./implementations/memory')); return impl; })(); }).call(this); }()); return module.exports; }, "src/db/implementations/memory.coffee": function(exports){ var module = {exports: exports}; var require = __createRequire({"src/utils":"src/utils/index.litcoffee"}); var exports = module.exports; (function(){ (function() { 'use strict'; var data, utils; utils = require('src/utils'); data = Object.create(null); exports.get = function(key, callback) { var val; val = data[key]; val = utils.cloneDeep(val); return callback(null, val); }; exports.set = function(key, val, callback) { data[key] = val; return callback(null); }; exports.remove = function(key, callback) { data[key] = void 0; return callback(null); }; }).call(this); }()); return module.exports; }, "src/db/implementations/native.coffee": function(exports){ var module = {exports: exports}; var require = __createRequire({"src/utils":"src/utils/index.litcoffee","src/native":"src/native/index.litcoffee"}); var exports = module.exports; (function(){ (function() { // when=NEFT_NATIVE; 'use strict'; var callNativeFunction, callbacks, encodeKey, lastId, onNativeEvent, ref, registerCallback, utils; utils = require('src/utils'); ref = require('src/native'), callNativeFunction = ref.callNativeFunction, onNativeEvent = ref.onNativeEvent; lastId = 0; callbacks = Object.create(null); onNativeEvent('__neftDbResponse', function(id, error, result) { if (typeof callbacks[id] === "function") { callbacks[id](error, result); } return delete callbacks[id]; }); registerCallback = function(callback) { callbacks[lastId] = callback; lastId += 1; return lastId %= 99999; }; encodeKey = function(key) { return encodeURIComponent(String(key)); }; exports.get = function(key, callback) { callNativeFunction('__neftDbGet', encodeKey(key), lastId); return registerCallback(function(error, result) { if (error) { error = new Error(error); } result = utils.tryFunction(JSON.parse, null, [result], result); return callback(error, result); }); }; exports.set = function(key, val, callback) { var error, error1; if (utils.isObject(val)) { try { val = JSON.stringify(val); } catch (error1) { error = error1; return callback(error); } } callNativeFunction('__neftDbSet', encodeKey(key), String(val), lastId); return registerCallback(callback); }; exports.remove = function(key, callback) { callNativeFunction('__neftDbRemove', encodeKey(key), lastId); return registerCallback(callback); }; }).call(this); }()); return module.exports; }, "src/db/index.litcoffee": function(exports){ var module = {exports: exports}; var require = __createRequire({"src/utils":"src/utils/index.litcoffee","src/assert":"src/assert/index.litcoffee","src/list":"src/list/index.litcoffee","src/dict":"src/dict/index.litcoffee","./implementation":"src/db/implementation.coffee"}); var exports = module.exports; (function(){ (function() { 'use strict'; var BITMASK, DbDict, DbList, Dict, List, NOP, assert, createPassProperty, impl, utils, watchers, watchersCount, extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }, hasProp = {}.hasOwnProperty; utils = require('src/utils'); assert = require('src/assert'); List = require('src/list'); Dict = require('src/dict'); assert = assert.scope('Database'); NOP = function() {}; impl = require('./implementation'); watchersCount = Object.create(null); watchers = Object.c