mirror of
https://github.com/facebook/docusaurus.git
synced 2025-12-30 22:23:00 +00:00
* 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>
49 lines
1.6 KiB
TypeScript
49 lines
1.6 KiB
TypeScript
/**
|
|
* 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);
|
|
}
|