forked from parcel-bundler/parcel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Upstream some changes from the REPL (parcel-bundler#7208)
- Loading branch information
1 parent
5ac8141
commit 6a87c0c
Showing
29 changed files
with
262 additions
and
46 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,120 @@ | ||
// @flow strict-local | ||
import type {Cache} from './types'; | ||
|
||
import {Readable} from 'stream'; | ||
import {serialize, deserialize, registerSerializableClass} from '@parcel/core'; | ||
import {bufferStream} from '@parcel/utils'; | ||
// $FlowFixMe[untyped-import] | ||
import packageJson from '../package.json'; | ||
// $FlowFixMe[untyped-import] | ||
import {openDB} from 'idb'; | ||
|
||
const STORE_NAME = 'cache'; | ||
|
||
export class IDBCache implements Cache { | ||
// $FlowFixMe | ||
store: any; | ||
|
||
constructor() { | ||
this.store = openDB('REPL-parcel-cache', 1, { | ||
upgrade(db) { | ||
db.createObjectStore(STORE_NAME); | ||
}, | ||
blocked() {}, | ||
blocking() {}, | ||
terminated() {}, | ||
}); | ||
} | ||
|
||
ensure(): Promise<void> { | ||
return Promise.resolve(); | ||
} | ||
|
||
serialize(): {||} { | ||
return {...null}; | ||
} | ||
|
||
static deserialize(): IDBCache { | ||
return new IDBCache(); | ||
} | ||
|
||
has(key: string): Promise<boolean> { | ||
return Promise.resolve(this.store.get(key) != null); | ||
} | ||
|
||
async get<T>(key: string): Promise<?T> { | ||
let data = await (await this.store).get(STORE_NAME, key); | ||
if (data == null) { | ||
return null; | ||
} | ||
|
||
return Promise.resolve(deserialize(data)); | ||
} | ||
|
||
async set(key: string, value: mixed): Promise<void> { | ||
await (await this.store).put(STORE_NAME, serialize(value), key); | ||
} | ||
|
||
getStream(key: string): Readable { | ||
let dataPromise = this.store | ||
.then(s => s.get(STORE_NAME, key)) | ||
.then(d => Buffer.from(d)) | ||
.catch(e => e); | ||
const stream = new Readable({ | ||
// $FlowFixMe(incompatible-call) | ||
async read() { | ||
let data = await dataPromise; | ||
if (data instanceof Error) { | ||
stream.emit('error', data); | ||
} else { | ||
stream.push(Buffer.from(data)); | ||
stream.push(null); | ||
} | ||
}, | ||
}); | ||
|
||
return stream; | ||
} | ||
|
||
async setStream(key: string, stream: Readable): Promise<void> { | ||
let buf = await bufferStream(stream); | ||
await (await this.store).put(STORE_NAME, buf, key); | ||
} | ||
|
||
async getBlob(key: string): Promise<Buffer> { | ||
let data = await (await this.store).get(STORE_NAME, key); | ||
if (data == null) { | ||
return Promise.reject(new Error(`Key ${key} not found in cache`)); | ||
} | ||
return Buffer.from(data.buffer); | ||
} | ||
|
||
async setBlob(key: string, contents: Buffer | string): Promise<void> { | ||
let data = | ||
contents instanceof Uint8Array ? contents : Buffer.from(contents); | ||
await (await this.store).put(STORE_NAME, data, key); | ||
} | ||
|
||
async getBuffer(key: string): Promise<?Buffer> { | ||
let data = await (await this.store).get(STORE_NAME, key); | ||
if (data == null) { | ||
return null; | ||
} | ||
|
||
return Buffer.from(data.buffer); | ||
} | ||
|
||
hasLargeBlob(key: string): Promise<boolean> { | ||
return this.has(key); | ||
} | ||
|
||
getLargeBlob(key: string): Promise<Buffer> { | ||
return this.getBlob(key); | ||
} | ||
|
||
setLargeBlob(key: string, contents: Buffer | string): Promise<void> { | ||
return this.setBlob(key, contents); | ||
} | ||
} | ||
|
||
registerSerializableClass(`${packageJson.version}:IDBCache`, IDBCache); |
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,9 @@ | ||
// @flow strict-local | ||
import type {Cache} from './types'; | ||
|
||
// $FlowFixMe | ||
export class IDBCache implements Cache { | ||
constructor() { | ||
throw new Error('IDBCache is only supported in the browser'); | ||
} | ||
} |
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
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
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,8 @@ | ||
// @flow | ||
import {Buffer} from 'buffer'; | ||
import * as msgpackr from 'msgpackr'; | ||
|
||
let encoder = new msgpackr.Encoder({structuredClone: true}); | ||
|
||
export let serializeRaw: any => Buffer = v => Buffer.from(encoder.encode(v)); | ||
export let deserializeRaw: Buffer => any = v => encoder.decode(v); |
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,7 @@ | ||
// @flow | ||
import v8 from 'v8'; | ||
|
||
// $FlowFixMe - Flow doesn't know about this method yet | ||
export let serializeRaw: any => Buffer = v8.serialize; | ||
// $FlowFixMe - Flow doesn't know about this method yet | ||
export let deserializeRaw: Buffer => any = v8.deserialize; |
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
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,9 @@ | ||
// @flow | ||
import type {FileSystem} from './types'; | ||
|
||
// $FlowFixMe[prop-missing] handled by the throwing constructor | ||
export class NodeFS implements FileSystem { | ||
constructor() { | ||
throw new Error("NodeFS isn't available in the browser"); | ||
} | ||
} |
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
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 was deleted.
Oops, something went wrong.
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
Oops, something went wrong.