@sveltejs/adapter-netlify 1.0.0-next.6 → 1.0.0-next.62

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,327 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ require('./shims.js');
6
+ var _0SERVER = require('0SERVER');
7
+ require('node:http');
8
+ require('node:https');
9
+ require('node:zlib');
10
+ require('node:stream');
11
+ require('node:util');
12
+ require('node:url');
13
+ require('net');
14
+ require('crypto');
15
+
16
+ var setCookie = {exports: {}};
17
+
18
+ var defaultParseOptions = {
19
+ decodeValues: true,
20
+ map: false,
21
+ silent: false,
22
+ };
23
+
24
+ function isNonEmptyString(str) {
25
+ return typeof str === "string" && !!str.trim();
26
+ }
27
+
28
+ function parseString(setCookieValue, options) {
29
+ var parts = setCookieValue.split(";").filter(isNonEmptyString);
30
+ var nameValue = parts.shift().split("=");
31
+ var name = nameValue.shift();
32
+ var value = nameValue.join("="); // everything after the first =, joined by a "=" if there was more than one part
33
+
34
+ options = options
35
+ ? Object.assign({}, defaultParseOptions, options)
36
+ : defaultParseOptions;
37
+
38
+ try {
39
+ value = options.decodeValues ? decodeURIComponent(value) : value; // decode cookie value
40
+ } catch (e) {
41
+ console.error(
42
+ "set-cookie-parser encountered an error while decoding a cookie with value '" +
43
+ value +
44
+ "'. Set options.decodeValues to false to disable this feature.",
45
+ e
46
+ );
47
+ }
48
+
49
+ var cookie = {
50
+ name: name, // grab everything before the first =
51
+ value: value,
52
+ };
53
+
54
+ parts.forEach(function (part) {
55
+ var sides = part.split("=");
56
+ var key = sides.shift().trimLeft().toLowerCase();
57
+ var value = sides.join("=");
58
+ if (key === "expires") {
59
+ cookie.expires = new Date(value);
60
+ } else if (key === "max-age") {
61
+ cookie.maxAge = parseInt(value, 10);
62
+ } else if (key === "secure") {
63
+ cookie.secure = true;
64
+ } else if (key === "httponly") {
65
+ cookie.httpOnly = true;
66
+ } else if (key === "samesite") {
67
+ cookie.sameSite = value;
68
+ } else {
69
+ cookie[key] = value;
70
+ }
71
+ });
72
+
73
+ return cookie;
74
+ }
75
+
76
+ function parse(input, options) {
77
+ options = options
78
+ ? Object.assign({}, defaultParseOptions, options)
79
+ : defaultParseOptions;
80
+
81
+ if (!input) {
82
+ if (!options.map) {
83
+ return [];
84
+ } else {
85
+ return {};
86
+ }
87
+ }
88
+
89
+ if (input.headers && input.headers["set-cookie"]) {
90
+ // fast-path for node.js (which automatically normalizes header names to lower-case
91
+ input = input.headers["set-cookie"];
92
+ } else if (input.headers) {
93
+ // slow-path for other environments - see #25
94
+ var sch =
95
+ input.headers[
96
+ Object.keys(input.headers).find(function (key) {
97
+ return key.toLowerCase() === "set-cookie";
98
+ })
99
+ ];
100
+ // warn if called on a request-like object with a cookie header rather than a set-cookie header - see #34, 36
101
+ if (!sch && input.headers.cookie && !options.silent) {
102
+ console.warn(
103
+ "Warning: set-cookie-parser appears to have been called on a request object. It is designed to parse Set-Cookie headers from responses, not Cookie headers from requests. Set the option {silent: true} to suppress this warning."
104
+ );
105
+ }
106
+ input = sch;
107
+ }
108
+ if (!Array.isArray(input)) {
109
+ input = [input];
110
+ }
111
+
112
+ options = options
113
+ ? Object.assign({}, defaultParseOptions, options)
114
+ : defaultParseOptions;
115
+
116
+ if (!options.map) {
117
+ return input.filter(isNonEmptyString).map(function (str) {
118
+ return parseString(str, options);
119
+ });
120
+ } else {
121
+ var cookies = {};
122
+ return input.filter(isNonEmptyString).reduce(function (cookies, str) {
123
+ var cookie = parseString(str, options);
124
+ cookies[cookie.name] = cookie;
125
+ return cookies;
126
+ }, cookies);
127
+ }
128
+ }
129
+
130
+ /*
131
+ Set-Cookie header field-values are sometimes comma joined in one string. This splits them without choking on commas
132
+ that are within a single set-cookie field-value, such as in the Expires portion.
133
+
134
+ This is uncommon, but explicitly allowed - see https://tools.ietf.org/html/rfc2616#section-4.2
135
+ Node.js does this for every header *except* set-cookie - see https://github.com/nodejs/node/blob/d5e363b77ebaf1caf67cd7528224b651c86815c1/lib/_http_incoming.js#L128
136
+ React Native's fetch does this for *every* header, including set-cookie.
137
+
138
+ Based on: https://github.com/google/j2objc/commit/16820fdbc8f76ca0c33472810ce0cb03d20efe25
139
+ Credits to: https://github.com/tomball for original and https://github.com/chrusart for JavaScript implementation
140
+ */
141
+ function splitCookiesString(cookiesString) {
142
+ if (Array.isArray(cookiesString)) {
143
+ return cookiesString;
144
+ }
145
+ if (typeof cookiesString !== "string") {
146
+ return [];
147
+ }
148
+
149
+ var cookiesStrings = [];
150
+ var pos = 0;
151
+ var start;
152
+ var ch;
153
+ var lastComma;
154
+ var nextStart;
155
+ var cookiesSeparatorFound;
156
+
157
+ function skipWhitespace() {
158
+ while (pos < cookiesString.length && /\s/.test(cookiesString.charAt(pos))) {
159
+ pos += 1;
160
+ }
161
+ return pos < cookiesString.length;
162
+ }
163
+
164
+ function notSpecialChar() {
165
+ ch = cookiesString.charAt(pos);
166
+
167
+ return ch !== "=" && ch !== ";" && ch !== ",";
168
+ }
169
+
170
+ while (pos < cookiesString.length) {
171
+ start = pos;
172
+ cookiesSeparatorFound = false;
173
+
174
+ while (skipWhitespace()) {
175
+ ch = cookiesString.charAt(pos);
176
+ if (ch === ",") {
177
+ // ',' is a cookie separator if we have later first '=', not ';' or ','
178
+ lastComma = pos;
179
+ pos += 1;
180
+
181
+ skipWhitespace();
182
+ nextStart = pos;
183
+
184
+ while (pos < cookiesString.length && notSpecialChar()) {
185
+ pos += 1;
186
+ }
187
+
188
+ // currently special character
189
+ if (pos < cookiesString.length && cookiesString.charAt(pos) === "=") {
190
+ // we found cookies separator
191
+ cookiesSeparatorFound = true;
192
+ // pos is inside the next cookie, so back up and return it.
193
+ pos = nextStart;
194
+ cookiesStrings.push(cookiesString.substring(start, lastComma));
195
+ start = pos;
196
+ } else {
197
+ // in param ',' or param separator ';',
198
+ // we continue from that comma
199
+ pos = lastComma + 1;
200
+ }
201
+ } else {
202
+ pos += 1;
203
+ }
204
+ }
205
+
206
+ if (!cookiesSeparatorFound || pos >= cookiesString.length) {
207
+ cookiesStrings.push(cookiesString.substring(start, cookiesString.length));
208
+ }
209
+ }
210
+
211
+ return cookiesStrings;
212
+ }
213
+
214
+ setCookie.exports = parse;
215
+ setCookie.exports.parse = parse;
216
+ setCookie.exports.parseString = parseString;
217
+ var splitCookiesString_1 = setCookie.exports.splitCookiesString = splitCookiesString;
218
+
219
+ /**
220
+ * Splits headers into two categories: single value and multi value
221
+ * @param {Headers} headers
222
+ * @returns {{
223
+ * headers: Record<string, string>,
224
+ * multiValueHeaders: Record<string, string[]>
225
+ * }}
226
+ */
227
+ function split_headers(headers) {
228
+ /** @type {Record<string, string>} */
229
+ const h = {};
230
+
231
+ /** @type {Record<string, string[]>} */
232
+ const m = {};
233
+
234
+ headers.forEach((value, key) => {
235
+ if (key === 'set-cookie') {
236
+ m[key] = splitCookiesString_1(value);
237
+ } else {
238
+ h[key] = value;
239
+ }
240
+ });
241
+
242
+ return {
243
+ headers: h,
244
+ multiValueHeaders: m
245
+ };
246
+ }
247
+
248
+ /**
249
+ * @param {import('@sveltejs/kit').SSRManifest} manifest
250
+ * @returns {import('@netlify/functions').Handler}
251
+ */
252
+ function init(manifest) {
253
+ const server = new _0SERVER.Server(manifest);
254
+
255
+ return async (event, context) => {
256
+ const response = await server.respond(to_request(event), {
257
+ platform: { context },
258
+ getClientAddress() {
259
+ return event.headers['x-nf-client-connection-ip'];
260
+ }
261
+ });
262
+
263
+ const partial_response = {
264
+ statusCode: response.status,
265
+ ...split_headers(response.headers)
266
+ };
267
+
268
+ if (!is_text(response.headers.get('content-type'))) {
269
+ // Function responses should be strings (or undefined), and responses with binary
270
+ // content should be base64 encoded and set isBase64Encoded to true.
271
+ // https://github.com/netlify/functions/blob/main/src/function/response.ts
272
+ return {
273
+ ...partial_response,
274
+ isBase64Encoded: true,
275
+ body: Buffer.from(await response.arrayBuffer()).toString('base64')
276
+ };
277
+ }
278
+
279
+ return {
280
+ ...partial_response,
281
+ body: await response.text()
282
+ };
283
+ };
284
+ }
285
+
286
+ /**
287
+ * @param {import('@netlify/functions').HandlerEvent} event
288
+ * @returns {Request}
289
+ */
290
+ function to_request(event) {
291
+ const { httpMethod, headers, rawUrl, body, isBase64Encoded } = event;
292
+
293
+ /** @type {RequestInit} */
294
+ const init = {
295
+ method: httpMethod,
296
+ headers: new Headers(headers)
297
+ };
298
+
299
+ if (httpMethod !== 'GET' && httpMethod !== 'HEAD') {
300
+ const encoding = isBase64Encoded ? 'base64' : 'utf-8';
301
+ init.body = typeof body === 'string' ? Buffer.from(body, encoding) : body;
302
+ }
303
+
304
+ return new Request(rawUrl, init);
305
+ }
306
+
307
+ const text_types = new Set([
308
+ 'application/xml',
309
+ 'application/json',
310
+ 'application/x-www-form-urlencoded',
311
+ 'multipart/form-data'
312
+ ]);
313
+
314
+ /**
315
+ * Decides how the body should be parsed based on its mime type
316
+ *
317
+ * @param {string | undefined | null} content_type The `content-type` header of a request/response.
318
+ * @returns {boolean}
319
+ */
320
+ function is_text(content_type) {
321
+ if (!content_type) return true; // defaults to json
322
+ const type = content_type.split(';')[0].toLowerCase(); // get the mime type
323
+
324
+ return type.startsWith('text/') || type.endsWith('+xml') || text_types.has(type);
325
+ }
326
+
327
+ exports.init = init;