Skip to content

Commit

Permalink
[ci] format
Browse files Browse the repository at this point in the history
  • Loading branch information
natemoo-re authored and astrobot-houston committed Jun 6, 2023
1 parent 4929332 commit a1144f7
Show file tree
Hide file tree
Showing 7 changed files with 234 additions and 238 deletions.
2 changes: 1 addition & 1 deletion packages/astro/src/core/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ function isInPagesDir(file: URL, config: AstroConfig): boolean {

function isInjectedRoute(file: URL, settings: AstroSettings) {
for (const route of settings.injectedRoutes) {
if(file.toString().endsWith(route.entryPoint)) return true;
if (file.toString().endsWith(route.entryPoint)) return true;
}
return false;
}
Expand Down
7 changes: 2 additions & 5 deletions packages/astro/test/astro-scripts.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,11 @@ describe('Scripts (hoisted and not)', () => {
name: 'test-script-injection-with-injected-route',
hooks: {
'astro:config:setup': ({ injectRoute, injectScript }) => {
injectScript(
'page',
`import '/src/scripts/something.js';`
);
injectScript('page', `import '/src/scripts/something.js';`);
injectRoute({ pattern: 'injected-route', entryPoint: 'src/external-page.astro' });
},
},
}
},
],
vite: {
build: {
Expand Down
48 changes: 25 additions & 23 deletions packages/integrations/node/src/createOutgoingHttpHeaders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,40 @@ import type { OutgoingHttpHeaders } from 'http';
/**
* Takes in a nullable WebAPI Headers object and produces a NodeJS OutgoingHttpHeaders object suitable for usage
* with ServerResponse.writeHead(..) or ServerResponse.setHeader(..)
*
*
* @param webHeaders WebAPI Headers object
* @returns NodeJS OutgoingHttpHeaders object with multiple set-cookie handled as an array of values
*/
export const createOutgoingHttpHeaders = (webHeaders: Headers | undefined | null): OutgoingHttpHeaders | undefined => {
if (!webHeaders) {
return undefined;
}
export const createOutgoingHttpHeaders = (
webHeaders: Headers | undefined | null
): OutgoingHttpHeaders | undefined => {
if (!webHeaders) {
return undefined;
}

// re-type to access Header.getSetCookie()
const headers = webHeaders as HeadersWithGetSetCookie;
// re-type to access Header.getSetCookie()
const headers = webHeaders as HeadersWithGetSetCookie;

// at this point, a multi-value'd set-cookie header is invalid (it was concatenated as a single CSV, which is not valid for set-cookie)
const nodeHeaders: OutgoingHttpHeaders = Object.fromEntries(headers.entries());
// at this point, a multi-value'd set-cookie header is invalid (it was concatenated as a single CSV, which is not valid for set-cookie)
const nodeHeaders: OutgoingHttpHeaders = Object.fromEntries(headers.entries());

if (Object.keys(nodeHeaders).length === 0) {
return undefined;
}
if (Object.keys(nodeHeaders).length === 0) {
return undefined;
}

// if there is > 1 set-cookie header, we have to fix it to be an array of values
if (headers.has('set-cookie')) {
const cookieHeaders = headers.getSetCookie();
if (cookieHeaders.length > 1) {
// the Headers.entries() API already normalized all header names to lower case so we can safely index this as 'set-cookie'
nodeHeaders['set-cookie'] = cookieHeaders;
}
}
// if there is > 1 set-cookie header, we have to fix it to be an array of values
if (headers.has('set-cookie')) {
const cookieHeaders = headers.getSetCookie();
if (cookieHeaders.length > 1) {
// the Headers.entries() API already normalized all header names to lower case so we can safely index this as 'set-cookie'
nodeHeaders['set-cookie'] = cookieHeaders;
}
}

return nodeHeaders;
return nodeHeaders;
};

interface HeadersWithGetSetCookie extends Headers {
// the @astrojs/webapi polyfill makes this available (as of [email protected]), but tsc doesn't pick it up on the built-in Headers type from DOM lib
getSetCookie(): string[];
// the @astrojs/webapi polyfill makes this available (as of [email protected]), but tsc doesn't pick it up on the built-in Headers type from DOM lib
getSetCookie(): string[];
}
4 changes: 2 additions & 2 deletions packages/integrations/node/src/http-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ export function createServer(
let location: string;
if (req.url!.includes('?')) {
const [url = '', search] = req.url!.split('?');
location = `${url}/?${search}`
location = `${url}/?${search}`;
} else {
location = req.url + '/'
location = req.url + '/';
}

res.statusCode = 301;
Expand Down
14 changes: 7 additions & 7 deletions packages/integrations/node/src/nodeMiddleware.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { NodeApp } from 'astro/app/node';
import type { IncomingMessage, ServerResponse } from 'http';
import type { Readable } from 'stream';
import { createOutgoingHttpHeaders } from './createOutgoingHttpHeaders';
import { responseIterator } from './response-iterator';
import type { Options } from './types';
import { createOutgoingHttpHeaders } from './createOutgoingHttpHeaders';

export default function (app: NodeApp, mode: Options['mode']) {
return async function (
Expand Down Expand Up @@ -45,16 +45,16 @@ async function writeWebResponse(app: NodeApp, res: ServerResponse, webResponse:

if (app.setCookieHeaders) {
const setCookieHeaders: Array<string> = Array.from(app.setCookieHeaders(webResponse));

if (setCookieHeaders.length) {
for (const setCookieHeader of setCookieHeaders) {
webResponse.headers.append('set-cookie', setCookieHeader);
}
for (const setCookieHeader of setCookieHeaders) {
webResponse.headers.append('set-cookie', setCookieHeader);
}
}
}

const nodeHeaders = createOutgoingHttpHeaders(headers);
res.writeHead(status, nodeHeaders);
const nodeHeaders = createOutgoingHttpHeaders(headers);
res.writeHead(status, nodeHeaders);
if (webResponse.body) {
try {
for await (const chunk of responseIterator(webResponse) as unknown as Readable) {
Expand Down
128 changes: 63 additions & 65 deletions packages/integrations/node/test/createOutgoingHttpHeaders.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,76 +3,74 @@ import { expect } from 'chai';
import { createOutgoingHttpHeaders } from '../dist/createOutgoingHttpHeaders.js';

describe('createOutgoingHttpHeaders', () => {
it('undefined input headers', async () => {
const result = createOutgoingHttpHeaders(undefined);
expect(result).to.equal(undefined);
});

it('undefined input headers', async () => {
const result = createOutgoingHttpHeaders(undefined);
expect(result).to.equal(undefined);
});
it('null input headers', async () => {
const result = createOutgoingHttpHeaders(undefined);
expect(result).to.equal(undefined);
});

it('null input headers', async () => {
const result = createOutgoingHttpHeaders(undefined);
expect(result).to.equal(undefined);
});
it('Empty Headers', async () => {
const headers = new Headers();
const result = createOutgoingHttpHeaders(headers);
expect(result).to.equal(undefined);
});

it('Empty Headers', async () => {
const headers = new Headers();
const result = createOutgoingHttpHeaders(headers);
expect(result).to.equal(undefined);
});
it('Headers with single key', async () => {
const headers = new Headers();
headers.append('x-test', 'hello world');
const result = createOutgoingHttpHeaders(headers);
expect(result).to.deep.equal({ 'x-test': 'hello world' });
});

it('Headers with single key', async () => {
const headers = new Headers();
headers.append('x-test', 'hello world');
const result = createOutgoingHttpHeaders(headers);
expect(result).to.deep.equal({ 'x-test': 'hello world' });
});
it('Headers with multiple keys', async () => {
const headers = new Headers();
headers.append('x-test1', 'hello');
headers.append('x-test2', 'world');
const result = createOutgoingHttpHeaders(headers);
expect(result).to.deep.equal({ 'x-test1': 'hello', 'x-test2': 'world' });
});

it('Headers with multiple keys', async () => {
const headers = new Headers();
headers.append('x-test1', 'hello');
headers.append('x-test2', 'world');
const result = createOutgoingHttpHeaders(headers);
expect(result).to.deep.equal({ 'x-test1': 'hello', 'x-test2': 'world' });
});
it('Headers with multiple values (not set-cookie)', async () => {
const headers = new Headers();
headers.append('x-test', 'hello');
headers.append('x-test', 'world');
const result = createOutgoingHttpHeaders(headers);
expect(result).to.deep.equal({ 'x-test': 'hello, world' });
});

it('Headers with multiple values (not set-cookie)', async () => {
const headers = new Headers();
headers.append('x-test', 'hello');
headers.append('x-test', 'world');
const result = createOutgoingHttpHeaders(headers);
expect(result).to.deep.equal({ 'x-test': 'hello, world' });
});
it('Headers with multiple values (set-cookie special case)', async () => {
const headers = new Headers();
headers.append('set-cookie', 'hello');
headers.append('set-cookie', 'world');
const result = createOutgoingHttpHeaders(headers);
expect(result).to.deep.equal({ 'set-cookie': ['hello', 'world'] });
});

it('Headers with multiple values (set-cookie special case)', async () => {
const headers = new Headers();
headers.append('set-cookie', 'hello');
headers.append('set-cookie', 'world');
const result = createOutgoingHttpHeaders(headers);
expect(result).to.deep.equal({ 'set-cookie': ['hello', 'world'] });
});
it('Headers with multiple values (set-cookie case handling)', async () => {
const headers = new Headers();
headers.append('Set-cookie', 'hello');
headers.append('Set-Cookie', 'world');
const result = createOutgoingHttpHeaders(headers);
expect(result).to.deep.equal({ 'set-cookie': ['hello', 'world'] });
});

it('Headers with multiple values (set-cookie case handling)', async () => {
const headers = new Headers();
headers.append('Set-cookie', 'hello');
headers.append('Set-Cookie', 'world');
const result = createOutgoingHttpHeaders(headers);
expect(result).to.deep.equal({ 'set-cookie': ['hello', 'world'] });
});

it('Headers with all use cases', async () => {
const headers = new Headers();
headers.append('x-single', 'single');
headers.append('x-triple', 'one');
headers.append('x-triple', 'two');
headers.append('x-triple', 'three');
headers.append('Set-cookie', 'hello');
headers.append('Set-Cookie', 'world');
const result = createOutgoingHttpHeaders(headers);
expect(result).to.deep.equal({
'x-single': 'single',
'x-triple': 'one, two, three',
'set-cookie': ['hello', 'world'],
});
});

});
it('Headers with all use cases', async () => {
const headers = new Headers();
headers.append('x-single', 'single');
headers.append('x-triple', 'one');
headers.append('x-triple', 'two');
headers.append('x-triple', 'three');
headers.append('Set-cookie', 'hello');
headers.append('Set-Cookie', 'world');
const result = createOutgoingHttpHeaders(headers);
expect(result).to.deep.equal({
'x-single': 'single',
'x-triple': 'one, two, three',
'set-cookie': ['hello', 'world'],
});
});
});
Loading

0 comments on commit a1144f7

Please sign in to comment.