javascript-adapter

javascript adapter used to execute javascript resources

License

License

Categories

Categories

JavaScript Languages
GroupId

GroupId

org.kgrid
ArtifactId

ArtifactId

javascript-adapter
Last Version

Last Version

1.0.0
Release Date

Release Date

Type

Type

jar
Description

Description

javascript-adapter
javascript adapter used to execute javascript resources
Project URL

Project URL

https://github.com/kgrid
Source Code Management

Source Code Management

https://github.com/kgrid/kgrid-adapter

Download javascript-adapter

How to add to project

<!-- https://jarcasting.com/artifacts/org.kgrid/javascript-adapter/ -->
<dependency>
    <groupId>org.kgrid</groupId>
    <artifactId>javascript-adapter</artifactId>
    <version>1.0.0</version>
</dependency>
// https://jarcasting.com/artifacts/org.kgrid/javascript-adapter/
implementation 'org.kgrid:javascript-adapter:1.0.0'
// https://jarcasting.com/artifacts/org.kgrid/javascript-adapter/
implementation ("org.kgrid:javascript-adapter:1.0.0")
'org.kgrid:javascript-adapter:jar:1.0.0'
<dependency org="org.kgrid" name="javascript-adapter" rev="1.0.0">
  <artifact name="javascript-adapter" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.kgrid', module='javascript-adapter', version='1.0.0')
)
libraryDependencies += "org.kgrid" % "javascript-adapter" % "1.0.0"
[org.kgrid/javascript-adapter "1.0.0"]

Dependencies

compile (1)

Group / Artifact Type Version
org.kgrid : adapter-api jar 1.0.0

test (4)

Group / Artifact Type Version
junit : junit jar 4.13
org.springframework : spring-test jar 5.1.3.RELEASE
org.mockito : mockito-core jar 2.23.4
com.fasterxml.jackson.dataformat : jackson-dataformat-yaml jar 2.9.6

Project Modules

There are no modules declared in this project.

Kgrid-Adapter

CircleCI License: GPL v3

Adapters provide a common interface to load & execute code in the Knowledge Grid

Table of Contents

  1. Build and Test Adapters
  2. Javascript V8 Adapter
  3. Javascript Nashorn Adapter
  4. Resource Adapter
  5. Proxy Adapter
  6. Additional Information

Build Adpaters

These instructions will get you a copy of the project up and running on your local machine for development and testing purposes. See deployment for notes on how to deploy these adapters in an activator on a live system.

Prerequisites

For building and running the application you need:

Clone

To get started you can simply clone this repository using git:

git clone https://github.com/kgrid/kgrid-adapter.git
cd kgrid-adapter

Install the adapters to your local maven repository where they can then be used as dependencies by the activator:

mvn clean install

Running the tests

Automated tests

Unit and Integration tests can be executed via

mvn clean test

Javascript v8 Adapter

This javascript engine uses the GraalVM javascript engine to run object payloads written in javascript locally. Any simple javascript file containing functions can be easily run using this adapter but more complex objects with external dependencies should be bundled into one file. (See the guide on creating a bundled KO.)

Example deployment descriptor:

  /welcome:
    artifact:
      - 'src/welcome.js'
    engine: 'javascript'
    function: 'welcome'

Pitfalls and current limitations

Currently the v8 engine cannot return native javascript arrays. You can work around this problem by using javascript objects instead. Or use the graal polyglot methods for creating a java array in javascript:

let intArray = Java.type('int[]');
let iarr = new intArray(3);
return iarr;

Calling other knowledge grid objects from javascript is supported but limited to passing primitive inputs due to how graal manages contexts. You can call another knowledge object from within your object using the code:

    let executor = context.getExecutor("hello-world/v1.0/welcome");
    return executor.execute(name);

Where hello-world/v1.0/welcome is the full arkId, version and endpoint name of the endpoint you wish to call and name is the input you wish to pass to it. Note that the input can only be a primitive(booleans, numbers or strings) and not an object. You can work around this by using JSON.stringify() to convert objects to strings before passing them between methods and JSON.parse to convert the JSON back into a string in the receiving object. See issue 631 for the graal project for updates.

**It is important to note, that there can be only one adapter of a particular engine supplied to the activator, so the V8 Graal Adapter and the Javascript Nashorn Adapter cannot be used simultaneously.

See the Javascript V8 Adapter Readme for more information.

Javascript Nashorn Adapter

The nashorn engine used by this adapter is being removed and use of this adapter is discouraged. Instead use either the js v8 adapter or the proxy adapter.

Example deployment descriptor:

  /welcome:
    artifact: 'src/welcome.js'
    engine: 'javascript'
    function: 'welcome'

Pitfalls and current limitations

The nashorn adapter transforms javascript arrays to a map with the key for each value set to the value's index in the array when it returns the value. Because of this using javascript arrays is discouraged.

**It is important to note, that there can be only one adapter of a particular engine supplied to the activator, so the V8 Graal Adapter and the Javascript Nashorn Adapter cannot be used simultaneously.

Proxy Adapter

The proxy adapter exposes endpoints which can be to register external runtime environments and make them accessible to execute object paylods.

There is a node external runtime that runs objects in the NodeJS environment. To run objects in the remove environment download, install and run that project after starting the activator. Note that the deployment descriptor of a proxy adapter object contains two more fields than a local javascript object:

/welcome:
  post:
    artifact:
      - 'src/welcome.js'
    engine: 'node'
    entry: 'src/welcome.js'
    function: 'welcome'

The engine field tells the adapter which remote environment the code is run in and the entry field specifies the file that contains the main function.

The proxy adapter can run any NodeJS project that you can run locally.

More external runtimes are planned such as a native python environment.

See the Proxy Adapter Readme for more information.

Resource Adapter

The nashorn engine used by this adapter is being removed and use of this adapter is discouraged. Instead use either the js v8 adapter or the proxy adapter.

Example deployment descriptor:

/welcome:
  get:
    artifact: 'src/welcome.js'
    engine: 'resource'

Pitfalls and current limitations

  • The endpoints that the activator exposes to work with this adapter are:

    GET <activator url>/resource/<naan>/<name>/<api version>/<endpoint>

    and

    GET <activator url>/resource/<naan>/<name>/<api version>/<endpoint>/<filename> See the Resource Adapter Readme for more information.

Additional Information

Adding a new Adapter

Creating new Adapters requires you implement the Adapter API interface. The adapters utilize Java Services and require that you package your adapter with a service identifer the META-INF. Create a file in the resources/META-INF/services directory called org.kgrid.adapter with a single line that is the fully qualified class name of your adapter.

resources
  META-INF
     services
        org.kgrid.adapter
org.kgrid

Knowledge Grid Projects

Repository of KGrid team projects under development

Versions

Version
1.0.0
0.10.15
0.10.12
0.10.11
0.10.10
0.10.9
0.10.8
0.10.7
0.10.6
0.10.5
0.10.4
0.10.3
0.10.1
0.10.0
0.9.0
0.8.2
0.8.1
0.8.0
0.0.5
0.0.4
0.0.3
0.0.2
0.0.1