This repository was archived by the owner on May 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathbundler.ts
185 lines (162 loc) · 5.59 KB
/
bundler.ts
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
import { field, logger } from "@coder/logger";
import * as nbin from "@coder/nbin";
import * as fs from "fs";
import * as fse from "fs-extra";
import * as glob from "glob";
import fetch from "node-fetch";
import * as os from "os";
import * as path from "path";
import { writeString } from "../common/buffer";
import { WritableFilesystem } from "../common/filesystem";
import { createFooter } from "../common/footer";
import ora, { Ora } from "ora";
export class Binary implements nbin.Binary {
private readonly fs: WritableFilesystem = new WritableFilesystem();
public constructor(
private readonly options: nbin.BinaryOptions,
) {}
public writeFile(pathName: string, content: Buffer): void {
const parts = path.normalize(pathName).split(path.sep).filter((i) => i.length);
let writableFs: WritableFilesystem = this.fs;
for (let i = 0; i < parts.length; i++) {
const part = parts[i];
if (i === parts.length - 1) {
writableFs.write(part, content);
} else {
writableFs = writableFs.cd(part);
}
}
}
public writeFiles(globName: string, callback?: (fileWritten: string) => void): number {
const files = glob.sync(globName, {
cwd: process.cwd(),
});
let fileCount: number = 0;
let spinner: Ora | undefined;
if (this.canLog) {
spinner = ora("Writing...");
}
for (let i = 0; i < files.length; i++) {
const file = files[i];
const stat = fs.statSync(file);
if (!stat.isFile()) {
continue;
}
this.writeFile(file, fs.readFileSync(file));
if (spinner) {
spinner.text = `Wrote "${file}"!`;
}
if (callback) {
callback(file);
}
fileCount++;
}
if (spinner) {
spinner.succeed(`Wrote ${fileCount} ${fileCount === 1 ? "file" : "files"}!`);
}
return fileCount;
}
public writeModule(modulePath: string): void {
if (!fs.existsSync(modulePath)) {
throw new Error(`"${modulePath}" does not exist`);
}
const paths = glob.sync(path.join(modulePath, "**"))
const moduleName = path.basename(modulePath);
for (let i = 0; i < paths.length; i++) {
const p = paths[i];
const newPath = path.join("/node_modules", moduleName, path.relative(modulePath, p));
const stat = fs.statSync(p);
if (!stat.isFile()) {
continue;
}
this.writeFile(newPath, fs.readFileSync(p));
}
if (this.canLog) {
logger.info("Packaged module", field("module", moduleName));
}
}
public async build(): Promise<Buffer> {
const nodeBuffer = await this.cacheBinary();
// Create a buffer containing a (most likely) unique ID and its length.
const idLength = 6;
const possible = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
const id = Array(idLength).fill(1)
.map(() => possible[Math.floor(Math.random() * possible.length)])
.join("");
const idBuffer = Buffer.alloc(2 + Buffer.byteLength(id));
writeString(idBuffer, id);
// Writing the entrypoint
const mainFileBuffer = Buffer.alloc(2 + Buffer.byteLength(this.options.mainFile));
writeString(mainFileBuffer, this.options.mainFile);
if (this.canLog) {
logger.info("Building filesystem");
}
// Filesystem contents
const fsBuffer = this.fs.build();
// Footer
const footerBuffer = createFooter(
fsBuffer.header.byteLength + idBuffer.byteLength + mainFileBuffer.byteLength, // Header byte length
nodeBuffer.byteLength, // Header byte offset
fsBuffer.fileContents.byteLength, // File contents length
nodeBuffer.byteLength + fsBuffer.header.byteLength + idBuffer.byteLength + mainFileBuffer.byteLength, // File contents offset
);
return Buffer.concat([nodeBuffer, idBuffer, mainFileBuffer, fsBuffer.header, fsBuffer.fileContents, footerBuffer]);
}
private async cacheBinary(): Promise<Buffer> {
let nodeBinaryPath = this.options.nodePath || path.join(__dirname, "../../lib/node/out/Release/node");
const nodeBinaryName = this.nodeBinaryName;
const cacheDir = path.join(os.homedir(), ".nbin");
if (!fs.existsSync(nodeBinaryPath)) {
if (!fs.existsSync(cacheDir)) {
if (this.canLog) {
logger.info("Creating node binary cache directory");
}
fse.mkdirpSync(cacheDir);
}
nodeBinaryPath = path.join(cacheDir, nodeBinaryName);
}
if (fs.existsSync(nodeBinaryPath)) {
if (this.canLog) {
logger.info("Returning cached binary", field("binary-name", nodeBinaryName));
}
return fs.readFileSync(nodeBinaryPath);
} else {
// The pulled binary we need doesn't exist
const binary = await this.fetchNodeBinary();
fse.mkdirpSync(path.dirname(path.join(cacheDir, nodeBinaryName)));
fse.writeFileSync(path.join(cacheDir, nodeBinaryName), binary);
if (this.canLog) {
logger.info("Wrote and cached binary", field("binary-name", nodeBinaryName), field("path", path.join(cacheDir, nodeBinaryName)));
}
return binary;
}
}
private async fetchNodeBinary(): Promise<Buffer> {
const binName = this.nodeBinaryName;
const url = `https://nbin.cdr.sh/${binName}`;
if (this.canLog) {
logger.info("Fetching", field("url", url));
}
const resp = await fetch(url);
if (resp.status !== 200) {
throw new Error(resp.statusText);
}
const buffer = await resp.arrayBuffer();
return Buffer.from(buffer);
}
private get nodeBinaryName(): string {
const currentPlatform = os.platform();
let currentArchitecture = os.arch();
if (currentArchitecture === "x64") {
currentArchitecture = "x86_64";
}
const nodeVersion = "10.15.1";
const packageJson = require("../../package.json");
const packageVersion = packageJson.version;
const binName = `${packageVersion}/node-${nodeVersion}-${currentPlatform}-${currentArchitecture}`;
return binName;
}
private get canLog(): boolean {
return !this.options.suppressOutput;
}
}