cspell-io 7.3.8 → 8.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -6,6 +6,7 @@ import { toURL, urlBasename, urlDirname } from './node/file/util.js';
6
6
  import { RequestFsReadFile, RequestFsReadFileSync, RequestFsStat, RequestFsStatSync, RequestFsWriteFile, } from './requests/index.js';
7
7
  let defaultCSpellIONode = undefined;
8
8
  export class CSpellIONode {
9
+ serviceBus;
9
10
  constructor(serviceBus = new ServiceBus()) {
10
11
  this.serviceBus = serviceBus;
11
12
  registerHandlers(serviceBus);
@@ -1,4 +1,5 @@
1
1
  export class ErrorNotImplemented extends Error {
2
+ method;
2
3
  constructor(method) {
3
4
  super(`Method ${method} is not supported.`);
4
5
  this.method = method;
@@ -5,5 +5,14 @@ export declare class FetchUrlError extends Error implements NodeJS.ErrnoExceptio
5
5
  readonly url: URL;
6
6
  constructor(message: string, code: string | undefined, status: number | undefined, url: URL);
7
7
  static create(url: URL, status: number, message?: string): FetchUrlError;
8
+ static fromError(url: URL, e: Error): FetchUrlError;
8
9
  }
10
+ export declare function isNodeError(e: unknown): e is NodeJS.ErrnoException;
11
+ export declare function isError(e: unknown): e is Error;
12
+ interface ErrorWithOptionalCause extends Error {
13
+ cause?: NodeJS.ErrnoException;
14
+ }
15
+ export declare function isErrorWithOptionalCause(e: unknown): e is ErrorWithOptionalCause;
16
+ export declare function getCause(e: unknown): NodeJS.ErrnoException | undefined;
17
+ export {};
9
18
  //# sourceMappingURL=FetchError.d.ts.map
@@ -1,4 +1,7 @@
1
1
  export class FetchUrlError extends Error {
2
+ code;
3
+ status;
4
+ url;
2
5
  constructor(message, code, status, url) {
3
6
  super(message);
4
7
  this.code = code;
@@ -13,5 +16,31 @@ export class FetchUrlError extends Error {
13
16
  return new FetchUrlError(message || 'Permission denied.', 'EACCES', status, url);
14
17
  return new FetchUrlError(message || 'Fatal Error', 'ECONNREFUSED', status, url);
15
18
  }
19
+ static fromError(url, e) {
20
+ const cause = getCause(e);
21
+ if (cause) {
22
+ return new FetchUrlError(cause.message, cause.code, undefined, url);
23
+ }
24
+ if (isNodeError(e)) {
25
+ return new FetchUrlError(e.message, e.code, undefined, url);
26
+ }
27
+ return new FetchUrlError(e.message, undefined, undefined, url);
28
+ }
29
+ }
30
+ export function isNodeError(e) {
31
+ if (e instanceof Error && 'code' in e && typeof e.code === 'string')
32
+ return true;
33
+ if (e && typeof e === 'object' && 'code' in e && typeof e.code === 'string')
34
+ return true;
35
+ return false;
36
+ }
37
+ export function isError(e) {
38
+ return e instanceof Error;
39
+ }
40
+ export function isErrorWithOptionalCause(e) {
41
+ return !!e && typeof e === 'object' && 'cause' in e && isNodeError(e.cause);
42
+ }
43
+ export function getCause(e) {
44
+ return isErrorWithOptionalCause(e) ? e.cause : undefined;
16
45
  }
17
46
  //# sourceMappingURL=FetchError.js.map
@@ -1,6 +1,4 @@
1
1
  /// <reference types="node" resolution-mode="require"/>
2
- import type { Headers, RequestInit, Response } from 'node-fetch';
3
2
  export declare function fetchHead(request: string | URL): Promise<Headers>;
4
3
  export declare function fetchURL(url: URL): Promise<Buffer>;
5
- export declare function fetch(url: string | URL, init?: RequestInit): Promise<Response>;
6
4
  //# sourceMappingURL=fetch.d.ts.map
@@ -1,19 +1,37 @@
1
- import nodeFetch from 'node-fetch';
2
- import { FetchUrlError } from './FetchError.js';
1
+ import { FetchUrlError, isError } from './FetchError.js';
3
2
  export async function fetchHead(request) {
4
- const r = await fetch(request, { method: 'HEAD' });
5
- return r.headers;
3
+ const url = toURL(request);
4
+ try {
5
+ const r = await fetch(url, { method: 'HEAD' });
6
+ return r.headers;
7
+ }
8
+ catch (e) {
9
+ console.warn('fetchHead Error %o', e);
10
+ if (isError(e)) {
11
+ throw FetchUrlError.fromError(url, e);
12
+ }
13
+ throw e;
14
+ }
6
15
  }
7
16
  export async function fetchURL(url) {
8
- const response = await fetch(url);
9
- if (!response.ok) {
10
- throw FetchUrlError.create(url, response.status);
17
+ try {
18
+ const response = await fetch(url);
19
+ if (!response.ok) {
20
+ throw FetchUrlError.create(url, response.status);
21
+ }
22
+ return Buffer.from(await response.arrayBuffer());
23
+ }
24
+ catch (e) {
25
+ // console.warn('fetchURL Error %o', e);
26
+ if (e instanceof FetchUrlError)
27
+ throw e;
28
+ if (isError(e)) {
29
+ throw FetchUrlError.fromError(url, e);
30
+ }
31
+ throw e;
11
32
  }
12
- return Buffer.from(await response.arrayBuffer());
13
33
  }
14
- export function fetch(url, init) {
15
- /// This is a n issue with how TypeScript handles packages without `type` being set.
16
- // @ts-ignore
17
- return nodeFetch(url, init);
34
+ function toURL(url) {
35
+ return typeof url === 'string' ? new URL(url) : url;
18
36
  }
19
37
  //# sourceMappingURL=fetch.js.map
@@ -7,8 +7,7 @@ import { promisify } from 'util';
7
7
  import * as zlib from 'zlib';
8
8
  import { decode } from '../../common/encode-decode.js';
9
9
  import { createDecoderTransformer } from '../../common/transformers.js';
10
- import { fetch } from './fetch.js';
11
- import { FetchUrlError } from './FetchError.js';
10
+ import { fetchURL } from './fetch.js';
12
11
  import { isFileURL, isSupportedURL, isZipped, toURL } from './util.js';
13
12
  const defaultEncoding = 'utf8';
14
13
  const pipeline = promisify(Stream.pipeline);
@@ -25,11 +24,8 @@ function _readFileText(url, encoding) {
25
24
  return _readText(() => fs.createReadStream(filename), isZipped(filename), encoding);
26
25
  }
27
26
  async function _fetchTextFromURL(url, encoding) {
28
- const response = await fetch(url);
29
- if (!response.ok) {
30
- throw FetchUrlError.create(url, response.status);
31
- }
32
- return _readText(() => response.body, isZipped(url), encoding);
27
+ const buffer = await fetchURL(url);
28
+ return _readText(() => Stream.Readable.from(buffer), isZipped(url), encoding);
33
29
  }
34
30
  async function _readText(getStream, isZipped, encoding) {
35
31
  const stream = getStream();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cspell-io",
3
- "version": "7.3.8",
3
+ "version": "8.0.0",
4
4
  "description": "A library of useful I/O functions used across various cspell tools.",
5
5
  "type": "module",
6
6
  "types": "dist/esm/index.d.ts",
@@ -43,16 +43,14 @@
43
43
  },
44
44
  "homepage": "https://github.com/streetsidesoftware/cspell#readme",
45
45
  "engines": {
46
- "node": ">=16"
46
+ "node": ">=18"
47
47
  },
48
48
  "devDependencies": {
49
- "@types/node-fetch": "^2.6.6",
50
49
  "lorem-ipsum": "^2.0.8",
51
50
  "typescript": "^5.2.2"
52
51
  },
53
52
  "dependencies": {
54
- "@cspell/cspell-service-bus": "7.3.8",
55
- "node-fetch": "^2.7.0"
53
+ "@cspell/cspell-service-bus": "8.0.0"
56
54
  },
57
- "gitHead": "6717f5726b74c695d9023dbccf6f7e8a7ac6361f"
55
+ "gitHead": "67c22bf98baed1c17bbc658fba8656262d17e370"
58
56
  }