Skip to content

Commit

Permalink
feat: add constructor options to configure max sizes (#586)
Browse files Browse the repository at this point in the history
* test: use the recommended sink constructor option
* feat: add constructor options to configure max sizes
* test: add test for max package size
* fix: update eik/core
  • Loading branch information
wkillerud authored Aug 8, 2024
1 parent fa309aa commit 37a60a0
Show file tree
Hide file tree
Showing 16 changed files with 94 additions and 34 deletions.
24 changes: 21 additions & 3 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import * as utils from './utils.js';
* @property {import('@eik/sink').default} [customSink] [Deprecated] Use sink instead
* @property {string} [aliasCacheControl]
* @property {string} [notFoundCacheControl="public, max-age=5"]
* @property {number} [pkgMaxFileSize=10000000] The limit in bytes before PUT /pkg/ starts returning 413 Content Too Large
* @property {number} [mapMaxFileSize=1000000] The limit in bytes before PUT /map/ starts returning 413 Content Too Large
*/

const EikService = class EikService {
Expand All @@ -26,7 +28,13 @@ const EikService = class EikService {
*/
constructor(options = {}) {
let { sink, logger } = options;
const { customSink, notFoundCacheControl, aliasCacheControl } = options;
const {
customSink,
notFoundCacheControl,
aliasCacheControl,
pkgMaxFileSize,
mapMaxFileSize,
} = options;
this._notFoundCacheControl =
notFoundCacheControl || 'public, max-age=5';

Expand Down Expand Up @@ -90,9 +98,19 @@ const EikService = class EikService {
});
this._pkgLog = new eik.http.PkgLog({ organizations, sink, logger });
this._pkgGet = new eik.http.PkgGet({ organizations, sink, logger });
this._pkgPut = new eik.http.PkgPut({ organizations, sink, logger });
this._pkgPut = new eik.http.PkgPut({
organizations,
sink,
logger,
pkgMaxFileSize,
});
this._mapGet = new eik.http.MapGet({ organizations, sink, logger });
this._mapPut = new eik.http.MapPut({ organizations, sink, logger });
this._mapPut = new eik.http.MapPut({
organizations,
sink,
logger,
mapMaxFileSize,
});

const mergeStreams = (...streams) => {
const str = new PassThrough({ objectMode: true });
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
},
"homepage": "https://github.com/eik-lib/service#readme",
"dependencies": {
"@eik/core": "1.3.53",
"@eik/core": "1.3.56",
"@eik/sink": "1.2.5",
"@eik/sink-file-system": "1.0.1",
"@eik/sink-memory": "1.1.1",
Expand Down
2 changes: 1 addition & 1 deletion test/400.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const FIXTURE_PKG = path.resolve(__dirname, '../fixtures/archive.tgz');

tap.test('400 - GET request with non-existing hostname', async (t) => {
const sink = new Sink();
const service = new Server({ customSink: sink });
const service = new Server({ sink });

const app = Fastify({
ignoreTrailingSlash: true,
Expand Down
4 changes: 2 additions & 2 deletions test/404.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Server from '../lib/main.js';

tap.test('404 - POST request to non existing pathname', async (t) => {
const sink = new Sink();
const service = new Server({ customSink: sink });
const service = new Server({ sink });

const app = Fastify({
ignoreTrailingSlash: true,
Expand Down Expand Up @@ -38,7 +38,7 @@ tap.test('404 - POST request to non existing pathname', async (t) => {

tap.test('404 - GET request to non existing pathname', async (t) => {
const sink = new Sink();
const service = new Server({ customSink: sink });
const service = new Server({ sink });

const app = Fastify({
ignoreTrailingSlash: true,
Expand Down
2 changes: 1 addition & 1 deletion test/alias.map.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const FIXTURE_MAP_B = path.resolve(__dirname, '../fixtures/import-map-b.json');

tap.beforeEach(async (t) => {
const sink = new Sink();
const service = new Server({ customSink: sink });
const service = new Server({ sink });

const app = Fastify({
ignoreTrailingSlash: true,
Expand Down
2 changes: 1 addition & 1 deletion test/alias.npm.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ tap.cleanSnapshot = (s) => {

tap.beforeEach(async (t) => {
const sink = new Sink();
const service = new Server({ customSink: sink });
const service = new Server({ sink });

const app = Fastify({
ignoreTrailingSlash: true,
Expand Down
2 changes: 1 addition & 1 deletion test/alias.pkg.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ tap.cleanSnapshot = (s) => {

tap.beforeEach(async (t) => {
const sink = new Sink();
const service = new Server({ customSink: sink });
const service = new Server({ sink });

const app = Fastify({
ignoreTrailingSlash: true,
Expand Down
4 changes: 2 additions & 2 deletions test/auth.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Server from '../lib/main.js';

tap.test('auth - authenticate - legal "key" value', async (t) => {
const sink = new Sink();
const service = new Server({ customSink: sink });
const service = new Server({ sink });

const app = Fastify({
ignoreTrailingSlash: true,
Expand Down Expand Up @@ -43,7 +43,7 @@ tap.test('auth - authenticate - legal "key" value', async (t) => {

tap.test('auth - authenticate - illegal "key" value', async (t) => {
const sink = new Sink();
const service = new Server({ customSink: sink });
const service = new Server({ sink });

const app = Fastify({
ignoreTrailingSlash: true,
Expand Down
2 changes: 1 addition & 1 deletion test/http.cache.control.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ tap.cleanSnapshot = (s) => {

tap.beforeEach(async (t) => {
const sink = new Sink();
const service = new Server({ customSink: sink });
const service = new Server({ sink });

const app = Fastify({
ignoreTrailingSlash: true,
Expand Down
24 changes: 12 additions & 12 deletions test/http.etag.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import Server from '../lib/main.js';

tap.test('ETag - pkg:get - ETag and "If-None-Match" is matching', async (t) => {
const sink = new Sink();
const service = new Server({ customSink: sink });
const service = new Server({ sink });

const app = Fastify({
ignoreTrailingSlash: true,
Expand Down Expand Up @@ -64,7 +64,7 @@ tap.test(
async (t) => {
const sink = new Sink();
const service = new Server({
customSink: sink,
sink,
});

const app = Fastify({
Expand Down Expand Up @@ -123,7 +123,7 @@ tap.test(
async (t) => {
const sink = new Sink();
const service = new Server({
customSink: sink,
sink,
});

const app = Fastify({
Expand Down Expand Up @@ -176,7 +176,7 @@ tap.test(
/*
tap.test('ETag - pkg:get - ETags is configured to not be set', async t => {
const sink = new Sink();
const service = new Server({ customSink: sink, port: 0, config: { etag: false }, logger: false });
const service = new Server({ sink, port: 0, config: { etag: false }, logger: false });
const address = await service.start();
const url = `${address}/pkg/fuzz/8.4.1/main/index.js`;
Expand Down Expand Up @@ -213,7 +213,7 @@ tap.test('ETag - pkg:get - ETags is configured to not be set', async t => {

tap.test('ETag - pkg:log - ETag and "If-None-Match" is matching', async (t) => {
const sink = new Sink();
const service = new Server({ customSink: sink });
const service = new Server({ sink });

const app = Fastify({
ignoreTrailingSlash: true,
Expand Down Expand Up @@ -266,7 +266,7 @@ tap.test(
async (t) => {
const sink = new Sink();
const service = new Server({
customSink: sink,
sink,
});

const app = Fastify({
Expand Down Expand Up @@ -325,7 +325,7 @@ tap.test(
async (t) => {
const sink = new Sink();
const service = new Server({
customSink: sink,
sink,
});

const app = Fastify({
Expand Down Expand Up @@ -378,7 +378,7 @@ tap.test(
/*
tap.test('ETag - pkg:log - ETags is configured to not be set', async t => {
const sink = new Sink();
const service = new Server({ customSink: sink, port: 0, config: { etag: false }, logger: false });
const service = new Server({ sink, port: 0, config: { etag: false }, logger: false });
const address = await service.start();
const url = `${address}/pkg/fuzz/8.4.1`;
Expand Down Expand Up @@ -415,7 +415,7 @@ tap.test('ETag - pkg:log - ETags is configured to not be set', async t => {

tap.test('ETag - map:get - ETag and "If-None-Match" is matching', async (t) => {
const sink = new Sink();
const service = new Server({ customSink: sink });
const service = new Server({ sink });

const app = Fastify({
ignoreTrailingSlash: true,
Expand Down Expand Up @@ -468,7 +468,7 @@ tap.test(
async (t) => {
const sink = new Sink();
const service = new Server({
customSink: sink,
sink,
});

const app = Fastify({
Expand Down Expand Up @@ -527,7 +527,7 @@ tap.test(
async (t) => {
const sink = new Sink();
const service = new Server({
customSink: sink,
sink,
});

const app = Fastify({
Expand Down Expand Up @@ -580,7 +580,7 @@ tap.test(
/*
tap.test('ETag - map:get - ETags is configured to not be set', async t => {
const sink = new Sink();
const service = new Server({ customSink: sink, port: 0, config: { etag: false }, logger: false });
const service = new Server({ sink, port: 0, config: { etag: false }, logger: false });
const address = await service.start();
const url = `${address}/map/buzz/4.2.2`;
Expand Down
2 changes: 1 addition & 1 deletion test/http.override.cache.control.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ tap.cleanSnapshot = (s) => {
tap.beforeEach(async (t) => {
const sink = new Sink();
const service = new Server({
customSink: sink,
sink,
aliasCacheControl: 'public, max-age=600',
});

Expand Down
2 changes: 1 addition & 1 deletion test/http.query.params.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ tap.cleanSnapshot = (s) => {

tap.beforeEach(async (t) => {
const sink = new Sink();
const service = new Server({ customSink: sink });
const service = new Server({ sink });

const app = Fastify({
ignoreTrailingSlash: true,
Expand Down
2 changes: 1 addition & 1 deletion test/map.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const FIXTURE_MAP = path.resolve(__dirname, '../fixtures/import-map.json');

tap.beforeEach(async (t) => {
const sink = new Sink();
const service = new Server({ customSink: sink });
const service = new Server({ sink });

const app = Fastify({
ignoreTrailingSlash: true,
Expand Down
2 changes: 1 addition & 1 deletion test/npm.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ tap.cleanSnapshot = (s) => {

tap.beforeEach(async (t) => {
const sink = new Sink();
const service = new Server({ customSink: sink });
const service = new Server({ sink });

const app = Fastify({
ignoreTrailingSlash: true,
Expand Down
8 changes: 4 additions & 4 deletions test/pkg-put-write-integrity.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ tap.test(
return Math.floor(Math.random() * max) + min;
};

const service = new Server({ customSink: sink });
const service = new Server({ sink });

const app = Fastify({
ignoreTrailingSlash: true,
Expand Down Expand Up @@ -96,7 +96,7 @@ tap.test(
return Math.floor(Math.random() * max) + min;
};

const service = new Server({ customSink: sink });
const service = new Server({ sink });

const app = Fastify({
ignoreTrailingSlash: true,
Expand Down Expand Up @@ -146,7 +146,7 @@ tap.test(
return Math.floor(Math.random() * max) + min;
};

const service = new Server({ customSink: sink });
const service = new Server({ sink });

const app = Fastify({
ignoreTrailingSlash: true,
Expand Down Expand Up @@ -196,7 +196,7 @@ tap.test(
return Math.floor(Math.random() * max) + min;
};

const service = new Server({ customSink: sink });
const service = new Server({ sink });

const app = Fastify({
ignoreTrailingSlash: true,
Expand Down
44 changes: 43 additions & 1 deletion test/pkg.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ tap.cleanSnapshot = (s) => {

tap.beforeEach(async (t) => {
const sink = new Sink();
const service = new Server({ customSink: sink });
const service = new Server({ sink });

const app = Fastify({
ignoreTrailingSlash: true,
Expand Down Expand Up @@ -372,3 +372,45 @@ tap.test('packages - get package versions - non scoped', async (t) => {
'on GET, response should match snapshot',
);
});

tap.test('packages - configure max size', async (t) => {
const sink = new Sink();
const service = new Server({ sink, pkgMaxFileSize: 10 });

const app = Fastify({
ignoreTrailingSlash: true,
});
t.after(() => app.close());

app.register(service.api());

const address = await app.listen({ port: 0, host: '127.0.0.1' });

const loginFormData = new FormData();
loginFormData.append('key', 'change_me');
const res = await fetch(`${address}/auth/login`, {
method: 'POST',
body: loginFormData,
headers: loginFormData.getHeaders(),
});

const { token } = /** @type {{ token: string }} */ (await res.json());
const headers = { Authorization: `Bearer ${token}` };

const formData = new FormData();
formData.append('package', fs.createReadStream(FIXTURE_PKG));

// PUT files on server
const uploaded = await fetch(`${address}/pkg/fuzz/8.4.1/`, {
method: 'PUT',
body: formData,
headers: { ...headers, ...formData.getHeaders() },
redirect: 'manual',
});

t.equal(
uploaded.status,
413,
'Expected to be told that the content is too large',
);
});

0 comments on commit 37a60a0

Please sign in to comment.