// 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 #include #include namespace kj { class HttpClient; } namespace workerd::api { class ReadableStreamSource; class ReadableStream; // NOTE: We don't currently actually use this as a structured object (hence the `kj::Own` // that we see pop up). // TODO(soon): Switch to structured objects and use jsg::Ref instead of kj::Own // to maintain ownership. class R2Error: public jsg::Object { public: R2Error(uint v4Code, kj::String message): v4Code(v4Code), message(kj::mv(message)) {} constexpr kj::StringPtr getName() const { return "R2Error"_kj; } uint getV4Code() const { return v4Code; } kj::StringPtr getMessage() const { return message; } kj::StringPtr getAction() const { return KJ_ASSERT_NONNULL(action); } jsg::JsValue getStack(jsg::Lock& js); JSG_RESOURCE_TYPE(R2Error) { JSG_INHERIT_INTRINSIC(v8::kErrorPrototype); JSG_READONLY_INSTANCE_PROPERTY(name, getName); JSG_READONLY_INSTANCE_PROPERTY(code, getV4Code); JSG_READONLY_INSTANCE_PROPERTY(message, getMessage); JSG_READONLY_INSTANCE_PROPERTY(action, getAction); JSG_READONLY_INSTANCE_PROPERTY(stack, getStack); // See getStack in dom-exception.h JSG_TS_ROOT(); } private: uint v4Code; kj::String message; kj::Maybe action; // Initialized when thrown. kj::Maybe> errorForStack; // See dom-exception.h. friend struct R2Result; }; using R2PutValue = kj::OneOf, kj::Array, jsg::NonCoercible, jsg::Ref>; struct R2Result { uint httpStatus; // Non-null if httpStatus >= 400. kj::Maybe> toThrow; kj::Maybe> metadataPayload; kj::Maybe> stream; bool objectNotFound() { return httpStatus == 404 && v4ErrorCode() == 10007; } bool preconditionFailed() { return httpStatus == 412 && (v4ErrorCode() == 10031 || v4ErrorCode() == 10032); } bool success() { return httpStatus >= 200 && httpStatus < 400; } kj::Maybe v4ErrorCode(); kj::Maybe getR2ErrorMessage(); void throwIfError(kj::StringPtr action, const jsg::TypeHandler>& errorType); }; kj::Promise doR2HTTPGetRequest(kj::Own client, kj::String metadataPayload, kj::ArrayPtr path, kj::Maybe jwt, CompatibilityFlags::Reader flags); kj::Promise doR2HTTPPutRequest(kj::Own client, kj::Maybe value, kj::Maybe streamSize, // Deprecated. For internal beta API only. kj::String metadataPayload, kj::ArrayPtr path, kj::Maybe jwt); } // namespace workerd::api