owlservable 0.2.6 → 0.2.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/index.cjs +24 -1
  2. package/index.js +24 -1
  3. package/package.json +1 -1
package/index.cjs CHANGED
@@ -4,6 +4,7 @@ const http = require('node:http')
4
4
  const https = require('node:https')
5
5
  const fs = require('node:fs')
6
6
  const path = require('node:path')
7
+ const zlib = require('node:zlib')
7
8
 
8
9
  const __dir = __dirname
9
10
 
@@ -105,6 +106,19 @@ function bodyFromInit(b) {
105
106
  return null
106
107
  }
107
108
 
109
+ function decompressBuffer(buf, encoding) {
110
+ return new Promise((resolve, reject) => {
111
+ if (encoding === 'gzip' || encoding === 'x-gzip')
112
+ zlib.gunzip(buf, (e, r) => e ? reject(e) : resolve(r.toString('utf8')))
113
+ else if (encoding === 'br')
114
+ zlib.brotliDecompress(buf, (e, r) => e ? reject(e) : resolve(r.toString('utf8')))
115
+ else if (encoding === 'deflate')
116
+ zlib.inflate(buf, (e, r) => e ? reject(e) : resolve(r.toString('utf8')))
117
+ else
118
+ resolve(buf.toString('utf8'))
119
+ })
120
+ }
121
+
108
122
  let patched = false
109
123
 
110
124
  function handleJsonResponse(record, getText) {
@@ -204,9 +218,10 @@ function patchHttpModule(mod, protocol) {
204
218
  if (r && isJson) {
205
219
  try {
206
220
  const chunks = []; let size = 0
221
+ const enc = res.headers['content-encoding'] || ''
207
222
  res.on('data', chunk => { try { if (size < MAX_PARSE) { chunks.push(chunk); size += chunk.length } } catch (_) {} })
208
223
  const getText = () => new Promise((resolve, reject) => {
209
- res.once('end', () => { try { resolve(Buffer.concat(chunks).toString('utf8')) } catch (e) { reject(e) } })
224
+ res.once('end', () => { try { decompressBuffer(Buffer.concat(chunks), enc).then(resolve).catch(reject) } catch (e) { reject(e) } })
210
225
  res.once('error', reject)
211
226
  })
212
227
  if (isJson) handleJsonResponse(r, getText)
@@ -266,6 +281,14 @@ function enableSave(retentionMs) {
266
281
  try { fs.mkdirSync(dir, { recursive: true }) } catch (_) {}
267
282
  try { const gi = path.join(dir, '.gitignore'); if (!fs.existsSync(gi)) fs.writeFileSync(gi, '*\n') } catch (_) {}
268
283
  try { fs.writeFileSync(path.join(dir, 'config.json'), JSON.stringify({ save: true, retention: retentionMs })) } catch (_) {}
284
+
285
+ if (saveState.enabled) {
286
+ saveState.retention = retentionMs
287
+ pruneFile()
288
+ emitter.emit('saveConfig', getSaveInfo())
289
+ return
290
+ }
291
+
269
292
  saveState.enabled = true
270
293
  saveState.filePath = filePath
271
294
  saveState.retention = retentionMs
package/index.js CHANGED
@@ -4,6 +4,7 @@ import * as http from 'node:http'
4
4
  import * as https from 'node:https'
5
5
  import * as fs from 'node:fs'
6
6
  import * as path from 'node:path'
7
+ import * as zlib from 'node:zlib'
7
8
 
8
9
  const __dir = path.dirname(fileURLToPath(import.meta.url))
9
10
 
@@ -105,6 +106,19 @@ function bodyFromInit(b) {
105
106
  return null
106
107
  }
107
108
 
109
+ function decompressBuffer(buf, encoding) {
110
+ return new Promise((resolve, reject) => {
111
+ if (encoding === 'gzip' || encoding === 'x-gzip')
112
+ zlib.gunzip(buf, (e, r) => e ? reject(e) : resolve(r.toString('utf8')))
113
+ else if (encoding === 'br')
114
+ zlib.brotliDecompress(buf, (e, r) => e ? reject(e) : resolve(r.toString('utf8')))
115
+ else if (encoding === 'deflate')
116
+ zlib.inflate(buf, (e, r) => e ? reject(e) : resolve(r.toString('utf8')))
117
+ else
118
+ resolve(buf.toString('utf8'))
119
+ })
120
+ }
121
+
108
122
  let patched = false
109
123
 
110
124
  function handleJsonResponse(record, getText) {
@@ -204,9 +218,10 @@ function patchHttpModule(mod, protocol) {
204
218
  if (r && isJson) {
205
219
  try {
206
220
  const chunks = []; let size = 0
221
+ const enc = res.headers['content-encoding'] || ''
207
222
  res.on('data', chunk => { try { if (size < MAX_PARSE) { chunks.push(chunk); size += chunk.length } } catch (_) {} })
208
223
  const getText = () => new Promise((resolve, reject) => {
209
- res.once('end', () => { try { resolve(Buffer.concat(chunks).toString('utf8')) } catch (e) { reject(e) } })
224
+ res.once('end', () => { try { decompressBuffer(Buffer.concat(chunks), enc).then(resolve).catch(reject) } catch (e) { reject(e) } })
210
225
  res.once('error', reject)
211
226
  })
212
227
  if (isJson) handleJsonResponse(r, getText)
@@ -266,6 +281,14 @@ function enableSave(retentionMs) {
266
281
  try { fs.mkdirSync(dir, { recursive: true }) } catch (_) {}
267
282
  try { const gi = path.join(dir, '.gitignore'); if (!fs.existsSync(gi)) fs.writeFileSync(gi, '*\n') } catch (_) {}
268
283
  try { fs.writeFileSync(path.join(dir, 'config.json'), JSON.stringify({ save: true, retention: retentionMs })) } catch (_) {}
284
+
285
+ if (saveState.enabled) {
286
+ saveState.retention = retentionMs
287
+ pruneFile()
288
+ emitter.emit('saveConfig', getSaveInfo())
289
+ return
290
+ }
291
+
269
292
  saveState.enabled = true
270
293
  saveState.filePath = filePath
271
294
  saveState.retention = retentionMs
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "owlservable",
3
- "version": "0.2.6",
3
+ "version": "0.2.7",
4
4
  "description": "Minimalist Observability Platform. Zero config, zero dependencies.",
5
5
  "type": "module",
6
6
  "main": "index.js",