Path: blob/main/system/lib/wasmfs/backends/fetch_backend.cpp
6175 views
// Copyright 2021 The Emscripten Authors. All rights reserved.1// Emscripten is available under two separate licenses, the MIT license and the2// University of Illinois/NCSA Open Source License. Both these licenses can be3// found in the LICENSE file.45// This file defines the fetch backend.67#include "fetch_backend.h"8#include "backend.h"9#include "proxied_async_js_impl_backend.h"10#include "wasmfs.h"1112namespace wasmfs {1314const uint32_t DEFAULT_CHUNK_SIZE = 16*1024*1024;1516class FetchBackend : public wasmfs::ProxiedAsyncJSBackend {17std::string baseUrl;18uint32_t chunkSize;19public:20FetchBackend(const std::string& baseUrl,21uint32_t chunkSize,22std::function<void(backend_t)> setupOnThread)23: ProxiedAsyncJSBackend(setupOnThread), baseUrl(baseUrl), chunkSize(chunkSize) {}24std::shared_ptr<DataFile> createFile(mode_t mode) override;25std::shared_ptr<Directory> createDirectory(mode_t mode) override;26const std::string getFileURL(const std::string& filePath);27uint32_t getChunkSize();28};293031class FetchFile : public ProxiedAsyncJSImplFile {32std::string filePath;33std::string fileUrl;3435public:36FetchFile(const std::string& path,37mode_t mode,38backend_t backend,39emscripten::ProxyWorker& proxy)40: ProxiedAsyncJSImplFile(mode, backend, proxy), filePath(path) {41this->fileUrl = dynamic_cast<FetchBackend*>(getBackend())->getFileURL(filePath);42}4344const std::string& getPath() const { return filePath; }45const std::string& getURL() const { return fileUrl; }46const uint32_t getChunkSize() const { return dynamic_cast<FetchBackend*>(getBackend())->getChunkSize(); }47};4849class FetchDirectory : public MemoryDirectory {50std::string dirPath;51emscripten::ProxyWorker& proxy;5253public:54FetchDirectory(const std::string& path,55mode_t mode,56backend_t backend,57emscripten::ProxyWorker& proxy)58: MemoryDirectory(mode, backend), dirPath(path), proxy(proxy) {}5960std::shared_ptr<DataFile> insertDataFile(const std::string& name,61mode_t mode) override {62auto childPath = getChildPath(name);63auto child =64std::make_shared<FetchFile>(childPath, mode, getBackend(), proxy);65insertChild(name, child);66return child;67}6869std::shared_ptr<Directory> insertDirectory(const std::string& name,70mode_t mode) override {71auto childPath = getChildPath(name);72auto childDir =73std::make_shared<FetchDirectory>(childPath, mode, getBackend(), proxy);74insertChild(name, childDir);75return childDir;76}7778std::string getChildPath(const std::string& name) const {79return dirPath + '/' + name;80}8182std::shared_ptr<File> getChild(const std::string& name) override {83return MemoryDirectory::getChild(name);84}85};8687std::shared_ptr<DataFile> FetchBackend::createFile(mode_t mode) {88return std::make_shared<FetchFile>("", mode, this, proxy);89}9091std::shared_ptr<Directory> FetchBackend::createDirectory(mode_t mode) {92return std::make_shared<FetchDirectory>("", mode, this, proxy);93}9495const std::string FetchBackend::getFileURL(const std::string& filePath) {96if (filePath == "") {97return baseUrl;98}99return baseUrl + "/" + filePath;100}101102uint32_t FetchBackend::getChunkSize() {103return chunkSize;104}105106extern "C" {107backend_t wasmfs_create_fetch_backend(const char* base_url, uint32_t chunkSize) {108// ProxyWorker cannot safely be synchronously spawned from the main browser109// thread. See comment in thread_utils.h for more details.110assert(!emscripten_is_main_browser_thread() &&111"Cannot safely create fetch backend on main browser thread");112return wasmFS.addBackend(std::make_unique<FetchBackend>(113base_url ? base_url : "",114chunkSize ? chunkSize : DEFAULT_CHUNK_SIZE,115[](backend_t backend) { _wasmfs_create_fetch_backend_js(backend); }));116}117118const char* _wasmfs_fetch_get_file_url(void* ptr) {119auto* file = reinterpret_cast<wasmfs::FetchFile*>(ptr);120return file ? file->getURL().data() : nullptr;121}122123uint32_t _wasmfs_fetch_get_chunk_size(void* ptr) {124auto* file = reinterpret_cast<wasmfs::FetchFile*>(ptr);125return file ? file->getChunkSize() : DEFAULT_CHUNK_SIZE;126}127128}129130} // namespace wasmfs131132133