2023-02-09 12:16:36 +01:00
|
|
|
|
|
|
|
// Called on server initialization
|
|
|
|
function onInit() {
|
|
|
|
console.log("server started");
|
|
|
|
|
|
|
|
// Register RPC exposed methods
|
|
|
|
rpc.register("echo", echo);
|
|
|
|
rpc.register("throwErrorFromClient", throwError);
|
|
|
|
|
|
|
|
rpc.register("add", add);
|
|
|
|
rpc.register("reset", reset);
|
|
|
|
rpc.register("total", total);
|
2023-02-24 14:40:28 +01:00
|
|
|
rpc.register("getUserInfo", getUserInfo);
|
2023-03-22 20:48:09 +01:00
|
|
|
|
|
|
|
rpc.register("listApps");
|
|
|
|
rpc.register("getApp");
|
|
|
|
rpc.register("getAppUrl");
|
2023-09-28 23:41:01 -06:00
|
|
|
|
|
|
|
rpc.register("serverSideCall", serverSideCall)
|
2023-02-09 12:16:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Called for each client message
|
2023-03-03 11:42:20 +01:00
|
|
|
function onClientMessage(ctx, message) {
|
|
|
|
console.log("onClientMessage", message);
|
|
|
|
|
|
|
|
switch (message.test) {
|
|
|
|
case "broadcast":
|
|
|
|
net.broadcast(message);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
net.send(ctx, message);
|
|
|
|
}
|
2023-02-09 12:16:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Called for each blob upload request
|
|
|
|
function onBlobUpload(ctx, blobId, blobInfo, metadata) {
|
|
|
|
console.log("onBlobUpload", blobId, blobInfo, metadata);
|
|
|
|
|
|
|
|
if (!blobInfo.contentType == "application/json") return { allow: false };
|
|
|
|
if (!blobInfo.filename == "blob") return { allow: false };
|
|
|
|
|
|
|
|
return { allow: true, bucket: "test-bucket" };
|
|
|
|
}
|
|
|
|
|
|
|
|
// Called for each blob download request
|
|
|
|
function onBlobDownload(ctx, bucket, blobId) {
|
|
|
|
console.log("onBlobDownload", bucket, blobId);
|
|
|
|
return { allow: true };
|
|
|
|
}
|
|
|
|
|
|
|
|
// RPC methods
|
|
|
|
|
|
|
|
function echo(ctx, params) {
|
|
|
|
console.log("echoing", params);
|
|
|
|
return params;
|
|
|
|
}
|
|
|
|
|
|
|
|
function throwError(ctx, params) {
|
|
|
|
throw new Error("oh no !");
|
|
|
|
}
|
|
|
|
|
|
|
|
var count = 0;
|
|
|
|
|
|
|
|
function add(ctx, params) {
|
|
|
|
console.log("add", params);
|
|
|
|
count += params.value;
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
function reset(ctx, params) {
|
|
|
|
count = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
function total(ctx, params) {
|
|
|
|
return count;
|
2023-02-24 14:40:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function getUserInfo(ctx, params) {
|
|
|
|
var subject = auth.getClaim(ctx, auth.CLAIM_SUBJECT);
|
|
|
|
var role = auth.getClaim(ctx, auth.CLAIM_ROLE);
|
|
|
|
var preferredUsername = auth.getClaim(ctx, auth.CLAIM_PREFERRED_USERNAME);
|
|
|
|
|
|
|
|
return {
|
|
|
|
subject: subject,
|
|
|
|
role: role,
|
|
|
|
preferredUsername: preferredUsername,
|
|
|
|
};
|
2023-03-22 20:48:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function listApps(ctx) {
|
|
|
|
return app.list(ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getApp(ctx, params) {
|
|
|
|
var appId = params.appId;
|
|
|
|
return app.get(ctx, appId);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getAppUrl(ctx, params) {
|
|
|
|
var appId = params.appId;
|
2023-04-06 11:52:04 +02:00
|
|
|
var from = params.from;
|
|
|
|
|
|
|
|
return app.getUrl(ctx, appId, from ? from : '');
|
2023-04-02 17:59:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function onClientFetch(ctx, url, remoteAddr) {
|
|
|
|
return { allow: url === 'http://example.com' };
|
2023-09-28 23:41:01 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
function serverSideCall(ctx, params) {
|
|
|
|
console.log("Calling %s.%s(args...)", params.module, params.func)
|
|
|
|
return globalThis[params.module][params.func].call(null, ctx, ...params.args);
|
2023-02-09 12:16:36 +01:00
|
|
|
}
|