@balena/pinejs-webresource-s3 2.1.49-build-commit-validate-provider-data-4173fa735fd1e982b454253cf167c93a5d1f6513-1 → 2.1.49
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/.versionbot/CHANGELOG.yml +6 -10
- package/CHANGELOG.md +1 -1
- package/build/index.js +8 -14
- package/build/index.js.map +1 -1
- package/lib/index.ts +9 -24
- package/package.json +2 -2
- package/test/index.test.ts +52 -35
|
@@ -1,21 +1,17 @@
|
|
|
1
1
|
- commits:
|
|
2
|
-
- subject:
|
|
3
|
-
hash:
|
|
2
|
+
- subject: Disable the signed url cache when its expiry is 0
|
|
3
|
+
hash: 7759b515b77531b49e917820e8ce62d8a8efe514
|
|
4
4
|
body: |
|
|
5
|
-
`
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
(`Parts: 'not-an-array'` was even serialized character by character)
|
|
9
|
-
and S3's error then surfaced as a 500. It is now checked to be a list
|
|
10
|
-
of parts with a `PartNumber` and `ETag`, rejecting anything else with a
|
|
11
|
-
400, and typed as `CompletedPart[]`.
|
|
5
|
+
memoizee only applies `maxAge` when it is truthy, so a
|
|
6
|
+
`signedUrlCacheExpireTimeSeconds` of 0 cached each file's signed url
|
|
7
|
+
forever, serving it long after it expired. 0 now skips the cache.
|
|
12
8
|
footer:
|
|
13
9
|
Change-type: patch
|
|
14
10
|
change-type: patch
|
|
15
11
|
author: Pagan Gazzard
|
|
16
12
|
version: 2.1.49
|
|
17
13
|
title: ""
|
|
18
|
-
date: 2026-10-27T22:
|
|
14
|
+
date: 2026-10-27T22:55:01.294Z
|
|
19
15
|
- commits:
|
|
20
16
|
- subject: Mark pinejs 25.x as compatible
|
|
21
17
|
hash: c41f9e36ecdd9215735cac36b7e04652b4131702
|
package/CHANGELOG.md
CHANGED
|
@@ -7,7 +7,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
|
|
|
7
7
|
# v2.1.49
|
|
8
8
|
## (2026-10-27)
|
|
9
9
|
|
|
10
|
-
*
|
|
10
|
+
* Disable the signed url cache when its expiry is 0 [Pagan Gazzard]
|
|
11
11
|
|
|
12
12
|
# v2.1.48
|
|
13
13
|
## (2026-10-27)
|
package/build/index.js
CHANGED
|
@@ -1,17 +1,12 @@
|
|
|
1
1
|
import { DeleteObjectCommand, GetObjectCommand, S3Client, CreateMultipartUploadCommand, UploadPartCommand, CompleteMultipartUploadCommand, HeadObjectCommand, AbortMultipartUploadCommand, } from '@aws-sdk/client-s3';
|
|
2
2
|
import { Upload } from '@aws-sdk/lib-storage';
|
|
3
3
|
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
|
|
4
|
-
import {
|
|
4
|
+
import { webResourceHandler } from '@balena/pinejs';
|
|
5
5
|
import memoize from 'memoizee';
|
|
6
6
|
import { randomUUID } from 'node:crypto';
|
|
7
7
|
const normalizeHref = (href) => {
|
|
8
8
|
return href.split('?')[0];
|
|
9
9
|
};
|
|
10
|
-
const isCompletedPartList = (parts) => Array.isArray(parts) &&
|
|
11
|
-
parts.every((part) => typeof part === 'object' &&
|
|
12
|
-
part != null &&
|
|
13
|
-
Number.isSafeInteger(part.PartNumber) &&
|
|
14
|
-
typeof part.ETag === 'string');
|
|
15
10
|
export class S3Handler {
|
|
16
11
|
config;
|
|
17
12
|
bucket;
|
|
@@ -44,9 +39,12 @@ export class S3Handler {
|
|
|
44
39
|
this.maxFileSize = config.maxSize ?? 52428800;
|
|
45
40
|
this.bucket = config.bucket;
|
|
46
41
|
this.client = new S3Client(this.config);
|
|
47
|
-
this.cachedGetSignedUrl =
|
|
48
|
-
|
|
49
|
-
|
|
42
|
+
this.cachedGetSignedUrl =
|
|
43
|
+
this.signedUrlCacheExpireTimeSeconds > 0
|
|
44
|
+
? memoize(this.s3SignUrl, {
|
|
45
|
+
maxAge: this.signedUrlCacheExpireTimeSeconds * 1000,
|
|
46
|
+
})
|
|
47
|
+
: this.s3SignUrl;
|
|
50
48
|
this.storageClass = config.storageClass;
|
|
51
49
|
}
|
|
52
50
|
async handleFile(resource) {
|
|
@@ -116,15 +114,11 @@ export class S3Handler {
|
|
|
116
114
|
};
|
|
117
115
|
},
|
|
118
116
|
commit: async ({ fileKey, uploadId, filename, providerCommitData, }) => {
|
|
119
|
-
const parts = providerCommitData?.Parts;
|
|
120
|
-
if (!isCompletedPartList(parts)) {
|
|
121
|
-
throw new errors.BadRequestError('providerCommitData must have `Parts` with a `PartNumber` and `ETag` for each uploaded part');
|
|
122
|
-
}
|
|
123
117
|
await this.client.send(new CompleteMultipartUploadCommand({
|
|
124
118
|
Bucket: this.bucket,
|
|
125
119
|
Key: fileKey,
|
|
126
120
|
UploadId: uploadId,
|
|
127
|
-
MultipartUpload:
|
|
121
|
+
MultipartUpload: providerCommitData,
|
|
128
122
|
}));
|
|
129
123
|
const headResult = await this.client.send(new HeadObjectCommand({
|
|
130
124
|
Bucket: this.bucket,
|
package/build/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../lib/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,mBAAmB,EACnB,gBAAgB,EAChB,QAAQ,EAGR,4BAA4B,EAC5B,iBAAiB,EACjB,8BAA8B,EAC9B,iBAAiB,EACjB,2BAA2B,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../lib/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,mBAAmB,EACnB,gBAAgB,EAChB,QAAQ,EAGR,4BAA4B,EAC5B,iBAAiB,EACjB,8BAA8B,EAC9B,iBAAiB,EACjB,2BAA2B,GAE3B,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAE,MAAM,EAAE,MAAM,sBAAsB,CAAC;AAC9C,OAAO,EAAE,YAAY,EAAE,MAAM,+BAA+B,CAAC;AAC7D,OAAO,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAC;AAGpD,OAAO,OAAO,MAAM,UAAU,CAAC;AAC/B,O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
11
11
|
AbortMultipartUploadCommand,
|
|
12
12
|
type StorageClass,
|
|
13
|
-
type CompletedPart,
|
|
14
13
|
} from '@aws-sdk/client-s3';
|
|
15
14
|
import { Upload } from '@aws-sdk/lib-storage';
|
|
16
15
|
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
|
|
17
|
-
import {
|
|
16
|
+
import { webResourceHandler } from '@balena/pinejs';
|
|
18
17
|
|
|
19
18
|
import type { WebResourceType as WebResource } from '@balena/sbvr-types';
|
|
20
19
|
import memoize from 'memoizee';
|
|
@@ -37,16 +36,6 @@ const normalizeHref = (href: string) => {
|
|
|
37
36
|
return href.split('?')[0];
|
|
38
37
|
};
|
|
39
38
|
|
|
40
|
-
const isCompletedPartList = (parts: unknown): parts is CompletedPart[] =>
|
|
41
|
-
Array.isArray(parts) &&
|
|
42
|
-
parts.every(
|
|
43
|
-
(part) =>
|
|
44
|
-
typeof part === 'object' &&
|
|
45
|
-
part != null &&
|
|
46
|
-
Number.isSafeInteger(part.PartNumber) &&
|
|
47
|
-
typeof part.ETag === 'string',
|
|
48
|
-
);
|
|
49
|
-
|
|
50
39
|
export class S3Handler implements webResourceHandler.WebResourceHandler {
|
|
51
40
|
private readonly config: S3ClientConfig;
|
|
52
41
|
private readonly bucket: string;
|
|
@@ -87,9 +76,13 @@ export class S3Handler implements webResourceHandler.WebResourceHandler {
|
|
|
87
76
|
|
|
88
77
|
// Memoize expects maxAge in MS and s3 signing method in seconds.
|
|
89
78
|
// Normalization to use only seconds and therefore convert here from seconds to MS
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
79
|
+
// memoizee treats a maxAge of 0 as never expiring, so 0 disables the cache instead
|
|
80
|
+
this.cachedGetSignedUrl =
|
|
81
|
+
this.signedUrlCacheExpireTimeSeconds > 0
|
|
82
|
+
? memoize(this.s3SignUrl, {
|
|
83
|
+
maxAge: this.signedUrlCacheExpireTimeSeconds * 1000,
|
|
84
|
+
})
|
|
85
|
+
: this.s3SignUrl;
|
|
93
86
|
this.storageClass = config.storageClass;
|
|
94
87
|
}
|
|
95
88
|
|
|
@@ -187,20 +180,12 @@ export class S3Handler implements webResourceHandler.WebResourceHandler {
|
|
|
187
180
|
filename,
|
|
188
181
|
providerCommitData,
|
|
189
182
|
}: webResourceHandler.CommitMultipartUploadPayload): Promise<WebResource> => {
|
|
190
|
-
// providerCommitData comes straight from the commitUpload request body
|
|
191
|
-
const parts = providerCommitData?.Parts;
|
|
192
|
-
if (!isCompletedPartList(parts)) {
|
|
193
|
-
throw new errors.BadRequestError(
|
|
194
|
-
'providerCommitData must have `Parts` with a `PartNumber` and `ETag` for each uploaded part',
|
|
195
|
-
);
|
|
196
|
-
}
|
|
197
|
-
|
|
198
183
|
await this.client.send(
|
|
199
184
|
new CompleteMultipartUploadCommand({
|
|
200
185
|
Bucket: this.bucket,
|
|
201
186
|
Key: fileKey,
|
|
202
187
|
UploadId: uploadId,
|
|
203
|
-
MultipartUpload:
|
|
188
|
+
MultipartUpload: providerCommitData,
|
|
204
189
|
}),
|
|
205
190
|
);
|
|
206
191
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@balena/pinejs-webresource-s3",
|
|
3
|
-
"version": "2.1.49
|
|
3
|
+
"version": "2.1.49",
|
|
4
4
|
"description": "A PineJS WebResource handler for storing & serving files on S3",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "build/index.js",
|
|
@@ -50,6 +50,6 @@
|
|
|
50
50
|
"@balena/pinejs": "^21.3.0 || ^22.0.0 || ^23.0.0 || ^24.0.0 || ^25.0.0"
|
|
51
51
|
},
|
|
52
52
|
"versionist": {
|
|
53
|
-
"publishedAt": "2026-10-27T22:
|
|
53
|
+
"publishedAt": "2026-10-27T22:55:01.339Z"
|
|
54
54
|
}
|
|
55
55
|
}
|
package/test/index.test.ts
CHANGED
|
@@ -14,9 +14,8 @@ import {
|
|
|
14
14
|
import { mockClient } from 'aws-sdk-client-mock';
|
|
15
15
|
import { S3Handler } from '../lib/index.js';
|
|
16
16
|
import { PassThrough } from 'stream';
|
|
17
|
-
import { inspect } from 'util';
|
|
18
17
|
import { Upload } from '@aws-sdk/lib-storage';
|
|
19
|
-
import {
|
|
18
|
+
import type { webResourceHandler } from '@balena/pinejs';
|
|
20
19
|
|
|
21
20
|
chai.use(chaiAsPromised);
|
|
22
21
|
const s3Mock = mockClient(S3Client);
|
|
@@ -135,6 +134,55 @@ describe('S3Handler', function () {
|
|
|
135
134
|
// X-Amz-Signature should be present in the signed URL
|
|
136
135
|
expect(result.href).to.match(/X-Amz-Signature=/);
|
|
137
136
|
});
|
|
137
|
+
|
|
138
|
+
describe('signed url cache', function () {
|
|
139
|
+
let signStub: SinonStub;
|
|
140
|
+
|
|
141
|
+
beforeEach(function () {
|
|
142
|
+
// Stubbed before constructing as the handler memoizes the method itself
|
|
143
|
+
signStub = sinon
|
|
144
|
+
.stub(S3Handler.prototype as any, 's3SignUrl')
|
|
145
|
+
.resolves('https://signed.test.com/file.txt');
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
afterEach(function () {
|
|
149
|
+
signStub.restore();
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
const handlerWithCacheTime = (signedUrlCacheExpireTimeSeconds?: number) =>
|
|
153
|
+
new S3Handler({
|
|
154
|
+
region: 'us-east-1',
|
|
155
|
+
accessKey: 'test-access-key',
|
|
156
|
+
secretKey: 'test-secret-key',
|
|
157
|
+
endpoint: 'https://s3.test.com',
|
|
158
|
+
bucket: 'test-bucket',
|
|
159
|
+
signedUrlCacheExpireTimeSeconds,
|
|
160
|
+
});
|
|
161
|
+
const webResource = () => ({
|
|
162
|
+
href: 'https://s3.test.com/test-bucket/file.txt',
|
|
163
|
+
filename: 'file.txt',
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
it('should reuse a signed url for the same file', async function () {
|
|
167
|
+
const cachingS3Handler = handlerWithCacheTime();
|
|
168
|
+
|
|
169
|
+
await cachingS3Handler.onPreRespond(webResource());
|
|
170
|
+
const result = await cachingS3Handler.onPreRespond(webResource());
|
|
171
|
+
|
|
172
|
+
expect(result.href).to.equal('https://signed.test.com/file.txt');
|
|
173
|
+
expect(signStub.callCount).to.equal(1);
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
it('should sign every time when the cache time is 0', async function () {
|
|
177
|
+
const uncachedS3Handler = handlerWithCacheTime(0);
|
|
178
|
+
|
|
179
|
+
await uncachedS3Handler.onPreRespond(webResource());
|
|
180
|
+
const result = await uncachedS3Handler.onPreRespond(webResource());
|
|
181
|
+
|
|
182
|
+
expect(result.href).to.equal('https://signed.test.com/file.txt');
|
|
183
|
+
expect(signStub.callCount).to.equal(2);
|
|
184
|
+
});
|
|
185
|
+
});
|
|
138
186
|
});
|
|
139
187
|
|
|
140
188
|
describe('multipartUpload', function () {
|
|
@@ -367,15 +415,11 @@ describe('S3Handler', function () {
|
|
|
367
415
|
.on(HeadObjectCommand)
|
|
368
416
|
.resolves({ ContentLength: 1024, ContentType: 'text/plain' });
|
|
369
417
|
|
|
370
|
-
const parts = [
|
|
371
|
-
{ PartNumber: 1, ETag: '"etag-1"' },
|
|
372
|
-
{ PartNumber: 2, ETag: '"etag-2"' },
|
|
373
|
-
];
|
|
374
418
|
const payload = {
|
|
375
419
|
fileKey: 'test-key',
|
|
376
420
|
uploadId: 'test-upload-id',
|
|
377
421
|
filename: 'file.txt',
|
|
378
|
-
providerCommitData: {
|
|
422
|
+
providerCommitData: {},
|
|
379
423
|
};
|
|
380
424
|
const result = await s3Handler.multipartUpload.commit(payload);
|
|
381
425
|
|
|
@@ -392,36 +436,9 @@ describe('S3Handler', function () {
|
|
|
392
436
|
Bucket: 'test-bucket',
|
|
393
437
|
Key: 'test-key',
|
|
394
438
|
UploadId: 'test-upload-id',
|
|
395
|
-
MultipartUpload: {
|
|
439
|
+
MultipartUpload: {},
|
|
396
440
|
});
|
|
397
441
|
});
|
|
398
|
-
|
|
399
|
-
const invalidProviderCommitData: Array<
|
|
400
|
-
webResourceHandler.CommitMultipartUploadPayload['providerCommitData']
|
|
401
|
-
> = [
|
|
402
|
-
undefined,
|
|
403
|
-
{},
|
|
404
|
-
{ Parts: 'not-an-array' },
|
|
405
|
-
{ Parts: [{ PartNumber: '1', ETag: '"etag-1"' }] },
|
|
406
|
-
{ Parts: [{ PartNumber: 1 }] },
|
|
407
|
-
{ Parts: [null] },
|
|
408
|
-
];
|
|
409
|
-
for (const providerCommitData of invalidProviderCommitData) {
|
|
410
|
-
it(`should reject providerCommitData ${inspect(providerCommitData, { breakLength: Infinity })} without contacting S3`, async function () {
|
|
411
|
-
await expect(
|
|
412
|
-
s3Handler.multipartUpload.commit({
|
|
413
|
-
fileKey: 'test-key',
|
|
414
|
-
uploadId: 'test-upload-id',
|
|
415
|
-
filename: 'file.txt',
|
|
416
|
-
providerCommitData,
|
|
417
|
-
}),
|
|
418
|
-
).to.be.rejectedWith(
|
|
419
|
-
errors.BadRequestError,
|
|
420
|
-
'providerCommitData must have `Parts` with a `PartNumber` and `ETag` for each uploaded part',
|
|
421
|
-
);
|
|
422
|
-
expect(s3Mock.calls()).to.have.lengthOf(0);
|
|
423
|
-
});
|
|
424
|
-
}
|
|
425
442
|
});
|
|
426
443
|
|
|
427
444
|
describe('cancel', function () {
|