cspell-io 6.5.0 → 6.6.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.
@@ -1,11 +1,13 @@
1
- import { BufferEncoding } from './models/BufferEncoding';
1
+ /// <reference types="node" />
2
2
  import type { Stats } from './models';
3
+ import { BufferEncoding } from './models/BufferEncoding';
4
+ import type { TextFileResource } from './models/FileResource';
3
5
  export interface CSpellIO {
4
- readFile(uriOrFilename: string, encoding?: BufferEncoding): Promise<string>;
5
- readFileSync(uriOrFilename: string, encoding?: BufferEncoding): string;
6
- writeFile(uriOrFilename: string, content: string): Promise<void>;
7
- getStat(uriOrFilename: string): Promise<Stats>;
8
- getStatSync(uriOrFilename: string): Stats;
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;
9
11
  compareStats(left: Stats, right: Stats): number;
10
12
  }
11
13
  //# sourceMappingURL=CSpellIO.d.ts.map
@@ -1,15 +1,18 @@
1
1
  /// <reference types="node" />
2
+ /// <reference types="node" />
2
3
  import { ServiceBus } from '@cspell/cspell-service-bus';
3
4
  import { CSpellIO } from './CSpellIO';
4
- import { Stats } from './models/Stats';
5
+ import type { TextFileResource } from './models/FileResource';
6
+ import type { Stats } from './models/Stats';
5
7
  export declare class CSpellIONode implements CSpellIO {
6
8
  readonly serviceBus: ServiceBus;
7
9
  constructor(serviceBus?: ServiceBus);
8
- readFile(uriOrFilename: string, encoding?: BufferEncoding): Promise<string>;
9
- readFileSync(uriOrFilename: string, encoding?: BufferEncoding): string;
10
- writeFile(uriOrFilename: string, content: string): Promise<void>;
11
- getStat(uriOrFilename: string): Promise<Stats>;
12
- getStatSync(uriOrFilename: string): Stats;
10
+ readFile(uriOrFilename: string | URL, encoding?: BufferEncoding): Promise<TextFileResource>;
11
+ readFileSync(uriOrFilename: string | URL, encoding?: BufferEncoding): TextFileResource;
12
+ writeFile(uriOrFilename: string | URL, content: string): Promise<void>;
13
+ getStat(uriOrFilename: string | URL): Promise<Stats>;
14
+ getStatSync(uriOrFilename: string | URL): Stats;
13
15
  compareStats(left: Stats, right: Stats): number;
14
16
  }
17
+ export declare function getDefaultCSpellIO(): CSpellIO;
15
18
  //# sourceMappingURL=CSpellIONode.d.ts.map
@@ -1,12 +1,13 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.CSpellIONode = void 0;
3
+ exports.getDefaultCSpellIO = exports.CSpellIONode = void 0;
4
4
  const cspell_service_bus_1 = require("@cspell/cspell-service-bus");
5
5
  const stat_1 = require("./common/stat");
6
6
  const ErrorNotImplemented_1 = require("./errors/ErrorNotImplemented");
7
7
  const file_1 = require("./handlers/node/file");
8
8
  const util_1 = require("./node/file/util");
9
9
  const requests_1 = require("./requests");
10
+ let defaultCSpellIONode = undefined;
10
11
  class CSpellIONode {
11
12
  constructor(serviceBus = new cspell_service_bus_1.ServiceBus()) {
12
13
  this.serviceBus = serviceBus;
@@ -60,4 +61,12 @@ exports.CSpellIONode = CSpellIONode;
60
61
  function genError(err, alt) {
61
62
  return err || new ErrorNotImplemented_1.ErrorNotImplemented(alt);
62
63
  }
64
+ function getDefaultCSpellIO() {
65
+ if (defaultCSpellIONode)
66
+ return defaultCSpellIONode;
67
+ const cspellIO = new CSpellIONode();
68
+ defaultCSpellIONode = cspellIO;
69
+ return cspellIO;
70
+ }
71
+ exports.getDefaultCSpellIO = getDefaultCSpellIO;
63
72
  //# sourceMappingURL=CSpellIONode.js.map
@@ -1,11 +1,13 @@
1
+ /// <reference types="node" />
1
2
  import { CSpellIO } from './CSpellIO';
2
- import { Stats } from './models/Stats';
3
+ import type { Stats } from './models/Stats';
4
+ import type { TextFileResource } from './models/FileResource';
3
5
  export declare class CSpellIOWeb implements CSpellIO {
4
- readFile(_uriOrFilename: string): Promise<string>;
5
- readFileSync(_uriOrFilename: string): string;
6
- writeFile(_uriOrFilename: string, _content: string): Promise<void>;
7
- getStat(_uriOrFilename: string): Promise<Stats>;
8
- getStatSync(_uriOrFilename: string): Stats;
6
+ readFile(_uriOrFilename: string | URL): Promise<TextFileResource>;
7
+ readFileSync(_uriOrFilename: string | URL): TextFileResource;
8
+ writeFile(_uriOrFilename: string | URL, _content: string): Promise<void>;
9
+ getStat(_uriOrFilename: string | URL): Promise<Stats>;
10
+ getStatSync(_uriOrFilename: string | URL): Stats;
9
11
  compareStats(left: Stats, right: Stats): number;
10
12
  }
11
13
  //# sourceMappingURL=CSpellIOWeb.d.ts.map
@@ -0,0 +1,6 @@
1
+ import type { getStat as GetStatFn, getStatSync as GetStatSyncFn, readFile as ReadFileFn, readFileSync as ReadFileSyncFn } from '../node/file';
2
+ export declare const readFile: typeof ReadFileFn;
3
+ export declare const readFileSync: typeof ReadFileSyncFn;
4
+ export declare const getStat: typeof GetStatFn;
5
+ export declare const getStatSync: typeof GetStatSyncFn;
6
+ //# sourceMappingURL=file.d.ts.map
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getStatSync = exports.getStat = exports.readFileSync = exports.readFile = void 0;
4
+ const CSpellIONode_1 = require("../CSpellIONode");
5
+ const errors_1 = require("../errors");
6
+ const readFile = function (filename, encoding) {
7
+ return (0, CSpellIONode_1.getDefaultCSpellIO)()
8
+ .readFile(filename, encoding)
9
+ .then((fr) => fr.content);
10
+ };
11
+ exports.readFile = readFile;
12
+ const readFileSync = function (filename, encoding) {
13
+ return (0, CSpellIONode_1.getDefaultCSpellIO)().readFileSync(filename, encoding).content;
14
+ };
15
+ exports.readFileSync = readFileSync;
16
+ const getStat = function (filenameOrUri) {
17
+ return (0, CSpellIONode_1.getDefaultCSpellIO)()
18
+ .getStat(filenameOrUri)
19
+ .catch((e) => (0, errors_1.toError)(e));
20
+ };
21
+ exports.getStat = getStat;
22
+ const getStatSync = function (filenameOrUri) {
23
+ try {
24
+ return (0, CSpellIONode_1.getDefaultCSpellIO)().getStatSync(filenameOrUri);
25
+ }
26
+ catch (e) {
27
+ return (0, errors_1.toError)(e);
28
+ }
29
+ };
30
+ exports.getStatSync = getStatSync;
31
+ //# sourceMappingURL=file.js.map
@@ -1,4 +1,3 @@
1
- export { readFile, readFileSync } from './../node/file';
2
1
  export { writeToFile, writeToFileIterable, writeToFileIterableP } from './../node/file';
3
- export { getStat, getStatSync } from './../node/file';
2
+ export { getStat, getStatSync, readFile, readFileSync } from './file';
4
3
  //# sourceMappingURL=index.d.ts.map
@@ -1,14 +1,13 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getStatSync = exports.getStat = exports.writeToFileIterableP = exports.writeToFileIterable = exports.writeToFile = exports.readFileSync = exports.readFile = void 0;
3
+ exports.readFileSync = exports.readFile = exports.getStatSync = exports.getStat = exports.writeToFileIterableP = exports.writeToFileIterable = exports.writeToFile = void 0;
4
4
  var file_1 = require("./../node/file");
5
- Object.defineProperty(exports, "readFile", { enumerable: true, get: function () { return file_1.readFile; } });
6
- Object.defineProperty(exports, "readFileSync", { enumerable: true, get: function () { return file_1.readFileSync; } });
7
- var file_2 = require("./../node/file");
8
- Object.defineProperty(exports, "writeToFile", { enumerable: true, get: function () { return file_2.writeToFile; } });
9
- Object.defineProperty(exports, "writeToFileIterable", { enumerable: true, get: function () { return file_2.writeToFileIterable; } });
10
- Object.defineProperty(exports, "writeToFileIterableP", { enumerable: true, get: function () { return file_2.writeToFileIterableP; } });
11
- var file_3 = require("./../node/file");
12
- Object.defineProperty(exports, "getStat", { enumerable: true, get: function () { return file_3.getStat; } });
13
- Object.defineProperty(exports, "getStatSync", { enumerable: true, get: function () { return file_3.getStatSync; } });
5
+ Object.defineProperty(exports, "writeToFile", { enumerable: true, get: function () { return file_1.writeToFile; } });
6
+ Object.defineProperty(exports, "writeToFileIterable", { enumerable: true, get: function () { return file_1.writeToFileIterable; } });
7
+ Object.defineProperty(exports, "writeToFileIterableP", { enumerable: true, get: function () { return file_1.writeToFileIterableP; } });
8
+ var file_2 = require("./file");
9
+ Object.defineProperty(exports, "getStat", { enumerable: true, get: function () { return file_2.getStat; } });
10
+ Object.defineProperty(exports, "getStatSync", { enumerable: true, get: function () { return file_2.getStatSync; } });
11
+ Object.defineProperty(exports, "readFile", { enumerable: true, get: function () { return file_2.readFile; } });
12
+ Object.defineProperty(exports, "readFileSync", { enumerable: true, get: function () { return file_2.readFileSync; } });
14
13
  //# sourceMappingURL=index.js.map
@@ -9,58 +9,92 @@ const assert_1 = __importDefault(require("assert"));
9
9
  const fs_1 = require("fs");
10
10
  const zlib_1 = require("zlib");
11
11
  const errors_1 = require("../../errors");
12
+ const dataUrl_1 = require("../../node/dataUrl");
12
13
  const fetch_1 = require("../../node/file/fetch");
13
14
  const stat_1 = require("../../node/file/stat");
14
15
  const requests_1 = require("../../requests");
16
+ const url_1 = require("url");
15
17
  const isGzFileRegExp = /\.gz($|[?#])/;
16
18
  function isGzFile(url) {
17
19
  return isGzFileRegExp.test(url.pathname);
18
20
  }
21
+ /*
22
+ * NOTE: fileURLToPath is used because of yarn bug https://github.com/yarnpkg/berry/issues/899
23
+ */
19
24
  /**
20
25
  * Handle Binary File Reads
21
26
  */
22
- const handleRequestFsReadBinaryFile = (0, cspell_service_bus_1.createRequestHandler)(requests_1.RequestFsReadBinaryFile, ({ params }) => (0, cspell_service_bus_1.createResponse)(fs_1.promises.readFile(params.url)), undefined, 'Node: Read Binary File.');
27
+ const handleRequestFsReadBinaryFile = requests_1.RequestFsReadBinaryFile.createRequestHandler(({ params }) => (0, cspell_service_bus_1.createResponse)(fs_1.promises.readFile((0, url_1.fileURLToPath)(params.url)).then((content) => ({ url: params.url, content }))), undefined, 'Node: Read Binary File.');
23
28
  /**
24
29
  * Handle Binary File Sync Reads
25
30
  */
26
- const handleRequestFsReadBinaryFileSync = (0, cspell_service_bus_1.createRequestHandler)(requests_1.RequestFsReadBinaryFileSync, ({ params }) => (0, cspell_service_bus_1.createResponse)((0, fs_1.readFileSync)(params.url)), undefined, 'Node: Sync Read Binary File.');
31
+ const handleRequestFsReadBinaryFileSync = requests_1.RequestFsReadBinaryFileSync.createRequestHandler(({ params }) => (0, cspell_service_bus_1.createResponse)({ url: params.url, content: (0, fs_1.readFileSync)((0, url_1.fileURLToPath)(params.url)) }), undefined, 'Node: Sync Read Binary File.');
27
32
  /**
28
33
  * Handle UTF-8 Text File Reads
29
34
  */
30
- const handleRequestFsReadFile = (0, cspell_service_bus_1.createRequestHandler)(requests_1.RequestFsReadFile, (req, _, dispatcher) => {
35
+ const handleRequestFsReadFile = requests_1.RequestFsReadFile.createRequestHandler((req, _, dispatcher) => {
31
36
  const { url, encoding } = req.params;
32
37
  const res = dispatcher.dispatch(requests_1.RequestFsReadBinaryFile.create({ url }));
33
38
  if (!(0, cspell_service_bus_1.isServiceResponseSuccess)(res)) {
34
39
  (0, assert_1.default)((0, cspell_service_bus_1.isServiceResponseFailure)(res));
35
40
  return (0, cspell_service_bus_1.createResponseFail)(req, res.error);
36
41
  }
37
- return (0, cspell_service_bus_1.createResponse)(res.value.then((buf) => bufferToText(buf, encoding)));
42
+ return (0, cspell_service_bus_1.createResponse)(res.value.then((res) => ({
43
+ url,
44
+ content: bufferToText(res.content, encoding),
45
+ baseFilename: res.baseFilename,
46
+ })));
38
47
  }, undefined, 'Node: Read Text File.');
39
48
  /**
40
49
  * Handle UTF-8 Text File Reads
41
50
  */
42
- const handleRequestFsReadFileSync = (0, cspell_service_bus_1.createRequestHandler)(requests_1.RequestFsReadFileSync, (req, _, dispatcher) => {
51
+ const handleRequestFsReadFileSync = requests_1.RequestFsReadFileSync.createRequestHandler((req, _, dispatcher) => {
43
52
  const { url, encoding } = req.params;
44
53
  const res = dispatcher.dispatch(requests_1.RequestFsReadBinaryFileSync.create({ url }));
45
54
  if (!(0, cspell_service_bus_1.isServiceResponseSuccess)(res)) {
46
55
  (0, assert_1.default)((0, cspell_service_bus_1.isServiceResponseFailure)(res));
47
56
  return (0, cspell_service_bus_1.createResponseFail)(req, res.error);
48
57
  }
49
- return (0, cspell_service_bus_1.createResponse)(bufferToText(res.value, encoding));
58
+ return (0, cspell_service_bus_1.createResponse)({
59
+ ...res.value,
60
+ content: bufferToText(res.value.content, encoding),
61
+ });
50
62
  }, undefined, 'Node: Sync Read Text File.');
51
63
  /**
52
64
  * Handle deflating gzip data
53
65
  */
54
- const handleRequestZlibInflate = (0, cspell_service_bus_1.createRequestHandler)(requests_1.RequestZlibInflate, ({ params }) => (0, cspell_service_bus_1.createResponse)((0, zlib_1.gunzipSync)(params.data).toString('utf-8')), undefined, 'Node: gz deflate.');
66
+ const handleRequestZlibInflate = requests_1.RequestZlibInflate.createRequestHandler(({ params }) => (0, cspell_service_bus_1.createResponse)((0, zlib_1.gunzipSync)(params.data).toString('utf-8')), undefined, 'Node: gz deflate.');
55
67
  const supportedFetchProtocols = { 'http:': true, 'https:': true };
56
68
  /**
57
- * Handle reading gzip'ed text files.
69
+ * Handle fetching a file from http
58
70
  */
59
- const handleRequestFsReadBinaryFileHttp = (0, cspell_service_bus_1.createRequestHandler)(requests_1.RequestFsReadBinaryFile, (req, next) => {
71
+ const handleRequestFsReadBinaryFileHttp = requests_1.RequestFsReadBinaryFile.createRequestHandler((req, next) => {
60
72
  const { url } = req.params;
61
73
  if (!(url.protocol in supportedFetchProtocols))
62
74
  return next(req);
63
- return (0, cspell_service_bus_1.createResponse)((0, fetch_1.fetchURL)(url));
75
+ return (0, cspell_service_bus_1.createResponse)((0, fetch_1.fetchURL)(url).then((content) => ({ url, content })));
76
+ }, undefined, 'Node: Read Http(s) file.');
77
+ /**
78
+ * Handle decoding a data url
79
+ */
80
+ const handleRequestFsReadBinaryFileSyncData = requests_1.RequestFsReadBinaryFileSync.createRequestHandler((req, next) => {
81
+ const { url } = req.params;
82
+ if (url.protocol !== 'data:')
83
+ return next(req);
84
+ const data = (0, dataUrl_1.decodeDataUrl)(url);
85
+ return (0, cspell_service_bus_1.createResponse)({ url, content: data.data, baseFilename: data.attributes.get('filename') });
86
+ }, undefined, 'Node: Read Http(s) file.');
87
+ /**
88
+ * Handle decoding a data url
89
+ */
90
+ const handleRequestFsReadBinaryFileData = requests_1.RequestFsReadBinaryFile.createRequestHandler((req, next, dispatcher) => {
91
+ const { url } = req.params;
92
+ if (url.protocol !== 'data:')
93
+ return next(req);
94
+ const res = dispatcher.dispatch(requests_1.RequestFsReadBinaryFileSync.create(req.params));
95
+ if (!(0, cspell_service_bus_1.isServiceResponseSuccess)(res))
96
+ return res;
97
+ return (0, cspell_service_bus_1.createResponse)(Promise.resolve(res.value));
64
98
  }, undefined, 'Node: Read Http(s) file.');
65
99
  function bufferToText(buf, encoding) {
66
100
  return buf[0] === 0x1f && buf[1] === 0x8b ? bufferToText((0, zlib_1.gunzipSync)(buf), encoding) : buf.toString(encoding);
@@ -68,14 +102,14 @@ function bufferToText(buf, encoding) {
68
102
  /**
69
103
  * Handle fs:stat
70
104
  */
71
- const handleRequestFsStat = (0, cspell_service_bus_1.createRequestHandler)(requests_1.RequestFsStat, ({ params }) => (0, cspell_service_bus_1.createResponse)(fs_1.promises.stat(params.url)), undefined, 'Node: fs.stat.');
105
+ const handleRequestFsStat = requests_1.RequestFsStat.createRequestHandler(({ params }) => (0, cspell_service_bus_1.createResponse)(fs_1.promises.stat((0, url_1.fileURLToPath)(params.url))), undefined, 'Node: fs.stat.');
72
106
  /**
73
107
  * Handle fs:statSync
74
108
  */
75
- const handleRequestFsStatSync = (0, cspell_service_bus_1.createRequestHandler)(requests_1.RequestFsStatSync, (req) => {
109
+ const handleRequestFsStatSync = requests_1.RequestFsStatSync.createRequestHandler((req) => {
76
110
  const { params } = req;
77
111
  try {
78
- return (0, cspell_service_bus_1.createResponse)((0, fs_1.statSync)(params.url));
112
+ return (0, cspell_service_bus_1.createResponse)((0, fs_1.statSync)((0, url_1.fileURLToPath)(params.url)));
79
113
  }
80
114
  catch (e) {
81
115
  return (0, cspell_service_bus_1.createResponseFail)(req, (0, errors_1.toError)(e));
@@ -84,7 +118,7 @@ const handleRequestFsStatSync = (0, cspell_service_bus_1.createRequestHandler)(r
84
118
  /**
85
119
  * Handle deflating gzip data
86
120
  */
87
- const handleRequestFsStatHttp = (0, cspell_service_bus_1.createRequestHandler)(requests_1.RequestFsStat, (req, next) => {
121
+ const handleRequestFsStatHttp = requests_1.RequestFsStat.createRequestHandler((req, next) => {
88
122
  const { url } = req.params;
89
123
  if (!(url.protocol in supportedFetchProtocols))
90
124
  return next(req);
@@ -93,11 +127,11 @@ const handleRequestFsStatHttp = (0, cspell_service_bus_1.createRequestHandler)(r
93
127
  /**
94
128
  * Handle fs:writeFile
95
129
  */
96
- const handleRequestFsWriteFile = (0, cspell_service_bus_1.createRequestHandler)(requests_1.RequestFsWriteFile, ({ params }) => (0, cspell_service_bus_1.createResponse)(fs_1.promises.writeFile(params.url, params.content)), undefined, 'Node: fs.writeFile');
130
+ const handleRequestFsWriteFile = requests_1.RequestFsWriteFile.createRequestHandler(({ params }) => (0, cspell_service_bus_1.createResponse)(fs_1.promises.writeFile(params.url, params.content)), undefined, 'Node: fs.writeFile');
97
131
  /**
98
132
  * Handle fs:writeFile compressed
99
133
  */
100
- const handleRequestFsWriteFileGz = (0, cspell_service_bus_1.createRequestHandler)(requests_1.RequestFsWriteFile, (req, next) => {
134
+ const handleRequestFsWriteFileGz = requests_1.RequestFsWriteFile.createRequestHandler((req, next) => {
101
135
  const { url, content } = req.params;
102
136
  if (!isGzFile(url))
103
137
  return next(req);
@@ -114,6 +148,8 @@ function registerHandlers(serviceBus) {
114
148
  handleRequestFsReadBinaryFile,
115
149
  handleRequestFsReadBinaryFileSync,
116
150
  handleRequestFsReadBinaryFileHttp,
151
+ handleRequestFsReadBinaryFileData,
152
+ handleRequestFsReadBinaryFileSyncData,
117
153
  handleRequestFsReadFile,
118
154
  handleRequestFsReadFileSync,
119
155
  handleRequestZlibInflate,
@@ -1,2 +1,2 @@
1
- export declare type BufferEncoding = 'ascii' | 'utf8' | 'utf-8' | 'utf16le' | 'ucs2' | 'ucs-2' | 'base64' | 'base64url' | 'latin1' | 'hex';
1
+ export declare type BufferEncoding = 'ascii' | 'utf8' | 'utf-8' | 'utf16le' | 'ucs2' | 'ucs-2' | 'base64' | 'base64url' | 'binary' | 'latin1' | 'hex';
2
2
  //# sourceMappingURL=BufferEncoding.d.ts.map
@@ -0,0 +1,24 @@
1
+ /// <reference types="node" />
2
+ /// <reference types="node" />
3
+ export interface FileResource {
4
+ /**
5
+ * The URL of the File
6
+ */
7
+ url: URL;
8
+ /**
9
+ * The contents of the file
10
+ */
11
+ content: string | Buffer;
12
+ /**
13
+ * The filename of the file if known.
14
+ * Useful for `data:` urls.
15
+ */
16
+ baseFilename?: string | undefined;
17
+ }
18
+ export interface TextFileResource extends FileResource {
19
+ content: string;
20
+ }
21
+ export interface BinaryFileResource extends FileResource {
22
+ content: Buffer;
23
+ }
24
+ //# sourceMappingURL=FileResource.d.ts.map
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=FileResource.js.map
@@ -18,5 +18,11 @@ export interface DecodedDataUrl {
18
18
  encoding?: string | undefined;
19
19
  attributes: Map<string, string>;
20
20
  }
21
- export declare function decodeDataUrl(url: string): DecodedDataUrl;
21
+ export declare function decodeDataUrl(url: string | URL): DecodedDataUrl;
22
+ export declare function encodeDataUrlFromFile(path: string | URL, mediaType?: string, attributes?: Iterable<readonly [string, string]> | undefined): Promise<string>;
23
+ export interface GuessMimeTypeResult {
24
+ mimeType: string;
25
+ encoding?: 'utf-8' | undefined;
26
+ }
27
+ export declare function guessMimeType(filename: string): GuessMimeTypeResult | undefined;
22
28
  //# sourceMappingURL=dataUrl.d.ts.map
@@ -1,6 +1,32 @@
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 __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
2
25
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.decodeDataUrl = exports.toDataUrl = exports.encodeDataUrl = void 0;
26
+ exports.guessMimeType = exports.encodeDataUrlFromFile = exports.decodeDataUrl = exports.toDataUrl = exports.encodeDataUrl = void 0;
27
+ const fsPath = __importStar(require("path"));
28
+ const util_1 = require("./file/util");
29
+ const fs_1 = require("fs");
4
30
  /**
5
31
  * Generates a string of the following format:
6
32
  *
@@ -40,6 +66,7 @@ function encodeAttributes(attributes) {
40
66
  }
41
67
  const dataUrlRegExHead = /^data:(?<mediaType>[^;,]*)(?<attributes>(?:;[^=]+=[^;,]*)*)(?<base64>;base64)?$/;
42
68
  function decodeDataUrl(url) {
69
+ url = url.toString();
43
70
  const [head, encodedData] = url.split(',', 2);
44
71
  if (!head || encodedData === undefined)
45
72
  throw Error('Not a data url');
@@ -59,4 +86,31 @@ function decodeDataUrl(url) {
59
86
  return { mediaType, data, encoding, attributes };
60
87
  }
61
88
  exports.decodeDataUrl = decodeDataUrl;
89
+ async function encodeDataUrlFromFile(path, mediaType, attributes) {
90
+ const url = (0, util_1.toURL)(path);
91
+ const filename = fsPath.basename(url.pathname);
92
+ const guess = guessMimeType(filename);
93
+ mediaType = mediaType || guess?.mimeType || 'text/plain';
94
+ const _attributes = new Map(attributes || []);
95
+ filename && _attributes.set('filename', filename);
96
+ const content = guess?.encoding ? await fs_1.promises.readFile(url, guess?.encoding) : await fs_1.promises.readFile(url);
97
+ return encodeDataUrl(content, mediaType, _attributes);
98
+ }
99
+ exports.encodeDataUrlFromFile = encodeDataUrlFromFile;
100
+ function guessMimeType(filename) {
101
+ if (filename.endsWith('.trie'))
102
+ return { mimeType: 'application/vnd.cspell.dictionary+trie', encoding: 'utf-8' };
103
+ if (filename.endsWith('.trie.gz'))
104
+ return { mimeType: 'application/vnd.cspell.dictionary+trie.gz' };
105
+ if (filename.endsWith('.txt'))
106
+ return { mimeType: 'text/plain', encoding: 'utf-8' };
107
+ if (filename.endsWith('.gz'))
108
+ return { mimeType: 'application/gzip' };
109
+ if (filename.endsWith('.json'))
110
+ return { mimeType: 'application/json', encoding: 'utf-8' };
111
+ if (filename.endsWith('.yaml') || filename.endsWith('.yml'))
112
+ return { mimeType: 'application/x-yaml', encoding: 'utf-8' };
113
+ return undefined;
114
+ }
115
+ exports.guessMimeType = guessMimeType;
62
116
  //# sourceMappingURL=dataUrl.js.map
@@ -1,5 +1,5 @@
1
1
  /// <reference types="node" />
2
- /// <reference types="node" />
2
+ import type { BufferEncoding } from '../../models/BufferEncoding';
3
3
  export declare function readFile(filename: string | URL, encoding?: BufferEncoding): Promise<string>;
4
4
  export declare function readFileSync(filename: string, encoding?: BufferEncoding): string;
5
5
  //# sourceMappingURL=fileReader.d.ts.map
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  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
- const isURLRegExp = /^\w{2,64}:\/\//i;
6
+ const isURLRegExp = /^(\w{2,64}:\/\/|data:)/i;
7
7
  const supportedProtocols = { 'file:': true, 'http:': true, 'https:': true };
8
8
  function isZipped(filename) {
9
9
  const path = typeof filename === 'string' ? filename : filename.pathname;
@@ -1,9 +1,12 @@
1
1
  /// <reference types="node" />
2
- /// <reference types="node" />
2
+ import { ServiceRequestFactoryRequestType } from '@cspell/cspell-service-bus';
3
+ import type { BinaryFileResource } from '../models/FileResource';
3
4
  interface RequestParams {
4
5
  readonly url: URL;
5
6
  }
6
- export declare const RequestFsReadBinaryFile: import("@cspell/cspell-service-bus/request").ServiceRequestFactory<import("@cspell/cspell-service-bus").ServiceRequest<"fs:readBinaryFile", RequestParams, Promise<Buffer>>, RequestParams, "fs:readBinaryFile">;
7
- export declare const RequestFsReadBinaryFileSync: import("@cspell/cspell-service-bus/request").ServiceRequestFactory<import("@cspell/cspell-service-bus").ServiceRequest<"fs:readBinaryFileSync", RequestParams, Buffer>, RequestParams, "fs:readBinaryFileSync">;
7
+ export declare const RequestFsReadBinaryFile: import("@cspell/cspell-service-bus/ServiceRequestFactory").ServiceRequestFactory<import("@cspell/cspell-service-bus").ServiceRequest<"fs:readBinaryFile", RequestParams, Promise<BinaryFileResource>>, RequestParams, "fs:readBinaryFile">;
8
+ export declare type RequestFsReadBinaryFile = ServiceRequestFactoryRequestType<typeof RequestFsReadBinaryFile>;
9
+ export declare const RequestFsReadBinaryFileSync: import("@cspell/cspell-service-bus/ServiceRequestFactory").ServiceRequestFactory<import("@cspell/cspell-service-bus").ServiceRequest<"fs:readBinaryFileSync", RequestParams, BinaryFileResource>, RequestParams, "fs:readBinaryFileSync">;
10
+ export declare type RequestFsReadBinaryFileSync = ServiceRequestFactoryRequestType<typeof RequestFsReadBinaryFileSync>;
8
11
  export {};
9
12
  //# sourceMappingURL=RequestFsReadBinaryFile.d.ts.map
@@ -1,9 +1,12 @@
1
1
  /// <reference types="node" />
2
2
  /// <reference types="node" />
3
+ import { ServiceRequestFactoryRequestType } from '@cspell/cspell-service-bus';
4
+ import type { TextFileResource } from '../models/FileResource';
3
5
  interface RequestParams {
4
6
  readonly url: URL;
5
7
  readonly encoding: BufferEncoding;
6
8
  }
7
- export declare const RequestFsReadFile: import("@cspell/cspell-service-bus/request").ServiceRequestFactory<import("@cspell/cspell-service-bus").ServiceRequest<"fs:readFile", RequestParams, Promise<string>>, RequestParams, "fs:readFile">;
9
+ export declare const RequestFsReadFile: import("@cspell/cspell-service-bus/ServiceRequestFactory").ServiceRequestFactory<import("@cspell/cspell-service-bus").ServiceRequest<"fs:readFile", RequestParams, Promise<TextFileResource>>, RequestParams, "fs:readFile">;
10
+ export declare type RequestFsReadFile = ServiceRequestFactoryRequestType<typeof RequestFsReadFile>;
8
11
  export {};
9
12
  //# sourceMappingURL=RequestFsReadFile.d.ts.map
@@ -1,9 +1,12 @@
1
1
  /// <reference types="node" />
2
2
  /// <reference types="node" />
3
+ import { ServiceRequestFactoryRequestType } from '@cspell/cspell-service-bus';
4
+ import type { TextFileResource } from '../models/FileResource';
3
5
  interface RequestParams {
4
6
  readonly url: URL;
5
7
  readonly encoding: BufferEncoding;
6
8
  }
7
- export declare const RequestFsReadFileSync: import("@cspell/cspell-service-bus/request").ServiceRequestFactory<import("@cspell/cspell-service-bus").ServiceRequest<"fs:readFileSync", RequestParams, string>, RequestParams, "fs:readFileSync">;
9
+ export declare const RequestFsReadFileSync: import("@cspell/cspell-service-bus/ServiceRequestFactory").ServiceRequestFactory<import("@cspell/cspell-service-bus").ServiceRequest<"fs:readFileSync", RequestParams, TextFileResource>, RequestParams, "fs:readFileSync">;
10
+ export declare type RequestFsReadFileSync = ServiceRequestFactoryRequestType<typeof RequestFsReadFileSync>;
8
11
  export {};
9
12
  //# sourceMappingURL=RequestFsReadFileSync.d.ts.map
@@ -1,9 +1,12 @@
1
1
  /// <reference types="node" />
2
+ import { ServiceRequestFactoryRequestType } from '@cspell/cspell-service-bus';
2
3
  import { Stats } from '../models';
3
4
  interface RequestStatParams {
4
5
  readonly url: URL;
5
6
  }
6
- export declare const RequestFsStat: import("@cspell/cspell-service-bus/request").ServiceRequestFactory<import("@cspell/cspell-service-bus").ServiceRequest<"fs:stat", RequestStatParams, Promise<Stats>>, RequestStatParams, "fs:stat">;
7
- export declare const RequestFsStatSync: import("@cspell/cspell-service-bus/request").ServiceRequestFactory<import("@cspell/cspell-service-bus").ServiceRequest<"fs:statSync", RequestStatParams, Stats>, RequestStatParams, "fs:statSync">;
7
+ export declare const RequestFsStat: import("@cspell/cspell-service-bus/ServiceRequestFactory").ServiceRequestFactory<import("@cspell/cspell-service-bus").ServiceRequest<"fs:stat", RequestStatParams, Promise<Stats>>, RequestStatParams, "fs:stat">;
8
+ export declare type RequestFsStat = ServiceRequestFactoryRequestType<typeof RequestFsStat>;
9
+ export declare const RequestFsStatSync: import("@cspell/cspell-service-bus/ServiceRequestFactory").ServiceRequestFactory<import("@cspell/cspell-service-bus").ServiceRequest<"fs:statSync", RequestStatParams, Stats>, RequestStatParams, "fs:statSync">;
10
+ export declare type RequestFsStatSync = ServiceRequestFactoryRequestType<typeof RequestFsStatSync>;
8
11
  export {};
9
12
  //# sourceMappingURL=RequestFsStat.d.ts.map
@@ -1,8 +1,10 @@
1
1
  /// <reference types="node" />
2
+ import { ServiceRequestFactoryRequestType } from '@cspell/cspell-service-bus';
2
3
  interface RequestParams {
3
4
  readonly url: URL;
4
5
  readonly content: string;
5
6
  }
6
- export declare const RequestFsWriteFile: import("@cspell/cspell-service-bus/request").ServiceRequestFactory<import("@cspell/cspell-service-bus").ServiceRequest<"fs:writeFile", RequestParams, Promise<void>>, RequestParams, "fs:writeFile">;
7
+ export declare const RequestFsWriteFile: import("@cspell/cspell-service-bus/ServiceRequestFactory").ServiceRequestFactory<import("@cspell/cspell-service-bus").ServiceRequest<"fs:writeFile", RequestParams, Promise<void>>, RequestParams, "fs:writeFile">;
8
+ export declare type RequestFsWriteFile = ServiceRequestFactoryRequestType<typeof RequestFsWriteFile>;
7
9
  export {};
8
10
  //# sourceMappingURL=RequestFsWriteFile.d.ts.map
@@ -1,7 +1,9 @@
1
1
  /// <reference types="node" />
2
+ import { ServiceRequestFactoryRequestType } from '@cspell/cspell-service-bus';
2
3
  interface RequestParams {
3
4
  readonly data: Buffer;
4
5
  }
5
- export declare const RequestZlibInflate: import("@cspell/cspell-service-bus/request").ServiceRequestFactory<import("@cspell/cspell-service-bus").ServiceRequest<"zlib:inflate", RequestParams, string>, RequestParams, "zlib:inflate">;
6
+ export declare const RequestZlibInflate: import("@cspell/cspell-service-bus/ServiceRequestFactory").ServiceRequestFactory<import("@cspell/cspell-service-bus").ServiceRequest<"zlib:inflate", RequestParams, string>, RequestParams, "zlib:inflate">;
7
+ export declare type RequestZlibInflate = ServiceRequestFactoryRequestType<typeof RequestZlibInflate>;
6
8
  export {};
7
9
  //# sourceMappingURL=RequestZlibInflate.d.ts.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cspell-io",
3
- "version": "6.5.0",
3
+ "version": "6.6.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.1",
42
+ "@types/node": "^18.6.4",
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.5.0",
49
+ "@cspell/cspell-service-bus": "^6.6.0",
50
50
  "@types/node-fetch": "^2.6.2",
51
51
  "node-fetch": "^2.6.7"
52
52
  },
53
- "gitHead": "65c80b01e17bb0d74b57ef8b29309684a8588e27"
53
+ "gitHead": "673377e6b8b79834d018f9b4d02aef90406b7a29"
54
54
  }