diff --git a/.dprint.base.json b/.dprint.base.json index fe451a206..369e5b16f 100644 --- a/.dprint.base.json +++ b/.dprint.base.json @@ -9,5 +9,5 @@ "taggedTemplate.spaceBeforeLiteral": false, "typeAssertion.spaceBeforeExpression": false }, - "plugins": ["https://plugins.dprint.dev/typescript-0.69.0.wasm"] + "plugins": ["https://plugins.dprint.dev/typescript-0.80.2.wasm"] } diff --git a/.lintstagedrc.cjs b/.lintstagedrc.cjs index 45d2dc606..beb35e315 100644 --- a/.lintstagedrc.cjs +++ b/.lintstagedrc.cjs @@ -140,9 +140,9 @@ module.exports = async filenames => { // ESLint will find the configuration files based on cwd. // If inside submodule directories, ESLint will fail because cwd is not the project root. // To avoid this, run ESLint using the "pnpm exec" command with the "--workspace-root" option. - const eslintCommand = ( - `pnpm --workspace-root exec eslint --cache --report-unused-disable-directives --fix ${tsOrJsFiles.join(' ')}` - ); + const eslintCommand = `pnpm --workspace-root exec eslint --cache --report-unused-disable-directives --fix ${ + tsOrJsFiles.join(' ') + }`; const dprintCommand = ( await Promise.all([ dprintCommandList(tsFiles, './.dprint.json'), diff --git a/packages/encrypted-archive/src/core/header/protocol-buffers-converter/utils.ts b/packages/encrypted-archive/src/core/header/protocol-buffers-converter/utils.ts index 7054befcc..1ae7feb8d 100644 --- a/packages/encrypted-archive/src/core/header/protocol-buffers-converter/utils.ts +++ b/packages/encrypted-archive/src/core/header/protocol-buffers-converter/utils.ts @@ -55,65 +55,62 @@ export function validateNumberOptionInRange( return value; } -export function createEnum2value(): ( - (enumRecord: Record) => ( - ( - /** - * Type checking asserts that all enum combinations are specified - * @see https://stackoverflow.com/a/60132060/4907315 - */ - pair: - & OneOrMoreReadonlyArray - & Cond2<{ - cond1: { - actual: TEnum2; - expected: TEnum; - onlyMatch: 'Invalid: All value types must be specified'; - }; - cond2: { - actual: TValue2; - expected: TValue; - onlyMatch: 'Invalid: All Enum type values must be specified'; - }; - allMatch: unknown; - notAllMatch: 'Invalid: All Enum type values and all value types must be specified'; - }>, - ) => { - enum2value: ( - builtin: BuiltinInspectRecord, - enumItem: Nullable, - exists: boolean, - opts: { fieldName: string; dataName: string }, - ) => TValue2; - value2enum: (builtin: BuiltinInspectRecord, value: TValue2) => TEnum2; - } - ) -) { - return enumRecord => - pair => { - const enum2valueMap = new Map(pair.map(([e, v]) => [e, { data: v }])); - const value2enumMap = new Map(pair.map(([e, v]) => [v, { data: e }])); +export function createEnum2value(): ( + enumRecord: Record, +) => ( + /** + * Type checking asserts that all enum combinations are specified + * @see https://stackoverflow.com/a/60132060/4907315 + */ + pair: + & OneOrMoreReadonlyArray + & Cond2<{ + cond1: { + actual: TEnum2; + expected: TEnum; + onlyMatch: 'Invalid: All value types must be specified'; + }; + cond2: { + actual: TValue2; + expected: TValue; + onlyMatch: 'Invalid: All Enum type values must be specified'; + }; + allMatch: unknown; + notAllMatch: 'Invalid: All Enum type values and all value types must be specified'; + }>, +) => { + enum2value: ( + builtin: BuiltinInspectRecord, + enumItem: Nullable, + exists: boolean, + opts: { fieldName: string; dataName: string }, + ) => TValue2; + value2enum: (builtin: BuiltinInspectRecord, value: TValue2) => TEnum2; +} { + return enumRecord => pair => { + const enum2valueMap = new Map(pair.map(([e, v]) => [e, { data: v }])); + const value2enumMap = new Map(pair.map(([e, v]) => [v, { data: e }])); - type TEnum2 = (typeof enum2valueMap) extends Map ? U : never; - const isEnumType = enum2valueMap.has.bind(enum2valueMap) as (value: unknown) => value is TEnum2; + type TEnum2 = (typeof enum2valueMap) extends Map ? U : never; + const isEnumType = enum2valueMap.has.bind(enum2valueMap) as (value: unknown) => value is TEnum2; - return { - enum2value: (builtin, enumItem, exists, { fieldName, dataName }) => { - if (!exists || (!isEnumType(enumItem) && (enumItem === undefined || enumItem === null))) { - reportNonDefinedField({ fieldName, dataName }); - } - const value = enum2valueMap.get(enumItem); - if (value) return value.data; - throw new Error( - `The value in the ${fieldName} field in the ${dataName} is unknown.` - + ` Received: ${getPropFromValue(enumRecord, enumItem) ?? builtin.inspect(enumItem)}`, - ); - }, - value2enum: (builtin, value) => { - const enumItem = value2enumMap.get(value); - if (enumItem) return enumItem.data; - throw new Error(`Unknown Argon2 algorithm received: ${builtin.inspect(value)}`); - }, - }; + return { + enum2value: (builtin, enumItem, exists, { fieldName, dataName }) => { + if (!exists || (!isEnumType(enumItem) && (enumItem === undefined || enumItem === null))) { + reportNonDefinedField({ fieldName, dataName }); + } + const value = enum2valueMap.get(enumItem); + if (value) return value.data; + throw new Error( + `The value in the ${fieldName} field in the ${dataName} is unknown.` + + ` Received: ${getPropFromValue(enumRecord, enumItem) ?? builtin.inspect(enumItem)}`, + ); + }, + value2enum: (builtin, value) => { + const enumItem = value2enumMap.get(value); + if (enumItem) return enumItem.data; + throw new Error(`Unknown Argon2 algorithm received: ${builtin.inspect(value)}`); + }, }; + }; } diff --git a/packages/encrypted-archive/src/core/key-derivation-function/argon2.ts b/packages/encrypted-archive/src/core/key-derivation-function/argon2.ts index d516ece12..39702b2ef 100644 --- a/packages/encrypted-archive/src/core/key-derivation-function/argon2.ts +++ b/packages/encrypted-archive/src/core/key-derivation-function/argon2.ts @@ -98,9 +98,9 @@ export function isArgon2Options(options: T): options is T extends Argon2Optio } function validatePositiveInteger(builtin: BuiltinInspectRecord, optionName: string, value: unknown): asserts value { - const messageSuffix = ( - `The "${optionName}" option must be of positive integers without 0, but received: ${builtin.inspect(value)}` - ); + const messageSuffix = `The "${optionName}" option must be of positive integers without 0, but received: ${ + builtin.inspect(value) + }`; if (!(Number.isInteger as isInteger)(value)) { throw new TypeError(`Invalid type value received for Argon2's option "${optionName}". ${messageSuffix}`); } @@ -256,27 +256,27 @@ const createDeriveKeyFunc = ( builtin: { argon2Hash: Argon2HashFn } & BuiltinInspectRecord, options: NormalizedArgon2Options, ): GetArgon2KDFResult['deriveKey'] => - async (password, salt, keyLengthBytes) => { - validateBetweenByteLength( - 'password', - password, - { min: ARGON2_PASSWORD.MIN, max: ARGON2_PASSWORD.MAX }, - ); - validateBetweenByteLength('salt', salt, { min: ARGON2_SALT.MIN, max: ARGON2_SALT.MAX }); - validateBetweenLength( - 'keyLengthBytes', - keyLengthBytes, - { shortName: 'key', min: ARGON2_OUTPUT.MIN, max: ARGON2_OUTPUT.MAX }, - ); +async (password, salt, keyLengthBytes) => { + validateBetweenByteLength( + 'password', + password, + { min: ARGON2_PASSWORD.MIN, max: ARGON2_PASSWORD.MAX }, + ); + validateBetweenByteLength('salt', salt, { min: ARGON2_SALT.MIN, max: ARGON2_SALT.MAX }); + validateBetweenLength( + 'keyLengthBytes', + keyLengthBytes, + { shortName: 'key', min: ARGON2_OUTPUT.MIN, max: ARGON2_OUTPUT.MAX }, + ); - return await builtin.argon2Hash({ - ...options, - password, - salt, - hashLength: keyLengthBytes, - }) - .catch(normalizeInternalError(builtin)); - }; + return await builtin.argon2Hash({ + ...options, + password, + salt, + hashLength: keyLengthBytes, + }) + .catch(normalizeInternalError(builtin)); +}; export function getArgon2KDF(builtin: { argon2Hash: Argon2HashFn } & BuiltinInspectRecord) { return (options: Readonly): GetArgon2KDFResult => { diff --git a/packages/encrypted-archive/src/core/types/compress.ts b/packages/encrypted-archive/src/core/types/compress.ts index aa11c0845..ed570fce2 100644 --- a/packages/encrypted-archive/src/core/types/compress.ts +++ b/packages/encrypted-archive/src/core/types/compress.ts @@ -24,11 +24,9 @@ export interface CreateCompressorResult( - options: TCompressOptions | Pick, - ) => CreateCompressorResult | undefined -); +export type TryCreateCompressor = ( + options: TCompressOptions | Pick, +) => CreateCompressorResult | undefined; export interface CompressionAlgorithmBuiltinAPI { algorithmRecord: BaseCompressorRecord; tryCreateCompressor: TryCreateCompressor; diff --git a/packages/encrypted-archive/src/core/types/utils.ts b/packages/encrypted-archive/src/core/types/utils.ts index 460dccf09..566f73348 100644 --- a/packages/encrypted-archive/src/core/types/utils.ts +++ b/packages/encrypted-archive/src/core/types/utils.ts @@ -49,11 +49,9 @@ export type objectEntries = (o: Record) => Array<[T, export type objectFromEntries = (entries: Iterable) => Record; -export type GetOptions unknown> = ( - T extends ((options?: infer U) => unknown) ? U - : T extends ((options: infer U) => unknown) ? U - : never -); +export type GetOptions unknown> = T extends ((options?: infer U) => unknown) ? U + : T extends ((options: infer U) => unknown) ? U + : never; /** * @see https://github.com/sindresorhus/type-fest/blob/v3.5.0/source/require-at-least-one.d.ts diff --git a/packages/encrypted-archive/src/runtimes/node/key-derivation-function/argon2.ts b/packages/encrypted-archive/src/runtimes/node/key-derivation-function/argon2.ts index 69adc59d3..64478062f 100644 --- a/packages/encrypted-archive/src/runtimes/node/key-derivation-function/argon2.ts +++ b/packages/encrypted-archive/src/runtimes/node/key-derivation-function/argon2.ts @@ -10,34 +10,32 @@ const nodeVersionMatch = /^v(\d+)\.(\d+)\./.exec(nodeVersion); * Because argon2-browser fails in Node.js 18.1.0 or later. * @see https://github.com/antelle/argon2-browser/issues/81 */ -export const argon2Hash: Argon2HashFn = ( - (Number(nodeVersionMatch?.[1]) >= 18 && Number(nodeVersionMatch?.[2]) >= 1) - ? // In Node.js >=18.1.0, use node-argon2 - (() => { - const argon2Mod = import('argon2'); // eslint-disable-line node/no-unsupported-features/es-syntax - return async options => { - const argon2 = await argon2Mod; - const typeRecord = { - argon2d: argon2.argon2d, - argon2id: argon2.argon2id, - } as const; - const password = typeof options.password === 'string' - ? options.password - : bufferFrom(options.password); - return await argon2.hash(password, { - salt: Buffer.from(options.salt), - timeCost: options.iterations, - memoryCost: options.memory, - hashLength: options.hashLength, - parallelism: options.parallelism, - type: typeRecord[options.algorithm], - raw: true, - }); - }; - })() - : // For Node.js <18.1.0, use argon2-browser - (() => { - const mod = import('../../web-interoperable/key-derivation-function/argon2.js'); // eslint-disable-line node/no-unsupported-features/es-syntax - return async options => await (await mod).argon2Hash(options); - })() -); +export const argon2Hash: Argon2HashFn = (Number(nodeVersionMatch?.[1]) >= 18 && Number(nodeVersionMatch?.[2]) >= 1) + // In Node.js >=18.1.0, use node-argon2 + ? (() => { + const argon2Mod = import('argon2'); // eslint-disable-line node/no-unsupported-features/es-syntax + return async options => { + const argon2 = await argon2Mod; + const typeRecord = { + argon2d: argon2.argon2d, + argon2id: argon2.argon2id, + } as const; + const password = typeof options.password === 'string' + ? options.password + : bufferFrom(options.password); + return await argon2.hash(password, { + salt: Buffer.from(options.salt), + timeCost: options.iterations, + memoryCost: options.memory, + hashLength: options.hashLength, + parallelism: options.parallelism, + type: typeRecord[options.algorithm], + raw: true, + }); + }; + })() + // For Node.js <18.1.0, use argon2-browser + : (() => { + const mod = import('../../web-interoperable/key-derivation-function/argon2.js'); // eslint-disable-line node/no-unsupported-features/es-syntax + return async options => await (await mod).argon2Hash(options); + })(); diff --git a/packages/jest-matchers/binary-data/src/utils/index.ts b/packages/jest-matchers/binary-data/src/utils/index.ts index 16a558f85..e73cbdb81 100644 --- a/packages/jest-matchers/binary-data/src/utils/index.ts +++ b/packages/jest-matchers/binary-data/src/utils/index.ts @@ -99,7 +99,7 @@ export function padTextColumns( .join('\n'); } -export function toMessageFn(func: () => (string | ReadonlyArray)): jest.CustomMatcherResult['message'] { +export function toMessageFn(func: () => string | ReadonlyArray): jest.CustomMatcherResult['message'] { return () => { const lines: string[] = toArray(func()).filter(isNotNull); return lines.join('\n'); diff --git a/packages/stream-transform-from/src/index.ts b/packages/stream-transform-from/src/index.ts index 6e2d63e9e..9c8ac2f73 100644 --- a/packages/stream-transform-from/src/index.ts +++ b/packages/stream-transform-from/src/index.ts @@ -10,9 +10,8 @@ type IfNeverThenUnknown = [T] extends [never] ? unknown : T; * If the `objectMode` and `writableObjectMode` options is not `true`, * the chunk value is always an instance of Buffer. */ -export type InputChunkType = ( - true extends GetPropValue ? unknown : Buffer -); +export type InputChunkType = true extends + GetPropValue ? unknown : Buffer; /** * If the `objectMode` and `readableObjectMode` options is not `true`, @@ -24,18 +23,14 @@ export type OutputChunkType = IfNeverThenUnkn : string | Buffer | Uint8Array >; -export type SourceIterator = ( - AsyncIterableIterator<{ - chunk: InputChunkType; - encoding: BufferEncoding; - }> -); +export type SourceIterator = AsyncIterableIterator<{ + chunk: InputChunkType; + encoding: BufferEncoding; +}>; -export type TransformFunction = ( - (source: SourceIterator) => - | Iterable> - | AsyncIterable> -); +export type TransformFunction = (source: SourceIterator) => + | Iterable> + | AsyncIterable>; type ReceivedData = | { chunk: InputChunkType; encoding: BufferEncoding; done?: false } @@ -160,13 +155,11 @@ export class TransformFromAsyncIterable< private async *createSource(): SourceIterator { while (true) { - const data: ReceivedData = ( - this.receivedDataList.shift() - ?? await new Promise(resolve => { - this.receiveData = resolve; - this.callTransformCallback(); - }) - ); + const data: ReceivedData = this.receivedDataList.shift() + ?? await new Promise(resolve => { + this.receiveData = resolve; + this.callTransformCallback(); + }); if (data.done) break; const { done: _, ...chunkData } = data; yield chunkData; diff --git a/packages/ts-utils/is-property-accessible/src/index.ts b/packages/ts-utils/is-property-accessible/src/index.ts index 55a1d155d..025202de5 100644 --- a/packages/ts-utils/is-property-accessible/src/index.ts +++ b/packages/ts-utils/is-property-accessible/src/index.ts @@ -1,7 +1,7 @@ -export function isPropertyAccessible(value: T): value is ( +export function isPropertyAccessible(value: T): value is & Exclude & Record -) { +{ return value !== null && value !== undefined; } diff --git a/scripts/fix-changelog.mjs b/scripts/fix-changelog.mjs index 8eea4ed7b..72e85bf3b 100644 --- a/scripts/fix-changelog.mjs +++ b/scripts/fix-changelog.mjs @@ -22,8 +22,8 @@ import { execa } from 'execa'; */ const SEMVER_REGEXP = /[0-9]+(?:\.[0-9]+){2}(?:-[.0-9a-zA-Z-]+)?(?:\+[.0-9a-zA-Z-]+)?/; const VERSION_HEADING_REGEXP = new RegExp( - String.raw - `\n+^###? *(?:<(\w+)[^>]*>)?(?(?:${SEMVER_REGEXP.source}|\[${SEMVER_REGEXP.source}\]\((?[^)]+)/compare/(?[^\s)]+?)\.{2,3}(?[^\s)]+?)\)) \([0-9]{4,}(?:-[0-9]{1,2}){2}\))(?:)?$`, + String + .raw`\n+^###? *(?:<(\w+)[^>]*>)?(?(?:${SEMVER_REGEXP.source}|\[${SEMVER_REGEXP.source}\]\((?[^)]+)/compare/(?[^\s)]+?)\.{2,3}(?[^\s)]+?)\)) \([0-9]{4,}(?:-[0-9]{1,2}){2}\))(?:)?$`, 'gm', ); const COMMITS_SECTION_REGEXP = /^### *Commits$(?:\n(?!#)[^\n]*)*\n*/m; @@ -201,8 +201,7 @@ async function fixChangelogSection(headingMatch, commitList, sectionRange) { .replace(COMMITS_SECTION_REGEXP, '') .replace(/\n{3,}#/g, '\n\n#') .trim(); - const commitsSectionText = ( - `### Commits\n\n
show ${commitList.length} commits\n\n` + const commitsSectionText = `### Commits\n\n
show ${commitList.length} commits\n\n` + commitList .map(commit => { const commitURL = `${repoUrlPrefix}/commit/${commit.longHash}`; @@ -210,8 +209,7 @@ async function fixChangelogSection(headingMatch, commitList, sectionRange) { return `* [\`${commit.shortHash}\`](${commitURL}) ${commitTitle}`; }) .join('\n') - + '\n\n
' - ); + + '\n\n
'; const newSectionBody = [ formattedSectionBody, commitsSectionText,