Repository navigation
Expand file tree
/
Copy pathapi.cjs
More file actions
878 lines (816 loc) · 31.3 KB
/
Copy pathapi.cjs
File metadata and controls
878 lines (816 loc) · 31.3 KB
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
'use strict';
const fs = require('node:fs');
const path = require('node:path');
const { spawn, spawnSync } = require('node:child_process');
const { TextDecoder } = require('node:util');
const PROTOCOL = 'zigcss-node-v1';
const INTERNAL_ARGUMENT = '--internal-node-v1';
const MAX_SOURCE_BYTES = 10 * 1024 * 1024;
const MAX_REQUEST_BYTES = 64 * 1024 * 1024;
const MAX_RESPONSE_BYTES = 128 * 1024 * 1024;
const MAX_STDERR_BYTES = 4 * 1024 * 1024;
const MAX_SYNC_FRAME_BYTES = 4 * 1024 * 1024;
const DEFAULT_TIMEOUT_MS = 30_000;
const MAX_TIMEOUT_MS = 120_000;
const MAX_PATH_BYTES = 4096;
// Keep the JavaScript admission boundary identical to the native target-query
// parser so an oversized query is rejected before a child process is created.
const MAX_BROWSERS_BYTES = 4096;
const MAX_REQUEST_ID_BYTES = 64;
const supportedSyntaxes = Object.freeze(['css', 'scss', 'sass', 'less', 'stylus']);
const extensions = Object.freeze({
'.css': 'css',
'.scss': 'scss',
'.sass': 'sass',
'.less': 'less',
'.styl': 'stylus',
'.stylus': 'stylus',
});
const syntheticExtensions = Object.freeze({
css: 'css',
scss: 'scss',
sass: 'sass',
less: 'less',
stylus: 'styl',
});
const allowedOptionNames = new Set([
'syntax',
'sourcePath',
'rootPaths',
'format',
'sourceMap',
'optimize',
'browsers',
'timeoutMs',
'signal',
]);
const utf8Decoder = new TextDecoder('utf-8', { fatal: true });
let requestSequence = 0;
class ZigCssCompileError extends Error {
constructor(code, message, diagnostics = [], options = undefined) {
super(message, options);
this.name = 'ZigCssCompileError';
this.code = code;
this.diagnostics = cloneAndFreezeObjectArray(diagnostics, 'diagnostics');
}
}
function fail(code, message, diagnostics = [], options = undefined) {
throw new ZigCssCompileError(code, message, diagnostics, options);
}
function isPlainObject(value) {
if (value === null || typeof value !== 'object' || Array.isArray(value)) return false;
const prototype = Object.getPrototypeOf(value);
return prototype === Object.prototype || prototype === null;
}
function exactKeys(value, expected, label) {
if (!isPlainObject(value)) fail('API_PROTOCOL', `${label} must be an object`);
const actual = Object.keys(value).sort();
const wanted = [...expected].sort();
if (actual.length !== wanted.length || actual.some((key, index) => key !== wanted[index])) {
fail('API_PROTOCOL', `${label} has unexpected or missing fields`);
}
}
function cloneJsonValue(value, label, depth = 0) {
if (depth > 64) fail('API_PROTOCOL', `${label} exceeds the nesting limit`);
if (value === null || typeof value === 'boolean') return value;
if (typeof value === 'string') {
if (!isWellFormedUtf8(value)) fail('API_PROTOCOL', `${label} is not well-formed Unicode`);
return value;
}
if (typeof value === 'number' && Number.isFinite(value)) return value;
if (Array.isArray(value)) {
return Object.freeze(value.map((item, index) => cloneJsonValue(item, `${label}[${index}]`, depth + 1)));
}
if (!isPlainObject(value)) fail('API_PROTOCOL', `${label} is not JSON-safe`);
const clone = {};
for (const [key, item] of Object.entries(value)) {
Object.defineProperty(clone, key, {
value: cloneJsonValue(item, `${label}.${key}`, depth + 1),
enumerable: true,
configurable: false,
writable: false,
});
}
return Object.freeze(clone);
}
function cloneAndFreezeObjectArray(value, label) {
if (!Array.isArray(value)) fail('API_PROTOCOL', `${label} must be an array`);
return Object.freeze(value.map((entry, index) => {
if (!isPlainObject(entry)) fail('API_PROTOCOL', `${label}[${index}] must be an object`);
return cloneJsonValue(entry, `${label}[${index}]`);
}));
}
function isProtocolString(value, { nonEmpty = false } = {}) {
return typeof value === 'string' &&
(!nonEmpty || value.length !== 0) &&
isWellFormedUtf8(value);
}
function validateDiagnostics(value, label) {
if (!Array.isArray(value)) fail('API_PROTOCOL', `${label} must be an array`);
for (const [index, diagnostic] of value.entries()) {
const itemLabel = `${label}[${index}]`;
if (!isPlainObject(diagnostic)) fail('API_PROTOCOL', `${itemLabel} must be an object`);
for (const field of ['severity', 'code', 'message', 'sourceUrl', 'line', 'column']) {
if (!Object.hasOwn(diagnostic, field)) {
fail('API_PROTOCOL', `${itemLabel} is missing ${field}`);
}
}
if (!['error', 'warning', 'note'].includes(diagnostic.severity)) {
fail('API_PROTOCOL', `${itemLabel}.severity is invalid`);
}
if (!isProtocolString(diagnostic.code, { nonEmpty: true }) ||
!/^[A-Z][A-Z0-9_]{1,63}$/.test(diagnostic.code)) {
fail('API_PROTOCOL', `${itemLabel}.code is invalid`);
}
if (!isProtocolString(diagnostic.message, { nonEmpty: true })) {
fail('API_PROTOCOL', `${itemLabel}.message is invalid`);
}
if (!isProtocolString(diagnostic.sourceUrl, { nonEmpty: true })) {
fail('API_PROTOCOL', `${itemLabel}.sourceUrl is invalid`);
}
if (!Number.isSafeInteger(diagnostic.line) || diagnostic.line < 1 ||
!Number.isSafeInteger(diagnostic.column) || diagnostic.column < 0) {
fail('API_PROTOCOL', `${itemLabel} location is invalid`);
}
}
return cloneAndFreezeObjectArray(value, label);
}
function validateDependencies(value) {
const label = 'result.dependencies';
if (!Array.isArray(value)) fail('API_PROTOCOL', `${label} must be an array`);
for (const [index, dependency] of value.entries()) {
const itemLabel = `${label}[${index}]`;
if (!isPlainObject(dependency)) fail('API_PROTOCOL', `${itemLabel} must be an object`);
if (dependency.kind === 'css-import' || dependency.kind === 'css-module') {
exactKeys(dependency, ['kind', 'specifier', 'sourceUrl', 'start', 'end'], itemLabel);
if (!isProtocolString(dependency.specifier, { nonEmpty: true }) ||
!isProtocolString(dependency.sourceUrl, { nonEmpty: true })) {
fail('API_PROTOCOL', `${itemLabel} paths are invalid`);
}
if (!Number.isSafeInteger(dependency.start) || dependency.start < 0 ||
!Number.isSafeInteger(dependency.end) || dependency.end < dependency.start) {
fail('API_PROTOCOL', `${itemLabel} span is invalid`);
}
} else if (['import', 'use', 'forward', 'reference'].includes(dependency.kind)) {
exactKeys(dependency, ['kind', 'url'], itemLabel);
if (!isProtocolString(dependency.url, { nonEmpty: true })) {
fail('API_PROTOCOL', `${itemLabel}.url is invalid`);
}
} else {
fail('API_PROTOCOL', `${itemLabel}.kind is invalid`);
}
}
return cloneAndFreezeObjectArray(value, label);
}
function validateSourceMapShape(value) {
if (
!isPlainObject(value) ||
value.version !== 3 ||
!Array.isArray(value.sources) ||
!value.sources.every(item => typeof item === 'string') ||
!Array.isArray(value.names) ||
!value.names.every(item => typeof item === 'string') ||
typeof value.mappings !== 'string'
) {
fail('API_PROTOCOL', 'result.sourceMap is not a Source Map v3 object');
}
if (value.file !== undefined && typeof value.file !== 'string') {
fail('API_PROTOCOL', 'result.sourceMap.file must be a string');
}
if (value.sourceRoot !== undefined && typeof value.sourceRoot !== 'string') {
fail('API_PROTOCOL', 'result.sourceMap.sourceRoot must be a string');
}
if (
value.sourcesContent !== undefined &&
(
!Array.isArray(value.sourcesContent) ||
value.sourcesContent.length !== value.sources.length ||
!value.sourcesContent.every(item => item === null || typeof item === 'string')
)
) {
fail('API_PROTOCOL', 'result.sourceMap.sourcesContent must align with sources');
}
}
function apiOptionsFailure(message) {
fail('API_OPTIONS', message);
}
function isWellFormedUtf8(value) {
return Buffer.from(value, 'utf8').toString('utf8') === value;
}
function validateBoundedPath(value, label) {
if (
typeof value !== 'string' ||
value.length === 0 ||
!isWellFormedUtf8(value) ||
Buffer.byteLength(value) > MAX_PATH_BYTES ||
/[\0\r\n]/.test(value)
) {
apiOptionsFailure(`${label} must be a bounded local path`);
}
const resolved = path.resolve(value);
if (process.platform === 'win32' && !/^[A-Za-z]:[\\/]/.test(resolved)) {
apiOptionsFailure(`${label} must resolve to a drive-local path`);
}
return resolved;
}
function canonicalDirectory(value, label) {
const resolved = validateBoundedPath(value, label);
let canonical;
try {
canonical = fs.realpathSync.native(resolved);
if (!fs.statSync(canonical).isDirectory()) {
apiOptionsFailure(`${label} must identify an existing directory`);
}
} catch (error) {
if (error instanceof ZigCssCompileError) throw error;
apiOptionsFailure(`${label} must identify an existing directory`);
}
return validateBoundedPath(canonical, label);
}
function canonicalSourcePath(value, label) {
const resolved = validateBoundedPath(value, label);
const parent = canonicalDirectory(path.dirname(resolved), `${label} parent`);
return validateBoundedPath(path.join(parent, path.basename(resolved)), label);
}
function canonicalInputFile(value) {
const resolved = validateBoundedPath(value, 'filename');
try {
const canonical = validateBoundedPath(fs.realpathSync.native(resolved), 'filename');
const stat = fs.lstatSync(canonical, { bigint: true });
if (!stat.isFile() || stat.isSymbolicLink()) {
fail('API_INPUT', 'filename must identify a regular file');
}
return Object.freeze({
filename: canonical,
device: stat.dev,
inode: stat.ino,
});
} catch (error) {
if (error instanceof ZigCssCompileError) throw error;
fail('API_INPUT', `source file could not be resolved: ${error.message}`, [], { cause: error });
}
}
function inputOpenFlags() {
return fs.constants.O_RDONLY | (fs.constants.O_NOFOLLOW ?? 0);
}
function validateOpenedInput(expected, stat, label) {
if (!stat.isFile() || stat.isSymbolicLink() ||
stat.dev !== expected.device || stat.ino !== expected.inode) {
fail('API_INPUT', `${label} changed before it could be read safely`);
}
if (stat.size < 0n || stat.size > BigInt(MAX_SOURCE_BYTES)) {
fail('API_INPUT', 'source exceeds the 10 MiB limit');
}
}
function validateStableInput(before, after, bytesRead, label) {
if (
!after.isFile() ||
after.dev !== before.dev ||
after.ino !== before.ino ||
after.size !== before.size ||
after.mtimeNs !== before.mtimeNs ||
after.ctimeNs !== before.ctimeNs ||
BigInt(bytesRead) !== before.size
) {
fail('API_INPUT', `${label} changed while it was being read`);
}
}
function containsLocalPath(root, candidate) {
const relative = path.relative(root, candidate);
return relative === '' || (
relative !== '..' &&
!relative.startsWith(`..${path.sep}`) &&
!path.isAbsolute(relative)
);
}
function detectSyntax(filename) {
if (
typeof filename !== 'string' ||
filename.length === 0 ||
!isWellFormedUtf8(filename) ||
/[\0\r\n]/.test(filename)
) {
apiOptionsFailure('filename must be a local path with a supported extension');
}
const syntax = extensions[path.extname(filename).toLowerCase()];
if (syntax === undefined) {
apiOptionsFailure('filename must end in .css, .scss, .sass, .less, .styl, or .stylus');
}
return syntax;
}
function normalizeSignal(value, asyncMode) {
if (value === undefined) return undefined;
if (!asyncMode) apiOptionsFailure('signal is only available for asynchronous compilation');
if (typeof AbortSignal === 'undefined' || !(value instanceof AbortSignal)) {
apiOptionsFailure('signal must be an AbortSignal');
}
return value;
}
function normalizeOptions(optionValue, sourcePathOverride, asyncMode, inferRequired = false) {
const options = optionValue === undefined ? {} : optionValue;
if (!isPlainObject(options)) apiOptionsFailure('compile options must be an object');
for (const key of Object.keys(options)) {
if (!allowedOptionNames.has(key)) apiOptionsFailure(`unknown compile option: ${key}`);
}
if (sourcePathOverride !== undefined && options.sourcePath !== undefined) {
apiOptionsFailure('compileFile does not accept sourcePath');
}
let syntax = options.syntax;
if (syntax !== undefined && !supportedSyntaxes.includes(syntax)) {
apiOptionsFailure(`unsupported syntax: ${String(syntax)}`);
}
let sourcePath;
if (sourcePathOverride !== undefined) {
sourcePath = canonicalSourcePath(sourcePathOverride, 'filename');
} else if (options.sourcePath !== undefined) {
sourcePath = canonicalSourcePath(options.sourcePath, 'sourcePath');
}
if (syntax === undefined && sourcePath !== undefined) syntax = detectSyntax(sourcePath);
if (syntax === undefined) {
if (inferRequired) apiOptionsFailure('syntax could not be inferred from the filename');
syntax = 'css';
}
if (sourcePath === undefined) {
sourcePath = canonicalSourcePath(
path.resolve(process.cwd(), `.zigcss-input.${syntheticExtensions[syntax]}`),
'sourcePath',
);
}
const format = options.format ?? 'pretty';
if (format !== 'pretty' && format !== 'minified') {
apiOptionsFailure('format must be pretty or minified');
}
const sourceMap = options.sourceMap ?? false;
const optimize = options.optimize ?? false;
if (typeof sourceMap !== 'boolean' || typeof optimize !== 'boolean') {
apiOptionsFailure('sourceMap and optimize must be boolean');
}
if (sourceMap && optimize) {
apiOptionsFailure('source maps are unavailable with fixed-point optimization');
}
let browsers = options.browsers ?? null;
if (
browsers !== null &&
(
typeof browsers !== 'string' ||
browsers.length === 0 ||
!isWellFormedUtf8(browsers) ||
Buffer.byteLength(browsers) > MAX_BROWSERS_BYTES
)
) {
apiOptionsFailure('browsers must be null or a non-empty bounded target query');
}
const timeoutMs = options.timeoutMs ?? DEFAULT_TIMEOUT_MS;
if (!Number.isSafeInteger(timeoutMs) || timeoutMs < 1 || timeoutMs > MAX_TIMEOUT_MS) {
apiOptionsFailure(`timeoutMs must be an integer from 1 through ${MAX_TIMEOUT_MS}`);
}
const signal = normalizeSignal(options.signal, asyncMode);
let rootPaths;
if (options.rootPaths === undefined) {
rootPaths = [path.dirname(sourcePath)];
} else {
if (!Array.isArray(options.rootPaths) || options.rootPaths.length < 1 || options.rootPaths.length > 16) {
apiOptionsFailure('rootPaths must contain from 1 through 16 local paths');
}
rootPaths = Array.from(options.rootPaths, (rootPath, index) => (
canonicalDirectory(rootPath, `rootPaths[${index}]`)
));
if (new Set(rootPaths).size !== rootPaths.length) {
apiOptionsFailure('rootPaths must be unique after resolution');
}
}
if (!rootPaths.some(rootPath => containsLocalPath(rootPath, sourcePath))) {
apiOptionsFailure('sourcePath must be contained by at least one rootPaths directory');
}
return Object.freeze({
syntax,
sourcePath,
rootPaths: Object.freeze(rootPaths),
format,
sourceMap,
optimize,
browsers,
timeoutMs,
signal,
});
}
function validateSource(source) {
if (typeof source !== 'string') fail('API_INPUT', 'source must be a string');
if (!isWellFormedUtf8(source)) fail('API_INPUT_ENCODING', 'source is not well-formed Unicode');
if (Buffer.byteLength(source) > MAX_SOURCE_BYTES) {
fail('API_INPUT', 'source exceeds the 10 MiB limit');
}
return source;
}
function nextRequestId() {
requestSequence = (requestSequence + 1) % 0x1_0000_0000;
const value = `node-${process.pid}-${Date.now().toString(36)}-${requestSequence.toString(36)}`;
if (Buffer.byteLength(value) > MAX_REQUEST_ID_BYTES) fail('API_INTERNAL', 'request identifier overflow');
return value;
}
function buildFrame(source, options) {
const requestId = nextRequestId();
const request = {
protocol: PROTOCOL,
requestId,
operation: 'compile',
source,
sourcePath: options.sourcePath,
rootPaths: options.rootPaths,
options: {
syntax: options.syntax,
format: options.format,
sourceMap: options.sourceMap,
optimize: options.optimize,
browsers: options.browsers,
},
};
let body;
try {
body = Buffer.from(JSON.stringify(request), 'utf8');
} catch (error) {
fail('API_INPUT', 'request could not be encoded as JSON', [], { cause: error });
}
if (body.length === 0 || body.length > MAX_REQUEST_BYTES) {
fail('API_INPUT', 'encoded request exceeds the 64 MiB limit');
}
const frame = Buffer.allocUnsafe(body.length + 4);
frame.writeUInt32BE(body.length, 0);
body.copy(frame, 4);
return Object.freeze({ frame, requestId });
}
function regularExecutable(filename) {
try {
const stat = fs.lstatSync(filename);
if (!stat.isFile() || stat.isSymbolicLink() ||
(process.platform !== 'win32' && (stat.mode & 0o111) === 0)) {
return null;
}
return fs.realpathSync(filename);
} catch {
return null;
}
}
function sourceCheckoutBinary(binaryName) {
let root;
try {
root = fs.realpathSync(__dirname);
} catch {
return null;
}
for (const marker of ['build.zig', path.join('src', 'node_protocol.zig')]) {
try {
const stat = fs.lstatSync(path.join(root, marker));
if (!stat.isFile() || stat.isSymbolicLink()) return null;
} catch {
return null;
}
}
const candidate = regularExecutable(path.join(root, 'zig-out', 'bin', binaryName));
return candidate !== null && containsLocalPath(root, candidate) ? candidate : null;
}
function binaryPath() {
const binaryName = process.platform === 'win32' ? 'zigcss.exe' : 'zigcss';
const sourceBinary = sourceCheckoutBinary(binaryName);
if (sourceBinary !== null) return sourceBinary;
let packageRoot;
try {
packageRoot = fs.realpathSync(__dirname);
} catch {
packageRoot = null;
}
const packagedBinary = packageRoot === null
? null
: regularExecutable(path.join(packageRoot, 'bin', binaryName));
if (
packagedBinary === null ||
packageRoot === null ||
!containsLocalPath(packageRoot, packagedBinary)
) {
fail(
'API_BINARY_NOT_FOUND',
'zigcss binary is missing or not executable; run zigcss-install, allow the package install script, or build this source checkout',
);
}
return packagedBinary;
}
function decodeUtf8(buffer, label) {
try {
return utf8Decoder.decode(buffer);
} catch (error) {
fail('API_PROTOCOL', `${label} is not valid UTF-8`, [], { cause: error });
}
}
function decodeResponse(frame, requestId) {
if (!Buffer.isBuffer(frame) || frame.length < 4) {
fail('API_PROTOCOL', 'response frame is truncated');
}
const declared = frame.readUInt32BE(0);
if (declared === 0 || declared > MAX_RESPONSE_BYTES) {
fail('API_RESPONSE_LIMIT', 'response frame exceeds the 128 MiB limit');
}
if (frame.length !== declared + 4) {
fail('API_PROTOCOL', frame.length < declared + 4
? 'response frame is truncated'
: 'response frame contains trailing bytes');
}
let response;
try {
response = JSON.parse(decodeUtf8(frame.subarray(4), 'response frame'));
} catch (error) {
if (error instanceof ZigCssCompileError) throw error;
fail('API_PROTOCOL', 'response frame does not contain valid JSON', [], { cause: error });
}
if (!isPlainObject(response)) fail('API_PROTOCOL', 'response must be an object');
if (response.protocol !== PROTOCOL) fail('API_PROTOCOL', 'response protocol does not match the request');
if (response.requestId !== requestId) fail('API_PROTOCOL', 'response requestId does not match the request');
if (typeof response.ok !== 'boolean') fail('API_PROTOCOL', 'response ok field must be boolean');
if (!response.ok) {
exactKeys(response, ['protocol', 'requestId', 'ok', 'error'], 'failure response');
exactKeys(response.error, ['code', 'message', 'diagnostics'], 'failure error');
const { code, message, diagnostics } = response.error;
if (typeof code !== 'string' || !/^[A-Z][A-Z0-9_]{1,63}$/.test(code)) {
fail('API_PROTOCOL', 'failure error code is invalid');
}
if (typeof message !== 'string' || message.length === 0) {
fail('API_PROTOCOL', 'failure error message is invalid');
}
if (!isProtocolString(message, { nonEmpty: true })) {
fail('API_PROTOCOL', 'failure error message is invalid');
}
const ownedDiagnostics = validateDiagnostics(diagnostics, 'failure diagnostics');
throw new ZigCssCompileError(code, message, ownedDiagnostics);
}
exactKeys(response, ['protocol', 'requestId', 'ok', 'result'], 'success response');
exactKeys(response.result, ['css', 'sourceMap', 'diagnostics', 'dependencies'], 'success result');
const { css, sourceMap, diagnostics, dependencies } = response.result;
if (!isProtocolString(css)) fail('API_PROTOCOL', 'result.css must be a well-formed string');
let ownedSourceMap = null;
if (sourceMap !== null) {
if (typeof sourceMap !== 'string') fail('API_PROTOCOL', 'result.sourceMap must be null or JSON text');
try {
const parsedMap = JSON.parse(sourceMap);
validateSourceMapShape(parsedMap);
ownedSourceMap = cloneJsonValue(parsedMap, 'result.sourceMap');
} catch (error) {
if (error instanceof ZigCssCompileError) throw error;
fail('API_PROTOCOL', 'result.sourceMap does not contain valid JSON', [], { cause: error });
}
}
return Object.freeze({
css,
sourceMap: ownedSourceMap,
diagnostics: validateDiagnostics(diagnostics, 'result.diagnostics'),
dependencies: validateDependencies(dependencies),
});
}
function boundedStderr(buffer) {
if (!Buffer.isBuffer(buffer) || buffer.length === 0) return '';
return buffer.subarray(0, 4096).toString('utf8').trim();
}
function processFailureMessage(status, signal, stderr) {
const detail = boundedStderr(stderr);
if (detail !== '') return `native compiler failed: ${detail}`;
if (signal) return `native compiler was terminated by ${signal}`;
return `native compiler exited with status ${status ?? 'unknown'}`;
}
function runSync(frame, requestId, options) {
const executable = binaryPath();
const result = spawnSync(executable, [INTERNAL_ARGUMENT], {
input: frame,
shell: false,
windowsHide: true,
encoding: null,
timeout: options.timeoutMs,
maxBuffer: MAX_SYNC_FRAME_BYTES,
});
const stdout = Buffer.isBuffer(result.stdout) ? result.stdout : Buffer.alloc(0);
const stderr = Buffer.isBuffer(result.stderr) ? result.stderr : Buffer.alloc(0);
if (stderr.length > MAX_STDERR_BYTES) {
fail('API_STDERR_LIMIT', 'native compiler stderr exceeds the 4 MiB limit');
}
if (stdout.length > MAX_SYNC_FRAME_BYTES) {
fail('API_RESPONSE_LIMIT', 'native compiler sync response exceeds the 4 MiB framed-output limit');
}
if (result.error) {
if (result.error.code === 'ENOENT') {
fail('API_BINARY_NOT_FOUND', 'zigcss binary could not be started', [], { cause: result.error });
}
if (result.error.code === 'ETIMEDOUT') {
fail('API_TIMEOUT', `native compiler exceeded the ${options.timeoutMs} ms timeout`, [], { cause: result.error });
}
if (result.error.code === 'ENOBUFS') {
const stderrLimit = stderr.length >= MAX_STDERR_BYTES && stdout.length < MAX_SYNC_FRAME_BYTES;
fail(
stderrLimit ? 'API_STDERR_LIMIT' : 'API_RESPONSE_LIMIT',
stderrLimit
? 'native compiler stderr exceeds the 4 MiB limit'
: 'native compiler sync response exceeds the 4 MiB framed-output limit',
[],
{ cause: result.error },
);
}
fail('API_SPAWN', `failed to run the native compiler: ${result.error.message}`, [], { cause: result.error });
}
if (result.signal || result.status !== 0) {
fail('API_PROCESS', processFailureMessage(result.status, result.signal, stderr));
}
return decodeResponse(stdout, requestId);
}
function runAsync(frame, requestId, options) {
if (options.signal?.aborted) {
return Promise.reject(new ZigCssCompileError('API_ABORTED', 'compilation was aborted'));
}
return new Promise((resolve, reject) => {
let child;
try {
child = spawn(binaryPath(), [INTERNAL_ARGUMENT], {
shell: false,
windowsHide: true,
stdio: ['pipe', 'pipe', 'pipe'],
});
} catch (error) {
reject(error instanceof ZigCssCompileError
? error
: new ZigCssCompileError('API_SPAWN', `failed to run the native compiler: ${error.message}`, [], { cause: error }));
return;
}
const stdoutChunks = [];
const stderrChunks = [];
let stdoutBytes = 0;
let stderrBytes = 0;
let terminal = null;
let spawnError = null;
let settled = false;
const terminate = reason => {
if (terminal === null) terminal = reason;
if (child.exitCode === null && child.signalCode === null) child.kill('SIGKILL');
};
const timer = setTimeout(() => terminate('timeout'), options.timeoutMs);
const abort = () => terminate('aborted');
options.signal?.addEventListener('abort', abort, { once: true });
child.stdout.on('data', chunk => {
stdoutBytes += chunk.length;
if (stdoutBytes > MAX_RESPONSE_BYTES + 4) {
terminate('response-limit');
return;
}
stdoutChunks.push(chunk);
});
child.stderr.on('data', chunk => {
stderrBytes += chunk.length;
if (stderrBytes > MAX_STDERR_BYTES) {
terminate('stderr-limit');
return;
}
stderrChunks.push(chunk);
});
child.on('error', error => {
spawnError = error;
});
child.stdin.on('error', error => {
if (error.code !== 'EPIPE' && spawnError === null) spawnError = error;
});
child.on('close', (status, signal) => {
if (settled) return;
settled = true;
clearTimeout(timer);
options.signal?.removeEventListener('abort', abort);
const stderr = Buffer.concat(stderrChunks, Math.min(stderrBytes, MAX_STDERR_BYTES));
try {
if (terminal === 'timeout') {
fail('API_TIMEOUT', `native compiler exceeded the ${options.timeoutMs} ms timeout`);
}
if (terminal === 'aborted') fail('API_ABORTED', 'compilation was aborted');
if (terminal === 'response-limit') {
fail('API_RESPONSE_LIMIT', 'native compiler response exceeds the 128 MiB limit');
}
if (terminal === 'stderr-limit') {
fail('API_STDERR_LIMIT', 'native compiler stderr exceeds the 4 MiB limit');
}
if (spawnError) {
if (spawnError.code === 'ENOENT') {
fail('API_BINARY_NOT_FOUND', 'zigcss binary could not be started', [], { cause: spawnError });
}
fail('API_SPAWN', `failed to run the native compiler: ${spawnError.message}`, [], { cause: spawnError });
}
if (signal || status !== 0) {
fail('API_PROCESS', processFailureMessage(status, signal, stderr));
}
resolve(decodeResponse(Buffer.concat(stdoutChunks, stdoutBytes), requestId));
} catch (error) {
reject(error instanceof ZigCssCompileError
? error
: new ZigCssCompileError('API_INTERNAL', error?.message || 'compilation failed', [], { cause: error }));
}
});
child.stdin.end(frame);
});
}
function prepare(source, optionValue, sourcePathOverride, asyncMode, inferRequired = false) {
const validatedSource = validateSource(source);
const options = normalizeOptions(optionValue, sourcePathOverride, asyncMode, inferRequired);
const { frame, requestId } = buildFrame(validatedSource, options);
return { frame, requestId, options };
}
async function readSourceFile(input) {
let handle;
try {
handle = await fs.promises.open(input.filename, inputOpenFlags());
const before = await handle.stat({ bigint: true });
validateOpenedInput(input, before, 'source file');
const chunks = [];
const buffer = Buffer.allocUnsafe(Math.min(64 * 1024, MAX_SOURCE_BYTES + 1));
let total = 0;
while (true) {
const remaining = MAX_SOURCE_BYTES + 1 - total;
const { bytesRead } = await handle.read(buffer, 0, Math.min(buffer.length, remaining), total);
if (bytesRead === 0) break;
total += bytesRead;
if (total > MAX_SOURCE_BYTES) fail('API_INPUT', 'source exceeds the 10 MiB limit');
chunks.push(Buffer.from(buffer.subarray(0, bytesRead)));
}
const after = await handle.stat({ bigint: true });
validateStableInput(before, after, total, 'source file');
const bytes = Buffer.concat(chunks, total);
try {
return utf8Decoder.decode(bytes);
} catch (error) {
fail('API_INPUT_ENCODING', 'source file is not valid UTF-8', [], { cause: error });
}
} catch (error) {
if (error instanceof ZigCssCompileError) throw error;
fail('API_INPUT', `source file could not be read: ${error.message}`, [], { cause: error });
} finally {
if (handle !== undefined) await handle.close().catch(() => {});
}
}
function readSourceFileSync(input) {
let descriptor;
try {
descriptor = fs.openSync(input.filename, inputOpenFlags());
const before = fs.fstatSync(descriptor, { bigint: true });
validateOpenedInput(input, before, 'source file');
const chunks = [];
const buffer = Buffer.allocUnsafe(Math.min(64 * 1024, MAX_SOURCE_BYTES + 1));
let total = 0;
while (true) {
const remaining = MAX_SOURCE_BYTES + 1 - total;
const bytesRead = fs.readSync(descriptor, buffer, 0, Math.min(buffer.length, remaining), total);
if (bytesRead === 0) break;
total += bytesRead;
if (total > MAX_SOURCE_BYTES) fail('API_INPUT', 'source exceeds the 10 MiB limit');
chunks.push(Buffer.from(buffer.subarray(0, bytesRead)));
}
const after = fs.fstatSync(descriptor, { bigint: true });
validateStableInput(before, after, total, 'source file');
const bytes = Buffer.concat(chunks, total);
try {
return utf8Decoder.decode(bytes);
} catch (error) {
fail('API_INPUT_ENCODING', 'source file is not valid UTF-8', [], { cause: error });
}
} catch (error) {
if (error instanceof ZigCssCompileError) throw error;
fail('API_INPUT', `source file could not be read: ${error.message}`, [], { cause: error });
} finally {
if (descriptor !== undefined) {
try {
fs.closeSync(descriptor);
} catch {
// Preserve the original read result or failure; the descriptor is not reused.
}
}
}
}
async function compile(source, options) {
const prepared = prepare(source, options, undefined, true);
return runAsync(prepared.frame, prepared.requestId, prepared.options);
}
function compileSync(source, options) {
const prepared = prepare(source, options, undefined, false);
return runSync(prepared.frame, prepared.requestId, prepared.options);
}
async function compileFile(filename, options) {
const input = canonicalInputFile(filename);
const normalizedOptions = normalizeOptions(options, input.filename, true, true);
const source = await readSourceFile(input);
const validatedSource = validateSource(source);
const { frame, requestId } = buildFrame(validatedSource, normalizedOptions);
return runAsync(frame, requestId, normalizedOptions);
}
function compileFileSync(filename, options) {
const input = canonicalInputFile(filename);
const normalizedOptions = normalizeOptions(options, input.filename, false, true);
const source = validateSource(readSourceFileSync(input));
const { frame, requestId } = buildFrame(source, normalizedOptions);
return runSync(frame, requestId, normalizedOptions);
}
module.exports = Object.freeze({
compile,
compileSync,
compileFile,
compileFileSync,
detectSyntax,
ZigCssCompileError,
});