diff --git a/src/workerd/api/BUILD.bazel b/src/workerd/api/BUILD.bazel index 7713e536263b..3197914ab805 100644 --- a/src/workerd/api/BUILD.bazel +++ b/src/workerd/api/BUILD.bazel @@ -12,6 +12,9 @@ filegroup( exclude = [ "html-rewriter.c++", "data-url.c++", + "encoding.c++", + "util.c++", + "util-iocontext.c++", "rtti.c++", "**/*test*.c++", "pyodide.c++", @@ -23,6 +26,9 @@ filegroup( exclude = [ "html-rewriter.c++", "data-url.c++", + "encoding.c++", + "util.c++", + "util-iocontext.c++", "rtti.c++", "**/*test*.c++", "pyodide.c++", @@ -38,6 +44,8 @@ api_header_exclusions = [ "html-rewriter.h", "deferred-proxy.h", "data-url.h", + "encoding.h", + "util.h", "modules.h", "rtti.h", "**/*test*.h", @@ -121,12 +129,48 @@ kj_test( ], ) +wd_cc_library( + name = "util", + srcs = ["util.c++"], + hdrs = ["util.h"], + visibility = ["//visibility:public"], + deps = [ + "//src/workerd/jsg", + "//src/workerd/util", + "@capnp-cpp//src/kj", + "@simdutf", + ], +) + +wd_cc_library( + name = "util-iocontext", + srcs = ["util-iocontext.c++"], + hdrs = ["util-iocontext.h"], + visibility = ["//visibility:public"], + deps = ["//src/workerd/io"], +) + +wd_cc_library( + name = "encoding", + srcs = ["encoding.c++"], + hdrs = ["encoding.h"], + visibility = ["//visibility:public"], + deps = [ + ":util", + "//src/workerd/io:compatibility_date_capnp", + "//src/workerd/jsg", + "//src/workerd/util", + "@capnp-cpp//src/kj", + ], +) + wd_cc_library( name = "data-url", srcs = ["data-url.c++"], hdrs = ["data-url.h"], visibility = ["//visibility:public"], deps = [ + ":encoding", "//src/workerd/jsg:url", "//src/workerd/util", "@capnp-cpp//src/kj", @@ -177,6 +221,7 @@ kj_test( src = "data-url-test.c++", deps = [ ":data-url", + ":encoding", ], ) diff --git a/src/workerd/api/analytics-engine.h b/src/workerd/api/analytics-engine.h index 5b0a7a3187cd..4176f3476c09 100644 --- a/src/workerd/api/analytics-engine.h +++ b/src/workerd/api/analytics-engine.h @@ -4,6 +4,8 @@ #pragma once +#include "util-iocontext.h" + #include #include #include diff --git a/src/workerd/api/data-url.c++ b/src/workerd/api/data-url.c++ index f2f19f30d181..0611d344870a 100644 --- a/src/workerd/api/data-url.c++ +++ b/src/workerd/api/data-url.c++ @@ -1,5 +1,7 @@ #include "data-url.h" +#include "encoding.h" + #include namespace workerd::api { @@ -11,18 +13,6 @@ kj::Maybe DataUrl::tryParse(kj::StringPtr url) { return kj::none; } -static constexpr kj::FixedArray ascii_whitespace_table = []() consteval { - kj::FixedArray result{}; - for (uint8_t c: {0x09, 0x0a, 0x0c, 0x0d, 0x20}) { - result[c] = true; - } - return result; -}(); - -constexpr bool isAsciiWhitespace(uint8_t c) noexcept { - return ascii_whitespace_table[c]; -} - kj::Maybe DataUrl::from(const jsg::Url& url) { if (url.getProtocol() != "data:"_kj) return kj::none; auto clone = url.clone(jsg::Url::EquivalenceOption::IGNORE_FRAGMENTS); diff --git a/src/workerd/api/encoding.c++ b/src/workerd/api/encoding.c++ index fe96f402c222..f334fe136fbf 100644 --- a/src/workerd/api/encoding.c++ +++ b/src/workerd/api/encoding.c++ @@ -15,6 +15,18 @@ namespace workerd::api { +constexpr kj::FixedArray ascii_whitespace_table = []() consteval { + kj::FixedArray result{}; + for (uint8_t c: {0x09, 0x0a, 0x0c, 0x0d, 0x20}) { + result[c] = true; + } + return result; +}(); + +constexpr bool isAsciiWhitespace(uint8_t c) noexcept { + return ascii_whitespace_table[c]; +} + // ======================================================================================= // TextDecoder implementation @@ -265,10 +277,6 @@ kj::StringPtr getEncodingId(Encoding encoding) { Encoding getEncodingForLabel(kj::StringPtr label) { kj::String labelInsensitive = toLower(label); const auto trim = [](kj::StringPtr label) { - const auto isAsciiWhitespace = [](auto c) { - return c == 0x09 /* tab */ || c == 0x0a /* lf */ || c == 0x0c /* ff */ || - c == 0x0d /* cr */ || c == 0x20 /* sp */; - }; size_t start = 0; auto end = label.size(); while (start < end && isAsciiWhitespace(label[start])) { diff --git a/src/workerd/api/encoding.h b/src/workerd/api/encoding.h index 1dc24be22576..71a39d39fbdb 100644 --- a/src/workerd/api/encoding.h +++ b/src/workerd/api/encoding.h @@ -11,6 +11,8 @@ namespace workerd::api { +constexpr bool isAsciiWhitespace(uint8_t c) noexcept; + // The encodings listed here are defined as required by the Encoding spec. // The first label is enum we use to identify the encoding in code, while // the second label is the public identifier. diff --git a/src/workerd/api/form-data.c++ b/src/workerd/api/form-data.c++ index c6a48c12ccf5..06911579736d 100644 --- a/src/workerd/api/form-data.c++ +++ b/src/workerd/api/form-data.c++ @@ -4,12 +4,12 @@ #include "form-data.h" +#include "util-iocontext.h" #include "util.h" #include #include -#include #include #include diff --git a/src/workerd/api/gpu/gpu.c++ b/src/workerd/api/gpu/gpu.c++ index 6b1b4199eba5..21eed9fa25e9 100644 --- a/src/workerd/api/gpu/gpu.c++ +++ b/src/workerd/api/gpu/gpu.c++ @@ -4,8 +4,6 @@ #include "gpu.h" -#include - #include namespace workerd::api::gpu { diff --git a/src/workerd/api/util-iocontext.c++ b/src/workerd/api/util-iocontext.c++ new file mode 100644 index 000000000000..fae34fee6466 --- /dev/null +++ b/src/workerd/api/util-iocontext.c++ @@ -0,0 +1,20 @@ +// Copyright (c) 2017-2022 Cloudflare, Inc. +// Licensed under the Apache 2.0 license found in the LICENSE file or at: +// https://opensource.org/licenses/Apache-2.0 + +#include "util-iocontext.h" + +#include + +namespace workerd::api { + +double dateNow() { + if (IoContext::hasCurrent()) { + auto& ioContext = IoContext::current(); + return (ioContext.now() - kj::UNIX_EPOCH) / kj::MILLISECONDS; + } + + return 0.0; +} + +} // namespace workerd::api diff --git a/src/workerd/api/util-iocontext.h b/src/workerd/api/util-iocontext.h new file mode 100644 index 000000000000..4c2c4c9132f7 --- /dev/null +++ b/src/workerd/api/util-iocontext.h @@ -0,0 +1,12 @@ +// Copyright (c) 2017-2022 Cloudflare, Inc. +// Licensed under the Apache 2.0 license found in the LICENSE file or at: +// https://opensource.org/licenses/Apache-2.0 + +#pragma once + +namespace workerd::api { + +// Returns exactly what Date.now() would return. +double dateNow(); + +} // namespace workerd::api diff --git a/src/workerd/api/util.c++ b/src/workerd/api/util.c++ index 65a53deef6c9..03e1b16092a4 100644 --- a/src/workerd/api/util.c++ +++ b/src/workerd/api/util.c++ @@ -6,9 +6,7 @@ #include "simdutf.h" -#include #include -#include #include @@ -259,15 +257,6 @@ kj::String redactUrl(kj::StringPtr url) { return kj::String(redacted.releaseAsArray()); } -double dateNow() { - if (IoContext::hasCurrent()) { - auto& ioContext = IoContext::current(); - return (ioContext.now() - kj::UNIX_EPOCH) / kj::MILLISECONDS; - } - - return 0.0; -} - kj::Maybe> cloneRequestCf( jsg::Lock& js, kj::Maybe> maybeCf) { KJ_IF_SOME(cf, maybeCf) { diff --git a/src/workerd/api/util.h b/src/workerd/api/util.h index 089f67d74967..09edc77812ef 100644 --- a/src/workerd/api/util.h +++ b/src/workerd/api/util.h @@ -95,11 +95,6 @@ kj::String redactUrl(kj::StringPtr url); // ======================================================================================= -// Returns exactly what Date.now() would return. -double dateNow(); - -// ======================================================================================= - void maybeWarnIfNotText(jsg::Lock& js, kj::StringPtr str); kj::String fastEncodeBase64Url(kj::ArrayPtr bytes); diff --git a/src/workerd/io/io-channels.h b/src/workerd/io/io-channels.h index fc1df935fa58..56d38e14b6f8 100644 --- a/src/workerd/io/io-channels.h +++ b/src/workerd/io/io-channels.h @@ -4,7 +4,7 @@ #pragma once -#include +#include #include #include