1
1
/* eslint-disable @typescript-eslint/no-var-requires */
2
- import { createHash } from 'crypto'
3
-
4
2
import Image , { ImageLoader , ImageProps } from 'next/dist/client/image'
5
3
import React from 'react'
6
4
@@ -10,6 +8,16 @@ import getConfig, { ParsedImageInfo } from './utils/getConfig'
10
8
11
9
const config = getConfig ( )
12
10
11
+ function hashCode ( src : string ) {
12
+ let hash = 0
13
+ for ( let i = 0 ; i < src . length ; i += 1 ) {
14
+ const chr = src . charCodeAt ( i )
15
+ hash = ( hash << 5 ) - hash + chr
16
+ hash |= 0 // Convert to 32bit integer
17
+ }
18
+ return `${ hash } `
19
+ }
20
+
13
21
const defaultImageParser : ( src : string ) => ParsedImageInfo = ( src : string ) => {
14
22
const path = src . split ( / \. ( [ ^ . ] * $ ) / ) [ 0 ]
15
23
const extension = ( src . split ( / \. ( [ ^ . ] * $ ) / ) [ 1 ] || '' ) . split ( '?' ) [ 0 ]
@@ -86,15 +94,13 @@ const exportableLoader: ImageLoader = ({ src: _src, width, quality }) => {
86
94
const path = require ( 'path' ) as typeof import ( 'path' )
87
95
88
96
if ( src . startsWith ( 'http' ) ) {
89
- json . src = `/${ externalOutputDir } /${ createHash ( 'sha1' )
90
- . update (
91
- src
92
- . replace ( / ^ h t t p s ? : \/ \/ / , '' )
93
- . split ( '/' )
94
- . slice ( 1 )
95
- . join ( '/' )
96
- )
97
- . digest ( 'hex' ) } .${ originalExtension } `
97
+ json . src = `/${ externalOutputDir } /${ hashCode (
98
+ src
99
+ . replace ( / ^ h t t p s ? : \/ \/ / , '' )
100
+ . split ( '/' )
101
+ . slice ( 1 )
102
+ . join ( '/' )
103
+ ) } .${ originalExtension } `
98
104
99
105
json . externalUrl = src
100
106
}
0 commit comments