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

Add webresource vocab & begin/commitUpload actions #754

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
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
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@
"typed-error": "^3.2.2"
},
"devDependencies": {
"@aws-sdk/client-s3": "^3.556.0",
"@balena/lint": "^8.0.0",
"@balena/pinejs-webresource-s3": "^0.2.0",
"@faker-js/faker": "^8.3.1",
"@types/busboy": "^1.5.3",
"@types/chai": "^4.3.11",
Expand Down Expand Up @@ -106,9 +108,6 @@
"webpack-dev-server": "^4.15.1"
},
"optionalDependencies": {
"@aws-sdk/client-s3": "^3.490.0",
"@aws-sdk/lib-storage": "^3.490.0",
"@aws-sdk/s3-request-presigner": "^3.490.0",
"bcrypt": "^5.1.1",
"body-parser": "^1.20.2",
"compression": "^1.7.4",
Expand All @@ -123,7 +122,7 @@
"serve-static": "^1.15.0"
},
"engines": {
"node": ">=16.13.0",
"node": ">=18.18.0",
"npm": ">=8.0.0"
},
"lint-staged": {
Expand Down
2 changes: 2 additions & 0 deletions src/server-glue/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import './sbvr-loader';
import * as dbModule from '../database-layer/db';
import * as configLoader from '../config-loader/config-loader';
import * as migrator from '../migrator/sync';
import * as webResourceHandler from '../webresource-handler';
import type * as migratorUtils from '../migrator/utils';

import * as sbvrUtils from '../sbvr-api/sbvr-utils';
Expand Down Expand Up @@ -63,6 +64,7 @@ export const init = async <T extends string>(
await sbvrUtils.setup(app, db);
const cfgLoader = await configLoader.setup(app);
await cfgLoader.loadConfig(migrator.config);
await cfgLoader.loadConfig(webResourceHandler.config);

const promises: Array<Promise<void>> = [];
if (process.env.SBVR_SERVER_ENABLED) {
Expand Down
15 changes: 14 additions & 1 deletion src/webresource-handler/handlers/NoopHandler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import type { WebResourceType as WebResource } from '@balena/sbvr-types';
import type { IncomingFile, UploadResponse, WebResourceHandler } from '..';
import type {
BeginMultipartUploadHandlerResponse,
IncomingFile,
UploadResponse,
WebResourceHandler,
} from '..';

export class NoopHandler implements WebResourceHandler {
public async handleFile(resource: IncomingFile): Promise<UploadResponse> {
Expand All @@ -18,4 +23,12 @@ export class NoopHandler implements WebResourceHandler {
public async onPreRespond(webResource: WebResource): Promise<WebResource> {
return webResource;
}

public async beginMultipartUpload(): Promise<BeginMultipartUploadHandlerResponse> {
return { fileKey: 'noop', uploadId: 'noop', uploadParts: [] };
}

public async commitMultipartUpload(): Promise<WebResource> {
return { filename: 'noop', href: 'noop' };
}
}
143 changes: 0 additions & 143 deletions src/webresource-handler/handlers/S3Handler.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/webresource-handler/handlers/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export * from './NoopHandler';
export * from './S3Handler';
84 changes: 63 additions & 21 deletions src/webresource-handler/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import {
} from '@balena/odata-to-abstract-sql';
import { errors, permissions } from '../server-glue/module';
import type { WebResourceType as WebResource } from '@balena/sbvr-types';
import { TypedError } from 'typed-error';
import type { AnyObject } from 'pinejs-client-core';
import { multipartUploadHooks } from './multipartUpload';

export * from './handlers';

Expand All @@ -30,19 +31,44 @@ export interface UploadResponse {
filename: string;
}

export interface BeginMultipartUploadPayload {
filename: string;
content_type: string;
size: number;
chunk_size: number;
}

export interface UploadPart {
url: string;
chunkSize: number;
partNumber: number;
}

export interface BeginMultipartUploadHandlerResponse {
uploadParts: UploadPart[];
fileKey: string;
uploadId: string;
}

export interface CommitMultipartUploadPayload {
fileKey: string;
uploadId: string;
filename: string;
providerCommitData?: AnyObject;
}

export interface WebResourceHandler {
handleFile: (resource: IncomingFile) => Promise<UploadResponse>;
removeFile: (fileReference: string) => Promise<void>;
onPreRespond: (webResource: WebResource) => Promise<WebResource>;
}

export class WebResourceError extends TypedError {}

export class FileSizeExceededError extends WebResourceError {
name = 'FileSizeExceededError';
constructor(maxSize: number) {
super(`File size exceeded the limit of ${maxSize} bytes.`);
}
beginMultipartUpload: (
fieldName: string,
payload: BeginMultipartUploadPayload,
) => Promise<BeginMultipartUploadHandlerResponse>;
commitMultipartUpload: (
commitInfo: CommitMultipartUploadPayload,
) => Promise<WebResource>;
}

type WebResourcesDbResponse = {
Expand Down Expand Up @@ -193,17 +219,12 @@ export const getUploaderMiddlware = (
next();
} catch (err: any) {
await clearFiles();

if (err instanceof FileSizeExceededError) {
return sbvrUtils.handleHttpErrors(
req,
res,
new errors.BadRequestError(err.message),
);
}

getLogger(getApiRoot(req)).error('Error uploading file', err);
next(err);
getLogger(getApiRoot(req)).warn('Error uploading file', err);
return sbvrUtils.handleHttpErrors(
req,
res,
new errors.BadRequestError(err),
);
}
});

Expand All @@ -216,7 +237,7 @@ export const getUploaderMiddlware = (
};
};

const getWebResourceFields = (
export const getWebResourceFields = (
request: uriParser.ODataRequest,
useTranslations = true,
): string[] => {
Expand Down Expand Up @@ -249,6 +270,8 @@ const throwIfWebresourceNotInMultipart = (
{ req, request }: HookArgs,
) => {
if (
request.custom.isAction !== 'beginUpload' &&
request.custom.isAction !== 'commitUpload' &&
!req.is?.('multipart') &&
webResourceFields.some((field) => request.values[field] != null)
) {
Expand Down Expand Up @@ -447,4 +470,23 @@ export const setupUploadHooks = (
resourceName,
getCreateWebResourceHooks(handler),
);

sbvrUtils.addPureHook(
'POST',
apiRoot,
resourceName,
multipartUploadHooks(handler),
);
};

// eslint-disable-next-line @typescript-eslint/no-var-requires
const webresourceModel: string = require('./webresource.sbvr');
export const config = {
models: [
{
apiRoot: 'webresource',
modelText: webresourceModel,
modelName: 'webresource',
},
] as sbvrUtils.ExecutableModel[],
};
Loading
Loading