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

Add cookie chunker methods to ssr #662

Merged
merged 2 commits into from
Oct 31, 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
5 changes: 5 additions & 0 deletions .changeset/cool-seas-talk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@supabase/ssr': patch
---

Add cookie chunker methods and expose them to the SDK
5 changes: 5 additions & 0 deletions .changeset/new-dodos-joke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@supabase/auth-helpers-shared': patch
---

Fix chunkCount to get the correct number of chunks
2 changes: 1 addition & 1 deletion packages/shared/src/chunker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function createChunks(key: string, value: string, chunkSize?: number): Ch
const re = chunkSize !== undefined ? createChunkRegExp(chunkSize) : MAX_CHUNK_REGEXP;

// check the length of the string to work out if it should be returned or chunked
const chunkCount = Math.ceil(value.length / MAX_CHUNK_SIZE);
const chunkCount = Math.ceil(value.length / (chunkSize ?? MAX_CHUNK_SIZE));

if (chunkCount === 1) {
return [{ name: key, value }];
Expand Down
7 changes: 5 additions & 2 deletions packages/ssr/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
],
"scripts": {
"lint": "tsc",
"build": "tsup"
"build": "tsup",
"test": "vitest run",
"test:watch": "vitest"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -41,7 +43,8 @@
"@types/cookie": "^0.5.1",
"@types/ramda": "^0.29.3",
"tsconfig": "workspace:*",
"tsup": "^6.7.0"
"tsup": "^6.7.0",
"vitest": "^0.34.6"
},
"peerDependencies": {
"@supabase/supabase-js": "^2.33.1"
Expand Down
94 changes: 94 additions & 0 deletions packages/ssr/src/utils/chunker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
interface Chunk {
name: string;
value: string;
}

function createChunkRegExp(chunkSize: number) {
return new RegExp('.{1,' + chunkSize + '}', 'g');
}

const MAX_CHUNK_SIZE = 3600;
const MAX_CHUNK_REGEXP = createChunkRegExp(MAX_CHUNK_SIZE);

/**
* create chunks from a string and return an array of object
*/
export function createChunks(key: string, value: string, chunkSize?: number): Chunk[] {
const re = chunkSize !== undefined ? createChunkRegExp(chunkSize) : MAX_CHUNK_REGEXP;

// check the length of the string to work out if it should be returned or chunked
const chunkCount = Math.ceil(value.length / (chunkSize ?? MAX_CHUNK_SIZE));

if (chunkCount === 1) {
return [{ name: key, value }];
}

const chunks: Chunk[] = [];
// split string into a array based on the regex
const values = value.match(re);
values?.forEach((value, i) => {
const name: string = `${key}.${i}`;
chunks.push({ name, value });
});

return chunks;
}

// Get fully constructed chunks
export function combineChunks(
key: string,
retrieveChunk: (name: string) => string | null | undefined = () => {
return null;
}
) {
const value = retrieveChunk(key);

// pkce code verifier
if (key.endsWith('-code-verifier') && value) {
return value;
}

if (value) {
return value;
}

let values: string[] = [];
for (let i = 0; ; i++) {
const chunkName = `${key}.${i}`;
const chunk = retrieveChunk(chunkName);

if (!chunk) {
break;
}

values.push(chunk);
}

return values.length ? values.join('') : null;
}

export function deleteChunks(
key: string,
retrieveChunk: (name: string) => string | null | undefined = () => {
return null;
},
removeChunk: (name: string) => void = () => {}
) {
const value = retrieveChunk(key);

if (value) {
removeChunk(key);
return;
}

for (let i = 0; ; i++) {
const chunkName = `${key}.${i}`;
const chunk = retrieveChunk(chunkName);

if (!chunk) {
break;
}

removeChunk(chunkName);
}
}
1 change: 1 addition & 0 deletions packages/ssr/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './helpers';
export * from './constants';
export * from './chunker';
46 changes: 46 additions & 0 deletions packages/ssr/tests/chunker.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { describe, expect, it } from 'vitest';
import { combineChunks, createChunks } from '../src/utils/chunker';
import { CHUNK_STRING } from './helper';

describe('chunker', () => {
it('should not chunk and return one item', () => {
const chunked = createChunks('my-chunks', 'hello-world');
expect(chunked.length).toBe(1);
});

it('should chunk and return two chunks', () => {
const chunked = createChunks('my-chunks', CHUNK_STRING, 2000);
const combined = combineChunks('my-chunks', (name) => {
let chunk = chunked.find((chunk) => {
return chunk.name === name;
});
return chunk?.value;
});
expect(chunked.length).toBe(2);
expect(combined).toBe(CHUNK_STRING);
});

it('should chunk and return twelve chunks', () => {
const chunked = createChunks('my-chunks', CHUNK_STRING, 320);
const combined = combineChunks('my-chunks', (name) => {
let chunk = chunked.find((chunk) => {
return chunk.name === name;
});
return chunk?.value;
});
expect(chunked.length).toBe(12);
expect(combined).toBe(CHUNK_STRING);
});

it('should chunk and return one hundred and one chunks', () => {
const chunked = createChunks('my-chunks', CHUNK_STRING, 36);
const combined = combineChunks('my-chunks', (name) => {
let chunk = chunked.find((chunk) => {
return chunk.name === name;
});
return chunk?.value;
});
expect(chunked.length).toBe(101);
expect(combined).toBe(CHUNK_STRING);
});
});
2 changes: 2 additions & 0 deletions packages/ssr/tests/helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const CHUNK_STRING =
'zDq8KDAdv4PwF3UOp3mnEyx1xY71CaY4ZJdPTG8HpLHy3bCYs1x3vwPXdUqY75d0LYHL8KhxgrKqBEK531igiQNk1KqUKmMsabNlwcaF5E2gXA79vpwlxvi1wecwmKGVig4mJ0dzEEXKNsLgyQCsjKOpI7Nw2gnGAKKFdHle1SJeuFj9PyAHx9stMvQFpRQhoLRt3iI0uFA.axN9TdbRmcyIVYroYWoIVHJCvQceRtCjF8dDpmEqD4PRhvuGPue3fLQITp90RXJ3Fchvz7uhJcyjPWchXFNSKQsxVd02bt1RizkNijBglwZQAWHB8qIBQ2XQ7iRXjWOjbOyOQk6I2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47 additions & 13 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.