Skip to content

Commit

Permalink
Treat ETags as opaque.
Browse files Browse the repository at this point in the history
Previously assumed it was the _rev
  • Loading branch information
awlayton committed Mar 23, 2022
1 parent c4e82f8 commit 943fa5a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
18 changes: 12 additions & 6 deletions lib/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
createNestedObject,
fixError,
getObjectAtPath,
toArray,
toArrayPath,
toStringPath,
} from './utils';
Expand Down Expand Up @@ -188,7 +189,8 @@ export interface PUTRequest {
path: string;
data: Body;
contentType?: string;
revIfMatch?: number; // If-match
/** If-Match */
etagIfMatch?: string | readonly string[];
tree?: Record<string, unknown>;
timeout?: number;
}
Expand Down Expand Up @@ -729,7 +731,7 @@ export class OADAClient {
contentType,
data: linkObject,
// Ensure the resource has not been modified (opportunistic lock)
revIfMatch: resourceCheckResult.rev,
etagIfMatch: resourceCheckResult.etag,
});
}

Expand Down Expand Up @@ -834,14 +836,15 @@ export class OADAClient {
}

const contentType = await this.#guessContentType(request, pathArray);
const etag = request.etagIfMatch && toArray(request.etagIfMatch);
const [response] = await this.#connection.request(
{
method: 'put',
headers: {
'authorization': `Bearer ${this.#token}`,
'content-type': contentType,
...(request.revIfMatch && {
'if-match': request.revIfMatch.toString(),
...(etag && {
'if-match': etag.join(', '),
}), // Add if-match header if revIfMatch is provided
},
path: request.path,
Expand Down Expand Up @@ -1054,7 +1057,7 @@ export class OADAClient {
*/
async #resourceExists(
path: string
): Promise<{ exist: boolean; rev?: number }> {
): Promise<{ exist: boolean; etag?: string }> {
// In tree put to /resources, the top-level "/resources" should
// look like it exists, even though oada doesn't allow GET on /resources
// directly.
Expand All @@ -1069,7 +1072,10 @@ export class OADAClient {
});
// Check status value
if (headResponse.status === 200) {
return { exist: true, rev: Number(headResponse.headers['x-oada-rev']) };
return {
exist: true,
etag: headResponse.headers.etag,
};
}

if (headResponse.status === 404) {
Expand Down
13 changes: 13 additions & 0 deletions lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,19 @@

import type { OADATree } from './client';

// Typescript sucks at figuring out Array.isArray on its own
function isArray<A extends unknown[] | readonly unknown[]>(
value: unknown
): value is A {
return Array.isArray(value);
}

export function toArray<E extends unknown[] | readonly unknown[]>(
itemOrArray: E | E[0]
): E {
return isArray(itemOrArray) ? itemOrArray : ([itemOrArray] as E);
}

export function toStringPath(path: readonly string[]): string {
return `/${path.join('/')}`;
}
Expand Down

0 comments on commit 943fa5a

Please sign in to comment.