Skip to content
File

Blob: npm/lib/node-platform.ts

typescript177 lines
1// Adapted from github.com/evanw/esbuild
2// Original copyright and license:
3// Copyright (c) 2020 Evan Wallace
4// MIT License
5//
6// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
7// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
9import fs from "fs";
10import os from "os";
11import path from "path";
12 
13declare const WORKERD_VERSION: string;
14 
15export const knownPackages: Record<string, string> = {
16 "darwin arm64 LE": "@cloudflare/workerd-darwin-arm64",
17 "darwin x64 LE": "@cloudflare/workerd-darwin-64",
18 "linux arm64 LE": "@cloudflare/workerd-linux-arm64",
19 "linux x64 LE": "@cloudflare/workerd-linux-64",
20 "win32 x64 LE": "@cloudflare/workerd-windows-64",
21};
22 
23const maybeExeExtension = process.platform === "win32" ? ".exe" : "";
24 
25export function pkgAndSubpathForCurrentPlatform(): {
26 pkg: string;
27 subpath: string;
28} {
29 let pkg: string;
30 let subpath: string;
31 let platformKey = `${process.platform} ${os.arch()} ${os.endianness()}`;
32 
33 if (platformKey in knownPackages) {
34 pkg = knownPackages[platformKey];
35 subpath = `bin/workerd${maybeExeExtension}`;
36 } else {
37 throw new Error(`Unsupported platform: ${platformKey}`);
38 }
39 
40 return { pkg, subpath };
41}
42 
43function pkgForSomeOtherPlatform(): string | null {
44 const libMain = require.resolve("workerd");
45 const nodeModulesDirectory = path.dirname(
46 path.dirname(path.dirname(libMain))
47 );
48 
49 if (path.basename(nodeModulesDirectory) === "node_modules") {
50 for (const unixKey in knownPackages) {
51 try {
52 const pkg = knownPackages[unixKey];
53 if (fs.existsSync(path.join(nodeModulesDirectory, pkg))) return pkg;
54 } catch {}
55 }
56 }
57 
58 return null;
59}
60 
61export function downloadedBinPath(pkg: string, subpath: string): string {
62 const libDir = path.dirname(require.resolve("workerd"));
63 return path.join(libDir, `downloaded-${pkg.replace("/", "-")}-${path.basename(subpath)}${maybeExeExtension}`);
64}
65 
66export function generateBinPath(): { binPath: string } {
67 const { pkg, subpath } = pkgAndSubpathForCurrentPlatform();
68 let binPath: string;
69 
70 try {
71 // First check for the binary package from our "optionalDependencies". This
72 // package should have been installed alongside this package at install time.
73 binPath = require.resolve(`${pkg}/${subpath}`);
74 } catch (e) {
75 // If that didn't work, then someone probably installed workerd with the
76 // "--no-optional" flag. Our install script attempts to compensate for this
77 // by manually downloading the package instead. Check for that next.
78 binPath = downloadedBinPath(pkg, subpath);
79 if (!fs.existsSync(binPath)) {
80 // If that didn't work too, check to see whether the package is even there
81 // at all. It may not be (for a few different reasons).
82 try {
83 require.resolve(pkg);
84 } catch {
85 // If we can't find the package for this platform, then it's possible
86 // that someone installed this for some other platform and is trying
87 // to use it without reinstalling. That won't work of course, but
88 // people do this all the time with systems like Docker. Try to be
89 // helpful in that case.
90 const otherPkg = pkgForSomeOtherPlatform();
91 if (otherPkg) {
92 throw new Error(`
93You installed workerd on another platform than the one you're currently using.
94This won't work because workerd is written with native code and needs to
95install a platform-specific binary executable.
96
97Specifically the "${otherPkg}" package is present but this platform
98needs the "${pkg}" package instead. People often get into this
99situation by installing workerd on macOS and copying "node_modules"
100into a Docker image that runs Linux.
101
102If you are installing with npm, you can try not copying the "node_modules"
103directory when you copy the files over, and running "npm ci" or "npm install"
104on the destination platform after the copy. Or you could consider using yarn
105instead which has built-in support for installing a package on multiple
106platforms simultaneously.
107
108If you are installing with yarn, you can try listing both this platform and the
109other platform in your ".yarnrc.yml" file using the "supportedArchitectures"
110feature: https://yarnpkg.com/configuration/yarnrc/#supportedArchitectures
111Keep in mind that this means multiple copies of workerd will be present.
112`);
113 }
114 
115 // If that didn't work too, then maybe someone installed workerd with
116 // both the "--no-optional" and the "--ignore-scripts" flags. The fix
117 // for this is to just not do that. We don't attempt to handle this
118 // case at all.
119 //
120 // In that case we try to have a nice error message if we think we know
121 // what's happening. Otherwise we just rethrow the original error message.
122 throw new Error(`The package "${pkg}" could not be found, and is needed by workerd.
123
124If you are installing workerd with npm, make sure that you don't specify the
125"--no-optional" flag. The "optionalDependencies" package.json feature is used
126by workerd to install the correct binary executable for your current platform.`);
127 }
128 throw e;
129 }
130 }
131 
132 // The workerd binary executable can't be used in Yarn 2 in PnP mode because
133 // it's inside a virtual file system and the OS needs it in the real file
134 // system. So we need to copy the file out of the virtual file system into
135 // the real file system.
136 //
137 // You might think that we could use "preferUnplugged: true" in each of the
138 // platform-specific packages for this instead, since that tells Yarn to not
139 // use the virtual file system for those packages. This is not done because:
140 //
141 // * Really early versions of Yarn don't support "preferUnplugged", so package
142 // installation would break on those Yarn versions if we did this.
143 //
144 // * Earlier Yarn versions always installed all optional dependencies for all
145 // platforms even though most of them are incompatible. To minimize file
146 // system space, we want these useless packages to take up as little space
147 // as possible so they should remain unzipped inside their ".zip" files.
148 //
149 // We have to explicitly pass "preferUnplugged: false" instead of leaving
150 // it up to Yarn's default behavior because Yarn's heuristics otherwise
151 // automatically unzip packages containing ".exe" files, and we don't want
152 // our Windows-specific packages to be unzipped either.
153 //
154 let pnpapi: any;
155 try {
156 pnpapi = require("pnpapi");
157 } catch (e) {}
158 if (pnpapi) {
159 const root = pnpapi.getPackageInformation(pnpapi.topLevel).packageLocation;
160 const binTargetPath = path.join(
161 root,
162 "node_modules",
163 ".cache",
164 "workerd",
165 `pnpapi-${pkg.replace("/", "-")}-${WORKERD_VERSION}-${path.basename(subpath)}`
166 );
167 if (!fs.existsSync(binTargetPath)) {
168 fs.mkdirSync(path.dirname(binTargetPath), { recursive: true });
169 fs.copyFileSync(binPath, binTargetPath);
170 fs.chmodSync(binTargetPath, 0o755);
171 }
172 return { binPath: binTargetPath };
173 }
174 
175 return { binPath };
176}