Skip to content

Commit

Permalink
docs: update usage
Browse files Browse the repository at this point in the history
  • Loading branch information
yukinotech committed Aug 10, 2023
1 parent 27f1b4a commit 545aa05
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 92 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,4 @@ const main = async()=>{

- optimizing CLI interaction
- auto find and analysis tsconfig
- support nodejs commonjs and esm
- support nodejs esm
91 changes: 0 additions & 91 deletions test/commonjs-project/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,94 +5,3 @@ const fs = require('node-fs')

const binding = require('./dist/crc')
const binding = require('./dist')

const raw = {
crc64: binding._crc64,
strToUint64Ptr: binding._str_to_uint64,
uint64PtrToStr: binding._uint64_to_str,
}

binding._crc64_init()

function strToUint64Ptr(str) {
const strPtr = binding._malloc(str.length + 1)
binding.stringToUTF8(str, strPtr, str.length + 1)

const uint64Ptr = binding._malloc(8)
raw.strToUint64Ptr(strPtr, uint64Ptr)
binding._free(strPtr)

return uint64Ptr
}

function uint64PtrToStr(uint64Ptr) {
const strPtr = binding._malloc(32)
raw.uint64PtrToStr(strPtr, uint64Ptr)
const str = binding.UTF8ToString(strPtr)
binding._free(strPtr)
return str
}

function buffToPtr(buff) {
if (typeof buff === 'string') {
buff = Buffer.from(buff)
} else if (!Buffer.isBuffer(buff)) {
throw new Error('Invalid buffer type.')
}

const buffPtr = binding._malloc(buff.length)
binding.writeArrayToMemory(buff, buffPtr)

return buffPtr
}

module.exports.crc64 = function (buff, prev) {
if (!prev) prev = '0'
if (typeof prev !== 'string' || !/\d+/.test(prev)) {
throw new Error('Invlid previous value.')
}

const prevPtr = strToUint64Ptr(prev)
const buffPtr = buffToPtr(buff)

raw.crc64(prevPtr, buffPtr, buff.length)
const ret = uint64PtrToStr(prevPtr)

binding._free(prevPtr)
binding._free(buffPtr)

return ret
}

module.exports.crc64File = function (filename, callback) {
let errored = false
const stream = fs.createReadStream(filename)
const crcPtr = strToUint64Ptr('0')
let crcPtrFreed = false
stream.on('error', function (err) {
errored = true
stream.destroy()
if (!crcPtrFreed) {
binding._free(crcPtr)
crcPtrFreed = true
}
return callback(err)
})

stream.on('data', function (chunk) {
const buffPtr = buffToPtr(chunk)
raw.crc64(crcPtr, buffPtr, chunk.length)
binding._free(buffPtr)
})
stream.on('end', function () {
if (errored) return

const ret = uint64PtrToStr(crcPtr)
if (!crcPtrFreed) {
binding._free(crcPtr)
crcPtrFreed = true
}

return callback(undefined, ret)
})
}

0 comments on commit 545aa05

Please sign in to comment.