@balena/pinejs 20.1.0-build-add-large-file-uploads-interfaces-1aa101d2ee3103f42cf7f7bc29d703a8958011e8-1 → 20.1.0-build-large-file-uploads-2-b83400458570d926f7a6abddeb7a149e89f52abe-2
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 +2950 -2
- package/CHANGELOG.md +1091 -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/sbvr-api/abstract-sql.js +19 -9
- package/out/sbvr-api/abstract-sql.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/index.d.ts +10 -0
- package/out/webresource-handler/index.js +53 -18
- package/out/webresource-handler/index.js.map +1 -1
- package/out/webresource-handler/multipartUpload.d.ts +17 -0
- package/out/webresource-handler/multipartUpload.js +259 -0
- package/out/webresource-handler/multipartUpload.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 +8 -8
- package/src/config-loader/env.ts +11 -0
- package/src/sbvr-api/abstract-sql.ts +28 -9
- package/src/server-glue/module.ts +2 -0
- package/src/webresource-handler/index.ts +93 -24
- package/src/webresource-handler/multipartUpload.ts +371 -0
- package/src/webresource-handler/webresource.sbvr +60 -0
- package/src/webresource-handler/webresource.ts +48 -0
@@ -0,0 +1,259 @@
|
|
1
|
+
import { randomUUID } from 'node:crypto';
|
2
|
+
import { getWebResourceFields } from './index.js';
|
3
|
+
import { api } from '../sbvr-api/sbvr-utils.js';
|
4
|
+
import { errors, sbvrUtils } from '../server-glue/module.js';
|
5
|
+
import { webResource as webResourceEnv } from '../config-loader/env.js';
|
6
|
+
import * as permissions from '../sbvr-api/permissions.js';
|
7
|
+
const MB = 1024 * 1024;
|
8
|
+
export const isMultipartUploadAvailable = (webResourceHandler) => {
|
9
|
+
return (webResourceEnv.multipartUploadEnabled &&
|
10
|
+
webResourceHandler.multipartUpload != null);
|
11
|
+
};
|
12
|
+
export const multipartUploadHooks = (webResourceHandler) => {
|
13
|
+
return {
|
14
|
+
POSTPARSE: async ({ req, request, tx, api: applicationApi }) => {
|
15
|
+
if (request.odataQuery.property?.resource === 'beginUpload') {
|
16
|
+
const uploadParams = await validateBeginUpload(request, applicationApi);
|
17
|
+
tx = await sbvrUtils.db.transaction();
|
18
|
+
req.tx = tx;
|
19
|
+
request.tx = tx;
|
20
|
+
request.method = 'PATCH';
|
21
|
+
request.values = uploadParams;
|
22
|
+
request.odataQuery.resource = request.resourceName;
|
23
|
+
delete request.odataQuery.property;
|
24
|
+
request.custom.isAction = 'beginUpload';
|
25
|
+
}
|
26
|
+
else if (request.odataQuery.property?.resource === 'commitUpload') {
|
27
|
+
const commitPayload = await validateCommitUpload(request, applicationApi);
|
28
|
+
const webresource = await webResourceHandler.multipartUpload.commit({
|
29
|
+
fileKey: commitPayload.metadata.fileKey,
|
30
|
+
uploadId: commitPayload.metadata.uploadId,
|
31
|
+
filename: commitPayload.metadata.filename,
|
32
|
+
providerCommitData: commitPayload.providerCommitData,
|
33
|
+
});
|
34
|
+
await api.webresource.patch({
|
35
|
+
resource: 'multipart_upload',
|
36
|
+
body: {
|
37
|
+
status: 'completed',
|
38
|
+
},
|
39
|
+
options: {
|
40
|
+
$filter: {
|
41
|
+
uuid: commitPayload.uuid,
|
42
|
+
},
|
43
|
+
},
|
44
|
+
passthrough: {
|
45
|
+
tx: tx,
|
46
|
+
req: permissions.root,
|
47
|
+
},
|
48
|
+
});
|
49
|
+
request.method = 'PATCH';
|
50
|
+
request.values = {
|
51
|
+
[commitPayload.metadata.fieldName]: webresource,
|
52
|
+
};
|
53
|
+
request.odataQuery.resource = request.resourceName;
|
54
|
+
delete request.odataQuery.property;
|
55
|
+
request.custom.isAction = 'commitUpload';
|
56
|
+
request.custom.commitUploadPayload = webresource;
|
57
|
+
}
|
58
|
+
else if (request.odataQuery.property?.resource === 'cancelUpload') {
|
59
|
+
const { uuid, fileKey, uploadId } = await validateCancelPayload(request, applicationApi);
|
60
|
+
await webResourceHandler.multipartUpload.cancel({ fileKey, uploadId });
|
61
|
+
await api.webresource.patch({
|
62
|
+
resource: 'multipart_upload',
|
63
|
+
body: {
|
64
|
+
status: 'cancelled',
|
65
|
+
},
|
66
|
+
options: {
|
67
|
+
$filter: { uuid },
|
68
|
+
},
|
69
|
+
passthrough: {
|
70
|
+
tx: tx,
|
71
|
+
req: permissions.root,
|
72
|
+
},
|
73
|
+
});
|
74
|
+
request.method = 'GET';
|
75
|
+
request.odataQuery.resource = request.resourceName;
|
76
|
+
delete request.odataQuery.property;
|
77
|
+
request.custom.isAction = 'cancelUpload';
|
78
|
+
}
|
79
|
+
},
|
80
|
+
PRERESPOND: async ({ req, request, response, tx }) => {
|
81
|
+
if (request.custom.isAction === 'beginUpload') {
|
82
|
+
await tx.rollback();
|
83
|
+
response.statusCode = 200;
|
84
|
+
response.body = await beginUpload({
|
85
|
+
webResourceHandler,
|
86
|
+
odataRequest: request,
|
87
|
+
actorId: req.user?.actor,
|
88
|
+
});
|
89
|
+
}
|
90
|
+
else if (request.custom.isAction === 'commitUpload') {
|
91
|
+
response.body = await webResourceHandler.onPreRespond(request.custom.commitUploadPayload);
|
92
|
+
}
|
93
|
+
else if (request.custom.isAction === 'cancelUpload') {
|
94
|
+
response.statusCode = 204;
|
95
|
+
delete response.body;
|
96
|
+
}
|
97
|
+
},
|
98
|
+
};
|
99
|
+
};
|
100
|
+
const beginUpload = async ({ webResourceHandler, odataRequest, actorId, }) => {
|
101
|
+
const payload = odataRequest.values;
|
102
|
+
const fieldName = Object.keys(payload)[0];
|
103
|
+
const metadata = payload[fieldName];
|
104
|
+
const { fileKey, uploadId, uploadParts } = await webResourceHandler.multipartUpload.begin(fieldName, metadata);
|
105
|
+
const uuid = randomUUID();
|
106
|
+
return await sbvrUtils.db.transaction(async (tx) => {
|
107
|
+
try {
|
108
|
+
await api.webresource.post({
|
109
|
+
resource: 'multipart_upload',
|
110
|
+
body: {
|
111
|
+
uuid,
|
112
|
+
resource_name: odataRequest.resourceName,
|
113
|
+
field_name: fieldName,
|
114
|
+
resource_id: odataRequest.affectedIds?.[0],
|
115
|
+
upload_id: uploadId,
|
116
|
+
file_key: fileKey,
|
117
|
+
status: 'pending',
|
118
|
+
filename: metadata.filename,
|
119
|
+
content_type: metadata.content_type,
|
120
|
+
size: metadata.size,
|
121
|
+
chunk_size: metadata.chunk_size,
|
122
|
+
expiry_date: Date.now() + 7 * 24 * 60 * 60 * 1000,
|
123
|
+
is_created_by__actor: actorId,
|
124
|
+
},
|
125
|
+
passthrough: {
|
126
|
+
req: permissions.root,
|
127
|
+
tx,
|
128
|
+
},
|
129
|
+
});
|
130
|
+
return { [fieldName]: { uuid, uploadParts } };
|
131
|
+
}
|
132
|
+
catch (err) {
|
133
|
+
console.error('failed to start multipart upload', err);
|
134
|
+
throw new errors.BadRequestError('Failed to start multipart upload');
|
135
|
+
}
|
136
|
+
});
|
137
|
+
};
|
138
|
+
const validateBeginUpload = async (request, applicationApi) => {
|
139
|
+
if (request.odataQuery.key == null) {
|
140
|
+
throw new errors.BadRequestError();
|
141
|
+
}
|
142
|
+
await applicationApi.request({
|
143
|
+
method: 'POST',
|
144
|
+
url: request.url.substring(1).replace('beginUpload', 'canAccess'),
|
145
|
+
body: { method: 'PATCH' },
|
146
|
+
});
|
147
|
+
const fieldNames = Object.keys(request.values);
|
148
|
+
if (fieldNames.length !== 1) {
|
149
|
+
throw new errors.BadRequestError('You can only get upload url for one field at a time');
|
150
|
+
}
|
151
|
+
const [fieldName] = fieldNames;
|
152
|
+
const webResourceFields = getWebResourceFields(request, false);
|
153
|
+
if (!webResourceFields.includes(fieldName)) {
|
154
|
+
throw new errors.BadRequestError(`You must provide a valid webresource field from: ${JSON.stringify(webResourceFields)}`);
|
155
|
+
}
|
156
|
+
const beginUploadPayload = parseBeginUploadPayload(request.values[fieldName]);
|
157
|
+
if (beginUploadPayload == null) {
|
158
|
+
throw new errors.BadRequestError('Invalid file metadata');
|
159
|
+
}
|
160
|
+
const uploadMetadataCheck = {
|
161
|
+
...beginUploadPayload,
|
162
|
+
href: 'metadata_check_probe',
|
163
|
+
};
|
164
|
+
return { [fieldName]: uploadMetadataCheck };
|
165
|
+
};
|
166
|
+
const parseBeginUploadPayload = (payload) => {
|
167
|
+
if (typeof payload !== 'object') {
|
168
|
+
return null;
|
169
|
+
}
|
170
|
+
let { filename, content_type, size, chunk_size } = payload;
|
171
|
+
if (typeof filename !== 'string' ||
|
172
|
+
typeof content_type !== 'string' ||
|
173
|
+
typeof size !== 'number' ||
|
174
|
+
(chunk_size != null && typeof chunk_size !== 'number') ||
|
175
|
+
(chunk_size != null && chunk_size < 5 * MB)) {
|
176
|
+
return null;
|
177
|
+
}
|
178
|
+
if (chunk_size == null) {
|
179
|
+
chunk_size = 5 * MB;
|
180
|
+
}
|
181
|
+
return { filename, content_type, size, chunk_size };
|
182
|
+
};
|
183
|
+
const validateCommitUpload = async (request, applicationApi) => {
|
184
|
+
if (request.odataQuery.key == null) {
|
185
|
+
throw new errors.BadRequestError();
|
186
|
+
}
|
187
|
+
await applicationApi.request({
|
188
|
+
method: 'POST',
|
189
|
+
url: request.url.substring(1).replace('commitUpload', 'canAccess'),
|
190
|
+
body: { method: 'PATCH' },
|
191
|
+
});
|
192
|
+
const { uuid, providerCommitData } = request.values;
|
193
|
+
if (typeof uuid !== 'string') {
|
194
|
+
throw new errors.BadRequestError('Invalid uuid type');
|
195
|
+
}
|
196
|
+
const [multipartUpload] = await api.webresource.get({
|
197
|
+
resource: 'multipart_upload',
|
198
|
+
options: {
|
199
|
+
$select: ['id', 'file_key', 'upload_id', 'field_name', 'filename'],
|
200
|
+
$filter: {
|
201
|
+
uuid,
|
202
|
+
status: 'pending',
|
203
|
+
expiry_date: { $gt: { $now: {} } },
|
204
|
+
},
|
205
|
+
},
|
206
|
+
passthrough: {
|
207
|
+
tx: request.tx,
|
208
|
+
req: permissions.rootRead,
|
209
|
+
},
|
210
|
+
});
|
211
|
+
if (multipartUpload == null) {
|
212
|
+
throw new errors.BadRequestError(`Invalid upload for uuid ${uuid}`);
|
213
|
+
}
|
214
|
+
const metadata = {
|
215
|
+
fileKey: multipartUpload.file_key,
|
216
|
+
uploadId: multipartUpload.upload_id,
|
217
|
+
filename: multipartUpload.filename,
|
218
|
+
fieldName: multipartUpload.field_name,
|
219
|
+
};
|
220
|
+
return { uuid, providerCommitData, metadata };
|
221
|
+
};
|
222
|
+
const validateCancelPayload = async (request, applicationApi) => {
|
223
|
+
if (request.odataQuery.key == null) {
|
224
|
+
throw new errors.BadRequestError();
|
225
|
+
}
|
226
|
+
await applicationApi.request({
|
227
|
+
method: 'POST',
|
228
|
+
url: request.url.substring(1).replace('cancelUpload', 'canAccess'),
|
229
|
+
body: { method: 'PATCH' },
|
230
|
+
});
|
231
|
+
const { uuid } = request.values;
|
232
|
+
if (typeof uuid !== 'string') {
|
233
|
+
throw new errors.BadRequestError('Invalid uuid type');
|
234
|
+
}
|
235
|
+
const [multipartUpload] = await api.webresource.get({
|
236
|
+
resource: 'multipart_upload',
|
237
|
+
options: {
|
238
|
+
$select: ['id', 'file_key', 'upload_id'],
|
239
|
+
$filter: {
|
240
|
+
uuid,
|
241
|
+
status: 'pending',
|
242
|
+
expiry_date: { $gt: { $now: {} } },
|
243
|
+
},
|
244
|
+
},
|
245
|
+
passthrough: {
|
246
|
+
tx: request.tx,
|
247
|
+
req: permissions.rootRead,
|
248
|
+
},
|
249
|
+
});
|
250
|
+
if (multipartUpload == null) {
|
251
|
+
throw new errors.BadRequestError(`Invalid upload for uuid ${uuid}`);
|
252
|
+
}
|
253
|
+
return {
|
254
|
+
uuid,
|
255
|
+
fileKey: multipartUpload.file_key,
|
256
|
+
uploadId: multipartUpload.upload_id,
|
257
|
+
};
|
258
|
+
};
|
259
|
+
//# sourceMappingURL=multipartUpload.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"multipartUpload.js","sourceRoot":"","sources":["../../src/webresource-handler/multipartUpload.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAOzC,OAAO,EAAE,oBAAoB,EAAE,MAAM,YAAY,CAAC;AAElD,OAAO,EAAE,GAAG,EAAE,MAAM,2BAA2B,CAAC;AAEhD,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,0BAA0B,CAAC;AAC7D,OAAO,EAAE,WAAW,IAAI,cAAc,EAAE,MAAM,yBAAyB,CAAC;AACxE,OAAO,KAAK,WAAW,MAAM,4BAA4B,CAAC;AAqB1D,MAAM,EAAE,GAAG,IAAI,GAAG,IAAI,CAAC;AAEvB,MAAM,CAAC,MAAM,0BAA0B,GAAG,CACzC,kBAAsC,EACS,EAAE;IACjD,OAAO,CACN,cAAc,CAAC,sBAAsB;QACrC,kBAAkB,CAAC,eAAe,IAAI,IAAI,CAC1C,CAAC;AACH,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,oBAAoB,GAAG,CACnC,kBAA0C,EACxB,EAAE;IACpB,OAAO;QACN,SAAS,EAAE,KAAK,EAAE,EAAE,GAAG,EAAE,OAAO,EAAE,EAAE,EAAE,GAAG,EAAE,cAAc,EAAE,EAAE,EAAE;YAC9D,IAAI,OAAO,CAAC,UAAU,CAAC,QAAQ,EAAE,QAAQ,KAAK,aAAa,EAAE,CAAC;gBAC7D,MAAM,YAAY,GAAG,MAAM,mBAAmB,CAAC,OAAO,EAAE,cAAc,CAAC,CAAC;gBAOxE,EAAE,GAAG,MAAM,SAAS,CAAC,EAAE,CAAC,WAAW,EAAE,CAAC;gBACtC,GAAG,CAAC,EAAE,GAAG,EAAE,CAAC;gBACZ,OAAO,CAAC,EAAE,GAAG,EAAE,CAAC;gBAEhB,OAAO,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AG,OAAO,CAAC,MAAM,CAAC;IAChC,IAAI,OAAO,IAAI,KAAK,QAAQ,EAAE,CAAC;QAC9B,MAAM,IAAI,MAAM,CAAC,eAAe,CAAC,mBAAmB,CAAC,CAAC;IACvD,CAAC;IAED,MAAM,CAAC,eAAe,CAAC,GAAG,MAAM,GAAG,CAAC,WAAW,CAAC,GAAG,CAAC;QACnD,QAAQ,EAAE,kBAAkB;QAC5B,OAAO,EAAE;YACR,OAAO,EAAE,CAAC,IAAI,EAAE,UAAU,EAAE,WAAW,CAAC;YACxC,OAAO,EAAE;gBACR,IAAI;gBACJ,MAAM,EAAE,SAAS;gBACjB,WAAW,EAAE,EAAE,GAAG,EAAE,EAAE,IAAI,EAAE,EAAE,EAAE,EAAE;aAClC;SACD;QACD,WAAW,EAAE;YACZ,EAAE,EAAE,OAAO,CAAC,EAAE;YACd,GAAG,EAAE,WAAW,CAAC,QAAQ;SACzB;KACD,CAAC,CAAC;IAEH,IAAI,eAAe,IAAI,IAAI,EAAE,CAAC;QAC7B,MAAM,IAAI,MAAM,CAAC,eAAe,CAAC,2BAA2B,IAAI,EAAE,CAAC,CAAC;IACrE,CAAC;IAED,OAAO;QACN,IAAI;QACJ,OAAO,EAAE,eAAe,CAAC,QAAQ;QACjC,QAAQ,EAAE,eAAe,CAAC,SAAS;KACnC,CAAC;AACH,CAAC,CAAC"}
|
@@ -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": "20.1.0-build-
|
3
|
+
"version": "20.1.0-build-large-file-uploads-2-b83400458570d926f7a6abddeb7a149e89f52abe-2",
|
4
4
|
"main": "out/server-glue/module.js",
|
5
5
|
"type": "module",
|
6
6
|
"repository": "git@github.com:balena-io/pinejs.git",
|
@@ -20,18 +20,18 @@
|
|
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 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
|
-
"@balena/abstract-sql-compiler": "^10.
|
29
|
+
"@balena/abstract-sql-compiler": "^10.2.0",
|
30
30
|
"@balena/abstract-sql-to-typescript": "^5.1.0",
|
31
31
|
"@balena/env-parsing": "^1.2.0",
|
32
32
|
"@balena/lf-to-abstract-sql": "^5.0.3",
|
33
|
-
"@balena/odata-parser": "^
|
34
|
-
"@balena/odata-to-abstract-sql": "^7.0
|
33
|
+
"@balena/odata-parser": "^4.1.0",
|
34
|
+
"@balena/odata-to-abstract-sql": "^7.1.0",
|
35
35
|
"@balena/sbvr-parser": "^1.4.6",
|
36
36
|
"@balena/sbvr-types": "^9.1.0",
|
37
37
|
"@types/body-parser": "^1.19.5",
|
@@ -69,7 +69,7 @@
|
|
69
69
|
"devDependencies": {
|
70
70
|
"@balena/lint": "^8.2.8",
|
71
71
|
"@balena/pinejs": "file:./",
|
72
|
-
"@balena/pinejs-webresource-s3": "
|
72
|
+
"@balena/pinejs-webresource-s3": "2.0.0-build-new-multiparthandle-interface-17681066eade039ed700105913ef65e00fbbfa2a-1",
|
73
73
|
"@faker-js/faker": "^9.3.0",
|
74
74
|
"@types/busboy": "^1.5.4",
|
75
75
|
"@types/chai": "^5.0.1",
|
@@ -148,6 +148,6 @@
|
|
148
148
|
"recursive": true
|
149
149
|
},
|
150
150
|
"versionist": {
|
151
|
-
"publishedAt": "2025-02-
|
151
|
+
"publishedAt": "2025-02-13T15:39:03.596Z"
|
152
152
|
}
|
153
153
|
}
|
package/src/config-loader/env.ts
CHANGED
@@ -157,6 +157,17 @@ export const tasks = {
|
|
157
157
|
queueIntervalMS: intVar('PINEJS_QUEUE_INTERVAL_MS', 1000),
|
158
158
|
};
|
159
159
|
|
160
|
+
export const webResource = {
|
161
|
+
multipartUploadEnabled: boolVar(
|
162
|
+
'PINEJS_WEBRESOURCE_MULTIPART_ENABLED',
|
163
|
+
false,
|
164
|
+
),
|
165
|
+
singleUploadMaxFilesize: intVar(
|
166
|
+
'PINEJS_WEBRESOURCE_MAXFILESIZE',
|
167
|
+
299 * 1024 * 1024,
|
168
|
+
),
|
169
|
+
};
|
170
|
+
|
160
171
|
export const guardTestMockOnly = () => {
|
161
172
|
if (process.env.DEPLOYMENT !== 'TEST') {
|
162
173
|
throw new Error('Attempting to use TEST_MOCK_ONLY outside of tests');
|
@@ -1,5 +1,6 @@
|
|
1
1
|
import _ from 'lodash';
|
2
2
|
|
3
|
+
import type { Engines } from '@balena/abstract-sql-compiler';
|
3
4
|
import AbstractSQLCompiler from '@balena/abstract-sql-compiler';
|
4
5
|
import type { BindKey } from '@balena/odata-parser';
|
5
6
|
import {
|
@@ -78,7 +79,7 @@ export const getAndCheckBindValues = async (
|
|
78
79
|
return await Promise.all(
|
79
80
|
bindings.map(async (binding) => {
|
80
81
|
let fieldName = '';
|
81
|
-
let
|
82
|
+
let dataType: string;
|
82
83
|
let value: any;
|
83
84
|
if (binding[0] === 'Bind') {
|
84
85
|
const bindValue = binding[1];
|
@@ -103,7 +104,7 @@ export const getAndCheckBindValues = async (
|
|
103
104
|
if (maybeField == null) {
|
104
105
|
throw new Error(`Could not find field '${fieldName}'`);
|
105
106
|
}
|
106
|
-
|
107
|
+
dataType = maybeField.dataType;
|
107
108
|
} else if (Number.isInteger(bindValue)) {
|
108
109
|
if (bindValue >= odataBinds.length) {
|
109
110
|
console.error(
|
@@ -112,9 +113,7 @@ export const getAndCheckBindValues = async (
|
|
112
113
|
);
|
113
114
|
throw new Error('Invalid binding');
|
114
115
|
}
|
115
|
-
let dataType;
|
116
116
|
[dataType, value] = odataBinds[bindValue];
|
117
|
-
field = { dataType };
|
118
117
|
} else if (typeof bindValue === 'string') {
|
119
118
|
if (!Object.hasOwn(odataBinds, bindValue)) {
|
120
119
|
console.error(
|
@@ -123,16 +122,12 @@ export const getAndCheckBindValues = async (
|
|
123
122
|
);
|
124
123
|
throw new Error('Invalid binding');
|
125
124
|
}
|
126
|
-
let dataType;
|
127
125
|
[dataType, value] = odataBinds[bindValue as BindKey];
|
128
|
-
field = { dataType };
|
129
126
|
} else {
|
130
127
|
throw new Error(`Unknown binding: ${binding}`);
|
131
128
|
}
|
132
129
|
} else {
|
133
|
-
let dataType;
|
134
130
|
[dataType, value] = binding;
|
135
|
-
field = { dataType };
|
136
131
|
}
|
137
132
|
|
138
133
|
if (value === undefined) {
|
@@ -140,7 +135,7 @@ export const getAndCheckBindValues = async (
|
|
140
135
|
}
|
141
136
|
|
142
137
|
try {
|
143
|
-
return await
|
138
|
+
return await validateBindingType(engine, value, dataType);
|
144
139
|
} catch (err: any) {
|
145
140
|
throw new BadRequestError(`"${fieldName}" ${err.message}`);
|
146
141
|
}
|
@@ -148,6 +143,30 @@ export const getAndCheckBindValues = async (
|
|
148
143
|
);
|
149
144
|
};
|
150
145
|
|
146
|
+
const validateBindingType = async (
|
147
|
+
engine: Engines,
|
148
|
+
$value: any,
|
149
|
+
$dataType: string,
|
150
|
+
): Promise<any> => {
|
151
|
+
if ($dataType === 'List') {
|
152
|
+
if (!Array.isArray($value)) {
|
153
|
+
throw new Error('List value binding must be an array');
|
154
|
+
}
|
155
|
+
return await Promise.all(
|
156
|
+
$value.map(async ([dataType, value]: [string, any]) => {
|
157
|
+
// Null is a special case for list values
|
158
|
+
if (dataType === 'Null') {
|
159
|
+
return null;
|
160
|
+
}
|
161
|
+
return await validateBindingType(engine, value, dataType);
|
162
|
+
}),
|
163
|
+
);
|
164
|
+
}
|
165
|
+
return await AbstractSQLCompiler[engine].dataTypeValidate($value, {
|
166
|
+
dataType: $dataType,
|
167
|
+
});
|
168
|
+
};
|
169
|
+
|
151
170
|
const checkModifiedFields = (
|
152
171
|
ruleReferencedFields: AbstractSQLCompiler.RuleReferencedFields,
|
153
172
|
modifiedFields: AbstractSQLCompiler.ModifiedFields,
|
@@ -7,6 +7,7 @@ import * as configLoader from '../config-loader/config-loader.js';
|
|
7
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
|
+
import * as webresource from '../webresource-handler/index.js';
|
10
11
|
|
11
12
|
import * as sbvrUtils from '../sbvr-api/sbvr-utils.js';
|
12
13
|
import { PINEJS_ADVISORY_LOCK } from '../config-loader/env.js';
|
@@ -66,6 +67,7 @@ export const init = async <T extends string>(
|
|
66
67
|
const cfgLoader = configLoader.setup(app);
|
67
68
|
await cfgLoader.loadConfig(migrator.config);
|
68
69
|
await cfgLoader.loadConfig(tasks.config);
|
70
|
+
await cfgLoader.loadConfig(webresource.config);
|
69
71
|
|
70
72
|
const promises: Array<Promise<void>> = [];
|
71
73
|
if (process.env.SBVR_SERVER_ENABLED) {
|