Skip to content

Commit

Permalink
Merge branch 'main' into auto-bump-lockfile
Browse files Browse the repository at this point in the history
  • Loading branch information
deebov authored May 8, 2024
2 parents a1b9cd9 + 8a2fc09 commit e6ed557
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"ignorePatterns": ["*.json", "*.md", "*.patch", "*.toml", "*.lockb", "*.gitignore"],
"ignorePatterns": ["*.json", "*.md", "*.patch", "*.toml", "*.lockb", "*.gitignore", "*.mp4"],
"parser": "@typescript-eslint/parser",
"env": {
"node": true
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ronin",
"version": "3.0.1",
"version": "3.0.2",
"type": "module",
"license": "Apache-2.0",
"main": "dist/index.cjs",
Expand Down
2 changes: 1 addition & 1 deletion src/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export const uploadStorableObjects = async (
const fetcher = typeof options?.fetch === 'function' ? options.fetch : fetch;

const requests: Promise<StoredObject>[] = storableObjects.map(async ({ value, contentType }) => {
const request = new Request('https://data.ronin.co/writable', {
const request = new Request('https://storage.ronin.co/', {
method: 'PUT',
body: value,
headers: { 'Content-Type': contentType, Authorization: `Bearer ${options.token}` },
Expand Down
17 changes: 11 additions & 6 deletions src/types/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,17 @@ export type StoredObject = {
placeholder: {
base64: string | null;
} | null;
meta: {
width: number;
height: number;
size: number;
type: string;
};
meta:
| {
size: number;
type: string;
}
| {
size: number;
type: string;
width: number;
height: number;
};
};

export type StorableObjectValue = File | ReadableStream | Buffer;
Binary file added tests/assets/example.mp4
Binary file not shown.
56 changes: 51 additions & 5 deletions tests/integration/factory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const mockFetch = mock(async (request) => {
mockRequestResolvedValue = request;
mockResolvedRequestText = await request.text();

if (request.url === 'https://data.ronin.co/writable') {
if (request.url === 'https://storage.ronin.co/') {
return Response.json({
results: [],
});
Expand Down Expand Up @@ -324,12 +324,12 @@ describe('factory', () => {

const factory = createSyntaxFactory({
fetch: async (request) => {
if ((request as Request).url === 'https://data.ronin.co/writable') {
if ((request as Request).url === 'https://storage.ronin.co/') {
mockResolvedStorageRequest = request as Request;

const responseBody: StoredObject = {
key: 'test-key',
src: 'https://media.ronin.co/test-key',
src: 'https://storage.ronin.co/test-key',
meta: {
height: 100,
width: 100,
Expand Down Expand Up @@ -362,14 +362,60 @@ describe('factory', () => {
expect(body).toBe(await file.text());

expect(mockResolvedRequestText).toEqual(
'{"queries":[{"create":{"account":{"with":{"avatar":{"key":"test-key","src":"https://media.ronin.co/test-key","meta":{"height":100,"width":100,"size":100,"type":"image/jpeg"},"placeholder":{"base64":""}}}}}}]}',
'{"queries":[{"create":{"account":{"with":{"avatar":{"key":"test-key","src":"https://storage.ronin.co/test-key","meta":{"height":100,"width":100,"size":100,"type":"image/jpeg"},"placeholder":{"base64":""}}}}}}]}',
);
});

test('upload a video', async () => {
const bunFile = Bun.file('tests/assets/example.mp4');
const file = new File([bunFile], 'example.mp4', { type: 'video/mp4' });

let mockResolvedStorageRequest: Request | undefined = undefined;

const factory = createSyntaxFactory({
fetch: async (request) => {
if ((request as Request).url === 'https://storage.ronin.co/') {
mockResolvedStorageRequest = request as Request;

const responseBody: StoredObject = {
key: 'test-key',
src: 'https://storage.ronin.co/test-key',
meta: {
size: 100,
type: 'video/mp4',
},
placeholder: null,
};

return Response.json(responseBody);
}

return mockFetch(request);
},
});

await factory.create.account({
with: {
video: file,
},
});

const body = await (mockResolvedStorageRequest as Request | undefined)?.text();

expect((mockResolvedStorageRequest as Request | undefined)?.headers.get('Content-Type')).toBe(
'video/mp4',
);
expect(body).toBe(await file.text());

expect(mockResolvedRequestText).toEqual(
'{"queries":[{"create":{"account":{"with":{"video":{"key":"test-key","src":"https://storage.ronin.co/test-key","meta":{"size":100,"type":"video/mp4"},"placeholder":null}}}}}]}',
);
});

test('handle storage service error', async () => {
const factory = createSyntaxFactory({
fetch: async (request) => {
if ((request as Request).url === 'https://data.ronin.co/writable') {
if ((request as Request).url === 'https://storage.ronin.co/') {
return Response.error();
}
return mockFetch(request);
Expand Down

0 comments on commit e6ed557

Please sign in to comment.