Skip to content

Commit

Permalink
feat: replaced crypto shim with separate entrypoints for node and bro…
Browse files Browse the repository at this point in the history
…wser environments
  • Loading branch information
Egor Manzhula committed Nov 13, 2024
1 parent 8dd6c19 commit 566aedf
Show file tree
Hide file tree
Showing 9 changed files with 145 additions and 504 deletions.
492 changes: 62 additions & 430 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
"version": "0.0.0",
"description": "Serialize JavaScript to a superset of JSON that includes regular expressions and functions.",
"main": "lib/index.js",
"browser": "lib/browser.js",
"types": "lib/index.d.ts",
"exports": {
"default": "./lib/index.js",
"browser": "./lib/browser.js"
},
"publishConfig": {
"access": "public"
},
Expand Down Expand Up @@ -41,7 +46,6 @@
"mocha": "^10.7.3",
"mochawesome": "^7.1.3",
"nyc": "^17.1.0",
"proxyquire": "^2.1.3",
"semantic-release": "^24.1.3",
"sinon": "^19.0.2",
"source-map-support": "^0.5.21",
Expand Down
10 changes: 10 additions & 0 deletions src/browser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Serializer } from './Serializer';
import { generateUUID } from './generateUUID';

const uuid = generateUUID(window.crypto);
const serializer = new Serializer(uuid);

const serialize = serializer.serialize.bind(serializer);

export { serialize };
export default serialize;
11 changes: 0 additions & 11 deletions src/crypto.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import crypto from './crypto';
import crypto from 'crypto';
import { Serializer } from './Serializer';
import { generateUUID } from './generateUUID';

Expand Down
64 changes: 64 additions & 0 deletions test/unit/browser.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import sinon from 'sinon';
import { expect } from 'chai';
import * as generateUUIDModule from '../../src/generateUUID';
import * as placeholderSerializerModule from '../../src/Serializer/PlaceholderSerializer';

describe('Browser entrypoint', () => {
let serializeStub: sinon.SinonStub;
let generateUUIDStub: sinon.SinonStub;
let placeholderSerializerSpy: sinon.SinonSpy;
let serializeFn;
let cryptoMock = {};

before(() => {
// Mock the `window` and `window.crypto` if they are not defined
if (typeof global.window === 'undefined') {
(global as any).window = {};
}

Object.defineProperty(global.window, 'crypto', {
value: cryptoMock,
configurable: true,
writable: true,
});

generateUUIDStub = sinon.stub(generateUUIDModule, 'generateUUID');
generateUUIDStub.returns('00000000000000000000000000000');

placeholderSerializerSpy = sinon.spy(placeholderSerializerModule, 'PlaceholderSerializer');

serializeStub = sinon.stub(placeholderSerializerModule.PlaceholderSerializer.prototype, 'serialize').returns('serialized-data');

const { serialize } = require('../../src/browser');
serializeFn = serialize;
});

after(() => {
Object.defineProperty(global.window, 'crypto', {
value: undefined,
configurable: true,
writable: true,
});

sinon.restore();
});

it('should invoke genereUUID function with window.crypto', () => {
expect(generateUUIDStub.calledOnce).to.be.true;
expect(generateUUIDStub.calledWith(cryptoMock)).to.be.true;
});

it('should create PlaceholderSerializer instance with UUID returned by generateUUID', () => {
expect(placeholderSerializerSpy.calledOnce).to.be.true;
expect(placeholderSerializerSpy.calledWith('00000000000000000000000000000')).to.be.true;
});

it('should invoke PlaceholderSerializer.serialize and return it\'s result', () => {
const toSerialize = { test: 1}
const serialized = serializeFn(toSerialize);

expect(serializeStub.calledOnce).to.be.true;
expect(serializeStub.calledWith(toSerialize)).to.be.true;
expect(serialized).to.be.equal('serialized-data');
});
});
58 changes: 0 additions & 58 deletions test/unit/crypto.spec.ts

This file was deleted.

4 changes: 2 additions & 2 deletions test/unit/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import sinon from 'sinon';
import { expect } from 'chai';
import crypto from '../../src/crypto';
import crypto from 'crypto';
import * as generateUUIDModule from '../../src/generateUUID';
import * as placeholderSerializerModule from '../../src/Serializer/PlaceholderSerializer';

describe('Entrypoint', () => {
describe('Default entrypoint', () => {
let serializeStub: sinon.SinonStub;
let generateUUIDStub: sinon.SinonStub;
let placeholderSerializerSpy: sinon.SinonSpy;
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"baseUrl": "./",
"noImplicitAny": false
},
"include": ["src/index.ts"],
"include": ["src/index.ts", "src/browser.ts"],
"exclude": ["node_modules"]
}

0 comments on commit 566aedf

Please sign in to comment.