Skip to content

Commit 754d031

Browse files
committed
node typescript, too
1 parent 53e7d4f commit 754d031

File tree

4 files changed

+15
-16
lines changed

4 files changed

+15
-16
lines changed
File renamed without changes.

docs/data-loaders.html

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -48,21 +48,13 @@
4848
As a more realistic example, below is a Node.js data loader cell that fetches download statistics for Observable Plot from npm.
4949
</script>
5050
<script id="4" type="application/vnd.node.javascript" pinned="" output="downloads" format="json">
51-
async function getNpmDownloads(
52-
name, // name of package
53-
{
54-
end: max, // exclusive
55-
start: min // inclusive
56-
}
57-
) {
51+
async function getNpmDownloads(name, {end, start}) {
5852
const data = [];
59-
for (let start = max, end; start > min; ) {
60-
end = start;
61-
start = addDate(start, -365); // fetch a year at a time
62-
if (start < min) start = min;
63-
const response = await fetch(
64-
`https://api.npmjs.org/downloads/range/${formatDate(start)}:${formatDate(addDate(end, -1))}${name ? `/${encodeURIComponent(name)}` : ``}`
65-
);
53+
for (let d1 = end, d2; d1 > start; ) {
54+
d2 = d1;
55+
d1 = addDate(d1, -365); // fetch a year at a time
56+
if (d1 < start) d1 = start;
57+
const response = await fetch(`https://api.npmjs.org/downloads/range/${formatDate(d1)}:${formatDate(addDate(d2, -1))}/${encodeURIComponent(name)}`);
6658
if (!response.ok) throw new Error(`fetch failed: ${response.status}`);
6759
const {downloads} = await response.json();
6860
for (const {downloads: value, day: date} of downloads.reverse()) {
@@ -129,6 +121,9 @@
129121
- `/usr/local/bin/node` (official Node.js installer)
130122
- `/usr/bin/node` (operating system)
131123
</script>
124+
<script id="31" type="text/markdown">
125+
TypeScript is supported via Node.js' built-in [type stripping](https://nodejs.org/api/typescript.html#type-stripping). Hence, no type checking is performed, and only erasable TypeScript syntax is supported (for example, `enum` declarations are not allowed).
126+
</script>
132127
<script id="8" type="text/markdown">
133128
To improve security, the Node.js interpreter uses [process-based permissions](https://nodejs.org/api/permissions.html): Node.js cells are only allowed to read files in the same directory as the notebook, with no other permissions. (We may offer a way to relax permissions in the future, but want to encourage safety; let us know if you run into issues.)
134129
</script>

src/interpreters/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export async function getInterpreterCachePath(
1717
export function getInterpreterCommand(interpreter: string): [command: string, args: string[]] {
1818
switch (interpreter) {
1919
case "node":
20-
return ["node", ["--input-type=module", "--permission", "--allow-fs-read=."]];
20+
return ["node", ["--input-type=module-typescript", "--permission", "--allow-fs-read=."]];
2121
case "python":
2222
return ["python3", []];
2323
default:

src/runtime/stdlib/highlight.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ export async function highlight(code: HTMLElement): Promise<void> {
5353
}
5454

5555
async function getParser(language: string): Promise<Parser | undefined> {
56+
switch (language) {
57+
case "node":
58+
language = "ts";
59+
break;
60+
}
5661
switch (language) {
5762
case "js":
5863
case "ts":
@@ -75,7 +80,6 @@ function getLanguage(code: HTMLElement): string | undefined {
7580
?.slice("language-".length)
7681
?.toLowerCase();
7782
switch (language) {
78-
case "node":
7983
case "javascript":
8084
return "js";
8185
case "typescript":

0 commit comments

Comments
 (0)