-
-
Notifications
You must be signed in to change notification settings - Fork 92
/
file.ts
203 lines (186 loc) · 5.38 KB
/
file.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import {
createRequestHandler,
createResponse,
createResponseFail,
isServiceResponseFailure,
isServiceResponseSuccess,
ServiceBus,
} from '@cspell/cspell-service-bus';
import assert from 'assert';
import { promises as fs, readFileSync, statSync } from 'fs';
import { gunzipSync, gzipSync } from 'zlib';
import { toError } from '../../errors';
import { fetchURL } from '../../node/file/fetch';
import { getStatHttp } from '../../node/file/stat';
import {
RequestFsReadBinaryFile,
RequestFsReadBinaryFileSync,
RequestFsReadFile,
RequestFsReadFileSync,
RequestFsStat,
RequestFsStatSync,
RequestFsWriteFile,
RequestZlibInflate,
} from '../../requests';
const isGzFileRegExp = /\.gz($|[?#])/;
function isGzFile(url: URL): boolean {
return isGzFileRegExp.test(url.pathname);
}
/**
* Handle Binary File Reads
*/
const handleRequestFsReadBinaryFile = createRequestHandler(
RequestFsReadBinaryFile,
({ params }) => createResponse(fs.readFile(params.url)),
undefined,
'Node: Read Binary File.'
);
/**
* Handle Binary File Sync Reads
*/
const handleRequestFsReadBinaryFileSync = createRequestHandler(
RequestFsReadBinaryFileSync,
({ params }) => createResponse(readFileSync(params.url)),
undefined,
'Node: Sync Read Binary File.'
);
/**
* Handle UTF-8 Text File Reads
*/
const handleRequestFsReadFile = createRequestHandler(
RequestFsReadFile,
(req, _, dispatcher) => {
const { url, encoding } = req.params;
const res = dispatcher.dispatch(RequestFsReadBinaryFile.create({ url }));
if (!isServiceResponseSuccess(res)) {
assert(isServiceResponseFailure(res));
return createResponseFail(req, res.error);
}
return createResponse(res.value.then((buf) => bufferToText(buf, encoding)));
},
undefined,
'Node: Read Text File.'
);
/**
* Handle UTF-8 Text File Reads
*/
const handleRequestFsReadFileSync = createRequestHandler(
RequestFsReadFileSync,
(req, _, dispatcher) => {
const { url, encoding } = req.params;
const res = dispatcher.dispatch(RequestFsReadBinaryFileSync.create({ url }));
if (!isServiceResponseSuccess(res)) {
assert(isServiceResponseFailure(res));
return createResponseFail(req, res.error);
}
return createResponse(bufferToText(res.value, encoding));
},
undefined,
'Node: Sync Read Text File.'
);
/**
* Handle deflating gzip data
*/
const handleRequestZlibInflate = createRequestHandler(
RequestZlibInflate,
({ params }) => createResponse(gunzipSync(params.data).toString('utf-8')),
undefined,
'Node: gz deflate.'
);
const supportedFetchProtocols: Record<string, true | undefined> = { 'http:': true, 'https:': true };
/**
* Handle reading gzip'ed text files.
*/
const handleRequestFsReadBinaryFileHttp = createRequestHandler(
RequestFsReadBinaryFile,
(req, next) => {
const { url } = req.params;
if (!(url.protocol in supportedFetchProtocols)) return next(req);
return createResponse(fetchURL(url));
},
undefined,
'Node: Read Http(s) file.'
);
function bufferToText(buf: Buffer, encoding: BufferEncoding): string {
return buf[0] === 0x1f && buf[1] === 0x8b ? bufferToText(gunzipSync(buf), encoding) : buf.toString(encoding);
}
/**
* Handle fs:stat
*/
const handleRequestFsStat = createRequestHandler(
RequestFsStat,
({ params }) => createResponse(fs.stat(params.url)),
undefined,
'Node: fs.stat.'
);
/**
* Handle fs:statSync
*/
const handleRequestFsStatSync = createRequestHandler(
RequestFsStatSync,
(req) => {
const { params } = req;
try {
return createResponse(statSync(params.url));
} catch (e) {
return createResponseFail(req, toError(e));
}
},
undefined,
'Node: fs.stat.'
);
/**
* Handle deflating gzip data
*/
const handleRequestFsStatHttp = createRequestHandler(
RequestFsStat,
(req, next) => {
const { url } = req.params;
if (!(url.protocol in supportedFetchProtocols)) return next(req);
return createResponse(getStatHttp(url));
},
undefined,
'Node: http get stat'
);
/**
* Handle fs:writeFile
*/
const handleRequestFsWriteFile = createRequestHandler(
RequestFsWriteFile,
({ params }) => createResponse(fs.writeFile(params.url, params.content)),
undefined,
'Node: fs.writeFile'
);
/**
* Handle fs:writeFile compressed
*/
const handleRequestFsWriteFileGz = createRequestHandler(
RequestFsWriteFile,
(req, next) => {
const { url, content } = req.params;
if (!isGzFile(url)) return next(req);
return createResponse(fs.writeFile(url, gzipSync(content)));
},
undefined,
'Node: http get stat'
);
export function registerHandlers(serviceBus: ServiceBus) {
/**
* Handlers are in order of low to high level
* Order is VERY important.
*/
const handlers = [
handleRequestFsWriteFile,
handleRequestFsWriteFileGz,
handleRequestFsReadBinaryFile,
handleRequestFsReadBinaryFileSync,
handleRequestFsReadBinaryFileHttp,
handleRequestFsReadFile,
handleRequestFsReadFileSync,
handleRequestZlibInflate,
handleRequestFsStatSync,
handleRequestFsStat,
handleRequestFsStatHttp,
];
handlers.forEach((handler) => serviceBus.addHandler(handler));
}