mirror of
https://github.com/LukeHagar/vercel.git
synced 2025-12-12 12:57:47 +00:00
Compare commits
81 Commits
@now/pytho
...
@now/bash@
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
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 | ||
|
|
35d56a34cb | ||
|
|
9dfd37e135 | ||
|
|
6f815f2645 |
@@ -6,12 +6,11 @@
|
|||||||
/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/*
|
||||||
|
|||||||
1
.github/CODEOWNERS
vendored
1
.github/CODEOWNERS
vendored
@@ -7,3 +7,4 @@
|
|||||||
/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.
|
||||||
48
README.md
48
README.md
@@ -2,29 +2,51 @@
|
|||||||
|
|
||||||
This is a monorepo containing the [Official Builders](https://zeit.co/docs/v2/deployments/builders/overview) provided by the ZEIT team.
|
This is a monorepo containing the [Official Builders](https://zeit.co/docs/v2/deployments/builders/overview) provided by the ZEIT team.
|
||||||
|
|
||||||
There are two branches:
|
## Channels
|
||||||
|
|
||||||
- canary - published to npm as `canary` dist-tag, eg `@now/node@canary`
|
There are two Channels:
|
||||||
- master - published to npm as `latest` dist-tag, eg `@now/node@latest`
|
|
||||||
|
| Channel | Git Branch | npm dist-tag | use example |
|
||||||
|
| ------- | ---------- | ------------ | ------------------ |
|
||||||
|
| Canary | `canary` | `@canary` | `@now/node@canary` |
|
||||||
|
| Stable | `master` | `@latest` | `@now/node@latest` |
|
||||||
|
|
||||||
|
All PRs should be submitted to the `canary` branch.
|
||||||
|
|
||||||
|
Once a PR is merged into the `canary` branch, it should be published to npm immediately using the Canary Channel.
|
||||||
|
|
||||||
### Publishing to npm
|
### Publishing to npm
|
||||||
|
|
||||||
Run the following command to publish modified builders to npm:
|
For the Canary Channel, publish the modified Builders to npm with the following:
|
||||||
|
|
||||||
For the stable channel use:
|
|
||||||
|
|
||||||
```
|
|
||||||
yarn publish-stable
|
|
||||||
```
|
|
||||||
|
|
||||||
For the canary channel use:
|
|
||||||
|
|
||||||
```
|
```
|
||||||
yarn publish-canary
|
yarn publish-canary
|
||||||
```
|
```
|
||||||
|
|
||||||
GitHub Actions will take care of publishing the updated packages to npm from there.
|
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 pull # make sure you're up to date
|
||||||
|
git cherry-pick <PR501_COMMIT_SHA>
|
||||||
|
git cherry-pick <PR502_COMMIT_SHA>
|
||||||
|
git cherry-pick <PR503_COMMIT_SHA>
|
||||||
|
git cherry-pick <PR504_COMMIT_SHA>
|
||||||
|
# ... etc ...
|
||||||
|
git diff origin/canary
|
||||||
|
yarn publish-stable
|
||||||
|
```
|
||||||
|
|
||||||
|
After running this publish step, GitHub Actions will take care of publishing the modified Builder packages to npm.
|
||||||
|
|
||||||
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
package.json
12
package.json
@@ -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 .",
|
||||||
@@ -32,6 +32,14 @@
|
|||||||
"*.ts": [
|
"*.ts": [
|
||||||
"prettier --write",
|
"prettier --write",
|
||||||
"git add"
|
"git add"
|
||||||
|
],
|
||||||
|
"*.json": [
|
||||||
|
"prettier --write",
|
||||||
|
"git add"
|
||||||
|
],
|
||||||
|
"*.md": [
|
||||||
|
"prettier --write",
|
||||||
|
"git add"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
|||||||
@@ -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,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@now/bash",
|
"name": "@now/bash",
|
||||||
"version": "0.2.3",
|
"version": "0.3.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>",
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@now/build-utils",
|
"name": "@now/build-utils",
|
||||||
"version": "0.5.6",
|
"version": "0.7.3",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"main": "./dist/index.js",
|
"main": "./dist/index.js",
|
||||||
"types": "./dist/index.d.js",
|
"types": "./dist/index.d.js",
|
||||||
@@ -9,8 +9,12 @@
|
|||||||
"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 +22,21 @@
|
|||||||
"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",
|
|
||||||
"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"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
45
packages/now-build-utils/src/fs/node-version.ts
Normal file
45
packages/now-build-utils/src/fs/node-version.ts
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
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
|
||||||
|
): Promise<NodeVersion> {
|
||||||
|
let selection = defaultSelection;
|
||||||
|
|
||||||
|
if (!engineRange) {
|
||||||
|
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) {
|
||||||
|
console.log(
|
||||||
|
'found `engines` in `package.json`, selecting range: ' + selection.range
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
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,
|
||||||
@@ -51,11 +54,32 @@ export async function runShellScript(fsPath: string) {
|
|||||||
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): Promise<NodeVersion> {
|
||||||
|
const { packageJson } = await scanParentDirs(destPath, true);
|
||||||
|
const range = packageJson && packageJson.engines && packageJson.engines.node;
|
||||||
|
return getSupportedNodeVersion(range);
|
||||||
|
}
|
||||||
|
|
||||||
|
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 +88,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 +103,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 +117,22 @@ 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', '--cwd', destPath]),
|
||||||
destPath,
|
destPath,
|
||||||
opts as SpawnOptions
|
opts
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -129,9 +143,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 +171,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.'
|
||||||
|
);
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import {
|
|||||||
PrepareCacheOptions,
|
PrepareCacheOptions,
|
||||||
ShouldServeOptions,
|
ShouldServeOptions,
|
||||||
Meta,
|
Meta,
|
||||||
|
Config,
|
||||||
} from './types';
|
} from './types';
|
||||||
import { Lambda, createLambda } from './lambda';
|
import { Lambda, createLambda } from './lambda';
|
||||||
import download, { DownloadedFiles } from './fs/download';
|
import download, { DownloadedFiles } from './fs/download';
|
||||||
@@ -20,6 +21,8 @@ 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';
|
||||||
@@ -42,10 +45,13 @@ export {
|
|||||||
runPackageJsonScript,
|
runPackageJsonScript,
|
||||||
runNpmInstall,
|
runNpmInstall,
|
||||||
runShellScript,
|
runShellScript,
|
||||||
|
getNodeVersion,
|
||||||
|
getSpawnOptions,
|
||||||
streamToBuffer,
|
streamToBuffer,
|
||||||
AnalyzeOptions,
|
AnalyzeOptions,
|
||||||
BuildOptions,
|
BuildOptions,
|
||||||
PrepareCacheOptions,
|
PrepareCacheOptions,
|
||||||
ShouldServeOptions,
|
ShouldServeOptions,
|
||||||
shouldServe,
|
shouldServe,
|
||||||
|
Config,
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -16,7 +16,22 @@ export interface Files {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export interface Config {
|
export interface Config {
|
||||||
[key: string]: string;
|
[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 {
|
||||||
@@ -155,3 +170,28 @@ 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;
|
||||||
|
}
|
||||||
|
|||||||
@@ -6,6 +6,10 @@ 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,
|
||||||
@@ -64,6 +68,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');
|
||||||
|
|||||||
@@ -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,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@now/go",
|
"name": "@now/go",
|
||||||
"version": "0.5.1",
|
"version": "0.5.3",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
@@ -31,6 +31,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
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,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,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,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>;
|
|
||||||
}
|
|
||||||
@@ -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,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@now/md",
|
"name": "@now/md",
|
||||||
"version": "0.5.3",
|
"version": "0.5.4",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@now/next",
|
"name": "@now/next",
|
||||||
"version": "0.4.1",
|
"version": "0.5.1",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"main": "./dist/index",
|
"main": "./dist/index",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
@@ -14,7 +14,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 +28,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 {
|
||||||
@@ -166,11 +169,14 @@ export const build = async ({
|
|||||||
|
|
||||||
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);
|
||||||
|
|
||||||
@@ -190,7 +196,7 @@ export const build = async ({
|
|||||||
// 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 +233,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 +265,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 +283,22 @@ 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 buildSpawnOptions = { ...spawnOpts };
|
||||||
env: {
|
const env = { ...buildSpawnOptions.env } as any;
|
||||||
...process.env,
|
env.NODE_OPTIONS = `--max_old_space_size=${memoryToConsume}`;
|
||||||
NODE_OPTIONS: `--max_old_space_size=${memoryToConsume}`,
|
await runPackageJsonScript(entryPath, 'now-build', buildSpawnOptions);
|
||||||
},
|
|
||||||
} 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) {
|
||||||
@@ -406,7 +415,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 +462,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));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -510,7 +519,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,
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -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;
|
||||||
@@ -146,7 +160,7 @@ exports.build = async ({
|
|||||||
const lambda = await createLambda({
|
const lambda = await createLambda({
|
||||||
files: { ...preparedFiles, ...launcherFiles },
|
files: { ...preparedFiles, ...launcherFiles },
|
||||||
handler: 'launcher.launcher',
|
handler: 'launcher.launcher',
|
||||||
runtime: 'nodejs8.10',
|
runtime: nodeVersion.runtime,
|
||||||
});
|
});
|
||||||
|
|
||||||
return { [entrypoint]: lambda };
|
return { [entrypoint]: lambda };
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@now/node-server",
|
"name": "@now/node-server",
|
||||||
"version": "0.7.4",
|
"version": "0.8.1",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
@@ -8,7 +8,7 @@
|
|||||||
"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;
|
||||||
|
}
|
||||||
@@ -9,4 +9,16 @@ if [ ! -e "$bridge_defs" ]; then
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
cp -v "$bridge_defs" src
|
cp -v "$bridge_defs" src
|
||||||
|
|
||||||
|
# build ts files
|
||||||
tsc
|
tsc
|
||||||
|
|
||||||
|
# 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
|
||||||
|
|
||||||
|
# todo: improve
|
||||||
|
# copy type file for ts test
|
||||||
|
cp dist/types.d.ts test/fixtures/15-helpers/ts/types.d.ts
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@now/node",
|
"name": "@now/node",
|
||||||
"version": "0.7.4",
|
"version": "0.11.0",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"main": "./dist/index",
|
"main": "./dist/index",
|
||||||
"repository": {
|
"repository": {
|
||||||
@@ -9,9 +9,10 @@
|
|||||||
"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 +23,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,25 @@ 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();
|
||||||
|
|
||||||
|
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 +54,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 +138,7 @@ async function compile(
|
|||||||
}
|
}
|
||||||
|
|
||||||
assets[fullPath] = {
|
assets[fullPath] = {
|
||||||
source: data,
|
source: toBuffer(data),
|
||||||
permissions: mode,
|
permissions: mode,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -89,11 +146,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 +162,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 +176,59 @@ 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.js': new FileBlob({
|
||||||
|
data: makeLauncher(entrypoint, shouldAddHelpers),
|
||||||
|
}),
|
||||||
'bridge.js': new FileFsRef({ fsPath: require('@now/node-bridge') }),
|
'bridge.js': new FileFsRef({ fsPath: require('@now/node-bridge') }),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if (shouldAddHelpers) {
|
||||||
|
launcherFiles['helpers.js'] = new FileFsRef({
|
||||||
|
fsPath: join(__dirname, 'helpers.js'),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
const lambda = await createLambda({
|
const lambda = await createLambda({
|
||||||
files: { ...preparedFiles, ...launcherFiles },
|
files: {
|
||||||
|
...preparedFiles,
|
||||||
|
...launcherFiles,
|
||||||
|
},
|
||||||
handler: 'launcher.launcher',
|
handler: 'launcher.launcher',
|
||||||
runtime: 'nodejs8.10',
|
runtime: nodeVersion.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,10 @@
|
|||||||
import { Server } from 'http';
|
export function makeLauncher(
|
||||||
import { Bridge } from './bridge';
|
entrypoint: string,
|
||||||
|
shouldAddHelpers: boolean
|
||||||
|
): string {
|
||||||
|
return `const { Bridge } = require("./bridge");
|
||||||
|
|
||||||
let listener;
|
let bridge;
|
||||||
|
|
||||||
if (!process.env.NODE_ENV) {
|
if (!process.env.NODE_ENV) {
|
||||||
process.env.NODE_ENV =
|
process.env.NODE_ENV =
|
||||||
@@ -9,13 +12,35 @@ if (!process.env.NODE_ENV) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// PLACEHOLDER
|
let listener = require("./${entrypoint}");
|
||||||
|
if (listener.default) listener = listener.default;
|
||||||
|
|
||||||
|
if(typeof listener.listen === 'function') {
|
||||||
|
const server = listener;
|
||||||
|
bridge = new Bridge(server);
|
||||||
|
} else if(typeof listener === 'function') {
|
||||||
|
${
|
||||||
|
shouldAddHelpers
|
||||||
|
? [
|
||||||
|
'bridge = new Bridge(undefined, true);',
|
||||||
|
'const server = require("./helpers").createServerWithHelpers(listener, bridge);',
|
||||||
|
'bridge.setServer(server);',
|
||||||
|
].join('\n')
|
||||||
|
: [
|
||||||
|
'const server = require("http").createServer(listener);',
|
||||||
|
'bridge = new Bridge(server);',
|
||||||
|
].join('\n')
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
console.error('Export in entrypoint is not valid');
|
||||||
|
console.error('Did you forget to export a function or a server?');
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
} 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 +48,7 @@ try {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const server = new Server(listener);
|
|
||||||
const bridge = new Bridge(server);
|
|
||||||
bridge.listen();
|
bridge.listen();
|
||||||
|
|
||||||
exports.launcher = bridge.launcher;
|
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;
|
||||||
|
};
|
||||||
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`);
|
||||||
|
};
|
||||||
14
packages/now-node/test/fixtures/15-helpers/micro-compat/index.js
vendored
Normal file
14
packages/now-node/test/fixtures/15-helpers/micro-compat/index.js
vendored
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
/* eslint-disable prefer-destructuring */
|
||||||
|
const { json, send } = require('micro');
|
||||||
|
|
||||||
|
module.exports = async (req, res) => {
|
||||||
|
const body = await json(req);
|
||||||
|
|
||||||
|
let who = 'anonymous';
|
||||||
|
|
||||||
|
if (body && body.who) {
|
||||||
|
who = body.who;
|
||||||
|
}
|
||||||
|
|
||||||
|
send(res, 200, `hello ${who}:RANDOMNESS_PLACEHOLDER`);
|
||||||
|
};
|
||||||
5
packages/now-node/test/fixtures/15-helpers/micro-compat/package.json
vendored
Normal file
5
packages/now-node/test/fixtures/15-helpers/micro-compat/package.json
vendored
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
{
|
||||||
|
"dependencies": {
|
||||||
|
"micro": "9.3.4"
|
||||||
|
}
|
||||||
|
}
|
||||||
5
packages/now-node/test/fixtures/15-helpers/no-helpers/index.js
vendored
Normal file
5
packages/now-node/test/fixtures/15-helpers/no-helpers/index.js
vendored
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
module.exports = (req, res) => {
|
||||||
|
const areHelpersAvailable = typeof req.query !== 'undefined';
|
||||||
|
|
||||||
|
res.end(`${areHelpersAvailable ? 'yes' : 'no'}:RANDOMNESS_PLACEHOLDER`);
|
||||||
|
};
|
||||||
48
packages/now-node/test/fixtures/15-helpers/now.json
vendored
Normal file
48
packages/now-node/test/fixtures/15-helpers/now.json
vendored
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
{
|
||||||
|
"version": 2,
|
||||||
|
"builds": [
|
||||||
|
{ "src": "index.js", "use": "@now/node" },
|
||||||
|
{ "src": "ts/index.ts", "use": "@now/node" },
|
||||||
|
{ "src": "micro-compat/index.js", "use": "@now/node" },
|
||||||
|
{
|
||||||
|
"src": "no-helpers/index.js",
|
||||||
|
"use": "@now/node",
|
||||||
|
"config": { "helpers": false }
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"probes": [
|
||||||
|
{
|
||||||
|
"path": "/",
|
||||||
|
"mustContain": "hello anonymous:RANDOMNESS_PLACEHOLDER"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "/?who=bill",
|
||||||
|
"mustContain": "hello bill:RANDOMNESS_PLACEHOLDER"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "/",
|
||||||
|
"method": "POST",
|
||||||
|
"body": { "who": "john" },
|
||||||
|
"mustContain": "hello john:RANDOMNESS_PLACEHOLDER"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "/",
|
||||||
|
"headers": { "cookie": "who=chris" },
|
||||||
|
"mustContain": "hello chris:RANDOMNESS_PLACEHOLDER"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "/ts",
|
||||||
|
"mustContain": "hello:RANDOMNESS_PLACEHOLDER"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "/micro-compat",
|
||||||
|
"method": "POST",
|
||||||
|
"body": { "who": "katie" },
|
||||||
|
"mustContain": "hello katie:RANDOMNESS_PLACEHOLDER"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "/no-helpers/",
|
||||||
|
"mustContain": "no:RANDOMNESS_PLACEHOLDER"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
6
packages/now-node/test/fixtures/15-helpers/ts/index.ts
vendored
Normal file
6
packages/now-node/test/fixtures/15-helpers/ts/index.ts
vendored
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
import { NowRequest, NowResponse } from './types';
|
||||||
|
|
||||||
|
export default function listener(req: NowRequest, res: NowResponse) {
|
||||||
|
res.status(200);
|
||||||
|
res.send('hello:RANDOMNESS_PLACEHOLDER');
|
||||||
|
}
|
||||||
11
packages/now-node/test/fixtures/15-helpers/ts/tsconfig.json
vendored
Normal file
11
packages/now-node/test/fixtures/15-helpers/ts/tsconfig.json
vendored
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"strict": true,
|
||||||
|
"esModuleInterop": true,
|
||||||
|
"sourceMap": true,
|
||||||
|
"lib": ["esnext"],
|
||||||
|
"target": "esnext",
|
||||||
|
"module": "commonjs"
|
||||||
|
},
|
||||||
|
"include": ["index.ts"]
|
||||||
|
}
|
||||||
9
packages/now-node/test/fixtures/16-servers/express/index.js
vendored
Normal file
9
packages/now-node/test/fixtures/16-servers/express/index.js
vendored
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
const express = require('express');
|
||||||
|
|
||||||
|
const app = express();
|
||||||
|
|
||||||
|
app.all('*', (req, res) => {
|
||||||
|
res.send('hello from express:RANDOMNESS_PLACEHOLDER');
|
||||||
|
});
|
||||||
|
|
||||||
|
module.exports = app;
|
||||||
5
packages/now-node/test/fixtures/16-servers/express/package.json
vendored
Normal file
5
packages/now-node/test/fixtures/16-servers/express/package.json
vendored
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
{
|
||||||
|
"dependencies": {
|
||||||
|
"express": "4.17.1"
|
||||||
|
}
|
||||||
|
}
|
||||||
17
packages/now-node/test/fixtures/16-servers/hapi/index.js
vendored
Normal file
17
packages/now-node/test/fixtures/16-servers/hapi/index.js
vendored
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
const Hapi = require('@hapi/hapi');
|
||||||
|
|
||||||
|
const server = Hapi.server({
|
||||||
|
port: 3000,
|
||||||
|
host: 'localhost',
|
||||||
|
});
|
||||||
|
|
||||||
|
server.route({
|
||||||
|
method: 'GET',
|
||||||
|
path: '/{p*}',
|
||||||
|
handler: () => 'hello from hapi:RANDOMNESS_PLACEHOLDER',
|
||||||
|
});
|
||||||
|
|
||||||
|
// server.listener is a node's http.Server
|
||||||
|
// server does not have the `listen` method so we need to export this instead
|
||||||
|
|
||||||
|
module.exports = server.listener;
|
||||||
5
packages/now-node/test/fixtures/16-servers/hapi/package.json
vendored
Normal file
5
packages/now-node/test/fixtures/16-servers/hapi/package.json
vendored
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
{
|
||||||
|
"dependencies": {
|
||||||
|
"@hapi/hapi": "18.3.1"
|
||||||
|
}
|
||||||
|
}
|
||||||
7
packages/now-node/test/fixtures/16-servers/index.js
vendored
Normal file
7
packages/now-node/test/fixtures/16-servers/index.js
vendored
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
const http = require('http');
|
||||||
|
|
||||||
|
const server = http.createServer((req, res) => {
|
||||||
|
res.end('hello:RANDOMNESS_PLACEHOLDER');
|
||||||
|
});
|
||||||
|
|
||||||
|
module.exports = server;
|
||||||
9
packages/now-node/test/fixtures/16-servers/koa/index.js
vendored
Normal file
9
packages/now-node/test/fixtures/16-servers/koa/index.js
vendored
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
const Koa = require('koa');
|
||||||
|
|
||||||
|
const app = new Koa();
|
||||||
|
|
||||||
|
app.use(async (ctx) => {
|
||||||
|
ctx.body = 'hello from koa:RANDOMNESS_PLACEHOLDER';
|
||||||
|
});
|
||||||
|
|
||||||
|
module.exports = app;
|
||||||
5
packages/now-node/test/fixtures/16-servers/koa/package.json
vendored
Normal file
5
packages/now-node/test/fixtures/16-servers/koa/package.json
vendored
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
{
|
||||||
|
"dependencies": {
|
||||||
|
"koa": "2.7.0"
|
||||||
|
}
|
||||||
|
}
|
||||||
22
packages/now-node/test/fixtures/16-servers/now.json
vendored
Normal file
22
packages/now-node/test/fixtures/16-servers/now.json
vendored
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
{
|
||||||
|
"version": 2,
|
||||||
|
"builds": [{ "src": "**/*.js", "use": "@now/node" }],
|
||||||
|
"probes": [
|
||||||
|
{
|
||||||
|
"path": "/",
|
||||||
|
"mustContain": "hello:RANDOMNESS_PLACEHOLDER"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "/express",
|
||||||
|
"mustContain": "hello from express:RANDOMNESS_PLACEHOLDER"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "/koa",
|
||||||
|
"mustContain": "hello from koa:RANDOMNESS_PLACEHOLDER"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "/hapi",
|
||||||
|
"mustContain": "hello from hapi:RANDOMNESS_PLACEHOLDER"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
780
packages/now-node/test/helpers.test.js
Normal file
780
packages/now-node/test/helpers.test.js
Normal file
@@ -0,0 +1,780 @@
|
|||||||
|
/* global beforeEach, afterEach, expect, it, jest */
|
||||||
|
const fetch = require('node-fetch');
|
||||||
|
const listen = require('test-listen');
|
||||||
|
const qs = require('querystring');
|
||||||
|
|
||||||
|
const { createServerWithHelpers } = require('../dist/helpers');
|
||||||
|
|
||||||
|
const mockListener = jest.fn();
|
||||||
|
const consumeEventMock = jest.fn();
|
||||||
|
const mockBridge = { consumeEvent: consumeEventMock };
|
||||||
|
|
||||||
|
let server;
|
||||||
|
let url;
|
||||||
|
|
||||||
|
async function fetchWithProxyReq(_url, opts = {}) {
|
||||||
|
if (opts.body) {
|
||||||
|
// eslint-disable-next-line
|
||||||
|
opts = { ...opts, body: Buffer.from(opts.body) };
|
||||||
|
}
|
||||||
|
|
||||||
|
consumeEventMock.mockImplementationOnce(() => opts);
|
||||||
|
|
||||||
|
return fetch(_url, {
|
||||||
|
...opts,
|
||||||
|
headers: { ...opts.headers, 'x-now-bridge-request-id': '2' },
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
beforeEach(async () => {
|
||||||
|
mockListener.mockClear();
|
||||||
|
consumeEventMock.mockClear();
|
||||||
|
|
||||||
|
mockListener.mockImplementation((req, res) => {
|
||||||
|
res.send('hello');
|
||||||
|
});
|
||||||
|
consumeEventMock.mockImplementation(() => ({}));
|
||||||
|
|
||||||
|
server = createServerWithHelpers(mockListener, mockBridge);
|
||||||
|
url = await listen(server);
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(async () => {
|
||||||
|
await server.close();
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('contract with @now/node-bridge', () => {
|
||||||
|
test('should call consumeEvent with the correct reqId', async () => {
|
||||||
|
await fetchWithProxyReq(`${url}/`);
|
||||||
|
|
||||||
|
expect(consumeEventMock).toHaveBeenLastCalledWith('2');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should not expose the request id header', async () => {
|
||||||
|
await fetchWithProxyReq(`${url}/`, { headers: { 'x-test-header': 'ok' } });
|
||||||
|
|
||||||
|
const [{ headers }] = mockListener.mock.calls[0];
|
||||||
|
|
||||||
|
expect(headers['x-now-bridge-request-id']).toBeUndefined();
|
||||||
|
expect(headers['x-test-header']).toBe('ok');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('all helpers', () => {
|
||||||
|
const nowHelpers = [
|
||||||
|
['query', 0],
|
||||||
|
['cookies', 0],
|
||||||
|
['body', 0],
|
||||||
|
['status', 1],
|
||||||
|
['send', 1],
|
||||||
|
['json', 1],
|
||||||
|
];
|
||||||
|
|
||||||
|
test('should not recalculate req properties twice', async () => {
|
||||||
|
const spy = jest.fn(() => {});
|
||||||
|
|
||||||
|
const nowReqHelpers = nowHelpers.filter(([, i]) => i === 0);
|
||||||
|
|
||||||
|
mockListener.mockImplementation((req, res) => {
|
||||||
|
spy(...nowReqHelpers.map(h => req[h]));
|
||||||
|
spy(...nowReqHelpers.map(h => req[h]));
|
||||||
|
res.end();
|
||||||
|
});
|
||||||
|
|
||||||
|
await fetchWithProxyReq(`${url}/?who=bill`, {
|
||||||
|
method: 'POST',
|
||||||
|
body: JSON.stringify({ who: 'mike' }),
|
||||||
|
headers: { 'content-type': 'application/json', cookie: 'who=jim' },
|
||||||
|
});
|
||||||
|
|
||||||
|
// here we test that bodySpy is called twice with exactly the same arguments
|
||||||
|
for (let i = 0; i < 3; i += 1) {
|
||||||
|
expect(spy.mock.calls[0][i]).toBe(spy.mock.calls[1][i]);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should be able to overwrite request properties', async () => {
|
||||||
|
const spy = jest.fn(() => {});
|
||||||
|
|
||||||
|
mockListener.mockImplementation((...args) => {
|
||||||
|
nowHelpers.forEach(([prop, n]) => {
|
||||||
|
/* eslint-disable */
|
||||||
|
args[n][prop] = 'ok';
|
||||||
|
args[n][prop] = 'ok2';
|
||||||
|
spy(args[n][prop]);
|
||||||
|
});
|
||||||
|
|
||||||
|
args[1].end();
|
||||||
|
});
|
||||||
|
|
||||||
|
await fetchWithProxyReq(url);
|
||||||
|
|
||||||
|
nowHelpers.forEach((_, i) => expect(spy.mock.calls[i][0]).toBe('ok2'));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should be able to reconfig request properties', async () => {
|
||||||
|
const spy = jest.fn(() => {});
|
||||||
|
|
||||||
|
mockListener.mockImplementation((...args) => {
|
||||||
|
nowHelpers.forEach(([prop, n]) => {
|
||||||
|
// eslint-disable-next-line
|
||||||
|
Object.defineProperty(args[n], prop, { value: 'ok' });
|
||||||
|
Object.defineProperty(args[n], prop, { value: 'ok2' });
|
||||||
|
spy(args[n][prop]);
|
||||||
|
});
|
||||||
|
|
||||||
|
args[1].end();
|
||||||
|
});
|
||||||
|
|
||||||
|
await fetchWithProxyReq(url);
|
||||||
|
|
||||||
|
nowHelpers.forEach((_, i) => expect(spy.mock.calls[i][0]).toBe('ok2'));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('req.query', () => {
|
||||||
|
test('req.query should reflect querystring in the url', async () => {
|
||||||
|
await fetchWithProxyReq(`${url}/?who=bill&where=us`);
|
||||||
|
|
||||||
|
expect(mockListener.mock.calls[0][0].query).toMatchObject({
|
||||||
|
who: 'bill',
|
||||||
|
where: 'us',
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test('req.query should be {} when there is no querystring', async () => {
|
||||||
|
await fetchWithProxyReq(url);
|
||||||
|
const [{ query }] = mockListener.mock.calls[0];
|
||||||
|
expect(Object.keys(query).length).toBe(0);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('req.cookies', () => {
|
||||||
|
test('req.cookies should reflect req.cookie header', async () => {
|
||||||
|
await fetchWithProxyReq(url, {
|
||||||
|
headers: {
|
||||||
|
cookie: 'who=bill; where=us',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(mockListener.mock.calls[0][0].cookies).toMatchObject({
|
||||||
|
who: 'bill',
|
||||||
|
where: 'us',
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('req.body', () => {
|
||||||
|
test('req.body should be undefined by default', async () => {
|
||||||
|
await fetchWithProxyReq(url);
|
||||||
|
expect(mockListener.mock.calls[0][0].body).toBe(undefined);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('req.body should be undefined if content-type is not defined', async () => {
|
||||||
|
await fetchWithProxyReq(url, {
|
||||||
|
method: 'POST',
|
||||||
|
body: 'hello',
|
||||||
|
});
|
||||||
|
expect(mockListener.mock.calls[0][0].body).toBe(undefined);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('req.body should be a string when content-type is `text/plain`', async () => {
|
||||||
|
await fetchWithProxyReq(url, {
|
||||||
|
method: 'POST',
|
||||||
|
body: 'hello',
|
||||||
|
headers: { 'content-type': 'text/plain' },
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(mockListener.mock.calls[0][0].body).toBe('hello');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('req.body should be a buffer when content-type is `application/octet-stream`', async () => {
|
||||||
|
await fetchWithProxyReq(url, {
|
||||||
|
method: 'POST',
|
||||||
|
body: 'hello',
|
||||||
|
headers: { 'content-type': 'application/octet-stream' },
|
||||||
|
});
|
||||||
|
|
||||||
|
const [{ body }] = mockListener.mock.calls[0];
|
||||||
|
|
||||||
|
const str = body.toString();
|
||||||
|
|
||||||
|
expect(Buffer.isBuffer(body)).toBe(true);
|
||||||
|
expect(str).toBe('hello');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('req.body should be an object when content-type is `application/x-www-form-urlencoded`', async () => {
|
||||||
|
const obj = { who: 'mike' };
|
||||||
|
|
||||||
|
await fetchWithProxyReq(url, {
|
||||||
|
method: 'POST',
|
||||||
|
body: qs.encode(obj),
|
||||||
|
headers: { 'content-type': 'application/x-www-form-urlencoded' },
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(mockListener.mock.calls[0][0].body).toMatchObject(obj);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('req.body should be an object when content-type is `application/json`', async () => {
|
||||||
|
const json = {
|
||||||
|
who: 'bill',
|
||||||
|
where: 'us',
|
||||||
|
};
|
||||||
|
|
||||||
|
await fetchWithProxyReq(url, {
|
||||||
|
method: 'POST',
|
||||||
|
body: JSON.stringify(json),
|
||||||
|
headers: { 'content-type': 'application/json' },
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(mockListener.mock.calls[0][0].body).toMatchObject(json);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should throw error when body is empty and content-type is `application/json`', async () => {
|
||||||
|
mockListener.mockImplementation((req, res) => {
|
||||||
|
console.log(req.body);
|
||||||
|
res.end();
|
||||||
|
});
|
||||||
|
|
||||||
|
const res = await fetchWithProxyReq(url, {
|
||||||
|
method: 'POST',
|
||||||
|
body: '',
|
||||||
|
headers: { 'content-type': 'application/json' },
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(res.status).toBe(400);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should be able to try/catch parse errors', async () => {
|
||||||
|
const bodySpy = jest.fn(() => {});
|
||||||
|
|
||||||
|
mockListener.mockImplementation((req, res) => {
|
||||||
|
try {
|
||||||
|
if (req.body === undefined) res.status(400);
|
||||||
|
} catch (error) {
|
||||||
|
bodySpy(error);
|
||||||
|
} finally {
|
||||||
|
res.end();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
await fetchWithProxyReq(url, {
|
||||||
|
method: 'POST',
|
||||||
|
body: '{"wrong":"json"',
|
||||||
|
headers: { 'content-type': 'application/json' },
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(bodySpy).toHaveBeenCalled();
|
||||||
|
|
||||||
|
const [error] = bodySpy.mock.calls[0];
|
||||||
|
expect(error.message).toMatch(/invalid json/i);
|
||||||
|
expect(error.statusCode).toBe(400);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('res.status', () => {
|
||||||
|
test('res.status() should set the status code', async () => {
|
||||||
|
mockListener.mockImplementation((req, res) => {
|
||||||
|
res.status(404);
|
||||||
|
res.end();
|
||||||
|
});
|
||||||
|
|
||||||
|
const res = await fetchWithProxyReq(url);
|
||||||
|
|
||||||
|
expect(res.status).toBe(404);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('res.status() should be chainable', async () => {
|
||||||
|
const spy = jest.fn();
|
||||||
|
|
||||||
|
mockListener.mockImplementation((req, res) => {
|
||||||
|
spy(res, res.status(404));
|
||||||
|
res.end();
|
||||||
|
});
|
||||||
|
|
||||||
|
await fetchWithProxyReq(url);
|
||||||
|
|
||||||
|
const [a, b] = spy.mock.calls[0];
|
||||||
|
expect(a).toBe(b);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// tests based on expressjs test suite
|
||||||
|
// see https://github.com/expressjs/express/blob/master/test/res.send.js
|
||||||
|
describe('res.send', () => {
|
||||||
|
test('should be chainable', async () => {
|
||||||
|
const spy = jest.fn();
|
||||||
|
|
||||||
|
mockListener.mockImplementation((req, res) => {
|
||||||
|
spy(res, res.send('hello'));
|
||||||
|
});
|
||||||
|
|
||||||
|
await fetchWithProxyReq(url);
|
||||||
|
|
||||||
|
const [a, b] = spy.mock.calls[0];
|
||||||
|
expect(a).toBe(b);
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('res.send()', () => {
|
||||||
|
test('should set body to ""', async () => {
|
||||||
|
mockListener.mockImplementation((req, res) => {
|
||||||
|
res.send();
|
||||||
|
});
|
||||||
|
|
||||||
|
const res = await fetchWithProxyReq(url);
|
||||||
|
expect(res.status).toBe(200);
|
||||||
|
expect(await res.text()).toBe('');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('.send(null)', () => {
|
||||||
|
test('should set body to ""', async () => {
|
||||||
|
mockListener.mockImplementation((req, res) => {
|
||||||
|
res.send(null);
|
||||||
|
});
|
||||||
|
|
||||||
|
const res = await fetchWithProxyReq(url);
|
||||||
|
expect(res.status).toBe(200);
|
||||||
|
expect(res.headers.get('content-length')).toBe('0');
|
||||||
|
expect(await res.text()).toBe('');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('.send(undefined)', () => {
|
||||||
|
test('should set body to ""', async () => {
|
||||||
|
mockListener.mockImplementation((req, res) => {
|
||||||
|
res.send(undefined);
|
||||||
|
});
|
||||||
|
|
||||||
|
const res = await fetchWithProxyReq(url);
|
||||||
|
expect(res.status).toBe(200);
|
||||||
|
expect(await res.text()).toBe('');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('.send(String)', () => {
|
||||||
|
test('should send as html', async () => {
|
||||||
|
mockListener.mockImplementation((req, res) => {
|
||||||
|
res.send('<p>hey</p>');
|
||||||
|
});
|
||||||
|
|
||||||
|
const res = await fetchWithProxyReq(url);
|
||||||
|
expect(res.headers.get('content-type')).toBe('text/html; charset=utf-8');
|
||||||
|
expect(await res.text()).toBe('<p>hey</p>');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should set Content-Length', async () => {
|
||||||
|
mockListener.mockImplementation((req, res) => {
|
||||||
|
res.send('½ + ¼ = ¾');
|
||||||
|
});
|
||||||
|
|
||||||
|
const res = await fetchWithProxyReq(url);
|
||||||
|
expect(res.status).toBe(200);
|
||||||
|
expect(Number(res.headers.get('content-length'))).toBe(12);
|
||||||
|
expect(await res.text()).toBe('½ + ¼ = ¾');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should set ETag', async () => {
|
||||||
|
mockListener.mockImplementation((req, res) => {
|
||||||
|
res.send(Array(1000).join('-'));
|
||||||
|
});
|
||||||
|
|
||||||
|
const res = await fetchWithProxyReq(url);
|
||||||
|
expect(res.status).toBe(200);
|
||||||
|
expect(res.headers.get('ETag')).toBe(
|
||||||
|
'W/"3e7-qPnkJ3CVdVhFJQvUBfF10TmVA7g"'
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should not override Content-Type', async () => {
|
||||||
|
mockListener.mockImplementation((req, res) => {
|
||||||
|
res.setHeader('Content-Type', 'text/plain');
|
||||||
|
res.send('hey');
|
||||||
|
});
|
||||||
|
|
||||||
|
const res = await fetchWithProxyReq(url);
|
||||||
|
expect(res.status).toBe(200);
|
||||||
|
expect(res.headers.get('Content-Type')).toBe('text/plain; charset=utf-8');
|
||||||
|
expect(await res.text()).toBe('hey');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should override charset in Content-Type', async () => {
|
||||||
|
mockListener.mockImplementation((req, res) => {
|
||||||
|
res.setHeader('Content-Type', 'text/plain; charset=iso-8859-1');
|
||||||
|
res.send('hey');
|
||||||
|
});
|
||||||
|
|
||||||
|
const res = await fetchWithProxyReq(url);
|
||||||
|
expect(res.status).toBe(200);
|
||||||
|
expect(res.headers.get('Content-Type')).toBe('text/plain; charset=utf-8');
|
||||||
|
expect(await res.text()).toBe('hey');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('.send(Buffer)', () => {
|
||||||
|
test('should keep charset in Content-Type', async () => {
|
||||||
|
mockListener.mockImplementation((req, res) => {
|
||||||
|
res.setHeader('Content-Type', 'text/plain; charset=iso-8859-1');
|
||||||
|
res.send(Buffer.from('hi'));
|
||||||
|
});
|
||||||
|
|
||||||
|
const res = await fetchWithProxyReq(url);
|
||||||
|
expect(res.status).toBe(200);
|
||||||
|
expect(res.headers.get('Content-Type')).toBe(
|
||||||
|
'text/plain; charset=iso-8859-1'
|
||||||
|
);
|
||||||
|
expect(await res.text()).toBe('hi');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should set Content-Length', async () => {
|
||||||
|
mockListener.mockImplementation((req, res) => {
|
||||||
|
res.send(Buffer.from('½ + ¼ = ¾'));
|
||||||
|
});
|
||||||
|
|
||||||
|
const res = await fetchWithProxyReq(url);
|
||||||
|
expect(res.status).toBe(200);
|
||||||
|
expect(Number(res.headers.get('content-length'))).toBe(12);
|
||||||
|
expect(await res.text()).toBe('½ + ¼ = ¾');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should send as octet-stream', async () => {
|
||||||
|
mockListener.mockImplementation((req, res) => {
|
||||||
|
res.send(Buffer.from('hello'));
|
||||||
|
});
|
||||||
|
|
||||||
|
const res = await fetchWithProxyReq(url);
|
||||||
|
expect(res.status).toBe(200);
|
||||||
|
expect(res.headers.get('Content-Type')).toBe('application/octet-stream');
|
||||||
|
expect((await res.buffer()).toString('hex')).toBe(
|
||||||
|
Buffer.from('hello').toString('hex')
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should set ETag', async () => {
|
||||||
|
mockListener.mockImplementation((req, res) => {
|
||||||
|
res.send(Buffer.alloc(999, '-'));
|
||||||
|
});
|
||||||
|
|
||||||
|
const res = await fetchWithProxyReq(url);
|
||||||
|
expect(res.status).toBe(200);
|
||||||
|
expect(res.headers.get('ETag')).toBe(
|
||||||
|
'W/"3e7-qPnkJ3CVdVhFJQvUBfF10TmVA7g"'
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should not override Content-Type', async () => {
|
||||||
|
mockListener.mockImplementation((req, res) => {
|
||||||
|
res.setHeader('Content-Type', 'text/plain; charset=utf-8');
|
||||||
|
res.send(Buffer.from('hey'));
|
||||||
|
});
|
||||||
|
|
||||||
|
const res = await fetchWithProxyReq(url);
|
||||||
|
expect(res.status).toBe(200);
|
||||||
|
expect(res.headers.get('Content-Type')).toBe('text/plain; charset=utf-8');
|
||||||
|
expect(await res.text()).toBe('hey');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should not override ETag', async () => {
|
||||||
|
mockListener.mockImplementation((req, res) => {
|
||||||
|
res.setHeader('ETag', '"foo"');
|
||||||
|
res.send(Buffer.from('hey'));
|
||||||
|
});
|
||||||
|
|
||||||
|
const res = await fetchWithProxyReq(url);
|
||||||
|
expect(res.status).toBe(200);
|
||||||
|
expect(res.headers.get('ETag')).toBe('"foo"');
|
||||||
|
expect(await res.text()).toBe('hey');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('.send(Object)', () => {
|
||||||
|
test('should send as application/json', async () => {
|
||||||
|
mockListener.mockImplementation((req, res) => {
|
||||||
|
res.send({ name: 'tobi' });
|
||||||
|
});
|
||||||
|
|
||||||
|
const res = await fetchWithProxyReq(url);
|
||||||
|
expect(res.status).toBe(200);
|
||||||
|
expect(res.headers.get('Content-Type')).toBe(
|
||||||
|
'application/json; charset=utf-8'
|
||||||
|
);
|
||||||
|
expect(await res.text()).toBe('{"name":"tobi"}');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('when the request method is HEAD', () => {
|
||||||
|
test('should ignore the body', async () => {
|
||||||
|
mockListener.mockImplementation((req, res) => {
|
||||||
|
res.send('yay');
|
||||||
|
});
|
||||||
|
|
||||||
|
// TODO: fix this test
|
||||||
|
// node-fetch is automatically ignoring the body so this test will never fail
|
||||||
|
const res = await fetchWithProxyReq(url, { method: 'HEAD' });
|
||||||
|
expect(res.status).toBe(200);
|
||||||
|
expect((await res.buffer()).toString()).toBe('');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('when .statusCode is 204', () => {
|
||||||
|
test('should strip Content-* fields, Transfer-Encoding field, and body', async () => {
|
||||||
|
mockListener.mockImplementation((req, res) => {
|
||||||
|
res.statusCode = 204;
|
||||||
|
res.setHeader('Transfer-Encoding', 'chunked');
|
||||||
|
res.send('foo');
|
||||||
|
});
|
||||||
|
|
||||||
|
const res = await fetchWithProxyReq(url);
|
||||||
|
expect(res.status).toBe(204);
|
||||||
|
expect(res.headers.get('Content-Type')).toBe(null);
|
||||||
|
expect(res.headers.get('Content-Length')).toBe(null);
|
||||||
|
expect(res.headers.get('Transfer-Encoding')).toBe(null);
|
||||||
|
expect(await res.text()).toBe('');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('when .statusCode is 304', () => {
|
||||||
|
test('should strip Content-* fields, Transfer-Encoding field, and body', async () => {
|
||||||
|
mockListener.mockImplementation((req, res) => {
|
||||||
|
res.statusCode = 304;
|
||||||
|
res.setHeader('Transfer-Encoding', 'chunked');
|
||||||
|
res.send('foo');
|
||||||
|
});
|
||||||
|
|
||||||
|
const res = await fetchWithProxyReq(url);
|
||||||
|
expect(res.status).toBe(304);
|
||||||
|
expect(res.headers.get('Content-Type')).toBe(null);
|
||||||
|
expect(res.headers.get('Content-Length')).toBe(null);
|
||||||
|
expect(res.headers.get('Transfer-Encoding')).toBe(null);
|
||||||
|
expect(await res.text()).toBe('');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// test('should always check regardless of length', async () => {
|
||||||
|
// const etag = '"asdf"';
|
||||||
|
|
||||||
|
// mockListener.mockImplementation((req, res) => {
|
||||||
|
// res.setHeader('ETag', etag);
|
||||||
|
// res.send('hey');
|
||||||
|
// });
|
||||||
|
|
||||||
|
// const res = await fetchWithProxyReq(url, {
|
||||||
|
// headers: { 'If-None-Match': etag },
|
||||||
|
// });
|
||||||
|
// expect(res.status).toBe(304);
|
||||||
|
// });
|
||||||
|
|
||||||
|
// test('should respond with 304 Not Modified when fresh', async () => {
|
||||||
|
// const etag = '"asdf"';
|
||||||
|
|
||||||
|
// mockListener.mockImplementation((req, res) => {
|
||||||
|
// res.setHeader('ETag', etag);
|
||||||
|
// res.send(Array(1000).join('-'));
|
||||||
|
// });
|
||||||
|
|
||||||
|
// const res = await fetchWithProxyReq(url, {
|
||||||
|
// headers: { 'If-None-Match': etag },
|
||||||
|
// });
|
||||||
|
// expect(res.status).toBe(304);
|
||||||
|
// });
|
||||||
|
|
||||||
|
// test('should not perform freshness check unless 2xx or 304', async () => {
|
||||||
|
// const etag = '"asdf"';
|
||||||
|
|
||||||
|
// mockListener.mockImplementation((req, res) => {
|
||||||
|
// res.status(500);
|
||||||
|
// res.setHeader('ETag', etag);
|
||||||
|
// res.send('hey');
|
||||||
|
// });
|
||||||
|
|
||||||
|
// const res = await fetchWithProxyReq(url, {
|
||||||
|
// headers: { 'If-None-Match': etag },
|
||||||
|
// });
|
||||||
|
// expect(res.status).toBe(500);
|
||||||
|
// expect(await res.text()).toBe('hey');
|
||||||
|
// });
|
||||||
|
|
||||||
|
describe('etag', () => {
|
||||||
|
test('should send ETag', async () => {
|
||||||
|
mockListener.mockImplementation((req, res) => {
|
||||||
|
res.send('kajdslfkasdf');
|
||||||
|
});
|
||||||
|
|
||||||
|
const res = await fetchWithProxyReq(url);
|
||||||
|
expect(res.status).toBe(200);
|
||||||
|
expect(res.headers.get('ETag')).toBe('W/"c-IgR/L5SF7CJQff4wxKGF/vfPuZ0"');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should send ETag for empty string response', async () => {
|
||||||
|
mockListener.mockImplementation((req, res) => {
|
||||||
|
res.send('');
|
||||||
|
});
|
||||||
|
|
||||||
|
const res = await fetchWithProxyReq(url);
|
||||||
|
expect(res.status).toBe(200);
|
||||||
|
expect(res.headers.get('ETag')).toBe('W/"0-2jmj7l5rSw0yVb/vlWAYkK/YBwk"');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should send ETag for long response', async () => {
|
||||||
|
mockListener.mockImplementation((req, res) => {
|
||||||
|
res.send(Array(1000).join('-'));
|
||||||
|
});
|
||||||
|
|
||||||
|
const res = await fetchWithProxyReq(url);
|
||||||
|
expect(res.status).toBe(200);
|
||||||
|
expect(res.headers.get('ETag')).toBe(
|
||||||
|
'W/"3e7-qPnkJ3CVdVhFJQvUBfF10TmVA7g"'
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should not override ETag when manually set', async () => {
|
||||||
|
mockListener.mockImplementation((req, res) => {
|
||||||
|
res.setHeader('etag', '"asdf"');
|
||||||
|
res.send('hello');
|
||||||
|
});
|
||||||
|
|
||||||
|
const res = await fetchWithProxyReq(url);
|
||||||
|
expect(res.status).toBe(200);
|
||||||
|
expect(res.headers.get('ETag')).toBe('"asdf"');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should not send ETag for res.send()', async () => {
|
||||||
|
mockListener.mockImplementation((req, res) => {
|
||||||
|
res.send();
|
||||||
|
});
|
||||||
|
|
||||||
|
const res = await fetchWithProxyReq(url);
|
||||||
|
expect(res.status).toBe(200);
|
||||||
|
expect(res.headers.get('ETag')).toBe(null);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// tests based on expressjs test suite
|
||||||
|
// see https://github.com/expressjs/express/blob/master/test/res.json.js
|
||||||
|
describe('res.json', () => {
|
||||||
|
test('should send be chainable', async () => {
|
||||||
|
const spy = jest.fn();
|
||||||
|
|
||||||
|
mockListener.mockImplementation((req, res) => {
|
||||||
|
spy(res, res.json({ hello: 'world' }));
|
||||||
|
});
|
||||||
|
|
||||||
|
await fetchWithProxyReq(url);
|
||||||
|
|
||||||
|
const [a, b] = spy.mock.calls[0];
|
||||||
|
expect(a).toBe(b);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('res.json() should send an empty body', async () => {
|
||||||
|
mockListener.mockImplementation((req, res) => {
|
||||||
|
res.json();
|
||||||
|
});
|
||||||
|
|
||||||
|
await fetchWithProxyReq(url);
|
||||||
|
|
||||||
|
const res = await fetchWithProxyReq(url);
|
||||||
|
expect(res.status).toBe(200);
|
||||||
|
expect(res.headers.get('content-type')).toBe(
|
||||||
|
'application/json; charset=utf-8'
|
||||||
|
);
|
||||||
|
expect(await res.text()).toBe('');
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('.json(object)', () => {
|
||||||
|
test('should not override previous Content-Types', async () => {
|
||||||
|
mockListener.mockImplementation((req, res) => {
|
||||||
|
res.setHeader('content-type', 'application/vnd.example+json');
|
||||||
|
res.json({ hello: 'world' });
|
||||||
|
});
|
||||||
|
|
||||||
|
const res = await fetchWithProxyReq(url);
|
||||||
|
expect(res.status).toBe(200);
|
||||||
|
expect(res.headers.get('content-type')).toBe(
|
||||||
|
'application/vnd.example+json; charset=utf-8'
|
||||||
|
);
|
||||||
|
expect(await res.text()).toBe('{"hello":"world"}');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should set Content-Length and Content-Type', async () => {
|
||||||
|
mockListener.mockImplementation((req, res) => {
|
||||||
|
res.json({ hello: '½ + ¼ = ¾' });
|
||||||
|
});
|
||||||
|
|
||||||
|
const res = await fetchWithProxyReq(url);
|
||||||
|
expect(res.status).toBe(200);
|
||||||
|
expect(res.headers.get('content-type')).toBe(
|
||||||
|
'application/json; charset=utf-8'
|
||||||
|
);
|
||||||
|
expect(Number(res.headers.get('content-length'))).toBe(24);
|
||||||
|
expect(await res.text()).toBe('{"hello":"½ + ¼ = ¾"}');
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('when given primitives', () => {
|
||||||
|
test('should respond with json for null', async () => {
|
||||||
|
mockListener.mockImplementation((req, res) => {
|
||||||
|
res.json(null);
|
||||||
|
});
|
||||||
|
|
||||||
|
const res = await fetchWithProxyReq(url);
|
||||||
|
expect(res.status).toBe(200);
|
||||||
|
expect(res.headers.get('content-type')).toBe(
|
||||||
|
'application/json; charset=utf-8'
|
||||||
|
);
|
||||||
|
expect(await res.text()).toBe('null');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should respond with json for Number', async () => {
|
||||||
|
mockListener.mockImplementation((req, res) => {
|
||||||
|
res.json(300);
|
||||||
|
});
|
||||||
|
|
||||||
|
const res = await fetchWithProxyReq(url);
|
||||||
|
expect(res.status).toBe(200);
|
||||||
|
expect(res.headers.get('content-type')).toBe(
|
||||||
|
'application/json; charset=utf-8'
|
||||||
|
);
|
||||||
|
expect(await res.text()).toBe('300');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should respond with json for String', async () => {
|
||||||
|
mockListener.mockImplementation((req, res) => {
|
||||||
|
res.json('str');
|
||||||
|
});
|
||||||
|
|
||||||
|
const res = await fetchWithProxyReq(url);
|
||||||
|
expect(res.status).toBe(200);
|
||||||
|
expect(res.headers.get('content-type')).toBe(
|
||||||
|
'application/json; charset=utf-8'
|
||||||
|
);
|
||||||
|
expect(await res.text()).toBe('"str"');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should respond with json when given an array', async () => {
|
||||||
|
mockListener.mockImplementation((req, res) => {
|
||||||
|
res.json(['foo', 'bar', 'baz']);
|
||||||
|
});
|
||||||
|
|
||||||
|
const res = await fetchWithProxyReq(url);
|
||||||
|
expect(res.status).toBe(200);
|
||||||
|
expect(res.headers.get('content-type')).toBe(
|
||||||
|
'application/json; charset=utf-8'
|
||||||
|
);
|
||||||
|
expect(await res.text()).toBe('["foo","bar","baz"]');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should respond with json when given an object', async () => {
|
||||||
|
mockListener.mockImplementation((req, res) => {
|
||||||
|
res.json({ name: 'tobi' });
|
||||||
|
});
|
||||||
|
|
||||||
|
const res = await fetchWithProxyReq(url);
|
||||||
|
expect(res.status).toBe(200);
|
||||||
|
expect(res.headers.get('content-type')).toBe(
|
||||||
|
'application/json; charset=utf-8'
|
||||||
|
);
|
||||||
|
expect(await res.text()).toBe('{"name":"tobi"}');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -7,7 +7,8 @@
|
|||||||
"module": "commonjs",
|
"module": "commonjs",
|
||||||
"outDir": "dist",
|
"outDir": "dist",
|
||||||
"sourceMap": false,
|
"sourceMap": false,
|
||||||
"declaration": false
|
"declaration": true,
|
||||||
|
"typeRoots": ["./@types", "./node_modules/@types"]
|
||||||
},
|
},
|
||||||
"include": ["src/**/*"],
|
"include": ["src/**/*"],
|
||||||
"exclude": ["node_modules"]
|
"exclude": ["node_modules"]
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@now/optipng",
|
"name": "@now/optipng",
|
||||||
"version": "0.6.2",
|
"version": "0.6.3",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"main": "./dist/index",
|
"main": "./dist/index",
|
||||||
"files": [
|
"files": [
|
||||||
@@ -21,6 +21,6 @@
|
|||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/node": "11.9.4",
|
"@types/node": "11.9.4",
|
||||||
"typescript": "3.3.3"
|
"typescript": "3.5.2"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,11 +2,11 @@ from http.server import BaseHTTPRequestHandler
|
|||||||
|
|
||||||
import base64
|
import base64
|
||||||
import json
|
import json
|
||||||
|
import inspect
|
||||||
|
|
||||||
import __NOW_HANDLER_FILENAME
|
import __NOW_HANDLER_FILENAME
|
||||||
__now_variables = dir(__NOW_HANDLER_FILENAME)
|
__now_variables = dir(__NOW_HANDLER_FILENAME)
|
||||||
|
|
||||||
|
|
||||||
if 'handler' in __now_variables or 'Handler' in __now_variables:
|
if 'handler' in __now_variables or 'Handler' in __now_variables:
|
||||||
base = __NOW_HANDLER_FILENAME.handler if ('handler' in __now_variables) else __NOW_HANDLER_FILENAME.Handler
|
base = __NOW_HANDLER_FILENAME.handler if ('handler' in __now_variables) else __NOW_HANDLER_FILENAME.Handler
|
||||||
if not issubclass(base, BaseHTTPRequestHandler):
|
if not issubclass(base, BaseHTTPRequestHandler):
|
||||||
@@ -47,6 +47,7 @@ if 'handler' in __now_variables or 'Handler' in __now_variables:
|
|||||||
'body': res.text,
|
'body': res.text,
|
||||||
}
|
}
|
||||||
elif 'app' in __now_variables:
|
elif 'app' in __now_variables:
|
||||||
|
if not inspect.iscoroutinefunction(__NOW_HANDLER_FILENAME.app.__call__):
|
||||||
print('using Web Server Gateway Interface (WSGI)')
|
print('using Web Server Gateway Interface (WSGI)')
|
||||||
import sys
|
import sys
|
||||||
from urllib.parse import urlparse, unquote
|
from urllib.parse import urlparse, unquote
|
||||||
@@ -68,8 +69,9 @@ elif 'app' in __now_variables:
|
|||||||
if isinstance(body, string_types):
|
if isinstance(body, string_types):
|
||||||
body = to_bytes(body, charset='utf-8')
|
body = to_bytes(body, charset='utf-8')
|
||||||
|
|
||||||
path = unquote(payload['path'])
|
url = urlparse(unquote(payload['path']))
|
||||||
query = urlparse(path).query
|
query = url.query
|
||||||
|
path = url.path
|
||||||
|
|
||||||
environ = {
|
environ = {
|
||||||
'CONTENT_LENGTH': str(len(body)),
|
'CONTENT_LENGTH': str(len(body)),
|
||||||
@@ -116,6 +118,131 @@ elif 'app' in __now_variables:
|
|||||||
return_dict['encoding'] = 'base64'
|
return_dict['encoding'] = 'base64'
|
||||||
|
|
||||||
return return_dict
|
return return_dict
|
||||||
|
else:
|
||||||
|
print('using Asynchronous Server Gateway Interface (ASGI)')
|
||||||
|
import asyncio
|
||||||
|
import enum
|
||||||
|
from urllib.parse import urlparse, unquote, urlencode
|
||||||
|
|
||||||
|
|
||||||
|
class ASGICycleState(enum.Enum):
|
||||||
|
REQUEST = enum.auto()
|
||||||
|
RESPONSE = enum.auto()
|
||||||
|
|
||||||
|
|
||||||
|
class ASGICycle:
|
||||||
|
def __init__(self, scope):
|
||||||
|
self.scope = scope
|
||||||
|
self.body = b''
|
||||||
|
self.state = ASGICycleState.REQUEST
|
||||||
|
self.app_queue = None
|
||||||
|
self.response = {}
|
||||||
|
|
||||||
|
def __call__(self, app, body):
|
||||||
|
"""
|
||||||
|
Receives the application and any body included in the request, then builds the
|
||||||
|
ASGI instance using the connection scope.
|
||||||
|
Runs until the response is completely read from the application.
|
||||||
|
"""
|
||||||
|
loop = asyncio.new_event_loop()
|
||||||
|
self.app_queue = asyncio.Queue(loop=loop)
|
||||||
|
self.put_message({'type': 'http.request', 'body': body, 'more_body': False})
|
||||||
|
|
||||||
|
asgi_instance = app(self.scope, self.receive, self.send)
|
||||||
|
|
||||||
|
asgi_task = loop.create_task(asgi_instance)
|
||||||
|
loop.run_until_complete(asgi_task)
|
||||||
|
return self.response
|
||||||
|
|
||||||
|
def put_message(self, message):
|
||||||
|
self.app_queue.put_nowait(message)
|
||||||
|
|
||||||
|
async def receive(self):
|
||||||
|
"""
|
||||||
|
Awaited by the application to receive messages in the queue.
|
||||||
|
"""
|
||||||
|
message = await self.app_queue.get()
|
||||||
|
return message
|
||||||
|
|
||||||
|
async def send(self, message):
|
||||||
|
"""
|
||||||
|
Awaited by the application to send messages to the current cycle instance.
|
||||||
|
"""
|
||||||
|
message_type = message['type']
|
||||||
|
|
||||||
|
if self.state is ASGICycleState.REQUEST:
|
||||||
|
if message_type != 'http.response.start':
|
||||||
|
raise RuntimeError(
|
||||||
|
f"Expected 'http.response.start', received: {message_type}"
|
||||||
|
)
|
||||||
|
|
||||||
|
status_code = message['status']
|
||||||
|
headers = {k: v for k, v in message.get('headers', [])}
|
||||||
|
|
||||||
|
self.on_request(headers, status_code)
|
||||||
|
self.state = ASGICycleState.RESPONSE
|
||||||
|
|
||||||
|
elif self.state is ASGICycleState.RESPONSE:
|
||||||
|
if message_type != 'http.response.body':
|
||||||
|
raise RuntimeError(
|
||||||
|
f"Expected 'http.response.body', received: {message_type}"
|
||||||
|
)
|
||||||
|
|
||||||
|
body = message.get('body', b'')
|
||||||
|
more_body = message.get('more_body', False)
|
||||||
|
|
||||||
|
# The body must be completely read before returning the response.
|
||||||
|
self.body += body
|
||||||
|
|
||||||
|
if not more_body:
|
||||||
|
self.on_response()
|
||||||
|
self.put_message({'type': 'http.disconnect'})
|
||||||
|
|
||||||
|
def on_request(self, headers, status_code):
|
||||||
|
self.response['statusCode'] = status_code
|
||||||
|
self.response['headers'] = {k.decode(): v.decode() for k, v in headers.items()}
|
||||||
|
|
||||||
|
def on_response(self):
|
||||||
|
if self.body:
|
||||||
|
self.response['body'] = base64.b64encode(self.body).decode('utf-8')
|
||||||
|
self.response['encoding'] = 'base64'
|
||||||
|
|
||||||
|
def now_handler(event, context):
|
||||||
|
payload = json.loads(event['body'])
|
||||||
|
|
||||||
|
headers = payload.get('headers', {})
|
||||||
|
|
||||||
|
body = payload.get('body', b'')
|
||||||
|
if payload.get('encoding') == 'base64':
|
||||||
|
body = base64.b64decode(body)
|
||||||
|
elif not isinstance(body, bytes):
|
||||||
|
body = body.encode()
|
||||||
|
|
||||||
|
url = urlparse(unquote(payload['path']))
|
||||||
|
query = url.query.encode()
|
||||||
|
path = url.path
|
||||||
|
|
||||||
|
scope = {
|
||||||
|
'server': (headers.get('host', 'lambda'), headers.get('x-forwarded-port', 80)),
|
||||||
|
'client': (headers.get(
|
||||||
|
'x-forwarded-for', headers.get(
|
||||||
|
'x-real-ip', payload.get(
|
||||||
|
'true-client-ip', ''))), 0),
|
||||||
|
'scheme': headers.get('x-forwarded-proto', 'http'),
|
||||||
|
'root_path': '',
|
||||||
|
'query_string': query,
|
||||||
|
'headers': [[k.lower().encode(), v.encode()] for k, v in headers.items()],
|
||||||
|
'type': 'http',
|
||||||
|
'http_version': '1.1',
|
||||||
|
'method': payload['method'],
|
||||||
|
'path': path,
|
||||||
|
'raw_path': path.encode(),
|
||||||
|
}
|
||||||
|
|
||||||
|
asgi_cycle = ASGICycle(scope)
|
||||||
|
response = asgi_cycle(__NOW_HANDLER_FILENAME.app, body)
|
||||||
|
return response
|
||||||
|
|
||||||
else:
|
else:
|
||||||
print('Missing variable `handler` or `app` in file __NOW_HANDLER_FILENAME.py')
|
print('Missing variable `handler` or `app` in file __NOW_HANDLER_FILENAME.py')
|
||||||
print('See the docs https://zeit.co/docs/v2/deployments/official-builders/python-now-python')
|
print('See the docs https://zeit.co/docs/v2/deployments/official-builders/python-now-python')
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"name": "@now/python",
|
"name": "@now/python",
|
||||||
"version": "0.2.5",
|
"version": "0.2.9",
|
||||||
"main": "index.js",
|
"main": "./dist/index.js",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"files": [
|
"files": [
|
||||||
"dist",
|
"dist",
|
||||||
@@ -15,7 +15,7 @@
|
|||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "tsc",
|
"build": "tsc",
|
||||||
"test": "tsc && jest",
|
"test": "tsc && jest",
|
||||||
"prepublish": "tsc"
|
"prepublishOnly": "tsc"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"execa": "^1.0.0",
|
"execa": "^1.0.0",
|
||||||
@@ -23,6 +23,6 @@
|
|||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/execa": "^0.9.0",
|
"@types/execa": "^0.9.0",
|
||||||
"typescript": "3.3.4000"
|
"typescript": "3.5.2"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -104,7 +104,6 @@ export const build = async ({
|
|||||||
workPath = destNow;
|
workPath = destNow;
|
||||||
}
|
}
|
||||||
|
|
||||||
const foundLockFile = 'Pipfile.lock' in downloadedFiles;
|
|
||||||
const pyUserBase = await getWriteableDirectory();
|
const pyUserBase = await getWriteableDirectory();
|
||||||
process.env.PYTHONUSERBASE = pyUserBase;
|
process.env.PYTHONUSERBASE = pyUserBase;
|
||||||
const pipPath = 'pip3';
|
const pipPath = 'pip3';
|
||||||
@@ -129,17 +128,25 @@ export const build = async ({
|
|||||||
await pipInstall(pipPath, workPath, 'werkzeug');
|
await pipInstall(pipPath, workPath, 'werkzeug');
|
||||||
await pipInstall(pipPath, workPath, 'requests');
|
await pipInstall(pipPath, workPath, 'requests');
|
||||||
|
|
||||||
if (foundLockFile) {
|
let fsFiles = await glob('**', workPath);
|
||||||
|
const entryDirectory = dirname(entrypoint);
|
||||||
|
|
||||||
|
const pipfileLockDir = fsFiles[join(entryDirectory, 'Pipfile.lock')]
|
||||||
|
? join(workPath, entryDirectory)
|
||||||
|
: fsFiles['Pipfile.lock']
|
||||||
|
? workPath
|
||||||
|
: null;
|
||||||
|
|
||||||
|
if (pipfileLockDir) {
|
||||||
console.log('found "Pipfile.lock"');
|
console.log('found "Pipfile.lock"');
|
||||||
|
|
||||||
// Install pipenv.
|
// Install pipenv.
|
||||||
await pipInstallUser(pipPath, ' pipenv_to_requirements');
|
await pipInstallUser(pipPath, ' pipenv_to_requirements');
|
||||||
|
|
||||||
await pipenvInstall(pyUserBase, workPath);
|
await pipenvInstall(pyUserBase, pipfileLockDir);
|
||||||
}
|
}
|
||||||
|
|
||||||
const fsFiles = await glob('**', workPath);
|
fsFiles = await glob('**', workPath);
|
||||||
const entryDirectory = dirname(entrypoint);
|
|
||||||
const requirementsTxt = join(entryDirectory, 'requirements.txt');
|
const requirementsTxt = join(entryDirectory, 'requirements.txt');
|
||||||
|
|
||||||
if (fsFiles[requirementsTxt]) {
|
if (fsFiles[requirementsTxt]) {
|
||||||
|
|||||||
12
packages/now-python/test/fixtures/04-cowsay-pipfile/Pipfile
vendored
Normal file
12
packages/now-python/test/fixtures/04-cowsay-pipfile/Pipfile
vendored
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
[[source]]
|
||||||
|
name = "pypi"
|
||||||
|
url = "https://pypi.org/simple"
|
||||||
|
verify_ssl = true
|
||||||
|
|
||||||
|
[dev-packages]
|
||||||
|
|
||||||
|
[packages]
|
||||||
|
cowpy = "*"
|
||||||
|
|
||||||
|
[requires]
|
||||||
|
python_version = "3.7"
|
||||||
29
packages/now-python/test/fixtures/04-cowsay-pipfile/Pipfile.lock
generated
vendored
Normal file
29
packages/now-python/test/fixtures/04-cowsay-pipfile/Pipfile.lock
generated
vendored
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
{
|
||||||
|
"_meta": {
|
||||||
|
"hash": {
|
||||||
|
"sha256": "841b49dd1836f7373490faa2a1d6abb4ecd53c0b45e8196df7c6e852e21f458b"
|
||||||
|
},
|
||||||
|
"pipfile-spec": 6,
|
||||||
|
"requires": {
|
||||||
|
"python_version": "3.7"
|
||||||
|
},
|
||||||
|
"sources": [
|
||||||
|
{
|
||||||
|
"name": "pypi",
|
||||||
|
"url": "https://pypi.org/simple",
|
||||||
|
"verify_ssl": true
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"default": {
|
||||||
|
"cowpy": {
|
||||||
|
"hashes": [
|
||||||
|
"sha256:1bdc61d107df02fd34a9241f2220d0704a01d8ce16bed8bff3512a34a0efa56a",
|
||||||
|
"sha256:91a861bfbfa644dfdba5b2250d141b2227a94f61d4dcbeaf7653524d048935a9"
|
||||||
|
],
|
||||||
|
"index": "pypi",
|
||||||
|
"version": "==1.1.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"develop": {}
|
||||||
|
}
|
||||||
13
packages/now-python/test/fixtures/04-cowsay-pipfile/index.py
vendored
Normal file
13
packages/now-python/test/fixtures/04-cowsay-pipfile/index.py
vendored
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
from http.server import BaseHTTPRequestHandler
|
||||||
|
from cowpy import cow
|
||||||
|
|
||||||
|
|
||||||
|
class handler(BaseHTTPRequestHandler):
|
||||||
|
|
||||||
|
def do_GET(self):
|
||||||
|
self.send_response(200)
|
||||||
|
self.send_header('Content-type', 'text/plain')
|
||||||
|
self.end_headers()
|
||||||
|
message = cow.Cowacter().milk('pip:RANDOMNESS_PLACEHOLDER')
|
||||||
|
self.wfile.write(message.encode())
|
||||||
|
return
|
||||||
5
packages/now-python/test/fixtures/04-cowsay-pipfile/now.json
vendored
Normal file
5
packages/now-python/test/fixtures/04-cowsay-pipfile/now.json
vendored
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
{
|
||||||
|
"version": 2,
|
||||||
|
"builds": [{ "src": "**/**.py", "use": "@now/python" }],
|
||||||
|
"probes": [{ "path": "/", "mustContain": "pip: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