@travetto/model-s3 3.0.0-rc.2 → 3.0.0-rc.21
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/README.md +14 -11
- package/{index.ts → __index__.ts} +0 -0
- package/package.json +19 -8
- package/src/config.ts +10 -7
- package/src/service.ts +9 -6
- package/support/service.s3.ts +28 -0
package/README.md
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
<!-- This file was generated by @travetto/doc and should not be modified directly -->
|
|
2
|
-
<!-- Please modify https://github.com/travetto/travetto/tree/main/module/model-s3/
|
|
2
|
+
<!-- Please modify https://github.com/travetto/travetto/tree/main/module/model-s3/DOC.ts and execute "npx trv doc" to rebuild -->
|
|
3
3
|
# S3 Model Support
|
|
4
4
|
## S3 backing for the travetto model module.
|
|
5
5
|
|
|
@@ -29,16 +29,15 @@ export class Init {
|
|
|
29
29
|
}
|
|
30
30
|
```
|
|
31
31
|
|
|
32
|
-
where the [S3ModelConfig](https://github.com/travetto/travetto/tree/main/module/model-s3/src/config.ts#
|
|
32
|
+
where the [S3ModelConfig](https://github.com/travetto/travetto/tree/main/module/model-s3/src/config.ts#L11) is defined by:
|
|
33
33
|
|
|
34
34
|
|
|
35
35
|
**Code: Structure of S3ModelConfig**
|
|
36
36
|
```typescript
|
|
37
37
|
import { fromIni } from '@aws-sdk/credential-provider-ini';
|
|
38
|
-
import
|
|
38
|
+
import type s3 from '@aws-sdk/client-s3';
|
|
39
39
|
|
|
40
|
-
import {
|
|
41
|
-
import { Config } from '@travetto/config';
|
|
40
|
+
import { Config, EnvVar } from '@travetto/config';
|
|
42
41
|
import { Field, Required } from '@travetto/schema';
|
|
43
42
|
|
|
44
43
|
/**
|
|
@@ -51,12 +50,16 @@ export class S3ModelConfig {
|
|
|
51
50
|
bucket = ''; // S3 bucket
|
|
52
51
|
endpoint = ''; // Endpoint url
|
|
53
52
|
|
|
54
|
-
|
|
55
|
-
|
|
53
|
+
@EnvVar('AWS_ACCESS_KEY_ID')
|
|
54
|
+
accessKeyId: string = '';
|
|
55
|
+
@EnvVar('AWS_SECRET_ACCESS_KEY')
|
|
56
|
+
secretAccessKey: string = '';
|
|
57
|
+
@EnvVar('AWS_PROFILE')
|
|
58
|
+
profile?: string;
|
|
56
59
|
|
|
57
60
|
@Field(Object)
|
|
58
61
|
@Required(false)
|
|
59
|
-
config:
|
|
62
|
+
config: s3.S3ClientConfig; // Additional s3 config
|
|
60
63
|
|
|
61
64
|
chunkSize = 5 * 2 ** 20; // Chunk size in bytes
|
|
62
65
|
|
|
@@ -74,7 +77,7 @@ export class S3ModelConfig {
|
|
|
74
77
|
*/
|
|
75
78
|
async postConstruct(): Promise<void> {
|
|
76
79
|
if (!this.accessKeyId && !this.secretAccessKey) {
|
|
77
|
-
const creds = await fromIni({ profile:
|
|
80
|
+
const creds = await fromIni({ profile: this.profile })();
|
|
78
81
|
this.accessKeyId = creds.accessKeyId;
|
|
79
82
|
this.secretAccessKey = creds.secretAccessKey;
|
|
80
83
|
}
|
|
@@ -92,8 +95,8 @@ export class S3ModelConfig {
|
|
|
92
95
|
}
|
|
93
96
|
```
|
|
94
97
|
|
|
95
|
-
Additionally, you can see that the class is registered with the [@Config](https://github.com/travetto/travetto/tree/main/module/config/src/decorator.ts#
|
|
96
|
-
standard [Configuration](https://github.com/travetto/travetto/tree/main/module/config#readme "
|
|
98
|
+
Additionally, you can see that the class is registered with the [@Config](https://github.com/travetto/travetto/tree/main/module/config/src/decorator.ts#L13) annotation, and so these values can be overridden using the
|
|
99
|
+
standard [Configuration](https://github.com/travetto/travetto/tree/main/module/config#readme "Configuration support")resolution paths.
|
|
97
100
|
|
|
98
101
|
|
|
99
102
|
**Note**: Do not commit your `accessKeyId` or `secretAccessKey` values to your source repository, especially if it is public facing. Not only is it a security risk, but Amazon will scan public repos, looking for keys, and if found will react swiftly.
|
|
File without changes
|
package/package.json
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@travetto/model-s3",
|
|
3
|
-
"
|
|
4
|
-
"version": "3.0.0-rc.2",
|
|
3
|
+
"version": "3.0.0-rc.21",
|
|
5
4
|
"description": "S3 backing for the travetto model module.",
|
|
6
5
|
"keywords": [
|
|
7
6
|
"s3",
|
|
@@ -16,19 +15,31 @@
|
|
|
16
15
|
"name": "Travetto Framework"
|
|
17
16
|
},
|
|
18
17
|
"files": [
|
|
19
|
-
"
|
|
18
|
+
"__index__.ts",
|
|
19
|
+
"support",
|
|
20
20
|
"src"
|
|
21
21
|
],
|
|
22
|
-
"main": "
|
|
22
|
+
"main": "__index__.ts",
|
|
23
23
|
"repository": {
|
|
24
24
|
"url": "https://github.com/travetto/travetto.git",
|
|
25
25
|
"directory": "module/model-s3"
|
|
26
26
|
},
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"@aws-sdk/client-s3": "^3.
|
|
29
|
-
"@aws-sdk/credential-provider-ini": "^3.
|
|
30
|
-
"@travetto/config": "^3.0.0-rc.
|
|
31
|
-
"@travetto/model": "^3.0.0-rc.
|
|
28
|
+
"@aws-sdk/client-s3": "^3.272.0",
|
|
29
|
+
"@aws-sdk/credential-provider-ini": "^3.218.0",
|
|
30
|
+
"@travetto/config": "^3.0.0-rc.20",
|
|
31
|
+
"@travetto/model": "^3.0.0-rc.20"
|
|
32
|
+
},
|
|
33
|
+
"peerDependencies": {
|
|
34
|
+
"@travetto/command": "^3.0.0-rc.16"
|
|
35
|
+
},
|
|
36
|
+
"peerDependenciesMeta": {
|
|
37
|
+
"@travetto/command": {
|
|
38
|
+
"optional": true
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
"travetto": {
|
|
42
|
+
"displayName": "S3 Model Support"
|
|
32
43
|
},
|
|
33
44
|
"publishConfig": {
|
|
34
45
|
"access": "public"
|
package/src/config.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import { fromIni } from '@aws-sdk/credential-provider-ini';
|
|
2
|
-
import
|
|
2
|
+
import type s3 from '@aws-sdk/client-s3';
|
|
3
3
|
|
|
4
|
-
import {
|
|
5
|
-
import { Config } from '@travetto/config';
|
|
4
|
+
import { Config, EnvVar } from '@travetto/config';
|
|
6
5
|
import { Field, Required } from '@travetto/schema';
|
|
7
6
|
|
|
8
7
|
/**
|
|
@@ -15,12 +14,16 @@ export class S3ModelConfig {
|
|
|
15
14
|
bucket = ''; // S3 bucket
|
|
16
15
|
endpoint = ''; // Endpoint url
|
|
17
16
|
|
|
18
|
-
|
|
19
|
-
|
|
17
|
+
@EnvVar('AWS_ACCESS_KEY_ID')
|
|
18
|
+
accessKeyId: string = '';
|
|
19
|
+
@EnvVar('AWS_SECRET_ACCESS_KEY')
|
|
20
|
+
secretAccessKey: string = '';
|
|
21
|
+
@EnvVar('AWS_PROFILE')
|
|
22
|
+
profile?: string;
|
|
20
23
|
|
|
21
24
|
@Field(Object)
|
|
22
25
|
@Required(false)
|
|
23
|
-
config:
|
|
26
|
+
config: s3.S3ClientConfig; // Additional s3 config
|
|
24
27
|
|
|
25
28
|
chunkSize = 5 * 2 ** 20; // Chunk size in bytes
|
|
26
29
|
|
|
@@ -38,7 +41,7 @@ export class S3ModelConfig {
|
|
|
38
41
|
*/
|
|
39
42
|
async postConstruct(): Promise<void> {
|
|
40
43
|
if (!this.accessKeyId && !this.secretAccessKey) {
|
|
41
|
-
const creds = await fromIni({ profile:
|
|
44
|
+
const creds = await fromIni({ profile: this.profile })();
|
|
42
45
|
this.accessKeyId = creds.accessKeyId;
|
|
43
46
|
this.secretAccessKey = creds.secretAccessKey;
|
|
44
47
|
}
|
package/src/service.ts
CHANGED
|
@@ -3,17 +3,18 @@ import { Readable } from 'stream';
|
|
|
3
3
|
import * as s3 from '@aws-sdk/client-s3';
|
|
4
4
|
import type { MetadataBearer } from '@aws-sdk/types';
|
|
5
5
|
|
|
6
|
-
import { StreamUtil } from '@travetto/boot';
|
|
7
6
|
import {
|
|
8
7
|
ModelCrudSupport, ModelStreamSupport, ModelStorageSupport, StreamMeta,
|
|
9
8
|
ModelType, ModelRegistry, ExistsError, NotFoundError, OptionalId
|
|
10
9
|
} from '@travetto/model';
|
|
11
10
|
import { Injectable } from '@travetto/di';
|
|
12
|
-
import { Class, AppError
|
|
11
|
+
import { StreamUtil, Class, AppError } from '@travetto/base';
|
|
13
12
|
|
|
14
13
|
import { ModelCrudUtil } from '@travetto/model/src/internal/service/crud';
|
|
15
14
|
import { ModelExpirySupport } from '@travetto/model/src/service/expiry';
|
|
16
15
|
import { ModelExpiryUtil } from '@travetto/model/src/internal/service/expiry';
|
|
16
|
+
import { ModelStorageUtil } from '@travetto/model/src/internal/service/storage';
|
|
17
|
+
import { ModelUtil } from '@travetto/model/src/internal/util';
|
|
17
18
|
|
|
18
19
|
import { S3ModelConfig } from './config';
|
|
19
20
|
|
|
@@ -22,10 +23,10 @@ function isMetadataBearer(o: unknown): o is MetadataBearer {
|
|
|
22
23
|
}
|
|
23
24
|
|
|
24
25
|
function hasContentType<T>(o: T): o is T & { contenttype?: string } {
|
|
25
|
-
return o && 'contenttype'
|
|
26
|
+
return o !== undefined && o !== null && Object.hasOwn(o, 'contenttype');
|
|
26
27
|
}
|
|
27
28
|
|
|
28
|
-
const STREAM_SPACE = '@
|
|
29
|
+
const STREAM_SPACE = '@travetto/model-s3:stream';
|
|
29
30
|
|
|
30
31
|
/**
|
|
31
32
|
* Asset source backed by S3
|
|
@@ -147,11 +148,12 @@ export class S3ModelService implements ModelCrudSupport, ModelStreamSupport, Mod
|
|
|
147
148
|
}
|
|
148
149
|
|
|
149
150
|
uuid(): string {
|
|
150
|
-
return
|
|
151
|
+
return ModelUtil.uuid(32);
|
|
151
152
|
}
|
|
152
153
|
|
|
153
154
|
async postConstruct(): Promise<void> {
|
|
154
155
|
this.client = new s3.S3(this.config.config);
|
|
156
|
+
ModelStorageUtil.registerModelChangeListener(this);
|
|
155
157
|
}
|
|
156
158
|
|
|
157
159
|
async head<T extends ModelType>(cls: Class<T>, id: string): Promise<boolean> {
|
|
@@ -176,7 +178,8 @@ export class S3ModelService implements ModelCrudSupport, ModelStreamSupport, Mod
|
|
|
176
178
|
try {
|
|
177
179
|
const result = await this.client.getObject(this.#q(cls, id));
|
|
178
180
|
if (result.Body) {
|
|
179
|
-
|
|
181
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
182
|
+
const body = (await StreamUtil.streamToBuffer(result.Body as Readable)).toString('utf8');
|
|
180
183
|
const output = await ModelCrudUtil.load(cls, body);
|
|
181
184
|
if (output) {
|
|
182
185
|
const { expiresAt } = ModelRegistry.get(cls);
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { mkdirSync } from 'fs';
|
|
2
|
+
import os from 'os';
|
|
3
|
+
|
|
4
|
+
import type { CommandService } from '@travetto/command';
|
|
5
|
+
|
|
6
|
+
const temp = `${os.tmpdir()}/local-stack`;
|
|
7
|
+
try {
|
|
8
|
+
mkdirSync(temp);
|
|
9
|
+
} catch { }
|
|
10
|
+
|
|
11
|
+
const version = 'latest';
|
|
12
|
+
|
|
13
|
+
export const service: CommandService = {
|
|
14
|
+
name: 's3',
|
|
15
|
+
version,
|
|
16
|
+
privileged: true,
|
|
17
|
+
env: {
|
|
18
|
+
TEST_AWS_ACCOUNT_ID: '000000000000',
|
|
19
|
+
LOCALSTACK_HOSTNAME: 'localhost',
|
|
20
|
+
DEFAULT_REGION: 'us-east-1',
|
|
21
|
+
HOST_TMP_FOLDER: temp
|
|
22
|
+
},
|
|
23
|
+
volumes: {
|
|
24
|
+
[temp]: '/tmp/localstack'
|
|
25
|
+
},
|
|
26
|
+
ports: { 4566: 4566, 4571: 4571, 8080: 8080, 8081: 8081 },
|
|
27
|
+
image: `localstack/localstack:${version}`
|
|
28
|
+
};
|