-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathpacker.ts
300 lines (283 loc) · 10.8 KB
/
packer.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import archiver, { Archiver } from "archiver";
import {
createWriteStream,
ensureDir,
mkdirp,
pathExists,
readFile,
stat
} from "fs-extra";
import MemoryFileSystem from "memory-fs";
import path from "path";
import { join } from "path";
import { PassThrough, Readable } from "stream";
import webpack from "webpack";
import { merge } from "webpack-merge";
import yauzl from "yauzl";
import { FaastError } from "./error";
import { LoaderOptions } from "./loader";
import { log } from "./log";
import { commonDefaults, CommonOptions, IncludeOption } from "./provider";
import { keysOf, streamToBuffer } from "./shared";
import { TrampolineFactory, WrapperOptionDefaults, WrapperOptions } from "./wrapper";
type ZipFile = yauzl.ZipFile;
export interface PackerResult {
archive: NodeJS.ReadableStream;
}
function getUrlEncodedQueryParameters(options: LoaderOptions) {
return keysOf(options)
.filter(key => options[key])
.map(key => `${key}=${encodeURIComponent(JSON.stringify(options[key]))}`)
.join(`&`);
}
export async function packer(
trampolineFactory: TrampolineFactory,
functionModule: string,
userOptions: CommonOptions,
userWrapperOptions: WrapperOptions,
FunctionName: string
): Promise<PackerResult> {
const options = { ...commonDefaults, ...userOptions };
const wrapperOptions = { ...WrapperOptionDefaults, ...userWrapperOptions };
const { webpackOptions, packageJson } = options;
log.info(`Running webpack`);
const mfs = new MemoryFileSystem();
function addToArchive(root: string, archive: Archiver) {
function addEntry(entry: string) {
const statEntry = mfs.statSync(entry);
if (statEntry.isDirectory()) {
for (const subEntry of mfs.readdirSync(entry)) {
const subEntryPath = path.join(entry, subEntry);
addEntry(subEntryPath);
}
} else if (statEntry.isFile()) {
log.info(`Adding file: ${entry}`);
archive.append((mfs as any).createReadStream(entry), {
name: path.relative(root, entry)
});
}
}
addEntry(root);
}
async function addPackageJson(packageJsonFile: string | object) {
const parsedPackageJson =
typeof packageJsonFile === "string"
? JSON.parse(
(await readFile(await resolvePath(packageJsonFile))).toString()
)
: { ...packageJsonFile };
parsedPackageJson.main = "index.js";
mfs.writeFileSync(
"/package.json",
JSON.stringify(parsedPackageJson, undefined, 2)
);
return Object.keys(parsedPackageJson.dependencies || {});
}
async function resolvePath(pathName: string) {
if (await pathExists(pathName)) {
return pathName;
}
throw new FaastError(`Could not find "${pathName}"`);
}
async function processIncludeExclude(
archive: Archiver,
include: (string | IncludeOption)[],
exclude: string[]
) {
for (const name of include) {
let cwd = ".";
let entry;
if (typeof name === "string") {
entry = name;
} else {
cwd = name.cwd || ".";
entry = name.path;
}
try {
const resolvedPath = path.resolve(cwd, entry);
const entryStat = await stat(resolvedPath);
if (entryStat.isDirectory()) {
entry = join(entry, "/**/*");
}
} catch {}
archive.glob(entry, { ignore: exclude as any, cwd });
}
}
async function prepareZipArchive(): Promise<PackerResult> {
const archive = archiver("zip", { zlib: { level: 8 } });
archive.on("error", err => log.warn(err));
archive.on("warning", err => log.warn(err));
addToArchive("/", archive);
const { include, exclude } = options;
await processIncludeExclude(archive, include, exclude);
archive.finalize();
return { archive };
}
const dependencies = (packageJson && (await addPackageJson(packageJson))) || [];
function runWebpack(entry: string, entryName: string) {
const coreConfig: webpack.Configuration = {
entry: { [entryName]: entry },
mode: "development",
output: {
path: "/",
filename: "[name].js",
libraryTarget: "commonjs2"
},
target: "node",
resolveLoader: { modules: [__dirname, `${__dirname}/dist`] },
node: { global: true, __dirname: false, __filename: false }
};
const dependencyExternals = {
externals: [...dependencies, ...dependencies.map(d => new RegExp(`^${d}/.*`))]
};
const config = merge(coreConfig, dependencyExternals, webpackOptions);
log.webpack(`webpack config: %O`, config);
const compiler = webpack(config);
compiler.outputFileSystem = mfs as any;
return new Promise<void>((resolve, reject) =>
compiler.run((err, stats) => {
if (err) {
reject(err);
} else {
if (stats?.hasErrors() || stats?.hasWarnings()) {
const c = stats.compilation;
const messages = [];
if (c.warnings.length > 0) {
messages.push(`${c.warnings.length} warning(s)`);
}
if (c.errors.length > 0) {
messages.push(`${c.errors.length} error(s)`);
}
log.warn(`webpack had ${messages.join(" and ")}`);
log.warn(
`set environment variable DEBUG=faast:webpack for details`
);
log.warn(
`see https://faastjs.org/docs/api/faastjs.commonoptions.packagejson`
);
}
if (log.webpack.enabled) {
log.webpack(stats?.toString());
log.webpack(`Memory filesystem: `);
for (const file of Object.keys(mfs.data)) {
log.webpack(` ${file}: ${mfs.data[file].length}`);
}
}
resolve();
}
})
);
}
const { childProcess, validateSerialization } = options;
const {
wrapperVerbose,
childProcess: _onlyUsedForLocalProviderDirectWrapperInstantiation,
childDir,
childProcessMemoryLimitMb,
childProcessTimeoutMs,
childProcessEnvironment: _onlyUsedForLocalProviderDirectWrapperInstantiation2,
wrapperLog: _onlyUsedForLocalProviderDirectWrapperInstantiation3,
validateSerialization: _ignoredInFavorOfCommonOptionsSetting,
...rest
} = wrapperOptions;
const _exhaustiveCheck2: Required<typeof rest> = {};
const isVerbose = wrapperVerbose || log.provider.enabled;
const loader = `loader?${getUrlEncodedQueryParameters({
trampolineFactoryModule: trampolineFactory.filename,
wrapperOptions: {
wrapperVerbose: isVerbose,
childProcess,
childDir,
childProcessMemoryLimitMb,
childProcessTimeoutMs,
validateSerialization
},
functionModule
})}!`;
try {
await runWebpack(loader, "index");
} catch (err: any) {
throw new FaastError(err, "failed running webpack");
}
try {
let { archive } = await prepareZipArchive();
const packageDir = process.env["FAAST_PACKAGE_DIR"];
if (packageDir) {
log.webpack(`FAAST_PACKAGE_DIR: ${packageDir}`);
const packageFile = join(packageDir, FunctionName) + ".zip";
await ensureDir(packageDir);
const writeStream = createWriteStream(packageFile);
const passThrough = archive.pipe(new PassThrough());
archive = archive.pipe(new PassThrough());
passThrough.pipe(writeStream);
writeStream.on("close", () => {
log.info(`Wrote ${packageFile}`);
});
}
return { archive };
} catch (err: any) {
throw new FaastError(err, "failed creating zip archive");
}
}
/**
* @param {NodeJS.ReadableStream | string} archive A zip archive as a stream or a filename
* @param {(filename: string, contents: Readable) => void} processEntry Every
* entry's contents must be consumed, otherwise the next entry won't be read.
*/
export async function processZip(
archive: NodeJS.ReadableStream | string,
processEntry: (filename: string, contents: Readable, mode: number) => void
) {
let zip: ZipFile;
if (typeof archive === "string") {
zip = await new Promise<ZipFile>((resolve, reject) =>
yauzl.open(archive, { lazyEntries: true }, (err, zipfile) =>
err ? reject(err) : resolve(zipfile!)
)
);
} else {
const buf = await streamToBuffer(archive);
zip = await new Promise<ZipFile>((resolve, reject) =>
yauzl.fromBuffer(buf, { lazyEntries: true }, (err, zipfile) =>
err ? reject(err) : resolve(zipfile!)
)
);
}
return new Promise<void>((resolve, reject) => {
zip.readEntry();
zip.on("entry", (entry: yauzl.Entry) => {
if (/\/$/.test(entry.fileName)) {
zip.readEntry();
} else {
zip.openReadStream(entry, (err, readStream) => {
if (err) {
reject(err);
return;
}
readStream!.on("end", () => zip.readEntry());
processEntry(
entry.fileName,
readStream!,
entry.externalFileAttributes >>> 16
);
});
}
});
zip.on("end", resolve);
});
}
export async function unzipInDir(dir: string, archive: NodeJS.ReadableStream) {
await mkdirp(dir);
let total = 0;
await processZip(archive, async (filename, contents, mode) => {
const destinationFilename = path.join(dir, filename);
const { dir: outputDir } = path.parse(destinationFilename);
if (!(await pathExists(outputDir))) {
await mkdirp(outputDir);
}
const stream = createWriteStream(destinationFilename, { mode });
contents.on("data", chunk => (total += chunk.length));
contents.pipe(stream);
});
return total;
}