-
Notifications
You must be signed in to change notification settings - Fork 302
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
nodejs: add support for storing URLs and introduce
fileReader
(#73)
* support node url storage * add tests and refactor node storage * add fingerprint for reactnative * make compatible with older node versions + catch JSON.parse exception * don't use FileStorage by default * fix rebase conflict * add option to set custom file reader
- Loading branch information
1 parent
b4069d1
commit e8aea8d
Showing
11 changed files
with
1,723 additions
and
463 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ node_modules | |
demos/reactnative/.expo | ||
lib.es5 | ||
dist | ||
.DS_Store |
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 |
---|---|---|
@@ -1,30 +1,32 @@ | ||
/* global window */ | ||
import Upload from "./upload"; | ||
import {canStoreURLs} from "./node/storage"; | ||
import * as storage from "./node/storage"; | ||
|
||
const {defaultOptions} = Upload; | ||
let isSupported; | ||
|
||
const moduleExport = { | ||
Upload, | ||
canStoreURLs: storage.canStoreURLs, | ||
defaultOptions | ||
}; | ||
|
||
if (typeof window !== "undefined") { | ||
// Browser environment using XMLHttpRequest | ||
const {XMLHttpRequest, Blob} = window; | ||
|
||
isSupported = ( | ||
moduleExport.isSupported = ( | ||
XMLHttpRequest && | ||
Blob && | ||
typeof Blob.prototype.slice === "function" | ||
); | ||
} else { | ||
// Node.js environment using http module | ||
isSupported = true; | ||
moduleExport.isSupported = true; | ||
// make FileStorage module available as it will not be set by default. | ||
moduleExport.FileStorage = storage.FileStorage; | ||
} | ||
|
||
// The usage of the commonjs exporting syntax instead of the new ECMAScript | ||
// one is actually inteded and prevents weird behaviour if we are trying to | ||
// import this module in another module using Babel. | ||
module.exports = { | ||
Upload, | ||
isSupported, | ||
canStoreURLs, | ||
defaultOptions | ||
}; | ||
module.exports = moduleExport; |
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,3 @@ | ||
const isReactNative = false; | ||
|
||
export default isReactNative; |
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 |
---|---|---|
@@ -1,15 +1,111 @@ | ||
/* eslint no-unused-vars: 0 */ | ||
import { readFile, writeFile } from "fs"; | ||
import * as lockfile from "proper-lockfile"; | ||
|
||
export const canStoreURLs = false; | ||
|
||
export function setItem(key, value) { | ||
export const canStoreURLs = true; | ||
|
||
export function getStorage() { | ||
// don't support storage by default. | ||
return null; | ||
} | ||
|
||
export function getItem(key) { | ||
|
||
} | ||
export class FileStorage { | ||
constructor(filePath) { | ||
this.path = filePath; | ||
} | ||
|
||
setItem(key, value, cb) { | ||
lockfile.lock(this.path, this._lockfileOptions(), (err, release) => { | ||
if (err) { | ||
return cb(err); | ||
} | ||
|
||
cb = this._releaseAndCb(release, cb); | ||
this._getData((err, data) => { | ||
if (err) { | ||
return cb(err); | ||
} | ||
|
||
data[key] = value; | ||
this._writeData(data, (err) => cb(err)); | ||
}); | ||
}); | ||
} | ||
|
||
getItem(key, cb) { | ||
this._getData((err, data) => { | ||
if (err) { | ||
return cb(err); | ||
} | ||
cb(null, data[key]); | ||
}); | ||
} | ||
|
||
removeItem(key, cb) { | ||
lockfile.lock(this.path, this._lockfileOptions(), (err, release) => { | ||
if (err) { | ||
return cb(err); | ||
} | ||
|
||
cb = this._releaseAndCb(release, cb); | ||
this._getData((err, data) => { | ||
if (err) { | ||
return cb(err); | ||
} | ||
|
||
delete data[key]; | ||
this._writeData(data, (err) => cb(err)); | ||
}); | ||
}); | ||
} | ||
|
||
_lockfileOptions() { | ||
return { | ||
realpath: false, | ||
retries: { | ||
retries: 5, | ||
minTimeout: 20 | ||
} | ||
}; | ||
} | ||
|
||
_releaseAndCb(release, cb) { | ||
return (err) => { | ||
if (err) { | ||
// @TODO consider combining error from release callback | ||
release(() => cb(err)); | ||
return; | ||
} | ||
|
||
release(cb); | ||
}; | ||
} | ||
|
||
export function removeItem(key) { | ||
_writeData(data, cb) { | ||
const opts = { | ||
encoding: "utf8", | ||
mode: 0o660, | ||
flag: "w" | ||
}; | ||
writeFile(this.path, JSON.stringify(data), opts, (err) => cb(err)); | ||
} | ||
|
||
_getData(cb) { | ||
readFile(this.path, "utf8", (err, data) => { | ||
if (err) { | ||
// return empty data if file does not exist | ||
err.code === "ENOENT" ? cb(null, {}) : cb(err); | ||
return; | ||
} else { | ||
try { | ||
data = !data.trim().length ? {} : JSON.parse(data); | ||
} catch (error) { | ||
cb(error); | ||
return; | ||
} | ||
cb(null, data); | ||
} | ||
}); | ||
} | ||
} |
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.