Skip to content
This repository has been archived by the owner on Jan 22, 2025. It is now read-only.

Commit

Permalink
Convert to ES module
Browse files Browse the repository at this point in the history
  • Loading branch information
jeroen committed Sep 28, 2024
1 parent 5219e7b commit 9f16c29
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 25 deletions.
13 changes: 6 additions & 7 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
const express = require('express');
const path = require('path');
const logger = require('morgan');
const cors = require('cors');
const createError = require('http-errors');
const cdnRouter = require('./routes/cdn');
import express from 'express';
import logger from 'morgan';
import cors from 'cors';
import createError from 'http-errors';
import cdnRouter from './routes/cdn.js';
const app = express();

app.use(cors())
Expand All @@ -23,4 +22,4 @@ app.use(function(err, req, res, next) {
res.type("text/plain").send(err.message || "CDN lookup error");
});

module.exports = app;
export default app;
8 changes: 2 additions & 6 deletions bin/www
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
#!/usr/bin/env node

/**
* Module dependencies.
*/

var app = require('../app');
var http = require('http');
import app from '../app.js';
import http from 'node:http';

/**
* Get port from environment and store in Express.
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "cdn-app",
"version": "0.0.1",
"type": "module",
"private": true,
"scripts": {
"start": "node ./bin/www"
Expand Down
21 changes: 9 additions & 12 deletions routes/cdn.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
const mongodb = require('mongodb');
const express = require('express');
const gunzip = require('gunzip-maybe');
const tar = require('tar-stream');
import mongodb from 'mongodb';
import express from 'express';
import gunzip from 'gunzip-maybe';
import tar from 'tar-stream';
import createError from 'http-errors';

const router = express.Router();
const createError = require('http-errors');
const HOST = process.env.CRANLIKE_MONGODB_SERVER || '127.0.0.1';
const PORT = process.env.CRANLIKE_MONGODB_PORT || 27017;
const USER = process.env.CRANLIKE_MONGODB_USERNAME || 'root';
const PASS = process.env.CRANLIKE_MONGODB_PASSWORD;
const AUTH = PASS ? (USER + ':' + PASS + "@") : "";
const URL = 'mongodb://' + AUTH + HOST + ':' + PORT;
const connection = mongodb.MongoClient.connect(URL);
var bucket;
let bucket;

function tar_index_files(input){
let files = [];
Expand Down Expand Up @@ -64,11 +65,7 @@ connection.then(function(client) {
bucket = new mongodb.GridFSBucket(db, {bucketName: 'files'});
});

stream_file = function(x){
return bucket.openDownloadStream(x['_id']);
}

send_from_bucket = function(hash, file, res){
function send_from_bucket(hash, file, res){
return bucket.find({_id: hash}, {limit:1}).next().then(function(pkg){
if(!pkg){
return res.status(410).type("text/plain").send(`File ${hash} not available (anymore)`);
Expand Down Expand Up @@ -124,4 +121,4 @@ router.get("/", function(req, res, next) {
next(createError(400, "Invalid CDN req: " + req.url));
});

module.exports = router;
export default router;

0 comments on commit 9f16c29

Please sign in to comment.