Skip to content

Commit

Permalink
Merge pull request #7 from encrypit/refactor/module
Browse files Browse the repository at this point in the history
refactor(module): move `mjs` files from `src` to `module` directory
  • Loading branch information
remarkablemark authored Jun 15, 2023
2 parents 157d297 + e6a2c98 commit fc6b3b0
Show file tree
Hide file tree
Showing 13 changed files with 111 additions and 22 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ jobs:
- name: Run tests
run: npm run test:ci

- name: Run module tests
run: npm run test:module

- name: Codecov
uses: codecov/codecov-action@v3

Expand Down
1 change: 1 addition & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@

npm run lint:tsc
npm run test:ci
npm run test:module
npx lint-staged
9 changes: 0 additions & 9 deletions __tests__/__snapshots__/index.test.ts.snap

This file was deleted.

1 change: 1 addition & 0 deletions module/browser.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { digest } from '../lib/browser.js';
45 changes: 45 additions & 0 deletions module/browser.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import crypto from 'node:crypto';
import { before, describe, it } from 'node:test';

import assert from 'assert';

describe('browser', () => {
let digest;

before(async () => {
global.crypto = crypto;
digest = (await import('./browser.mjs')).digest;
});

it('exports "digest" function', () => {
assert.strictEqual(typeof digest, 'function');
});

it('hashes message with algorithm "SHA-1"', async () => {
assert.strictEqual(
await digest('SHA-1', ''),
'da39a3ee5e6b4b0d3255bfef95601890afd80709'
);
});

it('hashes message with algorithm "SHA-256"', async () => {
assert.strictEqual(
await digest('SHA-256', ''),
'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855'
);
});

it('hashes message with algorithm "SHA-384"', async () => {
assert.strictEqual(
await digest('SHA-384', ''),
'38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b'
);
});

it('hashes message with algorithm "SHA-512"', async () => {
assert.strictEqual(
await digest('SHA-384', ''),
'38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b'
);
});
});
1 change: 1 addition & 0 deletions module/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { digest } from '../lib/index.js';
39 changes: 39 additions & 0 deletions module/index.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { describe, it } from 'node:test';

import assert from 'assert';

import { digest } from './index.mjs';

describe('index', () => {
it('exports "digest" function', () => {
assert.strictEqual(typeof digest, 'function');
});

it('hashes message with algorithm "SHA-1"', async () => {
assert.strictEqual(
await digest('SHA-1', ''),
'da39a3ee5e6b4b0d3255bfef95601890afd80709'
);
});

it('hashes message with algorithm "SHA-256"', async () => {
assert.strictEqual(
await digest('SHA-256', ''),
'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855'
);
});

it('hashes message with algorithm "SHA-384"', async () => {
assert.strictEqual(
await digest('SHA-384', ''),
'38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b'
);
});

it('hashes message with algorithm "SHA-512"', async () => {
assert.strictEqual(
await digest('SHA-384', ''),
'38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b'
);
});
});
14 changes: 8 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,30 @@
"author": "Mark <mark@remarkablemark.org>",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"module": "lib/index.mjs",
"module": "module/index.mjs",
"exports": {
"types": "./lib/index.d.ts",
"import": "./lib/index.mjs",
"import": "./module/index.mjs",
"require": "./lib/index.js"
},
"browser": {
"./lib/index.js": "./lib/browser.js",
"./lib/index.mjs": "./lib/browser.mjs"
"./module/index.mjs": "./module/browser.mjs"
},
"scripts": {
"build": "tsc && cp src/*.mjs lib",
"build": "tsc",
"build:watch": "tsc --watch",
"clean": "rm -rf coverage docs lib",
"docs": "typedoc",
"lint": "eslint --ignore-path .gitignore --ext .js,.ts,.tsx .",
"lint": "eslint --ignore-path .gitignore --ext .js,.mjs,.ts .",
"lint:fix": "npm run lint -- --fix",
"lint:tsc": "tsc --noEmit",
"postinstall": "husky install",
"postpublish": "pinst --enable",
"prepublishOnly": "pinst --disable && npm run lint && npm run lint:tsc && npm run test:ci && npm run clean && npm run build",
"test": "jest",
"test:ci": "CI=true jest --ci --colors --coverage",
"test:module": "npm run build && node --test module",
"test:watch": "jest --watch"
},
"repository": {
Expand Down Expand Up @@ -61,7 +62,8 @@
"typescript": "5.1.3"
},
"files": [
"lib/"
"lib/",
"module/"
],
"license": "MIT"
}
9 changes: 9 additions & 0 deletions src/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`digest hashes message with algorithm "SHA-1" 1`] = `"da39a3ee5e6b4b0d3255bfef95601890afd80709"`;

exports[`digest hashes message with algorithm "SHA-256" 1`] = `"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"`;

exports[`digest hashes message with algorithm "SHA-384" 1`] = `"38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b"`;

exports[`digest hashes message with algorithm "SHA-512" 1`] = `"cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e"`;
1 change: 0 additions & 1 deletion src/browser.mjs

This file was deleted.

1 change: 0 additions & 1 deletion src/index.mjs

This file was deleted.

6 changes: 2 additions & 4 deletions __tests__/index.test.ts → src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import { digest } from '../src/index.ts';
import { digest } from './index';

describe('digest', () => {
it.each(['SHA-1', 'SHA-256', 'SHA-384', 'SHA-512'] as const)(
'hashes message with algorithm %s',
'hashes message with algorithm "%s"',
async (algorithm) => {
expect(await digest(algorithm, '')).toMatchSnapshot();
}
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
"strict": true,
"outDir": "lib"
},
"include": ["src"]
"include": ["src"],
"exclude": ["node_modules", "src/**/*.test.ts"]
}

0 comments on commit fc6b3b0

Please sign in to comment.