cspell-io 6.1.3 → 6.2.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.
- package/dist/file/FetchError.d.ts +10 -0
- package/dist/file/FetchError.js +21 -0
- package/dist/file/fetch.d.ts +6 -0
- package/dist/file/fetch.js +14 -0
- package/dist/file/fileReader.d.ts +2 -1
- package/dist/file/fileReader.js +30 -8
- package/dist/file/index.d.ts +4 -2
- package/dist/file/index.js +11 -16
- package/dist/file/stat.d.ts +13 -0
- package/dist/file/stat.js +48 -0
- package/dist/file/util.d.ts +8 -0
- package/dist/file/util.js +29 -0
- package/package.json +7 -3
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
/// <reference types="node" />
|
|
3
|
+
export declare class FetchUrlError extends Error implements NodeJS.ErrnoException {
|
|
4
|
+
readonly code: string | undefined;
|
|
5
|
+
readonly status: number | undefined;
|
|
6
|
+
readonly url: URL;
|
|
7
|
+
constructor(message: string, code: string | undefined, status: number | undefined, url: URL);
|
|
8
|
+
static create(url: URL, status: number, message?: string): FetchUrlError;
|
|
9
|
+
}
|
|
10
|
+
//# sourceMappingURL=FetchError.d.ts.map
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.FetchUrlError = void 0;
|
|
4
|
+
class FetchUrlError extends Error {
|
|
5
|
+
constructor(message, code, status, url) {
|
|
6
|
+
super(message);
|
|
7
|
+
this.code = code;
|
|
8
|
+
this.status = status;
|
|
9
|
+
this.url = url;
|
|
10
|
+
this.name = 'FetchUrlError';
|
|
11
|
+
}
|
|
12
|
+
static create(url, status, message) {
|
|
13
|
+
if (status === 404)
|
|
14
|
+
return new FetchUrlError(message || 'URL not found.', 'ENOENT', status, url);
|
|
15
|
+
if (status >= 400 && status < 500)
|
|
16
|
+
return new FetchUrlError(message || 'Permission denied.', 'EACCES', status, url);
|
|
17
|
+
return new FetchUrlError(message || 'Fatal Error', 'ECONNREFUSED', status, url);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
exports.FetchUrlError = FetchUrlError;
|
|
21
|
+
//# sourceMappingURL=FetchError.js.map
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import type { Headers } from 'node-fetch';
|
|
3
|
+
import nodeFetch from 'node-fetch';
|
|
4
|
+
export declare const fetch: typeof nodeFetch;
|
|
5
|
+
export declare function fetchHead(request: string | URL): Promise<Headers>;
|
|
6
|
+
//# sourceMappingURL=fetch.d.ts.map
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.fetchHead = exports.fetch = void 0;
|
|
7
|
+
const node_fetch_1 = __importDefault(require("node-fetch"));
|
|
8
|
+
exports.fetch = node_fetch_1.default;
|
|
9
|
+
async function fetchHead(request) {
|
|
10
|
+
const r = await (0, exports.fetch)(request, { method: 'HEAD' });
|
|
11
|
+
return r.headers;
|
|
12
|
+
}
|
|
13
|
+
exports.fetchHead = fetchHead;
|
|
14
|
+
//# sourceMappingURL=fetch.js.map
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
-
|
|
2
|
+
/// <reference types="node" />
|
|
3
|
+
export declare function readFile(filename: string | URL, encoding?: BufferEncoding): Promise<string>;
|
|
3
4
|
export declare function readFileSync(filename: string, encoding?: BufferEncoding): string;
|
|
4
5
|
//# sourceMappingURL=fileReader.d.ts.map
|
package/dist/file/fileReader.js
CHANGED
|
@@ -28,11 +28,35 @@ exports.readFileSync = exports.readFile = void 0;
|
|
|
28
28
|
// cSpell:words zlib iconv
|
|
29
29
|
const fs = __importStar(require("fs"));
|
|
30
30
|
const zlib = __importStar(require("zlib"));
|
|
31
|
+
const fetch_1 = require("./fetch");
|
|
32
|
+
const FetchError_1 = require("./FetchError");
|
|
33
|
+
const util_1 = require("./util");
|
|
34
|
+
const url_1 = require("url");
|
|
31
35
|
const defaultEncoding = 'utf8';
|
|
32
|
-
function readFile(filename, encoding
|
|
36
|
+
async function readFile(filename, encoding) {
|
|
37
|
+
const url = (0, util_1.toURL)(filename);
|
|
38
|
+
if (!(0, util_1.isSupportedURL)(url)) {
|
|
39
|
+
throw new Error('Unsupported network protocol');
|
|
40
|
+
}
|
|
41
|
+
return (0, util_1.isFileURL)(url) ? _readFile(url, encoding) : _fetchURL(url, encoding);
|
|
42
|
+
}
|
|
43
|
+
exports.readFile = readFile;
|
|
44
|
+
function _readFile(url, encoding) {
|
|
45
|
+
// Convert it to a string because Yarn2 cannot handle URLs.
|
|
46
|
+
const filename = (0, url_1.fileURLToPath)(url);
|
|
47
|
+
return _read(() => fs.createReadStream(filename), (0, util_1.isZipped)(filename), encoding);
|
|
48
|
+
}
|
|
49
|
+
async function _fetchURL(url, encoding) {
|
|
50
|
+
const response = await (0, fetch_1.fetch)(url);
|
|
51
|
+
if (!response.ok) {
|
|
52
|
+
throw FetchError_1.FetchUrlError.create(url, response.status);
|
|
53
|
+
}
|
|
54
|
+
return _read(() => response.body, (0, util_1.isZipped)(url), encoding);
|
|
55
|
+
}
|
|
56
|
+
function _read(getStream, isZipped, encoding = defaultEncoding) {
|
|
33
57
|
return new Promise((resolve, reject) => {
|
|
34
58
|
const data = [];
|
|
35
|
-
const stream = prepareFileStream(
|
|
59
|
+
const stream = prepareFileStream(getStream, isZipped, encoding, reject);
|
|
36
60
|
let resolved = false;
|
|
37
61
|
function complete() {
|
|
38
62
|
resolve(data.join(''));
|
|
@@ -44,14 +68,12 @@ function readFile(filename, encoding = defaultEncoding) {
|
|
|
44
68
|
stream.on('end', complete);
|
|
45
69
|
});
|
|
46
70
|
}
|
|
47
|
-
|
|
48
|
-
const isZipped = /\.gz$/i;
|
|
49
|
-
function prepareFileStream(filename, encoding, fnError) {
|
|
71
|
+
function prepareFileStream(getStream, isZipped, encoding, fnError) {
|
|
50
72
|
const pipes = [];
|
|
51
|
-
if (isZipped
|
|
73
|
+
if (isZipped) {
|
|
52
74
|
pipes.push(zlib.createGunzip());
|
|
53
75
|
}
|
|
54
|
-
const fileStream =
|
|
76
|
+
const fileStream = getStream();
|
|
55
77
|
fileStream.on('error', fnError);
|
|
56
78
|
const stream = pipes.reduce((s, p) => s.pipe(p).on('error', fnError), fileStream);
|
|
57
79
|
stream.setEncoding(encoding);
|
|
@@ -59,7 +81,7 @@ function prepareFileStream(filename, encoding, fnError) {
|
|
|
59
81
|
}
|
|
60
82
|
function readFileSync(filename, encoding = defaultEncoding) {
|
|
61
83
|
const rawData = fs.readFileSync(filename);
|
|
62
|
-
const data = isZipped
|
|
84
|
+
const data = (0, util_1.isZipped)(filename) ? zlib.gunzipSync(rawData) : rawData;
|
|
63
85
|
return data.toString(encoding);
|
|
64
86
|
}
|
|
65
87
|
exports.readFileSync = readFileSync;
|
package/dist/file/index.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
-
export
|
|
2
|
-
export
|
|
1
|
+
export { readFile, readFileSync } from './fileReader';
|
|
2
|
+
export { writeToFile, writeToFileIterable, writeToFileIterableP } from './fileWriter';
|
|
3
|
+
export { getStat, getStatSync } from './stat';
|
|
4
|
+
export type { Stats } from './stat';
|
|
3
5
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/file/index.js
CHANGED
|
@@ -1,19 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
|
|
18
|
-
|
|
3
|
+
exports.getStatSync = exports.getStat = exports.writeToFileIterableP = exports.writeToFileIterable = exports.writeToFile = exports.readFileSync = exports.readFile = void 0;
|
|
4
|
+
var fileReader_1 = require("./fileReader");
|
|
5
|
+
Object.defineProperty(exports, "readFile", { enumerable: true, get: function () { return fileReader_1.readFile; } });
|
|
6
|
+
Object.defineProperty(exports, "readFileSync", { enumerable: true, get: function () { return fileReader_1.readFileSync; } });
|
|
7
|
+
var fileWriter_1 = require("./fileWriter");
|
|
8
|
+
Object.defineProperty(exports, "writeToFile", { enumerable: true, get: function () { return fileWriter_1.writeToFile; } });
|
|
9
|
+
Object.defineProperty(exports, "writeToFileIterable", { enumerable: true, get: function () { return fileWriter_1.writeToFileIterable; } });
|
|
10
|
+
Object.defineProperty(exports, "writeToFileIterableP", { enumerable: true, get: function () { return fileWriter_1.writeToFileIterableP; } });
|
|
11
|
+
var stat_1 = require("./stat");
|
|
12
|
+
Object.defineProperty(exports, "getStat", { enumerable: true, get: function () { return stat_1.getStat; } });
|
|
13
|
+
Object.defineProperty(exports, "getStatSync", { enumerable: true, get: function () { return stat_1.getStatSync; } });
|
|
19
14
|
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copied from the Node definition to avoid a dependency upon a specific version of Node
|
|
3
|
+
*/
|
|
4
|
+
interface StatsBase<T> {
|
|
5
|
+
size: T;
|
|
6
|
+
mtimeMs: T;
|
|
7
|
+
eTag?: string | undefined;
|
|
8
|
+
}
|
|
9
|
+
export declare function getStat(filenameOrUri: string): Promise<Stats | Error>;
|
|
10
|
+
export declare function getStatSync(uri: string): Stats | Error;
|
|
11
|
+
export declare type Stats = StatsBase<number>;
|
|
12
|
+
export {};
|
|
13
|
+
//# sourceMappingURL=stat.d.ts.map
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getStatSync = exports.getStat = void 0;
|
|
4
|
+
const fs_1 = require("fs");
|
|
5
|
+
const util_1 = require("util");
|
|
6
|
+
const fetch_1 = require("./fetch");
|
|
7
|
+
const util_2 = require("./util");
|
|
8
|
+
async function getStat(filenameOrUri) {
|
|
9
|
+
if ((0, util_2.isUrlLike)(filenameOrUri)) {
|
|
10
|
+
const url = (0, util_2.toURL)(filenameOrUri);
|
|
11
|
+
if (!(0, util_2.isFileURL)(url)) {
|
|
12
|
+
try {
|
|
13
|
+
const headers = await (0, fetch_1.fetchHead)(url);
|
|
14
|
+
return {
|
|
15
|
+
size: Number.parseInt(headers.get('content-length') || '0', 10),
|
|
16
|
+
mtimeMs: 0,
|
|
17
|
+
eTag: headers.get('etag') || undefined,
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
catch (e) {
|
|
21
|
+
return toError(e);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
return fs_1.promises.stat(filenameOrUri).catch((e) => toError(e));
|
|
26
|
+
}
|
|
27
|
+
exports.getStat = getStat;
|
|
28
|
+
function getStatSync(uri) {
|
|
29
|
+
try {
|
|
30
|
+
return (0, fs_1.statSync)(uri);
|
|
31
|
+
}
|
|
32
|
+
catch (e) {
|
|
33
|
+
return toError(e);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
exports.getStatSync = getStatSync;
|
|
37
|
+
function toError(e) {
|
|
38
|
+
if (isErrnoException(e) || e instanceof Error)
|
|
39
|
+
return e;
|
|
40
|
+
return new Error((0, util_1.format)(e));
|
|
41
|
+
}
|
|
42
|
+
function isErrnoException(e) {
|
|
43
|
+
if (!e || typeof e !== 'object')
|
|
44
|
+
return false;
|
|
45
|
+
const err = e;
|
|
46
|
+
return err.message !== undefined && err.name !== undefined;
|
|
47
|
+
}
|
|
48
|
+
//# sourceMappingURL=stat.js.map
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { URL } from 'url';
|
|
3
|
+
export declare function isZipped(filename: string | URL): boolean;
|
|
4
|
+
export declare function isUrlLike(filename: string | URL): boolean;
|
|
5
|
+
export declare function isSupportedURL(url: URL): boolean;
|
|
6
|
+
export declare function isFileURL(url: URL): boolean;
|
|
7
|
+
export declare function toURL(filename: string | URL): URL;
|
|
8
|
+
//# sourceMappingURL=util.d.ts.map
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.toURL = exports.isFileURL = exports.isSupportedURL = exports.isUrlLike = exports.isZipped = void 0;
|
|
4
|
+
const url_1 = require("url");
|
|
5
|
+
const isZippedRegExp = /\.gz($|[?#])/i;
|
|
6
|
+
const isURLRegExp = /^\w{2,64}:\/\//i;
|
|
7
|
+
const supportedProtocols = { 'file:': true, 'http:': true, 'https:': true };
|
|
8
|
+
function isZipped(filename) {
|
|
9
|
+
const path = typeof filename === 'string' ? filename : filename.pathname;
|
|
10
|
+
return isZippedRegExp.test(path);
|
|
11
|
+
}
|
|
12
|
+
exports.isZipped = isZipped;
|
|
13
|
+
function isUrlLike(filename) {
|
|
14
|
+
return filename instanceof url_1.URL || isURLRegExp.test(filename);
|
|
15
|
+
}
|
|
16
|
+
exports.isUrlLike = isUrlLike;
|
|
17
|
+
function isSupportedURL(url) {
|
|
18
|
+
return !!supportedProtocols[url.protocol];
|
|
19
|
+
}
|
|
20
|
+
exports.isSupportedURL = isSupportedURL;
|
|
21
|
+
function isFileURL(url) {
|
|
22
|
+
return url.protocol === 'file:';
|
|
23
|
+
}
|
|
24
|
+
exports.isFileURL = isFileURL;
|
|
25
|
+
function toURL(filename) {
|
|
26
|
+
return filename instanceof url_1.URL ? filename : isUrlLike(filename) ? new url_1.URL(filename) : (0, url_1.pathToFileURL)(filename);
|
|
27
|
+
}
|
|
28
|
+
exports.toURL = toURL;
|
|
29
|
+
//# sourceMappingURL=util.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cspell-io",
|
|
3
|
-
"version": "6.
|
|
3
|
+
"version": "6.2.0",
|
|
4
4
|
"description": "A library of useful I/O functions used across various cspell tools.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"typings": "dist/index.d.ts",
|
|
@@ -41,9 +41,13 @@
|
|
|
41
41
|
"@types/fs-extra": "^9.0.13",
|
|
42
42
|
"@types/node": "^18.0.0",
|
|
43
43
|
"fs-extra": "^10.1.0",
|
|
44
|
-
"jest": "^28.1.
|
|
44
|
+
"jest": "^28.1.2",
|
|
45
45
|
"lorem-ipsum": "^2.0.8",
|
|
46
46
|
"rimraf": "^3.0.2"
|
|
47
47
|
},
|
|
48
|
-
"
|
|
48
|
+
"dependencies": {
|
|
49
|
+
"@types/node-fetch": "^2.6.2",
|
|
50
|
+
"node-fetch": "^2.6.7"
|
|
51
|
+
},
|
|
52
|
+
"gitHead": "ef9bf8e390692145b067843dbcc44a6dc2d9aa31"
|
|
49
53
|
}
|