wirejs-deploy-amplify-basic 0.0.13-alpha → 0.0.14-alpha

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wirejs-deploy-amplify-basic",
3
- "version": "0.0.13-alpha",
3
+ "version": "0.0.14-alpha",
4
4
  "type": "module",
5
5
  "bin": {
6
6
  "wirejs-deploy-amplify-basic": "./build.js"
@@ -12,10 +12,10 @@
12
12
  "recursive-copy": "^2.0.14",
13
13
  "rimraf": "^6.0.1",
14
14
  "wirejs-dom": "^1.0.34",
15
- "wirejs-resources": "^0.1.5-alpha"
15
+ "wirejs-resources": "^0.1.8-alpha"
16
16
  },
17
17
  "devDependencies": {
18
18
  "@aws-amplify/backend": "^1.14.0",
19
- "@aws-sdk/client-s3": "^3.735.0"
19
+ "@aws-sdk/client-s3": "^3.738.0"
20
20
  }
21
21
  }
@@ -1,12 +1,17 @@
1
+ import { env } from 'process';
2
+
1
3
  import {
2
4
  S3Client,
3
5
  ListObjectsCommand,
4
6
  PutObjectCommand,
5
- GetObjectCommand
7
+ GetObjectCommand,
8
+ DeleteObjectCommand
6
9
  } from '@aws-sdk/client-s3';
7
10
 
8
- import { overrides } from 'wirejs-resources';
9
- import { Resource } from 'wirejs-resources';
11
+ import {
12
+ overrides,
13
+ Resource,
14
+ } from 'wirejs-resources';
10
15
 
11
16
  export {
12
17
  AuthenticationService,
@@ -14,40 +19,98 @@ export {
14
19
  requiresContext,
15
20
  Context,
16
21
  CookieJar,
22
+ FileService,
17
23
  Resource,
18
24
  overrides,
19
25
  } from 'wirejs-resources';
20
26
 
27
+ const Bucket = env['BUCKET'];
28
+ const s3 = new S3Client();
29
+
21
30
  export class FileService extends Resource {
22
31
  constructor(scope, id) {
23
32
  super(scope, id);
24
33
  addResource('FileService', { absoluteId: this.absoluteId });
25
34
  }
26
35
 
27
- async write(...args) {
28
- console.log('"writing secret" ... :/ ... ');
36
+ /**
37
+ * @param {string} filename
38
+ * @param {BufferEncoding} [encoding]
39
+ *
40
+ */
41
+ async read(filename, encoding = 'utf8') {
42
+ const Key = `${this.absoluteId}/${filename}`;
43
+ const command = new GetObjectCommand({ Bucket, Key });
44
+ const result = await s3.send(command);
45
+ return result.Body.transformToString(encoding);
46
+ }
47
+
48
+ /**
49
+ * @param {string} filename
50
+ * @param {string} data
51
+ * @param {{
52
+ * onlyIfNotExists: boolean
53
+ * }} [param2]
54
+ */
55
+ async write(filename, data, { onlyIfNotExists = false} = {}) {
56
+ const Key = `${this.absoluteId}/${filename}`;
57
+ const Body = data;
58
+
59
+ /**
60
+ * @type {ConstructorParameters<typeof PutObjectCommand>[0]}
61
+ */
62
+ const commandDetails = { Bucket, Key, Body };
63
+ if (onlyIfNotExists) {
64
+ commandDetails['IfNoneMatch'] = '*';
65
+ }
66
+
67
+ const command = new PutObjectCommand();
68
+ return s3.send(command);
69
+ }
70
+
71
+ /**
72
+ * @param {string} filename
73
+ */
74
+ async delete(filename) {
75
+ const Key = `${this.absoluteId}/${filename}`;
76
+ const command = new DeleteObjectCommand({
77
+ Bucket,
78
+ Key
79
+ });
80
+ return s3.send(command);
81
+ }
82
+
83
+ async * list({ prefix = '' } = {}) {
84
+ const Prefix = `${this.absoluteId}/${prefix}`;
85
+ let Marker = null;
86
+
87
+ while (true) {
88
+ const command = new ListObjectsCommand({
89
+ Bucket,
90
+ Prefix,
91
+ MaxKeys: 1000,
92
+ Marker
93
+ });
94
+ const result = await s3.send(command);
95
+ Marker = result.NextMarker;
96
+
97
+ for (const o of result.Contents || []) {
98
+ yield o.Key;
99
+ }
100
+
101
+ if (!Marker) break;
102
+ }
103
+ }
104
+
105
+ isAlreadyExistsError(error) {
106
+ // https://docs.aws.amazon.com/AmazonS3/latest/API/API_PutObject.html
107
+ return error?.$metadata?.httpStatusCode === 412;
29
108
  }
30
109
  }
31
110
 
32
111
  // expose resources to other resources that might depend on it.
33
112
  overrides.FileService = FileService;
34
113
 
35
- // export class AuthenticationService {
36
- // constructor(id) {
37
- // addResource('AuthenticationService', { id });
38
- // }
39
-
40
- // buildApi(...args) {
41
- // // console.log('AuthService.buildApi', [args]);
42
- // }
43
- // }
44
-
45
- // export class Secret {
46
- // constructor(id) {
47
- // addResource('Secret', { id });
48
- // }
49
- // }
50
-
51
114
  globalThis.wirejsResources = [];
52
115
 
53
116
  function addResource(type, options) {