Skip to content

Commit

Permalink
chore: update examples
Browse files Browse the repository at this point in the history
  • Loading branch information
dtfiedler committed Sep 19, 2023
1 parent 2524447 commit 264f092
Show file tree
Hide file tree
Showing 8 changed files with 2,322 additions and 19 deletions.
18 changes: 11 additions & 7 deletions examples/node/index.cjs → examples/cjs/index.cjs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
const path = require('path');
const fs = require('fs');

const Arweave = require('arweave');
const {
TurboFactory,
TurboUnauthenticatedPaymentService,
} = require('@ardrive/turbo-sdk/node');

// immediately invoked function expression
(async () => {
const { default: Arweave } = await import('arweave');
const { TurboFactory, TurboUnauthenticatedPaymentService } = await import(
'../../lib/node/index.js'
);
const path = require('path');
const fs = require('fs');
/**
* Fetching rates using an unauthenticated Turbo client.
*/
Expand Down Expand Up @@ -65,7 +69,7 @@
* Post local files to the Turbo service.
*/
console.log('Posting raw file to Turbo service...');
const filePath = path.join(__dirname, './files/0_kb.txt');
const filePath = path.join(__dirname, '../files/0_kb.txt');
const uploadResult = await turboAuthClient.uploadFile({
fileStreamFactory: () => fs.createReadStream(filePath),
signal: AbortSignal.timeout(10_000), // cancel the upload after 10 seconds
Expand Down
6 changes: 6 additions & 0 deletions examples/cjs/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"type": "commonjs",
"dependencies": {
"@ardrive/turbo-sdk": "^1.0.0-alpha.18"
}
}
1,140 changes: 1,140 additions & 0 deletions examples/cjs/yarn.lock

Large diffs are not rendered by default.

File renamed without changes.
27 changes: 17 additions & 10 deletions examples/node/index.mjs → examples/mjs/index.mjs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import Arweave from 'arweave';
import fs from 'fs';

import {
TurboFactory,
TurboUnauthenticatedPaymentService,
} from '../../lib/node/index.js';
} from '@ardrive/turbo-sdk/node';
import Arweave from 'arweave';
import fs from 'fs';

(async () => {
/**
Expand All @@ -14,7 +13,7 @@ import {
const rates = await turbo.getFiatRates();
console.log('Fetched rates:', JSON.stringify(rates, null, 2));

/*
/**
* Alternatively instantiate your own clients independently.
*/
const paymentService = new TurboUnauthenticatedPaymentService({
Expand All @@ -27,12 +26,20 @@ import {
);

/**
* Fetching balance using an authenticated Turbo client.
* Create a new arweave private key
*/
const arweave = new Arweave.init();
const arweave = new Arweave({});
const jwk = await Arweave.crypto.generateJWK();
const address = await arweave.wallets.jwkToAddress(jwk);

/**
* Use the arweave key to create an authenticated turbo client
*/
const turboAuthClient = TurboFactory.authenticated({ privateKey: jwk });

/**
* Fetch the balance for the private key.
*/
const balance = await turboAuthClient.getBalance();
console.log(
'Balance:',
Expand All @@ -58,11 +65,11 @@ import {
/**
* Post local files to the Turbo service.
*/
console.log('Posting raw files to Turbo service...');
const filePath = new URL('files/0_kb.txt', import.meta.url).pathname;
console.log('Posting raw file to Turbo service...');
const filePath = new URL('../files/0_kb.txt', import.meta.url).pathname;
const uploadResult = await turboAuthClient.uploadFile({
fileStreamFactory: () => fs.createReadStream(filePath),
signal: AbortSignal.timeout(10_000), // cancel the upload after 10 second
signal: AbortSignal.timeout(10_000), // cancel the upload after 10 seconds
});
console.log(JSON.stringify(uploadResult, null, 2));
})();
6 changes: 6 additions & 0 deletions examples/mjs/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"type": "module",
"dependencies": {
"@ardrive/turbo-sdk": "^1.0.0-alpha.18"
}
}
Loading

0 comments on commit 264f092

Please sign in to comment.