npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@eik/sink-gcs

v2.0.0

Published

Sink for Google Cloud Storage

Downloads

424

Readme

@eik/sink-gcs

A sink for writing to and reading from Google Cloud Storage.

The intention of the Eik sink modules is to be able to write to and read from files in different storage backends by swapping sink modules. Because each sink implements the same public API it is possible to use this sink in one environment and another sink in a different environment.

Installation

$ npm install @eik/sink-gcs

Example

Read a file from Google Cloud Storage and serve it on HTTP:

import { pipeline } from 'node:stream';
import express from 'express';
import Sink from '@eik/sink-gcs';

const app = express();
const sink = new Sink(
    {
        credentials: {
            client_email: '[email protected]',
            private_key: '[ ...snip... ]',
            projectId: 'myProject',
        },
    },
    {
        writeTimeout: 20000,
    },
);

app.get('/file.js', async (req, res, next) => {
    try {
        const file = await sink.read('/path/to/file/file.js');
        pipeline(file.stream, res, (error) => {
            if (error) return next(error);
        });
    } catch (error) {
        next(error);
    }
});

app.listen(8000);

Constructor

Create a new Sink instance.

import Sink from '@eik/sink-gcs';

const sink = new Sink({
    credentials: {
        client_email: '[email protected]',
        private_key: '[ ...snip... ]',
    },
    projectId: 'myProject',
}, {
    ...options...
});

This constructor takes the following arguments:

  • storageOptions - Object - A Google Cloud Storage storage options object - Required.
  • options - Object - Other options related to storage and behavior - Optional.
    • writeTimeout - Number - Timeout, in milliseconds, for write operations to the sink - Default: 60000 - Optional.
    • writeGzip - Boolean - If files should be written with gzip compression - Default: false - Optional.
    • rootPath - String - Root directory for where to store files in the GCS bucket - Default: eik - Optional.
    • bucket - String - Name of the bucket to store files in - Default: eik_files - Optional.

API

The sink instance has the following API:

.write(filePath, contentType)

Async method for writing a file to storage.

This method takes the following arguments:

  • filePath - String - Path to the file to be stored - Required.
  • contentType - String - The content type of the file - Required.

Resolves with a writable stream.

const { pipeline } = require('stream);

const fromStream = new SomeReadableStream();
const sink = new Sink({ ... });

try {
    const file = await sink.write('/path/to/file/file.js', 'application/javascript');
    pipeline(fromStream, file.stream, (error) => {
        if (error) console.log(error);
    });
} catch (error) {
    console.log(error);
}

.read(filePath)

Async method for reading a file from storage.

This method takes the following arguments:

  • filePath - String - Path to the file to be read - Required.

Resolves with a ReadFile object which holds metadata about the file and a readable stream with the byte stream of the file on the .stream property.

import { pipeline } from 'node:stream';

const toStream = new SomeWritableStream();
const sink = new Sink({ ... });

try {
    const file = await sink.read('/path/to/file/file.js');
    pipeline(file.stream, toStream, (error) => {
        if (error) console.log(error);
    });
} catch (error) {
    console.log(error);
}

.delete(filePath)

Async method for deleting a file in storage.

This method takes the following arguments:

  • filePath - String - Path to the file to be deleted - Required.

Resolves if file is deleted and rejects if file could not be deleted.

const sink = new Sink({ ... });

try {
    await sink.delete('/path/to/file/file.js');
} catch (error) {
    console.log(error);
}

.exist(filePath)

Async method for checking if a file exist in the storage.

This method takes the following arguments:

  • filePath - String - Path to the file to be checked for existence - Required.

Resolves if file exists and rejects if file does not exist.

const sink = new Sink({ ... });

try {
    await sink.exist('/path/to/file/file.js');
} catch (error) {
    console.log(error);
}

Properties

The Sink instance has the following properties:

.metrics

Attribute which holds a metrics stream that emits metrics data.

The stream will emit an event of the following character for each metric:

{
    name: 'eik_core_sink_gcs',
    description: 'Counter measuring access to the Google Cloud Storage sink',
    timestamp: 1579468682.531,
    type: 2,
    value: 1,
    labels: [
        {
            name: 'operation',
            value: 'write'
        },
        {
            name: 'success',
            value: true
        },
        {
            name: 'access',
            value: true
        }
    ],
    time: null,
    meta: {}
}

The metric will have the following labels:

  • operation - String - The operation which triggered the metric. Can be write, read, delete or exist.
  • success - Boolean - If the operation was successfull in terms of being a valid operation and running the operation against the Google Cloud Storage without erroring.
  • access - Boolean - If the operation triggered access to the Google Cloud Storage.

Do note that the access label is true when the Sink runs an operation against the Google Cloud Storage which can generate a cost. In other words; this can be used to monitor excessive access to prevent cost.

License

Copyright (c) 2020 FINN.no

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.