added fonts
This commit is contained in:
1 parent
4134e3a234
commit
142d2986b6
9079 files changed
+1498421
-2
No files matched your search
+43
@@ -0,0 +1,43 @@
|
||||
import type { IncomingHttpHeaders } from 'http';
|
||||
export type ReadonlyHeaders = Headers & {
|
||||
/** @deprecated Method unavailable on `ReadonlyHeaders`. Read more: https://nextjs.org/docs/app/api-reference/functions/headers */
|
||||
append(...args: any[]): void;
|
||||
/** @deprecated Method unavailable on `ReadonlyHeaders`. Read more: https://nextjs.org/docs/app/api-reference/functions/headers */
|
||||
set(...args: any[]): void;
|
||||
/** @deprecated Method unavailable on `ReadonlyHeaders`. Read more: https://nextjs.org/docs/app/api-reference/functions/headers */
|
||||
delete(...args: any[]): void;
|
||||
};
|
||||
export declare class HeadersAdapter extends Headers {
|
||||
private readonly headers;
|
||||
constructor(headers: IncomingHttpHeaders);
|
||||
/**
|
||||
* Seals a Headers instance to prevent modification by throwing an error when
|
||||
* any mutating method is called.
|
||||
*/
|
||||
static seal(headers: Headers): ReadonlyHeaders;
|
||||
/**
|
||||
* Merges a header value into a string. This stores multiple values as an
|
||||
* array, so we need to merge them into a string.
|
||||
*
|
||||
* @param value a header value
|
||||
* @returns a merged header value (a string)
|
||||
*/
|
||||
private merge;
|
||||
/**
|
||||
* Creates a Headers instance from a plain object or a Headers instance.
|
||||
*
|
||||
* @param headers a plain object or a Headers instance
|
||||
* @returns a headers instance
|
||||
*/
|
||||
static from(headers: IncomingHttpHeaders | Headers): Headers;
|
||||
append(name: string, value: string): void;
|
||||
delete(name: string): void;
|
||||
get(name: string): string | null;
|
||||
has(name: string): boolean;
|
||||
set(name: string, value: string): void;
|
||||
forEach(callbackfn: (value: string, name: string, parent: Headers) => void, thisArg?: any): void;
|
||||
entries(): HeadersIterator<[string, string]>;
|
||||
keys(): HeadersIterator<string>;
|
||||
values(): HeadersIterator<string>;
|
||||
[Symbol.iterator](): HeadersIterator<[string, string]>;
|
||||
}
|
||||
+192
@@ -0,0 +1,192 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
0 && (module.exports = {
|
||||
HeadersAdapter: null,
|
||||
ReadonlyHeadersError: null
|
||||
});
|
||||
function _export(target, all) {
|
||||
for(var name in all)Object.defineProperty(target, name, {
|
||||
enumerable: true,
|
||||
get: all[name]
|
||||
});
|
||||
}
|
||||
_export(exports, {
|
||||
HeadersAdapter: function() {
|
||||
return HeadersAdapter;
|
||||
},
|
||||
ReadonlyHeadersError: function() {
|
||||
return ReadonlyHeadersError;
|
||||
}
|
||||
});
|
||||
const _reflect = require("./reflect");
|
||||
class ReadonlyHeadersError extends Error {
|
||||
constructor(){
|
||||
super('Headers cannot be modified. Read more: https://nextjs.org/docs/app/api-reference/functions/headers');
|
||||
}
|
||||
static callable() {
|
||||
throw new ReadonlyHeadersError();
|
||||
}
|
||||
}
|
||||
class HeadersAdapter extends Headers {
|
||||
constructor(headers){
|
||||
// We've already overridden the methods that would be called, so we're just
|
||||
// calling the super constructor to ensure that the instanceof check works.
|
||||
super();
|
||||
this.headers = new Proxy(headers, {
|
||||
get (target, prop, receiver) {
|
||||
// Because this is just an object, we expect that all "get" operations
|
||||
// are for properties. If it's a "get" for a symbol, we'll just return
|
||||
// the symbol.
|
||||
if (typeof prop === 'symbol') {
|
||||
return _reflect.ReflectAdapter.get(target, prop, receiver);
|
||||
}
|
||||
const lowercased = prop.toLowerCase();
|
||||
// Let's find the original casing of the key. This assumes that there is
|
||||
// no mixed case keys (e.g. "Content-Type" and "content-type") in the
|
||||
// headers object.
|
||||
const original = Object.keys(headers).find((o)=>o.toLowerCase() === lowercased);
|
||||
// If the original casing doesn't exist, return undefined.
|
||||
if (typeof original === 'undefined') return;
|
||||
// If the original casing exists, return the value.
|
||||
return _reflect.ReflectAdapter.get(target, original, receiver);
|
||||
},
|
||||
set (target, prop, value, receiver) {
|
||||
if (typeof prop === 'symbol') {
|
||||
return _reflect.ReflectAdapter.set(target, prop, value, receiver);
|
||||
}
|
||||
const lowercased = prop.toLowerCase();
|
||||
// Let's find the original casing of the key. This assumes that there is
|
||||
// no mixed case keys (e.g. "Content-Type" and "content-type") in the
|
||||
// headers object.
|
||||
const original = Object.keys(headers).find((o)=>o.toLowerCase() === lowercased);
|
||||
// If the original casing doesn't exist, use the prop as the key.
|
||||
return _reflect.ReflectAdapter.set(target, original ?? prop, value, receiver);
|
||||
},
|
||||
has (target, prop) {
|
||||
if (typeof prop === 'symbol') return _reflect.ReflectAdapter.has(target, prop);
|
||||
const lowercased = prop.toLowerCase();
|
||||
// Let's find the original casing of the key. This assumes that there is
|
||||
// no mixed case keys (e.g. "Content-Type" and "content-type") in the
|
||||
// headers object.
|
||||
const original = Object.keys(headers).find((o)=>o.toLowerCase() === lowercased);
|
||||
// If the original casing doesn't exist, return false.
|
||||
if (typeof original === 'undefined') return false;
|
||||
// If the original casing exists, return true.
|
||||
return _reflect.ReflectAdapter.has(target, original);
|
||||
},
|
||||
deleteProperty (target, prop) {
|
||||
if (typeof prop === 'symbol') return _reflect.ReflectAdapter.deleteProperty(target, prop);
|
||||
const lowercased = prop.toLowerCase();
|
||||
// Let's find the original casing of the key. This assumes that there is
|
||||
// no mixed case keys (e.g. "Content-Type" and "content-type") in the
|
||||
// headers object.
|
||||
const original = Object.keys(headers).find((o)=>o.toLowerCase() === lowercased);
|
||||
// If the original casing doesn't exist, return true.
|
||||
if (typeof original === 'undefined') return true;
|
||||
// If the original casing exists, delete the property.
|
||||
return _reflect.ReflectAdapter.deleteProperty(target, original);
|
||||
}
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Seals a Headers instance to prevent modification by throwing an error when
|
||||
* any mutating method is called.
|
||||
*/ static seal(headers) {
|
||||
return new Proxy(headers, {
|
||||
get (target, prop, receiver) {
|
||||
switch(prop){
|
||||
case 'append':
|
||||
case 'delete':
|
||||
case 'set':
|
||||
return ReadonlyHeadersError.callable;
|
||||
default:
|
||||
return _reflect.ReflectAdapter.get(target, prop, receiver);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Merges a header value into a string. This stores multiple values as an
|
||||
* array, so we need to merge them into a string.
|
||||
*
|
||||
* @param value a header value
|
||||
* @returns a merged header value (a string)
|
||||
*/ merge(value) {
|
||||
if (Array.isArray(value)) return value.join(', ');
|
||||
return value;
|
||||
}
|
||||
/**
|
||||
* Creates a Headers instance from a plain object or a Headers instance.
|
||||
*
|
||||
* @param headers a plain object or a Headers instance
|
||||
* @returns a headers instance
|
||||
*/ static from(headers) {
|
||||
if (headers instanceof Headers) return headers;
|
||||
return new HeadersAdapter(headers);
|
||||
}
|
||||
append(name, value) {
|
||||
const existing = this.headers[name];
|
||||
if (typeof existing === 'string') {
|
||||
this.headers[name] = [
|
||||
existing,
|
||||
value
|
||||
];
|
||||
} else if (Array.isArray(existing)) {
|
||||
existing.push(value);
|
||||
} else {
|
||||
this.headers[name] = value;
|
||||
}
|
||||
}
|
||||
delete(name) {
|
||||
delete this.headers[name];
|
||||
}
|
||||
get(name) {
|
||||
const value = this.headers[name];
|
||||
if (typeof value !== 'undefined') return this.merge(value);
|
||||
return null;
|
||||
}
|
||||
has(name) {
|
||||
return typeof this.headers[name] !== 'undefined';
|
||||
}
|
||||
set(name, value) {
|
||||
this.headers[name] = value;
|
||||
}
|
||||
forEach(callbackfn, thisArg) {
|
||||
for (const [name, value] of this.entries()){
|
||||
callbackfn.call(thisArg, value, name, this);
|
||||
}
|
||||
}
|
||||
*entries() {
|
||||
for (const key of Object.keys(this.headers)){
|
||||
const name = key.toLowerCase();
|
||||
// We assert here that this is a string because we got it from the
|
||||
// Object.keys() call above.
|
||||
const value = this.get(name);
|
||||
yield [
|
||||
name,
|
||||
value
|
||||
];
|
||||
}
|
||||
}
|
||||
*keys() {
|
||||
for (const key of Object.keys(this.headers)){
|
||||
const name = key.toLowerCase();
|
||||
yield name;
|
||||
}
|
||||
}
|
||||
*values() {
|
||||
for (const key of Object.keys(this.headers)){
|
||||
// We assert here that this is a string because we got it from the
|
||||
// Object.keys() call above.
|
||||
const value = this.get(key);
|
||||
yield value;
|
||||
}
|
||||
}
|
||||
[Symbol.iterator]() {
|
||||
return this.entries();
|
||||
}
|
||||
}
|
||||
|
||||
//# sourceMappingURL=headers.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"sources":["../../../../../src/server/web/spec-extension/adapters/headers.ts"],"sourcesContent":["import type { IncomingHttpHeaders } from 'http'\n\nimport { ReflectAdapter } from './reflect'\n\n/**\n * @internal\n */\nexport class ReadonlyHeadersError extends Error {\n constructor() {\n super(\n 'Headers cannot be modified. Read more: https://nextjs.org/docs/app/api-reference/functions/headers'\n )\n }\n\n public static callable() {\n throw new ReadonlyHeadersError()\n }\n}\n\nexport type ReadonlyHeaders = Headers & {\n /** @deprecated Method unavailable on `ReadonlyHeaders`. Read more: https://nextjs.org/docs/app/api-reference/functions/headers */\n append(...args: any[]): void\n /** @deprecated Method unavailable on `ReadonlyHeaders`. Read more: https://nextjs.org/docs/app/api-reference/functions/headers */\n set(...args: any[]): void\n /** @deprecated Method unavailable on `ReadonlyHeaders`. Read more: https://nextjs.org/docs/app/api-reference/functions/headers */\n delete(...args: any[]): void\n}\nexport class HeadersAdapter extends Headers {\n private readonly headers: IncomingHttpHeaders\n\n constructor(headers: IncomingHttpHeaders) {\n // We've already overridden the methods that would be called, so we're just\n // calling the super constructor to ensure that the instanceof check works.\n super()\n\n this.headers = new Proxy(headers, {\n get(target, prop, receiver) {\n // Because this is just an object, we expect that all \"get\" operations\n // are for properties. If it's a \"get\" for a symbol, we'll just return\n // the symbol.\n if (typeof prop === 'symbol') {\n return ReflectAdapter.get(target, prop, receiver)\n }\n\n const lowercased = prop.toLowerCase()\n\n // Let's find the original casing of the key. This assumes that there is\n // no mixed case keys (e.g. \"Content-Type\" and \"content-type\") in the\n // headers object.\n const original = Object.keys(headers).find(\n (o) => o.toLowerCase() === lowercased\n )\n\n // If the original casing doesn't exist, return undefined.\n if (typeof original === 'undefined') return\n\n // If the original casing exists, return the value.\n return ReflectAdapter.get(target, original, receiver)\n },\n set(target, prop, value, receiver) {\n if (typeof prop === 'symbol') {\n return ReflectAdapter.set(target, prop, value, receiver)\n }\n\n const lowercased = prop.toLowerCase()\n\n // Let's find the original casing of the key. This assumes that there is\n // no mixed case keys (e.g. \"Content-Type\" and \"content-type\") in the\n // headers object.\n const original = Object.keys(headers).find(\n (o) => o.toLowerCase() === lowercased\n )\n\n // If the original casing doesn't exist, use the prop as the key.\n return ReflectAdapter.set(target, original ?? prop, value, receiver)\n },\n has(target, prop) {\n if (typeof prop === 'symbol') return ReflectAdapter.has(target, prop)\n\n const lowercased = prop.toLowerCase()\n\n // Let's find the original casing of the key. This assumes that there is\n // no mixed case keys (e.g. \"Content-Type\" and \"content-type\") in the\n // headers object.\n const original = Object.keys(headers).find(\n (o) => o.toLowerCase() === lowercased\n )\n\n // If the original casing doesn't exist, return false.\n if (typeof original === 'undefined') return false\n\n // If the original casing exists, return true.\n return ReflectAdapter.has(target, original)\n },\n deleteProperty(target, prop) {\n if (typeof prop === 'symbol')\n return ReflectAdapter.deleteProperty(target, prop)\n\n const lowercased = prop.toLowerCase()\n\n // Let's find the original casing of the key. This assumes that there is\n // no mixed case keys (e.g. \"Content-Type\" and \"content-type\") in the\n // headers object.\n const original = Object.keys(headers).find(\n (o) => o.toLowerCase() === lowercased\n )\n\n // If the original casing doesn't exist, return true.\n if (typeof original === 'undefined') return true\n\n // If the original casing exists, delete the property.\n return ReflectAdapter.deleteProperty(target, original)\n },\n })\n }\n\n /**\n * Seals a Headers instance to prevent modification by throwing an error when\n * any mutating method is called.\n */\n public static seal(headers: Headers): ReadonlyHeaders {\n return new Proxy<ReadonlyHeaders>(headers, {\n get(target, prop, receiver) {\n switch (prop) {\n case 'append':\n case 'delete':\n case 'set':\n return ReadonlyHeadersError.callable\n default:\n Line truncated
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
import type { BaseNextRequest } from '../../../base-http';
|
||||
import type { NodeNextRequest } from '../../../base-http/node';
|
||||
import type { WebNextRequest } from '../../../base-http/web';
|
||||
import type { Writable } from 'node:stream';
|
||||
import { NextRequest } from '../request';
|
||||
export declare const ResponseAbortedName = "ResponseAborted";
|
||||
export declare class ResponseAborted extends Error {
|
||||
readonly name = "ResponseAborted";
|
||||
}
|
||||
/**
|
||||
* Creates an AbortController tied to the closing of a ServerResponse (or other
|
||||
* appropriate Writable).
|
||||
*
|
||||
* If the `close` event is fired before the `finish` event, then we'll send the
|
||||
* `abort` signal.
|
||||
*/
|
||||
export declare function createAbortController(response: Writable): AbortController;
|
||||
/**
|
||||
* Creates an AbortSignal tied to the closing of a ServerResponse (or other
|
||||
* appropriate Writable).
|
||||
*
|
||||
* This cannot be done with the request (IncomingMessage or Readable) because
|
||||
* the `abort` event will not fire if to data has been fully read (because that
|
||||
* will "close" the readable stream and nothing fires after that).
|
||||
*/
|
||||
export declare function signalFromNodeResponse(response: Writable): AbortSignal;
|
||||
export declare class NextRequestAdapter {
|
||||
static fromBaseNextRequest(request: BaseNextRequest, signal: AbortSignal): NextRequest;
|
||||
static fromNodeNextRequest(request: NodeNextRequest, signal: AbortSignal): NextRequest;
|
||||
static fromWebNextRequest(request: WebNextRequest): NextRequest;
|
||||
}
|
||||
+142
@@ -0,0 +1,142 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
0 && (module.exports = {
|
||||
NextRequestAdapter: null,
|
||||
ResponseAborted: null,
|
||||
ResponseAbortedName: null,
|
||||
createAbortController: null,
|
||||
signalFromNodeResponse: null
|
||||
});
|
||||
function _export(target, all) {
|
||||
for(var name in all)Object.defineProperty(target, name, {
|
||||
enumerable: true,
|
||||
get: all[name]
|
||||
});
|
||||
}
|
||||
_export(exports, {
|
||||
NextRequestAdapter: function() {
|
||||
return NextRequestAdapter;
|
||||
},
|
||||
ResponseAborted: function() {
|
||||
return ResponseAborted;
|
||||
},
|
||||
ResponseAbortedName: function() {
|
||||
return ResponseAbortedName;
|
||||
},
|
||||
createAbortController: function() {
|
||||
return createAbortController;
|
||||
},
|
||||
signalFromNodeResponse: function() {
|
||||
return signalFromNodeResponse;
|
||||
}
|
||||
});
|
||||
const _requestmeta = require("../../../request-meta");
|
||||
const _utils = require("../../utils");
|
||||
const _request = require("../request");
|
||||
const _helpers = require("../../../base-http/helpers");
|
||||
const ResponseAbortedName = 'ResponseAborted';
|
||||
class ResponseAborted extends Error {
|
||||
constructor(...args){
|
||||
super(...args), this.name = ResponseAbortedName;
|
||||
}
|
||||
}
|
||||
function createAbortController(response) {
|
||||
const controller = new AbortController();
|
||||
// If `finish` fires first, then `res.end()` has been called and the close is
|
||||
// just us finishing the stream on our side. If `close` fires first, then we
|
||||
// know the client disconnected before we finished.
|
||||
response.once('close', ()=>{
|
||||
if (response.writableFinished) return;
|
||||
controller.abort(new ResponseAborted());
|
||||
});
|
||||
return controller;
|
||||
}
|
||||
function signalFromNodeResponse(response) {
|
||||
const { errored, destroyed } = response;
|
||||
if (errored || destroyed) {
|
||||
return AbortSignal.abort(errored ?? new ResponseAborted());
|
||||
}
|
||||
const { signal } = createAbortController(response);
|
||||
return signal;
|
||||
}
|
||||
class NextRequestAdapter {
|
||||
static fromBaseNextRequest(request, signal) {
|
||||
if (// The type check here ensures that `req` is correctly typed, and the
|
||||
// environment variable check provides dead code elimination.
|
||||
process.env.NEXT_RUNTIME === 'edge' && (0, _helpers.isWebNextRequest)(request)) {
|
||||
return NextRequestAdapter.fromWebNextRequest(request);
|
||||
} else if (// The type check here ensures that `req` is correctly typed, and the
|
||||
// environment variable check provides dead code elimination.
|
||||
process.env.NEXT_RUNTIME !== 'edge' && (0, _helpers.isNodeNextRequest)(request)) {
|
||||
return NextRequestAdapter.fromNodeNextRequest(request, signal);
|
||||
} else {
|
||||
throw Object.defineProperty(new Error('Invariant: Unsupported NextRequest type'), "__NEXT_ERROR_CODE", {
|
||||
value: "E345",
|
||||
enumerable: false,
|
||||
configurable: true
|
||||
});
|
||||
}
|
||||
}
|
||||
static fromNodeNextRequest(request, signal) {
|
||||
// HEAD and GET requests can not have a body.
|
||||
let body = null;
|
||||
if (request.method !== 'GET' && request.method !== 'HEAD' && request.body) {
|
||||
// @ts-expect-error - this is handled by undici, when streams/web land use it instead
|
||||
body = request.body;
|
||||
}
|
||||
let url;
|
||||
if (request.url.startsWith('http')) {
|
||||
url = new URL(request.url);
|
||||
} else {
|
||||
// Grab the full URL from the request metadata.
|
||||
const base = (0, _requestmeta.getRequestMeta)(request, 'initURL');
|
||||
if (!base || !base.startsWith('http')) {
|
||||
// Because the URL construction relies on the fact that the URL provided
|
||||
// is absolute, we need to provide a base URL. We can't use the request
|
||||
// URL because it's relative, so we use a dummy URL instead.
|
||||
url = new URL(request.url, 'http://n');
|
||||
} else {
|
||||
url = new URL(request.url, base);
|
||||
}
|
||||
}
|
||||
return new _request.NextRequest(url, {
|
||||
method: request.method,
|
||||
headers: (0, _utils.fromNodeOutgoingHttpHeaders)(request.headers),
|
||||
duplex: 'half',
|
||||
signal,
|
||||
// geo
|
||||
// ip
|
||||
// nextConfig
|
||||
// body can not be passed if request was aborted
|
||||
// or we get a Request body was disturbed error
|
||||
...signal.aborted ? {} : {
|
||||
body
|
||||
}
|
||||
});
|
||||
}
|
||||
static fromWebNextRequest(request) {
|
||||
// HEAD and GET requests can not have a body.
|
||||
let body = null;
|
||||
if (request.method !== 'GET' && request.method !== 'HEAD') {
|
||||
body = request.body;
|
||||
}
|
||||
return new _request.NextRequest(request.url, {
|
||||
method: request.method,
|
||||
headers: (0, _utils.fromNodeOutgoingHttpHeaders)(request.headers),
|
||||
duplex: 'half',
|
||||
signal: request.request.signal,
|
||||
// geo
|
||||
// ip
|
||||
// nextConfig
|
||||
// body can not be passed if request was aborted
|
||||
// or we get a Request body was disturbed error
|
||||
...request.request.signal.aborted ? {} : {
|
||||
body
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
//# sourceMappingURL=next-request.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"sources":["../../../../../src/server/web/spec-extension/adapters/next-request.ts"],"sourcesContent":["import type { BaseNextRequest } from '../../../base-http'\nimport type { NodeNextRequest } from '../../../base-http/node'\nimport type { WebNextRequest } from '../../../base-http/web'\nimport type { Writable } from 'node:stream'\n\nimport { getRequestMeta } from '../../../request-meta'\nimport { fromNodeOutgoingHttpHeaders } from '../../utils'\nimport { NextRequest } from '../request'\nimport { isNodeNextRequest, isWebNextRequest } from '../../../base-http/helpers'\n\nexport const ResponseAbortedName = 'ResponseAborted'\nexport class ResponseAborted extends Error {\n public readonly name = ResponseAbortedName\n}\n\n/**\n * Creates an AbortController tied to the closing of a ServerResponse (or other\n * appropriate Writable).\n *\n * If the `close` event is fired before the `finish` event, then we'll send the\n * `abort` signal.\n */\nexport function createAbortController(response: Writable): AbortController {\n const controller = new AbortController()\n\n // If `finish` fires first, then `res.end()` has been called and the close is\n // just us finishing the stream on our side. If `close` fires first, then we\n // know the client disconnected before we finished.\n response.once('close', () => {\n if (response.writableFinished) return\n\n controller.abort(new ResponseAborted())\n })\n\n return controller\n}\n\n/**\n * Creates an AbortSignal tied to the closing of a ServerResponse (or other\n * appropriate Writable).\n *\n * This cannot be done with the request (IncomingMessage or Readable) because\n * the `abort` event will not fire if to data has been fully read (because that\n * will \"close\" the readable stream and nothing fires after that).\n */\nexport function signalFromNodeResponse(response: Writable): AbortSignal {\n const { errored, destroyed } = response\n if (errored || destroyed) {\n return AbortSignal.abort(errored ?? new ResponseAborted())\n }\n\n const { signal } = createAbortController(response)\n return signal\n}\n\nexport class NextRequestAdapter {\n public static fromBaseNextRequest(\n request: BaseNextRequest,\n signal: AbortSignal\n ): NextRequest {\n if (\n // The type check here ensures that `req` is correctly typed, and the\n // environment variable check provides dead code elimination.\n process.env.NEXT_RUNTIME === 'edge' &&\n isWebNextRequest(request)\n ) {\n return NextRequestAdapter.fromWebNextRequest(request)\n } else if (\n // The type check here ensures that `req` is correctly typed, and the\n // environment variable check provides dead code elimination.\n process.env.NEXT_RUNTIME !== 'edge' &&\n isNodeNextRequest(request)\n ) {\n return NextRequestAdapter.fromNodeNextRequest(request, signal)\n } else {\n throw new Error('Invariant: Unsupported NextRequest type')\n }\n }\n\n public static fromNodeNextRequest(\n request: NodeNextRequest,\n signal: AbortSignal\n ): NextRequest {\n // HEAD and GET requests can not have a body.\n let body: BodyInit | null = null\n if (request.method !== 'GET' && request.method !== 'HEAD' && request.body) {\n // @ts-expect-error - this is handled by undici, when streams/web land use it instead\n body = request.body\n }\n\n let url: URL\n if (request.url.startsWith('http')) {\n url = new URL(request.url)\n } else {\n // Grab the full URL from the request metadata.\n const base = getRequestMeta(request, 'initURL')\n if (!base || !base.startsWith('http')) {\n // Because the URL construction relies on the fact that the URL provided\n // is absolute, we need to provide a base URL. We can't use the request\n // URL because it's relative, so we use a dummy URL instead.\n url = new URL(request.url, 'http://n')\n } else {\n url = new URL(request.url, base)\n }\n }\n\n return new NextRequest(url, {\n method: request.method,\n headers: fromNodeOutgoingHttpHeaders(request.headers),\n duplex: 'half',\n signal,\n // geo\n // ip\n // nextConfig\n\n // body can not be passed if request was aborted\n // or we get a Request body was disturbed error\n ...(signal.aborted\n ? {}\n : {\n body,\n }),\n })\n }\n\n public static fromWebNextRequest(request: WebNextRequest): NextRequest {\n // HEAD and GET requests can not have a body.\n let body: ReadableStream | null = null\n if (request.method !== 'GET' && request.method !== 'HEAD') {\n body = request.body\n }\n\n return new NextRequest(request.url, {\n method: request.method,\n headers: fromNodeOutgoingHttpHeaders(request.headers),\n duplex: 'half',\n signal: request.request.signal,\n // geo\n // ip\n // nextConfig\n\n // body can not be passed if rLine truncated
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
export declare class ReflectAdapter {
|
||||
static get<T extends object>(target: T, prop: string | symbol, receiver: unknown): any;
|
||||
static set<T extends object>(target: T, prop: string | symbol, value: any, receiver: any): boolean;
|
||||
static has<T extends object>(target: T, prop: string | symbol): boolean;
|
||||
static deleteProperty<T extends object>(target: T, prop: string | symbol): boolean;
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
Object.defineProperty(exports, "ReflectAdapter", {
|
||||
enumerable: true,
|
||||
get: function() {
|
||||
return ReflectAdapter;
|
||||
}
|
||||
});
|
||||
class ReflectAdapter {
|
||||
static get(target, prop, receiver) {
|
||||
const value = Reflect.get(target, prop, receiver);
|
||||
if (typeof value === 'function') {
|
||||
return value.bind(target);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
static set(target, prop, value, receiver) {
|
||||
return Reflect.set(target, prop, value, receiver);
|
||||
}
|
||||
static has(target, prop) {
|
||||
return Reflect.has(target, prop);
|
||||
}
|
||||
static deleteProperty(target, prop) {
|
||||
return Reflect.deleteProperty(target, prop);
|
||||
}
|
||||
}
|
||||
|
||||
//# sourceMappingURL=reflect.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"sources":["../../../../../src/server/web/spec-extension/adapters/reflect.ts"],"sourcesContent":["export class ReflectAdapter {\n static get<T extends object>(\n target: T,\n prop: string | symbol,\n receiver: unknown\n ): any {\n const value = Reflect.get(target, prop, receiver)\n if (typeof value === 'function') {\n return value.bind(target)\n }\n\n return value\n }\n\n static set<T extends object>(\n target: T,\n prop: string | symbol,\n value: any,\n receiver: any\n ): boolean {\n return Reflect.set(target, prop, value, receiver)\n }\n\n static has<T extends object>(target: T, prop: string | symbol): boolean {\n return Reflect.has(target, prop)\n }\n\n static deleteProperty<T extends object>(\n target: T,\n prop: string | symbol\n ): boolean {\n return Reflect.deleteProperty(target, prop)\n }\n}\n"],"names":["ReflectAdapter","get","target","prop","receiver","value","Reflect","bind","set","has","deleteProperty"],"mappings":";;;;+BAAaA;;;eAAAA;;;AAAN,MAAMA;IACX,OAAOC,IACLC,MAAS,EACTC,IAAqB,EACrBC,QAAiB,EACZ;QACL,MAAMC,QAAQC,QAAQL,GAAG,CAACC,QAAQC,MAAMC;QACxC,IAAI,OAAOC,UAAU,YAAY;YAC/B,OAAOA,MAAME,IAAI,CAACL;QACpB;QAEA,OAAOG;IACT;IAEA,OAAOG,IACLN,MAAS,EACTC,IAAqB,EACrBE,KAAU,EACVD,QAAa,EACJ;QACT,OAAOE,QAAQE,GAAG,CAACN,QAAQC,MAAME,OAAOD;IAC1C;IAEA,OAAOK,IAAsBP,MAAS,EAAEC,IAAqB,EAAW;QACtE,OAAOG,QAAQG,GAAG,CAACP,QAAQC;IAC7B;IAEA,OAAOO,eACLR,MAAS,EACTC,IAAqB,EACZ;QACT,OAAOG,QAAQI,cAAc,CAACR,QAAQC;IACxC;AACF"}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
import { RequestCookies } from '../cookies';
|
||||
import { ResponseCookies } from '../cookies';
|
||||
import { type RequestStore } from '../../../app-render/work-unit-async-storage.external';
|
||||
export type { ResponseCookies };
|
||||
export type ReadonlyRequestCookies = Omit<RequestCookies, 'set' | 'clear' | 'delete'> & Pick<ResponseCookies, 'set' | 'delete'>;
|
||||
export declare class RequestCookiesAdapter {
|
||||
static seal(cookies: RequestCookies): ReadonlyRequestCookies;
|
||||
}
|
||||
export declare function getModifiedCookieValues(cookies: ResponseCookies): ResponseCookie[];
|
||||
export declare function appendMutableCookies(headers: Headers, mutableCookies: ResponseCookies): boolean;
|
||||
type ResponseCookie = NonNullable<ReturnType<InstanceType<typeof ResponseCookies>['get']>>;
|
||||
export declare class MutableRequestCookiesAdapter {
|
||||
static wrap(cookies: RequestCookies, onUpdateCookies?: (cookies: string[]) => void): ResponseCookies;
|
||||
}
|
||||
export declare function wrapWithMutableAccessCheck(responseCookies: ResponseCookies): ResponseCookies;
|
||||
export declare function areCookiesMutableInCurrentPhase(requestStore: RequestStore): boolean;
|
||||
export declare function responseCookiesToRequestCookies(responseCookies: ResponseCookies): RequestCookies;
|
||||
+212
@@ -0,0 +1,212 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
0 && (module.exports = {
|
||||
MutableRequestCookiesAdapter: null,
|
||||
ReadonlyRequestCookiesError: null,
|
||||
RequestCookiesAdapter: null,
|
||||
appendMutableCookies: null,
|
||||
areCookiesMutableInCurrentPhase: null,
|
||||
getModifiedCookieValues: null,
|
||||
responseCookiesToRequestCookies: null,
|
||||
wrapWithMutableAccessCheck: null
|
||||
});
|
||||
function _export(target, all) {
|
||||
for(var name in all)Object.defineProperty(target, name, {
|
||||
enumerable: true,
|
||||
get: all[name]
|
||||
});
|
||||
}
|
||||
_export(exports, {
|
||||
MutableRequestCookiesAdapter: function() {
|
||||
return MutableRequestCookiesAdapter;
|
||||
},
|
||||
ReadonlyRequestCookiesError: function() {
|
||||
return ReadonlyRequestCookiesError;
|
||||
},
|
||||
RequestCookiesAdapter: function() {
|
||||
return RequestCookiesAdapter;
|
||||
},
|
||||
appendMutableCookies: function() {
|
||||
return appendMutableCookies;
|
||||
},
|
||||
areCookiesMutableInCurrentPhase: function() {
|
||||
return areCookiesMutableInCurrentPhase;
|
||||
},
|
||||
getModifiedCookieValues: function() {
|
||||
return getModifiedCookieValues;
|
||||
},
|
||||
responseCookiesToRequestCookies: function() {
|
||||
return responseCookiesToRequestCookies;
|
||||
},
|
||||
wrapWithMutableAccessCheck: function() {
|
||||
return wrapWithMutableAccessCheck;
|
||||
}
|
||||
});
|
||||
const _cookies = require("../cookies");
|
||||
const _reflect = require("./reflect");
|
||||
const _workasyncstorageexternal = require("../../../app-render/work-async-storage.external");
|
||||
const _workunitasyncstorageexternal = require("../../../app-render/work-unit-async-storage.external");
|
||||
class ReadonlyRequestCookiesError extends Error {
|
||||
constructor(){
|
||||
super('Cookies can only be modified in a Server Action or Route Handler. Read more: https://nextjs.org/docs/app/api-reference/functions/cookies#options');
|
||||
}
|
||||
static callable() {
|
||||
throw new ReadonlyRequestCookiesError();
|
||||
}
|
||||
}
|
||||
class RequestCookiesAdapter {
|
||||
static seal(cookies) {
|
||||
return new Proxy(cookies, {
|
||||
get (target, prop, receiver) {
|
||||
switch(prop){
|
||||
case 'clear':
|
||||
case 'delete':
|
||||
case 'set':
|
||||
return ReadonlyRequestCookiesError.callable;
|
||||
default:
|
||||
return _reflect.ReflectAdapter.get(target, prop, receiver);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
const SYMBOL_MODIFY_COOKIE_VALUES = Symbol.for('next.mutated.cookies');
|
||||
function getModifiedCookieValues(cookies) {
|
||||
const modified = cookies[SYMBOL_MODIFY_COOKIE_VALUES];
|
||||
if (!modified || !Array.isArray(modified) || modified.length === 0) {
|
||||
return [];
|
||||
}
|
||||
return modified;
|
||||
}
|
||||
function appendMutableCookies(headers, mutableCookies) {
|
||||
const modifiedCookieValues = getModifiedCookieValues(mutableCookies);
|
||||
if (modifiedCookieValues.length === 0) {
|
||||
return false;
|
||||
}
|
||||
// Return a new response that extends the response with
|
||||
// the modified cookies as fallbacks. `res` cookies
|
||||
// will still take precedence.
|
||||
const resCookies = new _cookies.ResponseCookies(headers);
|
||||
const returnedCookies = resCookies.getAll();
|
||||
// Set the modified cookies as fallbacks.
|
||||
for (const cookie of modifiedCookieValues){
|
||||
resCookies.set(cookie);
|
||||
}
|
||||
// Set the original cookies as the final values.
|
||||
for (const cookie of returnedCookies){
|
||||
resCookies.set(cookie);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
class MutableRequestCookiesAdapter {
|
||||
static wrap(cookies, onUpdateCookies) {
|
||||
const responseCookies = new _cookies.ResponseCookies(new Headers());
|
||||
for (const cookie of cookies.getAll()){
|
||||
responseCookies.set(cookie);
|
||||
}
|
||||
let modifiedValues = [];
|
||||
const modifiedCookies = new Set();
|
||||
const updateResponseCookies = ()=>{
|
||||
// TODO-APP: change method of getting workStore
|
||||
const workStore = _workasyncstorageexternal.workAsyncStorage.getStore();
|
||||
if (workStore) {
|
||||
workStore.pathWasRevalidated = true;
|
||||
}
|
||||
const allCookies = responseCookies.getAll();
|
||||
modifiedValues = allCookies.filter((c)=>modifiedCookies.has(c.name));
|
||||
if (onUpdateCookies) {
|
||||
const serializedCookies = [];
|
||||
for (const cookie of modifiedValues){
|
||||
const tempCookies = new _cookies.ResponseCookies(new Headers());
|
||||
tempCookies.set(cookie);
|
||||
serializedCookies.push(tempCookies.toString());
|
||||
}
|
||||
onUpdateCookies(serializedCookies);
|
||||
}
|
||||
};
|
||||
const wrappedCookies = new Proxy(responseCookies, {
|
||||
get (target, prop, receiver) {
|
||||
switch(prop){
|
||||
// A special symbol to get the modified cookie values
|
||||
case SYMBOL_MODIFY_COOKIE_VALUES:
|
||||
return modifiedValues;
|
||||
// TODO: Throw error if trying to set a cookie after the response
|
||||
// headers have been set.
|
||||
case 'delete':
|
||||
return function(...args) {
|
||||
modifiedCookies.add(typeof args[0] === 'string' ? args[0] : args[0].name);
|
||||
try {
|
||||
target.delete(...args);
|
||||
return wrappedCookies;
|
||||
} finally{
|
||||
updateResponseCookies();
|
||||
}
|
||||
};
|
||||
case 'set':
|
||||
return function(...args) {
|
||||
modifiedCookies.add(typeof args[0] === 'string' ? args[0] : args[0].name);
|
||||
try {
|
||||
target.set(...args);
|
||||
return wrappedCookies;
|
||||
} finally{
|
||||
updateResponseCookies();
|
||||
}
|
||||
};
|
||||
default:
|
||||
return _reflect.ReflectAdapter.get(target, prop, receiver);
|
||||
}
|
||||
}
|
||||
});
|
||||
return wrappedCookies;
|
||||
}
|
||||
}
|
||||
function wrapWithMutableAccessCheck(responseCookies) {
|
||||
const wrappedCookies = new Proxy(responseCookies, {
|
||||
get (target, prop, receiver) {
|
||||
switch(prop){
|
||||
case 'delete':
|
||||
return function(...args) {
|
||||
ensureCookiesAreStillMutable('cookies().delete');
|
||||
target.delete(...args);
|
||||
return wrappedCookies;
|
||||
};
|
||||
case 'set':
|
||||
return function(...args) {
|
||||
ensureCookiesAreStillMutable('cookies().set');
|
||||
target.set(...args);
|
||||
return wrappedCookies;
|
||||
};
|
||||
default:
|
||||
return _reflect.ReflectAdapter.get(target, prop, receiver);
|
||||
}
|
||||
}
|
||||
});
|
||||
return wrappedCookies;
|
||||
}
|
||||
function areCookiesMutableInCurrentPhase(requestStore) {
|
||||
return requestStore.phase === 'action';
|
||||
}
|
||||
/** Ensure that cookies() starts throwing on mutation
|
||||
* if we changed phases and can no longer mutate.
|
||||
*
|
||||
* This can happen when going:
|
||||
* 'render' -> 'after'
|
||||
* 'action' -> 'render'
|
||||
* */ function ensureCookiesAreStillMutable(callingExpression) {
|
||||
const requestStore = (0, _workunitasyncstorageexternal.getExpectedRequestStore)(callingExpression);
|
||||
if (!areCookiesMutableInCurrentPhase(requestStore)) {
|
||||
// TODO: maybe we can give a more precise error message based on callingExpression?
|
||||
throw new ReadonlyRequestCookiesError();
|
||||
}
|
||||
}
|
||||
function responseCookiesToRequestCookies(responseCookies) {
|
||||
const requestCookies = new _cookies.RequestCookies(new Headers());
|
||||
for (const cookie of responseCookies.getAll()){
|
||||
requestCookies.set(cookie);
|
||||
}
|
||||
return requestCookies;
|
||||
}
|
||||
|
||||
//# sourceMappingURL=request-cookies.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"sources":["../../../../../src/server/web/spec-extension/adapters/request-cookies.ts"],"sourcesContent":["import { RequestCookies } from '../cookies'\n\nimport { ResponseCookies } from '../cookies'\nimport { ReflectAdapter } from './reflect'\nimport { workAsyncStorage } from '../../../app-render/work-async-storage.external'\nimport {\n getExpectedRequestStore,\n type RequestStore,\n} from '../../../app-render/work-unit-async-storage.external'\n\n/**\n * @internal\n */\nexport class ReadonlyRequestCookiesError extends Error {\n constructor() {\n super(\n 'Cookies can only be modified in a Server Action or Route Handler. Read more: https://nextjs.org/docs/app/api-reference/functions/cookies#options'\n )\n }\n\n public static callable() {\n throw new ReadonlyRequestCookiesError()\n }\n}\n\n// We use this to type some APIs but we don't construct instances directly\nexport type { ResponseCookies }\n\n// The `cookies()` API is a mix of request and response cookies. For `.get()` methods,\n// we want to return the request cookie if it exists. For mutative methods like `.set()`,\n// we want to return the response cookie.\nexport type ReadonlyRequestCookies = Omit<\n RequestCookies,\n 'set' | 'clear' | 'delete'\n> &\n Pick<ResponseCookies, 'set' | 'delete'>\n\nexport class RequestCookiesAdapter {\n public static seal(cookies: RequestCookies): ReadonlyRequestCookies {\n return new Proxy(cookies as any, {\n get(target, prop, receiver) {\n switch (prop) {\n case 'clear':\n case 'delete':\n case 'set':\n return ReadonlyRequestCookiesError.callable\n default:\n return ReflectAdapter.get(target, prop, receiver)\n }\n },\n })\n }\n}\n\nconst SYMBOL_MODIFY_COOKIE_VALUES = Symbol.for('next.mutated.cookies')\n\nexport function getModifiedCookieValues(\n cookies: ResponseCookies\n): ResponseCookie[] {\n const modified: ResponseCookie[] | undefined = (cookies as unknown as any)[\n SYMBOL_MODIFY_COOKIE_VALUES\n ]\n if (!modified || !Array.isArray(modified) || modified.length === 0) {\n return []\n }\n\n return modified\n}\n\ntype SetCookieArgs =\n | [key: string, value: string, cookie?: Partial<ResponseCookie>]\n | [options: ResponseCookie]\n\nexport function appendMutableCookies(\n headers: Headers,\n mutableCookies: ResponseCookies\n): boolean {\n const modifiedCookieValues = getModifiedCookieValues(mutableCookies)\n if (modifiedCookieValues.length === 0) {\n return false\n }\n\n // Return a new response that extends the response with\n // the modified cookies as fallbacks. `res` cookies\n // will still take precedence.\n const resCookies = new ResponseCookies(headers)\n const returnedCookies = resCookies.getAll()\n\n // Set the modified cookies as fallbacks.\n for (const cookie of modifiedCookieValues) {\n resCookies.set(cookie)\n }\n\n // Set the original cookies as the final values.\n for (const cookie of returnedCookies) {\n resCookies.set(cookie)\n }\n\n return true\n}\n\ntype ResponseCookie = NonNullable<\n ReturnType<InstanceType<typeof ResponseCookies>['get']>\n>\n\nexport class MutableRequestCookiesAdapter {\n public static wrap(\n cookies: RequestCookies,\n onUpdateCookies?: (cookies: string[]) => void\n ): ResponseCookies {\n const responseCookies = new ResponseCookies(new Headers())\n for (const cookie of cookies.getAll()) {\n responseCookies.set(cookie)\n }\n\n let modifiedValues: ResponseCookie[] = []\n const modifiedCookies = new Set<string>()\n const updateResponseCookies = () => {\n // TODO-APP: change method of getting workStore\n const workStore = workAsyncStorage.getStore()\n if (workStore) {\n workStore.pathWasRevalidated = true\n }\n\n const allCookies = responseCookies.getAll()\n modifiedValues = allCookies.filter((c) => modifiedCookies.has(c.name))\n if (onUpdateCookies) {\n const serializedCookies: string[] = []\n for (const cookie of modifiedValues) {\n const tempCookies = new ResponseCookies(new Headers())\n tempCookies.set(cookie)\n serializedCookies.push(tempCookies.toString())\n }\n\n onUpdateCookies(serializedCookies)\n }\n }\n\n const wrappedCookies = new Proxy(responseCookies, {\n get(target, prop, receiver) {\n switch (prop) {\n // A special symbol to get the modified cookie values\n case SYMBOL_MODIFY_COOKIE_VALUES:\n return modifiedValues\n\n // TODO: Throw error if trying to set a cookie after the response\n // headers have been set.\n case 'delete':\n return function (...args: [string] | [ResponseCookie]) {\n modifiedCookies.add(\n typeof args[0] === 'string' ? args[0] : args[0].name\n )\n try {\n target.delete(...args)\n return Line truncated
|
||||
Reference in new issue
Block a user