-
-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
feat: support indexedDB
Showing
4 changed files
with
302 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
const targetOrigin = location.protocol + '//' + location.host | ||
function sendToDevtools(message) { | ||
devtoolsIframe.contentWindow.postMessage( | ||
JSON.stringify(message), | ||
targetOrigin | ||
) | ||
} | ||
let id = 1 | ||
function sendToChobitsu(message) { | ||
message.id = 'tmp' + id++ | ||
chobitsu.sendRawMessage(JSON.stringify(message)) | ||
} | ||
chobitsu.setOnMessage(message => { | ||
if (message.indexOf('"id":"tmp') > -1) { | ||
return | ||
} | ||
devtoolsIframe.contentWindow.postMessage(message, targetOrigin) | ||
}) | ||
window.addEventListener('message', event => { | ||
if (event.origin !== targetOrigin) { | ||
return | ||
} | ||
if (event.data && typeof event.data === 'string') { | ||
chobitsu.sendRawMessage(event.data) | ||
} | ||
}) | ||
window.onload = function () { | ||
setTimeout(function () { | ||
if ( | ||
typeof devtoolsIframe !== 'undefined' && | ||
devtoolsIframe.contentWindow.runtime | ||
) { | ||
resetDevtools() | ||
} | ||
}, 0) | ||
} | ||
function resetDevtools() { | ||
const window = devtoolsIframe.contentWindow | ||
setTimeout(() => { | ||
window.runtime.loadLegacyModule('core/sdk/sdk-legacy.js').then(() => { | ||
const SDK = window.SDK | ||
for (const resourceTreeModel of SDK.TargetManager.instance().models( | ||
SDK.ResourceTreeModel | ||
)) { | ||
resourceTreeModel.dispatchEventToListeners( | ||
SDK.ResourceTreeModel.Events.WillReloadPage, | ||
resourceTreeModel | ||
) | ||
} | ||
}) | ||
sendToDevtools({ | ||
method: 'Page.frameNavigated', | ||
params: { | ||
frame: { | ||
id: '1', | ||
mimeType: 'text/html', | ||
securityOrigin: location.origin, | ||
url: location.href, | ||
}, | ||
type: 'Navigation', | ||
}, | ||
}) | ||
sendToChobitsu({ method: 'Network.enable' }) | ||
sendToDevtools({ method: 'Runtime.executionContextsCleared' }) | ||
sendToChobitsu({ method: 'Runtime.enable' }) | ||
sendToChobitsu({ method: 'Debugger.enable' }) | ||
sendToChobitsu({ method: 'DOMStorage.enable' }) | ||
sendToChobitsu({ method: 'DOM.enable' }) | ||
sendToChobitsu({ method: 'CSS.enable' }) | ||
sendToChobitsu({ method: 'Overlay.enable' }) | ||
sendToDevtools({ method: 'DOM.documentUpdated' }) | ||
}, 0) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,195 @@ | ||
import each from 'licia/each' | ||
import map from 'licia/map' | ||
import isStr from 'licia/isStr' | ||
import isArr from 'licia/isArr' | ||
import * as objManager from '../lib/objManager' | ||
|
||
const indexedDB = window.indexedDB | ||
|
||
let databaseVersions: any = {} | ||
|
||
export async function requestDatabaseNames() { | ||
const databases = await indexedDB.databases() | ||
const databaseNames: string[] = [] | ||
|
||
databaseVersions = {} | ||
each(databases, database => { | ||
if (!database.name) { | ||
return | ||
} | ||
databaseVersions[database.name] = database.version | ||
databaseNames.push(database.name) | ||
}) | ||
|
||
return { | ||
databaseNames, | ||
} | ||
} | ||
|
||
export async function requestDatabase(params: any) { | ||
const { databaseName } = params | ||
const version = databaseVersions[databaseName] | ||
const objectStores: any[] = [] | ||
|
||
const db = await promisify(indexedDB.open(databaseName)) | ||
if (db.objectStoreNames.length) { | ||
const storeList = map(db.objectStoreNames, name => { | ||
return db.transaction(name, 'readonly').objectStore(name) | ||
}) | ||
each(storeList, store => { | ||
const indexes: any[] = [] | ||
each(store.indexNames, indexName => { | ||
const index = store.index(indexName) | ||
indexes.push({ | ||
name: index.name, | ||
multiEntry: index.multiEntry, | ||
keyPath: formatKeyPath(index.keyPath), | ||
unique: index.unique, | ||
}) | ||
}) | ||
objectStores.push({ | ||
name: store.name, | ||
indexes, | ||
keyPath: formatKeyPath(store.keyPath), | ||
autoIncrement: store.autoIncrement, | ||
}) | ||
}) | ||
} | ||
|
||
return { | ||
databaseWithObjectStores: { | ||
name: databaseName, | ||
objectStores, | ||
version, | ||
}, | ||
} | ||
} | ||
|
||
export async function requestData(params: any) { | ||
const { databaseName, objectStoreName, indexName, pageSize, skipCount } = | ||
params | ||
|
||
const db = await promisify(indexedDB.open(databaseName)) | ||
const objectStore = db | ||
.transaction(objectStoreName, 'readonly') | ||
.objectStore(objectStoreName) | ||
const count = await promisify(objectStore.count()) | ||
|
||
let currentIdx = 0 | ||
let cursorRequest: IDBRequest<IDBCursorWithValue | null> | ||
if (indexName) { | ||
const index = objectStore.index(indexName) | ||
cursorRequest = index.openCursor() | ||
} else { | ||
cursorRequest = objectStore.openCursor() | ||
} | ||
return new Promise((resolve, reject) => { | ||
const objectStoreDataEntries: any[] = [] | ||
cursorRequest.addEventListener('success', () => { | ||
const cursor = cursorRequest.result | ||
if (cursor && currentIdx < pageSize + skipCount) { | ||
if (currentIdx >= skipCount) { | ||
objectStoreDataEntries.push({ | ||
key: objManager.wrap(cursor.key, { | ||
generatePreview: true, | ||
}), | ||
primaryKey: objManager.wrap(cursor.primaryKey), | ||
value: objManager.wrap(cursor.value, { | ||
generatePreview: true, | ||
}), | ||
}) | ||
} | ||
cursor.continue() | ||
currentIdx++ | ||
} else { | ||
resolve({ | ||
hasMore: currentIdx < count, | ||
objectStoreDataEntries, | ||
}) | ||
} | ||
}) | ||
cursorRequest.addEventListener('error', reject) | ||
}) | ||
} | ||
|
||
export async function getMetadata(params: any) { | ||
const { databaseName, objectStoreName } = params | ||
|
||
const objectStore = await getObjectStore(databaseName, objectStoreName) | ||
|
||
return { | ||
entriesCount: await promisify(objectStore.count()), | ||
keyGeneratorValue: 1, | ||
} | ||
} | ||
|
||
export async function deleteObjectStoreEntries(params: any) { | ||
const { databaseName, objectStoreName, keyRange } = params | ||
|
||
const objectStore = await getObjectStore(databaseName, objectStoreName) | ||
await promisify( | ||
objectStore.delete( | ||
IDBKeyRange.bound( | ||
getKeyRangeBound(keyRange.lower), | ||
getKeyRangeBound(keyRange.upper), | ||
keyRange.lowerOpen, | ||
keyRange.upperOpen | ||
) | ||
) | ||
) | ||
} | ||
|
||
export async function clearObjectStore(params: any) { | ||
const { databaseName, objectStoreName } = params | ||
|
||
const objectStore = await getObjectStore(databaseName, objectStoreName) | ||
await promisify(objectStore.clear()) | ||
} | ||
|
||
export async function deleteDatabase(params: any) { | ||
await promisify(indexedDB.deleteDatabase(params.databaseName)) | ||
} | ||
|
||
function getKeyRangeBound(key: any) { | ||
return key.number || key.string || key.date || key.array || key | ||
} | ||
|
||
async function getObjectStore(databaseName: string, objectStoreName: string) { | ||
const db = await promisify(indexedDB.open(databaseName)) | ||
const objectStore = db | ||
.transaction(objectStoreName, 'readwrite') | ||
.objectStore(objectStoreName) | ||
|
||
return objectStore | ||
} | ||
|
||
function formatKeyPath(keyPath: any) { | ||
if (isStr(keyPath)) { | ||
return { | ||
type: 'string', | ||
string: keyPath, | ||
} | ||
} | ||
|
||
if (isArr(keyPath)) { | ||
return { | ||
type: 'array', | ||
array: keyPath, | ||
} | ||
} | ||
|
||
return { | ||
type: 'null', | ||
} | ||
} | ||
|
||
function promisify<T = any>(req: IDBRequest<T>): Promise<T> { | ||
return new Promise((resolve, reject) => { | ||
req.addEventListener('success', () => { | ||
resolve(req.result) | ||
}) | ||
req.addEventListener('error', () => { | ||
reject() | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters