Skip to content

Commit

Permalink
Disable the Browserify polyfill for vm (#3131)
Browse files Browse the repository at this point in the history
* Disable the Browserify polyfill for vm

* Add test

* Remove spaces and add lint rule.
  • Loading branch information
ekoleda-codaio authored Dec 20, 2024
1 parent 1c35cda commit 2d135fa
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 6 deletions.
4 changes: 4 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ module.exports = {
ignorePattern: '^import ',
},
],
'object-curly-spacing': [
'error',
'never'
],
},
overrides: [
{
Expand Down
4 changes: 2 additions & 2 deletions cli/extensions.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { ArgumentsCamelCase } from 'yargs';
import type {ArgumentsCamelCase} from 'yargs';
import fs from 'fs-extra';
import {isTestCommand} from './helpers';
import path from 'path';
import {print} from '../testing/helpers';
import { printAndExit } from '../testing/helpers';
import {printAndExit} from '../testing/helpers';

export enum Tools {
VSCode = 'vscode',
Expand Down
2 changes: 1 addition & 1 deletion cli/link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export async function handleLink({manifestDir, codaApiEndpoint, packIdOrUrl, api

const input = promptForInput(
`Overwrite existing deploy to pack https://coda.io/p/${existingPackId} with https://coda.io/p/${packId} instead? (y/N): `,
{ yesOrNo: true }
{yesOrNo: true}
);
if (input.toLocaleLowerCase() !== 'yes') {
return process.exit(1);
Expand Down
8 changes: 8 additions & 0 deletions dist/testing/compile.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion documentation/scripts/readthedocs_preprocess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/

import fs from 'fs';
import { print } from '../../testing/helpers';
import {print} from '../../testing/helpers';
import * as yaml from 'js-yaml';

const FilePath = 'mkdocs.yml';
Expand Down
2 changes: 1 addition & 1 deletion test/auth_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1337,7 +1337,7 @@ describe('Auth', () => {
afterTokenExchange({
accessToken: 'some-access-token',
refreshToken: 'some-refresh-token',
data: { server: 'https://foo.example.com' },
data: {server: 'https://foo.example.com'},
});
},
);
Expand Down
10 changes: 10 additions & 0 deletions test/compile_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {compilePackBundle} from '../testing/compile';
import {executeFormulaOrSyncWithVM} from '../testing/execution';
import {newMockSyncExecutionContext} from '../testing/mocks';
import path from 'path';
import {readFileSync} from 'fs';
import sinon from 'sinon';
import {translateErrorStackFromVM} from '../runtime/common/source_map';

Expand Down Expand Up @@ -88,4 +89,13 @@ describe('compile', () => {
),
);
});

it('no polyfill for vm', async () => {
const {bundlePath} = await compilePackBundle({
manifestPath: `${__dirname}/packs/fake_with_vm.ts`,
minify: false,
});
const bundleCode = readFileSync(bundlePath);
assert.notInclude(bundleCode.toString(), 'Script.prototype.runInNewContext');
});
});
15 changes: 15 additions & 0 deletions test/packs/fake_with_vm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import * as coda from '../..';
import vm from 'vm';

export const pack = coda.newPack();

pack.addFormula({
name: 'HelloWorld',
description: 'Returns "Hello World".',
parameters: [],
resultType: coda.ValueType.String,
execute: async ([], _context) => {
vm.runInNewContext('1');
return 'Hello World';
},
});
9 changes: 9 additions & 0 deletions testing/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ type BuildFunction = (options: {
options: CompilePackBundleOptions;
}) => Promise<void>;

// Modules that Browserify should ignore, and not try to polyfill.
const ModulesToDisableBrowserifyPolyfill = [
// The polyfill for vm doesn't work in the Packs runtime, and contains an eval() that breaks validation on upload.
'vm',
];

async function browserifyBundle({
lastBundleFilename,
outputBundleFilename,
Expand All @@ -76,6 +82,9 @@ async function browserifyBundle({
debug: true,
standalone: 'exports',
});
for (const module of ModulesToDisableBrowserifyPolyfill) {
browserifyCompiler.ignore(module);
}
const writer = fs.createWriteStream(outputBundleFilename);
const compiledStream = browserifyCompiler.bundle();
return new Promise(resolve => {
Expand Down
2 changes: 1 addition & 1 deletion testing/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type {PackVersionDefinition} from '../types';
import {ensureNonEmptyString} from '../helpers/ensure';
import fs from 'fs';
import { inspect } from 'util';
import {inspect} from 'util';
import path from 'path';
import * as readlineSync from 'readline-sync';
import {translateErrorStackFromVM} from '../runtime/common/source_map';
Expand Down

0 comments on commit 2d135fa

Please sign in to comment.