import * as azure from "@pulumi/azure" ; import * as cloud from "@pulumi/cloud" ; import * as pulumi from "@pulumi/pulumi" ; import * as stream from "stream" ; import * as shared from "./shared" ; import * as azStorage from "azure-storage" ; import * as streamBuffers from "stream-buffers" ; export class Bucket extends pulumi . ComponentResource implements cloud . Bucket { public readonly storageAccount : azure . storage . Account ; public readonly container : azure . storage . Container ; public onPut : ( name : string , handler : cloud . BucketHandler , filter : cloud . BucketFilter | undefined ) => void ; public onDelete : ( name : string , handler : cloud . BucketHandler , filter : cloud . BucketFilter | undefined ) => void ; public get : ( key : string ) => Promise < Buffer > ; public put : ( key : string , contents : Buffer ) => Promise < void > ; public delete : ( key : string ) => Promise < void > ; public constructor ( name , opts ? ) { super ( "cloud:bucket:Bucket" , name , { } , opts ) ; const preventDestroy = opts && opts . protect ; const storageAccount = shared . getGlobalStorageAccount ( ) ; this . storageAccount = storageAccount ; const container = new azure . storage . Container ( name , { storageAccountName : storageAccount . name , } , { parent : this , protect : preventDestroy } ) ; this . container = container ; this . get = async ( key ) => { const writableStream = new streamBuffers . WritableStreamBuffer ( ) ; const service = new azStorage . BlobService ( storageAccount . name . get ( ) ) ; await new Promise ( ( resolve , reject ) => { service . getBlobToStream ( container . name . get ( ) , key , writableStream , err => { if ( err ) { return reject ( err ) ; } resolve ( ) ; } ) ; } ) ; return writableStream . getContents ( ) || Buffer . from ( [ ] ) ; } ; this . put = async ( key , contents ) => { const service = new azStorage . BlobService ( storageAccount . name . get ( ) ) ; const readableStream = new ReadableStream ( contents ) ; await new Promise ( ( resolve , reject ) => { service . createBlockBlobFromStream ( container . name . get ( ) , key , readableStream , contents . length , err => { if ( err ) { return reject ( err ) ; } resolve ( ) ; } ) ; } ) ; } ; this . delete = async ( key ) => { const service = new azStorage . BlobService ( storageAccount . name . get ( ) ) ; await new Promise ( ( resolve , reject ) => { service . deleteBlob ( container . name . get ( ) , key , ( err ) => { if ( err ) { return reject ( err ) ; } resolve ( ) ; } ) ; } ) ; } ; this . onPut = async ( putName , handler , filter ) => { filter = filter || { } ; this . container . onBlobEvent ( putName , { account : storageAccount , callback : ( context , buffer ) => { handler ( { key : context . bindingData . blobTrigger , eventTime : context . bindingData . sys . utcNow , size : buffer . length , } ) . then ( ( ) => context . done ( ) ) ; } , filterPrefix : filter . keyPrefix , filterSuffix : filter . keySuffix , } , { parent : this } ) ; } ; this . onDelete = async ( delName , handler , filter ) => { throw new Error ( "@Bucket.onDelete is not yet implemented for @pulumi/cloud-azure." ) ; } ; this . registerOutputs ( { container : this . container , } ) ; } } class ReadableStream extends stream . Readable { public constructor ( private buffer ) { super ( ) ; } public _read ( ) { if ( this . buffer ) { this . push ( this . buffer ) ; this . buffer = undefined ; } } }