Skip to content

Extension base

WiredSpast edited this page Feb 20, 2022 · 4 revisions

0. Setting your project to be an ES module

G-Node is written using ES modules, so the easiest way to use G-Node in your project is create your project as an ES module itself. To do so follow the following steps.

  1. Open the package.json file, it should now look somewhat like this:
{
  "name": "extensionname",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "gnode-api": "^0.2.1"
  }
}
  1. Add in `"type": "module", somewhere at base level in to the file, for example:
{
  "name": "extensionname",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "type": "module",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "gnode-api": "^0.2.1"
  }
}

1. Accessing the API library

In Node.js you can use a package installed with npm as a library in the following way:

Option 1: Import the entire package

import * as GNode from 'gnode-api';

You can access a component in the following way:

GNode.Extension
GNode.HPacket

Option 2: Specify the required components you need

import { Extension, HPacket } from 'gnode-api';

2. Create an extensionInfo object containing 'name', 'description', 'version' and 'author'

const extensionInfo = {
    name: "My Extension",
    description: "My first G-Node extension",
    version: "0.1",
    author: "Your name"
}

3. Create an extension instance and run it

const ext = new Extension(extensionInfo);
ext.run();

4. Connecting your extension to G-Earth

At this point your javascript file should look something like this

import { Extension, HPacket } from 'gnode-api';

const extensionInfo = {
    name: "My Extension",
    description: "My first G-Node extension",
    version: "0.1",
    author: "Your name"
}

let ext = new Extension(extensionInfo);
ext.run();

This current script allows you to connect to G-Earth if you run it with the following command

$ node [filename] -p [port]

Example:

$ node index.js -p 9092

Read the other wiki pages to learn how to intercept packets or create and send packets