@vercel/go 2.0.14 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -17689,7 +17689,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
17689
17689
  return (mod && mod.__esModule) ? mod : { "default": mod };
17690
17690
  };
17691
17691
  Object.defineProperty(exports, "__esModule", ({ value: true }));
17692
- exports.prepareCache = exports.startDevServer = exports.build = exports.version = exports.shouldServe = void 0;
17692
+ exports.prepareCache = exports.startDevServer = exports.getNewHandlerFunctionName = exports.build = exports.version = exports.shouldServe = void 0;
17693
17693
  const execa_1 = __importDefault(__webpack_require__(580));
17694
17694
  const async_retry_1 = __importDefault(__webpack_require__(8571));
17695
17695
  const os_1 = __webpack_require__(2087);
@@ -17740,6 +17740,7 @@ async function build({ files, entrypoint, config, workPath, meta = {}, }) {
17740
17740
  // in order to undo the action, not what the original action was
17741
17741
  const undoFileActions = [];
17742
17742
  const undoDirectoryCreation = [];
17743
+ const undoFunctionRenames = [];
17743
17744
  try {
17744
17745
  if (process.env.GIT_CREDENTIALS) {
17745
17746
  build_utils_1.debug('Initialize Git credentials...');
@@ -17756,6 +17757,7 @@ async function build({ files, entrypoint, config, workPath, meta = {}, }) {
17756
17757
  Learn more: https://github.com/golang/go/wiki/Modules
17757
17758
  `);
17758
17759
  }
17760
+ const originalEntrypointAbsolute = path_1.join(workPath, entrypoint);
17759
17761
  const renamedEntrypoint = getRenamedEntrypoint(entrypoint);
17760
17762
  if (renamedEntrypoint) {
17761
17763
  await fs_extra_1.move(path_1.join(workPath, entrypoint), path_1.join(workPath, renamedEntrypoint));
@@ -17853,8 +17855,14 @@ async function build({ files, entrypoint, config, workPath, meta = {}, }) {
17853
17855
  }
17854
17856
  }
17855
17857
  }
17856
- const handlerFunctionName = parsedAnalyzed.functionName;
17857
- build_utils_1.debug(`Found exported function "${handlerFunctionName}" in "${entrypoint}"`);
17858
+ const originalFunctionName = parsedAnalyzed.functionName;
17859
+ const handlerFunctionName = getNewHandlerFunctionName(originalFunctionName, entrypoint);
17860
+ await renameHandlerFunction(entrypointAbsolute, originalFunctionName, handlerFunctionName);
17861
+ undoFunctionRenames.push({
17862
+ fsPath: originalEntrypointAbsolute,
17863
+ from: handlerFunctionName,
17864
+ to: originalFunctionName,
17865
+ });
17858
17866
  if (!isGoModExist) {
17859
17867
  if (await fs_extra_1.pathExists(path_1.join(workPath, 'vendor'))) {
17860
17868
  throw new Error('`go.mod` is required to use a `vendor` directory.');
@@ -17890,8 +17898,8 @@ async function build({ files, entrypoint, config, workPath, meta = {}, }) {
17890
17898
  throw err;
17891
17899
  }
17892
17900
  }
17893
- const mainModGoFileName = 'main__mod__.go';
17894
- const modMainGoContents = await fs_extra_1.readFile(__webpack_require__.ab + "main__mod__.go", 'utf8');
17901
+ const mainModGoFileName = 'main.go';
17902
+ const modMainGoContents = await fs_extra_1.readFile(__webpack_require__.ab + "main.go", 'utf8');
17895
17903
  let goPackageName = `${packageName}/${packageName}`;
17896
17904
  const goFuncName = `${packageName}.${handlerFunctionName}`;
17897
17905
  if (isGoModExist) {
@@ -17992,8 +18000,10 @@ async function build({ files, entrypoint, config, workPath, meta = {}, }) {
17992
18000
  const go = await go_helpers_1.createGo(workPath, goPath, process.platform, process.arch, {
17993
18001
  cwd: entrypointDirname,
17994
18002
  }, false);
17995
- const origianlMainGoContents = await fs_extra_1.readFile(__webpack_require__.ab + "main.go", 'utf8');
17996
- const mainGoContents = origianlMainGoContents.replace('__VC_HANDLER_FUNC_NAME', handlerFunctionName);
18003
+ const originalMainGoContents = await fs_extra_1.readFile(__webpack_require__.ab + "main.go", 'utf8');
18004
+ const mainGoContents = originalMainGoContents
18005
+ .replace('"__VC_HANDLER_PACKAGE_NAME"', '')
18006
+ .replace('__VC_HANDLER_FUNC_NAME', handlerFunctionName);
17997
18007
  // in order to allow the user to have `main.go`,
17998
18008
  // we need our `main.go` to be called something else
17999
18009
  const mainGoFileName = 'main__vc__go__.go';
@@ -18032,6 +18042,7 @@ async function build({ files, entrypoint, config, workPath, meta = {}, }) {
18032
18042
  files: { ...(await build_utils_1.glob('**', outDir)), ...includedFiles },
18033
18043
  handler: handlerFileName,
18034
18044
  runtime: 'go1.x',
18045
+ supportsWrapper: true,
18035
18046
  environment: {},
18036
18047
  });
18037
18048
  const watch = parsedAnalyzed.watch;
@@ -18053,7 +18064,7 @@ async function build({ files, entrypoint, config, workPath, meta = {}, }) {
18053
18064
  }
18054
18065
  finally {
18055
18066
  try {
18056
- await cleanupFileSystem(undoFileActions, undoDirectoryCreation);
18067
+ await cleanupFileSystem(undoFileActions, undoDirectoryCreation, undoFunctionRenames);
18057
18068
  }
18058
18069
  catch (error) {
18059
18070
  console.log(`Build cleanup failed: ${error.message}`);
@@ -18062,7 +18073,27 @@ async function build({ files, entrypoint, config, workPath, meta = {}, }) {
18062
18073
  }
18063
18074
  }
18064
18075
  exports.build = build;
18065
- async function cleanupFileSystem(undoFileActions, undoDirectoryCreation) {
18076
+ async function renameHandlerFunction(fsPath, from, to) {
18077
+ let fileContents = await fs_extra_1.readFile(fsPath, 'utf8');
18078
+ const fromRegex = new RegExp(`\\b${from}\\b`, 'g');
18079
+ fileContents = fileContents.replace(fromRegex, to);
18080
+ await fs_extra_1.writeFile(fsPath, fileContents);
18081
+ }
18082
+ function getNewHandlerFunctionName(originalFunctionName, entrypoint) {
18083
+ if (!originalFunctionName) {
18084
+ throw new Error('Handler function renaming failed because original function name was empty.');
18085
+ }
18086
+ if (!entrypoint) {
18087
+ throw new Error('Handler function renaming failed because entrypoint was empty.');
18088
+ }
18089
+ build_utils_1.debug(`Found exported function "${originalFunctionName}" in "${entrypoint}"`);
18090
+ const pathSlug = entrypoint.replace(/(\s|\\|\/|\]|\[|-|\.)/g, '_');
18091
+ const newHandlerName = `${originalFunctionName}_${pathSlug}`;
18092
+ build_utils_1.debug(`Renaming handler function temporarily from "${originalFunctionName}" to "${newHandlerName}"`);
18093
+ return newHandlerName;
18094
+ }
18095
+ exports.getNewHandlerFunctionName = getNewHandlerFunctionName;
18096
+ async function cleanupFileSystem(undoFileActions, undoDirectoryCreation, undoFunctionRenames) {
18066
18097
  // we have to undo the actions in reverse order in cases
18067
18098
  // where one file was moved multiple times, which happens
18068
18099
  // using files that start with brackets
@@ -18074,6 +18105,11 @@ async function cleanupFileSystem(undoFileActions, undoDirectoryCreation) {
18074
18105
  await fs_extra_1.remove(action.from);
18075
18106
  }
18076
18107
  }
18108
+ // after files are moved back, we can undo function renames
18109
+ // these reference the original file location
18110
+ for (const rename of undoFunctionRenames) {
18111
+ await renameHandlerFunction(rename.fsPath, rename.from, rename.to);
18112
+ }
18077
18113
  const undoDirectoryPromises = undoDirectoryCreation.map(async (directory) => {
18078
18114
  const contents = await fs_extra_1.readdir(directory);
18079
18115
  // only delete an empty directory
package/dist/main.go CHANGED
@@ -1,10 +1,31 @@
1
1
  package main
2
2
 
3
3
  import (
4
- vc "github.com/vercel/go-bridge/go/bridge"
5
4
  "net/http"
5
+ "os"
6
+ "syscall"
7
+
8
+ "__VC_HANDLER_PACKAGE_NAME"
9
+ vc "github.com/vercel/go-bridge/go/bridge"
6
10
  )
7
11
 
12
+ func checkForLambdaWrapper() {
13
+ wrapper := os.Getenv("AWS_LAMBDA_EXEC_WRAPPER")
14
+ if wrapper == "" {
15
+ return
16
+ }
17
+
18
+ // Removing the env var doesn't work
19
+ // Set it to empty string to override the previous value
20
+ os.Setenv("AWS_LAMBDA_EXEC_WRAPPER", "")
21
+ argv := append([]string{wrapper}, os.Args...)
22
+ err := syscall.Exec(wrapper, argv, os.Environ())
23
+ if err != nil {
24
+ panic(err)
25
+ }
26
+ }
27
+
8
28
  func main() {
29
+ checkForLambdaWrapper()
9
30
  vc.Start(http.HandlerFunc(__VC_HANDLER_FUNC_NAME))
10
31
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vercel/go",
3
- "version": "2.0.14",
3
+ "version": "2.1.0",
4
4
  "license": "MIT",
5
5
  "main": "./dist/index",
6
6
  "homepage": "https://vercel.com/docs/runtimes#official-runtimes/go",
@@ -17,14 +17,25 @@
17
17
  "files": [
18
18
  "dist"
19
19
  ],
20
+ "jest": {
21
+ "preset": "ts-jest",
22
+ "testEnvironment": "node",
23
+ "globals": {
24
+ "ts-jest": {
25
+ "diagnostics": true,
26
+ "isolatedModules": true
27
+ }
28
+ }
29
+ },
20
30
  "devDependencies": {
21
31
  "@tootallnate/once": "1.1.2",
22
32
  "@types/async-retry": "1.4.2",
23
33
  "@types/execa": "^0.9.0",
24
34
  "@types/fs-extra": "^5.0.5",
35
+ "@types/jest": "28.1.6",
25
36
  "@types/node-fetch": "^2.3.0",
26
37
  "@types/tar": "^4.0.0",
27
- "@vercel/build-utils": "5.1.1",
38
+ "@vercel/build-utils": "5.3.0",
28
39
  "@vercel/ncc": "0.24.0",
29
40
  "async-retry": "1.3.1",
30
41
  "execa": "^1.0.0",
@@ -34,5 +45,5 @@
34
45
  "tar": "4.4.6",
35
46
  "typescript": "4.3.4"
36
47
  },
37
- "gitHead": "5eb8b16cbdf6215618b4b8125673941757fa1a8e"
48
+ "gitHead": "31f79c7de1944420b1f6fa5be9235c9b3f1d62b6"
38
49
  }
@@ -1,12 +0,0 @@
1
- package main
2
-
3
- import (
4
- "__VC_HANDLER_PACKAGE_NAME"
5
- "net/http"
6
-
7
- vc "github.com/vercel/go-bridge/go/bridge"
8
- )
9
-
10
- func main() {
11
- vc.Start(http.HandlerFunc(__VC_HANDLER_FUNC_NAME))
12
- }