Skip to content

Commit

Permalink
fix: add fromArrayBufferToBuffer util
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub committed Dec 12, 2022
1 parent e515f5f commit 754163a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
19 changes: 19 additions & 0 deletions src/main/js/util.js
Expand Up @@ -191,3 +191,22 @@ export const time = (fn, label = fn.name) => async (...args) => {
export const setFnName = (fn, name) => Object.defineProperty(fn, 'name', { value: name })

export const jsonBuffer = (v) => Buffer.from(JSON.stringify(v))


export const fromBufferToArrayBuffer = (buf) => {
const ab = new ArrayBuffer(buf.length)
const view = new Uint8Array(ab)
for (let i = 0; i < buf.length; ++i) {
view[i] = buf[i]
}
return ab
}

export const fromArrayBufferToBuffer = (ab) => {
const buf = Buffer.alloc(ab.byteLength)
const view = new Uint8Array(ab)
for (let i = 0; i < buf.length; ++i) {
buf[i] = view[i]
}
return buf
}
9 changes: 5 additions & 4 deletions src/main/js/zip.js
@@ -1,8 +1,9 @@
import { promisify } from 'node:util'
import { getConfig } from './config.js'
import {fromArrayBufferToBuffer} from "./util.js";

export const gzip = async(...args) =>
promisify((await import(getConfig()?.zlib || 'node:zlib')).gzip)(...args).then(Buffer.from)
export const gzip = async(buf) =>
promisify((await import(getConfig()?.zlib || 'node:zlib')).gzip)(new Uint8Array(buf)).then(fromArrayBufferToBuffer)

export const gunzip = async(...args) =>
promisify((await import(getConfig()?.zlib || 'node:zlib')).gunzip)(...args).then(Buffer.from)
export const gunzip = async(buf) =>
promisify((await import(getConfig()?.zlib || 'node:zlib')).gunzip)(new Uint8Array(buf)).then(fromArrayBufferToBuffer)

0 comments on commit 754163a

Please sign in to comment.