Skip to content

Commit

Permalink
Merge pull request #3151 from balusch/fix-body-initializer
Browse files Browse the repository at this point in the history
  • Loading branch information
jasnell authored Dec 3, 2024
2 parents ea6878d + acefd77 commit 5ee569c
Show file tree
Hide file tree
Showing 18 changed files with 49 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/type-snapshot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,4 @@ jobs:
if: failure()
run: |
echo "The generated output of @cloudflare/workers-types has been changed by this PR"
echo "If this is intentional, run 'rm -rf types/generated-snapshot && cp -r bazel-bin/types/definitions types/generated-snapshot' to update the snapshot"
echo "If this is intentional, run 'bazel build //types && rm -rf types/generated-snapshot && cp -r bazel-bin/types/definitions types/generated-snapshot' to update the snapshot"
6 changes: 6 additions & 0 deletions src/workerd/api/http.c++
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,12 @@ Body::ExtractedBody Body::extractBody(jsg::Lock& js, Initializer init) {
contentType = type.toString();
buffer = searchParams->toString();
}
KJ_CASE_ONEOF(searchParams, jsg::Ref<url::URLSearchParams>) {
auto type = MimeType::FORM_URLENCODED.clone();
type.addParam("charset"_kj, "UTF-8"_kj);
contentType = type.toString();
buffer = searchParams->toString();
}
}

auto bodyStream = kj::heap<BodyBufferInputStream>(buffer.clone(js));
Expand Down
4 changes: 3 additions & 1 deletion src/workerd/api/http.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "form-data.h"
#include "web-socket.h"
#include <workerd/api/url.h>
#include <workerd/api/url-standard.h>
#include "blob.h"
#include <workerd/io/compatibility-date.capnp.h>
#include "worker-rpc.h"
Expand Down Expand Up @@ -252,7 +253,8 @@ class Body: public jsg::Object {
// from any of the other source types, Body can create a new ReadableStream from the source, and
// the POST will successfully retransmit.
using Initializer = kj::OneOf<jsg::Ref<ReadableStream>, kj::String, kj::Array<byte>,
jsg::Ref<Blob>, jsg::Ref<URLSearchParams>, jsg::Ref<FormData>>;
jsg::Ref<Blob>, jsg::Ref<FormData>,
jsg::Ref<URLSearchParams>, jsg::Ref<url::URLSearchParams>>;

struct RefcountedBytes final: public kj::Refcounted {
kj::Array<kj::byte> bytes;
Expand Down
12 changes: 11 additions & 1 deletion src/workerd/api/url-standard.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,17 @@ class URLSearchParams: public jsg::Object {
JSG_METHOD(toString);
JSG_ITERABLE(entries);

if (flags.getUrlSearchParamsDeleteHasValueArg()) {
if (!flags.getSpecCompliantUrl()) {
// This is a hack. The spec-compliant URLSearchParams type is used in the Body constructor,
// see https://github.com/cloudflare/workerd/blob/v1.20241127.0/src/workerd/api/http.h#L255
// This means that when the TypeScript generation scripts are visiting root types for
// inclusion, we'll always visit the spec-compliant type even if we have the "url-standard"
// flag disabled. Rather than updating those usages based on which flags are enabled, we just
// delete the spec compliant declaration in an override if "url-standard" is disabled.
// We do the same for the non-spec-compliant URLSearchParams
// (https://github.com/cloudflare/workerd/blob/v1.20241127.0/src/workerd/api/url.h#L219).
JSG_TS_OVERRIDE(type URLSearchParams = never);
} else if (flags.getUrlSearchParamsDeleteHasValueArg()) {
JSG_TS_OVERRIDE(URLSearchParams {
entries(): IterableIterator<[key: string, value: string]>;
[Symbol.iterator](): IterableIterator<[key: string, value: string]>;
Expand Down
3 changes: 2 additions & 1 deletion types/generated-snapshot/2022-03-21/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -457,8 +457,9 @@ declare abstract class Navigator {
| string
| (ArrayBuffer | ArrayBufferView)
| Blob
| FormData
| URLSearchParams
| FormData,
| URLSearchParams,
): boolean;
readonly userAgent: string;
readonly gpu: GPU;
Expand Down
3 changes: 2 additions & 1 deletion types/generated-snapshot/2022-03-21/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -462,8 +462,9 @@ export declare abstract class Navigator {
| string
| (ArrayBuffer | ArrayBufferView)
| Blob
| FormData
| URLSearchParams
| FormData,
| URLSearchParams,
): boolean;
readonly userAgent: string;
readonly gpu: GPU;
Expand Down
3 changes: 2 additions & 1 deletion types/generated-snapshot/2022-08-04/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -457,8 +457,9 @@ declare abstract class Navigator {
| string
| (ArrayBuffer | ArrayBufferView)
| Blob
| FormData
| URLSearchParams
| FormData,
| URLSearchParams,
): boolean;
readonly userAgent: string;
readonly gpu: GPU;
Expand Down
3 changes: 2 additions & 1 deletion types/generated-snapshot/2022-08-04/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -462,8 +462,9 @@ export declare abstract class Navigator {
| string
| (ArrayBuffer | ArrayBufferView)
| Blob
| FormData
| URLSearchParams
| FormData,
| URLSearchParams,
): boolean;
readonly userAgent: string;
readonly gpu: GPU;
Expand Down
3 changes: 2 additions & 1 deletion types/generated-snapshot/2022-10-31/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -457,8 +457,9 @@ declare abstract class Navigator {
| string
| (ArrayBuffer | ArrayBufferView)
| Blob
| FormData
| URLSearchParams
| FormData,
| URLSearchParams,
): boolean;
readonly userAgent: string;
readonly gpu: GPU;
Expand Down
3 changes: 2 additions & 1 deletion types/generated-snapshot/2022-10-31/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -462,8 +462,9 @@ export declare abstract class Navigator {
| string
| (ArrayBuffer | ArrayBufferView)
| Blob
| FormData
| URLSearchParams
| FormData,
| URLSearchParams,
): boolean;
readonly userAgent: string;
readonly gpu: GPU;
Expand Down
3 changes: 2 additions & 1 deletion types/generated-snapshot/2022-11-30/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -462,8 +462,9 @@ declare abstract class Navigator {
| string
| (ArrayBuffer | ArrayBufferView)
| Blob
| FormData
| URLSearchParams
| FormData,
| URLSearchParams,
): boolean;
readonly userAgent: string;
readonly gpu: GPU;
Expand Down
3 changes: 2 additions & 1 deletion types/generated-snapshot/2022-11-30/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -467,8 +467,9 @@ export declare abstract class Navigator {
| string
| (ArrayBuffer | ArrayBufferView)
| Blob
| FormData
| URLSearchParams
| FormData,
| URLSearchParams,
): boolean;
readonly userAgent: string;
readonly gpu: GPU;
Expand Down
3 changes: 2 additions & 1 deletion types/generated-snapshot/2023-03-01/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -462,8 +462,9 @@ declare abstract class Navigator {
| string
| (ArrayBuffer | ArrayBufferView)
| Blob
| FormData
| URLSearchParams
| FormData,
| URLSearchParams,
): boolean;
readonly userAgent: string;
readonly gpu: GPU;
Expand Down
3 changes: 2 additions & 1 deletion types/generated-snapshot/2023-03-01/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -467,8 +467,9 @@ export declare abstract class Navigator {
| string
| (ArrayBuffer | ArrayBufferView)
| Blob
| FormData
| URLSearchParams
| FormData,
| URLSearchParams,
): boolean;
readonly userAgent: string;
readonly gpu: GPU;
Expand Down
3 changes: 2 additions & 1 deletion types/generated-snapshot/2023-07-01/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -462,8 +462,9 @@ declare abstract class Navigator {
| string
| (ArrayBuffer | ArrayBufferView)
| Blob
| FormData
| URLSearchParams
| FormData,
| URLSearchParams,
): boolean;
readonly userAgent: string;
readonly gpu: GPU;
Expand Down
3 changes: 2 additions & 1 deletion types/generated-snapshot/2023-07-01/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -467,8 +467,9 @@ export declare abstract class Navigator {
| string
| (ArrayBuffer | ArrayBufferView)
| Blob
| FormData
| URLSearchParams
| FormData,
| URLSearchParams,
): boolean;
readonly userAgent: string;
readonly gpu: GPU;
Expand Down
3 changes: 2 additions & 1 deletion types/generated-snapshot/experimental/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -463,8 +463,9 @@ declare abstract class Navigator {
| string
| (ArrayBuffer | ArrayBufferView)
| Blob
| FormData
| URLSearchParams
| FormData,
| URLSearchParams,
): boolean;
readonly userAgent: string;
readonly gpu: GPU;
Expand Down
3 changes: 2 additions & 1 deletion types/generated-snapshot/experimental/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -468,8 +468,9 @@ export declare abstract class Navigator {
| string
| (ArrayBuffer | ArrayBufferView)
| Blob
| FormData
| URLSearchParams
| FormData,
| URLSearchParams,
): boolean;
readonly userAgent: string;
readonly gpu: GPU;
Expand Down

0 comments on commit 5ee569c

Please sign in to comment.