This repository has been archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
License: MIT Signed-off-by: Alan Shaw <alan.shaw@protocol.ai>
- Loading branch information
Alan Shaw
committed
Dec 10, 2018
1 parent
71b3604
commit 8d9b167
Showing
6 changed files
with
59 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
'use-strict' | ||
|
||
const { URL } = require('url') | ||
const fetch = require('../../runtime/fetch-nodejs') | ||
|
||
module.exports = (self) => { | ||
return async (url, options, callback) => { | ||
if (typeof options === 'function') { | ||
callback = options | ||
options = {} | ||
} | ||
|
||
let files | ||
|
||
try { | ||
const parsedUrl = new URL(url) | ||
const res = await fetch(url) | ||
|
||
if (!res.ok) { | ||
throw new Error('unexpected status code: ' + res.status) | ||
} | ||
|
||
// TODO: use res.body when supported | ||
const content = Buffer.from(await res.arrayBuffer()) | ||
const path = decodeURIComponent(parsedUrl.pathname.split('/').pop()) | ||
|
||
files = await self.add({ content, path }, options) | ||
} catch (err) { | ||
if (callback) { | ||
return callback(err) | ||
} | ||
throw err | ||
} | ||
|
||
if (callback) { | ||
callback(null, files) | ||
} | ||
|
||
return files | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
/* eslint-env browser */ | ||
'use strict' | ||
module.exports = fetch |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
'use strict' | ||
module.exports = require('node-fetch') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters