From 5ac38b1c371193e69ca2b75bee8b0d4bc14ed0a0 Mon Sep 17 00:00:00 2001 From: Oliverio Sousa <47525443+oliveriosousa@users.noreply.github.com> Date: Mon, 2 Aug 2021 17:17:59 +0100 Subject: [PATCH] feat: example with deno --- examples/ipfs-101-deno/hello.txt | 1 + examples/ipfs-101-deno/index.ts | 33 ++++++++++++++++++++++++++++ examples/ipfs-101-deno/jspm.ts | 27 +++++++++++++++++++++++ examples/ipfs-101-deno/package.json | 21 ++++++++++++++++++ examples/ipfs-101-deno/tests/test.js | 11 ++++++++++ examples/ipfs-101/package.json | 2 +- 6 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 examples/ipfs-101-deno/hello.txt create mode 100755 examples/ipfs-101-deno/index.ts create mode 100755 examples/ipfs-101-deno/jspm.ts create mode 100644 examples/ipfs-101-deno/package.json create mode 100644 examples/ipfs-101-deno/tests/test.js diff --git a/examples/ipfs-101-deno/hello.txt b/examples/ipfs-101-deno/hello.txt new file mode 100644 index 00000000..0c6da6cc --- /dev/null +++ b/examples/ipfs-101-deno/hello.txt @@ -0,0 +1 @@ +Hello, how are you today? Welcome to the Distributed Web! diff --git a/examples/ipfs-101-deno/index.ts b/examples/ipfs-101-deno/index.ts new file mode 100755 index 00000000..ff103e76 --- /dev/null +++ b/examples/ipfs-101-deno/index.ts @@ -0,0 +1,33 @@ +import { createRequire } from "https://deno.land/std@0.102.0/node/module.ts"; + +// import.meta.url will be the location of "this" module (like `__filename` in +// Node.js), and then serve as the root for your "package", where the +// `package.json` is expected to be, and where the `node_modules` will be used +// for resolution of packages. +const require = createRequire(import.meta.url); + +const all = require('it-all') +const uint8ArrayConcat = require('uint8arrays/concat') +const uint8ArrayFromString = require('uint8arrays/from-string') +const uint8ArrayToString = require('uint8arrays/to-string') +const IPFS = require("ipfs"); + +async function main () { + const node = await IPFS.create() + const version = await node.version() + + console.log('Version:', version.version) + + const file = await node.add({ + path: 'hello.txt', + content: uint8ArrayFromString('Hello World 101') + }) + + console.log('Added file:', file.path, file.cid.toString()) + + const data = uint8ArrayConcat(await all(node.cat(file.cid))) + + console.log('Added file contents:', uint8ArrayToString(data)) +} + +main() diff --git a/examples/ipfs-101-deno/jspm.ts b/examples/ipfs-101-deno/jspm.ts new file mode 100755 index 00000000..2f50d6b1 --- /dev/null +++ b/examples/ipfs-101-deno/jspm.ts @@ -0,0 +1,27 @@ +'use strict' + +import all from 'https://dev.jspm.io/it-all' +import uint8ArrayConcat from 'https://dev.jspm.io/uint8arrays/concat' +import uint8ArrayFromString from 'https://dev.jspm.io/uint8arrays/from-string' +import uint8ArrayToString from 'https://dev.jspm.io/uint8arrays/to-string' +import IPFS from 'https://dev.jspm.io/ipfs' + +async function main () { + const node = await IPFS.create() + const version = await node.version() + + console.log('Version:', version.version) + + const file = await node.add({ + path: 'hello.txt', + content: uint8ArrayFromString('Hello World 101') + }) + + console.log('Added file:', file.path, file.cid.toString()) + + const data = uint8ArrayConcat(await all(node.cat(file.cid))) + + console.log('Added file contents:', uint8ArrayToString(data)) +} + +main() diff --git a/examples/ipfs-101-deno/package.json b/examples/ipfs-101-deno/package.json new file mode 100644 index 00000000..58c9e91a --- /dev/null +++ b/examples/ipfs-101-deno/package.json @@ -0,0 +1,21 @@ +{ + "name": "example-ipfs-101-deno", + "version": "1.0.0", + "private": true, + "description": "Using ipfs with deno", + "license": "MIT", + "scripts": { + "clean": "echo 'Nothing to clean...'", + "start": "deno run --allow-read=node_modules --unstable --allow-env index.ts", + "serve": "npm run start", + "test:example": "node tests/test.js" + }, + "devDependencies": { + "test-util-ipfs-example": "^1.0.2" + }, + "dependencies": { + "ipfs": "^0.56.0", + "it-all": "^1.0.4", + "uint8arrays": "^2.1.6" + } +} diff --git a/examples/ipfs-101-deno/tests/test.js b/examples/ipfs-101-deno/tests/test.js new file mode 100644 index 00000000..5e9d98f8 --- /dev/null +++ b/examples/ipfs-101-deno/tests/test.js @@ -0,0 +1,11 @@ +'use strict' + +const { node } = require('test-util-ipfs-example'); +const path = require('path') + +async function test () { + await node.waitForOutput('Added file contents: Hello World 101', 'node', [path.resolve(__dirname, '../index.js')]) + await node.waitForOutput('Added file contents: Hello World 101', 'node', [path.resolve(__dirname, '../jspm.js')]) +} + +test(); diff --git a/examples/ipfs-101/package.json b/examples/ipfs-101/package.json index f23af356..d6bb488c 100644 --- a/examples/ipfs-101/package.json +++ b/examples/ipfs-101/package.json @@ -5,7 +5,7 @@ "description": "this package.json needs to exist because of new npm config https://github.com/ipfs/js-ipfs/issues/977#issuecomment-326741092", "license": "MIT", "author": "David Dias ", - "main": "1.js", + "main": "index.js", "scripts": { "clean": "echo 'Nothing to clean...'", "start": "node index.js",