Skip to content

Commit c6a6a92

Browse files
committed
use xxh3 in server-core package
1 parent 8c4566c commit c6a6a92

File tree

3 files changed

+10
-15
lines changed

3 files changed

+10
-15
lines changed

packages/cubejs-server-core/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"@cubejs-backend/schema-compiler": "1.2.27",
3838
"@cubejs-backend/shared": "1.2.27",
3939
"@cubejs-backend/templates": "1.2.27",
40+
"@node-rs/xxhash": "^1.7.6",
4041
"codesandbox-import-utils": "^2.1.12",
4142
"cross-spawn": "^7.0.1",
4243
"fs-extra": "^8.1.0",

packages/cubejs-server-core/src/core/CompilerApi.js

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import crypto from 'crypto';
1+
import { xxh3 } from '@node-rs/xxhash';
22
import R from 'ramda';
33
import { createQuery, compile, queryClass, PreAggregations, QueryFactory } from '@cubejs-backend/schema-compiler';
44
import { v4 as uuidv4, parse as uuidParse, stringify as uuidStringify } from 'uuid';
@@ -55,7 +55,7 @@ export class CompilerApi {
5555

5656
if (this.options.devServer || this.options.fastReload) {
5757
const files = await this.repository.dataSchemaFiles();
58-
compilerVersion += `_${crypto.createHash('md5').update(JSON.stringify(files)).digest('hex')}`;
58+
compilerVersion += `_${xxh3.xxh64(JSON.stringify(files)).toString(16)}`;
5959
}
6060

6161
if (!this.compilers || this.compilerVersion !== compilerVersion) {
@@ -224,7 +224,7 @@ export class CompilerApi {
224224

225225
hashRequestContext(context) {
226226
if (!context.__hash) {
227-
context.__hash = crypto.createHash('md5').update(JSON.stringify(context)).digest('hex');
227+
context.__hash = xxh3.xxh64(JSON.stringify(context)).toString(16);
228228
}
229229
return context.__hash;
230230
}
@@ -505,7 +505,7 @@ export class CompilerApi {
505505
const visibiliyMask = JSON.stringify(isMemberVisibleInContext, Object.keys(isMemberVisibleInContext).sort());
506506
// This hash will be returned along the modified meta config and can be used
507507
// to distinguish between different "schema versions" after DAP visibility is applied
508-
const visibilityMaskHash = crypto.createHash('sha256').update(visibiliyMask).digest('hex');
508+
const visibilityMaskHash = xxh3.xxh64(visibiliyMask).toString(16);
509509

510510
return {
511511
cubes: cubes
@@ -523,10 +523,7 @@ export class CompilerApi {
523523
}
524524

525525
mixInVisibilityMaskHash(compilerId, visibilityMaskHash) {
526-
const uuidBytes = uuidParse(compilerId);
527-
const hashBytes = Buffer.from(visibilityMaskHash, 'hex');
528-
return uuidv4({ random: crypto.createHash('sha256').update(uuidBytes).update(hashBytes).digest()
529-
.subarray(0, 16) });
526+
return uuidv4({ random: Buffer.from(xxh3.xxh64(`${compilerId}${visibilityMaskHash}`).toString(16), 'hex') });
530527
}
531528

532529
async metaConfig(requestContext, options = {}) {
@@ -542,8 +539,8 @@ export class CompilerApi {
542539
return {
543540
cubes: patchedCubes,
544541
// This compilerId is primarily used by the cubejs-backend-native or caching purposes.
545-
// By default it doesn't account for member visibility changes introduced above by DAP.
546-
// Here we're modifying the originila compilerId in a way that it's distinct for
542+
// By default, it doesn't account for member visibility changes introduced above by DAP.
543+
// Here we're modifying the original compilerId in a way that it's distinct for
547544
// distinct schema versions while still being a valid UUID.
548545
compilerId: visibilityMaskHash ? this.mixInVisibilityMaskHash(compilers.compilerId, visibilityMaskHash) : compilers.compilerId,
549546
};

packages/cubejs-server-core/src/core/RefreshScheduler.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import R from 'ramda';
22
import pLimit from 'p-limit';
33
import { v4 as uuidv4 } from 'uuid';
4-
import crypto from 'crypto';
4+
import { xxh3 } from '@node-rs/xxhash';
55
import { Required } from '@cubejs-backend/shared';
66
import {
77
PreAggregationDescription,
@@ -127,10 +127,7 @@ function getPreAggsJobsList(
127127
* Returns MD5 hash token of the job object.
128128
*/
129129
function getPreAggJobToken(job: PreAggJob) {
130-
return crypto
131-
.createHash('md5')
132-
.update(JSON.stringify(job))
133-
.digest('hex');
130+
return xxh3.xxh64(JSON.stringify(job)).toString(16);
134131
}
135132

136133
export class RefreshScheduler {

0 commit comments

Comments
 (0)