@hot-updater/aws 0.1.2 → 0.1.4
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 +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/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.4",
|
|
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.4",
|
|
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
|
-
};
|