@balena/pinejs 21.4.0-build-add-actions-example-6ac2b3e43d47ee50b22cbf3985c736f454ad73bf-1 → 21.4.0-build-add-odata-actions-8d4944e07924e5cf0a69183c18610e8fd2c41d3b-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 +2 -50
- package/CHANGELOG.md +0 -6
- package/out/config-loader/config-loader.js +7 -8
- package/out/config-loader/config-loader.js.map +1 -1
- package/out/config-loader/env.d.ts +0 -4
- package/out/config-loader/env.js +0 -4
- package/out/config-loader/env.js.map +1 -1
- package/out/sbvr-api/actions.js +1 -1
- package/out/sbvr-api/actions.js.map +1 -1
- package/out/server-glue/module.js +0 -2
- package/out/server-glue/module.js.map +1 -1
- package/out/tasks/index.d.ts +0 -1
- package/out/tasks/index.js +1 -6
- package/out/tasks/index.js.map +1 -1
- package/out/webresource-handler/index.d.ts +1 -10
- package/out/webresource-handler/index.js +45 -83
- package/out/webresource-handler/index.js.map +1 -1
- package/package.json +5 -5
- package/src/config-loader/config-loader.ts +9 -9
- package/src/config-loader/env.ts +0 -11
- package/src/sbvr-api/actions.ts +1 -1
- package/src/server-glue/module.ts +0 -2
- package/src/tasks/index.ts +1 -9
- package/src/webresource-handler/index.ts +83 -117
- package/out/tasks/pine-tasks.d.ts +0 -1
- package/out/tasks/pine-tasks.js +0 -7
- package/out/tasks/pine-tasks.js.map +0 -1
- package/out/webresource-handler/actions/beginUpload.d.ts +0 -3
- package/out/webresource-handler/actions/beginUpload.js +0 -105
- package/out/webresource-handler/actions/beginUpload.js.map +0 -1
- package/out/webresource-handler/actions/canceUpload.d.ts +0 -3
- package/out/webresource-handler/actions/canceUpload.js +0 -58
- package/out/webresource-handler/actions/canceUpload.js.map +0 -1
- package/out/webresource-handler/actions/commitUpload.d.ts +0 -3
- package/out/webresource-handler/actions/commitUpload.js +0 -85
- package/out/webresource-handler/actions/commitUpload.js.map +0 -1
- package/out/webresource-handler/delete-file-task.d.ts +0 -1
- package/out/webresource-handler/delete-file-task.js +0 -37
- package/out/webresource-handler/delete-file-task.js.map +0 -1
- package/out/webresource-handler/multipartUpload.d.ts +0 -4
- package/out/webresource-handler/multipartUpload.js +0 -14
- package/out/webresource-handler/multipartUpload.js.map +0 -1
- package/out/webresource-handler/webresource.d.ts +0 -42
- package/out/webresource-handler/webresource.js +0 -2
- package/out/webresource-handler/webresource.js.map +0 -1
- package/out/webresource-handler/webresource.sbvr +0 -60
- package/src/tasks/pine-tasks.ts +0 -7
- package/src/webresource-handler/actions/beginUpload.ts +0 -163
- package/src/webresource-handler/actions/canceUpload.ts +0 -92
- package/src/webresource-handler/actions/commitUpload.ts +0 -117
- package/src/webresource-handler/delete-file-task.ts +0 -42
- package/src/webresource-handler/multipartUpload.ts +0 -23
- package/src/webresource-handler/webresource.sbvr +0 -60
- package/src/webresource-handler/webresource.ts +0 -48
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@balena/pinejs",
|
3
|
-
"version": "21.4.0-build-add-actions-
|
3
|
+
"version": "21.4.0-build-add-odata-actions-8d4944e07924e5cf0a69183c18610e8fd2c41d3b-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
|
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",
|
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 &&
|
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"
|
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": "^1.0.4",
|
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-04T19:
|
152
|
+
"publishedAt": "2025-05-04T19:36:59.817Z"
|
153
153
|
}
|
154
154
|
}
|
@@ -33,7 +33,6 @@ import {
|
|
33
33
|
type WebResourceHandler,
|
34
34
|
setupUploadHooks,
|
35
35
|
setupWebresourceHandler,
|
36
|
-
setupUploadActions,
|
37
36
|
} from '../webresource-handler/index.js';
|
38
37
|
import type { AliasValidNodeType } from '../sbvr-api/translations.js';
|
39
38
|
|
@@ -222,17 +221,18 @@ export const setup = (app: Express.Application) => {
|
|
222
221
|
.delete(sbvrUtils.handleODataRequest);
|
223
222
|
|
224
223
|
const loadedModel = sbvrUtils.getModel(model.apiRoot!);
|
225
|
-
|
226
|
-
.
|
227
|
-
|
224
|
+
if (translateTo == null) {
|
225
|
+
const resourceNames = Object.values(
|
226
|
+
loadedModel.abstractSql.tables,
|
228
227
|
)
|
229
|
-
|
228
|
+
.filter((table) =>
|
229
|
+
table.fields.some((f) => f.dataType === 'WebResource'),
|
230
|
+
)
|
231
|
+
.map((table) => table.name);
|
230
232
|
|
231
|
-
|
232
|
-
|
233
|
-
setupUploadHooks(model.apiRoot!, resource);
|
233
|
+
for (const resource of new Set(resourceNames)) {
|
234
|
+
setupUploadHooks(webResourceHandler, model.apiRoot!, resource);
|
234
235
|
}
|
235
|
-
setupUploadActions(model.apiRoot!, resource);
|
236
236
|
}
|
237
237
|
|
238
238
|
console.info(
|
package/src/config-loader/env.ts
CHANGED
@@ -159,17 +159,6 @@ 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
|
-
|
173
162
|
export const guardTestMockOnly = () => {
|
174
163
|
if (process.env.DEPLOYMENT !== 'TEST') {
|
175
164
|
throw new Error('Attempting to use TEST_MOCK_ONLY outside of tests');
|
package/src/sbvr-api/actions.ts
CHANGED
@@ -67,7 +67,7 @@ export const runAction = async (
|
|
67
67
|
throw new BadRequestError();
|
68
68
|
}
|
69
69
|
|
70
|
-
return sbvrUtils.db.transaction(async (tx) => {
|
70
|
+
return await sbvrUtils.db.transaction(async (tx) => {
|
71
71
|
// in practice, the parser does not currently allow actions without a key
|
72
72
|
// e.g. /vocab/resource(...)/action compiles but
|
73
73
|
// /vocab/resource/action won't even compile, so (current) reality is that if canAcccess
|
@@ -8,7 +8,6 @@ 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';
|
12
11
|
|
13
12
|
import * as sbvrUtils from '../sbvr-api/sbvr-utils.js';
|
14
13
|
import { PINEJS_ADVISORY_LOCK } from '../config-loader/env.js';
|
@@ -68,7 +67,6 @@ export const init = async <T extends string>(
|
|
68
67
|
const cfgLoader = configLoader.setup(app);
|
69
68
|
await cfgLoader.loadConfig(migrator.config);
|
70
69
|
await cfgLoader.loadConfig(tasks.config);
|
71
|
-
await cfgLoader.loadConfig(webresource.config);
|
72
70
|
|
73
71
|
if (!process.env.CONFIG_LOADER_DISABLED) {
|
74
72
|
await cfgLoader.loadApplicationConfig(config);
|
package/src/tasks/index.ts
CHANGED
@@ -11,7 +11,6 @@ import type TasksModel from './tasks.js';
|
|
11
11
|
import type { Task } from './tasks.js';
|
12
12
|
import type { FromSchema } from 'json-schema-to-ts';
|
13
13
|
import { importSBVR } from '../server-glue/sbvr-loader.js';
|
14
|
-
import { addPineTaskHandlers } from './pine-tasks.js';
|
15
14
|
|
16
15
|
export type * from './tasks.js';
|
17
16
|
|
@@ -45,14 +44,9 @@ export const config: ConfigLoader.Config = {
|
|
45
44
|
};
|
46
45
|
|
47
46
|
export let worker: Worker | null = null;
|
48
|
-
|
49
|
-
export function isPineTasksAvailable() {
|
50
|
-
return sbvrUtils.db.engine === 'postgres';
|
51
|
-
}
|
52
|
-
|
53
47
|
export function setup(): void {
|
54
48
|
// Async task functionality is only supported on Postgres
|
55
|
-
if (
|
49
|
+
if (sbvrUtils.db.engine !== 'postgres') {
|
56
50
|
console.warn('Skipping task setup as database not supported');
|
57
51
|
return;
|
58
52
|
}
|
@@ -131,8 +125,6 @@ export function setup(): void {
|
|
131
125
|
}
|
132
126
|
},
|
133
127
|
});
|
134
|
-
|
135
|
-
addPineTaskHandlers();
|
136
128
|
}
|
137
129
|
|
138
130
|
// Register a task handler
|
@@ -11,20 +11,10 @@ import {
|
|
11
11
|
odataNameToSqlName,
|
12
12
|
sqlNameToODataName,
|
13
13
|
} from '@balena/odata-to-abstract-sql';
|
14
|
-
import type { ConfigLoader } from '../server-glue/module.js';
|
15
14
|
import { errors, permissions } from '../server-glue/module.js';
|
16
15
|
import type { WebResourceType as WebResource } from '@balena/sbvr-types';
|
17
16
|
import { TypedError } from 'typed-error';
|
18
17
|
import type { Resolvable } from '../sbvr-api/common-types.js';
|
19
|
-
import type { Tx } from '../database-layer/db.js';
|
20
|
-
import { isPineTasksAvailable } from '../tasks/index.js';
|
21
|
-
import type WebresourceModel from './webresource.js';
|
22
|
-
import { importSBVR } from '../server-glue/sbvr-loader.js';
|
23
|
-
import { beginUploadAction } from './actions/beginUpload.js';
|
24
|
-
import { isMultipartUploadAvailable } from './multipartUpload.js';
|
25
|
-
import { commitUploadAction } from './actions/commitUpload.js';
|
26
|
-
import { cancelUploadAction } from './actions/canceUpload.js';
|
27
|
-
import { addAction } from '../sbvr-api/actions.js';
|
28
18
|
|
29
19
|
export * from './handlers/index.js';
|
30
20
|
|
@@ -326,34 +316,12 @@ export const getWebResourceFields = (
|
|
326
316
|
.map((f) => sqlNameToODataName(f.fieldName));
|
327
317
|
};
|
328
318
|
|
329
|
-
const
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
passthrough: {
|
336
|
-
req: permissions.root,
|
337
|
-
tx,
|
338
|
-
},
|
339
|
-
body: {
|
340
|
-
key: crypto.randomUUID(),
|
341
|
-
is_executed_by__handler: 'delete_webresource_file',
|
342
|
-
is_executed_with__parameter_set: {
|
343
|
-
fileKey: fileKey,
|
344
|
-
},
|
345
|
-
attempt_limit: 2 ** 31 - 1,
|
346
|
-
},
|
347
|
-
});
|
348
|
-
}),
|
349
|
-
);
|
350
|
-
} else {
|
351
|
-
await Promise.all(
|
352
|
-
keysToDelete.map(async (fileKey) => {
|
353
|
-
return await configuredWebResourceHandler?.removeFile(fileKey);
|
354
|
-
}),
|
355
|
-
);
|
356
|
-
}
|
319
|
+
const deleteFiles = async (
|
320
|
+
keysToDelete: string[],
|
321
|
+
webResourceHandler: WebResourceHandler,
|
322
|
+
) => {
|
323
|
+
const promises = keysToDelete.map((r) => webResourceHandler.removeFile(r));
|
324
|
+
await Promise.all(promises);
|
357
325
|
};
|
358
326
|
|
359
327
|
const throwIfWebresourceNotInMultipart = (
|
@@ -361,13 +329,6 @@ const throwIfWebresourceNotInMultipart = (
|
|
361
329
|
{ req, request }: HookArgs,
|
362
330
|
) => {
|
363
331
|
if (
|
364
|
-
// root needs to be able to bypass the multipart check as
|
365
|
-
// it needs to pass the direct payload on multipart uploads (on storage provider)
|
366
|
-
req.user !== permissions.root.user &&
|
367
|
-
// This is checking for HTTP multipart form submission/request (e.g. send the actual file via the API)
|
368
|
-
// Not to confuse with multipart uploads
|
369
|
-
// See: https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Methods/POST#multipart_form_submission
|
370
|
-
// See: https://docs.aws.amazon.com/AmazonS3/latest/userguide/mpuoverview.html
|
371
332
|
!req.is?.('multipart') &&
|
372
333
|
webResourceFields.some((field) => request.values[field] != null)
|
373
334
|
) {
|
@@ -377,22 +338,18 @@ const throwIfWebresourceNotInMultipart = (
|
|
377
338
|
}
|
378
339
|
};
|
379
340
|
|
380
|
-
const getCreateWebResourceHooks = (
|
341
|
+
const getCreateWebResourceHooks = (
|
342
|
+
webResourceHandler: WebResourceHandler,
|
343
|
+
): sbvrUtils.Hooks => {
|
381
344
|
return {
|
382
345
|
PRERUN: (hookArgs) => {
|
383
346
|
const webResourceFields = getWebResourceFields(hookArgs.request);
|
384
347
|
throwIfWebresourceNotInMultipart(webResourceFields, hookArgs);
|
385
348
|
},
|
386
|
-
'POSTRUN-ERROR':
|
387
|
-
|
388
|
-
|
389
|
-
|
390
|
-
return;
|
391
|
-
}
|
392
|
-
|
393
|
-
const keysToDelete = getWebResourcesKeysFromRequest(fields, request);
|
394
|
-
// Explicitely not passing tx as it will be rolledback as it is an error hook
|
395
|
-
await deleteOnStorage(keysToDelete);
|
349
|
+
'POSTRUN-ERROR': ({ tx, request }) => {
|
350
|
+
tx.on('rollback', () => {
|
351
|
+
void deleteRollbackPendingFields(request, webResourceHandler);
|
352
|
+
});
|
396
353
|
},
|
397
354
|
};
|
398
355
|
};
|
@@ -421,12 +378,21 @@ const getWebResourcesKeysFromRequest = (
|
|
421
378
|
.filter((href) => href != null);
|
422
379
|
};
|
423
380
|
|
424
|
-
const getRemoveWebResourceHooks = (
|
381
|
+
const getRemoveWebResourceHooks = (
|
382
|
+
webResourceHandler: WebResourceHandler,
|
383
|
+
): sbvrUtils.Hooks => {
|
425
384
|
return {
|
426
385
|
PRERUN: async (args) => {
|
427
386
|
const { api, request, tx } = args;
|
428
387
|
let webResourceFields = getWebResourceFields(request);
|
429
388
|
|
389
|
+
throwIfWebresourceNotInMultipart(webResourceFields, args);
|
390
|
+
|
391
|
+
// Request failed on DB roundtrip (e.g. DB constraint) and pending files need to be deleted
|
392
|
+
tx.on('rollback', () => {
|
393
|
+
void deleteRollbackPendingFields(request, webResourceHandler);
|
394
|
+
});
|
395
|
+
|
430
396
|
if (request.method === 'PATCH') {
|
431
397
|
webResourceFields = Object.entries(request.values)
|
432
398
|
.filter(
|
@@ -435,8 +401,6 @@ const getRemoveWebResourceHooks = (): sbvrUtils.Hooks => {
|
|
435
401
|
)
|
436
402
|
.map(([key]) => key);
|
437
403
|
}
|
438
|
-
request.custom.webResourceFields = webResourceFields;
|
439
|
-
throwIfWebresourceNotInMultipart(webResourceFields, args);
|
440
404
|
|
441
405
|
if (webResourceFields.length === 0) {
|
442
406
|
// No need to delete anything as no file is in the wire
|
@@ -447,32 +411,43 @@ const getRemoveWebResourceHooks = (): sbvrUtils.Hooks => {
|
|
447
411
|
// This can only be validated here because we need to first ensure the
|
448
412
|
// request is actually modifying a webresource before erroring out
|
449
413
|
if (request.method === 'PATCH' && request.odataQuery?.key == null) {
|
414
|
+
// When we get here, files have already been uploaded. We need to mark them for deletion.
|
415
|
+
const keysToDelete = getWebResourcesKeysFromRequest(
|
416
|
+
webResourceFields,
|
417
|
+
request,
|
418
|
+
);
|
419
|
+
|
420
|
+
// Set deletion of files on the wire as request will throw
|
421
|
+
tx.on('end', () => {
|
422
|
+
deletePendingFiles(keysToDelete, request, webResourceHandler);
|
423
|
+
});
|
424
|
+
|
450
425
|
throw new errors.BadRequestError(
|
451
426
|
'WebResources can only be updated when providing a resource key.',
|
452
427
|
);
|
453
428
|
}
|
454
429
|
|
430
|
+
// This can be > 1 in both DELETE requests or PATCH requests to not accessible IDs.
|
455
431
|
const ids = await sbvrUtils.getAffectedIds(args);
|
456
432
|
if (ids.length === 0) {
|
457
433
|
// Set deletion of files on the wire as no resource was affected
|
458
|
-
request
|
434
|
+
// Note that for DELETE requests it should not find any request on the wire
|
435
|
+
const keysToDelete = getWebResourcesKeysFromRequest(
|
459
436
|
webResourceFields,
|
460
437
|
request,
|
461
438
|
);
|
439
|
+
deletePendingFiles(keysToDelete, request, webResourceHandler);
|
462
440
|
return;
|
463
441
|
}
|
464
442
|
|
465
|
-
// If it reaches here, it means that it will try to patch/delete the webresource
|
466
|
-
// So we need (before postrun) get what are the current keys to be deleted
|
467
|
-
// if post run succeeds
|
468
443
|
const webResources = (await api.get({
|
469
444
|
resource: request.resourceName,
|
470
445
|
passthrough: {
|
471
|
-
tx,
|
446
|
+
tx: args.tx,
|
472
447
|
req: permissions.root,
|
473
448
|
},
|
474
449
|
options: {
|
475
|
-
$select:
|
450
|
+
$select: webResourceFields,
|
476
451
|
$filter: {
|
477
452
|
id: {
|
478
453
|
$in: ids,
|
@@ -480,34 +455,60 @@ const getRemoveWebResourceHooks = (): sbvrUtils.Hooks => {
|
|
480
455
|
},
|
481
456
|
},
|
482
457
|
})) as WebResourcesDbResponse[] | undefined | null;
|
483
|
-
request.custom.onPostRunDelete = getWebResourcesHrefs(webResources);
|
484
|
-
},
|
485
458
|
|
486
|
-
|
487
|
-
|
488
|
-
|
489
|
-
|
490
|
-
|
491
|
-
|
492
|
-
|
493
|
-
|
494
|
-
);
|
495
|
-
// Explicitely not passing tx as it will be rolledback as it is an error hook
|
496
|
-
await deleteOnStorage(keysToDelete);
|
459
|
+
// Deletes previous stored resources in case they were patched or the whole entity was deleted
|
460
|
+
tx.on('end', () => {
|
461
|
+
deletePendingFiles(
|
462
|
+
getWebResourcesHrefs(webResources),
|
463
|
+
request,
|
464
|
+
webResourceHandler,
|
465
|
+
);
|
466
|
+
});
|
497
467
|
},
|
498
468
|
};
|
499
469
|
};
|
500
470
|
|
471
|
+
const deleteRollbackPendingFields = async (
|
472
|
+
request: uriParser.ODataRequest,
|
473
|
+
webResourceHandler: WebResourceHandler,
|
474
|
+
) => {
|
475
|
+
const fields = getWebResourceFields(request);
|
476
|
+
|
477
|
+
if (fields.length === 0) {
|
478
|
+
return;
|
479
|
+
}
|
480
|
+
|
481
|
+
const keysToDelete = getWebResourcesKeysFromRequest(fields, request);
|
482
|
+
await deleteFiles(keysToDelete, webResourceHandler);
|
483
|
+
};
|
484
|
+
|
485
|
+
const deletePendingFiles = (
|
486
|
+
keysToDelete: string[],
|
487
|
+
request: uriParser.ODataRequest,
|
488
|
+
webResourceHandler: WebResourceHandler,
|
489
|
+
): void => {
|
490
|
+
// on purpose does not await for this promise to resolve
|
491
|
+
try {
|
492
|
+
void deleteFiles(keysToDelete, webResourceHandler);
|
493
|
+
} catch (err) {
|
494
|
+
getLogger(request.vocabulary).error(`Failed to delete pending files`, err);
|
495
|
+
}
|
496
|
+
};
|
497
|
+
|
501
498
|
export const getDefaultHandler = (): WebResourceHandler => {
|
502
499
|
return new NoopHandler();
|
503
500
|
};
|
504
501
|
|
505
|
-
export const setupUploadHooks = (
|
502
|
+
export const setupUploadHooks = (
|
503
|
+
handler: WebResourceHandler,
|
504
|
+
apiRoot: string,
|
505
|
+
resourceName: string,
|
506
|
+
) => {
|
506
507
|
sbvrUtils.addPureHook(
|
507
508
|
'DELETE',
|
508
509
|
apiRoot,
|
509
510
|
resourceName,
|
510
|
-
getRemoveWebResourceHooks(),
|
511
|
+
getRemoveWebResourceHooks(handler),
|
511
512
|
);
|
512
513
|
|
513
514
|
sbvrUtils.addPureHook(
|
@@ -515,48 +516,13 @@ export const setupUploadHooks = (apiRoot: string, resourceName: string) => {
|
|
515
516
|
apiRoot,
|
516
517
|
resourceName,
|
517
518
|
// PATCH also needs to remove the old resource in case a webresource was modified
|
518
|
-
getRemoveWebResourceHooks(),
|
519
|
+
getRemoveWebResourceHooks(handler),
|
519
520
|
);
|
520
521
|
|
521
522
|
sbvrUtils.addPureHook(
|
522
523
|
'POST',
|
523
524
|
apiRoot,
|
524
525
|
resourceName,
|
525
|
-
getCreateWebResourceHooks(),
|
526
|
+
getCreateWebResourceHooks(handler),
|
526
527
|
);
|
527
528
|
};
|
528
|
-
|
529
|
-
export const setupUploadActions = (apiRoot: string, resourceName: string) => {
|
530
|
-
const resource = sqlNameToODataName(resourceName);
|
531
|
-
if (isMultipartUploadAvailable(configuredWebResourceHandler)) {
|
532
|
-
addAction(apiRoot, resource, 'beginUpload', beginUploadAction);
|
533
|
-
|
534
|
-
addAction(apiRoot, resource, 'commitUpload', commitUploadAction);
|
535
|
-
|
536
|
-
addAction(apiRoot, resource, 'cancelUpload', cancelUploadAction);
|
537
|
-
}
|
538
|
-
};
|
539
|
-
|
540
|
-
const initSql = `
|
541
|
-
CREATE INDEX IF NOT EXISTS idx_multipart_upload_uuid ON "multipart upload" (uuid);
|
542
|
-
CREATE INDEX IF NOT EXISTS idx_multipart_upload_status ON "multipart upload" (status);
|
543
|
-
`;
|
544
|
-
|
545
|
-
const modelText = await importSBVR('./webresource.sbvr', import.meta);
|
546
|
-
|
547
|
-
declare module '../sbvr-api/sbvr-utils.js' {
|
548
|
-
export interface API {
|
549
|
-
webresource: PinejsClient<WebresourceModel>;
|
550
|
-
}
|
551
|
-
}
|
552
|
-
|
553
|
-
export const config: ConfigLoader.Config = {
|
554
|
-
models: [
|
555
|
-
{
|
556
|
-
modelName: 'webresource',
|
557
|
-
apiRoot: 'webresource',
|
558
|
-
modelText,
|
559
|
-
initSql,
|
560
|
-
},
|
561
|
-
],
|
562
|
-
};
|
@@ -1 +0,0 @@
|
|
1
|
-
export declare const addPineTaskHandlers: () => void;
|
package/out/tasks/pine-tasks.js
DELETED
@@ -1,7 +0,0 @@
|
|
1
|
-
import { tasks } from '../server-glue/module.js';
|
2
|
-
import { addDeleteFileTaskHandler } from '../webresource-handler/delete-file-task.js';
|
3
|
-
export const addPineTaskHandlers = () => {
|
4
|
-
addDeleteFileTaskHandler();
|
5
|
-
void tasks.worker?.start();
|
6
|
-
};
|
7
|
-
//# sourceMappingURL=pine-tasks.js.map
|
@@ -1 +0,0 @@
|
|
1
|
-
{"version":3,"file":"pine-tasks.js","sourceRoot":"","sources":["../../src/tasks/pine-tasks.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,0BAA0B,CAAC;AACjD,OAAO,EAAE,wBAAwB,EAAE,MAAM,4CAA4C,CAAC;AAEtF,MAAM,CAAC,MAAM,mBAAmB,GAAG,GAAG,EAAE;IACvC,wBAAwB,EAAE,CAAC;IAC3B,KAAK,KAAK,CAAC,MAAM,EAAE,KAAK,EAAE,CAAC;AAC5B,CAAC,CAAC"}
|
@@ -1,105 +0,0 @@
|
|
1
|
-
import { api } from '../../sbvr-api/sbvr-utils.js';
|
2
|
-
import { getMultipartUploadHandler } from '../multipartUpload.js';
|
3
|
-
import { getWebResourceFields } from '../index.js';
|
4
|
-
import { BadRequestError, NotImplementedError } from '../../sbvr-api/errors.js';
|
5
|
-
import { permissions, sbvrUtils } from '../../server-glue/module.js';
|
6
|
-
import { randomUUID } from 'crypto';
|
7
|
-
import { ajv } from '../../tasks/common.js';
|
8
|
-
const beginUploadPayloadSchema = {
|
9
|
-
type: 'object',
|
10
|
-
minProperties: 1,
|
11
|
-
maxProperties: 1,
|
12
|
-
additionalProperties: {
|
13
|
-
type: 'object',
|
14
|
-
properties: {
|
15
|
-
filename: { type: 'string' },
|
16
|
-
content_type: { type: 'string' },
|
17
|
-
size: { type: 'number' },
|
18
|
-
chunk_size: { type: 'number' },
|
19
|
-
},
|
20
|
-
required: ['filename', 'content_type', 'size'],
|
21
|
-
additionalProperties: false,
|
22
|
-
},
|
23
|
-
};
|
24
|
-
const validateBeginUpload = ajv.compile(beginUploadPayloadSchema);
|
25
|
-
export const beginUploadAction = async ({ request, tx, id, req, }) => {
|
26
|
-
if (typeof id !== 'number') {
|
27
|
-
throw new NotImplementedError('multipart upload do not yet support non-numeric ids');
|
28
|
-
}
|
29
|
-
const handler = getMultipartUploadHandler();
|
30
|
-
const { fieldName, beginUploadPayload } = parseBeginUpload(request, handler);
|
31
|
-
await runFakeDbPatch(request, {
|
32
|
-
[fieldName]: { ...beginUploadPayload, href: 'fake_patch' },
|
33
|
-
});
|
34
|
-
const { fileKey, uploadId, uploadParts } = await handler.multipartUpload.begin(fieldName, beginUploadPayload);
|
35
|
-
const uuid = randomUUID();
|
36
|
-
await api.webresource.post({
|
37
|
-
resource: 'multipart_upload',
|
38
|
-
body: {
|
39
|
-
uuid,
|
40
|
-
resource_name: request.resourceName,
|
41
|
-
field_name: fieldName,
|
42
|
-
resource_id: id,
|
43
|
-
upload_id: uploadId,
|
44
|
-
file_key: fileKey,
|
45
|
-
status: 'pending',
|
46
|
-
...beginUploadPayload,
|
47
|
-
expiry_date: Date.now() + 7 * 24 * 60 * 60 * 1000,
|
48
|
-
is_created_by__actor: req.user?.actor,
|
49
|
-
},
|
50
|
-
passthrough: {
|
51
|
-
req: permissions.root,
|
52
|
-
tx,
|
53
|
-
},
|
54
|
-
});
|
55
|
-
return {
|
56
|
-
body: { [fieldName]: { uuid, uploadParts } },
|
57
|
-
statusCode: 200,
|
58
|
-
};
|
59
|
-
};
|
60
|
-
const parseBeginUpload = (request, webResourceHandler) => {
|
61
|
-
const { values } = request;
|
62
|
-
const isValid = validateBeginUpload(values);
|
63
|
-
if (!isValid) {
|
64
|
-
throw new BadRequestError('Invalid begin upload payload');
|
65
|
-
}
|
66
|
-
const fieldName = Object.keys(values)[0];
|
67
|
-
const webResourceFields = getWebResourceFields(request, false);
|
68
|
-
if (!webResourceFields.includes(fieldName)) {
|
69
|
-
throw new BadRequestError(`The provided field '${fieldName}' is not a valid webresource`);
|
70
|
-
}
|
71
|
-
const beginUploadPayload = {
|
72
|
-
...values[fieldName],
|
73
|
-
chunk_size: values[fieldName].chunk_size ??
|
74
|
-
webResourceHandler.multipartUpload.getMinimumPartSize(),
|
75
|
-
};
|
76
|
-
if (beginUploadPayload.chunk_size <
|
77
|
-
webResourceHandler.multipartUpload.getMinimumPartSize()) {
|
78
|
-
throw new BadRequestError('Chunk size is too small');
|
79
|
-
}
|
80
|
-
return {
|
81
|
-
beginUploadPayload,
|
82
|
-
fieldName,
|
83
|
-
};
|
84
|
-
};
|
85
|
-
const runFakeDbPatch = async (request, fakeDbPatch) => {
|
86
|
-
const fakeTx = await sbvrUtils.db.transaction();
|
87
|
-
try {
|
88
|
-
const newUrl = request.url
|
89
|
-
.slice(1)
|
90
|
-
.split('?', 1)[0]
|
91
|
-
.replace(/\/beginUpload$/, '');
|
92
|
-
await api[request.vocabulary].request({
|
93
|
-
method: 'PATCH',
|
94
|
-
url: newUrl,
|
95
|
-
body: fakeDbPatch,
|
96
|
-
passthrough: { tx: fakeTx, req: permissions.root },
|
97
|
-
});
|
98
|
-
}
|
99
|
-
finally {
|
100
|
-
if (!fakeTx.isClosed()) {
|
101
|
-
await fakeTx.rollback();
|
102
|
-
}
|
103
|
-
}
|
104
|
-
};
|
105
|
-
//# sourceMappingURL=beginUpload.js.map
|
@@ -1 +0,0 @@
|
|
1
|
-
{"version":3,"file":"beginUpload.js","sourceRoot":"","sources":["../../../src/webresource-handler/actions/beginUpload.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAiB,MAAM,8BAA8B,CAAC;AAElE,OAAO,EAAE,yBAAyB,EAAE,MAAM,uBAAuB,CAAC;AAElE,OAAO,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAC;AACnD,OAAO,EAAE,eAAe,EAAE,mBAAmB,EAAE,MAAM,0BAA0B,CAAC;AAChF,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,6BAA6B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B,CAAC;IACF,CAAC;AACF,CAAC,CAAC"}
|
@@ -1,58 +0,0 @@
|
|
1
|
-
import { BadRequestError, NotImplementedError, UnauthorizedError, } from '../../sbvr-api/errors.js';
|
2
|
-
import { api } from '../../sbvr-api/sbvr-utils.js';
|
3
|
-
import { permissions } from '../../server-glue/module.js';
|
4
|
-
import { getMultipartUploadHandler } from '../multipartUpload.js';
|
5
|
-
export const cancelUploadAction = async ({ request, tx, id: resourceId, }) => {
|
6
|
-
if (typeof resourceId !== 'number') {
|
7
|
-
throw new NotImplementedError('multipart upload do not yet support non-numeric ids');
|
8
|
-
}
|
9
|
-
const { id, fileKey, uploadId } = await getOngoingUpload(request, resourceId, tx);
|
10
|
-
const handler = getMultipartUploadHandler();
|
11
|
-
await api.webresource.patch({
|
12
|
-
resource: 'multipart_upload',
|
13
|
-
body: {
|
14
|
-
status: 'cancelled',
|
15
|
-
},
|
16
|
-
id,
|
17
|
-
passthrough: {
|
18
|
-
tx: tx,
|
19
|
-
req: permissions.root,
|
20
|
-
},
|
21
|
-
});
|
22
|
-
await handler.multipartUpload.cancel({ fileKey, uploadId });
|
23
|
-
return {
|
24
|
-
statusCode: 204,
|
25
|
-
};
|
26
|
-
};
|
27
|
-
const getOngoingUpload = async (request, affectedId, tx) => {
|
28
|
-
const { uuid } = request.values;
|
29
|
-
if (uuid == null || typeof uuid !== 'string') {
|
30
|
-
throw new BadRequestError('Invalid uuid type');
|
31
|
-
}
|
32
|
-
const [multipartUpload] = await api.webresource.get({
|
33
|
-
resource: 'multipart_upload',
|
34
|
-
options: {
|
35
|
-
$select: ['id', 'file_key', 'upload_id'],
|
36
|
-
$filter: {
|
37
|
-
uuid,
|
38
|
-
status: 'pending',
|
39
|
-
expiry_date: { $gt: { $now: {} } },
|
40
|
-
resource_name: request.resourceName,
|
41
|
-
resource_id: affectedId,
|
42
|
-
},
|
43
|
-
},
|
44
|
-
passthrough: {
|
45
|
-
tx,
|
46
|
-
req: permissions.rootRead,
|
47
|
-
},
|
48
|
-
});
|
49
|
-
if (multipartUpload == null) {
|
50
|
-
throw new UnauthorizedError();
|
51
|
-
}
|
52
|
-
return {
|
53
|
-
id: multipartUpload.id,
|
54
|
-
fileKey: multipartUpload.file_key,
|
55
|
-
uploadId: multipartUpload.upload_id,
|
56
|
-
};
|
57
|
-
};
|
58
|
-
//# sourceMappingURL=canceUpload.js.map
|