<commit_before>/*
 * Copyright (c) 2015 Internet of Protocols Alliance (IOPA)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

const FreeList = require('../util/freelist.js').FreeList,
    Events = require('events'),
    URL = require('url'),
    util = require('util'),

    Cancellation = require('../util/cancellation').default,
    PrototypeExtend = require('./contextPrototypeExtend').default,

    constants = require('./constants'),
    IOPA = constants.IOPA,
    SERVER = constants.SERVER,
    
    mergeContext = require('../util/shallow').mergeContext;

  

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

/* *********************************************************
 * IOPA CONTEXT
 * ********************************************************* */

/**
 * Represents IOPA Context object for any REST Request/Response
 * See http://iopa.io for further details
 *
 * @class IopaContext
 * @constructor
 */
function IopaContext() {
     return this;
}
    
/**
* Initialize blank IopaContext object
* Generic properties common to all server types included
*
* @method Init
*/
IopaContext.prototype.init = function init() {
    var self = this;
    this[IOPA.Version] = "1.2";
    var _cancellationTokenSource = Cancellation();
    this[SERVER.CallCancelledSource] = _cancellationTokenSource;
    this[IOPA.CallCancelled] = _cancellationTokenSource.token;
    this[IOPA.Events] = new Events.EventEmitter();
    this[IOPA.Seq] = _nextSequence();
    this[SERVER.Logger] = console;
    this.log = function log(){return self[SERVER.Logger]};
    return this;
};

 Object.defineProperty(IopaContext.prototype, "log", {
                       get: function () { return  this[SERVER.Logger] ;
                       }  });

/**
 * Represents IopaContext Factory of up to 100 items
 *
 * @instance IopaContextFactory

 * @method alloc()  get new IopaContext from pool or by creation if none available; remember to call context.init();
 * @method free(context)   return IopaContext to the pool for reuse
 */
function IopaContextFactory() {
    _classCallCheck(this, IopaContextFactory);
    FreeList.call(this, 'IopaContext', 100, function () { return IopaContext.apply(Object.create(IopaContext.prototype)); });
 }

util.inherits(IopaContextFactory, FreeList);

/**
* Create a new barebones IOPA Request with or without a response record
*/
 IopaContextFactory.prototype._create = function _create(withoutResponse) {

    var context = this.alloc().init();

    if (!withoutResponse) {
        var response = this.alloc().init();
        context.response = response;
        context.response.parent = context;
    }

    return context;
};

/**
* Release the memory used by a given IOPA Context
*
* @param context the context to free 
*/   
IopaContextFactory.prototype.dispose = function dispose(context) {

    if (context.response) {
         
        var response = context.response;
        for (var prop in response) { if (response.hasOwnProperty(prop)) { response[prop] = null; } }
        this.free(response);
    }
                    
    for (var prop in context) { if (context.hasOwnProperty(prop)) { context[prop] = null; } };

    this.free(context);
};

/*
* ES6 finally/dispose pattern for IOPA Context
* @param context Iopa
* @param appfunc function(context): Promise
* returns Promise that always ultimately resolves to callback's result or rejects
*/
IopaContextFactory.prototype.using = function using(context, appfunc) {

    var self = this;
   
    /**  bluebird version only -- not used:
    *  	 return Promise.using(Promise.resolve(context)
    *	 .disposer(function(context, promise){
    *		 self.dispose(context);
    *		 context = null; 
    *	 }), cb);
    */

    return new Promise(function (resolve, reject) {
        var v = appfunc(context);
        if (typeof v === 'undefined')
            v = null;
        resolve(v);
    }).then(function (value) {<commit_msg>disponse only once<commit_after>/*
 * Copyright (c) 2015 Internet of Protocols Alliance (IOPA)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

const FreeList = require('../util/freelist.js').FreeList,
    Events = require('events'),
    URL = require('url'),
    util = require('util'),

    Cancellation = require('../util/cancellation').default,
    PrototypeExtend = require('./contextPrototypeExtend').default,

    constants = require('./constants'),
    IOPA = constants.IOPA,
    SERVER = constants.SERVER,
    
    mergeContext = require('../util/shallow').mergeContext;

  

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

/* *********************************************************
 * IOPA CONTEXT
 * ********************************************************* */

/**
 * Represents IOPA Context object for any REST Request/Response
 * See http://iopa.io for further details
 *
 * @class IopaContext
 * @constructor
 */
function IopaContext() {
     return this;
}
    
/**
* Initialize blank IopaContext object
* Generic properties common to all server types included
*
* @method Init
*/
IopaContext.prototype.init = function init() {
    var self = this;
    this[IOPA.Version] = "1.2";
    var _cancellationTokenSource = Cancellation();
    this[SERVER.CallCancelledSource] = _cancellationTokenSource;
    this[IOPA.CallCancelled] = _cancellationTokenSource.token;
    this[IOPA.Events] = new Events.EventEmitter();
    this[IOPA.Seq] = _nextSequence();
    this[SERVER.Logger] = console;
    this.log = function log(){return self[SERVER.Logger]};
    return this;
};

 Object.defineProperty(IopaContext.prototype, "log", {
                       get: function () { return  this[SERVER.Logger] ;
                       }  });

/**
 * Represents IopaContext Factory of up to 100 items
 *
 * @instance IopaContextFactory

 * @method alloc()  get new IopaContext from pool or by creation if none available; remember to call context.init();
 * @method free(context)   return IopaContext to the pool for reuse
 */
function IopaContextFactory() {
    _classCallCheck(this, IopaContextFactory);
    FreeList.call(this, 'IopaContext', 100, function () { return IopaContext.apply(Object.create(IopaContext.prototype)); });
 }

util.inherits(IopaContextFactory, FreeList);

/**
* Create a new barebones IOPA Request with or without a response record
*/
 IopaContextFactory.prototype._create = function _create(withoutResponse) {

    var context = this.alloc().init();

    if (!withoutResponse) {
        var response = this.alloc().init();
        context.response = response;
        context.response.parent = context;
    }

    return context;
};

/**
* Release the memory used by a given IOPA Context
*
* @param context the context to free 
*/   
IopaContextFactory.prototype.dispose = function dispose(context) {
   if (context == null || context[IOPA.Seq] == null) 
      return;
      
    if (context.response) {
           var response = context.response;
        for (var prop in response) { if (response.hasOwnProperty(prop)) { response[prop] = null; } }
        this.free(response);
    }
              
    for (var prop in context) { if (context.hasOwnProperty(prop)) { context[prop] = null; } };

    this.free(context);
};

/*
* ES6 finally/dispose pattern for IOPA Context
* @param context Iopa
* @param appfunc function(context): Promise
* returns Promise that always ultimately resolves to callback's result or rejects
*/
IopaContextFactory.prototype.using = function using(context, appfunc) {

    var self = this;
   
    /**  bluebird version only -- not used:
    *  	 return Promise.using(Promise.resolve(context)
    *	 .disposer(function(context, promise){
    *		 self.dispose(context);
    *		 context = null; 
    *	 }), cb);
    */

    return new Promise(function (resolve, reject) {
        var v = appfunc(context);
        if (typeof v === 'undefined')
            v = null;
        resolve(v);
    }).then(function (value) {
{'fraction_non_alphanumeric': 0.1388095761036679, 'fraction_numerical': 0.0037338018888644848, 'mean_word_length': 3.3783653846153845, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1432223', 'n_tokens_mistral': 2596, 'n_tokens_neox': 2492, 'n_words': 758}