@travetto/model-s3 5.0.0-rc.8 → 5.0.0-rc.9
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/package.json +4 -4
- package/src/service.ts +9 -17
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@travetto/model-s3",
|
|
3
|
-
"version": "5.0.0-rc.
|
|
3
|
+
"version": "5.0.0-rc.9",
|
|
4
4
|
"description": "S3 backing for the travetto model module.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"s3",
|
|
@@ -27,11 +27,11 @@
|
|
|
27
27
|
"dependencies": {
|
|
28
28
|
"@aws-sdk/client-s3": "^3.609.0",
|
|
29
29
|
"@aws-sdk/credential-provider-ini": "^3.609.0",
|
|
30
|
-
"@travetto/config": "^5.0.0-rc.
|
|
31
|
-
"@travetto/model": "^5.0.0-rc.
|
|
30
|
+
"@travetto/config": "^5.0.0-rc.9",
|
|
31
|
+
"@travetto/model": "^5.0.0-rc.9"
|
|
32
32
|
},
|
|
33
33
|
"peerDependencies": {
|
|
34
|
-
"@travetto/command": "^5.0.0-rc.
|
|
34
|
+
"@travetto/command": "^5.0.0-rc.9"
|
|
35
35
|
},
|
|
36
36
|
"peerDependenciesMeta": {
|
|
37
37
|
"@travetto/command": {
|
package/src/service.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Readable } from 'node:stream';
|
|
2
|
-
import { buffer as toBuffer } from 'node:stream/consumers';
|
|
2
|
+
import { buffer as toBuffer, text as toText } from 'node:stream/consumers';
|
|
3
3
|
import { Agent } from 'node:https';
|
|
4
4
|
|
|
5
5
|
import { S3, CompletedPart, type CreateMultipartUploadRequest } from '@aws-sdk/client-s3';
|
|
@@ -12,7 +12,7 @@ import {
|
|
|
12
12
|
StreamRange
|
|
13
13
|
} from '@travetto/model';
|
|
14
14
|
import { Injectable } from '@travetto/di';
|
|
15
|
-
import { Class, AppError } from '@travetto/runtime';
|
|
15
|
+
import { Class, AppError, castTo, asFull } from '@travetto/runtime';
|
|
16
16
|
|
|
17
17
|
import { ModelCrudUtil } from '@travetto/model/src/internal/service/crud';
|
|
18
18
|
import { ModelExpirySupport } from '@travetto/model/src/service/expiry';
|
|
@@ -77,8 +77,7 @@ export class S3ModelService implements ModelCrudSupport, ModelStreamSupport, Mod
|
|
|
77
77
|
return key;
|
|
78
78
|
}
|
|
79
79
|
|
|
80
|
-
|
|
81
|
-
#q<U extends object>(cls: string | Class, id: string, extra: U = {} as U): (U & { Key: string, Bucket: string }) {
|
|
80
|
+
#q<U extends object>(cls: string | Class, id: string, extra: U = asFull({})): (U & { Key: string, Bucket: string }) {
|
|
82
81
|
const key = this.#resolveKey(cls, id);
|
|
83
82
|
return { Key: key, Bucket: this.config.bucket, ...extra };
|
|
84
83
|
}
|
|
@@ -203,8 +202,7 @@ export class S3ModelService implements ModelCrudSupport, ModelStreamSupport, Mod
|
|
|
203
202
|
try {
|
|
204
203
|
const result = await this.client.getObject(this.#q(cls, id));
|
|
205
204
|
if (result.Body) {
|
|
206
|
-
|
|
207
|
-
const body = (await toBuffer(result.Body as Readable)).toString('utf8');
|
|
205
|
+
const body = await toText(castTo(result.Body));
|
|
208
206
|
const output = await ModelCrudUtil.load(cls, body);
|
|
209
207
|
if (output) {
|
|
210
208
|
const { expiresAt } = ModelRegistry.get(cls);
|
|
@@ -230,8 +228,7 @@ export class S3ModelService implements ModelCrudSupport, ModelStreamSupport, Mod
|
|
|
230
228
|
}
|
|
231
229
|
|
|
232
230
|
async store<T extends ModelType>(cls: Class<T>, item: OptionalId<T>, preStore = true): Promise<T> {
|
|
233
|
-
|
|
234
|
-
let prepped: T = item as T;
|
|
231
|
+
let prepped: T = castTo(item);
|
|
235
232
|
if (preStore) {
|
|
236
233
|
prepped = await ModelCrudUtil.preStore(cls, item, this);
|
|
237
234
|
}
|
|
@@ -323,13 +320,11 @@ export class S3ModelService implements ModelCrudSupport, ModelStreamSupport, Mod
|
|
|
323
320
|
}
|
|
324
321
|
|
|
325
322
|
if (typeof res.Body === 'string') { // string
|
|
326
|
-
|
|
327
|
-
return Readable.from(res.Body, { encoding: (res.Body as string).endsWith('=') ? 'base64' : 'utf8' });
|
|
323
|
+
return Readable.from(res.Body, { encoding: castTo<string>(res.Body).endsWith('=') ? 'base64' : 'utf8' });
|
|
328
324
|
} else if (res.Body instanceof Buffer) { // Buffer
|
|
329
325
|
return Readable.from(res.Body);
|
|
330
326
|
} else if ('pipe' in res.Body) { // Stream
|
|
331
|
-
|
|
332
|
-
return res.Body as Readable;
|
|
327
|
+
return castTo<Readable>(res.Body);
|
|
333
328
|
}
|
|
334
329
|
throw new AppError(`Unable to read type: ${typeof res.Body}`);
|
|
335
330
|
}
|
|
@@ -339,8 +334,7 @@ export class S3ModelService implements ModelCrudSupport, ModelStreamSupport, Mod
|
|
|
339
334
|
const meta = await this.describeStream(location);
|
|
340
335
|
range = enforceRange(range, meta.size);
|
|
341
336
|
}
|
|
342
|
-
|
|
343
|
-
return this.#getObject(location, range as Required<StreamRange>);
|
|
337
|
+
return this.#getObject(location, castTo(range));
|
|
344
338
|
}
|
|
345
339
|
|
|
346
340
|
async headStream(location: string): Promise<{ Metadata?: Partial<StreamMeta>, ContentLength?: number }> {
|
|
@@ -362,10 +356,8 @@ export class S3ModelService implements ModelCrudSupport, ModelStreamSupport, Mod
|
|
|
362
356
|
|
|
363
357
|
if (obj) {
|
|
364
358
|
const ret: StreamMeta = {
|
|
365
|
-
// @ts-expect-error
|
|
366
359
|
contentType: '',
|
|
367
|
-
|
|
368
|
-
...obj.Metadata as StreamMeta,
|
|
360
|
+
...obj.Metadata,
|
|
369
361
|
size: obj.ContentLength!,
|
|
370
362
|
};
|
|
371
363
|
if (hasContentType(ret)) {
|