<commit_before>var Protocol = require('protocol');
var Url = require('fire-url');
var Path = require('fire-path');
var Fs = require('fire-fs');
var Winston = require('winston');

function protocolRegisterCallback ( err, scheme ) {
    var logFailed = Editor.failed ? Editor.failed : Winston.failed;
    var logSuccess = Editor.success ? Editor.success : Winston.success;

    if ( err ) {
        logFailed( 'Failed to register protocol %s, %s', scheme, err.message );
        return;
    }
    logSuccess( 'protocol %s registerred', scheme );
}
Editor.protocolRegisterCallback = protocolRegisterCallback;

/**
 * @module Editor
 */

// native protocol register

// register protocol editor-framework://
Protocol.registerFileProtocol('editor-framework', function(request, callback) {
    var url = decodeURIComponent(request.url);
    var uri = Url.parse(url);
    var relativePath = uri.hostname;
    if ( uri.pathname ) {
        relativePath = Path.join( relativePath, uri.pathname );
    }
    var file = Path.join( Editor.frameworkPath, relativePath );
    callback ( { path: file } );
}, protocolRegisterCallback );

// register protocol app://
Protocol.registerFileProtocol('app', function(request, callback) {
    var url = decodeURIComponent(request.url);
    var uri = Url.parse(url);
    var relativePath = uri.hostname;
    if ( uri.pathname ) {
        relativePath = Path.join( relativePath, uri.pathname );
    }
    var file = Path.join( Editor.appPath, relativePath );
    callback ( { path: file } );
}, protocolRegisterCallback );

// register protocol packages://

Protocol.registerFileProtocol('packages', function(request, callback) {
    var url = decodeURIComponent(request.url);
    var uri = Url.parse(url);

    var packagePath = Editor.Package.packagePath(uri.hostname);
    if ( packagePath ) {
        var packageInfo = Editor.Package.packageInfo(packagePath);
        if ( packageInfo ) {
            var file = Path.join( packageInfo._destPath, uri.pathname );
            callback ( { path: file } );
            return;
        }
    }

    // net::ERR_FILE_NOT_FOUND
    callback (-6);
}, protocolRegisterCallback );

// DISABLE: this make protocol can not use relative path
// // register protocol bower://
// Protocol.registerFileProtocol('bower', function(request, callback) {
//     var url = decodeURIComponent(request.url);
//     var uri = Url.parse(url);
//     var relativePath = uri.hostname;
//     if ( uri.pathname ) {
//         relativePath = Path.join( relativePath, uri.pathname );
//     }
//     var file = Path.join( Editor.appPath, 'bower_components', relativePath );
//     callback ( { path: file } );
// }, protocolRegisterCallback );

// DISABLE: same reason as bower
// // register protocol widgets://
// Protocol.registerFileProtocol('widgets', function(request, callback) {
//     var url = decodeURIComponent(request.url);
//     var uri = Url.parse(url);

//     var info = Editor.Package.widgetInfo(uri.hostname);
//     if ( info ) {
//         var file = Path.join( info.path, uri.pathname );
//         callback ( { path: file } );
//         return;
//     }
//     // net::ERR_FILE_NOT_FOUND
//     callback (-6);
// }, protocolRegisterCallback );

// Editor.url protocol register

Editor._protocol2fn = {};

function _url2path ( base ) {
    return function ( urlInfo ) {
        if ( urlInfo.pathname ) {
            return Path.join( base, urlInfo.host, urlInfo.pathname );
        }
        return Path.join( base, urlInfo.host );
    };
}

function _packages2path ( urlInfo ) {
    var packagePath = Editor.Package.packagePath(urlInfo.hostname);
    if ( packagePath ) {
        return Path.join( packagePath, urlInfo.pathname );
    }
    return '';
}

/**
 * Convert a url by its protocol to a filesystem path. This function is useful when you try to
 * get some internal file. You can use {@link Editor.registerProtocol} to register and map your filesystem
 * path to url. By default, Editor Framework register `editor-framework://` and `app://` protocol.
 * @method url<commit_msg>fix protocol register success information<commit_after>var Protocol = require('protocol');
var Url = require('fire-url');
var Path = require('fire-path');
var Fs = require('fire-fs');
var Winston = require('winston');

/**
 * @module Editor
 */

// native protocol register

// register protocol editor-framework://
Protocol.registerFileProtocol('editor-framework', function(request, callback) {
    var url = decodeURIComponent(request.url);
    var uri = Url.parse(url);
    var relativePath = uri.hostname;
    if ( uri.pathname ) {
        relativePath = Path.join( relativePath, uri.pathname );
    }
    var file = Path.join( Editor.frameworkPath, relativePath );
    callback ( { path: file } );
}, function ( err ) {
    if ( err ) {
        Winston.failed( 'Failed to register protocol editor-Framework, %s', err.message );
        return;
    }
    Winston.success( 'protocol editor-framework registerred' );
} );

// register protocol app://
Protocol.registerFileProtocol('app', function(request, callback) {
    var url = decodeURIComponent(request.url);
    var uri = Url.parse(url);
    var relativePath = uri.hostname;
    if ( uri.pathname ) {
        relativePath = Path.join( relativePath, uri.pathname );
    }
    var file = Path.join( Editor.appPath, relativePath );
    callback ( { path: file } );
}, function ( err ) {
    if ( err ) {
        Winston.failed( 'Failed to register protocol app, %s', err.message );
        return;
    }
    Winston.success( 'protocol app registerred' );
} );

// register protocol packages://

Protocol.registerFileProtocol('packages', function(request, callback) {
    var url = decodeURIComponent(request.url);
    var uri = Url.parse(url);

    var packagePath = Editor.Package.packagePath(uri.hostname);
    if ( packagePath ) {
        var packageInfo = Editor.Package.packageInfo(packagePath);
        if ( packageInfo ) {
            var file = Path.join( packageInfo._destPath, uri.pathname );
            callback ( { path: file } );
            return;
        }
    }

    // net::ERR_FILE_NOT_FOUND
    callback (-6);
}, function ( err ) {
    if ( err ) {
        Winston.failed( 'Failed to register protocol packages, %s', err.message );
        return;
    }
    Winston.success( 'protocol packages registerred' );
} );

// DISABLE: this make protocol can not use relative path
// // register protocol bower://
// Protocol.registerFileProtocol('bower', function(request, callback) {
//     var url = decodeURIComponent(request.url);
//     var uri = Url.parse(url);
//     var relativePath = uri.hostname;
//     if ( uri.pathname ) {
//         relativePath = Path.join( relativePath, uri.pathname );
//     }
//     var file = Path.join( Editor.appPath, 'bower_components', relativePath );
//     callback ( { path: file } );
// }, function ( err ) {
//     if ( err ) {
//         Winston.failed( 'Failed to register protocol bower, %s', err.message );
//         return;
//     }
//     Winston.success( 'protocol bower registerred' );
// } );

// DISABLE: same reason as bower
// // register protocol widgets://
// Protocol.registerFileProtocol('widgets', function(request, callback) {
//     var url = decodeURIComponent(request.url);
//     var uri = Url.parse(url);

//     var info = Editor.Package.widgetInfo(uri.hostname);
//     if ( info ) {
//         var file = Path.join( info.path, uri.pathname );
//         callback ( { path: file } );
//         return;
//     }
//     // net::ERR_FILE_NOT_FOUND
//     callback (-6);
// }, function ( err ) {
//     if ( err ) {
//         Winston.failed( 'Failed to register protocol widgets, %s', err.message );
//         return;
//     }
//     Winston.success( 'protocol widgets registerred' );
// } );

// Editor.url protocol register

Editor._protocol2fn = {};

function _url2path ( base ) {
    return function ( urlInfo ) {
        if ( urlInfo.pathname ) {
            return Path.join( base, urlInfo.host, urlInfo.pathname );
        }
        return Path.join( base, urlInfo.host );
    };
}

function _packages2path ( urlInfo ) {
    var packagePath = Editor.Package.packagePath(urlInfo.hostname);
    if ( packagePath ) {
        return Path.join( packagePath, urlInfo.pathname );
    }
    return '';
}

/**
 * Convert a url by its protocol to a filesystem path. This function is useful when you try to
 * get some internal file. You can use {@link Editor.registerProtocol} to register and map your filesystem
 * path to url. By default, Editor Framework register `editor-framework://` and `app://` protocol.
 * @method url
{'fraction_non_alphanumeric': 0.13725944817992117, 'fraction_numerical': 0.001159285879897983, 'mean_word_length': 2.763961605584642, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7018838', 'n_tokens_mistral': 2474, 'n_tokens_neox': 2472, 'n_words': 658}