@balena/pinejs 17.0.0-build-large-file-uploads-293e65ee371a69130834c50fef2f7f42cc18133f-1 → 17.0.0-build-17-x-74b0e8403edbd7922c684e3ce19bdb42ac41846b-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.
Files changed (66) hide show
  1. package/.pinejs-cache.json +1 -1
  2. package/.versionbot/CHANGELOG.yml +2295 -17
  3. package/CHANGELOG.md +860 -5
  4. package/out/bin/abstract-sql-compiler.js +1 -1
  5. package/out/bin/abstract-sql-compiler.js.map +1 -1
  6. package/out/bin/sbvr-compiler.js +1 -1
  7. package/out/bin/sbvr-compiler.js.map +1 -1
  8. package/out/database-layer/db.js +30 -19
  9. package/out/database-layer/db.js.map +1 -1
  10. package/out/http-transactions/transactions.js +2 -2
  11. package/out/http-transactions/transactions.js.map +1 -1
  12. package/out/migrator/async.js +8 -9
  13. package/out/migrator/async.js.map +1 -1
  14. package/out/migrator/sync.js +6 -6
  15. package/out/migrator/sync.js.map +1 -1
  16. package/out/pinejs-session-store/pinejs-session-store.js +103 -108
  17. package/out/pinejs-session-store/pinejs-session-store.js.map +1 -1
  18. package/out/sbvr-api/abstract-sql.js +1 -1
  19. package/out/sbvr-api/abstract-sql.js.map +1 -1
  20. package/out/sbvr-api/errors.js +3 -0
  21. package/out/sbvr-api/errors.js.map +1 -1
  22. package/out/sbvr-api/hooks.js +4 -5
  23. package/out/sbvr-api/hooks.js.map +1 -1
  24. package/out/sbvr-api/permissions.js +3 -3
  25. package/out/sbvr-api/permissions.js.map +1 -1
  26. package/out/sbvr-api/sbvr-utils.d.ts +5 -12
  27. package/out/sbvr-api/sbvr-utils.js +16 -15
  28. package/out/sbvr-api/sbvr-utils.js.map +1 -1
  29. package/out/sbvr-api/uri-parser.js +1 -1
  30. package/out/sbvr-api/uri-parser.js.map +1 -1
  31. package/out/server-glue/module.js +0 -2
  32. package/out/server-glue/module.js.map +1 -1
  33. package/out/webresource-handler/handlers/NoopHandler.d.ts +1 -3
  34. package/out/webresource-handler/handlers/NoopHandler.js +0 -6
  35. package/out/webresource-handler/handlers/NoopHandler.js.map +1 -1
  36. package/out/webresource-handler/handlers/S3Handler.d.ts +28 -0
  37. package/out/webresource-handler/handlers/S3Handler.js +104 -0
  38. package/out/webresource-handler/handlers/S3Handler.js.map +1 -0
  39. package/out/webresource-handler/handlers/index.d.ts +1 -0
  40. package/out/webresource-handler/handlers/index.js +1 -0
  41. package/out/webresource-handler/handlers/index.js.map +1 -1
  42. package/out/webresource-handler/index.d.ts +7 -31
  43. package/out/webresource-handler/index.js +22 -23
  44. package/out/webresource-handler/index.js.map +1 -1
  45. package/package.json +40 -39
  46. package/src/bin/abstract-sql-compiler.ts +1 -1
  47. package/src/bin/sbvr-compiler.ts +1 -1
  48. package/src/http-transactions/transactions.js +2 -2
  49. package/src/migrator/async.ts +10 -11
  50. package/src/migrator/sync.ts +6 -6
  51. package/src/sbvr-api/abstract-sql.ts +1 -1
  52. package/src/sbvr-api/permissions.ts +3 -3
  53. package/src/sbvr-api/sbvr-utils.ts +21 -30
  54. package/src/sbvr-api/uri-parser.ts +1 -1
  55. package/src/server-glue/module.ts +0 -2
  56. package/src/webresource-handler/handlers/NoopHandler.ts +1 -14
  57. package/src/webresource-handler/handlers/S3Handler.ts +143 -0
  58. package/src/webresource-handler/handlers/index.ts +1 -0
  59. package/src/webresource-handler/index.ts +22 -64
  60. package/tsconfig.json +1 -1
  61. package/out/webresource-handler/multipartUpload.d.ts +0 -16
  62. package/out/webresource-handler/multipartUpload.js +0 -182
  63. package/out/webresource-handler/multipartUpload.js.map +0 -1
  64. package/out/webresource-handler/webresource.sbvr +0 -63
  65. package/src/webresource-handler/multipartUpload.ts +0 -275
  66. package/src/webresource-handler/webresource.sbvr +0 -63
@@ -1 +0,0 @@
1
- {"version":3,"file":"multipartUpload.js","sourceRoot":"","sources":["../../src/webresource-handler/multipartUpload.ts"],"names":[],"mappings":";;;AACA,6CAAyC;AAOzC,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CAAC;IAEF,OAAO,EAAE,IAAI,EAAE,kBAAkB,EAAE,QAAQ,EAAE,CAAC;AAC/C,CAAC,CAAC"}
@@ -1,63 +0,0 @@
1
- Vocabulary: Auth
2
-
3
- Term: actor
4
- Term: expiry date
5
- Concept Type: Date Time (Type)
6
-
7
- Vocabulary: webresource
8
-
9
- Term: uuid
10
- Concept Type: Short Text (Type)
11
- Term: resource name
12
- Concept Type: Short Text (Type)
13
- Term: field name
14
- Concept Type: Short Text (Type)
15
- Term: resource id
16
- Concept Type: Integer (Type)
17
- Term: upload id
18
- Concept Type: Short Text (Type)
19
- Term: file key
20
- Concept Type: Short Text (Type)
21
- Term: status
22
- Concept Type: Short Text (Type)
23
- Term: filename
24
- Concept Type: Short Text (Type)
25
- Term: content type
26
- Concept Type: Short Text (Type)
27
- Term: size
28
- Concept Type: Integer (Type)
29
- Term: chunk size
30
- Concept Type: Integer (Type)
31
- Term: valid until date
32
- Concept Type: Date Time (Type)
33
-
34
- Term: multipart upload
35
- Fact type: multipart upload has uuid
36
- Necessity: each multipart upload has exactly one uuid
37
- Necessity: each uuid is of exactly one multipart upload
38
- Fact type: multipart upload has resource name
39
- Necessity: each multipart upload has exactly one resource name
40
- Fact type: multipart upload has field name
41
- Necessity: each multipart upload has exactly one field name
42
- Fact type: multipart upload has resource id
43
- Necessity: each multipart upload has exactly one resource id
44
- Fact type: multipart upload has upload id
45
- Necessity: each multipart upload has exactly one upload id
46
- Fact type: multipart upload has file key
47
- Necessity: each multipart upload has exactly one file key
48
- Fact type: multipart upload has status
49
- Necessity: each multipart upload has exactly one status
50
- Definition: "pending" or "completed" or "cancelled"
51
- Fact type: multipart upload has filename
52
- Necessity: each multipart upload has exactly one filename
53
- Fact type: multipart upload has content type
54
- Necessity: each multipart upload has exactly one content type
55
- Fact type: multipart upload has size
56
- Necessity: each multipart upload has exactly one size
57
- Fact type: multipart upload has chunk size
58
- Necessity: each multipart upload has exactly one chunk size
59
- Fact type: multipart upload has expiry date (Auth)
60
- Necessity: each multipart upload has exactly one expiry date (Auth)
61
- Fact type: multipart upload is created by actor (Auth)
62
- Necessity: each multipart upload is created by at most one actor (Auth)
63
- Reference Type: informative
@@ -1,275 +0,0 @@
1
- import type { WebResourceType as WebResource } from '@balena/sbvr-types';
2
- import { randomUUID } from 'node:crypto';
3
- import type { AnyObject } from 'pinejs-client-core';
4
- import type {
5
- BeginMultipartUploadPayload,
6
- UploadPart,
7
- WebResourceHandler,
8
- } from '.';
9
- import { getWebResourceFields } from '.';
10
- import type { PinejsClient } from '../sbvr-api/sbvr-utils';
11
- import { api } from '../sbvr-api/sbvr-utils';
12
- import type { ODataRequest } from '../sbvr-api/uri-parser';
13
- import { errors, sbvrUtils } from '../server-glue/module';
14
-
15
- type BeginUploadDbCheck = BeginMultipartUploadPayload & WebResource;
16
-
17
- export interface PendingUpload extends BeginMultipartUploadPayload {
18
- fieldName: string;
19
- fileKey: string;
20
- uploadId: string;
21
- }
22
-
23
- export interface BeginUploadResponse {
24
- [fieldName: string]: {
25
- uuid: string;
26
- uploadParts: UploadPart[];
27
- };
28
- }
29
-
30
- const MB = 1024 * 1024;
31
-
32
- export const multipartUploadHooks = (
33
- webResourceHandler: WebResourceHandler,
34
- ): sbvrUtils.Hooks => {
35
- return {
36
- POSTPARSE: async ({ req, request, tx, api: applicationApi }) => {
37
- if (request.odataQuery.property?.resource === 'beginUpload') {
38
- const uploadParams = await validateBeginUpload(request, applicationApi);
39
-
40
- // This transaction is necessary because beginUpload requests
41
- // will rollback the transaction (in order to first validate)
42
- // The metadata requested. If we don't pass any transaction
43
- // It will use the default transaction handler which will error out
44
- // on any rollback.
45
- tx = await sbvrUtils.db.transaction();
46
- req.tx = tx;
47
- request.tx = tx;
48
-
49
- request.method = 'PATCH';
50
- request.values = uploadParams;
51
- request.odataQuery.resource = request.resourceName;
52
- delete request.odataQuery.property;
53
- request.custom.isAction = 'beginUpload';
54
- } else if (request.odataQuery.property?.resource === 'commitUpload') {
55
- const commitPayload = await validateCommitUpload(
56
- request,
57
- applicationApi,
58
- );
59
-
60
- const webresource = await webResourceHandler.commitMultipartUpload({
61
- fileKey: commitPayload.metadata.fileKey,
62
- uploadId: commitPayload.metadata.uploadId,
63
- filename: commitPayload.metadata.filename,
64
- providerCommitData: commitPayload.providerCommitData,
65
- });
66
-
67
- await api.webresource.patch({
68
- resource: 'multipart_upload',
69
- body: {
70
- status: 'completed',
71
- },
72
- options: {
73
- $filter: {
74
- uuid: commitPayload.uuid,
75
- },
76
- },
77
- passthrough: {
78
- tx: tx,
79
- },
80
- });
81
-
82
- request.method = 'PATCH';
83
- request.values = {
84
- [commitPayload.metadata.fieldName]: webresource,
85
- };
86
- request.odataQuery.resource = request.resourceName;
87
- delete request.odataQuery.property;
88
- request.custom.isAction = 'commitUpload';
89
- request.custom.commitUploadPayload = webresource;
90
- }
91
- },
92
- PRERESPOND: async ({ req, request, response, tx }) => {
93
- if (request.custom.isAction === 'beginUpload') {
94
- // In the case where the transaction has failed because it had invalid payload
95
- // such as breaking a db constraint, this hook wouldn't have been called
96
- // and would rather throw with the rule it failed to validate
97
- // We rollback here as the patch was just a way to validate the upload payload
98
- await tx.rollback();
99
-
100
- response.statusCode = 200;
101
- response.body = await beginUpload(
102
- webResourceHandler,
103
- request,
104
- req.user?.actor,
105
- );
106
- } else if (request.custom.isAction === 'commitUpload') {
107
- response.body = await webResourceHandler.onPreRespond(
108
- request.custom.commitUploadPayload,
109
- );
110
- }
111
- },
112
- };
113
- };
114
-
115
- export const beginUpload = async (
116
- webResourceHandler: WebResourceHandler,
117
- odataRequest: ODataRequest,
118
- actorId?: number,
119
- ): Promise<BeginUploadResponse> => {
120
- const payload = odataRequest.values as {
121
- [x: string]: BeginMultipartUploadPayload;
122
- };
123
- const fieldName = Object.keys(payload)[0];
124
- const metadata = payload[fieldName];
125
-
126
- const { fileKey, uploadId, uploadParts } =
127
- await webResourceHandler.beginMultipartUpload(fieldName, metadata);
128
- const uuid = randomUUID();
129
-
130
- try {
131
- await api.webresource.post({
132
- resource: 'multipart_upload',
133
- body: {
134
- uuid,
135
- resource_name: odataRequest.resourceName,
136
- field_name: fieldName,
137
- resource_id: odataRequest.affectedIds?.[0],
138
- upload_id: uploadId,
139
- file_key: fileKey,
140
- status: 'pending',
141
- filename: metadata.filename,
142
- content_type: metadata.content_type,
143
- size: metadata.size,
144
- chunk_size: metadata.chunk_size,
145
- expiry_date: Date.now() + 7 * 24 * 60 * 60 * 1000, // 7 days in ms
146
- is_created_by__actor: actorId,
147
- },
148
- });
149
- } catch (err) {
150
- console.error('failed to start multipart upload', err);
151
- throw new errors.BadRequestError('Failed to start multipart upload');
152
- }
153
-
154
- return { [fieldName]: { uuid, uploadParts } };
155
- };
156
-
157
- const validateBeginUpload = async (
158
- request: ODataRequest,
159
- applicationApi: PinejsClient,
160
- ) => {
161
- if (request.odataQuery.key == null) {
162
- throw new errors.BadRequestError();
163
- }
164
-
165
- await applicationApi.post({
166
- url: request.url.substring(1).replace('beginUpload', 'canAccess'),
167
- body: { method: 'PATCH' },
168
- });
169
-
170
- const fieldNames = Object.keys(request.values);
171
- if (fieldNames.length !== 1) {
172
- throw new errors.BadRequestError(
173
- 'You can only get upload url for one field at a time',
174
- );
175
- }
176
-
177
- const [fieldName] = fieldNames;
178
- const webResourceFields = getWebResourceFields(request, false);
179
- if (!webResourceFields.includes(fieldName)) {
180
- throw new errors.BadRequestError(
181
- `You must provide a valid webresource field from: ${JSON.stringify(webResourceFields)}`,
182
- );
183
- }
184
-
185
- const beginUploadPayload = parseBeginUploadPayload(request.values[fieldName]);
186
- if (beginUploadPayload == null) {
187
- throw new errors.BadRequestError('Invalid file metadata');
188
- }
189
-
190
- const uploadMetadataCheck: BeginUploadDbCheck = {
191
- ...beginUploadPayload,
192
- href: 'metadata_check',
193
- };
194
-
195
- return { [fieldName]: uploadMetadataCheck };
196
- };
197
-
198
- const parseBeginUploadPayload = (
199
- payload: AnyObject,
200
- ): BeginMultipartUploadPayload | null => {
201
- if (typeof payload !== 'object') {
202
- return null;
203
- }
204
-
205
- let { filename, content_type, size, chunk_size } = payload;
206
- if (
207
- typeof filename !== 'string' ||
208
- typeof content_type !== 'string' ||
209
- typeof size !== 'number' ||
210
- (chunk_size != null && typeof chunk_size !== 'number') ||
211
- (chunk_size != null && chunk_size < 5 * MB)
212
- ) {
213
- return null;
214
- }
215
-
216
- if (chunk_size == null) {
217
- chunk_size = 5 * MB;
218
- }
219
- return { filename, content_type, size, chunk_size };
220
- };
221
-
222
- const validateCommitUpload = async (
223
- request: ODataRequest,
224
- applicationApi: PinejsClient,
225
- ) => {
226
- if (request.odataQuery.key == null) {
227
- throw new errors.BadRequestError();
228
- }
229
-
230
- await applicationApi.post({
231
- url: request.url.substring(1).replace('commitUpload', 'canAccess'),
232
- body: { method: 'PATCH' },
233
- });
234
-
235
- const { uuid, providerCommitData } = request.values;
236
- if (typeof uuid !== 'string') {
237
- throw new errors.BadRequestError('Invalid uuid type');
238
- }
239
-
240
- const [multipartUpload] = (await api.webresource.get({
241
- resource: 'multipart_upload',
242
- options: {
243
- $select: ['id', 'file_key', 'upload_id', 'field_name', 'filename'],
244
- $filter: {
245
- uuid,
246
- status: 'pending',
247
- expiry_date: { $gt: { $now: {} } },
248
- },
249
- },
250
- passthrough: {
251
- tx: request.tx,
252
- },
253
- })) as [
254
- {
255
- id: number;
256
- file_key: string;
257
- upload_id: string;
258
- field_name: string;
259
- filename: string;
260
- }?,
261
- ];
262
-
263
- if (multipartUpload == null) {
264
- throw new errors.BadRequestError(`Invalid upload for uuid ${uuid}`);
265
- }
266
-
267
- const metadata = {
268
- fileKey: multipartUpload.file_key,
269
- uploadId: multipartUpload.upload_id,
270
- filename: multipartUpload.filename,
271
- fieldName: multipartUpload.field_name,
272
- };
273
-
274
- return { uuid, providerCommitData, metadata };
275
- };
@@ -1,63 +0,0 @@
1
- Vocabulary: Auth
2
-
3
- Term: actor
4
- Term: expiry date
5
- Concept Type: Date Time (Type)
6
-
7
- Vocabulary: webresource
8
-
9
- Term: uuid
10
- Concept Type: Short Text (Type)
11
- Term: resource name
12
- Concept Type: Short Text (Type)
13
- Term: field name
14
- Concept Type: Short Text (Type)
15
- Term: resource id
16
- Concept Type: Integer (Type)
17
- Term: upload id
18
- Concept Type: Short Text (Type)
19
- Term: file key
20
- Concept Type: Short Text (Type)
21
- Term: status
22
- Concept Type: Short Text (Type)
23
- Term: filename
24
- Concept Type: Short Text (Type)
25
- Term: content type
26
- Concept Type: Short Text (Type)
27
- Term: size
28
- Concept Type: Integer (Type)
29
- Term: chunk size
30
- Concept Type: Integer (Type)
31
- Term: valid until date
32
- Concept Type: Date Time (Type)
33
-
34
- Term: multipart upload
35
- Fact type: multipart upload has uuid
36
- Necessity: each multipart upload has exactly one uuid
37
- Necessity: each uuid is of exactly one multipart upload
38
- Fact type: multipart upload has resource name
39
- Necessity: each multipart upload has exactly one resource name
40
- Fact type: multipart upload has field name
41
- Necessity: each multipart upload has exactly one field name
42
- Fact type: multipart upload has resource id
43
- Necessity: each multipart upload has exactly one resource id
44
- Fact type: multipart upload has upload id
45
- Necessity: each multipart upload has exactly one upload id
46
- Fact type: multipart upload has file key
47
- Necessity: each multipart upload has exactly one file key
48
- Fact type: multipart upload has status
49
- Necessity: each multipart upload has exactly one status
50
- Definition: "pending" or "completed" or "cancelled"
51
- Fact type: multipart upload has filename
52
- Necessity: each multipart upload has exactly one filename
53
- Fact type: multipart upload has content type
54
- Necessity: each multipart upload has exactly one content type
55
- Fact type: multipart upload has size
56
- Necessity: each multipart upload has exactly one size
57
- Fact type: multipart upload has chunk size
58
- Necessity: each multipart upload has exactly one chunk size
59
- Fact type: multipart upload has expiry date (Auth)
60
- Necessity: each multipart upload has exactly one expiry date (Auth)
61
- Fact type: multipart upload is created by actor (Auth)
62
- Necessity: each multipart upload is created by at most one actor (Auth)
63
- Reference Type: informative