Skip to content

Commit

Permalink
Merge pull request #22 from serverless-components/fix/rollback-to-1.0.11
Browse files Browse the repository at this point in the history
Fix/rollback-to-1.0.11
  • Loading branch information
tianmeng0103 authored Mar 2, 2022
2 parents 3e9891f + 4d368ea commit 1bc94d8
Show file tree
Hide file tree
Showing 11 changed files with 978 additions and 883 deletions.
2 changes: 1 addition & 1 deletion serverless.component.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: nuxtjs
version: 1.0.12
version: 1.0.13
# version: dev
author: 'Tencent Cloud, Inc.'
org: 'Tencent Cloud, Inc.'
Expand Down
56 changes: 33 additions & 23 deletions src/_shims/handler.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
require('tencent-component-monitor')
try {
require('tencent-component-monitor')
} catch (e) {
console.log(e)
}
const fs = require('fs')
const path = require('path')
const { createServer, proxy } = require('tencent-serverless-http')
Expand All @@ -7,33 +11,39 @@ let app
let server

module.exports.handler = async (event, context) => {
const userSls = path.join(__dirname, '..', process.env.SLS_ENTRY_FILE)
if (fs.existsSync(userSls)) {
// eslint-disable-next-line
console.log(`Using user custom entry file ${process.env.SLS_ENTRY_FILE}`)
app = await require(userSls)(true)
} else {
app = await require('./sls')(false)
if (!app) {
const userSls = path.join(__dirname, '..', process.env.SLS_ENTRY_FILE)
if (fs.existsSync(userSls)) {
// eslint-disable-next-line
console.log(`Using user custom entry file ${process.env.SLS_ENTRY_FILE}`)
app = await require(userSls)(true)
} else {
app = await require('./sls')(false)
}

// provide sls intialize hooks
if (app.slsInitialize && typeof app.slsInitialize === 'function') {
await app.slsInitialize()
}
}

// attach event and context to request
app.request.__SLS_EVENT__ = event
app.request.__SLS_CONTEXT__ = context

if (!server) {
server = createServer(
app.callback && typeof app.callback === 'function' ? app.callback() : app,
null,
app.binaryTypes || []
)
try {
app.request.__SLS_EVENT__ = event
app.request.__SLS_CONTEXT__ = context
} catch (e) {
// no op
}

context.callbackWaitsForEmptyEventLoop = app.callbackWaitsForEmptyEventLoop === true
// do not cache server, so we can pass latest event to server
server = createServer(
app.callback && typeof app.callback === 'function' ? app.callback() : app,
null,
app.binaryTypes || []
)

if (app.slsInitialize && typeof app.slsInitialize === 'function') {
await app.slsInitialize()
}
context.callbackWaitsForEmptyEventLoop = app.callbackWaitsForEmptyEventLoop === true

const result = await proxy(server, event, context, 'PROMISE')
return result.promise
const { promise } = await proxy(server, event, context, 'PROMISE')
return promise
}
1 change: 1 addition & 0 deletions src/_shims/sls.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ async function createServer() {
// if includes, will return base64 encoded, very useful for images
server.binaryTypes = ['*/*']

// 返回 server
return server
}

Expand Down
157 changes: 141 additions & 16 deletions src/config.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,139 @@
Object.defineProperty(exports, '__esModule', { value: true })
exports.getConfig = void 0
const fs = require('fs')
const path = require('path')
const YAML = require('js-yaml')
const TEMPLATE_BASE_URL = 'https://serverless-templates-1300862921.cos.ap-beijing.myqcloud.com'
const frameworks = {
express: {
injectSlsSdk: true,
runtime: 'Nodejs10.15',
defaultEntryFile: 'sls.js',
defaultStatics: [{ src: 'public', targetDir: '/' }]
},
koa: {
injectSlsSdk: true,
runtime: 'Nodejs10.15',
defaultEntryFile: 'sls.js',
defaultStatics: [{ src: 'public', targetDir: '/' }]
},
egg: {
injectSlsSdk: true,
runtime: 'Nodejs10.15',
defaultEntryFile: 'sls.js',
defaultStatics: [{ src: 'public', targetDir: '/' }],
defaultEnvs: [
{
key: 'SERVERLESS',
value: '1'
},
{
key: 'EGG_APP_CONFIG',
value: '{"rundir":"/tmp","logger":{"dir":"/tmp"}}'
}
]
},
nestjs: {
injectSlsSdk: true,
runtime: 'Nodejs10.15',
defaultEntryFile: 'sls.js',
defaultStatics: [{ src: 'public', targetDir: '/' }]
},
nextjs: {
injectSlsSdk: true,
runtime: 'Nodejs10.15',
defaultEntryFile: 'sls.js',
defaultStatics: [
{ src: '.next/static', targetDir: '/_next/static' },
{ src: 'public', targetDir: '/' }
]
},
nuxtjs: {
injectSlsSdk: true,
runtime: 'Nodejs10.15',
defaultEntryFile: 'sls.js',
defaultStatics: [
{ src: '.nuxt/dist/client', targetDir: '/' },
{ src: 'static', targetDir: '/' }
]
},
laravel: {
injectSlsSdk: false,
runtime: 'Php7',
defaultEnvs: [
{
key: 'SERVERLESS',
value: '1'
},
{
key: 'VIEW_COMPILED_PATH',
value: '/tmp/storage/framework/views'
},
{
key: 'SESSION_DRIVER',
value: 'array'
},
{
key: 'LOG_CHANNEL',
value: 'stderr'
},
{
key: 'APP_STORAGE',
value: '/tmp/storage'
}
]
},
thinkphp: {
injectSlsSdk: false,
runtime: 'Php7'
},
flask: {
injectSlsSdk: false,
runtime: 'Python3.6'
},
django: {
injectSlsSdk: false,
runtime: 'Python3.6'
}
}
const CONFIGS = {
templateUrl:
'https://serverless-templates-1300862921.cos.ap-beijing.myqcloud.com/nuxtjs-demo.zip',
compName: 'nuxtjs',
compFullname: 'Nuxt.js',
defaultEntryFile: 'sls.js',
// support metrics frameworks
pythonFrameworks: ['flask', 'django'],
supportMetrics: ['express', 'next', 'nuxt'],
region: 'ap-guangzhou',
description: 'Created by Serverless Component',
handler: 'sl_handler.handler',
runtime: 'Nodejs10.15',
timeout: 3,
timeout: 10,
memorySize: 128,
namespace: 'default',
description: 'Created by Serverless Component',
defaultStatics: [
{ src: '.nuxt/dist/client', targetDir: '/' },
{ src: 'static', targetDir: '/' }
defaultEnvs: [
{
key: 'SERVERLESS',
value: '1'
}
],
defaultCdnConf: {
autoRefresh: true,
cos: {
lifecycle: [
{
status: 'Enabled',
id: 'deleteObject',
expiration: { days: '10' },
abortIncompleteMultipartUpload: { daysAfterInitiation: '10' }
}
]
},
cdn: {
forceRedirect: {
switch: 'on',
redirectType: 'https',
redirectStatusCode: 301
},
https: {
switch: 'on',
http2: 'on'
}
},
defaultCdnConfig: {
forceRedirect: {
switch: 'on',
redirectType: 'https',
Expand Down Expand Up @@ -47,12 +164,20 @@ const CONFIGS = {
'name/cos:HeadObject',
'name/cos:OptionsObject'
],
Resource: [`qcs::cos:${region}:uid/${appid}:${bucket}-${appid}/*`]
Resource: [`qcs::cos:${region}:uid/${appid}:${bucket}/*`]
}
],
version: '2.0'
}
}
}

module.exports = CONFIGS
const getConfig = () => {
const { name: framework } = YAML.load(
// framework.yml 会在组件部署流程中动态生成
fs.readFileSync(path.join(__dirname, 'framework.yml'), 'utf-8')
)
const templateUrl = `${TEMPLATE_BASE_URL}/${framework}-demo.zip`
const frameworkConfigs = frameworks[framework]
return Object.assign(Object.assign({ framework, templateUrl }, CONFIGS), frameworkConfigs)
}
exports.getConfig = getConfig
Loading

0 comments on commit 1bc94d8

Please sign in to comment.