cspell-io 6.6.1 → 6.7.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.
@@ -2,12 +2,69 @@
2
2
  import type { Stats } from './models';
3
3
  import { BufferEncoding } from './models/BufferEncoding';
4
4
  import type { TextFileResource } from './models/FileResource';
5
+ export declare type UrlOrFilename = string | URL;
5
6
  export interface CSpellIO {
6
- readFile(uriOrFilename: string | URL, encoding?: BufferEncoding): Promise<TextFileResource>;
7
- readFileSync(uriOrFilename: string | URL, encoding?: BufferEncoding): TextFileResource;
8
- writeFile(uriOrFilename: string | URL, content: string): Promise<void>;
9
- getStat(uriOrFilename: string | URL): Promise<Stats>;
10
- getStatSync(uriOrFilename: string | URL): Stats;
7
+ /**
8
+ * Read a file
9
+ * @param uriOrFilename - uri of the file to read
10
+ * @param encoding - optional encoding.
11
+ * @returns A TextFileResource.
12
+ */
13
+ readFile(uriOrFilename: UrlOrFilename, encoding?: BufferEncoding): Promise<TextFileResource>;
14
+ /**
15
+ * Read a file in Sync mode.
16
+ * Note: `http` requests will fail.
17
+ * @param uriOrFilename - uri of the file to read
18
+ * @param encoding - optional encoding.
19
+ * @returns A TextFileResource.
20
+ */
21
+ readFileSync(uriOrFilename: UrlOrFilename, encoding?: BufferEncoding): TextFileResource;
22
+ /**
23
+ * Write content to a file using utf-8 encoding.
24
+ * It will fail to write to non-file uris.
25
+ * @param uriOrFilename - uri
26
+ * @param content - string to write.
27
+ */
28
+ writeFile(uriOrFilename: UrlOrFilename, content: string): Promise<void>;
29
+ /**
30
+ * Get Stats on a uri.
31
+ * @param uriOrFilename - uri to fetch stats on
32
+ * @returns Stats if successful.
33
+ */
34
+ getStat(uriOrFilename: UrlOrFilename): Promise<Stats>;
35
+ /**
36
+ * Get Stats on a uri.
37
+ * @param uriOrFilename - uri to fetch stats on
38
+ * @returns Stats if successful, otherwise it throws an error.
39
+ */
40
+ getStatSync(uriOrFilename: UrlOrFilename): Stats;
41
+ /**
42
+ * Compare two Stats.
43
+ * @param left - left stat
44
+ * @param right - right stat
45
+ * @returns 0 if they are equal and non-zero if they are not.
46
+ */
11
47
  compareStats(left: Stats, right: Stats): number;
48
+ /**
49
+ * Convert a string to a URL
50
+ * @param uriOrFilename - string or URL to convert.
51
+ * If it is a URL, then it is just returned as is.
52
+ */
53
+ toURL(uriOrFilename: UrlOrFilename): URL;
54
+ /**
55
+ * Try to determine the base name of a URL.
56
+ *
57
+ * Note: this handles `data:` URLs with `filename` attribute:
58
+ *
59
+ * Example: `data:text/plain;charset=utf8;filename=hello.txt,Hello` would have a filename of `hello.txt`
60
+ * @param uriOrFilename - string or URL to extract the basename from.
61
+ */
62
+ uriBasename(uriOrFilename: UrlOrFilename): string;
63
+ /**
64
+ * Try to determine the directory URL of the uri.
65
+ *
66
+ * @param uriOrFilename - string or URL to extract the basename from.
67
+ */
68
+ uriDirname(uriOrFilename: UrlOrFilename): URL;
12
69
  }
13
70
  //# sourceMappingURL=CSpellIO.d.ts.map
@@ -13,6 +13,9 @@ export declare class CSpellIONode implements CSpellIO {
13
13
  getStat(uriOrFilename: string | URL): Promise<Stats>;
14
14
  getStatSync(uriOrFilename: string | URL): Stats;
15
15
  compareStats(left: Stats, right: Stats): number;
16
+ toURL(uriOrFilename: string | URL): URL;
17
+ uriBasename(uriOrFilename: string | URL): string;
18
+ uriDirname(uriOrFilename: string | URL): URL;
16
19
  }
17
20
  export declare function getDefaultCSpellIO(): CSpellIO;
18
21
  //# sourceMappingURL=CSpellIONode.d.ts.map
@@ -56,6 +56,15 @@ class CSpellIONode {
56
56
  compareStats(left, right) {
57
57
  return (0, stat_1.compareStats)(left, right);
58
58
  }
59
+ toURL(uriOrFilename) {
60
+ return (0, util_1.toURL)(uriOrFilename);
61
+ }
62
+ uriBasename(uriOrFilename) {
63
+ return (0, util_1.urlBasename)(uriOrFilename);
64
+ }
65
+ uriDirname(uriOrFilename) {
66
+ return (0, util_1.urlDirname)(uriOrFilename);
67
+ }
59
68
  }
60
69
  exports.CSpellIONode = CSpellIONode;
61
70
  function genError(err, alt) {
@@ -9,5 +9,8 @@ export declare class CSpellIOWeb implements CSpellIO {
9
9
  getStat(_uriOrFilename: string | URL): Promise<Stats>;
10
10
  getStatSync(_uriOrFilename: string | URL): Stats;
11
11
  compareStats(left: Stats, right: Stats): number;
12
+ toURL(_uriOrFilename: string | URL): URL;
13
+ uriBasename(_uriOrFilename: string | URL): string;
14
+ uriDirname(_uriOrFilename: string | URL): URL;
12
15
  }
13
16
  //# sourceMappingURL=CSpellIOWeb.d.ts.map
@@ -22,6 +22,15 @@ class CSpellIOWeb {
22
22
  compareStats(left, right) {
23
23
  return (0, stat_1.compareStats)(left, right);
24
24
  }
25
+ toURL(_uriOrFilename) {
26
+ throw new ErrorNotImplemented_1.ErrorNotImplemented('toURL');
27
+ }
28
+ uriBasename(_uriOrFilename) {
29
+ throw new ErrorNotImplemented_1.ErrorNotImplemented('uriBasename');
30
+ }
31
+ uriDirname(_uriOrFilename) {
32
+ throw new ErrorNotImplemented_1.ErrorNotImplemented('uriDirname');
33
+ }
25
34
  }
26
35
  exports.CSpellIOWeb = CSpellIOWeb;
27
36
  //# sourceMappingURL=CSpellIOWeb.js.map
package/dist/file/file.js CHANGED
@@ -14,9 +14,7 @@ const readFileSync = function (filename, encoding) {
14
14
  };
15
15
  exports.readFileSync = readFileSync;
16
16
  const getStat = function (filenameOrUri) {
17
- return (0, CSpellIONode_1.getDefaultCSpellIO)()
18
- .getStat(filenameOrUri)
19
- .catch((e) => (0, errors_1.toError)(e));
17
+ return (0, CSpellIONode_1.getDefaultCSpellIO)().getStat(filenameOrUri).catch(errors_1.toError);
20
18
  };
21
19
  exports.getStat = getStat;
22
20
  const getStatSync = function (filenameOrUri) {
@@ -83,7 +83,7 @@ const handleRequestFsReadBinaryFileSyncData = requests_1.RequestFsReadBinaryFile
83
83
  return next(req);
84
84
  const data = (0, dataUrl_1.decodeDataUrl)(url);
85
85
  return (0, cspell_service_bus_1.createResponse)({ url, content: data.data, baseFilename: data.attributes.get('filename') });
86
- }, undefined, 'Node: Read Http(s) file.');
86
+ }, undefined, 'Node: Read data: urls.');
87
87
  /**
88
88
  * Handle decoding a data url
89
89
  */
@@ -95,7 +95,7 @@ const handleRequestFsReadBinaryFileData = requests_1.RequestFsReadBinaryFile.cre
95
95
  if (!(0, cspell_service_bus_1.isServiceResponseSuccess)(res))
96
96
  return res;
97
97
  return (0, cspell_service_bus_1.createResponse)(Promise.resolve(res.value));
98
- }, undefined, 'Node: Read Http(s) file.');
98
+ }, undefined, 'Node: Read data: urls.');
99
99
  function bufferToText(buf, encoding) {
100
100
  return buf[0] === 0x1f && buf[1] === 0x8b ? bufferToText((0, zlib_1.gunzipSync)(buf), encoding) : buf.toString(encoding);
101
101
  }
package/dist/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  export { toArray as asyncIterableToArray } from './async/asyncIterable';
2
- export { getStat, getStatSync, readFile, readFileSync, writeToFile, writeToFileIterable, writeToFileIterableP, } from './file';
3
- export type { Stats } from './models/Stats';
4
2
  export type { CSpellIO } from './CSpellIO';
5
3
  export { CSpellIONode } from './CSpellIONode';
4
+ export { getStat, getStatSync, readFile, readFileSync, writeToFile, writeToFileIterable, writeToFileIterableP, } from './file';
5
+ export type { Stats } from './models/Stats';
6
6
  export { encodeDataUrl, toDataUrl } from './node/dataUrl';
7
7
  //# sourceMappingURL=index.d.ts.map
package/dist/index.js CHANGED
@@ -1,8 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.toDataUrl = exports.encodeDataUrl = exports.CSpellIONode = exports.writeToFileIterableP = exports.writeToFileIterable = exports.writeToFile = exports.readFileSync = exports.readFile = exports.getStatSync = exports.getStat = exports.asyncIterableToArray = void 0;
3
+ exports.toDataUrl = exports.encodeDataUrl = exports.writeToFileIterableP = exports.writeToFileIterable = exports.writeToFile = exports.readFileSync = exports.readFile = exports.getStatSync = exports.getStat = exports.CSpellIONode = exports.asyncIterableToArray = void 0;
4
4
  var asyncIterable_1 = require("./async/asyncIterable");
5
5
  Object.defineProperty(exports, "asyncIterableToArray", { enumerable: true, get: function () { return asyncIterable_1.toArray; } });
6
+ var CSpellIONode_1 = require("./CSpellIONode");
7
+ Object.defineProperty(exports, "CSpellIONode", { enumerable: true, get: function () { return CSpellIONode_1.CSpellIONode; } });
6
8
  var file_1 = require("./file");
7
9
  Object.defineProperty(exports, "getStat", { enumerable: true, get: function () { return file_1.getStat; } });
8
10
  Object.defineProperty(exports, "getStatSync", { enumerable: true, get: function () { return file_1.getStatSync; } });
@@ -11,8 +13,6 @@ Object.defineProperty(exports, "readFileSync", { enumerable: true, get: function
11
13
  Object.defineProperty(exports, "writeToFile", { enumerable: true, get: function () { return file_1.writeToFile; } });
12
14
  Object.defineProperty(exports, "writeToFileIterable", { enumerable: true, get: function () { return file_1.writeToFileIterable; } });
13
15
  Object.defineProperty(exports, "writeToFileIterableP", { enumerable: true, get: function () { return file_1.writeToFileIterableP; } });
14
- var CSpellIONode_1 = require("./CSpellIONode");
15
- Object.defineProperty(exports, "CSpellIONode", { enumerable: true, get: function () { return CSpellIONode_1.CSpellIONode; } });
16
16
  var dataUrl_1 = require("./node/dataUrl");
17
17
  Object.defineProperty(exports, "encodeDataUrl", { enumerable: true, get: function () { return dataUrl_1.encodeDataUrl; } });
18
18
  Object.defineProperty(exports, "toDataUrl", { enumerable: true, get: function () { return dataUrl_1.toDataUrl; } });
@@ -4,4 +4,7 @@ export declare function isUrlLike(filename: string | URL): boolean;
4
4
  export declare function isSupportedURL(url: URL): boolean;
5
5
  export declare function isFileURL(url: URL): boolean;
6
6
  export declare function toURL(filename: string | URL): URL;
7
+ export declare function urlBasename(url: string | URL): string;
8
+ export declare function urlDirname(url: string | URL): URL;
9
+ export declare function basename(path: string): string;
7
10
  //# sourceMappingURL=util.d.ts.map
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.toURL = exports.isFileURL = exports.isSupportedURL = exports.isUrlLike = exports.isZipped = void 0;
3
+ exports.basename = exports.urlDirname = exports.urlBasename = exports.toURL = exports.isFileURL = exports.isSupportedURL = exports.isUrlLike = exports.isZipped = void 0;
4
4
  const url_1 = require("url");
5
5
  const isZippedRegExp = /\.gz($|[?#])/i;
6
6
  const isURLRegExp = /^(\w{2,64}:\/\/|data:)/i;
@@ -30,4 +30,40 @@ function toURL(filename) {
30
30
  : (0, url_1.pathToFileURL)(filename);
31
31
  }
32
32
  exports.toURL = toURL;
33
+ const regMatchFilename = /filename=([^;,]*)/;
34
+ function urlBasename(url) {
35
+ function guessDataUrlName(header) {
36
+ const filenameMatch = header.match(regMatchFilename);
37
+ if (filenameMatch)
38
+ return filenameMatch[1];
39
+ const mime = header.split(';', 1)[0];
40
+ return mime.replace(/\W/g, '.');
41
+ }
42
+ if (typeof url === 'string' && url.startsWith('data:')) {
43
+ return guessDataUrlName(url.split(',', 1)[0].split(':', 2)[1]);
44
+ }
45
+ url = toURL(url);
46
+ if (url.protocol === 'data:') {
47
+ return guessDataUrlName(url.pathname.split(',', 1)[0]);
48
+ }
49
+ return basename(url.pathname);
50
+ }
51
+ exports.urlBasename = urlBasename;
52
+ function urlDirname(url) {
53
+ if (typeof url === 'string' && url.startsWith('data:')) {
54
+ return toURL('data:');
55
+ }
56
+ url = toURL(url);
57
+ if (url.protocol === 'data:') {
58
+ return toURL('data:');
59
+ }
60
+ return new URL(url.pathname.endsWith('/') ? '..' : '.', url);
61
+ }
62
+ exports.urlDirname = urlDirname;
63
+ function basename(path) {
64
+ path = path.endsWith('/') ? path.slice(0, path.length - 1) : path;
65
+ const idx = path.lastIndexOf('/');
66
+ return idx >= 0 ? path.slice(idx + 1) || '' : path;
67
+ }
68
+ exports.basename = basename;
33
69
  //# sourceMappingURL=util.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cspell-io",
3
- "version": "6.6.1",
3
+ "version": "6.7.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",
@@ -39,16 +39,16 @@
39
39
  },
40
40
  "devDependencies": {
41
41
  "@types/fs-extra": "^9.0.13",
42
- "@types/node": "^18.6.5",
42
+ "@types/node": "^18.7.6",
43
43
  "fs-extra": "^10.1.0",
44
44
  "jest": "^28.1.3",
45
45
  "lorem-ipsum": "^2.0.8",
46
46
  "rimraf": "^3.0.2"
47
47
  },
48
48
  "dependencies": {
49
- "@cspell/cspell-service-bus": "^6.6.1",
49
+ "@cspell/cspell-service-bus": "^6.7.0",
50
50
  "@types/node-fetch": "^2.6.2",
51
51
  "node-fetch": "^2.6.7"
52
52
  },
53
- "gitHead": "3c9c24d1cebd558ac3729d3fbf441e6ed751d8cf"
53
+ "gitHead": "3a7312a15d2df1507d9e01863ec5842f5a99e0cc"
54
54
  }