Skip to content

Commit

Permalink
feat: better process polyfill
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Nov 13, 2020
1 parent 3049212 commit f06a437
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
7 changes: 5 additions & 2 deletions src/rollup/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,16 @@ export const getRollupConfig = (config: SLSOptions) => {
if (config.node === false) {
// Globals
injects.Buffer = ['buffer', 'Buffer']
injects.process = '~mocks/node/process'

// Aliases
Object.assign(aliases, {
// Node
...mapArrToVal('~mocks/generic', Module.builtinModules),
http: '~mocks/node/http',
fs: '~mocks/node/fs',
process: '~mocks/node/process',
'node-process': require.resolve('process/browser.js'),
buffer: require.resolve('buffer/index.js'),
util: require.resolve('util/util.js'),
events: require.resolve('events/events.js'),
Expand Down Expand Up @@ -97,9 +100,9 @@ export const getRollupConfig = (config: SLSOptions) => {
}

if (config.logStartup) {
options.output.intro += 'global._startTime = process.hrtime();'
options.output.intro += 'global._startTime = global.process.hrtime();'
// eslint-disable-next-line no-template-curly-in-string
options.output.outro += 'global._endTime = process.hrtime(global._startTime); global._coldstart = ((global._endTime[0] * 1e9) + global._endTime[1]) / 1e6; console.log(`λ Cold start took: ${global._coldstart}ms`);'
options.output.outro += 'global._endTime = global.process.hrtime(global._startTime); global._coldstart = ((global._endTime[0] * 1e9) + global._endTime[1]) / 1e6; console.log(`λ Cold start took: ${global._coldstart}ms`);'
}

// https://github.com/rollup/plugins/tree/master/packages/replace
Expand Down
5 changes: 2 additions & 3 deletions src/targets/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ import { SLSTarget } from '../config'
const polyfill = `
const exports = {};
const module = { exports };
const process = { env: {} };
const global = { process };
const global = { process: {} };
const window = global;
(function(){const o=Date.now(),t=()=>Date.now()-o;process.hrtime=o=>{const e=Math.floor(.001*(Date.now()-t())),n=.001*t();let a=Math.floor(n)+e,r=Math.floor(n%1*1e9);return o&&(a-=o[0],r-=o[1],r<0&&(a--,r+=1e9)),[a,r]};})();
(function(){const o=Date.now(),t=()=>Date.now()-o;global.process.hrtime=o=>{const e=Math.floor(.001*(Date.now()-t())),n=.001*t();let a=Math.floor(n)+e,r=Math.floor(n%1*1e9);return o&&(a-=o[0],r-=o[1],r<0&&(a--,r+=1e9)),[a,r]};})();
`

export const worker: SLSTarget = {
Expand Down

0 comments on commit f06a437

Please sign in to comment.