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
feat: rest client worker for aries JS #1321
Merged
troyronda
merged 1 commit into
hyperledger-archives:master
from
sudeshrshetty:aries-js-worker
Feb 23, 2020
+322
−3
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 ]; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@llorllale @sudeshrshetty I wonder if
web
andnode
names should include that they are wasm. (followup).