f2e-server3
Version:
f2e-server 3.0
143 lines (142 loc) • 6.81 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createMemoryTree = exports.createStore = void 0;
const _ = __importStar(require("../utils/misc"));
const defaults_1 = require("./defaults");
const input_1 = require("./input");
const output_1 = require("./output");
const node_path_1 = __importDefault(require("node:path"));
const node_crypto_1 = require("node:crypto");
const logger_1 = __importDefault(require("../utils/logger"));
const types_1 = require("node:util/types");
__exportStar(require("./interface"), exports);
__exportStar(require("./defaults"), exports);
const inEntries = (entries = [], pathname) => {
return entries.find(item => new RegExp(item).test(pathname));
};
const createStore = function (options) {
const { onGet, namehash, watch } = options;
let o = {};
const origin_map = new Map();
const output_map = new Map();
const store = {
ignores: new Set(),
origin_map,
output_map,
_get(pathname) {
const arr = _.pathname_arr(pathname);
return arr.length ? _.get(o, arr) : o;
},
async load(_pathname) {
const pathname = _.pathname_fixer(_pathname);
let result = await onGet(pathname, store._get(pathname), store);
if (result && namehash && namehash.entries && namehash.searchValue) {
if (inEntries(namehash.entries, pathname)) {
const searchValues = namehash.searchValue.map(t => new RegExp(t, 'g'));
result = result.toString();
for (let i = 0; i < searchValues.length; i++) {
const searchValue = searchValues[i];
const replacer = (mat, src) => {
const key = /^https?:\/\//.test(src) ? src : (_.pathname_fixer('/' === src.charAt(0) ? src : node_path_1.default.join(node_path_1.default.dirname(pathname), src)));
const out = origin_map.get(key);
if (!out)
return mat;
const targetSrc = (namehash.publicPath || '/') + out.outputPath.replace(/^\/+/, '');
return mat.replace(src, targetSrc + (watch ? `" data-origin="${key}" data-hash="${out.hash}"` : ''));
};
result = result.replace(searchValue, replacer);
}
}
}
return result;
},
save(result) {
// outputPath 需要携带根路径 /
if (namehash && ((0, types_1.isArrayBufferView)(result.data) || typeof result.data === 'string')) {
const hash = (0, node_crypto_1.createHash)('md5').update(result.data).digest('hex').slice(0, 8);
result.hash = hash;
if (namehash.replacer) {
if (!inEntries(namehash?.entries, result.originPath)) {
result.outputPath = namehash.replacer(_.pathname_fixer(result.outputPath || result.originPath), hash) || result.outputPath;
}
else if (namehash.searchValue && watch) {
const searchValues = namehash.searchValue.map(t => new RegExp(t, 'g'));
const context = result.data.toString();
const deps = [];
for (let i = 0; i < searchValues.length; i++) {
const searchValue = searchValues[i];
context.replace(searchValue, (mat, src) => {
const key = /^https?:\/\//.test(src) ? src : (_.pathname_fixer('/' === src.charAt(0) ? src : node_path_1.default.join(node_path_1.default.dirname(result.originPath), src)));
deps.push(key);
return mat;
});
}
result.deps = deps;
}
}
}
result.updateTime = +new Date();
if (result.error) {
logger_1.default.error(`${result.originPath} -> ${result.outputPath}`, result.error);
origin_map.set(result.originPath, result);
return;
}
origin_map.set(result.originPath, result);
if (result.originPath) {
output_map.set(_.pathname_fixer(result.outputPath), result.originPath);
_.set(o, _.pathname_fixer(result.outputPath), result.data);
}
if (!_.isPlainObject(result.data)) {
logger_1.default.debug(`save ${result.originPath} -> ${result.outputPath}`);
}
},
};
return store;
};
exports.createStore = createStore;
const createMemoryTree = (options) => {
const _options = {
...defaults_1.defaultOptions,
...(options || {}),
};
const { onGet, namehash, watch } = _options;
const store = (0, exports.createStore)({ onGet, namehash, watch });
const build = (0, input_1.inputProvider)(_options, store);
return {
store,
input: build,
output: (0, output_1.outputProvider)(_options, store),
watch: (0, input_1.beginWatch)(_options, store, build),
};
};
exports.createMemoryTree = createMemoryTree;
exports.default = exports.createMemoryTree;