Skip to content

Commit

Permalink
also implement in copy
Browse files Browse the repository at this point in the history
  • Loading branch information
vvo committed Sep 12, 2024
1 parent bb4958d commit cdc40b7
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 14 deletions.
6 changes: 6 additions & 0 deletions packages/blob/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ import { debug } from './debug';
import type { BlobCommandOptions } from './helpers';
import { BlobError, getTokenFromOptionsOrEnv } from './helpers';

// maximum pathname length is:
// 1024 (provider limit) - 26 chars (vercel internal suffixes) - 31 chars (blob `-randomId` suffix) = 967
// we round it to 950 to make it more human friendly, and we apply the limit whatever the value of
// addRandomSuffix is, to make it consistent
export const MAXIMUM_PATHNAME_LENGTH = 950;

export class BlobAccessError extends BlobError {
constructor() {
super('Access denied, please provide a valid token for this resource.');
Expand Down
8 changes: 7 additions & 1 deletion packages/blob/src/copy.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { requestApi } from './api';
import { MAXIMUM_PATHNAME_LENGTH, requestApi } from './api';
import type { CommonCreateBlobOptions } from './helpers';
import { BlobError } from './helpers';

Expand Down Expand Up @@ -35,6 +35,12 @@ export async function copy(
throw new BlobError('access must be "public"');
}

if (toPathname.length > MAXIMUM_PATHNAME_LENGTH) {
throw new BlobError(
`pathname is too long, maximum length is ${MAXIMUM_PATHNAME_LENGTH}`,
);
}

const headers: Record<string, string> = {};

if (options.addRandomSuffix !== undefined) {
Expand Down
19 changes: 12 additions & 7 deletions packages/blob/src/index.node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -577,13 +577,6 @@ describe('blob client', () => {
});

it('throws when filepath is too long', async () => {
mockClient
.intercept({
path: () => true,
method: 'PUT',
})
.reply(200, mockedFileMetaPut);

await expect(
put('a'.repeat(951), 'Test Body', {
access: 'public',
Expand Down Expand Up @@ -718,4 +711,16 @@ describe('blob client', () => {
);
});
});

describe('copy', () => {
it('throws when filepath is too long', async () => {
await expect(
copy('source', 'a'.repeat(951), {
access: 'public',
}),
).rejects.toThrow(
new Error('Vercel Blob: pathname is too long, maximum length is 950'),
);
});
});
});
7 changes: 1 addition & 6 deletions packages/blob/src/put-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { Readable } from 'stream';
import type { ClientCommonCreateBlobOptions } from './client';
import type { CommonCreateBlobOptions } from './helpers';
import { BlobError } from './helpers';
import { MAXIMUM_PATHNAME_LENGTH } from './api';

const putOptionHeaderMap = {
cacheControlMaxAge: 'x-cache-control-max-age',
Expand Down Expand Up @@ -68,12 +69,6 @@ export function createPutHeaders<TOptions extends CommonPutCommandOptions>(
return headers;
}

// maximum pathname length is:
// 1024 (provider limit) - 26 chars (vercel internal suffixes) - 31 chars (blob `-randomId` suffix) = 967
// we round it to 950 to make it more human friendly, and we apply the limit whatever the value of
// addRandomSuffix is, to make it consistent
const MAXIMUM_PATHNAME_LENGTH = 950;

export async function createPutOptions<
TOptions extends CommonPutCommandOptions,
>({
Expand Down

0 comments on commit cdc40b7

Please sign in to comment.