Skip to content

Commit 6c10bff

Browse files
committed
Initial commit
1 parent a55d48b commit 6c10bff

7 files changed

+133
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
*.swp
2+
node_modules
3+
npm-debug.log

.travis.yml

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
language: node_js
2+
sudo: false
3+
node_js:
4+
- node
5+
env:
6+
global:
7+
- secure: EP8oid1it/TGaZkO2dfqDsBEw5GJwdRV+Cnso9L/6Gu+MRMY3YJ9Dgx8LBnHRWs+cJK0Zjg/ZX8b1ES+kMylDaCevjuY6veI83wBx717tq+oF5zLDiS1kmSN9LXCsAz5jzZePN/6/I5HswqlfDuNpdUB+RiykoKapOynPmwVAbUUGv2HgZ6lZCqANNsiALEjvfMfMVnDI6BynXLVqaawusx8dP2Af1seSDskllRfqDCMB2Cn1rWKvfJnCWnsNOMykjOWPwr/XTkHsL+qhWRci9jCstfN/ucKoeePjdJib2wtkiSyQp5P0cDzzHjb5Q3CYTNSTRl91RyvUb/EK0Kl9mrRjLwOSzF4JUvoglcTpL2zdmSDuLaYPZz43e7GbbeI5mCw9tohYXdSc3wOH5f+/iD2RKH/px0uO6I+xcXdtc9mMQwBU3kfKRcGpCxn32MP7UPABdjKoP8D7X7U3VtCiqzGv/KlHfsrHYWn0FPmgmZEkAhQzlldmqzaBVANd/MyP7lTiFwYFXGinFRROYrcm7L6eChnoyoYbCqmsnfQIFAsVNWDTRz0jPAR/0Qtjv1k7v9GHf5PT/r3wa+jHYsv9IjwF9P2a5oQeOq/ptQ7qZ+o7yxKDmJEvTbL5+NEtuuGII/E6lKlo0r2uPvoiGwGFlZ4ihmU7GEk1SEiTVJFfNY=
8+
- secure: NG2ODujSmrz/7vuEaAxa4urAbQhuBnqz61ZzFe25/w7N9CwGue6/IW0EBrvBp1ERyU9iVDeYxAX2r3abSaRSgBctbf0rsQptZdVdFXcw1+MO5FAxukEJ9Cizyl+zJvZsOrBmEZMS1IbYq8MBnCkQXGwwSaxsF7xOXOf9Z9VRvpCtdfHQJ2bnw0WJiDESCyrOm09T2MFSdVBlCRQ7sR1Zj/sMicD5MCUUkMTGy9vvtw0nC5AZBfuvNe1yN4sFD4+SFRpMycLq5h40MQl9A2TB47lRGFjFDeXyYSV/CBoIAG9binz1bnnNLxYi8DV0hiBP/SUnt2Wo4u34lN6F6rBug6+8MU/dg0JHRTOI/tcanCVB06vf1iaYwmQc+aBjGkNG4K0tLOSDW9bxLlDfPUT/7OPcDJNfCHFsmGvu60WL9maY6/FTffGQvuC8CQ6wttG4weKVTBL3zXATUP5B/m/bIZD+q2BGwkSzpIqikm+otM9Uszd8Sy0vZ/31T/BFnUs9zS+q07RTnoiAuLmP49dmuMK+7h0KutGgorqe557WXAutJV0LqNVI8ydHrSh7hhuXULCDuFrC6G9Id32x/s/PM6ByIjYgSo8c+BhKdd1t5S39T3UeL8bNytFmmgJuCi594mh8I5xv6KDeGxEJc/7j9WBcJqAgn6P3KyXHXsKuR3E=

.zuul.yml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
ui: tape
2+
browsers:
3+
- name: chrome
4+
version: -1..latest
5+
- name: firefox
6+
version: -1..latest
7+
- name: ie
8+
version: latest
9+
- name: edge
10+
version: latest
11+
- name: safari
12+
version: latest

idbchunkstore.min.js

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.js

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
module.exports = IdbChunkStore
2+
3+
var IdbKvStore = require('idb-kv-store')
4+
5+
function IdbChunkStore (chunkLength, opts) {
6+
var self = this
7+
if (!(this instanceof IdbChunkStore)) return new IdbChunkStore(chunkLength)
8+
if (!opts) opts = {}
9+
10+
self.chunkLength = chunkLength
11+
self._store = new IdbKvStore(opts.name || '' + Math.round(9e16 * Math.random()))
12+
}
13+
14+
IdbChunkStore.prototype.put = function (index, buffer, cb) {
15+
var self = this
16+
if (!cb) cb = noop
17+
if (!self._store) return cb(new Error('Store is closed'))
18+
if (typeof index !== 'number') return cb(new Error('index must be a number'))
19+
if (!buffer) return cb(new Error('buffer must be defined'))
20+
if (buffer.length !== self.chunkLength) return cb(new Error('The buffer length must match the chunkLength'))
21+
// TODO check if buffer is actually a buffer
22+
23+
self._store.set(index, buffer, cb)
24+
}
25+
26+
IdbChunkStore.prototype.get = function (index, opts, cb) {
27+
var self = this
28+
if (typeof opts === 'function') return self.get(index, null, opts)
29+
if (typeof cb !== 'function') throw new Error('cb must be a function')
30+
if (!self._store) return cb(new Error('Store is closed'))
31+
if (typeof index !== 'number') return cb(new Error('index must be a number'))
32+
if (!opts) opts = {}
33+
34+
self._store.get(index, function (err, buffer) {
35+
if (err) return cb(err)
36+
if (typeof buffer === 'undefined') return cb(new Error('Chunk does not exist'))
37+
var offset = 'offset' in opts ? opts.offset : 0
38+
var length = 'length' in opts ? opts.length : buffer.length - offset
39+
cb(null, buffer.slice(offset, offset + length))
40+
})
41+
}
42+
43+
IdbChunkStore.prototype.close = function (cb) {
44+
var self = this
45+
if (!cb) cb = noop
46+
if (!self._store) return cb(new Error('Store is closed'))
47+
48+
self._store = null
49+
cb(null)
50+
}
51+
52+
IdbChunkStore.prototype.destroy = function (cb) {
53+
var self = this
54+
if (!cb) cb = noop
55+
if (!self._store) return cb(new Error('Store is closed'))
56+
57+
var s = self._store
58+
self._store = null
59+
s.clear(cb)
60+
}
61+
62+
function noop () {
63+
// do nothing
64+
}

package.json

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"name": "idb-chunk-store",
3+
"version": "1.0.0",
4+
"description": "A Chunk Store compatible store backed by IndexDB",
5+
"main": "index.js",
6+
"scripts": {
7+
"build": "standard && browserify -r . -s IdbChunkStore | uglifyjs -m > idbchunkstore.min.js",
8+
"standard": "standard",
9+
"test": "standard && zuul -- test.js",
10+
"test-local": "standard && zuul --local -- test.js"
11+
},
12+
"repository": {
13+
"type": "git",
14+
"url": "git+https://github.com/xuset/idb-chunk-store.git"
15+
},
16+
"keywords": [
17+
"abstract-chunk-store",
18+
"chunk",
19+
"store",
20+
"indexdb",
21+
"idb"
22+
],
23+
"author": "xuset",
24+
"license": "MIT",
25+
"bugs": {
26+
"url": "https://github.com/xuset/idb-chunk-store/issues"
27+
},
28+
"homepage": "https://github.com/xuset/idb-chunk-store#readme",
29+
"dependencies": {
30+
"idb-kv-store": "^2.2.0"
31+
},
32+
"devDependencies": {
33+
"abstract-chunk-store": "^1.2.1",
34+
"browserify": "^13.1.1",
35+
"standard": "^8.6.0",
36+
"tape": "^4.6.3",
37+
"uglify-js": "^2.7.4",
38+
"zuul": "^3.11.1"
39+
}
40+
}

test.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
var IdbChunkStore = require('.')
2+
var abstractTests = require('abstract-chunk-store/tests')
3+
var test = require('tape')
4+
5+
abstractTests(test, IdbChunkStore)

0 commit comments

Comments
 (0)