Skip to content

Commit 3190ce6

Browse files
binghaiwangPeterRao
authored andcommitted
feat: publish CDN check script (#452)
* feat: publish CDN check script * feat: publish check add md5 check
1 parent 54db574 commit 3190ce6

File tree

4 files changed

+70
-10
lines changed

4 files changed

+70
-10
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,13 @@
2525
"build-test": "node browser-build.js > test/browser/build/aliyun-oss-sdk.js && node task/browser-test-build.js > test/browser/build/tests.js",
2626
"browser-test": "npm run build-test && karma start",
2727
"build-dist": "node browser-build.js > dist/aliyun-oss-sdk.js && MINIFY=1 node browser-build.js > dist/aliyun-oss-sdk.min.js",
28+
"publish-to-npm": "node publish-npm-check.js && npm publish",
2829
"publish-to-cdn": "node publish.js"
2930
},
3031
"git-pre-hooks": {
3132
"pre-release": "npm run build-dist",
3233
"post-release": [
33-
"npm publish",
34+
"npm run publish-to-npm",
3435
"npm run publish-to-cdn"
3536
]
3637
},

publish-check.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2+
const fs = require('fs');
3+
const crypto = require('crypto');
4+
const pkg = require('./package.json');
5+
6+
exports.checkDist = function checkDist(filePath) {
7+
const stat = fs.statSync(filePath);
8+
if (stat.size === 0) {
9+
throw new Error('dist file size is 0');
10+
}
11+
const data = fs.readFileSync(filePath, 'utf8');
12+
const arr = data.split('\n')[0].split(' ');
13+
const distVer = arr[arr.length - 1];
14+
console.log('pkgVer', `v${pkg.version}`);
15+
console.log('distVer', distVer);
16+
if (distVer !== `v${pkg.version}`) {
17+
throw new Error('version is not match');
18+
}
19+
};
20+
21+
exports.checkCDNFile = function* (object, store) {
22+
const result = yield store.head(object);
23+
if (result.status !== 200 || result.res.headers['content-length'] === '0') {
24+
yield store.delete(object);
25+
throw new Error('CDN file is incorrect or size is 0');
26+
}
27+
};
28+
29+
exports.caculateFileMd5 = function (filePath) {
30+
return new Promise((resolve, reject) => {
31+
const rs = fs.createReadStream(filePath);
32+
33+
const hash = crypto.createHash('md5');
34+
rs.on('data', hash.update.bind(hash));
35+
rs.on('error', (err) => {
36+
reject(err);
37+
});
38+
rs.on('end', () => {
39+
const md5Content = hash.digest('base64');
40+
resolve(md5Content);
41+
});
42+
});
43+
};
44+

publish-npm-check.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
const check = require('./publish-check');
3+
4+
const dist = './dist/aliyun-oss-sdk.min.js';
5+
6+
check.checkDist(dist);

publish.js

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,29 @@
1-
var env = process.env;
2-
var oss = require('.');
3-
var co = require('co');
4-
var pkg = require('./package.json');
1+
const { env } = process;
2+
const oss = require('.');
3+
const co = require('co');
4+
const pkg = require('./package.json');
5+
const check = require('./publish-check');
56

6-
var store = oss({
7+
const store = oss({
78
accessKeyId: env.ALI_SDK_OSS_CDN_ID,
89
accessKeySecret: env.ALI_SDK_OSS_CDN_SECRET,
910
endpoint: env.ALI_SDK_OSS_CDN_ENDPOINT,
1011
bucket: env.ALI_SDK_OSS_CDN_BUCKET,
1112
});
1213

13-
var current = 'aliyun-oss-sdk-' + pkg.version + '.min.js';
14-
var dist = './dist/aliyun-oss-sdk.min.js';
14+
const current = `aliyun-oss-sdk-${pkg.version}.min.js`;
15+
const dist = './dist/aliyun-oss-sdk.min.js';
1516

1617
co(function* () {
17-
yield store.put(current, dist);
18-
}).catch(function (err) {
18+
check.checkDist(dist); // check file exist
19+
const contentMd5 = yield check.caculateFileMd5(dist); // check md5 to server
20+
yield store.put(current, dist, {
21+
headers: {
22+
'Content-Md5': contentMd5,
23+
},
24+
});
25+
yield check.checkCDNFile(current, store);// check cdn file
26+
console.log(`publish CDN success, version is ${pkg.version}`);
27+
}).catch((err) => {
1928
console.log(err);
2029
});

0 commit comments

Comments
 (0)