Ikagaka/named-kernel-manager.js

View on GitHub
lib/named-kernel-manager-ghost-module.js.map

Summary

Maintainability
Test Coverage
{"version":3,"sources":["lib/named-kernel-manager-ghost-module.js"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;AACA;;AACA;;;;IAEa,wCAAA;;;;;;;;;;;;;;;;;;;6FAkCK,SAAS,SAAS,QAAQ,aAAa;YAK/C;;;;;qBAJF,KAAK,cAAL,CAAoB,OAApB;;;;;sBACI,IAAI,KAAJ,aAAoB,OAApB;;;;uBAGa,KAAK,wBAAL,CAA8B,OAA9B,EAAuC,OAAvC,EAAgD,MAAhD,EAAwD,WAAxD,EAAqE,cAArE;;;AAAf;;AACN,qBAAK,cAAL,CAAoB,OAApB,EAA6B,MAA7B;iDACO;;;;;;;;;;;;;;;;;;;;;;;;;;+FAQgB,SAAS;;;;;+BACzB;;;;;;;;uBAAiB,KAAK,UAAL,CAAgB,aAAhB,CAA8B,aAA9B,CAA4C,OAA5C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;+FAWK,SAAS,SAAS,QAAQ,aAAa;YAC9D;;;;;;;uBAAiB,KAAK,kBAAL,CAAwB,OAAxB,EAAiC,OAAjC;;;AAAjB;;uBACO,KAAK,iBAAL,CAAuB,OAAvB,EAAgC,SAAS,SAAzC,EAAoD,SAAS,WAA7D,EAA0E,MAA1E,EAAkF,WAAlF,EAA+F,cAA/F;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;+FAaS,SAAS,WAAW,aAAa,QAAQ,aAAa;;;;;;;kDACrE,kBAAQ,GAAR,CAAY,CACjB,KAAK,UAAL,CAAgB,OAAhB,CADiB,EAEjB,KAAK,UAAL,CAAgB,OAAhB,EAAyB,SAAzB,EAAoC,cAApC,CAFiB,EAGjB,KAAK,YAAL,CAAkB,WAAlB,EAA+B,cAA/B,CAHiB,CAAZ,EAIJ,IAJI,CAKL,iBAA8B;AAAA;;AAAA,sBAA5B,MAA4B;AA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,SAAb,EAAwB,QAAQ,SAAR,GAAoB,QAApB;AACxB,oBAAI,CAAC,QAAQ,WAAb,EAA0B;AACxB,sBAAI,cAAJ,EAAoB;;AAElB,4BAAQ,WAAR,GAAsB,eAAe,SAArC;AACD,mBAHD,MAGO;AACL,4BAAQ,WAAR,GAAsB,QAAtB;AACD;AACF;;uBACK,cAAc,aAAd,CAA4B,aAAa,SAAzC,EAAoD,OAApD;;;AAER,qBAAK,IAAL,CAAU,iBAAV,EAA6B,MAA7B,EAAqC,GAArC;;;;;;;;AAEA,qBAAK,IAAL,CAAU,iBAAV;;;;;;;;;;;;;;;;;;;;;;;;;wBA5PiB;AAAE,aAAO,KAAK,eAAL,IAAwB,uCAAmB,eAAlD;AAAoE;;;;;;;sBAMxE,OAAO;AAAE,WAAK,eAAL,GAAuB,KAAvB;AAA+B;;;;;;;;wBAlB/B;AAAE,aAAO,uCAAmB,eAA1B;AAA4C;;;;;;;sBAMhD,OAAO;AAAE,6CAAmB,eAAnB,GAAqC,KAArC;AAA6C","file":"named-kernel-manager-ghost-module.js","sourcesContent":["import {Shiorif} from 'shiorif';\r\nimport {GhostKernel} from 'ghost-kernel';\r\nimport {NamedKernelManager} from './named-kernel-manager';\r\n\r\nexport class NamedKernelManagerGhostModule {\r\n  /**\r\n   * デフォルトのゴーストのビュークラス\r\n   * @type {cuttlebone}\r\n   */\r\n  static get GhostViewClass() { return NamedKernelManager._GhostViewClass; }\r\n\r\n  /**\r\n   * デフォルトのゴーストのビュークラス\r\n   * @type {cuttlebone}\r\n   */\r\n  static set GhostViewClass(value) { NamedKernelManager._GhostViewClass = value; }\r\n\r\n  /**\r\n   * ゴーストのビュークラス\r\n   * @type {cuttlebone}\r\n   */\r\n  get GhostViewClass() { return this._GhostViewClass || NamedKernelManager._GhostViewClass; }\r\n\r\n  /**\r\n   * ゴーストのビュークラス\r\n   * @type {cuttlebone}\r\n   */\r\n  set GhostViewClass(value) { this._GhostViewClass = value; }\r\n\r\n  /**\r\n   * load ghost\r\n   * @param {string} namedId - named id\r\n   * @param {GhostProfile} [profile] - profile\r\n   * @param {RoutableComponentRoutes} [routes] - ルーティング\r\n   * @param {Hash<GhostKernelController>} [controllers] - コントローラ\r\n   * @param {cuttlebone} [GhostViewClass] ghost view class\r\n   * @return {Promise<GhostKernel>} ghost kernel instance\r\n   */\r\n  async loadGhost(namedId, profile, routes, controllers, GhostViewClass) {\r\n    if (this.isKernelExists(namedId)) {\r\n      throw new Error(`ghost [${namedId}] already exists`);\r\n    }\r\n\r\n    const kernel = await this._getGhostKernelAsProfile(namedId, profile, routes, controllers, GhostViewClass);\r\n    this.registerKernel(namedId, kernel);\r\n    return kernel;\r\n  }\r\n\r\n  /**\r\n   * load ghost\r\n   * @param {string} namedId - named id\r\n   * @param {GhostProfile} [profile] - profile\r\n   */\r\n  async _get_ghost_profile(namedId, profile) {\r\n    return profile || await this.components.NanikaStorage.ghost_profile(namedId);\r\n  }\r\n\r\n  /**\r\n   * load ghost\r\n   * @param {string} namedId - named id\r\n   * @param {GhostProfile} [profile] - profile\r\n   * @param {RoutableComponentRoutes} [routes] - ルーティング\r\n   * @param {Hash<GhostKernelController>} [controllers] - コントローラ\r\n   * @param {cuttlebone} [GhostViewClass] ghost view class\r\n   */\r\n  async _getGhostKernelAsProfile(namedId, profile, routes, controllers, GhostViewClass) {\r\n    const _profile = await this._get_ghost_profile(namedId, profile);\r\n    return await this._get_ghost_kernel(namedId, _profile.shellname, _profile.balloonname, routes, controllers, GhostViewClass);\r\n  }\r\n\r\n  /**\r\n   * build ghost kernel\r\n   * @param {string} namedId - named id\r\n   * @param {string} shellname - shellname\r\n   * @param {string} balloonname - balloonname\r\n   * @param {RoutableComponentRoutes} [routes] - ルーティング\r\n   * @param {Hash<GhostKernelController>} [controllers] - コントローラ\r\n   * @param {cuttlebone} [GhostViewClass] ghost view class\r\n   * @return {Promise<GhostKernel>} ghost kernel instance\r\n   */\r\n  async _get_ghost_kernel(namedId, shellname, balloonname, routes, controllers, GhostViewClass) {\r\n    return Promise.all([\r\n      this._get_ghost(namedId),\r\n      this._get_shell(namedId, shellname, GhostViewClass),\r\n      this._get_balloon(balloonname, GhostViewClass),\r\n    ]).then(\r\n      ([shiori, shell, balloon]) => {\r\n        const shiorif = new Shiorif(shiori);\r\n        const named = this.components.NamedManager.materialize2(shell, balloon);\r\n        return new GhostKernel({\r\n          Shiorif: shiorif,\r\n          Named: named,\r\n          NanikaStorage: this.components.NanikaStorage,\r\n          NamedKernelManager: this,\r\n          TimerEventSource: this.components.TimerEventSource,\r\n        }, routes, controllers);\r\n      }\r\n    )\r\n  }\r\n\r\n  /**\r\n   * ensure path separator at dirpath' end\r\n   * @param {string} dirpath - dirpath\r\n   * @return {string} dirpath that ends with path separator\r\n   */\r\n  _canondirpath(dirpath) {\r\n    const path_separator = dirpath.match(/[\\\\\\/]/)[0];\r\n    return dirpath.replace(\r\n      new RegExp('\\\\' + path_separator + '?$'),\r\n      path_separator\r\n    );\r\n  }\r\n\r\n  /**\r\n   * get ghost(shiori) instance\r\n   * @param {string} namedId - named id\r\n   * @return {Promise<Shiori>} ghost(shiori) instance\r\n   */\r\n  async _get_ghost(namedId) {\r\n    this.emit('ghost_load', namedId);\r\n    const dirpath = this._get_ghost_directory_path(namedId);\r\n    const ghost = await this._load_ghost(this.components.NanikaStorage.backend.fs, dirpath);\r\n    this.emit('ghost_loaded', namedId);\r\n    return ghost;\r\n  }\r\n\r\n  /**\r\n   * get ghost directory path\r\n   * @param {string} namedId - named id\r\n   * @return {string} ghost directory path\r\n   */\r\n  _get_ghost_directory_path(namedId) {\r\n    return this._canondirpath(this.components.NanikaStorage.ghost_master_path(namedId));\r\n  }\r\n\r\n  /**\r\n   * load ghost(shiori)\r\n   * @param {NanikaDirectory} ghost directory contents\r\n   * @return {Promise<Shiori>} ghost(shiori) instance\r\n   */\r\n  async _load_ghost(fs, dirpath) {\r\n    const shiori = await ShioriLoader.detect_shiori(fs, dirpath);\r\n    const result = await shiori.load(dirpath); // TODO load error\r\n    return shiori;\r\n  }\r\n\r\n  /**\r\n   * get shell instance\r\n   * @param {string} namedId - named id\r\n   * @param {string} shellname - shellname\r\n   * @param {cuttlebone} [GhostViewClass] ghost view class\r\n   * @return {Promise<Shell>} shell instance\r\n   */\r\n  async _get_shell(namedId, shellname, GhostViewClass) {\r\n    this.emit('load_shell_files', namedId, shellname);\r\n    const directory = await this._get_shell_directory(namedId, shellname);\r\n    this.emit('shell_load', namedId, shellname, directory);\r\n    const shell = await this._load_shell(directory, GhostViewClass);\r\n    this.emit('shell_loaded', namedId, shellname, shell);\r\n    return shell;\r\n  }\r\n\r\n  /**\r\n   * get shell directory contents\r\n   * @param {string} namedId - named id\r\n   * @param {string} shellname - shellname\r\n   * @return {Promise<NanikaDirectory>} shell directory contents\r\n   */\r\n  async _get_shell_directory(namedId, shellname) {\r\n    return await this.components.NanikaStorage.shell(namedId, shellname);\r\n  }\r\n\r\n  /**\r\n   * load shell\r\n   * @param {NanikaDirectory} shell directory contents\r\n   * @param {cuttlebone} [GhostViewClass] ghost view class\r\n   * @return {Promise<Shell>} shell instance\r\n   */\r\n  _load_shell(directory, GhostViewClass = this.GhostViewClass) {\r\n    const shell = new GhostViewClass.Shell(directory.asArrayBuffer());\r\n    return shell.load();\r\n  }\r\n\r\n  /**\r\n   * get balloon instance\r\n   * @param {string} balloonname - balloonname\r\n   * @param {cuttlebone} [GhostViewClass] ghost view class\r\n   * @return {Promise<Balloon>} balloon instance\r\n   */\r\n  async _get_balloon(balloonname, GhostViewClass) {\r\n    this.emit('load_balloon_files', balloonname);\r\n    const directory = await this._get_balloon_directory(balloonname);\r\n    this.emit('balloon_load', balloonname, directory);\r\n    const balloon = await this._load_balloon(directory, GhostViewClass);\r\n    this.emit('balloon_loaded', balloonname, balloon);\r\n    return balloon;\r\n  }\r\n\r\n  /**\r\n   * get balloon directory contents\r\n   * @param {string} balloonname - balloonname\r\n   * @return {Promise<NanikaDirectory>} balloon directory contents\r\n   */\r\n  async _get_balloon_directory(balloonname) {\r\n    return await this.components.NanikaStorage.balloon(balloonname);\r\n  }\r\n\r\n  /**\r\n   * load balloon\r\n   * @param {NanikaDirectory} balloon directory contents\r\n   * @param {cuttlebone} [GhostViewClass] ghost view class\r\n   * @return {Promise<Balloon>} balloon instance\r\n   */\r\n  _load_balloon(directory, GhostViewClass = this.GhostViewClass) {\r\n    const balloon = new GhostViewClass.Balloon(directory.asArrayBuffer());\r\n    return balloon.load();\r\n  }\r\n\r\n  /**\r\n   * install named\r\n   * @param {Blob | URL | string} target install target resource (file, url or some)\r\n   * @param {NamedKernel | string} [from] who handled the target? kernel or named id\r\n   * @return {Promise}\r\n   */\r\n  async installNar(target, from = null) {\r\n    const nanikaStorage = this.components.NanikaStorage;\r\n    this.emit('install_begin', target);\r\n    let nar;\r\n    try {\r\n      if (target instanceof Blob) {\r\n        nar = await NarLoader.loadFromBlob(target);\r\n      } else if (target instanceof URL || target instanceof String || typeof target === 'string') {\r\n        nar = await NarLoader.loadFromURL(target);\r\n      } else {\r\n        throw new Error(`target is not Blob or URL : ${target.name || target}`); // TODO: typed error\r\n      }\r\n      this.emit('install_nar_loaded', target, nar);\r\n      let dirpath, sakuraname;\r\n      if (from) { // from が指定された場合\r\n        dirpath = typeof from === 'string' || from instanceof String ? from : this.namedId(from);\r\n        // TODO: fromの他形式対応\r\n        sakuraname = nanikaStorage.ghost_descript(dirpath)['sakura.name'];\r\n      }\r\n      const install_results = await nanikaStorage.install_nar(nar, dirpath, sakuraname);\r\n      if (!install_results) {\r\n        this.emit('install_not_accepted', target, nar);\r\n        return;\r\n      }\r\n      let ghost_result, balloon_result;\r\n      install_results.forEach((install_result) => {\r\n        if (install_result.type === 'ghost') {\r\n          ghost_result = install_result;\r\n        } else if (install_result.type === 'balloon') {\r\n          balloon_result = install_result;\r\n        }\r\n      });\r\n      if (ghost_result) {\r\n        // 初期profileを設定\r\n        // TODO: vanish回数など\r\n        const profile = await nanikaStorage.ghost_profile(ghost_result.directory);\r\n        if (!profile.shellname) profile.shellname = 'master';\r\n        if (!profile.balloonname) {\r\n          if (balloon_result) {\r\n            // 同梱バルーンを初期設定\r\n            profile.balloonname = balloon_result.directory;\r\n          } else {\r\n            profile.balloonname = 'origin'; // TODO: 設定を読む\r\n          }\r\n        }\r\n        await nanikaStorage.ghost_profile(ghost_result.directory, profile);\r\n      }\r\n      this.emit('install_succeed', target, nar);\r\n    } catch (error) {\r\n      this.emit('install_failure', error);\r\n      throw error;\r\n    }\r\n  }\r\n}\r\n"]}