Skip to content

Commit

Permalink
fix(dav): use of webdav library
Browse files Browse the repository at this point in the history
nextcloud/server#41202

Signed-off-by: Varun Patil <varunpatil@ucla.edu>
  • Loading branch information
pulsejet committed Oct 30, 2023
1 parent 5a568e2 commit a081072
Showing 1 changed file with 25 additions and 15 deletions.
40 changes: 25 additions & 15 deletions lib/dav/dav.ts
Expand Up @@ -20,7 +20,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
import type { DAVResultResponseProps, FileStat, Response, ResponseDataDetailed, WebDAVClient } from 'webdav'
import type { DAVResultResponseProps, FileStat, ResponseDataDetailed, WebDAVClient } from 'webdav'
import type { Node } from '../files/node'

import { File } from '../files/file'
Expand All @@ -29,10 +29,9 @@ import { NodeData } from '../files/nodeData'
import { davParsePermissions } from './davPermissions'
import { davGetFavoritesReport } from './davProperties'

import { getCurrentUser, getRequestToken } from '@nextcloud/auth'
import { getCurrentUser, getRequestToken, onRequestTokenUpdate } from '@nextcloud/auth'
import { generateRemoteUrl } from '@nextcloud/router'
import { createClient, getPatcher, RequestOptions } from 'webdav'
import { request } from 'webdav/dist/node/request.js'
import { createClient, getPatcher } from 'webdav'

/**
* Nextcloud DAV result response
Expand All @@ -59,28 +58,39 @@ export const davRemoteURL = generateRemoteUrl('dav')
* @param remoteURL The DAV server remote URL
*/
export const davGetClient = function(remoteURL = davRemoteURL) {
const client = createClient(remoteURL, {
headers: {
requesttoken: getRequestToken() || '',
},
})
const client = createClient(remoteURL);

Check failure on line 61 in lib/dav/dav.ts

View workflow job for this annotation

GitHub Actions / eslint

Extra semicolon

Check warning on line 61 in lib/dav/dav.ts

View check run for this annotation

Codecov / codecov/patch

lib/dav/dav.ts#L61

Added line #L61 was not covered by tests

// set CSRF token header
function setHeaders(token: string | null) {

Check warning on line 64 in lib/dav/dav.ts

View workflow job for this annotation

GitHub Actions / eslint

Missing JSDoc comment
client.setHeaders({

Check warning on line 65 in lib/dav/dav.ts

View check run for this annotation

Codecov / codecov/patch

lib/dav/dav.ts#L65

Added line #L65 was not covered by tests
// Add this so the server knows it is an request from the browser
'X-Requested-With': 'XMLHttpRequest',
// Inject user auth
requesttoken: token ?? '',
});

Check failure on line 70 in lib/dav/dav.ts

View workflow job for this annotation

GitHub Actions / eslint

Extra semicolon
}

// refresh headers when request token changes
onRequestTokenUpdate(setHeaders);

Check failure on line 74 in lib/dav/dav.ts

View workflow job for this annotation

GitHub Actions / eslint

Extra semicolon
setHeaders(getRequestToken());

Check failure on line 75 in lib/dav/dav.ts

View workflow job for this annotation

GitHub Actions / eslint

Extra semicolon

Check warning on line 75 in lib/dav/dav.ts

View check run for this annotation

Codecov / codecov/patch

lib/dav/dav.ts#L74-L75

Added lines #L74 - L75 were not covered by tests

/**
* Allow to override the METHOD to support dav REPORT
*
* @see https://github.com/perry-mitchell/webdav-client/blob/8d9694613c978ce7404e26a401c39a41f125f87f/source/request.ts
*/
const patcher = getPatcher()
// https://github.com/perry-mitchell/hot-patcher/issues/6
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
patcher.patch('request', (options: RequestOptions): Promise<Response> => {
if (options.headers?.method) {
options.method = options.headers.method
delete options.headers.method
// https://github.com/perry-mitchell/hot-patcher/issues/6
patcher.patch('fetch', (url: string, options: RequestInit): Promise<Response> => {

Check warning on line 86 in lib/dav/dav.ts

View check run for this annotation

Codecov / codecov/patch

lib/dav/dav.ts#L86

Added line #L86 was not covered by tests
if (options.headers?.['method']) {

Check failure on line 87 in lib/dav/dav.ts

View workflow job for this annotation

GitHub Actions / eslint

["method"] is better written in dot notation
options.method = options.headers['method']

Check failure on line 88 in lib/dav/dav.ts

View workflow job for this annotation

GitHub Actions / eslint

["method"] is better written in dot notation
delete options.headers['method']

Check failure on line 89 in lib/dav/dav.ts

View workflow job for this annotation

GitHub Actions / eslint

["method"] is better written in dot notation

Check warning on line 89 in lib/dav/dav.ts

View check run for this annotation

Codecov / codecov/patch

lib/dav/dav.ts#L88-L89

Added lines #L88 - L89 were not covered by tests
}
return request(options)
return fetch(url, options);

Check failure on line 91 in lib/dav/dav.ts

View workflow job for this annotation

GitHub Actions / eslint

Extra semicolon

Check warning on line 91 in lib/dav/dav.ts

View check run for this annotation

Codecov / codecov/patch

lib/dav/dav.ts#L91

Added line #L91 was not covered by tests
})

return client
}

Expand Down

0 comments on commit a081072

Please sign in to comment.