|
48 | 48 | As a more realistic example, below is a Node.js data loader cell that fetches download statistics for Observable Plot from npm. |
49 | 49 | </script> |
50 | 50 | <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}) { |
58 | 52 | 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)}`); |
66 | 58 | if (!response.ok) throw new Error(`fetch failed: ${response.status}`); |
67 | 59 | const {downloads} = await response.json(); |
68 | 60 | for (const {downloads: value, day: date} of downloads.reverse()) { |
|
129 | 121 | - `/usr/local/bin/node` (official Node.js installer) |
130 | 122 | - `/usr/bin/node` (operating system) |
131 | 123 | </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> |
132 | 127 | <script id="8" type="text/markdown"> |
133 | 128 | 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.) |
134 | 129 | </script> |
|
0 commit comments