Skip to content

Commit

Permalink
fix merge conflict typo
Browse files Browse the repository at this point in the history
  • Loading branch information
daryl-cecile committed Nov 11, 2023
1 parent 95871e8 commit afb709e
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions packages/blob/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const put = createPutMethod<ClientPutCommandOptions>({
extraChecks(options: ClientPutCommandOptions) {
if (typeof window === 'undefined') {
throw new BlobError(
'client/`put` must be called from a client environment',
'client/`put` must be called from a client environment'
);
}

Expand All @@ -37,7 +37,7 @@ export const put = createPutMethod<ClientPutCommandOptions>({
options.cacheControlMaxAge !== undefined
) {
throw new BlobError(
'addRandomSuffix and cacheControlMaxAge are not supported in client uploads. Configure these options at the server side when generating client tokens.',
'addRandomSuffix and cacheControlMaxAge are not supported in client uploads. Configure these options at the server side when generating client tokens.'
);
}
},
Expand Down Expand Up @@ -79,7 +79,7 @@ export const upload = createPutMethod<UploadOptions>({
extraChecks(options: UploadOptions) {
if (typeof window === 'undefined') {
throw new BlobError(
'client/`upload` must be called from a client environment',
'client/`upload` must be called from a client environment'
);
}

Expand All @@ -95,7 +95,7 @@ export const upload = createPutMethod<UploadOptions>({
options.cacheControlMaxAge !== undefined
) {
throw new BlobError(
'addRandomSuffix and cacheControlMaxAge are not supported in client uploads. Configure these options at the server side when generating client tokens.',
'addRandomSuffix and cacheControlMaxAge are not supported in client uploads. Configure these options at the server side when generating client tokens.'
);
}
},
Expand All @@ -115,13 +115,13 @@ async function importKey(token: string): Promise<CryptoKey> {
new TextEncoder().encode(token),
{ name: 'HMAC', hash: 'SHA-256' },
false,
['sign', 'verify'],
['sign', 'verify']
);
}

async function signPayload(
payload: string,
token: string,
token: string
): Promise<string | undefined> {
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -- Node.js < 20: globalThis.crypto is undefined (in a real script.js, because the REPL has it linked to the crypto module). Node.js >= 20, Browsers and Cloudflare workers: globalThis.crypto is defined and is the Web Crypto API.
if (!globalThis.crypto) {
Expand All @@ -131,7 +131,7 @@ async function signPayload(
const signature = await globalThis.crypto.subtle.sign(
'HMAC',
await importKey(token),
new TextEncoder().encode(payload),
new TextEncoder().encode(payload)
);
return Buffer.from(new Uint8Array(signature)).toString('hex');
}
Expand Down Expand Up @@ -168,7 +168,7 @@ async function verifyCallbackSignature({
'HMAC',
await importKey(token),
hexToArrayByte(signature),
new TextEncoder().encode(body),
new TextEncoder().encode(body)
);
return verified;
}
Expand All @@ -194,7 +194,7 @@ export type DecodedClientTokenPayload = Omit<
};

export function getPayloadFromClientToken(
clientToken: string,
clientToken: string
): DecodedClientTokenPayload {
const [, , , , encodedToken] = clientToken.split('_');
const encodedPayload = Buffer.from(encodedToken ?? '', 'base64')
Expand Down Expand Up @@ -229,7 +229,7 @@ export interface HandleUploadOptions {
body: HandleUploadBody;
onBeforeGenerateToken: (
pathname: string,
clientPayload?: string,
clientPayload?: string
) => Promise<
Pick<
GenerateClientTokenOptions,
Expand Down Expand Up @@ -350,6 +350,7 @@ async function tryGetErrorMessageFromResponse(res: Response) {
if ('error' in jsonBody && typeof jsonBody.error === 'string') {
return jsonBody.error;
}
}

function toAbsoluteUrl(url: string): string {
return new URL(url, window.location.href).href;
Expand All @@ -369,7 +370,7 @@ export async function generateClientTokenFromReadWriteToken({
}: GenerateClientTokenOptions): Promise<string> {
if (typeof window !== 'undefined') {
throw new BlobError(
'"generateClientTokenFromReadWriteToken" must be called from a server environment',
'"generateClientTokenFromReadWriteToken" must be called from a server environment'
);
}

Expand All @@ -381,15 +382,15 @@ export async function generateClientTokenFromReadWriteToken({

if (!storeId) {
throw new BlobError(
token ? 'Invalid `token` parameter' : 'Invalid `BLOB_READ_WRITE_TOKEN`',
token ? 'Invalid `token` parameter' : 'Invalid `BLOB_READ_WRITE_TOKEN`'
);
}

const payload = Buffer.from(
JSON.stringify({
...argsWithoutToken,
validUntil: argsWithoutToken.validUntil ?? timestamp.getTime(),
}),
})
).toString('base64');

const securedKey = await signPayload(payload, readWriteToken);
Expand All @@ -398,7 +399,7 @@ export async function generateClientTokenFromReadWriteToken({
throw new BlobError('Unable to sign client token');
}
return `vercel_blob_client_${storeId}_${Buffer.from(
`${securedKey}.${payload}`,
`${securedKey}.${payload}`
).toString('base64')}`;
}

Expand Down

0 comments on commit afb709e

Please sign in to comment.