Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 38 additions & 10 deletions rn-nodeify/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import { StyleSheet, Text, View } from "react-native";

import ipfsClient from "ipfs-http-client";

const { urlSource, globSource } = ipfsClient;

// When not using the async iterators Babel plugin, you need to get the
// dist version of the file
// const ipfsClient = require("ipfs-http-client/dist/index.js");
Expand All @@ -36,7 +38,7 @@ import ipfsClient from "ipfs-http-client";

const ipfs = ipfsClient({
host: "localhost",
port: "5001",
port: "5002", // 5001 for go-ipfs
protocol: "http"
});
// const ipfs = ipfsClient({
Expand All @@ -60,9 +62,9 @@ export default function App() {
console.log("HASH", hash);
console.log("Symbol", Symbol);
console.log("Symbol.iterator", Symbol.iterator);
console.log("Getting updates");
console.log("window.Request", window.Request);
console.log("global.Request", global.Request);
// console.log("Getting updates");
// console.log("window.Request", window.Request);
// console.log("global.Request", global.Request);

// Note: Something is up with the JSON parsing when using dag.put rather
// than add
Expand Down Expand Up @@ -94,24 +96,50 @@ export default function App() {
// needs to be newline-delimited JSON according to the spec
// like `['{"id": 1}\n', '{"id"', ': 2}', '\n{"id": 3}\n']`
//
// Before add was returned an async iterable
// const int = await ipfs.add(Buffer.from("hello native"));
// console.log("TCL: test -> int", int);
//
// Now that add returns an async iterable:
const int = await ipfs.add(Buffer.from("hello native"));
function getRandomInt(max) {
return Math.floor(Math.random() * Math.floor(max));
}
const randomInt = getRandomInt(10000);
const randomString = `hello ${randomInt}`;
console.log({ randomString });
const randomBuffer = Buffer.from(randomString);
console.log({ randomBuffer });
console.log("randomBuffer string", randomBuffer.toJSON());

const int = await ipfs.add(randomBuffer);
for await (let chunk of int) {
// console.log({ chunk });
console.log("chunk", chunk);
}

// const dagResult = await ipfs.dag.put({ test: randomString });
// console.log({ dagResult });

//
// const experiment = require("./experiment.json");
// const experiment = globSource("./experiment.json");
// const resultForLocalFile = await ipfs.add(experiment);
// const resultForLocalFile = await ipfs.add("./experiment.json");
// for await (let chunk of resultForLocalFile) {
// // console.log({ chunk });
// console.log("chunk", chunk);
// }
//
// const int2 = await ipfs.add(
// urlSource("https://ipfs.io/images/ipfs-logo.svg")
// );
// for await (let chunk of int2) {
// // console.log({ chunk });
// console.log("chunk", chunk);
// }
//
// console.log("About to make ky request");
// (async () => {
// console.log("About to make ky request");
// const parsed = await ky
// .post("https://example.com", { json: { foo: true } })
// .json();

// console.log(parsed);
// //=> `{data: '🦄'}`
// })();
Expand Down
1 change: 1 addition & 0 deletions rn-nodeify/experiment.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "experiment": 1 }
4 changes: 2 additions & 2 deletions rn-nodeify/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
"events": "^1.1.1",
"expo": "^36.0.2",
"https-browserify": "0.0.1",
"ipfs-http-client": "file:/Users/paulcowgill/Code/contract-work/3box/react-native-ipfs-http-client/rn-nodeify/.connect-deps-cache/ipfs-http-client-42.0.0-pre.0-1580924045131.tgz",
"ky": "file:/Users/paulcowgill/Code/contract-work/3box/react-native-ipfs-http-client/rn-nodeify/.connect-deps-cache/ky-0.15.1-1580923943181.tgz",
"ipfs-http-client": "file:/Users/paulcowgill/Code/contract-work/3box/front-ends/react-native-ipfs-http-client/rn-nodeify/.connect-deps-cache/ipfs-http-client-42.0.0-1583877254225.tgz",
"ky": "0.15.0",
"ky-universal": "^0.3.0",
"path-browserify": "0.0.0",
"process": "^0.11.10",
Expand Down
16 changes: 13 additions & 3 deletions rn-nodeify/shim.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { URL, URLSearchParams } from "whatwg-url";

console.log({ URL });
// console.log({ URL });

global.URL = URL;
global.URLSearchParams = URLSearchParams;
Expand Down Expand Up @@ -225,6 +225,7 @@ var clz32 =
};
var fromCharCode = String.fromCharCode;
var Object_prototype_toString = {}.toString;
// console.log("Object_prototype_toString", Object_prototype_toString);
var NativeSharedArrayBuffer = window["SharedArrayBuffer"];
var sharedArrayBufferString = NativeSharedArrayBuffer
? Object_prototype_toString.call(NativeSharedArrayBuffer)
Expand All @@ -235,6 +236,7 @@ var arrayBufferString = Object_prototype_toString.call(
(NativeUint8Array ? ArrayBuffer : patchedU8Array).prototype
);
function decoderReplacer(encoded) {
console.log({ encoded });
var codePoint = encoded.charCodeAt(0) << 24;
var leadingOnes = clz32(~codePoint) | 0;
var endPos = 0,
Expand Down Expand Up @@ -266,6 +268,7 @@ TextDecoder["prototype"]["decode"] = function(inputArrayOrBuffer) {
var buffer =
(inputArrayOrBuffer && inputArrayOrBuffer.buffer) || inputArrayOrBuffer;
var asObjectString = Object_prototype_toString.call(buffer);
console.log({ asObjectString });
if (
asObjectString !== arrayBufferString &&
asObjectString !== sharedArrayBufferString
Expand All @@ -287,8 +290,15 @@ TextDecoder["prototype"]["decode"] = function(inputArrayOrBuffer) {
(index + 32768) | 0
)
);

return resultingString.replace(/[\xc0-\xff][\x80-\xbf]*/g, decoderReplacer);
// TODO: Determine how this is turning into "\":\"QmbFMke1KXqnYyBBWxB74N4c5SBnJMVAiMNRcGu6x1AwQH\",\"Hash\":\"QmbFMke1KXqnYyBBWxB74N4c5SBnJMVAiMNRcGu6x1AwQH\",\"Size\":\"6\"
// console.log({ resultingString });

const finalResult = resultingString.replace(
/[\xc0-\xff][\x80-\xbf]*/g,
decoderReplacer
);
console.log("last decoder step complete", finalResult);
return finalResult;
};

global.TextDecoder = TextDecoder;
Expand Down
Loading