Skip to content

Commit

Permalink
fix: support for NodeJS 15, fixes #732
Browse files Browse the repository at this point in the history
  • Loading branch information
harttle committed Aug 16, 2024
1 parent 22ba21d commit 4548c11
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 16 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ jobs:
- os: ubuntu-latest
timezone: Etc/GMT
node-version: 16
- os: ubuntu-latest
timezone: Asia/Shanghai
node-version: 15
- os: ubuntu-latest
timezone: Asia/Shanghai
node-version: 14
Expand Down
7 changes: 0 additions & 7 deletions rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,6 @@ const browserStream = {
delimiters: ['', ''],
'./streamed-emitter': '../build/streamed-emitter-browser'
}
const browserPerf = {
include: ['./src/context/context.ts', './src/render/render.ts'],
'node:perf_hooks': '../build/perf_hooks-browser'
}
const esmRequire = {
include: './src/fs/node.ts',
delimiters: ['', ''],
Expand Down Expand Up @@ -99,7 +95,6 @@ const browserEsm = {
versionInjection,
replace(browserFS),
replace(browserStream),
replace(browserPerf),
typescript(tsconfig('es6'))
],
treeshake,
Expand All @@ -118,7 +113,6 @@ const browserUmd = {
versionInjection,
replace(browserFS),
replace(browserStream),
replace(browserPerf),
typescript(tsconfig('es5'))
],
treeshake,
Expand All @@ -137,7 +131,6 @@ const browserMin = {
versionInjection,
replace(browserFS),
replace(browserStream),
replace(browserPerf),
typescript(tsconfig('es5')),
uglify()
],
Expand Down
5 changes: 0 additions & 5 deletions src/build/perf_hooks-browser.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/context/context.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { performance } from 'node:perf_hooks'
import { getPerformance } from '../util/performance'
import { Drop } from '../drop/drop'
import { __assign } from 'tslib'
import { NormalizedFullOptions, defaultOptions, RenderOptions } from '../liquid-options'
Expand Down Expand Up @@ -44,7 +44,7 @@ export class Context {
this.strictVariables = renderOptions.strictVariables ?? this.opts.strictVariables
this.ownPropertyOnly = renderOptions.ownPropertyOnly ?? opts.ownPropertyOnly
this.memoryLimit = memoryLimit ?? new Limiter('memory alloc', renderOptions.memoryLimit ?? opts.memoryLimit)
this.renderLimit = renderLimit ?? new Limiter('template render', performance.now() + (renderOptions.renderLimit ?? opts.renderLimit))
this.renderLimit = renderLimit ?? new Limiter('template render', getPerformance().now() + (renderOptions.renderLimit ?? opts.renderLimit))
}
public getRegister (key: string) {
return (this.registers[key] = this.registers[key] || {})
Expand Down
4 changes: 2 additions & 2 deletions src/render/render.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { performance } from 'node:perf_hooks'
import { getPerformance } from '../util/performance'
import { toPromise, RenderError, LiquidErrors, LiquidError } from '../util'
import { Context } from '../context'
import { Template } from '../template'
Expand All @@ -17,7 +17,7 @@ export class Render {
}
const errors = []
for (const tpl of templates) {
ctx.renderLimit.check(performance.now())
ctx.renderLimit.check(getPerformance().now())
try {
// if tpl.render supports emitter, it'll return empty `html`
const html = yield tpl.render(ctx, emitter)
Expand Down
28 changes: 28 additions & 0 deletions src/util/performance.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { getPerformance } from './performance'

describe('performance', () => {
let globalPerformance: Performance
beforeEach(() => {
globalPerformance = global.performance
})
afterEach(() => {
global.performance = globalPerformance
delete (global as any).window
})
it('should use global.performance if exist', () => {
const performance = {} as any as Performance
global.performance = performance
expect(getPerformance()).toEqual(performance)
})
it('should use window.performance if exist', () => {
const performance = {} as any as Performance
delete (global as any).performance
global.window = { performance } as any
expect(getPerformance()).toEqual(performance)
})
it('should use polyfill if no window/global.performance', () => {
delete (global as any).performance
const now = getPerformance().now()
expect(Number.isInteger(now)).toBeTruthy()
})
})
13 changes: 13 additions & 0 deletions src/util/performance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
interface LiquidPerformance {
now: () => number
}

const polyfill: LiquidPerformance = {
now: () => Date.now()
}

export function getPerformance (): LiquidPerformance {
return (typeof global === 'object' && global.performance) ||
(typeof window === 'object' && window.performance) ||
polyfill
}

0 comments on commit 4548c11

Please sign in to comment.