Skip to content

Commit

Permalink
Rework client router filter handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ijjk committed May 13, 2023
1 parent 68b5141 commit 30041b3
Show file tree
Hide file tree
Showing 12 changed files with 104 additions and 883 deletions.
4 changes: 1 addition & 3 deletions packages/next/src/lib/create-client-router-filter.ts
Expand Up @@ -5,12 +5,10 @@ import { removeTrailingSlash } from '../shared/lib/router/utils/remove-trailing-
import { Redirect } from './load-custom-routes'
import { tryToParsePath } from './try-to-parse-path'

const POTENTIAL_ERROR_RATE = 0.01

export function createClientRouterFilter(
paths: string[],
redirects: Redirect[],
allowedErrorRate: number = POTENTIAL_ERROR_RATE
allowedErrorRate?: number
): {
staticFilter: ReturnType<BloomFilter['export']>
dynamicFilter: ReturnType<BloomFilter['export']>
Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/server/lib/incremental-cache/index.ts
Expand Up @@ -8,7 +8,7 @@ import {
IncrementalCacheValue,
IncrementalCacheEntry,
} from '../../response-cache'
import { encode } from '../../../shared/lib/bloom-filter/base64-arraybuffer'
import { encode } from '../../../shared/lib/base64-arraybuffer'
import { encodeText } from '../../stream-utils/encode-decode'
import {
CACHE_ONE_YEAR,
Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/server/lib/patch-fetch.ts
Expand Up @@ -446,7 +446,7 @@ export function patchFetch({

if (process.env.NEXT_RUNTIME === 'edge') {
const { decode } =
require('../../shared/lib/bloom-filter/base64-arraybuffer') as typeof import('../../shared/lib/bloom-filter/base64-arraybuffer')
require('../../shared/lib/base64-arraybuffer') as typeof import('../../shared/lib/base64-arraybuffer')
decodedBody = decode(resData.body)
} else {
decodedBody = Buffer.from(resData.body, 'base64').subarray()
Expand Down
92 changes: 92 additions & 0 deletions packages/next/src/shared/lib/bloom-filter.ts
@@ -0,0 +1,92 @@
// minimal implementation MurmurHash2 hash function
function murmurhash2(str: string) {
let h = 0
for (let i = 0; i < str.length; i++) {
const c = str.charCodeAt(i)
h = Math.imul(h ^ c, 0x5bd1e995)
h ^= h >>> 13
h = Math.imul(h, 0x5bd1e995)
}
return h >>> 0
}

export class BloomFilter {
numItems: number
errorRate: number
numBits: number
numHashes: number
bitArray: number[]

constructor(numItems: number, errorRate: number) {
this.numItems = numItems
this.errorRate = errorRate
this.numBits = Math.ceil(
-(numItems * Math.log(errorRate)) / (Math.log(2) * Math.log(2))
)
this.numHashes = Math.ceil((this.numBits / numItems) * Math.log(2))
this.bitArray = new Array(this.numBits).fill(0)

if (typeof window === 'undefined') {
if (errorRate < 0.01) {
const filterData = JSON.stringify(this.export())
const gzipSize = require('next/dist/compiled/gzip-size').sync(
filterData
)

if (gzipSize > 1024) {
console.warn(
`Creating filter with error rate less than 1% (0.01) can increase the size dramatically proceed with caution. Received error rate ${errorRate} resulted in size ${gzipSize} bytes (gzip)`
)
}
}
}
}

static from(items: string[], errorRate = 0.01) {
const filter = new BloomFilter(items.length, errorRate)

for (const item of items) {
filter.add(item)
}
return filter
}

export() {
return {
numItems: this.numItems,
errorRate: this.errorRate,
numBits: this.numBits,
numHashes: this.numHashes,
bitArray: this.bitArray,
}
}

import(data: ReturnType<typeof this['export']>) {
this.numItems = data.numItems
this.errorRate = data.errorRate
this.numBits = data.numBits
this.numHashes = data.numHashes
this.bitArray = data.bitArray
}

add(item: string) {
const hashValues = this.getHashValues(item)
hashValues.forEach((hash) => {
this.bitArray[hash] = 1
})
}

contains(item: string) {
const hashValues = this.getHashValues(item)
return hashValues.every((hash) => this.bitArray[hash])
}

getHashValues(item: string) {
const hashValues = []
for (let i = 1; i <= this.numHashes; i++) {
const hash = murmurhash2(`${item}${i}`) % this.numBits
hashValues.push(hash)
}
return hashValues
}
}
119 changes: 0 additions & 119 deletions packages/next/src/shared/lib/bloom-filter/base-filter.ts

This file was deleted.

149 changes: 0 additions & 149 deletions packages/next/src/shared/lib/bloom-filter/bit-set.ts

This file was deleted.

0 comments on commit 30041b3

Please sign in to comment.