Using global objects from external libs

Hey, guys!,

I’m trying to use a lib that is a OSLC REST API client - oslc-client.
On the example file, it is requiring the file namespaces.js.
In this file there’s some global objects declared.
I am not being able to import this file in my server.js.
Tried:

require(“oslc-client/namespaces”);
require(“./node_modules/oslc-client/namespaces”);

Nothing seems to work.

But some others resources, since they are classes, works!

var OSLCServer = require(“oslc-client”);
var OSLCResource = require(“oslc-client/OSLCResource”);

So, what am I missing here? :thinking:

1 Like

Well, I kind gave up using this lib, since I found another issue with this lib when I run fdk validate:

[ERROR] Package ‘oslc-client’ version contains special characters(^ or ~).

But what does this error means? Is there someway to fix it?

Hey Samuel,

For issue 2 - [ERROR] Package ‘oslc-client’ version contains special characters(^ or ~).
Try including the exact version instead of semver version

Let me know fixing issue 2 resolves issue 1

2 Likes

Ahh, it was it! I missed that on the manifest file hehe
Thank you!

About issue 1, any ideas?

Actually it seems that the file is being imported, I can affirm it because if I require it mistyping a letter, a exception is thrown, so this is fine:

require(“oslc-client/namespaces.js”);

On this file, there’s some declarations:

var rdflib = require(‘rdflib’)

global.OSLCCM10 = rdflib.Namespace(‘http://open-services.net/xmlns/cm/1.0/’)
global.OSLCRM10 = rdflib.Namespace(‘http://open-services.net/xmlns/rm/1.0/’)
global.JD = rdflib.Namespace(‘http://jazz.net/xmlns/prod/jazz/discovery/1.0/’)

On my server.js, when I try to use it:

server.connect(OSLCCM10(“cmServiceProviders”), callback)

I get:

ReferenceError: OSLCCM10 is not defined

Hey Samuel,

Can you log the global object and add the response in this thread. Please check if OSLC variables are there in the result
console.log(global)

I guess the platform wipes and overwrites global object in node run time and so I don’t think the external global objects will be available

Yeah, I guess you are right
You can see that I created the global TEST, it worked, but the vars from lib is not there:

{ fdkcoverage:
{ ‘server/server.js’:
{ path: ‘server/server.js’,
statementMap: [Object],
fnMap: [Object],
branchMap: {},
s: [Object],
f: [Object],
b: {},
_coverageSchema: ‘43e27e138ebf9cfc5966b082cf9a028302ed4184’,
hash: ‘1475a96cc15a9a606dd1f0a05291d66de6d4dd93’ } },
exports: [Function: handler],
console:
{ log: [Function: bound consoleCall],
info: [Function: bound consoleCall],
error: [Function: bound consoleCall] },
require: [Function: require],
process: { cwd: [Function: cwd] },
generateTargetUrl: [Function],
‘$schedule’:
ScheduleApi {
[Symbol()]:
ProductEvent {
event: [Object],
req: [IncomingMessage],
res: [ServerResponse],
product: ‘freshdesk’,
cachedModules: {},
nestedPath: [Array],
serverScript:
‘\n (function() {\n var global = Function('return this;')();\n\n if (!global.fdkcoverage) {\n global.fdkcoverage = {};\n\n if (typeof fetch !== 'undefined') {\n var previous;\n\n setInterval(function() {\n var current = JSON.stringify(global.fdkcoverage);\n\n if (current === previous) {\n return;\n }\n\n fetch('http://localhost:10001/iframe/coverage\’, {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/json'\n },\n body: current\n }).then(() => previous = current);\n }, 2000);\n }\n }\n })();\n"use strict";\n\nvar cov_1llao29e16 = function () {\n var path = “server/server.js”;\n var hash = “1475a96cc15a9a606dd1f0a05291d66de6d4dd93”;\n var global = new Function(“return this”)();\n var gcv = “fdkcoverage”;\n var coverageData = {\n path: “server/server.js”,\n statementMap: {\n “0”: {\n start: {\n line: 2,\n column: 12\n },\n end: {\n line: 2,\n column: 28\n }\n },\n “1”: {\n start: {\n line: 3,\n column: 17\n },\n end: {\n line: 3,\n column: 39\n }\n },\n “2”: {\n start: {\n line: 4,\n column: 19\n },\n end: {\n line: 4,\n column: 54\n }\n },\n “3”: {\n start: {\n line: 6,\n column: 0\n },\n end: {\n line: 6,\n column: 37\n }\n },\n “4”: {\n start: {\n line: 7,\n column: 13\n },\n end: {\n line: 7,\n column: 30\n }\n },\n “5”: {\n start: {\n line: 9,\n column: 0\n },\n end: {\n line: 122,\n column: 2\n }\n },\n “6”: {\n start: {\n line: 21,\n column: 4\n },\n end: {\n line: 21,\n column: 25\n }\n },\n “7”: {\n start: {\n line: 23,\n column: 4\n },\n end: {\n line: 23,\n column: 31\n }\n },\n “8”: {\n start: {\n line: 33,\n column: 4\n },\n end: {\n line: 33,\n column: 11\n }\n },\n “9”: {\n start: {\n line: 34,\n column: 4\n },\n end: {\n line: 58,\n column: 6\n }\n },\n “10”: {\n start: {\n line: 37,\n column: 22\n },\n end: {\n line: 37,\n column: 78\n }\n },\n “11”: {\n start: {\n line: 39,\n column: 22\n },\n end: {\n line: 39,\n column: 69\n }\n },\n “12”: {\n start: {\n line: 42,\n column: 25\n },\n end: {\n line: 42,\n column: 43\n }\n },\n “13”: {\n start: {\n line: 43,\n column: 10\n },\n end: {\n line: 43,\n column: 40\n }\n },\n “14”: {\n start: {\n line: 44,\n column: 10\n },\n end: {\n line: 44,\n column: 63\n }\n },\n “15”: {\n start: {\n line: 45,\n column: 10\n },\n end: {\n line: 45,\n column: 61\n }\n },\n “16”: {\n start: {\n line: 46,\n column: 10\n },\n end: {\n line: 49,\n column: 13\n }\n },\n “17”: {\n start: {\n line: 47,\n column: 12\n },\n end: {\n line: 47,\n column: 53\n }\n },\n “18”: {\n start: {\n line: 48,\n column: 12\n },\n end: {\n line: 48,\n column: 26\n }\n },\n “19”: {\n start: {\n line: 54,\n column: 8\n },\n end: {\n line: 54,\n column: 31\n }\n },\n “20”: {\n start: {\n line: 55,\n column: 8\n },\n end: {\n line: 55,\n column: 34\n }\n },\n “21”: {\n start: {\n line: 56,\n column: 8\n },\n end: {\n line: 56,\n column: 40\n }\n },\n “22”: {\n start: {\n line: 125,\n column: 2\n },\n end: {\n line: 125,\n column: 84\n }\n }\n },\n fnMap: {\n “0”: {\n name: “(anonymous_0)”,\n decl: {\n start: {\n line: 14,\n column: 25\n },\n end: {\n line: 14,\n column: 26\n }\n },\n loc: {\n start: {\n line: 14,\n column: 41\n },\n end: {\n line: 121,\n column: 3\n }\n },\n line: 14\n },\n “1”: {\n name: “(anonymous_1)”,\n decl: {\n start: {\n line: 37,\n column: 8\n },\n end: {\n line: 37,\n column: 9\n }\n },\n loc: {\n start: {\n line: 37,\n column: 22\n },\n end: {\n line: 37,\n column: 78\n }\n },\n line: 37\n },\n “2”: {\n name: “(anonymous_2)”,\n decl: {\n start: {\n line: 39,\n column: 8\n },\n end: {\n line: 39,\n column: 9\n }\n },\n loc: {\n start: {\n line: 39,\n column: 22\n },\n end: {\n line: 39,\n column: 69\n }\n },\n line: 39\n },\n “3”: {\n name: “(anonymous_3)”,\n decl: {\n start: {\n line: 41,\n column: 8\n },\n end: {\n line: 41,\n column: 9\n }\n },\n loc: {\n start: {\n line: 41,\n column: 22\n },\n end: {\n line: 50,\n column: 9\n }\n },\n line: 41\n },\n “4”: {\n name: “(anonymous_4)”,\n decl: {\n start: {\n line: 46,\n column: 42\n },\n end: {\n line: 46,\n column: 43\n }\n },\n loc: {\n start: {\n line: 46,\n column: 65\n },\n end: {\n line: 49,\n column: 11\n }\n },\n line: 46\n },\n “5”: {\n name: “(anonymous_5)”,\n decl: {\n start: {\n line: 52,\n column: 6\n },\n end: {\n line: 52,\n column: 7\n }\n },\n loc: {\n start: {\n line: 52,\n column: 30\n },\n end: {\n line: 57,\n column: 7\n }\n },\n line: 52\n },\n “6”: {\n name: “getServerConnection”,\n decl: {\n start: {\n line: 124,\n column: 9\n },\n end: {\n line: 124,\n column: 28\n }\n },\n loc: {\n start: {\n line: 124,\n column: 38\n },\n end: {\n line: 127,\n column: 1\n }\n },\n line: 124\n }\n },\n branchMap: {},\n s: {\n “0”: 0,\n “1”: 0,\n “2”: 0,\n “3”: 0,\n “4”: 0,\n “5”: 0,\n “6”: 0,\n “7”: 0,\n “8”: 0,\n “9”: 0,\n “10”: 0,\n “11”: 0,\n “12”: 0,\n “13”: 0,\n “14”: 0,\n “15”: 0,\n “16”: 0,\n “17”: 0,\n “18”: 0,\n “19”: 0,\n “20”: 0,\n “21”: 0,\n “22”: 0\n },\n f: {\n “0”: 0,\n “1”: 0,\n “2”: 0,\n “3”: 0,\n “4”: 0,\n “5”: 0,\n “6”: 0\n },\n b: {},\n _coverageSchema: “43e27e138ebf9cfc5966b082cf9a028302ed4184”,\n hash: “1475a96cc15a9a606dd1f0a05291d66de6d4dd93”\n };\n var coverage = global[gcv] || (global[gcv] = {});\n\n if (coverage[path] && coverage[path].hash === hash) {\n return coverage[path];\n }\n\n return coverage[path] = coverageData;\n}();\n\nvar async = (cov_1llao29e16.s[0]++, require(“async”));\nvar OSLCServer = (cov_1llao29e16.s[1]++, require(“oslc-client”));\nvar OSLCResource = (cov_1llao29e16.s[2]++, require(“oslc-client/OSLCResource”));\ncov_1llao29e16.s[3]++;\n\nrequire(“oslc-client/namespaces.js”);\n\nvar rdflib = (cov_1llao29e16.s[4]++, require(“rdflib”));\ncov_1llao29e16.s[5]++;\nexports = {\n events: [{\n event: “onTicketCreate”,\n callback: “onTicketCreateHandler”\n }],\n onTicketCreateHandler: function (args) {\n cov_1llao29e16.f[0]++;\n cov_1llao29e16.s[6]++;\n global.TEST = “TEST”;\n cov_1llao29e16.s[7]++;\n console.log(global.global);\n cov_1llao29e16.s[8]++;\n return;\n cov_1llao29e16.s[9]++;\n async.series([callback => {\n cov_1llao29e16.f[1]++;\n cov_1llao29e16.s[10]++;\n return server.connect(OSLCCM10(“cmServiceProviders”), callback);\n }, callback => {\n cov_1llao29e16.f[2]++;\n cov_1llao29e16.s[11]++;\n return server.use(“_Jv0jwFXOEeuKc9Bmrq6cOA”, callback);\n }, callback => {\n cov_1llao29e16.f[3]++;\n var deleteMe = (cov_1llao29e16.s[12]++, new OSLCResource());\n cov_1llao29e16.s[13]++;\n deleteMe.setTitle(“deleteMe”);\n cov_1llao29e16.s[14]++;\n deleteMe.setDescription(“A test resource to delete”);\n cov_1llao29e16.s[15]++;\n deleteMe.set(RDF(“type”), OSLCCM(“ChangeRequest”));\n cov_1llao29e16.s[16]++;\n server.create(“task”, deleteMe, function (err, result) {\n cov_1llao29e16.f[4]++;\n cov_1llao29e16.s[17]++;\n console.log("Created: " + result.id.uri);\n cov_1llao29e16.s[18]++;\n callback(err);\n });\n }], function (err, results) {\n cov_1llao29e16.f[5]++;\n cov_1llao29e16.s[19]++;\n console.log(“to aqui”);\n cov_1llao29e16.s[20]++;\n console.log(“erros”, err);\n cov_1llao29e16.s[21]++;\n console.log(“results”, results);\n });\n }\n};\n\nfunction getServerConnection(iparams) {\n cov_1llao29e16.f[6]++;\n cov_1llao29e16.s[22]++;\n return new OSLCServer(iparams.server_uri, iparams.user_id, iparams.user_password);\n}',
sandboxAPI: [Object],
contextifiedGlobal: [Object],
svrExecScript: [Object] } },
‘$db’: DBApi { product: ‘freshdesk’ },
‘$request’: RequestApi {},
renderData: [Function: renderData],
global:
{ fdkcoverage: { ‘server/server.js’: [Object] },
exports: [Function: handler],
console:
{ log: [Function: bound consoleCall],
info: [Function: bound consoleCall],
error: [Function: bound consoleCall] },
require: [Function: require],
process: { cwd: [Function: cwd] },
generateTargetUrl: [Function],
‘$schedule’: ScheduleApi { [Symbol()]: [ProductEvent] },
‘$db’: DBApi { product: ‘freshdesk’ },
‘$request’: RequestApi {},
renderData: [Function: renderData],
global: [Circular],
__svrScript:
{ events: [Array],
onTicketCreateHandler: [Function: onTicketCreateHandler] },
TEST: ‘TEST’ },
__svrScript:
{ events: [ [Object] ],
onTicketCreateHandler: [Function: onTicketCreateHandler] },
TEST: ‘TEST’ }

Don’t you think this should be checked by the platform team? Or is this a expected behavior?

This is the known shortcoming Samuel. I would suggest you use a similar npm package that exposes this in the module exports and not as a global object

1 Like

I see…
Well, I copied the variables and pasted on my server.js
I guess it will do for now hehe