Before Remote Download Worker Code Sample

JFrog Platform Administration Documentation

Content Type
Administration / Platform
ft:sourceType
Paligo

The following section provides a sample code for a Before Remote Download worker.

export default async (context: PlatformContext, data: BeforeRemoteDownloadRequest): Promise<BeforeRemoteDownloadResponse> => {
    let status: ActionStatus = ActionStatus.UNSPECIFIED;
    let requestHeaders: { [key: string]: Header } = {};
    try {
        // The in-browser HTTP client facilitates making calls to the JFrog REST APIs
        //To call an external endpoint, use 'await context.clients.axios.get("https://foo.com")'
        const res = await context.clients.platformHttp.get('/artifactory/api/v1/system/readiness');

        // You should reach this part if the HTTP request status is successful (HTTP Status 399 or lower)
        if (res.status === 200) {
            status = ActionStatus.PROCEED;
            requestHeaders['Content-Type'] = { value: ['text/plain'] };
            console.log("Artifactory ping success");
        } else {
            status = ActionStatus.WARN;
            console.warn(`Request is successful but returned status other than 200. Status code : ${res.status}`);
        }
    } catch(error) {
        // The platformHttp client throws PlatformHttpClientError if the HTTP request status is 400 or higher
        status = ActionStatus.STOP;
        console.error(`Request failed with status code ${error.status || '<none>'} caused by : ${error.message}`)
    }

    return {
        message: "proceed",
        status: ActionStatus.PROCEED,
        requestHeaders,
    }
}

Input Parameters

context

Provides baseUrl, token, and clients to communicate with the JFrog Platform (for more information, see PlatformContext).

data

The request with download details sent by Artifactory.

{
  "metadata": {  // Object containing metadata information about the artifact
    "repoPath": {  // Current repository path information for the artifact
      "key": "local-repo",  // Unique key identifier for the repository
      "path": "folder/subfolder/my-file",  // Current path to the specific file within the repository
      "id": "local-repo:folder/subfolder/my-file",  // Unique identifier combining the repository key and path
      "isRoot": false,  // Indicates if the path is a root directory (false means it is nested)
      "isFolder": false  // Indicates if the path is a folder (false means it is a file)
    },
    "originalRepoPath": {  // Information about the original path of the artifact before any modifications
      "key": "local-repo",  // Unique key identifier for the original repository
      "path": "old/folder/subfolder/my-file",  // Previous path to the file before modification
      "id": "local-repo:old/folder/subfolder/my-file",  // Unique identifier for the original path
      "isRoot": false,  // Indicates if the original path is a root directory (false means it is nested)
      "isFolder": false  // Indicates if the original path is a folder (false means it is a file)
    },
    "name": "my-file",  // Name of the file being referenced
    "headOnly": false,  // Indicates if only the headers should be processed (false means full request will be processed)
    "checksum": false,  // Indicates whether a checksum should be calculated for the file
    "recursive": false,  // Indicates if the operation should be recursive (false means it operates only on this file)
    "modificationTime": 0,  // Timestamp of the last modification (0 indicates the file has not been modified)
    "directoryRequest": false,  // Indicates if the request is for a directory (false means it is for a file)
    "metadata": false,  // Indicates if metadata should be included in the request (false means it will not)
    "lastModified": 1,  // Timestamp of the last modification (assuming it is a Unix timestamp)
    "ifModifiedSince": 0,  // Timestamp to check if the file has been modified since this time (0 means no check)
    "servletContextUrl": "https://jpd.jfrog.io/artifactory",  // URL for accessing the servlet context in Artifactory
    "uri": "/artifactory/local-repo/folder/subfolder/my-file",  // URI for accessing the artifact in the repository
    "clientAddress": "100.100.100.100",  // IP address of the client making the request
    "zipResourcePath": "",  // Path to a ZIP resource if applicable (empty indicates none)
    "zipResourceRequest": false,  // Indicates if the request involves a ZIP resource
    "replaceHeadRequestWithGet": false,  // Indicates if HEAD requests should be replaced with GET requests
    "repoType": 1  // Numeric identifier representing the type of repository (e.g., local = 1)
  },
  "userContext": {  // Object containing information about the user making the request
    "id": "jffe@00xxxxxxxxxxxxxxxxxxxxxxxx/users/bob",  // Unique identifier for the user
    "isToken": true,  // Indicates if the user is authenticated using a token (true means they are)
    "realm": "realm"  // Realm for user authentication context
  },
  "headers": {  // Object containing HTTP headers associated with the request
    "Content-Type": {  // Content-Type header indicating the type of data being sent
      "value": [  // Array of values for the Content-Type header
        "text/plain"  // Indicates that the content type is plain text
      ]
    },
    "Accept": {  // Accept header indicating the formats that can be accepted in the response
      "value": [  // Array of values for the Accept header
        "application/json"  // Indicates that the expected response format is JSON
      ]
    }
  }
}
Response
{
  "data": {
    "message": "proceed", // Message to print to the log. In case of an error, it will be printed as a warning.
    "status": 1, // The instruction of how to proceed
    "requestHeaders": { // The additional request headers to sent
      "Content-Type": {
        "value": [
          "text/plain"
        ]
      }
    }
  },
  "executionStatus": "STATUS_SUCCESS"
}
  • message and status : These are mandatory fields.

  • requestHeaders: This is an optional field.

Possible Statuses
  • ActionStatus.PROCEED - The worker allows Artifactory to proceed with downloading an artifact from the remote.

  • ActionStatus.STOP - The worker does not allow Artifactory to download an artifact from the remote.

  • ActionStatus.WARN - The worker provides a warning before Artifactory can proceed with downloading an artifact from the remote.