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