Skip to content

Commit

Permalink
cjs + esm
Browse files Browse the repository at this point in the history
  • Loading branch information
dominikzogg committed Jul 25, 2023
1 parent fd98f58 commit 944856a
Show file tree
Hide file tree
Showing 12 changed files with 59 additions and 402 deletions.
File renamed without changes.
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ A minimal static file handler for chubbyts-http-types.
## Requirements

* node: 16
* [@chubbyts/chubbyts-http-error][2]: ^2.2.1
* [@chubbyts/chubbyts-http-types][3]: ^1.1.1
* [@chubbyts/chubbyts-http-error][2]: ^2.3.0
* [@chubbyts/chubbyts-http-types][3]: ^1.2.2

## Installation

Through [NPM](https://www.npmjs.com) as [@chubbyts/chubbyts-static-file][1].

```ts
npm i @chubbyts/chubbyts-static-file@^1.2.1
npm i @chubbyts/chubbyts-static-file@^2.0.0
```

## Usage
Expand All @@ -45,7 +45,13 @@ import { createGetRoute } from '@chubbyts/chubbyts-framework/dist/router/route';
const responseFactory: ResponseFactory = ...;
const streamFromFileFactory: StreamFromFileFactory = ...;

const handler = createStaticFileHandler(responseFactory, streamFromFileFactory, '/path/to/public/directory');
const handler = createStaticFileHandler(
responseFactory,
streamFromFileFactory,
'/path/to/public/directory',
(await import('../src/mimetypes')).default, // typescript / ecmascript module
// require('../src/mimetypes').default, // commonjs (cjs)
);

// for example as a fallback route matching everything
const route = createGetRoute({
Expand Down
34 changes: 22 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "@chubbyts/chubbyts-static-file",
"version": "1.2.1",
"type": "module",
"version": "2.0.0",
"description": "A minimal static file handler for chubbyts-http-types.",
"keywords": [
"chubbyts",
Expand All @@ -11,14 +12,14 @@
"license": "MIT",
"repository": "chubbyts/chubbyts-static-file",
"scripts": {
"build": "rm -rf ./dist && tsc -b ./tsconfig.types.json ./tsconfig.esm.json ./tsconfig.cjs.json && node ./rename-commonjs.js",
"cs-fix": "prettier --write src tests",
"cs": "prettier --check src tests",
"infection": "stryker run",
"lint-fix": "eslint src tests --fix",
"lint": "eslint src tests",
"test": "jest",
"infection": "stryker run",
"build": "rm -Rf dist && tsc",
"prepare": "npm run build"
"prepare": "npm run build",
"test": "jest"
},
"jest": {
"preset": "ts-jest",
Expand All @@ -41,20 +42,29 @@
"files": [
"dist"
],
"exports": {
"./*": {
"types": "./*.d.ts",
"require": "./*.cjs",
"import": "./*.js",
"default": "./*.js"
}
},
"engines": {
"node": ">=16"
},
"dependencies": {
"@chubbyts/chubbyts-http-error": "^2.2.1",
"@chubbyts/chubbyts-http-types": "^1.1.1"
"@chubbyts/chubbyts-http-error": "^2.3.0",
"@chubbyts/chubbyts-http-types": "^1.2.2"
},
"devDependencies": {
"@chubbyts/chubbyts-eslint": "^1.1.2",
"@chubbyts/chubbyts-function-mock": "^1.3.6",
"@stryker-mutator/core": "^7.1.0",
"@stryker-mutator/jest-runner": "^7.1.0",
"@chubbyts/chubbyts-eslint": "^2.0.2",
"@chubbyts/chubbyts-function-mock": "^1.4.0",
"@chubbyts/chubbyts-packaging": "^1.0.3",
"@stryker-mutator/core": "^7.1.1",
"@stryker-mutator/jest-runner": "^7.1.1",
"@types/jest": "^29.5.3",
"@types/node": "^20.4.1",
"@types/node": "^20.4.5",
"jest": "^29.6.1",
"prettier": "^3.0.0",
"ts-jest": "^29.1.1",
Expand Down
1 change: 1 addition & 0 deletions rename-commonjs.js
3 changes: 1 addition & 2 deletions src/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,8 @@ export const createStaticFileHandler = (
responseFactory: ResponseFactory,
streamFromFileFactory: StreamFromFileFactory,
publicDirectory: string,
mimeTypes: MimeTypes,
hashAlgorithm = 'md5',
// eslint-disable-next-line @typescript-eslint/no-var-requires
mimeTypes: MimeTypes = require('./mimetypes').default,
): Handler => {
assertHashAlgorithm(hashAlgorithm);

Expand Down
39 changes: 0 additions & 39 deletions src/middleware.ts

This file was deleted.

27 changes: 21 additions & 6 deletions tests/handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ describe('handler', () => {
const [streamFromFileFactory, streamFromFileFactoryMocks] = useFunctionMock<StreamFromFileFactory>([]);

try {
createStaticFileHandler(responseFactory, streamFromFileFactory, '/unknown', 'some-algo');
createStaticFileHandler(responseFactory, streamFromFileFactory, '/unknown', new Map(), 'some-algo');
throw new Error('Missing error');
} catch (e) {
expect(e).toBeInstanceOf(Error);
Expand All @@ -92,7 +92,7 @@ describe('handler', () => {

mkdirSync(publicDirectory, { recursive: true });

const handler = createStaticFileHandler(responseFactory, streamFromFileFactory, publicDirectory);
const handler = createStaticFileHandler(responseFactory, streamFromFileFactory, publicDirectory, new Map());

try {
await handler(request);
Expand Down Expand Up @@ -121,7 +121,7 @@ describe('handler', () => {
const [responseFactory, responseFactoryMocks] = useFunctionMock<ResponseFactory>([]);
const [streamFromFileFactory, streamFromFileFactoryMocks] = useFunctionMock<StreamFromFileFactory>([]);

const handler = createStaticFileHandler(responseFactory, streamFromFileFactory, publicDirectory);
const handler = createStaticFileHandler(responseFactory, streamFromFileFactory, publicDirectory, new Map());

try {
await handler(request);
Expand Down Expand Up @@ -165,7 +165,12 @@ describe('handler', () => {
},
]);

const handler = createStaticFileHandler(responseFactory, streamFromFileFactory, publicDirectory);
const handler = createStaticFileHandler(
responseFactory,
streamFromFileFactory,
publicDirectory,
(await import('../src/mimetypes')).default,
);

const handlerResponse = await handler(request);

Expand Down Expand Up @@ -213,7 +218,12 @@ describe('handler', () => {

const [streamFromFileFactory, streamFromFileFactoryMocks] = useFunctionMock<StreamFromFileFactory>([]);

const handler = createStaticFileHandler(responseFactory, streamFromFileFactory, publicDirectory);
const handler = createStaticFileHandler(
responseFactory,
streamFromFileFactory,
publicDirectory,
(await import('../src/mimetypes')).default,
);

const handlerResponse = await handler(request);

Expand Down Expand Up @@ -260,7 +270,12 @@ describe('handler', () => {
},
]);

const handler = createStaticFileHandler(responseFactory, streamFromFileFactory, publicDirectory);
const handler = createStaticFileHandler(
responseFactory,
streamFromFileFactory,
publicDirectory,
(await import('../src/mimetypes')).default,
);

const handlerResponse = await handler(request);

Expand Down
Loading

0 comments on commit 944856a

Please sign in to comment.