Skip to content

Commit

Permalink
Make AnonymousScriptRunner.downloadFileByHiveUrl static.
Browse files Browse the repository at this point in the history
  • Loading branch information
fred-yu-2013 authored and stiartsly committed Oct 26, 2023
1 parent abf5bca commit 79b06ea
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/anonymous.scriptrunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ export class AnonymousScriptRunner extends ServiceEndpoint{
*
* @param hiveUrl
*/
async downloadFileByHiveUrl(hiveUrl: string): Promise<Buffer> {
return await this.scriptService.downloadFileByHiveUrl(hiveUrl, true);
static async downloadFileByHiveUrl(hiveUrl: string): Promise<Buffer> {
return await ScriptingService.downloadFileByHiveUrlDirect(hiveUrl);
}

/**
Expand Down
19 changes: 11 additions & 8 deletions src/service/scripting/scriptingservice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ export class ScriptingService extends RestServiceT<ScriptingAPI> {
return dataBuffer;
}

private parseHiveUrl(hiveUrl: string): HiveUrl {
private static parseHiveUrl(hiveUrl: string): HiveUrl {
if (!hiveUrl || !hiveUrl.startsWith('hive://')) {
throw new InvalidParameterException('Invalid hive url: no hive prefix');
}
Expand Down Expand Up @@ -216,28 +216,31 @@ export class ScriptingService extends RestServiceT<ScriptingAPI> {
}
}

async downloadFileByHiveUrl(hiveUrl: string): Promise<Buffer> {
return ScriptingService.downloadFileByHiveUrlDirect(hiveUrl, this.getServiceContext().getAppContext());
}

/**
* This is the compatible implementation for downloading file by the hive url
* which comes from v1 version SDK. The hive url definition is as this format:
* <br>
* hive://&lt;targetDid&gt;@&lt;targetAppDid&gt;/&lt;scriptName&gt;?params=&lt;paramJsonStr&gt;
*
* @param hiveUrl
* @param anonymous If anonymous is true, this will not compatible with v1.
* This also requires the script called has pr
* @param context AppContext, MUST provide if anonymous is false.
*/
async downloadFileByHiveUrl(hiveUrl: string, anonymous=false): Promise<Buffer> {
const params = this.parseHiveUrl(hiveUrl);
static async downloadFileByHiveUrlDirect(hiveUrl: string, context: AppContext=null): Promise<Buffer> {
const params = ScriptingService.parseHiveUrl(hiveUrl);

// Get the provider address for targetDid.
const targetUrl = await AppContext.getProviderAddressByUserDid(params.targetUsrDid, null, true);
ScriptingService.LOG.info(`Got the hive url for targetDid: ${targetUrl}`);

// Prepare the new scripting service for targetDid with current user's appContext.
const endpoint = new ServiceEndpoint(anonymous ? null : this.getServiceContext().getAppContext(), targetUrl);
const scriptingService = new ScriptingService(endpoint);
const endpoint = new ServiceEndpoint(context, targetUrl);
const scriptingService = new ScriptingService(endpoint);

// Call on the node contains targetDid vault.
// Call on the node contains targetDid vault.
const result = await scriptingService.callScriptUrl(params.scriptName, params.params, params.targetUsrDid, params.targetAppDid);
return await scriptingService.downloadFile(Object.values(result)[0]['transaction_id']);
}
Expand Down
7 changes: 6 additions & 1 deletion tests/src/scripting/scriptrunner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,12 @@ describe("test scripting runner function", () => {
// download by hive url
const paramStr = hasParams ? `?params={"path":"${FILE_NAME}"}` : '?params={}';
const hiveUrl = `hive://${targetDid}@${appDid}/${scriptName}${paramStr}`;
const buffer = await getScriptRunner(anonymous).downloadFileByHiveUrl(hiveUrl);
let buffer;
if (anonymous) {
buffer = await AnonymousScriptRunner.downloadFileByHiveUrl(hiveUrl);
} else {
buffer = await scriptRunner.downloadFileByHiveUrl(hiveUrl);
}
expectBuffersToBeEqual(Buffer.from(FILE_CONTENT), buffer);

// unregister the script.
Expand Down

0 comments on commit 79b06ea

Please sign in to comment.