Skip to content

Commit

Permalink
feat: remove figgy-pudding
Browse files Browse the repository at this point in the history
  • Loading branch information
claudiahdz committed Feb 14, 2020
1 parent 79ba4ec commit 0e78fd7
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 97 deletions.
60 changes: 29 additions & 31 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict'

const crypto = require('crypto')
const figgyPudding = require('figgy-pudding')
const MiniPass = require('minipass')

const SPEC_ALGORITHMS = ['sha256', 'sha384', 'sha512']
Expand All @@ -11,17 +10,17 @@ const SRI_REGEX = /^([^-]+)-([^?]+)([?\S*]*)$/
const STRICT_SRI_REGEX = /^([^-]+)-([A-Za-z0-9+/=]{44,88})(\?[\x21-\x7E]*)*$/
const VCHAR_REGEX = /^[\x21-\x7E]+$/

const SsriOpts = figgyPudding({
algorithms: { default: ['sha512'] },
error: { default: false },
integrity: {},
options: { default: [] },
pickAlgorithm: { default: () => getPrioritizedHash },
sep: { default: ' ' },
single: { default: false },
size: {},
strict: { default: false }
})
const defaultOpts = {
algorithms: ['sha512'],
error: false,
options: [],
pickAlgorithm: getPrioritizedHash,
sep: ' ',
single: false,
strict: false
}

const ssriOpts = (opts = {}) => ({ ...defaultOpts, ...opts })

const getOptString = options => !options || !options.length ? ''
: `?${options.join('?')}`
Expand Down Expand Up @@ -101,7 +100,7 @@ class IntegrityStream extends MiniPass {
class Hash {
get isHash () { return true }
constructor (hash, opts) {
opts = SsriOpts(opts)
opts = ssriOpts(opts)
const strict = !!opts.strict
this.source = hash.trim()

Expand Down Expand Up @@ -138,7 +137,7 @@ class Hash {
}

toString (opts) {
opts = SsriOpts(opts)
opts = ssriOpts(opts)
if (opts.strict) {
// Strict mode enforces the standard as close to the foot of the
// letter as it can.
Expand Down Expand Up @@ -173,7 +172,7 @@ class Integrity {
}

toString (opts) {
opts = SsriOpts(opts)
opts = ssriOpts(opts)
let sep = opts.sep || ' '
if (opts.strict) {
// Entries must be separated by whitespace, according to spec.
Expand All @@ -187,7 +186,7 @@ class Integrity {
}

concat (integrity, opts) {
opts = SsriOpts(opts)
opts = ssriOpts(opts)
const other = typeof integrity === 'string'
? integrity
: stringify(integrity, opts)
Expand All @@ -201,7 +200,7 @@ class Integrity {
// add additional hashes to an integrity value, but prevent
// *changing* an existing integrity hash.
merge (integrity, opts) {
opts = SsriOpts(opts)
opts = ssriOpts(opts)
const other = parse(integrity, opts)
for (const algo in other) {
if (this[algo]) {
Expand All @@ -217,7 +216,7 @@ class Integrity {
}

match (integrity, opts) {
opts = SsriOpts(opts)
opts = ssriOpts(opts)
const other = parse(integrity, opts)
const algo = other.pickAlgorithm(opts)
return (
Expand All @@ -232,7 +231,7 @@ class Integrity {
}

pickAlgorithm (opts) {
opts = SsriOpts(opts)
opts = ssriOpts(opts)
const pickAlgorithm = opts.pickAlgorithm
const keys = Object.keys(this)
if (!keys.length) {
Expand All @@ -248,7 +247,7 @@ class Integrity {

module.exports.parse = parse
function parse (sri, opts) {
opts = SsriOpts(opts)
opts = ssriOpts(opts)
if (typeof sri === 'string') {
return _parse(sri, opts)
} else if (sri.algorithm && sri.digest) {
Expand Down Expand Up @@ -279,7 +278,7 @@ function _parse (integrity, opts) {

module.exports.stringify = stringify
function stringify (obj, opts) {
opts = SsriOpts(opts)
opts = ssriOpts(opts)
if (obj.algorithm && obj.digest) {
return Hash.prototype.toString.call(obj, opts)
} else if (typeof obj === 'string') {
Expand All @@ -291,7 +290,7 @@ function stringify (obj, opts) {

module.exports.fromHex = fromHex
function fromHex (hexDigest, algorithm, opts) {
opts = SsriOpts(opts)
opts = ssriOpts(opts)
const optString = getOptString(opts.options)
return parse(
`${algorithm}-${
Expand All @@ -302,7 +301,7 @@ function fromHex (hexDigest, algorithm, opts) {

module.exports.fromData = fromData
function fromData (data, opts) {
opts = SsriOpts(opts)
opts = ssriOpts(opts)
const algorithms = opts.algorithms
const optString = getOptString(opts.options)
return algorithms.reduce((acc, algo) => {
Expand All @@ -325,7 +324,7 @@ function fromData (data, opts) {

module.exports.fromStream = fromStream
function fromStream (stream, opts) {
opts = SsriOpts(opts)
opts = ssriOpts(opts)
const istream = integrityStream(opts)
return new Promise((resolve, reject) => {
stream.pipe(istream)
Expand All @@ -340,7 +339,7 @@ function fromStream (stream, opts) {

module.exports.checkData = checkData
function checkData (data, sri, opts) {
opts = SsriOpts(opts)
opts = ssriOpts(opts)
sri = parse(sri, opts)
if (!Object.keys(sri).length) {
if (opts.error) {
Expand Down Expand Up @@ -379,10 +378,9 @@ function checkData (data, sri, opts) {

module.exports.checkStream = checkStream
function checkStream (stream, sri, opts) {
opts = SsriOpts(opts)
const checker = integrityStream(opts.concat({
integrity: sri
}))
opts = ssriOpts(opts)
opts.integrity = sri
const checker = integrityStream(opts)
return new Promise((resolve, reject) => {
stream.pipe(checker)
stream.on('error', reject)
Expand All @@ -396,12 +394,12 @@ function checkStream (stream, sri, opts) {

module.exports.integrityStream = integrityStream
function integrityStream (opts) {
return new IntegrityStream(SsriOpts(opts))
return new IntegrityStream(ssriOpts(opts))
}

module.exports.create = createIntegrity
function createIntegrity (opts) {
opts = SsriOpts(opts)
opts = ssriOpts(opts)
const algorithms = opts.algorithms
const optString = getOptString(opts.options)

Expand Down
5 changes: 0 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
},
"license": "ISC",
"dependencies": {
"figgy-pudding": "^3.5.1",
"minipass": "^3.1.1"
},
"devDependencies": {
Expand Down
60 changes: 0 additions & 60 deletions test/mutable-opts-resilience.js

This file was deleted.

0 comments on commit 0e78fd7

Please sign in to comment.