Skip to content

Commit 56cb68f

Browse files
committed
make streaming optional #109
1 parent c549e1e commit 56cb68f

File tree

4 files changed

+35
-23
lines changed

4 files changed

+35
-23
lines changed

dist/lib/renderer/render.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ function renderAstNode(data, options, sourcemap, position, errors, reducer, cach
209209
str = `${node.nam}:${options.indent}${(options.minify ? filterValues(node.val) : node.val).reduce(reducer, '').trimEnd()};`;
210210
}
211211
else if (node.typ == EnumToken.AtRuleNodeType && !('chi' in node)) {
212-
str = `${data.val === '' ? '' : options.indent || ' '}${data.val};`;
212+
str = `${node.val === '' ? '' : options.indent || ' '}${node.val};`;
213213
}
214214
else {
215215
str = renderAstNode(node, options, sourcemap, { ...position }, errors, reducer, cache, level + 1, indents);

src/lib/renderer/render.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ function renderAstNode(data: AstNode, options: RenderOptions, sourcemap: SourceM
331331
str = `${(<AstDeclaration>node).nam}:${options.indent}${(options.minify ? filterValues((<AstDeclaration>node).val) : (<AstDeclaration>node).val).reduce(reducer, '').trimEnd()};`;
332332
} else if (node.typ == EnumToken.AtRuleNodeType && !('chi' in node)) {
333333

334-
str = `${(<AstAtRule>data).val === '' ? '' : options.indent || ' '}${(<AstAtRule>data).val};`;
334+
str = `${(<AstAtRule>node).val === '' ? '' : options.indent || ' '}${(<AstAtRule>node).val};`;
335335
} else {
336336

337337
str = renderAstNode(node, options, sourcemap, {...position}, errors, reducer, cache, level + 1, indents);

src/node.ts

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type {
22
AstNode,
3+
LoadResult,
34
ParseInfo,
45
ParseResult,
56
ParserOptions,
@@ -13,7 +14,7 @@ import {doParse, doRender, tokenize, tokenizeStream} from "./lib/index.ts";
1314
import {dirname, matchUrl, resolve} from "./lib/fs/index.ts";
1415
import {Readable} from "node:stream";
1516
import {createReadStream} from "node:fs";
16-
import {lstat} from "node:fs/promises";
17+
import {lstat, readFile} from "node:fs/promises";
1718

1819
export type * from "./@types/index.d.ts";
1920
export type * from "./@types/ast.d.ts";
@@ -57,38 +58,45 @@ export {dirname, resolve};
5758
* load file or url as stream
5859
* @param url
5960
* @param currentFile
61+
* @param asStream
6062
* @throws Error file not found
6163
*
6264
* @private
6365
*/
64-
export async function load(url: string, currentFile: string = '.'): Promise<ReadableStream<Uint8Array> | string> {
66+
export async function load(url: string, currentFile: string = '.', asStream: boolean = false): Promise<string | ReadableStream<Uint8Array<ArrayBufferLike>>> {
6567

6668
const resolved = resolve(url, currentFile);
6769

68-
// @ts-ignore
6970
if (matchUrl.test(resolved.absolute)) {
7071

71-
return fetch(resolved.absolute).then((response: Response) => {
72+
return fetch(resolved.absolute).then(async (response: Response): Promise<string | ReadableStream<Uint8Array<ArrayBufferLike>>> => {
7273

7374
if (!response.ok) {
7475

7576
throw new Error(`${response.status} ${response.statusText} ${response.url}`)
7677
}
7778

78-
return response.body as ReadableStream<Uint8Array<ArrayBuffer>>;
79-
})
79+
return asStream ? response.body as ReadableStream<Uint8Array<ArrayBuffer>> : await response.text();
80+
});
8081
}
8182

8283
try {
8384

85+
if (!asStream) {
86+
87+
return readFile(resolved.absolute, 'utf-8');
88+
}
89+
8490
const stats = await lstat(resolved.absolute);
8591

8692
if (stats.isFile()) {
8793

88-
return Readable.toWeb(createReadStream(resolved.absolute)) as ReadableStream<Uint8Array>;
94+
return Readable.toWeb(createReadStream(resolved.absolute, {encoding: 'utf-8', highWaterMark: 64 * 1024})) as ReadableStream<Uint8Array>;
8995
}
96+
9097
} catch (error) {
9198

99+
console.warn(error);
92100
}
93101

94102
throw new Error(`File not found: '${resolved.absolute || url}'`);
@@ -131,6 +139,7 @@ export function render(data: AstNode, options: RenderOptions = {}): RenderResult
131139
* parse css file
132140
* @param file url or path
133141
* @param options
142+
* @param asStream load file as stream
134143
*
135144
* @throws Error file not found
136145
*
@@ -149,9 +158,9 @@ export function render(data: AstNode, options: RenderOptions = {}): RenderResult
149158
* console.log(result.ast);
150159
* ```
151160
*/
152-
export async function parseFile(file: string, options: ParserOptions = {}): Promise<ParseResult> {
161+
export async function parseFile(file: string, options: ParserOptions = {}, asStream: boolean = false): Promise<ParseResult> {
153162

154-
return load(file).then(stream => parse(stream, {src: file, ...options}));
163+
return Promise.resolve(((options.load ?? load) as (file: string, currentFile: string, asStream: boolean) => LoadResult)(file, '.', asStream)).then(stream => parse(stream, {src: file, ...options}));
155164
}
156165

157166
/**
@@ -211,6 +220,7 @@ export async function parse(stream: string | ReadableStream<Uint8Array>, options
211220
* transform css file
212221
* @param file url or path
213222
* @param options
223+
* @param asStream load file as stream
214224
*
215225
* @throws Error file not found
216226
*
@@ -229,9 +239,9 @@ export async function parse(stream: string | ReadableStream<Uint8Array>, options
229239
* console.log(result.code);
230240
* ```
231241
*/
232-
export async function transformFile(file: string, options: TransformOptions = {}): Promise<TransformResult> {
242+
export async function transformFile(file: string, options: TransformOptions = {}, asStream: boolean = false): Promise<TransformResult> {
233243

234-
return load(file).then(stream => transform(stream, {src: file, ...options}));
244+
return Promise.resolve(((options.load ?? load) as (file: string, currentFile: string, asStream: boolean) => LoadResult)(file,'.', asStream)).then(stream => transform(stream, {src: file, ...options}));
235245
}
236246

237247
/**

src/web.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type {
22
AstNode,
3+
LoadResult,
34
ParseInfo,
45
ParseResult,
56
ParserOptions,
@@ -55,9 +56,10 @@ export {dirname, resolve};
5556
* @param url
5657
* @param currentFile
5758
*
59+
* @param asStream
5860
* @private
5961
*/
60-
export async function load(url: string, currentFile: string = '.'): Promise<ReadableStream<Uint8Array>> {
62+
export async function load(url: string, currentFile: string = '.', asStream: boolean = false): Promise<string | ReadableStream<Uint8Array<ArrayBufferLike>>> {
6163

6264
let t: URL;
6365

@@ -70,20 +72,18 @@ export async function load(url: string, currentFile: string = '.'): Promise<Read
7072
} else {
7173

7274
const path: string = resolve(url, currentFile).absolute;
73-
// @ts-ignore
7475
t = new URL(path, self.origin);
7576
}
7677

77-
// @ts-ignore
78-
return fetch(t, t.origin != self.origin ? {mode: 'cors'} : {}).then((response: Response) => {
78+
return fetch(t, t.origin != self.origin ? {mode: 'cors'} : {}).then(async (response: Response) => {
7979

8080
if (!response.ok) {
8181

8282
throw new Error(`${response.status} ${response.statusText} ${response.url}`)
8383
}
8484

85-
return response.body;
86-
});
85+
return asStream ? response.body : await response.text();
86+
}) as Promise<string | ReadableStream<Uint8Array<ArrayBufferLike>>>;
8787
}
8888

8989
/**
@@ -128,6 +128,7 @@ export function render(data: AstNode, options: RenderOptions = {}): RenderResult
128128
* parse css file
129129
* @param file url or path
130130
* @param options
131+
* @param asStream load file as stream
131132
*
132133
* @throws Error file not found
133134
*
@@ -146,9 +147,9 @@ export function render(data: AstNode, options: RenderOptions = {}): RenderResult
146147
* console.log(result.ast);
147148
* ```
148149
*/
149-
export async function parseFile(file: string, options: ParserOptions = {}): Promise<ParseResult> {
150+
export async function parseFile(file: string, options: ParserOptions = {}, asStream: boolean = false): Promise<ParseResult> {
150151

151-
return load(file).then(stream => parse(stream, {src: file, ...options}));
152+
return Promise.resolve(((options.load ?? load) as (file: string, currentFile: string, asStream: boolean) => LoadResult)(file, '.', asStream)).then(stream => parse(stream, {src: file, ...options}));
152153
}
153154

154155
/**
@@ -198,6 +199,7 @@ export async function parse(stream: string | ReadableStream<Uint8Array>, options
198199
* transform css file
199200
* @param file url or path
200201
* @param options
202+
* @param asStream load file as stream
201203
*
202204
* Example:
203205
*
@@ -214,9 +216,9 @@ export async function parse(stream: string | ReadableStream<Uint8Array>, options
214216
* console.log(result.code);
215217
* ```
216218
*/
217-
export async function transformFile(file: string, options: TransformOptions = {}): Promise<TransformResult> {
219+
export async function transformFile(file: string, options: TransformOptions = {}, asStream: boolean = false): Promise<TransformResult> {
218220

219-
return load(file).then(stream => transform(stream, {src: file, ...options}));
221+
return Promise.resolve(((options.load ?? load) as (file: string, currentFile: string, asStream: boolean) => LoadResult)(file, '.', asStream)).then(stream => transform(stream, {src: file, ...options}));
220222
}
221223

222224
/**

0 commit comments

Comments
 (0)