|
| 1 | +/* @internal */ |
| 2 | +namespace ts.deno { |
| 3 | + export type IsNodeSourceFileCallback = (sourceFile: SourceFile) => boolean; |
| 4 | + |
| 5 | + let isNodeSourceFile: IsNodeSourceFileCallback = () => false; |
| 6 | + |
| 7 | + export function setIsNodeSourceFileCallback(callback: IsNodeSourceFileCallback) { |
| 8 | + isNodeSourceFile = callback; |
| 9 | + } |
| 10 | + |
| 11 | + // When upgrading: |
| 12 | + // 1. Inspect all usages of "globals" and "globalThisSymbol" in checker.ts |
| 13 | + // - Beware that `globalThisType` might refer to the global `this` type |
| 14 | + // and not the global `globalThis` type |
| 15 | + // 2. Inspect the types in @types/node for anything that might need to go below |
| 16 | + // as well. |
| 17 | + |
| 18 | + const nodeOnlyGlobalNames = new Set([ |
| 19 | + "NodeRequire", |
| 20 | + "RequireResolve", |
| 21 | + "RequireResolve", |
| 22 | + "process", |
| 23 | + "console", |
| 24 | + "__filename", |
| 25 | + "__dirname", |
| 26 | + "require", |
| 27 | + "module", |
| 28 | + "exports", |
| 29 | + "gc", |
| 30 | + "BufferEncoding", |
| 31 | + "BufferConstructor", |
| 32 | + "WithImplicitCoercion", |
| 33 | + "Buffer", |
| 34 | + "Console", |
| 35 | + "ImportMeta", |
| 36 | + "setTimeout", |
| 37 | + "setInterval", |
| 38 | + "setImmediate", |
| 39 | + "Global", |
| 40 | + "AbortController", |
| 41 | + "AbortSignal", |
| 42 | + "Blob", |
| 43 | + "BroadcastChannel", |
| 44 | + "MessageChannel", |
| 45 | + "MessagePort", |
| 46 | + "Event", |
| 47 | + "EventTarget", |
| 48 | + "performance", |
| 49 | + "TextDecoder", |
| 50 | + "TextEncoder", |
| 51 | + "URL", |
| 52 | + "URLSearchParams", |
| 53 | + ]) as Set<ts.__String>; |
| 54 | + |
| 55 | + export function createDenoForkContext({ |
| 56 | + mergeSymbol, |
| 57 | + globals, |
| 58 | + nodeGlobals, |
| 59 | + ambientModuleSymbolRegex, |
| 60 | + }: { |
| 61 | + mergeSymbol(target: Symbol, source: Symbol, unidirectional?: boolean): Symbol; |
| 62 | + globals: SymbolTable; |
| 63 | + nodeGlobals: SymbolTable; |
| 64 | + ambientModuleSymbolRegex: RegExp, |
| 65 | + }) { |
| 66 | + return { |
| 67 | + hasNodeSourceFile, |
| 68 | + getGlobalsForName, |
| 69 | + mergeGlobalSymbolTable, |
| 70 | + combinedGlobals: createNodeGlobalsSymbolTable(), |
| 71 | + }; |
| 72 | + |
| 73 | + function hasNodeSourceFile(node: Node | undefined) { |
| 74 | + if (!node) return false; |
| 75 | + const sourceFile = getSourceFileOfNode(node); |
| 76 | + return isNodeSourceFile(sourceFile); |
| 77 | + } |
| 78 | + |
| 79 | + function getGlobalsForName(id: ts.__String) { |
| 80 | + // Node ambient modules are only accessible in the node code, |
| 81 | + // so put them on the node globals |
| 82 | + if (ambientModuleSymbolRegex.test(id as string)) |
| 83 | + return nodeGlobals; |
| 84 | + return nodeOnlyGlobalNames.has(id) ? nodeGlobals : globals; |
| 85 | + } |
| 86 | + |
| 87 | + function mergeGlobalSymbolTable(node: Node, source: SymbolTable, unidirectional = false) { |
| 88 | + const sourceFile = getSourceFileOfNode(node); |
| 89 | + const isNodeFile = hasNodeSourceFile(sourceFile); |
| 90 | + source.forEach((sourceSymbol, id) => { |
| 91 | + const target = isNodeFile ? getGlobalsForName(id) : globals; |
| 92 | + const targetSymbol = target.get(id); |
| 93 | + target.set(id, targetSymbol ? mergeSymbol(targetSymbol, sourceSymbol, unidirectional) : sourceSymbol); |
| 94 | + }); |
| 95 | + } |
| 96 | + |
| 97 | + function createNodeGlobalsSymbolTable() { |
| 98 | + return new Proxy(globals, { |
| 99 | + get(target, prop: string | symbol, receiver) { |
| 100 | + if (prop === "get") { |
| 101 | + return (key: ts.__String) => { |
| 102 | + return nodeGlobals.get(key) ?? globals.get(key); |
| 103 | + }; |
| 104 | + } else if (prop === "has") { |
| 105 | + return (key: ts.__String) => { |
| 106 | + return nodeGlobals.has(key) || globals.has(key); |
| 107 | + }; |
| 108 | + } else if (prop === "size") { |
| 109 | + let i = 0; |
| 110 | + forEachEntry(() => { |
| 111 | + i++; |
| 112 | + }); |
| 113 | + return i; |
| 114 | + } else if (prop === "forEach") { |
| 115 | + return (action: (value: Symbol, key: ts.__String) => void) => { |
| 116 | + forEachEntry(([key, value]) => { |
| 117 | + action(value, key); |
| 118 | + }); |
| 119 | + }; |
| 120 | + } else if (prop === "entries") { |
| 121 | + return () => { |
| 122 | + return getEntries(kv => kv); |
| 123 | + }; |
| 124 | + } else if (prop === "keys") { |
| 125 | + return () => { |
| 126 | + return getEntries(kv => kv[0]); |
| 127 | + }; |
| 128 | + } else if (prop === "values") { |
| 129 | + return () => { |
| 130 | + return getEntries(kv => kv[1]); |
| 131 | + }; |
| 132 | + } else if (prop === Symbol.iterator) { |
| 133 | + return () => { |
| 134 | + // Need to convert this to an array since typescript targets ES5 |
| 135 | + // and providing back the iterator won't work here. I don't want |
| 136 | + // to change the target to ES6 because I'm not sure if that would |
| 137 | + // surface any issues. |
| 138 | + return arrayFrom(getEntries(kv => kv))[Symbol.iterator](); |
| 139 | + }; |
| 140 | + } else { |
| 141 | + const value = (target as any)[prop]; |
| 142 | + if (value instanceof Function) { |
| 143 | + return function (this: any, ...args: any[]) { |
| 144 | + return value.apply(this === receiver ? target : this, args); |
| 145 | + }; |
| 146 | + } |
| 147 | + return value; |
| 148 | + } |
| 149 | + }, |
| 150 | + }); |
| 151 | + |
| 152 | + function forEachEntry(action: (value: [__String, Symbol]) => void) { |
| 153 | + const iterator = getEntries((entry) => { |
| 154 | + action(entry); |
| 155 | + }); |
| 156 | + // drain the iterator to do the action |
| 157 | + while (!iterator.next().done) {} |
| 158 | + } |
| 159 | + |
| 160 | + function* getEntries<R>( |
| 161 | + transform: (value: [__String, Symbol]) => R |
| 162 | + ) { |
| 163 | + const foundKeys = new Set<ts.__String>(); |
| 164 | + for (const entries of [nodeGlobals.entries(), globals.entries()]) { |
| 165 | + let next = entries.next(); |
| 166 | + while (!next.done) { |
| 167 | + if (!foundKeys.has(next.value[0])) { |
| 168 | + yield transform(next.value); |
| 169 | + foundKeys.add(next.value[0]); |
| 170 | + } |
| 171 | + next = entries.next(); |
| 172 | + } |
| 173 | + } |
| 174 | + } |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + export interface NpmPackageReference { |
| 179 | + name: string; |
| 180 | + versionReq: string; |
| 181 | + subPath: string | undefined; |
| 182 | + } |
| 183 | + |
| 184 | + export function tryParseNpmPackageReference(text: string) { |
| 185 | + try { |
| 186 | + return parseNpmPackageReference(text); |
| 187 | + } catch { |
| 188 | + return undefined; |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + export function parseNpmPackageReference(text: string) { |
| 193 | + if (!text.startsWith("npm:")) { |
| 194 | + throw new Error(`Not an npm specifier: ${text}`); |
| 195 | + } |
| 196 | + text = text.replace(/^npm:\/?/, ""); |
| 197 | + const parts = text.split("/"); |
| 198 | + const namePartLen = text.startsWith("@") ? 2 : 1; |
| 199 | + if (parts.length < namePartLen) { |
| 200 | + throw new Error(`Not a valid package: ${text}`); |
| 201 | + } |
| 202 | + const nameParts = parts.slice(0, namePartLen); |
| 203 | + const lastNamePart = nameParts.at(-1)!; |
| 204 | + const lastAtIndex = lastNamePart.lastIndexOf("@"); |
| 205 | + let versionReq: string | undefined = undefined; |
| 206 | + if (lastAtIndex > 0) { |
| 207 | + versionReq = lastNamePart.substring(lastAtIndex + 1); |
| 208 | + nameParts[nameParts.length - 1] = lastNamePart.substring(0, lastAtIndex); |
| 209 | + } |
| 210 | + const name = nameParts.join("/"); |
| 211 | + if (name.length === 0) { |
| 212 | + throw new Error(`Npm specifier did not have a name: ${text}`); |
| 213 | + } |
| 214 | + return { |
| 215 | + name, |
| 216 | + versionReq, |
| 217 | + subPath: parts.length > nameParts.length ? parts.slice(nameParts.length).join("/") : undefined, |
| 218 | + }; |
| 219 | + } |
| 220 | +} |
0 commit comments