-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
30 additions
and
0 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
packages/preview-process/src/parts/GenerateEtag/GenerateEtag.ts
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,7 @@ | ||
import { createHash } from 'node:crypto' | ||
|
||
export const generateEtag = (content: string): string => { | ||
const hash = createHash('sha1') | ||
hash.update(content) | ||
return `W/"${hash.digest('hex')}"` | ||
} |
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,23 @@ | ||
import { expect, test } from '@jest/globals' | ||
import * as GenerateEtag from '../src/parts/GenerateEtag/GenerateEtag.ts' | ||
|
||
test('generateEtag - generates weak etag from content', () => { | ||
const content = 'test content' | ||
const etag = GenerateEtag.generateEtag(content) | ||
expect(etag).toMatch(/^W\/"[a-f0-9]+"$/) | ||
}) | ||
|
||
test('generateEtag - generates different etags for different content', () => { | ||
const content1 = 'content 1' | ||
const content2 = 'content 2' | ||
const etag1 = GenerateEtag.generateEtag(content1) | ||
const etag2 = GenerateEtag.generateEtag(content2) | ||
expect(etag1).not.toBe(etag2) | ||
}) | ||
|
||
test('generateEtag - generates same etag for same content', () => { | ||
const content = 'same content' | ||
const etag1 = GenerateEtag.generateEtag(content) | ||
const etag2 = GenerateEtag.generateEtag(content) | ||
expect(etag1).toBe(etag2) | ||
}) |