From 413d5a5269b5c598d19b76a99222adb2314ce7d1 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 | 37 ++++++++++++++++++++++++++++ src/workerd/api/analytics-engine.h | 1 + src/workerd/api/data-url.c++ | 14 ++--------- src/workerd/api/encoding.c++ | 4 --- src/workerd/api/encoding.h | 12 +++++++++ src/workerd/api/form-data.c++ | 2 +- src/workerd/api/gpu/gpu.c++ | 2 -- src/workerd/api/memory-cache.c++ | 3 ++- src/workerd/api/node/async-hooks.c++ | 2 -- src/workerd/api/streams/encoding.h | 3 ++- src/workerd/api/util.c++ | 11 --------- src/workerd/api/util.h | 5 ---- src/workerd/io/BUILD.bazel | 3 +++ src/workerd/io/io-channels.h | 4 +-- src/workerd/io/io-util.c++ | 22 +++++++++++++++++ src/workerd/io/io-util.h | 12 +++++++++ src/workerd/jsg/BUILD.bazel | 2 +- 17 files changed, 97 insertions(+), 42 deletions(-) create mode 100644 src/workerd/io/io-util.c++ create mode 100644 src/workerd/io/io-util.h diff --git a/src/workerd/api/BUILD.bazel b/src/workerd/api/BUILD.bazel index 7713e536263..b840091c1f4 100644 --- a/src/workerd/api/BUILD.bazel +++ b/src/workerd/api/BUILD.bazel @@ -12,6 +12,8 @@ filegroup( exclude = [ "html-rewriter.c++", "data-url.c++", + "encoding.c++", + "util.c++", "rtti.c++", "**/*test*.c++", "pyodide.c++", @@ -23,6 +25,8 @@ filegroup( exclude = [ "html-rewriter.c++", "data-url.c++", + "encoding.c++", + "util.c++", "rtti.c++", "**/*test*.c++", "pyodide.c++", @@ -38,6 +42,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 +127,40 @@ kj_test( ], ) +wd_cc_library( + name = "util", + srcs = ["util.c++"], + hdrs = ["util.h"], + implementation_deps = ["@simdutf"], + visibility = ["//visibility:public"], + deps = [ + "//src/workerd/jsg", + "//src/workerd/util", + "@capnp-cpp//src/kj", + ], +) + +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", @@ -206,6 +242,7 @@ kj_test( kj_test( src = "api-rtti-test.c++", deps = [ + ":encoding", ":html-rewriter", "//src/workerd/io", "//src/workerd/jsg:rtti", diff --git a/src/workerd/api/analytics-engine.h b/src/workerd/api/analytics-engine.h index 5b0a7a3187c..e5d25ed66f4 100644 --- a/src/workerd/api/analytics-engine.h +++ b/src/workerd/api/analytics-engine.h @@ -7,6 +7,7 @@ #include #include #include +#include #include namespace workerd::api { diff --git a/src/workerd/api/data-url.c++ b/src/workerd/api/data-url.c++ index f2f19f30d18..61808802b0a 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 fe96f402c22..7186ad837a1 100644 --- a/src/workerd/api/encoding.c++ +++ b/src/workerd/api/encoding.c++ @@ -265,10 +265,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 1dc24be2257..420ec1c7b53 100644 --- a/src/workerd/api/encoding.h +++ b/src/workerd/api/encoding.h @@ -11,6 +11,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]; +} + // 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 c6a48c12ccf..85e11ea7e79 100644 --- a/src/workerd/api/form-data.c++ +++ b/src/workerd/api/form-data.c++ @@ -6,10 +6,10 @@ #include "util.h" +#include #include #include -#include #include #include diff --git a/src/workerd/api/gpu/gpu.c++ b/src/workerd/api/gpu/gpu.c++ index 6b1b4199eba..21eed9fa25e 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/memory-cache.c++ b/src/workerd/api/memory-cache.c++ index 21d2c8e392f..a27d716ed6b 100644 --- a/src/workerd/api/memory-cache.c++ +++ b/src/workerd/api/memory-cache.c++ @@ -2,6 +2,7 @@ #include #include +#include #include #include #include @@ -29,7 +30,7 @@ static bool hasExpired(const kj::Maybe& expiration, bool allowOutsideIoC KJ_IF_SOME(e, expiration) { double now = (allowOutsideIoContext && !IoContext::hasCurrent()) ? getCurrentTimeOutsideIoContext() - : api::dateNow(); + : dateNow(); return e < now; } return false; diff --git a/src/workerd/api/node/async-hooks.c++ b/src/workerd/api/node/async-hooks.c++ index 6cdd82d588b..92e55fb2d27 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 2472803c414..5b3868d2b74 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.c++ b/src/workerd/api/util.c++ index 65a53deef6c..03e1b16092a 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 089f67d7496..09edc77812e 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 07f9b69721a..975d14c89e9 100644 --- a/src/workerd/io/BUILD.bazel +++ b/src/workerd/io/BUILD.bazel @@ -49,6 +49,7 @@ wd_cc_library( "hibernation-manager.c++", "io-context.c++", "io-own.c++", + "io-util.c++", "worker.c++", ] + ["//src/workerd/api:srcs"], hdrs = [ @@ -57,6 +58,7 @@ wd_cc_library( "hibernation-manager.h", "io-context.h", "io-own.h", + "io-util.h", "promise-wrapper.h", "worker.h", ] + ["//src/workerd/api:hdrs"], @@ -89,6 +91,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", diff --git a/src/workerd/io/io-channels.h b/src/workerd/io/io-channels.h index fc1df935fa5..1430e493425 100644 --- a/src/workerd/io/io-channels.h +++ b/src/workerd/io/io-channels.h @@ -4,8 +4,8 @@ #pragma once -#include #include +#include #include #include @@ -101,7 +101,7 @@ class IoChannelFactory { kj::Maybe featureFlagsForFl; // Timestamp for when a subrequest is started. (ms since the Unix Epoch) - double startTime = api::dateNow(); + double startTime = dateNow(); }; virtual kj::Own startSubrequest(uint channel, SubrequestMetadata metadata) = 0; diff --git a/src/workerd/io/io-util.c++ b/src/workerd/io/io-util.c++ new file mode 100644 index 00000000000..6bc8930da38 --- /dev/null +++ b/src/workerd/io/io-util.c++ @@ -0,0 +1,22 @@ +// 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 "io-util.h" + +#include "io-context.h" + +#include + +namespace workerd { + +double dateNow() { + if (IoContext::hasCurrent()) { + auto& ioContext = IoContext::current(); + return (ioContext.now() - kj::UNIX_EPOCH) / kj::MILLISECONDS; + } + + return 0.0; +} + +} // namespace workerd diff --git a/src/workerd/io/io-util.h b/src/workerd/io/io-util.h new file mode 100644 index 00000000000..6d0bca617ec --- /dev/null +++ b/src/workerd/io/io-util.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 { + +// Returns exactly what Date.now() would return. +double dateNow(); + +} // namespace workerd diff --git a/src/workerd/jsg/BUILD.bazel b/src/workerd/jsg/BUILD.bazel index b84b00b48b7..8cce150449b 100644 --- a/src/workerd/jsg/BUILD.bazel +++ b/src/workerd/jsg/BUILD.bazel @@ -52,6 +52,7 @@ wd_cc_library( "web-idl.h", "wrappable.h", ], + implementation_deps = ["@simdutf"], visibility = ["//visibility:public"], deps = [ ":exception", @@ -64,7 +65,6 @@ wd_cc_library( "//src/workerd/util:thread-scopes", "//src/workerd/util:uuid", "@capnp-cpp//src/kj", - "@simdutf", "@workerd-v8//:v8", ], )