Files
pdf.js/external/systemjs/plugin-babel-cached.js
T

153 lines
4.1 KiB
JavaScript
Raw Normal View History

2017-05-02 15:59:08 -05: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.
*/
/* eslint-disable no-var */
var babel = require("plugin-babel");
2017-05-02 15:59:08 -05:00
var cacheExpiration = 60 /* min */ * 60 * 1000;
var dbVersion = 1;
var dbName = "babelcache";
var dbCacheTable = "translated";
2017-05-02 15:59:08 -05:00
var dbPromise;
function getDb() {
if (!dbPromise) {
2020-04-14 12:28:14 +02:00
dbPromise = new Promise(function (resolve, reject) {
2017-05-02 15:59:08 -05:00
var request = indexedDB.open(dbName, dbVersion);
2020-04-14 12:28:14 +02:00
request.onupgradeneeded = function () {
2017-05-02 15:59:08 -05:00
var db = request.result;
db.createObjectStore(dbCacheTable, { keyPath: "address" });
2017-05-02 15:59:08 -05:00
};
2020-04-14 12:28:14 +02:00
request.onsuccess = function () {
2017-05-02 15:59:08 -05:00
var db = request.result;
resolve(db);
};
2020-04-14 12:28:14 +02:00
request.onerror = function () {
console.warn("getDb: " + request.error);
2017-05-02 15:59:08 -05:00
reject(request.error);
};
});
}
return dbPromise;
}
2021-04-09 10:34:15 -07:00
function storeCache(address, hashCode, translated, format, sourceMap) {
2020-04-14 12:28:14 +02:00
return getDb().then(function (db) {
var tx = db.transaction(dbCacheTable, "readwrite");
2017-05-02 15:59:08 -05:00
var store = tx.objectStore(dbCacheTable);
store.put({
address,
hashCode,
translated,
2017-05-02 15:59:08 -05:00
expires: Date.now() + cacheExpiration,
format,
2021-04-09 10:34:15 -07:00
sourceMap,
2017-05-02 15:59:08 -05:00
});
2020-04-14 12:28:14 +02:00
return new Promise(function (resolve, reject) {
tx.oncomplete = function () {
2017-05-02 15:59:08 -05:00
resolve();
};
2020-04-14 12:28:14 +02:00
tx.onerror = function () {
resolve();
};
2017-05-02 15:59:08 -05:00
});
});
}
function loadCache(address, hashCode) {
2020-04-14 12:28:14 +02:00
return getDb().then(function (db) {
var tx = db.transaction(dbCacheTable, "readonly");
2017-05-02 15:59:08 -05:00
var store = tx.objectStore(dbCacheTable);
var getAddress = store.get(address);
2020-04-14 12:28:14 +02:00
return new Promise(function (resolve, reject) {
tx.oncomplete = function () {
2017-05-02 15:59:08 -05:00
var found = getAddress.result;
var isValid =
found && found.hashCode === hashCode && Date.now() < found.expires;
resolve(
isValid
? {
translated: found.translated,
format: found.format,
2021-04-09 10:34:15 -07:00
sourceMap: found.sourceMap,
}
: null
);
2017-05-02 15:59:08 -05:00
};
2020-04-14 12:28:14 +02:00
tx.onerror = function () {
resolve(null);
};
2017-05-02 15:59:08 -05:00
});
});
}
var encoder = new TextEncoder("utf-8");
2017-05-02 15:59:08 -05:00
function sha256(str) {
var buffer = encoder.encode(str);
2020-04-14 12:28:14 +02:00
return crypto.subtle.digest("SHA-256", buffer).then(function (hash) {
2017-05-02 15:59:08 -05:00
var data = new Int32Array(hash);
return (
data[0].toString(36) +
"-" +
data[1].toString(36) +
"-" +
data[2].toString(36) +
"-" +
data[3].toString(36)
);
2017-05-02 15:59:08 -05:00
});
}
2020-04-14 12:28:14 +02:00
exports.translate = function (load, opt) {
var savedHashCode, babelTranslateError;
return sha256(load.source)
2020-04-14 12:28:14 +02:00
.then(function (hashCode) {
savedHashCode = hashCode;
return loadCache(load.address, hashCode);
})
.then(
2020-04-14 12:28:14 +02:00
function (cache) {
if (cache) {
load.metadata.format = cache.format;
return cache.translated;
}
return babel.translate.call(this, load, opt).then(
2020-04-14 12:28:14 +02:00
function (translated) {
return storeCache(
load.address,
savedHashCode,
translated,
2021-04-09 10:34:15 -07:00
load.metadata.format,
load.metadata.sourceMap
2020-04-14 12:28:14 +02:00
).then(function () {
return translated;
});
},
2020-04-14 12:28:14 +02:00
function (reason) {
throw (babelTranslateError = reason);
}
);
}.bind(this)
)
.catch(
2020-04-14 12:28:14 +02:00
function (reason) {
if (babelTranslateError) {
throw babelTranslateError;
}
return babel.translate.call(this, load, opt);
}.bind(this)
);
2017-05-02 15:59:08 -05:00
};