Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix memory leak #419 #425 #431

Merged
merged 4 commits into from
Dec 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions packages/universal-cookie/src/Cookies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,16 @@ export default class Cookies {
}
}

private _checkChanges(newCookies: { [name: string]: Cookie }) {
private _checkChanges(previousCookies: { [name: string]: Cookie }) {
const names = new Set(
Object.keys(newCookies).concat(Object.keys(this.cookies)),
Object.keys(previousCookies).concat(Object.keys(this.cookies)),
);

names.forEach((name) => {
if (newCookies[name] !== this.cookies[name]) {
if (previousCookies[name] !== this.cookies[name]) {
this._emitChange({
name,
value: readCookie(newCookies[name]),
value: readCookie(this.cookies[name]),
});
}
});
Expand Down Expand Up @@ -136,7 +136,7 @@ export default class Cookies {
public addChangeListener(callback: CookieChangeListener) {
this.changeListeners.push(callback);

if (this.changeListeners.length === 1) {
if (this.HAS_DOCUMENT_COOKIE && this.changeListeners.length === 1) {
if (typeof window === 'object' && 'cookieStore' in window) {
(window.cookieStore as any).addEventListener('change', this.update);
} else {
Expand All @@ -151,7 +151,7 @@ export default class Cookies {
this.changeListeners.splice(idx, 1);
}

if (this.changeListeners.length === 0) {
if (this.HAS_DOCUMENT_COOKIE && this.changeListeners.length === 0) {
if (typeof window === 'object' && 'cookieStore' in window) {
(window.cookieStore as any).removeEventListener('change', this.update);
} else {
Expand Down
52 changes: 43 additions & 9 deletions packages/universal-cookie/src/__tests__/Cookies-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { cleanCookies } from '../utils';
describe('Cookies', () => {
beforeEach(() => {
cleanCookies();
global.TEST_HAS_DOCUMENT_COOKIE = undefined;
});

describe('constructor()', () => {
Expand Down Expand Up @@ -125,8 +126,8 @@ describe('Cookies', () => {
cookies.addChangeListener(onChange);
cookies.set('test', 'meow', { path: '/' });

expect(onChange).toBeCalledTimes(1);
expect(onChange).toBeCalledWith({
expect(onChange).toHaveBeenCalledTimes(1);
expect(onChange).toHaveBeenCalledWith({
name: 'test',
value: 'meow',
options: {
Expand All @@ -135,15 +136,48 @@ describe('Cookies', () => {
});
});

it('detect if the cookie was externally changed in the browser', async () => {
const cookies = new Cookies();

const onChange = jest.fn();
cookies.addChangeListener(onChange);

document.cookie = `test="${JSON.stringify({ test: true })}"`;

await new Promise((r) => setTimeout(r, 500));

expect(onChange).toHaveBeenCalledTimes(1);
expect(onChange).toHaveBeenCalledWith({
name: 'test',
value: {
test: true,
},
});
});

it('ignore if the cookie was externally changed in the server', async () => {
global.TEST_HAS_DOCUMENT_COOKIE = false;
const cookies = new Cookies();

const onChange = jest.fn();
cookies.addChangeListener(onChange);

document.cookie = 'test=meow';

await new Promise((r) => setTimeout(r, 500));

expect(onChange).not.toHaveBeenCalled();
});

it('keeps the value as original object', () => {
const cookies = new Cookies();

const onChange = jest.fn();
cookies.addChangeListener(onChange);
cookies.set('test', [0, 1, 2], { path: '/' });

expect(onChange).toBeCalledTimes(1);
expect(onChange).toBeCalledWith({
expect(onChange).toHaveBeenCalledTimes(1);
expect(onChange).toHaveBeenCalledWith({
name: 'test',
value: [0, 1, 2],
options: {
Expand All @@ -159,7 +193,7 @@ describe('Cookies', () => {
cookies.addChangeListener(onChange);
cookies.set('test', 'test');

expect(onChange).toBeCalledWith({
expect(onChange).toHaveBeenCalledWith({
name: 'test',
value: 'test',
options: {
Expand All @@ -175,7 +209,7 @@ describe('Cookies', () => {
cookies.addChangeListener(onChange);
cookies.set('test', 'test', { path: '/woof' });

expect(onChange).toBeCalledWith({
expect(onChange).toHaveBeenCalledWith({
name: 'test',
value: 'test',
options: {
Expand All @@ -191,8 +225,8 @@ describe('Cookies', () => {
cookies.addChangeListener(onChange);
cookies.remove('test', { path: '/' });

expect(onChange).toBeCalledTimes(1);
expect(onChange).toBeCalledWith({
expect(onChange).toHaveBeenCalledTimes(1);
expect(onChange).toHaveBeenCalledWith({
name: 'test',
value: undefined,
options: {
Expand All @@ -211,7 +245,7 @@ describe('Cookies', () => {
cookies.removeChangeListener(onChange);

cookies.remove('test', 'boom!');
expect(onChange).not.toBeCalled();
expect(onChange).not.toHaveBeenCalled();
});
});
});
5 changes: 5 additions & 0 deletions packages/universal-cookie/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import * as cookie from 'cookie';
import { Cookie, CookieGetOptions } from './types';

export function hasDocumentCookie() {
const testingValue = (global as any).TEST_HAS_DOCUMENT_COOKIE;
if (typeof testingValue === 'boolean') {
return testingValue;
}

// Can we get/set cookies on document.cookie?
return typeof document === 'object' && typeof document.cookie === 'string';
}
Expand Down
2 changes: 1 addition & 1 deletion setup-tests.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ArrayBuffer, TextDecoder, TextEncoder, Uint8Array } from 'util';
import '@testing-library/jest-dom';
import { TextEncoder, TextDecoder } from 'util';

global.TextEncoder = TextEncoder;
global.TextDecoder = TextDecoder;
Expand Down