hw-weapp-compiler 1.0.24 → 1.0.25
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/build/utils/upload.js +41 -4
- package/package.json +2 -2
- package/test/upload-async-client.js +72 -0
package/build/utils/upload.js
CHANGED
|
@@ -23,6 +23,7 @@ const OBS_PENDING_WARNING_MS = 15 * 1000;
|
|
|
23
23
|
const OBS_TIMEOUT_BUFFER_MS = 5 * 1000;
|
|
24
24
|
|
|
25
25
|
let obsClient;
|
|
26
|
+
let obsClientReady;
|
|
26
27
|
let ossClient;
|
|
27
28
|
let progress;
|
|
28
29
|
let uploadQueue = {};
|
|
@@ -201,6 +202,37 @@ function runObsRequest(operation, file, key, request) {
|
|
|
201
202
|
});
|
|
202
203
|
}
|
|
203
204
|
|
|
205
|
+
function waitForObsClientReady(client) {
|
|
206
|
+
if (client.util && client.util.signatureContext && client.util.server) {
|
|
207
|
+
return Promise.resolve(client);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
const { operationTimeoutMs } = getObsRuntimeOptions();
|
|
211
|
+
const startedAt = Date.now();
|
|
212
|
+
|
|
213
|
+
return new Promise((resolve, reject) => {
|
|
214
|
+
const checkReady = () => {
|
|
215
|
+
if (client.util && client.util.signatureContext && client.util.server) {
|
|
216
|
+
resolve(client);
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
if (Date.now() - startedAt >= operationTimeoutMs) {
|
|
221
|
+
const error = new Error(
|
|
222
|
+
`OBS client initialization timed out after ${operationTimeoutMs / 1000}s`,
|
|
223
|
+
);
|
|
224
|
+
error.code = 'OBS_CLIENT_INIT_TIMEOUT';
|
|
225
|
+
reject(error);
|
|
226
|
+
return;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
setImmediate(checkReady);
|
|
230
|
+
};
|
|
231
|
+
|
|
232
|
+
setImmediate(checkReady);
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
|
|
204
236
|
function getObsClient() {
|
|
205
237
|
if (!obsClient) {
|
|
206
238
|
const { timeoutSeconds, maxRetryCount } = getObsRuntimeOptions();
|
|
@@ -209,8 +241,11 @@ function getObsClient() {
|
|
|
209
241
|
timeout: timeoutSeconds,
|
|
210
242
|
max_retry_count: maxRetryCount,
|
|
211
243
|
});
|
|
244
|
+
// esdk-obs-nodejs 3.26.8 将 initFactory 改成了 async,但构造函数没有等待它。
|
|
245
|
+
// 立即请求会在 signatureContext 尚未赋值时破坏 SDK 的 bucket 协商锁。
|
|
246
|
+
obsClientReady = waitForObsClientReady(obsClient);
|
|
212
247
|
}
|
|
213
|
-
return
|
|
248
|
+
return obsClientReady;
|
|
214
249
|
}
|
|
215
250
|
|
|
216
251
|
function getOssClient() {
|
|
@@ -233,10 +268,11 @@ function getOssStat(file) {
|
|
|
233
268
|
return getOssClient().head(getRemoteKey(ossConfig, file));
|
|
234
269
|
}
|
|
235
270
|
|
|
236
|
-
function doObsUpload(file) {
|
|
271
|
+
async function doObsUpload(file) {
|
|
237
272
|
const key = getRemoteKey(obsConfig, file);
|
|
273
|
+
const client = await getObsClient();
|
|
238
274
|
return runObsRequest('upload', file, key, (finish, reportProgress, registerRequest) => {
|
|
239
|
-
|
|
275
|
+
client.putObject(
|
|
240
276
|
{
|
|
241
277
|
Bucket: obsConfig.bucket,
|
|
242
278
|
Key: key,
|
|
@@ -261,8 +297,9 @@ function doObsUpload(file) {
|
|
|
261
297
|
}
|
|
262
298
|
async function getObsStat(file) {
|
|
263
299
|
const key = getRemoteKey(obsConfig, file);
|
|
300
|
+
const client = await getObsClient();
|
|
264
301
|
return runObsRequest('metadata check', file, key, (finish, _reportProgress, registerRequest) => {
|
|
265
|
-
|
|
302
|
+
client.getObjectMetadata(
|
|
266
303
|
{
|
|
267
304
|
Bucket: obsConfig.bucket,
|
|
268
305
|
Key: key,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "hw-weapp-compiler",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.25",
|
|
4
4
|
"description": "基于 webpack5 的小程序构建工具,兼容 Node.js 14~24",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"bin": {
|
|
@@ -53,6 +53,6 @@
|
|
|
53
53
|
"build": "node ./index.js build",
|
|
54
54
|
"dev": "nodemon --inspect --trace-deprecation --watch build ./index.js dev",
|
|
55
55
|
"simulation": "node ./index.js build -s",
|
|
56
|
-
"test": "node ./test/upload.js"
|
|
56
|
+
"test": "node ./test/upload.js && node ./test/upload-async-client.js"
|
|
57
57
|
}
|
|
58
58
|
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
const assert = require('assert');
|
|
2
|
+
const fs = require('fs');
|
|
3
|
+
const Module = require('module');
|
|
4
|
+
const os = require('os');
|
|
5
|
+
const path = require('path');
|
|
6
|
+
|
|
7
|
+
class AsyncOBSClient {
|
|
8
|
+
constructor(config) {
|
|
9
|
+
this.config = config;
|
|
10
|
+
this.util = {
|
|
11
|
+
server: null,
|
|
12
|
+
signatureContext: null,
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
// 模拟 esdk-obs-nodejs 3.26.8:initFactory 在第一次 await 后才完成赋值。
|
|
16
|
+
Promise.resolve().then(() => {
|
|
17
|
+
this.util.server = config.server;
|
|
18
|
+
this.util.signatureContext = { signature: 'obs' };
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
getObjectMetadata(_params, callback) {
|
|
23
|
+
assert(this.util.signatureContext, 'request started before OBS client initialization');
|
|
24
|
+
callback(null, { CommonMsg: { Status: 200 } });
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
async function run() {
|
|
29
|
+
const projectDir = fs.mkdtempSync(path.join(os.tmpdir(), 'hw-upload-async-client-test-'));
|
|
30
|
+
const originalCwd = process.cwd();
|
|
31
|
+
const originalLoad = Module._load;
|
|
32
|
+
|
|
33
|
+
try {
|
|
34
|
+
fs.mkdirSync(path.join(projectDir, 'dist/assets'), { recursive: true });
|
|
35
|
+
fs.writeFileSync(path.join(projectDir, 'dist/assets/ready.png'), 'ready');
|
|
36
|
+
fs.writeFileSync(
|
|
37
|
+
path.join(projectDir, '.weapp.js'),
|
|
38
|
+
`module.exports = { obsConfig: ${JSON.stringify({
|
|
39
|
+
access_key_id: 'test-ak',
|
|
40
|
+
secret_access_key: 'test-sk',
|
|
41
|
+
server: 'http://obs.example.com',
|
|
42
|
+
bucket: 'test-bucket',
|
|
43
|
+
dir: 'test-dir',
|
|
44
|
+
operation_timeout: 1,
|
|
45
|
+
})} };`,
|
|
46
|
+
);
|
|
47
|
+
|
|
48
|
+
Module._load = function load(request, parent, isMain) {
|
|
49
|
+
if (request === 'esdk-obs-nodejs') {
|
|
50
|
+
return AsyncOBSClient;
|
|
51
|
+
}
|
|
52
|
+
return originalLoad.call(this, request, parent, isMain);
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
process.chdir(projectDir);
|
|
56
|
+
const uploadModule = path.resolve(originalCwd, 'build/utils/upload.js');
|
|
57
|
+
const { addToUploadQueue } = require(uploadModule);
|
|
58
|
+
await addToUploadQueue(['assets/ready.png']);
|
|
59
|
+
|
|
60
|
+
// 等待 storage.js 的防抖写入结束后再清理测试目录。
|
|
61
|
+
await new Promise((resolve) => setTimeout(resolve, 400));
|
|
62
|
+
} finally {
|
|
63
|
+
Module._load = originalLoad;
|
|
64
|
+
process.chdir(originalCwd);
|
|
65
|
+
fs.rmSync(projectDir, { recursive: true, force: true });
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
run().catch((error) => {
|
|
70
|
+
console.error(error);
|
|
71
|
+
process.exitCode = 1;
|
|
72
|
+
});
|