Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Carlos dev typescript interface #362

Merged
merged 4 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,5 @@ cscope.*
compile_commands.json
report.pdf
report.xml
# infracost
.infracost/
5 changes: 4 additions & 1 deletion clients/nodejs/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ test-local: ## run unit tests against local running sliderule server
test-developer: ## run unit tests against developers cluster
cd sliderule; jest $(TEST) --setupFiles $(ROOT)/sliderule/tests/developer.config.js

test-uofmdtest: ## run unit tests against UofMDTest cluster in testsliderule.org domain
cd sliderule; jest $(TEST) --setupFiles $(ROOT)/sliderule/tests/uofmdtest.config.js

publish: ## publish package to npm registry
cd sliderule; npm publish --access public

Expand All @@ -30,4 +33,4 @@ distclean: ## fully remove all non-version controlled files and directories

help: ## That's me!
@grep -E '^[a-zA-Z_-].+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
@echo ENVVER:$(ENVVER)
@echo ROOT:$(ROOT)
58 changes: 58 additions & 0 deletions clients/nodejs/sliderule/core.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import https from 'https';

// Define types for the constants
export const INT8: number;
export const INT16: number;
export const INT32: number;
export const INT64: number;
export const UINT8: number;
export const UINT16: number;
export const UINT32: number;
export const UINT64: number;
export const BITFIELD: number;
export const FLOAT: number;
export const DOUBLE: number;
export const TIME8: number;
export const STRING: number;
export const USER: number;

// Define type for fieldtypes
export const fieldtypes: {
[key: string]: { code: number; size: number };
};

// Define type for the init function
export function init(config: {
domain?: string;
organization?: string;
protocol?: https;
verbose?: boolean;
desired_nodes?: any; // Replace 'any' with a more specific type if possible
time_to_live?: number;
timeout?: number;
}): void;

// Define type for the source function
export function source(
api: string,
parm?: any, // Replace 'any' with a more specific type if possible
stream?: boolean,
callbacks?: { [key: string]: (...args: any[]) => void }
): Promise<any>; // Replace 'any' with a more specific return type if possible

// Define type for the authenticate function
export function authenticate(
ps_username?: string | null,
ps_password?: string | null
): Promise<number | undefined>;

// Define type for the get_version function
export function get_version(): Promise<{
client: { version: string };
organization: string;
[key: string]: any; // Additional dynamic properties
}>;

// Define type for the get_values function
export function get_values(bytearray: Uint8Array, fieldtype: number): any[]; // Replace 'any' with a more specific type if possible

22 changes: 11 additions & 11 deletions clients/nodejs/sliderule/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

const https = require('https');
const netrc = require('netrc');
const pkg = require('./package.json')
import https from 'https';
import netrc from 'netrc';
import pkg from './package.json' assert { type: 'json' };

//------------------------------------
// File Data
Expand Down Expand Up @@ -113,7 +113,7 @@ function populateDefinition(rec_type) {
}
else {
return new Promise((resolve, reject) => {
exports.source("definition", {"rectype" : rec_type}).then(
source("definition", {"rectype" : rec_type}).then(
result => {
recordDefinitions[rec_type] = result;
resolve(recordDefinitions[rec_type]);
Expand Down Expand Up @@ -253,7 +253,7 @@ async function decodeRecord(rec_type, buffer, offset, rec_size) {
//
// parseResponse
//
function parseResponse (response, resolve, reject, callbacks) {
function parseResponse(response, resolve, reject, callbacks) {
// Check Response Code
if (response.statusCode !== 200) {
response.resume();
Expand Down Expand Up @@ -396,14 +396,14 @@ async function httpRequest(options, body, callbacks) {
//
// Initialize Client
//
exports.init = (config) => {
export function init(config) {
sysConfig = Object.assign(sysConfig, config)
}

//
// Source Endpoint
//
exports.source = (api, parm=null, stream=false, callbacks={}) => {
export function source(api, parm=null, stream=false, callbacks={}) {

// Setup Request Options
const options = {
Expand All @@ -426,7 +426,7 @@ exports.source = (api, parm=null, stream=false, callbacks={}) => {
//
// Authenticate User
//
exports.authenticate = (ps_username=null, ps_password=null) => {
export function authenticate(ps_username=null, ps_password=null) {

// Build Provisioning System URL
let psHost = 'ps.' + sysConfig.domain;
Expand Down Expand Up @@ -480,8 +480,8 @@ exports.authenticate = (ps_username=null, ps_password=null) => {
//
// Get Version
//
exports.get_version = () => {
return exports.source('version').then(
export function get_version() {
return source('version').then(
result => {
result['client'] = {version: pkg['version']};
result['organization'] = sysConfig.organization;
Expand All @@ -493,7 +493,7 @@ exports.get_version = () => {
//
// Get Values
//
exports.get_values = (bytearray, fieldtype) => {
export function get_values(bytearray, fieldtype) {
let values = [];
let buffer = Buffer.from(bytearray);
switch (fieldtype) {
Expand Down
22 changes: 22 additions & 0 deletions clients/nodejs/sliderule/h5coro.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Define the constants
export const ALL_ROWS: number;

// Define type for datatypes
export const datatypes: {
TEXT: number;
REAL: number;
INTEGER: number;
DYNAMIC: number;
};

// Define type for the h5 function
export function h5(
dataset: string,
resource: string,
asset: string,
datatype?: number,
col?: number,
startrow?: number,
numrows?: number,
callbacks?: { [key: string]: (...args: any[]) => void } | null
): Promise<any> | void; // Replace 'any' with a more specific return type if possible
6 changes: 2 additions & 4 deletions clients/nodejs/sliderule/h5coro.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

const https = require('https');
const core = require('./core.js')
const events = require('events');
import * as core from './core.js';

//------------------------------------
// File Data
Expand All @@ -51,7 +49,7 @@ const datatypes = {
//
// h5
//
exports.h5 = (dataset, resource, asset, datatype=datatypes.DYNAMIC, col=0, startrow=0, numrows=ALL_ROWS, callbacks=null) => {
export function h5(dataset, resource, asset, datatype=datatypes.DYNAMIC, col=0, startrow=0, numrows=ALL_ROWS, callbacks=null){
let parm = {
asset: asset,
resource: resource,
Expand Down
55 changes: 55 additions & 0 deletions clients/nodejs/sliderule/icesat2.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { Emitter } from 'mitt';

// Define the types for the constants
export const CNF_POSSIBLE_TEP: number;
export const CNF_NOT_CONSIDERED: number;
export const CNF_BACKGROUND: number;
export const CNF_WITHIN_10M: number;
export const CNF_SURFACE_LOW: number;
export const CNF_SURFACE_MEDIUM: number;
export const CNF_SURFACE_HIGH: number;
export const SRT_LAND: number;
export const SRT_OCEAN: number;
export const SRT_SEA_ICE: number;
export const SRT_LAND_ICE: number;
export const SRT_INLAND_WATER: number;
export const MAX_COORDS_IN_POLYGON: number;
export const GT1L: number;
export const GT1R: number;
export const GT2L: number;
export const GT2R: number;
export const GT3L: number;
export const GT3R: number;
export const STRONG_SPOTS: number[];
export const WEAK_SPOTS: number[];
export const LEFT_PAIR: number;
export const RIGHT_PAIR: number;
export const SC_BACKWARD: number;
export const SC_FORWARD: number;
export const ATL08_WATER: number;
export const ATL08_LAND: number;
export const ATL08_SNOW: number;
export const ATL08_ICE: number;

// Define the P constant as an object with string keys and number values
export const P: { [key: string]: number };

// Define the parameter type for the atl06p function
interface Atl06pParams {
asset?: string;
[key: string]: any; // Other dynamic keys
}
type Resource = any; // Replace 'any' with the actual type of a resource

// Define the callback type
interface Callbacks {
atl06rec?: (result: any) => void;
[key: string]: ((result: any) => void) | undefined;
}

// Define the function atl06p
export function atl06p(
parm: Atl06pParams,
resources: Resource[],
callbacks?: Callbacks | null
): Promise<any[]> | void;
76 changes: 56 additions & 20 deletions clients/nodejs/sliderule/icesat2.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,8 @@
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

const https = require('https');
const core = require('./core.js')
const events = require('events');

import mitt from 'mitt';
import {core} from '../sliderule/index.js';
//------------------------------------
// File Data
//------------------------------------
Expand Down Expand Up @@ -71,7 +69,7 @@ const ATL08_ICE = 3;
//
// PhoREAL Percentiles
//
P = { '5': 0, '10': 1, '15': 2, '20': 3, '25': 4, '30': 5, '35': 6, '40': 7, '45': 8, '50': 9,
const P = { '5': 0, '10': 1, '15': 2, '20': 3, '25': 4, '30': 5, '35': 6, '40': 7, '45': 8, '50': 9,
'55': 10, '60': 11, '65': 12, '70': 13, '75': 14, '80': 15, '85': 16, '90': 17, '95': 18 };

//------------------------------------
Expand All @@ -81,41 +79,79 @@ P = { '5': 0, '10': 1, '15': 2, '20': 3, '25': 4, '30': 5, '35': 6, '40'
//
// ATL06P
//
exports.atl06p = (parm, resources, callbacks=null) => {
if ('asset' in parm === false) {
export function atl06p(parm, resources, callbacks = null){
console.log("atl06p: ", parm, resources);
if (!('asset' in parm)) {
parm['asset'] = 'icesat2';
}
let rqst = {
"resources": resources,
"parms": parm
}
};
if (callbacks != null) {
return core.source('atl06p', rqst, true, callbacks);
}
else {
var event = new events.EventEmitter();
var total_recs = null;
var recs = [];
var callbacks = {
} else {
let emitter = mitt();

let total_recs = null;
let recs = [];
callbacks = {
atl06rec: (result) => {
recs.push(result["elevation"]);
if ((total_recs != null) && (recs.length == total_recs)) {
event.emit('complete');
if (total_recs != null && recs.length == total_recs) {
emitter.emit('complete');
}
},
};
return new Promise(resolve => {
core.source('atl06p', rqst, true, callbacks).then(
result => {
event.once('complete', () => {
const onComplete = () => {
resolve(recs.flat(1));
});
emitter.off('complete', onComplete); // Remove the event listener after it's called
};
emitter.on('complete', onComplete);

total_recs = result["atl06rec"] ?? 0;
if (recs.length == total_recs) {
event.emit('complete');
emitter.emit('complete');
}
}
);
});
}
}
};

// Export any other constants or functions if necessary
export {
CNF_POSSIBLE_TEP,
CNF_NOT_CONSIDERED,
CNF_BACKGROUND,
CNF_WITHIN_10M,
CNF_SURFACE_LOW,
CNF_SURFACE_MEDIUM,
CNF_SURFACE_HIGH,
SRT_LAND,
SRT_OCEAN,
SRT_SEA_ICE,
SRT_LAND_ICE,
SRT_INLAND_WATER,
MAX_COORDS_IN_POLYGON,
GT1L,
GT1R,
GT2L,
GT2R,
GT3L,
GT3R,
STRONG_SPOTS,
WEAK_SPOTS,
LEFT_PAIR,
RIGHT_PAIR,
SC_BACKWARD,
SC_FORWARD,
ATL08_WATER,
ATL08_LAND,
ATL08_SNOW,
ATL08_ICE
};

4 changes: 4 additions & 0 deletions clients/nodejs/sliderule/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import * as core from './core.js';
import * as icesat2 from './icesat2.js';
import * as h5coro from './h5coro.js';
export { core, h5coro, icesat2, };
12 changes: 4 additions & 8 deletions clients/nodejs/sliderule/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
const core = require("./core.js");
const icesat2 = require("./icesat2.js");
const h5coro = require("./h5coro.js");
module.exports = {
core: core,
h5coro: h5coro,
icesat2: icesat2,
};
import * as core from './core.js';
import * as icesat2 from './icesat2.js';
import * as h5coro from './h5coro.js';
export { core, h5coro, icesat2, };
Loading
Loading