@hot-updater/aws 0.1.3 → 0.1.5
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/dist/index.cjs +55 -47
- package/dist/index.d.cts +9 -4
- package/dist/index.d.ts +9 -4
- package/dist/index.js +55 -48
- package/package.json +6 -2
- package/src/aws.ts +0 -173
- package/src/index.ts +0 -1
- package/src/utils/readDir.ts +0 -11
- package/src/utils/streamToString.ts +0 -8
- package/tsconfig.json +0 -5
package/dist/index.cjs
CHANGED
|
@@ -30,16 +30,14 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
30
30
|
// src/index.ts
|
|
31
31
|
var src_exports = {};
|
|
32
32
|
__export(src_exports, {
|
|
33
|
-
|
|
33
|
+
s3Database: () => s3Database,
|
|
34
|
+
s3Storage: () => s3Storage
|
|
34
35
|
});
|
|
35
36
|
module.exports = __toCommonJS(src_exports);
|
|
36
37
|
|
|
37
|
-
// src/
|
|
38
|
-
var import_path = __toESM(require("path"), 1);
|
|
38
|
+
// src/s3Database.ts
|
|
39
39
|
var import_client_s3 = require("@aws-sdk/client-s3");
|
|
40
40
|
var import_lib_storage = require("@aws-sdk/lib-storage");
|
|
41
|
-
var import_plugin_core = require("@hot-updater/plugin-core");
|
|
42
|
-
var import_promises = __toESM(require("fs/promises"), 1);
|
|
43
41
|
var import_mime = __toESM(require("mime"), 1);
|
|
44
42
|
|
|
45
43
|
// src/utils/streamToString.ts
|
|
@@ -52,13 +50,13 @@ var streamToString = (stream) => {
|
|
|
52
50
|
});
|
|
53
51
|
};
|
|
54
52
|
|
|
55
|
-
// src/
|
|
56
|
-
var
|
|
53
|
+
// src/s3Database.ts
|
|
54
|
+
var s3Database = (config, hooks) => (_) => {
|
|
57
55
|
const { bucketName, ...s3Config } = config;
|
|
58
56
|
const client = new import_client_s3.S3Client(s3Config);
|
|
59
|
-
let
|
|
57
|
+
let bundles = [];
|
|
60
58
|
return {
|
|
61
|
-
async
|
|
59
|
+
async commitBundle() {
|
|
62
60
|
try {
|
|
63
61
|
const command = new import_client_s3.GetObjectCommand({
|
|
64
62
|
Bucket: bucketName,
|
|
@@ -66,15 +64,12 @@ var aws = (config) => (_) => {
|
|
|
66
64
|
});
|
|
67
65
|
await client.send(command);
|
|
68
66
|
} catch (e) {
|
|
69
|
-
if (e instanceof import_client_s3.NoSuchKey) {
|
|
70
|
-
import_plugin_core.log.info("Creating new update.json");
|
|
71
|
-
} else {
|
|
67
|
+
if (!(e instanceof import_client_s3.NoSuchKey)) {
|
|
72
68
|
throw e;
|
|
73
69
|
}
|
|
74
70
|
}
|
|
75
|
-
import_plugin_core.log.info("Uploading update.json");
|
|
76
71
|
const Key = "update.json";
|
|
77
|
-
const Body = JSON.stringify(
|
|
72
|
+
const Body = JSON.stringify(bundles);
|
|
78
73
|
const ContentType = import_mime.default.getType(Key) ?? void 0;
|
|
79
74
|
const upload = new import_lib_storage.Upload({
|
|
80
75
|
client,
|
|
@@ -86,29 +81,31 @@ var aws = (config) => (_) => {
|
|
|
86
81
|
}
|
|
87
82
|
});
|
|
88
83
|
await upload.done();
|
|
84
|
+
hooks?.onDatabaseUpdated?.();
|
|
89
85
|
},
|
|
90
|
-
async
|
|
91
|
-
|
|
92
|
-
const targetIndex =
|
|
93
|
-
(u) => u.bundleVersion === targetBundleVersion
|
|
94
|
-
);
|
|
86
|
+
async updateBundle(targetBundleId, newBundle) {
|
|
87
|
+
bundles = await this.getBundles();
|
|
88
|
+
const targetIndex = bundles.findIndex((u) => u.id === targetBundleId);
|
|
95
89
|
if (targetIndex === -1) {
|
|
96
90
|
throw new Error("target bundle version not found");
|
|
97
91
|
}
|
|
98
|
-
Object.assign(
|
|
92
|
+
Object.assign(bundles[targetIndex], newBundle);
|
|
93
|
+
},
|
|
94
|
+
async appendBundle(inputBundle) {
|
|
95
|
+
bundles = await this.getBundles();
|
|
96
|
+
bundles.unshift(inputBundle);
|
|
99
97
|
},
|
|
100
|
-
async
|
|
101
|
-
|
|
102
|
-
updateSources.unshift(source);
|
|
98
|
+
async setBundles(inputBundles) {
|
|
99
|
+
bundles = inputBundles;
|
|
103
100
|
},
|
|
104
|
-
async
|
|
105
|
-
|
|
101
|
+
async getBundleById(bundleId) {
|
|
102
|
+
const bundles2 = await this.getBundles();
|
|
103
|
+
return bundles2.find((bundle) => bundle.id === bundleId) ?? null;
|
|
106
104
|
},
|
|
107
|
-
async
|
|
108
|
-
if (
|
|
109
|
-
return
|
|
105
|
+
async getBundles(refresh = false) {
|
|
106
|
+
if (bundles.length > 0 && !refresh) {
|
|
107
|
+
return bundles;
|
|
110
108
|
}
|
|
111
|
-
import_plugin_core.log.info("Getting update.json");
|
|
112
109
|
try {
|
|
113
110
|
const command = new import_client_s3.GetObjectCommand({
|
|
114
111
|
Bucket: bucketName,
|
|
@@ -116,21 +113,34 @@ var aws = (config) => (_) => {
|
|
|
116
113
|
});
|
|
117
114
|
const { Body: UpdateJsonBody } = await client.send(command);
|
|
118
115
|
const bodyContents = await streamToString(UpdateJsonBody);
|
|
119
|
-
const
|
|
120
|
-
|
|
121
|
-
return
|
|
116
|
+
const _bundle = JSON.parse(bodyContents);
|
|
117
|
+
bundles = _bundle;
|
|
118
|
+
return _bundle;
|
|
122
119
|
} catch (e) {
|
|
123
120
|
if (e instanceof import_client_s3.NoSuchKey) {
|
|
124
121
|
return [];
|
|
125
122
|
}
|
|
126
123
|
throw e;
|
|
127
124
|
}
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
// src/s3Storage.ts
|
|
130
|
+
var import_path = __toESM(require("path"), 1);
|
|
131
|
+
var import_client_s32 = require("@aws-sdk/client-s3");
|
|
132
|
+
var import_lib_storage2 = require("@aws-sdk/lib-storage");
|
|
133
|
+
var import_promises = __toESM(require("fs/promises"), 1);
|
|
134
|
+
var import_mime2 = __toESM(require("mime"), 1);
|
|
135
|
+
var s3Storage = (config, hooks) => (_) => {
|
|
136
|
+
const { bucketName, ...s3Config } = config;
|
|
137
|
+
const client = new import_client_s32.S3Client(s3Config);
|
|
138
|
+
return {
|
|
139
|
+
async deleteBundle(bundleId) {
|
|
140
|
+
const Key = [bundleId].join("/");
|
|
141
|
+
const listCommand = new import_client_s32.ListObjectsV2Command({
|
|
132
142
|
Bucket: bucketName,
|
|
133
|
-
Prefix:
|
|
143
|
+
Prefix: bundleId
|
|
134
144
|
});
|
|
135
145
|
const listResponse = await client.send(listCommand);
|
|
136
146
|
if (listResponse.Contents && listResponse.Contents.length > 0) {
|
|
@@ -144,20 +154,18 @@ var aws = (config) => (_) => {
|
|
|
144
154
|
Quiet: true
|
|
145
155
|
}
|
|
146
156
|
};
|
|
147
|
-
const deleteCommand = new
|
|
157
|
+
const deleteCommand = new import_client_s32.DeleteObjectsCommand(deleteParams);
|
|
148
158
|
await client.send(deleteCommand);
|
|
149
159
|
return Key;
|
|
150
160
|
}
|
|
151
|
-
import_plugin_core.log.error("Bundle Not Found");
|
|
152
161
|
throw new Error("Bundle Not Found");
|
|
153
162
|
},
|
|
154
|
-
async uploadBundle(
|
|
155
|
-
import_plugin_core.log.info("Uploading Bundle");
|
|
163
|
+
async uploadBundle(bundleId, bundlePath) {
|
|
156
164
|
const Body = await import_promises.default.readFile(bundlePath);
|
|
157
|
-
const ContentType =
|
|
165
|
+
const ContentType = import_mime2.default.getType(bundlePath) ?? void 0;
|
|
158
166
|
const filename = import_path.default.basename(bundlePath);
|
|
159
|
-
const Key = [
|
|
160
|
-
const upload = new
|
|
167
|
+
const Key = [bundleId, filename].join("/");
|
|
168
|
+
const upload = new import_lib_storage2.Upload({
|
|
161
169
|
client,
|
|
162
170
|
params: {
|
|
163
171
|
ContentType,
|
|
@@ -168,10 +176,9 @@ var aws = (config) => (_) => {
|
|
|
168
176
|
});
|
|
169
177
|
const response = await upload.done();
|
|
170
178
|
if (!response.Location) {
|
|
171
|
-
import_plugin_core.log.error("Upload Failed");
|
|
172
179
|
throw new Error("Upload Failed");
|
|
173
180
|
}
|
|
174
|
-
|
|
181
|
+
hooks?.onStorageUploaded?.();
|
|
175
182
|
return {
|
|
176
183
|
file: response.Location
|
|
177
184
|
};
|
|
@@ -180,5 +187,6 @@ var aws = (config) => (_) => {
|
|
|
180
187
|
};
|
|
181
188
|
// Annotate the CommonJS export names for ESM import in node:
|
|
182
189
|
0 && (module.exports = {
|
|
183
|
-
|
|
190
|
+
s3Database,
|
|
191
|
+
s3Storage
|
|
184
192
|
});
|
package/dist/index.d.cts
CHANGED
|
@@ -1,9 +1,14 @@
|
|
|
1
1
|
import { S3ClientConfig } from '@aws-sdk/client-s3';
|
|
2
|
-
import { BasePluginArgs,
|
|
2
|
+
import { DatabasePluginHooks, BasePluginArgs, DatabasePlugin, StoragePluginHooks, StoragePlugin } from '@hot-updater/plugin-core';
|
|
3
3
|
|
|
4
|
-
interface
|
|
4
|
+
interface S3DatabaseConfig extends Pick<S3ClientConfig, "credentials" | "region"> {
|
|
5
5
|
bucketName: string;
|
|
6
6
|
}
|
|
7
|
-
declare const
|
|
7
|
+
declare const s3Database: (config: S3DatabaseConfig, hooks?: DatabasePluginHooks) => (_: BasePluginArgs) => DatabasePlugin;
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
interface S3StorageConfig extends Pick<S3ClientConfig, "credentials" | "region"> {
|
|
10
|
+
bucketName: string;
|
|
11
|
+
}
|
|
12
|
+
declare const s3Storage: (config: S3StorageConfig, hooks?: StoragePluginHooks) => (_: BasePluginArgs) => StoragePlugin;
|
|
13
|
+
|
|
14
|
+
export { type S3DatabaseConfig, type S3StorageConfig, s3Database, s3Storage };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,9 +1,14 @@
|
|
|
1
1
|
import { S3ClientConfig } from '@aws-sdk/client-s3';
|
|
2
|
-
import { BasePluginArgs,
|
|
2
|
+
import { DatabasePluginHooks, BasePluginArgs, DatabasePlugin, StoragePluginHooks, StoragePlugin } from '@hot-updater/plugin-core';
|
|
3
3
|
|
|
4
|
-
interface
|
|
4
|
+
interface S3DatabaseConfig extends Pick<S3ClientConfig, "credentials" | "region"> {
|
|
5
5
|
bucketName: string;
|
|
6
6
|
}
|
|
7
|
-
declare const
|
|
7
|
+
declare const s3Database: (config: S3DatabaseConfig, hooks?: DatabasePluginHooks) => (_: BasePluginArgs) => DatabasePlugin;
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
interface S3StorageConfig extends Pick<S3ClientConfig, "credentials" | "region"> {
|
|
10
|
+
bucketName: string;
|
|
11
|
+
}
|
|
12
|
+
declare const s3Storage: (config: S3StorageConfig, hooks?: StoragePluginHooks) => (_: BasePluginArgs) => StoragePlugin;
|
|
13
|
+
|
|
14
|
+
export { type S3DatabaseConfig, type S3StorageConfig, s3Database, s3Storage };
|
package/dist/index.js
CHANGED
|
@@ -1,17 +1,10 @@
|
|
|
1
|
-
// src/
|
|
2
|
-
import path from "path";
|
|
1
|
+
// src/s3Database.ts
|
|
3
2
|
import {
|
|
4
|
-
DeleteObjectsCommand,
|
|
5
3
|
GetObjectCommand,
|
|
6
|
-
ListObjectsV2Command,
|
|
7
4
|
NoSuchKey,
|
|
8
5
|
S3Client
|
|
9
6
|
} from "@aws-sdk/client-s3";
|
|
10
7
|
import { Upload } from "@aws-sdk/lib-storage";
|
|
11
|
-
import {
|
|
12
|
-
log
|
|
13
|
-
} from "@hot-updater/plugin-core";
|
|
14
|
-
import fs from "fs/promises";
|
|
15
8
|
import mime from "mime";
|
|
16
9
|
|
|
17
10
|
// src/utils/streamToString.ts
|
|
@@ -24,13 +17,13 @@ var streamToString = (stream) => {
|
|
|
24
17
|
});
|
|
25
18
|
};
|
|
26
19
|
|
|
27
|
-
// src/
|
|
28
|
-
var
|
|
20
|
+
// src/s3Database.ts
|
|
21
|
+
var s3Database = (config, hooks) => (_) => {
|
|
29
22
|
const { bucketName, ...s3Config } = config;
|
|
30
23
|
const client = new S3Client(s3Config);
|
|
31
|
-
let
|
|
24
|
+
let bundles = [];
|
|
32
25
|
return {
|
|
33
|
-
async
|
|
26
|
+
async commitBundle() {
|
|
34
27
|
try {
|
|
35
28
|
const command = new GetObjectCommand({
|
|
36
29
|
Bucket: bucketName,
|
|
@@ -38,15 +31,12 @@ var aws = (config) => (_) => {
|
|
|
38
31
|
});
|
|
39
32
|
await client.send(command);
|
|
40
33
|
} catch (e) {
|
|
41
|
-
if (e instanceof NoSuchKey) {
|
|
42
|
-
log.info("Creating new update.json");
|
|
43
|
-
} else {
|
|
34
|
+
if (!(e instanceof NoSuchKey)) {
|
|
44
35
|
throw e;
|
|
45
36
|
}
|
|
46
37
|
}
|
|
47
|
-
log.info("Uploading update.json");
|
|
48
38
|
const Key = "update.json";
|
|
49
|
-
const Body = JSON.stringify(
|
|
39
|
+
const Body = JSON.stringify(bundles);
|
|
50
40
|
const ContentType = mime.getType(Key) ?? void 0;
|
|
51
41
|
const upload = new Upload({
|
|
52
42
|
client,
|
|
@@ -58,29 +48,31 @@ var aws = (config) => (_) => {
|
|
|
58
48
|
}
|
|
59
49
|
});
|
|
60
50
|
await upload.done();
|
|
51
|
+
hooks?.onDatabaseUpdated?.();
|
|
61
52
|
},
|
|
62
|
-
async
|
|
63
|
-
|
|
64
|
-
const targetIndex =
|
|
65
|
-
(u) => u.bundleVersion === targetBundleVersion
|
|
66
|
-
);
|
|
53
|
+
async updateBundle(targetBundleId, newBundle) {
|
|
54
|
+
bundles = await this.getBundles();
|
|
55
|
+
const targetIndex = bundles.findIndex((u) => u.id === targetBundleId);
|
|
67
56
|
if (targetIndex === -1) {
|
|
68
57
|
throw new Error("target bundle version not found");
|
|
69
58
|
}
|
|
70
|
-
Object.assign(
|
|
59
|
+
Object.assign(bundles[targetIndex], newBundle);
|
|
71
60
|
},
|
|
72
|
-
async
|
|
73
|
-
|
|
74
|
-
|
|
61
|
+
async appendBundle(inputBundle) {
|
|
62
|
+
bundles = await this.getBundles();
|
|
63
|
+
bundles.unshift(inputBundle);
|
|
75
64
|
},
|
|
76
|
-
async
|
|
77
|
-
|
|
65
|
+
async setBundles(inputBundles) {
|
|
66
|
+
bundles = inputBundles;
|
|
78
67
|
},
|
|
79
|
-
async
|
|
80
|
-
|
|
81
|
-
|
|
68
|
+
async getBundleById(bundleId) {
|
|
69
|
+
const bundles2 = await this.getBundles();
|
|
70
|
+
return bundles2.find((bundle) => bundle.id === bundleId) ?? null;
|
|
71
|
+
},
|
|
72
|
+
async getBundles(refresh = false) {
|
|
73
|
+
if (bundles.length > 0 && !refresh) {
|
|
74
|
+
return bundles;
|
|
82
75
|
}
|
|
83
|
-
log.info("Getting update.json");
|
|
84
76
|
try {
|
|
85
77
|
const command = new GetObjectCommand({
|
|
86
78
|
Bucket: bucketName,
|
|
@@ -88,21 +80,38 @@ var aws = (config) => (_) => {
|
|
|
88
80
|
});
|
|
89
81
|
const { Body: UpdateJsonBody } = await client.send(command);
|
|
90
82
|
const bodyContents = await streamToString(UpdateJsonBody);
|
|
91
|
-
const
|
|
92
|
-
|
|
93
|
-
return
|
|
83
|
+
const _bundle = JSON.parse(bodyContents);
|
|
84
|
+
bundles = _bundle;
|
|
85
|
+
return _bundle;
|
|
94
86
|
} catch (e) {
|
|
95
87
|
if (e instanceof NoSuchKey) {
|
|
96
88
|
return [];
|
|
97
89
|
}
|
|
98
90
|
throw e;
|
|
99
91
|
}
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
// src/s3Storage.ts
|
|
97
|
+
import path from "path";
|
|
98
|
+
import {
|
|
99
|
+
DeleteObjectsCommand,
|
|
100
|
+
ListObjectsV2Command,
|
|
101
|
+
S3Client as S3Client2
|
|
102
|
+
} from "@aws-sdk/client-s3";
|
|
103
|
+
import { Upload as Upload2 } from "@aws-sdk/lib-storage";
|
|
104
|
+
import fs from "fs/promises";
|
|
105
|
+
import mime2 from "mime";
|
|
106
|
+
var s3Storage = (config, hooks) => (_) => {
|
|
107
|
+
const { bucketName, ...s3Config } = config;
|
|
108
|
+
const client = new S3Client2(s3Config);
|
|
109
|
+
return {
|
|
110
|
+
async deleteBundle(bundleId) {
|
|
111
|
+
const Key = [bundleId].join("/");
|
|
103
112
|
const listCommand = new ListObjectsV2Command({
|
|
104
113
|
Bucket: bucketName,
|
|
105
|
-
Prefix:
|
|
114
|
+
Prefix: bundleId
|
|
106
115
|
});
|
|
107
116
|
const listResponse = await client.send(listCommand);
|
|
108
117
|
if (listResponse.Contents && listResponse.Contents.length > 0) {
|
|
@@ -120,16 +129,14 @@ var aws = (config) => (_) => {
|
|
|
120
129
|
await client.send(deleteCommand);
|
|
121
130
|
return Key;
|
|
122
131
|
}
|
|
123
|
-
log.error("Bundle Not Found");
|
|
124
132
|
throw new Error("Bundle Not Found");
|
|
125
133
|
},
|
|
126
|
-
async uploadBundle(
|
|
127
|
-
log.info("Uploading Bundle");
|
|
134
|
+
async uploadBundle(bundleId, bundlePath) {
|
|
128
135
|
const Body = await fs.readFile(bundlePath);
|
|
129
|
-
const ContentType =
|
|
136
|
+
const ContentType = mime2.getType(bundlePath) ?? void 0;
|
|
130
137
|
const filename = path.basename(bundlePath);
|
|
131
|
-
const Key = [
|
|
132
|
-
const upload = new
|
|
138
|
+
const Key = [bundleId, filename].join("/");
|
|
139
|
+
const upload = new Upload2({
|
|
133
140
|
client,
|
|
134
141
|
params: {
|
|
135
142
|
ContentType,
|
|
@@ -140,10 +147,9 @@ var aws = (config) => (_) => {
|
|
|
140
147
|
});
|
|
141
148
|
const response = await upload.done();
|
|
142
149
|
if (!response.Location) {
|
|
143
|
-
log.error("Upload Failed");
|
|
144
150
|
throw new Error("Upload Failed");
|
|
145
151
|
}
|
|
146
|
-
|
|
152
|
+
hooks?.onStorageUploaded?.();
|
|
147
153
|
return {
|
|
148
154
|
file: response.Location
|
|
149
155
|
};
|
|
@@ -151,5 +157,6 @@ var aws = (config) => (_) => {
|
|
|
151
157
|
};
|
|
152
158
|
};
|
|
153
159
|
export {
|
|
154
|
-
|
|
160
|
+
s3Database,
|
|
161
|
+
s3Storage
|
|
155
162
|
};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hot-updater/aws",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.1.
|
|
4
|
+
"version": "0.1.5",
|
|
5
5
|
"description": "React Native OTA solution for self-hosted",
|
|
6
6
|
"main": "dist/index.cjs",
|
|
7
7
|
"module": "dist/index.js",
|
|
@@ -16,8 +16,12 @@
|
|
|
16
16
|
"publishConfig": {
|
|
17
17
|
"access": "public"
|
|
18
18
|
},
|
|
19
|
+
"files": [
|
|
20
|
+
"dist",
|
|
21
|
+
"package.json"
|
|
22
|
+
],
|
|
19
23
|
"dependencies": {
|
|
20
|
-
"@hot-updater/plugin-core": "0.1.
|
|
24
|
+
"@hot-updater/plugin-core": "0.1.5",
|
|
21
25
|
"@aws-sdk/client-s3": "^3.685.0",
|
|
22
26
|
"@aws-sdk/lib-storage": "^3.685.0",
|
|
23
27
|
"mime": "^4.0.4"
|
package/src/aws.ts
DELETED
|
@@ -1,173 +0,0 @@
|
|
|
1
|
-
import path from "path";
|
|
2
|
-
import {
|
|
3
|
-
DeleteObjectsCommand,
|
|
4
|
-
GetObjectCommand,
|
|
5
|
-
ListObjectsV2Command,
|
|
6
|
-
NoSuchKey,
|
|
7
|
-
S3Client,
|
|
8
|
-
type S3ClientConfig,
|
|
9
|
-
} from "@aws-sdk/client-s3";
|
|
10
|
-
import { Upload } from "@aws-sdk/lib-storage";
|
|
11
|
-
import {
|
|
12
|
-
type BasePluginArgs,
|
|
13
|
-
type DeployPlugin,
|
|
14
|
-
type UpdateSource,
|
|
15
|
-
log,
|
|
16
|
-
} from "@hot-updater/plugin-core";
|
|
17
|
-
import fs from "fs/promises";
|
|
18
|
-
import mime from "mime";
|
|
19
|
-
import { streamToString } from "./utils/streamToString";
|
|
20
|
-
|
|
21
|
-
export interface AwsConfig
|
|
22
|
-
extends Pick<S3ClientConfig, "credentials" | "region"> {
|
|
23
|
-
bucketName: string;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export const aws =
|
|
27
|
-
(config: AwsConfig) =>
|
|
28
|
-
(_: BasePluginArgs): DeployPlugin => {
|
|
29
|
-
const { bucketName, ...s3Config } = config;
|
|
30
|
-
const client = new S3Client(s3Config);
|
|
31
|
-
|
|
32
|
-
let updateSources: UpdateSource[] = [];
|
|
33
|
-
|
|
34
|
-
return {
|
|
35
|
-
async commitUpdateSource() {
|
|
36
|
-
try {
|
|
37
|
-
const command = new GetObjectCommand({
|
|
38
|
-
Bucket: bucketName,
|
|
39
|
-
Key: "update.json",
|
|
40
|
-
});
|
|
41
|
-
await client.send(command);
|
|
42
|
-
} catch (e) {
|
|
43
|
-
if (e instanceof NoSuchKey) {
|
|
44
|
-
log.info("Creating new update.json");
|
|
45
|
-
} else {
|
|
46
|
-
throw e;
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
log.info("Uploading update.json");
|
|
51
|
-
const Key = "update.json";
|
|
52
|
-
const Body = JSON.stringify(updateSources);
|
|
53
|
-
const ContentType = mime.getType(Key) ?? void 0;
|
|
54
|
-
|
|
55
|
-
const upload = new Upload({
|
|
56
|
-
client,
|
|
57
|
-
params: {
|
|
58
|
-
ContentType,
|
|
59
|
-
Bucket: bucketName,
|
|
60
|
-
Key,
|
|
61
|
-
Body,
|
|
62
|
-
},
|
|
63
|
-
});
|
|
64
|
-
await upload.done();
|
|
65
|
-
},
|
|
66
|
-
async updateUpdateSource(
|
|
67
|
-
targetBundleVersion: number,
|
|
68
|
-
newSource: Partial<UpdateSource>,
|
|
69
|
-
) {
|
|
70
|
-
updateSources = await this.getUpdateSources();
|
|
71
|
-
|
|
72
|
-
const targetIndex = updateSources.findIndex(
|
|
73
|
-
(u) => u.bundleVersion === targetBundleVersion,
|
|
74
|
-
);
|
|
75
|
-
if (targetIndex === -1) {
|
|
76
|
-
throw new Error("target bundle version not found");
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
Object.assign(updateSources[targetIndex], newSource);
|
|
80
|
-
},
|
|
81
|
-
async appendUpdateSource(source) {
|
|
82
|
-
updateSources = await this.getUpdateSources();
|
|
83
|
-
updateSources.unshift(source);
|
|
84
|
-
},
|
|
85
|
-
async setUpdateSources(sources) {
|
|
86
|
-
updateSources = sources;
|
|
87
|
-
},
|
|
88
|
-
|
|
89
|
-
async getUpdateSources(refresh = false) {
|
|
90
|
-
if (updateSources.length > 0 && !refresh) {
|
|
91
|
-
return updateSources;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
log.info("Getting update.json");
|
|
95
|
-
|
|
96
|
-
try {
|
|
97
|
-
const command = new GetObjectCommand({
|
|
98
|
-
Bucket: bucketName,
|
|
99
|
-
Key: "update.json",
|
|
100
|
-
});
|
|
101
|
-
const { Body: UpdateJsonBody } = await client.send(command);
|
|
102
|
-
const bodyContents = await streamToString(UpdateJsonBody);
|
|
103
|
-
const _updateSource = JSON.parse(bodyContents);
|
|
104
|
-
updateSources = _updateSource;
|
|
105
|
-
return _updateSource as UpdateSource[];
|
|
106
|
-
} catch (e) {
|
|
107
|
-
if (e instanceof NoSuchKey) {
|
|
108
|
-
return [];
|
|
109
|
-
}
|
|
110
|
-
throw e;
|
|
111
|
-
}
|
|
112
|
-
},
|
|
113
|
-
async deleteBundle(platform, bundleVersion) {
|
|
114
|
-
const Key = [bundleVersion, platform].join("/");
|
|
115
|
-
|
|
116
|
-
const listCommand = new ListObjectsV2Command({
|
|
117
|
-
Bucket: bucketName,
|
|
118
|
-
Prefix: `${bundleVersion}/${platform}`,
|
|
119
|
-
});
|
|
120
|
-
const listResponse = await client.send(listCommand);
|
|
121
|
-
|
|
122
|
-
if (listResponse.Contents && listResponse.Contents.length > 0) {
|
|
123
|
-
const objectsToDelete = listResponse.Contents.map((obj) => ({
|
|
124
|
-
Key: obj.Key,
|
|
125
|
-
}));
|
|
126
|
-
|
|
127
|
-
const deleteParams = {
|
|
128
|
-
Bucket: bucketName,
|
|
129
|
-
Delete: {
|
|
130
|
-
Objects: objectsToDelete,
|
|
131
|
-
Quiet: true,
|
|
132
|
-
},
|
|
133
|
-
};
|
|
134
|
-
|
|
135
|
-
const deleteCommand = new DeleteObjectsCommand(deleteParams);
|
|
136
|
-
await client.send(deleteCommand);
|
|
137
|
-
return Key;
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
log.error("Bundle Not Found");
|
|
141
|
-
throw new Error("Bundle Not Found");
|
|
142
|
-
},
|
|
143
|
-
async uploadBundle(platform, bundleVersion, bundlePath) {
|
|
144
|
-
log.info("Uploading Bundle");
|
|
145
|
-
|
|
146
|
-
const Body = await fs.readFile(bundlePath);
|
|
147
|
-
const ContentType = mime.getType(bundlePath) ?? void 0;
|
|
148
|
-
|
|
149
|
-
const filename = path.basename(bundlePath);
|
|
150
|
-
|
|
151
|
-
const Key = [bundleVersion, platform, filename].join("/");
|
|
152
|
-
const upload = new Upload({
|
|
153
|
-
client,
|
|
154
|
-
params: {
|
|
155
|
-
ContentType,
|
|
156
|
-
Bucket: bucketName,
|
|
157
|
-
Key,
|
|
158
|
-
Body,
|
|
159
|
-
},
|
|
160
|
-
});
|
|
161
|
-
const response = await upload.done();
|
|
162
|
-
if (!response.Location) {
|
|
163
|
-
log.error("Upload Failed");
|
|
164
|
-
throw new Error("Upload Failed");
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
log?.info(`Uploaded: ${Key}`);
|
|
168
|
-
return {
|
|
169
|
-
file: response.Location,
|
|
170
|
-
};
|
|
171
|
-
},
|
|
172
|
-
};
|
|
173
|
-
};
|
package/src/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from "./aws";
|
package/src/utils/readDir.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { lstatSync } from "fs";
|
|
2
|
-
import path from "path";
|
|
3
|
-
import { readdir } from "fs/promises";
|
|
4
|
-
|
|
5
|
-
export const readDir = async (dir: string) => {
|
|
6
|
-
const files = await readdir(dir, {
|
|
7
|
-
recursive: true,
|
|
8
|
-
});
|
|
9
|
-
|
|
10
|
-
return files.filter((file) => !lstatSync(path.join(dir, file)).isDirectory());
|
|
11
|
-
};
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
export const streamToString = (stream: any): Promise<string> => {
|
|
2
|
-
return new Promise((resolve, reject) => {
|
|
3
|
-
const chunks: Uint8Array[] = [];
|
|
4
|
-
stream.on("data", (chunk: Uint8Array) => chunks.push(chunk));
|
|
5
|
-
stream.on("error", reject);
|
|
6
|
-
stream.on("end", () => resolve(Buffer.concat(chunks).toString("utf8")));
|
|
7
|
-
});
|
|
8
|
-
};
|