Skip to content

Commit

Permalink
feat: Use CSpellIO as the default file read layer. (#3323)
Browse files Browse the repository at this point in the history
* feat: Use CSpellIO as the default file read layer.
* Update BufferEncoding.ts
* fix: use `fileURLToPath` to workaround yarn bug
  • Loading branch information
Jason3S committed Aug 1, 2022
1 parent 4d4f350 commit c1ebd70
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 6 deletions.
12 changes: 12 additions & 0 deletions packages/cspell-io/src/CSpellIONode.ts
Expand Up @@ -14,6 +14,8 @@ import {
RequestFsWriteFile,
} from './requests';

let defaultCSpellIONode: CSpellIO | undefined = undefined;

export class CSpellIONode implements CSpellIO {
constructor(readonly serviceBus = new ServiceBus()) {
registerHandlers(serviceBus);
Expand Down Expand Up @@ -67,3 +69,13 @@ export class CSpellIONode implements CSpellIO {
function genError(err: Error | undefined, alt: string): Error {
return err || new ErrorNotImplemented(alt);
}

export function getDefaultCSpellIO(): CSpellIO {
if (defaultCSpellIONode) return defaultCSpellIONode;

const cspellIO = new CSpellIONode();

defaultCSpellIONode = cspellIO;

return cspellIO;
}
40 changes: 40 additions & 0 deletions packages/cspell-io/src/file/file.ts
@@ -0,0 +1,40 @@
import { getDefaultCSpellIO } from '../CSpellIONode';
import { toError } from '../errors';
import { Stats } from '../models';
import type { BufferEncoding } from '../models/BufferEncoding';
import type {
getStat as GetStatFn,
getStatSync as GetStatSyncFn,
readFile as ReadFileFn,
readFileSync as ReadFileSyncFn,
} from '../node/file';

export const readFile: typeof ReadFileFn = function (
filename: string | URL,
encoding?: BufferEncoding
): Promise<string> {
return getDefaultCSpellIO()
.readFile(filename, encoding)
.then((fr) => fr.content);
};

export const readFileSync: typeof ReadFileSyncFn = function (
filename: string | URL,
encoding?: BufferEncoding
): string {
return getDefaultCSpellIO().readFileSync(filename, encoding).content;
};

export const getStat: typeof GetStatFn = function (filenameOrUri: string): Promise<Stats | Error> {
return getDefaultCSpellIO()
.getStat(filenameOrUri)
.catch((e) => toError(e));
};

export const getStatSync: typeof GetStatSyncFn = function (filenameOrUri: string): Stats | Error {
try {
return getDefaultCSpellIO().getStatSync(filenameOrUri);
} catch (e) {
return toError(e);
}
};
3 changes: 1 addition & 2 deletions packages/cspell-io/src/file/index.ts
@@ -1,3 +1,2 @@
export { readFile, readFileSync } from './../node/file';
export { writeToFile, writeToFileIterable, writeToFileIterableP } from './../node/file';
export { getStat, getStatSync } from './../node/file';
export { getStat, getStatSync, readFile, readFileSync } from './file';
14 changes: 10 additions & 4 deletions packages/cspell-io/src/handlers/node/file.ts
Expand Up @@ -22,18 +22,24 @@ import {
RequestFsWriteFile,
RequestZlibInflate,
} from '../../requests';
import { URL, fileURLToPath } from 'url';

const isGzFileRegExp = /\.gz($|[?#])/;

function isGzFile(url: URL): boolean {
return isGzFileRegExp.test(url.pathname);
}

/*
* NOTE: fileURLToPath is used because of yarn bug https://github.com/yarnpkg/berry/issues/899
*/

/**
* Handle Binary File Reads
*/
const handleRequestFsReadBinaryFile = RequestFsReadBinaryFile.createRequestHandler(
({ params }) => createResponse(fs.readFile(params.url).then((content) => ({ url: params.url, content }))),
({ params }) =>
createResponse(fs.readFile(fileURLToPath(params.url)).then((content) => ({ url: params.url, content }))),
undefined,
'Node: Read Binary File.'
);
Expand All @@ -42,7 +48,7 @@ const handleRequestFsReadBinaryFile = RequestFsReadBinaryFile.createRequestHandl
* Handle Binary File Sync Reads
*/
const handleRequestFsReadBinaryFileSync = RequestFsReadBinaryFileSync.createRequestHandler(
({ params }) => createResponse({ url: params.url, content: readFileSync(params.url) }),
({ params }) => createResponse({ url: params.url, content: readFileSync(fileURLToPath(params.url)) }),
undefined,
'Node: Sync Read Binary File.'
);
Expand Down Expand Up @@ -151,7 +157,7 @@ function bufferToText(buf: Buffer, encoding: BufferEncoding): string {
* Handle fs:stat
*/
const handleRequestFsStat = RequestFsStat.createRequestHandler(
({ params }) => createResponse(fs.stat(params.url)),
({ params }) => createResponse(fs.stat(fileURLToPath(params.url))),
undefined,
'Node: fs.stat.'
);
Expand All @@ -163,7 +169,7 @@ const handleRequestFsStatSync = RequestFsStatSync.createRequestHandler(
(req) => {
const { params } = req;
try {
return createResponse(statSync(params.url));
return createResponse(statSync(fileURLToPath(params.url)));
} catch (e) {
return createResponseFail(req, toError(e));
}
Expand Down
1 change: 1 addition & 0 deletions packages/cspell-io/src/models/BufferEncoding.ts
Expand Up @@ -7,5 +7,6 @@ export type BufferEncoding =
| 'ucs-2'
| 'base64'
| 'base64url'
| 'binary'
| 'latin1'
| 'hex';
1 change: 1 addition & 0 deletions packages/cspell-io/src/node/file/fileReader.ts
Expand Up @@ -6,6 +6,7 @@ import { fetch } from './fetch';
import { FetchUrlError } from './FetchError';
import { toURL, isSupportedURL, isFileURL, isZipped } from './util';
import { fileURLToPath } from 'url';
import type { BufferEncoding } from '../../models/BufferEncoding';

const defaultEncoding: BufferEncoding = 'utf8';

Expand Down

0 comments on commit c1ebd70

Please sign in to comment.