From 4548c1140629bf270d163a403c2994d785c7f710 Mon Sep 17 00:00:00 2001 From: Yang Jun Date: Fri, 16 Aug 2024 23:23:07 +0800 Subject: [PATCH] fix: support for NodeJS 15, fixes #732 --- .github/workflows/test.yml | 3 +++ rollup.config.mjs | 7 ------- src/build/perf_hooks-browser.ts | 5 ----- src/context/context.ts | 4 ++-- src/render/render.ts | 4 ++-- src/util/performance.spec.ts | 28 ++++++++++++++++++++++++++++ src/util/performance.ts | 13 +++++++++++++ 7 files changed, 48 insertions(+), 16 deletions(-) delete mode 100644 src/build/perf_hooks-browser.ts create mode 100644 src/util/performance.spec.ts create mode 100644 src/util/performance.ts diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 8c3ef8f97b..d4583987e8 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -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 diff --git a/rollup.config.mjs b/rollup.config.mjs index 7c32fa4951..87b6edbce1 100644 --- a/rollup.config.mjs +++ b/rollup.config.mjs @@ -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: ['', ''], @@ -99,7 +95,6 @@ const browserEsm = { versionInjection, replace(browserFS), replace(browserStream), - replace(browserPerf), typescript(tsconfig('es6')) ], treeshake, @@ -118,7 +113,6 @@ const browserUmd = { versionInjection, replace(browserFS), replace(browserStream), - replace(browserPerf), typescript(tsconfig('es5')) ], treeshake, @@ -137,7 +131,6 @@ const browserMin = { versionInjection, replace(browserFS), replace(browserStream), - replace(browserPerf), typescript(tsconfig('es5')), uglify() ], diff --git a/src/build/perf_hooks-browser.ts b/src/build/perf_hooks-browser.ts deleted file mode 100644 index d68935436d..0000000000 --- a/src/build/perf_hooks-browser.ts +++ /dev/null @@ -1,5 +0,0 @@ -const polyfill = { - now: () => Date.now() -} - -export const performance = typeof window === 'object' && window.performance || polyfill diff --git a/src/context/context.ts b/src/context/context.ts index f12bcfff2e..94707089f4 100644 --- a/src/context/context.ts +++ b/src/context/context.ts @@ -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' @@ -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] || {}) diff --git a/src/render/render.ts b/src/render/render.ts index cb4f17db32..bebc8f366b 100644 --- a/src/render/render.ts +++ b/src/render/render.ts @@ -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' @@ -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) diff --git a/src/util/performance.spec.ts b/src/util/performance.spec.ts new file mode 100644 index 0000000000..a6525a354d --- /dev/null +++ b/src/util/performance.spec.ts @@ -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() + }) +}) diff --git a/src/util/performance.ts b/src/util/performance.ts new file mode 100644 index 0000000000..9c2d802ed8 --- /dev/null +++ b/src/util/performance.ts @@ -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 +}