Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ _tl;dr:_ You're free to use this code, make any changes you need, have fun with
- [x] ContainerTop
- [x] ContainerLogs
- [x] ContainerChanges
- [ ] ContainerExport
- [x] ContainerExport
- [x] ContainerStats
- [x] ContainerResize
- [x] ContainerStart
Expand All @@ -50,7 +50,6 @@ _tl;dr:_ You're free to use this code, make any changes you need, have fun with
- [x] ContainerPause
- [x] ContainerUnpause
- [x] ContainerAttach
- [ ] ContainerAttachWebsocket
- [x] ContainerWait
- [x] ContainerDelete
- [x] ContainerArchiveInfo
Expand All @@ -61,20 +60,20 @@ _tl;dr:_ You're free to use this code, make any changes you need, have fun with
### Image

- [x] ImageList
- [ ] ImageBuild
- [x] ImageBuild
- [x] BuildPrune
- [x] ImageCreate
- [x] ImageInspect
- [x] ImageHistory
- [ ] ImagePush
- [x] ImagePush
- [x] ImageTag
- [x] ImageDelete
- [ ] ImageSearch
- [x] ImagePrune
- [ ] ImageCommit
- [ ] ImageGet
- [ ] ImageGetAll
- [ ] ImageLoad
- [x] ImageCommit
- [x] ImageGet
- [x] ImageGetAll
- [x] ImageLoad

### Network

Expand Down Expand Up @@ -122,7 +121,6 @@ _tl;dr:_ You're free to use this code, make any changes you need, have fun with
- [x] SystemInfo
- [x] SystemVersion
- [x] SystemPing
- [ ] SystemPingHead
- [x] SystemEvents
- [x] SystemDataUsage

Expand Down
27 changes: 17 additions & 10 deletions lib/docker-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,8 @@ export class DockerClient {
) {
await this.api.sendHTTPRequest('GET', '/events', {
params: options,
callback: (data: Buffer) => {
data.toString('utf-8')
callback: (data: Buffer, encoding?: BufferEncoding) => {
data.toString(encoding)
.split('\n')
.filter((line) => line.trim() !== '')
.forEach((line) => {
Expand Down Expand Up @@ -489,11 +489,18 @@ export class DockerClient {
/**
* Export the contents of a container as a tarball.
* Export a container
* @param options
* @param options.id ID or name of the container
* @param id ID or name of the container
*/
public async containerExport(options?: { id: string }): Promise<void> {
// TODO
public async containerExport(
id: string,
w: stream.Writable,
): Promise<void> {
return this.api.get<void>(
`/containers/${id}/export`,
undefined,
'application/x-tar',
(data: any) => w.write(data),
);
}

/**
Expand Down Expand Up @@ -1099,8 +1106,8 @@ export class DockerClient {
},
buildContext,
headers,
(data: Buffer) => {
data.toString('utf-8')
(data: Buffer, encoding?: BufferEncoding) => {
data.toString(encoding)
.split('\n')
.filter((line) => line.trim() !== '')
.forEach((line) => {
Expand Down Expand Up @@ -1203,8 +1210,8 @@ export class DockerClient {
},
undefined,
headers,
(data: Buffer) => {
data.toString('utf-8')
(data: Buffer, encoding?: BufferEncoding) => {
data.toString(encoding)
.split('\n')
.filter((line) => line.trim() !== '')
.forEach((line) => {
Expand Down
10 changes: 6 additions & 4 deletions lib/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export class HTTPClient {
options?: {
params?: Record<string, any>;
data?: any;
callback?: (data: Buffer, encoding?: string) => void;
callback?: (data: Buffer, encoding?: BufferEncoding) => void;
accept?: string;
headers?: Record<string, string>;
},
Expand Down Expand Up @@ -217,14 +217,16 @@ export class HTTPClient {
const contentType = res.headers['content-type'];
const { type: mimeType, charset } =
parseContentType(contentType);
var encoding = (charset || 'utf8') as BufferEncoding;

const isDockerStream =
mimeType === DOCKER_RAW_STREAM ||
mimeType === DOCKER_MULTIPLEXED_STREAM;

if (isDockerStream && callback) {
// For upgrade protocols, forward all data directly to callback
res.on('data', (data: Buffer) => {
callback(data, charset || 'utf8');
callback(data, encoding);
});

// Resolve immediately with upgrade response
Expand All @@ -238,14 +240,14 @@ export class HTTPClient {
callback
) {
res.on('data', (chunk: Buffer) => {
callback(chunk, charset);
callback(chunk, encoding);
});

res.on('end', () => handleResponseEnd());
} else {
// Collect response body for non-streaming responses
res.on('data', (chunk: Buffer) => {
responseBody += chunk.toString('utf8');
responseBody += chunk.toString(encoding);
});

res.on('end', () => handleResponseEnd(responseBody));
Expand Down
8 changes: 1 addition & 7 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,8 @@ try {
return value.Id;
});

const fileinfo = await docker.containerArchiveInfo(ctr, '/etc/resolv.conf');
console.dir(fileinfo, { depth: null });

const out = fs.createWriteStream('/tmp/test.tar');
await docker.containerArchive(ctr, '/etc/resolv.conf', out);

const input = fs.createReadStream('/tmp/test.tar');
await docker.putContainerArchive(ctr, '/etc', input);
await docker.containerExport(ctr, out);

docker.close();
} catch (error) {
Expand Down