2023-08-25 00:15:49 +07:00
|
|
|
/*
|
|
|
|
* Copyright 2023 Fluence Labs Limited
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import fs from 'fs';
|
|
|
|
import path from 'path';
|
2023-09-29 16:23:06 +07:00
|
|
|
import module from 'module';
|
2023-08-25 00:15:49 +07:00
|
|
|
|
2023-09-29 16:23:06 +07:00
|
|
|
export async function fetchResource(pkg: string, assetPath: string) {
|
|
|
|
const require = module.createRequire(import.meta.url);
|
|
|
|
const packagePathIndex = require.resolve(pkg);
|
|
|
|
|
|
|
|
// Ensure that windows path is converted to posix path. So we can find a package
|
|
|
|
const posixPath = packagePathIndex.split(path.sep).join(path.posix.sep);
|
|
|
|
|
|
|
|
const matches = new RegExp(`(.+${pkg})`).exec(posixPath);
|
|
|
|
|
|
|
|
const packagePath = matches?.[0];
|
|
|
|
|
|
|
|
if (!packagePath) {
|
|
|
|
throw new Error(`Cannot find dependency ${pkg} in path ${posixPath}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
const pathToResource = path.join(packagePath, assetPath);
|
|
|
|
|
2023-08-25 00:15:49 +07:00
|
|
|
const file = await new Promise<ArrayBuffer>((resolve, reject) => {
|
|
|
|
// Cannot use 'fs/promises' with current vite config. This module is not polyfilled by default.
|
2023-09-29 16:23:06 +07:00
|
|
|
fs.readFile(pathToResource, (err, data) => {
|
2023-08-25 00:15:49 +07:00
|
|
|
if (err) {
|
|
|
|
reject(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
resolve(data);
|
|
|
|
});
|
2023-09-29 16:23:06 +07:00
|
|
|
});
|
|
|
|
|
2023-08-25 00:15:49 +07:00
|
|
|
return new Response(file, {
|
|
|
|
headers: {
|
|
|
|
'Content-type':
|
|
|
|
assetPath.endsWith('.wasm')
|
|
|
|
? 'application/wasm'
|
|
|
|
: assetPath.endsWith('.js')
|
|
|
|
? 'application/javascript'
|
|
|
|
: 'application/text'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|