DuyTa's picture
Upload folder using huggingface_hub
bc20498 verified
raw
history blame
2.55 kB
'use strict';
const fetch = (m => /* c8 ignore start */ m.__esModule ? m.default : m /* c8 ignore stop */)(require('@webreflection/fetch'));
const { interpreter } = require('./interpreters.js');
const { absoluteURL, resolve } = require('./utils.js');
const { parse } = require('./toml.js');
// REQUIRES INTEGRATION TEST
/* c8 ignore start */
const getConfigURLAndType = (config, configURL = './config.txt') => {
let type = typeof config;
if (type === 'string' && /\.(json|toml|txt)$/.test(config))
type = RegExp.$1;
else
config = configURL;
return [absoluteURL(config), type];
};
exports.getConfigURLAndType = getConfigURLAndType;
const parseString = config => {
try {
return JSON.parse(config);
}
// eslint-disable-next-line no-unused-vars
catch (_) {
return parse(config);
}
};
/* c8 ignore stop */
/**
* Parse a generic config if it came from an attribute either as URL
* or as a serialized string. In XWorker case, accepts a pre-defined
* options to use as it is to avoid needing at all a fetch operation.
* In latter case, config will be suffixed as `config.txt`.
* @param {string} id the interpreter name @ version identifier
* @param {string | object} config optional config file to parse
* @param {string} [configURL] optional config URL if config is not string
* @param {object} [options] optional options used to bootstrap XWorker
* @returns
*/
const getRuntime = (id, config, configURL, options = {}) => {
if (config) {
// REQUIRES INTEGRATION TEST
/* c8 ignore start */
const [absolute, type] = getConfigURLAndType(config, configURL);
if (type === 'json') {
options = fetch(absolute).json();
} else if (type === 'toml') {
options = fetch(absolute).text().then(parse);
} else if (type === 'string') {
options = parseString(config);
} else if (type === 'object' && config) {
options = config;
} else if (type === 'txt' && typeof options === 'string') {
options = parseString(options);
}
config = absolute;
/* c8 ignore stop */
}
return resolve(options).then(options => interpreter[id](options, config));
};
exports.getRuntime = getRuntime;
/**
* @param {string} type the interpreter type
* @param {string} [version] the optional interpreter version
* @returns
*/
const getRuntimeID = (type, version = '') =>
`${type}@${version}`.replace(/@$/, '');
exports.getRuntimeID = getRuntimeID;