MainframeOS / erebos

JavaScript client and CLI for Swarm

Home Page:https://erebos.js.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Method for updating resource at <manifest>/<path>

kyriediculous opened this issue · comments

Hi,

There's currently no method in the bzz-api for updating a resource in an existing manifest at a certain path.

According to the docs this can be done by uploading a file to a url with the manifest and path locator options.

From the HTTP API docs:

Locator: bzz:/<manifest_hash?>/<resource_path?>/<encrypt?>

Locator Parts:

  • manifest hash - optional - an existing manifest address to update a resource included in the manifest.
  • resource path - optional - which resource to update in the manifest. encrypt - optional flag to enable encryption

I wrote this but don't have time to look into it further at this point. At first glance, it should work.

 updateManifest (
  hash: string,
  path: string,
  data: string | Buffer,
  options?: UploadOptions = {},
): Promise<hexValue> {

  const body = typeof data === 'string' ? Buffer.from(data) : data
    const raw = options.contentType == null

    if (options.headers == null) {
      options.headers = {}
    }
    options.headers['content-length'] = body.length
    if (
      options.headers != null &&
      options.headers['content-type'] == null &&
      !raw
    ) {
      options.headers['content-type'] = options.contentType
    }
    const url = this.getUploadURL({
      manifestHash: hash,
      path
    })

    return this._fetchTimeout(url, options, {method: 'POST' })

}

Hello,
It is already possible to achieve this by providing an existing manifestHash and the destination path in the upload options when calling one of the upload methods.

Cool, thank you for the blazingly fast response !

You're welcome!

Forgive me but I do see the manifesthash option, but isn't the defaultPath option to set which path resolves automatically when accessing the manifest through bzz:/ ?

Yes the defaultPath option is only relevant when using uploadDirectory(), but the UploadOptions extend the FileOptions, where the path field is declared.
The docs might be confusing in that regard if you're not familiar with Flow, I'll update these at some point.