Skip to content

Commit

Permalink
Squashed revert "allow to compress files in virtual file system (verc…
Browse files Browse the repository at this point in the history
…el#1115)"

This reverts commit 82ce625.
This reverts commit ea23196.
This reverts commit ea7d72b.

Bug: vercel#1191, vercel#1192
  • Loading branch information
jesec committed May 31, 2021
1 parent e8a2c63 commit 52ddf23
Show file tree
Hide file tree
Showing 33 changed files with 437 additions and 1,258 deletions.
7 changes: 2 additions & 5 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
// note you must disable the base rule as it can report incorrect errors
"extends": ["airbnb-base", "prettier"],
"rules": {
"no-bitwise": "off",
Expand All @@ -11,10 +10,8 @@
"consistent-return": "off",
"no-restricted-syntax": "off",
"import/prefer-default-export": "off",
"camelcase": "off",
"no-shadow": "off",
"@typescript-eslint/no-shadow": ["error"]
},
"camelcase": "off"
},
"overrides": [
{
"files": ["*.ts"],
Expand Down
12 changes: 1 addition & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,16 +209,6 @@ requirements to compile original Node.js:

See [pkg-fetch](https://github.com/vercel/pkg-fetch) for more info.

### Compression

Pass `--compress Brotli` or `--compress GZip` to `pkg` to compress further the content of the files store in the exectable.

This option can reduce the size of the embedded file system by up to 60%.

The startup time of the application might be reduced slightly.

`-C` can be used as a shortcut for `--compress `.

### Environment

| Var | Description |
Expand Down Expand Up @@ -368,7 +358,7 @@ and check that all the required files for your application are properly
incorporated to the final executable.

$ pkg --debug app.js -o output
$ DEBUG_PKG=1 output
$ DEBUG_PKG output

or

Expand Down
6 changes: 0 additions & 6 deletions lib/compress_type.ts

This file was deleted.

7 changes: 0 additions & 7 deletions lib/help.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ export default function help() {
--public speed up and disclose the sources of top-level project
--public-packages force specified packages to be considered public
--no-bytecode skip bytecode generation and include source files as plain js
-C, --compress [default=None] compression algorithm = Brotli or GZip
${chalk.dim('Examples:')}
Expand All @@ -37,11 +36,5 @@ export default function help() {
${chalk.cyan('$ pkg --public-packages "packageA,packageB" index.js')}
${chalk.gray('–')} Consider all packages to be public
${chalk.cyan('$ pkg --public-packages "*" index.js')}
${chalk.gray('–')} Bakes '--expose-gc' into executable
${chalk.cyan('$ pkg --options expose-gc index.js')}
${chalk.gray(
'–'
)} reduce size of the data packed inside the executable with GZip
${chalk.cyan('$ pkg --compress GZip index.js')}
`);
}
33 changes: 2 additions & 31 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import refine from './refiner';
import { shutdown } from './fabricator';
import walk, { Marker, WalkerParams } from './walker';
import { Target, NodeTarget, SymLinks } from './types';
import { CompressType } from './compress_type';
import { patchMachOExecutable } from './mach-o';

const { version } = JSON.parse(
Expand Down Expand Up @@ -248,8 +247,6 @@ export async function exec(argv2: string[]) {
't',
'target',
'targets',
'C',
'compress',
],
default: { bytecode: true },
});
Expand Down Expand Up @@ -277,31 +274,7 @@ export async function exec(argv2: string[]) {

const forceBuild = argv.b || argv.build;

// doCompress
const algo = argv.C || argv.compress || 'None';

let doCompress: CompressType = CompressType.None;
switch (algo.toLowerCase()) {
case 'brotli':
case 'br':
doCompress = CompressType.Brotli;
break;
case 'gzip':
case 'gz':
doCompress = CompressType.GZip;
break;
case 'none':
break;
default:
// eslint-disable-next-line no-console
throw wasReported(
`Invalid compression algorithm ${algo} ( should be None, Brotli or Gzip)`
);
}
if (doCompress !== CompressType.None) {
// eslint-disable-next-line no-console
console.log('compression: ', CompressType[doCompress]);
}
// _

if (!argv._.length) {
throw wasReported('Entry file/directory is expected', [
Expand Down Expand Up @@ -652,14 +625,12 @@ export async function exec(argv2: string[]) {
await mkdirp(path.dirname(target.output));
}

const slash = target.platform === 'win' ? '\\' : '/';
await producer({
backpack,
bakes,
slash,
slash: target.platform === 'win' ? '\\' : '/',
target: target as Target,
symLinks,
doCompress,
});

if (target.platform !== 'win' && target.output) {
Expand Down
10 changes: 3 additions & 7 deletions lib/packer.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* eslint-disable complexity */

import assert from 'assert';
import * as fs from 'fs-extra';
import * as path from 'path';
import fs from 'fs-extra';
import path from 'path';

import {
STORE_BLOB,
Expand Down Expand Up @@ -157,7 +157,7 @@ export default function packer({
}
}
const prelude =
`return (function (REQUIRE_COMMON, VIRTUAL_FILESYSTEM, DEFAULT_ENTRYPOINT, SYMLINKS, DICT, DOCOMPRESS) {
`return (function (REQUIRE_COMMON, VIRTUAL_FILESYSTEM, DEFAULT_ENTRYPOINT, SYMLINKS) {
${bootstrapText}${
log.debugMode ? diagnosticText : ''
}\n})(function (exports) {\n${commonText}\n},\n` +
Expand All @@ -166,10 +166,6 @@ export default function packer({
`%DEFAULT_ENTRYPOINT%` +
`\n,\n` +
`%SYMLINKS%` +
'\n,\n' +
'%DICT%' +
'\n,\n' +
'%DOCOMPRESS%' +
`\n);`;

return { prelude, entrypoint, stripes };
Expand Down
87 changes: 24 additions & 63 deletions lib/producer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { createBrotliCompress, createGzip } from 'zlib';
import Multistream from 'multistream';
import assert from 'assert';
import { execSync } from 'child_process';
Expand All @@ -13,7 +12,6 @@ import { log, wasReported } from './log';
import { fabricateTwice } from './fabricator';
import { platform, SymLinks, Target } from './types';
import { Stripe } from './packer';
import { CompressType } from './compress_type';

interface NotFound {
notFound: true;
Expand Down Expand Up @@ -227,7 +225,8 @@ function nativePrebuildInstall(target: Target, nodeFile: string) {
}

execSync(
`${prebuild} -t ${nodeVersion} --platform ${platform[target.platform]
`${prebuild} -t ${nodeVersion} --platform ${
platform[target.platform]
} --arch ${target.arch}`,
{ cwd: dir }
);
Expand All @@ -243,34 +242,14 @@ interface ProducerOptions {
slash: string;
target: Target;
symLinks: SymLinks;
doCompress: CompressType;
}

const fileDictionary: { [key: string]: string } = {};
let counter = 0;
function replace(k: string) {
let existingKey = fileDictionary[k];
if (!existingKey) {
const newkey = counter;
counter += 1;
existingKey = newkey.toString(36);
fileDictionary[k] = existingKey;
}
return existingKey;
}
const separator = '$';

function makeKey(filename: string, slash: string): string {
const a = filename.split(slash).map(replace).join(separator);
return a;
}
export default function producer({
backpack,
bakes,
slash,
target,
symLinks,
doCompress
}: ProducerOptions) {
return new Promise<void>((resolve, reject) => {
if (!Buffer.alloc) {
Expand All @@ -289,17 +268,18 @@ export default function producer({
for (const stripe of stripes) {
let { snap } = stripe;
snap = snapshotify(snap, slash);
const vfsKey = makeKey(snap, slash);
if (!vfs[vfsKey]) vfs[vfsKey] = {};

if (!vfs[snap]) {
vfs[snap] = {};
}
}

const snapshotSymLinks: SymLinks = {};

for (const [key, value] of Object.entries(symLinks)) {
const k = snapshotify(key, slash);
const v = snapshotify(value, slash);
const vfsKey = makeKey(k, slash);
snapshotSymLinks[vfsKey] = makeKey(v, slash);
snapshotSymLinks[k] = v;
}

let meter: streamMeter.StreamMeter;
Expand All @@ -309,15 +289,6 @@ export default function producer({
meter = streamMeter();
return s.pipe(meter);
}
function pipeMayCompressToNewMeter(s: Readable): streamMeter.StreamMeter {
if (doCompress === CompressType.GZip) {
return pipeToNewMeter(s.pipe(createGzip()));
}
if (doCompress === CompressType.Brotli) {
return pipeToNewMeter(s.pipe(createBrotliCompress()));
}
return pipeToNewMeter(s);
}

function next(s: Readable) {
count += 1;
Expand Down Expand Up @@ -350,8 +321,7 @@ export default function producer({
const { store } = prevStripe;
let { snap } = prevStripe;
snap = snapshotify(snap, slash);
const vfsKey = makeKey(snap, slash);
vfs[vfsKey][store] = [track, meter.bytes];
vfs[snap][store] = [track, meter.bytes];
track += meter.bytes;
}

Expand All @@ -377,14 +347,15 @@ export default function producer({
return cb(null, intoStream(Buffer.alloc(0)));
}

cb(null, pipeMayCompressToNewMeter(intoStream(buffer || Buffer.from(''))));
cb(
null,
pipeToNewMeter(intoStream(buffer || Buffer.from('')))
);
}
);
}
return cb(
null,
pipeMayCompressToNewMeter(intoStream(stripe.buffer))
);

return cb(null, pipeToNewMeter(intoStream(stripe.buffer)));
}

if (stripe.file) {
Expand All @@ -407,17 +378,15 @@ export default function producer({
if (fs.existsSync(platformFile)) {
return cb(
null,
pipeMayCompressToNewMeter(fs.createReadStream(platformFile))
pipeToNewMeter(fs.createReadStream(platformFile))
);
}
} catch (err) {
log.debug(`prebuild-install failed[${stripe.file}]:`, err);
}
}
return cb(
null,
pipeMayCompressToNewMeter(fs.createReadStream(stripe.file))
);

return cb(null, pipeToNewMeter(fs.createReadStream(stripe.file)));
}

assert(false, 'producer: bad stripe');
Expand All @@ -432,23 +401,15 @@ export default function producer({
replaceDollarWise(
replaceDollarWise(
replaceDollarWise(
replaceDollarWise(
replaceDollarWise(
prelude,
'%VIRTUAL_FILESYSTEM%',
JSON.stringify(vfs)
),
'%DEFAULT_ENTRYPOINT%',
JSON.stringify(entrypoint)
),
'%SYMLINKS%',
JSON.stringify(snapshotSymLinks)
prelude,
'%VIRTUAL_FILESYSTEM%',
JSON.stringify(vfs)
),
'%DICT%',
JSON.stringify(fileDictionary)
'%DEFAULT_ENTRYPOINT%',
JSON.stringify(entrypoint)
),
'%DOCOMPRESS%',
JSON.stringify(doCompress)
'%SYMLINKS%',
JSON.stringify(snapshotSymLinks)
)
)
)
Expand Down
1 change: 0 additions & 1 deletion lib/walker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,6 @@ class Walker {
assets = expandFiles(assets, base);

for (const asset of assets) {
log.debug(' Adding asset : .... ', asset);
const stat = await fs.stat(asset);

if (stat.isFile()) {
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@
"lint": "eslint lib prelude test",
"lint:fix": "npm run lint -- --fix",
"prepare": "npm run build",
"prepublishOnly": "npm run lint",
"test": "npm run build && node test/test.js node14 no-npm && node test/test.js node12 no-npm && node test/test.js node10 no-npm && node test/test.js host only-npm"
},
"greenkeeper": {
Expand Down
Loading

0 comments on commit 52ddf23

Please sign in to comment.