This repository has been archived by the owner on Mar 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 158
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1321 from sudeshrshetty/aries-js-worker
feat: rest client worker for aries JS
- Loading branch information
Showing
8 changed files
with
322 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
/* | ||
Copyright SecureKey Technologies Inc. All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
|
||
import axios from 'axios'; | ||
|
||
// All REST endpoints provided by agent controller | ||
const pkgs = { | ||
didexchange: { | ||
CreateInvitation: { | ||
path: "/connections/create-invitation", | ||
method: "POST" | ||
}, | ||
ReceiveInvitation: { | ||
path: "/connections/receive-invitation", | ||
method: "POST" | ||
}, | ||
AcceptInvitation: { | ||
path: "/connections/{id}/accept-invitation", | ||
method: "POST", | ||
pathParam:"id" | ||
}, | ||
AcceptExchangeRequest: { | ||
path: "/connections/{id}/accept-request", | ||
method: "POST", | ||
pathParam:"id" | ||
}, | ||
CreateImplicitInvitation: { | ||
path: "/connections/create-implicit-invitation", | ||
method: "POST" | ||
}, | ||
RemoveConnection: { | ||
path: "/connections/{id}/remove", | ||
method: "POST", | ||
pathParam:"id" | ||
}, | ||
QueryConnectionByID: { | ||
path: "/connections/{id}", | ||
method: "GET", | ||
pathParam:"id" | ||
}, | ||
QueryConnections: { | ||
path: "/connections", | ||
method: "GET" | ||
}, | ||
}, | ||
vdri: { | ||
CreatePublicDID: { | ||
path: "/vdri/create-public-did", | ||
method: "POST" | ||
}, | ||
}, | ||
messaging: { | ||
RegisteredServices: { | ||
path: "/message/services", | ||
method: "GET" | ||
}, | ||
RegisterMessageService: { | ||
path: "/message/register-service", | ||
method: "POST" | ||
}, | ||
RegisterHTTPMessageService: { | ||
path: "/http-over-didcomm/register", | ||
method: "POST" | ||
}, | ||
UnregisterMessageService: { | ||
path: "/message/unregister-service", | ||
method: "POST" | ||
}, | ||
SendNewMessage: { | ||
path: "/message/send", | ||
method: "POST" | ||
}, | ||
SendReplyMessage: { | ||
path: "/message/reply", | ||
method: "POST" | ||
}, | ||
}, | ||
route: { | ||
Register: { | ||
path: "/route/register", | ||
method: "POST" | ||
}, | ||
Unregister: { | ||
path: "/route/unregister", | ||
method: "DELETE" | ||
}, | ||
GetConnection: { | ||
path: "/route/connection", | ||
method: "GET" | ||
}, | ||
}, | ||
} | ||
|
||
/** | ||
* Agent rest client for given agent endpoint | ||
* @param url is rest endpoint url | ||
* @class | ||
*/ | ||
export const Client = class { | ||
constructor(url) { | ||
this.url = url | ||
} | ||
|
||
async handle(request) { | ||
const r = (pkgs[request.pkg]) ? pkgs[request.pkg][request.fn] : null; | ||
if (!r) { | ||
return "unable to find given pkg/fn:"+request.pkg+"/"+request.fn; | ||
} | ||
|
||
let url = this.url + r.path | ||
if (r.pathParam){ | ||
const p = ((request.payload[r.pathParam])) ? (request.payload[r.pathParam]) : ""; | ||
url = this.url + r.path.replace("{"+r.pathParam+"}", p); | ||
} | ||
|
||
console.debug(`[${r.method}] ${url}, request ${JSON.stringify(request.payload)}`) | ||
|
||
const resp = await axios({ | ||
method: r.method, | ||
url: url, | ||
data: request.payload | ||
}); | ||
|
||
return resp.data; | ||
} | ||
}; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
Copyright SecureKey Technologies Inc. All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
self.importScripts("./agent-rest-client.js") | ||
|
||
postMessage({topic: "asset-ready"}) | ||
|
||
// TODO synchronized access to controller | ||
let handler, controller | ||
|
||
const ariesHandle = { | ||
aries: { | ||
Start: (data) => { | ||
if (controller) { | ||
return newResponse(data.id, null, "aries already started"); | ||
} | ||
|
||
if (!data.payload["agent-rest-url"]) { | ||
return newResponse(data.id, null, "'agent-rest-url' is required"); | ||
} | ||
|
||
controller = new RESTAgent.Client(data.payload["agent-rest-url"]); | ||
return newResponse(data.id, "aries is started"); | ||
}, | ||
Stop: (data) => { | ||
if (!controller) { | ||
return newResponse(data.id, null, "aries already stopped"); | ||
} | ||
controller = null; | ||
return newResponse(data.id, "aries stopped"); | ||
} | ||
} | ||
} | ||
|
||
onmessage = async function (e) { | ||
console.debug('message received :', e.data); | ||
if (ariesHandle[e.data.pkg] && ariesHandle[e.data.pkg][e.data.fn]) { | ||
postMessage(ariesHandle[e.data.pkg][e.data.fn](e.data)); | ||
return; | ||
} | ||
|
||
if (controller) { | ||
try{ | ||
const response = await controller.handle(e.data); | ||
postMessage(newResponse(e.data.id, response)); | ||
console.debug("response from rest controller", response); | ||
} catch(error){ | ||
console.debug("error from rest controller", error.response); | ||
postMessage(newResponse(e.data.id, null, JSON.stringify(error.response.data))); | ||
} | ||
return; | ||
} | ||
|
||
postMessage(newResponse(e.data.id, null, "aries not started")); | ||
} | ||
|
||
|
||
function newResponse(id, payload, errMsg, topic) { | ||
const isErr = (errMsg) ? true : false; | ||
return { | ||
id: id, | ||
payload: payload, | ||
isErr: isErr, | ||
errMsg: errMsg, | ||
topic: topic | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
Copyright SecureKey Technologies Inc. All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
export function loadWorker(pending, notifications, paths) { | ||
const workerJS = paths.dir + "/worker-impl-rest.js" | ||
const worker = new Worker(workerJS) | ||
worker.onmessage = e => { | ||
const result = e.data | ||
if (result.topic){ | ||
if (notifications.get(result.topic)) { | ||
notifications.get(result.topic)(result) | ||
} else if (notifications.get("all")){ | ||
notifications.get("all")(result) | ||
} else { | ||
console.log("no subscribers found for this topic", result.topic) | ||
} | ||
return | ||
} | ||
const cb = pending.get(result.id) | ||
pending.delete(result.id) | ||
cb(result) | ||
} | ||
worker.onerror = e => { | ||
throw new Error("aries: failed to load worker: " + e.message) | ||
} | ||
return worker | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
Copyright SecureKey Technologies Inc. All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
const path = require("path") | ||
|
||
const { CleanWebpackPlugin } = require('clean-webpack-plugin') | ||
const WebpackShellPlugin = require('webpack-shell-plugin') | ||
|
||
const { PATHS } = require("./webpack.config.common.js") | ||
|
||
const OUTPUT = path.join(PATHS.buildDir, "rest/") | ||
|
||
const restConfig = { | ||
entry: path.join(PATHS.srcDir, "aries.js"), | ||
target: 'web', | ||
output: { | ||
path: OUTPUT, | ||
publicPath: "dist/rest", | ||
libraryTarget: "umd", | ||
filename: 'aries.js', | ||
library: 'Aries' | ||
}, | ||
plugins: [ | ||
new CleanWebpackPlugin(), | ||
new WebpackShellPlugin({ | ||
onBuildStart: [ | ||
"mkdir -p " + OUTPUT, | ||
"cp -p src/worker-impl-rest.js dist/assets" | ||
] | ||
}) | ||
], | ||
resolve: { | ||
alias: { | ||
worker_loader$: path.join(PATHS.srcDir, "worker-loader-rest.js"), | ||
}, | ||
mainFields: ['browser', 'module', 'main'] | ||
} | ||
} | ||
|
||
const assetConfig = { | ||
entry: path.join(PATHS.srcDir, "agent-rest-client.js"), | ||
target: 'webworker', | ||
output: { | ||
path: path.resolve(PATHS.buildDir, 'assets'), | ||
publicPath: "dist/assets", | ||
filename: 'agent-rest-client.js', | ||
library: "RESTAgent" | ||
}, | ||
} | ||
|
||
module.exports = [ restConfig, assetConfig ]; |