fix(v2): truncate docuhash return value in order to avoid ERRNAMETOOLONG error (#4899)

* fix: truncate docuhash return value in order to avoid ERRNAMETOOLONG error

* chore: add deep file path test page to website

* refactor: reorganize docuHash/pathUtils code and tests

* chore: git support longpaths on v2 windows tests workflow

Co-authored-by: slorber <lorber.sebastien@gmail.com>
This commit is contained in:
Lucas Correia 2021-06-15 13:39:06 -03:00 committed by GitHub
parent 3d95a3e6b1
commit 34411e12e5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 202 additions and 50 deletions

View file

@ -0,0 +1,48 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
// Based on https://github.com/gatsbyjs/gatsby/pull/21518/files
import {createHash} from 'crypto';
// MacOS (APFS) and Windows (NTFS) filename length limit = 255 chars, Others = 255 bytes
const MAX_PATH_SEGMENT_CHARS = 255;
const MAX_PATH_SEGMENT_BYTES = 255;
// Space for appending things to the string like file extensions and so on
const SPACE_FOR_APPENDING = 10;
const isMacOs = process.platform === `darwin`;
const isWindows = process.platform === `win32`;
export const isNameTooLong = (str: string): boolean => {
return isMacOs || isWindows
? str.length + SPACE_FOR_APPENDING > MAX_PATH_SEGMENT_CHARS // MacOS (APFS) and Windows (NTFS) filename length limit (255 chars)
: Buffer.from(str).length + SPACE_FOR_APPENDING > MAX_PATH_SEGMENT_BYTES; // Other (255 bytes)
};
export const shortName = (str: string): string => {
if (isMacOs || isWindows) {
const overflowingChars = str.length - MAX_PATH_SEGMENT_CHARS;
return str.slice(
0,
str.length - overflowingChars - SPACE_FOR_APPENDING - 1,
);
}
const strBuffer = Buffer.from(str);
const overflowingBytes =
Buffer.byteLength(strBuffer) - MAX_PATH_SEGMENT_BYTES;
return strBuffer
.slice(
0,
Buffer.byteLength(strBuffer) - overflowingBytes - SPACE_FOR_APPENDING - 1,
)
.toString();
};
export function simpleHash(str: string, length: number): string {
return createHash('md5').update(str).digest('hex').substr(0, length);
}