All files / composer-runtime-embedded/lib embeddedcontext.js

100% Statements 24/24
100% Branches 7/7
100% Functions 7/7
100% Lines 24/24
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106                                1x 1x 1x 1x 1x 1x                                 10x 10x 10x 10x 10x               1x               2x                 2x 1x   2x               2x 1x   2x               2x 1x   2x             1x         1x  
/*
 * 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.
 */
 
'use strict';
 
const Context = require('composer-runtime').Context;
const EmbeddedDataService = require('./embeddeddataservice');
const EmbeddedIdentityService = require('./embeddedidentityservice');
const EmbeddedEventService = require('./embeddedeventservice');
const EmbeddedHTTPService = require('./embeddedhttpservice');
const EmbeddedScriptCompiler = require('./embeddedscriptcompiler');
 
/**
 * A class representing the current request being handled by the JavaScript engine.
 * @protected
 */
class EmbeddedContext extends Context {
 
    /**
     * Constructor.
     * @param {Engine} engine The owning engine.
     * @param {Object} identity The current identity.
     * @param {EventEmitter} eventSink The event emitter
     * @param {InstalledBusinessNetwork} installedBusinessNetwork The installed business network
     * @param {Object} [additionalConnectorOptions] Additional connector specific options for this transaction.
     */
    constructor(engine, identity, eventSink, installedBusinessNetwork, additionalConnectorOptions = {}) {
        super(engine, installedBusinessNetwork);
        this.dataService = new EmbeddedDataService(engine.getContainer().getUUID(), false, additionalConnectorOptions);
        this.identityService = new EmbeddedIdentityService(identity);
        this.eventSink = eventSink;
        this.additionalConnectorOptions = additionalConnectorOptions;
    }
 
    /**
     * Get the data service provided by the chaincode container.
     * @return {DataService} The data service provided by the chaincode container.
     */
    getDataService() {
        return this.dataService;
    }
 
    /**
     * Get the identity service provided by the chaincode container.
     * @return {IdentityService} The identity service provided by the chaincode container.
     */
    getIdentityService() {
        return this.identityService;
    }
 
 
    /**
     * Get the event service provided by the chaincode container.
     * @return {EventService} The event service provided by the chaincode container.
     */
    getEventService() {
        if (!this.eventService) {
            this.eventService = new EmbeddedEventService(this.eventSink);
        }
        return this.eventService;
    }
 
    /**
     * Get the event service provided by the chaincode container.
     * @return {EventService} The event service provided by the chaincode container.
     */
    getHTTPService() {
        if (!this.httpService) {
            this.httpService = new EmbeddedHTTPService();
        }
        return this.httpService;
    }
 
    /**
     * Get the script compiler.
     * @return {ScriptCompiler} scriptCompiler The script compiler.
     */
    getScriptCompiler() {
        if (!this.scriptCompiler) {
            this.scriptCompiler = new EmbeddedScriptCompiler();
        }
        return this.scriptCompiler;
    }
 
    /**
     * Get the native api
     */
    getNativeAPI() {
        throw new Error('Native API not available in embedded runtime');
    }
 
}
 
module.exports = EmbeddedContext;