NodeJS Cloudflare Stream TUS upload example broken

The NodeJS cloudflare stream TUS upload example on https://developers.cloudflare.com/stream/uploading-videos/upload-video-file is broken.

As of Friday (2020-12-04), or a few days earlier, the response from your TUS server (on a successful upload) has “?tusv2=true” appended to the returned media url.

The line below, in particular, will cause problems if the variable “mediaId” is used to create signed urls.

var mediaId = upload.url.substr(index)

Before you updated your TUS server, the returned upload.url would not contain “?tusv2=true” and the code would work. If you’d try this code today, youd get “?tusv2=true” appended to the mediaId string.

In order for your example code to work, you need to trim away “?tusv2=true” from upload.url.

Adding these lines below “var mediaId” fixes the problem.

var index2 = mediaId.indexOf("?");  
if (index2 > -1){
   mediaId = mediaId.substr(0, index2);
}

Docs show the correct way to get the video ID here: https://developers.cloudflare.com/stream/uploading-videos/upload-video-file#getting-the-video-id-when-using-tus

When an initial TUS request is made, Stream responds with a URL in the location header. While this URL may contain the video ID, it is not recommend to parse this URL to get the ID.

Instead, the stream-media-id HTTP header in the response should be used to retrieve the video ID.

For example, a request made to https://api.cloudflare.com/client/v4/accounts/$ACCOUNT/stream with the TUS protocol, the response will contain a HTTP header like this:

stream-media-id: cab807e0c477d01baq20f66c3d1dfc26cf

I do see how the docs and the node.js example contradict each other. I’ll make sure we fix the node.js example.

1 Like

Made a pull request to the docs to update the node.js example: https://github.com/cloudflare/cloudflare-docs/pull/445/files. Let us know if this works for you!

1 Like

Thanks!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.