Skip to content

Commit a8b3d14

Browse files
author
Egor Manzhula
committed
feat: replaced crypto shim with separate entrypoints for node and browser environments
1 parent 8dd6c19 commit a8b3d14

9 files changed

Lines changed: 143 additions & 506 deletions

File tree

package-lock.json

Lines changed: 62 additions & 430 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
"name": "@namecheap/serialize-javascript",
33
"version": "0.0.0",
44
"description": "Serialize JavaScript to a superset of JSON that includes regular expressions and functions.",
5-
"main": "lib/index.js",
6-
"types": "lib/index.d.ts",
5+
"main": "./lib/index.js",
6+
"browser": "./lib/browser.js",
7+
"types": "./lib/index.d.ts",
78
"publishConfig": {
89
"access": "public"
910
},
@@ -41,7 +42,6 @@
4142
"mocha": "^10.7.3",
4243
"mochawesome": "^7.1.3",
4344
"nyc": "^17.1.0",
44-
"proxyquire": "^2.1.3",
4545
"semantic-release": "^24.1.3",
4646
"sinon": "^19.0.2",
4747
"source-map-support": "^0.5.21",

src/browser.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { Serializer } from './Serializer';
2+
import { generateUUID } from './generateUUID';
3+
4+
const uuid = generateUUID(window.crypto);
5+
const serializer = new Serializer(uuid);
6+
7+
const serialize = serializer.serialize.bind(serializer);
8+
9+
export { serialize };
10+
export default serialize;

src/crypto.ts

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import crypto from './crypto';
1+
import crypto from 'crypto';
22
import { Serializer } from './Serializer';
33
import { generateUUID } from './generateUUID';
44

test/unit/browser.spec.ts

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import sinon from 'sinon';
2+
import { expect } from 'chai';
3+
import * as generateUUIDModule from '../../src/generateUUID';
4+
import * as placeholderSerializerModule from '../../src/Serializer/PlaceholderSerializer';
5+
6+
describe('Browser entrypoint', () => {
7+
let serializeStub: sinon.SinonStub;
8+
let generateUUIDStub: sinon.SinonStub;
9+
let placeholderSerializerSpy: sinon.SinonSpy;
10+
let serializeFn;
11+
let cryptoMock = {};
12+
13+
before(() => {
14+
// Mock the `window` and `window.crypto` if they are not defined
15+
if (typeof global.window === 'undefined') {
16+
(global as any).window = {};
17+
}
18+
19+
Object.defineProperty(global.window, 'crypto', {
20+
value: cryptoMock,
21+
configurable: true,
22+
writable: true,
23+
});
24+
25+
generateUUIDStub = sinon.stub(generateUUIDModule, 'generateUUID');
26+
generateUUIDStub.returns('00000000000000000000000000000');
27+
28+
placeholderSerializerSpy = sinon.spy(placeholderSerializerModule, 'PlaceholderSerializer');
29+
30+
serializeStub = sinon.stub(placeholderSerializerModule.PlaceholderSerializer.prototype, 'serialize').returns('serialized-data');
31+
32+
const { serialize } = require('../../src/browser');
33+
serializeFn = serialize;
34+
});
35+
36+
after(() => {
37+
Object.defineProperty(global.window, 'crypto', {
38+
value: undefined,
39+
configurable: true,
40+
writable: true,
41+
});
42+
43+
sinon.restore();
44+
});
45+
46+
it('should invoke genereUUID function with window.crypto', () => {
47+
expect(generateUUIDStub.calledOnce).to.be.true;
48+
expect(generateUUIDStub.calledWith(cryptoMock)).to.be.true;
49+
});
50+
51+
it('should create PlaceholderSerializer instance with UUID returned by generateUUID', () => {
52+
expect(placeholderSerializerSpy.calledOnce).to.be.true;
53+
expect(placeholderSerializerSpy.calledWith('00000000000000000000000000000')).to.be.true;
54+
});
55+
56+
it('should invoke PlaceholderSerializer.serialize and return it\'s result', () => {
57+
const toSerialize = { test: 1}
58+
const serialized = serializeFn(toSerialize);
59+
60+
expect(serializeStub.calledOnce).to.be.true;
61+
expect(serializeStub.calledWith(toSerialize)).to.be.true;
62+
expect(serialized).to.be.equal('serialized-data');
63+
});
64+
});

test/unit/crypto.spec.ts

Lines changed: 0 additions & 58 deletions
This file was deleted.

test/unit/index.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import sinon from 'sinon';
22
import { expect } from 'chai';
3-
import crypto from '../../src/crypto';
3+
import crypto from 'crypto';
44
import * as generateUUIDModule from '../../src/generateUUID';
55
import * as placeholderSerializerModule from '../../src/Serializer/PlaceholderSerializer';
66

7-
describe('Entrypoint', () => {
7+
describe('Default entrypoint', () => {
88
let serializeStub: sinon.SinonStub;
99
let generateUUIDStub: sinon.SinonStub;
1010
let placeholderSerializerSpy: sinon.SinonSpy;

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"baseUrl": "./",
1212
"noImplicitAny": false
1313
},
14-
"include": ["src/index.ts"],
14+
"include": ["src/index.ts", "src/browser.ts"],
1515
"exclude": ["node_modules"]
1616
}
1717

0 commit comments

Comments
 (0)