@pocinnovation/alumi
Version:
Dynamic pulumi provider for Aleph
143 lines (142 loc) • 6.69 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.StoreString = void 0;
const pulumi = __importStar(require("@pulumi/pulumi"));
const client_1 = require("@aleph-sdk/client");
const utils_1 = require("./utils");
const propStringContent = 'stringContent';
const propStorageEngine = 'storageEngine';
const propChannel = 'channel';
const propAccountEnvName = 'accountEnvName';
const StoreStringProvider = {
diff(id, olds, news) {
return __awaiter(this, void 0, void 0, function* () {
const replaces = [];
if (olds[propChannel] !== news[propChannel]) {
replaces.push(propChannel);
}
if (olds[propStorageEngine] !== news[propStorageEngine]) {
replaces.push(propStorageEngine);
}
if (olds.string_content_hashed !== (0, utils_1.hashData)(news[propStringContent])) {
replaces.push(propStringContent);
}
if (replaces.length === 0) {
return { changes: false };
}
return { replaces: replaces };
});
},
update(id, olds, news) {
return __awaiter(this, void 0, void 0, function* () {
throw new Error('Cannot update StoreString; Only Delete And Created');
});
},
delete(id, props) {
return __awaiter(this, void 0, void 0, function* () {
const account = yield (0, utils_1.getAccount)(props[propAccountEnvName]);
const client = new client_1.AuthenticatedAlephHttpClient(account);
yield client.forget({
channel: props[propChannel],
hashes: [props.item_hash],
sync: true,
});
});
},
create(inputs) {
return __awaiter(this, void 0, void 0, function* () {
const account = yield (0, utils_1.getAccount)(inputs[propAccountEnvName]);
const client = new client_1.AuthenticatedAlephHttpClient(account);
const stringBlob = new Blob([inputs[propStringContent]], {
type: 'text/plain',
});
const stringHash = (0, utils_1.hashData)(inputs[propStringContent]);
const res = yield client.createStore({
channel: inputs[propChannel],
fileObject: stringBlob,
storageEngine: inputs[propStorageEngine],
sync: true,
});
const out = {
// inputs
stringContent: inputs[propStringContent],
storageEngine: inputs[propStorageEngine],
accountEnvName: inputs[propAccountEnvName],
// outputs
channel: res.channel || '',
signature: res.signature,
chain: res.chain,
sender: res.sender,
type: `${res.type}`,
time: res.time,
item_type: res.item_type,
item_hash: res.item_hash,
// Created
content_address: res.content.address,
content_item_type: res.content.item_type,
content_item_hash: res.content.item_hash,
content_time: res.content.time,
aleph_explorer_url: (0, utils_1.getAlephExplorerUrl)(res.chain, res.sender, 'STORE', res.item_hash),
raw_file_url: (0, utils_1.getRawFileUrl)(res.content.item_hash),
string_content_hashed: stringHash,
};
return {
id: `${account.address}-${res.item_hash}`,
outs: out,
};
});
},
read(id, props) {
return __awaiter(this, void 0, void 0, function* () {
const account = yield (0, utils_1.getAccount)(props[propAccountEnvName]);
const client = new client_1.AuthenticatedAlephHttpClient(account);
const res = yield client.downloadFile(props.item_hash);
const raw_file_url = (0, utils_1.getRawFileUrl)(props.content_item_hash);
props[propStringContent] = new TextDecoder().decode(res);
props.string_content_hashed = (0, utils_1.hashData)(props[propStringContent]);
const out = Object.assign(Object.assign({}, props), { raw_file_url: raw_file_url });
return {
id: id,
outs: out,
};
});
},
};
class StoreString extends pulumi.dynamic.Resource {
constructor(name, props, opts) {
super(StoreStringProvider, name, Object.assign({ signature: undefined, chain: undefined, sender: undefined, type: undefined, time: undefined, item_type: undefined, item_hash: undefined, content_address: undefined, content_item_type: undefined, content_item_hash: undefined, content_time: undefined, aleph_explorer_url: undefined, raw_file_url: undefined, string_content_hashed: undefined }, props), opts);
}
}
exports.StoreString = StoreString;