UNPKG

@bufbuild/cel

Version:

A CEL evaluator for ECMAScript

165 lines (164 loc) 9.06 kB
"use strict"; // Copyright 2024-2025 Buf Technologies, Inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. Object.defineProperty(exports, "__esModule", { value: true }); exports.addTime = addTime; const wkt_1 = require("@bufbuild/protobuf/wkt"); const type_js_1 = require("../type.js"); const func_js_1 = require("../func.js"); const olc = require("../gen/dev/cel/expr/overload_const.js"); const protobuf_1 = require("@bufbuild/protobuf"); function addTime(funcs) { funcs.add(getFullYearFunc); funcs.add(getMonthFunc); funcs.add(getDateFunc); funcs.add(getDayOfWeekFunc); funcs.add(getDayOfMonthFunc); funcs.add(getDayOfYearFunc); funcs.add(getSecondsFunc); funcs.add(getMinutesFunc); funcs.add(getHoursFunc); funcs.add(getMillisecondsFunc); } function getDayOfYear(date) { const start = new Date(0, 0, 1); start.setFullYear(date.getFullYear()); const diff = date.getTime() - start.getTime(); const oneDay = 1000 * 60 * 60 * 24; return Math.floor(diff / oneDay); } function makeTimeOp(t) { return (msg, tz) => { const ts = msg.message; let val = (0, wkt_1.timestampDate)(ts); if (tz !== undefined) { // Timezone can either be Fixed or IANA or "UTC". // We first check for the fixed offset case. // // Ref: https://github.com/google/cel-spec/blob/master/doc/langdef.md#timezones const timeOffset = tz.match(/^(?<sign>[+-]?)(?<hours>\d\d):(?<minutes>\d\d)$/); if (timeOffset?.groups) { const sign = timeOffset.groups.sign == "-" ? 1 : -1; const hours = parseInt(timeOffset.groups.hours); const minutes = parseInt(timeOffset.groups.minutes); const offset = sign * (hours * 60 * 60 * 1000 + minutes * 60 * 1000); val = new Date(val.getTime() - offset); val = new Date(val.getUTCFullYear(), val.getUTCMonth(), val.getUTCDate(), val.getUTCHours(), val.getUTCMinutes(), val.getUTCSeconds(), val.getUTCMilliseconds()); } else { // Must be an IANA timezone, so we use the Intl API to format the string // in the desired timezone and extract the parts from that. // // The APIs are part of baseline 2020. const format = new Intl.DateTimeFormat("en-US", { hourCycle: "h23", hour12: false, timeZone: tz, year: "numeric", month: "numeric", day: "2-digit", hour: "2-digit", minute: "2-digit", second: "2-digit", }); let year, month, day, hour, minute, second; for (const part of format.formatToParts(val)) { switch (part.type) { case "year": year = parseInt(part.value); break; case "month": month = parseInt(part.value) - 1; break; case "day": day = parseInt(part.value); break; case "hour": hour = parseInt(part.value); break; case "minute": minute = parseInt(part.value); break; case "second": second = parseInt(part.value); break; } } if (year === undefined || month === undefined || day === undefined || hour === undefined || minute === undefined || second === undefined) { throw new Error(`Error converting ${(0, protobuf_1.toJson)(wkt_1.TimestampSchema, ts)} to IANA timezone ${tz}`); } val = new Date(year, month, day, hour, minute, second, val.getUTCMilliseconds()); } } else { val = new Date(val.getUTCFullYear(), val.getUTCMonth(), val.getUTCDate(), val.getUTCHours(), val.getUTCMinutes(), val.getUTCSeconds(), val.getUTCMilliseconds()); } const result = t(val); try { return BigInt(result); } catch (_e) { throw new Error(`Error converting ${result} of ${String(val)} of ${(0, protobuf_1.toJson)(wkt_1.TimestampSchema, ts)} to BigInt`); } }; } const getFullYearFunc = (0, func_js_1.celFunc)(olc.TIME_GET_FULL_YEAR, [ (0, func_js_1.celOverload)([type_js_1.TIMESTAMP], type_js_1.CelScalar.INT, makeTimeOp((d) => d.getFullYear())), (0, func_js_1.celOverload)([type_js_1.TIMESTAMP, type_js_1.CelScalar.STRING], type_js_1.CelScalar.INT, makeTimeOp((d) => d.getFullYear())), ]); const getMonthFunc = (0, func_js_1.celFunc)(olc.TIME_GET_MONTH, [ (0, func_js_1.celOverload)([type_js_1.TIMESTAMP], type_js_1.CelScalar.INT, makeTimeOp((d) => d.getMonth())), (0, func_js_1.celOverload)([type_js_1.TIMESTAMP, type_js_1.CelScalar.STRING], type_js_1.CelScalar.INT, makeTimeOp((d) => d.getMonth())), ]); const getDateFunc = (0, func_js_1.celFunc)(olc.TIME_GET_DATE, [ (0, func_js_1.celOverload)([type_js_1.TIMESTAMP], type_js_1.CelScalar.INT, makeTimeOp((d) => d.getDate())), (0, func_js_1.celOverload)([type_js_1.TIMESTAMP, type_js_1.CelScalar.STRING], type_js_1.CelScalar.INT, makeTimeOp((d) => d.getDate())), ]); const getDayOfMonthFunc = (0, func_js_1.celFunc)(olc.TIME_GET_DAY_OF_MONTH, [ (0, func_js_1.celOverload)([type_js_1.TIMESTAMP], type_js_1.CelScalar.INT, makeTimeOp((d) => d.getDate() - 1)), (0, func_js_1.celOverload)([type_js_1.TIMESTAMP, type_js_1.CelScalar.STRING], type_js_1.CelScalar.INT, makeTimeOp((d) => d.getDate() - 1)), ]); const getDayOfWeekFunc = (0, func_js_1.celFunc)(olc.TIME_GET_DAY_OF_WEEK, [ (0, func_js_1.celOverload)([type_js_1.TIMESTAMP], type_js_1.CelScalar.INT, makeTimeOp((d) => d.getDay())), (0, func_js_1.celOverload)([type_js_1.TIMESTAMP, type_js_1.CelScalar.STRING], type_js_1.CelScalar.INT, makeTimeOp((d) => d.getDay())), ]); const getDayOfYearFunc = (0, func_js_1.celFunc)(olc.TIME_GET_DAY_OF_YEAR, [ (0, func_js_1.celOverload)([type_js_1.TIMESTAMP], type_js_1.CelScalar.INT, makeTimeOp((d) => getDayOfYear(d))), (0, func_js_1.celOverload)([type_js_1.TIMESTAMP, type_js_1.CelScalar.STRING], type_js_1.CelScalar.INT, makeTimeOp((d) => getDayOfYear(d))), ]); const getSecondsFunc = (0, func_js_1.celFunc)(olc.TIME_GET_SECONDS, [ (0, func_js_1.celOverload)([type_js_1.TIMESTAMP], type_js_1.CelScalar.INT, makeTimeOp((d) => d.getSeconds())), (0, func_js_1.celOverload)([type_js_1.TIMESTAMP, type_js_1.CelScalar.STRING], type_js_1.CelScalar.INT, makeTimeOp((d) => d.getSeconds())), (0, func_js_1.celOverload)([type_js_1.DURATION], type_js_1.CelScalar.INT, (dur) => dur.message.seconds), ]); const getMinutesFunc = (0, func_js_1.celFunc)(olc.TIME_GET_MINUTES, [ (0, func_js_1.celOverload)([type_js_1.TIMESTAMP], type_js_1.CelScalar.INT, makeTimeOp((d) => d.getMinutes())), (0, func_js_1.celOverload)([type_js_1.TIMESTAMP, type_js_1.CelScalar.STRING], type_js_1.CelScalar.INT, makeTimeOp((d) => d.getMinutes())), (0, func_js_1.celOverload)([type_js_1.DURATION], type_js_1.CelScalar.INT, (dur) => dur.message.seconds / 60n), ]); const getHoursFunc = (0, func_js_1.celFunc)(olc.TIME_GET_HOURS, [ (0, func_js_1.celOverload)([type_js_1.TIMESTAMP], type_js_1.CelScalar.INT, makeTimeOp((d) => d.getHours())), (0, func_js_1.celOverload)([type_js_1.TIMESTAMP, type_js_1.CelScalar.STRING], type_js_1.CelScalar.INT, makeTimeOp((d) => d.getHours())), (0, func_js_1.celOverload)([type_js_1.DURATION], type_js_1.CelScalar.INT, (dur) => dur.message.seconds / 3600n), ]); const getMillisecondsFunc = (0, func_js_1.celFunc)(olc.TIME_GET_MILLISECONDS, [ (0, func_js_1.celOverload)([type_js_1.TIMESTAMP], type_js_1.CelScalar.INT, makeTimeOp((d) => d.getMilliseconds())), (0, func_js_1.celOverload)([type_js_1.TIMESTAMP, type_js_1.CelScalar.STRING], type_js_1.CelScalar.INT, makeTimeOp((d) => d.getMilliseconds())), (0, func_js_1.celOverload)([type_js_1.DURATION], type_js_1.CelScalar.INT, (dur) => BigInt(dur.message.nanos) / 1000000n), ]);