From 8fe171863cdd30ded798b6d421cca7b88943d0ac Mon Sep 17 00:00:00 2001 From: Kiko Beats Date: Mon, 1 Apr 2024 00:00:35 +0200 Subject: [PATCH] chore: rewrite to cjs (#10) --- package.json | 13 ++++++------- src/index.js | 13 +++++-------- src/keys.js | 10 +++++++--- src/plans.js | 10 +++++++--- src/util.js | 19 ++++++++++++++----- test/keys.js | 12 +++++++----- test/plans.js | 12 +++++++----- 7 files changed, 53 insertions(+), 36 deletions(-) diff --git a/package.json b/package.json index b987f15..7ebba17 100644 --- a/package.json +++ b/package.json @@ -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", @@ -13,8 +12,8 @@ "openkey": "bin/index.js" }, "author": { - "name": "microlink.io", "email": "hello@microlink.io", + "name": "microlink.io", "url": "https://microlink.io" }, "repository": { @@ -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", @@ -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", diff --git a/src/index.js b/src/index.js index aeb2fcc..1105592 100644 --- a/src/index.js +++ b/src/index.js @@ -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 }) diff --git a/src/keys.js b/src/keys.js index 918aeb9..4f0a9b9 100644 --- a/src/keys.js +++ b/src/keys.js @@ -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. * @@ -105,3 +107,5 @@ export default ({ serialize, deserialize, plans, redis } = {}) => { return { create, retrieve, del, update, list } } + +module.exports.KEY_PREFIX = KEY_PREFIX diff --git a/src/plans.js b/src/plans.js index b735227..5d7aad1 100644 --- a/src/plans.js +++ b/src/plans.js @@ -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. * @@ -109,3 +111,5 @@ export default ({ serialize, deserialize, redis } = {}) => { return { create, del, retrieve, update, list } } + +module.exports.PLAN_PREFIX = PLAN_PREFIX diff --git a/src/util.js b/src/util.js index 6127f4e..9ae601e 100644 --- a/src/util.js +++ b/src/util.js @@ -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 @@ -33,3 +35,10 @@ export const validateKey = } return id } + +module.exports = { + uid, + pick, + assert, + validateKey +} diff --git a/test/keys.js b/test/keys.js index bd9e6bb..fcba50b 100644 --- a/test/keys.js +++ b/test/keys.js @@ -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() diff --git a/test/plans.js b/test/plans.js index a75334a..c62a359 100644 --- a/test/plans.js +++ b/test/plans.js @@ -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()