-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
363 lines (353 loc) · 20.2 KB
/
Copy pathindex.html
File metadata and controls
363 lines (353 loc) · 20.2 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
<!doctype html>
<html lang="en" data-theme="dark">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Node compatibility · SxfeScript</title>
<meta name="description" content="CommonJS, the node: builtins, and .node native addons through a from-scratch Node-API.">
<link rel="icon" type="image/png" href="../../logo.png">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=JetBrains+Mono:wght@400;500;600&family=Newsreader:opsz,wght@6..72,400;6..72,500;6..72,600&family=Public+Sans:wght@400;500;600;700&display=swap" rel="stylesheet">
<link rel="stylesheet" href="../../site.css">
<style>
.docs-layout {
display: grid; grid-template-columns: 232px minmax(0, 1fr);
gap: 52px; align-items: start; padding: 34px 0 0;
}
@media (max-width: 900px) { .docs-layout { grid-template-columns: 1fr; gap: 24px; } }
/* ---- sidebar ---- */
.sidebar { position: sticky; top: 78px; max-height: calc(100vh - 100px); overflow-y: auto; }
@media (max-width: 900px) {
.sidebar { position: static; max-height: none; border-bottom: 1px solid var(--rule-soft); padding-bottom: 18px; }
}
.sidebar h4 {
font-family: "JetBrains Mono", monospace; font-size: 10.5px; letter-spacing: .12em;
text-transform: uppercase; color: var(--ink-soft); margin: 22px 0 8px; font-weight: 600;
}
.sidebar h4:first-child { margin-top: 0; }
.sidebar ul { list-style: none; margin: 0; padding: 0; }
.sidebar li a {
display: block; padding: 5px 10px; margin-left: -10px; border-radius: 6px;
color: var(--ink-mid); text-decoration: none; font-size: 14px; line-height: 1.45;
}
.sidebar li a:hover { color: var(--ink); background: var(--card-2); }
.sidebar li a.current { color: var(--accent); background: var(--accent-wash); font-weight: 600; }
/* ---- rendered markdown ---- */
.doc { min-width: 0; padding-bottom: 40px; }
.doc > h1:first-child { margin-top: 0; }
.doc h1 { font-size: clamp(30px, 4vw, 40px); font-weight: 600; margin: 0 0 20px; }
.doc h2 {
font-size: 25px; font-weight: 600; margin: 46px 0 12px;
padding-top: 18px; border-top: 1px solid var(--rule-soft);
}
.doc h3 { font-size: 19px; font-weight: 600; margin: 30px 0 8px; }
.doc h4 { font-size: 16px; font-weight: 700; margin: 22px 0 6px; font-family: "Public Sans", sans-serif; }
.doc p { color: var(--ink-mid); margin: 0 0 15px; }
.doc li { color: var(--ink-mid); margin: 4px 0; }
.doc ul, .doc ol { padding-left: 22px; margin: 0 0 15px; }
.doc li > p { margin: 0 0 6px; }
.doc strong { color: var(--ink); font-weight: 600; }
.doc a { text-decoration-color: color-mix(in srgb, var(--accent) 40%, transparent); }
.doc blockquote {
margin: 0 0 15px; padding: 2px 0 2px 18px; border-left: 3px solid var(--rule);
color: var(--ink-soft);
}
.doc hr { border: 0; border-top: 1px solid var(--rule-soft); margin: 34px 0; }
/* Room on the right for the copy button, so a long line scrolls under
nothing but empty padding. */
.doc pre { position: relative; margin: 0 0 18px; padding-right: 52px; }
/* A highlighted fence arrives as .highlight > pre with the tokens as bare
spans and no <code>, so the code-sized type has to be set on the pre. */
.doc .highlight { margin: 0 0 18px; }
.doc .highlight pre { margin: 0; font-size: 13.5px; line-height: 1.6; }
/* A fence with no language is a command or a program's output rather than
source; the renderer leaves those bare, while highlighted code arrives
wrapped in .highlight. Flatten them so the two read differently. */
.doc > pre, .doc li > pre { background: var(--card); box-shadow: none; }
.doc > pre code, .doc li > pre code { color: var(--ink-mid); }
.doc img { max-width: 100%; }
/* Tables come out of the renderer wrapped in this element, which has no
display of its own, so it has to be told to be a scroll container --
otherwise a wide table pushes the whole page sideways. */
.doc .table-scroll {
display: block; overflow-x: auto; margin: 0 0 18px;
border: 1px solid var(--rule); border-radius: 8px; background: var(--card);
}
.doc table { border-collapse: collapse; width: 100%; }
.doc th, .doc td {
text-align: left; padding: 10px 15px; border-bottom: 1px solid var(--rule-soft);
vertical-align: top; font-size: 14px; color: var(--ink-mid);
}
.doc thead th {
font-family: "JetBrains Mono", monospace; font-size: 10.5px; letter-spacing: .09em;
text-transform: uppercase; color: var(--ink-mid); font-weight: 600;
background: var(--card-2); border-bottom: 2px solid var(--rule); white-space: nowrap;
}
.doc tbody tr:last-child td { border-bottom: 0; }
.doc td code { background: none; border: 0; padding: 0; }
/* Anchor links on headings: present for linking, invisible until hovered. */
.doc .anchor {
float: left; margin-left: -22px; padding-right: 8px; text-decoration: none;
color: var(--ink-soft); opacity: 0; font-weight: 400;
}
.doc h2:hover .anchor, .doc h3:hover .anchor, .doc .anchor:focus { opacity: 1; }
/* ---- prev/next ---- */
.doc-nav { display: flex; justify-content: space-between; gap: 14px; margin-top: 46px; flex-wrap: wrap; }
.doc-nav a {
flex: 1 1 220px; border: 1px solid var(--rule); border-radius: 10px; padding: 13px 16px;
background: var(--card); text-decoration: none; color: var(--ink); font-weight: 600; font-size: 14.5px;
}
.doc-nav a:hover { border-color: color-mix(in srgb, var(--accent) 40%, var(--rule)); }
.doc-nav a span {
display: block; font-family: "JetBrains Mono", monospace; font-size: 10.5px;
letter-spacing: .1em; text-transform: uppercase; color: var(--ink-soft);
font-weight: 600; margin-bottom: 3px;
}
.doc-nav a.next { text-align: right; }
.doc-source { margin-top: 26px; font-size: 13px; color: var(--ink-soft); }
</style>
</head>
<body>
<nav class="top">
<div class="row">
<a class="wordmark" href="../../"><img src="../../logo.png" alt="" width="30" height="30"><span>Sxfe<span class="wm-accent">Script</span></span></a>
<ul>
<li><a class="navlink current" href="../../docs/">Docs</a></li>
<li><a class="navlink" href="../../#vs-typescript">vs TypeScript</a></li>
<li><a class="navlink" href="../../#arcsx">ArcSX runtime</a></li>
<li><a class="navlink" href="../../#contributing">Contributing</a></li>
</ul>
<a class="gh-link" href="https://github.com/SxfeScript/sxfescript">GitHub</a>
</div>
</nav>
<div class="wrap">
<div class="docs-layout">
<aside class="sidebar">
<h4>Get started</h4>
<ul>
<li><a href="../../docs/quickstart/">Quick start</a></li>
<li><a href="../../docs/examples/">Examples</a></li>
<li><a href="../../docs/cli/">CLI reference</a></li>
</ul>
<h4>The language</h4>
<ul>
<li><a href="../../docs/language/">Language contract</a></li>
<li><a href="../../docs/abi/">ABI</a></li>
<li><a href="../../docs/bytecode/">Bytecode</a></li>
</ul>
<h4>The runtime</h4>
<ul>
<li><a href="../../docs/runtime/">Runtime surface</a></li>
<li><a class="current" href="../../docs/node/">Node compatibility</a></li>
<li><a href="../../docs/native/">Native code</a></li>
<li><a href="../../docs/network/">Networking</a></li>
</ul>
<h4>Project</h4>
<ul>
<li><a href="../../docs/performance/">Performance</a></li>
<li><a href="../../docs/benchmark-references/">Benchmark references</a></li>
<li><a href="../../docs/implementation/">Implementation ledger</a></li>
<li><a href="../../docs/contributing/">Contributing</a></li>
<li><a href="../../docs/readme/">Project README</a></li>
</ul>
</aside>
<article class="doc">
<div class="markdown-heading"><h1 class="heading-element">The Node-compatibility layer</h1><a id="user-content-the-node-compatibility-layer" class="anchor" aria-label="Permalink: The Node-compatibility layer" href="#the-node-compatibility-layer"><span aria-hidden="true" class="octicon octicon-link"></span></a></div>
<p>This is what makes <code>sxn</code> usable as a Node alternative: CommonJS, the <code>node:</code>
builtins, and native-addon loading. It's the half of the runtime that a
mobile or embedded build can drop entirely without losing anything on the
<code>spec/RUNTIME.md</code> side — <code>spec/NATIVE.md</code> explains why that split exists for
the native-code case, and the same reasoning applies to this whole layer:
Node emulation is dead weight to an embedder with no Node surface of its own.</p>
<div class="markdown-heading"><h2 id="running-a-file-the-way-node-runs-it" class="heading-element"><a class="anchor" href="#running-a-file-the-way-node-runs-it" aria-hidden="true">#</a>Running a file the way Node runs it</h2><a id="user-content-running-a-file-the-way-node-runs-it" class="anchor" aria-label="Permalink: Running a file the way Node runs it" href="#running-a-file-the-way-node-runs-it"><span aria-hidden="true" class="octicon octicon-link"></span></a></div>
<p><code>sxn</code> decides module-or-CommonJS the way Node does: <code>.mjs</code>/<code>.mts</code> are always
modules, <code>.cjs</code> is always CommonJS, and a plain <code>.js</code> file or an extensionless
one (every CLI an npm package ships) follows the nearest <code>package.json</code>'s
<code>"type"</code>, defaulting to CommonJS. A <code>#!/usr/bin/env node</code> shebang line is
stripped before evaluation, the way Node strips it, so those extensionless
CLIs run directly: <code>sxn ./node_modules/.bin/whatever</code> works.</p>
<p>A CommonJS module gets <code>require</code>, <code>module</code>, <code>exports</code>, <code>__filename</code>, and
<code>__dirname</code>, wrapped exactly the way Node wraps it. <code>require.resolve(spec)</code>
exists on every <code>require</code>. <code>require("node:module").createRequire(path)</code>
returns a <code>require</code> anchored at that path's directory rather than the caller's
— get this wrong and it resolves the wrong package's siblings.</p>
<div class="markdown-heading"><h2 id="module-resolution" class="heading-element"><a class="anchor" href="#module-resolution" aria-hidden="true">#</a>Module resolution</h2><a id="user-content-module-resolution" class="anchor" aria-label="Permalink: Module resolution" href="#module-resolution"><span aria-hidden="true" class="octicon octicon-link"></span></a></div>
<p>Bare specifiers resolve through <code>node_modules</code>, walking up from the importing
file — plain packages, scoped packages (<code>@scope/name</code>), and subpath imports.
A package's <code>exports</code> field is read for the <code>.</code> subpath, checking conditions
in the order <code>import</code>, <code>module</code>, <code>default</code>, <code>require</code>, <code>node</code>, then falling
back to <code>module</code>, then <code>main</code>. Circular <code>require</code> sees the same partially
filled <code>exports</code> a cycle sees in Node, rather than recursing forever.</p>
<div class="markdown-heading"><h2 id="node-addons" class="heading-element"><a class="anchor" href="#node-addons" aria-hidden="true">#</a>
<code>.node</code> addons</h2><a id="user-content-node-addons" class="anchor" aria-label="Permalink: .node addons" href="#node-addons"><span aria-hidden="true" class="octicon octicon-link"></span></a></div>
<p><code>require("./thing.node")</code> and the <code>process.dlopen</code> it calls under the hood
both work, through a Node-API implementation built on QuickJS — full detail,
including what's implemented and what isn't, is <code>spec/NATIVE.md</code>. It's
listed here because it's the other reason this layer exists as a separate,
droppable piece: on iOS you can't <code>dlopen</code> code that arrived after the app
was signed, so this half of native-code support is inherently a
desktop-and-server capability, unlike <code>Sxn.ffi</code>.</p>
<div class="markdown-heading"><h2 id="node-builtins" class="heading-element"><a class="anchor" href="#node-builtins" aria-hidden="true">#</a>
<code>node:</code> builtins</h2><a id="user-content-node-builtins" class="anchor" aria-label="Permalink: node: builtins" href="#node-builtins"><span aria-hidden="true" class="octicon octicon-link"></span></a></div>
<p>24 of the ~37 Node ships. What each one covers, briefly, and where it's
worth knowing the gap:</p>
<div class="table-scroll"><table>
<thead>
<tr>
<th>Module</th>
<th>Covers</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<code>assert</code>, <code>assert/strict</code>
</td>
<td>The standard assertion functions.</td>
</tr>
<tr>
<td><code>buffer</code></td>
<td>See below — this one gets its own section.</td>
</tr>
<tr>
<td><code>crypto</code></td>
<td>
<code>Hash</code>, <code>Hmac</code> (standard construction over the digest primitive), <code>randomBytes</code>, <code>randomUUID</code>, <code>timingSafeEqual</code>.</td>
</tr>
<tr>
<td><code>events</code></td>
<td>
<code>EventEmitter</code>, including the mixin pattern (<code>Object.assign(fn, EventEmitter.prototype)</code>) Express uses, where <code>_events</code> is created lazily on first <code>on()</code>/<code>emit()</code> rather than in a constructor that never runs.</td>
</tr>
<tr>
<td>
<code>fs</code>, <code>fs/promises</code>
</td>
<td>File I/O, sync and promise-based.</td>
</tr>
<tr>
<td><code>http</code></td>
<td>
<code>createServer</code>, <code>IncomingMessage</code>, <code>ServerResponse</code>, <code>ClientRequest</code>, <code>STATUS_CODES</code>, <code>METHODS</code>. The request body defers behind <code>_read</code> rather than pushing eagerly, because a body-parser attaches its listener after the handler returns — push first and it gets nothing.</td>
</tr>
<tr>
<td><code>module</code></td>
<td>The <code>Module</code> constructor (what <code>require('module').prototype</code> expects), <code>createRequire</code>, <code>builtinModules</code>, <code>isBuiltin</code>.</td>
</tr>
<tr>
<td><code>net</code></td>
<td>
<code>isIP</code>/<code>isIPv4</code>/<code>isIPv6</code>, including IPv6 zone-index stripping (<code>fe80::1%eth0</code>). <code>Socket</code>/<code>Server</code> are not implemented and throw.</td>
</tr>
<tr>
<td>
<code>os</code>, <code>path</code>, <code>querystring</code>, <code>url</code>, <code>util</code>
</td>
<td>The usual surface — <code>inspect</code>, <code>format</code>, <code>promisify</code>, <code>deepEqual</code>, POSIX/Win32 path handling, and so on.</td>
</tr>
<tr>
<td><code>perf_hooks</code></td>
<td>Enough for timing code that reads <code>performance.now</code>-equivalent values.</td>
</tr>
<tr>
<td><code>process</code></td>
<td>
<code>platform</code>, <code>arch</code>, <code>version</code>/<code>versions</code>, <code>stdout</code>/<code>stderr</code>/<code>stdin</code>, <code>hrtime</code>, <code>emitWarning</code>, <code>uptime</code>, <code>pid</code>, <code>env</code>, <code>argv</code>, <code>dlopen</code>.</td>
</tr>
<tr>
<td>
<code>stream</code>, <code>stream/promises</code>
</td>
<td>
<code>Readable</code>/<code>Writable</code>/<code>Duplex</code>/<code>Transform</code>/<code>PassThrough</code>, <code>pipeline</code>, <code>finished</code>. The module export is the <code>Stream</code> function itself (some packages <code>require('stream')</code> and call it as a constructor), and <code>Readable</code> supports real <code>pipe</code>/<code>unpipe</code> — the latter matters because <code>finalhandler</code> calls it on every response, piped or not.</td>
</tr>
<tr>
<td>
<code>string_decoder</code>, <code>timers</code>, <code>timers/promises</code>, <code>tty</code>
</td>
<td>Small, focused shims.</td>
</tr>
<tr>
<td><code>zlib</code></td>
<td>
<code>gzipSync</code>/<code>gunzipSync</code>/<code>deflateSync</code>/<code>inflateSync</code> and the stream equivalents (<code>createGzip</code> etc.), over the zlib already linked in. No <code>promises</code> namespace — Node doesn't have one either.</td>
</tr>
</tbody>
</table></div>
<p>Not implemented: <code>child_process</code>, <code>cluster</code>, <code>dns</code>, <code>http2</code>, <code>https</code>,
<code>readline</code>, <code>stream/web</code>, <code>tls</code>, <code>v8</code>, <code>vm</code>, <code>worker_threads</code>, <code>inspector</code>,
<code>async_hooks</code>. <code>child_process</code> is the one that stops <code>next build</code> today —
see <code>spec/NATIVE.md</code>'s account of running Next.js's own compiler for the
full trace of what does and doesn't stand in the way.</p>
<div class="markdown-heading"><h2 id="buffer" class="heading-element"><a class="anchor" href="#buffer" aria-hidden="true">#</a>Buffer</h2><a id="user-content-buffer" class="anchor" aria-label="Permalink: Buffer" href="#buffer"><span aria-hidden="true" class="octicon octicon-link"></span></a></div>
<p><code>Buffer</code> extends <code>Uint8Array</code> and matches Node's encoding behavior, verified
against Node's own output rather than against itself — a divergence in either
runtime fails the fixture:</p>
<ul>
<li>
<code>utf8</code>/<code>utf-8</code>, <code>hex</code>, <code>base64</code>, <code>base64url</code>, <code>latin1</code>/<code>binary</code>, <code>ascii</code>,
<code>ucs2</code>/<code>ucs-2</code>/<code>utf16le</code>/<code>utf-16le</code> — every encoding name, case-insensitive,
in both directions.</li>
<li>
<code>hex</code> decoding stops at the first invalid pair rather than throwing, the
way Node's own reader does (unlike the standard <code>Uint8Array.fromHex</code>, which
throws).</li>
<li>
<code>base64</code> decoding skips characters it can't use, stops at <code>=</code>, accepts
either alphabet, needs no padding, and reads one byte per UTF-16 code unit
— which is why a multi-byte character truncates a base64 string early: its
high surrogate half masks down to <code>=</code>.</li>
<li>
<code>Buffer.byteLength</code>, <code>compare</code>, <code>equals</code>, <code>concat</code>, <code>toJSON</code> (Node's
<code>{type:"Buffer",data:[...]}</code> shape).</li>
</ul>
<div class="markdown-heading"><h2 id="encoding-name-and-buffer-performance" class="heading-element"><a class="anchor" href="#encoding-name-and-buffer-performance" aria-hidden="true">#</a>Encoding-name and Buffer performance</h2><a id="user-content-encoding-name-and-buffer-performance" class="anchor" aria-label="Permalink: Encoding-name and Buffer performance" href="#encoding-name-and-buffer-performance"><span aria-hidden="true" class="octicon octicon-link"></span></a></div>
<p>Two things specific to this layer are worth knowing if you're profiling
Buffer-heavy code: a literal encoding string (<code>"utf-8"</code>, <code>"hex"</code>) at a call
site is recognized by pointer identity against the atom table rather than by
hashing and comparing, and <code>Buffer.byteLength</code> computes the UTF-8 byte count
directly rather than encoding the string to measure it. Both are covered in
more depth, with numbers, in the README's benchmark section.</p>
<div class="doc-nav">
<a class="prev" href="../../docs/runtime/"><span>Previous</span>Runtime surface</a>
<a class="next" href="../../docs/native/"><span>Next</span>Native code</a>
</div>
<p class="doc-source">This page is generated from
<a href="https://github.com/SxfeScript/sxfescript/blob/main/spec/NODE.md"><code>spec/NODE.md</code></a>.
Machine-readable copies of the whole set:
<a href="../../llms.txt">llms.txt</a>,
<a href="../../llms-full.txt">llms-full.txt</a>.</p>
</article>
</div>
<footer>
<div class="row">
<p>SxfeScript / ArcSX</p>
<div class="links">
<a href="../../">Home</a>
<a href="../../docs/">Docs</a>
<a href="https://github.com/SxfeScript/sxfescript">Source</a>
<a href="https://github.com/SxfeScript/sxfescript/issues">Issues</a>
</div>
</div>
</footer>
</div>
<script>
(function () {
document.querySelectorAll(".doc pre").forEach(function (pre) {
var btn = document.createElement("button");
btn.type = "button";
btn.className = "copy-btn";
btn.setAttribute("aria-label", "Copy code");
btn.addEventListener("click", function () {
navigator.clipboard.writeText(pre.textContent).then(function () {
btn.classList.add("copied");
setTimeout(function () { btn.classList.remove("copied"); }, 1800);
});
});
pre.appendChild(btn);
});
})();
</script>
</body>
</html>