Milestone 0: import zappier billing, Verae middleware, and Zapier research
Compose-ready workspace: packages/zappier (rate card, portal, Stripe), packages/verae-zapier-middleware (timestamp + NATS), packages/verae-zapier (CLI app), vendor/zapier-platform, and research/zapier vendor corpus. Gate 0 structure checks pass. Product code and research are not yet wired.
This commit is contained in:
commit
b4150c8250
184 changed files with 6814366 additions and 0 deletions
8
vendor/zapier-platform/packages/core/.gitignore
vendored
Normal file
8
vendor/zapier-platform/packages/core/.gitignore
vendored
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
node_modules
|
||||
*.zip
|
||||
*.log
|
||||
.zapier-*
|
||||
.DS_Store
|
||||
build-boilerplate
|
||||
*.tgz
|
||||
.vscode
|
||||
1
vendor/zapier-platform/packages/core/.nvmrc
vendored
Normal file
1
vendor/zapier-platform/packages/core/.nvmrc
vendored
Normal file
|
|
@ -0,0 +1 @@
|
|||
v22
|
||||
43
vendor/zapier-platform/packages/core/ARCHITECTURE.md
vendored
Normal file
43
vendor/zapier-platform/packages/core/ARCHITECTURE.md
vendored
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
# `zapier-platform-core` Architecture
|
||||
|
||||
## Purpose
|
||||
|
||||
- This package, released as `zapier-platform-core` publicly on [npm](https://www.npmjs.com/package/zapier-platform-core), ships code that partners (literally) depend on.
|
||||
- Once declared as a dependency and installed, developers can import it to help with testing & TypeScript types.
|
||||
- It's not actually `require`d directly in app code, but it runs an app's exposed functions at runtime.
|
||||
- Its main responsibilities are:
|
||||
- organizing data passed from the Zapier monolith into the developer code (see [A bridge between...](#a-bridge-between-monolith-and-developer) below)
|
||||
- maintaining the `z` object, on which developers rely (see [Z Object](#z-object) below)
|
||||
- The `core`, `schema`, and `cli` packages are always released together under matching version numbers.
|
||||
|
||||
## Technical Organization
|
||||
|
||||
> `core/...`, when used in a path to a file, is shorthand for `zapier-platform/packages/core/...`
|
||||
|
||||
### A Bridge Between Monolith and Developer
|
||||
|
||||
- The monolith has a method called `call_into_js` which builds an `event` dict and sends it directly to [AWS Lambda](https://aws.amazon.com/lambda/)
|
||||
- The event is the first argument in the function returned from `createLambdaHandler` (in `core/src/tools/create-lambda-handler.js`).
|
||||
- That function creates a Node.js `Domain`, which captures all errors. Inside said `domain`, it runs the dev's code with the `event` (which holds, among other things, the `bundle`). Note that the `Domain` class has been deprecated, but is still available for use until it's actually removed.
|
||||
- There's some app-level middleware that runs (see `src/create-app.js`), then the function itself is run. Output (or errors) are returned from lambda to the monolith.
|
||||
|
||||
### Command Handling
|
||||
|
||||
- Compiled apps (aka "fully resolved") can have commands run on them. Most commonly, `execute` and `validate`.
|
||||
- That's set up in `core/src/tools/create-lambda-handler`.
|
||||
- This is the interface by which tools can perform app-specific tasks.
|
||||
|
||||
### Z Object
|
||||
|
||||
- At runtime, a developer's `perform` function is invoked with the following signature: `perform(z, bundle)`.
|
||||
- The `z` object is a collection of functions used commonly by developers and is defined in `core/src/tools/create-lambda-handler.js`
|
||||
- Its functions are either Zapier-specific (such as `z.cursor` and `z.dehydrate`) or wrappers around common JS functionality (such as `z.JSON.parse` and `z.console.log`) with better error handling or extra logging
|
||||
- Each of those functions has its own file in `core/src/tools`
|
||||
- The most important method is probably `z.request` (from `core/src/tools/create-app-request-client.js`), which devs use to make external requests. This is different from normal http requests in a number of ways:
|
||||
- there are `beforeRequest` and `afterResponse` functions that run before and after the request. They can be declared by the developer or inserted automatically based on the authentication type.
|
||||
- all requests are logged to our internal logging service, even if a developer uses their own request library (see `core/src/tools/create-http-patch.js`)
|
||||
|
||||
### Tests
|
||||
|
||||
- Most files in `core/src` have a matching file in `core/test`
|
||||
- there are test apps in `core/test/*app` directories
|
||||
1
vendor/zapier-platform/packages/core/CHANGELOG.md
vendored
Normal file
1
vendor/zapier-platform/packages/core/CHANGELOG.md
vendored
Normal file
|
|
@ -0,0 +1 @@
|
|||
See [CHANGELOG.md](https://github.com/zapier/zapier-platform/blob/main/CHANGELOG.md).
|
||||
3
vendor/zapier-platform/packages/core/LICENSE
vendored
Normal file
3
vendor/zapier-platform/packages/core/LICENSE
vendored
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
Copyright (c) Zapier, Inc.
|
||||
|
||||
This repository is part of Zapier Platform. By downloading, installing, accessing, or using any part of the Zapier Platform, including this repository, you agree to the Zapier Platform Agreement, which can be found at: https://zapier.com/platform/tos. If you do not agree to the Zapier Platform Agreement, you may not download, install, access, or use any part of the Zapier Platform, including this repository.
|
||||
27
vendor/zapier-platform/packages/core/README.md
vendored
Normal file
27
vendor/zapier-platform/packages/core/README.md
vendored
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
# Zapier Platform Core
|
||||
|
||||
This is the SDK used in Zapier integrations.
|
||||
|
||||
## Development
|
||||
|
||||
See [CONTRIBUTING.md](https://github.com/zapier/zapier-platform/blob/main/CONTRIBUTING.md) and [ARCHITECTURE.md](https://github.com/zapier/zapier-platform/blob/main/packages/core/ARCHITECTURE.md) of this package in particular.
|
||||
|
||||
Useful commands:
|
||||
|
||||
* `npm install` for getting started
|
||||
* `npm test` for running unit tests
|
||||
* `npm run local-integration-test` for running integration tests locally
|
||||
* `npm run build-boilerplate -- --debug` for building a `build-boilerplate/*.zip` (if you want to test buildless locally)
|
||||
|
||||
### Integration Test on AWS Lambda
|
||||
|
||||
Make sure your AWS access key have permission to update and run Lambda functions, and then you can use these commands to run tests on AWS Lambda:
|
||||
|
||||
* `npm run deploy-integration-test` builds and deploys a zip to a function named `integration-test-cli` on Lambda
|
||||
* `npm run lambda-integration-test` runs the integration test using the live Lambda function `integration-test-cli`
|
||||
|
||||
## Publishing
|
||||
|
||||
Only do this after merging your PR to `main`.
|
||||
|
||||
* `npm version [patch|minor|major]` will pull, test, update schema version in dependencies for this package, update docs, increment version in package.json, and push tags, which then will tell Travis to publish to npm
|
||||
28
vendor/zapier-platform/packages/core/bin/build-ec2.sh
vendored
Executable file
28
vendor/zapier-platform/packages/core/bin/build-ec2.sh
vendored
Executable file
|
|
@ -0,0 +1,28 @@
|
|||
# Usage like so:
|
||||
# ./build-ec2.sh ec2-user@ip-10-5-40-104.ec2.internal ami.bundle.zip
|
||||
|
||||
echo "###### Uploading..."
|
||||
echo ""
|
||||
ssh $1 "mkdir -p /home/ec2-user/zapier-core-platform"
|
||||
scp -rp package.json index.js src include bin $1:/home/ec2-user/zapier-core-platform
|
||||
echo ""
|
||||
echo ""
|
||||
echo "###### Building..."
|
||||
echo ""
|
||||
ssh $1 "cd /home/ec2-user/zapier-core-platform && rm -rf node_modules"
|
||||
ssh $1 "cd /home/ec2-user/zapier-core-platform && npm install --production"
|
||||
echo ""
|
||||
echo ""
|
||||
echo "###### Zipping..."
|
||||
echo ""
|
||||
ssh $1 "cd /home/ec2-user/zapier-core-platform && bin/build.sh $2"
|
||||
echo ""
|
||||
echo ""
|
||||
echo "###### Downloading..."
|
||||
echo ""
|
||||
scp -rp $1:/home/ec2-user/zapier-core-platform/$2 .
|
||||
echo ""
|
||||
echo ""
|
||||
echo "Done buiding! :-)"
|
||||
|
||||
node ./upload-lambda.js
|
||||
63
vendor/zapier-platform/packages/core/bin/build.sh
vendored
Executable file
63
vendor/zapier-platform/packages/core/bin/build.sh
vendored
Executable file
|
|
@ -0,0 +1,63 @@
|
|||
#!/usr/bin/env bash
|
||||
|
||||
# Usage like so:
|
||||
# ./build.sh local.bundle.zip
|
||||
|
||||
CORE_REPO_DIR=$(pwd)
|
||||
BUILD_DIR="./build"
|
||||
|
||||
if [ $# -eq 0 ]
|
||||
then
|
||||
echo "No arguments supplied."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [ -f $1 ]
|
||||
then
|
||||
rm -rf $1
|
||||
fi
|
||||
|
||||
if [ -d $BUILD_DIR ]
|
||||
then
|
||||
rm -rf $BUILD_DIR
|
||||
fi
|
||||
|
||||
mkdir $BUILD_DIR
|
||||
|
||||
PACK_FILENAME=$(npm pack)
|
||||
|
||||
tar xzf $PACK_FILENAME -C $BUILD_DIR
|
||||
|
||||
cp -R test $BUILD_DIR/package/
|
||||
|
||||
cd $BUILD_DIR/package
|
||||
|
||||
npm install --production
|
||||
|
||||
echo "Top 10 biggest dependent Node packages, FYI:"
|
||||
|
||||
du -s node_modules/* | sort -n -r | head -n 10
|
||||
|
||||
find . -print | \
|
||||
# removing test and example is bold!
|
||||
grep -v "\.git" | \
|
||||
grep -v "DS_Store" | \
|
||||
grep -v "/LICENSE" | \
|
||||
grep -v "\.min\.js" | \
|
||||
grep -v "/min/" | \
|
||||
grep -v "\.html" | \
|
||||
grep -v "\.css" | \
|
||||
grep -v "\.png" | \
|
||||
grep -v "\.gif" | \
|
||||
grep -v "\.jpg" | \
|
||||
grep -v "\.md" | \
|
||||
grep -v "\.sh" | \
|
||||
grep -v "\.zip" | \
|
||||
grep -v "tags" | \
|
||||
zip $1 -@ > /dev/null
|
||||
|
||||
cp ./local.bundle.zip $CORE_REPO_DIR/
|
||||
|
||||
cd $CORE_REPO_DIR
|
||||
rm $PACK_FILENAME
|
||||
rm -rf $BUILD_DIR
|
||||
9
vendor/zapier-platform/packages/core/bin/bump-dependencies.js
vendored
Executable file
9
vendor/zapier-platform/packages/core/bin/bump-dependencies.js
vendored
Executable file
|
|
@ -0,0 +1,9 @@
|
|||
#!/usr/bin/env node
|
||||
|
||||
const fs = require('fs');
|
||||
|
||||
const packageJson = require('../package.json');
|
||||
|
||||
packageJson.dependencies['zapier-platform-schema'] = packageJson.version;
|
||||
|
||||
fs.writeFile('./package.json', JSON.stringify(packageJson, null, ' ') + '\n');
|
||||
40
vendor/zapier-platform/packages/core/bin/upload-lambda.js
vendored
Executable file
40
vendor/zapier-platform/packages/core/bin/upload-lambda.js
vendored
Executable file
|
|
@ -0,0 +1,40 @@
|
|||
#!/usr/bin/env node
|
||||
|
||||
var fs = require('fs');
|
||||
var AWS = require('aws-sdk');
|
||||
|
||||
console.log('Uploading zip to test Lambda.');
|
||||
|
||||
// run it in real lambda...
|
||||
var lambda = new AWS.Lambda({ apiVersion: '2015-03-31', region: 'us-east-1' });
|
||||
var fileName = process.argv.slice(2)[0];
|
||||
var zipFileLambda = fs.readFileSync(fileName);
|
||||
console.log(zipFileLambda.length, 'bytes of code');
|
||||
|
||||
var params = {
|
||||
Code: {
|
||||
ZipFile: zipFileLambda,
|
||||
},
|
||||
FunctionName: 'integration-test-cli',
|
||||
Handler: 'index.integrationTestHandler',
|
||||
Role: 'arn:aws:iam::996097627176:role/allow_nothing_role',
|
||||
Runtime: 'nodejs8.10',
|
||||
Description: 'Via node ./lambda-upload.js for dev-platform-cli.',
|
||||
MemorySize: 512,
|
||||
Timeout: 30,
|
||||
};
|
||||
|
||||
// lambda.createFunction(
|
||||
// params,
|
||||
lambda.updateFunctionCode(
|
||||
{ FunctionName: params.FunctionName, ZipFile: params.Code.ZipFile },
|
||||
(err, data) => {
|
||||
console.log('update code:');
|
||||
if (err) {
|
||||
console.log(err, err.stack); // an error occurred
|
||||
} else {
|
||||
console.log(data); // successful response
|
||||
console.log('Now you can try `npm run lambda-integration-test`.');
|
||||
}
|
||||
},
|
||||
);
|
||||
6
vendor/zapier-platform/packages/core/include/zapierwrapper.js
vendored
Normal file
6
vendor/zapier-platform/packages/core/include/zapierwrapper.js
vendored
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
// not intended to be loaded via require() - copied during build step
|
||||
const path = require('path');
|
||||
const zapier = require('zapier-platform-core');
|
||||
const appPath = path.resolve(__dirname, 'index.js');
|
||||
// don't `require(appPath)` out here
|
||||
module.exports = { handler: zapier.createAppHandler(appPath) };
|
||||
26
vendor/zapier-platform/packages/core/include/zapierwrapper.mjs
vendored
Normal file
26
vendor/zapier-platform/packages/core/include/zapierwrapper.mjs
vendored
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
// not intended to be loaded via require() or import() - copied during build step
|
||||
import zapier from 'zapier-platform-core';
|
||||
|
||||
let _appRaw;
|
||||
try {
|
||||
_appRaw = await import('{REPLACE_ME_PACKAGE_NAME}');
|
||||
} catch (err) {
|
||||
if (
|
||||
err.code === 'ERR_MODULE_NOT_FOUND' &&
|
||||
err.message?.includes('{REPLACE_ME_PACKAGE_NAME}')
|
||||
) {
|
||||
err.message =
|
||||
'It seems you are using ESM because your package.json has `"type": "module"`. ' +
|
||||
'For ESM to work, make sure you specify a valid entry point using `exports` (instead of `main`) in package.json.\n\n' +
|
||||
err.message;
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
|
||||
// Allows a developer to use named exports or default export in entry point
|
||||
if (_appRaw && _appRaw.default) {
|
||||
_appRaw = _appRaw.default;
|
||||
}
|
||||
|
||||
export const appRaw = _appRaw;
|
||||
export const handler = zapier.createAppHandler(_appRaw);
|
||||
6
vendor/zapier-platform/packages/core/index.js
vendored
Normal file
6
vendor/zapier-platform/packages/core/index.js
vendored
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
const zapier = require('./src');
|
||||
zapier.version = require('./package.json').version;
|
||||
zapier.tools = require('./src/tools/exported');
|
||||
zapier.errors = require('./src/errors');
|
||||
zapier.console = require('./src/tools/console-singleton').consoleProxy;
|
||||
module.exports = zapier;
|
||||
27
vendor/zapier-platform/packages/core/index.mjs
vendored
Normal file
27
vendor/zapier-platform/packages/core/index.mjs
vendored
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
import zapier from './src/index.js';
|
||||
import packageJson from './package.json' with { type: 'json' };
|
||||
import _tools from './src/tools/exported.js';
|
||||
import _errors from './src/errors.js';
|
||||
import { consoleProxy } from './src/tools/console-singleton.js';
|
||||
zapier.version = packageJson.version;
|
||||
zapier.tools = _tools;
|
||||
zapier.errors = _errors;
|
||||
zapier.console = consoleProxy;
|
||||
// Allows `import { ... } from 'zapier-platform-core'`
|
||||
export const {
|
||||
createAppHandler,
|
||||
createAppTester,
|
||||
defineApp,
|
||||
defineCreate,
|
||||
defineInputField,
|
||||
defineInputFields,
|
||||
defineSearch,
|
||||
defineTrigger,
|
||||
integrationTestHandler,
|
||||
console,
|
||||
tools,
|
||||
version,
|
||||
errors,
|
||||
} = zapier;
|
||||
// Allows `import zapier from 'zapier-platform-core'`
|
||||
export default zapier;
|
||||
849
vendor/zapier-platform/packages/core/integration-test/integration-test.js
vendored
Normal file
849
vendor/zapier-platform/packages/core/integration-test/integration-test.js
vendored
Normal file
|
|
@ -0,0 +1,849 @@
|
|||
'use strict';
|
||||
|
||||
const fs = require('fs');
|
||||
const os = require('os');
|
||||
const path = require('path');
|
||||
const { promisify } = require('util');
|
||||
|
||||
const AWS = require('aws-sdk');
|
||||
const crypto = require('crypto');
|
||||
const nock = require('nock');
|
||||
const should = require('should');
|
||||
|
||||
const createLambdaHandler = require('../src/tools/create-lambda-handler');
|
||||
const mocky = require('../test/tools/mocky');
|
||||
const { HTTPBIN_URL } = require('../test/constants');
|
||||
|
||||
const sleep = promisify(setTimeout);
|
||||
|
||||
const lambda = new AWS.Lambda({
|
||||
apiVersion: '2015-03-31',
|
||||
region: 'us-east-1',
|
||||
});
|
||||
|
||||
const runLambda = (event) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
const params = {
|
||||
FunctionName: 'integration-test-cli',
|
||||
Payload: JSON.stringify(event),
|
||||
LogType: 'Tail',
|
||||
};
|
||||
|
||||
lambda.invoke(params, (err, data) => {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
return reject(err);
|
||||
}
|
||||
|
||||
const logs = Buffer.from(data.LogResult, 'base64').toString();
|
||||
console.log('\n=== LOGS ===\n', logs, '\n===\n');
|
||||
|
||||
const response = data.Payload ? JSON.parse(data.Payload) : data;
|
||||
if (response.errorMessage) {
|
||||
return reject(new Error(response.errorMessage));
|
||||
}
|
||||
|
||||
return resolve(response);
|
||||
});
|
||||
});
|
||||
};
|
||||
runLambda.testName = 'runLambda';
|
||||
|
||||
const runLocally = (event) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
const handler = createLambdaHandler(
|
||||
path.resolve(__dirname, '../test/userapp/'),
|
||||
);
|
||||
|
||||
try {
|
||||
resolve(handler(event));
|
||||
} catch (err) {
|
||||
reject(err);
|
||||
}
|
||||
});
|
||||
};
|
||||
runLocally.testName = 'runLocally';
|
||||
|
||||
const doTest = (runner) => {
|
||||
describe(`${runner.testName} integration tests`, () => {
|
||||
afterEach(() => {
|
||||
// Clear cache files
|
||||
const tmpdir = os.tmpdir();
|
||||
const cacheFilenames = ['cli-override.json', 'cli-hash.txt'];
|
||||
cacheFilenames.forEach((filename) => {
|
||||
const filepath = path.join(tmpdir, filename);
|
||||
if (fs.existsSync(filepath)) {
|
||||
fs.unlinkSync(filepath);
|
||||
}
|
||||
});
|
||||
|
||||
// Remove all the mocked requests
|
||||
nock.cleanAll();
|
||||
});
|
||||
|
||||
it('should return data from app function call', () => {
|
||||
const event = {
|
||||
command: 'execute',
|
||||
method: 'resources.list.list.operation.perform',
|
||||
bundle: {
|
||||
'param a': 'say, can u see me?',
|
||||
'param b': 'oh, can u see me too?',
|
||||
},
|
||||
};
|
||||
return runner(event).then((response) => {
|
||||
should.exist(response.results);
|
||||
response.results.should.eql([{ id: 1234 }, { id: 5678 }]);
|
||||
});
|
||||
});
|
||||
|
||||
it('should validate an app', () => {
|
||||
const event = {
|
||||
command: 'validate',
|
||||
};
|
||||
return runner(event).then((response) => {
|
||||
should.exist(response.results);
|
||||
});
|
||||
});
|
||||
|
||||
it('should provide the definition for an app', () => {
|
||||
const event = {
|
||||
command: 'definition',
|
||||
};
|
||||
return runner(event).then((response) => {
|
||||
should.exist(response.results);
|
||||
});
|
||||
});
|
||||
|
||||
it('should do a logging function', () => {
|
||||
const event = {
|
||||
command: 'execute',
|
||||
method: 'resources.loggingfunc.list.operation.perform',
|
||||
logExtra: {
|
||||
app_cli_id: 666,
|
||||
},
|
||||
};
|
||||
return runner(event).then((response) => {
|
||||
should.exist(response.results);
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle appRawOverride', () => {
|
||||
const event = {
|
||||
command: 'execute',
|
||||
method: 'triggers.fooList.operation.perform',
|
||||
appRawOverride: {
|
||||
resources: {
|
||||
foo: {
|
||||
key: 'foo',
|
||||
noun: 'Foo',
|
||||
list: {
|
||||
display: {},
|
||||
operation: {
|
||||
perform: { source: 'return [{id: 45678}]' },
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
return runner(event).then((response) => {
|
||||
response.results.should.deepEqual([{ id: 45678 }]);
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle appRawOverride as hash', () => {
|
||||
const definition = {
|
||||
resources: {
|
||||
foo: {
|
||||
key: 'foo',
|
||||
noun: 'Foo',
|
||||
list: {
|
||||
display: {},
|
||||
operation: {
|
||||
perform: { source: 'return [{id: 45678}]' },
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
mocky.mockRpcCall(definition);
|
||||
|
||||
const definitionHash = crypto
|
||||
.createHash('md5')
|
||||
.update(JSON.stringify(definition))
|
||||
.digest('hex');
|
||||
|
||||
const event = {
|
||||
command: 'execute',
|
||||
method: 'triggers.fooList.operation.perform',
|
||||
appRawOverride: definitionHash,
|
||||
rpc_base: 'https://mock.zapier.com/platform/rpc/cli',
|
||||
token: 'fake',
|
||||
};
|
||||
|
||||
return runner(event).then((response) => {
|
||||
response.results.length.should.eql(1);
|
||||
response.results[0].id.should.eql(45678);
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle array of [appRawOverrideHash, appRawExtension] and override inputFields', () => {
|
||||
const definition = {
|
||||
creates: {
|
||||
foo: {
|
||||
key: 'foo',
|
||||
noun: 'Foo',
|
||||
operation: {
|
||||
perform: { source: 'return [{id: 12345}]' },
|
||||
inputFields: [
|
||||
{ key: 'name', type: 'string' },
|
||||
{ key: 'testing', type: 'string' },
|
||||
],
|
||||
sample: {
|
||||
id: 123,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
mocky.mockRpcCall(definition);
|
||||
|
||||
const definitionExtension = {
|
||||
creates: {
|
||||
foo: {
|
||||
key: 'foo',
|
||||
noun: 'Foo',
|
||||
operation: {
|
||||
perform: { source: 'return [{id: 12345}]' },
|
||||
inputFields: [{ key: 'message', type: 'string' }],
|
||||
sample: {
|
||||
name: 'sample',
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const definitionHash = crypto
|
||||
.createHash('md5')
|
||||
.update(JSON.stringify(definition))
|
||||
.digest('hex');
|
||||
|
||||
const event = {
|
||||
command: 'execute',
|
||||
method: 'creates.foo.operation.inputFields',
|
||||
appRawOverride: [definitionHash, definitionExtension],
|
||||
rpc_base: 'https://mock.zapier.com/platform/rpc/cli',
|
||||
token: 'fake',
|
||||
};
|
||||
|
||||
return runner(event).then((response) => {
|
||||
response.results.should.eql([{ key: 'message', type: 'string' }]);
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle array of [appRawOverrideHash, appRawExtension] and override specific inputField', () => {
|
||||
const definition = {
|
||||
creates: {
|
||||
foo: {
|
||||
key: 'foo',
|
||||
noun: 'Foo',
|
||||
operation: {
|
||||
perform: { source: 'return [{id: 12345}]' },
|
||||
inputFields: [{ key: 'message', type: 'integer' }],
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
mocky.mockRpcCall(definition);
|
||||
|
||||
const definitionExtension = {
|
||||
creates: {
|
||||
foo: {
|
||||
noun: 'Foobar',
|
||||
operation: {
|
||||
inputFields: [{ key: 'message', type: 'string' }],
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const definitionHash = crypto
|
||||
.createHash('md5')
|
||||
.update(JSON.stringify(definition))
|
||||
.digest('hex');
|
||||
|
||||
const event = {
|
||||
command: 'execute',
|
||||
method: 'creates.foo.operation.inputFields',
|
||||
appRawOverride: [definitionHash, definitionExtension],
|
||||
rpc_base: 'https://mock.zapier.com/platform/rpc/cli',
|
||||
token: 'fake',
|
||||
};
|
||||
|
||||
return runner(event).then((response) => {
|
||||
response.results.should.eql([{ key: 'message', type: 'string' }]);
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle array of [appRawOverrideHash, appRawExtension] and overrides with an operation create key', () => {
|
||||
const definition = {
|
||||
creates: {
|
||||
operation: {
|
||||
key: 'operation',
|
||||
operation: {
|
||||
perform: { source: 'return [{id: 12345}]' },
|
||||
inputFields: [
|
||||
{ key: 'name', type: 'string' },
|
||||
{ key: 'testing', type: 'string' },
|
||||
],
|
||||
sample: {
|
||||
id: 123,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
mocky.mockRpcCall(definition);
|
||||
|
||||
const definitionExtension = {
|
||||
creates: {
|
||||
operation: {
|
||||
operation: {
|
||||
perform: { source: 'return [{id: 123}]' },
|
||||
inputFields: [{ key: 'message', type: 'string' }],
|
||||
sample: {
|
||||
name: 'sample',
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const definitionHash = crypto
|
||||
.createHash('md5')
|
||||
.update(JSON.stringify(definition))
|
||||
.digest('hex');
|
||||
|
||||
const event = {
|
||||
command: 'execute',
|
||||
method: 'creates.operation.operation.inputFields',
|
||||
appRawOverride: [definitionHash, definitionExtension],
|
||||
rpc_base: 'https://mock.zapier.com/platform/rpc/cli',
|
||||
token: 'fake',
|
||||
};
|
||||
|
||||
return runner(event).then((response) => {
|
||||
response.results.should.eql([{ key: 'message', type: 'string' }]);
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle array of [null, appRawExtension] without resource key collision', async () => {
|
||||
const definitionExtension = {
|
||||
// Example app test/userapp/index.js has a 'contact' resource with a
|
||||
// 'create' operation ('contactCreate' after compiled). So this should
|
||||
// merge with that and not collide.
|
||||
creates: {
|
||||
contactCreate: {
|
||||
operation: {
|
||||
perform: {
|
||||
source: 'return { id: 8, name: "Yoshi" }',
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const event = {
|
||||
comamnd: 'execute',
|
||||
method: 'creates.contactCreate.operation.perform',
|
||||
appRawOverride: [null, definitionExtension],
|
||||
};
|
||||
|
||||
const response = await runner(event);
|
||||
response.results.should.deepEqual({ id: 8, name: 'Yoshi' });
|
||||
});
|
||||
|
||||
it('should handle array of [appRawOverrideHash, appRawExtension] and override perform with request', () => {
|
||||
const definition = {
|
||||
triggers: {
|
||||
foo: {
|
||||
key: 'foo',
|
||||
noun: 'Foo',
|
||||
operation: {
|
||||
perform: {
|
||||
url: `${HTTPBIN_URL}/get`,
|
||||
params: {
|
||||
id: 54321,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
mocky.mockRpcCall(definition);
|
||||
|
||||
const definitionExtension = {
|
||||
triggers: {
|
||||
foo: {
|
||||
noun: 'Foobar',
|
||||
operation: {
|
||||
perform: { source: 'return [{id: 12345}]' },
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const definitionHash = crypto
|
||||
.createHash('md5')
|
||||
.update(JSON.stringify(definition))
|
||||
.digest('hex');
|
||||
|
||||
const event = {
|
||||
command: 'execute',
|
||||
method: 'triggers.foo.operation.perform',
|
||||
appRawOverride: [definitionHash, definitionExtension],
|
||||
rpc_base: 'https://mock.zapier.com/platform/rpc/cli',
|
||||
token: 'fake',
|
||||
};
|
||||
|
||||
return runner(event).then((response) => {
|
||||
response.results.should.eql([{ id: 12345 }]);
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle array of [appRawOverrideHash, appRawExtension] and override perform with source', async () => {
|
||||
const definition = {
|
||||
creates: {
|
||||
foo: {
|
||||
key: 'foo',
|
||||
noun: 'Foo',
|
||||
operation: {
|
||||
perform: { source: 'return [{id: 12345}]' },
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
mocky.mockRpcCall(definition);
|
||||
|
||||
const definitionExtension = {
|
||||
creates: {
|
||||
foo: {
|
||||
noun: 'Foobar',
|
||||
operation: {
|
||||
perform: {
|
||||
method: 'POST',
|
||||
url: `${HTTPBIN_URL}/post`,
|
||||
params: {
|
||||
id: 54321,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const definitionHash = crypto
|
||||
.createHash('md5')
|
||||
.update(JSON.stringify(definition))
|
||||
.digest('hex');
|
||||
|
||||
const event = {
|
||||
command: 'execute',
|
||||
method: 'creates.foo.operation.perform',
|
||||
appRawOverride: [definitionHash, definitionExtension],
|
||||
rpc_base: 'https://mock.zapier.com/platform/rpc/cli',
|
||||
token: 'fake',
|
||||
};
|
||||
|
||||
const response = await runner(event);
|
||||
response.results.should.containEql({
|
||||
args: {
|
||||
id: ['54321'],
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle array of [appRawOverrideHash, appRawExtension] and override perform with function', () => {
|
||||
const definition = {
|
||||
searches: {
|
||||
foo: {
|
||||
key: 'foo',
|
||||
noun: 'Foo',
|
||||
operation: {
|
||||
perform: '$func$2$f$',
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
mocky.mockRpcCall(definition);
|
||||
|
||||
const definitionExtension = {
|
||||
searches: {
|
||||
foo: {
|
||||
noun: 'Foobar',
|
||||
operation: {
|
||||
perform: { source: 'return [{id: 12345}]' },
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const definitionHash = crypto
|
||||
.createHash('md5')
|
||||
.update(JSON.stringify(definition))
|
||||
.digest('hex');
|
||||
|
||||
const event = {
|
||||
command: 'execute',
|
||||
method: 'searches.foo.operation.perform',
|
||||
appRawOverride: [definitionHash, definitionExtension],
|
||||
rpc_base: 'https://mock.zapier.com/platform/rpc/cli',
|
||||
token: 'fake',
|
||||
};
|
||||
|
||||
return runner(event).then((response) => {
|
||||
response.results.should.eql([
|
||||
{
|
||||
id: 12345,
|
||||
},
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle array of [appRawOverrideHash, appRawExtension] and add perform', () => {
|
||||
const definition = {
|
||||
triggers: {
|
||||
test: {
|
||||
key: 'test',
|
||||
noun: 'Foo',
|
||||
operation: {
|
||||
type: 'hook',
|
||||
performList: { source: 'return [{id: 54321}]' },
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
mocky.mockRpcCall(definition);
|
||||
|
||||
const definitionExtension = {
|
||||
triggers: {
|
||||
test: {
|
||||
operation: {
|
||||
perform: { source: 'return [{id: 12345}]' },
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const definitionHash = crypto
|
||||
.createHash('md5')
|
||||
.update(JSON.stringify(definition))
|
||||
.digest('hex');
|
||||
|
||||
const event = {
|
||||
command: 'execute',
|
||||
method: 'triggers.test.operation.perform',
|
||||
appRawOverride: [definitionHash, definitionExtension],
|
||||
rpc_base: 'https://mock.zapier.com/platform/rpc/cli',
|
||||
token: 'fake',
|
||||
};
|
||||
|
||||
return runner(event).then((response) => {
|
||||
response.results.should.eql([
|
||||
{
|
||||
id: 12345,
|
||||
},
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle array of [appRawOverrideHash, appRawExtension] and add new trigger', () => {
|
||||
const definition = {
|
||||
triggers: {
|
||||
test: {
|
||||
key: 'test',
|
||||
noun: 'Foo',
|
||||
operation: {
|
||||
perform: { source: 'return [{id: 54321}]' },
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
mocky.mockRpcCall(definition);
|
||||
|
||||
const definitionExtension = {
|
||||
triggers: {
|
||||
perform: {
|
||||
key: 'perform',
|
||||
noun: 'Foo',
|
||||
operation: {
|
||||
perform: { source: 'return [{id: 12345}]' },
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const definitionHash = crypto
|
||||
.createHash('md5')
|
||||
.update(JSON.stringify(definition))
|
||||
.digest('hex');
|
||||
|
||||
const event = {
|
||||
command: 'execute',
|
||||
method: 'triggers.perform.operation.perform',
|
||||
appRawOverride: [definitionHash, definitionExtension],
|
||||
rpc_base: 'https://mock.zapier.com/platform/rpc/cli',
|
||||
token: 'fake',
|
||||
};
|
||||
|
||||
return runner(event).then((response) => {
|
||||
response.results.should.eql([
|
||||
{
|
||||
id: 12345,
|
||||
},
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle array of non-existent appRawOverride (CLI app) with an appRawExtension, and add new trigger', () => {
|
||||
let definition; // CLI apps have no definition override
|
||||
|
||||
const definitionExtension = {
|
||||
triggers: {
|
||||
perform: {
|
||||
key: 'perform',
|
||||
noun: 'Foo',
|
||||
operation: {
|
||||
perform: { source: 'return [{id: 12345}]' },
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const event = {
|
||||
command: 'execute',
|
||||
method: 'triggers.perform.operation.perform',
|
||||
appRawOverride: [definition, definitionExtension],
|
||||
rpc_base: 'https://mock.zapier.com/platform/rpc/cli',
|
||||
token: 'fake',
|
||||
};
|
||||
|
||||
return runner(event).then((response) => {
|
||||
response.results.should.eql([
|
||||
{
|
||||
id: 12345,
|
||||
},
|
||||
]);
|
||||
// CLI code should be merged in with extension
|
||||
event.appRawOverride[0].should.not.eql(undefined);
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle function source in beforeRequest', async () => {
|
||||
const definition = {
|
||||
beforeRequest: [
|
||||
{
|
||||
source: "request.headers['X-Foo'] = 'it worked!'; return request",
|
||||
args: ['request', 'z', 'bundle'],
|
||||
},
|
||||
],
|
||||
creates: {
|
||||
foo: {
|
||||
operation: {
|
||||
perform: {
|
||||
method: 'POST',
|
||||
url: `${HTTPBIN_URL}/post`,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const event = {
|
||||
command: 'execute',
|
||||
method: 'creates.foo.operation.perform',
|
||||
appRawOverride: definition,
|
||||
};
|
||||
|
||||
const response = await runner(event);
|
||||
response.results.headers['X-Foo'].should.deepEqual(['it worked!']);
|
||||
});
|
||||
|
||||
it('should log requests', () => {
|
||||
const event = {
|
||||
command: 'execute',
|
||||
method: 'resources.requestsugar.list.operation.perform',
|
||||
logExtra: {
|
||||
app_cli_id: 666,
|
||||
},
|
||||
};
|
||||
return runner(event).then((response) => {
|
||||
should.exist(response.results);
|
||||
});
|
||||
});
|
||||
|
||||
it('should not leave leftover env vars', () => {
|
||||
const event = {
|
||||
environment: {
|
||||
_ZAPIER_ONE_TIME_SECRET: 'foo',
|
||||
},
|
||||
method: 'resources.env.list.operation.perform',
|
||||
};
|
||||
return runner(event)
|
||||
.then((response) => {
|
||||
response.results.length.should.eql(1);
|
||||
response.results[0].key.should.eql('_ZAPIER_ONE_TIME_SECRET');
|
||||
response.results[0].value.should.eql('foo');
|
||||
|
||||
delete event.environment;
|
||||
return runner(event);
|
||||
})
|
||||
.then((response) => {
|
||||
response.results.length.should.eql(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('hang detection', () => {
|
||||
let originalEnv;
|
||||
|
||||
before(() => {
|
||||
originalEnv = process.env;
|
||||
process.env = {
|
||||
...originalEnv,
|
||||
LOGGING_ENDPOINT: `${mocky.FAKE_LOG_URL}/input`,
|
||||
LOGGING_TOKEN: 'fake-token',
|
||||
};
|
||||
});
|
||||
|
||||
after(() => {
|
||||
process.env = originalEnv;
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
mocky.clearLogs();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
mocky.clearLogs();
|
||||
});
|
||||
|
||||
it('should end logger even a callback runs after lambda handler returns', async () => {
|
||||
mocky.mockLogServer();
|
||||
|
||||
const event = {
|
||||
method: 'resources.bad_callback.create.operation.perform',
|
||||
};
|
||||
const response = await runner(event);
|
||||
response.results.message.should.eql('ok');
|
||||
should.not.exist(process.teapot);
|
||||
|
||||
// Hopefully long enough for the callback to complete
|
||||
await sleep(2000);
|
||||
|
||||
process.teapot.should.eql("I'm a teapot!");
|
||||
|
||||
const logs = mocky.getLogs();
|
||||
logs.length.should.eql(1);
|
||||
|
||||
const log = logs[0];
|
||||
log.message.should.eql(`418 GET ${HTTPBIN_URL}/status/418`);
|
||||
});
|
||||
});
|
||||
|
||||
describe('error handling', () => {
|
||||
let originalEnv;
|
||||
|
||||
before(() => {
|
||||
originalEnv = process.env;
|
||||
process.env = {
|
||||
...originalEnv,
|
||||
LOGGING_ENDPOINT: `${mocky.FAKE_LOG_URL}/input`,
|
||||
LOGGING_TOKEN: 'fake-token',
|
||||
};
|
||||
});
|
||||
|
||||
after(() => {
|
||||
process.env = originalEnv;
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
mocky.clearLogs();
|
||||
mocky.mockLogServer();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
mocky.clearLogs();
|
||||
});
|
||||
|
||||
const testError = (method, errorMessage, logMessage) => {
|
||||
it(`should catch errors from ${method}`, async () => {
|
||||
const event = {
|
||||
command: 'execute',
|
||||
method,
|
||||
};
|
||||
|
||||
try {
|
||||
await runner(event);
|
||||
should(true).eql(false, 'Expected an error!');
|
||||
} catch (err) {
|
||||
should.exist(err);
|
||||
err.message.should.startWith(errorMessage);
|
||||
}
|
||||
|
||||
if (logMessage) {
|
||||
const logs = mocky.getLogs();
|
||||
const log = logs[0];
|
||||
log.message.should.startWith(logMessage);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
testError(
|
||||
'resources.failerfunc404.list.operation.perform',
|
||||
'{"status":404,', // ResponseError JSON-encodes the response body in its .message
|
||||
`404 GET ${HTTPBIN_URL}/status/404`,
|
||||
);
|
||||
testError(
|
||||
'resources.failerfuncasync.list.operation.perform',
|
||||
'Uncaught failure on async function!',
|
||||
'Uncaught error: Error: Uncaught failure on async function!',
|
||||
);
|
||||
testError(
|
||||
'resources.failerfunc.list.operation.perform',
|
||||
'Failer on sync function!',
|
||||
'Unhandled error: Error: Failer on sync function!',
|
||||
);
|
||||
testError(
|
||||
'resources.failerfuncpromise.list.operation.perform',
|
||||
'Failer on promise function!',
|
||||
);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
if (process.argv.indexOf('integration-test') > 0) {
|
||||
if (process.argv.indexOf('--lambda') > 0) {
|
||||
doTest(runLambda);
|
||||
} else if (process.argv.indexOf('--local') > 0) {
|
||||
doTest(runLocally);
|
||||
} else {
|
||||
doTest(runLambda);
|
||||
doTest(runLocally);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
runLambda,
|
||||
runLocally,
|
||||
doTest,
|
||||
};
|
||||
90
vendor/zapier-platform/packages/core/package.json
vendored
Normal file
90
vendor/zapier-platform/packages/core/package.json
vendored
Normal file
|
|
@ -0,0 +1,90 @@
|
|||
{
|
||||
"name": "zapier-platform-core",
|
||||
"version": "19.1.0",
|
||||
"description": "The core SDK for CLI apps in the Zapier Developer Platform.",
|
||||
"repository": "zapier/zapier-platform",
|
||||
"homepage": "https://platform.zapier.com/",
|
||||
"author": "Zapier Engineering <contact@zapier.com>",
|
||||
"license": "SEE LICENSE IN LICENSE",
|
||||
"types": "types/index.d.ts",
|
||||
"exports": {
|
||||
".": {
|
||||
"types": "./types/index.d.ts",
|
||||
"require": "./index.js",
|
||||
"import": "./index.mjs"
|
||||
},
|
||||
"./src/*": {
|
||||
"require": "./src/*.js"
|
||||
}
|
||||
},
|
||||
"files": [
|
||||
"/include/",
|
||||
"/index.js",
|
||||
"/index.mjs",
|
||||
"/src/",
|
||||
"/types/"
|
||||
],
|
||||
"scripts": {
|
||||
"preversion": "git pull && pnpm test",
|
||||
"version": "node bin/bump-dependencies.js && pnpm install && git add package.json pnpm-lock.yaml",
|
||||
"postversion": "git push && git push --tags",
|
||||
"main-tests": "mocha -t 20s --recursive test --exit",
|
||||
"type-tests": "tsd --files types/**/*.test-d.ts",
|
||||
"solo-test": "test $(OPT_OUT_PATCH_TEST_ONLY=yes mocha --recursive test -g 'should be able to opt out of patch' -R json | jq '.stats.passes') -eq 1 && echo 'Ran 1 test and it passed!'",
|
||||
"test": "pnpm main-tests && pnpm solo-test && pnpm type-tests",
|
||||
"test:debug": "mocha inspect -t 10s --recursive test",
|
||||
"debug": "mocha -t 10s --inspect-brk --recursive test",
|
||||
"test:w": "mocha -t 10s --recursive test --watch",
|
||||
"integration-test": "mocha -t 20s integration-test",
|
||||
"local-integration-test": "mocha -t 10s integration-test --local",
|
||||
"lambda-integration-test": "mocha -t 10s integration-test --lambda",
|
||||
"smoke-test": "mocha -t 2m smoke-test",
|
||||
"lint": "eslint src test",
|
||||
"lint:fix": "eslint --fix src test",
|
||||
"build-integration-test": "bin/build.sh local.bundle.zip",
|
||||
"upload-integration-test": "bin/upload-lambda.js local.bundle.zip",
|
||||
"deploy-integration-test": "pnpm build-integration-test && pnpm upload-integration-test",
|
||||
"validate": "pnpm test && pnpm smoke-test && pnpm lint"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=16",
|
||||
"npm": ">=5.6.0"
|
||||
},
|
||||
"engineStrict": true,
|
||||
"dependencies": {
|
||||
"@zapier/secret-scrubber": "^1.1.2",
|
||||
"content-disposition": "0.5.4",
|
||||
"dotenv": "17.2.1",
|
||||
"fernet": "^0.3.3",
|
||||
"form-data": "4.0.5",
|
||||
"json-schema-to-ts": "3.1.1",
|
||||
"lodash": "4.18.1",
|
||||
"mime-types": "3.0.1",
|
||||
"node-abort-controller": "3.1.1",
|
||||
"node-fetch": "2.7.0",
|
||||
"oauth-sign": "0.9.0",
|
||||
"semver": "7.7.2",
|
||||
"zapier-platform-schema": "workspace:*"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node-fetch": "^2.6.11",
|
||||
"adm-zip": "0.5.16",
|
||||
"aws-sdk": "^2.1397.0",
|
||||
"dicer": "^0.3.1",
|
||||
"fs-extra": "^11.3.0",
|
||||
"mock-fs": "^5.5.0",
|
||||
"nock": "^13.5.4",
|
||||
"tsd": "^0.31.1"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"zapier-platform-legacy-scripting-runner": ">=3"
|
||||
},
|
||||
"peerDependenciesMeta": {
|
||||
"zapier-platform-legacy-scripting-runner": {
|
||||
"optional": true
|
||||
}
|
||||
},
|
||||
"optionalDependencies": {
|
||||
"@types/node": "^20.3.1"
|
||||
}
|
||||
}
|
||||
289
vendor/zapier-platform/packages/core/smoke-test/smoke-test.js
vendored
Normal file
289
vendor/zapier-platform/packages/core/smoke-test/smoke-test.js
vendored
Normal file
|
|
@ -0,0 +1,289 @@
|
|||
// Any important changes here need to be made to src/smoke-tests/ in the cli repo!
|
||||
|
||||
const { spawnSync } = require('child_process');
|
||||
const crypto = require('crypto');
|
||||
const fs = require('fs-extra');
|
||||
const os = require('os');
|
||||
const path = require('path');
|
||||
|
||||
require('should');
|
||||
const fetch = require('node-fetch');
|
||||
|
||||
const CORE_PACKAGE_NAME = 'zapier-platform-core';
|
||||
|
||||
const TEST_APPS = [
|
||||
'basic-auth',
|
||||
'create',
|
||||
// 'custom-auth',
|
||||
// 'oauth2',
|
||||
// 'resource',
|
||||
// 'search',
|
||||
// 'session-auth',
|
||||
'trigger',
|
||||
];
|
||||
|
||||
const setupZapierRC = () => {
|
||||
let hasRC = false;
|
||||
const rcPath = path.join(os.homedir(), '.zapierrc');
|
||||
if (fs.existsSync(rcPath)) {
|
||||
hasRC = true;
|
||||
} else if (process.env.DEPLOY_KEY) {
|
||||
fs.writeFileSync(
|
||||
rcPath,
|
||||
JSON.stringify({ deployKey: process.env.DEPLOY_KEY }),
|
||||
);
|
||||
hasRC = true;
|
||||
}
|
||||
|
||||
return hasRC;
|
||||
};
|
||||
|
||||
const setupZapierAppRC = (workdir) => {
|
||||
let hasAppRC = false;
|
||||
if (process.env.TEST_APP_ID && process.env.TEST_APP_KEY) {
|
||||
const rcPath = path.join(workdir, '.zapierapprc');
|
||||
if (!fs.existsSync(rcPath)) {
|
||||
fs.writeFileSync(
|
||||
rcPath,
|
||||
JSON.stringify({
|
||||
id: parseInt(process.env.TEST_APP_ID),
|
||||
key: process.env.TEST_APP_KEY,
|
||||
}),
|
||||
);
|
||||
hasAppRC = true;
|
||||
}
|
||||
}
|
||||
return hasAppRC;
|
||||
};
|
||||
|
||||
const getPackageDir = (dirname) =>
|
||||
path.resolve(path.dirname(process.cwd()), dirname);
|
||||
|
||||
const npmPack = (workingDir) => {
|
||||
const proc = spawnSync('npm', ['pack'], {
|
||||
encoding: 'utf8',
|
||||
cwd: workingDir,
|
||||
});
|
||||
const lines = proc.stdout.split('\n');
|
||||
let filename;
|
||||
for (let i = lines.length - 1; i >= 0; i--) {
|
||||
const line = lines[i].trim();
|
||||
if (line) {
|
||||
filename = line;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return filename;
|
||||
};
|
||||
|
||||
const npmPackCore = (schemaPackagePath) => {
|
||||
// Patch core's package.json to use schema from local
|
||||
const packageJsonPath = path.join(process.cwd(), 'package.json');
|
||||
const originalPackageJsonText = fs.readFileSync(packageJsonPath, {
|
||||
encoding: 'utf8',
|
||||
});
|
||||
const packageJson = JSON.parse(originalPackageJsonText);
|
||||
packageJson.dependencies['zapier-platform-schema'] =
|
||||
`file:${schemaPackagePath}`;
|
||||
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson));
|
||||
|
||||
let filename;
|
||||
|
||||
try {
|
||||
const proc = spawnSync('npm', ['pack'], { encoding: 'utf8' });
|
||||
const lines = proc.stdout.split('\n');
|
||||
for (let i = lines.length - 1; i >= 0; i--) {
|
||||
const line = lines[i].trim();
|
||||
if (line) {
|
||||
filename = line;
|
||||
break;
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
fs.writeFile(packageJsonPath, originalPackageJsonText);
|
||||
}
|
||||
|
||||
return filename;
|
||||
};
|
||||
|
||||
const setupTempWorkingDir = () => {
|
||||
let workdir;
|
||||
const tmpBaseDir = os.tmpdir();
|
||||
while (!workdir || fs.existsSync(workdir)) {
|
||||
workdir = path.join(
|
||||
tmpBaseDir,
|
||||
'zapier-' + crypto.randomBytes(20).toString('hex'),
|
||||
);
|
||||
}
|
||||
fs.mkdirSync(workdir);
|
||||
return workdir;
|
||||
};
|
||||
|
||||
const copyTestApps = (workdir) => {
|
||||
const repoRoot = path.dirname(path.dirname(path.dirname(__dirname)));
|
||||
TEST_APPS.forEach((appName) => {
|
||||
const srcAppDir = path.join(repoRoot, 'example-apps', appName);
|
||||
const destAppDir = path.join(workdir, appName);
|
||||
fs.copySync(srcAppDir, destAppDir, {
|
||||
filter: (src, dest) => !src.includes('node_modules/'),
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
const npmInstalls = (coreZipPath, cliZipPath, workdir) => {
|
||||
// When releasing a new core version, example apps would have bumped their
|
||||
// core before the new core version is published to npm. So we need to patch
|
||||
// example app's package.json to use local core temporarily to avoid `npm
|
||||
// install` error.
|
||||
const packageJsonPath = path.join(workdir, 'package.json');
|
||||
const origPackageJsonText = fs.readFileSync(packageJsonPath);
|
||||
const packageJson = JSON.parse(origPackageJsonText);
|
||||
packageJson.dependencies[CORE_PACKAGE_NAME] = `file:${coreZipPath}`;
|
||||
|
||||
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson));
|
||||
|
||||
try {
|
||||
spawnSync('npm', ['install'], {
|
||||
encoding: 'utf8',
|
||||
cwd: workdir,
|
||||
});
|
||||
spawnSync('npm', ['install', '--no-save', cliZipPath], {
|
||||
encoding: 'utf8',
|
||||
cwd: workdir,
|
||||
});
|
||||
} finally {
|
||||
fs.writeFileSync(packageJsonPath, origPackageJsonText);
|
||||
}
|
||||
};
|
||||
|
||||
describe('smoke tests - setup will take some time', () => {
|
||||
const context = {
|
||||
// Global context that will be available for all test cases in this test suite
|
||||
corePackage: {
|
||||
filename: null,
|
||||
path: null,
|
||||
},
|
||||
schemaPackage: {
|
||||
filename: null,
|
||||
path: null,
|
||||
},
|
||||
cliPackage: {
|
||||
filename: null,
|
||||
path: null,
|
||||
},
|
||||
workRepoDir: null,
|
||||
workAppDir: null,
|
||||
cliBin: null,
|
||||
hasRC: false,
|
||||
hasAppRC: false,
|
||||
};
|
||||
|
||||
before(async () => {
|
||||
context.hasRC = setupZapierRC();
|
||||
|
||||
const cliDir = getPackageDir('cli');
|
||||
context.cliPackage.filename = npmPack(cliDir);
|
||||
context.cliPackage.path = path.join(cliDir, context.cliPackage.filename);
|
||||
|
||||
const schemaDir = getPackageDir('schema');
|
||||
context.schemaPackage.filename = npmPack(schemaDir);
|
||||
context.schemaPackage.path = path.join(
|
||||
schemaDir,
|
||||
context.schemaPackage.filename,
|
||||
);
|
||||
|
||||
context.corePackage.filename = npmPackCore(context.schemaPackage.path);
|
||||
context.corePackage.path = path.join(
|
||||
process.cwd(),
|
||||
context.corePackage.filename,
|
||||
);
|
||||
|
||||
context.workRepoDir = setupTempWorkingDir();
|
||||
|
||||
copyTestApps(context.workRepoDir);
|
||||
});
|
||||
|
||||
after(() => {
|
||||
fs.unlinkSync(context.cliPackage.path);
|
||||
fs.unlinkSync(context.corePackage.path);
|
||||
fs.unlinkSync(context.schemaPackage.path);
|
||||
fs.removeSync(context.workRepoDir);
|
||||
});
|
||||
|
||||
it('package size should not change much', async function () {
|
||||
const baseUrl = 'https://registry.npmjs.org/zapier-platform-core';
|
||||
let res = await fetch(baseUrl);
|
||||
const packageInfo = await res.json();
|
||||
const latestVersion = packageInfo['dist-tags'].latest;
|
||||
|
||||
res = await fetch(`${baseUrl}/-/zapier-platform-core-${latestVersion}.tgz`);
|
||||
const baselineSize = res.headers.get('content-length');
|
||||
const newSize = fs.statSync(context.corePackage.path).size;
|
||||
newSize.should.be.within(baselineSize * 0.7, baselineSize * 1.3);
|
||||
|
||||
this.test.title += ` (${baselineSize} -> ${newSize} bytes)`;
|
||||
});
|
||||
|
||||
TEST_APPS.forEach((appName) => {
|
||||
describe(appName, () => {
|
||||
before(async () => {
|
||||
context.workAppDir = path.join(context.workRepoDir, appName);
|
||||
npmInstalls(
|
||||
context.corePackage.path,
|
||||
context.cliPackage.path,
|
||||
context.workAppDir,
|
||||
);
|
||||
|
||||
context.hasAppRC = setupZapierAppRC(context.workAppDir);
|
||||
|
||||
context.cliBin = path.join(
|
||||
context.workAppDir,
|
||||
'node_modules',
|
||||
'.bin',
|
||||
'zapier-platform',
|
||||
);
|
||||
});
|
||||
|
||||
it('zapier-platform test', () => {
|
||||
const proc = spawnSync(context.cliBin, ['test'], {
|
||||
encoding: 'utf8',
|
||||
cwd: context.workAppDir,
|
||||
env: {
|
||||
PATH: process.env.PATH,
|
||||
DISABLE_ZAPIER_ANALYTICS: 1,
|
||||
},
|
||||
});
|
||||
if (proc.status !== 0) {
|
||||
console.log(proc.stdout);
|
||||
console.log(proc.stderr);
|
||||
}
|
||||
proc.status.should.eql(0);
|
||||
});
|
||||
|
||||
it('zapier-platform build', function () {
|
||||
if (!context.hasAppRC) {
|
||||
this.skip();
|
||||
return;
|
||||
}
|
||||
|
||||
const proc = spawnSync(
|
||||
context.cliBin,
|
||||
['build', '--skip-npm-install'],
|
||||
{
|
||||
encoding: 'utf8',
|
||||
cwd: context.workAppDir,
|
||||
env: {
|
||||
PATH: process.env.PATH,
|
||||
DISABLE_ZAPIER_ANALYTICS: 1,
|
||||
},
|
||||
},
|
||||
);
|
||||
if (proc.status !== 0) {
|
||||
console.log(proc.stdout);
|
||||
console.log(proc.stderr);
|
||||
}
|
||||
proc.status.should.eql(0);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
20
vendor/zapier-platform/packages/core/src/app-middlewares/after/callback-status-catcher.js
vendored
Normal file
20
vendor/zapier-platform/packages/core/src/app-middlewares/after/callback-status-catcher.js
vendored
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
'use strict';
|
||||
const STATUSES = require('../../constants').STATUSES;
|
||||
const _ = require('lodash');
|
||||
/*
|
||||
this method creates the correct envelope responses if the app has used a callback url in their code
|
||||
by signalling to Zapier that this app/method is returning a callback status the task will be placed
|
||||
in a waiting state until the callback is called.
|
||||
*/
|
||||
const callbackStatusCatcher = (output) => {
|
||||
const input = output.input || {};
|
||||
|
||||
const callbackUsed = _.get(input, '_zapier.event.callbackUsed');
|
||||
if (callbackUsed) {
|
||||
// output is an envelope so we can set status here
|
||||
output.status = STATUSES.CALLBACK;
|
||||
}
|
||||
return output;
|
||||
};
|
||||
|
||||
module.exports = callbackStatusCatcher;
|
||||
57
vendor/zapier-platform/packages/core/src/app-middlewares/after/checks.js
vendored
Normal file
57
vendor/zapier-platform/packages/core/src/app-middlewares/after/checks.js
vendored
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
'use strict';
|
||||
|
||||
const _ = require('lodash');
|
||||
|
||||
const constants = require('../../constants');
|
||||
const errors = require('../../errors');
|
||||
|
||||
const checks = _.values(require('../../checks'));
|
||||
|
||||
/*
|
||||
Take a look at our output results, run some checks on it, and depending on if we
|
||||
are running "locally or live" we will "raise or log" the errors, respectively.
|
||||
*/
|
||||
const checkOutput = (output) => {
|
||||
const input = output.input || {};
|
||||
const _zapier = input._zapier || {};
|
||||
const event = _zapier.event || {};
|
||||
const compiledApp = _zapier.app || {};
|
||||
|
||||
const runChecks =
|
||||
event.method && event.command === 'execute' && !_zapier.skipChecks;
|
||||
|
||||
const bundleSkipChecks = event.bundle.skipChecks || [];
|
||||
|
||||
if (runChecks) {
|
||||
const rawResults = checks
|
||||
.filter((check) => {
|
||||
return (
|
||||
!bundleSkipChecks.includes(check.name) &&
|
||||
check.shouldRun(event.method, event.bundle, compiledApp)
|
||||
);
|
||||
})
|
||||
.map((check) => {
|
||||
return check
|
||||
.run(event.method, output.results, compiledApp, event.bundle)
|
||||
.map((err) => ({ name: check.name, error: err }));
|
||||
});
|
||||
const checkResults = _.flatten(rawResults);
|
||||
|
||||
if (checkResults.length > 0) {
|
||||
if (constants.IS_TESTING || event.isDeveloper || event.calledFromCli) {
|
||||
const shortMsgs = checkResults.map((info) => ` - ${info.error}`);
|
||||
throw new errors.CheckError(
|
||||
'Invalid API Response:\n' + shortMsgs.join('\n'),
|
||||
);
|
||||
} else {
|
||||
const longMsgs = checkResults.map(
|
||||
(info) => `Zapier check "${info.name}" failed: ${info.error}`,
|
||||
);
|
||||
longMsgs.forEach((err) => input.z.console.error(err));
|
||||
}
|
||||
}
|
||||
}
|
||||
return output;
|
||||
};
|
||||
|
||||
module.exports = checkOutput;
|
||||
39
vendor/zapier-platform/packages/core/src/app-middlewares/after/large-response-cacher.js
vendored
Normal file
39
vendor/zapier-platform/packages/core/src/app-middlewares/after/large-response-cacher.js
vendored
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
'use strict';
|
||||
|
||||
const constants = require('../../constants');
|
||||
const cleaner = require('../../tools/cleaner');
|
||||
const responseStasher = require('../../tools/create-response-stasher');
|
||||
|
||||
const largeResponseCachePointer = async (output) => {
|
||||
const response = cleaner.maskOutput(output);
|
||||
if (!response.results) {
|
||||
return output;
|
||||
}
|
||||
|
||||
const autostashLimit = output.input._zapier.event.autostashPayloadOutputLimit;
|
||||
|
||||
const payload = JSON.stringify(response.results);
|
||||
const size = payload.length;
|
||||
|
||||
// If autostash limit is defined, and is within the range, stash the response
|
||||
// If it is -1, stash the response regardless of size
|
||||
if (
|
||||
(autostashLimit &&
|
||||
size >= constants.RESPONSE_SIZE_LIMIT &&
|
||||
size <= autostashLimit) ||
|
||||
autostashLimit === -1
|
||||
) {
|
||||
const url = await responseStasher(output.input, payload);
|
||||
output.resultsUrl = url;
|
||||
output.results = Array.isArray(output.results) ? [] : {};
|
||||
} else if (autostashLimit && size > autostashLimit) {
|
||||
// If the limit is defined and is out of range, throw a descriptive error
|
||||
// indicating the size of the response and the autostash limit
|
||||
throw new Error(
|
||||
`Response size of ${size} bytes exceeds maximum allowed size: ${autostashLimit}`,
|
||||
);
|
||||
}
|
||||
return output;
|
||||
};
|
||||
|
||||
module.exports = largeResponseCachePointer;
|
||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Reference in a new issue