Skip to content

Commit

Permalink
chore: add runkit example
Browse files Browse the repository at this point in the history
  • Loading branch information
brc-dd committed Aug 29, 2023
1 parent d54d640 commit aa10bdf
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .prettierrc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ printWidth: 100
proseWrap: always
semi: false
singleQuote: true
trailingComma: es5
trailingComma: none
overrides:
- files: '*.md'
options:
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,6 @@
"tsx": "^3.12.7",
"typescript": "^5.2.2"
},
"packageManager": "[email protected]"
"packageManager": "[email protected]",
"runkitExampleFilename": "dist/example.js"
}
19 changes: 19 additions & 0 deletions public/example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const { webcrypto } = require('node:crypto')
const Iron = require('iron-webcrypto')

const obj = {
a: 1,
b: 2,
c: [3, 4, 5],
d: {
e: 'f'
}
}

const password = 'a_password_having_at_least_32_chars'

const sealed = await Iron.seal(webcrypto, obj, password, Iron.defaults)
console.log({ sealed })

const unsealed = await Iron.unseal(webcrypto, sealed, password, Iron.defaults)
console.log({ unsealed })
10 changes: 5 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type {
password,
Password,
RawPassword,
SealOptions,
SealOptions
} from './types.js'

// re-export all types
Expand Down Expand Up @@ -41,13 +41,13 @@ export const defaults: SealOptions = {
integrity: { saltBits: 256, algorithm: 'sha256', iterations: 1, minPasswordlength: 32 },
ttl: 0,
timestampSkewSec: 60,
localtimeOffsetMsec: 0,
localtimeOffsetMsec: 0
}

export const clone = (options: SealOptions): SealOptions => ({
...options,
encryption: { ...options.encryption },
integrity: { ...options.integrity },
integrity: { ...options.integrity }
})

/**
Expand All @@ -56,7 +56,7 @@ export const clone = (options: SealOptions): SealOptions => ({
export const algorithms = {
'aes-128-ctr': { keyBits: 128, ivBits: 128, name: 'AES-CTR' },
'aes-256-cbc': { keyBits: 256, ivBits: 128, name: 'AES-CBC' },
sha256: { keyBits: 256, name: 'SHA-256' },
sha256: { keyBits: 256, name: 'SHA-256' }
} as const

/**
Expand Down Expand Up @@ -106,7 +106,7 @@ const pbkdf2 = async (
): Promise<ArrayBuffer> => {
const passwordBuffer = stringToBuffer(password)
const importedKey = await _crypto.subtle.importKey('raw', passwordBuffer, 'PBKDF2', false, [
'deriveBits',
'deriveBits'
])
const saltBuffer = stringToBuffer(salt)
const params = { name: 'PBKDF2', hash, salt: saltBuffer, iterations }
Expand Down
4 changes: 2 additions & 2 deletions tests/bun/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ async function rejects(fn: Promise<unknown>, re: RegExp): Promise<void> {
} catch (error) {
if (!isPromiseReturned)
throw new AssertionError({
message: `Function throws when expected to reject${msgToAppendToError}`,
message: `Function throws when expected to reject${msgToAppendToError}`
})
if (!(error instanceof Error))
throw new AssertionError({ message: 'A non-Error object was rejected.' })
if (!re.test(error.message))
throw new AssertionError({
message: `Expected error message to include "${re.toString()}", but got "${error.message}"`,
message: `Expected error message to include "${re.toString()}", but got "${error.message}"`
})
doesThrow = true
}
Expand Down
12 changes: 6 additions & 6 deletions tests/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const tests = ({
describe,
it,
deepEqual,
rejects: origRejects,
rejects: origRejects
}: TestContext): void => {
const rejects = async (fn: Promise<unknown>, msgIncludes: string[] | string): Promise<void> => {
// eslint-disable-next-line security/detect-non-literal-regexp
Expand Down Expand Up @@ -98,7 +98,7 @@ export const tests = ({
const key = Iron.randomBits(crypto, 128)
await rejects(Iron.seal(crypto, cyclic, key, Iron.defaults), [
'Converting circular structure to JSON',
'JSON.stringify cannot serialize cyclic structures.',
'JSON.stringify cannot serialize cyclic structures.'
])
})

Expand All @@ -112,7 +112,7 @@ export const tests = ({
const key = {
id: '1',
encryption: Iron.randomBits(crypto, 256),
integrity: Iron.randomBits(crypto, 256),
integrity: Iron.randomBits(crypto, 256)
}
const sealed = await Iron.seal(crypto, obj, key, Iron.defaults)
const unsealed = await Iron.unseal(crypto, sealed, { '1': key }, Iron.defaults)
Expand Down Expand Up @@ -175,11 +175,11 @@ export const tests = ({
saltBits: 999999999999999,
algorithm: 'sha256' as const,
iterations: 2,
minPasswordlength: 32,
minPasswordlength: 32
}
await rejects(Iron.generateKey(crypto, password, options), [
'Invalid typed array length',
'length too large',
'length too large'
])
})
})
Expand Down Expand Up @@ -319,7 +319,7 @@ export const tests = ({
await rejects(Iron.unseal(crypto, ticket, password, Iron.defaults), [
"Expected property name or '}' in JSON at position 1",
'Unexpected token a in JSON at position 1',
"JSON Parse error: Expected '}'",
"JSON Parse error: Expected '}'"
])
})

Expand Down
2 changes: 1 addition & 1 deletion tests/node/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ tests({
describe: test.describe,
it: test.it,
deepEqual: assert.deepStrictEqual,
rejects: assert.rejects,
rejects: assert.rejects
})
1 change: 1 addition & 0 deletions tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ export default defineConfig({
dts: true,
format: ['esm', 'cjs'],
treeshake: true,
publicDir: true
})

0 comments on commit aa10bdf

Please sign in to comment.