From 30f7bb5dc8dc72e056f14242c32f8e39b0b292da Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Wed, 25 Sep 2024 11:50:01 -0400 Subject: [PATCH] merge isAsciiWhitespace implementations --- src/workerd/api/BUILD.bazel | 53 ++++++++++++++++++++++++++++ src/workerd/api/analytics-engine.h | 1 + src/workerd/api/data-url.c++ | 14 ++------ src/workerd/api/encoding.c++ | 16 ++++++--- src/workerd/api/encoding.h | 2 ++ src/workerd/api/form-data.c++ | 2 +- src/workerd/api/gpu/gpu.c++ | 2 -- src/workerd/api/node/async-hooks.c++ | 2 -- src/workerd/api/streams/encoding.h | 3 +- src/workerd/api/util-iocontext.c++ | 20 +++++++++++ src/workerd/api/util-iocontext.h | 12 +++++++ src/workerd/api/util.c++ | 11 ------ src/workerd/api/util.h | 5 --- src/workerd/io/BUILD.bazel | 9 ++++- src/workerd/io/io-channels.h | 2 +- src/workerd/server/BUILD.bazel | 3 ++ 16 files changed, 117 insertions(+), 40 deletions(-) create mode 100644 src/workerd/api/util-iocontext.c++ create mode 100644 src/workerd/api/util-iocontext.h diff --git a/src/workerd/api/BUILD.bazel b/src/workerd/api/BUILD.bazel index 7713e536263b..3a1cc3a478bb 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,10 +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"], + implementation_deps = ["//src/workerd/io"], + visibility = ["//visibility:public"], +) + +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"], + implementation_deps = [ + ":encoding", + ], visibility = ["//visibility:public"], deps = [ "//src/workerd/jsg:url", @@ -159,6 +205,8 @@ wd_cc_capnp_library( kj_test( src = f, deps = [ + ":encoding", + ":util-iocontext", "//src/workerd/io", ], ) @@ -177,6 +225,7 @@ kj_test( src = "data-url-test.c++", deps = [ ":data-url", + ":encoding", ], ) @@ -190,6 +239,7 @@ kj_test( kj_test( src = "streams/internal-test.c++", deps = [ + ":util-iocontext", "//src/workerd/io", "//src/workerd/tests:test-fixture", ], @@ -198,6 +248,7 @@ kj_test( kj_test( src = "actor-state-iocontext-test.c++", deps = [ + ":util-iocontext", "//src/workerd/io", "//src/workerd/tests:test-fixture", ], @@ -206,7 +257,9 @@ kj_test( kj_test( src = "api-rtti-test.c++", deps = [ + ":encoding", ":html-rewriter", + ":util-iocontext", "//src/workerd/io", "//src/workerd/jsg:rtti", ], diff --git a/src/workerd/api/analytics-engine.h b/src/workerd/api/analytics-engine.h index 5b0a7a3187cd..640df7b165e4 100644 --- a/src/workerd/api/analytics-engine.h +++ b/src/workerd/api/analytics-engine.h @@ -6,6 +6,7 @@ #include #include +#include #include #include diff --git a/src/workerd/api/data-url.c++ b/src/workerd/api/data-url.c++ index f2f19f30d181..61808802b0a6 100644 --- a/src/workerd/api/data-url.c++ +++ b/src/workerd/api/data-url.c++ @@ -1,5 +1,7 @@ #include "data-url.h" +#include + #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..85e39025ef93 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; +}(); + +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..324a2c5057b0 100644 --- a/src/workerd/api/encoding.h +++ b/src/workerd/api/encoding.h @@ -11,6 +11,8 @@ namespace workerd::api { +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/node/async-hooks.c++ b/src/workerd/api/node/async-hooks.c++ index 6cdd82d588b7..92e55fb2d27d 100644 --- a/src/workerd/api/node/async-hooks.c++ +++ b/src/workerd/api/node/async-hooks.c++ @@ -3,8 +3,6 @@ // https://opensource.org/licenses/Apache-2.0 #include "async-hooks.h" -#include - namespace workerd::api::node { jsg::Ref AsyncLocalStorage::constructor(jsg::Lock& js) { diff --git a/src/workerd/api/streams/encoding.h b/src/workerd/api/streams/encoding.h index 2472803c414b..5b3868d2b742 100644 --- a/src/workerd/api/streams/encoding.h +++ b/src/workerd/api/streams/encoding.h @@ -4,11 +4,12 @@ #pragma once -#include "../encoding.h" #include "readable.h" #include "transform.h" #include "writable.h" +#include + namespace workerd::api { class TextEncoderStream: public TransformStream { 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/BUILD.bazel b/src/workerd/io/BUILD.bazel index 07f9b69721a0..dbcfce25e8fa 100644 --- a/src/workerd/io/BUILD.bazel +++ b/src/workerd/io/BUILD.bazel @@ -89,6 +89,7 @@ wd_cc_library( "//src/workerd/api:analytics-engine_capnp", "//src/workerd/api:data-url", "//src/workerd/api:deferred-proxy", + "//src/workerd/api:encoding", "//src/workerd/api:r2-api_capnp", "//src/workerd/jsg", "//src/workerd/util:autogate", @@ -332,13 +333,19 @@ kj_test( kj_test( src = "promise-wrapper-test.c++", - deps = [":io"], + deps = [ + ":io", + "//src/workerd/api:encoding", + "//src/workerd/api:util-iocontext", + ], ) kj_test( src = "compatibility-date-test.c++", deps = [ ":io", + "//src/workerd/api:encoding", + "//src/workerd/api:util-iocontext", "@capnp-cpp//src/capnp:capnpc", ], ) 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 diff --git a/src/workerd/server/BUILD.bazel b/src/workerd/server/BUILD.bazel index 7d19fb27540b..1be2e396f345 100644 --- a/src/workerd/server/BUILD.bazel +++ b/src/workerd/server/BUILD.bazel @@ -112,6 +112,9 @@ wd_cc_library( "//src/workerd/io:set_enable_experimental_webgpu": ["WORKERD_EXPERIMENTAL_ENABLE_WEBGPU"], "//conditions:default": [], }), + implementation_deps = [ + "//src/workerd/api:util-iocontext", + ], visibility = ["//visibility:public"], deps = [ ":actor-id-impl",