File
Blob: src/workerd/io/bundle-fs.c++
| 1 | #include "bundle-fs.h" |
| 2 | |
| 3 | namespace workerd { |
| 4 | kj::Rc<Directory> getBundleDirectory(const WorkerSource& conf) { |
| 5 | // Note that we are using a lazy directory here. That means we won't actually |
| 6 | // build the directory structure out until it is actually accessed in order |
| 7 | // to avoid unnecessary operations in the case a worker never actually uses |
| 8 | // this part of the filesystem. |
| 9 | |
| 10 | // Importantly, the WorkerSource we get here won't be sticking around. |
| 11 | // We need to copy the details we need out of it now...Critically, however, |
| 12 | // the caller needs to arrange to keep the original source alive for the |
| 13 | // lifetime of the directory since the directory only contains pointers. |
| 14 | struct Entry { |
| 15 | kj::StringPtr name; |
| 16 | kj::ArrayPtr<const kj::byte> data; |
| 17 | }; |
| 18 | kj::Vector<Entry> entries; |
| 19 | KJ_SWITCH_ONEOF(conf.variant) { |
| 20 | KJ_CASE_ONEOF(script, WorkerSource::ScriptSource) { |
| 21 | entries.add(Entry{ |
| 22 | .name = script.mainScriptName, |
| 23 | .data = script.mainScript.asBytes(), |
| 24 | }); |
| 25 | } |
| 26 | KJ_CASE_ONEOF(modules, WorkerSource::ModulesSource) { |
| 27 | for (auto& module: modules.modules) { |
| 28 | KJ_SWITCH_ONEOF(module.content) { |
| 29 | KJ_CASE_ONEOF(esModule, WorkerSource::EsModule) { |
| 30 | entries.add(Entry{ |
| 31 | .name = module.name, |
| 32 | .data = esModule.body.asBytes(), |
| 33 | }); |
| 34 | } |
| 35 | KJ_CASE_ONEOF(commonJsModule, WorkerSource::CommonJsModule) { |
| 36 | entries.add(Entry{ |
| 37 | .name = module.name, |
| 38 | .data = commonJsModule.body.asBytes(), |
| 39 | }); |
| 40 | } |
| 41 | KJ_CASE_ONEOF(textModule, WorkerSource::TextModule) { |
| 42 | entries.add(Entry{ |
| 43 | .name = module.name, |
| 44 | .data = textModule.body.asBytes(), |
| 45 | }); |
| 46 | } |
| 47 | KJ_CASE_ONEOF(dataModule, WorkerSource::DataModule) { |
| 48 | entries.add(Entry{ |
| 49 | .name = module.name, |
| 50 | .data = dataModule.body, |
| 51 | }); |
| 52 | } |
| 53 | KJ_CASE_ONEOF(wasmModule, WorkerSource::WasmModule) { |
| 54 | entries.add(Entry{ |
| 55 | .name = module.name, |
| 56 | .data = wasmModule.body, |
| 57 | }); |
| 58 | } |
| 59 | KJ_CASE_ONEOF(jsonModule, WorkerSource::JsonModule) { |
| 60 | entries.add(Entry{ |
| 61 | .name = module.name, |
| 62 | .data = jsonModule.body.asBytes(), |
| 63 | }); |
| 64 | } |
| 65 | KJ_CASE_ONEOF(pythonModule, WorkerSource::PythonModule) { |
| 66 | entries.add(Entry{ |
| 67 | .name = module.name, |
| 68 | .data = pythonModule.body.asBytes(), |
| 69 | }); |
| 70 | } |
| 71 | KJ_CASE_ONEOF(pythonRequirement, WorkerSource::PythonRequirement) { |
| 72 | // Just ignore it. |
| 73 | } |
| 74 | KJ_CASE_ONEOF(capnpModule, WorkerSource::CapnpModule) { |
| 75 | // Capnp modules are not supported in the bundle. |
| 76 | // Just ignore it. |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | return getLazyDirectoryImpl([entries = entries.releaseAsArray()] { |
| 84 | Directory::Builder builder; |
| 85 | kj::Path kRoot{}; |
| 86 | for (auto& entry: entries) { |
| 87 | auto url = KJ_ASSERT_NONNULL(jsg::Url::tryParse(entry.name, "file:///"_kj)); |
| 88 | // If the name is not a valid file URL path, ignore it. |
| 89 | if (url.getProtocol() != "file:"_kj) { |
| 90 | continue; |
| 91 | } |
| 92 | auto pathStr = kj::str(url.getPathname().slice(1)); |
| 93 | auto path = kRoot.eval(pathStr); |
| 94 | builder.addPath(path, File::newReadable(entry.data)); |
| 95 | } |
| 96 | return builder.finish(); |
| 97 | }); |
| 98 | } |
| 99 | |
| 100 | } // namespace workerd |