-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #49 from VirgilSecurity/v4.0.0-alpha.2
v4.0.0-alpha.2
- Loading branch information
Showing
34 changed files
with
273 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,5 @@ | ||
import { dataToUint8Array } from './utils'; | ||
import { Buffer as NodeBuffer } from '@virgilsecurity/data-utils'; | ||
|
||
export const DATA_SIGNATURE_KEY = dataToUint8Array({ | ||
value: 'VIRGIL-DATA-SIGNATURE', | ||
encoding: 'utf8', | ||
}); | ||
export const DATA_SIGNATURE_KEY = NodeBuffer.from('VIRGIL-DATA-SIGNATURE', 'utf8'); | ||
|
||
export const DATA_SIGNER_ID_KEY = dataToUint8Array({ | ||
value: 'VIRGIL-DATA-SIGNER-ID', | ||
encoding: 'utf8', | ||
}); | ||
export const DATA_SIGNER_ID_KEY = NodeBuffer.from('VIRGIL-DATA-SIGNER-ID', 'utf8'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,3 @@ | ||
import { Buffer as NodeBuffer } from 'buffer'; | ||
|
||
import { StringEncoding, Data } from './types'; | ||
|
||
export const dataToUint8Array = ( | ||
data: Data, | ||
defaultEncoding?: keyof typeof StringEncoding, | ||
): Uint8Array => { | ||
if (typeof data === 'string') { | ||
return NodeBuffer.from(data, defaultEncoding); | ||
} | ||
if (data instanceof Uint8Array) { | ||
return data; | ||
} | ||
if ( | ||
typeof data === 'object' && | ||
typeof data.value === 'string' && | ||
typeof StringEncoding[data.encoding] !== 'undefined' | ||
) { | ||
return NodeBuffer.from(data.value, data.encoding); | ||
} | ||
throw new TypeError('Invalid format of Data'); | ||
}; | ||
|
||
export const toArray = <T>(val?: T | T[]): T[] => { | ||
return val == null ? [] : Array.isArray(val) ? val : [val]; | ||
}; | ||
|
||
export const toBuffer = (array: Uint8Array) => NodeBuffer.from(array.buffer); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
{ | ||
"name": "@virgilsecurity/data-utils", | ||
"version": "0.1.0", | ||
"description": "Library that contains different functions / classes that are used for data manipulation in different Virgil Security libraries.", | ||
"main": "./dist/node.cjs.js", | ||
"module": "./dist/node.es.js", | ||
"browser": { | ||
"./dist/node.cjs.js": "./dist/browser.cjs.js", | ||
"./dist/node.es.js": "./dist/browser.es.js" | ||
}, | ||
"typings": "./dist/types/node.d.ts", | ||
"files": [ | ||
"dist" | ||
], | ||
"repository": "https://github.com/VirgilSecurity/virgil-crypto-javascript/tree/master/packages/data-utils", | ||
"author": "Virgil Security Inc. <support@virgilsecurity.com>", | ||
"license": "BSD-3-Clause", | ||
"scripts": { | ||
"test": "mocha -r ts-node/register src/**/*.test.ts", | ||
"build": "rollup -c", | ||
"clean": "rimraf .rpt2_cache dist", | ||
"prepare": "npm run clean && npm run build" | ||
}, | ||
"dependencies": { | ||
"buffer": "^5.2.1" | ||
}, | ||
"devDependencies": { | ||
"@types/chai": "^4.1.7", | ||
"@types/mocha": "^5.2.7", | ||
"chai": "^4.2.0", | ||
"mocha": "^6.1.4", | ||
"rimraf": "^2.6.3", | ||
"rollup": "^1.18.0", | ||
"rollup-plugin-commonjs": "^10.0.2", | ||
"rollup-plugin-node-resolve": "^5.2.0", | ||
"rollup-plugin-typescript2": "^0.21.1", | ||
"ts-node": "^8.3.0", | ||
"typescript": "^3.5.1" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
const path = require('path'); | ||
|
||
const commonjs = require('rollup-plugin-commonjs'); | ||
const nodeResolve = require('rollup-plugin-node-resolve'); | ||
const typescript = require('rollup-plugin-typescript2'); | ||
|
||
const FORMAT = { | ||
CJS: 'cjs', | ||
ES: 'es', | ||
}; | ||
|
||
const sourcePath = path.join(__dirname, 'src'); | ||
const outputPath = path.join(__dirname, 'dist'); | ||
|
||
const createNodeEntry = format => ({ | ||
external: ['buffer/'], | ||
input: path.join(sourcePath, 'node.ts'), | ||
output: { | ||
format, | ||
file: path.join(outputPath, `node.${format}.js`), | ||
}, | ||
plugins: [typescript({ useTsconfigDeclarationDir: true })], | ||
}); | ||
|
||
const createBrowserEntry = format => ({ | ||
input: path.join(sourcePath, 'browser.ts'), | ||
output: { | ||
format, | ||
file: path.join(outputPath, `browser.${format}.js`), | ||
}, | ||
plugins: [ | ||
nodeResolve({ browser: true }), | ||
commonjs(), | ||
typescript({ useTsconfigDeclarationDir: true }), | ||
] | ||
}); | ||
|
||
module.exports = [ | ||
createBrowserEntry(FORMAT.CJS), | ||
createBrowserEntry(FORMAT.ES), | ||
createNodeEntry(FORMAT.CJS), | ||
createNodeEntry(FORMAT.ES), | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { expect } from 'chai'; | ||
|
||
import { Buffer as BrowserBuffer } from 'buffer/'; | ||
|
||
describe('browser', () => { | ||
it('exports correct Buffer', () => { | ||
const { Buffer } = require('../browser'); | ||
expect(Buffer).to.eql(BrowserBuffer); | ||
}); | ||
}); |
47 changes: 47 additions & 0 deletions
47
packages/data-utils/src/__tests__/dataToUint8Array.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { expect } from 'chai'; | ||
|
||
import { createDataToUint8ArrayFunction } from '../dataToUint8Array'; | ||
|
||
describe('dataToUint8Array', () => { | ||
let dataToUint8Array: ReturnType<typeof createDataToUint8ArrayFunction>; | ||
|
||
before(() => { | ||
dataToUint8Array = createDataToUint8ArrayFunction(Buffer); | ||
}); | ||
|
||
it('returns Uint8Array based on a string that was converted to it using defaultEncoding', () => { | ||
const data = 'data'; | ||
const result = dataToUint8Array(data, 'utf8'); | ||
const expected = Buffer.from(data, 'utf8'); | ||
expect(expected.equals(result)).to.be.true; | ||
}); | ||
|
||
it('returns Uint8Array based on a string that was converted to it using default encoding (utf-8)', () => { | ||
const data = 'data'; | ||
const result = dataToUint8Array(data); | ||
const expected = Buffer.from(data, 'utf8'); | ||
expect(expected.equals(result)).to.be.true; | ||
}); | ||
|
||
it('returns the same Uint8Array if argument is an instance of Uint8Array', () => { | ||
const data = new Uint8Array(0); | ||
const result = dataToUint8Array(data); | ||
expect(result).to.eql(data); | ||
}); | ||
|
||
it('throws if first argument is not a string / Data object / Uint8Array', () => { | ||
const error = () => { | ||
// @ts-ignore | ||
dataToUint8Array(123); | ||
}; | ||
expect(error).to.throw; | ||
}); | ||
|
||
it('throws if Data object is invalid', () => { | ||
const error = () => { | ||
// @ts-ignore | ||
dataToUint8Array({ value: 123, encoding: 'utf8' }); | ||
}; | ||
expect(error).to.throw; | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { expect } from 'chai'; | ||
|
||
describe('node', () => { | ||
it('exports correct Buffer', () => { | ||
const { Buffer } = require('../node'); | ||
expect(Buffer).to.eql(global.Buffer); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { expect } from 'chai'; | ||
|
||
import { createToBufferFunction } from '../toBuffer'; | ||
|
||
describe('toBuffer', () => { | ||
let toBuffer: ReturnType<typeof createToBufferFunction>; | ||
|
||
before(() => { | ||
toBuffer = createToBufferFunction(Buffer); | ||
}); | ||
|
||
it('returns correct buffer', () => { | ||
const data = Buffer.from('data', 'utf8'); | ||
const result = toBuffer(data); | ||
expect(result.buffer).to.eql(data.buffer); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { Buffer as NodeBuffer } from 'buffer/'; | ||
|
||
import { createDataToUint8ArrayFunction } from './dataToUint8Array'; | ||
import { createToBufferFunction } from './toBuffer'; | ||
|
||
export { Buffer } from 'buffer/'; | ||
|
||
export * from './types'; | ||
|
||
export const dataToUint8Array = createDataToUint8ArrayFunction(NodeBuffer); | ||
export const toBuffer = createToBufferFunction(NodeBuffer); |
Oops, something went wrong.