mirror of
https://github.com/LukeHagar/vercel.git
synced 2025-12-12 12:57:47 +00:00
Compare commits
103 Commits
@now/pytho
...
@now/pytho
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f7bc3b3490 | ||
|
|
d83b09ffbd | ||
|
|
0c120f6202 | ||
|
|
48a01415f8 | ||
|
|
9198b72382 | ||
|
|
79048b83de | ||
|
|
f798e2cf19 | ||
|
|
be5057a738 | ||
|
|
059d44fde7 | ||
|
|
11ad481546 | ||
|
|
4061ed2eb7 | ||
|
|
b54c79e619 | ||
|
|
2203ae1a20 | ||
|
|
5e58c1738b | ||
|
|
290577ddfb | ||
|
|
a5e651403e | ||
|
|
df2769717b | ||
|
|
880ef77b7b | ||
|
|
af2616c283 | ||
|
|
4300f4d797 | ||
|
|
4921e541af | ||
|
|
6ce00eda8c | ||
|
|
060e952f6c | ||
|
|
1639127b24 | ||
|
|
ebbb62eaea | ||
|
|
66954e84fe | ||
|
|
15a21bb28c | ||
|
|
96ca1e1d8c | ||
|
|
587cb52191 | ||
|
|
95422ffd46 | ||
|
|
391a883799 | ||
|
|
43d6960df4 | ||
|
|
5c128003d8 | ||
|
|
2f8fd1b14b | ||
|
|
625553c146 | ||
|
|
3b0ce7bad3 | ||
|
|
489ec1dfa5 | ||
|
|
d3f92d7143 | ||
|
|
3072b044ef | ||
|
|
3b4968657f | ||
|
|
cafae4c800 | ||
|
|
64952d24f1 | ||
|
|
72758b6e0d | ||
|
|
4f80bc74d5 | ||
|
|
a6e62ed61c | ||
|
|
d8eecd6172 | ||
|
|
0e70608511 | ||
|
|
da0de150df | ||
|
|
a58c35fb9e | ||
|
|
fe88a69ab7 | ||
|
|
9767682006 | ||
|
|
3285b31721 | ||
|
|
70353c7fc0 | ||
|
|
f85cf99325 | ||
|
|
8b14a46d04 | ||
|
|
383cbfd82f | ||
|
|
81e268a3c9 | ||
|
|
ac8b33213b | ||
|
|
de12e7b8c8 | ||
|
|
b9346603f0 | ||
|
|
0b793dfc35 | ||
|
|
9dd672c383 | ||
|
|
1b743aeea8 | ||
|
|
d4af4b9f5c | ||
|
|
b734ca3e01 | ||
|
|
f81d753104 | ||
|
|
db31b9a207 | ||
|
|
b80b5182e6 | ||
|
|
268a7c2b81 | ||
|
|
667a16c996 | ||
|
|
7b851f81c0 | ||
|
|
80fbbcd194 | ||
|
|
3108332043 | ||
|
|
7509c82c32 | ||
|
|
c4f5a5b48d | ||
|
|
05314da810 | ||
|
|
5f1cf714c1 | ||
|
|
2623e2e799 | ||
|
|
bac1da09d4 | ||
|
|
5b57f1a3ac | ||
|
|
2e95dd5329 | ||
|
|
215f6367d6 | ||
|
|
e8cd348a79 | ||
|
|
168f373641 | ||
|
|
8c3174be29 | ||
|
|
898de78b63 | ||
|
|
26e33c1c4b | ||
|
|
c2f95de3ec | ||
|
|
6a7de860db | ||
|
|
acb8cadafe | ||
|
|
1a8df7080d | ||
|
|
5a92826eb0 | ||
|
|
e083aa3750 | ||
|
|
941f675657 | ||
|
|
6fad726abb | ||
|
|
dd22051d6b | ||
|
|
7e86cb403f | ||
|
|
d19d557738 | ||
|
|
e4281f698c | ||
|
|
86ff681c6d | ||
|
|
ba97a7cf19 | ||
|
|
0a94397700 | ||
|
|
5c8e2f2ccc |
@@ -6,12 +6,12 @@
|
|||||||
/packages/now-build-utils/src/*.js
|
/packages/now-build-utils/src/*.js
|
||||||
/packages/now-build-utils/src/fs/*.js
|
/packages/now-build-utils/src/fs/*.js
|
||||||
/packages/now-node/dist/*
|
/packages/now-node/dist/*
|
||||||
/packages/now-layer-node/dist/*
|
|
||||||
/packages/now-layer-npm/dist/*
|
|
||||||
/packages/now-layer-yarn/dist/*
|
|
||||||
/packages/now-next/dist/*
|
/packages/now-next/dist/*
|
||||||
/packages/now-node-bridge/*
|
/packages/now-node-bridge/*
|
||||||
/packages/now-python/dist/*
|
/packages/now-python/dist/*
|
||||||
/packages/now-optipng/dist/*
|
/packages/now-optipng/dist/*
|
||||||
/packages/now-go/*
|
/packages/now-go/*
|
||||||
/packages/now-rust/dist/*
|
/packages/now-rust/dist/*
|
||||||
|
/packages/now-ruby/dist/*
|
||||||
|
/packages/now-static-build/dist/*
|
||||||
|
/packages/now-static-build/test/fixtures/**
|
||||||
|
|||||||
4
.github/CODEOWNERS
vendored
4
.github/CODEOWNERS
vendored
@@ -2,8 +2,10 @@
|
|||||||
# https://help.github.com/en/articles/about-code-owners
|
# https://help.github.com/en/articles/about-code-owners
|
||||||
|
|
||||||
* @styfle
|
* @styfle
|
||||||
/packages/now-node @styfle @tootallnate
|
/packages/now-node @styfle @tootallnate @lucleray
|
||||||
|
/packages/now-node-bridge @styfle @tootallnate @lucleray
|
||||||
/packages/now-next @timer @dav-is
|
/packages/now-next @timer @dav-is
|
||||||
/packages/now-go @styfle @sophearak
|
/packages/now-go @styfle @sophearak
|
||||||
/packages/now-python @styfle @sophearak
|
/packages/now-python @styfle @sophearak
|
||||||
/packages/now-rust @styfle @mike-engel @anmonteiro
|
/packages/now-rust @styfle @mike-engel @anmonteiro
|
||||||
|
/packages/now-ruby @styfle @coetry @nathancahill
|
||||||
|
|||||||
3
.vscode/settings.json
vendored
3
.vscode/settings.json
vendored
@@ -1,3 +0,0 @@
|
|||||||
{
|
|
||||||
"eslint.enable": false
|
|
||||||
}
|
|
||||||
85
CONTRIBUTING.md
Normal file
85
CONTRIBUTING.md
Normal file
@@ -0,0 +1,85 @@
|
|||||||
|
# Contributing
|
||||||
|
|
||||||
|
When contributing to this repository, please first discuss the change you wish to make via issue or [spectrum](https://spectrum.chat/zeit) with the owners of this repository before submitting a Pull Request.
|
||||||
|
|
||||||
|
Please read our [code of conduct](CODE_OF_CONDUCT.md) and follow it in all your interactions with the project.
|
||||||
|
|
||||||
|
## Local development
|
||||||
|
|
||||||
|
This project is configured in a monorepo pattern where one repo contains multiple npm packages. Dependencies are installed and managed with `yarn`, not `npm` CLI.
|
||||||
|
|
||||||
|
To get started, execute the following:
|
||||||
|
|
||||||
|
```
|
||||||
|
git clone https://github.com/zeit/now-builders
|
||||||
|
yarn install
|
||||||
|
yarn bootstrap
|
||||||
|
yarn build
|
||||||
|
yarn lint
|
||||||
|
yarn test
|
||||||
|
```
|
||||||
|
|
||||||
|
Make sure all the tests pass before making changes.
|
||||||
|
|
||||||
|
## Verifying your change
|
||||||
|
|
||||||
|
Once you are done with your changes (we even suggest doing it along the way ), make sure all the test still run by running
|
||||||
|
|
||||||
|
```
|
||||||
|
yarn build && yarn test
|
||||||
|
```
|
||||||
|
|
||||||
|
from the root of the project.
|
||||||
|
|
||||||
|
If any test fails, make sure to fix it along with your changes. See [Interpreting test errors](#Interpreting-test-errors) for more information about how the tests are executed, especially the integration tests.
|
||||||
|
|
||||||
|
## Pull Request Process
|
||||||
|
|
||||||
|
Once you are confident that your changes work properly, open a pull request on the main repository.
|
||||||
|
|
||||||
|
The pull request will be reviewed by the maintainers and the tests will be checked by our continuous integration platform.
|
||||||
|
|
||||||
|
## Interpreting test errors
|
||||||
|
|
||||||
|
There are 2 kinds of tests in this repository – Unit tests and Integration tests.
|
||||||
|
|
||||||
|
Unit tests are run locally with `jest` and execute quickly because they are testing the smallest units of code.
|
||||||
|
|
||||||
|
### Integration tests
|
||||||
|
|
||||||
|
Integration tests create deployments to your ZEIT account using the `test` project name. After each test is deployed, the `probes` key is used to check if the response is the expected value. If the value doesn't match, you'll see a message explaining the difference. If the deployment failed to build, you'll see a more generic message like the following:
|
||||||
|
|
||||||
|
```
|
||||||
|
[Error: Fetched page https://test-8ashcdlew.now.sh/root.js does not contain hello Root!. Instead it contains An error occurred with this application.
|
||||||
|
|
||||||
|
NO_STATUS_CODE_FRO Response headers:
|
||||||
|
cache-control=s-maxage=0
|
||||||
|
connection=close
|
||||||
|
content-type=text/plain; charset=utf-8
|
||||||
|
date=Wed, 19 Jun 2019 18:01:37 GMT
|
||||||
|
server=now
|
||||||
|
strict-transport-security=max-age=63072000
|
||||||
|
transfer-encoding=chunked
|
||||||
|
x-now-id=iad1:hgtzj-1560967297876-44ae12559f95
|
||||||
|
x-now-trace=iad1]
|
||||||
|
```
|
||||||
|
|
||||||
|
In such cases you can visit the URL of the failed deployment and append `/_logs` so see the build error. In the case above, that would be https://test-8ashcdlew.now.sh/_logs
|
||||||
|
|
||||||
|
The logs of this deployment will contain the actual error which may help you to understand what went wrong.
|
||||||
|
|
||||||
|
### @zeit/ncc integration
|
||||||
|
|
||||||
|
Some of the builders use `@zeit/ncc` to bundle files before deployment. If you suspect an error with the bundling mechanism, you can run the `ncc` CLI with a couple modifications to the test.
|
||||||
|
|
||||||
|
For example if an error occurred in `now-node/test/fixtures/08-assets`
|
||||||
|
|
||||||
|
```
|
||||||
|
cd packages/now-node/test/fixtures/08-assets
|
||||||
|
yarn install
|
||||||
|
echo 'require("http").createServer(module.exports).listen(3000)' >> index.js
|
||||||
|
npx @zeit/ncc@0.20.1 build index.js --source-map
|
||||||
|
node dist
|
||||||
|
```
|
||||||
|
|
||||||
|
This will compile the test with the specific version of `ncc` and run the resulting file. If it fails here, then there is likely a bug in `ncc` and not the Builder.
|
||||||
11
README.md
11
README.md
@@ -23,7 +23,11 @@ For the Canary Channel, publish the modified Builders to npm with the following:
|
|||||||
yarn publish-canary
|
yarn publish-canary
|
||||||
```
|
```
|
||||||
|
|
||||||
For the Stable Channel, you must cherry pick each commit from canary to master and then deploy the modified Builders:
|
For the Stable Channel, you must do the following:
|
||||||
|
|
||||||
|
- Cherry pick each commit from canary to master
|
||||||
|
- Verify that you are _in-sync_ with canary (with the exception of the `version` line in `package.json`)
|
||||||
|
- Deploy the modified Builders
|
||||||
|
|
||||||
```
|
```
|
||||||
git checkout master
|
git checkout master
|
||||||
@@ -33,6 +37,7 @@ git cherry-pick <PR502_COMMIT_SHA>
|
|||||||
git cherry-pick <PR503_COMMIT_SHA>
|
git cherry-pick <PR503_COMMIT_SHA>
|
||||||
git cherry-pick <PR504_COMMIT_SHA>
|
git cherry-pick <PR504_COMMIT_SHA>
|
||||||
# ... etc ...
|
# ... etc ...
|
||||||
|
git diff origin/canary
|
||||||
yarn publish-stable
|
yarn publish-stable
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -41,3 +46,7 @@ After running this publish step, GitHub Actions will take care of publishing the
|
|||||||
If for some reason GitHub Actions fails to publish the npm package, you may do so
|
If for some reason GitHub Actions fails to publish the npm package, you may do so
|
||||||
manually by running `npm publish` from the package directory. Make sure to
|
manually by running `npm publish` from the package directory. Make sure to
|
||||||
use `npm publish --tag canary` if you are publishing a canary release!
|
use `npm publish --tag canary` if you are publishing a canary release!
|
||||||
|
|
||||||
|
### Contributing
|
||||||
|
|
||||||
|
See the [Contribution guidelines for this project](CONTRIBUTING.md), it also contains guidance on interpreting tests failures.
|
||||||
|
|||||||
@@ -1,31 +1,29 @@
|
|||||||
const childProcess = require('child_process');
|
const { execSync } = require('child_process');
|
||||||
const path = require('path');
|
const { relative } = require('path');
|
||||||
|
|
||||||
const command = 'git diff HEAD~1 --name-only';
|
const branch = execSync('git branch | grep "*" | cut -d " " -f2')
|
||||||
const diff = childProcess.execSync(command).toString();
|
.toString()
|
||||||
|
.trim();
|
||||||
|
console.log(`Running tests on branch "${branch}"`);
|
||||||
|
const gitPath = branch === 'master' ? 'HEAD~1' : 'origin/canary...HEAD';
|
||||||
|
const diff = execSync(`git diff ${gitPath} --name-only`).toString();
|
||||||
|
|
||||||
const changed = diff
|
const changed = diff
|
||||||
.split('\n')
|
.split('\n')
|
||||||
.filter(item => Boolean(item) && item.includes('packages/'))
|
.filter(item => Boolean(item) && item.includes('packages/'))
|
||||||
.map(item => path.relative('packages', item).split('/')[0]);
|
.map(item => relative('packages', item).split('/')[0]);
|
||||||
|
|
||||||
const matches = [];
|
const matches = Array.from(new Set(changed));
|
||||||
|
|
||||||
if (changed.length > 0) {
|
if (matches.length === 0) {
|
||||||
console.log('The following packages have changed:');
|
|
||||||
|
|
||||||
changed.map((item) => {
|
|
||||||
matches.push(item);
|
|
||||||
console.log(item);
|
|
||||||
|
|
||||||
return null;
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
matches.push('now-node');
|
matches.push('now-node');
|
||||||
console.log(`No packages changed, defaulting to ${matches[0]}`);
|
console.log(`No packages changed, defaulting to ${matches[0]}`);
|
||||||
|
} else {
|
||||||
|
console.log('The following packages have changed:');
|
||||||
|
console.log(matches.join('\n'));
|
||||||
}
|
}
|
||||||
|
|
||||||
const testMatch = Array.from(new Set(matches)).map(
|
const testMatch = matches.map(
|
||||||
item => `**/${item}/**/?(*.)+(spec|test).[jt]s?(x)`,
|
item => `**/${item}/**/?(*.)+(spec|test).[jt]s?(x)`,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
@@ -12,8 +12,8 @@
|
|||||||
"scripts": {
|
"scripts": {
|
||||||
"lerna": "lerna",
|
"lerna": "lerna",
|
||||||
"bootstrap": "lerna bootstrap",
|
"bootstrap": "lerna bootstrap",
|
||||||
"publish-stable": "git checkout master && git pull && lerna version",
|
"publish-stable": "git checkout master && git pull && lerna version --exact",
|
||||||
"publish-canary": "git checkout canary && git pull && lerna version prerelease --preid canary",
|
"publish-canary": "git checkout canary && git pull && lerna version prerelease --preid canary --exact",
|
||||||
"publish-from-github": "./.circleci/publish.sh",
|
"publish-from-github": "./.circleci/publish.sh",
|
||||||
"build": "./.circleci/build.sh",
|
"build": "./.circleci/build.sh",
|
||||||
"lint": "eslint .",
|
"lint": "eslint .",
|
||||||
@@ -33,6 +33,10 @@
|
|||||||
"prettier --write",
|
"prettier --write",
|
||||||
"git add"
|
"git add"
|
||||||
],
|
],
|
||||||
|
"*.json": [
|
||||||
|
"prettier --write",
|
||||||
|
"git add"
|
||||||
|
],
|
||||||
"*.md": [
|
"*.md": [
|
||||||
"prettier --write",
|
"prettier --write",
|
||||||
"git add"
|
"git add"
|
||||||
|
|||||||
@@ -13,6 +13,15 @@ exports.config = {
|
|||||||
maxLambdaSize: '10mb',
|
maxLambdaSize: '10mb',
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// From this list: https://import.pw/importpw/import/docs/config.md
|
||||||
|
const allowedConfigImports = new Set([
|
||||||
|
'CACHE',
|
||||||
|
'CURL_OPTS',
|
||||||
|
'DEBUG',
|
||||||
|
'RELOAD',
|
||||||
|
'SERVER',
|
||||||
|
]);
|
||||||
|
|
||||||
exports.analyze = ({ files, entrypoint }) => files[entrypoint].digest;
|
exports.analyze = ({ files, entrypoint }) => files[entrypoint].digest;
|
||||||
|
|
||||||
exports.build = async ({
|
exports.build = async ({
|
||||||
@@ -24,10 +33,23 @@ exports.build = async ({
|
|||||||
await download(files, srcDir);
|
await download(files, srcDir);
|
||||||
|
|
||||||
const configEnv = Object.keys(config).reduce((o, v) => {
|
const configEnv = Object.keys(config).reduce((o, v) => {
|
||||||
o[`IMPORT_${snakeCase(v).toUpperCase()}`] = config[v]; // eslint-disable-line no-param-reassign
|
const name = snakeCase(v).toUpperCase();
|
||||||
|
|
||||||
|
if (allowedConfigImports.has(name)) {
|
||||||
|
o[`IMPORT_${name}`] = config[v]; // eslint-disable-line no-param-reassign
|
||||||
|
}
|
||||||
|
|
||||||
return o;
|
return o;
|
||||||
}, {});
|
}, {});
|
||||||
|
|
||||||
|
if (config && config.import) {
|
||||||
|
Object.keys(config.import).forEach((key) => {
|
||||||
|
const name = snakeCase(key).toUpperCase();
|
||||||
|
// eslint-disable-next-line no-param-reassign
|
||||||
|
configEnv[`IMPORT_${name}`] = config.import[key];
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
const IMPORT_CACHE = `${workPath}/.import-cache`;
|
const IMPORT_CACHE = `${workPath}/.import-cache`;
|
||||||
const env = Object.assign({}, process.env, configEnv, {
|
const env = Object.assign({}, process.env, configEnv, {
|
||||||
PATH: `${IMPORT_CACHE}/bin:${process.env.PATH}`,
|
PATH: `${IMPORT_CACHE}/bin:${process.env.PATH}`,
|
||||||
|
|||||||
@@ -1,10 +1,11 @@
|
|||||||
{
|
{
|
||||||
"name": "@now/bash",
|
"name": "@now/bash",
|
||||||
"version": "0.2.3",
|
"version": "1.0.0",
|
||||||
"description": "Now 2.0 builder for HTTP endpoints written in Bash",
|
"description": "Now 2.0 builder for HTTP endpoints written in Bash",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"author": "Nathan Rajlich <nate@zeit.co>",
|
"author": "Nathan Rajlich <nate@zeit.co>",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
"homepage": "https://zeit.co/docs/v2/deployments/official-builders/bash-now-bash",
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "https://github.com/zeit/now-builders.git",
|
"url": "https://github.com/zeit/now-builders.git",
|
||||||
|
|||||||
@@ -1,16 +1,21 @@
|
|||||||
{
|
{
|
||||||
"name": "@now/build-utils",
|
"name": "@now/build-utils",
|
||||||
"version": "0.5.6",
|
"version": "0.8.0",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"main": "./dist/index.js",
|
"main": "./dist/index.js",
|
||||||
"types": "./dist/index.d.js",
|
"types": "./dist/index.d.js",
|
||||||
|
"homepage": "https://zeit.co/docs/v2/deployments/builders/developer-guide",
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "https://github.com/zeit/now-builders.git",
|
"url": "https://github.com/zeit/now-builders.git",
|
||||||
"directory": "packages/now-build-utils"
|
"directory": "packages/now-build-utils"
|
||||||
},
|
},
|
||||||
|
"scripts": {
|
||||||
|
"build": "tsc",
|
||||||
|
"test": "tsc && jest",
|
||||||
|
"prepublishOnly": "tsc"
|
||||||
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@types/cross-spawn": "6.0.0",
|
|
||||||
"async-retry": "1.2.3",
|
"async-retry": "1.2.3",
|
||||||
"async-sema": "2.1.4",
|
"async-sema": "2.1.4",
|
||||||
"cross-spawn": "6.0.5",
|
"cross-spawn": "6.0.5",
|
||||||
@@ -18,24 +23,22 @@
|
|||||||
"fs-extra": "7.0.0",
|
"fs-extra": "7.0.0",
|
||||||
"glob": "7.1.3",
|
"glob": "7.1.3",
|
||||||
"into-stream": "5.0.0",
|
"into-stream": "5.0.0",
|
||||||
"memory-fs": "0.4.1",
|
"minimatch": "3.0.4",
|
||||||
"multistream": "2.1.1",
|
"multistream": "2.1.1",
|
||||||
"node-fetch": "2.2.0",
|
"node-fetch": "2.2.0",
|
||||||
|
"semver": "6.1.1",
|
||||||
"yazl": "2.4.3"
|
"yazl": "2.4.3"
|
||||||
},
|
},
|
||||||
"scripts": {
|
|
||||||
"build": "tsc",
|
|
||||||
"test": "tsc && jest",
|
|
||||||
"prepublish": "tsc"
|
|
||||||
},
|
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/async-retry": "^1.2.1",
|
"@types/async-retry": "^1.2.1",
|
||||||
|
"@types/cross-spawn": "6.0.0",
|
||||||
"@types/end-of-stream": "^1.4.0",
|
"@types/end-of-stream": "^1.4.0",
|
||||||
"@types/fs-extra": "^5.0.5",
|
"@types/fs-extra": "^5.0.5",
|
||||||
"@types/glob": "^7.1.1",
|
"@types/glob": "^7.1.1",
|
||||||
"@types/node-fetch": "^2.1.6",
|
"@types/node-fetch": "^2.1.6",
|
||||||
|
"@types/semver": "6.0.0",
|
||||||
"@types/yazl": "^2.4.1",
|
"@types/yazl": "^2.4.1",
|
||||||
"execa": "^1.0.0",
|
"execa": "^1.0.0",
|
||||||
"typescript": "3.3.4000"
|
"typescript": "3.5.2"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
47
packages/now-build-utils/src/detect-builder.ts
Normal file
47
packages/now-build-utils/src/detect-builder.ts
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
import { PackageJson, Builder, Config } from './types';
|
||||||
|
import minimatch from 'minimatch';
|
||||||
|
|
||||||
|
const src: string = 'package.json';
|
||||||
|
const config: Config = { zeroConfig: true };
|
||||||
|
|
||||||
|
// Static builders are special cased in `@now/static-build`
|
||||||
|
const BUILDERS = new Map<string, Builder>([
|
||||||
|
['next', { src, use: '@now/next', config }],
|
||||||
|
]);
|
||||||
|
|
||||||
|
const API_BUILDERS: Builder[] = [
|
||||||
|
{ src: 'api/**/*.js', use: '@now/node', config },
|
||||||
|
{ src: 'api/**/*.ts', use: '@now/node', config },
|
||||||
|
{ src: 'api/**/*.rs', use: '@now/rust', config },
|
||||||
|
{ src: 'api/**/*.go', use: '@now/go', config },
|
||||||
|
{ src: 'api/**/*.php', use: '@now/php', config },
|
||||||
|
{ src: 'api/**/*.py', use: '@now/python', config },
|
||||||
|
{ src: 'api/**/*.rb', use: '@now/ruby', config },
|
||||||
|
{ src: 'api/**/*.sh', use: '@now/bash', config },
|
||||||
|
];
|
||||||
|
|
||||||
|
export async function detectBuilder(pkg: PackageJson): Promise<Builder> {
|
||||||
|
for (const [dependency, builder] of BUILDERS) {
|
||||||
|
const deps = Object.assign({}, pkg.dependencies, pkg.devDependencies);
|
||||||
|
|
||||||
|
// Return the builder when a dependency matches
|
||||||
|
if (deps[dependency]) {
|
||||||
|
return builder;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// By default we'll choose the `static-build` builder
|
||||||
|
return { src, use: '@now/static-build', config };
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function detectApiBuilders(
|
||||||
|
files: string[]
|
||||||
|
): Promise<Builder[] | null> {
|
||||||
|
const builds = files.map(file => {
|
||||||
|
return API_BUILDERS.find(({ src }): boolean => minimatch(file, src));
|
||||||
|
});
|
||||||
|
|
||||||
|
// We can use `new Set` here since `builds` contains references to `API_BUILDERS`
|
||||||
|
const finishedBuilds = Array.from(new Set(builds.filter(Boolean)));
|
||||||
|
return finishedBuilds.length > 0 ? (finishedBuilds as Builder[]) : null;
|
||||||
|
}
|
||||||
239
packages/now-build-utils/src/detect-routes.ts
Normal file
239
packages/now-build-utils/src/detect-routes.ts
Normal file
@@ -0,0 +1,239 @@
|
|||||||
|
import path from 'path';
|
||||||
|
import { Route } from './types';
|
||||||
|
|
||||||
|
function concatArrayOfText(texts: string[]): string {
|
||||||
|
const last = texts.pop();
|
||||||
|
return `${texts.join(', ')}, and ${last}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Takes a filename or foldername, strips the extension
|
||||||
|
// gets the part between the "[]" brackets.
|
||||||
|
// It will return `null` if there are no brackets
|
||||||
|
// and therefore no segment.
|
||||||
|
function getSegmentName(segment: string): string | null {
|
||||||
|
const { name } = path.parse(segment);
|
||||||
|
|
||||||
|
if (name.startsWith('[') && name.endsWith(']')) {
|
||||||
|
return name.slice(1, -1);
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
function createRouteFromPath(filePath: string): Route {
|
||||||
|
const parts = filePath.split('/');
|
||||||
|
|
||||||
|
let append: string = '';
|
||||||
|
let counter: number = 1;
|
||||||
|
const query: string[] = [];
|
||||||
|
|
||||||
|
const srcParts = parts.map(
|
||||||
|
(segment, index): string => {
|
||||||
|
const name = getSegmentName(segment);
|
||||||
|
const isLast = index === parts.length - 1;
|
||||||
|
|
||||||
|
if (name !== null) {
|
||||||
|
query.push(`${name}=$${counter++}`);
|
||||||
|
|
||||||
|
if (isLast) {
|
||||||
|
// We append this to the last one
|
||||||
|
// to make sure GET params still work
|
||||||
|
// and are just appended to our GET params
|
||||||
|
append += `$${counter++}`;
|
||||||
|
return `([^\\/|\\?]+)\\/?(?:\\?(.*))?`;
|
||||||
|
}
|
||||||
|
|
||||||
|
return `([^\\/]+)`;
|
||||||
|
} else if (isLast) {
|
||||||
|
// If it is the last part we want to remove the extension
|
||||||
|
// and capture everything after that as regex group and append it
|
||||||
|
const { name: fileName } = path.parse(segment);
|
||||||
|
append += `$${counter++}`;
|
||||||
|
return `${fileName}(.*)`;
|
||||||
|
}
|
||||||
|
|
||||||
|
return segment;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
const src = `^/${srcParts.join('/')}$`;
|
||||||
|
const dest = `/${filePath}${query.length ? '?' : ''}${query.join('&')}${
|
||||||
|
query.length ? '&' : ''
|
||||||
|
}${append}`;
|
||||||
|
|
||||||
|
return { src, dest };
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if the path partially matches and has the same
|
||||||
|
// name for the path segment at the same position
|
||||||
|
function partiallyMatches(pathA: string, pathB: string): boolean {
|
||||||
|
const partsA = pathA.split('/');
|
||||||
|
const partsB = pathB.split('/');
|
||||||
|
|
||||||
|
const long = partsA.length > partsB.length ? partsA : partsB;
|
||||||
|
const short = long === partsA ? partsB : partsA;
|
||||||
|
|
||||||
|
let index = 0;
|
||||||
|
|
||||||
|
for (const segmentShort of short) {
|
||||||
|
const segmentLong = long[index];
|
||||||
|
|
||||||
|
const nameLong = getSegmentName(segmentLong);
|
||||||
|
const nameShort = getSegmentName(segmentShort);
|
||||||
|
|
||||||
|
// If there are no segments or the paths differ we
|
||||||
|
// return as they are not matching
|
||||||
|
if (segmentShort !== segmentLong && (!nameLong || !nameShort)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (nameLong !== nameShort) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
index += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Counts how often a path occurres when all placeholders
|
||||||
|
// got resolved, so we can check if they have conflicts
|
||||||
|
function pathOccurrences(filePath: string, files: string[]): string[] {
|
||||||
|
const getAbsolutePath = (unresolvedPath: string): string => {
|
||||||
|
const { dir, name } = path.parse(unresolvedPath);
|
||||||
|
const parts = path.join(dir, name).split('/');
|
||||||
|
return parts.map(part => part.replace(/\[.*\]/, '1')).join('/');
|
||||||
|
};
|
||||||
|
|
||||||
|
const currentAbsolutePath = getAbsolutePath(filePath);
|
||||||
|
|
||||||
|
return files.reduce((prev: string[], file: string): string[] => {
|
||||||
|
const absolutePath = getAbsolutePath(file);
|
||||||
|
|
||||||
|
if (absolutePath === currentAbsolutePath) {
|
||||||
|
prev.push(file);
|
||||||
|
} else if (partiallyMatches(filePath, file)) {
|
||||||
|
prev.push(file);
|
||||||
|
}
|
||||||
|
|
||||||
|
return prev;
|
||||||
|
}, []);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Checks if a placeholder with the same name is used
|
||||||
|
// multiple times inside the same path
|
||||||
|
function getConflictingSegment(filePath: string): string | null {
|
||||||
|
const segments = new Set<string>();
|
||||||
|
|
||||||
|
for (const segment of filePath.split('/')) {
|
||||||
|
const name = getSegmentName(segment);
|
||||||
|
|
||||||
|
if (name !== null && segments.has(name)) {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (name) {
|
||||||
|
segments.add(name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
function sortFilesBySegmentCount(fileA: string, fileB: string): number {
|
||||||
|
const lengthA = fileA.split('/').length;
|
||||||
|
const lengthB = fileB.split('/').length;
|
||||||
|
|
||||||
|
if (lengthA > lengthB) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (lengthA < lengthB) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Paths that have the same segment length but
|
||||||
|
// less placeholders are preferred
|
||||||
|
const countSegments = (prev: number, segment: string) =>
|
||||||
|
getSegmentName(segment) ? prev + 1 : 0;
|
||||||
|
const segmentLengthA = fileA.split('/').reduce(countSegments, 0);
|
||||||
|
const segmentLengthB = fileB.split('/').reduce(countSegments, 0);
|
||||||
|
|
||||||
|
if (segmentLengthA > segmentLengthB) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (segmentLengthA < segmentLengthB) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function detectApiRoutes(
|
||||||
|
files: string[]
|
||||||
|
): Promise<{
|
||||||
|
defaultRoutes: Route[] | null;
|
||||||
|
error: { [key: string]: string } | null;
|
||||||
|
}> {
|
||||||
|
if (!files || files.length === 0) {
|
||||||
|
return { defaultRoutes: null, error: null };
|
||||||
|
}
|
||||||
|
|
||||||
|
// The deepest routes need to be
|
||||||
|
// the first ones to get handled
|
||||||
|
const sortedFiles = files.sort(sortFilesBySegmentCount);
|
||||||
|
|
||||||
|
const defaultRoutes: Route[] = [];
|
||||||
|
|
||||||
|
for (const file of sortedFiles) {
|
||||||
|
// We only consider every file in the api directory
|
||||||
|
// as we will strip extensions as well as resolving "[segments]"
|
||||||
|
if (!file.startsWith('api/')) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
const conflictingSegment = getConflictingSegment(file);
|
||||||
|
|
||||||
|
console.log({ file, conflictingSegment });
|
||||||
|
|
||||||
|
if (conflictingSegment) {
|
||||||
|
return {
|
||||||
|
defaultRoutes: null,
|
||||||
|
error: {
|
||||||
|
code: 'conflicting_path_segment',
|
||||||
|
message:
|
||||||
|
`The segment "${conflictingSegment}" occurres more than ` +
|
||||||
|
`one time in your path "${file}". Please make sure that ` +
|
||||||
|
`every segment in a path is unique`,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const occurrences = pathOccurrences(file, sortedFiles).filter(
|
||||||
|
name => name !== file
|
||||||
|
);
|
||||||
|
|
||||||
|
if (occurrences.length > 0) {
|
||||||
|
const messagePaths = concatArrayOfText(
|
||||||
|
occurrences.map(name => `"${name}"`)
|
||||||
|
);
|
||||||
|
|
||||||
|
return {
|
||||||
|
defaultRoutes: null,
|
||||||
|
error: {
|
||||||
|
code: 'conflicting_file_path',
|
||||||
|
message:
|
||||||
|
`Two or more files have conflicting paths or names. ` +
|
||||||
|
`Please make sure path segments and filenames, without their extension, are unique. ` +
|
||||||
|
`The path "${file}" has conflicts with ${messagePaths}`,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
defaultRoutes.push(createRouteFromPath(file));
|
||||||
|
}
|
||||||
|
|
||||||
|
return { defaultRoutes, error: null };
|
||||||
|
}
|
||||||
@@ -39,10 +39,10 @@ export default async function download(
|
|||||||
basePath: string,
|
basePath: string,
|
||||||
meta?: Meta
|
meta?: Meta
|
||||||
): Promise<DownloadedFiles> {
|
): Promise<DownloadedFiles> {
|
||||||
const { isDev = false, filesChanged = null, filesRemoved = null } =
|
const { isDev = false, skipDownload = false, filesChanged = null, filesRemoved = null } =
|
||||||
meta || {};
|
meta || {};
|
||||||
|
|
||||||
if (isDev) {
|
if (isDev || skipDownload) {
|
||||||
// In `now dev`, the `download()` function is a no-op because
|
// In `now dev`, the `download()` function is a no-op because
|
||||||
// the `basePath` matches the `cwd` of the dev server, so the
|
// the `basePath` matches the `cwd` of the dev server, so the
|
||||||
// source files are already available.
|
// source files are already available.
|
||||||
|
|||||||
53
packages/now-build-utils/src/fs/node-version.ts
Normal file
53
packages/now-build-utils/src/fs/node-version.ts
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
import { intersects } from 'semver';
|
||||||
|
import { NodeVersion } from '../types';
|
||||||
|
|
||||||
|
const supportedOptions: NodeVersion[] = [
|
||||||
|
{ major: 10, range: '10.x', runtime: 'nodejs10.x' },
|
||||||
|
{ major: 8, range: '8.10.x', runtime: 'nodejs8.10' },
|
||||||
|
];
|
||||||
|
|
||||||
|
// This version should match Fargate's default in the PATH
|
||||||
|
// Today that is Node 8
|
||||||
|
export const defaultSelection = supportedOptions.find(
|
||||||
|
o => o.major === 8
|
||||||
|
) as NodeVersion;
|
||||||
|
|
||||||
|
export async function getSupportedNodeVersion(
|
||||||
|
engineRange?: string,
|
||||||
|
silent?: boolean
|
||||||
|
): Promise<NodeVersion> {
|
||||||
|
let selection = defaultSelection;
|
||||||
|
|
||||||
|
if (!engineRange) {
|
||||||
|
if (!silent) {
|
||||||
|
console.log(
|
||||||
|
'missing `engines` in `package.json`, using default range: ' +
|
||||||
|
selection.range
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
const found = supportedOptions.some(o => {
|
||||||
|
// the array is already in order so return the first
|
||||||
|
// match which will be the newest version of node
|
||||||
|
selection = o;
|
||||||
|
return intersects(o.range, engineRange);
|
||||||
|
});
|
||||||
|
if (found) {
|
||||||
|
if (!silent) {
|
||||||
|
console.log(
|
||||||
|
'Found `engines` in `package.json`, selecting range: ' +
|
||||||
|
selection.range
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (!silent) {
|
||||||
|
throw new Error(
|
||||||
|
'found `engines` in `package.json` with an unsupported node range: ' +
|
||||||
|
engineRange +
|
||||||
|
'\nplease use `10.x` or `8.10.x` instead'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return selection;
|
||||||
|
}
|
||||||
@@ -3,6 +3,9 @@ import fs from 'fs-extra';
|
|||||||
import path from 'path';
|
import path from 'path';
|
||||||
import spawn from 'cross-spawn';
|
import spawn from 'cross-spawn';
|
||||||
import { SpawnOptions } from 'child_process';
|
import { SpawnOptions } from 'child_process';
|
||||||
|
import { deprecate } from 'util';
|
||||||
|
import { Meta, PackageJson, NodeVersion } from '../types';
|
||||||
|
import { getSupportedNodeVersion } from './node-version';
|
||||||
|
|
||||||
function spawnAsync(
|
function spawnAsync(
|
||||||
command: string,
|
command: string,
|
||||||
@@ -43,19 +46,49 @@ async function chmodPlusX(fsPath: string) {
|
|||||||
await fs.chmod(fsPath, base8);
|
await fs.chmod(fsPath, base8);
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function runShellScript(fsPath: string) {
|
export async function runShellScript(
|
||||||
|
fsPath: string,
|
||||||
|
args: string[] = [],
|
||||||
|
spawnOpts?: SpawnOptions
|
||||||
|
) {
|
||||||
assert(path.isAbsolute(fsPath));
|
assert(path.isAbsolute(fsPath));
|
||||||
const destPath = path.dirname(fsPath);
|
const destPath = path.dirname(fsPath);
|
||||||
await chmodPlusX(fsPath);
|
await chmodPlusX(fsPath);
|
||||||
await spawnAsync(`./${path.basename(fsPath)}`, [], destPath);
|
await spawnAsync(`./${path.basename(fsPath)}`, args, destPath, spawnOpts);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function scanParentDirs(destPath: string, scriptName?: string) {
|
export function getSpawnOptions(
|
||||||
|
meta: Meta,
|
||||||
|
nodeVersion: NodeVersion
|
||||||
|
): SpawnOptions {
|
||||||
|
const opts = {
|
||||||
|
env: { ...process.env },
|
||||||
|
};
|
||||||
|
|
||||||
|
if (!meta.isDev) {
|
||||||
|
opts.env.PATH = `/node${nodeVersion.major}/bin:${opts.env.PATH}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
return opts;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function getNodeVersion(
|
||||||
|
destPath: string,
|
||||||
|
minNodeVersion?: string
|
||||||
|
): Promise<NodeVersion> {
|
||||||
|
const { packageJson } = await scanParentDirs(destPath, true);
|
||||||
|
const range =
|
||||||
|
(packageJson && packageJson.engines && packageJson.engines.node) ||
|
||||||
|
minNodeVersion;
|
||||||
|
return getSupportedNodeVersion(range, typeof minNodeVersion !== 'undefined');
|
||||||
|
}
|
||||||
|
|
||||||
|
async function scanParentDirs(destPath: string, readPackageJson = false) {
|
||||||
assert(path.isAbsolute(destPath));
|
assert(path.isAbsolute(destPath));
|
||||||
|
|
||||||
let hasScript = false;
|
|
||||||
let hasPackageLockJson = false;
|
let hasPackageLockJson = false;
|
||||||
|
let packageJson: PackageJson | undefined;
|
||||||
let currentDestPath = destPath;
|
let currentDestPath = destPath;
|
||||||
|
|
||||||
// eslint-disable-next-line no-constant-condition
|
// eslint-disable-next-line no-constant-condition
|
||||||
@@ -64,12 +97,9 @@ async function scanParentDirs(destPath: string, scriptName?: string) {
|
|||||||
// eslint-disable-next-line no-await-in-loop
|
// eslint-disable-next-line no-await-in-loop
|
||||||
if (await fs.pathExists(packageJsonPath)) {
|
if (await fs.pathExists(packageJsonPath)) {
|
||||||
// eslint-disable-next-line no-await-in-loop
|
// eslint-disable-next-line no-await-in-loop
|
||||||
const packageJson = JSON.parse(
|
if (readPackageJson) {
|
||||||
await fs.readFile(packageJsonPath, 'utf8')
|
packageJson = JSON.parse(await fs.readFile(packageJsonPath, 'utf8'));
|
||||||
);
|
}
|
||||||
hasScript = Boolean(
|
|
||||||
packageJson.scripts && scriptName && packageJson.scripts[scriptName]
|
|
||||||
);
|
|
||||||
// eslint-disable-next-line no-await-in-loop
|
// eslint-disable-next-line no-await-in-loop
|
||||||
hasPackageLockJson = await fs.pathExists(
|
hasPackageLockJson = await fs.pathExists(
|
||||||
path.join(currentDestPath, 'package-lock.json')
|
path.join(currentDestPath, 'package-lock.json')
|
||||||
@@ -82,12 +112,13 @@ async function scanParentDirs(destPath: string, scriptName?: string) {
|
|||||||
currentDestPath = newDestPath;
|
currentDestPath = newDestPath;
|
||||||
}
|
}
|
||||||
|
|
||||||
return { hasScript, hasPackageLockJson };
|
return { hasPackageLockJson, packageJson };
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function installDependencies(
|
export async function runNpmInstall(
|
||||||
destPath: string,
|
destPath: string,
|
||||||
args: string[] = []
|
args: string[] = [],
|
||||||
|
spawnOpts?: SpawnOptions
|
||||||
) {
|
) {
|
||||||
assert(path.isAbsolute(destPath));
|
assert(path.isAbsolute(destPath));
|
||||||
|
|
||||||
@@ -95,30 +126,28 @@ export async function installDependencies(
|
|||||||
console.log(`installing to ${destPath}`);
|
console.log(`installing to ${destPath}`);
|
||||||
const { hasPackageLockJson } = await scanParentDirs(destPath);
|
const { hasPackageLockJson } = await scanParentDirs(destPath);
|
||||||
|
|
||||||
const opts = {
|
const opts = spawnOpts || { env: process.env };
|
||||||
env: {
|
|
||||||
...process.env,
|
|
||||||
// This is a little hack to force `node-gyp` to build for the
|
|
||||||
// Node.js version that `@now/node` and `@now/node-server` use
|
|
||||||
npm_config_target: '8.10.0',
|
|
||||||
},
|
|
||||||
stdio: 'pipe',
|
|
||||||
};
|
|
||||||
|
|
||||||
if (hasPackageLockJson) {
|
if (hasPackageLockJson) {
|
||||||
commandArgs = args.filter(a => a !== '--prefer-offline');
|
commandArgs = args.filter(a => a !== '--prefer-offline');
|
||||||
await spawnAsync(
|
await spawnAsync(
|
||||||
'npm',
|
'npm',
|
||||||
['install', '--unsafe-perm'].concat(commandArgs),
|
commandArgs.concat(['install', '--unsafe-perm']),
|
||||||
destPath,
|
destPath,
|
||||||
opts as SpawnOptions
|
opts
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
await spawnAsync(
|
await spawnAsync(
|
||||||
'yarn',
|
'yarn',
|
||||||
['--ignore-engines', '--cwd', destPath].concat(commandArgs),
|
commandArgs.concat([
|
||||||
|
'--ignore-engines',
|
||||||
|
'--mutex',
|
||||||
|
'network',
|
||||||
|
'--cwd',
|
||||||
destPath,
|
destPath,
|
||||||
opts as SpawnOptions
|
]),
|
||||||
|
destPath,
|
||||||
|
opts
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -129,9 +158,15 @@ export async function runPackageJsonScript(
|
|||||||
opts?: SpawnOptions
|
opts?: SpawnOptions
|
||||||
) {
|
) {
|
||||||
assert(path.isAbsolute(destPath));
|
assert(path.isAbsolute(destPath));
|
||||||
const { hasScript, hasPackageLockJson } = await scanParentDirs(
|
const { packageJson, hasPackageLockJson } = await scanParentDirs(
|
||||||
destPath,
|
destPath,
|
||||||
scriptName
|
true
|
||||||
|
);
|
||||||
|
const hasScript = Boolean(
|
||||||
|
packageJson &&
|
||||||
|
packageJson.scripts &&
|
||||||
|
scriptName &&
|
||||||
|
packageJson.scripts[scriptName]
|
||||||
);
|
);
|
||||||
if (!hasScript) return false;
|
if (!hasScript) return false;
|
||||||
|
|
||||||
@@ -151,4 +186,11 @@ export async function runPackageJsonScript(
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const runNpmInstall = installDependencies;
|
/**
|
||||||
|
* @deprecate installDependencies() is deprecated.
|
||||||
|
* Please use runNpmInstall() instead.
|
||||||
|
*/
|
||||||
|
export const installDependencies = deprecate(
|
||||||
|
runNpmInstall,
|
||||||
|
'installDependencies() is deprecated. Please use runNpmInstall() instead.'
|
||||||
|
);
|
||||||
|
|||||||
@@ -1,15 +1,6 @@
|
|||||||
import FileBlob from './file-blob';
|
import FileBlob from './file-blob';
|
||||||
import FileFsRef from './file-fs-ref';
|
import FileFsRef from './file-fs-ref';
|
||||||
import FileRef from './file-ref';
|
import FileRef from './file-ref';
|
||||||
import {
|
|
||||||
File,
|
|
||||||
Files,
|
|
||||||
AnalyzeOptions,
|
|
||||||
BuildOptions,
|
|
||||||
PrepareCacheOptions,
|
|
||||||
ShouldServeOptions,
|
|
||||||
Meta,
|
|
||||||
} from './types';
|
|
||||||
import { Lambda, createLambda } from './lambda';
|
import { Lambda, createLambda } from './lambda';
|
||||||
import download, { DownloadedFiles } from './fs/download';
|
import download, { DownloadedFiles } from './fs/download';
|
||||||
import getWriteableDirectory from './fs/get-writable-directory';
|
import getWriteableDirectory from './fs/get-writable-directory';
|
||||||
@@ -20,17 +11,18 @@ import {
|
|||||||
runPackageJsonScript,
|
runPackageJsonScript,
|
||||||
runNpmInstall,
|
runNpmInstall,
|
||||||
runShellScript,
|
runShellScript,
|
||||||
|
getNodeVersion,
|
||||||
|
getSpawnOptions,
|
||||||
} from './fs/run-user-scripts';
|
} from './fs/run-user-scripts';
|
||||||
import streamToBuffer from './fs/stream-to-buffer';
|
import streamToBuffer from './fs/stream-to-buffer';
|
||||||
import shouldServe from './should-serve';
|
import shouldServe from './should-serve';
|
||||||
|
import { detectBuilder, detectApiBuilders } from './detect-builder';
|
||||||
|
import { detectApiRoutes } from './detect-routes';
|
||||||
|
|
||||||
export {
|
export {
|
||||||
FileBlob,
|
FileBlob,
|
||||||
FileFsRef,
|
FileFsRef,
|
||||||
FileRef,
|
FileRef,
|
||||||
Files,
|
|
||||||
File,
|
|
||||||
Meta,
|
|
||||||
Lambda,
|
Lambda,
|
||||||
createLambda,
|
createLambda,
|
||||||
download,
|
download,
|
||||||
@@ -42,10 +34,13 @@ export {
|
|||||||
runPackageJsonScript,
|
runPackageJsonScript,
|
||||||
runNpmInstall,
|
runNpmInstall,
|
||||||
runShellScript,
|
runShellScript,
|
||||||
|
getNodeVersion,
|
||||||
|
getSpawnOptions,
|
||||||
streamToBuffer,
|
streamToBuffer,
|
||||||
AnalyzeOptions,
|
|
||||||
BuildOptions,
|
|
||||||
PrepareCacheOptions,
|
|
||||||
ShouldServeOptions,
|
|
||||||
shouldServe,
|
shouldServe,
|
||||||
|
detectBuilder,
|
||||||
|
detectApiBuilders,
|
||||||
|
detectApiRoutes,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export * from './types';
|
||||||
|
|||||||
@@ -15,12 +15,38 @@ export interface Files {
|
|||||||
[filePath: string]: File;
|
[filePath: string]: File;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface Config {
|
export interface Route {
|
||||||
|
src?: string;
|
||||||
|
dest?: string;
|
||||||
|
handle?: string;
|
||||||
|
type?: string;
|
||||||
|
headers?: {
|
||||||
[key: string]: string;
|
[key: string]: string;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface Config {
|
||||||
|
[key: string]:
|
||||||
|
| string
|
||||||
|
| string[]
|
||||||
|
| boolean
|
||||||
|
| number
|
||||||
|
| { [key: string]: string }
|
||||||
|
| undefined;
|
||||||
|
maxLambdaSize?: string;
|
||||||
|
includeFiles?: string | string[];
|
||||||
|
bundle?: boolean;
|
||||||
|
ldsflags?: string;
|
||||||
|
helpers?: boolean;
|
||||||
|
rust?: string;
|
||||||
|
debug?: boolean;
|
||||||
|
zeroConfig?: boolean;
|
||||||
|
import?: { [key: string]: string };
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface Meta {
|
export interface Meta {
|
||||||
isDev?: boolean;
|
isDev?: boolean;
|
||||||
|
skipDownload?: boolean;
|
||||||
requestPath?: string;
|
requestPath?: string;
|
||||||
filesChanged?: string[];
|
filesChanged?: string[];
|
||||||
filesRemoved?: string[];
|
filesRemoved?: string[];
|
||||||
@@ -155,3 +181,34 @@ export interface ShouldServeOptions {
|
|||||||
*/
|
*/
|
||||||
config: Config;
|
config: Config;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface PackageJson {
|
||||||
|
name: string;
|
||||||
|
version: string;
|
||||||
|
engines?: {
|
||||||
|
[key: string]: string;
|
||||||
|
node: string;
|
||||||
|
npm: string;
|
||||||
|
};
|
||||||
|
scripts?: {
|
||||||
|
[key: string]: string;
|
||||||
|
};
|
||||||
|
dependencies?: {
|
||||||
|
[key: string]: string;
|
||||||
|
};
|
||||||
|
devDependencies?: {
|
||||||
|
[key: string]: string;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface NodeVersion {
|
||||||
|
major: number;
|
||||||
|
range: string;
|
||||||
|
runtime: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface Builder {
|
||||||
|
use: string;
|
||||||
|
src: string;
|
||||||
|
config?: Config;
|
||||||
|
}
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
{
|
{
|
||||||
"src": "index.js",
|
"src": "index.js",
|
||||||
"use": "@now/node",
|
"use": "@now/node",
|
||||||
"config": { "maxLambdaSize": "15mb" }
|
"config": { "maxLambdaSize": "18mb" }
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"probes": [{ "path": "/", "mustContain": "found:RANDOMNESS_PLACEHOLDER" }]
|
"probes": [{ "path": "/", "mustContain": "found:RANDOMNESS_PLACEHOLDER" }]
|
||||||
|
|||||||
@@ -6,12 +6,22 @@ const execa = require('execa');
|
|||||||
const assert = require('assert');
|
const assert = require('assert');
|
||||||
const { glob, download } = require('../');
|
const { glob, download } = require('../');
|
||||||
const { createZip } = require('../dist/lambda');
|
const { createZip } = require('../dist/lambda');
|
||||||
|
const {
|
||||||
|
getSupportedNodeVersion,
|
||||||
|
defaultSelection,
|
||||||
|
} = require('../dist/fs/node-version');
|
||||||
|
|
||||||
const {
|
const {
|
||||||
packAndDeploy,
|
packAndDeploy,
|
||||||
testDeployment,
|
testDeployment,
|
||||||
} = require('../../../test/lib/deployment/test-deployment.js');
|
} = require('../../../test/lib/deployment/test-deployment.js');
|
||||||
|
|
||||||
|
const {
|
||||||
|
detectBuilder,
|
||||||
|
detectApiBuilders,
|
||||||
|
detectApiRoutes,
|
||||||
|
} = require('../dist');
|
||||||
|
|
||||||
jest.setTimeout(4 * 60 * 1000);
|
jest.setTimeout(4 * 60 * 1000);
|
||||||
const builderUrl = '@canary';
|
const builderUrl = '@canary';
|
||||||
let buildUtilsUrl;
|
let buildUtilsUrl;
|
||||||
@@ -64,6 +74,52 @@ it('should create zip files with symlinks properly', async () => {
|
|||||||
assert(aStat.isFile());
|
assert(aStat.isFile());
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should only match supported node versions', () => {
|
||||||
|
expect(getSupportedNodeVersion('10.x')).resolves.toHaveProperty('major', 10);
|
||||||
|
expect(getSupportedNodeVersion('8.10.x')).resolves.toHaveProperty('major', 8);
|
||||||
|
expect(getSupportedNodeVersion('8.11.x')).rejects.toThrow();
|
||||||
|
expect(getSupportedNodeVersion('6.x')).rejects.toThrow();
|
||||||
|
expect(getSupportedNodeVersion('999.x')).rejects.toThrow();
|
||||||
|
expect(getSupportedNodeVersion('foo')).rejects.toThrow();
|
||||||
|
expect(getSupportedNodeVersion('')).resolves.toBe(defaultSelection);
|
||||||
|
expect(getSupportedNodeVersion(null)).resolves.toBe(defaultSelection);
|
||||||
|
expect(getSupportedNodeVersion(undefined)).resolves.toBe(defaultSelection);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should match all semver ranges', () => {
|
||||||
|
// See https://docs.npmjs.com/files/package.json#engines
|
||||||
|
expect(getSupportedNodeVersion('10.0.0')).resolves.toHaveProperty(
|
||||||
|
'major',
|
||||||
|
10,
|
||||||
|
);
|
||||||
|
expect(getSupportedNodeVersion('10.x')).resolves.toHaveProperty('major', 10);
|
||||||
|
expect(getSupportedNodeVersion('>=10')).resolves.toHaveProperty('major', 10);
|
||||||
|
expect(getSupportedNodeVersion('>=10.3.0')).resolves.toHaveProperty(
|
||||||
|
'major',
|
||||||
|
10,
|
||||||
|
);
|
||||||
|
expect(getSupportedNodeVersion('8.5.0 - 10.5.0')).resolves.toHaveProperty(
|
||||||
|
'major',
|
||||||
|
10,
|
||||||
|
);
|
||||||
|
expect(getSupportedNodeVersion('>=9.0.0')).resolves.toHaveProperty(
|
||||||
|
'major',
|
||||||
|
10,
|
||||||
|
);
|
||||||
|
expect(getSupportedNodeVersion('>=9.5.0 <=10.5.0')).resolves.toHaveProperty(
|
||||||
|
'major',
|
||||||
|
10,
|
||||||
|
);
|
||||||
|
expect(getSupportedNodeVersion('~10.5.0')).resolves.toHaveProperty(
|
||||||
|
'major',
|
||||||
|
10,
|
||||||
|
);
|
||||||
|
expect(getSupportedNodeVersion('^10.5.0')).resolves.toHaveProperty(
|
||||||
|
'major',
|
||||||
|
10,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
// own fixtures
|
// own fixtures
|
||||||
|
|
||||||
const fixturesPath = path.resolve(__dirname, 'fixtures');
|
const fixturesPath = path.resolve(__dirname, 'fixtures');
|
||||||
@@ -108,3 +164,84 @@ for (const builder of buildersToTestWith) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
it('Test `detectBuilder`', async () => {
|
||||||
|
{
|
||||||
|
const pkg = { dependencies: { next: '1.0.0' } };
|
||||||
|
const builder = await detectBuilder(pkg);
|
||||||
|
expect(builder.use).toBe('@now/next');
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
const pkg = { devDependencies: { next: '1.0.0' } };
|
||||||
|
const builder = await detectBuilder(pkg);
|
||||||
|
expect(builder.use).toBe('@now/next');
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
const pkg = {};
|
||||||
|
const builder = await detectBuilder(pkg);
|
||||||
|
expect(builder.use).toBe('@now/static-build');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Test `detectApiBuilders`', async () => {
|
||||||
|
{
|
||||||
|
const files = ['package.json', 'api/user.js', 'api/team.js'];
|
||||||
|
|
||||||
|
const builders = await detectApiBuilders(files);
|
||||||
|
expect(builders[0].use).toBe('@now/node');
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
const files = ['package.json', 'api/user.go', 'api/team.js'];
|
||||||
|
|
||||||
|
const builders = await detectApiBuilders(files);
|
||||||
|
expect(builders.some(({ use }) => use === '@now/go')).toBeTruthy();
|
||||||
|
expect(builders.some(({ use }) => use === '@now/node')).toBeTruthy();
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
const files = ['package.json'];
|
||||||
|
|
||||||
|
const builders = await detectApiBuilders(files);
|
||||||
|
expect(builders).toBe(null);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Test `detectApiRoutes`', async () => {
|
||||||
|
{
|
||||||
|
const files = ['api/user.go', 'api/team.js'];
|
||||||
|
|
||||||
|
const { defaultRoutes } = await detectApiRoutes(files);
|
||||||
|
expect(defaultRoutes.length).toBe(2);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
const files = ['api/user.go', 'api/user.js'];
|
||||||
|
|
||||||
|
const { error } = await detectApiRoutes(files);
|
||||||
|
expect(error.code).toBe('conflicting_file_path');
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
const files = ['api/[user].go', 'api/[team]/[id].js'];
|
||||||
|
|
||||||
|
const { error } = await detectApiRoutes(files);
|
||||||
|
expect(error.code).toBe('conflicting_file_path');
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
const files = ['api/[team]/[team].js'];
|
||||||
|
|
||||||
|
const { error } = await detectApiRoutes(files);
|
||||||
|
expect(error.code).toBe('conflicting_path_segment');
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
const files = ['api/[endpoint].js', 'api/[endpoint]/[id].js'];
|
||||||
|
|
||||||
|
const { defaultRoutes } = await detectApiRoutes(files);
|
||||||
|
expect(defaultRoutes.length).toBe(2);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|||||||
@@ -61,6 +61,18 @@ export async function build({
|
|||||||
await initPrivateGit(process.env.GIT_CREDENTIALS);
|
await initPrivateGit(process.env.GIT_CREDENTIALS);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (process.env.GO111MODULE) {
|
||||||
|
console.log(`\nManually assigning 'GO111MODULE' is not recommended.
|
||||||
|
|
||||||
|
By default:
|
||||||
|
- 'GO111MODULE=on' If entrypoint package name is not 'main'
|
||||||
|
- 'GO111MODULE=off' If entrypoint package name is 'main'
|
||||||
|
|
||||||
|
We highly recommend you leverage Go Modules in your project.
|
||||||
|
Learn more: https://github.com/golang/go/wiki/Modules
|
||||||
|
`);
|
||||||
|
}
|
||||||
|
|
||||||
console.log('Downloading user files...');
|
console.log('Downloading user files...');
|
||||||
const entrypointArr = entrypoint.split(sep);
|
const entrypointArr = entrypoint.split(sep);
|
||||||
|
|
||||||
@@ -128,18 +140,18 @@ Learn more: https://zeit.co/docs/v2/deployments/official-builders/go-now-go/#ent
|
|||||||
const entrypointDirname = dirname(downloadedFiles[entrypoint].fsPath);
|
const entrypointDirname = dirname(downloadedFiles[entrypoint].fsPath);
|
||||||
let isGoModExist = false;
|
let isGoModExist = false;
|
||||||
let goModPath = '';
|
let goModPath = '';
|
||||||
let goModPathArr: string[] = [];
|
let isGoModInRootDir = false;
|
||||||
for (const file of Object.keys(downloadedFiles)) {
|
for (const file of Object.keys(downloadedFiles)) {
|
||||||
const fileDirname = dirname(downloadedFiles[file].fsPath);
|
const fileDirname = dirname(downloadedFiles[file].fsPath);
|
||||||
if (file === 'go.mod') {
|
if (file === 'go.mod') {
|
||||||
isGoModExist = true;
|
isGoModExist = true;
|
||||||
|
isGoModInRootDir = true;
|
||||||
goModPath = fileDirname;
|
goModPath = fileDirname;
|
||||||
goModPathArr = goModPath.split(sep);
|
} else if (file.endsWith('go.mod') && !file.endsWith('vendor')) {
|
||||||
} else if (file.includes('go.mod')) {
|
|
||||||
isGoModExist = true;
|
|
||||||
if (entrypointDirname === fileDirname) {
|
if (entrypointDirname === fileDirname) {
|
||||||
|
isGoModExist = true;
|
||||||
goModPath = fileDirname;
|
goModPath = fileDirname;
|
||||||
goModPathArr = goModPath.split(sep);
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -161,6 +173,10 @@ Learn more: https://zeit.co/docs/v2/deployments/official-builders/go-now-go/#ent
|
|||||||
`Found exported function "${handlerFunctionName}" in "${entrypoint}"`
|
`Found exported function "${handlerFunctionName}" in "${entrypoint}"`
|
||||||
);
|
);
|
||||||
|
|
||||||
|
if (!isGoModExist && 'vendor' in downloadedFiles) {
|
||||||
|
throw new Error('`go.mod` is required to use a `vendor` directory.');
|
||||||
|
}
|
||||||
|
|
||||||
// check if package name other than main
|
// check if package name other than main
|
||||||
// using `go.mod` way building the handler
|
// using `go.mod` way building the handler
|
||||||
const packageName = parsedAnalyzed.packageName;
|
const packageName = parsedAnalyzed.packageName;
|
||||||
@@ -202,14 +218,28 @@ Learn more: https://zeit.co/docs/v2/deployments/official-builders/go-now-go/#ent
|
|||||||
if (isGoModExist) {
|
if (isGoModExist) {
|
||||||
const goModContents = await readFile(join(goModPath, 'go.mod'), 'utf8');
|
const goModContents = await readFile(join(goModPath, 'go.mod'), 'utf8');
|
||||||
const usrModName = goModContents.split('\n')[0].split(' ')[1];
|
const usrModName = goModContents.split('\n')[0].split(' ')[1];
|
||||||
|
|
||||||
|
if (entrypointArr.length > 1 && isGoModInRootDir) {
|
||||||
|
let cleanPackagePath = [...entrypointArr];
|
||||||
|
cleanPackagePath.pop();
|
||||||
|
goPackageName = `${usrModName}/${cleanPackagePath.join('/')}`;
|
||||||
|
} else {
|
||||||
goPackageName = `${usrModName}/${packageName}`;
|
goPackageName = `${usrModName}/${packageName}`;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const mainModGoContents = modMainGoContents
|
const mainModGoContents = modMainGoContents
|
||||||
.replace('__NOW_HANDLER_PACKAGE_NAME', goPackageName)
|
.replace('__NOW_HANDLER_PACKAGE_NAME', goPackageName)
|
||||||
.replace('__NOW_HANDLER_FUNC_NAME', goFuncName);
|
.replace('__NOW_HANDLER_FUNC_NAME', goFuncName);
|
||||||
|
|
||||||
if (goModPathArr.length > 1) {
|
if (meta.isDev && isGoModExist && isGoModInRootDir) {
|
||||||
|
await writeFile(
|
||||||
|
join(dirname(downloadedFiles['now.json'].fsPath), mainModGoFileName),
|
||||||
|
mainModGoContents
|
||||||
|
);
|
||||||
|
} else if (isGoModExist && isGoModInRootDir) {
|
||||||
|
await writeFile(join(srcPath, mainModGoFileName), mainModGoContents);
|
||||||
|
} else if (isGoModExist && !isGoModInRootDir) {
|
||||||
// using `go.mod` path to write main__mod__.go
|
// using `go.mod` path to write main__mod__.go
|
||||||
await writeFile(join(goModPath, mainModGoFileName), mainModGoContents);
|
await writeFile(join(goModPath, mainModGoFileName), mainModGoContents);
|
||||||
} else {
|
} else {
|
||||||
@@ -252,21 +282,28 @@ Learn more: https://zeit.co/docs/v2/deployments/official-builders/go-now-go/#ent
|
|||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (meta.isDev) {
|
let baseGoModPath = '';
|
||||||
let entrypointDir = entrypointDirname;
|
if (meta.isDev && isGoModExist && isGoModInRootDir) {
|
||||||
if (goModPathArr.length > 1) {
|
baseGoModPath = dirname(downloadedFiles['now.json'].fsPath);
|
||||||
entrypointDir = goModPath;
|
} else if (isGoModExist && isGoModInRootDir) {
|
||||||
|
baseGoModPath = srcPath;
|
||||||
|
} else if (isGoModExist && !isGoModInRootDir) {
|
||||||
|
baseGoModPath = goModPath;
|
||||||
|
} else {
|
||||||
|
baseGoModPath = entrypointDirname;
|
||||||
}
|
}
|
||||||
const isGoModBk = await pathExists(join(entrypointDir, 'go.mod.bk'));
|
|
||||||
|
if (meta.isDev) {
|
||||||
|
const isGoModBk = await pathExists(join(baseGoModPath, 'go.mod.bk'));
|
||||||
if (isGoModBk) {
|
if (isGoModBk) {
|
||||||
await move(
|
await move(
|
||||||
join(entrypointDir, 'go.mod.bk'),
|
join(baseGoModPath, 'go.mod.bk'),
|
||||||
join(entrypointDir, 'go.mod'),
|
join(baseGoModPath, 'go.mod'),
|
||||||
{ overwrite: true }
|
{ overwrite: true }
|
||||||
);
|
);
|
||||||
await move(
|
await move(
|
||||||
join(entrypointDir, 'go.sum.bk'),
|
join(baseGoModPath, 'go.sum.bk'),
|
||||||
join(entrypointDir, 'go.sum'),
|
join(baseGoModPath, 'go.sum'),
|
||||||
{ overwrite: true }
|
{ overwrite: true }
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -283,8 +320,7 @@ Learn more: https://zeit.co/docs/v2/deployments/official-builders/go-now-go/#ent
|
|||||||
|
|
||||||
console.log('Running `go build`...');
|
console.log('Running `go build`...');
|
||||||
const destPath = join(outDir, 'handler');
|
const destPath = join(outDir, 'handler');
|
||||||
const isGoModInRootDir = goModPathArr.length === 1;
|
|
||||||
const baseGoModPath = isGoModInRootDir ? entrypointDirname : goModPath;
|
|
||||||
try {
|
try {
|
||||||
let src = [join(baseGoModPath, mainModGoFileName)];
|
let src = [join(baseGoModPath, mainModGoFileName)];
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
{
|
{
|
||||||
"name": "@now/go",
|
"name": "@now/go",
|
||||||
"version": "0.5.1",
|
"version": "0.5.4",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
"homepage": "https://zeit.co/docs/v2/deployments/official-builders/go-now-go",
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "https://github.com/zeit/now-builders.git",
|
"url": "https://github.com/zeit/now-builders.git",
|
||||||
@@ -31,6 +32,6 @@
|
|||||||
"@types/fs-extra": "^5.0.5",
|
"@types/fs-extra": "^5.0.5",
|
||||||
"@types/node-fetch": "^2.3.0",
|
"@types/node-fetch": "^2.3.0",
|
||||||
"@types/tar": "^4.0.0",
|
"@types/tar": "^4.0.0",
|
||||||
"typescript": "^3.4.2"
|
"typescript": "3.5.2"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
12
packages/now-go/test/fixtures/13-go-mod-nested/api/nested/index.go
vendored
Normal file
12
packages/now-go/test/fixtures/13-go-mod-nested/api/nested/index.go
vendored
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
package nested
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"with-nested/shared"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Handler func
|
||||||
|
func Handler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
fmt.Fprintf(w, shared.Say("lol:RANDOMNESS_PLACEHOLDER"))
|
||||||
|
}
|
||||||
3
packages/now-go/test/fixtures/13-go-mod-nested/go.mod
vendored
Normal file
3
packages/now-go/test/fixtures/13-go-mod-nested/go.mod
vendored
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
module with-nested
|
||||||
|
|
||||||
|
go 1.12
|
||||||
5
packages/now-go/test/fixtures/13-go-mod-nested/now.json
vendored
Normal file
5
packages/now-go/test/fixtures/13-go-mod-nested/now.json
vendored
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
{
|
||||||
|
"version": 2,
|
||||||
|
"builds": [{ "src": "api/nested/*.go", "use": "@now/go" }],
|
||||||
|
"probes": [{ "path": "/api/nested", "mustContain": "RANDOMNESS_PLACEHOLDER" }]
|
||||||
|
}
|
||||||
6
packages/now-go/test/fixtures/13-go-mod-nested/shared/shared.go
vendored
Normal file
6
packages/now-go/test/fixtures/13-go-mod-nested/shared/shared.go
vendored
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
package shared
|
||||||
|
|
||||||
|
// Say func
|
||||||
|
func Say(text string) string {
|
||||||
|
return text
|
||||||
|
}
|
||||||
11
packages/now-go/test/fixtures/14-go-mod-sub/api/index.go
vendored
Normal file
11
packages/now-go/test/fixtures/14-go-mod-sub/api/index.go
vendored
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
package handler
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Handler func
|
||||||
|
func Handler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
fmt.Fprintf(w, "hello:RANDOMNESS_PLACEHOLDER")
|
||||||
|
}
|
||||||
5
packages/now-go/test/fixtures/14-go-mod-sub/now.json
vendored
Normal file
5
packages/now-go/test/fixtures/14-go-mod-sub/now.json
vendored
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
{
|
||||||
|
"version": 2,
|
||||||
|
"builds": [{ "src": "api/**/*.go", "use": "@now/go" }],
|
||||||
|
"probes": [{ "path": "/api", "mustContain": "RANDOMNESS_PLACEHOLDER" }]
|
||||||
|
}
|
||||||
3
packages/now-go/test/fixtures/14-go-mod-sub/other-folder/go.mod
vendored
Normal file
3
packages/now-go/test/fixtures/14-go-mod-sub/other-folder/go.mod
vendored
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
module other-folder
|
||||||
|
|
||||||
|
go 1.12
|
||||||
11
packages/now-go/test/fixtures/14-go-mod-sub/other-folder/index.go
vendored
Normal file
11
packages/now-go/test/fixtures/14-go-mod-sub/other-folder/index.go
vendored
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
package handler
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Handler func
|
||||||
|
func Handler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
fmt.Fprintf(w, "hello:RANDOMNESS_PLACEHOLDER")
|
||||||
|
}
|
||||||
@@ -1,7 +1,8 @@
|
|||||||
{
|
{
|
||||||
"name": "@now/html-minifier",
|
"name": "@now/html-minifier",
|
||||||
"version": "1.1.3",
|
"version": "1.1.4",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
"homepage": "https://zeit.co/docs/v2/deployments/official-builders/html-minifier-now-html-minifier",
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "https://github.com/zeit/now-builders.git",
|
"url": "https://github.com/zeit/now-builders.git",
|
||||||
|
|||||||
1
packages/now-layer-node/.gitignore
vendored
1
packages/now-layer-node/.gitignore
vendored
@@ -1 +0,0 @@
|
|||||||
/dist
|
|
||||||
@@ -1,32 +0,0 @@
|
|||||||
{
|
|
||||||
"name": "@now/layer-node",
|
|
||||||
"version": "0.0.2",
|
|
||||||
"main": "./dist/src/index",
|
|
||||||
"license": "MIT",
|
|
||||||
"files": [
|
|
||||||
"dist"
|
|
||||||
],
|
|
||||||
"repository": {
|
|
||||||
"type": "git",
|
|
||||||
"url": "https://github.com/zeit/now-builders.git",
|
|
||||||
"directory": "packages/now-layer-node"
|
|
||||||
},
|
|
||||||
"scripts": {
|
|
||||||
"build": "tsc",
|
|
||||||
"test": "tsc && jest",
|
|
||||||
"prepublishOnly": "tsc"
|
|
||||||
},
|
|
||||||
"dependencies": {
|
|
||||||
"fs-extra": "7.0.1",
|
|
||||||
"node-fetch": "2.6.0",
|
|
||||||
"promisepipe": "3.0.0",
|
|
||||||
"stream-to-promise": "2.2.0",
|
|
||||||
"tar": "4.4.6",
|
|
||||||
"yauzl-promise": "2.1.3"
|
|
||||||
},
|
|
||||||
"devDependencies": {
|
|
||||||
"@types/tar": "4.0.0",
|
|
||||||
"@types/yauzl-promise": "2.1.0",
|
|
||||||
"typescript": "3.3.3"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,37 +0,0 @@
|
|||||||
import { tmpdir } from 'os';
|
|
||||||
import { join } from 'path';
|
|
||||||
import { glob, Files } from '@now/build-utils';
|
|
||||||
import { mkdir, remove, pathExists } from 'fs-extra';
|
|
||||||
import { install } from './install';
|
|
||||||
|
|
||||||
interface BuildLayerConfig {
|
|
||||||
runtimeVersion: string;
|
|
||||||
platform: string;
|
|
||||||
arch: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
interface BuildLayerResult {
|
|
||||||
files: Files;
|
|
||||||
entrypoint: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function buildLayer({
|
|
||||||
runtimeVersion,
|
|
||||||
platform,
|
|
||||||
arch,
|
|
||||||
}: BuildLayerConfig): Promise<BuildLayerResult> {
|
|
||||||
const dir = join(
|
|
||||||
tmpdir(),
|
|
||||||
`now-layer-node-${runtimeVersion}-${platform}-${arch}`
|
|
||||||
);
|
|
||||||
const exists = await pathExists(dir);
|
|
||||||
if (exists) {
|
|
||||||
await remove(dir);
|
|
||||||
}
|
|
||||||
await mkdir(dir);
|
|
||||||
const { entrypoint } = await install(dir, runtimeVersion, platform, arch);
|
|
||||||
const files = await glob('{bin/node,bin/node.exe,include/**}', {
|
|
||||||
cwd: dir,
|
|
||||||
});
|
|
||||||
return { files, entrypoint };
|
|
||||||
}
|
|
||||||
@@ -1,68 +0,0 @@
|
|||||||
import { basename, join } from 'path';
|
|
||||||
import fetch from 'node-fetch';
|
|
||||||
import { extract } from 'tar';
|
|
||||||
import pipe from 'promisepipe';
|
|
||||||
import { createWriteStream } from 'fs-extra';
|
|
||||||
import { unzip, zipFromFile } from './unzip';
|
|
||||||
|
|
||||||
export async function install(
|
|
||||||
dest: string,
|
|
||||||
version: string,
|
|
||||||
platform: string,
|
|
||||||
arch: string
|
|
||||||
) {
|
|
||||||
const tarballUrl = getUrl(version, platform, arch);
|
|
||||||
console.log('Downloading from ' + tarballUrl);
|
|
||||||
console.log('Downloading to ' + dest);
|
|
||||||
const res = await fetch(tarballUrl);
|
|
||||||
if (!res.ok) {
|
|
||||||
throw new Error(`HTTP request failed: ${res.status}`);
|
|
||||||
}
|
|
||||||
let entrypoint: string;
|
|
||||||
if (platform === 'win32') {
|
|
||||||
// Put it in the `bin` dir for consistency with the tarballs
|
|
||||||
const finalDest = join(dest, 'bin');
|
|
||||||
const zipName = basename(tarballUrl);
|
|
||||||
const zipPath = join(dest, zipName);
|
|
||||||
|
|
||||||
await pipe(
|
|
||||||
res.body,
|
|
||||||
createWriteStream(zipPath)
|
|
||||||
);
|
|
||||||
|
|
||||||
const zipFile = await zipFromFile(zipPath);
|
|
||||||
await unzip(zipFile, finalDest, { strip: 1 });
|
|
||||||
entrypoint = join('bin', 'node.exe');
|
|
||||||
} else {
|
|
||||||
const extractStream = extract({ strip: 1, C: dest });
|
|
||||||
if (!extractStream.destroy) {
|
|
||||||
// If there is an error in promisepipe,
|
|
||||||
// it expects a destroy method
|
|
||||||
extractStream.destroy = () => {};
|
|
||||||
}
|
|
||||||
await pipe(
|
|
||||||
res.body,
|
|
||||||
extractStream
|
|
||||||
);
|
|
||||||
entrypoint = join('bin', 'node');
|
|
||||||
}
|
|
||||||
|
|
||||||
return { entrypoint };
|
|
||||||
}
|
|
||||||
|
|
||||||
export function getUrl(
|
|
||||||
version: string,
|
|
||||||
platform: string = process.platform,
|
|
||||||
arch: string = process.arch
|
|
||||||
): string {
|
|
||||||
let ext: string;
|
|
||||||
let plat: string;
|
|
||||||
if (platform === 'win32') {
|
|
||||||
ext = 'zip';
|
|
||||||
plat = 'win';
|
|
||||||
} else {
|
|
||||||
ext = 'tar.gz';
|
|
||||||
plat = platform;
|
|
||||||
}
|
|
||||||
return `https://nodejs.org/dist/v${version}/node-v${version}-${plat}-${arch}.${ext}`;
|
|
||||||
}
|
|
||||||
@@ -1,96 +0,0 @@
|
|||||||
import { tmpdir } from 'os';
|
|
||||||
import pipe from 'promisepipe';
|
|
||||||
import { dirname, join } from 'path';
|
|
||||||
import { createWriteStream, mkdirp, symlink, unlink } from 'fs-extra';
|
|
||||||
import streamToPromise from 'stream-to-promise';
|
|
||||||
import {
|
|
||||||
Entry,
|
|
||||||
ZipFile,
|
|
||||||
open as zipFromFile,
|
|
||||||
fromBuffer as zipFromBuffer,
|
|
||||||
} from 'yauzl-promise';
|
|
||||||
|
|
||||||
export { zipFromFile, zipFromBuffer, ZipFile };
|
|
||||||
|
|
||||||
export async function unzipToTemp(
|
|
||||||
data: Buffer | string,
|
|
||||||
tmpDir: string = tmpdir()
|
|
||||||
): Promise<string> {
|
|
||||||
const dir = join(
|
|
||||||
tmpDir,
|
|
||||||
`zeit-fun-${Math.random()
|
|
||||||
.toString(16)
|
|
||||||
.substring(2)}`
|
|
||||||
);
|
|
||||||
let zip: ZipFile;
|
|
||||||
if (Buffer.isBuffer(data)) {
|
|
||||||
zip = await zipFromBuffer(data);
|
|
||||||
} else {
|
|
||||||
zip = await zipFromFile(data);
|
|
||||||
}
|
|
||||||
await unzip(zip, dir);
|
|
||||||
await zip.close();
|
|
||||||
return dir;
|
|
||||||
}
|
|
||||||
|
|
||||||
interface UnzipOptions {
|
|
||||||
strip?: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function unzip(
|
|
||||||
zipFile: ZipFile,
|
|
||||||
dir: string,
|
|
||||||
opts: UnzipOptions = {}
|
|
||||||
): Promise<void> {
|
|
||||||
let entry: Entry;
|
|
||||||
const strip = opts.strip || 0;
|
|
||||||
while ((entry = await zipFile.readEntry()) !== null) {
|
|
||||||
const fileName =
|
|
||||||
strip === 0
|
|
||||||
? entry.fileName
|
|
||||||
: entry.fileName
|
|
||||||
.split('/')
|
|
||||||
.slice(strip)
|
|
||||||
.join('/');
|
|
||||||
const destPath = join(dir, fileName);
|
|
||||||
if (/\/$/.test(entry.fileName)) {
|
|
||||||
await mkdirp(destPath);
|
|
||||||
} else {
|
|
||||||
const [entryStream] = await Promise.all([
|
|
||||||
entry.openReadStream(),
|
|
||||||
// ensure parent directory exists
|
|
||||||
mkdirp(dirname(destPath)),
|
|
||||||
]);
|
|
||||||
const mode = entry.externalFileAttributes >>> 16;
|
|
||||||
if (isSymbolicLink(mode)) {
|
|
||||||
const linkDest = String(await streamToPromise(entryStream));
|
|
||||||
await symlink(linkDest, destPath);
|
|
||||||
} else {
|
|
||||||
const octal = mode & 4095 /* 07777 */;
|
|
||||||
const modeOctal = ('0000' + octal.toString(8)).slice(-4);
|
|
||||||
const modeVal = parseInt(modeOctal, 8);
|
|
||||||
try {
|
|
||||||
await unlink(destPath);
|
|
||||||
} catch (err) {
|
|
||||||
if (err.code !== 'ENOENT') {
|
|
||||||
throw err;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
const destStream = createWriteStream(destPath, {
|
|
||||||
mode: modeVal,
|
|
||||||
});
|
|
||||||
await pipe(
|
|
||||||
entryStream,
|
|
||||||
destStream
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const S_IFMT = 61440; /* 0170000 type of file */
|
|
||||||
const S_IFLNK = 40960; /* 0120000 symbolic link */
|
|
||||||
|
|
||||||
export function isSymbolicLink(mode: number): boolean {
|
|
||||||
return (mode & S_IFMT) === S_IFLNK;
|
|
||||||
}
|
|
||||||
@@ -1,54 +0,0 @@
|
|||||||
/* global jest, expect, it */
|
|
||||||
jest.setTimeout(30 * 1000);
|
|
||||||
const { buildLayer } = require('../');
|
|
||||||
|
|
||||||
describe('buildLayer', () => {
|
|
||||||
it('should get node 10 and metadata for windows', async () => {
|
|
||||||
const { files, entrypoint } = await buildLayer({
|
|
||||||
runtimeVersion: '10.16.0',
|
|
||||||
platform: 'win32',
|
|
||||||
arch: 'x64',
|
|
||||||
});
|
|
||||||
const names = new Set(Object.keys(files));
|
|
||||||
expect(names).toBeTruthy();
|
|
||||||
expect(names.size).toBeGreaterThan(0);
|
|
||||||
expect(entrypoint).toBe('bin/node.exe');
|
|
||||||
expect(names.has('bin/node.exe')).toBeTruthy();
|
|
||||||
expect(names.has('bin/npm.cmd')).toBeFalsy();
|
|
||||||
expect(names.has('bin/npx.cmd')).toBeFalsy();
|
|
||||||
expect(names.has('bin/node_modules')).toBeFalsy();
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should get node 10 and metadata for macos', async () => {
|
|
||||||
const { files, entrypoint } = await buildLayer({
|
|
||||||
runtimeVersion: '10.16.0',
|
|
||||||
platform: 'darwin',
|
|
||||||
arch: 'x64',
|
|
||||||
});
|
|
||||||
const names = new Set(Object.keys(files));
|
|
||||||
expect(names).toBeTruthy();
|
|
||||||
expect(names.size).toBeGreaterThan(0);
|
|
||||||
expect(entrypoint).toBe('bin/node');
|
|
||||||
expect(names.has('bin/node')).toBeTruthy();
|
|
||||||
expect(names.has('bin/npm')).toBeFalsy();
|
|
||||||
expect(names.has('bin/npx')).toBeFalsy();
|
|
||||||
expect(names.has('lib/node_modules')).toBeFalsy();
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should get node 10 and metadata for linux', async () => {
|
|
||||||
const { files, entrypoint } = await buildLayer({
|
|
||||||
runtimeVersion: '10.16.0',
|
|
||||||
platform: 'linux',
|
|
||||||
arch: 'x64',
|
|
||||||
});
|
|
||||||
const names = new Set(Object.keys(files));
|
|
||||||
expect(names).toBeTruthy();
|
|
||||||
expect(names.size).toBeGreaterThan(0);
|
|
||||||
expect(entrypoint).toBe('bin/node');
|
|
||||||
expect(names.has('bin/node')).toBeTruthy();
|
|
||||||
expect(names.has('include/node/node.h')).toBeTruthy();
|
|
||||||
expect(names.has('bin/npm')).toBeFalsy();
|
|
||||||
expect(names.has('bin/npx')).toBeFalsy();
|
|
||||||
expect(names.has('lib/node_modules')).toBeFalsy();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
declare module 'promisepipe' {
|
|
||||||
import { Stream } from 'stream';
|
|
||||||
export default function pipe(...args: Stream[]): Promise<void>;
|
|
||||||
}
|
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
declare module 'stream-to-promise' {
|
|
||||||
import { Stream } from 'stream';
|
|
||||||
export default function streamToPromise(
|
|
||||||
stream: NodeJS.ReadableStream
|
|
||||||
): Promise<string>;
|
|
||||||
}
|
|
||||||
1
packages/now-layer-npm/.gitignore
vendored
1
packages/now-layer-npm/.gitignore
vendored
@@ -1 +0,0 @@
|
|||||||
/dist
|
|
||||||
@@ -1,29 +0,0 @@
|
|||||||
{
|
|
||||||
"name": "@now/layer-npm",
|
|
||||||
"version": "0.0.2",
|
|
||||||
"main": "./dist/src/index",
|
|
||||||
"license": "MIT",
|
|
||||||
"files": [
|
|
||||||
"dist"
|
|
||||||
],
|
|
||||||
"repository": {
|
|
||||||
"type": "git",
|
|
||||||
"url": "https://github.com/zeit/now-builders.git",
|
|
||||||
"directory": "packages/now-layer-npm"
|
|
||||||
},
|
|
||||||
"scripts": {
|
|
||||||
"build": "tsc",
|
|
||||||
"test": "tsc && jest",
|
|
||||||
"prepublishOnly": "tsc"
|
|
||||||
},
|
|
||||||
"dependencies": {
|
|
||||||
"fs-extra": "7.0.1",
|
|
||||||
"node-fetch": "2.6.0",
|
|
||||||
"promisepipe": "3.0.0",
|
|
||||||
"tar": "4.4.6"
|
|
||||||
},
|
|
||||||
"devDependencies": {
|
|
||||||
"@types/tar": "4.0.0",
|
|
||||||
"typescript": "3.3.3"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,37 +0,0 @@
|
|||||||
import { tmpdir } from 'os';
|
|
||||||
import { join } from 'path';
|
|
||||||
import { glob, Files } from '@now/build-utils';
|
|
||||||
import { mkdir, remove, pathExists } from 'fs-extra';
|
|
||||||
import { install } from './install';
|
|
||||||
|
|
||||||
interface BuildLayerConfig {
|
|
||||||
runtimeVersion: string;
|
|
||||||
platform: string;
|
|
||||||
arch: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
interface BuildLayerResult {
|
|
||||||
files: Files;
|
|
||||||
entrypoint: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function buildLayer({
|
|
||||||
runtimeVersion,
|
|
||||||
platform,
|
|
||||||
arch,
|
|
||||||
}: BuildLayerConfig): Promise<BuildLayerResult> {
|
|
||||||
const dir = join(
|
|
||||||
tmpdir(),
|
|
||||||
`now-layer-npm-${runtimeVersion}-${platform}-${arch}`
|
|
||||||
);
|
|
||||||
const exists = await pathExists(dir);
|
|
||||||
if (exists) {
|
|
||||||
await remove(dir);
|
|
||||||
}
|
|
||||||
await mkdir(dir);
|
|
||||||
const { entrypoint } = await install(dir, runtimeVersion);
|
|
||||||
const files = await glob('{bin/**,lib/**,node_modules/**}', {
|
|
||||||
cwd: dir,
|
|
||||||
});
|
|
||||||
return { files, entrypoint };
|
|
||||||
}
|
|
||||||
@@ -1,29 +0,0 @@
|
|||||||
import { join } from 'path';
|
|
||||||
import fetch from 'node-fetch';
|
|
||||||
import { extract } from 'tar';
|
|
||||||
import pipe from 'promisepipe';
|
|
||||||
|
|
||||||
export async function install(dest: string, version: string) {
|
|
||||||
const tarballUrl = `https://registry.npmjs.org/npm/-/npm-${version}.tgz`;
|
|
||||||
console.log('Downloading from ' + tarballUrl);
|
|
||||||
console.log('Downloading to ' + dest);
|
|
||||||
const res = await fetch(tarballUrl);
|
|
||||||
if (!res.ok) {
|
|
||||||
throw new Error(`HTTP request failed: ${res.status}`);
|
|
||||||
}
|
|
||||||
const extractStream = extract({ strip: 1, C: dest });
|
|
||||||
if (!extractStream.destroy) {
|
|
||||||
// If there is an error in promisepipe,
|
|
||||||
// it expects a destroy method
|
|
||||||
extractStream.destroy = () => {};
|
|
||||||
}
|
|
||||||
await pipe(
|
|
||||||
res.body,
|
|
||||||
extractStream
|
|
||||||
);
|
|
||||||
|
|
||||||
const pathToManifest = join(dest, 'package.json');
|
|
||||||
const manifest = require(pathToManifest);
|
|
||||||
const entrypoint = manifest.bin.npm;
|
|
||||||
return { entrypoint };
|
|
||||||
}
|
|
||||||
@@ -1,50 +0,0 @@
|
|||||||
/* global jest, expect, it */
|
|
||||||
jest.setTimeout(30 * 1000);
|
|
||||||
const { buildLayer } = require('../');
|
|
||||||
|
|
||||||
describe('buildLayer', () => {
|
|
||||||
it('should get npm 6 but not npm for windows', async () => {
|
|
||||||
const { files, entrypoint } = await buildLayer({
|
|
||||||
runtimeVersion: '6.9.0',
|
|
||||||
platform: 'win32',
|
|
||||||
arch: 'x64',
|
|
||||||
});
|
|
||||||
const names = new Set(Object.keys(files));
|
|
||||||
expect(names).toBeTruthy();
|
|
||||||
expect(entrypoint).toBe('./bin/npm-cli.js');
|
|
||||||
expect(names.size).toBeGreaterThan(0);
|
|
||||||
expect(names.has('bin/npm.cmd')).toBeTruthy();
|
|
||||||
expect(names.has('bin/npx.cmd')).toBeTruthy();
|
|
||||||
expect(names.has('README.md')).toBeFalsy();
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should get npm 6 but not npm for macos', async () => {
|
|
||||||
const { files, entrypoint } = await buildLayer({
|
|
||||||
runtimeVersion: '6.9.0',
|
|
||||||
platform: 'darwin',
|
|
||||||
arch: 'x64',
|
|
||||||
});
|
|
||||||
const names = new Set(Object.keys(files));
|
|
||||||
expect(names).toBeTruthy();
|
|
||||||
expect(entrypoint).toBe('./bin/npm-cli.js');
|
|
||||||
expect(names.size).toBeGreaterThan(0);
|
|
||||||
expect(names.has('bin/npm')).toBeTruthy();
|
|
||||||
expect(names.has('bin/npx')).toBeTruthy();
|
|
||||||
expect(names.has('README.md')).toBeFalsy();
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should get npm 6 but not npm for linux', async () => {
|
|
||||||
const { files, entrypoint } = await buildLayer({
|
|
||||||
runtimeVersion: '6.9.0',
|
|
||||||
platform: 'linux',
|
|
||||||
arch: 'x64',
|
|
||||||
});
|
|
||||||
const names = new Set(Object.keys(files));
|
|
||||||
expect(names).toBeTruthy();
|
|
||||||
expect(entrypoint).toBe('./bin/npm-cli.js');
|
|
||||||
expect(names.size).toBeGreaterThan(0);
|
|
||||||
expect(names.has('bin/npm')).toBeTruthy();
|
|
||||||
expect(names.has('bin/npx')).toBeTruthy();
|
|
||||||
expect(names.has('README.md')).toBeFalsy();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
@@ -1,18 +0,0 @@
|
|||||||
{
|
|
||||||
"compilerOptions": {
|
|
||||||
"declaration": false,
|
|
||||||
"esModuleInterop": true,
|
|
||||||
"lib": ["esnext"],
|
|
||||||
"module": "commonjs",
|
|
||||||
"moduleResolution": "node",
|
|
||||||
"noEmitOnError": true,
|
|
||||||
"noFallthroughCasesInSwitch": true,
|
|
||||||
"noImplicitReturns": true,
|
|
||||||
"noUnusedLocals": true,
|
|
||||||
"noUnusedParameters": true,
|
|
||||||
"outDir": "dist",
|
|
||||||
"types": ["node"],
|
|
||||||
"strict": true,
|
|
||||||
"target": "esnext"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
declare module 'promisepipe' {
|
|
||||||
import { Stream } from 'stream';
|
|
||||||
export default function pipe(...args: Stream[]): Promise<void>;
|
|
||||||
}
|
|
||||||
1
packages/now-layer-yarn/.gitignore
vendored
1
packages/now-layer-yarn/.gitignore
vendored
@@ -1 +0,0 @@
|
|||||||
/dist
|
|
||||||
@@ -1,29 +0,0 @@
|
|||||||
{
|
|
||||||
"name": "@now/layer-yarn",
|
|
||||||
"version": "0.0.2",
|
|
||||||
"main": "./dist/src/index",
|
|
||||||
"license": "MIT",
|
|
||||||
"files": [
|
|
||||||
"dist"
|
|
||||||
],
|
|
||||||
"repository": {
|
|
||||||
"type": "git",
|
|
||||||
"url": "https://github.com/zeit/now-builders.git",
|
|
||||||
"directory": "packages/now-layer-yarn"
|
|
||||||
},
|
|
||||||
"scripts": {
|
|
||||||
"build": "tsc",
|
|
||||||
"test": "tsc && jest",
|
|
||||||
"prepublishOnly": "tsc"
|
|
||||||
},
|
|
||||||
"dependencies": {
|
|
||||||
"fs-extra": "7.0.1",
|
|
||||||
"node-fetch": "2.6.0",
|
|
||||||
"promisepipe": "3.0.0",
|
|
||||||
"tar": "4.4.6"
|
|
||||||
},
|
|
||||||
"devDependencies": {
|
|
||||||
"@types/tar": "4.0.0",
|
|
||||||
"typescript": "3.3.3"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,37 +0,0 @@
|
|||||||
import { tmpdir } from 'os';
|
|
||||||
import { join } from 'path';
|
|
||||||
import { glob, Files } from '@now/build-utils';
|
|
||||||
import { mkdir, remove, pathExists } from 'fs-extra';
|
|
||||||
import { install } from './install';
|
|
||||||
|
|
||||||
interface BuildLayerConfig {
|
|
||||||
runtimeVersion: string;
|
|
||||||
platform: string;
|
|
||||||
arch: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
interface BuildLayerResult {
|
|
||||||
files: Files;
|
|
||||||
entrypoint: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function buildLayer({
|
|
||||||
runtimeVersion,
|
|
||||||
platform,
|
|
||||||
arch,
|
|
||||||
}: BuildLayerConfig): Promise<BuildLayerResult> {
|
|
||||||
const dir = join(
|
|
||||||
tmpdir(),
|
|
||||||
`now-layer-yarn-${runtimeVersion}-${platform}-${arch}`
|
|
||||||
);
|
|
||||||
const exists = await pathExists(dir);
|
|
||||||
if (exists) {
|
|
||||||
await remove(dir);
|
|
||||||
}
|
|
||||||
await mkdir(dir);
|
|
||||||
const { entrypoint } = await install(dir, runtimeVersion);
|
|
||||||
const files = await glob('{bin/**,lib/**}', {
|
|
||||||
cwd: dir,
|
|
||||||
});
|
|
||||||
return { files, entrypoint };
|
|
||||||
}
|
|
||||||
@@ -1,29 +0,0 @@
|
|||||||
import { join } from 'path';
|
|
||||||
import fetch from 'node-fetch';
|
|
||||||
import { extract } from 'tar';
|
|
||||||
import pipe from 'promisepipe';
|
|
||||||
|
|
||||||
export async function install(dest: string, version: string) {
|
|
||||||
const tarballUrl = `https://registry.npmjs.org/yarn/-/yarn-${version}.tgz`;
|
|
||||||
console.log('Downloading from ' + tarballUrl);
|
|
||||||
console.log('Downloading to ' + dest);
|
|
||||||
const res = await fetch(tarballUrl);
|
|
||||||
if (!res.ok) {
|
|
||||||
throw new Error(`HTTP request failed: ${res.status}`);
|
|
||||||
}
|
|
||||||
const extractStream = extract({ strip: 1, C: dest });
|
|
||||||
if (!extractStream.destroy) {
|
|
||||||
// If there is an error in promisepipe,
|
|
||||||
// it expects a destroy method
|
|
||||||
extractStream.destroy = () => {};
|
|
||||||
}
|
|
||||||
await pipe(
|
|
||||||
res.body,
|
|
||||||
extractStream
|
|
||||||
);
|
|
||||||
|
|
||||||
const pathToManifest = join(dest, 'package.json');
|
|
||||||
const manifest = require(pathToManifest);
|
|
||||||
const entrypoint = manifest.bin.yarn;
|
|
||||||
return { entrypoint };
|
|
||||||
}
|
|
||||||
@@ -1,49 +0,0 @@
|
|||||||
/* global jest, expect, it */
|
|
||||||
jest.setTimeout(30 * 1000);
|
|
||||||
const { buildLayer } = require('../');
|
|
||||||
|
|
||||||
describe('buildLayer', () => {
|
|
||||||
it('should get yarn for windows', async () => {
|
|
||||||
const { files, entrypoint } = await buildLayer({
|
|
||||||
runtimeVersion: '1.16.0',
|
|
||||||
platform: 'win32',
|
|
||||||
arch: 'x64',
|
|
||||||
});
|
|
||||||
const names = new Set(Object.keys(files));
|
|
||||||
expect(names).toBeTruthy();
|
|
||||||
expect(entrypoint).toBe('./bin/yarn.js');
|
|
||||||
expect(names.size).toBeGreaterThan(0);
|
|
||||||
expect(names.has('bin/yarn.cmd')).toBeTruthy();
|
|
||||||
expect(names.has('lib/cli.js')).toBeTruthy();
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should get yarn for macos', async () => {
|
|
||||||
const { files, entrypoint } = await buildLayer({
|
|
||||||
runtimeVersion: '1.16.0',
|
|
||||||
platform: 'darwin',
|
|
||||||
arch: 'x64',
|
|
||||||
});
|
|
||||||
const names = new Set(Object.keys(files));
|
|
||||||
expect(names).toBeTruthy();
|
|
||||||
expect(entrypoint).toBe('./bin/yarn.js');
|
|
||||||
expect(names.size).toBeGreaterThan(0);
|
|
||||||
expect(names.has('bin/yarn')).toBeTruthy();
|
|
||||||
expect(names.has('lib/cli.js')).toBeTruthy();
|
|
||||||
expect(names.has('README.md')).toBeFalsy();
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should get yarn for linux', async () => {
|
|
||||||
const { files, entrypoint } = await buildLayer({
|
|
||||||
runtimeVersion: '1.16.0',
|
|
||||||
platform: 'linux',
|
|
||||||
arch: 'x64',
|
|
||||||
});
|
|
||||||
const names = new Set(Object.keys(files));
|
|
||||||
expect(names).toBeTruthy();
|
|
||||||
expect(entrypoint).toBe('./bin/yarn.js');
|
|
||||||
expect(names.size).toBeGreaterThan(0);
|
|
||||||
expect(names.has('bin/yarn')).toBeTruthy();
|
|
||||||
expect(names.has('lib/cli.js')).toBeTruthy();
|
|
||||||
expect(names.has('README.md')).toBeFalsy();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
declare module 'promisepipe' {
|
|
||||||
import { Stream } from 'stream';
|
|
||||||
export default function pipe(...args: Stream[]): Promise<void>;
|
|
||||||
}
|
|
||||||
@@ -39,4 +39,7 @@ exports.build = async ({ files, entrypoint, config }) => {
|
|||||||
return { [replacedEntrypoint]: result };
|
return { [replacedEntrypoint]: result };
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.shouldServe = shouldServe;
|
exports.shouldServe = (options) => {
|
||||||
|
const requestPath = options.requestPath.replace(/\.html$/, '.md');
|
||||||
|
return shouldServe({ ...options, requestPath });
|
||||||
|
};
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
{
|
{
|
||||||
"name": "@now/md",
|
"name": "@now/md",
|
||||||
"version": "0.5.3",
|
"version": "0.5.5",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
"homepage": "https://zeit.co/docs/v2/deployments/official-builders/markdown-now-md",
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "https://github.com/zeit/now-builders.git",
|
"url": "https://github.com/zeit/now-builders.git",
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
{
|
{
|
||||||
"name": "@now/mdx-deck",
|
"name": "@now/mdx-deck",
|
||||||
"version": "0.5.4",
|
"version": "0.5.5",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
"homepage": "https://zeit.co/docs/v2/deployments/official-builders/mdx-deck-now-mdx-deck",
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "https://github.com/zeit/now-builders.git",
|
"url": "https://github.com/zeit/now-builders.git",
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
{
|
{
|
||||||
"name": "@now/next",
|
"name": "@now/next",
|
||||||
"version": "0.4.1",
|
"version": "0.5.3",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"main": "./dist/index",
|
"main": "./dist/index",
|
||||||
|
"homepage": "https://zeit.co/docs/v2/deployments/official-builders/next-js-now-next",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "./getBridgeTypes.sh && tsc",
|
"build": "./getBridgeTypes.sh && tsc",
|
||||||
"test": "npm run build && jest",
|
"test": "npm run build && jest",
|
||||||
@@ -14,7 +15,7 @@
|
|||||||
"directory": "packages/now-next"
|
"directory": "packages/now-next"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@now/node-bridge": "^1.1.4",
|
"@now/node-bridge": "1.2.2",
|
||||||
"fs-extra": "^7.0.0",
|
"fs-extra": "^7.0.0",
|
||||||
"get-port": "^5.0.0",
|
"get-port": "^5.0.0",
|
||||||
"resolve-from": "^5.0.0",
|
"resolve-from": "^5.0.0",
|
||||||
@@ -28,6 +29,6 @@
|
|||||||
"@types/resolve-from": "^5.0.1",
|
"@types/resolve-from": "^5.0.1",
|
||||||
"@types/semver": "^6.0.0",
|
"@types/semver": "^6.0.0",
|
||||||
"jest": "^24.7.1",
|
"jest": "^24.7.1",
|
||||||
"typescript": "^3.4.3"
|
"typescript": "3.5.2"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,6 +21,8 @@ import {
|
|||||||
PrepareCacheOptions,
|
PrepareCacheOptions,
|
||||||
runNpmInstall,
|
runNpmInstall,
|
||||||
runPackageJsonScript,
|
runPackageJsonScript,
|
||||||
|
getNodeVersion,
|
||||||
|
getSpawnOptions,
|
||||||
} from '@now/build-utils';
|
} from '@now/build-utils';
|
||||||
|
|
||||||
import nextLegacyVersions from './legacy-versions';
|
import nextLegacyVersions from './legacy-versions';
|
||||||
@@ -38,6 +40,7 @@ import {
|
|||||||
validateEntrypoint,
|
validateEntrypoint,
|
||||||
normalizePage,
|
normalizePage,
|
||||||
getDynamicRoutes,
|
getDynamicRoutes,
|
||||||
|
isDynamicRoute,
|
||||||
} from './utils';
|
} from './utils';
|
||||||
|
|
||||||
interface BuildParamsMeta {
|
interface BuildParamsMeta {
|
||||||
@@ -162,15 +165,20 @@ export const build = async ({
|
|||||||
watch?: string[];
|
watch?: string[];
|
||||||
childProcesses: ChildProcess[];
|
childProcesses: ChildProcess[];
|
||||||
}> => {
|
}> => {
|
||||||
|
process.env.__NEXT_BUILDER_EXPERIMENTAL_TARGET = 'serverless';
|
||||||
|
|
||||||
validateEntrypoint(entrypoint);
|
validateEntrypoint(entrypoint);
|
||||||
|
|
||||||
const entryDirectory = path.dirname(entrypoint);
|
const entryDirectory = path.dirname(entrypoint);
|
||||||
const entryPath = path.join(workPath, entryDirectory);
|
const entryPath = path.join(workPath, entryDirectory);
|
||||||
const dotNext = path.join(entryPath, '.next');
|
const dotNextStatic = path.join(entryPath, '.next/static');
|
||||||
|
|
||||||
console.log(`${name} Downloading user files...`);
|
console.log(`${name} Downloading user files...`);
|
||||||
await download(files, workPath, meta);
|
await download(files, workPath, meta);
|
||||||
|
|
||||||
|
const nodeVersion = await getNodeVersion(entryPath);
|
||||||
|
const spawnOpts = getSpawnOptions(meta, nodeVersion);
|
||||||
|
|
||||||
const pkg = await readPackageJson(entryPath);
|
const pkg = await readPackageJson(entryPath);
|
||||||
const nextVersion = getNextVersion(pkg);
|
const nextVersion = getNextVersion(pkg);
|
||||||
|
|
||||||
@@ -180,17 +188,13 @@ export const build = async ({
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
process.env.__NEXT_BUILDER_EXPERIMENTAL_TARGET = 'serverless';
|
|
||||||
|
|
||||||
if (meta.isDev) {
|
if (meta.isDev) {
|
||||||
// eslint-disable-next-line no-underscore-dangle
|
|
||||||
process.env.__NEXT_BUILDER_EXPERIMENTAL_DEBUG = 'true';
|
|
||||||
let childProcess: ChildProcess | undefined;
|
let childProcess: ChildProcess | undefined;
|
||||||
|
|
||||||
// If this is the initial build, we want to start the server
|
// If this is the initial build, we want to start the server
|
||||||
if (!urls[entrypoint]) {
|
if (!urls[entrypoint]) {
|
||||||
console.log(`${name} Installing dependencies...`);
|
console.log(`${name} Installing dependencies...`);
|
||||||
await runNpmInstall(entryPath, ['--prefer-offline']);
|
await runNpmInstall(entryPath, ['--prefer-offline'], spawnOpts);
|
||||||
|
|
||||||
if (!process.env.NODE_ENV) {
|
if (!process.env.NODE_ENV) {
|
||||||
process.env.NODE_ENV = 'development';
|
process.env.NODE_ENV = 'development';
|
||||||
@@ -227,7 +231,7 @@ export const build = async ({
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
if (await pathExists(dotNext)) {
|
if (await pathExists(dotNextStatic)) {
|
||||||
console.warn(
|
console.warn(
|
||||||
'WARNING: You should not upload the `.next` directory. See https://zeit.co/docs/v2/deployments/official-builders/next-js-now-next/ for more details.'
|
'WARNING: You should not upload the `.next` directory. See https://zeit.co/docs/v2/deployments/official-builders/next-js-now-next/ for more details.'
|
||||||
);
|
);
|
||||||
@@ -259,14 +263,15 @@ export const build = async ({
|
|||||||
console.log('normalized package.json result: ', packageJson);
|
console.log('normalized package.json result: ', packageJson);
|
||||||
await writePackageJson(entryPath, packageJson);
|
await writePackageJson(entryPath, packageJson);
|
||||||
} else if (!pkg.scripts || !pkg.scripts['now-build']) {
|
} else if (!pkg.scripts || !pkg.scripts['now-build']) {
|
||||||
console.warn(
|
console.log(
|
||||||
'WARNING: "now-build" script not found. Adding \'"now-build": "next build"\' to "package.json" automatically'
|
'Your application is being built using `next build`. ' +
|
||||||
|
'If you need to define a different build step, please create a `now-build` script in your `package.json` ' +
|
||||||
|
'(e.g. `{ "scripts": { "now-build": "npm run prepare && next build" } }`).'
|
||||||
);
|
);
|
||||||
pkg.scripts = {
|
pkg.scripts = {
|
||||||
'now-build': 'next build',
|
'now-build': 'next build',
|
||||||
...(pkg.scripts || {}),
|
...(pkg.scripts || {}),
|
||||||
};
|
};
|
||||||
console.log('normalized package.json result: ', pkg);
|
|
||||||
await writePackageJson(entryPath, pkg);
|
await writePackageJson(entryPath, pkg);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -276,20 +281,21 @@ export const build = async ({
|
|||||||
}
|
}
|
||||||
|
|
||||||
console.log('installing dependencies...');
|
console.log('installing dependencies...');
|
||||||
await runNpmInstall(entryPath, ['--prefer-offline']);
|
await runNpmInstall(entryPath, ['--prefer-offline'], spawnOpts);
|
||||||
|
|
||||||
console.log('running user script...');
|
console.log('running user script...');
|
||||||
const memoryToConsume = Math.floor(os.totalmem() / 1024 ** 2) - 128;
|
const memoryToConsume = Math.floor(os.totalmem() / 1024 ** 2) - 128;
|
||||||
await runPackageJsonScript(entryPath, 'now-build', {
|
const env = { ...spawnOpts.env } as any;
|
||||||
env: {
|
env.NODE_OPTIONS = `--max_old_space_size=${memoryToConsume}`;
|
||||||
...process.env,
|
await runPackageJsonScript(entryPath, 'now-build', { ...spawnOpts, env });
|
||||||
NODE_OPTIONS: `--max_old_space_size=${memoryToConsume}`,
|
|
||||||
},
|
|
||||||
} as SpawnOptions);
|
|
||||||
|
|
||||||
if (isLegacy) {
|
if (isLegacy) {
|
||||||
console.log('running npm install --production...');
|
console.log('running npm install --production...');
|
||||||
await runNpmInstall(entryPath, ['--prefer-offline', '--production']);
|
await runNpmInstall(
|
||||||
|
entryPath,
|
||||||
|
['--prefer-offline', '--production'],
|
||||||
|
spawnOpts
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (process.env.NPM_AUTH_TOKEN) {
|
if (process.env.NPM_AUTH_TOKEN) {
|
||||||
@@ -380,7 +386,7 @@ export const build = async ({
|
|||||||
'now__launcher.js': new FileBlob({ data: launcher }),
|
'now__launcher.js': new FileBlob({ data: launcher }),
|
||||||
},
|
},
|
||||||
handler: 'now__launcher.launcher',
|
handler: 'now__launcher.launcher',
|
||||||
runtime: 'nodejs8.10',
|
runtime: nodeVersion.runtime,
|
||||||
});
|
});
|
||||||
console.log(`Created lambda for page: "${page}"`);
|
console.log(`Created lambda for page: "${page}"`);
|
||||||
})
|
})
|
||||||
@@ -406,7 +412,7 @@ export const build = async ({
|
|||||||
|
|
||||||
const pathname = page.replace(/\.html$/, '');
|
const pathname = page.replace(/\.html$/, '');
|
||||||
|
|
||||||
if (pathname.startsWith('$') || pathname.includes('/$')) {
|
if (isDynamicRoute(pathname)) {
|
||||||
dynamicPages.push(pathname);
|
dynamicPages.push(pathname);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -453,7 +459,7 @@ export const build = async ({
|
|||||||
|
|
||||||
const pathname = page.replace(/\.js$/, '');
|
const pathname = page.replace(/\.js$/, '');
|
||||||
|
|
||||||
if (pathname.startsWith('$') || pathname.includes('/$')) {
|
if (isDynamicRoute(pathname)) {
|
||||||
dynamicPages.push(normalizePage(pathname));
|
dynamicPages.push(normalizePage(pathname));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -465,7 +471,7 @@ export const build = async ({
|
|||||||
'page.js': pages[page],
|
'page.js': pages[page],
|
||||||
},
|
},
|
||||||
handler: 'now__launcher.launcher',
|
handler: 'now__launcher.launcher',
|
||||||
runtime: 'nodejs8.10',
|
runtime: nodeVersion.runtime,
|
||||||
});
|
});
|
||||||
console.log(`Created lambda for page: "${page}"`);
|
console.log(`Created lambda for page: "${page}"`);
|
||||||
})
|
})
|
||||||
@@ -510,7 +516,7 @@ export const build = async ({
|
|||||||
).map(route => {
|
).map(route => {
|
||||||
// make sure .html is added to dest for now until
|
// make sure .html is added to dest for now until
|
||||||
// outputting static files to clean routes is available
|
// outputting static files to clean routes is available
|
||||||
if (staticPages[`${route.dest}.html`]) {
|
if (staticPages[`${route.dest}.html`.substr(1)]) {
|
||||||
route.dest = `${route.dest}.html`;
|
route.dest = `${route.dest}.html`;
|
||||||
}
|
}
|
||||||
return route;
|
return route;
|
||||||
|
|||||||
@@ -9,6 +9,14 @@ export interface EnvConfig {
|
|||||||
[name: string]: string | undefined;
|
[name: string]: string | undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Identify /[param]/ in route string
|
||||||
|
const TEST_DYNAMIC_ROUTE = /\/\[[^\/]+?\](?=\/|$)/;
|
||||||
|
|
||||||
|
function isDynamicRoute(route: string): boolean {
|
||||||
|
route = route.startsWith('/') ? route : `/${route}`;
|
||||||
|
return TEST_DYNAMIC_ROUTE.test(route);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Validate if the entrypoint is allowed to be used
|
* Validate if the entrypoint is allowed to be used
|
||||||
*/
|
*/
|
||||||
@@ -226,7 +234,7 @@ function getRoutes(
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pageName.startsWith('$') || pageName.includes('/$')) {
|
if (isDynamicRoute(pageName)) {
|
||||||
dynamicPages.push(normalizePage(pageName));
|
dynamicPages.push(normalizePage(pageName));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -351,4 +359,5 @@ export {
|
|||||||
stringMap,
|
stringMap,
|
||||||
syncEnvVars,
|
syncEnvVars,
|
||||||
normalizePage,
|
normalizePage,
|
||||||
|
isDynamicRoute,
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -10,7 +10,8 @@ it(
|
|||||||
const {
|
const {
|
||||||
buildResult: { output },
|
buildResult: { output },
|
||||||
} = await runBuildLambda(path.join(__dirname, 'standard'));
|
} = await runBuildLambda(path.join(__dirname, 'standard'));
|
||||||
expect(output.index).toBeDefined();
|
expect(output['index.html']).toBeDefined();
|
||||||
|
expect(output.goodbye).toBeDefined();
|
||||||
const filePaths = Object.keys(output);
|
const filePaths = Object.keys(output);
|
||||||
const serverlessError = filePaths.some(filePath => filePath.match(/_error/));
|
const serverlessError = filePaths.some(filePath => filePath.match(/_error/));
|
||||||
const hasUnderScoreAppStaticFile = filePaths.some(filePath => filePath.match(/static.*\/pages\/_app\.js$/));
|
const hasUnderScoreAppStaticFile = filePaths.some(filePath => filePath.match(/static.*\/pages\/_app\.js$/));
|
||||||
|
|||||||
@@ -1,3 +0,0 @@
|
|||||||
module.exports = {
|
|
||||||
target: 'serverless',
|
|
||||||
};
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
{
|
{
|
||||||
"version": 2,
|
"version": 2,
|
||||||
"builds": [{ "src": "next.config.js", "use": "@now/next" }]
|
"builds": [{ "src": "package.json", "use": "@now/next" }]
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,3 @@
|
|||||||
|
const F = () => 'Goodbye World!';
|
||||||
|
F.getInitialProps = () => ({});
|
||||||
|
export default F;
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@now/node-bridge",
|
"name": "@now/node-bridge",
|
||||||
"version": "1.1.4",
|
"version": "1.2.2",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"main": "./index.js",
|
"main": "./index.js",
|
||||||
"repository": {
|
"repository": {
|
||||||
@@ -21,6 +21,6 @@
|
|||||||
"@types/aws-lambda": "8.10.19",
|
"@types/aws-lambda": "8.10.19",
|
||||||
"@types/node": "11.9.4",
|
"@types/node": "11.9.4",
|
||||||
"jest": "24.1.0",
|
"jest": "24.1.0",
|
||||||
"typescript": "3.3.3"
|
"typescript": "3.5.2"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,6 +27,16 @@ export interface NowProxyResponse {
|
|||||||
encoding: string;
|
encoding: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface ServerLike {
|
||||||
|
listen: (
|
||||||
|
opts: {
|
||||||
|
host?: string;
|
||||||
|
port?: number;
|
||||||
|
},
|
||||||
|
callback: (this: Server | null) => void
|
||||||
|
) => Server | void;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* If the `http.Server` handler function throws an error asynchronously,
|
* If the `http.Server` handler function throws an error asynchronously,
|
||||||
* then it ends up being an unhandled rejection which doesn't kill the node
|
* then it ends up being an unhandled rejection which doesn't kill the node
|
||||||
@@ -92,12 +102,16 @@ function normalizeEvent(
|
|||||||
}
|
}
|
||||||
|
|
||||||
export class Bridge {
|
export class Bridge {
|
||||||
private server: Server | null;
|
private server: ServerLike | null;
|
||||||
private listening: Promise<AddressInfo>;
|
private listening: Promise<AddressInfo>;
|
||||||
private resolveListening: (info: AddressInfo) => void;
|
private resolveListening: (info: AddressInfo) => void;
|
||||||
|
private events: { [key: string]: NowProxyRequest } = {};
|
||||||
|
private reqIdSeed: number = 1;
|
||||||
|
private shouldStoreEvents: boolean = false;
|
||||||
|
|
||||||
constructor(server?: Server) {
|
constructor(server?: ServerLike, shouldStoreEvents: boolean = false) {
|
||||||
this.server = null;
|
this.server = null;
|
||||||
|
this.shouldStoreEvents = shouldStoreEvents;
|
||||||
if (server) {
|
if (server) {
|
||||||
this.setServer(server);
|
this.setServer(server);
|
||||||
}
|
}
|
||||||
@@ -112,18 +126,8 @@ export class Bridge {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
setServer(server: Server) {
|
setServer(server: ServerLike) {
|
||||||
this.server = server;
|
this.server = server;
|
||||||
server.once('listening', () => {
|
|
||||||
const addr = server.address();
|
|
||||||
if (typeof addr === 'string') {
|
|
||||||
throw new Error(`Unexpected string for \`server.address()\`: ${addr}`);
|
|
||||||
} else if (!addr) {
|
|
||||||
throw new Error('`server.address()` returned `null`');
|
|
||||||
} else {
|
|
||||||
this.resolveListening(addr);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
listen() {
|
listen() {
|
||||||
@@ -131,10 +135,35 @@ export class Bridge {
|
|||||||
throw new Error('Server has not been set!');
|
throw new Error('Server has not been set!');
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.server.listen({
|
const resolveListening = this.resolveListening;
|
||||||
|
|
||||||
|
return this.server.listen(
|
||||||
|
{
|
||||||
host: '127.0.0.1',
|
host: '127.0.0.1',
|
||||||
port: 0,
|
port: 0,
|
||||||
});
|
},
|
||||||
|
function listeningCallback() {
|
||||||
|
if (!this || typeof this.address !== 'function') {
|
||||||
|
throw new Error(
|
||||||
|
'Missing server.address() function on `this` in server.listen()'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const addr = this.address();
|
||||||
|
|
||||||
|
if (!addr) {
|
||||||
|
throw new Error('`server.address()` returned `null`');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof addr === 'string') {
|
||||||
|
throw new Error(
|
||||||
|
`Unexpected string for \`server.address()\`: ${addr}`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
resolveListening(addr);
|
||||||
|
}
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
async launcher(
|
async launcher(
|
||||||
@@ -144,15 +173,16 @@ export class Bridge {
|
|||||||
context.callbackWaitsForEmptyEventLoop = false;
|
context.callbackWaitsForEmptyEventLoop = false;
|
||||||
const { port } = await this.listening;
|
const { port } = await this.listening;
|
||||||
|
|
||||||
const { isApiGateway, method, path, headers, body } = normalizeEvent(event);
|
const normalizedEvent = normalizeEvent(event);
|
||||||
|
const { isApiGateway, method, path, headers, body } = normalizedEvent;
|
||||||
|
|
||||||
const opts = {
|
if (this.shouldStoreEvents) {
|
||||||
hostname: '127.0.0.1',
|
const reqId = `${this.reqIdSeed++}`;
|
||||||
port,
|
this.events[reqId] = normalizedEvent;
|
||||||
path,
|
headers['x-now-bridge-request-id'] = reqId;
|
||||||
method,
|
}
|
||||||
headers,
|
|
||||||
};
|
const opts = { hostname: '127.0.0.1', port, path, method, headers };
|
||||||
|
|
||||||
// eslint-disable-next-line consistent-return
|
// eslint-disable-next-line consistent-return
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
@@ -192,4 +222,10 @@ export class Bridge {
|
|||||||
req.end();
|
req.end();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
consumeEvent(reqId: string) {
|
||||||
|
const event = this.events[reqId];
|
||||||
|
delete this.events[reqId];
|
||||||
|
return event;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -83,3 +83,41 @@ test('`NowProxyEvent` normalizing', async () => {
|
|||||||
|
|
||||||
server.close();
|
server.close();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('consumeEvent', async () => {
|
||||||
|
const mockListener = jest.fn((req, res) => {
|
||||||
|
res.end('hello');
|
||||||
|
});
|
||||||
|
|
||||||
|
const server = new Server(mockListener);
|
||||||
|
const bridge = new Bridge(server, true);
|
||||||
|
bridge.listen();
|
||||||
|
|
||||||
|
const context = { callbackWaitsForEmptyEventLoop: true };
|
||||||
|
await bridge.launcher(
|
||||||
|
{
|
||||||
|
Action: 'Invoke',
|
||||||
|
body: JSON.stringify({
|
||||||
|
method: 'POST',
|
||||||
|
headers: { foo: 'baz' },
|
||||||
|
path: '/nowproxy',
|
||||||
|
body: 'body=1',
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
context
|
||||||
|
);
|
||||||
|
|
||||||
|
const headers = mockListener.mock.calls[0][0].headers;
|
||||||
|
const reqId = headers['x-now-bridge-request-id'];
|
||||||
|
|
||||||
|
expect(reqId).toBeTruthy();
|
||||||
|
|
||||||
|
const event = bridge.consumeEvent(reqId);
|
||||||
|
expect(event.body.toString()).toBe('body=1');
|
||||||
|
|
||||||
|
// an event can't be consumed multiple times
|
||||||
|
// to avoid memory leaks
|
||||||
|
expect(bridge.consumeEvent(reqId)).toBeUndefined();
|
||||||
|
|
||||||
|
server.close();
|
||||||
|
});
|
||||||
|
|||||||
@@ -1,15 +1,17 @@
|
|||||||
const { createLambda } = require('@now/build-utils/lambda.js'); // eslint-disable-line import/no-extraneous-dependencies
|
|
||||||
const download = require('@now/build-utils/fs/download.js'); // eslint-disable-line import/no-extraneous-dependencies
|
|
||||||
const FileBlob = require('@now/build-utils/file-blob.js'); // eslint-disable-line import/no-extraneous-dependencies
|
|
||||||
const FileFsRef = require('@now/build-utils/file-fs-ref.js'); // eslint-disable-line import/no-extraneous-dependencies
|
|
||||||
const fs = require('fs-extra');
|
const fs = require('fs-extra');
|
||||||
const glob = require('@now/build-utils/fs/glob.js'); // eslint-disable-line import/no-extraneous-dependencies
|
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const {
|
const {
|
||||||
|
FileBlob,
|
||||||
|
FileFsRef,
|
||||||
|
download,
|
||||||
|
createLambda,
|
||||||
|
glob,
|
||||||
runNpmInstall,
|
runNpmInstall,
|
||||||
runPackageJsonScript,
|
runPackageJsonScript,
|
||||||
} = require('@now/build-utils/fs/run-user-scripts.js'); // eslint-disable-line import/no-extraneous-dependencies
|
getNodeVersion,
|
||||||
const { shouldServe } = require('@now/build-utils'); // eslint-disable-line import/no-extraneous-dependencies
|
getSpawnOptions,
|
||||||
|
shouldServe,
|
||||||
|
} = require('@now/build-utils'); // eslint-disable-line import/no-extraneous-dependencies
|
||||||
|
|
||||||
/** @typedef { import('@now/build-utils/file-ref') } FileRef */
|
/** @typedef { import('@now/build-utils/file-ref') } FileRef */
|
||||||
/** @typedef {{[filePath: string]: FileRef}} Files */
|
/** @typedef {{[filePath: string]: FileRef}} Files */
|
||||||
@@ -38,8 +40,15 @@ async function downloadInstallAndBundle(
|
|||||||
|
|
||||||
console.log("installing dependencies for user's code...");
|
console.log("installing dependencies for user's code...");
|
||||||
const entrypointFsDirname = path.join(workPath, path.dirname(entrypoint));
|
const entrypointFsDirname = path.join(workPath, path.dirname(entrypoint));
|
||||||
await runNpmInstall(entrypointFsDirname, npmArguments);
|
const nodeVersion = await getNodeVersion(entrypointFsDirname);
|
||||||
return [downloadedFiles, entrypointFsDirname];
|
const spawnOpts = getSpawnOptions(meta, nodeVersion);
|
||||||
|
await runNpmInstall(entrypointFsDirname, npmArguments, spawnOpts);
|
||||||
|
return {
|
||||||
|
downloadedFiles,
|
||||||
|
entrypointFsDirname,
|
||||||
|
spawnOpts,
|
||||||
|
nodeVersion,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
async function compile(workPath, downloadedFiles, entrypoint, config) {
|
async function compile(workPath, downloadedFiles, entrypoint, config) {
|
||||||
@@ -101,9 +110,14 @@ exports.config = {
|
|||||||
* @returns {Promise<Files>}
|
* @returns {Promise<Files>}
|
||||||
*/
|
*/
|
||||||
exports.build = async ({
|
exports.build = async ({
|
||||||
files, entrypoint, config, workPath, meta,
|
files, entrypoint, config, workPath, meta = {},
|
||||||
}) => {
|
}) => {
|
||||||
const [downloadedFiles, entrypointFsDirname] = await downloadInstallAndBundle(
|
const {
|
||||||
|
downloadedFiles,
|
||||||
|
entrypointFsDirname,
|
||||||
|
spawnOptions,
|
||||||
|
nodeVersion,
|
||||||
|
} = await downloadInstallAndBundle(
|
||||||
{
|
{
|
||||||
files,
|
files,
|
||||||
entrypoint,
|
entrypoint,
|
||||||
@@ -114,7 +128,7 @@ exports.build = async ({
|
|||||||
);
|
);
|
||||||
|
|
||||||
console.log('running user script...');
|
console.log('running user script...');
|
||||||
await runPackageJsonScript(entrypointFsDirname, 'now-build');
|
await runPackageJsonScript(entrypointFsDirname, 'now-build', spawnOptions);
|
||||||
|
|
||||||
console.log('preparing lambda files...');
|
console.log('preparing lambda files...');
|
||||||
let preparedFiles;
|
let preparedFiles;
|
||||||
@@ -143,10 +157,13 @@ exports.build = async ({
|
|||||||
'bridge.js': new FileFsRef({ fsPath: require('@now/node-bridge') }),
|
'bridge.js': new FileFsRef({ fsPath: require('@now/node-bridge') }),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Use the system-installed version of `node` when running via `now dev`
|
||||||
|
const runtime = meta.isDev ? 'nodejs' : nodeVersion.runtime;
|
||||||
|
|
||||||
const lambda = await createLambda({
|
const lambda = await createLambda({
|
||||||
files: { ...preparedFiles, ...launcherFiles },
|
files: { ...preparedFiles, ...launcherFiles },
|
||||||
handler: 'launcher.launcher',
|
handler: 'launcher.launcher',
|
||||||
runtime: 'nodejs8.10',
|
runtime,
|
||||||
});
|
});
|
||||||
|
|
||||||
return { [entrypoint]: lambda };
|
return { [entrypoint]: lambda };
|
||||||
|
|||||||
@@ -1,14 +1,15 @@
|
|||||||
{
|
{
|
||||||
"name": "@now/node-server",
|
"name": "@now/node-server",
|
||||||
"version": "0.7.4",
|
"version": "0.8.2",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
"homepage": "https://zeit.co/docs/v2/deployments/official-builders/node-js-server-now-node-server",
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "https://github.com/zeit/now-builders.git",
|
"url": "https://github.com/zeit/now-builders.git",
|
||||||
"directory": "packages/now-node-server"
|
"directory": "packages/now-node-server"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@now/node-bridge": "^1.1.4",
|
"@now/node-bridge": "1.2.2",
|
||||||
"@zeit/ncc": "0.18.5",
|
"@zeit/ncc": "0.18.5",
|
||||||
"fs-extra": "7.0.1"
|
"fs-extra": "7.0.1"
|
||||||
},
|
},
|
||||||
|
|||||||
1
packages/now-node/.gitignore
vendored
1
packages/now-node/.gitignore
vendored
@@ -1,2 +1,3 @@
|
|||||||
/dist
|
/dist
|
||||||
/src/bridge.d.ts
|
/src/bridge.d.ts
|
||||||
|
/test/fixtures/**/types.d.ts
|
||||||
|
|||||||
45
packages/now-node/@types/zeit__ncc/index.d.ts
vendored
Normal file
45
packages/now-node/@types/zeit__ncc/index.d.ts
vendored
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
declare function ncc(
|
||||||
|
entrypoint: string,
|
||||||
|
options?: ncc.NccOptions
|
||||||
|
): ncc.NccResult;
|
||||||
|
|
||||||
|
declare namespace ncc {
|
||||||
|
export interface NccOptions {
|
||||||
|
watch?: any;
|
||||||
|
sourceMap?: boolean;
|
||||||
|
sourceMapRegister?: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface Asset {
|
||||||
|
source: Buffer;
|
||||||
|
permissions: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface Assets {
|
||||||
|
[name: string]: Asset;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface BuildResult {
|
||||||
|
err: Error | null | undefined;
|
||||||
|
code: string;
|
||||||
|
map: string | undefined;
|
||||||
|
assets: Assets | undefined;
|
||||||
|
permissions: number | undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
export type HandlerFn = (params: BuildResult) => void;
|
||||||
|
export type HandlerCallback = (fn: HandlerFn) => void;
|
||||||
|
export type RebuildFn = () => void;
|
||||||
|
export type RebuildCallback = (fn: RebuildFn) => void;
|
||||||
|
export type CloseCallback = () => void;
|
||||||
|
|
||||||
|
export interface NccResult {
|
||||||
|
handler: HandlerCallback;
|
||||||
|
rebuild: RebuildCallback;
|
||||||
|
close: CloseCallback;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
declare module '@zeit/ncc' {
|
||||||
|
export = ncc;
|
||||||
|
}
|
||||||
1
packages/now-node/bench/.gitignore
vendored
Normal file
1
packages/now-node/bench/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
lambda
|
||||||
19
packages/now-node/bench/entrypoint-express.js
Normal file
19
packages/now-node/bench/entrypoint-express.js
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
const express = require('express');
|
||||||
|
|
||||||
|
const app = express();
|
||||||
|
|
||||||
|
module.exports = app;
|
||||||
|
|
||||||
|
app.use(express.json());
|
||||||
|
|
||||||
|
app.post('*', (req, res) => {
|
||||||
|
if (req.body == null) {
|
||||||
|
return res.status(400).send({ error: 'no JSON object in the request' });
|
||||||
|
}
|
||||||
|
|
||||||
|
return res.status(200).send(JSON.stringify(req.body, null, 4));
|
||||||
|
});
|
||||||
|
|
||||||
|
app.all('*', (req, res) => {
|
||||||
|
res.status(405).send({ error: 'only POST requests are accepted' });
|
||||||
|
});
|
||||||
7
packages/now-node/bench/entrypoint-helpers.js
Normal file
7
packages/now-node/bench/entrypoint-helpers.js
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
module.exports = (req, res) => {
|
||||||
|
if (req.body == null) {
|
||||||
|
return res.status(400).send({ error: 'no JSON object in the request' });
|
||||||
|
}
|
||||||
|
|
||||||
|
return res.status(200).send(JSON.stringify(req.body, null, 4));
|
||||||
|
};
|
||||||
9
packages/now-node/bench/entrypoint-load-helpers.js
Normal file
9
packages/now-node/bench/entrypoint-load-helpers.js
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
function doNothing() {}
|
||||||
|
|
||||||
|
module.exports = (req, res) => {
|
||||||
|
doNothing(req.query.who);
|
||||||
|
doNothing(req.body);
|
||||||
|
doNothing(req.cookies);
|
||||||
|
|
||||||
|
res.end('hello');
|
||||||
|
};
|
||||||
3
packages/now-node/bench/entrypoint-notload-helpers.js
Normal file
3
packages/now-node/bench/entrypoint-notload-helpers.js
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
module.exports = (req, res) => {
|
||||||
|
res.end('hello');
|
||||||
|
};
|
||||||
10
packages/now-node/bench/package.json
Normal file
10
packages/now-node/bench/package.json
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
{
|
||||||
|
"name": "bench",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"main": "index.js",
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"express": "4.17.1",
|
||||||
|
"fs-extra": "8.0.1"
|
||||||
|
}
|
||||||
|
}
|
||||||
91
packages/now-node/bench/run.js
Normal file
91
packages/now-node/bench/run.js
Normal file
@@ -0,0 +1,91 @@
|
|||||||
|
const fs = require('fs-extra');
|
||||||
|
const { join } = require('path');
|
||||||
|
const { makeLauncher } = require('../dist/launcher');
|
||||||
|
|
||||||
|
const setupFiles = async (entrypoint, shouldAddHelpers) => {
|
||||||
|
await fs.remove(join(__dirname, 'lambda'));
|
||||||
|
await fs.ensureDir(join(__dirname, 'lambda'));
|
||||||
|
|
||||||
|
await fs.copyFile(
|
||||||
|
join(__dirname, '../dist/helpers.js'),
|
||||||
|
join(__dirname, 'lambda/helpers.js'),
|
||||||
|
);
|
||||||
|
await fs.copyFile(
|
||||||
|
require.resolve('@now/node-bridge/bridge'),
|
||||||
|
join(__dirname, 'lambda/bridge.js'),
|
||||||
|
);
|
||||||
|
await fs.copyFile(
|
||||||
|
join(process.cwd(), entrypoint),
|
||||||
|
join(__dirname, 'lambda/entrypoint.js'),
|
||||||
|
);
|
||||||
|
|
||||||
|
let launcher = makeLauncher('./entrypoint', shouldAddHelpers);
|
||||||
|
launcher += '\nexports.bridge=bridge';
|
||||||
|
|
||||||
|
await fs.writeFile(join(__dirname, 'lambda/launcher.js'), launcher);
|
||||||
|
};
|
||||||
|
|
||||||
|
const createBigJSONObj = () => {
|
||||||
|
const obj = {};
|
||||||
|
for (let i = 0; i < 1000; i += 1) {
|
||||||
|
obj[`idx${i}`] = `val${i}`;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const createEvent = () => ({
|
||||||
|
Action: 'Invoke',
|
||||||
|
body: JSON.stringify({
|
||||||
|
method: 'POST',
|
||||||
|
path: '/',
|
||||||
|
headers: { 'content-type': 'application/json' },
|
||||||
|
encoding: undefined,
|
||||||
|
body: createBigJSONObj(),
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
|
const runTests = async (entrypoint, shouldAddHelpers = true, nb) => {
|
||||||
|
console.log(
|
||||||
|
`setting up files with entrypoint ${entrypoint} and ${
|
||||||
|
shouldAddHelpers ? 'helpers' : 'no helpers'
|
||||||
|
}`,
|
||||||
|
);
|
||||||
|
await setupFiles(entrypoint, shouldAddHelpers);
|
||||||
|
|
||||||
|
console.log('importing launcher');
|
||||||
|
const launcher = require('./lambda/launcher');
|
||||||
|
|
||||||
|
const event = createEvent();
|
||||||
|
const context = {};
|
||||||
|
|
||||||
|
const start = process.hrtime();
|
||||||
|
|
||||||
|
console.log(`throwing ${nb} events at lambda`);
|
||||||
|
for (let i = 0; i < nb; i += 1) {
|
||||||
|
// eslint-disable-next-line
|
||||||
|
await launcher.launcher(event, context);
|
||||||
|
}
|
||||||
|
const timer = process.hrtime(start);
|
||||||
|
const ms = (timer[0] * 1e9 + timer[1]) / 1e6;
|
||||||
|
|
||||||
|
await launcher.bridge.server.close();
|
||||||
|
delete require.cache[require.resolve('./lambda/launcher')];
|
||||||
|
|
||||||
|
console.log({ nb, sum: ms, avg: ms / nb });
|
||||||
|
};
|
||||||
|
|
||||||
|
const main = async () => {
|
||||||
|
if (process.argv.length !== 5) {
|
||||||
|
console.log(
|
||||||
|
'usage : node run.js <entrypoint-file> <add-helpers> <nb-of-request>',
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const [, , entrypoint, helpers, nbRequests] = process.argv;
|
||||||
|
const shouldAddHelpers = helpers !== 'false' && helpers !== 'no';
|
||||||
|
const nb = Number(nbRequests);
|
||||||
|
|
||||||
|
await runTests(entrypoint, shouldAddHelpers, nb);
|
||||||
|
};
|
||||||
|
|
||||||
|
main();
|
||||||
378
packages/now-node/bench/yarn.lock
Normal file
378
packages/now-node/bench/yarn.lock
Normal file
@@ -0,0 +1,378 @@
|
|||||||
|
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
|
||||||
|
# yarn lockfile v1
|
||||||
|
|
||||||
|
|
||||||
|
accepts@~1.3.7:
|
||||||
|
version "1.3.7"
|
||||||
|
resolved "https://registry.yarnpkg.com/accepts/-/accepts-1.3.7.tgz#531bc726517a3b2b41f850021c6cc15eaab507cd"
|
||||||
|
integrity sha512-Il80Qs2WjYlJIBNzNkK6KYqlVMTbZLXgHx2oT0pU/fjRHyEp+PEfEPY0R3WCwAGVOtauxh1hOxNgIf5bv7dQpA==
|
||||||
|
dependencies:
|
||||||
|
mime-types "~2.1.24"
|
||||||
|
negotiator "0.6.2"
|
||||||
|
|
||||||
|
array-flatten@1.1.1:
|
||||||
|
version "1.1.1"
|
||||||
|
resolved "https://registry.yarnpkg.com/array-flatten/-/array-flatten-1.1.1.tgz#9a5f699051b1e7073328f2a008968b64ea2955d2"
|
||||||
|
integrity sha1-ml9pkFGx5wczKPKgCJaLZOopVdI=
|
||||||
|
|
||||||
|
body-parser@1.19.0:
|
||||||
|
version "1.19.0"
|
||||||
|
resolved "https://registry.yarnpkg.com/body-parser/-/body-parser-1.19.0.tgz#96b2709e57c9c4e09a6fd66a8fd979844f69f08a"
|
||||||
|
integrity sha512-dhEPs72UPbDnAQJ9ZKMNTP6ptJaionhP5cBb541nXPlW60Jepo9RV/a4fX4XWW9CuFNK22krhrj1+rgzifNCsw==
|
||||||
|
dependencies:
|
||||||
|
bytes "3.1.0"
|
||||||
|
content-type "~1.0.4"
|
||||||
|
debug "2.6.9"
|
||||||
|
depd "~1.1.2"
|
||||||
|
http-errors "1.7.2"
|
||||||
|
iconv-lite "0.4.24"
|
||||||
|
on-finished "~2.3.0"
|
||||||
|
qs "6.7.0"
|
||||||
|
raw-body "2.4.0"
|
||||||
|
type-is "~1.6.17"
|
||||||
|
|
||||||
|
bytes@3.1.0:
|
||||||
|
version "3.1.0"
|
||||||
|
resolved "https://registry.yarnpkg.com/bytes/-/bytes-3.1.0.tgz#f6cf7933a360e0588fa9fde85651cdc7f805d1f6"
|
||||||
|
integrity sha512-zauLjrfCG+xvoyaqLoV8bLVXXNGC4JqlxFCutSDWA6fJrTo2ZuvLYTqZ7aHBLZSMOopbzwv8f+wZcVzfVTI2Dg==
|
||||||
|
|
||||||
|
content-disposition@0.5.3:
|
||||||
|
version "0.5.3"
|
||||||
|
resolved "https://registry.yarnpkg.com/content-disposition/-/content-disposition-0.5.3.tgz#e130caf7e7279087c5616c2007d0485698984fbd"
|
||||||
|
integrity sha512-ExO0774ikEObIAEV9kDo50o+79VCUdEB6n6lzKgGwupcVeRlhrj3qGAfwq8G6uBJjkqLrhT0qEYFcWng8z1z0g==
|
||||||
|
dependencies:
|
||||||
|
safe-buffer "5.1.2"
|
||||||
|
|
||||||
|
content-type@~1.0.4:
|
||||||
|
version "1.0.4"
|
||||||
|
resolved "https://registry.yarnpkg.com/content-type/-/content-type-1.0.4.tgz#e138cc75e040c727b1966fe5e5f8c9aee256fe3b"
|
||||||
|
integrity sha512-hIP3EEPs8tB9AT1L+NUqtwOAps4mk2Zob89MWXMHjHWg9milF/j4osnnQLXBCBFBk/tvIG/tUc9mOUJiPBhPXA==
|
||||||
|
|
||||||
|
cookie-signature@1.0.6:
|
||||||
|
version "1.0.6"
|
||||||
|
resolved "https://registry.yarnpkg.com/cookie-signature/-/cookie-signature-1.0.6.tgz#e303a882b342cc3ee8ca513a79999734dab3ae2c"
|
||||||
|
integrity sha1-4wOogrNCzD7oylE6eZmXNNqzriw=
|
||||||
|
|
||||||
|
cookie@0.4.0:
|
||||||
|
version "0.4.0"
|
||||||
|
resolved "https://registry.yarnpkg.com/cookie/-/cookie-0.4.0.tgz#beb437e7022b3b6d49019d088665303ebe9c14ba"
|
||||||
|
integrity sha512-+Hp8fLp57wnUSt0tY0tHEXh4voZRDnoIrZPqlo3DPiI4y9lwg/jqx+1Om94/W6ZaPDOUbnjOt/99w66zk+l1Xg==
|
||||||
|
|
||||||
|
debug@2.6.9:
|
||||||
|
version "2.6.9"
|
||||||
|
resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.9.tgz#5d128515df134ff327e90a4c93f4e077a536341f"
|
||||||
|
integrity sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==
|
||||||
|
dependencies:
|
||||||
|
ms "2.0.0"
|
||||||
|
|
||||||
|
depd@~1.1.2:
|
||||||
|
version "1.1.2"
|
||||||
|
resolved "https://registry.yarnpkg.com/depd/-/depd-1.1.2.tgz#9bcd52e14c097763e749b274c4346ed2e560b5a9"
|
||||||
|
integrity sha1-m81S4UwJd2PnSbJ0xDRu0uVgtak=
|
||||||
|
|
||||||
|
destroy@~1.0.4:
|
||||||
|
version "1.0.4"
|
||||||
|
resolved "https://registry.yarnpkg.com/destroy/-/destroy-1.0.4.tgz#978857442c44749e4206613e37946205826abd80"
|
||||||
|
integrity sha1-l4hXRCxEdJ5CBmE+N5RiBYJqvYA=
|
||||||
|
|
||||||
|
ee-first@1.1.1:
|
||||||
|
version "1.1.1"
|
||||||
|
resolved "https://registry.yarnpkg.com/ee-first/-/ee-first-1.1.1.tgz#590c61156b0ae2f4f0255732a158b266bc56b21d"
|
||||||
|
integrity sha1-WQxhFWsK4vTwJVcyoViyZrxWsh0=
|
||||||
|
|
||||||
|
encodeurl@~1.0.2:
|
||||||
|
version "1.0.2"
|
||||||
|
resolved "https://registry.yarnpkg.com/encodeurl/-/encodeurl-1.0.2.tgz#ad3ff4c86ec2d029322f5a02c3a9a606c95b3f59"
|
||||||
|
integrity sha1-rT/0yG7C0CkyL1oCw6mmBslbP1k=
|
||||||
|
|
||||||
|
escape-html@~1.0.3:
|
||||||
|
version "1.0.3"
|
||||||
|
resolved "https://registry.yarnpkg.com/escape-html/-/escape-html-1.0.3.tgz#0258eae4d3d0c0974de1c169188ef0051d1d1988"
|
||||||
|
integrity sha1-Aljq5NPQwJdN4cFpGI7wBR0dGYg=
|
||||||
|
|
||||||
|
etag@~1.8.1:
|
||||||
|
version "1.8.1"
|
||||||
|
resolved "https://registry.yarnpkg.com/etag/-/etag-1.8.1.tgz#41ae2eeb65efa62268aebfea83ac7d79299b0887"
|
||||||
|
integrity sha1-Qa4u62XvpiJorr/qg6x9eSmbCIc=
|
||||||
|
|
||||||
|
express@4.17.1:
|
||||||
|
version "4.17.1"
|
||||||
|
resolved "https://registry.yarnpkg.com/express/-/express-4.17.1.tgz#4491fc38605cf51f8629d39c2b5d026f98a4c134"
|
||||||
|
integrity sha512-mHJ9O79RqluphRrcw2X/GTh3k9tVv8YcoyY4Kkh4WDMUYKRZUq0h1o0w2rrrxBqM7VoeUVqgb27xlEMXTnYt4g==
|
||||||
|
dependencies:
|
||||||
|
accepts "~1.3.7"
|
||||||
|
array-flatten "1.1.1"
|
||||||
|
body-parser "1.19.0"
|
||||||
|
content-disposition "0.5.3"
|
||||||
|
content-type "~1.0.4"
|
||||||
|
cookie "0.4.0"
|
||||||
|
cookie-signature "1.0.6"
|
||||||
|
debug "2.6.9"
|
||||||
|
depd "~1.1.2"
|
||||||
|
encodeurl "~1.0.2"
|
||||||
|
escape-html "~1.0.3"
|
||||||
|
etag "~1.8.1"
|
||||||
|
finalhandler "~1.1.2"
|
||||||
|
fresh "0.5.2"
|
||||||
|
merge-descriptors "1.0.1"
|
||||||
|
methods "~1.1.2"
|
||||||
|
on-finished "~2.3.0"
|
||||||
|
parseurl "~1.3.3"
|
||||||
|
path-to-regexp "0.1.7"
|
||||||
|
proxy-addr "~2.0.5"
|
||||||
|
qs "6.7.0"
|
||||||
|
range-parser "~1.2.1"
|
||||||
|
safe-buffer "5.1.2"
|
||||||
|
send "0.17.1"
|
||||||
|
serve-static "1.14.1"
|
||||||
|
setprototypeof "1.1.1"
|
||||||
|
statuses "~1.5.0"
|
||||||
|
type-is "~1.6.18"
|
||||||
|
utils-merge "1.0.1"
|
||||||
|
vary "~1.1.2"
|
||||||
|
|
||||||
|
finalhandler@~1.1.2:
|
||||||
|
version "1.1.2"
|
||||||
|
resolved "https://registry.yarnpkg.com/finalhandler/-/finalhandler-1.1.2.tgz#b7e7d000ffd11938d0fdb053506f6ebabe9f587d"
|
||||||
|
integrity sha512-aAWcW57uxVNrQZqFXjITpW3sIUQmHGG3qSb9mUah9MgMC4NeWhNOlNjXEYq3HjRAvL6arUviZGGJsBg6z0zsWA==
|
||||||
|
dependencies:
|
||||||
|
debug "2.6.9"
|
||||||
|
encodeurl "~1.0.2"
|
||||||
|
escape-html "~1.0.3"
|
||||||
|
on-finished "~2.3.0"
|
||||||
|
parseurl "~1.3.3"
|
||||||
|
statuses "~1.5.0"
|
||||||
|
unpipe "~1.0.0"
|
||||||
|
|
||||||
|
forwarded@~0.1.2:
|
||||||
|
version "0.1.2"
|
||||||
|
resolved "https://registry.yarnpkg.com/forwarded/-/forwarded-0.1.2.tgz#98c23dab1175657b8c0573e8ceccd91b0ff18c84"
|
||||||
|
integrity sha1-mMI9qxF1ZXuMBXPozszZGw/xjIQ=
|
||||||
|
|
||||||
|
fresh@0.5.2:
|
||||||
|
version "0.5.2"
|
||||||
|
resolved "https://registry.yarnpkg.com/fresh/-/fresh-0.5.2.tgz#3d8cadd90d976569fa835ab1f8e4b23a105605a7"
|
||||||
|
integrity sha1-PYyt2Q2XZWn6g1qx+OSyOhBWBac=
|
||||||
|
|
||||||
|
fs-extra@8.0.1:
|
||||||
|
version "8.0.1"
|
||||||
|
resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-8.0.1.tgz#90294081f978b1f182f347a440a209154344285b"
|
||||||
|
integrity sha512-W+XLrggcDzlle47X/XnS7FXrXu9sDo+Ze9zpndeBxdgv88FHLm1HtmkhEwavruS6koanBjp098rUpHs65EmG7A==
|
||||||
|
dependencies:
|
||||||
|
graceful-fs "^4.1.2"
|
||||||
|
jsonfile "^4.0.0"
|
||||||
|
universalify "^0.1.0"
|
||||||
|
|
||||||
|
graceful-fs@^4.1.2, graceful-fs@^4.1.6:
|
||||||
|
version "4.1.15"
|
||||||
|
resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.1.15.tgz#ffb703e1066e8a0eeaa4c8b80ba9253eeefbfb00"
|
||||||
|
integrity sha512-6uHUhOPEBgQ24HM+r6b/QwWfZq+yiFcipKFrOFiBEnWdy5sdzYoi+pJeQaPI5qOLRFqWmAXUPQNsielzdLoecA==
|
||||||
|
|
||||||
|
http-errors@1.7.2, http-errors@~1.7.2:
|
||||||
|
version "1.7.2"
|
||||||
|
resolved "https://registry.yarnpkg.com/http-errors/-/http-errors-1.7.2.tgz#4f5029cf13239f31036e5b2e55292bcfbcc85c8f"
|
||||||
|
integrity sha512-uUQBt3H/cSIVfch6i1EuPNy/YsRSOUBXTVfZ+yR7Zjez3qjBz6i9+i4zjNaoqcoFVI4lQJ5plg63TvGfRSDCRg==
|
||||||
|
dependencies:
|
||||||
|
depd "~1.1.2"
|
||||||
|
inherits "2.0.3"
|
||||||
|
setprototypeof "1.1.1"
|
||||||
|
statuses ">= 1.5.0 < 2"
|
||||||
|
toidentifier "1.0.0"
|
||||||
|
|
||||||
|
iconv-lite@0.4.24:
|
||||||
|
version "0.4.24"
|
||||||
|
resolved "https://registry.yarnpkg.com/iconv-lite/-/iconv-lite-0.4.24.tgz#2022b4b25fbddc21d2f524974a474aafe733908b"
|
||||||
|
integrity sha512-v3MXnZAcvnywkTUEZomIActle7RXXeedOR31wwl7VlyoXO4Qi9arvSenNQWne1TcRwhCL1HwLI21bEqdpj8/rA==
|
||||||
|
dependencies:
|
||||||
|
safer-buffer ">= 2.1.2 < 3"
|
||||||
|
|
||||||
|
inherits@2.0.3:
|
||||||
|
version "2.0.3"
|
||||||
|
resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.3.tgz#633c2c83e3da42a502f52466022480f4208261de"
|
||||||
|
integrity sha1-Yzwsg+PaQqUC9SRmAiSA9CCCYd4=
|
||||||
|
|
||||||
|
ipaddr.js@1.9.0:
|
||||||
|
version "1.9.0"
|
||||||
|
resolved "https://registry.yarnpkg.com/ipaddr.js/-/ipaddr.js-1.9.0.tgz#37df74e430a0e47550fe54a2defe30d8acd95f65"
|
||||||
|
integrity sha512-M4Sjn6N/+O6/IXSJseKqHoFc+5FdGJ22sXqnjTpdZweHK64MzEPAyQZyEU3R/KRv2GLoa7nNtg/C2Ev6m7z+eA==
|
||||||
|
|
||||||
|
jsonfile@^4.0.0:
|
||||||
|
version "4.0.0"
|
||||||
|
resolved "https://registry.yarnpkg.com/jsonfile/-/jsonfile-4.0.0.tgz#8771aae0799b64076b76640fca058f9c10e33ecb"
|
||||||
|
integrity sha1-h3Gq4HmbZAdrdmQPygWPnBDjPss=
|
||||||
|
optionalDependencies:
|
||||||
|
graceful-fs "^4.1.6"
|
||||||
|
|
||||||
|
media-typer@0.3.0:
|
||||||
|
version "0.3.0"
|
||||||
|
resolved "https://registry.yarnpkg.com/media-typer/-/media-typer-0.3.0.tgz#8710d7af0aa626f8fffa1ce00168545263255748"
|
||||||
|
integrity sha1-hxDXrwqmJvj/+hzgAWhUUmMlV0g=
|
||||||
|
|
||||||
|
merge-descriptors@1.0.1:
|
||||||
|
version "1.0.1"
|
||||||
|
resolved "https://registry.yarnpkg.com/merge-descriptors/-/merge-descriptors-1.0.1.tgz#b00aaa556dd8b44568150ec9d1b953f3f90cbb61"
|
||||||
|
integrity sha1-sAqqVW3YtEVoFQ7J0blT8/kMu2E=
|
||||||
|
|
||||||
|
methods@~1.1.2:
|
||||||
|
version "1.1.2"
|
||||||
|
resolved "https://registry.yarnpkg.com/methods/-/methods-1.1.2.tgz#5529a4d67654134edcc5266656835b0f851afcee"
|
||||||
|
integrity sha1-VSmk1nZUE07cxSZmVoNbD4Ua/O4=
|
||||||
|
|
||||||
|
mime-db@1.40.0:
|
||||||
|
version "1.40.0"
|
||||||
|
resolved "https://registry.yarnpkg.com/mime-db/-/mime-db-1.40.0.tgz#a65057e998db090f732a68f6c276d387d4126c32"
|
||||||
|
integrity sha512-jYdeOMPy9vnxEqFRRo6ZvTZ8d9oPb+k18PKoYNYUe2stVEBPPwsln/qWzdbmaIvnhZ9v2P+CuecK+fpUfsV2mA==
|
||||||
|
|
||||||
|
mime-types@~2.1.24:
|
||||||
|
version "2.1.24"
|
||||||
|
resolved "https://registry.yarnpkg.com/mime-types/-/mime-types-2.1.24.tgz#b6f8d0b3e951efb77dedeca194cff6d16f676f81"
|
||||||
|
integrity sha512-WaFHS3MCl5fapm3oLxU4eYDw77IQM2ACcxQ9RIxfaC3ooc6PFuBMGZZsYpvoXS5D5QTWPieo1jjLdAm3TBP3cQ==
|
||||||
|
dependencies:
|
||||||
|
mime-db "1.40.0"
|
||||||
|
|
||||||
|
mime@1.6.0:
|
||||||
|
version "1.6.0"
|
||||||
|
resolved "https://registry.yarnpkg.com/mime/-/mime-1.6.0.tgz#32cd9e5c64553bd58d19a568af452acff04981b1"
|
||||||
|
integrity sha512-x0Vn8spI+wuJ1O6S7gnbaQg8Pxh4NNHb7KSINmEWKiPE4RKOplvijn+NkmYmmRgP68mc70j2EbeTFRsrswaQeg==
|
||||||
|
|
||||||
|
ms@2.0.0:
|
||||||
|
version "2.0.0"
|
||||||
|
resolved "https://registry.yarnpkg.com/ms/-/ms-2.0.0.tgz#5608aeadfc00be6c2901df5f9861788de0d597c8"
|
||||||
|
integrity sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=
|
||||||
|
|
||||||
|
ms@2.1.1:
|
||||||
|
version "2.1.1"
|
||||||
|
resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.1.tgz#30a5864eb3ebb0a66f2ebe6d727af06a09d86e0a"
|
||||||
|
integrity sha512-tgp+dl5cGk28utYktBsrFqA7HKgrhgPsg6Z/EfhWI4gl1Hwq8B/GmY/0oXZ6nF8hDVesS/FpnYaD/kOWhYQvyg==
|
||||||
|
|
||||||
|
negotiator@0.6.2:
|
||||||
|
version "0.6.2"
|
||||||
|
resolved "https://registry.yarnpkg.com/negotiator/-/negotiator-0.6.2.tgz#feacf7ccf525a77ae9634436a64883ffeca346fb"
|
||||||
|
integrity sha512-hZXc7K2e+PgeI1eDBe/10Ard4ekbfrrqG8Ep+8Jmf4JID2bNg7NvCPOZN+kfF574pFQI7mum2AUqDidoKqcTOw==
|
||||||
|
|
||||||
|
on-finished@~2.3.0:
|
||||||
|
version "2.3.0"
|
||||||
|
resolved "https://registry.yarnpkg.com/on-finished/-/on-finished-2.3.0.tgz#20f1336481b083cd75337992a16971aa2d906947"
|
||||||
|
integrity sha1-IPEzZIGwg811M3mSoWlxqi2QaUc=
|
||||||
|
dependencies:
|
||||||
|
ee-first "1.1.1"
|
||||||
|
|
||||||
|
parseurl@~1.3.3:
|
||||||
|
version "1.3.3"
|
||||||
|
resolved "https://registry.yarnpkg.com/parseurl/-/parseurl-1.3.3.tgz#9da19e7bee8d12dff0513ed5b76957793bc2e8d4"
|
||||||
|
integrity sha512-CiyeOxFT/JZyN5m0z9PfXw4SCBJ6Sygz1Dpl0wqjlhDEGGBP1GnsUVEL0p63hoG1fcj3fHynXi9NYO4nWOL+qQ==
|
||||||
|
|
||||||
|
path-to-regexp@0.1.7:
|
||||||
|
version "0.1.7"
|
||||||
|
resolved "https://registry.yarnpkg.com/path-to-regexp/-/path-to-regexp-0.1.7.tgz#df604178005f522f15eb4490e7247a1bfaa67f8c"
|
||||||
|
integrity sha1-32BBeABfUi8V60SQ5yR6G/qmf4w=
|
||||||
|
|
||||||
|
proxy-addr@~2.0.5:
|
||||||
|
version "2.0.5"
|
||||||
|
resolved "https://registry.yarnpkg.com/proxy-addr/-/proxy-addr-2.0.5.tgz#34cbd64a2d81f4b1fd21e76f9f06c8a45299ee34"
|
||||||
|
integrity sha512-t/7RxHXPH6cJtP0pRG6smSr9QJidhB+3kXu0KgXnbGYMgzEnUxRQ4/LDdfOwZEMyIh3/xHb8PX3t+lfL9z+YVQ==
|
||||||
|
dependencies:
|
||||||
|
forwarded "~0.1.2"
|
||||||
|
ipaddr.js "1.9.0"
|
||||||
|
|
||||||
|
qs@6.7.0:
|
||||||
|
version "6.7.0"
|
||||||
|
resolved "https://registry.yarnpkg.com/qs/-/qs-6.7.0.tgz#41dc1a015e3d581f1621776be31afb2876a9b1bc"
|
||||||
|
integrity sha512-VCdBRNFTX1fyE7Nb6FYoURo/SPe62QCaAyzJvUjwRaIsc+NePBEniHlvxFmmX56+HZphIGtV0XeCirBtpDrTyQ==
|
||||||
|
|
||||||
|
range-parser@~1.2.1:
|
||||||
|
version "1.2.1"
|
||||||
|
resolved "https://registry.yarnpkg.com/range-parser/-/range-parser-1.2.1.tgz#3cf37023d199e1c24d1a55b84800c2f3e6468031"
|
||||||
|
integrity sha512-Hrgsx+orqoygnmhFbKaHE6c296J+HTAQXoxEF6gNupROmmGJRoyzfG3ccAveqCBrwr/2yxQ5BVd/GTl5agOwSg==
|
||||||
|
|
||||||
|
raw-body@2.4.0:
|
||||||
|
version "2.4.0"
|
||||||
|
resolved "https://registry.yarnpkg.com/raw-body/-/raw-body-2.4.0.tgz#a1ce6fb9c9bc356ca52e89256ab59059e13d0332"
|
||||||
|
integrity sha512-4Oz8DUIwdvoa5qMJelxipzi/iJIi40O5cGV1wNYp5hvZP8ZN0T+jiNkL0QepXs+EsQ9XJ8ipEDoiH70ySUJP3Q==
|
||||||
|
dependencies:
|
||||||
|
bytes "3.1.0"
|
||||||
|
http-errors "1.7.2"
|
||||||
|
iconv-lite "0.4.24"
|
||||||
|
unpipe "1.0.0"
|
||||||
|
|
||||||
|
safe-buffer@5.1.2:
|
||||||
|
version "5.1.2"
|
||||||
|
resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.1.2.tgz#991ec69d296e0313747d59bdfd2b745c35f8828d"
|
||||||
|
integrity sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==
|
||||||
|
|
||||||
|
"safer-buffer@>= 2.1.2 < 3":
|
||||||
|
version "2.1.2"
|
||||||
|
resolved "https://registry.yarnpkg.com/safer-buffer/-/safer-buffer-2.1.2.tgz#44fa161b0187b9549dd84bb91802f9bd8385cd6a"
|
||||||
|
integrity sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==
|
||||||
|
|
||||||
|
send@0.17.1:
|
||||||
|
version "0.17.1"
|
||||||
|
resolved "https://registry.yarnpkg.com/send/-/send-0.17.1.tgz#c1d8b059f7900f7466dd4938bdc44e11ddb376c8"
|
||||||
|
integrity sha512-BsVKsiGcQMFwT8UxypobUKyv7irCNRHk1T0G680vk88yf6LBByGcZJOTJCrTP2xVN6yI+XjPJcNuE3V4fT9sAg==
|
||||||
|
dependencies:
|
||||||
|
debug "2.6.9"
|
||||||
|
depd "~1.1.2"
|
||||||
|
destroy "~1.0.4"
|
||||||
|
encodeurl "~1.0.2"
|
||||||
|
escape-html "~1.0.3"
|
||||||
|
etag "~1.8.1"
|
||||||
|
fresh "0.5.2"
|
||||||
|
http-errors "~1.7.2"
|
||||||
|
mime "1.6.0"
|
||||||
|
ms "2.1.1"
|
||||||
|
on-finished "~2.3.0"
|
||||||
|
range-parser "~1.2.1"
|
||||||
|
statuses "~1.5.0"
|
||||||
|
|
||||||
|
serve-static@1.14.1:
|
||||||
|
version "1.14.1"
|
||||||
|
resolved "https://registry.yarnpkg.com/serve-static/-/serve-static-1.14.1.tgz#666e636dc4f010f7ef29970a88a674320898b2f9"
|
||||||
|
integrity sha512-JMrvUwE54emCYWlTI+hGrGv5I8dEwmco/00EvkzIIsR7MqrHonbD9pO2MOfFnpFntl7ecpZs+3mW+XbQZu9QCg==
|
||||||
|
dependencies:
|
||||||
|
encodeurl "~1.0.2"
|
||||||
|
escape-html "~1.0.3"
|
||||||
|
parseurl "~1.3.3"
|
||||||
|
send "0.17.1"
|
||||||
|
|
||||||
|
setprototypeof@1.1.1:
|
||||||
|
version "1.1.1"
|
||||||
|
resolved "https://registry.yarnpkg.com/setprototypeof/-/setprototypeof-1.1.1.tgz#7e95acb24aa92f5885e0abef5ba131330d4ae683"
|
||||||
|
integrity sha512-JvdAWfbXeIGaZ9cILp38HntZSFSo3mWg6xGcJJsd+d4aRMOqauag1C63dJfDw7OaMYwEbHMOxEZ1lqVRYP2OAw==
|
||||||
|
|
||||||
|
"statuses@>= 1.5.0 < 2", statuses@~1.5.0:
|
||||||
|
version "1.5.0"
|
||||||
|
resolved "https://registry.yarnpkg.com/statuses/-/statuses-1.5.0.tgz#161c7dac177659fd9811f43771fa99381478628c"
|
||||||
|
integrity sha1-Fhx9rBd2Wf2YEfQ3cfqZOBR4Yow=
|
||||||
|
|
||||||
|
toidentifier@1.0.0:
|
||||||
|
version "1.0.0"
|
||||||
|
resolved "https://registry.yarnpkg.com/toidentifier/-/toidentifier-1.0.0.tgz#7e1be3470f1e77948bc43d94a3c8f4d7752ba553"
|
||||||
|
integrity sha512-yaOH/Pk/VEhBWWTlhI+qXxDFXlejDGcQipMlyxda9nthulaxLZUNcUqFxokp0vcYnvteJln5FNQDRrxj3YcbVw==
|
||||||
|
|
||||||
|
type-is@~1.6.17, type-is@~1.6.18:
|
||||||
|
version "1.6.18"
|
||||||
|
resolved "https://registry.yarnpkg.com/type-is/-/type-is-1.6.18.tgz#4e552cd05df09467dcbc4ef739de89f2cf37c131"
|
||||||
|
integrity sha512-TkRKr9sUTxEH8MdfuCSP7VizJyzRNMjj2J2do2Jr3Kym598JVdEksuzPQCnlFPW4ky9Q+iA+ma9BGm06XQBy8g==
|
||||||
|
dependencies:
|
||||||
|
media-typer "0.3.0"
|
||||||
|
mime-types "~2.1.24"
|
||||||
|
|
||||||
|
universalify@^0.1.0:
|
||||||
|
version "0.1.2"
|
||||||
|
resolved "https://registry.yarnpkg.com/universalify/-/universalify-0.1.2.tgz#b646f69be3942dabcecc9d6639c80dc105efaa66"
|
||||||
|
integrity sha512-rBJeI5CXAlmy1pV+617WB9J63U6XcazHHF2f2dbJix4XzpUF0RS3Zbj0FGIOCAva5P/d/GBOYaACQ1w+0azUkg==
|
||||||
|
|
||||||
|
unpipe@1.0.0, unpipe@~1.0.0:
|
||||||
|
version "1.0.0"
|
||||||
|
resolved "https://registry.yarnpkg.com/unpipe/-/unpipe-1.0.0.tgz#b2bf4ee8514aae6165b4817829d21b2ef49904ec"
|
||||||
|
integrity sha1-sr9O6FFKrmFltIF4KdIbLvSZBOw=
|
||||||
|
|
||||||
|
utils-merge@1.0.1:
|
||||||
|
version "1.0.1"
|
||||||
|
resolved "https://registry.yarnpkg.com/utils-merge/-/utils-merge-1.0.1.tgz#9f95710f50a267947b2ccc124741c1028427e713"
|
||||||
|
integrity sha1-n5VxD1CiZ5R7LMwSR0HBAoQn5xM=
|
||||||
|
|
||||||
|
vary@~1.1.2:
|
||||||
|
version "1.1.2"
|
||||||
|
resolved "https://registry.yarnpkg.com/vary/-/vary-1.1.2.tgz#2299f02c6ded30d4a5961b0b9f74524a18f634fc"
|
||||||
|
integrity sha1-IpnwLG3tMNSllhsLn3RSShj2NPw=
|
||||||
@@ -9,4 +9,21 @@ if [ ! -e "$bridge_defs" ]; then
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
cp -v "$bridge_defs" src
|
cp -v "$bridge_defs" src
|
||||||
|
|
||||||
|
# build ts files
|
||||||
tsc
|
tsc
|
||||||
|
|
||||||
|
# todo: improve
|
||||||
|
# copy type file for ts test
|
||||||
|
cp dist/types.d.ts test/fixtures/15-helpers/ts/types.d.ts
|
||||||
|
|
||||||
|
# use types.d.ts as the main types export
|
||||||
|
mv dist/types.d.ts dist/types
|
||||||
|
rm dist/*.d.ts
|
||||||
|
mv dist/types dist/index.d.ts
|
||||||
|
|
||||||
|
# bundle helpers.ts with ncc
|
||||||
|
rm dist/helpers.js
|
||||||
|
ncc build src/helpers.ts -o dist/helpers
|
||||||
|
mv dist/helpers/index.js dist/helpers.js
|
||||||
|
rm -rf dist/helpers
|
||||||
|
|||||||
@@ -1,17 +1,19 @@
|
|||||||
{
|
{
|
||||||
"name": "@now/node",
|
"name": "@now/node",
|
||||||
"version": "0.7.4",
|
"version": "0.11.1",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"main": "./dist/index",
|
"main": "./dist/index",
|
||||||
|
"homepage": "https://zeit.co/docs/v2/deployments/official-builders/node-js-now-node",
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "https://github.com/zeit/now-builders.git",
|
"url": "https://github.com/zeit/now-builders.git",
|
||||||
"directory": "packages/now-node"
|
"directory": "packages/now-node"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@now/node-bridge": "^1.1.4",
|
"@now/node-bridge": "1.2.2",
|
||||||
|
"@types/node": "*",
|
||||||
"@zeit/ncc": "0.18.5",
|
"@zeit/ncc": "0.18.5",
|
||||||
"fs-extra": "7.0.1"
|
"@zeit/ncc-watcher": "1.0.3"
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "./build.sh",
|
"build": "./build.sh",
|
||||||
@@ -22,8 +24,15 @@
|
|||||||
"dist"
|
"dist"
|
||||||
],
|
],
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/node": "11.9.4",
|
"@types/content-type": "1.1.3",
|
||||||
"jest": "24.1.0",
|
"@types/cookie": "0.3.3",
|
||||||
"typescript": "3.3.3"
|
"@types/etag": "1.8.0",
|
||||||
|
"@types/test-listen": "1.1.0",
|
||||||
|
"content-type": "1.0.4",
|
||||||
|
"cookie": "0.4.0",
|
||||||
|
"etag": "1.8.1",
|
||||||
|
"node-fetch": "2.6.0",
|
||||||
|
"test-listen": "1.1.0",
|
||||||
|
"typescript": "3.5.2"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
3
packages/now-node/src/example-import.ts
Normal file
3
packages/now-node/src/example-import.ts
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
// we intentionally import these types here
|
||||||
|
// to test that they are exported from index
|
||||||
|
import { NowRequest, NowResponse } from './index';
|
||||||
274
packages/now-node/src/helpers.ts
Normal file
274
packages/now-node/src/helpers.ts
Normal file
@@ -0,0 +1,274 @@
|
|||||||
|
import {
|
||||||
|
NowRequest,
|
||||||
|
NowResponse,
|
||||||
|
NowRequestCookies,
|
||||||
|
NowRequestQuery,
|
||||||
|
NowRequestBody,
|
||||||
|
} from './types';
|
||||||
|
import { Server } from 'http';
|
||||||
|
import { Bridge } from './bridge';
|
||||||
|
|
||||||
|
function getBodyParser(req: NowRequest, body: Buffer) {
|
||||||
|
return function parseBody(): NowRequestBody {
|
||||||
|
if (!req.headers['content-type']) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
const { parse: parseCT } = require('content-type');
|
||||||
|
const { type } = parseCT(req.headers['content-type']);
|
||||||
|
|
||||||
|
if (type === 'application/json') {
|
||||||
|
try {
|
||||||
|
return JSON.parse(body.toString());
|
||||||
|
} catch (error) {
|
||||||
|
throw new ApiError(400, 'Invalid JSON');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (type === 'application/octet-stream') {
|
||||||
|
return body;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (type === 'application/x-www-form-urlencoded') {
|
||||||
|
const { parse: parseQS } = require('querystring');
|
||||||
|
// remark : querystring.parse does not produce an iterable object
|
||||||
|
// https://nodejs.org/api/querystring.html#querystring_querystring_parse_str_sep_eq_options
|
||||||
|
return parseQS(body.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (type === 'text/plain') {
|
||||||
|
return body.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
return undefined;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function getQueryParser({ url = '/' }: NowRequest) {
|
||||||
|
return function parseQuery(): NowRequestQuery {
|
||||||
|
const { URL } = require('url');
|
||||||
|
// we provide a placeholder base url because we only want searchParams
|
||||||
|
const params = new URL(url, 'https://n').searchParams;
|
||||||
|
|
||||||
|
const query: { [key: string]: string | string[] } = {};
|
||||||
|
for (const [key, value] of params) {
|
||||||
|
query[key] = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
return query;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function getCookieParser(req: NowRequest) {
|
||||||
|
return function parseCookie(): NowRequestCookies {
|
||||||
|
const header: undefined | string | string[] = req.headers.cookie;
|
||||||
|
|
||||||
|
if (!header) {
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
const { parse } = require('cookie');
|
||||||
|
return parse(Array.isArray(header) ? header.join(';') : header);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function status(res: NowResponse, statusCode: number): NowResponse {
|
||||||
|
res.statusCode = statusCode;
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
function setCharset(type: string, charset: string) {
|
||||||
|
const { parse, format } = require('content-type');
|
||||||
|
const parsed = parse(type);
|
||||||
|
parsed.parameters.charset = charset;
|
||||||
|
return format(parsed);
|
||||||
|
}
|
||||||
|
|
||||||
|
function createETag(body: any, encoding: string | undefined) {
|
||||||
|
const etag = require('etag');
|
||||||
|
const buf = !Buffer.isBuffer(body) ? Buffer.from(body, encoding) : body;
|
||||||
|
return etag(buf, { weak: true });
|
||||||
|
}
|
||||||
|
|
||||||
|
function send(req: NowRequest, res: NowResponse, body: any): NowResponse {
|
||||||
|
let chunk: unknown = body;
|
||||||
|
let encoding: string | undefined;
|
||||||
|
|
||||||
|
switch (typeof chunk) {
|
||||||
|
// string defaulting to html
|
||||||
|
case 'string':
|
||||||
|
if (!res.getHeader('content-type')) {
|
||||||
|
res.setHeader('content-type', 'text/html');
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 'boolean':
|
||||||
|
case 'number':
|
||||||
|
case 'object':
|
||||||
|
if (chunk === null) {
|
||||||
|
chunk = '';
|
||||||
|
} else if (Buffer.isBuffer(chunk)) {
|
||||||
|
if (!res.getHeader('content-type')) {
|
||||||
|
res.setHeader('content-type', 'application/octet-stream');
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return json(req, res, chunk);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// write strings in utf-8
|
||||||
|
if (typeof chunk === 'string') {
|
||||||
|
encoding = 'utf8';
|
||||||
|
|
||||||
|
// reflect this in content-type
|
||||||
|
const type = res.getHeader('content-type');
|
||||||
|
if (typeof type === 'string') {
|
||||||
|
res.setHeader('content-type', setCharset(type, 'utf-8'));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// populate Content-Length
|
||||||
|
let len: number | undefined;
|
||||||
|
if (chunk !== undefined) {
|
||||||
|
if (Buffer.isBuffer(chunk)) {
|
||||||
|
// get length of Buffer
|
||||||
|
len = chunk.length;
|
||||||
|
} else if (typeof chunk === 'string') {
|
||||||
|
if (chunk.length < 1000) {
|
||||||
|
// just calculate length small chunk
|
||||||
|
len = Buffer.byteLength(chunk, encoding);
|
||||||
|
} else {
|
||||||
|
// convert chunk to Buffer and calculate
|
||||||
|
const buf = Buffer.from(chunk, encoding);
|
||||||
|
len = buf.length;
|
||||||
|
chunk = buf;
|
||||||
|
encoding = undefined;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
throw new Error(
|
||||||
|
'`body` is not a valid string, object, boolean, number, Stream, or Buffer'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (len !== undefined) {
|
||||||
|
res.setHeader('content-length', len);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// populate ETag
|
||||||
|
let etag: string | undefined;
|
||||||
|
if (
|
||||||
|
!res.getHeader('etag') &&
|
||||||
|
len !== undefined &&
|
||||||
|
(etag = createETag(chunk, encoding))
|
||||||
|
) {
|
||||||
|
res.setHeader('etag', etag);
|
||||||
|
}
|
||||||
|
|
||||||
|
// strip irrelevant headers
|
||||||
|
if (204 === res.statusCode || 304 === res.statusCode) {
|
||||||
|
res.removeHeader('Content-Type');
|
||||||
|
res.removeHeader('Content-Length');
|
||||||
|
res.removeHeader('Transfer-Encoding');
|
||||||
|
chunk = '';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.method === 'HEAD') {
|
||||||
|
// skip body for HEAD
|
||||||
|
res.end();
|
||||||
|
} else {
|
||||||
|
// respond
|
||||||
|
res.end(chunk, encoding);
|
||||||
|
}
|
||||||
|
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
function json(req: NowRequest, res: NowResponse, jsonBody: any): NowResponse {
|
||||||
|
const body = JSON.stringify(jsonBody);
|
||||||
|
|
||||||
|
// content-type
|
||||||
|
if (!res.getHeader('content-type')) {
|
||||||
|
res.setHeader('content-type', 'application/json; charset=utf-8');
|
||||||
|
}
|
||||||
|
|
||||||
|
return send(req, res, body);
|
||||||
|
}
|
||||||
|
|
||||||
|
export class ApiError extends Error {
|
||||||
|
readonly statusCode: number;
|
||||||
|
|
||||||
|
constructor(statusCode: number, message: string) {
|
||||||
|
super(message);
|
||||||
|
this.statusCode = statusCode;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function sendError(
|
||||||
|
res: NowResponse,
|
||||||
|
statusCode: number,
|
||||||
|
message: string
|
||||||
|
) {
|
||||||
|
res.statusCode = statusCode;
|
||||||
|
res.statusMessage = message;
|
||||||
|
res.end();
|
||||||
|
}
|
||||||
|
|
||||||
|
function setLazyProp<T>(req: NowRequest, prop: string, getter: () => T) {
|
||||||
|
const opts = { configurable: true, enumerable: true };
|
||||||
|
const optsReset = { ...opts, writable: true };
|
||||||
|
|
||||||
|
Object.defineProperty(req, prop, {
|
||||||
|
...opts,
|
||||||
|
get: () => {
|
||||||
|
const value = getter();
|
||||||
|
// we set the property on the object to avoid recalculating it
|
||||||
|
Object.defineProperty(req, prop, { ...optsReset, value });
|
||||||
|
return value;
|
||||||
|
},
|
||||||
|
set: value => {
|
||||||
|
Object.defineProperty(req, prop, { ...optsReset, value });
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export function createServerWithHelpers(
|
||||||
|
listener: (req: NowRequest, res: NowResponse) => void | Promise<void>,
|
||||||
|
bridge: Bridge
|
||||||
|
) {
|
||||||
|
const server = new Server(async (_req, _res) => {
|
||||||
|
const req = _req as NowRequest;
|
||||||
|
const res = _res as NowResponse;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const reqId = req.headers['x-now-bridge-request-id'];
|
||||||
|
|
||||||
|
// don't expose this header to the client
|
||||||
|
delete req.headers['x-now-bridge-request-id'];
|
||||||
|
|
||||||
|
if (typeof reqId !== 'string') {
|
||||||
|
throw new ApiError(500, 'Internal Server Error');
|
||||||
|
}
|
||||||
|
|
||||||
|
const event = bridge.consumeEvent(reqId);
|
||||||
|
|
||||||
|
setLazyProp<NowRequestCookies>(req, 'cookies', getCookieParser(req));
|
||||||
|
setLazyProp<NowRequestQuery>(req, 'query', getQueryParser(req));
|
||||||
|
setLazyProp<NowRequestBody>(req, 'body', getBodyParser(req, event.body));
|
||||||
|
|
||||||
|
res.status = statusCode => status(res, statusCode);
|
||||||
|
res.send = body => send(req, res, body);
|
||||||
|
res.json = jsonBody => json(req, res, jsonBody);
|
||||||
|
|
||||||
|
await listener(req, res);
|
||||||
|
} catch (err) {
|
||||||
|
if (err instanceof ApiError) {
|
||||||
|
sendError(res, err.statusCode, err.message);
|
||||||
|
} else {
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return server;
|
||||||
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
import { join, dirname, sep } from 'path';
|
import { Assets, NccOptions } from '@zeit/ncc';
|
||||||
import { readFile } from 'fs-extra';
|
import { join, dirname, relative, sep } from 'path';
|
||||||
|
import { NccWatcher, WatcherResult } from '@zeit/ncc-watcher';
|
||||||
import {
|
import {
|
||||||
glob,
|
glob,
|
||||||
download,
|
download,
|
||||||
@@ -10,10 +11,14 @@ import {
|
|||||||
createLambda,
|
createLambda,
|
||||||
runNpmInstall,
|
runNpmInstall,
|
||||||
runPackageJsonScript,
|
runPackageJsonScript,
|
||||||
|
getNodeVersion,
|
||||||
|
getSpawnOptions,
|
||||||
PrepareCacheOptions,
|
PrepareCacheOptions,
|
||||||
BuildOptions,
|
BuildOptions,
|
||||||
shouldServe,
|
shouldServe,
|
||||||
} from '@now/build-utils';
|
} from '@now/build-utils';
|
||||||
|
export { NowRequest, NowResponse } from './types';
|
||||||
|
import { makeLauncher } from './launcher';
|
||||||
|
|
||||||
interface CompilerConfig {
|
interface CompilerConfig {
|
||||||
includeFiles?: string | string[];
|
includeFiles?: string | string[];
|
||||||
@@ -23,8 +28,29 @@ interface DownloadOptions {
|
|||||||
files: Files;
|
files: Files;
|
||||||
entrypoint: string;
|
entrypoint: string;
|
||||||
workPath: string;
|
workPath: string;
|
||||||
meta?: Meta;
|
meta: Meta;
|
||||||
npmArguments?: string[];
|
}
|
||||||
|
|
||||||
|
const watchers: Map<string, NccWatcher> = new Map();
|
||||||
|
|
||||||
|
const LAUNCHER_FILENAME = '___now_launcher';
|
||||||
|
const BRIDGE_FILENAME = '___now_bridge';
|
||||||
|
const HELPERS_FILENAME = '___now_helpers';
|
||||||
|
|
||||||
|
function getWatcher(entrypoint: string, options: NccOptions): NccWatcher {
|
||||||
|
let watcher = watchers.get(entrypoint);
|
||||||
|
if (!watcher) {
|
||||||
|
watcher = new NccWatcher(entrypoint, options);
|
||||||
|
watchers.set(entrypoint, watcher);
|
||||||
|
}
|
||||||
|
return watcher;
|
||||||
|
}
|
||||||
|
|
||||||
|
function toBuffer(data: string | Buffer): Buffer {
|
||||||
|
if (typeof data === 'string') {
|
||||||
|
return Buffer.from(data, 'utf8');
|
||||||
|
}
|
||||||
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function downloadInstallAndBundle({
|
async function downloadInstallAndBundle({
|
||||||
@@ -32,32 +58,67 @@ async function downloadInstallAndBundle({
|
|||||||
entrypoint,
|
entrypoint,
|
||||||
workPath,
|
workPath,
|
||||||
meta,
|
meta,
|
||||||
npmArguments = [],
|
|
||||||
}: DownloadOptions) {
|
}: DownloadOptions) {
|
||||||
console.log('downloading user files...');
|
console.log('downloading user files...');
|
||||||
const downloadedFiles = await download(files, workPath, meta);
|
const downloadedFiles = await download(files, workPath, meta);
|
||||||
|
|
||||||
console.log("installing dependencies for user's code...");
|
console.log("installing dependencies for user's code...");
|
||||||
const entrypointFsDirname = join(workPath, dirname(entrypoint));
|
const entrypointFsDirname = join(workPath, dirname(entrypoint));
|
||||||
await runNpmInstall(entrypointFsDirname, npmArguments);
|
const nodeVersion = await getNodeVersion(entrypointFsDirname);
|
||||||
|
const spawnOpts = getSpawnOptions(meta, nodeVersion);
|
||||||
|
await runNpmInstall(entrypointFsDirname, ['--prefer-offline'], spawnOpts);
|
||||||
|
|
||||||
const entrypointPath = downloadedFiles[entrypoint].fsPath;
|
const entrypointPath = downloadedFiles[entrypoint].fsPath;
|
||||||
return { entrypointPath, entrypointFsDirname };
|
return { entrypointPath, entrypointFsDirname, nodeVersion, spawnOpts };
|
||||||
}
|
}
|
||||||
|
|
||||||
async function compile(
|
async function compile(
|
||||||
|
workPath: string,
|
||||||
entrypointPath: string,
|
entrypointPath: string,
|
||||||
entrypoint: string,
|
entrypoint: string,
|
||||||
config: CompilerConfig
|
config: CompilerConfig,
|
||||||
): Promise<Files> {
|
{ isDev, filesChanged, filesRemoved }: Meta
|
||||||
|
): Promise<{ preparedFiles: Files; watch: string[] }> {
|
||||||
const input = entrypointPath;
|
const input = entrypointPath;
|
||||||
const inputDir = dirname(input);
|
const inputDir = dirname(input);
|
||||||
const rootIncludeFiles = inputDir.split(sep).pop() || '';
|
const rootIncludeFiles = inputDir.split(sep).pop() || '';
|
||||||
|
const options: NccOptions = {
|
||||||
|
sourceMap: true,
|
||||||
|
sourceMapRegister: true,
|
||||||
|
};
|
||||||
|
let code: string;
|
||||||
|
let map: string | undefined;
|
||||||
|
let assets: Assets | undefined;
|
||||||
|
let watch: string[] = [];
|
||||||
|
if (isDev) {
|
||||||
|
const watcher = getWatcher(entrypointPath, options);
|
||||||
|
const result = await watcher.build(
|
||||||
|
Array.isArray(filesChanged)
|
||||||
|
? filesChanged.map(f => join(workPath, f))
|
||||||
|
: undefined,
|
||||||
|
Array.isArray(filesRemoved)
|
||||||
|
? filesRemoved.map(f => join(workPath, f))
|
||||||
|
: undefined
|
||||||
|
);
|
||||||
|
code = result.code;
|
||||||
|
map = result.map;
|
||||||
|
assets = result.assets;
|
||||||
|
watch = [...result.files, ...result.dirs, ...result.missing]
|
||||||
|
.filter(f => f.startsWith(workPath))
|
||||||
|
.map(f => relative(workPath, f))
|
||||||
|
.concat(Object.keys(assets || {}));
|
||||||
|
} else {
|
||||||
const ncc = require('@zeit/ncc');
|
const ncc = require('@zeit/ncc');
|
||||||
const { code, map, assets } = await ncc(input, {
|
const result = await ncc(input, {
|
||||||
sourceMap: true,
|
sourceMap: true,
|
||||||
sourceMapRegister: true,
|
sourceMapRegister: true,
|
||||||
});
|
});
|
||||||
|
code = result.code;
|
||||||
|
map = result.map;
|
||||||
|
assets = result.assets;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!assets) assets = {};
|
||||||
|
|
||||||
if (config && config.includeFiles) {
|
if (config && config.includeFiles) {
|
||||||
const includeFiles =
|
const includeFiles =
|
||||||
@@ -81,7 +142,7 @@ async function compile(
|
|||||||
}
|
}
|
||||||
|
|
||||||
assets[fullPath] = {
|
assets[fullPath] = {
|
||||||
source: data,
|
source: toBuffer(data),
|
||||||
permissions: mode,
|
permissions: mode,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -89,11 +150,15 @@ async function compile(
|
|||||||
}
|
}
|
||||||
|
|
||||||
const preparedFiles: Files = {};
|
const preparedFiles: Files = {};
|
||||||
// move all user code to 'user' subdirectory
|
|
||||||
preparedFiles[entrypoint] = new FileBlob({ data: code });
|
preparedFiles[entrypoint] = new FileBlob({ data: code });
|
||||||
|
|
||||||
|
if (map) {
|
||||||
preparedFiles[`${entrypoint.replace('.ts', '.js')}.map`] = new FileBlob({
|
preparedFiles[`${entrypoint.replace('.ts', '.js')}.map`] = new FileBlob({
|
||||||
data: map,
|
data: toBuffer(map),
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// move all user code to 'user' subdirectory
|
||||||
// eslint-disable-next-line no-restricted-syntax
|
// eslint-disable-next-line no-restricted-syntax
|
||||||
for (const assetName of Object.keys(assets)) {
|
for (const assetName of Object.keys(assets)) {
|
||||||
const { source: data, permissions: mode } = assets[assetName];
|
const { source: data, permissions: mode } = assets[assetName];
|
||||||
@@ -101,9 +166,11 @@ async function compile(
|
|||||||
preparedFiles[join(dirname(entrypoint), assetName)] = blob2;
|
preparedFiles[join(dirname(entrypoint), assetName)] = blob2;
|
||||||
}
|
}
|
||||||
|
|
||||||
return preparedFiles;
|
return { preparedFiles, watch };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const version = 2;
|
||||||
|
|
||||||
export const config = {
|
export const config = {
|
||||||
maxLambdaSize: '5mb',
|
maxLambdaSize: '5mb',
|
||||||
};
|
};
|
||||||
@@ -113,47 +180,69 @@ export async function build({
|
|||||||
entrypoint,
|
entrypoint,
|
||||||
workPath,
|
workPath,
|
||||||
config,
|
config,
|
||||||
meta,
|
meta = {},
|
||||||
}: BuildOptions) {
|
}: BuildOptions) {
|
||||||
|
const shouldAddHelpers = !(config && config.helpers === false);
|
||||||
|
|
||||||
const {
|
const {
|
||||||
entrypointPath,
|
entrypointPath,
|
||||||
entrypointFsDirname,
|
entrypointFsDirname,
|
||||||
|
nodeVersion,
|
||||||
|
spawnOpts,
|
||||||
} = await downloadInstallAndBundle({
|
} = await downloadInstallAndBundle({
|
||||||
files,
|
files,
|
||||||
entrypoint,
|
entrypoint,
|
||||||
workPath,
|
workPath,
|
||||||
meta,
|
meta,
|
||||||
npmArguments: ['--prefer-offline'],
|
|
||||||
});
|
});
|
||||||
|
|
||||||
console.log('running user script...');
|
console.log('running user script...');
|
||||||
await runPackageJsonScript(entrypointFsDirname, 'now-build');
|
await runPackageJsonScript(entrypointFsDirname, 'now-build', spawnOpts);
|
||||||
|
|
||||||
console.log('compiling entrypoint with ncc...');
|
console.log('compiling entrypoint with ncc...');
|
||||||
const preparedFiles = await compile(entrypointPath, entrypoint, config);
|
const { preparedFiles, watch } = await compile(
|
||||||
const launcherPath = join(__dirname, 'launcher.js');
|
workPath,
|
||||||
let launcherData = await readFile(launcherPath, 'utf8');
|
entrypointPath,
|
||||||
|
entrypoint,
|
||||||
launcherData = launcherData.replace(
|
config,
|
||||||
'// PLACEHOLDER',
|
meta
|
||||||
[
|
|
||||||
`listener = require("./${entrypoint}");`,
|
|
||||||
'if (listener.default) listener = listener.default;',
|
|
||||||
].join(' ')
|
|
||||||
);
|
);
|
||||||
|
|
||||||
const launcherFiles = {
|
const launcherFiles: Files = {
|
||||||
'launcher.js': new FileBlob({ data: launcherData }),
|
[`${LAUNCHER_FILENAME}.js`]: new FileBlob({
|
||||||
'bridge.js': new FileFsRef({ fsPath: require('@now/node-bridge') }),
|
data: makeLauncher({
|
||||||
|
entrypointPath: `./${entrypoint}`,
|
||||||
|
bridgePath: `./${BRIDGE_FILENAME}`,
|
||||||
|
helpersPath: `./${HELPERS_FILENAME}`,
|
||||||
|
shouldAddHelpers,
|
||||||
|
}),
|
||||||
|
}),
|
||||||
|
[`${BRIDGE_FILENAME}.js`]: new FileFsRef({
|
||||||
|
fsPath: require('@now/node-bridge'),
|
||||||
|
}),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if (shouldAddHelpers) {
|
||||||
|
launcherFiles[`${HELPERS_FILENAME}.js`] = new FileFsRef({
|
||||||
|
fsPath: join(__dirname, 'helpers.js'),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Use the system-installed version of `node` when running via `now dev`
|
||||||
|
const runtime = meta.isDev ? 'nodejs' : nodeVersion.runtime;
|
||||||
|
|
||||||
const lambda = await createLambda({
|
const lambda = await createLambda({
|
||||||
files: { ...preparedFiles, ...launcherFiles },
|
files: {
|
||||||
handler: 'launcher.launcher',
|
...preparedFiles,
|
||||||
runtime: 'nodejs8.10',
|
...launcherFiles,
|
||||||
|
},
|
||||||
|
handler: `${LAUNCHER_FILENAME}.launcher`,
|
||||||
|
runtime,
|
||||||
});
|
});
|
||||||
|
|
||||||
return { [entrypoint]: lambda };
|
const output = { [entrypoint]: lambda };
|
||||||
|
const result = { output, watch };
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function prepareCache({ workPath }: PrepareCacheOptions) {
|
export async function prepareCache({ workPath }: PrepareCacheOptions) {
|
||||||
|
|||||||
@@ -1,7 +1,29 @@
|
|||||||
import { Server } from 'http';
|
type LauncherConfiguration = {
|
||||||
import { Bridge } from './bridge';
|
entrypointPath: string;
|
||||||
|
bridgePath: string;
|
||||||
|
helpersPath: string;
|
||||||
|
shouldAddHelpers?: boolean;
|
||||||
|
};
|
||||||
|
|
||||||
let listener;
|
export function makeLauncher({
|
||||||
|
entrypointPath,
|
||||||
|
bridgePath,
|
||||||
|
helpersPath,
|
||||||
|
shouldAddHelpers = false,
|
||||||
|
}: LauncherConfiguration): string {
|
||||||
|
return `const { Bridge } = require("${bridgePath}");
|
||||||
|
const { Server } = require("http");
|
||||||
|
|
||||||
|
let isServerListening = false;
|
||||||
|
let bridge = new Bridge();
|
||||||
|
const saveListen = Server.prototype.listen;
|
||||||
|
Server.prototype.listen = function listen() {
|
||||||
|
isServerListening = true;
|
||||||
|
console.log('Legacy server listening...');
|
||||||
|
bridge.setServer(this);
|
||||||
|
Server.prototype.listen = saveListen;
|
||||||
|
return bridge.listen();
|
||||||
|
};
|
||||||
|
|
||||||
if (!process.env.NODE_ENV) {
|
if (!process.env.NODE_ENV) {
|
||||||
process.env.NODE_ENV =
|
process.env.NODE_ENV =
|
||||||
@@ -9,13 +31,43 @@ if (!process.env.NODE_ENV) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// PLACEHOLDER
|
let listener = require("${entrypointPath}");
|
||||||
|
if (listener.default) listener = listener.default;
|
||||||
|
|
||||||
|
if (typeof listener.listen === 'function') {
|
||||||
|
Server.prototype.listen = saveListen;
|
||||||
|
const server = listener;
|
||||||
|
bridge.setServer(server);
|
||||||
|
bridge.listen();
|
||||||
|
} else if (typeof listener === 'function') {
|
||||||
|
Server.prototype.listen = saveListen;
|
||||||
|
let server;
|
||||||
|
${
|
||||||
|
shouldAddHelpers
|
||||||
|
? [
|
||||||
|
'bridge = new Bridge(undefined, true);',
|
||||||
|
`server = require("${helpersPath}").createServerWithHelpers(listener, bridge);`,
|
||||||
|
].join('\n')
|
||||||
|
: ['server = require("http").createServer(listener);'].join('\n')
|
||||||
|
}
|
||||||
|
bridge.setServer(server);
|
||||||
|
bridge.listen();
|
||||||
|
} else if (typeof listener === 'object' && Object.keys(listener).length === 0) {
|
||||||
|
setTimeout(() => {
|
||||||
|
if (!isServerListening) {
|
||||||
|
console.error('No exports found in module "${entrypointPath}".');
|
||||||
|
console.error('Did you forget to export a function or a server?');
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
}, 5000);
|
||||||
|
} else {
|
||||||
|
console.error('Invalid export found in module "${entrypointPath}".');
|
||||||
|
console.error('The default export must be a function or server.');
|
||||||
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (err.code === 'MODULE_NOT_FOUND') {
|
if (err.code === 'MODULE_NOT_FOUND') {
|
||||||
console.error(err.message);
|
console.error(err.message);
|
||||||
console.error(
|
console.error('Did you forget to add it to "dependencies" in \`package.json\`?');
|
||||||
'Did you forget to add it to "dependencies" in `package.json`?'
|
|
||||||
);
|
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
} else {
|
} else {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
@@ -23,8 +75,5 @@ try {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const server = new Server(listener);
|
exports.launcher = bridge.launcher;`;
|
||||||
const bridge = new Bridge(server);
|
}
|
||||||
bridge.listen();
|
|
||||||
|
|
||||||
exports.launcher = bridge.launcher;
|
|
||||||
|
|||||||
17
packages/now-node/src/types.ts
Normal file
17
packages/now-node/src/types.ts
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
import { ServerResponse, IncomingMessage } from 'http';
|
||||||
|
|
||||||
|
export type NowRequestCookies = { [key: string]: string };
|
||||||
|
export type NowRequestQuery = { [key: string]: string | string[] };
|
||||||
|
export type NowRequestBody = any;
|
||||||
|
|
||||||
|
export type NowRequest = IncomingMessage & {
|
||||||
|
query: NowRequestQuery;
|
||||||
|
cookies: NowRequestCookies;
|
||||||
|
body: NowRequestBody;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type NowResponse = ServerResponse & {
|
||||||
|
send: (body: any) => NowResponse;
|
||||||
|
json: (jsonBody: any) => NowResponse;
|
||||||
|
status: (statusCode: number) => NowResponse;
|
||||||
|
};
|
||||||
25
packages/now-node/test/fixtures/02-node-server/hapi-async/index.js
vendored
Normal file
25
packages/now-node/test/fixtures/02-node-server/hapi-async/index.js
vendored
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
const Hapi = require('@hapi/hapi');
|
||||||
|
|
||||||
|
const init = async () => {
|
||||||
|
const server = Hapi.server({
|
||||||
|
port: 3000,
|
||||||
|
host: 'localhost',
|
||||||
|
});
|
||||||
|
|
||||||
|
server.route({
|
||||||
|
method: 'GET',
|
||||||
|
path: '/{p*}',
|
||||||
|
handler: () => 'hapi-async:RANDOMNESS_PLACEHOLDER',
|
||||||
|
});
|
||||||
|
|
||||||
|
await server.start();
|
||||||
|
console.log('Hapi server running on %s', server.info.uri);
|
||||||
|
};
|
||||||
|
|
||||||
|
process.on('unhandledRejection', (err) => {
|
||||||
|
console.log('Hapi failed in an unexpected way');
|
||||||
|
console.log(err);
|
||||||
|
process.exit(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
init();
|
||||||
5
packages/now-node/test/fixtures/02-node-server/hapi-async/package.json
vendored
Normal file
5
packages/now-node/test/fixtures/02-node-server/hapi-async/package.json
vendored
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
{
|
||||||
|
"dependencies": {
|
||||||
|
"@hapi/hapi": "18.3.1"
|
||||||
|
}
|
||||||
|
}
|
||||||
7
packages/now-node/test/fixtures/02-node-server/index.js
vendored
Normal file
7
packages/now-node/test/fixtures/02-node-server/index.js
vendored
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
const http = require('http');
|
||||||
|
|
||||||
|
const server = http.createServer((req, resp) => {
|
||||||
|
resp.end('root:RANDOMNESS_PLACEHOLDER');
|
||||||
|
});
|
||||||
|
|
||||||
|
server.listen();
|
||||||
15
packages/now-node/test/fixtures/02-node-server/now.json
vendored
Normal file
15
packages/now-node/test/fixtures/02-node-server/now.json
vendored
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
{
|
||||||
|
"version": 2,
|
||||||
|
"builds": [{ "src": "**/*.js", "use": "@now/node" }],
|
||||||
|
"probes": [
|
||||||
|
{ "path": "/", "mustContain": "root:RANDOMNESS_PLACEHOLDER" },
|
||||||
|
{
|
||||||
|
"path": "/subdirectory/",
|
||||||
|
"mustContain": "subdir:RANDOMNESS_PLACEHOLDER"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "/hapi-async/",
|
||||||
|
"mustContain": "hapi-async:RANDOMNESS_PLACEHOLDER"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
7
packages/now-node/test/fixtures/02-node-server/subdirectory/index.js
vendored
Normal file
7
packages/now-node/test/fixtures/02-node-server/subdirectory/index.js
vendored
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
const http = require('http');
|
||||||
|
|
||||||
|
const server = http.createServer((req, resp) => {
|
||||||
|
resp.end('subdir:RANDOMNESS_PLACEHOLDER');
|
||||||
|
});
|
||||||
|
|
||||||
|
server.listen();
|
||||||
3
packages/now-node/test/fixtures/10-node-engines/empty/index.js
vendored
Normal file
3
packages/now-node/test/fixtures/10-node-engines/empty/index.js
vendored
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
module.exports = (req, res) => {
|
||||||
|
res.end(`RANDOMNESS_PLACEHOLDER:${process.versions.node}`);
|
||||||
|
};
|
||||||
3
packages/now-node/test/fixtures/10-node-engines/empty/package.json
vendored
Normal file
3
packages/now-node/test/fixtures/10-node-engines/empty/package.json
vendored
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"name": "missing-engines-key-on-purpose"
|
||||||
|
}
|
||||||
3
packages/now-node/test/fixtures/10-node-engines/exact/index.js
vendored
Normal file
3
packages/now-node/test/fixtures/10-node-engines/exact/index.js
vendored
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
module.exports = (req, res) => {
|
||||||
|
res.end(`RANDOMNESS_PLACEHOLDER:${process.versions.node}`);
|
||||||
|
};
|
||||||
5
packages/now-node/test/fixtures/10-node-engines/exact/package.json
vendored
Normal file
5
packages/now-node/test/fixtures/10-node-engines/exact/package.json
vendored
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
{
|
||||||
|
"engines": {
|
||||||
|
"node": "10.5.0"
|
||||||
|
}
|
||||||
|
}
|
||||||
3
packages/now-node/test/fixtures/10-node-engines/greater/index.js
vendored
Normal file
3
packages/now-node/test/fixtures/10-node-engines/greater/index.js
vendored
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
module.exports = (req, res) => {
|
||||||
|
res.end(`RANDOMNESS_PLACEHOLDER:${process.versions.node}`);
|
||||||
|
};
|
||||||
5
packages/now-node/test/fixtures/10-node-engines/greater/package.json
vendored
Normal file
5
packages/now-node/test/fixtures/10-node-engines/greater/package.json
vendored
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
{
|
||||||
|
"engines": {
|
||||||
|
"node": ">=10.0.0"
|
||||||
|
}
|
||||||
|
}
|
||||||
3
packages/now-node/test/fixtures/10-node-engines/major/index.js
vendored
Normal file
3
packages/now-node/test/fixtures/10-node-engines/major/index.js
vendored
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
module.exports = (req, res) => {
|
||||||
|
res.end(`RANDOMNESS_PLACEHOLDER:${process.versions.node}`);
|
||||||
|
};
|
||||||
5
packages/now-node/test/fixtures/10-node-engines/major/package.json
vendored
Normal file
5
packages/now-node/test/fixtures/10-node-engines/major/package.json
vendored
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
{
|
||||||
|
"engines": {
|
||||||
|
"node": "10.x"
|
||||||
|
}
|
||||||
|
}
|
||||||
11
packages/now-node/test/fixtures/10-node-engines/now.json
vendored
Normal file
11
packages/now-node/test/fixtures/10-node-engines/now.json
vendored
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
{
|
||||||
|
"version": 2,
|
||||||
|
"builds": [{ "src": "**/*.js", "use": "@now/node" }],
|
||||||
|
"probes": [
|
||||||
|
{ "path": "/empty", "mustContain": "RANDOMNESS_PLACEHOLDER:8" },
|
||||||
|
{ "path": "/exact", "mustContain": "RANDOMNESS_PLACEHOLDER:10" },
|
||||||
|
{ "path": "/greater", "mustContain": "RANDOMNESS_PLACEHOLDER:10" },
|
||||||
|
{ "path": "/major", "mustContain": "RANDOMNESS_PLACEHOLDER:10" },
|
||||||
|
{ "path": "/range", "mustContain": "RANDOMNESS_PLACEHOLDER:10" }
|
||||||
|
]
|
||||||
|
}
|
||||||
3
packages/now-node/test/fixtures/10-node-engines/range/index.js
vendored
Normal file
3
packages/now-node/test/fixtures/10-node-engines/range/index.js
vendored
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
module.exports = (req, res) => {
|
||||||
|
res.end(`RANDOMNESS_PLACEHOLDER:${process.versions.node}`);
|
||||||
|
};
|
||||||
5
packages/now-node/test/fixtures/10-node-engines/range/package.json
vendored
Normal file
5
packages/now-node/test/fixtures/10-node-engines/range/package.json
vendored
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
{
|
||||||
|
"engines": {
|
||||||
|
"node": "10.x"
|
||||||
|
}
|
||||||
|
}
|
||||||
17
packages/now-node/test/fixtures/15-helpers/index.js
vendored
Normal file
17
packages/now-node/test/fixtures/15-helpers/index.js
vendored
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
/* eslint-disable prefer-destructuring */
|
||||||
|
|
||||||
|
module.exports = (req, res) => {
|
||||||
|
res.status(200);
|
||||||
|
|
||||||
|
let who = 'anonymous';
|
||||||
|
|
||||||
|
if (req.body && req.body.who) {
|
||||||
|
who = req.body.who;
|
||||||
|
} else if (req.query.who) {
|
||||||
|
who = req.query.who;
|
||||||
|
} else if (req.cookies.who) {
|
||||||
|
who = req.cookies.who;
|
||||||
|
}
|
||||||
|
|
||||||
|
res.send(`hello ${who}:RANDOMNESS_PLACEHOLDER`);
|
||||||
|
};
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user