Skip to content

Fix fetch cache body handling and update edge-runtime deps #48365

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Apr 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions packages/next/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@
"@babel/traverse": "7.18.0",
"@babel/types": "7.18.0",
"@capsizecss/metrics": "1.1.0",
"@edge-runtime/cookies": "3.0.4",
"@edge-runtime/primitives": "2.0.5",
"@edge-runtime/cookies": "3.0.6",
"@edge-runtime/primitives": "2.1.2",
"@hapi/accept": "5.0.2",
"@napi-rs/cli": "2.14.7",
"@napi-rs/triples": "1.1.0",
Expand Down Expand Up @@ -220,7 +220,7 @@
"debug": "4.1.1",
"devalue": "2.0.1",
"domain-browser": "4.19.0",
"edge-runtime": "2.0.0",
"edge-runtime": "2.1.4",
"events": "3.3.0",
"find-cache-dir": "3.3.1",
"find-up": "4.1.0",
Expand Down
4 changes: 3 additions & 1 deletion packages/next/src/compiled/@edge-runtime/cookies/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,13 @@ interface CookieSerializeOptions {
* {@link https://wicg.github.io/cookie-store/#dictdef-cookielistitem CookieListItem}
* as specified by W3C.
*/
interface CookieListItem extends Pick<CookieSerializeOptions, 'domain' | 'path' | 'expires' | 'secure' | 'sameSite'> {
interface CookieListItem extends Pick<CookieSerializeOptions, 'domain' | 'path' | 'secure' | 'sameSite'> {
/** A string with the name of a cookie. */
name: string;
/** A string containing the value of the cookie. */
value: string;
/** A number of milliseconds or Date interface containing the expires of the cookie. */
expires?: number | CookieSerializeOptions['expires'];
}
/**
* Superset of {@link CookieListItem} extending it with
Expand Down
5 changes: 4 additions & 1 deletion packages/next/src/compiled/@edge-runtime/cookies/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ module.exports = __toCommonJS(src_exports);
function serialize(c) {
const attrs = [
"path" in c && c.path && `Path=${c.path}`,
"expires" in c && c.expires && `Expires=${c.expires.toUTCString()}`,
"expires" in c && (c.expires || c.expires === 0) && `Expires=${(typeof c.expires === "number" ? new Date(c.expires) : c.expires).toUTCString()}`,
"maxAge" in c && c.maxAge && `Max-Age=${c.maxAge}`,
"domain" in c && c.domain && `Domain=${c.domain}`,
"secure" in c && c.secure && "Secure",
Expand Down Expand Up @@ -207,6 +207,9 @@ function replace(bag, headers) {
}
}
function normalizeCookie(cookie = { name: "", value: "" }) {
if (typeof cookie.expires === "number") {
cookie.expires = new Date(cookie.expires);
}
if (cookie.maxAge) {
cookie.expires = new Date(Date.now() + cookie.maxAge * 1e3);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"name":"@edge-runtime/cookies","version":"3.0.4","main":"./index.js","license":"MPLv2"}
{"name":"@edge-runtime/cookies","version":"3.0.6","main":"./index.js","license":"MPL-2.0"}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"name":"@edge-runtime/primitives","version":"2.0.5","main":"./index.js","license":"MPLv2"}
{"name":"@edge-runtime/primitives","version":"2.1.2","main":"./index.js","license":"MPL-2.0"}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ declare class ReadableStreamBYOBReader {
declare const ReadableStreamConstructor: typeof ReadableStream
declare const ReadableStreamBYOBReaderConstructor: typeof ReadableStreamBYOBReader
declare const ReadableStreamDefaultReaderConstructor: typeof ReadableStreamDefaultReader
declare const TextDecoderStreamConstructor: typeof TextDecoderStream
declare const TextEncoderStreamConstructor: typeof TextEncoderStream
declare const TransformStreamConstructor: typeof TransformStream
declare const WritableStreamConstructor: typeof WritableStream
declare const WritableStreamDefaultWriterConstructor: typeof WritableStreamDefaultWriter

export { ReadableStreamConstructor as ReadableStream, ReadableStreamBYOBReaderConstructor as ReadableStreamBYOBReader, ReadableStreamDefaultReaderConstructor as ReadableStreamDefaultReader, TransformStreamConstructor as TransformStream, WritableStreamConstructor as WritableStream, WritableStreamDefaultWriterConstructor as WritableStreamDefaultWriter };
export { ReadableStreamConstructor as ReadableStream, ReadableStreamBYOBReaderConstructor as ReadableStreamBYOBReader, ReadableStreamDefaultReaderConstructor as ReadableStreamDefaultReader, TextDecoderStreamConstructor as TextDecoderStream, TextEncoderStreamConstructor as TextEncoderStream, TransformStreamConstructor as TransformStream, WritableStreamConstructor as WritableStream, WritableStreamDefaultWriterConstructor as WritableStreamDefaultWriter };
109 changes: 104 additions & 5 deletions packages/next/src/compiled/@edge-runtime/primitives/streams.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ __export(streams_exports, {
ReadableStream: () => ReadableStream,
ReadableStreamBYOBReader: () => ReadableStreamBYOBReader,
ReadableStreamDefaultReader: () => ReadableStreamDefaultReader,
TransformStream: () => TransformStream,
TextDecoderStream: () => TextDecoderStream,
TextEncoderStream: () => TextEncoderStream,
TransformStream: () => TransformStream2,
WritableStream: () => WritableStream,
WritableStreamDefaultWriter: () => WritableStreamDefaultWriter
});
Expand Down Expand Up @@ -1954,7 +1956,7 @@ function sr(e2, t2, r2) {
}
__name(sr, "sr");
Object.defineProperties(CountQueuingStrategy.prototype, { highWaterMark: { enumerable: true }, size: { enumerable: true } }), "symbol" == typeof e.toStringTag && Object.defineProperty(CountQueuingStrategy.prototype, e.toStringTag, { value: "CountQueuingStrategy", configurable: true });
var TransformStream = class {
var TransformStream2 = class {
constructor(e2 = {}, t2 = {}, r2 = {}) {
void 0 === e2 && (e2 = null);
const o2 = Ye(t2, "Second parameter"), n2 = Ye(r2, "Third parameter"), a2 = function(e3, t3) {
Expand Down Expand Up @@ -2084,9 +2086,9 @@ var TransformStream = class {
return this._writable;
}
};
__name(TransformStream, "TransformStream");
__name(TransformStream2, "TransformStream");
function ur(e2) {
return !!r(e2) && (!!Object.prototype.hasOwnProperty.call(e2, "_transformStreamController") && e2 instanceof TransformStream);
return !!r(e2) && (!!Object.prototype.hasOwnProperty.call(e2, "_transformStreamController") && e2 instanceof TransformStream2);
}
__name(ur, "ur");
function cr(e2, t2) {
Expand All @@ -2106,7 +2108,7 @@ function fr(e2, t2) {
}), e2._backpressure = t2;
}
__name(fr, "fr");
Object.defineProperties(TransformStream.prototype, { readable: { enumerable: true }, writable: { enumerable: true } }), "symbol" == typeof e.toStringTag && Object.defineProperty(TransformStream.prototype, e.toStringTag, { value: "TransformStream", configurable: true });
Object.defineProperties(TransformStream2.prototype, { readable: { enumerable: true }, writable: { enumerable: true } }), "symbol" == typeof e.toStringTag && Object.defineProperty(TransformStream2.prototype, e.toStringTag, { value: "TransformStream", configurable: true });
var TransformStreamDefaultController = class {
constructor() {
throw new TypeError("Illegal constructor");
Expand Down Expand Up @@ -2226,11 +2228,108 @@ function Cr(e2) {
}
__name(Cr, "Cr");
Object.defineProperties(TransformStreamDefaultController.prototype, { enqueue: { enumerable: true }, error: { enumerable: true }, terminate: { enumerable: true }, desiredSize: { enumerable: true } }), n(TransformStreamDefaultController.prototype.enqueue, "enqueue"), n(TransformStreamDefaultController.prototype.error, "error"), n(TransformStreamDefaultController.prototype.terminate, "terminate"), "symbol" == typeof e.toStringTag && Object.defineProperty(TransformStreamDefaultController.prototype, e.toStringTag, { value: "TransformStreamDefaultController", configurable: true });

// ../../node_modules/.pnpm/@[email protected]/node_modules/@stardazed/streams-text-encoding/dist/sd-streams-text-encoding.esm.js
var decDecoder = Symbol("decDecoder");
var decTransform = Symbol("decTransform");
var TextDecodeTransformer = class {
constructor(decoder) {
this.decoder_ = decoder;
}
transform(chunk, controller) {
if (!(chunk instanceof ArrayBuffer || ArrayBuffer.isView(chunk))) {
throw new TypeError("Input data must be a BufferSource");
}
const text = this.decoder_.decode(chunk, { stream: true });
if (text.length !== 0) {
controller.enqueue(text);
}
}
flush(controller) {
const text = this.decoder_.decode();
if (text.length !== 0) {
controller.enqueue(text);
}
}
};
__name(TextDecodeTransformer, "TextDecodeTransformer");
var TextDecoderStream = class {
constructor(label, options) {
this[decDecoder] = new TextDecoder(label, options);
this[decTransform] = new TransformStream(new TextDecodeTransformer(this[decDecoder]));
}
get encoding() {
return this[decDecoder].encoding;
}
get fatal() {
return this[decDecoder].fatal;
}
get ignoreBOM() {
return this[decDecoder].ignoreBOM;
}
get readable() {
return this[decTransform].readable;
}
get writable() {
return this[decTransform].writable;
}
};
__name(TextDecoderStream, "TextDecoderStream");
var encEncoder = Symbol("encEncoder");
var encTransform = Symbol("encTransform");
var TextEncodeTransformer = class {
constructor(encoder) {
this.encoder_ = encoder;
this.partial_ = void 0;
}
transform(chunk, controller) {
let stringChunk = String(chunk);
if (this.partial_ !== void 0) {
stringChunk = this.partial_ + stringChunk;
this.partial_ = void 0;
}
const lastCharIndex = stringChunk.length - 1;
const lastCodeUnit = stringChunk.charCodeAt(lastCharIndex);
if (lastCodeUnit >= 55296 && lastCodeUnit < 56320) {
this.partial_ = String.fromCharCode(lastCodeUnit);
stringChunk = stringChunk.substring(0, lastCharIndex);
}
const bytes = this.encoder_.encode(stringChunk);
if (bytes.length !== 0) {
controller.enqueue(bytes);
}
}
flush(controller) {
if (this.partial_) {
controller.enqueue(this.encoder_.encode(this.partial_));
this.partial_ = void 0;
}
}
};
__name(TextEncodeTransformer, "TextEncodeTransformer");
var TextEncoderStream = class {
constructor() {
this[encEncoder] = new TextEncoder();
this[encTransform] = new TransformStream(new TextEncodeTransformer(this[encEncoder]));
}
get encoding() {
return this[encEncoder].encoding;
}
get readable() {
return this[encTransform].readable;
}
get writable() {
return this[encTransform].writable;
}
};
__name(TextEncoderStream, "TextEncoderStream");
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
ReadableStream,
ReadableStreamBYOBReader,
ReadableStreamDefaultReader,
TextDecoderStream,
TextEncoderStream,
TransformStream,
WritableStream,
WritableStreamDefaultWriter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ __export(structured_clone_exports, {
});
module.exports = __toCommonJS(structured_clone_exports);

// ../../node_modules/.pnpm/@[email protected].1/node_modules/@ungap/structured-clone/esm/types.js
// ../../node_modules/.pnpm/@[email protected].2/node_modules/@ungap/structured-clone/esm/types.js
var VOID = -1;
var PRIMITIVE = 0;
var ARRAY = 1;
Expand All @@ -37,7 +37,7 @@ var SET = 6;
var ERROR = 7;
var BIGINT = 8;

// ../../node_modules/.pnpm/@[email protected].1/node_modules/@ungap/structured-clone/esm/deserialize.js
// ../../node_modules/.pnpm/@[email protected].2/node_modules/@ungap/structured-clone/esm/deserialize.js
var env = typeof self === "object" ? self : globalThis;
var deserializer = /* @__PURE__ */ __name(($, _) => {
const as = /* @__PURE__ */ __name((out, index) => {
Expand Down Expand Up @@ -97,7 +97,7 @@ var deserializer = /* @__PURE__ */ __name(($, _) => {
}, "deserializer");
var deserialize = /* @__PURE__ */ __name((serialized) => deserializer(/* @__PURE__ */ new Map(), serialized)(0), "deserialize");

// ../../node_modules/.pnpm/@[email protected].1/node_modules/@ungap/structured-clone/esm/serialize.js
// ../../node_modules/.pnpm/@[email protected].2/node_modules/@ungap/structured-clone/esm/serialize.js
var EMPTY = "";
var { toString } = {};
var { keys } = Object;
Expand Down Expand Up @@ -221,7 +221,7 @@ var serialize = /* @__PURE__ */ __name((value, { json, lossy } = {}) => {
return serializer(!(json || lossy), !!json, /* @__PURE__ */ new Map(), _)(value), _;
}, "serialize");

// ../../node_modules/.pnpm/@[email protected].1/node_modules/@ungap/structured-clone/esm/index.js
// ../../node_modules/.pnpm/@[email protected].2/node_modules/@ungap/structured-clone/esm/index.js
var esm_default = typeof structuredClone === "function" ? (any, options) => options && ("json" in options || "lossy" in options) ? deserialize(serialize(any, options)) : structuredClone(any) : (any, options) => deserialize(serialize(any, options));
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
Expand Down
4 changes: 2 additions & 2 deletions packages/next/src/compiled/babel-packages/packages-bundle.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/next/src/compiled/edge-runtime/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/next/src/compiled/edge-runtime/package.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"name":"edge-runtime","main":"index.js","license":"MPLv2"}
{"name":"edge-runtime","main":"index.js","license":"MPL-2.0"}
18 changes: 4 additions & 14 deletions packages/next/src/server/lib/patch-fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,17 +242,7 @@ export function patchFetch({
typeof revalidate === 'number' &&
revalidate > 0
) {
let base64Body = ''
const resBlob = await res.blob()
const arrayBuffer = await resBlob.arrayBuffer()

if (process.env.NEXT_RUNTIME === 'edge') {
const { encode } =
require('../../shared/lib/bloom-filter/base64-arraybuffer') as typeof import('../../shared/lib/bloom-filter/base64-arraybuffer')
base64Body = encode(arrayBuffer)
} else {
base64Body = Buffer.from(arrayBuffer).toString('base64')
}
const bodyBuffer = Buffer.from(await res.arrayBuffer())

try {
await staticGenerationStore.incrementalCache.set(
Expand All @@ -261,7 +251,7 @@ export function patchFetch({
kind: 'FETCH',
data: {
headers: Object.fromEntries(res.headers.entries()),
body: base64Body,
body: bodyBuffer.toString('base64'),
status: res.status,
},
revalidate,
Expand All @@ -275,8 +265,8 @@ export function patchFetch({
console.warn(`Failed to set fetch cache`, input, err)
}

return new Response(resBlob, {
headers: res.headers,
return new Response(bodyBuffer, {
headers: new Headers(res.headers),
status: res.status,
})
}
Expand Down
3 changes: 0 additions & 3 deletions packages/next/src/shared/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@ export const EDGE_UNSUPPORTED_NODE_APIS = [
'clearImmediate',
'setImmediate',
'BroadcastChannel',
'Buffer',
'ByteLengthQueuingStrategy',
'CompressionStream',
'CountQueuingStrategy',
Expand All @@ -144,8 +143,6 @@ export const EDGE_UNSUPPORTED_NODE_APIS = [
'ReadableByteStreamController',
'ReadableStreamBYOBRequest',
'ReadableStreamDefaultController',
'TextDecoderStream',
'TextEncoderStream',
'TransformStreamDefaultController',
'WritableStreamDefaultController',
]
Expand Down
Loading