Skip to content

Commit

Permalink
✨ Export core server util (#976)
Browse files Browse the repository at this point in the history
Adds a small util to encapsulate creating a new server and exports it with other core utils.

Some code was adjusted to make use of the new util which makes it covered by existing tests.
  • Loading branch information
wwilsman authored Jun 27, 2022
1 parent 0cb1612 commit e12f15e
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 10 deletions.
11 changes: 5 additions & 6 deletions packages/core/src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import fs from 'fs';
import path from 'path';
import { createRequire } from 'module';
import logger from '@percy/logger';
import { getPackageJSON } from './utils.js';
import Server from './server.js';
import { getPackageJSON, Server } from './utils.js';

// need require.resolve until import.meta.resolve can be transpiled
export const PERCY_DOM = createRequire(import.meta.url).resolve('@percy/dom');
Expand All @@ -12,7 +11,7 @@ export const PERCY_DOM = createRequire(import.meta.url).resolve('@percy/dom');
export function createPercyServer(percy, port) {
let pkg = getPackageJSON(import.meta.url);

return new Server({ port })
return Server.createServer({ port })
// facilitate logger websocket connections
.websocket('/(logger)?', ws => {
ws.addEventListener('message', ({ data }) => {
Expand Down Expand Up @@ -88,8 +87,8 @@ export function createPercyServer(percy, port) {

// Create a static server instance with an automatic sitemap
export function createStaticServer(options) {
let { serve, port, baseUrl = '/', ...opts } = options;
let server = new Server({ port }).serve(baseUrl, serve, opts);
let { serve: dir, baseUrl = '/' } = options;
let server = Server.createServer(options);

// used when generating an automatic sitemap
let toURL = Server.createRewriter((
Expand All @@ -105,7 +104,7 @@ export function createStaticServer(options) {
// include automatic sitemap route
server.route('get', '/sitemap.xml', async (req, res) => {
let { default: glob } = await import('fast-glob');
let files = await glob('**/*.html', { cwd: serve, fs });
let files = await glob('**/*.html', { cwd: dir, fs });

return res.send(200, 'application/xml', [
'<?xml version="1.0" encoding="UTF-8"?>',
Expand Down
13 changes: 12 additions & 1 deletion packages/core/src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,18 @@ function parseByteRange(range, size) {
if (start >= 0 && start < end) return { start, end };
}

// include ServerError and createRewriter as static properties
// shorthand function for creating a new server with specific options
export function createServer(options = {}) {
let { serve, port, baseUrl = '/', ...opts } = options;
let server = new Server({ port });

return serve ? (
server.serve(baseUrl, serve, opts)
) : server;
}

// include some exports as static properties
Server.Error = ServerError;
Server.createRewriter = createRewriter;
Server.createServer = createServer;
export default Server;
5 changes: 5 additions & 0 deletions packages/core/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ export {
hostnameMatches
} from '@percy/client/utils';

export {
Server,
createServer
} from './server.js';

// Returns the hostname portion of a URL.
export function hostname(url) {
return new URL(url).hostname;
Expand Down
8 changes: 5 additions & 3 deletions packages/core/test/unit/server.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe('Unit / Server', () => {
}

beforeEach(async () => {
server = new Server({ port: 8000 });
server = Server.createServer({ port: 8000 });
await mockfs();
});

Expand All @@ -37,7 +37,7 @@ describe('Unit / Server', () => {
});

it('does not include the port without a default when not listening', () => {
expect(new Server().address()).toEqual('http://localhost');
expect(Server.createServer().address()).toEqual('http://localhost');
});
});

Expand All @@ -56,7 +56,9 @@ describe('Unit / Server', () => {

it('rejects when an error occurs trying to listen', async () => {
await server.listen();
await expectAsync(new Server().listen(server.port)).toBeRejected();
await expectAsync(
Server.createServer().listen(server.port)
).toBeRejected();
});
});

Expand Down

0 comments on commit e12f15e

Please sign in to comment.