parse-url.js 5.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // parse-url.js
  2. // This file defines a helper function for parsing an incoming URL into
  3. // the Public Path, Application, Instance and Private Path components.
  4. // incoming path public_path application instance private_path
  5. // /path/to/component "/path/to/component" "index.vwf" nil nil
  6. // /path/to/component/ "/path/to/component" "index.vwf" nil nil
  7. // /path/to/component/path/to/client/file "/path/to/component" "index.vwf" nil "path/to/client/file"
  8. // /path/to/component/path/to/component/file "/path/to/component" "index.vwf" nil "path/to/component/file"
  9. // /path/to/component/socket/path "/path/to/component" "index.vwf" nil "socket/path"
  10. // /path/to/component.vwf "/path/to" "component.vwf" nil nil
  11. // /path/to/component.vwf/ "/path/to" "component.vwf" nil nil
  12. // /path/to/component.vwf/path/to/client/file "/path/to" "component.vwf" nil "path/to/client/file"
  13. // /path/to/component.vwf/path/to/component/file "/path/to" "component.vwf" nil "path/to/component/file"
  14. // /path/to/component.vwf/socket/path "/path/to" "component.vwf" nil "socket/path"
  15. // /path/to/component/instance "/path/to/component" "index.vwf" "instance" nil
  16. // /path/to/component/instance/ "/path/to/component" "index.vwf" "instance" nil
  17. // /path/to/component/instance/path/to/client/file "/path/to/component" "index.vwf" "instance" "path/to/client/file"
  18. // /path/to/component/instance/path/to/component/file "/path/to/component" "index.vwf" "instance" "path/to/component/file"
  19. // /path/to/component/instance/socket/path "/path/to/component" "index.vwf" "instance" "socket/path"
  20. // /path/to/component.vwf/instance "/path/to" "component.vwf" "instance" nil
  21. // /path/to/component.vwf/instance/ "/path/to" "component.vwf" "instance" nil
  22. // /path/to/component.vwf/instance/path/to/client/file "/path/to" "component.vwf" "instance" "path/to/client/file"
  23. // /path/to/component.vwf/instance/path/to/component/file "/path/to" "component.vwf" "instance" "path/to/component/file"
  24. // /path/to/component.vwf/instance/socket/path "/path/to" "component.vwf" "instance" "socket/path"
  25. var helpers = require( './helpers' );
  26. // List of valid extensions for VWF components.
  27. var template_extensions = [ "", ".yaml", ".json" ];
  28. function GetExtension( path ) {
  29. if ( path.match( /\.vwf$/ ) ) {
  30. for ( var index = 0; index < template_extensions.length; index++ ) {
  31. if ( helpers.IsFile( helpers.JoinPath( global.applicationRoot, path + template_extensions[ index ] ) ) ) {
  32. return template_extensions[ index ];
  33. }
  34. }
  35. }
  36. // else if ( path.match( /\.(dae|unity3d)$/ ) ) {
  37. // if ( helpers.IsFile( helpers.JoinPath( global.applicationRoot, path ) ) ) {
  38. // return "";
  39. // }
  40. // }
  41. return undefined;
  42. }
  43. function Process( updatedURL ) {
  44. var result = { 'public_path': "/", 'application': undefined, 'instance': undefined, 'private_path': undefined };
  45. var segments = helpers.GenerateSegments( updatedURL );
  46. var extension = undefined;
  47. while ( ( segments.length > 0 ) && ( helpers.IsDirectory( helpers.JoinPath( global.applicationRoot + result[ 'public_path' ], segments[ 0 ] ) ) ) ) {
  48. result[ 'public_path' ] = helpers.JoinPath( result[ 'public_path' ], segments.shift( ) );
  49. }
  50. if ( ( segments.length > 0 ) && ( extension = GetExtension( helpers.JoinPath( result[ 'public_path' ], segments[ 0 ] ) ) ) ) {
  51. result[ 'application' ] = segments.shift( );
  52. } else if ( extension = GetExtension( helpers.JoinPath( result[ 'public_path' ], "index.vwf" ) ) ) {
  53. result[ 'application' ] = "index.vwf";
  54. } else if ( extension = GetExtension( helpers.JoinPath( result[ 'public_path' ], "index.dae" ) ) ) {
  55. result[ 'application' ] = "index.dae";
  56. } else if ( extension = GetExtension( helpers.JoinPath( result[ 'public_path' ], "index.unity3d" ) ) ) {
  57. result[ 'application' ] = "index.unity3d";
  58. }
  59. if ( extension ) {
  60. if ( ( segments.length > 0 ) && ( helpers.IsInstanceID( segments[ 0 ] ) ) ) {
  61. result[ 'instance' ] = segments.shift();
  62. }
  63. if ( segments.length > 0 ) {
  64. result[ 'private_path' ] = segments.join("/");
  65. }
  66. }
  67. return result;
  68. }
  69. exports.Process = Process;