-
-
Notifications
You must be signed in to change notification settings - Fork 236
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add chunker code * Remove unused constant * Add changeset * Store the regex in a constant * Always return an array of objects from createChunks * Remove array check as chunker always returns array * Added vitest and tests for chunker * Add chunkSize property to createChunks
- Loading branch information
1 parent
a2c0abc
commit 9fa8f2b
Showing
12 changed files
with
460 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@supabase/auth-helpers-shared': minor | ||
--- | ||
|
||
Add cookie chunking for large JWTs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,8 @@ | |
"build:ssr": "turbo run build --filter=@supabase/ssr", | ||
"dev": "turbo run dev --parallel", | ||
"lint": "turbo run lint --filter=!@example/*", | ||
"test": "turbo run test", | ||
"test:watch": "turbo run test:watch", | ||
"check": "prettier --check .", | ||
"format": "prettier --write .", | ||
"docs": "typedoc", | ||
|
@@ -40,17 +42,6 @@ | |
"peerDependencies": { | ||
"typescript": "4.0.x || 4.1.x || 4.2.x || 4.3.x || 4.4.x || 4.5.x || 4.6.x" | ||
}, | ||
"pnpm": { | ||
"overrides": { | ||
"@changesets/assemble-release-plan": "5.2.3" | ||
}, | ||
"overrides-notes": { | ||
"@changesets/assemble-release-plan": "patched until https://github.com/changesets/changesets/issues/835 is resolved" | ||
}, | ||
"patchedDependencies": { | ||
"@changesets/[email protected]": "patches/@[email protected]" | ||
} | ||
}, | ||
"lint-staged": { | ||
"**/*.{ts,tsx,md}": [ | ||
"prettier --check" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
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 / 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 combineChunk( | ||
key: string, | ||
retrieveChunk: (name: string) => string | null | undefined = () => { | ||
return null; | ||
} | ||
) { | ||
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import { combineChunk, createChunks } from '../src/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 = combineChunk('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 = combineChunk('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 = combineChunk('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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export const CHUNK_STRING = | ||
'zDq8KDAdv4PwF3UOp3mnEyx1xY71CaY4ZJdPTG8HpLHy3bCYs1x3vwPXdUqY75d0LYHL8KhxgrKqBEK531igiQNk1KqUKmMsabNlwcaF5E2gXA79vpwlxvi1wecwmKGVig4mJ0dzEEXKNsLgyQCsjKOpI7Nw2gnGAKKFdHle1SJeuFj9PyAHx9stMvQFpRQhoLRt3iI0uFA.axN9TdbRmcyIVYroYWoIVHJCvQceRtCjF8dDpmEqD4PRhvuGPue3fLQITp90RXJ3Fchvz7uhJcyjPWchXFNSKQsxVd02bt1RizkNijBglwZQAWHB8qIBQ2XQ7iRXjWOjbOyOQk6I2tr99FWDOKVD5XElchlu0GgQNsxQoC6Q3twecZKWenvYNAhjoPQZCxzOdg4kFBGrCZzOJp6cIZD8Mu4XCnlZTXzDCqyLPusfUvgzABe87pj8h9seo2yllyq8CtQaGysRE849qcoLRQVeBFSEm6FHvJw3QJla9K25wiBTfxVWr7JzIE7za5IrNQUyHqGnlC577AX1TPrqPWyIMGNs5nxrEHJdUNrHV3PD0iXob7WWsjFFc9HS3m2BSPCn4gnNgfHVkewlf1sYm8C0notbKvBV4MPloXdxEtC1QM79ElTr1VrhHWRT2RewabZYcQuTh4kl2BbZCZ3yjlrJ0Sj2Ndl4pyk95nOU3JaboyPJtAjMDc6625OJFRHL9USACMojBSVOzYhSUk6gV2TYkBjzY4L5KytYDc0ONey8LnXUVLwzZLVVFuiuiXO1WQf7DY0TBttEr1tsApBMYveSR39o73yPeTnVikjRpJtEfqhgwU22afFfiaTqnaRU51WDjW0o7ncHONI9PxhAxKjOwcZzjqnrHu9lN6n2dbRLGArqKyFdRaVETsNRxREJv4E57blQqAgCClSIJvvBW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