2017-05-03 05:59:08 +09:00
|
|
|
/* Copyright 2017 Mozilla Foundation
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
var babel = require('plugin-babel');
|
|
|
|
|
|
|
|
var cacheExpiration = 60 /* min */ * 60 * 1000;
|
|
|
|
var dbVersion = 1;
|
|
|
|
var dbName = 'babelcache';
|
|
|
|
var dbCacheTable = 'translated';
|
|
|
|
var dbPromise;
|
|
|
|
|
|
|
|
function getDb() {
|
|
|
|
if (!dbPromise) {
|
|
|
|
dbPromise = new Promise(function (resolve, reject) {
|
|
|
|
var request = indexedDB.open(dbName, dbVersion);
|
|
|
|
request.onupgradeneeded = function() {
|
|
|
|
var db = request.result;
|
Fix the remaining cases of inconsistent spacing and trailing commas in objects, and enable the `comma-dangle` and `object-curly-spacing` ESLint rules
http://eslint.org/docs/rules/comma-dangle
http://eslint.org/docs/rules/object-curly-spacing
*Please note:* This patch was created automatically, using the ESLint `--fix` command line option. In a couple of places this caused lines to become too long, and I've fixed those manually; please refer to the interdiff below for the only hand-edits in this patch.
```diff
diff --git a/gulpfile.js b/gulpfile.js
index d18b9c58..7d47fd8d 100644
--- a/gulpfile.js
+++ b/gulpfile.js
@@ -1247,7 +1247,8 @@ gulp.task('gh-pages-git', ['gh-pages-prepare', 'wintersmith'], function () {
var reason = process.env['PDFJS_UPDATE_REASON'];
safeSpawnSync('git', ['init'], { cwd: GH_PAGES_DIR, });
- safeSpawnSync('git', ['remote', 'add', 'origin', REPO], { cwd: GH_PAGES_DIR, });
+ safeSpawnSync('git', ['remote', 'add', 'origin', REPO],
+ { cwd: GH_PAGES_DIR, });
safeSpawnSync('git', ['add', '-A'], { cwd: GH_PAGES_DIR, });
safeSpawnSync('git', [
'commit', '-am', 'gh-pages site created via gulpfile.js script',
```
2017-06-03 00:13:35 +09:00
|
|
|
db.createObjectStore(dbCacheTable, { keyPath: 'address', });
|
2017-05-03 05:59:08 +09:00
|
|
|
};
|
|
|
|
request.onsuccess = function() {
|
|
|
|
var db = request.result;
|
|
|
|
resolve(db);
|
|
|
|
};
|
|
|
|
request.onerror = function () {
|
2018-07-02 21:01:45 +09:00
|
|
|
console.warn('getDb: ' + request.error);
|
2017-05-03 05:59:08 +09:00
|
|
|
reject(request.error);
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return dbPromise;
|
|
|
|
}
|
|
|
|
|
|
|
|
function storeCache(address, hashCode, translated, format) {
|
|
|
|
return getDb().then(function (db) {
|
|
|
|
var tx = db.transaction(dbCacheTable, 'readwrite');
|
|
|
|
var store = tx.objectStore(dbCacheTable);
|
|
|
|
store.put({
|
|
|
|
address: address,
|
|
|
|
hashCode: hashCode,
|
|
|
|
translated: translated,
|
|
|
|
expires: Date.now() + cacheExpiration,
|
|
|
|
format: format,
|
|
|
|
});
|
|
|
|
return new Promise(function (resolve, reject) {
|
|
|
|
tx.oncomplete = function () {
|
|
|
|
resolve();
|
|
|
|
};
|
2017-05-14 23:01:32 +09:00
|
|
|
tx.onerror = function () {
|
|
|
|
resolve();
|
|
|
|
};
|
2017-05-03 05:59:08 +09:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function loadCache(address, hashCode) {
|
|
|
|
return getDb().then(function (db) {
|
|
|
|
var tx = db.transaction(dbCacheTable, 'readonly');
|
|
|
|
var store = tx.objectStore(dbCacheTable);
|
|
|
|
var getAddress = store.get(address);
|
|
|
|
return new Promise(function (resolve, reject) {
|
|
|
|
tx.oncomplete = function () {
|
|
|
|
var found = getAddress.result;
|
|
|
|
var isValid = found && found.hashCode === hashCode &&
|
|
|
|
Date.now() < found.expires;
|
|
|
|
resolve(isValid ? {
|
|
|
|
translated: found.translated,
|
|
|
|
format: found.format,
|
|
|
|
} : null);
|
|
|
|
};
|
2017-05-14 23:01:32 +09:00
|
|
|
tx.onerror = function () {
|
|
|
|
resolve(null);
|
|
|
|
};
|
2017-05-03 05:59:08 +09:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
var encoder = new TextEncoder('utf-8');
|
|
|
|
function sha256(str) {
|
|
|
|
var buffer = encoder.encode(str);
|
|
|
|
return crypto.subtle.digest('SHA-256', buffer).then(function (hash) {
|
|
|
|
var data = new Int32Array(hash);
|
|
|
|
return data[0].toString(36) + '-' + data[1].toString(36) + '-' +
|
|
|
|
data[2].toString(36) + '-' + data[3].toString(36);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.translate = function (load, opt) {
|
2018-07-02 21:01:45 +09:00
|
|
|
var savedHashCode, babelTranslateError;
|
2017-05-03 05:59:08 +09:00
|
|
|
return sha256(load.source).then(function (hashCode) {
|
|
|
|
savedHashCode = hashCode;
|
|
|
|
return loadCache(load.address, hashCode);
|
|
|
|
}).then(function (cache) {
|
|
|
|
if (cache) {
|
|
|
|
load.metadata.format = cache.format;
|
|
|
|
return cache.translated;
|
|
|
|
}
|
|
|
|
return babel.translate.call(this, load, opt).then(function(translated) {
|
|
|
|
return storeCache(load.address, savedHashCode,
|
|
|
|
translated, load.metadata.format).then(function () {
|
|
|
|
return translated;
|
|
|
|
});
|
2018-07-02 21:01:45 +09:00
|
|
|
}, function(reason) {
|
|
|
|
throw (babelTranslateError = reason);
|
2017-05-03 05:59:08 +09:00
|
|
|
});
|
2018-07-02 21:01:45 +09:00
|
|
|
}.bind(this)).catch(function(reason) {
|
|
|
|
if (babelTranslateError) {
|
|
|
|
throw babelTranslateError;
|
|
|
|
}
|
|
|
|
return babel.translate.call(this, load, opt);
|
2017-05-03 05:59:08 +09:00
|
|
|
}.bind(this));
|
|
|
|
};
|