Skip to content

Commit

Permalink
chore: rewrite to cjs (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats authored Mar 31, 2024
1 parent 5eaeb9d commit 8fe1718
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 36 deletions.
13 changes: 6 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"description": "authentication & rate limit for HTTP backed in Redis.",
"homepage": "https://github.com/microlinkhq/openkey",
"version": "0.0.0",
"type": "module",
"exports": {
".": "./src/index.js",
"./plans": "./src/plans.js",
Expand All @@ -13,8 +12,8 @@
"openkey": "bin/index.js"
},
"author": {
"name": "microlink.io",
"email": "[email protected]",
"name": "microlink.io",
"url": "https://microlink.io"
},
"repository": {
Expand All @@ -28,20 +27,17 @@
"dependencies": {
"mri": "~1.2.0"
},
"files": [
"src"
],
"devDependencies": {
"ioredis": "latest",
"@commitlint/cli": "latest",
"@commitlint/config-conventional": "latest",
"@ksmithut/prettier-standard": "latest",
"ava": "5",
"c8": "latest",
"ci-publish": "latest",
"github-generate-release": "latest",
"finepack": "latest",
"git-authors-cli": "latest",
"github-generate-release": "latest",
"ioredis": "latest",
"nano-staged": "latest",
"npm-check-updates": "latest",
"simple-git-hooks": "latest",
Expand All @@ -52,6 +48,9 @@
"engines": {
"node": ">= 18"
},
"files": [
"src"
],
"scripts": {
"clean": "rm -rf node_modules",
"contributors": "(npx git-authors-cli && npx finepack && git add package.json && git commit -m 'build: contributors' --no-verify) || true",
Expand Down
13 changes: 5 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import JSONB from 'json-buffer'
'use strict'

import createKeys from './keys.js'
import createPlans from './plans.js'
const JSONB = require('json-buffer')
const createKeys = require('./keys')
const createPlans = require('./plans')

export default ({
serialize = JSONB.stringify,
deserialize = JSONB.parse,
redis = new Map()
} = {}) => {
module.exports = ({ serialize = JSONB.stringify, deserialize = JSONB.parse, redis = new Map() } = {}) => {
if (!redis) throw TypeError('The argument `store` is required.')
const plans = createPlans({ serialize, deserialize, redis })
const keys = createKeys({ serialize, deserialize, redis, plans })
Expand Down
10 changes: 7 additions & 3 deletions src/keys.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { pick, uid, validateKey, assert } from './util.js'
'use strict'

export const KEY_PREFIX = 'key_'
const { pick, uid, validateKey, assert } = require('./util')

const KEY_PREFIX = 'key_'
const KEY_FIELDS = ['name', 'description', 'enabled', 'value', 'plan']
const KEY_FIELDS_OBJECT = ['metadata']

export default ({ serialize, deserialize, plans, redis } = {}) => {
module.exports = ({ serialize, deserialize, plans, redis } = {}) => {
/**
* Create a key.
*
Expand Down Expand Up @@ -105,3 +107,5 @@ export default ({ serialize, deserialize, plans, redis } = {}) => {

return { create, retrieve, del, update, list }
}

module.exports.KEY_PREFIX = KEY_PREFIX
10 changes: 7 additions & 3 deletions src/plans.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { pick, uid, validateKey, assert } from './util.js'
'use strict'

export const PLAN_PREFIX = 'plan_'
const { pick, uid, validateKey, assert } = require('./util')

const PLAN_PREFIX = 'plan_'
const PLAN_QUOTA_PERIODS = ['day', 'week', 'month']
const PLAN_FIELDS = ['name', 'description']
const PLAN_FIELDS_OBJECT = ['quota', 'throttle', 'metadata']

export default ({ serialize, deserialize, redis } = {}) => {
module.exports = ({ serialize, deserialize, redis } = {}) => {
/**
* Create a plan.
*
Expand Down Expand Up @@ -109,3 +111,5 @@ export default ({ serialize, deserialize, redis } = {}) => {

return { create, del, retrieve, update, list }
}

module.exports.PLAN_PREFIX = PLAN_PREFIX
19 changes: 14 additions & 5 deletions src/util.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,32 @@
import { getRandomValues } from 'crypto'
'use strict'

const { getRandomValues } = require('crypto')

const BASE_58 = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'

const rand = size =>
getRandomValues(new Uint8Array(size)).reduce((id, value) => id + BASE_58.charAt(value % BASE_58.length), '')

export const uid = async ({ redis, prefix = '', size }) => {
const uid = async ({ redis, prefix = '', size }) => {
let uid
do uid = `${prefix}${rand(size)}`
while ((await redis.keys(`${prefix}*`)).includes(uid))
return uid
}

export const pick = (obj, keys) => {
const pick = (obj, keys) => {
const result = {}
for (const key of keys) if (obj[key] !== undefined) result[key] = obj[key]
return result
}

export const assert = (value, message) =>
const assert = (value, message) =>
value ||
(() => {
throw new TypeError(message)
})()

export const validateKey =
const validateKey =
({ prefix }) =>
(id, { validate = true } = {}) => {
if (!validate) return id
Expand All @@ -33,3 +35,10 @@ export const validateKey =
}
return id
}

module.exports = {
uid,
pick,
assert,
validateKey
}
12 changes: 7 additions & 5 deletions test/keys.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { setTimeout } from 'timers/promises'
import openkey from 'openkey'
import Redis from 'ioredis'
import test from 'ava'
'use strict'

import { KEY_PREFIX } from 'openkey/keys'
const { setTimeout } = require('timers/promises')
const openkey = require('openkey')
const Redis = require('ioredis')
const test = require('ava')

const { KEY_PREFIX } = require('openkey/keys')

const redis = new Redis()

Expand Down
12 changes: 7 additions & 5 deletions test/plans.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { setTimeout } from 'timers/promises'
import openkey from 'openkey'
import Redis from 'ioredis'
import test from 'ava'
'use strict'

import { PLAN_PREFIX } from 'openkey/plans'
const { setTimeout } = require('timers/promises')
const openkey = require('openkey')
const Redis = require('ioredis')
const test = require('ava')

const { PLAN_PREFIX } = require('openkey/plans')

const redis = new Redis()

Expand Down

0 comments on commit 8fe1718

Please sign in to comment.