Compare commits

...

113 Commits

Author SHA1 Message Date
Nathan Rajlich
9e1d577fc0 Publish
- @now/bash@0.1.2-canary.0
 - @now/build-utils@0.4.33-canary.2
 - @now/cgi@0.0.16-canary.0
 - @now/go@0.2.13-canary.0
 - @now/html-minifier@1.0.8-canary.0
 - @now/lambda@0.4.10-canary.0
 - @now/md@0.4.10-canary.0
 - @now/mdx-deck@0.4.19-canary.0
 - @now/next@0.0.85-canary.1
 - @now/node-bridge@0.1.11-canary.0
 - @now/node-server@0.4.27-canary.2
 - @now/node@0.4.29-canary.2
 - @now/optipng@0.4.9-canary.0
 - @now/php-bridge@0.4.14-canary.0
 - @now/php@0.4.14-canary.0
 - @now/python@0.0.41-canary.0
 - @now/rust@0.0.2-canary.0
 - @now/static-build@0.4.18-canary.0
 - @now/wordpress@0.4.15-canary.0
2019-01-31 14:32:48 -08:00
António Nuno Monteiro
cf2f542c71 Initial import of @now/rust (#191)
* Fix docstrings, remove unused rust dev dependency

* Add support for multiple binaries, prepareCache too

* Remove context from the handler signature, abstract over the AWS error

* Fix Rust tarball URL, caching and lint

* GCC URL
2019-01-31 22:29:50 +00:00
Igor Klopov
e608861e4e [now-node] bump ncc 2019-01-31 02:52:37 +03:00
Steven
a99b999209 Remove @now/build-utils peer dependency (#187)
* Remove peer dependencies

* Suppress eslint warning
2019-01-29 15:18:36 -05:00
Steven
fd9c6e7847 Add license to satisify warning (#186)
This solves the warning seen in `/_logs` all the time:

```
warning package.json: No license field
```
2019-01-29 12:59:25 -05:00
Steven
b2ad3a6147 Add repository key to package.json (#185) 2019-01-29 17:52:01 +01:00
Steven
997d3c2a30 Add link to builder docs (#184) 2019-01-29 17:24:54 +01:00
Igor Klopov
ca575bf0a6 Publish
- @now/next@0.0.85-canary.0
 - @now/node-server@0.4.27-canary.1
 - @now/node@0.4.29-canary.1
2019-01-29 19:09:01 +03:00
Igor Klopov
4c2e93ccef [now-node] bump ncc 2019-01-29 18:28:13 +03:00
paulogdm
4d6437d235 Fix broken error link (#175)
* Fix broken error link

* Added legacy mode error

Standard "Next.js" for all citations of Next.
Add "Legacy Mode" error.

* Update no serverless pages built doc

* Update doc on now/next legacy mode

* Add link after the error message
2019-01-28 19:05:02 +01:00
Igor Klopov
0d8058d062 Publish
- @now/node-server@0.4.27-canary.0
 - @now/node@0.4.29-canary.0
2019-01-27 21:58:47 +03:00
Igor Klopov
2b5cdfc0a7 [now-node] bump ncc 2019-01-27 15:27:00 +03:00
Igor Klopov
69a41f78fb Publish
- @now/build-utils@0.4.33-canary.1
2019-01-26 00:59:33 +03:00
Igor Klopov
a013d59d62 [build-utils] yarn in linux calls chown. mock it as well 2019-01-26 00:11:09 +03:00
Igor Klopov
173a29cfdb Publish
- @now/build-utils@0.4.33-canary.0
2019-01-25 02:51:46 +03:00
Igor Klopov
3f73451311 [build-utils] lint fixes 2019-01-25 00:55:32 +03:00
Igor Klopov
2fc706be43 [build-utils] make in-memory cache support git deps 2019-01-25 00:41:33 +03:00
Igor Klopov
0fb7eb6093 [build-utils] don't print download time of individual files 2019-01-24 18:44:10 +03:00
Igor Klopov
aa43c0bc87 [tests] consider now's 404 a case to retry 2019-01-23 18:43:55 +03:00
Igor Klopov
3c5925a6e3 [tests] print what failed page contains 2019-01-23 01:14:01 +03:00
Igor Klopov
9fc7b047f5 [tests] buildDelegate argument for testDeployment 2019-01-23 00:14:43 +03:00
Igor Klopov
ecae29457f Revert "[lerna] trying to set the order of package to publish"
This reverts commit 79251ad180.
2019-01-20 03:35:23 +03:00
Igor Klopov
77d445af71 Publish
- @now/bash@0.1.1
 - @now/build-utils@0.4.32
 - @now/cgi@0.0.15
 - @now/go@0.2.12
 - @now/html-minifier@1.0.7
 - @now/lambda@0.4.9
 - @now/md@0.4.9
 - @now/mdx-deck@0.4.18
 - @now/next@0.0.84
 - @now/node-bridge@0.1.10
 - @now/node-server@0.4.26
 - @now/node@0.4.28
 - @now/optipng@0.4.8
 - @now/php-bridge@0.4.13
 - @now/php@0.4.13
 - @now/static-build@0.4.17
 - @now/wordpress@0.4.14
2019-01-20 01:14:09 +03:00
Igor Klopov
79251ad180 [lerna] trying to set the order of package to publish 2019-01-20 00:01:16 +03:00
Igor Klopov
a215dc9103 [node-server] update tests for latest ncc 2019-01-19 23:23:44 +03:00
Nathan Rajlich
ea7d8f0f6c Update @zeit/ncc to v0.11.0 (#172) 2019-01-19 19:30:20 +03:00
Igor Klopov
2232efc984 fix misleading 'running npm install' messages in logs 2019-01-15 00:54:24 +03:00
Igor Klopov
b64ce0f3c0 [now-node] fix test for content-length 2019-01-14 03:27:15 +03:00
Igor Klopov
74233d50ad now/node: bump ncc 2019-01-12 05:20:37 +03:00
Nathan Rajlich
8aebec9fc3 Publish
- @now/bash@0.1.0
2019-01-10 12:27:59 -08:00
Igor Klopov
54584b7763 [tests] support NOW_TOKEN env var 2019-01-09 20:44:20 +03:00
Igor Klopov
d163fcbd71 [now-node-server] test for default content-type 2019-01-09 20:16:58 +03:00
Tim Neutkens
04c2996c76 [now-next] Add test for monorepo 2019-01-09 15:07:54 +01:00
Igor Klopov
2b69b898ed Publish
- @now/build-utils@0.4.32-canary.6
 - @now/node-bridge@0.1.10-canary.2
 - @now/node-server@0.4.26-canary.7
 - @now/node@0.4.28-canary.6
2019-01-08 01:42:13 +03:00
Igor Klopov
846aa11d6a now/node-bridge: postpone rejecting, let express print the error 2019-01-08 01:22:01 +03:00
Nathan Rajlich
a314a74479 Publish
- @now/bash@0.0.4-canary.2
2019-01-04 18:34:29 -08:00
Nathan Rajlich
40f029a72c [now-bash] Pipe response to jq stdin and use long options
Otherwise `jq` errors out with "arguments too long" when the response
body base64 is too large.

The long options are just for maintainability.
2019-01-04 18:33:56 -08:00
Nathan Rajlich
493d8a778f [now-bash] Update static-binaries to v1.0.0 2019-01-04 18:33:56 -08:00
Nathan Rajlich
cb5dcb658f [now-bash] Remove final build tree output
It's not necessary and can be too noisey if the lambda has a lot of files.
2019-01-04 18:33:56 -08:00
Nathan Rajlich
d77287d07b [now-bash] Add .editorconfig file 2019-01-04 18:33:56 -08:00
Igor Klopov
61d66bd957 Publish
- @now/build-utils@0.4.32-canary.5
 - @now/next@0.0.84-canary.0
2019-01-03 16:52:48 +03:00
Igor Klopov
ae73df9e3c build-utils: account google cloud functions 2019-01-03 16:29:55 +03:00
Tim Neutkens
cb09ae5bbf Publish
- @now/next@0.0.83
2019-01-03 11:56:29 +01:00
Tim Neutkens
a28eeacdaa Publish
- @now/build-utils@0.4.32-canary.4
 - @now/cgi@0.0.15-canary.0
 - @now/go@0.2.12-canary.0
 - @now/html-minifier@1.0.7-canary.0
 - @now/lambda@0.4.9-canary.0
 - @now/md@0.4.9-canary.0
 - @now/mdx-deck@0.4.18-canary.1
 - @now/next@0.0.83-canary.0
 - @now/node-bridge@0.1.10-canary.1
 - @now/node-server@0.4.26-canary.6
 - @now/node@0.4.28-canary.5
 - @now/optipng@0.4.8-canary.0
 - @now/php-bridge@0.4.13-canary.1
 - @now/php@0.4.13-canary.2
 - @now/static-build@0.4.17-canary.0
 - @now/wordpress@0.4.14-canary.1
2019-01-02 17:48:58 +01:00
Tim Neutkens
dd9d46d555 Add Next.js serverless target instead of lambdas build (#150)
* Give test more time

* Implement legacy flag

* Move tests

* Add test for new builder

* Add serverless target flow

* Remove prepareCache

* Add semver check

* Upgrade for latest Next.js canary
2019-01-02 17:48:01 +01:00
Igor Klopov
4472331ee0 tests: use token factory endpoint 2019-01-01 03:17:27 +03:00
Tim Neutkens
ac69836b44 Publish
- @now/next@0.0.82
2018-12-27 13:28:44 +01:00
Steven
15949a4ab4 Add missing license to package.json (#147) 2018-12-27 13:13:49 +01:00
Igor Klopov
697ada9d73 now-node: tests for content-length 2018-12-24 07:51:18 +03:00
Igor Klopov
cafbe30fa3 Publish
- @now/next@0.0.82-canary.1
 - @now/node-bridge@0.1.10-canary.0
 - @now/node-server@0.4.26-canary.5
 - @now/node@0.4.28-canary.4
2018-12-24 07:21:38 +03:00
Igor Klopov
583ebcc526 node-bridge: set content-length in response if user sets it 2018-12-24 06:57:17 +03:00
Connor Davis
52d1bd410c Lock Next.js Version (#145)
* Update utils.js

* Update utils.test.js

* Update utils.test.js

* Temp Commit

* Revert Temp Commit
2018-12-23 11:19:37 +01:00
Nathan Rajlich
11d0753bc1 Publish
- @now/bash@0.0.4-canary.1
 - @now/build-utils@0.4.32-canary.3
2018-12-21 13:46:41 -08:00
Nathan Rajlich
538710fe56 [now-bash] make the curl calls to AWS API silent (#142)
* [now-bash] make the `curl` calls to AWS API silent

Also log the exit status of the `handler` function.

* Remove `echo Request-Id`
2018-12-21 13:44:57 -08:00
Nathan Rajlich
2828c89e8d [now-bash] add stdio: inherit to builder.sh script (#143)
So that the build logs of the bash script are sent to the deployment
logs.
2018-12-21 13:44:42 -08:00
Igor Klopov
e40b45a939 tests: Accept = application/json for all api calls 2018-12-21 03:07:33 +03:00
Igor Klopov
38ba8a36fc Publish
- @now/build-utils@0.4.32-canary.2
 - @now/wordpress@0.4.14-canary.0
2018-12-21 00:59:08 +03:00
Igor Klopov
0323c505a3 now/build-utils: fix cross-install inssue in yarn in-memory cache 2018-12-20 09:03:52 +03:00
Igor Klopov
17ee07f4f6 now/build-utils: add cross-install test 2018-12-20 08:53:29 +03:00
Igor Klopov
0a6ada77ac now/wordpress: support non-root placement (monorepo) 2018-12-19 14:38:42 +03:00
Igor Klopov
4d817dd67d update yarn.lock file to avoid 'lerna ERR! EUNCOMMIT' 2018-12-19 01:55:54 +03:00
Igor Klopov
9682a7cc0b now/wordpress: release 0.4.13 manually 2018-12-18 10:25:24 +03:00
Igor Klopov
3456f23b3e now/wordpress: make latest.zip the default 2018-12-18 10:24:16 +03:00
Igor Klopov
800ca2cb0e Publish
- @now/php@0.4.13-canary.1
 - @now/wordpress@0.4.13-canary.1
2018-12-18 10:09:34 +03:00
Igor Klopov
ba54b4d706 now/wordpress: don't test it yet 2018-12-18 10:09:16 +03:00
Igor Klopov
e9482d66a9 now/php: dummy changes 2018-12-18 10:00:49 +03:00
Igor Klopov
401b669363 Publish
- @now/node-server@0.4.26-canary.4
 - @now/node@0.4.28-canary.3
 - @now/php-bridge@0.4.13-canary.0
 - @now/php@0.4.13-canary.0
 - @now/wordpress@0.4.13-canary.0
2018-12-18 09:51:38 +03:00
Igor Klopov
a2a0ede1f6 now/php-bridge: minor improvements 2018-12-18 09:49:19 +03:00
Igor Klopov
3c9fcff743 now/wordpress (#136)
* now/wordpress copied from new now/php

* download, decompress and merge release url

* static files

* require entrypoint be named 'wp-config.php'

* patch wp-db.php to use persistent connections

* "/wp-admin/index.php" rule is not needed

* emit lambda with name 'index.php'

* more staticRegexps

* make patchForPersistentConnections opt-in

* remove one more extra route

* update they way secrets are passed

* update route regexp a little
2018-12-18 09:47:07 +03:00
Igor Klopov
e5aa526583 now/php: update to use php-fpm 2018-12-18 04:24:08 +03:00
Igor Klopov
822b0ee3de now/node-server: yodasay.umd -> yodasay + pin 2018-12-18 03:21:45 +03:00
Igor Klopov
d612e46233 Publish
- @now/node-server@0.4.26-canary.3
 - @now/node@0.4.28-canary.2
2018-12-17 09:13:13 +03:00
Igor Klopov
77ee10cead now/node: bump ncc 2018-12-17 09:07:45 +03:00
Tim Neutkens
fb2029c464 Publish
- @now/build-utils@0.4.32-canary.1
 - @now/next@0.0.82-canary.0
 - @now/node-server@0.4.26-canary.2
 - @now/node@0.4.28-canary.1
 - @now/php@0.4.13-canary.1
2018-12-17 00:11:08 +01:00
Tim Neutkens
3b15755054 Add heap increase to now/next (#134) 2018-12-17 00:05:41 +01:00
Igor Klopov
4f65cc3aa8 now/build-utils: catch per-file stream errors 2018-12-16 03:17:04 +03:00
Igor Klopov
9936e35280 tests: retry on ETIMEDOUT 2018-12-15 21:55:55 +03:00
Igor Klopov
a04fd242b8 Revert "now/php: create php engine outside of request handling"
This reverts commit a7c2d9648a.
2018-12-12 01:09:12 +03:00
Igor Klopov
17bc6174a9 now/php: a regression test for engine reusage 2018-12-12 00:49:38 +03:00
Igor Klopov
a7c2d9648a now/php: create php engine outside of request handling 2018-12-11 23:04:47 +03:00
Igor Klopov
faa5ea9e21 now/php: fix $HTTP_RAW_POST_DATA 2018-12-11 08:49:45 +03:00
Igor Klopov
c52f30c898 now/php: fix $_SERVER['HTTP_HEADER'] 2018-12-11 07:53:46 +03:00
Igor Klopov
d675edf1dc this fixes several-values-per-header-name issue + test (#129) 2018-12-11 03:12:43 +03:00
Anthony Mittaz
f85c4f496f Bump ncc to v0.5.3 (#128)
* Bump `ncc` to v0.5.3

* Also bump `ncc` to v0.5.3 in now-node
2018-12-10 14:11:23 -08:00
Nathan Rajlich
d52d7904c2 Publish
- @now/bash@0.0.4-canary.0
2018-12-10 11:07:34 -08:00
Nathan Rajlich
79232024bd [now-bash] Rename serve function to handler (#125)
This is more consistent with what you would expect for a lambda
function, and we should maintain that convention.

For example: https://github.com/gkrizek/bash-lambda-layer
2018-12-10 10:55:38 -08:00
Igor Klopov
660b787bc3 tests: print headers in deploymentPost too 2018-12-10 06:34:21 +03:00
Igor Klopov
2dbf983ddb tests: account API_HOST in fetchApi, not in fetchWithAuth 2018-12-10 06:19:42 +03:00
Igor Klopov
0866ba9391 tests: export fetchApi and fetchWithAuth 2018-12-10 05:45:46 +03:00
Igor Klopov
d259a722a0 [test] print resp.headers on filePost failure 2018-12-10 05:10:32 +03:00
Igor Klopov
bf77c51f64 tests: retry on some network failures 2018-12-10 03:59:13 +03:00
Igor Klopov
062b78130c Publish
- @now/build-utils@0.4.32-canary.0
 - @now/mdx-deck@0.4.18-canary.0
 - @now/node-server@0.4.26-canary.1
 - @now/php@0.4.13-canary.0
2018-12-10 02:38:22 +03:00
Igor Klopov
fa70bc50cb sema around await streamToBuffer(zipFile.outputStream) (#124)
* sema around await streamToBuffer(zipFile.outputStream)

* lint fixes

* fix looking for 'undefined' scriptName
2018-12-10 02:30:16 +03:00
Igor Klopov
08e22b35d1 now/node-server: an improvement for 09-no-bundle 2018-12-09 00:32:15 +03:00
Igor Klopov
9d8f3315a1 now/php: partially revert set-cookie commit 2018-12-09 00:31:29 +03:00
Igor Klopov
a737a99a9d now/php: new test for setcookie + fix for bridge.go 2018-12-09 00:08:54 +03:00
Igor Klopov
ee92d92c9f now/php: fixed $_REQUEST 2018-12-08 07:58:08 +03:00
Igor Klopov
34d3ebd289 now/php: fix $_SERVER['SERVER_PROTOCOL'] 2018-12-08 06:35:46 +03:00
Igor Klopov
785f187e5d now/php: unescape cookie values 2018-12-08 06:25:39 +03:00
Igor Klopov
44449f474e now/php: fixed $_COOKIE 2018-12-08 01:03:18 +03:00
Igor Klopov
f44dae7f39 now/php: lint fixes 2018-12-07 23:06:45 +03:00
Igor Klopov
06f973f641 now/php: fixed $_SERVER['REQUEST_METHOD'] 2018-12-07 23:04:47 +03:00
Igor Klopov
47bb47804e now/php: fixed $_POST 2018-12-07 22:52:11 +03:00
António Nuno Monteiro
5df692979a Provide a complete implementation for @now/mdx-deck's analyze function (#97)
* Provide a complete implementation for `@now/mdx-deck`'s analyze function

Fixes #93

* Delete the analyze step
2018-12-07 03:44:56 +03:00
Igor Klopov
cd02d5390f now/php: simplify test.go and test.php 2018-12-07 03:37:25 +03:00
Igor Klopov
c93fd416c4 tests: pass 'env' from now.json to nowDeploy 2018-12-07 02:45:12 +03:00
Igor Klopov
431db7a62d bridge.go: remove duplicating 'host' header check 2018-12-07 02:40:09 +03:00
Igor Klopov
6f86c70313 now/php: use v[len(v) - 1] instead of iterating 2018-12-07 02:13:28 +03:00
Igor Klopov
0923fc9200 now/php: better parsing of _GET 2018-12-07 02:05:25 +03:00
Igor Klopov
787675f462 probe.js for more complex tests. probe.js for now/php globals 2018-12-06 20:53:41 +03:00
Igor Klopov
977615720a improvements and a test for now/php 2018-12-06 08:23:12 +03:00
Igor Klopov
1a4e64cd27 Publish
- @now/node-server@0.4.26-canary.0
 - @now/node@0.4.28-canary.0
2018-12-06 04:16:39 +03:00
Igor Klopov
c9fc255002 don't force set NODE_ENV=production. default instead (#120) 2018-12-06 03:03:02 +03:00
172 changed files with 4799 additions and 709 deletions

1
.gitignore vendored
View File

@@ -1,2 +1,3 @@
node_modules
tmp
target/

View File

@@ -2,7 +2,7 @@
This is the full list of official Builders provided by the ZEIT team.
More details here: http://zeit.co/docs
More details here: https://zeit.co/docs/v2/deployments/builders/overview/
### Publishing to npm

View File

@@ -0,0 +1,73 @@
# `@now/next` Legacy Mode
#### Why This Warning Occurred
`@now/next` has two modes: `legacy` and `serverless`. You will always want to use the `serverless` mode. `legacy` is to provide backwards compatibility with previous `@now/next` versions.
The differences:
Legacy:
- Minimal lambda size of `2.2Mb` (approximately)
- Forces `next@v7.0.2-canary.49` and `next-server@v7.0.2-canary.49`
- Forces all `dependencies` to be `devDependencies`
- Loads `next.config.js` on bootup, breaking sometimes when users didn't use `phases` to load files
- Used `next-server` which is the full Next.js server with routing etc.
- Runs `npm install`
- Runs `npm run now-build`
- Runs `npm install --production` after build
Serverless:
- Minimal lambda size of `49Kb` (approximately)
- Uses Next.js build targets (`target: 'serverless'`) in `next.config.js`. [documentation](https://github.com/zeit/next.js#summary)
- Does not make changes to your application dependencies
- Does not load `next.config.js` ([as per the serverless target documentation](https://github.com/zeit/next.js#summary))
- Runs `npm install`
- Runs `npm run now-build`
- Does not run `npm install --production` as the output from the build is all that's needed to bundle lambdas.
- No runtime dependencies, meaning smaller lambda functions
- Optimized for fast [cold start](https://zeit.co/blog/serverless-ssr#cold-start)
#### Possible Ways to Fix It
In order to create the smallest possible lambdas Next.js has to be configured to build for the `serverless` target.
1. Serverless Next.js requires Next.js 8 or later, currently this version is out on the `canary` release channel:
```
npm install next@canary
```
2. Add the `now-build` script to your `package.json`
```json
{
"scripts": {
"now-build": "next build"
},
}
```
3. Add `target: 'serverless'` to `next.config.js`
```js
module.exports = {
target: 'serverless'
// Other options are still valid
}
```
4. Optionally make sure the `"src"` in `"builds"` points to your application `package.json`
```js
{
"version": 2,
"builds": [{ "src": "package.json", "use": "@now/next" }]
}
```
### Useful Links
- [Serverless target implementation](https://github.com/zeit/now-builders/pull/150)

View File

@@ -0,0 +1,43 @@
# `@now/next` No Serverless Pages Built
#### Why This Error Occurred
This error occurs when you have your application is not configured for Serverless Next.js build output.
#### Possible Ways to Fix It
In order to create the smallest possible lambdas Next.js has to be configured to build for the `serverless` target.
1. Serverless Next.js requires Next.js 8 or later, currently this version is out on the `canary` release channel:
```
npm install next@canary
```
2. Add the `now-build` script to your `package.json`
```json
{
"scripts": {
"now-build": "next build"
},
}
```
3. Add `target: 'serverless'` to `next.config.js`
```js
module.exports = {
target: 'serverless'
// Other options are still valid
}
```
4. Optionally make sure the `"src"` in `"builds"` points to your application `package.json`
```js
{
"version": 2,
"builds": [{ "src": "package.json", "use": "@now/next" }]
}
```

View File

@@ -31,6 +31,7 @@
"@types/glob": "^7.1.1",
"@types/multistream": "^2.1.1",
"@types/node": "^10.12.8",
"async-retry": "1.2.3",
"buffer-replace": "^1.0.0",
"eslint": "^5.9.0",
"eslint-config-airbnb-base": "^13.1.0",

View File

@@ -0,0 +1,32 @@
root = true
[*]
indent_style = tab
indent_size = 4
tab_width = 4
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
[{*.json,*.json.example,*.gyp,*.yml}]
indent_style = space
indent_size = 2
[*.py]
indent_style = space
indent_size = 4
[*.md]
trim_trailing_whitespace = false
# Ideal settings - some plugins might support these.
[*.js]
quote_type = single
[{*.c,*.cc,*.h,*.hh,*.cpp,*.hpp,*.m,*.mm,*.mpp,*.js,*.java,*.go,*.rs,*.php,*.ng,*.jsx,*.ts,*.d,*.cs,*.swift}]
curly_bracket_next_line = false
spaces_around_operators = true
spaces_around_brackets = outside
# close enough to 1TB
indent_brace_style = K&R

View File

@@ -33,17 +33,8 @@ if declare -f build > /dev/null; then
build "$@"
fi
# Ensure the entrypoint defined a `serve` function
if ! declare -f serve > /dev/null; then
echo "ERROR: A \`serve\` function must be defined in \"$ENTRYPOINT\"!" >&2
# Ensure the entrypoint defined a `handler` function
if ! declare -f handler > /dev/null; then
echo "ERROR: A \`handler\` function must be defined in \"$ENTRYPOINT\"!" >&2
exit 1
fi
# Show a tree of the final lambda build
show_tree() {
import "static-binaries@0.0.6"
static_binaries tree
echo "Final lambda file tree:"
tree -a .
}
IMPORT_DEBUG= IMPORT_CACHE="$(mktemp -d)" show_tree

View File

@@ -1,10 +1,10 @@
const execa = require('execa');
const { join } = require('path');
const snakeCase = require('snake-case');
const glob = require('@now/build-utils/fs/glob');
const download = require('@now/build-utils/fs/download');
const { createLambda } = require('@now/build-utils/lambda');
const getWritableDirectory = require('@now/build-utils/fs/get-writable-directory');
const glob = require('@now/build-utils/fs/glob'); // eslint-disable-line import/no-extraneous-dependencies
const download = require('@now/build-utils/fs/download'); // eslint-disable-line import/no-extraneous-dependencies
const { createLambda } = require('@now/build-utils/lambda'); // eslint-disable-line import/no-extraneous-dependencies
const getWritableDirectory = require('@now/build-utils/fs/get-writable-directory'); // eslint-disable-line import/no-extraneous-dependencies
exports.config = {
maxLambdaSize: '10mb',
@@ -38,6 +38,7 @@ exports.build = async ({ files, entrypoint, config }) => {
await execa(builderPath, [entrypoint], {
env,
cwd: workDir,
stdio: 'inherit',
});
const lambda = await createLambda({

View File

@@ -1,10 +1,15 @@
{
"name": "@now/bash",
"version": "0.0.3",
"version": "0.1.2-canary.0",
"description": "Now 2.0 builder for HTTP endpoints written in Bash",
"main": "index.js",
"author": "Nathan Rajlich <nate@zeit.co>",
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/zeit/now-builders.git",
"directory": "packages/now-bash"
},
"files": [
"builder.sh",
"runtime.sh",
@@ -15,8 +20,5 @@
"dependencies": {
"execa": "^1.0.0",
"snake-case": "^2.1.0"
},
"peerDependencies": {
"@now/build-utils": ">=0.0.1"
}
}

View File

@@ -1,4 +1,4 @@
import "static-binaries@0.0.6"
import "static-binaries@1.0.0"
static_binaries jq
# These get reset upon each request
@@ -35,11 +35,10 @@ _lambda_runtime_next() {
# Get an event
local event
event="$(mktemp)"
_lambda_runtime_api invocation/next -D "$headers" | jq -r '.body' > "$event"
_lambda_runtime_api invocation/next -D "$headers" | jq --raw-output --monochrome-output '.body' > "$event"
local request_id
request_id="$(grep -Fi Lambda-Runtime-Aws-Request-Id "$headers" | tr -d '[:space:]' | cut -d: -f2)"
echo "Request-Id: $request_id" >&2
rm -f "$headers"
# Execute the handler function from the script
@@ -49,7 +48,7 @@ _lambda_runtime_next() {
local exit_code=0
REQUEST="$event"
# Stdin of the `serve` function is the HTTP request body.
# Stdin of the `handler` function is the HTTP request body.
# Need to use a fifo here instead of bash <() because Lambda
# errors with "/dev/fd/63 not found" for some reason :/
local stdin
@@ -57,33 +56,30 @@ _lambda_runtime_next() {
mkfifo "$stdin"
_lambda_runtime_body "$event" > "$stdin" &
serve "$event" < "$stdin" > "$body" || exit_code="$?"
handler "$event" < "$stdin" > "$body" || exit_code="$?"
rm -f "$event" "$stdin"
if [ "$exit_code" -eq 0 ]; then
# Send the response
local response
response="$(jq -cnMr \
jq --raw-input --raw-output --compact-output --slurp --monochrome-output \
--arg statusCode "$(cat "$_STATUS_CODE")" \
--argjson headers "$(cat "$_HEADERS")" \
--arg body "$(base64 --wrap=0 < "$body")" \
'{statusCode:$statusCode|tonumber, headers:$headers, encoding:"base64", body:$body}')"
'{statusCode:$statusCode|tonumber, headers:$headers, encoding:"base64", body:.|@base64}' < "$body" \
| _lambda_runtime_api "invocation/$request_id/response" -X POST -d @- > /dev/null
rm -f "$body" "$_HEADERS"
_lambda_runtime_api "invocation/$request_id/response" -X POST -d "$response"
else
local error
error='{"exitCode":'"$exit_code"'}'
_lambda_runtime_api "invocation/$request_id/error" -X POST -d "$error"
echo "\`handler\` function return code: $exit_code"
_lambda_runtime_api "invocation/$request_id/error" -X POST -d @- > /dev/null <<< '{"exitCode":'"$exit_code"'}'
fi
}
_lambda_runtime_body() {
if [ "$(jq -r '.body | type' < "$1")" = "string" ]; then
if [ "$(jq -r '.encoding' < "$1")" = "base64" ]; then
jq -r '.body' < "$1" | base64 -d
if [ "$(jq --raw-output '.body | type' < "$1")" = "string" ]; then
if [ "$(jq --raw-output '.encoding' < "$1")" = "base64" ]; then
jq --raw-output '.body' < "$1" | base64 -d
else
# assume plain-text body
jq -r '.body' < "$1"
jq --raw-output '.body' < "$1"
fi
fi
}

View File

@@ -49,14 +49,14 @@ class FileRef {
assert(url);
await semaToDownloadFromS3.acquire();
console.time(`downloading ${url}`);
// console.time(`downloading ${url}`);
try {
return await retry(
async () => {
const resp = await fetch(url);
if (!resp.ok) {
const error = new BailableError(
`${resp.status} ${resp.statusText}`,
`download: ${resp.status} ${resp.statusText} for ${url}`,
);
if (resp.status === 403) error.bail = true;
throw error;
@@ -66,7 +66,7 @@ class FileRef {
{ factor: 1, retries: 3 },
);
} finally {
console.timeEnd(`downloading ${url}`);
// console.timeEnd(`downloading ${url}`);
semaToDownloadFromS3.release();
}
}

View File

@@ -1,3 +1,5 @@
/* eslint-disable arrow-body-style,no-multi-assign,no-param-reassign */
const MemoryFileSystem = require('memory-fs');
const fs = require('fs');
const path = require('path');
@@ -14,70 +16,227 @@ const cachePath = spawnSync(yarnPath, ['cache', 'dir'])
spawnSync(yarnPath, ['cache', 'clean']);
const vfs = new MemoryFileSystem();
function isOutsideCachePath(filename) {
function isInsideCachePath(filename) {
const relative = path.relative(cachePath, filename);
return relative.startsWith('..');
return !relative.startsWith('..');
}
const saveCreateWriteStream = fs.createWriteStream;
fs.createWriteStream = (...args) => {
const filename = args[0];
if (isOutsideCachePath(filename)) {
return saveCreateWriteStream.call(fs, ...args);
}
function replaceFn(name, newFnFactory) {
const prevFn = fs[name];
fs[name] = newFnFactory(prevFn);
}
vfs.mkdirpSync(path.dirname(filename));
fs.writeFileSync(filename, Buffer.alloc(0));
const stream = vfs.createWriteStream(...args);
stream.on('finish', () => {
setTimeout(() => {
stream.emit('close');
});
});
return stream;
};
const saveReadFile = fs.readFile;
fs.readFile = (...args) => {
const filename = args[0];
if (isOutsideCachePath(filename)) {
return saveReadFile.call(fs, ...args);
}
const callback = args[args.length - 1];
return vfs.readFile(...args.slice(0, -1), (error, result) => {
if (error) {
saveReadFile.call(fs, ...args);
return;
replaceFn('createWriteStream', (prevFn) => {
return (...args) => {
const filename = args[0];
if (!isInsideCachePath(filename)) {
return prevFn.call(fs, ...args);
}
callback(error, result);
});
};
const stream = vfs.createWriteStream(...args);
const saveCopyFile = fs.copyFile;
fs.copyFile = (...args) => {
const src = args[0];
if (isOutsideCachePath(src)) {
return saveCopyFile.call(fs, ...args);
}
stream.on('finish', () => {
setTimeout(() => {
stream.emit('close');
});
});
const dest = args[1];
const callback = args[args.length - 1];
const buffer = vfs.readFileSync(src);
return fs.writeFile(dest, buffer, callback);
};
setTimeout(() => {
stream.emit('open');
});
const saveWriteFile = fs.writeFile;
fs.writeFile = (...args) => {
const filename = args[0];
if (isOutsideCachePath(filename)) {
return saveWriteFile.call(fs, ...args);
}
return stream;
};
});
return vfs.writeFile(...args);
};
replaceFn('readFile', (prevFn) => {
return (...args) => {
const filename = args[0];
if (!isInsideCachePath(filename)) {
return prevFn.call(fs, ...args);
}
const callback = args[args.length - 1];
return vfs.readFile(...args.slice(0, -1), (error, result) => {
if (error) {
return prevFn.call(fs, ...args);
}
return callback(error, result);
});
};
});
replaceFn('readdir', (prevFn) => {
return (...args) => {
const dirname = args[0];
if (!isInsideCachePath(dirname)) {
return prevFn.call(fs, ...args);
}
const callback = args[args.length - 1];
return prevFn.call(fs, dirname, (error, results) => {
if (error) {
results = [];
}
return vfs.readdir(dirname, (error2, results2) => {
if (error2) {
return callback(error2);
}
// eslint-disable-next-line no-restricted-syntax
for (const result2 of results2) {
if (!results.includes(result2)) {
results.push(result2);
}
}
return callback(error2, results);
});
});
};
});
replaceFn('stat', (prevFn) => {
return (...args) => {
const filename = args[0];
if (!isInsideCachePath(filename)) {
return prevFn.call(fs, ...args);
}
const callback = args[args.length - 1];
return vfs.stat(...args.slice(0, -1), (error, result) => {
if (error) {
return prevFn.call(fs, ...args);
}
result.atime = result.mtime = new Date();
return callback(error, result);
});
};
});
replaceFn('lstat', (prevFn) => {
return (...args) => {
const filename = args[0];
if (!isInsideCachePath(filename)) {
return prevFn.call(fs, ...args);
}
const callback = args[args.length - 1];
return vfs.stat(...args.slice(0, -1), (error, result) => {
if (error) {
return prevFn.call(fs, ...args);
}
result.atime = result.mtime = new Date();
return callback(error, result);
});
};
});
replaceFn('exists', (prevFn) => {
return (...args) => {
const filename = args[0];
if (!isInsideCachePath(filename)) {
return prevFn.call(fs, ...args);
}
const callback = args[args.length - 1];
return vfs.exists(...args.slice(0, -1), (result) => {
if (!result) {
return prevFn.call(fs, ...args);
}
return callback(result);
});
};
});
replaceFn('copyFile', (prevFn) => {
return (...args) => {
const src = args[0];
const dest = args[1];
const callback = args[args.length - 1];
if (isInsideCachePath(src) && !isInsideCachePath(dest)) {
const buffer = vfs.readFileSync(src);
return fs.writeFile(dest, buffer, callback);
}
if (!isInsideCachePath(src) && isInsideCachePath(dest)) {
const buffer = fs.readFileSync(src);
return vfs.writeFile(dest, buffer, callback);
}
return prevFn.call(fs, ...args);
};
});
replaceFn('writeFile', (prevFn) => {
return (...args) => {
const filename = args[0];
if (!isInsideCachePath(filename)) {
return prevFn.call(fs, ...args);
}
return vfs.writeFile(...args);
};
});
replaceFn('mkdir', (prevFn) => {
return (...args) => {
const dirname = args[0];
if (!isInsideCachePath(dirname)) {
return prevFn.call(fs, ...args);
}
const callback = args[args.length - 1];
return prevFn.call(fs, dirname, (error) => {
if (error) {
return callback(error);
}
return vfs.mkdirp(dirname, callback);
});
};
});
replaceFn('utimes', (prevFn) => {
return (...args) => {
const filename = args[0];
if (!isInsideCachePath(filename)) {
return prevFn.call(fs, ...args);
}
const callback = args[args.length - 1];
return setTimeout(callback, 0);
};
});
replaceFn('chmod', (prevFn) => {
return (...args) => {
const filename = args[0];
if (!isInsideCachePath(filename)) {
return prevFn.call(fs, ...args);
}
const callback = args[args.length - 1];
return setTimeout(callback, 0);
};
});
replaceFn('chown', (prevFn) => {
return (...args) => {
const filename = args[0];
if (!isInsideCachePath(filename)) {
return prevFn.call(fs, ...args);
}
const callback = args[args.length - 1];
return setTimeout(callback, 0);
};
});
require(yarnPath);

View File

@@ -1,8 +1,8 @@
const path = require('path');
const fs = require('fs-extra');
const dev = !process.env.AWS_EXECUTION_ENV;
const TMP_PATH = dev ? path.join(process.cwd(), 'tmp') : '/tmp';
const prod = process.env.AWS_EXECUTION_ENV || process.env.X_GOOGLE_CODE_LOCATION;
const TMP_PATH = prod ? '/tmp' : path.join(__dirname, 'tmp');
module.exports = async function getWritableDirectory() {
const name = Math.floor(Math.random() * 0x7fffffff).toString(16);

View File

@@ -3,6 +3,9 @@ const fs = require('fs-extra');
const path = require('path');
const { spawn } = require('child_process');
const prod = process.env.AWS_EXECUTION_ENV
|| process.env.X_GOOGLE_CODE_LOCATION;
function spawnAsync(command, args, cwd) {
return new Promise((resolve, reject) => {
const child = spawn(command, args, { stdio: 'inherit', cwd });
@@ -35,7 +38,7 @@ async function scanParentDirs(destPath, scriptName) {
// eslint-disable-next-line no-await-in-loop
const packageJson = JSON.parse(await fs.readFile(packageJsonPath));
hasScript = Boolean(
packageJson.scripts && packageJson.scripts[scriptName],
packageJson.scripts && scriptName && packageJson.scripts[scriptName],
);
// eslint-disable-next-line no-await-in-loop
hasPackageLockJson = await fs.exists(
@@ -52,7 +55,7 @@ async function scanParentDirs(destPath, scriptName) {
return { hasScript, hasPackageLockJson };
}
async function runNpmInstall(destPath, args = []) {
async function installDependencies(destPath, args = []) {
assert(path.isAbsolute(destPath));
let commandArgs = args;
@@ -63,7 +66,7 @@ async function runNpmInstall(destPath, args = []) {
commandArgs = args.filter(a => a !== '--prefer-offline');
await spawnAsync('npm', ['install'].concat(commandArgs), destPath);
await spawnAsync('npm', ['cache', 'clean', '--force'], destPath);
} else if (process.env.AWS_EXECUTION_ENV) {
} else if (prod) {
console.log('using memory-fs for yarn cache');
await spawnAsync(
'node',
@@ -99,6 +102,7 @@ async function runPackageJsonScript(destPath, scriptName) {
module.exports = {
runShellScript,
runNpmInstall,
installDependencies,
runNpmInstall: installDependencies,
runPackageJsonScript,
};

View File

@@ -1,4 +1,5 @@
const assert = require('assert');
const Sema = require('async-sema');
const { ZipFile } = require('yazl');
const streamToBuffer = require('./fs/stream-to-buffer.js');
@@ -14,6 +15,7 @@ class Lambda {
}
}
const sema = new Sema(10);
const mtime = new Date(1540000000000);
async function createLambda({
@@ -23,24 +25,33 @@ async function createLambda({
assert(typeof handler === 'string', '"handler" is not a string');
assert(typeof runtime === 'string', '"runtime" is not a string');
assert(typeof environment === 'object', '"environment" is not an object');
const zipFile = new ZipFile();
Object.keys(files)
.sort()
.forEach((name) => {
const file = files[name];
const stream = file.toStream();
zipFile.addReadStream(stream, name, { mode: file.mode, mtime });
await sema.acquire();
try {
const zipFile = new ZipFile();
const zipBuffer = await new Promise((resolve, reject) => {
Object.keys(files)
.sort()
.forEach((name) => {
const file = files[name];
const stream = file.toStream();
stream.on('error', reject);
zipFile.addReadStream(stream, name, { mode: file.mode, mtime });
});
zipFile.end();
streamToBuffer(zipFile.outputStream).then(resolve).catch(reject);
});
zipFile.end();
const zipBuffer = await streamToBuffer(zipFile.outputStream);
return new Lambda({
zipBuffer,
handler,
runtime,
environment,
});
return new Lambda({
zipBuffer,
handler,
runtime,
environment,
});
} finally {
sema.release();
}
}
module.exports = {

View File

@@ -1,6 +1,12 @@
{
"name": "@now/build-utils",
"version": "0.4.31",
"version": "0.4.33-canary.2",
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/zeit/now-builders.git",
"directory": "packages/now-build-utils"
},
"dependencies": {
"async-retry": "1.2.3",
"async-sema": "2.1.4",

View File

@@ -0,0 +1,5 @@
const cowsay = require('cowsay').say;
module.exports = (req, resp) => {
resp.end(cowsay({ text: 'cross-cow:RANDOMNESS_PLACEHOLDER' }));
};

View File

@@ -0,0 +1,5 @@
{
"dependencies": {
"lib": "../lib"
}
}

View File

@@ -0,0 +1,7 @@
{
"name": "lib",
"version": "0.0.1",
"dependencies": {
"cowsay": "*"
}
}

View File

@@ -0,0 +1,9 @@
{
"version": 2,
"builds": [
{ "src": "api/index.js", "use": "@now/node" }
],
"probes": [
{ "path": "/api/index.js", "mustContain": "cross-cow:RANDOMNESS_PLACEHOLDER" }
]
}

View File

@@ -1,10 +1,10 @@
const path = require('path');
const { mkdirp, copyFile } = require('fs-extra');
const glob = require('@now/build-utils/fs/glob');
const download = require('@now/build-utils/fs/download');
const { createLambda } = require('@now/build-utils/lambda');
const getWritableDirectory = require('@now/build-utils/fs/get-writable-directory');
const glob = require('@now/build-utils/fs/glob'); // eslint-disable-line import/no-extraneous-dependencies
const download = require('@now/build-utils/fs/download'); // eslint-disable-line import/no-extraneous-dependencies
const { createLambda } = require('@now/build-utils/lambda'); // eslint-disable-line import/no-extraneous-dependencies
const getWritableDirectory = require('@now/build-utils/fs/get-writable-directory'); // eslint-disable-line import/no-extraneous-dependencies
exports.analyze = ({ files, entrypoint }) => files[entrypoint].digest;

View File

@@ -1,6 +1,12 @@
{
"name": "@now/cgi",
"version": "0.0.14",
"version": "0.0.16-canary.0",
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/zeit/now-builders.git",
"directory": "packages/now-cgi"
},
"scripts": {
"test": "best -I test/*.js",
"prepublish": "./build.sh"
@@ -15,8 +21,5 @@
"devDependencies": {
"@zeit/best": "0.4.3",
"rmfr": "2.0.0"
},
"peerDependencies": {
"@now/build-utils": ">=0.0.1"
}
}

View File

@@ -2,7 +2,7 @@ const path = require('path');
const fetch = require('node-fetch');
const tar = require('tar');
const getWritableDirectory = require('@now/build-utils/fs/get-writable-directory.js');
const getWritableDirectory = require('@now/build-utils/fs/get-writable-directory.js'); // eslint-disable-line import/no-extraneous-dependencies
const url = 'https://dl.google.com/go/go1.11.1.linux-amd64.tar.gz';

View File

@@ -2,11 +2,11 @@ const path = require('path');
const { mkdirp, readFile, writeFile } = require('fs-extra');
const execa = require('execa');
const { createLambda } = require('@now/build-utils/lambda.js');
const getWritableDirectory = require('@now/build-utils/fs/get-writable-directory.js');
const download = require('@now/build-utils/fs/download.js');
const { createLambda } = require('@now/build-utils/lambda.js'); // eslint-disable-line import/no-extraneous-dependencies
const getWritableDirectory = require('@now/build-utils/fs/get-writable-directory.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 downloadGit = require('lambda-git');
const glob = require('@now/build-utils/fs/glob.js');
const glob = require('@now/build-utils/fs/glob.js'); // eslint-disable-line import/no-extraneous-dependencies
const downloadGoBin = require('./download-go-bin');
// creates a `$GOPATH` directory tree, as per

View File

@@ -1,6 +1,12 @@
{
"name": "@now/go",
"version": "0.2.11",
"version": "0.2.13-canary.0",
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/zeit/now-builders.git",
"directory": "packages/now-go"
},
"scripts": {
"test": "best -I test/*.js",
"prepublish": "./build.sh"
@@ -22,8 +28,5 @@
"devDependencies": {
"@zeit/best": "0.4.3",
"rmfr": "2.0.0"
},
"peerDependencies": {
"@now/build-utils": ">=0.0.1"
}
}

View File

@@ -1,4 +1,4 @@
const FileBlob = require('@now/build-utils/file-blob.js');
const FileBlob = require('@now/build-utils/file-blob.js'); // eslint-disable-line import/no-extraneous-dependencies
const { minify } = require('html-minifier');
const defaultOptions = {

View File

@@ -1,10 +1,13 @@
{
"name": "@now/html-minifier",
"version": "1.0.6",
"version": "1.0.8-canary.0",
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/zeit/now-builders.git",
"directory": "packages/now-html-minifier"
},
"dependencies": {
"html-minifier": "3.5.21"
},
"peerDependencies": {
"@now/build-utils": ">=0.0.1"
}
}

View File

@@ -1,5 +1,5 @@
const { Lambda } = require('@now/build-utils/lambda.js');
const streamToBuffer = require('@now/build-utils/fs/stream-to-buffer.js');
const { Lambda } = require('@now/build-utils/lambda.js'); // eslint-disable-line import/no-extraneous-dependencies
const streamToBuffer = require('@now/build-utils/fs/stream-to-buffer.js'); // eslint-disable-line import/no-extraneous-dependencies
exports.build = async ({ files, entrypoint, config }) => {
if (!files[entrypoint]) throw new Error('Entrypoint not found in files');

View File

@@ -1,8 +1,11 @@
{
"name": "@now/lambda",
"version": "0.4.8",
"peerDependencies": {
"@now/build-utils": ">=0.0.1"
"version": "0.4.10-canary.0",
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/zeit/now-builders.git",
"directory": "packages/now-lambda"
},
"scripts": {
"test": "jest"

View File

@@ -1,4 +1,4 @@
const FileBlob = require('@now/build-utils/file-blob.js');
const FileBlob = require('@now/build-utils/file-blob.js'); // eslint-disable-line import/no-extraneous-dependencies
const unified = require('unified');
const unifiedStream = require('unified-stream');
const markdown = require('remark-parse');

View File

@@ -1,6 +1,12 @@
{
"name": "@now/md",
"version": "0.4.8",
"version": "0.4.10-canary.0",
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/zeit/now-builders.git",
"directory": "packages/now-md"
},
"dependencies": {
"rehype-document": "^2.2.0",
"rehype-format": "^2.3.0",
@@ -10,9 +16,6 @@
"unified": "^7.0.0",
"unified-stream": "^1.0.2"
},
"peerDependencies": {
"@now/build-utils": ">=0.0.1"
},
"scripts": {
"test": "jest"
}

View File

@@ -1,12 +1,10 @@
const download = require('@now/build-utils/fs/download.js');
const download = require('@now/build-utils/fs/download.js'); // eslint-disable-line import/no-extraneous-dependencies
const fs = require('fs');
const { promisify } = require('util');
const getWritableDirectory = require('@now/build-utils/fs/get-writable-directory.js');
const glob = require('@now/build-utils/fs/glob.js');
const getWritableDirectory = require('@now/build-utils/fs/get-writable-directory.js'); // eslint-disable-line import/no-extraneous-dependencies
const glob = require('@now/build-utils/fs/glob.js'); // eslint-disable-line import/no-extraneous-dependencies
const path = require('path');
const { runNpmInstall } = require('@now/build-utils/fs/run-user-scripts.js');
exports.analyze = ({ files, entrypoint }) => files[entrypoint].digest;
const { runNpmInstall } = require('@now/build-utils/fs/run-user-scripts.js'); // eslint-disable-line import/no-extraneous-dependencies
const writeFile = promisify(fs.writeFile);
@@ -17,7 +15,7 @@ exports.build = async ({ files, entrypoint, workPath }) => {
const packageJson = { dependencies: { 'mdx-deck': '1.7.15' } };
const packageJsonPath = path.join(workPath, 'package.json');
await writeFile(packageJsonPath, JSON.stringify(packageJson));
console.log('running npm install...');
console.log('installing dependencies...');
process.env.PUPPETEER_SKIP_CHROMIUM_DOWNLOAD = '1'; // TODO opts argument for runNpmInstall
await runNpmInstall(path.dirname(packageJsonPath), [
'--prod',

View File

@@ -1,8 +1,11 @@
{
"name": "@now/mdx-deck",
"version": "0.4.17",
"peerDependencies": {
"@now/build-utils": ">=0.0.1"
"version": "0.4.19-canary.0",
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/zeit/now-builders.git",
"directory": "packages/now-mdx-deck"
},
"scripts": {
"test": "jest"

View File

@@ -1,20 +1,21 @@
const { createLambda } = require('@now/build-utils/lambda.js');
const download = require('@now/build-utils/fs/download.js');
const FileFsRef = require('@now/build-utils/file-fs-ref.js');
const FileBlob = require('@now/build-utils/file-blob');
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 FileFsRef = require('@now/build-utils/file-fs-ref.js'); // eslint-disable-line import/no-extraneous-dependencies
const FileBlob = require('@now/build-utils/file-blob'); // eslint-disable-line import/no-extraneous-dependencies
const path = require('path');
const { readFile, writeFile, unlink } = require('fs.promised');
const {
runNpmInstall,
runPackageJsonScript,
} = require('@now/build-utils/fs/run-user-scripts.js');
const glob = require('@now/build-utils/fs/glob.js');
} = require('@now/build-utils/fs/run-user-scripts.js'); // eslint-disable-line import/no-extraneous-dependencies
const glob = require('@now/build-utils/fs/glob.js'); // eslint-disable-line import/no-extraneous-dependencies
const semver = require('semver');
const nextLegacyVersions = require('./legacy-versions');
const {
excludeFiles,
validateEntrypoint,
includeOnlyEntryDirectory,
moveEntryDirectoryToRoot,
excludeLockFiles,
normalizePackageJson,
excludeStaticDirectory,
onlyStaticDirectory,
@@ -88,113 +89,227 @@ exports.build = async ({ files, workPath, entrypoint }) => {
filesOnlyEntryDirectory,
entryDirectory,
);
const filesWithoutLockfiles = excludeLockFiles(filesWithEntryDirectoryRoot);
const filesWithoutStaticDirectory = excludeStaticDirectory(
filesWithoutLockfiles,
filesWithEntryDirectoryRoot,
);
const downloadedFiles = await download(filesWithoutStaticDirectory, workPath);
console.log('normalizing package.json');
const packageJson = normalizePackageJson(
await readPackageJson(downloadedFiles),
);
console.log('normalized package.json result: ', packageJson);
await writePackageJson(workPath, packageJson);
const pkg = await readPackageJson(downloadedFiles);
let nextVersion;
if (pkg.dependencies && pkg.dependencies.next) {
nextVersion = pkg.dependencies.next;
} else if (pkg.devDependencies && pkg.devDependencies.next) {
nextVersion = pkg.devDependencies.next;
}
if (!nextVersion) {
throw new Error(
'No Next.js version could be detected in "package.json". Make sure `"next"` is installed in "dependencies" or "devDependencies"',
);
}
const isLegacy = (() => {
// If version is using the dist-tag instead of a version range
if (nextVersion === 'canary' || nextVersion === 'latest') {
return false;
}
// If the version is an exact match with the legacy versions
if (nextLegacyVersions.indexOf(nextVersion) !== -1) {
return true;
}
const maxSatisfying = semver.maxSatisfying(nextLegacyVersions, nextVersion);
// When the version can't be matched with legacy versions, so it must be a newer version
if (maxSatisfying === null) {
return false;
}
return true;
})();
console.log(`MODE: ${isLegacy ? 'legacy' : 'serverless'}`);
if (isLegacy) {
try {
await unlink(path.join(workPath, 'yarn.lock'));
} catch (err) {
console.log('no yarn.lock removed');
}
try {
await unlink(path.join(workPath, 'package-lock.json'));
} catch (err) {
console.log('no package-lock.json removed');
}
console.warn(
"WARNING: your application is being deployed in @now/next's legacy mode. http://err.sh/zeit/now-builders/now-next-legacy-mode",
);
console.log('normalizing package.json');
const packageJson = normalizePackageJson(pkg);
console.log('normalized package.json result: ', packageJson);
await writePackageJson(workPath, packageJson);
} else if (!pkg.scripts || !pkg.scripts['now-build']) {
console.warn(
'WARNING: "now-build" script not found. Adding \'"now-build": "next build"\' to "package.json" automatically',
);
pkg.scripts = {
'now-build': 'next build',
...(pkg.scripts || {}),
};
console.log('normalized package.json result: ', pkg);
await writePackageJson(workPath, pkg);
}
if (process.env.NPM_AUTH_TOKEN) {
console.log('found NPM_AUTH_TOKEN in environment, creating .npmrc');
await writeNpmRc(workPath, process.env.NPM_AUTH_TOKEN);
}
console.log('running npm install...');
console.log('installing dependencies...');
await runNpmInstall(workPath, ['--prefer-offline']);
console.log('running user script...');
await runPackageJsonScript(workPath, 'now-build');
console.log('running npm install --production...');
await runNpmInstall(workPath, ['--prefer-offline', '--production']);
if (isLegacy) {
console.log('running npm install --production...');
await runNpmInstall(workPath, ['--prefer-offline', '--production']);
}
if (process.env.NPM_AUTH_TOKEN) {
await unlink(path.join(workPath, '.npmrc'));
}
const filesAfterBuild = await glob('**', workPath);
console.log('preparing lambda files...');
let buildId;
try {
buildId = await readFile(path.join(workPath, '.next', 'BUILD_ID'), 'utf8');
} catch (err) {
console.error(
'BUILD_ID not found in ".next". The "package.json" "build" script did not run "next build"',
);
throw new Error('Missing BUILD_ID');
}
const dotNextRootFiles = await glob('.next/*', workPath);
const dotNextServerRootFiles = await glob('.next/server/*', workPath);
const nodeModules = excludeFiles(
await glob('node_modules/**', workPath),
file => file.startsWith('node_modules/.cache'),
);
const launcherFiles = {
'now__bridge.js': new FileFsRef({ fsPath: require('@now/node-bridge') }),
};
const nextFiles = {
...nodeModules,
...dotNextRootFiles,
...dotNextServerRootFiles,
...launcherFiles,
};
if (filesAfterBuild['next.config.js']) {
nextFiles['next.config.js'] = filesAfterBuild['next.config.js'];
}
const pages = await glob(
'**/*.js',
path.join(workPath, '.next', 'server', 'static', buildId, 'pages'),
);
const launcherPath = path.join(__dirname, 'launcher.js');
const launcherData = await readFile(launcherPath, 'utf8');
const lambdas = {};
await Promise.all(
Object.keys(pages).map(async (page) => {
// These default pages don't have to be handled as they'd always 404
if (['_app.js', '_error.js', '_document.js'].includes(page)) {
return;
}
const pathname = page.replace(/\.js$/, '');
const launcher = launcherData.replace(
'PATHNAME_PLACEHOLDER',
`/${pathname.replace(/(^|\/)index$/, '')}`,
if (isLegacy) {
const filesAfterBuild = await glob('**', workPath);
console.log('preparing lambda files...');
let buildId;
try {
buildId = await readFile(
path.join(workPath, '.next', 'BUILD_ID'),
'utf8',
);
} catch (err) {
console.error(
'BUILD_ID not found in ".next". The "package.json" "build" script did not run "next build"',
);
throw new Error('Missing BUILD_ID');
}
const dotNextRootFiles = await glob('.next/*', workPath);
const dotNextServerRootFiles = await glob('.next/server/*', workPath);
const nodeModules = excludeFiles(
await glob('node_modules/**', workPath),
file => file.startsWith('node_modules/.cache'),
);
const launcherFiles = {
'now__bridge.js': new FileFsRef({ fsPath: require('@now/node-bridge') }),
};
const nextFiles = {
...nodeModules,
...dotNextRootFiles,
...dotNextServerRootFiles,
...launcherFiles,
};
if (filesAfterBuild['next.config.js']) {
nextFiles['next.config.js'] = filesAfterBuild['next.config.js'];
}
const pages = await glob(
'**/*.js',
path.join(workPath, '.next', 'server', 'static', buildId, 'pages'),
);
const launcherPath = path.join(__dirname, 'legacy-launcher.js');
const launcherData = await readFile(launcherPath, 'utf8');
const pageFiles = {
[`.next/server/static/${buildId}/pages/_document.js`]: filesAfterBuild[
`.next/server/static/${buildId}/pages/_document.js`
],
[`.next/server/static/${buildId}/pages/_app.js`]: filesAfterBuild[
`.next/server/static/${buildId}/pages/_app.js`
],
[`.next/server/static/${buildId}/pages/_error.js`]: filesAfterBuild[
`.next/server/static/${buildId}/pages/_error.js`
],
[`.next/server/static/${buildId}/pages/${page}`]: filesAfterBuild[
`.next/server/static/${buildId}/pages/${page}`
],
};
await Promise.all(
Object.keys(pages).map(async (page) => {
// These default pages don't have to be handled as they'd always 404
if (['_app.js', '_error.js', '_document.js'].includes(page)) {
return;
}
console.log(`Creating lambda for page: "${page}"...`);
lambdas[path.join(entryDirectory, pathname)] = await createLambda({
files: {
...nextFiles,
...pageFiles,
'now__launcher.js': new FileBlob({ data: launcher }),
},
handler: 'now__launcher.launcher',
runtime: 'nodejs8.10',
});
console.log(`Created lambda for page: "${page}"`);
}),
);
const pathname = page.replace(/\.js$/, '');
const launcher = launcherData.replace(
'PATHNAME_PLACEHOLDER',
`/${pathname.replace(/(^|\/)index$/, '')}`,
);
const pageFiles = {
[`.next/server/static/${buildId}/pages/_document.js`]: filesAfterBuild[
`.next/server/static/${buildId}/pages/_document.js`
],
[`.next/server/static/${buildId}/pages/_app.js`]: filesAfterBuild[
`.next/server/static/${buildId}/pages/_app.js`
],
[`.next/server/static/${buildId}/pages/_error.js`]: filesAfterBuild[
`.next/server/static/${buildId}/pages/_error.js`
],
[`.next/server/static/${buildId}/pages/${page}`]: filesAfterBuild[
`.next/server/static/${buildId}/pages/${page}`
],
};
console.log(`Creating lambda for page: "${page}"...`);
lambdas[path.join(entryDirectory, pathname)] = await createLambda({
files: {
...nextFiles,
...pageFiles,
'now__launcher.js': new FileBlob({ data: launcher }),
},
handler: 'now__launcher.launcher',
runtime: 'nodejs8.10',
});
console.log(`Created lambda for page: "${page}"`);
}),
);
} else {
console.log('preparing lambda files...');
const launcherFiles = {
'now__bridge.js': new FileFsRef({ fsPath: require('@now/node-bridge') }),
'now__launcher.js': new FileFsRef({
fsPath: path.join(__dirname, 'launcher.js'),
}),
};
const pages = await glob(
'**/*.js',
path.join(workPath, '.next', 'serverless', 'pages'),
);
const pageKeys = Object.keys(pages);
if (pageKeys.length === 0) {
throw new Error(
'No serverless pages were built. https://err.sh/zeit/now-builders/now-next-no-serverless-pages-built',
);
}
await Promise.all(
pageKeys.map(async (page) => {
// These default pages don't have to be handled as they'd always 404
if (['_app.js', '_error.js', '_document.js'].includes(page)) {
return;
}
const pathname = page.replace(/\.js$/, '');
console.log(`Creating lambda for page: "${page}"...`);
lambdas[path.join(entryDirectory, pathname)] = await createLambda({
files: {
...launcherFiles,
'page.js': pages[page],
},
handler: 'now__launcher.launcher',
runtime: 'nodejs8.10',
});
console.log(`Created lambda for page: "${page}"`);
}),
);
}
const nextStaticFiles = await glob(
'**',
@@ -208,7 +323,7 @@ exports.build = async ({ files, workPath, entrypoint }) => {
{},
);
const nextStaticDirectory = onlyStaticDirectory(filesWithoutLockfiles);
const nextStaticDirectory = onlyStaticDirectory(filesWithEntryDirectoryRoot);
const staticDirectoryFiles = Object.keys(nextStaticDirectory).reduce(
(mappedFiles, file) => ({
...mappedFiles,
@@ -219,41 +334,3 @@ exports.build = async ({ files, workPath, entrypoint }) => {
return { ...lambdas, ...staticFiles, ...staticDirectoryFiles };
};
exports.prepareCache = async ({
files, entrypoint, cachePath, workPath,
}) => {
console.log('downloading user files...');
const entryDirectory = path.dirname(entrypoint);
const filesOnlyEntryDirectory = includeOnlyEntryDirectory(
files,
entryDirectory,
);
const filesWithEntryDirectoryRoot = moveEntryDirectoryToRoot(
filesOnlyEntryDirectory,
entryDirectory,
);
const filesWithoutLockfiles = excludeLockFiles(filesWithEntryDirectoryRoot);
const filesWithoutStaticDirectory = excludeStaticDirectory(
filesWithoutLockfiles,
);
await download(filesWithoutStaticDirectory, workPath);
await download(await glob('.next/**', workPath), cachePath);
await download(await glob('node_modules/**', workPath), cachePath);
console.log('.next folder contents', await glob('.next/**', cachePath));
console.log(
'.cache folder contents',
await glob('node_modules/.cache/**', cachePath),
);
console.log('running npm install...');
await runNpmInstall(cachePath);
return {
...(await glob('.next/records.json', cachePath)),
...(await glob('.next/server/records.json', cachePath)),
...(await glob('node_modules/**', cachePath)),
...(await glob('yarn.lock', cachePath)),
};
};

View File

@@ -1,19 +1,13 @@
process.env.NODE_ENV = 'production';
const { Server } = require('http');
const next = require('next-server');
const url = require('url');
const { Bridge } = require('./now__bridge.js');
const page = require('./page.js');
const bridge = new Bridge();
bridge.port = 3000;
process.env.NODE_ENV = 'production';
const app = next({});
const server = new Server((req, res) => {
const parsedUrl = url.parse(req.url, true);
app.render(req, res, 'PATHNAME_PLACEHOLDER', parsedUrl.query, parsedUrl);
});
const server = new Server(page.render);
server.listen(bridge.port);
exports.launcher = bridge.launcher;

View File

@@ -0,0 +1,19 @@
const { Server } = require('http');
const next = require('next-server');
const url = require('url');
const { Bridge } = require('./now__bridge.js');
const bridge = new Bridge();
bridge.port = 3000;
process.env.NODE_ENV = 'production';
const app = next({});
const server = new Server((req, res) => {
const parsedUrl = url.parse(req.url, true);
app.render(req, res, 'PATHNAME_PLACEHOLDER', parsedUrl.query, parsedUrl);
});
server.listen(bridge.port);
exports.launcher = bridge.launcher;

View File

@@ -0,0 +1,336 @@
module.exports = [
'0.1.0',
'0.1.1',
'0.2.0',
'0.2.1',
'0.2.2',
'0.2.3',
'0.2.4',
'0.2.5',
'0.2.6',
'0.2.7',
'0.2.8',
'0.2.9',
'0.2.10',
'0.2.11',
'0.2.12',
'0.2.13',
'0.2.14',
'0.3.0',
'0.3.1',
'0.3.2',
'0.3.3',
'0.4.0',
'0.4.1',
'0.9.9',
'0.9.10',
'0.9.11',
'1.0.0',
'1.0.1',
'1.0.2',
'1.1.0',
'1.1.1',
'1.1.2',
'1.2.0',
'1.2.1',
'1.2.2',
'1.2.3',
'2.0.0-beta.0',
'2.0.0-beta.1',
'2.0.0-beta.2',
'2.0.0-beta.3',
'2.0.0-beta.4',
'2.0.0-beta.5',
'2.0.0-beta.6',
'2.0.0-beta.7',
'2.0.0-beta.8',
'2.0.0-beta.9',
'2.0.0-beta.10',
'2.0.0-beta.11',
'2.0.0-beta.12',
'2.0.0-beta.13',
'2.0.0-beta.14',
'2.0.0-beta.15',
'2.0.0-beta.16',
'2.0.0-beta.17',
'2.0.0-beta.18',
'2.0.0-beta.19',
'2.0.0-beta.20',
'2.0.0-beta.21',
'2.0.0-beta.22',
'2.0.0-beta.23',
'2.0.0-beta.24',
'2.0.0-beta.25',
'2.0.0-beta.26',
'2.0.0-beta.27',
'2.0.0-beta.28',
'2.0.0-beta.29',
'2.0.0-beta.30',
'2.0.0-beta.31',
'2.0.0-beta.32',
'2.0.0-beta.33',
'2.0.0-beta.34',
'2.0.0-beta.35',
'2.0.0-beta.36',
'2.0.0-beta.37',
'2.0.0-beta.38',
'2.0.0-beta.39',
'2.0.0-beta.40',
'2.0.0-beta.41',
'2.0.0-beta.42',
'2.0.0',
'2.0.1',
'2.1.0',
'2.1.1',
'2.2.0',
'2.3.0-alpha1',
'2.3.0',
'2.3.1',
'2.4.0',
'2.4.1',
'2.4.2',
'2.4.3',
'2.4.4',
'2.4.5',
'2.4.6',
'2.4.7',
'2.4.8',
'2.4.9',
'3.0.0-beta1',
'3.0.0-beta10',
'3.0.0-beta11',
'3.0.0-beta12',
'3.0.0-beta13',
'3.0.0-beta14',
'3.0.0-beta15',
'3.0.0-beta16',
'3.0.0-beta2',
'3.0.0-beta3',
'3.0.0-beta4',
'3.0.0-beta5',
'3.0.0-beta6',
'3.0.0-beta7',
'3.0.0-beta8',
'3.0.0-beta9',
'3.0.1-beta.1',
'3.0.1-beta.2',
'3.0.1-beta.3',
'3.0.1-beta.4',
'3.0.1-beta.5',
'3.0.1-beta.6',
'3.0.1-beta.7',
'3.0.1-beta.8',
'3.0.1-beta.9',
'3.0.1-beta.10',
'3.0.1-beta.11',
'3.0.1-beta.12',
'3.0.1-beta.13',
'3.0.1-beta.14',
'3.0.1-beta.15',
'3.0.1-beta.16',
'3.0.1-beta.17',
'3.0.1-beta.18',
'3.0.1-beta.19',
'3.0.1-beta.20',
'3.0.1-beta.21',
'3.0.1',
'3.0.2',
'3.0.3',
'3.0.4',
'3.0.5',
'3.0.6',
'3.1.0',
'3.2.0',
'3.2.1',
'3.2.2',
'3.2.3',
'4.0.0-beta.1',
'4.0.0-beta.2',
'4.0.0-beta.3',
'4.0.0-beta.4',
'4.0.0-beta.5',
'4.0.0-beta.6',
'4.0.0',
'4.0.1',
'4.0.2',
'4.0.3',
'4.0.4',
'4.0.5',
'4.1.0',
'4.1.1',
'4.1.2',
'4.1.3',
'4.1.4-canary.1',
'4.1.4-canary.2',
'4.1.4',
'4.2.0-canary.1',
'4.2.0-zones.2',
'4.2.0',
'4.2.1',
'4.2.2',
'4.2.3',
'4.3.0-canary.1',
'4.3.0-universal-alpha.1',
'4.3.0-universal-alpha.2',
'4.3.0-universal-alpha.3',
'4.3.0-universal-alpha.4',
'4.3.0-zones.1',
'4.4.0-canary.2',
'4.4.0-canary.3',
'5.0.0-universal-alpha.1',
'5.0.0-universal-alpha.2',
'5.0.0-universal-alpha.3',
'5.0.0-universal-alpha.4',
'5.0.0-universal-alpha.5',
'5.0.0-universal-alpha.6',
'5.0.0-universal-alpha.7',
'5.0.0-universal-alpha.8',
'5.0.0-universal-alpha.9',
'5.0.0-universal-alpha.10',
'5.0.0-universal-alpha.11',
'5.0.0-universal-alpha.12',
'5.0.0-universal-alpha.13',
'5.0.0-universal-alpha.14',
'5.0.0-universal-alpha.15',
'5.0.0-universal-alpha.16',
'5.0.0-universal-alpha.17',
'5.0.0-universal-alpha.18',
'5.0.0-universal-alpha.19',
'5.0.0-universal-alpha.20',
'5.0.0-universal-alpha.21',
'5.0.0-universal-alpha.22',
'5.0.0-universal-alpha.23',
'5.0.0-zones.1',
'5.0.0',
'5.0.1-canary.1',
'5.0.1-canary.2',
'5.0.1-canary.3',
'5.0.1-canary.4',
'5.0.1-canary.5',
'5.0.1-canary.6',
'5.0.1-canary.7',
'5.0.1-canary.8',
'5.0.1-canary.9',
'5.0.1-canary.10',
'5.0.1-canary.11',
'5.0.1-canary.12',
'5.0.1-canary.13',
'5.0.1-canary.14',
'5.0.1-canary.15',
'5.0.1-canary.16',
'5.0.1-canary.17',
'5.1.0',
'6.0.0-canary.1',
'6.0.0-canary.2',
'6.0.0-canary.3',
'6.0.0-canary.4',
'6.0.0-canary.5',
'6.0.0-canary.6',
'6.0.0-canary.7',
'6.0.0',
'6.0.1-canary.0',
'6.0.1-canary.1',
'6.0.1-canary.2',
'6.0.1',
'6.0.2-canary.0',
'6.0.2',
'6.0.3-canary.0',
'6.0.3-canary.1',
'6.0.3',
'6.0.4-canary.0',
'6.0.4-canary.1',
'6.0.4-canary.2',
'6.0.4-canary.3',
'6.0.4-canary.4',
'6.0.4-canary.5',
'6.0.4-canary.6',
'6.0.4-canary.7',
'6.0.4-canary.8',
'6.0.4-canary.9',
'6.1.0-canary.0',
'6.1.0',
'6.1.1-canary.0',
'6.1.1-canary.1',
'6.1.1-canary.2',
'6.1.1-canary.3',
'6.1.1-canary.4',
'6.1.1-canary.5',
'6.1.1',
'6.1.2',
'7.0.0-canary.0',
'7.0.0-canary.1',
'7.0.0-canary.2',
'7.0.0-canary.3',
'7.0.0-canary.4',
'7.0.0-canary.5',
'7.0.0-canary.6',
'7.0.0-canary.7',
'7.0.0-canary.8',
'7.0.0-canary.9',
'7.0.0-canary.10',
'7.0.0-canary.11',
'7.0.0-canary.12',
'7.0.0-canary.13',
'7.0.0-canary.14',
'7.0.0-canary.15',
'7.0.0-canary.16',
'7.0.0-canary.18',
'7.0.0-canary.19',
'7.0.0-canary.20',
'7.0.0',
'7.0.1-canary.0',
'7.0.1-canary.1',
'7.0.1-canary.2',
'7.0.1-canary.3',
'7.0.1-canary.4',
'7.0.1-canary.5',
'7.0.1-canary.6',
'7.0.1',
'7.0.2-alpha.1',
'7.0.2-alpha.3',
'7.0.2-canary.5',
'7.0.2-canary.6',
'7.0.2-canary.7',
'7.0.2-canary.8',
'7.0.2-canary.9',
'7.0.2-canary.10',
'7.0.2-canary.11',
'7.0.2-canary.12',
'7.0.2-canary.13',
'7.0.2-canary.14',
'7.0.2-canary.15',
'7.0.2-canary.16',
'7.0.2-canary.17',
'7.0.2-canary.18',
'7.0.2-canary.19',
'7.0.2-canary.20',
'7.0.2-canary.21',
'7.0.2-canary.22',
'7.0.2-canary.23',
'7.0.2-canary.24',
'7.0.2-canary.25',
'7.0.2-canary.26',
'7.0.2-canary.27',
'7.0.2-canary.28',
'7.0.2-canary.29',
'7.0.2-canary.31',
'7.0.2-canary.33',
'7.0.2-canary.34',
'7.0.2-canary.35',
'7.0.2-canary.36',
'7.0.2-canary.37',
'7.0.2-canary.38',
'7.0.2-canary.39',
'7.0.2-canary.40',
'7.0.2-canary.41',
'7.0.2-canary.42',
'7.0.2-canary.43',
'7.0.2-canary.44',
'7.0.2-canary.45',
'7.0.2-canary.46',
'7.0.2-canary.47',
'7.0.2-canary.48',
'7.0.2-canary.49',
'7.0.2-canary.50',
'7.0.2',
];

View File

@@ -1,12 +1,16 @@
{
"name": "@now/next",
"version": "0.0.81",
"version": "0.0.85-canary.1",
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/zeit/now-builders.git",
"directory": "packages/now-next"
},
"dependencies": {
"@now/node-bridge": "0.1.4",
"execa": "^1.0.0",
"fs.promised": "^3.0.0"
},
"peerDependencies": {
"@now/build-utils": ">=0.0.1"
"fs.promised": "^3.0.0",
"semver": "^5.6.0"
}
}

View File

@@ -1,4 +1,4 @@
const rename = require('@now/build-utils/fs/rename.js');
const rename = require('@now/build-utils/fs/rename.js'); // eslint-disable-line import/no-extraneous-dependencies
/** @typedef { import('@now/build-utils/file-ref') } FileRef */
/** @typedef { import('@now/build-utils/file-fs-ref') } FileFsRef */
@@ -153,18 +153,18 @@ function normalizePackageJson(defaultPackageJson = {}) {
'react-dom': 'latest',
...dependencies, // override react if user provided it
// next-server is forced to canary
'next-server': 'canary',
'next-server': 'v7.0.2-canary.49',
},
devDependencies: {
...devDependencies,
// next is forced to canary
next: 'canary',
next: 'v7.0.2-canary.49',
// next-server is a dependency here
'next-server': undefined,
},
scripts: {
...defaultPackageJson.scripts,
'now-build': 'next build --lambdas',
'now-build': 'NODE_OPTIONS=--max_old_space_size=3000 next build --lambdas',
},
};
}

View File

@@ -1,7 +1,10 @@
const http = require('http');
function normalizeEvent(event) {
let isApiGateway = true;
if (event.Action === 'Invoke') {
isApiGateway = false;
const invokeEvent = JSON.parse(event.body);
const {
@@ -21,10 +24,7 @@ function normalizeEvent(event) {
}
return {
method,
path,
headers,
body,
isApiGateway, method, path, headers, body,
};
}
@@ -33,10 +33,7 @@ function normalizeEvent(event) {
} = event;
return {
method,
path,
headers,
body,
isApiGateway, method, path, headers, body,
};
}
@@ -58,7 +55,7 @@ class Bridge {
}
const {
method, path, headers, body,
isApiGateway, method, path, headers, body,
} = normalizeEvent(event);
const opts = {
@@ -73,20 +70,35 @@ class Bridge {
const response = res;
const respBodyChunks = [];
response.on('data', chunk => respBodyChunks.push(Buffer.from(chunk)));
response.on('error', error => reject(error));
response.on('error', reject);
response.on('end', () => {
const bodyBuffer = Buffer.concat(respBodyChunks);
delete response.headers.connection;
delete response.headers['content-length'];
if (isApiGateway) {
delete response.headers['content-length'];
} else
if (response.headers['content-length']) {
response.headers['content-length'] = bodyBuffer.length;
}
resolve({
statusCode: response.statusCode,
headers: response.headers,
body: Buffer.concat(respBodyChunks).toString('base64'),
body: bodyBuffer.toString('base64'),
encoding: 'base64',
});
});
});
req.on('error', (error) => {
setTimeout(() => {
// this lets express print the true error of why the connection was closed.
// it is probably 'Cannot set headers after they are sent to the client'
reject(error);
}, 2);
});
if (body) req.write(body);
req.end();
});

View File

@@ -1,7 +1,10 @@
{
"name": "@now/node-bridge",
"version": "0.1.9",
"peerDependencies": {
"@now/build-utils": ">=0.0.1"
"version": "0.1.11-canary.0",
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/zeit/now-builders.git",
"directory": "packages/now-node-bridge"
}
}

View File

@@ -1,15 +1,15 @@
const { createLambda } = require('@now/build-utils/lambda.js');
const download = require('@now/build-utils/fs/download.js');
const FileBlob = require('@now/build-utils/file-blob.js');
const FileFsRef = require('@now/build-utils/file-fs-ref.js');
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 glob = require('@now/build-utils/fs/glob.js');
const glob = require('@now/build-utils/fs/glob.js'); // eslint-disable-line import/no-extraneous-dependencies
const path = require('path');
const rename = require('@now/build-utils/fs/rename.js');
const rename = require('@now/build-utils/fs/rename.js'); // eslint-disable-line import/no-extraneous-dependencies
const {
runNpmInstall,
runPackageJsonScript,
} = require('@now/build-utils/fs/run-user-scripts.js');
} = require('@now/build-utils/fs/run-user-scripts.js'); // eslint-disable-line import/no-extraneous-dependencies
/** @typedef { import('@now/build-utils/file-ref') } FileRef */
/** @typedef {{[filePath: string]: FileRef}} Files */
@@ -37,7 +37,7 @@ async function downloadInstallAndBundle(
console.log('downloading user files...');
const downloadedFiles = await download(files, userPath);
console.log('running npm install for user...');
console.log("installing dependencies for user's code...");
const entrypointFsDirname = path.join(userPath, path.dirname(entrypoint));
await runNpmInstall(entrypointFsDirname, npmArguments);
@@ -46,8 +46,9 @@ async function downloadInstallAndBundle(
{
'package.json': new FileBlob({
data: JSON.stringify({
license: 'UNLICENSED',
dependencies: {
'@zeit/ncc': '0.4.1',
'@zeit/ncc': '0.13.2',
},
}),
}),
@@ -55,7 +56,7 @@ async function downloadInstallAndBundle(
nccPath,
);
console.log('running npm install for ncc...');
console.log('installing dependencies for ncc...');
await runNpmInstall(nccPath, npmArguments);
return [downloadedFiles, userPath, nccPath, entrypointFsDirname];
}
@@ -71,7 +72,8 @@ async function compile(workNccPath, downloadedFiles, entrypoint) {
preparedFiles[path.join('user', entrypoint)] = blob;
// eslint-disable-next-line no-restricted-syntax
for (const assetName of Object.keys(assets)) {
const blob2 = new FileBlob({ data: assets[assetName] });
const { source: data, permissions: mode } = assets[assetName];
const blob2 = new FileBlob({ data, mode });
preparedFiles[
path.join('user', path.dirname(entrypoint), assetName)
] = blob2;

View File

@@ -12,7 +12,10 @@ Server.prototype.listen = function listen(...args) {
};
try {
process.env.NODE_ENV = 'production';
if (!process.env.NODE_ENV) {
process.env.NODE_ENV = 'production';
}
// PLACEHOLDER
} catch (error) {
console.error(error);

View File

@@ -1,12 +1,15 @@
{
"name": "@now/node-server",
"version": "0.4.25",
"dependencies": {
"@now/node-bridge": "^0.1.9",
"fs-extra": "7.0.1"
"version": "0.4.27-canary.2",
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/zeit/now-builders.git",
"directory": "packages/now-node-server"
},
"peerDependencies": {
"@now/build-utils": ">=0.0.1"
"dependencies": {
"@now/node-bridge": "^0.1.11-canary.0",
"fs-extra": "7.0.1"
},
"scripts": {
"test": "jest"

View File

@@ -1,4 +1,4 @@
const cowsay = require('cowsay/build/cowsay.umd.js').say;
const cowsay = require('cowsay').say;
const http = require('http');
// test that process.env is not replaced by webpack

View File

@@ -1,5 +1,5 @@
{
"dependencies": {
"cowsay": "^1.3.1"
"cowsay": "1.3.1"
}
}

View File

@@ -1,4 +1,4 @@
const yodasay = require('yodasay/build/yodasay.umd.js').say;
const yodasay = require('yodasay').say;
const http = require('http');
// test that process.env is not replaced by webpack

View File

@@ -1,5 +1,5 @@
{
"dependencies": {
"yodasay": "^1.1.6"
"yodasay": "1.1.9"
}
}

View File

@@ -0,0 +1,7 @@
const http = require('http');
const server = http.createServer((req, resp) => {
resp.end('RANDOMNESS_PLACEHOLDER');
});
server.listen();

View File

@@ -0,0 +1,6 @@
{
"version": 2,
"builds": [
{ "src": "index.js", "use": "@now/node-server" }
]
}

View File

@@ -0,0 +1,7 @@
const assert = require('assert');
module.exports = async ({ deploymentUrl, fetch, randomness }) => {
const resp = await fetch(`https://${deploymentUrl}/`);
assert.equal(resp.headers.get('content-type'), null);
assert.equal(await resp.text(), randomness);
};

View File

@@ -5,7 +5,7 @@
{ "src": "without-bundle/index.js", "use": "@now/node-server", "config": { "bundle": false } }
],
"probes": [
{ "path": "/with-bundle", "mustContain": "RANDOMNESS_PLACEHOLDER" },
{ "path": "/without-bundle", "mustContain": "RANDOMNESS_PLACEHOLDER" }
{ "path": "/with-bundle", "mustContain": "RANDOMNESS_PLACEHOLDER:with-bundle" },
{ "path": "/without-bundle", "mustContain": "RANDOMNESS_PLACEHOLDER:without-bundle" }
]
}

View File

@@ -2,7 +2,7 @@ const http = require('http');
const isBundled = require('./is-bundled.js');
const server = http.createServer((req, resp) => {
resp.end(isBundled() ? 'RANDOMNESS_PLACEHOLDER' : '');
resp.end(isBundled() ? 'RANDOMNESS_PLACEHOLDER:with-bundle' : 'WITHOUT-BUNDLE-THAT-IS-WRONG');
});
server.listen();

View File

@@ -1,3 +1,4 @@
const path = require('path');
module.exports = () => path.basename(__filename) === 'index.js';
// eslint-disable-next-line no-eval
module.exports = () => path.basename(eval('__filename')) === 'index.js';

View File

@@ -2,7 +2,7 @@ const http = require('http');
const isBundled = require('./is-bundled.js');
const server = http.createServer((req, resp) => {
resp.end(isBundled() ? '' : 'RANDOMNESS_PLACEHOLDER');
resp.end(isBundled() ? 'WITH-BUNDLE-THAT-IS-WRONG' : 'RANDOMNESS_PLACEHOLDER:without-bundle');
});
server.listen();

View File

@@ -1,3 +1,4 @@
const path = require('path');
module.exports = () => path.basename(__filename) === 'index.js';
// eslint-disable-next-line no-eval
module.exports = () => path.basename(eval('__filename')) === 'index.js';

View File

@@ -1,14 +1,14 @@
const { createLambda } = require('@now/build-utils/lambda.js');
const download = require('@now/build-utils/fs/download.js');
const FileBlob = require('@now/build-utils/file-blob.js');
const FileFsRef = require('@now/build-utils/file-fs-ref.js');
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 glob = require('@now/build-utils/fs/glob.js');
const glob = require('@now/build-utils/fs/glob.js'); // eslint-disable-line import/no-extraneous-dependencies
const path = require('path');
const {
runNpmInstall,
runPackageJsonScript,
} = require('@now/build-utils/fs/run-user-scripts.js');
} = require('@now/build-utils/fs/run-user-scripts.js'); // eslint-disable-line import/no-extraneous-dependencies
/** @typedef { import('@now/build-utils/file-ref') } FileRef */
/** @typedef {{[filePath: string]: FileRef}} Files */
@@ -35,7 +35,7 @@ async function downloadInstallAndBundle(
console.log('downloading user files...');
const downloadedFiles = await download(files, userPath);
console.log('running npm install for user...');
console.log("installing dependencies for user's code...");
const entrypointFsDirname = path.join(userPath, path.dirname(entrypoint));
await runNpmInstall(entrypointFsDirname, npmArguments);
@@ -44,8 +44,9 @@ async function downloadInstallAndBundle(
{
'package.json': new FileBlob({
data: JSON.stringify({
license: 'UNLICENSED',
dependencies: {
'@zeit/ncc': '0.4.1',
'@zeit/ncc': '0.13.2',
},
}),
}),
@@ -53,7 +54,7 @@ async function downloadInstallAndBundle(
nccPath,
);
console.log('running npm install for ncc...');
console.log('installing dependencies for ncc...');
await runNpmInstall(nccPath, npmArguments);
return [downloadedFiles, nccPath, entrypointFsDirname];
}
@@ -69,7 +70,8 @@ async function compile(workNccPath, downloadedFiles, entrypoint) {
preparedFiles[path.join('user', entrypoint)] = blob;
// eslint-disable-next-line no-restricted-syntax
for (const assetName of Object.keys(assets)) {
const blob2 = new FileBlob({ data: assets[assetName] });
const { source: data, permissions: mode } = assets[assetName];
const blob2 = new FileBlob({ data, mode });
preparedFiles[
path.join('user', path.dirname(entrypoint), assetName)
] = blob2;

View File

@@ -6,7 +6,10 @@ bridge.port = 3000;
let listener;
try {
process.env.NODE_ENV = 'production';
if (!process.env.NODE_ENV) {
process.env.NODE_ENV = 'production';
}
// PLACEHOLDER
} catch (error) {
console.error(error);

View File

@@ -1,12 +1,15 @@
{
"name": "@now/node",
"version": "0.4.27",
"dependencies": {
"@now/node-bridge": "^0.1.9",
"fs-extra": "7.0.1"
"version": "0.4.29-canary.2",
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/zeit/now-builders.git",
"directory": "packages/now-node"
},
"peerDependencies": {
"@now/build-utils": ">=0.0.1"
"dependencies": {
"@now/node-bridge": "^0.1.11-canary.0",
"fs-extra": "7.0.1"
},
"scripts": {
"test": "jest"

View File

@@ -1,4 +1,4 @@
const cowsay = require('cowsay/build/cowsay.umd.js').say;
const cowsay = require('cowsay').say;
// test that process.env is not replaced by webpack
process.env.NODE_ENV = 'development';

View File

@@ -1,5 +1,5 @@
{
"dependencies": {
"cowsay": "^1.3.1"
"cowsay": "1.3.1"
}
}

View File

@@ -1,4 +1,4 @@
const yodasay = require('yodasay/build/yodasay.umd.js').say;
const yodasay = require('yodasay').say;
// test that process.env is not replaced by webpack
process.env.NODE_ENV = 'development';

View File

@@ -1,5 +1,5 @@
{
"dependencies": {
"yodasay": "^1.1.6"
"yodasay": "1.1.9"
}
}

View File

@@ -0,0 +1,8 @@
{
"version": 2,
"builds": [
{ "src": "test1.js", "use": "@now/node" },
{ "src": "test2.js", "use": "@now/node" },
{ "src": "test3.js", "use": "@now/node" }
]
}

View File

@@ -0,0 +1,31 @@
const assert = require('assert');
async function test1({ deploymentUrl, fetch, randomness }) {
const bodyMustBe = `${randomness}:content-length`;
const resp = await fetch(`https://${deploymentUrl}/test1.js`);
assert.equal(resp.status, 401);
assert.equal(await resp.text(), bodyMustBe);
assert.equal(resp.headers.get('content-length'), bodyMustBe.length);
}
async function test2({ deploymentUrl, fetch }) {
const bodyMustBe = '';
const resp = await fetch(`https://${deploymentUrl}/test2.js`);
assert.equal(resp.status, 401);
assert.equal(await resp.text(), bodyMustBe);
assert.equal(resp.headers.get('content-length'), bodyMustBe.length);
}
async function test3({ deploymentUrl, fetch, randomness }) {
const bodyMustBe = `${randomness}:content-length`;
const resp = await fetch(`https://${deploymentUrl}/test3.js`);
assert.equal(resp.status, 401);
assert.equal(await resp.text(), bodyMustBe);
assert.equal(resp.headers.get('content-length'), null);
}
module.exports = async ({ deploymentUrl, fetch, randomness }) => {
await test1({ deploymentUrl, fetch, randomness });
await test2({ deploymentUrl, fetch, randomness });
await test3({ deploymentUrl, fetch, randomness });
};

View File

@@ -0,0 +1,4 @@
module.exports = (_, resp) => {
resp.writeHead(401, { 'content-length': 4242 });
resp.end(`${process.env.RANDOMNESS_ENV_VAR}:content-length`);
};

View File

@@ -0,0 +1,4 @@
module.exports = (_, resp) => {
resp.writeHead(401, { 'content-length': 2 });
resp.end();
};

View File

@@ -0,0 +1,4 @@
module.exports = (_, resp) => {
resp.writeHead(401);
resp.end(`${process.env.RANDOMNESS_ENV_VAR}:content-length`);
};

View File

@@ -1,4 +1,4 @@
const FileBlob = require('@now/build-utils/file-blob.js');
const FileBlob = require('@now/build-utils/file-blob.js'); // eslint-disable-line import/no-extraneous-dependencies
const OptiPng = require('optipng');
const pipe = require('multipipe');

View File

@@ -1,11 +1,14 @@
{
"name": "@now/optipng",
"version": "0.4.7",
"version": "0.4.9-canary.0",
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/zeit/now-builders.git",
"directory": "packages/now-optipng"
},
"dependencies": {
"multipipe": "2.0.3",
"optipng": "1.1.0"
},
"peerDependencies": {
"@now/build-utils": ">=0.0.1"
}
}

View File

@@ -0,0 +1 @@
/build

View File

@@ -0,0 +1,20 @@
FROM library/centos:6.8
RUN yum -y install wget git
RUN rpm -Uvh https://mirror.webtatic.com/yum/el6/latest.rpm
RUN yum -y install php71w-cli php71w-fpm php71w-mbstring php71w-mysql php71w-opcache
RUN yum -y install epel-release
RUN yum -y install patchelf
RUN mkdir -p /root/app/public
WORKDIR /root/app
COPY ./php.ini /root/app/php.ini
COPY ./php-fpm.ini /root/app/php-fpm.ini
COPY ./test.php /root/app/test.php
COPY ./test.sh /root/app/test.sh
RUN patchelf --set-rpath '$ORIGIN' /usr/bin/php
RUN patchelf --set-rpath '$ORIGIN' /usr/sbin/php-fpm
RUN patchelf --set-rpath '$ORIGIN' /usr/lib64/php/modules/mysqli.so
CMD ["/bin/bash", "test.sh"]

View File

@@ -0,0 +1,15 @@
rm -rf ../native
mkdir -p ../native/modules
docker rmi now-php-docker-image --force
docker build . -t now-php-docker-image
docker run now-php-docker-image
docker run now-php-docker-image /bin/cat /usr/sbin/php-fpm > ../native/php-fpm
docker run now-php-docker-image /bin/cat /root/app/php.ini > ../native/php.ini
docker run now-php-docker-image /bin/cat /root/app/php-fpm.ini > ../native/php-fpm.ini
docker run now-php-docker-image /bin/cat /usr/lib64/php/modules/curl.so > ../native/modules/curl.so
docker run now-php-docker-image /bin/cat /usr/lib64/php/modules/json.so > ../native/modules/json.so
docker run now-php-docker-image /bin/cat /usr/lib64/php/modules/mbstring.so > ../native/modules/mbstring.so
docker run now-php-docker-image /bin/cat /usr/lib64/php/modules/mysqli.so > ../native/modules/mysqli.so
docker run now-php-docker-image /bin/cat /usr/lib64/mysql/libmysqlclient.so.16 > ../native/modules/libmysqlclient.so.16
docker run now-php-docker-image /bin/cat /usr/lib64/php/modules/opcache.so > ../native/modules/opcache.so
chmod +x ../native/php-fpm

View File

@@ -0,0 +1,9 @@
[global]
error_log=/tmp/fpm-error.log
[www]
listen=0.0.0.0:9000
pm=static
pm.max_children=1
catch_workers_output=yes
clear_env=no

View File

@@ -0,0 +1,8 @@
extension=/root/app/modules/curl.so
extension=/root/app/modules/json.so
extension=/root/app/modules/mbstring.so
extension=/root/app/modules/mysqli.so
zend_extension=/root/app/modules/opcache.so
opcache.enable_cli=1
mysqli.max_links=10
mysqli.max_persistent=10

View File

@@ -0,0 +1,4 @@
<?php
mysqli_connect();
print('php_sapi_name=' . php_sapi_name() . PHP_EOL);
print('opcache_enabled=' . opcache_get_status()['opcache_enabled'] . PHP_EOL);

View File

@@ -0,0 +1,18 @@
mkdir -p /root/app/modules
cp /usr/bin/php /root/app/php
cp /usr/sbin/php-fpm /root/app/php-fpm
cp /usr/lib64/php/modules/curl.so /root/app/modules/curl.so
cp /usr/lib64/php/modules/json.so /root/app/modules/json.so
cp /usr/lib64/php/modules/mbstring.so /root/app/modules/mbstring.so
cp /usr/lib64/php/modules/mysqli.so /root/app/modules/mysqli.so
cp /usr/lib64/mysql/libmysqlclient.so.16 /root/app/modules/libmysqlclient.so.16
cp /usr/lib64/php/modules/opcache.so /root/app/modules/opcache.so
rm -rf $(which php)
rm -rf $(which php-fpm)
rm -rf /usr/lib64/php
rm -rf /usr/lib64/mysql
rm -rf /etc/php.d
./php-fpm --help
./php -c php.ini test.php
echo "if you see 'can't connect to local mysql' - it is good - mysql library is found and used"
echo "if you see 'call to undefined function mysqli_connect' - it is bad - something went wrong"

View File

@@ -0,0 +1,149 @@
/* eslint-disable no-bitwise,no-use-before-define */
const assert = require('assert');
const { freeParser } = require('_http_common');
const { spawn } = require('child_process');
const createConnection = require('./connection.js');
const { MSG_TYPE, PROTOCOL_STATUS } = require('./consts.js');
const { whenPortOpens } = require('./port.js');
const { HTTPParser } = process.binding('http_parser');
const BEGIN_REQUEST_DATA_KEEP_CONN = Buffer.from('\0\x01\x01\0\0\0\0\0'); // FCGI_ROLE_RESPONDER && FCGI_KEEP_CONN
const MESSAGE_FCGI_STDOUT = `message-${MSG_TYPE.FCGI_STDOUT}`;
const MESSAGE_FCGI_STDERR = `message-${MSG_TYPE.FCGI_STDERR}`;
const MESSAGE_FCGI_END_REQUEST = `message-${MSG_TYPE.FCGI_END_REQUEST}`;
let curReqId = 0;
let connection;
async function startPhp() {
assert(!connection);
const child = spawn(
'./php-fpm',
['-c', 'php.ini',
'--fpm-config', 'php-fpm.ini',
'--nodaemonize'],
{
stdio: 'inherit',
cwd: '/var/task/native',
},
);
child.on('exit', () => {
console.error('php exited');
process.exit(1);
});
child.on('error', (error) => {
console.error(error);
process.exit(1);
});
await whenPortOpens(9000, 400);
const newConnection = createConnection({
_host: '127.0.0.1',
_port: 9000,
});
await new Promise((resolve, reject) => {
function onError() {
cleanup();
reject();
}
function onConnect() {
connection = newConnection;
cleanup();
resolve();
}
newConnection.on('error', onError);
newConnection.on('connect', onConnect);
function cleanup() {
newConnection.removeListener('error', onError);
newConnection.removeListener('connect', onConnect);
}
});
}
async function query({ params, stdin }) {
if (!connection) {
await startPhp();
}
return new Promise((resolve) => {
assert(connection);
const chunks = [
Buffer.from('HTTP/1.1 200 MAKES-PARSER-WORK\n'),
];
function onError(error) {
console.error(error);
process.exit(1);
}
function onStdout(reqId, data) {
chunks.push(data);
}
function onStderr(reqId, data) {
console.error(data.toString().trim());
}
function onEndRequest(reqId, data) {
const protocolStatus = data.readUInt8(4, true);
if (protocolStatus !== PROTOCOL_STATUS.FCGI_REQUEST_COMPLETE) {
console.error('protocolStatus', protocolStatus);
process.exit(1);
}
const response = Buffer.concat(chunks);
const parser = new HTTPParser(HTTPParser.RESPONSE);
let tuples = [];
parser[HTTPParser.kOnHeadersComplete | 0] = (major, minor, t) => {
tuples = t;
};
let body;
parser[HTTPParser.kOnBody | 0] = (b, start, len) => {
body = b.slice(start, start + len);
};
parser.execute(response);
freeParser(parser);
cleanup();
resolve({ tuples, body });
}
connection.on('error', onError);
connection.on(MESSAGE_FCGI_STDOUT, onStdout);
connection.on(MESSAGE_FCGI_STDERR, onStderr);
connection.on(MESSAGE_FCGI_END_REQUEST, onEndRequest);
function cleanup() {
connection.removeListener('error', onError);
connection.removeListener(MESSAGE_FCGI_STDOUT, onStdout);
connection.removeListener(MESSAGE_FCGI_STDERR, onStderr);
connection.removeListener(MESSAGE_FCGI_END_REQUEST, onEndRequest);
}
curReqId += 1;
// TODO these things have callbacks. what to do with them?
connection.send(MSG_TYPE.FCGI_BEGIN_REQUEST, curReqId, BEGIN_REQUEST_DATA_KEEP_CONN);
connection.send(MSG_TYPE.FCGI_PARAMS, curReqId, params);
connection.send(MSG_TYPE.FCGI_PARAMS, curReqId, null);
if (stdin) connection.send(MSG_TYPE.FCGI_STDIN, curReqId, stdin);
connection.send(MSG_TYPE.FCGI_STDIN, curReqId, null);
});
}
module.exports = {
query,
};
/*
(async function() {
console.log(await query({ params: {
REQUEST_METHOD: 'GET', SCRIPT_FILENAME: '/phpinfo.php'
} }));
})();
*/

View File

@@ -0,0 +1,41 @@
/* eslint-disable consistent-return */
const net = require('net');
function whenPortOpensCallback(port, attempts, cb) {
const client = net.connect(port, '127.0.0.1');
client.on('error', (error) => {
if (!attempts) return cb(error);
setTimeout(() => {
whenPortOpensCallback(port, attempts - 1, cb);
}, 50);
});
client.on('connect', () => {
client.destroy();
cb();
});
}
function isPortOpen(port) {
return new Promise((resolve) => {
whenPortOpensCallback(port, 0, (error) => {
if (error) return resolve(false);
resolve(true);
});
});
}
function whenPortOpens(port, attempts) {
return new Promise((resolve, reject) => {
whenPortOpensCallback(port, attempts, (error) => {
if (error) return reject(error);
resolve();
});
});
}
module.exports = {
isPortOpen,
whenPortOpensCallback,
whenPortOpens,
};

View File

@@ -0,0 +1,43 @@
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 glob = require('@now/build-utils/fs/glob.js'); // eslint-disable-line import/no-extraneous-dependencies
const path = require('path');
async function getFiles() {
const files = await glob('native/**', __dirname);
const phpConfig = await FileBlob.fromStream({
stream: files['native/php.ini'].toStream(),
});
phpConfig.data = phpConfig.data
.toString()
.replace(/\/root\/app\/modules/g, '/var/task/native/modules');
files['native/php.ini'] = phpConfig;
Object.assign(files, {
'fastcgi/connection.js': new FileFsRef({
fsPath: require.resolve('fastcgi-client/lib/connection.js'),
}),
'fastcgi/consts.js': new FileFsRef({
fsPath: require.resolve('fastcgi-client/lib/consts.js'),
}),
'fastcgi/stringifykv.js': new FileFsRef({
fsPath: require.resolve('fastcgi-client/lib/stringifykv.js'),
}),
'fastcgi/index.js': new FileFsRef({
fsPath: path.join(__dirname, 'fastcgi/index.js'),
}),
'fastcgi/port.js': new FileFsRef({
fsPath: path.join(__dirname, 'fastcgi/port.js'),
}),
'launcher.js': new FileFsRef({
fsPath: path.join(__dirname, 'launcher.js'),
}),
});
return files;
}
module.exports = {
getFiles,
};

View File

@@ -0,0 +1,142 @@
/* eslint-disable prefer-template */
const assert = require('assert');
const fs = require('fs');
const { join: pathJoin } = require('path');
const { parse: parseUrl } = require('url');
const { query } = require('./fastcgi/index.js');
function normalizeEvent(event) {
if (event.Action === 'Invoke') {
const invokeEvent = JSON.parse(event.body);
const {
method, path, headers, encoding,
} = invokeEvent;
let { body } = invokeEvent;
if (body) {
if (encoding === 'base64') {
body = Buffer.from(body, encoding);
} else if (encoding === undefined) {
body = Buffer.from(body);
} else {
throw new Error(`Unsupported encoding: ${encoding}`);
}
}
return {
method, path, headers, body,
};
}
const {
httpMethod: method, path, headers, body,
} = event;
return {
method, path, headers, body,
};
}
function isDirectory(p) {
return new Promise((resolve) => {
fs.stat(p, (error, s) => {
if (error) {
resolve(false);
return;
}
if (s.isDirectory()) {
resolve(true);
return;
}
resolve(false);
});
});
}
async function transformFromAwsRequest({
method, path, headers, body,
}) {
const { pathname, search, query: queryString } = parseUrl(path);
let requestUri = pathname + (search || '');
let filename = pathJoin('/var/task/user', pathname);
if (await isDirectory(filename)) {
if (!filename.endsWith('/')) {
filename += '/';
requestUri = pathname + '/' + (search || '');
}
filename += 'index.php';
}
const params = {};
params.REQUEST_METHOD = method;
params.REQUEST_URI = requestUri;
params.QUERY_STRING = queryString || ''; // can be null
params.SCRIPT_FILENAME = filename;
params.SERVER_PROTOCOL = 'HTTP/1.1';
params.SERVER_PORT = 443;
params.HTTPS = 'on';
// eslint-disable-next-line no-restricted-syntax
for (const [k, v] of Object.entries(headers)) {
const camel = k.toUpperCase().replace(/-/g, '_');
params[`HTTP_${camel}`] = v;
if (camel === 'HOST') {
params.SERVER_NAME = v;
} else
if (['CONTENT_TYPE', 'CONTENT_LENGTH'].includes(camel)) {
params[camel] = v; // without HOST_ prepended
}
}
return { params, stdin: body };
}
function transformToAwsResponse({ tuples, body }) {
let statusCode = 200;
const headers = {};
// eslint-disable-next-line no-param-reassign
if (!body) body = Buffer.alloc(0);
assert(Buffer.isBuffer(body));
for (let i = 0; i < tuples.length; i += 2) {
const k = tuples[i].toLowerCase();
const v = tuples[i + 1];
if (k === 'status') {
statusCode = Number(v.split(' ')[0]); // '408 Request Timeout'
} else {
if (!headers[k]) headers[k] = [];
headers[k].push(v);
}
}
return {
statusCode,
headers,
body: body.toString('base64'),
encoding: 'base64',
};
}
async function launcher(event) {
const awsRequest = normalizeEvent(event);
const input = await transformFromAwsRequest(awsRequest);
const output = await query(input);
return transformToAwsResponse(output);
}
exports.launcher = launcher;
/*
(async function() {
console.log(await launcher({
httpMethod: 'GET',
path: '/phpinfo.php'
}));
})();
*/

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -0,0 +1,9 @@
[global]
error_log=/tmp/fpm-error.log
[www]
listen=0.0.0.0:9000
pm=static
pm.max_children=1
catch_workers_output=yes
clear_env=no

View File

@@ -0,0 +1,8 @@
extension=/root/app/modules/curl.so
extension=/root/app/modules/json.so
extension=/root/app/modules/mbstring.so
extension=/root/app/modules/mysqli.so
zend_extension=/root/app/modules/opcache.so
opcache.enable_cli=1
mysqli.max_links=10
mysqli.max_persistent=10

View File

@@ -0,0 +1,13 @@
{
"name": "@now/php-bridge",
"version": "0.4.14-canary.0",
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/zeit/now-builders.git",
"directory": "packages/now-php-bridge"
},
"dependencies": {
"fastcgi-client": "0.0.1"
}
}

View File

@@ -1,2 +1 @@
/launcher
/test

Binary file not shown.

Binary file not shown.

View File

@@ -1,3 +0,0 @@
extension=modules/curl.so
extension=modules/json.so
extension=modules/mbstring.so

View File

@@ -1,7 +1,7 @@
const { createLambda } = require('@now/build-utils/lambda.js');
const glob = require('@now/build-utils/fs/glob.js');
const { createLambda } = require('@now/build-utils/lambda.js'); // eslint-disable-line import/no-extraneous-dependencies
const path = require('path');
const rename = require('@now/build-utils/fs/rename.js');
const rename = require('@now/build-utils/fs/rename.js'); // eslint-disable-line import/no-extraneous-dependencies
const { getFiles } = require('@now/php-bridge');
exports.config = {
maxLambdaSize: '10mb',
@@ -10,17 +10,16 @@ exports.config = {
exports.build = async ({ files, entrypoint }) => {
// move all user code to 'user' subdirectory
const userFiles = rename(files, name => path.join('user', name));
const launcherFiles = await glob('**', path.join(__dirname, 'dist'));
const zipFiles = { ...userFiles, ...launcherFiles };
const bridgeFiles = await getFiles();
// TODO config.extensions. OR php.ini from user
delete bridgeFiles['native/modules/mysqli.so'];
delete bridgeFiles['native/modules/libmysqlclient.so.16'];
const lambda = await createLambda({
files: zipFiles,
handler: 'launcher',
runtime: 'go1.x',
environment: {
SCRIPT_NAME: path.join('/', entrypoint),
NOW_PHP_SCRIPT: path.join('user', entrypoint),
},
files: { ...userFiles, ...bridgeFiles },
handler: 'launcher.launcher',
runtime: 'nodejs8.10',
});
return { [entrypoint]: lambda };

View File

@@ -1,28 +0,0 @@
FROM library/centos:6.8
RUN yum -y install wget git
RUN rpm -Uvh https://mirror.webtatic.com/yum/el6/latest.rpm
RUN yum -y install php71w-cli php71w-devel php71w-embedded php71w-mbstring
RUN yum -y install epel-release
RUN yum -y install golang
RUN mkdir -p /root/go/app/public
RUN go get -v github.com/aws/aws-lambda-go/lambda
RUN go get -v github.com/aws/aws-lambda-go/events
RUN go get -v github.com/deuill/go-php
WORKDIR /root/go/app
COPY ./utils/bridge.go /root/go/app/utils/bridge.go
COPY ./launcher.go /root/go/app/launcher.go
COPY ./php.ini /root/go/app/php.ini
COPY ./test.go /root/go/app/test.go
COPY ./test.php /root/go/app/public/test.php
COPY ./test.sh /root/go/app/test.sh
RUN GOOS=linux GOARCH=amd64 CGO_ENABLED=1 go build -o launcher launcher.go
RUN GOOS=linux GOARCH=amd64 CGO_ENABLED=1 go build -o test test.go
RUN strip launcher
RUN strip test
CMD ["/bin/bash", "test.sh"]

View File

@@ -1,15 +0,0 @@
rm -rf ../dist
mkdir -p ../dist/modules
mkdir ./utils
cp ../../../utils/go/bridge/bridge.go ./utils/bridge.go
docker rmi go-php-builder --force
docker build . -t go-php-builder
docker run go-php-builder
docker run go-php-builder /bin/cat /root/go/app/launcher > ../dist/launcher
docker run go-php-builder /bin/cat /root/go/app/php.ini > ../dist/php.ini
docker run go-php-builder /bin/cat /usr/lib64/libphp7-7.1.so > ../dist/libphp7-7.1.so
docker run go-php-builder /bin/cat /usr/lib64/php/modules/curl.so > ../dist/modules/curl.so
docker run go-php-builder /bin/cat /usr/lib64/php/modules/json.so > ../dist/modules/json.so
docker run go-php-builder /bin/cat /usr/lib64/php/modules/mbstring.so > ../dist/modules/mbstring.so
chmod +x ../dist/launcher
rm -rf ./utils

View File

@@ -1,80 +0,0 @@
package main
import (
now "./utils"
"bytes"
php "github.com/deuill/go-php"
"net/http"
"os"
"path"
"path/filepath"
"strings"
)
type PhpHandler struct {
http.Handler
ScriptFull string
}
func (h *PhpHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
engine, _ := php.New()
context, _ := engine.NewContext()
var query = r.URL.Query()
getMap := make(map[string]string)
for k, v := range query {
for _, s := range v {
getMap[k] = s
}
}
context.Bind("_GET", getMap)
r.ParseForm()
postMap := make(map[string]string)
for k, v := range r.PostForm {
for _, s := range v {
postMap[k] = s
}
}
context.Bind("_POST", postMap)
envMap := make(map[string]string)
for _, e := range os.Environ() {
pair := strings.Split(e, "=")
envMap[pair[0]] = pair[1]
}
context.Bind("_ENV", envMap)
context.Eval("$_SERVER[\"SERVER_NAME\"]=\"" + r.Host + "\";")
context.Eval("$_SERVER[\"SERVER_PORT\"]=\"443\";")
context.Eval("$_SERVER[\"HTTPS\"]=\"on\";")
context.Eval("http_response_code(200);")
var stdout bytes.Buffer
context.Output = &stdout
context.Exec(h.ScriptFull)
statusCodeVal, _ := context.Eval("return http_response_code();")
w.WriteHeader(int(statusCodeVal.Int()))
headers := w.Header()
headers.Add("content-type", "text/html")
for k, v := range context.Header {
for _, s := range v {
headers.Add(k, s)
}
}
w.Write(stdout.Bytes())
engine.Destroy()
}
func main() {
ex, _ := os.Executable()
handler := &PhpHandler{
nil,
path.Join(filepath.Dir(ex), os.Getenv("NOW_PHP_SCRIPT")),
}
now.Start(handler)
}

View File

@@ -1,3 +0,0 @@
extension=modules/curl.so
extension=modules/json.so
extension=modules/mbstring.so

Some files were not shown because too many files have changed in this diff Show More