Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

release: Amplify JS release #13690

Merged
merged 6 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/aws-amplify/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@
"name": "[Storage] getUrl (S3)",
"path": "./dist/esm/storage/index.mjs",
"import": "{ getUrl }",
"limit": "15.54 kB"
"limit": "15.64 kB"
},
{
"name": "[Storage] list (S3)",
Expand All @@ -497,7 +497,7 @@
"name": "[Storage] uploadData (S3)",
"path": "./dist/esm/storage/index.mjs",
"import": "{ uploadData }",
"limit": "19.64 kB"
"limit": "19.66 kB"
}
]
}
58 changes: 58 additions & 0 deletions packages/core/__tests__/singleton/Auth/type.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { JWT } from '../../../src/singleton/Auth/types';

describe('type validity', () => {
describe('JWT type', () => {
it('can contain property that has a value as array of JsonObjects', () => {
type OtherProperty1 = (
| { key: string }
| number
| string
| (
| { key: string }
| number
| string
| ({ key: string } | number | string)[]
)[]
)[];
// For testing purpose, use type alias here, as TS will complain while using
// an interface which triggers structural typing check
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
type OtherProperty2 = {
key: number;
array: (
| { key: string }
| number
| string
| ({ key: string } | number | string)[]
)[];
};
const expectedOtherProperty1 = [
{ key: '123' },
1,
'hi',
[1, 'hi', { key: '345' }, [2, 'hi', { key: '456' }]],
];
const expectedOtherProperty2 = {
key: 1,
array: [1, 'hi', { key: '123' }, [2, 'hi', { key: '456' }]],
};
const value: JWT = {
payload: {
otherProperty1: expectedOtherProperty1,
otherProperty2: expectedOtherProperty2,
},
toString: () => 'mock',
};

const extractedOtherProperty1 = value.payload
.otherProperty1 as OtherProperty1;
const a: OtherProperty1 = extractedOtherProperty1;
expect(a).toEqual(expectedOtherProperty1);

const extractedOtherProperty2 = value.payload
.otherProperty2 as OtherProperty2;
const b: OtherProperty2 = extractedOtherProperty2;
expect(b).toEqual(expectedOtherProperty2);
});
});
});
2 changes: 1 addition & 1 deletion packages/core/src/singleton/Auth/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ interface JwtPayloadStandardFields {
type JsonPrimitive = null | string | number | boolean;

/** JSON array type */
type JsonArray = JsonPrimitive[];
type JsonArray = (JsonPrimitive | JsonObject | JsonArray)[];

/** JSON Object type */
interface JsonObject {
Expand Down
130 changes: 130 additions & 0 deletions packages/storage/__tests__/providers/s3/apis/getUrl.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,136 @@ describe('getUrl test with path', () => {
},
);
});
describe('Happy cases: With path and Content Disposition, Content Type', () => {
const config = {
credentials,
region,
userAgentValue: expect.any(String),
};
beforeEach(() => {
jest.mocked(headObject).mockResolvedValue({
ContentLength: 100,
ContentType: 'text/plain',
ETag: 'etag',
LastModified: new Date('01-01-1980'),
Metadata: { meta: 'value' },
$metadata: {} as any,
});
jest.mocked(getPresignedGetObjectUrl).mockResolvedValue(mockURL);
});
afterEach(() => {
jest.clearAllMocks();
});

test.each([
{
path: 'path',
expectedKey: 'path',
contentDisposition: 'inline; filename="example.txt"',
contentType: 'text/plain',
},
{
path: () => 'path',
expectedKey: 'path',
contentDisposition: {
type: 'attachment' as const,
filename: 'example.pdf',
},
contentType: 'application/pdf',
},
])(
'should getUrl with path $path and expectedKey $expectedKey and content disposition and content type',
async ({ path, expectedKey, contentDisposition, contentType }) => {
const headObjectOptions = {
Bucket: bucket,
Key: expectedKey,
};
const { url, expiresAt } = await getUrlWrapper({
path,
options: {
validateObjectExistence: true,
contentDisposition,
contentType,
},
});
expect(getPresignedGetObjectUrl).toHaveBeenCalledTimes(1);
expect(headObject).toHaveBeenCalledTimes(1);
await expect(headObject).toBeLastCalledWithConfigAndInput(
config,
headObjectOptions,
);
expect({ url, expiresAt }).toEqual({
url: mockURL,
expiresAt: expect.any(Date),
});
},
);
});
describe('Error cases: With invalid Content Disposition', () => {
const config = {
credentials,
region,
userAgentValue: expect.any(String),
};
beforeEach(() => {
jest.mocked(headObject).mockResolvedValue({
ContentLength: 100,
ContentType: 'text/plain',
ETag: 'etag',
LastModified: new Date('01-01-1980'),
Metadata: { meta: 'value' },
$metadata: {} as any,
});
jest.mocked(getPresignedGetObjectUrl).mockResolvedValue(mockURL);
});

afterEach(() => {
jest.clearAllMocks();
});

test.each([
{
path: 'path',
expectedKey: 'path',
contentDisposition: {
type: 'invalid' as 'attachment' | 'inline',
filename: '"example.txt',
},
},
{
path: 'path',
expectedKey: 'path',
contentDisposition: {
type: 'invalid' as 'attachment' | 'inline',
},
},
])(
'should ignore for invalid content disposition: $contentDisposition',
async ({ path, expectedKey, contentDisposition }) => {
const headObjectOptions = {
Bucket: bucket,
Key: expectedKey,
};
const { url, expiresAt } = await getUrlWrapper({
path,
options: {
validateObjectExistence: true,
contentDisposition,
},
});
expect(getPresignedGetObjectUrl).toHaveBeenCalledTimes(1);
expect(headObject).toHaveBeenCalledTimes(1);
await expect(headObject).toBeLastCalledWithConfigAndInput(
config,
headObjectOptions,
);
expect({ url, expiresAt }).toEqual({
url: mockURL,
expiresAt: expect.any(Date),
});
},
);
});
describe('Error cases : With path', () => {
afterAll(() => {
jest.clearAllMocks();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,44 @@ describe('serializeGetObjectRequest', () => {
}),
);
});

it('should return get object API request with disposition and content type', async () => {
const actual = await getPresignedGetObjectUrl(
{
...defaultConfigWithStaticCredentials,
signingRegion: defaultConfigWithStaticCredentials.region,
signingService: 's3',
expiration: 900,
userAgentValue: 'UA',
},
{
Bucket: 'bucket',
Key: 'key',
ResponseContentDisposition: 'attachment; filename="filename.jpg"',
ResponseContentType: 'application/pdf',
},
);

expect(actual).toEqual(
expect.objectContaining({
hostname: `bucket.s3.${defaultConfigWithStaticCredentials.region}.amazonaws.com`,
pathname: '/key',
searchParams: expect.objectContaining({
get: expect.any(Function),
}),
}),
);

expect(actual.searchParams.get('X-Amz-Expires')).toBe('900');
expect(actual.searchParams.get('x-amz-content-sha256')).toEqual(
expect.any(String),
);
expect(actual.searchParams.get('response-content-disposition')).toBe(
'attachment; filename="filename.jpg"',
);
expect(actual.searchParams.get('response-content-type')).toBe(
'application/pdf',
);
expect(actual.searchParams.get('x-amz-user-agent')).toBe('UA');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { constructContentDisposition } from '../../../../src/providers/s3/utils/constructContentDisposition';
import { ContentDisposition } from '../../../../src/providers/s3/types/options';

describe('constructContentDisposition', () => {
it('should return undefined when input is undefined', () => {
expect(constructContentDisposition(undefined)).toBeUndefined();
});

it('should return the input string when given a string', () => {
const input = 'inline; filename="example.txt"';
expect(constructContentDisposition(input)).toBe(input);
});

it('should construct disposition string with filename when given an object with type and filename', () => {
const input: ContentDisposition = {
type: 'attachment',
filename: 'example.pdf',
};
expect(constructContentDisposition(input)).toBe(
'attachment; filename="example.pdf"',
);
});

it('should return only the type when given an object with type but no filename', () => {
const input: ContentDisposition = {
type: 'inline',
};
expect(constructContentDisposition(input)).toBe('inline');
});

it('should handle empty string filename', () => {
const input: ContentDisposition = {
type: 'attachment',
filename: '',
};
expect(constructContentDisposition(input)).toBe('attachment; filename=""');
});

it('should handle filenames with spaces', () => {
const input: ContentDisposition = {
type: 'attachment',
filename: 'my file.txt',
};
expect(constructContentDisposition(input)).toBe(
'attachment; filename="my file.txt"',
);
});

it('should handle filenames with special characters', () => {
const input: ContentDisposition = {
type: 'attachment',
filename: 'file"with"quotes.txt',
};
expect(constructContentDisposition(input)).toBe(
'attachment; filename="file"with"quotes.txt"',
);
});

// Edge cases
it('should return undefined for null input', () => {
expect(constructContentDisposition(null as any)).toBeUndefined();
});

it('should return undefined for number input', () => {
expect(constructContentDisposition(123 as any)).toBeUndefined();
});
});
9 changes: 9 additions & 0 deletions packages/storage/src/providers/s3/apis/internal/getUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
MAX_URL_EXPIRATION,
STORAGE_INPUT_KEY,
} from '../../utils/constants';
import { constructContentDisposition } from '../../utils/constructContentDisposition';

import { getProperties } from './getProperties';

Expand Down Expand Up @@ -74,6 +75,14 @@ export const getUrl = async (
{
Bucket: bucket,
Key: finalKey,
...(getUrlOptions?.contentDisposition && {
ResponseContentDisposition: constructContentDisposition(
getUrlOptions.contentDisposition,
),
}),
...(getUrlOptions?.contentType && {
ResponseContentType: getUrlOptions.contentType,
}),
},
),
expiresAt: new Date(Date.now() + urlExpirationInSec * 1000),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@

import { StorageAccessLevel } from '@aws-amplify/core';

import { ResolvedS3Config } from '../../../types/options';
import { ContentDisposition, ResolvedS3Config } from '../../../types/options';
import { StorageUploadDataPayload } from '../../../../../types';
import { Part, createMultipartUpload } from '../../../utils/client';
import { logger } from '../../../../../utils';
import { constructContentDisposition } from '../../../utils/constructContentDisposition';

import {
cacheMultipartUpload,
Expand All @@ -22,7 +23,7 @@ interface LoadOrCreateMultipartUploadOptions {
keyPrefix?: string;
key: string;
contentType?: string;
contentDisposition?: string;
contentDisposition?: string | ContentDisposition;
contentEncoding?: string;
metadata?: Record<string, string>;
size?: number;
Expand Down Expand Up @@ -102,7 +103,7 @@ export const loadOrCreateMultipartUpload = async ({
Bucket: bucket,
Key: finalKey,
ContentType: contentType,
ContentDisposition: contentDisposition,
ContentDisposition: constructContentDisposition(contentDisposition),
ContentEncoding: contentEncoding,
Metadata: metadata,
},
Expand Down
Loading
Loading