Skip to content

Commit 1bc94d8

Browse files
authored
Merge pull request #22 from serverless-components/fix/rollback-to-1.0.11
Fix/rollback-to-1.0.11
2 parents 3e9891f + 4d368ea commit 1bc94d8

File tree

11 files changed

+978
-883
lines changed

11 files changed

+978
-883
lines changed

serverless.component.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: nuxtjs
2-
version: 1.0.12
2+
version: 1.0.13
33
# version: dev
44
author: 'Tencent Cloud, Inc.'
55
org: 'Tencent Cloud, Inc.'

src/_shims/handler.js

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
require('tencent-component-monitor')
1+
try {
2+
require('tencent-component-monitor')
3+
} catch (e) {
4+
console.log(e)
5+
}
26
const fs = require('fs')
37
const path = require('path')
48
const { createServer, proxy } = require('tencent-serverless-http')
@@ -7,33 +11,39 @@ let app
711
let server
812

913
module.exports.handler = async (event, context) => {
10-
const userSls = path.join(__dirname, '..', process.env.SLS_ENTRY_FILE)
11-
if (fs.existsSync(userSls)) {
12-
// eslint-disable-next-line
13-
console.log(`Using user custom entry file ${process.env.SLS_ENTRY_FILE}`)
14-
app = await require(userSls)(true)
15-
} else {
16-
app = await require('./sls')(false)
14+
if (!app) {
15+
const userSls = path.join(__dirname, '..', process.env.SLS_ENTRY_FILE)
16+
if (fs.existsSync(userSls)) {
17+
// eslint-disable-next-line
18+
console.log(`Using user custom entry file ${process.env.SLS_ENTRY_FILE}`)
19+
app = await require(userSls)(true)
20+
} else {
21+
app = await require('./sls')(false)
22+
}
23+
24+
// provide sls intialize hooks
25+
if (app.slsInitialize && typeof app.slsInitialize === 'function') {
26+
await app.slsInitialize()
27+
}
1728
}
1829

1930
// attach event and context to request
20-
app.request.__SLS_EVENT__ = event
21-
app.request.__SLS_CONTEXT__ = context
22-
23-
if (!server) {
24-
server = createServer(
25-
app.callback && typeof app.callback === 'function' ? app.callback() : app,
26-
null,
27-
app.binaryTypes || []
28-
)
31+
try {
32+
app.request.__SLS_EVENT__ = event
33+
app.request.__SLS_CONTEXT__ = context
34+
} catch (e) {
35+
// no op
2936
}
3037

31-
context.callbackWaitsForEmptyEventLoop = app.callbackWaitsForEmptyEventLoop === true
38+
// do not cache server, so we can pass latest event to server
39+
server = createServer(
40+
app.callback && typeof app.callback === 'function' ? app.callback() : app,
41+
null,
42+
app.binaryTypes || []
43+
)
3244

33-
if (app.slsInitialize && typeof app.slsInitialize === 'function') {
34-
await app.slsInitialize()
35-
}
45+
context.callbackWaitsForEmptyEventLoop = app.callbackWaitsForEmptyEventLoop === true
3646

37-
const result = await proxy(server, event, context, 'PROMISE')
38-
return result.promise
47+
const { promise } = await proxy(server, event, context, 'PROMISE')
48+
return promise
3949
}

src/_shims/sls.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ async function createServer() {
2121
// if includes, will return base64 encoded, very useful for images
2222
server.binaryTypes = ['*/*']
2323

24+
// 返回 server
2425
return server
2526
}
2627

src/config.js

Lines changed: 141 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,139 @@
1+
Object.defineProperty(exports, '__esModule', { value: true })
2+
exports.getConfig = void 0
3+
const fs = require('fs')
4+
const path = require('path')
5+
const YAML = require('js-yaml')
6+
const TEMPLATE_BASE_URL = 'https://serverless-templates-1300862921.cos.ap-beijing.myqcloud.com'
7+
const frameworks = {
8+
express: {
9+
injectSlsSdk: true,
10+
runtime: 'Nodejs10.15',
11+
defaultEntryFile: 'sls.js',
12+
defaultStatics: [{ src: 'public', targetDir: '/' }]
13+
},
14+
koa: {
15+
injectSlsSdk: true,
16+
runtime: 'Nodejs10.15',
17+
defaultEntryFile: 'sls.js',
18+
defaultStatics: [{ src: 'public', targetDir: '/' }]
19+
},
20+
egg: {
21+
injectSlsSdk: true,
22+
runtime: 'Nodejs10.15',
23+
defaultEntryFile: 'sls.js',
24+
defaultStatics: [{ src: 'public', targetDir: '/' }],
25+
defaultEnvs: [
26+
{
27+
key: 'SERVERLESS',
28+
value: '1'
29+
},
30+
{
31+
key: 'EGG_APP_CONFIG',
32+
value: '{"rundir":"/tmp","logger":{"dir":"/tmp"}}'
33+
}
34+
]
35+
},
36+
nestjs: {
37+
injectSlsSdk: true,
38+
runtime: 'Nodejs10.15',
39+
defaultEntryFile: 'sls.js',
40+
defaultStatics: [{ src: 'public', targetDir: '/' }]
41+
},
42+
nextjs: {
43+
injectSlsSdk: true,
44+
runtime: 'Nodejs10.15',
45+
defaultEntryFile: 'sls.js',
46+
defaultStatics: [
47+
{ src: '.next/static', targetDir: '/_next/static' },
48+
{ src: 'public', targetDir: '/' }
49+
]
50+
},
51+
nuxtjs: {
52+
injectSlsSdk: true,
53+
runtime: 'Nodejs10.15',
54+
defaultEntryFile: 'sls.js',
55+
defaultStatics: [
56+
{ src: '.nuxt/dist/client', targetDir: '/' },
57+
{ src: 'static', targetDir: '/' }
58+
]
59+
},
60+
laravel: {
61+
injectSlsSdk: false,
62+
runtime: 'Php7',
63+
defaultEnvs: [
64+
{
65+
key: 'SERVERLESS',
66+
value: '1'
67+
},
68+
{
69+
key: 'VIEW_COMPILED_PATH',
70+
value: '/tmp/storage/framework/views'
71+
},
72+
{
73+
key: 'SESSION_DRIVER',
74+
value: 'array'
75+
},
76+
{
77+
key: 'LOG_CHANNEL',
78+
value: 'stderr'
79+
},
80+
{
81+
key: 'APP_STORAGE',
82+
value: '/tmp/storage'
83+
}
84+
]
85+
},
86+
thinkphp: {
87+
injectSlsSdk: false,
88+
runtime: 'Php7'
89+
},
90+
flask: {
91+
injectSlsSdk: false,
92+
runtime: 'Python3.6'
93+
},
94+
django: {
95+
injectSlsSdk: false,
96+
runtime: 'Python3.6'
97+
}
98+
}
199
const CONFIGS = {
2-
templateUrl:
3-
'https://serverless-templates-1300862921.cos.ap-beijing.myqcloud.com/nuxtjs-demo.zip',
4-
compName: 'nuxtjs',
5-
compFullname: 'Nuxt.js',
6-
defaultEntryFile: 'sls.js',
100+
// support metrics frameworks
101+
pythonFrameworks: ['flask', 'django'],
102+
supportMetrics: ['express', 'next', 'nuxt'],
7103
region: 'ap-guangzhou',
104+
description: 'Created by Serverless Component',
8105
handler: 'sl_handler.handler',
9-
runtime: 'Nodejs10.15',
10-
timeout: 3,
106+
timeout: 10,
11107
memorySize: 128,
12108
namespace: 'default',
13-
description: 'Created by Serverless Component',
14-
defaultStatics: [
15-
{ src: '.nuxt/dist/client', targetDir: '/' },
16-
{ src: 'static', targetDir: '/' }
109+
defaultEnvs: [
110+
{
111+
key: 'SERVERLESS',
112+
value: '1'
113+
}
17114
],
18-
defaultCdnConf: {
19-
autoRefresh: true,
115+
cos: {
116+
lifecycle: [
117+
{
118+
status: 'Enabled',
119+
id: 'deleteObject',
120+
expiration: { days: '10' },
121+
abortIncompleteMultipartUpload: { daysAfterInitiation: '10' }
122+
}
123+
]
124+
},
125+
cdn: {
126+
forceRedirect: {
127+
switch: 'on',
128+
redirectType: 'https',
129+
redirectStatusCode: 301
130+
},
131+
https: {
132+
switch: 'on',
133+
http2: 'on'
134+
}
135+
},
136+
defaultCdnConfig: {
20137
forceRedirect: {
21138
switch: 'on',
22139
redirectType: 'https',
@@ -47,12 +164,20 @@ const CONFIGS = {
47164
'name/cos:HeadObject',
48165
'name/cos:OptionsObject'
49166
],
50-
Resource: [`qcs::cos:${region}:uid/${appid}:${bucket}-${appid}/*`]
167+
Resource: [`qcs::cos:${region}:uid/${appid}:${bucket}/*`]
51168
}
52169
],
53170
version: '2.0'
54171
}
55172
}
56173
}
57-
58-
module.exports = CONFIGS
174+
const getConfig = () => {
175+
const { name: framework } = YAML.load(
176+
// framework.yml 会在组件部署流程中动态生成
177+
fs.readFileSync(path.join(__dirname, 'framework.yml'), 'utf-8')
178+
)
179+
const templateUrl = `${TEMPLATE_BASE_URL}/${framework}-demo.zip`
180+
const frameworkConfigs = frameworks[framework]
181+
return Object.assign(Object.assign({ framework, templateUrl }, CONFIGS), frameworkConfigs)
182+
}
183+
exports.getConfig = getConfig

0 commit comments

Comments
 (0)