cspell-io
Advanced tools
Comparing version 7.3.9 to 8.0.0
@@ -9,2 +9,3 @@ import { isServiceResponseSuccess, ServiceBus } from '@cspell/cspell-service-bus'; | ||
export class CSpellIONode { | ||
serviceBus; | ||
constructor(serviceBus = new ServiceBus()) { | ||
@@ -11,0 +12,0 @@ this.serviceBus = serviceBus; |
export class ErrorNotImplemented extends Error { | ||
method; | ||
constructor(method) { | ||
@@ -3,0 +4,0 @@ super(`Method ${method} is not supported.`); |
/// <reference types="node" resolution-mode="require"/> | ||
import type { Headers, RequestInit, Response } from 'node-fetch'; | ||
export declare function fetchHead(request: string | URL): Promise<Headers>; | ||
export declare function fetchURL(url: URL): Promise<Buffer>; | ||
export declare function fetch(url: string | URL, init?: RequestInit): Promise<Response>; | ||
//# sourceMappingURL=fetch.d.ts.map |
@@ -1,19 +0,37 @@ | ||
import nodeFetch from 'node-fetch'; | ||
import { FetchUrlError } from './FetchError.js'; | ||
import { FetchUrlError, isError } from './FetchError.js'; | ||
export async function fetchHead(request) { | ||
const r = await fetch(request, { method: 'HEAD' }); | ||
return r.headers; | ||
const url = toURL(request); | ||
try { | ||
const r = await fetch(url, { method: 'HEAD' }); | ||
return r.headers; | ||
} | ||
catch (e) { | ||
console.warn('fetchHead Error %o', e); | ||
if (isError(e)) { | ||
throw FetchUrlError.fromError(url, e); | ||
} | ||
throw e; | ||
} | ||
} | ||
export async function fetchURL(url) { | ||
const response = await fetch(url); | ||
if (!response.ok) { | ||
throw FetchUrlError.create(url, response.status); | ||
try { | ||
const response = await fetch(url); | ||
if (!response.ok) { | ||
throw FetchUrlError.create(url, response.status); | ||
} | ||
return Buffer.from(await response.arrayBuffer()); | ||
} | ||
return Buffer.from(await response.arrayBuffer()); | ||
catch (e) { | ||
// console.warn('fetchURL Error %o', e); | ||
if (e instanceof FetchUrlError) | ||
throw e; | ||
if (isError(e)) { | ||
throw FetchUrlError.fromError(url, e); | ||
} | ||
throw e; | ||
} | ||
} | ||
export function fetch(url, init) { | ||
/// This is a n issue with how TypeScript handles packages without `type` being set. | ||
// @ts-ignore | ||
return nodeFetch(url, init); | ||
function toURL(url) { | ||
return typeof url === 'string' ? new URL(url) : url; | ||
} | ||
//# sourceMappingURL=fetch.js.map |
@@ -8,3 +8,12 @@ /// <reference types="node" resolution-mode="require"/> | ||
static create(url: URL, status: number, message?: string): FetchUrlError; | ||
static fromError(url: URL, e: Error): FetchUrlError; | ||
} | ||
export declare function isNodeError(e: unknown): e is NodeJS.ErrnoException; | ||
export declare function isError(e: unknown): e is Error; | ||
interface ErrorWithOptionalCause extends Error { | ||
cause?: NodeJS.ErrnoException; | ||
} | ||
export declare function isErrorWithOptionalCause(e: unknown): e is ErrorWithOptionalCause; | ||
export declare function getCause(e: unknown): NodeJS.ErrnoException | undefined; | ||
export {}; | ||
//# sourceMappingURL=FetchError.d.ts.map |
export class FetchUrlError extends Error { | ||
code; | ||
status; | ||
url; | ||
constructor(message, code, status, url) { | ||
@@ -16,3 +19,29 @@ super(message); | ||
} | ||
static fromError(url, e) { | ||
const cause = getCause(e); | ||
if (cause) { | ||
return new FetchUrlError(cause.message, cause.code, undefined, url); | ||
} | ||
if (isNodeError(e)) { | ||
return new FetchUrlError(e.message, e.code, undefined, url); | ||
} | ||
return new FetchUrlError(e.message, undefined, undefined, url); | ||
} | ||
} | ||
export function isNodeError(e) { | ||
if (e instanceof Error && 'code' in e && typeof e.code === 'string') | ||
return true; | ||
if (e && typeof e === 'object' && 'code' in e && typeof e.code === 'string') | ||
return true; | ||
return false; | ||
} | ||
export function isError(e) { | ||
return e instanceof Error; | ||
} | ||
export function isErrorWithOptionalCause(e) { | ||
return !!e && typeof e === 'object' && 'cause' in e && isNodeError(e.cause); | ||
} | ||
export function getCause(e) { | ||
return isErrorWithOptionalCause(e) ? e.cause : undefined; | ||
} | ||
//# sourceMappingURL=FetchError.js.map |
@@ -10,4 +10,3 @@ // cSpell:ignore curr | ||
import { createDecoderTransformer } from '../../common/transformers.js'; | ||
import { fetch } from './fetch.js'; | ||
import { FetchUrlError } from './FetchError.js'; | ||
import { fetchURL } from './fetch.js'; | ||
import { isFileURL, isSupportedURL, isZipped, toURL } from './util.js'; | ||
@@ -29,7 +28,4 @@ const defaultEncoding = 'utf8'; | ||
async function _fetchTextFromURL(url, encoding) { | ||
const response = await fetch(url); | ||
if (!response.ok) { | ||
throw FetchUrlError.create(url, response.status); | ||
} | ||
return _readText(() => response.body, isZipped(url), encoding); | ||
const buffer = await fetchURL(url); | ||
return _readText(() => Stream.Readable.from(buffer), isZipped(url), encoding); | ||
} | ||
@@ -36,0 +32,0 @@ async function _readText(getStream, isZipped, encoding) { |
{ | ||
"name": "cspell-io", | ||
"version": "7.3.9", | ||
"version": "8.0.0", | ||
"description": "A library of useful I/O functions used across various cspell tools.", | ||
@@ -46,6 +46,5 @@ "type": "module", | ||
"engines": { | ||
"node": ">=16" | ||
"node": ">=18" | ||
}, | ||
"devDependencies": { | ||
"@types/node-fetch": "^2.6.8", | ||
"lorem-ipsum": "^2.0.8", | ||
@@ -55,6 +54,5 @@ "typescript": "^5.2.2" | ||
"dependencies": { | ||
"@cspell/cspell-service-bus": "7.3.9", | ||
"node-fetch": "^2.7.0" | ||
"@cspell/cspell-service-bus": "8.0.0" | ||
}, | ||
"gitHead": "77c7fb3ffd80a626ec07d704cefcaa21d62fd460" | ||
"gitHead": "67c22bf98baed1c17bbc658fba8656262d17e370" | ||
} |
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
60188
1
2
1298
2
+ Added@cspell/cspell-service-bus@8.0.0(transitive)
- Removednode-fetch@^2.7.0
- Removed@cspell/cspell-service-bus@7.3.9(transitive)
- Removednode-fetch@2.7.0(transitive)
- Removedtr46@0.0.3(transitive)
- Removedwebidl-conversions@3.0.1(transitive)
- Removedwhatwg-url@5.0.0(transitive)