Skip to content

Commit

Permalink
Upgrade erreur to v7
Browse files Browse the repository at this point in the history
  • Loading branch information
etienne-dldc committed Apr 22, 2024
1 parent d72a4b8 commit fa387ae
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 105 deletions.
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,22 +87,22 @@
"root": true
},
"dependencies": {
"@dldc/erreur": "^6.0.6",
"@dldc/file": "^1.1.4"
"@dldc/erreur": "7.0.0-1",
"@dldc/file": "^2.0.0"
},
"devDependencies": {
"@types/node": "^20.12.7",
"@types/sql.js": "^1.4.9",
"@typescript-eslint/eslint-plugin": "^7.6.0",
"@typescript-eslint/parser": "^7.6.0",
"@typescript-eslint/eslint-plugin": "^7.7.1",
"@typescript-eslint/parser": "^7.7.1",
"@vitest/coverage-v8": "^1.5.0",
"auto-changelog": "^2.4.0",
"eslint": "^8.56.0",
"eslint-config-prettier": "^9.1.0",
"prettier": "^3.2.5",
"release-it": "^17.2.0",
"rimraf": "^5.0.5",
"sql.js": "^1.10.2",
"sql.js": "^1.10.3",
"tsup": "^8.0.2",
"typescript": "^5.4.5",
"vitest": "^1.5.0"
Expand Down
110 changes: 52 additions & 58 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 0 additions & 33 deletions src/RsyncErreur.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/apply.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { writeBuffer, writeUint32, writer } from '@dldc/file';
import { RsyncErreur } from './RsyncErreur';
import { throwInvalidDiff } from './erreur';
import { parseDiff } from './files/Diff';
import type { Data } from './types';
import { arrayBufferEquals } from './utils/arrayBufferEquals';
Expand Down Expand Up @@ -45,7 +45,7 @@ export function apply(file: Data, patch: ArrayBuffer): ArrayBuffer {
// write all matched block until nextPatch.blockIndex
while (nextMatchedBlock === null || nextMatchedBlock !== nextPatch.blockIndex) {
if (nextMatchedBlock === null) {
throw RsyncErreur.InvalidDiff(nextPatch.blockIndex);
return throwInvalidDiff(nextPatch.blockIndex);
}
nextMatchedBlock = applyMatchedBlock(nextMatchedBlock);
}
Expand Down
29 changes: 29 additions & 0 deletions src/erreur.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { createErreurStore } from '@dldc/erreur';

export type TRsyncErreurData =
| { kind: 'InvalidDiff'; blockIndex: number }
| { kind: 'BlockCountMismatch'; expected: number; actual: number }
| { kind: 'UnexpectedEof' };

const RsyncErreurInternal = createErreurStore<TRsyncErreurData>();

export const RsyncErreur = RsyncErreurInternal.asReadonly;

export function throwInvalidDiff(blockIndex: number): never {
throw RsyncErreurInternal.setAndThrow(
`Unexpected diff, patch block after ${blockIndex}, but the block is not matched`,
{ kind: 'InvalidDiff', blockIndex },
);
}

export function throwBlockCountMismatch(expected: number, actual: number): never {
throw RsyncErreurInternal.setAndThrow(`Block count mismatch, expected ${expected}, got ${actual}`, {
kind: 'BlockCountMismatch',
expected,
actual,
});
}

export function throwUnexpectedEof(): never {
throw RsyncErreurInternal.setAndThrow('Unexpected end of file', { kind: 'UnexpectedEof' });
}
6 changes: 3 additions & 3 deletions src/files/Checksum.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { readUint32, reader, writeUint32, writer } from '@dldc/file';
import { RsyncErreur } from '../RsyncErreur';
import { throwBlockCountMismatch } from '../erreur';
import { readMd5, writeMd5 } from '../utils/blocks';
import type { Md5Hash } from '../utils/md5';

Expand Down Expand Up @@ -64,7 +64,7 @@ export function parseChecksum(data: ArrayBuffer): IChecksumParser {

function readEof() {
if (blocksCount !== currentBlockCount) {
throw RsyncErreur.BlockCountMismatch(blocksCount, currentBlockCount);
return throwBlockCountMismatch(blocksCount, currentBlockCount);
}
file.readEof();
}
Expand Down Expand Up @@ -93,7 +93,7 @@ export function buildChecksum(blockSize: number, blocksCount: number): IChecksum

function getArrayBuffer(): ArrayBuffer {
if (blocksCount !== currentBlockCount) {
throw RsyncErreur.BlockCountMismatch(blocksCount, currentBlockCount);
return throwBlockCountMismatch(blocksCount, currentBlockCount);
}
return file.getArrayBuffer();
}
Expand Down
6 changes: 3 additions & 3 deletions src/files/Diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/

import { readBufferFixed, readUint32, reader, writeBuffer, writeUint32, writer } from '@dldc/file';
import { RsyncErreur } from '../RsyncErreur';
import { throwUnexpectedEof } from '../erreur';

export interface IDiffBuilder {
addMatchedBlock(index: number): void;
Expand Down Expand Up @@ -125,7 +125,7 @@ export function parseAllDiff(data: ArrayBuffer) {
for (let i = 0; i < matchedBlocksCount; i += 1) {
const blockIndex = readMatchedBlock();
if (blockIndex === null) {
throw RsyncErreur.UnexpectedEof();
return throwUnexpectedEof();
}
matchedBlocks.push(blockIndex);
}
Expand All @@ -134,7 +134,7 @@ export function parseAllDiff(data: ArrayBuffer) {
for (let i = 0; i < patchesCount; i += 1) {
const patch = readPatch();
if (patch === null) {
throw RsyncErreur.UnexpectedEof();
return throwUnexpectedEof();
}
patches.push(patch);
}
Expand Down
Loading

0 comments on commit fa387ae

Please sign in to comment.