@balena/pinejs 21.4.0-build-add-odata-actions-57ea3eb16fe9ec8e28ea12b969da062ec4374a2d-1 → 21.4.0-build-add-actions-example-6e3f5d12bf1644c266f898558c5a1a25ef595f2b-1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.pinejs-cache.json +1 -1
- package/.versionbot/CHANGELOG.yml +17 -1
- package/CHANGELOG.md +2 -0
- package/out/config-loader/config-loader.js +7 -6
- package/out/config-loader/config-loader.js.map +1 -1
- package/out/config-loader/env.d.ts +4 -0
- package/out/config-loader/env.js +4 -0
- package/out/config-loader/env.js.map +1 -1
- package/out/server-glue/module.js +2 -0
- package/out/server-glue/module.js.map +1 -1
- package/out/webresource-handler/actions/beginUpload.d.ts +3 -0
- package/out/webresource-handler/actions/beginUpload.js +98 -0
- package/out/webresource-handler/actions/beginUpload.js.map +1 -0
- package/out/webresource-handler/actions/canceUpload.d.ts +3 -0
- package/out/webresource-handler/actions/canceUpload.js +58 -0
- package/out/webresource-handler/actions/canceUpload.js.map +1 -0
- package/out/webresource-handler/actions/commitUpload.d.ts +3 -0
- package/out/webresource-handler/actions/commitUpload.js +85 -0
- package/out/webresource-handler/actions/commitUpload.js.map +1 -0
- package/out/webresource-handler/index.d.ts +9 -0
- package/out/webresource-handler/index.js +31 -1
- package/out/webresource-handler/index.js.map +1 -1
- package/out/webresource-handler/multiparUpload.d.ts +4 -0
- package/out/webresource-handler/multiparUpload.js +14 -0
- package/out/webresource-handler/multiparUpload.js.map +1 -0
- package/out/webresource-handler/webresource.d.ts +42 -0
- package/out/webresource-handler/webresource.js +2 -0
- package/out/webresource-handler/webresource.js.map +1 -0
- package/out/webresource-handler/webresource.sbvr +60 -0
- package/package.json +5 -5
- package/src/config-loader/config-loader.ts +8 -8
- package/src/config-loader/env.ts +11 -0
- package/src/server-glue/module.ts +2 -0
- package/src/webresource-handler/actions/beginUpload.ts +160 -0
- package/src/webresource-handler/actions/canceUpload.ts +92 -0
- package/src/webresource-handler/actions/commitUpload.ts +117 -0
- package/src/webresource-handler/index.ts +48 -0
- package/src/webresource-handler/multiparUpload.ts +23 -0
- package/src/webresource-handler/webresource.sbvr +60 -0
- package/src/webresource-handler/webresource.ts +48 -0
@@ -0,0 +1,42 @@
|
|
1
|
+
import type { Types } from '@balena/abstract-sql-to-typescript';
|
2
|
+
export interface MultipartUpload {
|
3
|
+
Read: {
|
4
|
+
created_at: Types['Date Time']['Read'];
|
5
|
+
modified_at: Types['Date Time']['Read'];
|
6
|
+
id: Types['Serial']['Read'];
|
7
|
+
uuid: Types['Short Text']['Read'];
|
8
|
+
resource_name: Types['Short Text']['Read'];
|
9
|
+
field_name: Types['Short Text']['Read'];
|
10
|
+
resource_id: Types['Integer']['Read'];
|
11
|
+
upload_id: Types['Short Text']['Read'];
|
12
|
+
file_key: Types['Short Text']['Read'];
|
13
|
+
status: 'pending' | 'completed' | 'cancelled';
|
14
|
+
filename: Types['Short Text']['Read'];
|
15
|
+
content_type: Types['Short Text']['Read'];
|
16
|
+
size: Types['Integer']['Read'];
|
17
|
+
chunk_size: Types['Integer']['Read'];
|
18
|
+
expiry_date: Types['Date Time']['Read'];
|
19
|
+
is_created_by__actor: Types['Integer']['Read'] | null;
|
20
|
+
};
|
21
|
+
Write: {
|
22
|
+
created_at: Types['Date Time']['Write'];
|
23
|
+
modified_at: Types['Date Time']['Write'];
|
24
|
+
id: Types['Serial']['Write'];
|
25
|
+
uuid: Types['Short Text']['Write'];
|
26
|
+
resource_name: Types['Short Text']['Write'];
|
27
|
+
field_name: Types['Short Text']['Write'];
|
28
|
+
resource_id: Types['Integer']['Write'];
|
29
|
+
upload_id: Types['Short Text']['Write'];
|
30
|
+
file_key: Types['Short Text']['Write'];
|
31
|
+
status: 'pending' | 'completed' | 'cancelled';
|
32
|
+
filename: Types['Short Text']['Write'];
|
33
|
+
content_type: Types['Short Text']['Write'];
|
34
|
+
size: Types['Integer']['Write'];
|
35
|
+
chunk_size: Types['Integer']['Write'];
|
36
|
+
expiry_date: Types['Date Time']['Write'];
|
37
|
+
is_created_by__actor: Types['Integer']['Write'] | null;
|
38
|
+
};
|
39
|
+
}
|
40
|
+
export default interface $Model {
|
41
|
+
multipart_upload: MultipartUpload;
|
42
|
+
}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"webresource.js","sourceRoot":"","sources":["../../src/webresource-handler/webresource.ts"],"names":[],"mappings":""}
|
@@ -0,0 +1,60 @@
|
|
1
|
+
Vocabulary: webresource
|
2
|
+
|
3
|
+
Term: actor
|
4
|
+
Concept Type: Integer (Type)
|
5
|
+
Term: expiry date
|
6
|
+
Concept Type: Date Time (Type)
|
7
|
+
Term: uuid
|
8
|
+
Concept Type: Short Text (Type)
|
9
|
+
Term: resource name
|
10
|
+
Concept Type: Short Text (Type)
|
11
|
+
Term: field name
|
12
|
+
Concept Type: Short Text (Type)
|
13
|
+
Term: resource id
|
14
|
+
Concept Type: Integer (Type)
|
15
|
+
Term: upload id
|
16
|
+
Concept Type: Short Text (Type)
|
17
|
+
Term: file key
|
18
|
+
Concept Type: Short Text (Type)
|
19
|
+
Term: status
|
20
|
+
Concept Type: Short Text (Type)
|
21
|
+
Term: filename
|
22
|
+
Concept Type: Short Text (Type)
|
23
|
+
Term: content type
|
24
|
+
Concept Type: Short Text (Type)
|
25
|
+
Term: size
|
26
|
+
Concept Type: Integer (Type)
|
27
|
+
Term: chunk size
|
28
|
+
Concept Type: Integer (Type)
|
29
|
+
Term: valid until date
|
30
|
+
Concept Type: Date Time (Type)
|
31
|
+
|
32
|
+
Term: multipart upload
|
33
|
+
Fact type: multipart upload has uuid
|
34
|
+
Necessity: each multipart upload has exactly one uuid
|
35
|
+
Necessity: each uuid is of exactly one multipart upload
|
36
|
+
Fact type: multipart upload has resource name
|
37
|
+
Necessity: each multipart upload has exactly one resource name
|
38
|
+
Fact type: multipart upload has field name
|
39
|
+
Necessity: each multipart upload has exactly one field name
|
40
|
+
Fact type: multipart upload has resource id
|
41
|
+
Necessity: each multipart upload has exactly one resource id
|
42
|
+
Fact type: multipart upload has upload id
|
43
|
+
Necessity: each multipart upload has exactly one upload id
|
44
|
+
Fact type: multipart upload has file key
|
45
|
+
Necessity: each multipart upload has exactly one file key
|
46
|
+
Fact type: multipart upload has status
|
47
|
+
Necessity: each multipart upload has exactly one status
|
48
|
+
Definition: "pending" or "completed" or "cancelled"
|
49
|
+
Fact type: multipart upload has filename
|
50
|
+
Necessity: each multipart upload has exactly one filename
|
51
|
+
Fact type: multipart upload has content type
|
52
|
+
Necessity: each multipart upload has exactly one content type
|
53
|
+
Fact type: multipart upload has size
|
54
|
+
Necessity: each multipart upload has exactly one size
|
55
|
+
Fact type: multipart upload has chunk size
|
56
|
+
Necessity: each multipart upload has exactly one chunk size
|
57
|
+
Fact type: multipart upload has expiry date
|
58
|
+
Necessity: each multipart upload has exactly one expiry date
|
59
|
+
Fact type: multipart upload is created by actor
|
60
|
+
Necessity: each multipart upload is created by at most one actor
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@balena/pinejs",
|
3
|
-
"version": "21.4.0-build-add-
|
3
|
+
"version": "21.4.0-build-add-actions-example-6e3f5d12bf1644c266f898558c5a1a25ef595f2b-1",
|
4
4
|
"main": "out/server-glue/module.js",
|
5
5
|
"type": "module",
|
6
6
|
"repository": "git@github.com:balena-io/pinejs.git",
|
@@ -20,10 +20,10 @@
|
|
20
20
|
"webpack-build": "npm run webpack-browser && npm run webpack-module && npm run webpack-server",
|
21
21
|
"lint": "balena-lint -t tsconfig.dev.json -e js -e ts src test build typings Gruntfile.cts && npx tsc --project tsconfig.dev.json --noEmit",
|
22
22
|
"test": "npm run build && npm run lint && npm run webpack-build && npm run test:compose && npm run test:generated-types",
|
23
|
-
"test:compose": "trap 'docker compose -f docker-compose.npm-test.yml down ; echo Stopped ; exit 0' INT; docker compose -f docker-compose.npm-test.yml up -d && sleep 2 && DATABASE_URL=postgres://docker:docker@localhost:5431/postgres PINEJS_WEBRESOURCE_MAXFILESIZE=1000000000 S3_ENDPOINT=http://localhost:43680 S3_ACCESS_KEY=USERNAME S3_SECRET_KEY=PASSWORD S3_STORAGE_ADAPTER_BUCKET=balena-pine-web-resources S3_REGION=us-east-1 PINEJS_QUEUE_CONCURRENCY=1 TZ=UTC npx mocha",
|
23
|
+
"test:compose": "trap 'docker compose -f docker-compose.npm-test.yml down ; echo Stopped ; exit 0' INT; docker compose -f docker-compose.npm-test.yml up -d && sleep 2 && DATABASE_URL=postgres://docker:docker@localhost:5431/postgres PINEJS_WEBRESOURCE_MAXFILESIZE=1000000000 S3_ENDPOINT=http://localhost:43680 S3_ACCESS_KEY=USERNAME S3_SECRET_KEY=PASSWORD S3_STORAGE_ADAPTER_BUCKET=balena-pine-web-resources S3_REGION=us-east-1 PINEJS_QUEUE_CONCURRENCY=1 TZ=UTC PINEJS_WEBRESOURCE_MULTIPART_ENABLED=true npx mocha",
|
24
24
|
"test:generated-types": "npm run generate-types && git diff --exit-code ./src/sbvr-api/user.ts ./src/migrator/migrations.ts ./src/sbvr-api/dev.ts",
|
25
25
|
"lint-fix": "balena-lint -t tsconfig.dev.json -e js -e ts --fix src test build typings Gruntfile.cts",
|
26
|
-
"generate-types": "node ./bin/sbvr-compiler.js generate-types ./src/sbvr-api/user.sbvr ./src/sbvr-api/user.ts && node ./bin/sbvr-compiler.js generate-types ./src/migrator/migrations.sbvr ./src/migrator/migrations.ts && node ./bin/sbvr-compiler.js generate-types ./src/sbvr-api/dev.sbvr ./src/sbvr-api/dev.ts && node ./bin/sbvr-compiler.js generate-types ./src/tasks/tasks.sbvr ./src/tasks/tasks.ts && balena-lint -t tsconfig.dev.json --fix ./src/sbvr-api/user.ts ./src/migrator/migrations.ts ./src/sbvr-api/dev.ts"
|
26
|
+
"generate-types": "node ./bin/sbvr-compiler.js generate-types ./src/sbvr-api/user.sbvr ./src/sbvr-api/user.ts && node ./bin/sbvr-compiler.js generate-types ./src/migrator/migrations.sbvr ./src/migrator/migrations.ts && node ./bin/sbvr-compiler.js generate-types ./src/sbvr-api/dev.sbvr ./src/sbvr-api/dev.ts && node ./bin/sbvr-compiler.js generate-types ./src/tasks/tasks.sbvr ./src/tasks/tasks.ts && node ./bin/sbvr-compiler.js generate-types ./src/webresource-handler/webresource.sbvr ./src/webresource-handler/webresource.ts && balena-lint -t tsconfig.dev.json --fix ./src/sbvr-api/user.ts ./src/migrator/migrations.ts ./src/sbvr-api/dev.ts"
|
27
27
|
},
|
28
28
|
"dependencies": {
|
29
29
|
"@balena/abstract-sql-compiler": "^10.2.3",
|
@@ -70,7 +70,7 @@
|
|
70
70
|
"devDependencies": {
|
71
71
|
"@balena/lint": "^9.1.6",
|
72
72
|
"@balena/pinejs": "file:./",
|
73
|
-
"@balena/pinejs-webresource-s3": "
|
73
|
+
"@balena/pinejs-webresource-s3": "2.0.0-build-new-multiparthandle-interface-946a8ca8f89769da46d151f4fc5ba9480e81a2be-3",
|
74
74
|
"@faker-js/faker": "^9.6.0",
|
75
75
|
"@types/busboy": "^1.5.4",
|
76
76
|
"@types/chai": "^5.2.1",
|
@@ -149,6 +149,6 @@
|
|
149
149
|
"recursive": true
|
150
150
|
},
|
151
151
|
"versionist": {
|
152
|
-
"publishedAt": "2025-05-01T13:
|
152
|
+
"publishedAt": "2025-05-01T13:16:36.421Z"
|
153
153
|
}
|
154
154
|
}
|
@@ -33,6 +33,7 @@ import {
|
|
33
33
|
type WebResourceHandler,
|
34
34
|
setupUploadHooks,
|
35
35
|
setupWebresourceHandler,
|
36
|
+
setupUploadActions,
|
36
37
|
} from '../webresource-handler/index.js';
|
37
38
|
import type { AliasValidNodeType } from '../sbvr-api/translations.js';
|
38
39
|
|
@@ -221,18 +222,17 @@ export const setup = (app: Express.Application) => {
|
|
221
222
|
.delete(sbvrUtils.handleODataRequest);
|
222
223
|
|
223
224
|
const loadedModel = sbvrUtils.getModel(model.apiRoot!);
|
224
|
-
|
225
|
-
|
226
|
-
|
225
|
+
const resourceNames = Object.values(loadedModel.abstractSql.tables)
|
226
|
+
.filter((table) =>
|
227
|
+
table.fields.some((f) => f.dataType === 'WebResource'),
|
227
228
|
)
|
228
|
-
|
229
|
-
table.fields.some((f) => f.dataType === 'WebResource'),
|
230
|
-
)
|
231
|
-
.map((table) => table.name);
|
229
|
+
.map((table) => table.name);
|
232
230
|
|
233
|
-
|
231
|
+
for (const resource of new Set(resourceNames)) {
|
232
|
+
if (translateTo == null) {
|
234
233
|
setupUploadHooks(webResourceHandler, model.apiRoot!, resource);
|
235
234
|
}
|
235
|
+
setupUploadActions(webResourceHandler, model.apiRoot!, resource);
|
236
236
|
}
|
237
237
|
|
238
238
|
console.info(
|
package/src/config-loader/env.ts
CHANGED
@@ -159,6 +159,17 @@ export const tasks = {
|
|
159
159
|
queueIntervalMS: intVar('PINEJS_QUEUE_INTERVAL_MS', 1000),
|
160
160
|
};
|
161
161
|
|
162
|
+
export const webResource = {
|
163
|
+
multipartUploadEnabled: boolVar(
|
164
|
+
'PINEJS_WEBRESOURCE_MULTIPART_ENABLED',
|
165
|
+
false,
|
166
|
+
),
|
167
|
+
singleUploadMaxFilesize: intVar(
|
168
|
+
'PINEJS_WEBRESOURCE_MAXFILESIZE',
|
169
|
+
299 * 1024 * 1024,
|
170
|
+
),
|
171
|
+
};
|
172
|
+
|
162
173
|
export const guardTestMockOnly = () => {
|
163
174
|
if (process.env.DEPLOYMENT !== 'TEST') {
|
164
175
|
throw new Error('Attempting to use TEST_MOCK_ONLY outside of tests');
|
@@ -8,6 +8,7 @@ import * as migrator from '../migrator/sync.js';
|
|
8
8
|
import type * as migratorUtils from '../migrator/utils.js';
|
9
9
|
import * as tasks from '../tasks/index.js';
|
10
10
|
export * as actions from '../sbvr-api/actions.js';
|
11
|
+
import * as webresource from '../webresource-handler/index.js';
|
11
12
|
|
12
13
|
import * as sbvrUtils from '../sbvr-api/sbvr-utils.js';
|
13
14
|
import { PINEJS_ADVISORY_LOCK } from '../config-loader/env.js';
|
@@ -67,6 +68,7 @@ export const init = async <T extends string>(
|
|
67
68
|
const cfgLoader = configLoader.setup(app);
|
68
69
|
await cfgLoader.loadConfig(migrator.config);
|
69
70
|
await cfgLoader.loadConfig(tasks.config);
|
71
|
+
await cfgLoader.loadConfig(webresource.config);
|
70
72
|
|
71
73
|
if (!process.env.CONFIG_LOADER_DISABLED) {
|
72
74
|
await cfgLoader.loadApplicationConfig(config);
|
@@ -0,0 +1,160 @@
|
|
1
|
+
import type { AnyObject } from 'pinejs-client-core';
|
2
|
+
|
3
|
+
import { api, type Response } from '../../sbvr-api/sbvr-utils.js';
|
4
|
+
import type { MultipartUploadHandler } from '../multiparUpload.js';
|
5
|
+
import { getMultipartUploadHandler } from '../multiparUpload.js';
|
6
|
+
import type { BeginMultipartUploadPayload } from '../index.js';
|
7
|
+
import { getWebResourceFields } from '../index.js';
|
8
|
+
import { BadRequestError, NotImplementedError } from '../../sbvr-api/errors.js';
|
9
|
+
import { permissions, sbvrUtils } from '../../server-glue/module.js';
|
10
|
+
import { TransactionClosedError } from '../../database-layer/db.js';
|
11
|
+
import { randomUUID } from 'crypto';
|
12
|
+
import type { WebResourceType as WebResource } from '@balena/sbvr-types';
|
13
|
+
import type {
|
14
|
+
ODataActionArgs,
|
15
|
+
ODataActionRequest,
|
16
|
+
} from '../../sbvr-api/actions.js';
|
17
|
+
|
18
|
+
type FakeWebResourcePatch = {
|
19
|
+
[key: string]: Omit<WebResource, 'href'> & {
|
20
|
+
href: 'fake_patch';
|
21
|
+
};
|
22
|
+
};
|
23
|
+
|
24
|
+
export const beginUploadAction = async ({
|
25
|
+
request,
|
26
|
+
tx,
|
27
|
+
id,
|
28
|
+
req,
|
29
|
+
}: ODataActionArgs): Promise<Response> => {
|
30
|
+
if (typeof id !== 'number') {
|
31
|
+
throw new NotImplementedError(
|
32
|
+
'multipart upload do not yet support non-numeric ids',
|
33
|
+
);
|
34
|
+
}
|
35
|
+
|
36
|
+
const handler = getMultipartUploadHandler();
|
37
|
+
const { fieldName, beginUploadPayload } = parseBeginUpload(request, handler);
|
38
|
+
|
39
|
+
await runFakeDbPatch(request, {
|
40
|
+
[fieldName]: { ...beginUploadPayload, href: 'fake_patch' },
|
41
|
+
});
|
42
|
+
|
43
|
+
const { fileKey, uploadId, uploadParts } =
|
44
|
+
await handler.multipartUpload.begin(fieldName, beginUploadPayload);
|
45
|
+
const uuid = randomUUID();
|
46
|
+
await api.webresource.post({
|
47
|
+
resource: 'multipart_upload',
|
48
|
+
body: {
|
49
|
+
uuid,
|
50
|
+
resource_name: request.resourceName,
|
51
|
+
field_name: fieldName,
|
52
|
+
resource_id: id,
|
53
|
+
upload_id: uploadId,
|
54
|
+
file_key: fileKey,
|
55
|
+
status: 'pending',
|
56
|
+
...beginUploadPayload,
|
57
|
+
expiry_date: Date.now() + 7 * 24 * 60 * 60 * 1000, // 7 days in ms
|
58
|
+
is_created_by__actor: req.user?.actor,
|
59
|
+
},
|
60
|
+
passthrough: {
|
61
|
+
req: permissions.root,
|
62
|
+
tx,
|
63
|
+
},
|
64
|
+
});
|
65
|
+
|
66
|
+
return {
|
67
|
+
body: { [fieldName]: { uuid, uploadParts } },
|
68
|
+
statusCode: 200,
|
69
|
+
};
|
70
|
+
};
|
71
|
+
|
72
|
+
const parseBeginUpload = (
|
73
|
+
request: ODataActionRequest,
|
74
|
+
webResourceHandler: MultipartUploadHandler,
|
75
|
+
) => {
|
76
|
+
const fieldNames = Object.keys(request.values);
|
77
|
+
if (fieldNames.length !== 1) {
|
78
|
+
throw new BadRequestError(
|
79
|
+
'You can only get upload url for one field at a time',
|
80
|
+
);
|
81
|
+
}
|
82
|
+
|
83
|
+
const [fieldName] = fieldNames;
|
84
|
+
const webResourceFields = getWebResourceFields(request, false);
|
85
|
+
if (!webResourceFields.includes(fieldName)) {
|
86
|
+
throw new BadRequestError(
|
87
|
+
`The provided field '${fieldName}' is not a valid webresource`,
|
88
|
+
);
|
89
|
+
}
|
90
|
+
|
91
|
+
const beginUploadPayload = parseBeginUploadPayload(
|
92
|
+
request.values[fieldName],
|
93
|
+
webResourceHandler,
|
94
|
+
);
|
95
|
+
if (beginUploadPayload == null) {
|
96
|
+
throw new BadRequestError('Invalid file metadata');
|
97
|
+
}
|
98
|
+
|
99
|
+
return {
|
100
|
+
beginUploadPayload,
|
101
|
+
fieldName,
|
102
|
+
};
|
103
|
+
};
|
104
|
+
|
105
|
+
const parseBeginUploadPayload = (
|
106
|
+
payload: AnyObject,
|
107
|
+
webResourceHandler: MultipartUploadHandler,
|
108
|
+
): BeginMultipartUploadPayload | null => {
|
109
|
+
if (payload == null || typeof payload !== 'object') {
|
110
|
+
return null;
|
111
|
+
}
|
112
|
+
|
113
|
+
let { filename, content_type, size, chunk_size } = payload;
|
114
|
+
if (
|
115
|
+
typeof filename !== 'string' ||
|
116
|
+
typeof content_type !== 'string' ||
|
117
|
+
typeof size !== 'number' ||
|
118
|
+
(chunk_size != null && typeof chunk_size !== 'number') ||
|
119
|
+
(chunk_size != null &&
|
120
|
+
chunk_size < webResourceHandler.multipartUpload.getMinimumPartSize())
|
121
|
+
) {
|
122
|
+
return null;
|
123
|
+
}
|
124
|
+
|
125
|
+
chunk_size ??= webResourceHandler.multipartUpload.getDefaultPartSize();
|
126
|
+
|
127
|
+
return { filename, content_type, size, chunk_size };
|
128
|
+
};
|
129
|
+
|
130
|
+
// TODO: comment explaining why run this fake transaction is important
|
131
|
+
const runFakeDbPatch = async (
|
132
|
+
request: ODataActionRequest,
|
133
|
+
fakeDbPatch: FakeWebResourcePatch,
|
134
|
+
) => {
|
135
|
+
try {
|
136
|
+
await sbvrUtils.db.transaction(async (fakeTx) => {
|
137
|
+
const newUrl = request.url.slice(1).replace(/\/beginUpload$/, '');
|
138
|
+
await api[request.vocabulary].request({
|
139
|
+
method: 'PATCH',
|
140
|
+
url: newUrl,
|
141
|
+
body: fakeDbPatch,
|
142
|
+
// TODO: comment explaining why root is necessary
|
143
|
+
passthrough: { tx: fakeTx, req: permissions.root },
|
144
|
+
});
|
145
|
+
|
146
|
+
await fakeTx.rollback();
|
147
|
+
});
|
148
|
+
} catch (e) {
|
149
|
+
if (
|
150
|
+
// calling rollback() always makes it throw, so we capture and move on in case
|
151
|
+
// the error was just because the tx was (on purpose) rolledback
|
152
|
+
!(
|
153
|
+
e instanceof TransactionClosedError &&
|
154
|
+
e.message === 'Transaction has been rolled back.'
|
155
|
+
)
|
156
|
+
) {
|
157
|
+
throw e;
|
158
|
+
}
|
159
|
+
}
|
160
|
+
};
|
@@ -0,0 +1,92 @@
|
|
1
|
+
import type {
|
2
|
+
ODataActionArgs,
|
3
|
+
ODataActionRequest,
|
4
|
+
} from '../../sbvr-api/actions.js';
|
5
|
+
import type { Tx } from '../../database-layer/db.js';
|
6
|
+
import {
|
7
|
+
BadRequestError,
|
8
|
+
NotImplementedError,
|
9
|
+
UnauthorizedError,
|
10
|
+
} from '../../sbvr-api/errors.js';
|
11
|
+
import { api, type Response } from '../../sbvr-api/sbvr-utils.js';
|
12
|
+
import { permissions } from '../../server-glue/module.js';
|
13
|
+
import { getMultipartUploadHandler } from '../multiparUpload.js';
|
14
|
+
|
15
|
+
export const cancelUploadAction = async ({
|
16
|
+
request,
|
17
|
+
tx,
|
18
|
+
id: resourceId,
|
19
|
+
}: ODataActionArgs): Promise<Response> => {
|
20
|
+
if (typeof resourceId !== 'number') {
|
21
|
+
throw new NotImplementedError(
|
22
|
+
'multipart upload do not yet support non-numeric ids',
|
23
|
+
);
|
24
|
+
}
|
25
|
+
const { id, fileKey, uploadId } = await getOngoingUpload(
|
26
|
+
request,
|
27
|
+
resourceId,
|
28
|
+
tx,
|
29
|
+
);
|
30
|
+
const handler = getMultipartUploadHandler();
|
31
|
+
|
32
|
+
await api.webresource.patch({
|
33
|
+
resource: 'multipart_upload',
|
34
|
+
body: {
|
35
|
+
status: 'cancelled',
|
36
|
+
},
|
37
|
+
id,
|
38
|
+
passthrough: {
|
39
|
+
tx: tx,
|
40
|
+
req: permissions.root,
|
41
|
+
},
|
42
|
+
});
|
43
|
+
|
44
|
+
// Note that different then beginUpload/commitUpload where we first do the action on the external service
|
45
|
+
// and then reflect it on the DB, for cancel upload it is the other way around
|
46
|
+
// as the worst case scenario is having a canceled upload which is marked on the DB as something else
|
47
|
+
await handler.multipartUpload.cancel({ fileKey, uploadId });
|
48
|
+
|
49
|
+
return {
|
50
|
+
statusCode: 204,
|
51
|
+
};
|
52
|
+
};
|
53
|
+
|
54
|
+
const getOngoingUpload = async (
|
55
|
+
request: ODataActionRequest,
|
56
|
+
affectedId: number,
|
57
|
+
tx: Tx,
|
58
|
+
) => {
|
59
|
+
const { uuid } = request.values;
|
60
|
+
if (!uuid || typeof uuid !== 'string') {
|
61
|
+
throw new BadRequestError('Invalid uuid type');
|
62
|
+
}
|
63
|
+
|
64
|
+
const [multipartUpload] = await api.webresource.get({
|
65
|
+
resource: 'multipart_upload',
|
66
|
+
options: {
|
67
|
+
$select: ['id', 'file_key', 'upload_id'],
|
68
|
+
$filter: {
|
69
|
+
// TODO: swap this into the ID to see if it works
|
70
|
+
uuid,
|
71
|
+
status: 'pending',
|
72
|
+
expiry_date: { $gt: { $now: {} } },
|
73
|
+
resource_name: request.resourceName,
|
74
|
+
resource_id: affectedId,
|
75
|
+
},
|
76
|
+
},
|
77
|
+
passthrough: {
|
78
|
+
tx,
|
79
|
+
req: permissions.rootRead,
|
80
|
+
},
|
81
|
+
});
|
82
|
+
|
83
|
+
if (multipartUpload == null) {
|
84
|
+
throw new UnauthorizedError();
|
85
|
+
}
|
86
|
+
|
87
|
+
return {
|
88
|
+
id: multipartUpload.id,
|
89
|
+
fileKey: multipartUpload.file_key,
|
90
|
+
uploadId: multipartUpload.upload_id,
|
91
|
+
};
|
92
|
+
};
|
@@ -0,0 +1,117 @@
|
|
1
|
+
import type { Tx } from '../../database-layer/db.js';
|
2
|
+
import type {
|
3
|
+
ODataActionArgs,
|
4
|
+
ODataActionRequest,
|
5
|
+
} from '../../sbvr-api/actions.js';
|
6
|
+
import {
|
7
|
+
BadRequestError,
|
8
|
+
NotImplementedError,
|
9
|
+
UnauthorizedError,
|
10
|
+
} from '../../sbvr-api/errors.js';
|
11
|
+
import type { Response } from '../../sbvr-api/sbvr-utils.js';
|
12
|
+
import { api } from '../../sbvr-api/sbvr-utils.js';
|
13
|
+
import { permissions } from '../../server-glue/module.js';
|
14
|
+
import { getMultipartUploadHandler } from '../multiparUpload.js';
|
15
|
+
|
16
|
+
export const commitUploadAction = async ({
|
17
|
+
request,
|
18
|
+
tx,
|
19
|
+
id,
|
20
|
+
api: applicationApi,
|
21
|
+
}: ODataActionArgs): Promise<Response> => {
|
22
|
+
if (typeof id !== 'number') {
|
23
|
+
throw new NotImplementedError(
|
24
|
+
'multipart upload do not yet support non-numeric ids',
|
25
|
+
);
|
26
|
+
}
|
27
|
+
|
28
|
+
const multipartUpload = await getOngoingUpload(request, id, tx);
|
29
|
+
const handler = getMultipartUploadHandler();
|
30
|
+
|
31
|
+
const webresource = await handler.multipartUpload.commit({
|
32
|
+
fileKey: multipartUpload.fileKey,
|
33
|
+
uploadId: multipartUpload.uploadId,
|
34
|
+
filename: multipartUpload.filename,
|
35
|
+
providerCommitData: multipartUpload.providerCommitData,
|
36
|
+
});
|
37
|
+
|
38
|
+
await Promise.all([
|
39
|
+
api.webresource.patch({
|
40
|
+
resource: 'multipart_upload',
|
41
|
+
body: {
|
42
|
+
status: 'completed',
|
43
|
+
},
|
44
|
+
options: {
|
45
|
+
$filter: {
|
46
|
+
uuid: multipartUpload.uuid,
|
47
|
+
},
|
48
|
+
},
|
49
|
+
passthrough: {
|
50
|
+
tx: tx,
|
51
|
+
req: permissions.root,
|
52
|
+
},
|
53
|
+
}),
|
54
|
+
applicationApi.patch({
|
55
|
+
resource: request.resourceName,
|
56
|
+
id,
|
57
|
+
body: {
|
58
|
+
[multipartUpload.fieldName]: webresource,
|
59
|
+
},
|
60
|
+
passthrough: {
|
61
|
+
tx: tx,
|
62
|
+
// Root is needed as, if you are not root, you are not allowed to directly modify the actual metadata
|
63
|
+
req: permissions.root,
|
64
|
+
},
|
65
|
+
}),
|
66
|
+
]);
|
67
|
+
|
68
|
+
const body = await handler.onPreRespond(webresource);
|
69
|
+
|
70
|
+
return {
|
71
|
+
body,
|
72
|
+
statusCode: 200,
|
73
|
+
};
|
74
|
+
};
|
75
|
+
|
76
|
+
const getOngoingUpload = async (
|
77
|
+
request: ODataActionRequest,
|
78
|
+
affectedId: number,
|
79
|
+
tx: Tx,
|
80
|
+
) => {
|
81
|
+
const { uuid, providerCommitData } = request.values;
|
82
|
+
if (!uuid || typeof uuid !== 'string') {
|
83
|
+
throw new BadRequestError('Invalid uuid type');
|
84
|
+
}
|
85
|
+
|
86
|
+
const [multipartUpload] = await api.webresource.get({
|
87
|
+
resource: 'multipart_upload',
|
88
|
+
options: {
|
89
|
+
$select: ['id', 'file_key', 'upload_id', 'field_name', 'filename'],
|
90
|
+
$filter: {
|
91
|
+
// TODO: swap this into the ID to see if it works
|
92
|
+
uuid,
|
93
|
+
status: 'pending',
|
94
|
+
expiry_date: { $gt: { $now: {} } },
|
95
|
+
resource_name: request.resourceName,
|
96
|
+
resource_id: affectedId,
|
97
|
+
},
|
98
|
+
},
|
99
|
+
passthrough: {
|
100
|
+
tx,
|
101
|
+
req: permissions.rootRead,
|
102
|
+
},
|
103
|
+
});
|
104
|
+
|
105
|
+
if (multipartUpload == null) {
|
106
|
+
throw new UnauthorizedError();
|
107
|
+
}
|
108
|
+
|
109
|
+
return {
|
110
|
+
uuid,
|
111
|
+
providerCommitData,
|
112
|
+
fileKey: multipartUpload.file_key,
|
113
|
+
uploadId: multipartUpload.upload_id,
|
114
|
+
filename: multipartUpload.filename,
|
115
|
+
fieldName: multipartUpload.field_name,
|
116
|
+
};
|
117
|
+
};
|
@@ -11,10 +11,18 @@ import {
|
|
11
11
|
odataNameToSqlName,
|
12
12
|
sqlNameToODataName,
|
13
13
|
} from '@balena/odata-to-abstract-sql';
|
14
|
+
import type { ConfigLoader } from '../server-glue/module.js';
|
14
15
|
import { errors, permissions } from '../server-glue/module.js';
|
15
16
|
import type { WebResourceType as WebResource } from '@balena/sbvr-types';
|
16
17
|
import { TypedError } from 'typed-error';
|
17
18
|
import type { Resolvable } from '../sbvr-api/common-types.js';
|
19
|
+
import type WebresourceModel from './webresource.js';
|
20
|
+
import { importSBVR } from '../server-glue/sbvr-loader.js';
|
21
|
+
import { beginUploadAction } from './actions/beginUpload.js';
|
22
|
+
import { isMultipartUploadAvailable } from './multiparUpload.js';
|
23
|
+
import { commitUploadAction } from './actions/commitUpload.js';
|
24
|
+
import { cancelUploadAction } from './actions/canceUpload.js';
|
25
|
+
import { addAction } from '../sbvr-api/actions.js';
|
18
26
|
|
19
27
|
export * from './handlers/index.js';
|
20
28
|
|
@@ -329,6 +337,7 @@ const throwIfWebresourceNotInMultipart = (
|
|
329
337
|
{ req, request }: HookArgs,
|
330
338
|
) => {
|
331
339
|
if (
|
340
|
+
req.user !== permissions.root.user &&
|
332
341
|
!req.is?.('multipart') &&
|
333
342
|
webResourceFields.some((field) => request.values[field] != null)
|
334
343
|
) {
|
@@ -526,3 +535,42 @@ export const setupUploadHooks = (
|
|
526
535
|
getCreateWebResourceHooks(handler),
|
527
536
|
);
|
528
537
|
};
|
538
|
+
|
539
|
+
export const setupUploadActions = (
|
540
|
+
handler: WebResourceHandler,
|
541
|
+
apiRoot: string,
|
542
|
+
resourceName: string,
|
543
|
+
) => {
|
544
|
+
const resource = sqlNameToODataName(resourceName);
|
545
|
+
if (isMultipartUploadAvailable(handler)) {
|
546
|
+
addAction(apiRoot, resource, 'beginUpload', beginUploadAction);
|
547
|
+
|
548
|
+
addAction(apiRoot, resource, 'commitUpload', commitUploadAction);
|
549
|
+
|
550
|
+
addAction(apiRoot, resource, 'cancelUpload', cancelUploadAction);
|
551
|
+
}
|
552
|
+
};
|
553
|
+
|
554
|
+
const initSql = `
|
555
|
+
CREATE INDEX IF NOT EXISTS idx_multipart_upload_uuid ON "multipart upload" (uuid);
|
556
|
+
CREATE INDEX IF NOT EXISTS idx_multipart_upload_status ON "multipart upload" (status);
|
557
|
+
`;
|
558
|
+
|
559
|
+
const modelText = await importSBVR('./webresource.sbvr', import.meta);
|
560
|
+
|
561
|
+
declare module '../sbvr-api/sbvr-utils.js' {
|
562
|
+
export interface API {
|
563
|
+
webresource: PinejsClient<WebresourceModel>;
|
564
|
+
}
|
565
|
+
}
|
566
|
+
|
567
|
+
export const config: ConfigLoader.Config = {
|
568
|
+
models: [
|
569
|
+
{
|
570
|
+
modelName: 'webresource',
|
571
|
+
apiRoot: 'webresource',
|
572
|
+
modelText,
|
573
|
+
initSql,
|
574
|
+
},
|
575
|
+
],
|
576
|
+
};
|
@@ -0,0 +1,23 @@
|
|
1
|
+
import { webResource as webResourceEnv } from '../config-loader/env.js';
|
2
|
+
import { NotImplementedError } from '../sbvr-api/errors.js';
|
3
|
+
import type { WebResourceHandler } from './index.js';
|
4
|
+
import { getWebresourceHandler } from './index.js';
|
5
|
+
|
6
|
+
export type MultipartUploadHandler = WebResourceHandler &
|
7
|
+
Required<Pick<WebResourceHandler, 'multipartUpload'>>;
|
8
|
+
|
9
|
+
export const isMultipartUploadAvailable = (
|
10
|
+
handler: WebResourceHandler | undefined,
|
11
|
+
): handler is MultipartUploadHandler => {
|
12
|
+
return (
|
13
|
+
webResourceEnv.multipartUploadEnabled && handler?.multipartUpload != null
|
14
|
+
);
|
15
|
+
};
|
16
|
+
|
17
|
+
export const getMultipartUploadHandler = () => {
|
18
|
+
const handler = getWebresourceHandler();
|
19
|
+
if (!isMultipartUploadAvailable(handler)) {
|
20
|
+
throw new NotImplementedError('Multipart uploads not available');
|
21
|
+
}
|
22
|
+
return handler;
|
23
|
+
};
|