1- 'use strict' ;
2-
3- const async = require ( 'async' ) ;
4- const azure = require ( 'azure-storage' ) ;
5- const Cache = require ( 'nice-cache' ) ;
6- const format = require ( 'stringformat' ) ;
7- const fs = require ( 'fs-extra' ) ;
8- const nodeDir = require ( 'node-dir' ) ;
9- const _ = require ( 'lodash' ) ;
10- const stream = require ( 'stream' ) ;
11- const { fromCallback } = require ( 'universalify' ) ;
12-
13- const { getFileInfo, strings } = require ( 'oc-storage-adapters-utils' ) ;
14-
15- module . exports = function ( conf ) {
1+ import async from 'async' ;
2+ import azure from 'azure-storage' ;
3+ import Cache from 'nice-cache' ;
4+ import format from 'stringformat' ;
5+ import fs from 'fs-extra' ;
6+ import nodeDir from 'node-dir' ;
7+ import _ from 'lodash' ;
8+ import stream from 'stream' ;
9+ import { fromCallback } from 'universalify' ;
10+
11+ import {
12+ getFileInfo ,
13+ strings ,
14+ StorageAdapter
15+ } from 'oc-storage-adapters-utils' ;
16+
17+ export interface AzureConfig {
18+ publicContainerName : string ;
19+ privateContainerName : string ;
20+ accountName : string ;
21+ accountKey : string ;
22+ path : string ;
23+ verbosity ?: boolean ;
24+ refreshInterval ?: number ;
25+ }
26+
27+ export default function azureAdapter ( conf : AzureConfig ) : StorageAdapter {
1628 const isValid = ( ) => {
1729 if (
1830 ! conf . publicContainerName ||
@@ -33,19 +45,19 @@ module.exports = function (conf) {
3345 const getClient = ( ) =>
3446 azure . createBlobService ( conf . accountName , conf . accountKey ) ;
3547
36- const getFile = ( filePath , force , callback ) => {
48+ const getFile = ( filePath : string , force : boolean , callback : any ) => {
3749 if ( _ . isFunction ( force ) ) {
3850 callback = force ;
3951 force = false ;
4052 }
4153
42- const getFromAzure = cb => {
54+ const getFromAzure = ( cb : any ) => {
4355 getClient ( ) . getBlobToText (
4456 conf . privateContainerName ,
4557 filePath ,
4658 ( err , fileContent ) => {
4759 if ( err ) {
48- if ( err . statusCode === 404 ) {
60+ if ( ( err as any ) . statusCode === 404 ) {
4961 return cb ( {
5062 code : strings . errors . STORAGE . FILE_NOT_FOUND_CODE ,
5163 msg : format ( strings . errors . STORAGE . FILE_NOT_FOUND , filePath )
@@ -70,7 +82,7 @@ module.exports = function (conf) {
7082 return callback ( null , cached ) ;
7183 }
7284
73- getFromAzure ( ( err , result ) => {
85+ getFromAzure ( ( err : Error | null , result : any ) => {
7486 if ( err ) {
7587 return callback ( err ) ;
7688 }
@@ -80,13 +92,13 @@ module.exports = function (conf) {
8092 } ) ;
8193 } ;
8294
83- const getJson = ( filePath , force , callback ) => {
95+ const getJson = ( filePath : string , force : boolean , callback : any ) => {
8496 if ( _ . isFunction ( force ) ) {
8597 callback = force ;
8698 force = false ;
8799 }
88100
89- getFile ( filePath , force , ( err , file ) => {
101+ getFile ( filePath , force , ( err : Error | null , file : string ) => {
90102 if ( err ) {
91103 return callback ( err ) ;
92104 }
@@ -104,21 +116,21 @@ module.exports = function (conf) {
104116 } ) ;
105117 } ;
106118
107- const getUrl = ( componentName , version , fileName ) =>
119+ const getUrl = ( componentName : string , version : string , fileName : string ) =>
108120 `${ conf . path } ${ componentName } /${ version } /${ fileName } ` ;
109121
110- const listSubDirectories = ( dir , callback ) => {
122+ const listSubDirectories = ( dir : string , callback : any ) => {
111123 const normalisedPath =
112124 dir . lastIndexOf ( '/' ) === dir . length - 1 && dir . length > 0
113125 ? dir
114126 : `${ dir } /` ;
115127
116128 const listBlobsWithPrefix = (
117- azureClient ,
118- containerName ,
119- prefix ,
120- continuationToken ,
121- callback
129+ azureClient : azure . BlobService ,
130+ containerName : string ,
131+ prefix : string ,
132+ continuationToken : azure . common . ContinuationToken ,
133+ callback : any
122134 ) => {
123135 azureClient . listBlobsSegmentedWithPrefix (
124136 containerName ,
@@ -165,7 +177,7 @@ module.exports = function (conf) {
165177 containerName ,
166178 prefix ,
167179 result . continuationToken ,
168- ( err , entryNames ) => {
180+ ( err : Error | null , entryNames : string [ ] ) => {
169181 if ( err ) {
170182 return callback ( err ) ;
171183 }
@@ -182,15 +194,15 @@ module.exports = function (conf) {
182194 getClient ( ) ,
183195 conf . privateContainerName ,
184196 normalisedPath ,
185- null ,
186- ( err , res ) => {
197+ null as unknown as azure . common . ContinuationToken ,
198+ ( err : Error | null , res : string [ ] ) => {
187199 if ( err ) return callback ( err ) ;
188200 callback ( null , _ . uniq ( res ) ) ;
189201 }
190202 ) ;
191203 } ;
192204
193- const putDir = ( dirInput , dirOutput , callback ) => {
205+ const putDir = ( dirInput : string , dirOutput : string , callback : any ) => {
194206 nodeDir . paths ( dirInput , ( err , paths ) => {
195207 async . each (
196208 paths . files ,
@@ -213,12 +225,18 @@ module.exports = function (conf) {
213225 } ) ;
214226 } ;
215227
216- const putFileContent = ( fileContent , fileName , isPrivate , callback ) => {
228+ const putFileContent = (
229+ fileContent : string | fs . ReadStream ,
230+ fileName : string ,
231+ isPrivate : boolean ,
232+ callback : any
233+ ) => {
217234 try {
218235 const fileInfo = getFileInfo ( fileName ) ;
219- const contentSettings = {
220- cacheControl : 'public, max-age=31556926'
221- } ;
236+ const contentSettings : azure . BlobService . CreateBlockBlobRequestOptions [ 'contentSettings' ] =
237+ {
238+ cacheControl : 'public, max-age=31556926'
239+ } ;
222240 if ( fileInfo . mimeType ) {
223241 contentSettings . contentType = fileInfo . mimeType ;
224242 }
@@ -227,7 +245,11 @@ module.exports = function (conf) {
227245 contentSettings . contentEncoding = 'gzip' ;
228246 }
229247
230- const uploadToAzureContainer = ( rereadable , containerName , cb ) => {
248+ const uploadToAzureContainer = (
249+ rereadable : boolean ,
250+ containerName : string ,
251+ cb : any
252+ ) => {
231253 try {
232254 if ( fileContent instanceof stream . Stream ) {
233255 return fileContent . pipe (
@@ -239,6 +261,8 @@ module.exports = function (conf) {
239261 if ( rereadable ) {
240262 // I need a fresh read stream and this is the only thing I came up with
241263 // very ugly and has poor performance, but works
264+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
265+ // @ts -ignore
242266 fileContent = getClient ( ) . createReadStream (
243267 containerName ,
244268 fileName
@@ -267,7 +291,7 @@ module.exports = function (conf) {
267291 uploadToAzureContainer (
268292 makeReReadable ,
269293 conf . privateContainerName ,
270- ( err , result ) => {
294+ ( err : Error | null , result : any ) => {
271295 if ( err ) {
272296 return callback ( err ) ;
273297 }
@@ -288,7 +312,12 @@ module.exports = function (conf) {
288312 }
289313 } ;
290314
291- const putFile = ( filePath , fileName , isPrivate , callback ) => {
315+ const putFile = (
316+ filePath : string ,
317+ fileName : string ,
318+ isPrivate : boolean ,
319+ callback : any
320+ ) => {
292321 try {
293322 const stream = fs . createReadStream ( filePath ) ;
294323 putFileContent ( stream , fileName , isPrivate , callback ) ;
@@ -308,5 +337,7 @@ module.exports = function (conf) {
308337 putFileContent : fromCallback ( putFileContent ) ,
309338 adapterType : 'azure-blob-storage' ,
310339 isValid
311- } ;
312- } ;
340+ } as any ;
341+ }
342+
343+ module . exports = azureAdapter ;
0 commit comments