Skip to content

Commit b0d91f1

Browse files
committed
feat : add support umd
1 parent c2d99a5 commit b0d91f1

File tree

10 files changed

+234
-94
lines changed

10 files changed

+234
-94
lines changed

README.md

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,15 @@ Create data storage that uses a simple key-value method for Node, Browser, Deno,
1414
[https://codesandbox.io/p/devbox/simple-kv-storage-pzr9ld](https://codesandbox.io/p/devbox/simple-kv-storage-pzr9ld)
1515

1616
## Installation
17+
18+
NPM (node, browser, deno)
1719
```javascript
1820
npm install kv-storage
1921
```
22+
CDN (browser)
23+
```javascript
24+
<script src="https://cdn.jsdelivr.net/npm/kv-storage@0.0.4/dist/umd/kv-storage.js"></script>
25+
```
2026

2127
## Initialization
2228

@@ -31,7 +37,15 @@ import {KVStorage} from 'kv-storage'
3137
import {KVStorage} from 'npm:kv-storage'
3238

3339
const db = await KVStorage({
34-
runtime:'node', //node | deno | browser
40+
runtime:'node', //node | browser| deno
41+
storageName:'storage'
42+
})
43+
```
44+
```javascript
45+
//Browser using CDN
46+
47+
const db = await kvstorage.KVStorage({
48+
runtime:'browser',
3549
storageName:'storage'
3650
})
3751
```
@@ -73,14 +87,14 @@ void async function main() {
7387
console.log(await db.has('key'))
7488
}()
7589
```
76-
7790
```javascript
78-
//Deno example
79-
import {KVStorage} from 'npm:kv-storage'
91+
<script src="https://cdn.jsdelivr.net/npm/kv-storage@0.0.4/dist/umd/kv-storage.js"></script>
92+
<script>
93+
//Browser using CDN example
8094

8195
void async function main() {
82-
const db = await KVStorage({
83-
runtime:'deno',
96+
const db = await kvstorage.KVStorage({
97+
runtime:'browser',
8498
storageName:'storage'
8599
})
86100

@@ -90,12 +104,13 @@ void async function main() {
90104
console.log(await db.delete('key'))
91105
console.log(await db.has('key'))
92106
}()
107+
</script>
93108
```
94109

95110
```javascript
96111
<script type="module">
97-
//Directly in Browser example
98-
import {KVStorage} from 'https://cdn.jsdelivr.net/npm/kv-storage@0.0.3/dist/mjs/kv-storage.js'
112+
//Browser ES Modules example
113+
import {KVStorage} from 'https://cdn.jsdelivr.net/npm/kv-storage@0.0.4/dist/mjs/kv-storage.js'
99114

100115
void async function main() {
101116
const db = await KVStorage({
@@ -111,14 +126,31 @@ void async function main() {
111126
}()
112127
</script>
113128
```
129+
```javascript
130+
//Deno example
131+
import {KVStorage} from 'npm:kv-storage'
132+
133+
void async function main() {
134+
const db = await KVStorage({
135+
runtime:'deno',
136+
storageName:'storage'
137+
})
138+
139+
console.log(await db.put('key','value'))
140+
console.log(await db.get('key'))
141+
console.log(await db.list())
142+
console.log(await db.delete('key'))
143+
console.log(await db.has('key'))
144+
}()
145+
```
114146

115147
## API Reference
116148

117149
### Documentation
118150

119151
[https://nuzulul.github.io/kv-storage/](https://nuzulul.github.io/kv-storage/)
120152

121-
### Init Parameters
153+
### Initialization parameters
122154

123155
```javascript
124156
await init({
@@ -128,12 +160,12 @@ await init({
128160
```
129161
```
130162
runtime = Javascript runtime
131-
storageName = Alphanumeric name of storage
163+
storageName = Alphanumeric storage name
132164
```
133165
Supported runtime :
134166
- [x] `node`
135167
- [x] `deno` need `--allow-read --allow-write`
136-
- [x] `browser`
168+
- [x] `browser` use IndexedDB
137169
- [ ] `bun`
138170
- [ ] `cloudflare-workers`
139171
- [ ] `memory`

config/rollup.config.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import typescript from '@rollup/plugin-typescript';
2+
3+
export default [
4+
{
5+
input: 'src/kv-storage.ts',
6+
exclude:["test","src","**/*.d.ts"],
7+
output: [
8+
{
9+
file: 'public/umd/kv-storage.umd.js',
10+
format: 'umd',
11+
name: 'kvstorage',
12+
inlineDynamicImports:true,
13+
},
14+
{
15+
file: 'public/umd/kv-storage.ems.js',
16+
format: 'es',
17+
//name: 'kvstorage',
18+
inlineDynamicImports:true,
19+
},
20+
],
21+
plugins: [typescript()],
22+
},
23+
{
24+
input: 'src/browser-kv-storage.ts',
25+
exclude:["test","src","**/*.d.ts"],
26+
output: [
27+
{
28+
file: 'public/umd/browser-kv-storage.js',
29+
format: 'es',
30+
},
31+
],
32+
plugins: [typescript()],
33+
}
34+
]

config/rollup.config.umd.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import typescript from '@rollup/plugin-typescript';
2+
3+
export default [
4+
{
5+
input: 'src/kv-storage.ts',
6+
exclude:["test","src","**/*.d.ts"],
7+
output: [
8+
{
9+
file: 'dist/umd/kv-storage.js',
10+
format: 'umd',
11+
name: 'kvstorage',
12+
inlineDynamicImports:true,
13+
}
14+
],
15+
plugins: [typescript()],
16+
},
17+
{
18+
input: 'src/browser-kv-storage.ts',
19+
exclude:["test","src","**/*.d.ts"],
20+
output: [
21+
{
22+
file: 'dist/umd/browser-kv-storage.js',
23+
format: 'es',
24+
},
25+
],
26+
plugins: [typescript()],
27+
}
28+
]

package.json

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "kv-storage",
3-
"version": "0.0.3",
3+
"version": "0.0.4",
44
"description": "Create data storage that uses a simple key-value method for Node, Browser, Deno, Bun, Cloudflare Workers",
55
"main": "dist/cjs/kv-storage.js",
66
"module": "dist/mjs/kv-storage.js",
@@ -16,18 +16,20 @@
1616
"start": "node public/test/server.js",
1717
"start-mjs": "npm run build && node public/test/server-mjs.js",
1818
"dev-ts": "nodemon -e js,ts --watch src --watch test --exec \"ts-node test/server\"",
19-
"dev-browser": "nodemon -e js,ts,html --watch src --watch test --exec \"npm run prepare-public && tsc -p tsconfig-browser.json && ts-node test/server-browser.ts\"",
20-
"dev-deno": "nodemon -e js,ts --watch src --watch test --exec \"deno run --allow-read --allow-write --unstable-sloppy-imports test/server-deno.ts\"",
19+
"dev-browser": "nodemon -e js,ts,html --watch src --watch test --exec \"npm run prepare-public && tsc -p tsconfig-browser.json && tsc -p tsconfig-umd.json && rollup -c public/config/rollup.config.js && ts-node test/server-browser.ts\"",
20+
"dev-umd": "nodemon -e js,ts,html --watch src --watch test --exec \"npm run prepare-public && tsc -p tsconfig-umd.json && rollup -c public/config/rollup.config.js && ts-node test/server-browser.ts\"",
21+
"dev-deno": "nodemon -e js,ts --watch src --watch test --exec \"deno run --allow-read --allow-write --unstable-sloppy-imports test/server-deno.ts\"",
2122
"dev": "nodemon -e js,ts --watch src --watch test --exec \"npm run build && npm start\"",
2223
"dev-mjs": "nodemon -e js,ts --watch src --watch test --exec \"npm run start-mjs\"",
23-
"build-win": "npm run prepare-build-win && tsc -p tsconfig-mjs.json && tsc -p tsconfig-cjs.json && echo {\"type\": \"commonjs\"}>dist\\cjs\\package.json && echo {\"type\": \"module\"}>dist\\mjs\\package.json",
24+
"build-win": "npm run prepare-build-win && tsc -p tsconfig-mjs.json && tsc -p tsconfig-cjs.json && tsc -p tsconfig-umd.json && rollup -c public/config/rollup.config.umd.js && echo {\"type\": \"commonjs\"}>dist\\cjs\\package.json && echo {\"type\": \"module\"}>dist\\mjs\\package.json",
2425
"prepare-build": "if exist .\\dist (echo ok) && mkdir dist && del /S /Q .\\dist\\*",
2526
"prepare-build-win": "if not exist .\\dist (mkdir dist) else (rmdir /S /Q .\\dist\\)",
26-
"prepare-typedoc": "if not exist .\\docs (mkdir docs) else (rmdir /S /Q .\\docs\\)",
27-
"prepare-public": "if not exist .\\public (mkdir public) else (rmdir /S /Q .\\public\\)",
28-
"typedoc":"npm run prepare-typedoc && typedoc src/kv-storage.ts src/node-kv-storage.ts src/deno-kv-storage.ts src/browser-kv-storage.ts",
27+
"prepare-typedoc": "if not exist .\\docs (mkdir docs) else (rmdir /S /Q .\\docs\\)",
28+
"prepare-public": "if not exist .\\public (mkdir public) else (rmdir /S /Q .\\public\\)",
29+
"typedoc": "npm run prepare-typedoc && typedoc src/kv-storage.ts src/node-kv-storage.ts src/deno-kv-storage.ts src/browser-kv-storage.ts",
2930
"gh-deploy": "git push origin :gh-pages && git subtree push --prefix docs origin gh-pages",
30-
"gh-deploy-init": "git push origin && git subtree push --prefix docs origin gh-pages"
31+
"gh-deploy-init": "git push origin && git subtree push --prefix docs origin gh-pages",
32+
"gh-deploydoc": "npm run typedoc && git add docs -f && git commit -m \"docs\" && npm run gh-deploy && git reset --soft HEAD~ && git restore --staged ."
3133
},
3234
"files": [
3335
"dist/",
@@ -61,8 +63,11 @@
6163
},
6264
"homepage": "https://github.com/nuzulul/kv-storage#readme",
6365
"devDependencies": {
66+
"@rollup/plugin-typescript": "^11.1.6",
6467
"nodemon": "^3.0.3",
68+
"rollup": "^4.9.6",
6569
"ts-node": "^10.9.2",
70+
"tslib": "^2.6.2",
6671
"typedoc": "^0.25.7",
6772
"typescript": "^5.3.3"
6873
}

src/kv-storage.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export async function KVStorage({
4040
let browserpkg = './browser-kv-storage'
4141
if(window)browserpkg = './browser-kv-storage.js'
4242
const runbrowser = await import(browserpkg)
43+
//const runbrowser = await import('./browser-kv-storage')
4344
const dbbrowser = await runbrowser.BrowserKVStorage.init({
4445
databaseName,
4546
storageName

test/index.html renamed to test/esm.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<html>
22
<body>
3-
<p>123</p>
3+
<p>open console</p>
44
<script type="module">
55
import {KVStorage} from './../public/src/kv-storage.js'
66

test/server-browser.ts

Lines changed: 54 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -6,74 +6,63 @@ const serverport = process.env.PORT || 8080
66

77
const server = http.createServer(async(request:any,response:any)=>{
88

9-
//let result = fs.readFileSync('./test/index.html')
10-
11-
//res.statusCode = 200
12-
13-
//res.setHeader('Content-Type','application/xml')
14-
15-
//res.setHeader('Content-Type','text/plain')
16-
17-
//res.setHeader('Access-Control-Allow-Origin','*')
18-
19-
//res.setHeader('Content-Type','text/html')
20-
21-
//res.end(result)
22-
23-
//console.log('request starting...');
24-
259
var filePath = '.' + request.url;
26-
if (filePath == './')
27-
filePath = './index.html';
10+
if (filePath == './'){
11+
const html = '<a href="test/umd.html">umd</a><br><a href="test/esm.html">esm</a>'
12+
response.statusCode = 200
13+
response.setHeader('Content-Type','text/html')
14+
response.end(html)
15+
}else{
2816

29-
var extname = path.extname(filePath);
30-
var contentType = 'text/html';
31-
switch (extname) {
32-
case '.html':
33-
contentType = 'text/html';
34-
break;
35-
case '.js':
36-
contentType = 'text/javascript';
37-
break;
38-
case '.css':
39-
contentType = 'text/css';
40-
break;
41-
case '.json':
42-
contentType = 'application/json';
43-
break;
44-
case '.png':
45-
contentType = 'image/png';
46-
break;
47-
case '.jpg':
48-
contentType = 'image/jpg';
49-
break;
50-
case '.wav':
51-
contentType = 'audio/wav';
52-
break;
53-
default:
54-
filePath += filePath+'.js';
55-
contentType = 'text/javascript';
56-
}
17+
var extname = path.extname(filePath);
18+
var contentType = 'text/html';
19+
switch (extname) {
20+
case '.html':
21+
contentType = 'text/html';
22+
break;
23+
case '.js':
24+
contentType = 'text/javascript';
25+
break;
26+
case '.css':
27+
contentType = 'text/css';
28+
break;
29+
case '.json':
30+
contentType = 'application/json';
31+
break;
32+
case '.png':
33+
contentType = 'image/png';
34+
break;
35+
case '.jpg':
36+
contentType = 'image/jpg';
37+
break;
38+
case '.wav':
39+
contentType = 'audio/wav';
40+
break;
41+
default:
42+
filePath += filePath+'.js';
43+
contentType = 'text/javascript';
44+
}
5745

58-
fs.readFile(filePath, function(error:any, content:any) {
59-
if (error) {
60-
if(error.code == 'ENOENT'){
61-
fs.readFile('./404.html', function(error:any, content:any) {
62-
response.writeHead(200, { 'Content-Type': contentType });
63-
response.end(content, 'utf-8');
64-
});
65-
}
66-
else {
67-
response.writeHead(500);
68-
response.end('Sorry, check with the site admin for error: '+error.code+' ..\n');
69-
response.end();
70-
}
71-
}
72-
else {
73-
response.writeHead(200, { 'Content-Type': contentType });
74-
response.end(content, 'utf-8');
75-
}
76-
});
46+
fs.readFile(filePath, function(error:any, content:any) {
47+
if (error) {
48+
if(error.code == 'ENOENT'){
49+
fs.readFile('./404.html', function( content:any) {
50+
response.writeHead(200, { 'Content-Type': contentType });
51+
response.end(content, 'utf-8');
52+
});
53+
}
54+
else {
55+
response.writeHead(500);
56+
response.end('Sorry, check with the site admin for error: '+error.code+' ..\n');
57+
response.end();
58+
}
59+
}
60+
else {
61+
response.writeHead(200, { 'Content-Type': contentType });
62+
response.end(content, 'utf-8');
63+
}
64+
});
65+
}
7766

7867
})
7968

0 commit comments

Comments
 (0)