{"action":"created","issue":{"url":"https://api.github.com/repos/stockulus/pouchdb-react-native/issues/68","repository_url":"https://api.github.com/repos/stockulus/pouchdb-react-native","labels_url":"https://api.github.com/repos/stockulus/pouchdb-react-native/issues/68/labels{/name}","comments_url":"https://api.github.com/repos/stockulus/pouchdb-react-native/issues/68/comments","events_url":"https://api.github.com/repos/stockulus/pouchdb-react-native/issues/68/events","html_url":"https://github.com/stockulus/pouchdb-react-native/issues/68","id":232695159,"number":68,"title":"Attachments Support","user":{"login":"jurassix","id":1869117,"avatar_url":"https://avatars3.githubusercontent.com/u/1869117?v=4","gravatar_id":"","url":"https://api.github.com/users/jurassix","html_url":"https://github.com/jurassix","followers_url":"https://api.github.com/users/jurassix/followers","following_url":"https://api.github.com/users/jurassix/following{/other_user}","gists_url":"https://api.github.com/users/jurassix/gists{/gist_id}","starred_url":"https://api.github.com/users/jurassix/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jurassix/subscriptions","organizations_url":"https://api.github.com/users/jurassix/orgs","repos_url":"https://api.github.com/users/jurassix/repos","events_url":"https://api.github.com/users/jurassix/events{/privacy}","received_events_url":"https://api.github.com/users/jurassix/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":{"login":"jurassix","id":1869117,"avatar_url":"https://avatars3.githubusercontent.com/u/1869117?v=4","gravatar_id":"","url":"https://api.github.com/users/jurassix","html_url":"https://github.com/jurassix","followers_url":"https://api.github.com/users/jurassix/followers","following_url":"https://api.github.com/users/jurassix/following{/other_user}","gists_url":"https://api.github.com/users/jurassix/gists{/gist_id}","starred_url":"https://api.github.com/users/jurassix/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jurassix/subscriptions","organizations_url":"https://api.github.com/users/jurassix/orgs","repos_url":"https://api.github.com/users/jurassix/repos","events_url":"https://api.github.com/users/jurassix/events{/privacy}","received_events_url":"https://api.github.com/users/jurassix/received_events","type":"User","site_admin":false},"assignees":[{"login":"jurassix","id":1869117,"avatar_url":"https://avatars3.githubusercontent.com/u/1869117?v=4","gravatar_id":"","url":"https://api.github.com/users/jurassix","html_url":"https://github.com/jurassix","followers_url":"https://api.github.com/users/jurassix/followers","following_url":"https://api.github.com/users/jurassix/following{/other_user}","gists_url":"https://api.github.com/users/jurassix/gists{/gist_id}","starred_url":"https://api.github.com/users/jurassix/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jurassix/subscriptions","organizations_url":"https://api.github.com/users/jurassix/orgs","repos_url":"https://api.github.com/users/jurassix/repos","events_url":"https://api.github.com/users/jurassix/events{/privacy}","received_events_url":"https://api.github.com/users/jurassix/received_events","type":"User","site_admin":false}],"milestone":null,"comments":21,"created_at":"2017-05-31T21:02:24Z","updated_at":"2018-02-13T18:55:43Z","closed_at":null,"author_association":"COLLABORATOR","body":"## This issue is to act as a placeholder for work remaining to fully support Attachments.\r\n\r\n### The remaining issues are as follows:\r\n\r\n1. On replication **to** remote CouchDB where the local doc contains an attachment it should successfully sync the doc and attachment data. **If doc contains an attachment sync fails.**\r\n1. On replication **from** remote CouchDB were the remote doc contain an attachment it should successfully sync the doc and attachment data.  **If doc contains an attachment sync fails.**\r\n\r\n### Considerations unique to ReactNative: \r\n\r\n_(Note: these following information is based on my observations, some accuracy may be missing, please correct where appropriate. I'm still learning how the ReactNative packager works.)_\r\n\r\n1. ReactNative has 2 runtimes V8 in Debug and JSC in Release.\r\n    1. During Debug (simulator build in Debug) the application is packaged targeting the browser (Chrome V8) and we have access to the **window** and **DOM** and other browser native implementations e.g. Blob\r\n    1. During JSC Release (simulator build in Release) the application is packaged targeting JSC and we do **not** have access to the **window** and **DOM**. JSC isn't at feature parity with V8. Blob is replaced with node's Buffer.\r\n1. Most polyfills fallback to native browser when available, which causes some interesting results between Debug and Release.\r\n1. ReactNative only uses fetch\r\n1. PouchDB has many dependencies - adapters, plugins, utils - each of these are packages for node _or_ browser. However, ReactNative packager always chooses the **browser** version during package resolution (based on my testing)\r\n\r\n### Research:\r\n\r\n1. This library currently has a hack during the [`postinstal` phase](https://github.com/stockulus/pouchdb-react-native/blob/master/packages/pouchdb-react-native/setup.js), where the `pouchdb-binary-utils/package.json` is patched to force the ReactNative packager to chose the **node** versions of the library.\r\n1. If I remove the above hack locally:\r\n    1. I can successfully get **sync** to work for all use cases, on iOS simulator in V8 Debug. This is due to the reliance on both this library and all Pouchdb dependencies choosing browser installs, and and native browser lib implementations, all the way down.\r\n    1. During JSC release mode, on iOS simulator, I receive Blobs from the `pouchdb-adapter-http`, which in turn delegated to `pouchdb-ajax`. Since ReactNative runtime JSC has no support for **FileReader/FileReaderSync** we do not have a utility to transform a `Blob` to `Buffer` or `Base64`.  [more context here](https://github.com/pouchdb/pouchdb/pull/5687)\r\n         1. I believe we can get an `ArrayBuffer` in all cases instead of returning a` Blob` and be in a better situation. More research needed here.\r\n         1. We also cannot opt for the the node version of `pouchdb-ajax` because it relies on `request` which will not work in ReactNative without hacks to expose additional core node modules.\r\n\r\n### Next Steps:\r\n\r\n1. I've been building out test cases for Attachments. All scenarios pass. This is due to our test suite being run in a fully node.js context and doesn't represent the ReactNative runtime. In the future it would be beneficial to extend coverage to more environment.\r\n1.  I've currently forked both `pouchdb-adapter-http` and `pouchdb-ajax` and have created ReactNative versions of these and am experimenting with getting full support for both Debug and Release. For these versions I'm supporting only `fetch` and always returning `ArrayBuffer` never `Blob`. This is getting close. However, many tests are not passing locally.\r\n1. I believe if we can patch the above utilities then we will have full - or very close - support for Attachments in this library, for both the Debug and Release contexts.\r\n1. I am worried that this effort could snowball into to essentially forking PouchDB, but right now I'm not seeing the need for this.\r\n1. I could use some additional direction in terms of patching `pouchdb-adapter-http`. My current build hasn't made any changes except to the new  **pouchdb-ajax** delegation library. There is currently no way to provide _strategies_ without having direct access to the PouchDB instance. Long term this will not be maintainable without a strategy pattern in place at **plugin registry**. Also, there may be much better ways of injecting a different implementation, will need some direction from @nolanlawson e.g. \r\n```js\r\nconst db = new PouchDB('local');\r\ndb._ajax = fetchCore;\r\n```"},"comment":{"url":"https://api.github.com/repos/stockulus/pouchdb-react-native/issues/comments/365367247","html_url":"https://github.com/stockulus/pouchdb-react-native/issues/68#issuecomment-365367247","issue_url":"https://api.github.com/repos/stockulus/pouchdb-react-native/issues/68","id":365367247,"user":{"login":"MrGurns","id":15333090,"avatar_url":"https://avatars0.githubusercontent.com/u/15333090?v=4","gravatar_id":"","url":"https://api.github.com/users/MrGurns","html_url":"https://github.com/MrGurns","followers_url":"https://api.github.com/users/MrGurns/followers","following_url":"https://api.github.com/users/MrGurns/following{/other_user}","gists_url":"https://api.github.com/users/MrGurns/gists{/gist_id}","starred_url":"https://api.github.com/users/MrGurns/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/MrGurns/subscriptions","organizations_url":"https://api.github.com/users/MrGurns/orgs","repos_url":"https://api.github.com/users/MrGurns/repos","events_url":"https://api.github.com/users/MrGurns/events{/privacy}","received_events_url":"https://api.github.com/users/MrGurns/received_events","type":"User","site_admin":false},"created_at":"2018-02-13T18:55:43Z","updated_at":"2018-02-13T18:55:43Z","author_association":"NONE","body":"Subscribing."}}
{"actor":{"display_login":"MrGurns"}}