-
-
Notifications
You must be signed in to change notification settings - Fork 139
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #362 from ArcticLampyrid/ArcticLampyrid/issue303
Add partialUpdateFileContents method
- Loading branch information
Showing
7 changed files
with
261 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { joinURL } from "../tools/url.js"; | ||
import { encodePath } from "../tools/path.js"; | ||
import { request, prepareRequestOptions } from "../request.js"; | ||
import { handleResponseCode } from "../response.js"; | ||
import { | ||
DAVCompliance, | ||
WebDAVClientContext, | ||
WebDAVClientError, | ||
WebDAVMethodOptions | ||
} from "../types.js"; | ||
|
||
export async function getDAVCompliance( | ||
context: WebDAVClientContext, | ||
filePath: string, | ||
options: WebDAVMethodOptions = {} | ||
): Promise<DAVCompliance> { | ||
const requestOptions = prepareRequestOptions( | ||
{ | ||
url: joinURL(context.remoteURL, encodePath(filePath)), | ||
method: "OPTIONS" | ||
}, | ||
context, | ||
options | ||
); | ||
const response = await request(requestOptions); | ||
try { | ||
handleResponseCode(context, response); | ||
} catch (err) { | ||
const error = err as WebDAVClientError; | ||
throw error; | ||
} | ||
const davHeader = response.headers.get("DAV") ?? ""; | ||
const compliance = davHeader.split(",").map(item => item.trim()); | ||
const server = response.headers.get("Server") ?? ""; | ||
return { | ||
compliance, | ||
server | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
import { Readable } from "node:stream"; | ||
import { Layerr } from "layerr"; | ||
import { joinURL } from "../tools/url.js"; | ||
import { encodePath } from "../tools/path.js"; | ||
import { request, prepareRequestOptions } from "../request.js"; | ||
import { handleResponseCode } from "../response.js"; | ||
import { getDAVCompliance } from "./getDAVCompliance.js"; | ||
import { | ||
BufferLike, | ||
ErrorCode, | ||
Headers, | ||
WebDAVMethodOptions, | ||
WebDAVClientContext, | ||
WebDAVClientError | ||
} from "../types.js"; | ||
|
||
export async function partialUpdateFileContents( | ||
context: WebDAVClientContext, | ||
filePath: string, | ||
start: number | null, | ||
end: number | null, | ||
data: string | BufferLike | Readable, | ||
options: WebDAVMethodOptions = {} | ||
): Promise<void> { | ||
const compliance = await getDAVCompliance(context, filePath, options); | ||
if (compliance.compliance.includes("sabredav-partialupdate")) { | ||
return await partialUpdateFileContentsSabredav( | ||
context, | ||
filePath, | ||
start, | ||
end, | ||
data, | ||
options | ||
); | ||
} | ||
if ( | ||
compliance.server.includes("Apache") && | ||
compliance.compliance.includes("<http://apache.org/dav/propset/fs/1>") | ||
) { | ||
return await partialUpdateFileContentsApache(context, filePath, start, end, data, options); | ||
} | ||
throw new Layerr( | ||
{ | ||
info: { | ||
code: ErrorCode.NotSupported | ||
} | ||
}, | ||
"Not supported" | ||
); | ||
} | ||
|
||
async function partialUpdateFileContentsSabredav( | ||
context: WebDAVClientContext, | ||
filePath: string, | ||
start: number, | ||
end: number, | ||
data: string | BufferLike | Readable, | ||
options: WebDAVMethodOptions = {} | ||
): Promise<void> { | ||
if (start > end || start < 0) { | ||
// Actually, SabreDAV support negative start value, | ||
// Do not support here for compatibility with Apache-style way | ||
throw new Layerr( | ||
{ | ||
info: { | ||
code: ErrorCode.InvalidUpdateRange | ||
} | ||
}, | ||
`Invalid update range ${start} for partial update` | ||
); | ||
} | ||
const headers: Headers = { | ||
"Content-Type": "application/x-sabredav-partialupdate", | ||
"Content-Length": `${end - start + 1}`, | ||
"X-Update-Range": `bytes=${start}-${end}` | ||
}; | ||
const requestOptions = prepareRequestOptions( | ||
{ | ||
url: joinURL(context.remoteURL, encodePath(filePath)), | ||
method: "PATCH", | ||
headers, | ||
data | ||
}, | ||
context, | ||
options | ||
); | ||
const response = await request(requestOptions); | ||
handleResponseCode(context, response); | ||
} | ||
|
||
async function partialUpdateFileContentsApache( | ||
context: WebDAVClientContext, | ||
filePath: string, | ||
start: number, | ||
end: number, | ||
data: string | BufferLike | Readable, | ||
options: WebDAVMethodOptions = {} | ||
): Promise<void> { | ||
if (start > end || start < 0) { | ||
throw new Layerr( | ||
{ | ||
info: { | ||
code: ErrorCode.InvalidUpdateRange | ||
} | ||
}, | ||
`Invalid update range ${start} for partial update` | ||
); | ||
} | ||
const headers: Headers = { | ||
"Content-Type": "application/octet-stream", | ||
"Content-Length": `${end - start + 1}`, | ||
"Content-Range": `bytes ${start}-${end}/*` | ||
}; | ||
const requestOptions = prepareRequestOptions( | ||
{ | ||
url: joinURL(context.remoteURL, encodePath(filePath)), | ||
method: "PUT", | ||
headers, | ||
data | ||
}, | ||
context, | ||
options | ||
); | ||
const response = await request(requestOptions); | ||
handleResponseCode(context, response); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import path from "path"; | ||
import { fileURLToPath } from "url"; | ||
import fileExists from "exists-file"; | ||
import directoryExists from "directory-exists"; | ||
import { expect } from "chai"; | ||
import { | ||
SERVER_PASSWORD, | ||
SERVER_PORT, | ||
SERVER_USERNAME, | ||
clean, | ||
createWebDAVClient, | ||
createWebDAVServer, | ||
restoreRequests, | ||
useRequestSpy | ||
} from "../../helpers.node.js"; | ||
|
||
const dirname = path.dirname(fileURLToPath(import.meta.url)); | ||
|
||
const TEST_CONTENTS = path.resolve(dirname, "../../testContents"); | ||
|
||
describe("partialUpdateFileContents", function () { | ||
beforeEach(function () { | ||
this.client = createWebDAVClient(`http://localhost:${SERVER_PORT}/webdav/server`, { | ||
username: SERVER_USERNAME, | ||
password: SERVER_PASSWORD | ||
}); | ||
clean(); | ||
this.server = createWebDAVServer(); | ||
this.requestSpy = useRequestSpy(); | ||
return this.server.start(); | ||
}); | ||
|
||
afterEach(function () { | ||
restoreRequests(); | ||
return this.server.stop(); | ||
}); | ||
|
||
it("partial update should be failed with server support", async function () { | ||
let err: Error | null = null; | ||
try { | ||
// the server does not support partial update | ||
await this.client.partialUpdateFileContents("/patch.bin", 1, 3, "foo"); | ||
} catch (error) { | ||
err = error; | ||
} | ||
expect(err).to.be.an.instanceof(Error); | ||
expect(fileExists.sync(path.join(TEST_CONTENTS, "./patch.bin"))).to.be.false; | ||
}); | ||
}); |