cspell-io
Advanced tools
Comparing version 8.16.0 to 8.16.1
import type { BufferEncoding } from '../models/BufferEncoding.js'; | ||
import type { FileReference, UrlOrReference } from '../models/FileResource.js'; | ||
import type { FileReference, FileResourceRequest, UrlOrReference } from '../models/FileResource.js'; | ||
export declare class CFileReference implements FileReference { | ||
@@ -35,2 +35,3 @@ readonly url: URL; | ||
export declare function renameFileReference(ref: FileReference, newUrl: URL): FileReference; | ||
export declare function toFileResourceRequest(file: UrlOrReference, encoding?: BufferEncoding, signal?: AbortSignal): FileResourceRequest; | ||
//# sourceMappingURL=CFileReference.d.ts.map |
@@ -57,2 +57,8 @@ import { toFileURL } from '../node/file/url.js'; | ||
} | ||
export function toFileResourceRequest(file, encoding, signal) { | ||
const fileReference = typeof file === 'string' ? toFileURL(file) : file; | ||
if (fileReference instanceof URL) | ||
return { url: fileReference, encoding, signal }; | ||
return { url: fileReference.url, encoding: encoding ?? fileReference.encoding, signal }; | ||
} | ||
//# sourceMappingURL=CFileReference.js.map |
@@ -13,2 +13,3 @@ import type { BufferEncoding } from '../models/BufferEncoding.js'; | ||
getText(encoding?: BufferEncoding): string; | ||
getBytes(): Uint8Array; | ||
toJson(): { | ||
@@ -15,0 +16,0 @@ url: string; |
import { assert } from '../errors/assert.js'; | ||
import { decode, isGZipped } from './encode-decode.js'; | ||
import { decode, encodeString, isGZipped } from './encode-decode.js'; | ||
export class CFileResource { | ||
@@ -33,2 +33,8 @@ url; | ||
} | ||
getBytes() { | ||
const arrayBufferview = typeof this.content === 'string' ? encodeString(this.content, this.encoding) : this.content; | ||
return arrayBufferview instanceof Uint8Array | ||
? arrayBufferview | ||
: new Uint8Array(arrayBufferview.buffer, arrayBufferview.byteOffset, arrayBufferview.byteLength); | ||
} | ||
toJson() { | ||
@@ -35,0 +41,0 @@ return { |
@@ -9,3 +9,3 @@ /* eslint-disable unicorn/text-encoding-identifier-case */ | ||
const decoderUTF16BE = createTextDecoderUtf16BE(); | ||
// const encoderUTF8 = new TextEncoder(); | ||
const encoderUTF8 = new TextEncoder(); | ||
// const encoderUTF16LE = new TextEncoder('utf-16le'); | ||
@@ -64,2 +64,7 @@ export function decodeUtf16LE(data) { | ||
switch (encoding) { | ||
case undefined: | ||
case 'utf-8': | ||
case 'utf8': { | ||
return encoderUTF8.encode(str); | ||
} | ||
case 'utf-16be': | ||
@@ -66,0 +71,0 @@ case 'utf16be': { |
import type { BufferEncoding } from './models/BufferEncoding.js'; | ||
import type { FileReference, TextFileResource, UrlOrFilename, UrlOrReference } from './models/FileResource.js'; | ||
import type { DirEntry, Stats } from './models/index.js'; | ||
export interface ReadFileOptions { | ||
signal?: AbortSignal; | ||
encoding?: BufferEncoding; | ||
} | ||
export type ReadFileOptionsOrEncoding = ReadFileOptions | BufferEncoding; | ||
export interface CSpellIO { | ||
@@ -8,6 +13,6 @@ /** | ||
* @param urlOrFilename - uri of the file to read | ||
* @param encoding - optional encoding. | ||
* @param options - optional options for reading the file. | ||
* @returns A TextFileResource. | ||
*/ | ||
readFile(urlOrFilename: UrlOrReference, encoding?: BufferEncoding): Promise<TextFileResource>; | ||
readFile(urlOrFilename: UrlOrReference, options?: ReadFileOptionsOrEncoding): Promise<TextFileResource>; | ||
/** | ||
@@ -92,2 +97,3 @@ * Read a file in Sync mode. | ||
} | ||
export declare function toReadFileOptions(options?: ReadFileOptionsOrEncoding): ReadFileOptions | undefined; | ||
//# sourceMappingURL=CSpellIO.d.ts.map |
@@ -1,2 +0,9 @@ | ||
export {}; | ||
export function toReadFileOptions(options) { | ||
if (!options) | ||
return options; | ||
if (typeof options === 'string') { | ||
return { encoding: options }; | ||
} | ||
return options; | ||
} | ||
//# sourceMappingURL=CSpellIO.js.map |
import { ServiceBus } from '@cspell/cspell-service-bus'; | ||
import type { CSpellIO } from './CSpellIO.js'; | ||
import type { CSpellIO, ReadFileOptionsOrEncoding } from './CSpellIO.js'; | ||
import type { BufferEncoding, DirEntry, FileReference, Stats, TextFileResource, UrlOrReference } from './models/index.js'; | ||
@@ -7,3 +7,3 @@ export declare class CSpellIONode implements CSpellIO { | ||
constructor(serviceBus?: ServiceBus); | ||
readFile(urlOrFilename: UrlOrReference, encoding?: BufferEncoding): Promise<TextFileResource>; | ||
readFile(urlOrFilename: UrlOrReference, options?: ReadFileOptionsOrEncoding): Promise<TextFileResource>; | ||
readDirectory(urlOrFilename: string | URL): Promise<DirEntry[]>; | ||
@@ -10,0 +10,0 @@ readFileSync(urlOrFilename: UrlOrReference, encoding?: BufferEncoding): TextFileResource; |
import { isServiceResponseSuccess, ServiceBus } from '@cspell/cspell-service-bus'; | ||
import { isFileReference, toFileReference } from './common/CFileReference.js'; | ||
import { isFileReference, toFileReference, toFileResourceRequest } from './common/CFileReference.js'; | ||
import { CFileResource } from './common/CFileResource.js'; | ||
import { compareStats } from './common/stat.js'; | ||
import { toReadFileOptions } from './CSpellIO.js'; | ||
import { ErrorNotImplemented } from './errors/errors.js'; | ||
@@ -17,4 +18,5 @@ import { registerHandlers } from './handlers/node/file.js'; | ||
} | ||
readFile(urlOrFilename, encoding) { | ||
const ref = toFileReference(urlOrFilename, encoding); | ||
readFile(urlOrFilename, options) { | ||
const readOptions = toReadFileOptions(options); | ||
const ref = toFileResourceRequest(urlOrFilename, readOptions?.encoding, readOptions?.signal); | ||
const res = this.serviceBus.dispatch(RequestFsReadFile.create(ref)); | ||
@@ -21,0 +23,0 @@ if (!isServiceResponseSuccess(res)) { |
@@ -121,3 +121,3 @@ import { urlOrReferenceToUrl } from './common/index.js'; | ||
stat: async (url) => gfs(url, 'stat').stat(url), | ||
readFile: async (url) => gfs(url, 'readFile').readFile(url), | ||
readFile: async (url, options) => gfs(url, 'readFile').readFile(url, options), | ||
writeFile: async (file) => gfs(file, 'writeFile').writeFile(file), | ||
@@ -124,0 +124,0 @@ readDirectory: async (url) => gfs(url, 'readDirectory') |
@@ -56,6 +56,6 @@ import { promises as fs, readFileSync, statSync } from 'node:fs'; | ||
const handleRequestFsReadFileHttp = RequestFsReadFile.createRequestHandler((req, next) => { | ||
const { url } = req.params; | ||
const { url, signal, encoding } = req.params; | ||
if (!(url.protocol in supportedFetchProtocols)) | ||
return next(req); | ||
return createResponse(fetchURL(url).then((content) => CFileResource.from({ ...req.params, content }))); | ||
return createResponse(fetchURL(url, signal).then((content) => CFileResource.from({ url, encoding, content }))); | ||
}, undefined, 'Node: Read Http(s) file.'); | ||
@@ -62,0 +62,0 @@ /** |
@@ -23,2 +23,16 @@ import type { BufferEncoding } from './BufferEncoding.js'; | ||
} | ||
export interface FileResourceRequest { | ||
/** | ||
* The URL of the File | ||
*/ | ||
readonly url: URL; | ||
/** | ||
* The encoding to use when reading the file. | ||
*/ | ||
readonly encoding?: BufferEncoding | undefined; | ||
/** | ||
* The signal to use to abort the request. | ||
*/ | ||
readonly signal?: AbortSignal | undefined; | ||
} | ||
export interface FileResource extends FileReference { | ||
@@ -38,2 +52,6 @@ /** | ||
getText(encoding?: BufferEncoding): string; | ||
/** | ||
* Get the bytes of the file. | ||
*/ | ||
getBytes(): Uint8Array; | ||
} | ||
@@ -40,0 +58,0 @@ export type UrlOrFilename = string | URL; |
export declare function fetchHead(request: string | URL): Promise<Headers>; | ||
export declare function fetchURL(url: URL): Promise<Buffer>; | ||
export declare function fetchURL(url: URL, signal?: AbortSignal): Promise<Buffer>; | ||
//# sourceMappingURL=fetch.d.ts.map |
@@ -17,5 +17,7 @@ import { _fetch as fetch } from './_fetch.js'; | ||
} | ||
export async function fetchURL(url) { | ||
export async function fetchURL(url, signal) { | ||
try { | ||
const response = await fetch(url); | ||
// eslint-disable-next-line n/no-unsupported-features/node-builtins | ||
const request = signal ? new Request(url, { signal }) : url; | ||
const response = await fetch(request); | ||
if (!response.ok) { | ||
@@ -22,0 +24,0 @@ throw FetchUrlError.create(url, response.status); |
import type { ServiceRequestFactoryRequestType } from '@cspell/cspell-service-bus'; | ||
import type { BufferEncoding } from '../models/BufferEncoding.js'; | ||
import type { TextFileResource } from '../models/FileResource.js'; | ||
interface RequestParams { | ||
readonly url: URL; | ||
readonly encoding?: BufferEncoding | undefined; | ||
} | ||
export declare const RequestFsReadFile: import("@cspell/cspell-service-bus").ServiceRequestFactory<import("@cspell/cspell-service-bus").ServiceRequest<"fs:readFile", RequestParams, Promise<TextFileResource>>, RequestParams, "fs:readFile">; | ||
import type { FileResourceRequest, TextFileResource } from '../models/FileResource.js'; | ||
export declare const RequestFsReadFile: import("@cspell/cspell-service-bus").ServiceRequestFactory<import("@cspell/cspell-service-bus").ServiceRequest<"fs:readFile", FileResourceRequest, Promise<TextFileResource>>, FileResourceRequest, "fs:readFile">; | ||
export type RequestFsReadFile = ServiceRequestFactoryRequestType<typeof RequestFsReadFile>; | ||
export {}; | ||
//# sourceMappingURL=RequestFsReadFile.d.ts.map |
@@ -16,2 +16,6 @@ import type { BufferEncoding, DirEntry, FileReference, FileResource, Stats, TextFileResource } from './models/index.js'; | ||
} | ||
export interface ReadFileOptions { | ||
signal?: AbortSignal; | ||
encoding?: BufferEncoding; | ||
} | ||
export interface VFileSystemCore { | ||
@@ -24,4 +28,11 @@ /** | ||
*/ | ||
readFile(url: UrlOrReference, encoding?: BufferEncoding): Promise<TextFileResource>; | ||
readFile(url: UrlOrReference, encoding: BufferEncoding): Promise<TextFileResource>; | ||
/** | ||
* Read a file. | ||
* @param url - URL to read | ||
* @param options - options for reading the file. | ||
* @returns A FileResource, the content will not be decoded. Use `.getText()` to get the decoded text. | ||
*/ | ||
readFile(url: UrlOrReference, options?: ReadFileOptions | BufferEncoding): Promise<TextFileResource>; | ||
/** | ||
* Write a file | ||
@@ -28,0 +39,0 @@ * @param file - the file to write |
@@ -29,4 +29,9 @@ import type { DirEntry, Disposable, FileReference, FileResource, Stats } from './models/index.js'; | ||
} | ||
export interface OptionAbort { | ||
signal?: AbortSignal; | ||
} | ||
export type VProviderFileSystemReadFileOptions = OptionAbort; | ||
export type VProviderFileSystemReadDirectoryOptions = OptionAbort; | ||
export interface VProviderFileSystem extends Disposable { | ||
readFile(url: UrlOrReference): Promise<FileResource>; | ||
readFile(url: UrlOrReference, options?: VProviderFileSystemReadFileOptions): Promise<FileResource>; | ||
writeFile(file: FileResource): Promise<FileReference>; | ||
@@ -33,0 +38,0 @@ /** |
@@ -96,5 +96,5 @@ import assert from 'node:assert'; | ||
}, | ||
readFile: async (url) => { | ||
readFile: async (url, options) => { | ||
const url2 = mapUrlOrReferenceToPrivate(url); | ||
const file = await fs.readFile(url2); | ||
const file = await fs.readFile(url2, options); | ||
return mapFileResourceToPublic(file); | ||
@@ -101,0 +101,0 @@ }, |
@@ -5,4 +5,4 @@ import type { CSpellIO } from '../CSpellIO.js'; | ||
import type { LogEvent } from '../models/LogEvent.js'; | ||
import { FileSystemProviderInfo, FSCapabilities, FSCapabilityFlags, UrlOrReference, VFileSystemCore, VfsDirEntry, VfsStat } from '../VFileSystem.js'; | ||
import { VFileSystemProvider, VProviderFileSystem } from '../VirtualFS.js'; | ||
import { FileSystemProviderInfo, FSCapabilities, FSCapabilityFlags, ReadFileOptions, UrlOrReference, VFileSystemCore, VfsDirEntry, VfsStat } from '../VFileSystem.js'; | ||
import type { VFileSystemProvider, VProviderFileSystem } from '../VirtualFS.js'; | ||
export declare function cspellIOToFsProvider(cspellIO: CSpellIO): VFileSystemProvider; | ||
@@ -32,3 +32,3 @@ export declare class VFSError extends Error { | ||
stat(urlRef: UrlOrReference): Promise<VfsStat>; | ||
readFile(urlRef: UrlOrReference, encoding?: BufferEncoding): Promise<TextFileResource>; | ||
readFile(urlRef: UrlOrReference, optionsOrEncoding?: BufferEncoding | ReadFileOptions): Promise<TextFileResource>; | ||
readDirectory(url: URL): Promise<VfsDirEntry[]>; | ||
@@ -35,0 +35,0 @@ writeFile(file: FileResource): Promise<FileReference>; |
@@ -17,3 +17,3 @@ import { createTextFileResource, urlOrReferenceToUrl } from '../common/index.js'; | ||
stat: (url) => cspellIO.getStat(url), | ||
readFile: (url) => cspellIO.readFile(url), | ||
readFile: (url, options) => cspellIO.readFile(url, options), | ||
readDirectory: (url) => cspellIO.readDirectory(url), | ||
@@ -119,3 +119,3 @@ writeFile: (file) => cspellIO.writeFile(file.url, file.content), | ||
} | ||
async readFile(urlRef, encoding) { | ||
async readFile(urlRef, optionsOrEncoding) { | ||
const traceID = performance.now(); | ||
@@ -126,3 +126,4 @@ const url = urlOrReferenceToUrl(urlRef); | ||
checkCapabilityOrThrow(this.fs, this.capabilities, FSCapabilityFlags.Read, 'readFile', url); | ||
return createTextFileResource(await this.fs.readFile(urlRef), encoding); | ||
const readOptions = toOptions(optionsOrEncoding); | ||
return createTextFileResource(await this.fs.readFile(urlRef, readOptions), readOptions?.encoding); | ||
} | ||
@@ -253,2 +254,5 @@ catch (e) { | ||
} | ||
function toOptions(val) { | ||
return typeof val === 'string' ? { encoding: val } : val; | ||
} | ||
//# sourceMappingURL=WrappedProviderFs.js.map |
@@ -7,3 +7,3 @@ { | ||
}, | ||
"version": "8.16.0", | ||
"version": "8.16.1", | ||
"description": "A library of useful I/O functions used across various cspell tools.", | ||
@@ -57,9 +57,10 @@ "type": "module", | ||
"lorem-ipsum": "^2.0.8", | ||
"typescript": "~5.6.3" | ||
"typescript": "~5.6.3", | ||
"vitest-fetch-mock": "^0.4.2" | ||
}, | ||
"dependencies": { | ||
"@cspell/cspell-service-bus": "8.16.0", | ||
"@cspell/url": "8.16.0" | ||
"@cspell/cspell-service-bus": "8.16.1", | ||
"@cspell/url": "8.16.1" | ||
}, | ||
"gitHead": "41cd50f9ba34033b6da32408855d7fc3b888c5e0" | ||
"gitHead": "773bb6e701ff0b7fbeb30e4621119bb4315415ad" | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
116070
2730
3
+ Added@cspell/cspell-service-bus@8.16.1(transitive)
+ Added@cspell/url@8.16.1(transitive)
- Removed@cspell/cspell-service-bus@8.16.0(transitive)
- Removed@cspell/url@8.16.0(transitive)
Updated@cspell/url@8.16.1