UNPKG

wakaq

Version:

Background task queue for Node backed by Redis, a super minimal Celery

88 lines (87 loc) 2.51 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.deserialize = exports.serialize = void 0; const ts_duration_1 = require("ts-duration"); function replacer(key, v) { const orig = this[key]; switch (typeof orig) { case 'bigint': return { __class__: 'BigInt', value: orig.toString(), }; case 'boolean': return v; case 'number': if (orig === Infinity) { return { __class__: 'Infinity', }; } else if (orig === -Infinity) { return { __class__: '-Infinity', }; } else if (isNaN(orig)) { return { __class__: 'NaN', }; } return v; case 'object': if (orig instanceof Date) { return JSON.stringify({ __class__: 'Date', iso: orig.toISOString(), }); } else if (orig instanceof ts_duration_1.Duration) { return JSON.stringify({ __class__: 'Duration', nanoseconds: orig.nanoseconds, }); } return v; case 'string': return v; case 'undefined': return { __class__: 'undefined', }; } return v; } function reviver(key, value) { if (typeof value === 'object') { if (typeof value.__type__ !== 'string') return value; switch (value.__type__) { case 'NaN': return NaN; case 'Infinity': return Infinity; case '-Infinity': return -Infinity; case 'undefined': return undefined; case 'BigInt': return BigInt(value.value); case 'Date': return new Date(Date.parse(value.iso)); case 'Duration': return ts_duration_1.Duration.nanosecond(value); default: return value; } } return value; } const serialize = (obj) => { return JSON.stringify(obj, replacer); }; exports.serialize = serialize; const deserialize = (jsonStr) => { return JSON.parse(jsonStr, reviver); }; exports.deserialize = deserialize;