Skip to content

Commit f22f269

Browse files
author
Gal Dubitski
authored
fix(deps): fnv1a included which supports Safari (#500)
* fix #496 safari broken dep * including fnv1a to project * reverting version
1 parent c7c7505 commit f22f269

File tree

4 files changed

+137
-2
lines changed

4 files changed

+137
-2
lines changed

packages/graphql-hooks-memcache/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
"author": "Brian Mullan <[email protected]>",
3232
"license": "Apache-2.0",
3333
"dependencies": {
34-
"@sindresorhus/fnv1a": "^2.0.0",
3534
"tiny-lru": "^7.0.0"
3635
},
3736
"repository": {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
'use strict'
2+
3+
// MIT License
4+
//
5+
// Copyright (c) Sindre Sorhus <[email protected]> (sindresorhus.com)
6+
//
7+
// Permission is hereby granted, free of charge, to any person obtaining a
8+
// copy of this software and associated documentation files (the "Software"),
9+
// to deal in the Software without restriction, including without limitation
10+
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
11+
// and/or sell copies of the Software, and to permit persons to whom the Software
12+
// is furnished to do so, subject to the following conditions:
13+
//
14+
// The above copyright notice and this permission notice shall be included in all
15+
// copies or substantial portions of the Software.
16+
//
17+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
18+
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
19+
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20+
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21+
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22+
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23+
24+
const OFFSET_BASIS_32 = 2166136261
25+
26+
function fnv1aString(string) {
27+
let hash = OFFSET_BASIS_32
28+
29+
for (let i = 0; i < string.length; i++) {
30+
hash ^= string.charCodeAt(i)
31+
32+
// 32-bit FNV prime: 2**24 + 2**8 + 0x93 = 16777619
33+
// Using bitshift for accuracy and performance. Numbers in JS suck.
34+
hash += (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) + (hash << 24)
35+
}
36+
37+
return hash >>> 0
38+
}
39+
40+
function fnv1aBuffer(buf) {
41+
let hash = OFFSET_BASIS_32
42+
43+
for (let i = 0; i < buf.length; ) {
44+
hash ^= buf[i++]
45+
46+
// 32-bit FNV prime: 2**24 + 2**8 + 0x93 = 16777619
47+
// Using bitshift for accuracy and performance. Numbers in JS suck.
48+
hash += (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) + (hash << 24)
49+
}
50+
51+
return hash >>> 0
52+
}
53+
54+
export default function fnv1a(input) {
55+
if (input instanceof Buffer) {
56+
return fnv1aBuffer(input)
57+
} else if (typeof input === 'string') {
58+
return fnv1aString(input)
59+
} else {
60+
throw new Error('input must be a string or a Buffer')
61+
}
62+
}

packages/graphql-hooks-memcache/src/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import LRU from 'tiny-lru'
2-
import fnv1a from '@sindresorhus/fnv1a'
2+
import fnv1a from './fnv1a'
33

44
function generateKey(keyObj) {
55
return fnv1a(JSON.stringify(keyObj)).toString(36)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
'use strict'
2+
3+
// MIT License
4+
//
5+
// Copyright (c) Sindre Sorhus <[email protected]> (sindresorhus.com)
6+
//
7+
// Permission is hereby granted, free of charge, to any person obtaining a
8+
// copy of this software and associated documentation files (the "Software"),
9+
// to deal in the Software without restriction, including without limitation
10+
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
11+
// and/or sell copies of the Software, and to permit persons to whom the Software
12+
// is furnished to do so, subject to the following conditions:
13+
//
14+
// The above copyright notice and this permission notice shall be included in all
15+
// copies or substantial portions of the Software.
16+
//
17+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
18+
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
19+
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20+
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21+
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22+
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23+
24+
import fnv1a from '../src/fnv1a'
25+
26+
describe('fnv1a tests', () => {
27+
it('works for string', () => {
28+
expect(fnv1a('')).toEqual(2166136261)
29+
expect(fnv1a('🦄🌈')).toEqual(582881315)
30+
expect(fnv1a('h')).toEqual(3977000791)
31+
expect(fnv1a('he')).toEqual(1547363254)
32+
expect(fnv1a('hel')).toEqual(179613742)
33+
expect(fnv1a('hell')).toEqual(477198310)
34+
expect(fnv1a('hello')).toEqual(1335831723)
35+
expect(fnv1a('hello ')).toEqual(3801292497)
36+
expect(fnv1a('hello w')).toEqual(1402552146)
37+
expect(fnv1a('hello wo')).toEqual(3611200775)
38+
expect(fnv1a('hello wor')).toEqual(1282977583)
39+
expect(fnv1a('hello worl')).toEqual(2767971961)
40+
expect(fnv1a('hello world')).toEqual(3582672807)
41+
expect(
42+
fnv1a(
43+
'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium.'
44+
)
45+
).toEqual(2964896417)
46+
})
47+
48+
it('works for buffer', () => {
49+
expect(fnv1a(Buffer.from(''))).toEqual(2166136261)
50+
expect(fnv1a(Buffer.from('🦄🌈'))).toEqual(2868248295)
51+
expect(fnv1a(Buffer.from('h'))).toEqual(3977000791)
52+
expect(fnv1a(Buffer.from('he'))).toEqual(1547363254)
53+
expect(fnv1a(Buffer.from('hel'))).toEqual(179613742)
54+
expect(fnv1a(Buffer.from('hell'))).toEqual(477198310)
55+
expect(fnv1a(Buffer.from('hello'))).toEqual(1335831723)
56+
expect(fnv1a(Buffer.from('hello '))).toEqual(3801292497)
57+
expect(fnv1a(Buffer.from('hello w'))).toEqual(1402552146)
58+
expect(fnv1a(Buffer.from('hello wo'))).toEqual(3611200775)
59+
expect(fnv1a(Buffer.from('hello wor'))).toEqual(1282977583)
60+
expect(fnv1a(Buffer.from('hello worl'))).toEqual(2767971961)
61+
expect(fnv1a(Buffer.from('hello world'))).toEqual(3582672807)
62+
expect(
63+
fnv1a(
64+
Buffer.from(
65+
'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium.'
66+
)
67+
)
68+
).toEqual(2964896417)
69+
})
70+
71+
it("can't hash objects", () => {
72+
expect(() => fnv1a({})).toThrow(Error)
73+
})
74+
})

0 commit comments

Comments
 (0)